pl111_display.c 15 KB

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