pl111_display.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. u32 cntl;
  80. u32 ppl, hsw, hfp, hbp;
  81. u32 lpp, vsw, vfp, vbp;
  82. u32 cpl, tim2;
  83. int ret;
  84. ret = clk_set_rate(priv->clk, mode->clock * 1000);
  85. if (ret) {
  86. dev_err(drm->dev,
  87. "Failed to set pixel clock rate to %d: %d\n",
  88. mode->clock * 1000, ret);
  89. }
  90. clk_prepare_enable(priv->clk);
  91. ppl = (mode->hdisplay / 16) - 1;
  92. hsw = mode->hsync_end - mode->hsync_start - 1;
  93. hfp = mode->hsync_start - mode->hdisplay - 1;
  94. hbp = mode->htotal - mode->hsync_end - 1;
  95. lpp = mode->vdisplay - 1;
  96. vsw = mode->vsync_end - mode->vsync_start - 1;
  97. vfp = mode->vsync_start - mode->vdisplay;
  98. vbp = mode->vtotal - mode->vsync_end;
  99. cpl = mode->hdisplay - 1;
  100. writel((ppl << 2) |
  101. (hsw << 8) |
  102. (hfp << 16) |
  103. (hbp << 24),
  104. priv->regs + CLCD_TIM0);
  105. writel(lpp |
  106. (vsw << 10) |
  107. (vfp << 16) |
  108. (vbp << 24),
  109. priv->regs + CLCD_TIM1);
  110. spin_lock(&priv->tim2_lock);
  111. tim2 = readl(priv->regs + CLCD_TIM2);
  112. tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
  113. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  114. tim2 |= TIM2_IHS;
  115. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  116. tim2 |= TIM2_IVS;
  117. if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
  118. tim2 |= TIM2_IOE;
  119. if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
  120. tim2 |= TIM2_IPC;
  121. tim2 |= cpl << 16;
  122. writel(tim2, priv->regs + CLCD_TIM2);
  123. spin_unlock(&priv->tim2_lock);
  124. writel(0, priv->regs + CLCD_TIM3);
  125. /* Hard-code TFT panel */
  126. cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDVCOMP(1);
  127. /* Note that the the hardware's format reader takes 'r' from
  128. * the low bit, while DRM formats list channels from high bit
  129. * to low bit as you read left to right.
  130. */
  131. switch (fb->format->format) {
  132. case DRM_FORMAT_ABGR8888:
  133. case DRM_FORMAT_XBGR8888:
  134. cntl |= CNTL_LCDBPP24;
  135. break;
  136. case DRM_FORMAT_ARGB8888:
  137. case DRM_FORMAT_XRGB8888:
  138. cntl |= CNTL_LCDBPP24 | CNTL_BGR;
  139. break;
  140. case DRM_FORMAT_BGR565:
  141. cntl |= CNTL_LCDBPP16_565;
  142. break;
  143. case DRM_FORMAT_RGB565:
  144. cntl |= CNTL_LCDBPP16_565 | CNTL_BGR;
  145. break;
  146. case DRM_FORMAT_ABGR1555:
  147. case DRM_FORMAT_XBGR1555:
  148. cntl |= CNTL_LCDBPP16;
  149. break;
  150. case DRM_FORMAT_ARGB1555:
  151. case DRM_FORMAT_XRGB1555:
  152. cntl |= CNTL_LCDBPP16 | CNTL_BGR;
  153. break;
  154. case DRM_FORMAT_ABGR4444:
  155. case DRM_FORMAT_XBGR4444:
  156. cntl |= CNTL_LCDBPP16_444;
  157. break;
  158. case DRM_FORMAT_ARGB4444:
  159. case DRM_FORMAT_XRGB4444:
  160. cntl |= CNTL_LCDBPP16_444 | CNTL_BGR;
  161. break;
  162. default:
  163. WARN_ONCE(true, "Unknown FB format 0x%08x\n",
  164. fb->format->format);
  165. break;
  166. }
  167. /* Power sequence: first enable and chill */
  168. writel(cntl, priv->regs + priv->ctrl);
  169. /*
  170. * We expect this delay to stabilize the contrast
  171. * voltage Vee as stipulated by the manual
  172. */
  173. msleep(20);
  174. if (priv->variant_display_enable)
  175. priv->variant_display_enable(drm, fb->format->format);
  176. /* Power Up */
  177. cntl |= CNTL_LCDPWR;
  178. writel(cntl, priv->regs + priv->ctrl);
  179. drm_crtc_vblank_on(crtc);
  180. }
  181. void pl111_display_disable(struct drm_simple_display_pipe *pipe)
  182. {
  183. struct drm_crtc *crtc = &pipe->crtc;
  184. struct drm_device *drm = crtc->dev;
  185. struct pl111_drm_dev_private *priv = drm->dev_private;
  186. u32 cntl;
  187. drm_crtc_vblank_off(crtc);
  188. /* Power Down */
  189. cntl = readl(priv->regs + priv->ctrl);
  190. if (cntl & CNTL_LCDPWR) {
  191. cntl &= ~CNTL_LCDPWR;
  192. writel(cntl, priv->regs + priv->ctrl);
  193. }
  194. /*
  195. * We expect this delay to stabilize the contrast voltage Vee as
  196. * stipulated by the manual
  197. */
  198. msleep(20);
  199. if (priv->variant_display_disable)
  200. priv->variant_display_disable(drm);
  201. /* Disable */
  202. writel(0, priv->regs + priv->ctrl);
  203. clk_disable_unprepare(priv->clk);
  204. }
  205. static void pl111_display_update(struct drm_simple_display_pipe *pipe,
  206. struct drm_plane_state *old_pstate)
  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. struct drm_pending_vblank_event *event = crtc->state->event;
  212. struct drm_plane *plane = &pipe->plane;
  213. struct drm_plane_state *pstate = plane->state;
  214. struct drm_framebuffer *fb = pstate->fb;
  215. if (fb) {
  216. u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  217. writel(addr, priv->regs + CLCD_UBAS);
  218. }
  219. if (event) {
  220. crtc->state->event = NULL;
  221. spin_lock_irq(&crtc->dev->event_lock);
  222. if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
  223. drm_crtc_arm_vblank_event(crtc, event);
  224. else
  225. drm_crtc_send_vblank_event(crtc, event);
  226. spin_unlock_irq(&crtc->dev->event_lock);
  227. }
  228. }
  229. int pl111_enable_vblank(struct drm_device *drm, unsigned int crtc)
  230. {
  231. struct pl111_drm_dev_private *priv = drm->dev_private;
  232. writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + priv->ienb);
  233. return 0;
  234. }
  235. void pl111_disable_vblank(struct drm_device *drm, unsigned int crtc)
  236. {
  237. struct pl111_drm_dev_private *priv = drm->dev_private;
  238. writel(0, priv->regs + priv->ienb);
  239. }
  240. static int pl111_display_prepare_fb(struct drm_simple_display_pipe *pipe,
  241. struct drm_plane_state *plane_state)
  242. {
  243. return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
  244. }
  245. static const struct drm_simple_display_pipe_funcs pl111_display_funcs = {
  246. .check = pl111_display_check,
  247. .enable = pl111_display_enable,
  248. .disable = pl111_display_disable,
  249. .update = pl111_display_update,
  250. .prepare_fb = pl111_display_prepare_fb,
  251. };
  252. static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
  253. unsigned long *prate, bool set_parent)
  254. {
  255. int best_div = 1, div;
  256. struct clk_hw *parent = clk_hw_get_parent(hw);
  257. unsigned long best_prate = 0;
  258. unsigned long best_diff = ~0ul;
  259. int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
  260. for (div = 1; div < max_div; div++) {
  261. unsigned long this_prate, div_rate, diff;
  262. if (set_parent)
  263. this_prate = clk_hw_round_rate(parent, rate * div);
  264. else
  265. this_prate = *prate;
  266. div_rate = DIV_ROUND_UP_ULL(this_prate, div);
  267. diff = abs(rate - div_rate);
  268. if (diff < best_diff) {
  269. best_div = div;
  270. best_diff = diff;
  271. best_prate = this_prate;
  272. }
  273. }
  274. *prate = best_prate;
  275. return best_div;
  276. }
  277. static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
  278. unsigned long *prate)
  279. {
  280. int div = pl111_clk_div_choose_div(hw, rate, prate, true);
  281. return DIV_ROUND_UP_ULL(*prate, div);
  282. }
  283. static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
  284. unsigned long prate)
  285. {
  286. struct pl111_drm_dev_private *priv =
  287. container_of(hw, struct pl111_drm_dev_private, clk_div);
  288. u32 tim2 = readl(priv->regs + CLCD_TIM2);
  289. int div;
  290. if (tim2 & TIM2_BCD)
  291. return prate;
  292. div = tim2 & TIM2_PCD_LO_MASK;
  293. div |= (tim2 & TIM2_PCD_HI_MASK) >>
  294. (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
  295. div += 2;
  296. return DIV_ROUND_UP_ULL(prate, div);
  297. }
  298. static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
  299. unsigned long prate)
  300. {
  301. struct pl111_drm_dev_private *priv =
  302. container_of(hw, struct pl111_drm_dev_private, clk_div);
  303. int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
  304. u32 tim2;
  305. spin_lock(&priv->tim2_lock);
  306. tim2 = readl(priv->regs + CLCD_TIM2);
  307. tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
  308. if (div == 1) {
  309. tim2 |= TIM2_BCD;
  310. } else {
  311. div -= 2;
  312. tim2 |= div & TIM2_PCD_LO_MASK;
  313. tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
  314. }
  315. writel(tim2, priv->regs + CLCD_TIM2);
  316. spin_unlock(&priv->tim2_lock);
  317. return 0;
  318. }
  319. static const struct clk_ops pl111_clk_div_ops = {
  320. .recalc_rate = pl111_clk_div_recalc_rate,
  321. .round_rate = pl111_clk_div_round_rate,
  322. .set_rate = pl111_clk_div_set_rate,
  323. };
  324. static int
  325. pl111_init_clock_divider(struct drm_device *drm)
  326. {
  327. struct pl111_drm_dev_private *priv = drm->dev_private;
  328. struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
  329. struct clk_hw *div = &priv->clk_div;
  330. const char *parent_name;
  331. struct clk_init_data init = {
  332. .name = "pl111_div",
  333. .ops = &pl111_clk_div_ops,
  334. .parent_names = &parent_name,
  335. .num_parents = 1,
  336. .flags = CLK_SET_RATE_PARENT,
  337. };
  338. int ret;
  339. if (IS_ERR(parent)) {
  340. dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
  341. return PTR_ERR(parent);
  342. }
  343. parent_name = __clk_get_name(parent);
  344. spin_lock_init(&priv->tim2_lock);
  345. div->init = &init;
  346. ret = devm_clk_hw_register(drm->dev, div);
  347. priv->clk = div->clk;
  348. return ret;
  349. }
  350. int pl111_display_init(struct drm_device *drm)
  351. {
  352. struct pl111_drm_dev_private *priv = drm->dev_private;
  353. struct device *dev = drm->dev;
  354. struct device_node *endpoint;
  355. u32 tft_r0b0g0[3];
  356. int ret;
  357. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  358. if (!endpoint)
  359. return -ENODEV;
  360. if (of_property_read_u32_array(endpoint,
  361. "arm,pl11x,tft-r0g0b0-pads",
  362. tft_r0b0g0,
  363. ARRAY_SIZE(tft_r0b0g0)) != 0) {
  364. dev_err(dev, "arm,pl11x,tft-r0g0b0-pads should be 3 ints\n");
  365. of_node_put(endpoint);
  366. return -ENOENT;
  367. }
  368. of_node_put(endpoint);
  369. ret = pl111_init_clock_divider(drm);
  370. if (ret)
  371. return ret;
  372. ret = drm_simple_display_pipe_init(drm, &priv->pipe,
  373. &pl111_display_funcs,
  374. priv->variant->formats,
  375. priv->variant->nformats,
  376. NULL,
  377. priv->connector);
  378. if (ret)
  379. return ret;
  380. return 0;
  381. }