pl111_display.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 * 1000; /* 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. {
  102. struct drm_crtc *crtc = &pipe->crtc;
  103. struct drm_plane *plane = &pipe->plane;
  104. struct drm_device *drm = crtc->dev;
  105. struct pl111_drm_dev_private *priv = drm->dev_private;
  106. const struct drm_display_mode *mode = &cstate->mode;
  107. struct drm_framebuffer *fb = plane->state->fb;
  108. struct drm_connector *connector = priv->connector;
  109. struct drm_bridge *bridge = priv->bridge;
  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. dev_err(drm->dev,
  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_NEGEDGE)
  155. tim2 |= TIM2_IPC;
  156. }
  157. if (bridge) {
  158. const struct drm_bridge_timings *btimings = bridge->timings;
  159. /*
  160. * Here is when things get really fun. Sometimes the bridge
  161. * timings are such that the signal out from PL11x is not
  162. * stable before the receiving bridge (such as a dumb VGA DAC
  163. * or similar) samples it. If that happens, we compensate by
  164. * the only method we have: output the data on the opposite
  165. * edge of the clock so it is for sure stable when it gets
  166. * sampled.
  167. *
  168. * The PL111 manual does not contain proper timining diagrams
  169. * or data for these details, but we know from experiments
  170. * that the setup time is more than 3000 picoseconds (3 ns).
  171. * If we have a bridge that requires the signal to be stable
  172. * earlier than 3000 ps before the clock pulse, we have to
  173. * output the data on the opposite edge to avoid flicker.
  174. */
  175. if (btimings && btimings->setup_time_ps >= 3000)
  176. tim2 ^= TIM2_IPC;
  177. }
  178. tim2 |= cpl << 16;
  179. writel(tim2, priv->regs + CLCD_TIM2);
  180. spin_unlock(&priv->tim2_lock);
  181. writel(0, priv->regs + CLCD_TIM3);
  182. /* Hard-code TFT panel */
  183. cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDVCOMP(1);
  184. /* Note that the the hardware's format reader takes 'r' from
  185. * the low bit, while DRM formats list channels from high bit
  186. * to low bit as you read left to right.
  187. */
  188. switch (fb->format->format) {
  189. case DRM_FORMAT_ABGR8888:
  190. case DRM_FORMAT_XBGR8888:
  191. cntl |= CNTL_LCDBPP24;
  192. break;
  193. case DRM_FORMAT_ARGB8888:
  194. case DRM_FORMAT_XRGB8888:
  195. cntl |= CNTL_LCDBPP24 | CNTL_BGR;
  196. break;
  197. case DRM_FORMAT_BGR565:
  198. if (priv->variant->is_pl110)
  199. cntl |= CNTL_LCDBPP16;
  200. else
  201. cntl |= CNTL_LCDBPP16_565;
  202. break;
  203. case DRM_FORMAT_RGB565:
  204. if (priv->variant->is_pl110)
  205. cntl |= CNTL_LCDBPP16;
  206. else
  207. cntl |= CNTL_LCDBPP16_565;
  208. cntl |= CNTL_BGR;
  209. break;
  210. case DRM_FORMAT_ABGR1555:
  211. case DRM_FORMAT_XBGR1555:
  212. cntl |= CNTL_LCDBPP16;
  213. break;
  214. case DRM_FORMAT_ARGB1555:
  215. case DRM_FORMAT_XRGB1555:
  216. cntl |= CNTL_LCDBPP16 | CNTL_BGR;
  217. break;
  218. case DRM_FORMAT_ABGR4444:
  219. case DRM_FORMAT_XBGR4444:
  220. cntl |= CNTL_LCDBPP16_444;
  221. break;
  222. case DRM_FORMAT_ARGB4444:
  223. case DRM_FORMAT_XRGB4444:
  224. cntl |= CNTL_LCDBPP16_444 | CNTL_BGR;
  225. break;
  226. default:
  227. WARN_ONCE(true, "Unknown FB format 0x%08x\n",
  228. fb->format->format);
  229. break;
  230. }
  231. /* The PL110 in Integrator/Versatile does the BGR routing externally */
  232. if (priv->variant->external_bgr)
  233. cntl &= ~CNTL_BGR;
  234. /* Power sequence: first enable and chill */
  235. writel(cntl, priv->regs + priv->ctrl);
  236. /*
  237. * We expect this delay to stabilize the contrast
  238. * voltage Vee as stipulated by the manual
  239. */
  240. msleep(20);
  241. if (priv->variant_display_enable)
  242. priv->variant_display_enable(drm, fb->format->format);
  243. /* Power Up */
  244. cntl |= CNTL_LCDPWR;
  245. writel(cntl, priv->regs + priv->ctrl);
  246. if (!priv->variant->broken_vblank)
  247. drm_crtc_vblank_on(crtc);
  248. }
  249. void pl111_display_disable(struct drm_simple_display_pipe *pipe)
  250. {
  251. struct drm_crtc *crtc = &pipe->crtc;
  252. struct drm_device *drm = crtc->dev;
  253. struct pl111_drm_dev_private *priv = drm->dev_private;
  254. u32 cntl;
  255. if (!priv->variant->broken_vblank)
  256. drm_crtc_vblank_off(crtc);
  257. /* Power Down */
  258. cntl = readl(priv->regs + priv->ctrl);
  259. if (cntl & CNTL_LCDPWR) {
  260. cntl &= ~CNTL_LCDPWR;
  261. writel(cntl, priv->regs + priv->ctrl);
  262. }
  263. /*
  264. * We expect this delay to stabilize the contrast voltage Vee as
  265. * stipulated by the manual
  266. */
  267. msleep(20);
  268. if (priv->variant_display_disable)
  269. priv->variant_display_disable(drm);
  270. /* Disable */
  271. writel(0, priv->regs + priv->ctrl);
  272. clk_disable_unprepare(priv->clk);
  273. }
  274. static void pl111_display_update(struct drm_simple_display_pipe *pipe,
  275. struct drm_plane_state *old_pstate)
  276. {
  277. struct drm_crtc *crtc = &pipe->crtc;
  278. struct drm_device *drm = crtc->dev;
  279. struct pl111_drm_dev_private *priv = drm->dev_private;
  280. struct drm_pending_vblank_event *event = crtc->state->event;
  281. struct drm_plane *plane = &pipe->plane;
  282. struct drm_plane_state *pstate = plane->state;
  283. struct drm_framebuffer *fb = pstate->fb;
  284. if (fb) {
  285. u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  286. writel(addr, priv->regs + CLCD_UBAS);
  287. }
  288. if (event) {
  289. crtc->state->event = NULL;
  290. spin_lock_irq(&crtc->dev->event_lock);
  291. if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
  292. drm_crtc_arm_vblank_event(crtc, event);
  293. else
  294. drm_crtc_send_vblank_event(crtc, event);
  295. spin_unlock_irq(&crtc->dev->event_lock);
  296. }
  297. }
  298. static int pl111_display_enable_vblank(struct drm_simple_display_pipe *pipe)
  299. {
  300. struct drm_crtc *crtc = &pipe->crtc;
  301. struct drm_device *drm = crtc->dev;
  302. struct pl111_drm_dev_private *priv = drm->dev_private;
  303. writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + priv->ienb);
  304. return 0;
  305. }
  306. static void pl111_display_disable_vblank(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. writel(0, priv->regs + priv->ienb);
  312. }
  313. static int pl111_display_prepare_fb(struct drm_simple_display_pipe *pipe,
  314. struct drm_plane_state *plane_state)
  315. {
  316. return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
  317. }
  318. static struct drm_simple_display_pipe_funcs pl111_display_funcs = {
  319. .mode_valid = pl111_mode_valid,
  320. .check = pl111_display_check,
  321. .enable = pl111_display_enable,
  322. .disable = pl111_display_disable,
  323. .update = pl111_display_update,
  324. .prepare_fb = pl111_display_prepare_fb,
  325. };
  326. static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
  327. unsigned long *prate, bool set_parent)
  328. {
  329. int best_div = 1, div;
  330. struct clk_hw *parent = clk_hw_get_parent(hw);
  331. unsigned long best_prate = 0;
  332. unsigned long best_diff = ~0ul;
  333. int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
  334. for (div = 1; div < max_div; div++) {
  335. unsigned long this_prate, div_rate, diff;
  336. if (set_parent)
  337. this_prate = clk_hw_round_rate(parent, rate * div);
  338. else
  339. this_prate = *prate;
  340. div_rate = DIV_ROUND_UP_ULL(this_prate, div);
  341. diff = abs(rate - div_rate);
  342. if (diff < best_diff) {
  343. best_div = div;
  344. best_diff = diff;
  345. best_prate = this_prate;
  346. }
  347. }
  348. *prate = best_prate;
  349. return best_div;
  350. }
  351. static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
  352. unsigned long *prate)
  353. {
  354. int div = pl111_clk_div_choose_div(hw, rate, prate, true);
  355. return DIV_ROUND_UP_ULL(*prate, div);
  356. }
  357. static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
  358. unsigned long prate)
  359. {
  360. struct pl111_drm_dev_private *priv =
  361. container_of(hw, struct pl111_drm_dev_private, clk_div);
  362. u32 tim2 = readl(priv->regs + CLCD_TIM2);
  363. int div;
  364. if (tim2 & TIM2_BCD)
  365. return prate;
  366. div = tim2 & TIM2_PCD_LO_MASK;
  367. div |= (tim2 & TIM2_PCD_HI_MASK) >>
  368. (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
  369. div += 2;
  370. return DIV_ROUND_UP_ULL(prate, div);
  371. }
  372. static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
  373. unsigned long prate)
  374. {
  375. struct pl111_drm_dev_private *priv =
  376. container_of(hw, struct pl111_drm_dev_private, clk_div);
  377. int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
  378. u32 tim2;
  379. spin_lock(&priv->tim2_lock);
  380. tim2 = readl(priv->regs + CLCD_TIM2);
  381. tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
  382. if (div == 1) {
  383. tim2 |= TIM2_BCD;
  384. } else {
  385. div -= 2;
  386. tim2 |= div & TIM2_PCD_LO_MASK;
  387. tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
  388. }
  389. writel(tim2, priv->regs + CLCD_TIM2);
  390. spin_unlock(&priv->tim2_lock);
  391. return 0;
  392. }
  393. static const struct clk_ops pl111_clk_div_ops = {
  394. .recalc_rate = pl111_clk_div_recalc_rate,
  395. .round_rate = pl111_clk_div_round_rate,
  396. .set_rate = pl111_clk_div_set_rate,
  397. };
  398. static int
  399. pl111_init_clock_divider(struct drm_device *drm)
  400. {
  401. struct pl111_drm_dev_private *priv = drm->dev_private;
  402. struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
  403. struct clk_hw *div = &priv->clk_div;
  404. const char *parent_name;
  405. struct clk_init_data init = {
  406. .name = "pl111_div",
  407. .ops = &pl111_clk_div_ops,
  408. .parent_names = &parent_name,
  409. .num_parents = 1,
  410. .flags = CLK_SET_RATE_PARENT,
  411. };
  412. int ret;
  413. if (IS_ERR(parent)) {
  414. dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
  415. return PTR_ERR(parent);
  416. }
  417. /* If the clock divider is broken, use the parent directly */
  418. if (priv->variant->broken_clockdivider) {
  419. priv->clk = parent;
  420. return 0;
  421. }
  422. parent_name = __clk_get_name(parent);
  423. spin_lock_init(&priv->tim2_lock);
  424. div->init = &init;
  425. ret = devm_clk_hw_register(drm->dev, div);
  426. priv->clk = div->clk;
  427. return ret;
  428. }
  429. int pl111_display_init(struct drm_device *drm)
  430. {
  431. struct pl111_drm_dev_private *priv = drm->dev_private;
  432. struct device *dev = drm->dev;
  433. struct device_node *endpoint;
  434. u32 tft_r0b0g0[3];
  435. int ret;
  436. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  437. if (!endpoint)
  438. return -ENODEV;
  439. if (of_property_read_u32_array(endpoint,
  440. "arm,pl11x,tft-r0g0b0-pads",
  441. tft_r0b0g0,
  442. ARRAY_SIZE(tft_r0b0g0)) != 0) {
  443. dev_err(dev, "arm,pl11x,tft-r0g0b0-pads should be 3 ints\n");
  444. of_node_put(endpoint);
  445. return -ENOENT;
  446. }
  447. of_node_put(endpoint);
  448. ret = pl111_init_clock_divider(drm);
  449. if (ret)
  450. return ret;
  451. if (!priv->variant->broken_vblank) {
  452. pl111_display_funcs.enable_vblank = pl111_display_enable_vblank;
  453. pl111_display_funcs.disable_vblank = pl111_display_disable_vblank;
  454. }
  455. ret = drm_simple_display_pipe_init(drm, &priv->pipe,
  456. &pl111_display_funcs,
  457. priv->variant->formats,
  458. priv->variant->nformats,
  459. NULL,
  460. priv->connector);
  461. if (ret)
  462. return ret;
  463. return 0;
  464. }