pl111_display.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved.
  3. *
  4. * Parts of this file were based on sources as follows:
  5. *
  6. * Copyright (c) 2006-2008 Intel Corporation
  7. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  8. * Copyright (C) 2011 Texas Instruments
  9. *
  10. * This program is free software and is provided to you under the terms of the
  11. * GNU General Public License version 2 as published by the Free Software
  12. * Foundation, and any use by you of this program is subject to the terms of
  13. * such GNU licence.
  14. *
  15. */
  16. #include <linux/amba/clcd-regs.h>
  17. #include <linux/clk.h>
  18. #include <linux/version.h>
  19. #include <linux/dma-buf.h>
  20. #include <linux/of_graph.h>
  21. #include <drm/drmP.h>
  22. #include <drm/drm_panel.h>
  23. #include <drm/drm_gem_cma_helper.h>
  24. #include <drm/drm_gem_framebuffer_helper.h>
  25. #include <drm/drm_fb_cma_helper.h>
  26. #include "pl111_drm.h"
  27. irqreturn_t pl111_irq(int irq, void *data)
  28. {
  29. struct pl111_drm_dev_private *priv = data;
  30. u32 irq_stat;
  31. irqreturn_t status = IRQ_NONE;
  32. irq_stat = readl(priv->regs + CLCD_PL111_MIS);
  33. if (!irq_stat)
  34. return IRQ_NONE;
  35. if (irq_stat & CLCD_IRQ_NEXTBASE_UPDATE) {
  36. drm_crtc_handle_vblank(&priv->pipe.crtc);
  37. status = IRQ_HANDLED;
  38. }
  39. /* Clear the interrupt once done */
  40. writel(irq_stat, priv->regs + CLCD_PL111_ICR);
  41. return status;
  42. }
  43. static int pl111_display_check(struct drm_simple_display_pipe *pipe,
  44. struct drm_plane_state *pstate,
  45. struct drm_crtc_state *cstate)
  46. {
  47. const struct drm_display_mode *mode = &cstate->mode;
  48. struct drm_framebuffer *old_fb = pipe->plane.state->fb;
  49. struct drm_framebuffer *fb = pstate->fb;
  50. if (mode->hdisplay % 16)
  51. return -EINVAL;
  52. if (fb) {
  53. u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  54. /* FB base address must be dword aligned. */
  55. if (offset & 3)
  56. return -EINVAL;
  57. /* There's no pitch register -- the mode's hdisplay
  58. * controls it.
  59. */
  60. if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0])
  61. return -EINVAL;
  62. /* We can't change the FB format in a flicker-free
  63. * manner (and only update it during CRTC enable).
  64. */
  65. if (old_fb && old_fb->format != fb->format)
  66. cstate->mode_changed = true;
  67. }
  68. return 0;
  69. }
  70. static void pl111_display_enable(struct drm_simple_display_pipe *pipe,
  71. struct drm_crtc_state *cstate)
  72. {
  73. struct drm_crtc *crtc = &pipe->crtc;
  74. struct drm_plane *plane = &pipe->plane;
  75. struct drm_device *drm = crtc->dev;
  76. struct pl111_drm_dev_private *priv = drm->dev_private;
  77. const struct drm_display_mode *mode = &cstate->mode;
  78. struct drm_framebuffer *fb = plane->state->fb;
  79. struct drm_connector *connector = &priv->connector.connector;
  80. u32 cntl;
  81. u32 ppl, hsw, hfp, hbp;
  82. u32 lpp, vsw, vfp, vbp;
  83. u32 cpl, tim2;
  84. int ret;
  85. ret = clk_set_rate(priv->clk, mode->clock * 1000);
  86. if (ret) {
  87. dev_err(drm->dev,
  88. "Failed to set pixel clock rate to %d: %d\n",
  89. mode->clock * 1000, ret);
  90. }
  91. clk_prepare_enable(priv->clk);
  92. ppl = (mode->hdisplay / 16) - 1;
  93. hsw = mode->hsync_end - mode->hsync_start - 1;
  94. hfp = mode->hsync_start - mode->hdisplay - 1;
  95. hbp = mode->htotal - mode->hsync_end - 1;
  96. lpp = mode->vdisplay - 1;
  97. vsw = mode->vsync_end - mode->vsync_start - 1;
  98. vfp = mode->vsync_start - mode->vdisplay;
  99. vbp = mode->vtotal - mode->vsync_end;
  100. cpl = mode->hdisplay - 1;
  101. writel((ppl << 2) |
  102. (hsw << 8) |
  103. (hfp << 16) |
  104. (hbp << 24),
  105. priv->regs + CLCD_TIM0);
  106. writel(lpp |
  107. (vsw << 10) |
  108. (vfp << 16) |
  109. (vbp << 24),
  110. priv->regs + CLCD_TIM1);
  111. spin_lock(&priv->tim2_lock);
  112. tim2 = readl(priv->regs + CLCD_TIM2);
  113. tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
  114. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  115. tim2 |= TIM2_IHS;
  116. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  117. tim2 |= TIM2_IVS;
  118. if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
  119. tim2 |= TIM2_IOE;
  120. if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
  121. tim2 |= TIM2_IPC;
  122. tim2 |= cpl << 16;
  123. writel(tim2, priv->regs + CLCD_TIM2);
  124. spin_unlock(&priv->tim2_lock);
  125. writel(0, priv->regs + CLCD_TIM3);
  126. drm_panel_prepare(priv->connector.panel);
  127. /* Enable and Power Up */
  128. cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDPWR | CNTL_LCDVCOMP(1);
  129. /* Note that the the hardware's format reader takes 'r' from
  130. * the low bit, while DRM formats list channels from high bit
  131. * to low bit as you read left to right.
  132. */
  133. switch (fb->format->format) {
  134. case DRM_FORMAT_ABGR8888:
  135. case DRM_FORMAT_XBGR8888:
  136. cntl |= CNTL_LCDBPP24;
  137. break;
  138. case DRM_FORMAT_ARGB8888:
  139. case DRM_FORMAT_XRGB8888:
  140. cntl |= CNTL_LCDBPP24 | CNTL_BGR;
  141. break;
  142. case DRM_FORMAT_BGR565:
  143. cntl |= CNTL_LCDBPP16_565;
  144. break;
  145. case DRM_FORMAT_RGB565:
  146. cntl |= CNTL_LCDBPP16_565 | CNTL_BGR;
  147. break;
  148. case DRM_FORMAT_ABGR1555:
  149. case DRM_FORMAT_XBGR1555:
  150. cntl |= CNTL_LCDBPP16;
  151. break;
  152. case DRM_FORMAT_ARGB1555:
  153. case DRM_FORMAT_XRGB1555:
  154. cntl |= CNTL_LCDBPP16 | CNTL_BGR;
  155. break;
  156. case DRM_FORMAT_ABGR4444:
  157. case DRM_FORMAT_XBGR4444:
  158. cntl |= CNTL_LCDBPP16_444;
  159. break;
  160. case DRM_FORMAT_ARGB4444:
  161. case DRM_FORMAT_XRGB4444:
  162. cntl |= CNTL_LCDBPP16_444 | CNTL_BGR;
  163. break;
  164. default:
  165. WARN_ONCE(true, "Unknown FB format 0x%08x\n",
  166. fb->format->format);
  167. break;
  168. }
  169. writel(cntl, priv->regs + CLCD_PL111_CNTL);
  170. drm_panel_enable(priv->connector.panel);
  171. drm_crtc_vblank_on(crtc);
  172. }
  173. void pl111_display_disable(struct drm_simple_display_pipe *pipe)
  174. {
  175. struct drm_crtc *crtc = &pipe->crtc;
  176. struct drm_device *drm = crtc->dev;
  177. struct pl111_drm_dev_private *priv = drm->dev_private;
  178. drm_crtc_vblank_off(crtc);
  179. drm_panel_disable(priv->connector.panel);
  180. /* Disable and Power Down */
  181. writel(0, priv->regs + CLCD_PL111_CNTL);
  182. drm_panel_unprepare(priv->connector.panel);
  183. clk_disable_unprepare(priv->clk);
  184. }
  185. static void pl111_display_update(struct drm_simple_display_pipe *pipe,
  186. struct drm_plane_state *old_pstate)
  187. {
  188. struct drm_crtc *crtc = &pipe->crtc;
  189. struct drm_device *drm = crtc->dev;
  190. struct pl111_drm_dev_private *priv = drm->dev_private;
  191. struct drm_pending_vblank_event *event = crtc->state->event;
  192. struct drm_plane *plane = &pipe->plane;
  193. struct drm_plane_state *pstate = plane->state;
  194. struct drm_framebuffer *fb = pstate->fb;
  195. if (fb) {
  196. u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  197. writel(addr, priv->regs + CLCD_UBAS);
  198. }
  199. if (event) {
  200. crtc->state->event = NULL;
  201. spin_lock_irq(&crtc->dev->event_lock);
  202. if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
  203. drm_crtc_arm_vblank_event(crtc, event);
  204. else
  205. drm_crtc_send_vblank_event(crtc, event);
  206. spin_unlock_irq(&crtc->dev->event_lock);
  207. }
  208. }
  209. int pl111_enable_vblank(struct drm_device *drm, unsigned int crtc)
  210. {
  211. struct pl111_drm_dev_private *priv = drm->dev_private;
  212. writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + CLCD_PL111_IENB);
  213. return 0;
  214. }
  215. void pl111_disable_vblank(struct drm_device *drm, unsigned int crtc)
  216. {
  217. struct pl111_drm_dev_private *priv = drm->dev_private;
  218. writel(0, priv->regs + CLCD_PL111_IENB);
  219. }
  220. static int pl111_display_prepare_fb(struct drm_simple_display_pipe *pipe,
  221. struct drm_plane_state *plane_state)
  222. {
  223. return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
  224. }
  225. static const struct drm_simple_display_pipe_funcs pl111_display_funcs = {
  226. .check = pl111_display_check,
  227. .enable = pl111_display_enable,
  228. .disable = pl111_display_disable,
  229. .update = pl111_display_update,
  230. .prepare_fb = pl111_display_prepare_fb,
  231. };
  232. static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
  233. unsigned long *prate, bool set_parent)
  234. {
  235. int best_div = 1, div;
  236. struct clk_hw *parent = clk_hw_get_parent(hw);
  237. unsigned long best_prate = 0;
  238. unsigned long best_diff = ~0ul;
  239. int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
  240. for (div = 1; div < max_div; div++) {
  241. unsigned long this_prate, div_rate, diff;
  242. if (set_parent)
  243. this_prate = clk_hw_round_rate(parent, rate * div);
  244. else
  245. this_prate = *prate;
  246. div_rate = DIV_ROUND_UP_ULL(this_prate, div);
  247. diff = abs(rate - div_rate);
  248. if (diff < best_diff) {
  249. best_div = div;
  250. best_diff = diff;
  251. best_prate = this_prate;
  252. }
  253. }
  254. *prate = best_prate;
  255. return best_div;
  256. }
  257. static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
  258. unsigned long *prate)
  259. {
  260. int div = pl111_clk_div_choose_div(hw, rate, prate, true);
  261. return DIV_ROUND_UP_ULL(*prate, div);
  262. }
  263. static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
  264. unsigned long prate)
  265. {
  266. struct pl111_drm_dev_private *priv =
  267. container_of(hw, struct pl111_drm_dev_private, clk_div);
  268. u32 tim2 = readl(priv->regs + CLCD_TIM2);
  269. int div;
  270. if (tim2 & TIM2_BCD)
  271. return prate;
  272. div = tim2 & TIM2_PCD_LO_MASK;
  273. div |= (tim2 & TIM2_PCD_HI_MASK) >>
  274. (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
  275. div += 2;
  276. return DIV_ROUND_UP_ULL(prate, div);
  277. }
  278. static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
  279. unsigned long prate)
  280. {
  281. struct pl111_drm_dev_private *priv =
  282. container_of(hw, struct pl111_drm_dev_private, clk_div);
  283. int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
  284. u32 tim2;
  285. spin_lock(&priv->tim2_lock);
  286. tim2 = readl(priv->regs + CLCD_TIM2);
  287. tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
  288. if (div == 1) {
  289. tim2 |= TIM2_BCD;
  290. } else {
  291. div -= 2;
  292. tim2 |= div & TIM2_PCD_LO_MASK;
  293. tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
  294. }
  295. writel(tim2, priv->regs + CLCD_TIM2);
  296. spin_unlock(&priv->tim2_lock);
  297. return 0;
  298. }
  299. static const struct clk_ops pl111_clk_div_ops = {
  300. .recalc_rate = pl111_clk_div_recalc_rate,
  301. .round_rate = pl111_clk_div_round_rate,
  302. .set_rate = pl111_clk_div_set_rate,
  303. };
  304. static int
  305. pl111_init_clock_divider(struct drm_device *drm)
  306. {
  307. struct pl111_drm_dev_private *priv = drm->dev_private;
  308. struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
  309. struct clk_hw *div = &priv->clk_div;
  310. const char *parent_name;
  311. struct clk_init_data init = {
  312. .name = "pl111_div",
  313. .ops = &pl111_clk_div_ops,
  314. .parent_names = &parent_name,
  315. .num_parents = 1,
  316. .flags = CLK_SET_RATE_PARENT,
  317. };
  318. int ret;
  319. if (IS_ERR(parent)) {
  320. dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
  321. return PTR_ERR(parent);
  322. }
  323. parent_name = __clk_get_name(parent);
  324. spin_lock_init(&priv->tim2_lock);
  325. div->init = &init;
  326. ret = devm_clk_hw_register(drm->dev, div);
  327. priv->clk = div->clk;
  328. return ret;
  329. }
  330. int pl111_display_init(struct drm_device *drm)
  331. {
  332. struct pl111_drm_dev_private *priv = drm->dev_private;
  333. struct device *dev = drm->dev;
  334. struct device_node *endpoint;
  335. u32 tft_r0b0g0[3];
  336. int ret;
  337. static const u32 formats[] = {
  338. DRM_FORMAT_ABGR8888,
  339. DRM_FORMAT_XBGR8888,
  340. DRM_FORMAT_ARGB8888,
  341. DRM_FORMAT_XRGB8888,
  342. DRM_FORMAT_BGR565,
  343. DRM_FORMAT_RGB565,
  344. DRM_FORMAT_ABGR1555,
  345. DRM_FORMAT_XBGR1555,
  346. DRM_FORMAT_ARGB1555,
  347. DRM_FORMAT_XRGB1555,
  348. DRM_FORMAT_ABGR4444,
  349. DRM_FORMAT_XBGR4444,
  350. DRM_FORMAT_ARGB4444,
  351. DRM_FORMAT_XRGB4444,
  352. };
  353. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  354. if (!endpoint)
  355. return -ENODEV;
  356. if (of_property_read_u32_array(endpoint,
  357. "arm,pl11x,tft-r0g0b0-pads",
  358. tft_r0b0g0,
  359. ARRAY_SIZE(tft_r0b0g0)) != 0) {
  360. dev_err(dev, "arm,pl11x,tft-r0g0b0-pads should be 3 ints\n");
  361. of_node_put(endpoint);
  362. return -ENOENT;
  363. }
  364. of_node_put(endpoint);
  365. if (tft_r0b0g0[0] != 0 ||
  366. tft_r0b0g0[1] != 8 ||
  367. tft_r0b0g0[2] != 16) {
  368. dev_err(dev, "arm,pl11x,tft-r0g0b0-pads != [0,8,16] not yet supported\n");
  369. return -EINVAL;
  370. }
  371. ret = pl111_init_clock_divider(drm);
  372. if (ret)
  373. return ret;
  374. ret = drm_simple_display_pipe_init(drm, &priv->pipe,
  375. &pl111_display_funcs,
  376. formats, ARRAY_SIZE(formats),
  377. NULL, &priv->connector.connector);
  378. if (ret)
  379. return ret;
  380. return 0;
  381. }