pl111_display.c 15 KB

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