pl111_display.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 enum drm_mode_status
  43. pl111_mode_valid(struct drm_crtc *crtc,
  44. const struct drm_display_mode *mode)
  45. {
  46. struct drm_device *drm = crtc->dev;
  47. struct pl111_drm_dev_private *priv = drm->dev_private;
  48. u32 cpp = priv->variant->fb_bpp / 8;
  49. u64 bw;
  50. /*
  51. * We use the pixelclock to also account for interlaced modes, the
  52. * resulting bandwidth is in bytes per second.
  53. */
  54. bw = mode->clock * 1000ULL; /* In Hz */
  55. bw = bw * mode->hdisplay * mode->vdisplay * cpp;
  56. bw = div_u64(bw, mode->htotal * mode->vtotal);
  57. /*
  58. * If no bandwidth constraints, anything goes, else
  59. * check if we are too fast.
  60. */
  61. if (priv->memory_bw && (bw > priv->memory_bw)) {
  62. DRM_DEBUG_KMS("%d x %d @ %d Hz, %d cpp, bw %llu too fast\n",
  63. mode->hdisplay, mode->vdisplay,
  64. mode->clock * 1000, cpp, bw);
  65. return MODE_BAD;
  66. }
  67. DRM_DEBUG_KMS("%d x %d @ %d Hz, %d cpp, bw %llu bytes/s OK\n",
  68. mode->hdisplay, mode->vdisplay,
  69. mode->clock * 1000, cpp, bw);
  70. return MODE_OK;
  71. }
  72. static int pl111_display_check(struct drm_simple_display_pipe *pipe,
  73. struct drm_plane_state *pstate,
  74. struct drm_crtc_state *cstate)
  75. {
  76. const struct drm_display_mode *mode = &cstate->mode;
  77. struct drm_framebuffer *old_fb = pipe->plane.state->fb;
  78. struct drm_framebuffer *fb = pstate->fb;
  79. if (mode->hdisplay % 16)
  80. return -EINVAL;
  81. if (fb) {
  82. u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  83. /* FB base address must be dword aligned. */
  84. if (offset & 3)
  85. return -EINVAL;
  86. /* There's no pitch register -- the mode's hdisplay
  87. * controls it.
  88. */
  89. if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0])
  90. return -EINVAL;
  91. /* We can't change the FB format in a flicker-free
  92. * manner (and only update it during CRTC enable).
  93. */
  94. if (old_fb && old_fb->format != fb->format)
  95. cstate->mode_changed = true;
  96. }
  97. return 0;
  98. }
  99. static void pl111_display_enable(struct drm_simple_display_pipe *pipe,
  100. struct drm_crtc_state *cstate,
  101. struct drm_plane_state *plane_state)
  102. {
  103. struct drm_crtc *crtc = &pipe->crtc;
  104. struct drm_plane *plane = &pipe->plane;
  105. struct drm_device *drm = crtc->dev;
  106. struct pl111_drm_dev_private *priv = drm->dev_private;
  107. const struct drm_display_mode *mode = &cstate->mode;
  108. struct drm_framebuffer *fb = plane->state->fb;
  109. struct drm_connector *connector = priv->connector;
  110. struct drm_bridge *bridge = priv->bridge;
  111. u32 cntl;
  112. u32 ppl, hsw, hfp, hbp;
  113. u32 lpp, vsw, vfp, vbp;
  114. u32 cpl, tim2;
  115. int ret;
  116. ret = clk_set_rate(priv->clk, mode->clock * 1000);
  117. if (ret) {
  118. dev_err(drm->dev,
  119. "Failed to set pixel clock rate to %d: %d\n",
  120. mode->clock * 1000, ret);
  121. }
  122. clk_prepare_enable(priv->clk);
  123. ppl = (mode->hdisplay / 16) - 1;
  124. hsw = mode->hsync_end - mode->hsync_start - 1;
  125. hfp = mode->hsync_start - mode->hdisplay - 1;
  126. hbp = mode->htotal - mode->hsync_end - 1;
  127. lpp = mode->vdisplay - 1;
  128. vsw = mode->vsync_end - mode->vsync_start - 1;
  129. vfp = mode->vsync_start - mode->vdisplay;
  130. vbp = mode->vtotal - mode->vsync_end;
  131. cpl = mode->hdisplay - 1;
  132. writel((ppl << 2) |
  133. (hsw << 8) |
  134. (hfp << 16) |
  135. (hbp << 24),
  136. priv->regs + CLCD_TIM0);
  137. writel(lpp |
  138. (vsw << 10) |
  139. (vfp << 16) |
  140. (vbp << 24),
  141. priv->regs + CLCD_TIM1);
  142. spin_lock(&priv->tim2_lock);
  143. tim2 = readl(priv->regs + CLCD_TIM2);
  144. tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
  145. if (priv->variant->broken_clockdivider)
  146. tim2 |= TIM2_BCD;
  147. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  148. tim2 |= TIM2_IHS;
  149. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  150. tim2 |= TIM2_IVS;
  151. if (connector) {
  152. if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
  153. tim2 |= TIM2_IOE;
  154. if (connector->display_info.bus_flags &
  155. DRM_BUS_FLAG_PIXDATA_NEGEDGE)
  156. tim2 |= TIM2_IPC;
  157. }
  158. if (bridge) {
  159. const struct drm_bridge_timings *btimings = bridge->timings;
  160. /*
  161. * Here is when things get really fun. Sometimes the bridge
  162. * timings are such that the signal out from PL11x is not
  163. * stable before the receiving bridge (such as a dumb VGA DAC
  164. * or similar) samples it. If that happens, we compensate by
  165. * the only method we have: output the data on the opposite
  166. * edge of the clock so it is for sure stable when it gets
  167. * sampled.
  168. *
  169. * The PL111 manual does not contain proper timining diagrams
  170. * or data for these details, but we know from experiments
  171. * that the setup time is more than 3000 picoseconds (3 ns).
  172. * If we have a bridge that requires the signal to be stable
  173. * earlier than 3000 ps before the clock pulse, we have to
  174. * output the data on the opposite edge to avoid flicker.
  175. */
  176. if (btimings && btimings->setup_time_ps >= 3000)
  177. tim2 ^= TIM2_IPC;
  178. }
  179. tim2 |= cpl << 16;
  180. writel(tim2, priv->regs + CLCD_TIM2);
  181. spin_unlock(&priv->tim2_lock);
  182. writel(0, priv->regs + CLCD_TIM3);
  183. /* Hard-code TFT panel */
  184. cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDVCOMP(1);
  185. /* On the ST Micro variant, assume all 24 bits are connected */
  186. if (priv->variant->st_bitmux_control)
  187. cntl |= CNTL_ST_CDWID_24;
  188. /*
  189. * Note that the the ARM hardware's format reader takes 'r' from
  190. * the low bit, while DRM formats list channels from high bit
  191. * to low bit as you read left to right. The ST Micro version of
  192. * the PL110 (LCDC) however uses the standard DRM format.
  193. */
  194. switch (fb->format->format) {
  195. case DRM_FORMAT_BGR888:
  196. /* Only supported on the ST Micro variant */
  197. if (priv->variant->st_bitmux_control)
  198. cntl |= CNTL_ST_LCDBPP24_PACKED | CNTL_BGR;
  199. break;
  200. case DRM_FORMAT_RGB888:
  201. /* Only supported on the ST Micro variant */
  202. if (priv->variant->st_bitmux_control)
  203. cntl |= CNTL_ST_LCDBPP24_PACKED;
  204. break;
  205. case DRM_FORMAT_ABGR8888:
  206. case DRM_FORMAT_XBGR8888:
  207. if (priv->variant->st_bitmux_control)
  208. cntl |= CNTL_LCDBPP24 | CNTL_BGR;
  209. else
  210. cntl |= CNTL_LCDBPP24;
  211. break;
  212. case DRM_FORMAT_ARGB8888:
  213. case DRM_FORMAT_XRGB8888:
  214. if (priv->variant->st_bitmux_control)
  215. cntl |= CNTL_LCDBPP24;
  216. else
  217. cntl |= CNTL_LCDBPP24 | CNTL_BGR;
  218. break;
  219. case DRM_FORMAT_BGR565:
  220. if (priv->variant->is_pl110)
  221. cntl |= CNTL_LCDBPP16;
  222. else if (priv->variant->st_bitmux_control)
  223. cntl |= CNTL_LCDBPP16 | CNTL_ST_1XBPP_565 | CNTL_BGR;
  224. else
  225. cntl |= CNTL_LCDBPP16_565;
  226. break;
  227. case DRM_FORMAT_RGB565:
  228. if (priv->variant->is_pl110)
  229. cntl |= CNTL_LCDBPP16 | CNTL_BGR;
  230. else if (priv->variant->st_bitmux_control)
  231. cntl |= CNTL_LCDBPP16 | CNTL_ST_1XBPP_565;
  232. else
  233. cntl |= CNTL_LCDBPP16_565 | CNTL_BGR;
  234. break;
  235. case DRM_FORMAT_ABGR1555:
  236. case DRM_FORMAT_XBGR1555:
  237. cntl |= CNTL_LCDBPP16;
  238. if (priv->variant->st_bitmux_control)
  239. cntl |= CNTL_ST_1XBPP_5551 | CNTL_BGR;
  240. break;
  241. case DRM_FORMAT_ARGB1555:
  242. case DRM_FORMAT_XRGB1555:
  243. cntl |= CNTL_LCDBPP16;
  244. if (priv->variant->st_bitmux_control)
  245. cntl |= CNTL_ST_1XBPP_5551;
  246. else
  247. cntl |= CNTL_BGR;
  248. break;
  249. case DRM_FORMAT_ABGR4444:
  250. case DRM_FORMAT_XBGR4444:
  251. cntl |= CNTL_LCDBPP16_444;
  252. if (priv->variant->st_bitmux_control)
  253. cntl |= CNTL_ST_1XBPP_444 | CNTL_BGR;
  254. break;
  255. case DRM_FORMAT_ARGB4444:
  256. case DRM_FORMAT_XRGB4444:
  257. cntl |= CNTL_LCDBPP16_444;
  258. if (priv->variant->st_bitmux_control)
  259. cntl |= CNTL_ST_1XBPP_444;
  260. else
  261. cntl |= CNTL_BGR;
  262. break;
  263. default:
  264. WARN_ONCE(true, "Unknown FB format 0x%08x\n",
  265. fb->format->format);
  266. break;
  267. }
  268. /* The PL110 in Integrator/Versatile does the BGR routing externally */
  269. if (priv->variant->external_bgr)
  270. cntl &= ~CNTL_BGR;
  271. /* Power sequence: first enable and chill */
  272. writel(cntl, priv->regs + priv->ctrl);
  273. /*
  274. * We expect this delay to stabilize the contrast
  275. * voltage Vee as stipulated by the manual
  276. */
  277. msleep(20);
  278. if (priv->variant_display_enable)
  279. priv->variant_display_enable(drm, fb->format->format);
  280. /* Power Up */
  281. cntl |= CNTL_LCDPWR;
  282. writel(cntl, priv->regs + priv->ctrl);
  283. if (!priv->variant->broken_vblank)
  284. drm_crtc_vblank_on(crtc);
  285. }
  286. void pl111_display_disable(struct drm_simple_display_pipe *pipe)
  287. {
  288. struct drm_crtc *crtc = &pipe->crtc;
  289. struct drm_device *drm = crtc->dev;
  290. struct pl111_drm_dev_private *priv = drm->dev_private;
  291. u32 cntl;
  292. if (!priv->variant->broken_vblank)
  293. drm_crtc_vblank_off(crtc);
  294. /* Power Down */
  295. cntl = readl(priv->regs + priv->ctrl);
  296. if (cntl & CNTL_LCDPWR) {
  297. cntl &= ~CNTL_LCDPWR;
  298. writel(cntl, priv->regs + priv->ctrl);
  299. }
  300. /*
  301. * We expect this delay to stabilize the contrast voltage Vee as
  302. * stipulated by the manual
  303. */
  304. msleep(20);
  305. if (priv->variant_display_disable)
  306. priv->variant_display_disable(drm);
  307. /* Disable */
  308. writel(0, priv->regs + priv->ctrl);
  309. clk_disable_unprepare(priv->clk);
  310. }
  311. static void pl111_display_update(struct drm_simple_display_pipe *pipe,
  312. struct drm_plane_state *old_pstate)
  313. {
  314. struct drm_crtc *crtc = &pipe->crtc;
  315. struct drm_device *drm = crtc->dev;
  316. struct pl111_drm_dev_private *priv = drm->dev_private;
  317. struct drm_pending_vblank_event *event = crtc->state->event;
  318. struct drm_plane *plane = &pipe->plane;
  319. struct drm_plane_state *pstate = plane->state;
  320. struct drm_framebuffer *fb = pstate->fb;
  321. if (fb) {
  322. u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  323. writel(addr, priv->regs + CLCD_UBAS);
  324. }
  325. if (event) {
  326. crtc->state->event = NULL;
  327. spin_lock_irq(&crtc->dev->event_lock);
  328. if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
  329. drm_crtc_arm_vblank_event(crtc, event);
  330. else
  331. drm_crtc_send_vblank_event(crtc, event);
  332. spin_unlock_irq(&crtc->dev->event_lock);
  333. }
  334. }
  335. static int pl111_display_enable_vblank(struct drm_simple_display_pipe *pipe)
  336. {
  337. struct drm_crtc *crtc = &pipe->crtc;
  338. struct drm_device *drm = crtc->dev;
  339. struct pl111_drm_dev_private *priv = drm->dev_private;
  340. writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + priv->ienb);
  341. return 0;
  342. }
  343. static void pl111_display_disable_vblank(struct drm_simple_display_pipe *pipe)
  344. {
  345. struct drm_crtc *crtc = &pipe->crtc;
  346. struct drm_device *drm = crtc->dev;
  347. struct pl111_drm_dev_private *priv = drm->dev_private;
  348. writel(0, priv->regs + priv->ienb);
  349. }
  350. static struct drm_simple_display_pipe_funcs pl111_display_funcs = {
  351. .mode_valid = pl111_mode_valid,
  352. .check = pl111_display_check,
  353. .enable = pl111_display_enable,
  354. .disable = pl111_display_disable,
  355. .update = pl111_display_update,
  356. .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
  357. };
  358. static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
  359. unsigned long *prate, bool set_parent)
  360. {
  361. int best_div = 1, div;
  362. struct clk_hw *parent = clk_hw_get_parent(hw);
  363. unsigned long best_prate = 0;
  364. unsigned long best_diff = ~0ul;
  365. int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
  366. for (div = 1; div < max_div; div++) {
  367. unsigned long this_prate, div_rate, diff;
  368. if (set_parent)
  369. this_prate = clk_hw_round_rate(parent, rate * div);
  370. else
  371. this_prate = *prate;
  372. div_rate = DIV_ROUND_UP_ULL(this_prate, div);
  373. diff = abs(rate - div_rate);
  374. if (diff < best_diff) {
  375. best_div = div;
  376. best_diff = diff;
  377. best_prate = this_prate;
  378. }
  379. }
  380. *prate = best_prate;
  381. return best_div;
  382. }
  383. static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
  384. unsigned long *prate)
  385. {
  386. int div = pl111_clk_div_choose_div(hw, rate, prate, true);
  387. return DIV_ROUND_UP_ULL(*prate, div);
  388. }
  389. static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
  390. unsigned long prate)
  391. {
  392. struct pl111_drm_dev_private *priv =
  393. container_of(hw, struct pl111_drm_dev_private, clk_div);
  394. u32 tim2 = readl(priv->regs + CLCD_TIM2);
  395. int div;
  396. if (tim2 & TIM2_BCD)
  397. return prate;
  398. div = tim2 & TIM2_PCD_LO_MASK;
  399. div |= (tim2 & TIM2_PCD_HI_MASK) >>
  400. (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
  401. div += 2;
  402. return DIV_ROUND_UP_ULL(prate, div);
  403. }
  404. static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
  405. unsigned long prate)
  406. {
  407. struct pl111_drm_dev_private *priv =
  408. container_of(hw, struct pl111_drm_dev_private, clk_div);
  409. int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
  410. u32 tim2;
  411. spin_lock(&priv->tim2_lock);
  412. tim2 = readl(priv->regs + CLCD_TIM2);
  413. tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
  414. if (div == 1) {
  415. tim2 |= TIM2_BCD;
  416. } else {
  417. div -= 2;
  418. tim2 |= div & TIM2_PCD_LO_MASK;
  419. tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
  420. }
  421. writel(tim2, priv->regs + CLCD_TIM2);
  422. spin_unlock(&priv->tim2_lock);
  423. return 0;
  424. }
  425. static const struct clk_ops pl111_clk_div_ops = {
  426. .recalc_rate = pl111_clk_div_recalc_rate,
  427. .round_rate = pl111_clk_div_round_rate,
  428. .set_rate = pl111_clk_div_set_rate,
  429. };
  430. static int
  431. pl111_init_clock_divider(struct drm_device *drm)
  432. {
  433. struct pl111_drm_dev_private *priv = drm->dev_private;
  434. struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
  435. struct clk_hw *div = &priv->clk_div;
  436. const char *parent_name;
  437. struct clk_init_data init = {
  438. .name = "pl111_div",
  439. .ops = &pl111_clk_div_ops,
  440. .parent_names = &parent_name,
  441. .num_parents = 1,
  442. .flags = CLK_SET_RATE_PARENT,
  443. };
  444. int ret;
  445. if (IS_ERR(parent)) {
  446. dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
  447. return PTR_ERR(parent);
  448. }
  449. spin_lock_init(&priv->tim2_lock);
  450. /* If the clock divider is broken, use the parent directly */
  451. if (priv->variant->broken_clockdivider) {
  452. priv->clk = parent;
  453. return 0;
  454. }
  455. parent_name = __clk_get_name(parent);
  456. div->init = &init;
  457. ret = devm_clk_hw_register(drm->dev, div);
  458. priv->clk = div->clk;
  459. return ret;
  460. }
  461. int pl111_display_init(struct drm_device *drm)
  462. {
  463. struct pl111_drm_dev_private *priv = drm->dev_private;
  464. struct device *dev = drm->dev;
  465. struct device_node *endpoint;
  466. u32 tft_r0b0g0[3];
  467. int ret;
  468. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  469. if (!endpoint)
  470. return -ENODEV;
  471. if (of_property_read_u32_array(endpoint,
  472. "arm,pl11x,tft-r0g0b0-pads",
  473. tft_r0b0g0,
  474. ARRAY_SIZE(tft_r0b0g0)) != 0) {
  475. dev_err(dev, "arm,pl11x,tft-r0g0b0-pads should be 3 ints\n");
  476. of_node_put(endpoint);
  477. return -ENOENT;
  478. }
  479. of_node_put(endpoint);
  480. ret = pl111_init_clock_divider(drm);
  481. if (ret)
  482. return ret;
  483. if (!priv->variant->broken_vblank) {
  484. pl111_display_funcs.enable_vblank = pl111_display_enable_vblank;
  485. pl111_display_funcs.disable_vblank = pl111_display_disable_vblank;
  486. }
  487. ret = drm_simple_display_pipe_init(drm, &priv->pipe,
  488. &pl111_display_funcs,
  489. priv->variant->formats,
  490. priv->variant->nformats,
  491. NULL,
  492. priv->connector);
  493. if (ret)
  494. return ret;
  495. return 0;
  496. }