tve200_display.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  3. * Parts of this file were based on sources as follows:
  4. *
  5. * Copyright (C) 2006-2008 Intel Corporation
  6. * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
  7. * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
  8. * Copyright (C) 2011 Texas Instruments
  9. * Copyright (C) 2017 Eric Anholt
  10. *
  11. * This program is free software and is provided to you under the terms of the
  12. * GNU General Public License version 2 as published by the Free Software
  13. * Foundation, and any use by you of this program is subject to the terms of
  14. * such GNU licence.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/version.h>
  18. #include <linux/dma-buf.h>
  19. #include <linux/of_graph.h>
  20. #include <drm/drmP.h>
  21. #include <drm/drm_panel.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 "tve200_drm.h"
  26. irqreturn_t tve200_irq(int irq, void *data)
  27. {
  28. struct tve200_drm_dev_private *priv = data;
  29. u32 stat;
  30. u32 val;
  31. stat = readl(priv->regs + TVE200_INT_STAT);
  32. if (!stat)
  33. return IRQ_NONE;
  34. /*
  35. * Vblank IRQ
  36. *
  37. * The hardware is a bit tilted: the line stays high after clearing
  38. * the vblank IRQ, firing many more interrupts. We counter this
  39. * by toggling the IRQ back and forth from firing at vblank and
  40. * firing at start of active image, which works around the problem
  41. * since those occur strictly in sequence, and we get two IRQs for each
  42. * frame, one at start of Vblank (that we make call into the CRTC) and
  43. * another one at the start of the image (that we discard).
  44. */
  45. if (stat & TVE200_INT_V_STATUS) {
  46. val = readl(priv->regs + TVE200_CTRL);
  47. /* We have an actual start of vsync */
  48. if (!(val & TVE200_VSTSTYPE_BITS)) {
  49. drm_crtc_handle_vblank(&priv->pipe.crtc);
  50. /* Toggle trigger to start of active image */
  51. val |= TVE200_VSTSTYPE_VAI;
  52. } else {
  53. /* Toggle trigger back to start of vsync */
  54. val &= ~TVE200_VSTSTYPE_BITS;
  55. }
  56. writel(val, priv->regs + TVE200_CTRL);
  57. } else
  58. dev_err(priv->drm->dev, "stray IRQ %08x\n", stat);
  59. /* Clear the interrupt once done */
  60. writel(stat, priv->regs + TVE200_INT_CLR);
  61. return IRQ_HANDLED;
  62. }
  63. static int tve200_display_check(struct drm_simple_display_pipe *pipe,
  64. struct drm_plane_state *pstate,
  65. struct drm_crtc_state *cstate)
  66. {
  67. const struct drm_display_mode *mode = &cstate->mode;
  68. struct drm_framebuffer *old_fb = pipe->plane.state->fb;
  69. struct drm_framebuffer *fb = pstate->fb;
  70. /*
  71. * We support these specific resolutions and nothing else.
  72. */
  73. if (!(mode->hdisplay == 352 && mode->vdisplay == 240) && /* SIF(525) */
  74. !(mode->hdisplay == 352 && mode->vdisplay == 288) && /* CIF(625) */
  75. !(mode->hdisplay == 640 && mode->vdisplay == 480) && /* VGA */
  76. !(mode->hdisplay == 720 && mode->vdisplay == 480) && /* D1 */
  77. !(mode->hdisplay == 720 && mode->vdisplay == 576)) { /* D1 */
  78. DRM_DEBUG_KMS("unsupported display mode (%u x %u)\n",
  79. mode->hdisplay, mode->vdisplay);
  80. return -EINVAL;
  81. }
  82. if (fb) {
  83. u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
  84. /* FB base address must be dword aligned. */
  85. if (offset & 3) {
  86. DRM_DEBUG_KMS("FB not 32-bit aligned\n");
  87. return -EINVAL;
  88. }
  89. /*
  90. * There's no pitch register, the mode's hdisplay
  91. * controls this.
  92. */
  93. if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0]) {
  94. DRM_DEBUG_KMS("can't handle pitches\n");
  95. return -EINVAL;
  96. }
  97. /*
  98. * We can't change the FB format in a flicker-free
  99. * manner (and only update it during CRTC enable).
  100. */
  101. if (old_fb && old_fb->format != fb->format)
  102. cstate->mode_changed = true;
  103. }
  104. return 0;
  105. }
  106. static void tve200_display_enable(struct drm_simple_display_pipe *pipe,
  107. struct drm_crtc_state *cstate,
  108. struct drm_plane_state *plane_state)
  109. {
  110. struct drm_crtc *crtc = &pipe->crtc;
  111. struct drm_plane *plane = &pipe->plane;
  112. struct drm_device *drm = crtc->dev;
  113. struct tve200_drm_dev_private *priv = drm->dev_private;
  114. const struct drm_display_mode *mode = &cstate->mode;
  115. struct drm_framebuffer *fb = plane->state->fb;
  116. struct drm_connector *connector = priv->connector;
  117. u32 format = fb->format->format;
  118. u32 ctrl1 = 0;
  119. clk_prepare_enable(priv->clk);
  120. /* Function 1 */
  121. ctrl1 |= TVE200_CTRL_CSMODE;
  122. /* Interlace mode for CCIR656: parameterize? */
  123. ctrl1 |= TVE200_CTRL_NONINTERLACE;
  124. /* 32 words per burst */
  125. ctrl1 |= TVE200_CTRL_BURST_32_WORDS;
  126. /* 16 retries */
  127. ctrl1 |= TVE200_CTRL_RETRYCNT_16;
  128. /* NTSC mode: parametrize? */
  129. ctrl1 |= TVE200_CTRL_NTSC;
  130. /* Vsync IRQ at start of Vsync at first */
  131. ctrl1 |= TVE200_VSTSTYPE_VSYNC;
  132. if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
  133. ctrl1 |= TVE200_CTRL_TVCLKP;
  134. if ((mode->hdisplay == 352 && mode->vdisplay == 240) || /* SIF(525) */
  135. (mode->hdisplay == 352 && mode->vdisplay == 288)) { /* CIF(625) */
  136. ctrl1 |= TVE200_CTRL_IPRESOL_CIF;
  137. dev_info(drm->dev, "CIF mode\n");
  138. } else if (mode->hdisplay == 640 && mode->vdisplay == 480) {
  139. ctrl1 |= TVE200_CTRL_IPRESOL_VGA;
  140. dev_info(drm->dev, "VGA mode\n");
  141. } else if ((mode->hdisplay == 720 && mode->vdisplay == 480) ||
  142. (mode->hdisplay == 720 && mode->vdisplay == 576)) {
  143. ctrl1 |= TVE200_CTRL_IPRESOL_D1;
  144. dev_info(drm->dev, "D1 mode\n");
  145. }
  146. if (format & DRM_FORMAT_BIG_ENDIAN) {
  147. ctrl1 |= TVE200_CTRL_BBBP;
  148. format &= ~DRM_FORMAT_BIG_ENDIAN;
  149. }
  150. switch (format) {
  151. case DRM_FORMAT_XRGB8888:
  152. ctrl1 |= TVE200_IPDMOD_RGB888;
  153. break;
  154. case DRM_FORMAT_RGB565:
  155. ctrl1 |= TVE200_IPDMOD_RGB565;
  156. break;
  157. case DRM_FORMAT_XRGB1555:
  158. ctrl1 |= TVE200_IPDMOD_RGB555;
  159. break;
  160. case DRM_FORMAT_XBGR8888:
  161. ctrl1 |= TVE200_IPDMOD_RGB888 | TVE200_BGR;
  162. break;
  163. case DRM_FORMAT_BGR565:
  164. ctrl1 |= TVE200_IPDMOD_RGB565 | TVE200_BGR;
  165. break;
  166. case DRM_FORMAT_XBGR1555:
  167. ctrl1 |= TVE200_IPDMOD_RGB555 | TVE200_BGR;
  168. break;
  169. case DRM_FORMAT_YUYV:
  170. ctrl1 |= TVE200_IPDMOD_YUV422;
  171. ctrl1 |= TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0;
  172. break;
  173. case DRM_FORMAT_YVYU:
  174. ctrl1 |= TVE200_IPDMOD_YUV422;
  175. ctrl1 |= TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0;
  176. break;
  177. case DRM_FORMAT_UYVY:
  178. ctrl1 |= TVE200_IPDMOD_YUV422;
  179. ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0;
  180. break;
  181. case DRM_FORMAT_VYUY:
  182. ctrl1 |= TVE200_IPDMOD_YUV422;
  183. ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0;
  184. break;
  185. case DRM_FORMAT_YUV420:
  186. ctrl1 |= TVE200_CTRL_YUV420;
  187. ctrl1 |= TVE200_IPDMOD_YUV420;
  188. break;
  189. default:
  190. dev_err(drm->dev, "Unknown FB format 0x%08x\n",
  191. fb->format->format);
  192. break;
  193. }
  194. ctrl1 |= TVE200_TVEEN;
  195. /* Turn it on */
  196. writel(ctrl1, priv->regs + TVE200_CTRL);
  197. drm_crtc_vblank_on(crtc);
  198. }
  199. static void tve200_display_disable(struct drm_simple_display_pipe *pipe)
  200. {
  201. struct drm_crtc *crtc = &pipe->crtc;
  202. struct drm_device *drm = crtc->dev;
  203. struct tve200_drm_dev_private *priv = drm->dev_private;
  204. drm_crtc_vblank_off(crtc);
  205. /* Disable and Power Down */
  206. writel(0, priv->regs + TVE200_CTRL);
  207. clk_disable_unprepare(priv->clk);
  208. }
  209. static void tve200_display_update(struct drm_simple_display_pipe *pipe,
  210. struct drm_plane_state *old_pstate)
  211. {
  212. struct drm_crtc *crtc = &pipe->crtc;
  213. struct drm_device *drm = crtc->dev;
  214. struct tve200_drm_dev_private *priv = drm->dev_private;
  215. struct drm_pending_vblank_event *event = crtc->state->event;
  216. struct drm_plane *plane = &pipe->plane;
  217. struct drm_plane_state *pstate = plane->state;
  218. struct drm_framebuffer *fb = pstate->fb;
  219. if (fb) {
  220. /* For RGB, the Y component is used as base address */
  221. writel(drm_fb_cma_get_gem_addr(fb, pstate, 0),
  222. priv->regs + TVE200_Y_FRAME_BASE_ADDR);
  223. /* For three plane YUV we need two more addresses */
  224. if (fb->format->format == DRM_FORMAT_YUV420) {
  225. writel(drm_fb_cma_get_gem_addr(fb, pstate, 1),
  226. priv->regs + TVE200_U_FRAME_BASE_ADDR);
  227. writel(drm_fb_cma_get_gem_addr(fb, pstate, 2),
  228. priv->regs + TVE200_V_FRAME_BASE_ADDR);
  229. }
  230. }
  231. if (event) {
  232. crtc->state->event = NULL;
  233. spin_lock_irq(&crtc->dev->event_lock);
  234. if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
  235. drm_crtc_arm_vblank_event(crtc, event);
  236. else
  237. drm_crtc_send_vblank_event(crtc, event);
  238. spin_unlock_irq(&crtc->dev->event_lock);
  239. }
  240. }
  241. static int tve200_display_enable_vblank(struct drm_simple_display_pipe *pipe)
  242. {
  243. struct drm_crtc *crtc = &pipe->crtc;
  244. struct drm_device *drm = crtc->dev;
  245. struct tve200_drm_dev_private *priv = drm->dev_private;
  246. writel(TVE200_INT_V_STATUS, priv->regs + TVE200_INT_EN);
  247. return 0;
  248. }
  249. static void tve200_display_disable_vblank(struct drm_simple_display_pipe *pipe)
  250. {
  251. struct drm_crtc *crtc = &pipe->crtc;
  252. struct drm_device *drm = crtc->dev;
  253. struct tve200_drm_dev_private *priv = drm->dev_private;
  254. writel(0, priv->regs + TVE200_INT_EN);
  255. }
  256. static const struct drm_simple_display_pipe_funcs tve200_display_funcs = {
  257. .check = tve200_display_check,
  258. .enable = tve200_display_enable,
  259. .disable = tve200_display_disable,
  260. .update = tve200_display_update,
  261. .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
  262. .enable_vblank = tve200_display_enable_vblank,
  263. .disable_vblank = tve200_display_disable_vblank,
  264. };
  265. int tve200_display_init(struct drm_device *drm)
  266. {
  267. struct tve200_drm_dev_private *priv = drm->dev_private;
  268. int ret;
  269. static const u32 formats[] = {
  270. DRM_FORMAT_XRGB8888,
  271. DRM_FORMAT_XBGR8888,
  272. DRM_FORMAT_RGB565,
  273. DRM_FORMAT_BGR565,
  274. DRM_FORMAT_XRGB1555,
  275. DRM_FORMAT_XBGR1555,
  276. /*
  277. * The controller actually supports any YCbCr ordering,
  278. * for packed YCbCr. This just lists the orderings that
  279. * DRM supports.
  280. */
  281. DRM_FORMAT_YUYV,
  282. DRM_FORMAT_YVYU,
  283. DRM_FORMAT_UYVY,
  284. DRM_FORMAT_VYUY,
  285. /* This uses three planes */
  286. DRM_FORMAT_YUV420,
  287. };
  288. ret = drm_simple_display_pipe_init(drm, &priv->pipe,
  289. &tve200_display_funcs,
  290. formats, ARRAY_SIZE(formats),
  291. NULL,
  292. priv->connector);
  293. if (ret)
  294. return ret;
  295. return 0;
  296. }