pl111_display.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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_gem_cma_helper.h>
  23. #include <drm/drm_gem_framebuffer_helper.h>
  24. #include <drm/drm_fb_cma_helper.h>
  25. #include "pl111_drm.h"
  26. irqreturn_t pl111_irq(int irq, void *data)
  27. {
  28. struct pl111_drm_dev_private *priv = data;
  29. u32 irq_stat;
  30. irqreturn_t status = IRQ_NONE;
  31. irq_stat = readl(priv->regs + CLCD_PL111_MIS);
  32. if (!irq_stat)
  33. return IRQ_NONE;
  34. if (irq_stat & CLCD_IRQ_NEXTBASE_UPDATE) {
  35. drm_crtc_handle_vblank(&priv->pipe.crtc);
  36. status = IRQ_HANDLED;
  37. }
  38. /* Clear the interrupt once done */
  39. writel(irq_stat, priv->regs + CLCD_PL111_ICR);
  40. return status;
  41. }
  42. static int pl111_display_check(struct drm_simple_display_pipe *pipe,
  43. struct drm_plane_state *pstate,
  44. struct drm_crtc_state *cstate)
  45. {
  46. const struct drm_display_mode *mode = &cstate->mode;
  47. struct drm_framebuffer *old_fb = pipe->plane.state->fb;
  48. struct drm_framebuffer *fb = pstate->fb;
  49. if (mode->hdisplay % 16)
  50. return -EINVAL;
  51. if (fb) {
  52. u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  53. /* FB base address must be dword aligned. */
  54. if (offset & 3)
  55. return -EINVAL;
  56. /* There's no pitch register -- the mode's hdisplay
  57. * controls it.
  58. */
  59. if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0])
  60. return -EINVAL;
  61. /* We can't change the FB format in a flicker-free
  62. * manner (and only update it during CRTC enable).
  63. */
  64. if (old_fb && old_fb->format != fb->format)
  65. cstate->mode_changed = true;
  66. }
  67. return 0;
  68. }
  69. static void pl111_display_enable(struct drm_simple_display_pipe *pipe,
  70. struct drm_crtc_state *cstate)
  71. {
  72. struct drm_crtc *crtc = &pipe->crtc;
  73. struct drm_plane *plane = &pipe->plane;
  74. struct drm_device *drm = crtc->dev;
  75. struct pl111_drm_dev_private *priv = drm->dev_private;
  76. const struct drm_display_mode *mode = &cstate->mode;
  77. struct drm_framebuffer *fb = plane->state->fb;
  78. struct drm_connector *connector = priv->connector;
  79. struct drm_bridge *bridge = priv->bridge;
  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) {
  119. if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
  120. tim2 |= TIM2_IOE;
  121. if (connector->display_info.bus_flags &
  122. DRM_BUS_FLAG_PIXDATA_NEGEDGE)
  123. tim2 |= TIM2_IPC;
  124. }
  125. if (bridge) {
  126. const struct drm_bridge_timings *btimings = bridge->timings;
  127. /*
  128. * Here is when things get really fun. Sometimes the bridge
  129. * timings are such that the signal out from PL11x is not
  130. * stable before the receiving bridge (such as a dumb VGA DAC
  131. * or similar) samples it. If that happens, we compensate by
  132. * the only method we have: output the data on the opposite
  133. * edge of the clock so it is for sure stable when it gets
  134. * sampled.
  135. *
  136. * The PL111 manual does not contain proper timining diagrams
  137. * or data for these details, but we know from experiments
  138. * that the setup time is more than 3000 picoseconds (3 ns).
  139. * If we have a bridge that requires the signal to be stable
  140. * earlier than 3000 ps before the clock pulse, we have to
  141. * output the data on the opposite edge to avoid flicker.
  142. */
  143. if (btimings && btimings->setup_time_ps >= 3000)
  144. tim2 ^= TIM2_IPC;
  145. }
  146. tim2 |= cpl << 16;
  147. writel(tim2, priv->regs + CLCD_TIM2);
  148. spin_unlock(&priv->tim2_lock);
  149. writel(0, priv->regs + CLCD_TIM3);
  150. /* Hard-code TFT panel */
  151. cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDVCOMP(1);
  152. /* Note that the the hardware's format reader takes 'r' from
  153. * the low bit, while DRM formats list channels from high bit
  154. * to low bit as you read left to right.
  155. */
  156. switch (fb->format->format) {
  157. case DRM_FORMAT_ABGR8888:
  158. case DRM_FORMAT_XBGR8888:
  159. cntl |= CNTL_LCDBPP24;
  160. break;
  161. case DRM_FORMAT_ARGB8888:
  162. case DRM_FORMAT_XRGB8888:
  163. cntl |= CNTL_LCDBPP24 | CNTL_BGR;
  164. break;
  165. case DRM_FORMAT_BGR565:
  166. cntl |= CNTL_LCDBPP16_565;
  167. break;
  168. case DRM_FORMAT_RGB565:
  169. cntl |= CNTL_LCDBPP16_565 | CNTL_BGR;
  170. break;
  171. case DRM_FORMAT_ABGR1555:
  172. case DRM_FORMAT_XBGR1555:
  173. cntl |= CNTL_LCDBPP16;
  174. break;
  175. case DRM_FORMAT_ARGB1555:
  176. case DRM_FORMAT_XRGB1555:
  177. cntl |= CNTL_LCDBPP16 | CNTL_BGR;
  178. break;
  179. case DRM_FORMAT_ABGR4444:
  180. case DRM_FORMAT_XBGR4444:
  181. cntl |= CNTL_LCDBPP16_444;
  182. break;
  183. case DRM_FORMAT_ARGB4444:
  184. case DRM_FORMAT_XRGB4444:
  185. cntl |= CNTL_LCDBPP16_444 | CNTL_BGR;
  186. break;
  187. default:
  188. WARN_ONCE(true, "Unknown FB format 0x%08x\n",
  189. fb->format->format);
  190. break;
  191. }
  192. /* Power sequence: first enable and chill */
  193. writel(cntl, priv->regs + priv->ctrl);
  194. /*
  195. * We expect this delay to stabilize the contrast
  196. * voltage Vee as stipulated by the manual
  197. */
  198. msleep(20);
  199. if (priv->variant_display_enable)
  200. priv->variant_display_enable(drm, fb->format->format);
  201. /* Power Up */
  202. cntl |= CNTL_LCDPWR;
  203. writel(cntl, priv->regs + priv->ctrl);
  204. drm_crtc_vblank_on(crtc);
  205. }
  206. void pl111_display_disable(struct drm_simple_display_pipe *pipe)
  207. {
  208. struct drm_crtc *crtc = &pipe->crtc;
  209. struct drm_device *drm = crtc->dev;
  210. struct pl111_drm_dev_private *priv = drm->dev_private;
  211. u32 cntl;
  212. drm_crtc_vblank_off(crtc);
  213. /* Power Down */
  214. cntl = readl(priv->regs + priv->ctrl);
  215. if (cntl & CNTL_LCDPWR) {
  216. cntl &= ~CNTL_LCDPWR;
  217. writel(cntl, priv->regs + priv->ctrl);
  218. }
  219. /*
  220. * We expect this delay to stabilize the contrast voltage Vee as
  221. * stipulated by the manual
  222. */
  223. msleep(20);
  224. if (priv->variant_display_disable)
  225. priv->variant_display_disable(drm);
  226. /* Disable */
  227. writel(0, priv->regs + priv->ctrl);
  228. clk_disable_unprepare(priv->clk);
  229. }
  230. static void pl111_display_update(struct drm_simple_display_pipe *pipe,
  231. struct drm_plane_state *old_pstate)
  232. {
  233. struct drm_crtc *crtc = &pipe->crtc;
  234. struct drm_device *drm = crtc->dev;
  235. struct pl111_drm_dev_private *priv = drm->dev_private;
  236. struct drm_pending_vblank_event *event = crtc->state->event;
  237. struct drm_plane *plane = &pipe->plane;
  238. struct drm_plane_state *pstate = plane->state;
  239. struct drm_framebuffer *fb = pstate->fb;
  240. if (fb) {
  241. u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  242. writel(addr, priv->regs + CLCD_UBAS);
  243. }
  244. if (event) {
  245. crtc->state->event = NULL;
  246. spin_lock_irq(&crtc->dev->event_lock);
  247. if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
  248. drm_crtc_arm_vblank_event(crtc, event);
  249. else
  250. drm_crtc_send_vblank_event(crtc, event);
  251. spin_unlock_irq(&crtc->dev->event_lock);
  252. }
  253. }
  254. int pl111_enable_vblank(struct drm_device *drm, unsigned int crtc)
  255. {
  256. struct pl111_drm_dev_private *priv = drm->dev_private;
  257. writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + priv->ienb);
  258. return 0;
  259. }
  260. void pl111_disable_vblank(struct drm_device *drm, unsigned int crtc)
  261. {
  262. struct pl111_drm_dev_private *priv = drm->dev_private;
  263. writel(0, priv->regs + priv->ienb);
  264. }
  265. static int pl111_display_prepare_fb(struct drm_simple_display_pipe *pipe,
  266. struct drm_plane_state *plane_state)
  267. {
  268. return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
  269. }
  270. static const struct drm_simple_display_pipe_funcs pl111_display_funcs = {
  271. .check = pl111_display_check,
  272. .enable = pl111_display_enable,
  273. .disable = pl111_display_disable,
  274. .update = pl111_display_update,
  275. .prepare_fb = pl111_display_prepare_fb,
  276. };
  277. static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
  278. unsigned long *prate, bool set_parent)
  279. {
  280. int best_div = 1, div;
  281. struct clk_hw *parent = clk_hw_get_parent(hw);
  282. unsigned long best_prate = 0;
  283. unsigned long best_diff = ~0ul;
  284. int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
  285. for (div = 1; div < max_div; div++) {
  286. unsigned long this_prate, div_rate, diff;
  287. if (set_parent)
  288. this_prate = clk_hw_round_rate(parent, rate * div);
  289. else
  290. this_prate = *prate;
  291. div_rate = DIV_ROUND_UP_ULL(this_prate, div);
  292. diff = abs(rate - div_rate);
  293. if (diff < best_diff) {
  294. best_div = div;
  295. best_diff = diff;
  296. best_prate = this_prate;
  297. }
  298. }
  299. *prate = best_prate;
  300. return best_div;
  301. }
  302. static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
  303. unsigned long *prate)
  304. {
  305. int div = pl111_clk_div_choose_div(hw, rate, prate, true);
  306. return DIV_ROUND_UP_ULL(*prate, div);
  307. }
  308. static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
  309. unsigned long prate)
  310. {
  311. struct pl111_drm_dev_private *priv =
  312. container_of(hw, struct pl111_drm_dev_private, clk_div);
  313. u32 tim2 = readl(priv->regs + CLCD_TIM2);
  314. int div;
  315. if (tim2 & TIM2_BCD)
  316. return prate;
  317. div = tim2 & TIM2_PCD_LO_MASK;
  318. div |= (tim2 & TIM2_PCD_HI_MASK) >>
  319. (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
  320. div += 2;
  321. return DIV_ROUND_UP_ULL(prate, div);
  322. }
  323. static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
  324. unsigned long prate)
  325. {
  326. struct pl111_drm_dev_private *priv =
  327. container_of(hw, struct pl111_drm_dev_private, clk_div);
  328. int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
  329. u32 tim2;
  330. spin_lock(&priv->tim2_lock);
  331. tim2 = readl(priv->regs + CLCD_TIM2);
  332. tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
  333. if (div == 1) {
  334. tim2 |= TIM2_BCD;
  335. } else {
  336. div -= 2;
  337. tim2 |= div & TIM2_PCD_LO_MASK;
  338. tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
  339. }
  340. writel(tim2, priv->regs + CLCD_TIM2);
  341. spin_unlock(&priv->tim2_lock);
  342. return 0;
  343. }
  344. static const struct clk_ops pl111_clk_div_ops = {
  345. .recalc_rate = pl111_clk_div_recalc_rate,
  346. .round_rate = pl111_clk_div_round_rate,
  347. .set_rate = pl111_clk_div_set_rate,
  348. };
  349. static int
  350. pl111_init_clock_divider(struct drm_device *drm)
  351. {
  352. struct pl111_drm_dev_private *priv = drm->dev_private;
  353. struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
  354. struct clk_hw *div = &priv->clk_div;
  355. const char *parent_name;
  356. struct clk_init_data init = {
  357. .name = "pl111_div",
  358. .ops = &pl111_clk_div_ops,
  359. .parent_names = &parent_name,
  360. .num_parents = 1,
  361. .flags = CLK_SET_RATE_PARENT,
  362. };
  363. int ret;
  364. if (IS_ERR(parent)) {
  365. dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
  366. return PTR_ERR(parent);
  367. }
  368. parent_name = __clk_get_name(parent);
  369. spin_lock_init(&priv->tim2_lock);
  370. div->init = &init;
  371. ret = devm_clk_hw_register(drm->dev, div);
  372. priv->clk = div->clk;
  373. return ret;
  374. }
  375. int pl111_display_init(struct drm_device *drm)
  376. {
  377. struct pl111_drm_dev_private *priv = drm->dev_private;
  378. struct device *dev = drm->dev;
  379. struct device_node *endpoint;
  380. u32 tft_r0b0g0[3];
  381. int ret;
  382. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  383. if (!endpoint)
  384. return -ENODEV;
  385. if (of_property_read_u32_array(endpoint,
  386. "arm,pl11x,tft-r0g0b0-pads",
  387. tft_r0b0g0,
  388. ARRAY_SIZE(tft_r0b0g0)) != 0) {
  389. dev_err(dev, "arm,pl11x,tft-r0g0b0-pads should be 3 ints\n");
  390. of_node_put(endpoint);
  391. return -ENOENT;
  392. }
  393. of_node_put(endpoint);
  394. ret = pl111_init_clock_divider(drm);
  395. if (ret)
  396. return ret;
  397. ret = drm_simple_display_pipe_init(drm, &priv->pipe,
  398. &pl111_display_funcs,
  399. priv->variant->formats,
  400. priv->variant->nformats,
  401. NULL,
  402. priv->connector);
  403. if (ret)
  404. return ret;
  405. return 0;
  406. }