tve200_display.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. {
  109. struct drm_crtc *crtc = &pipe->crtc;
  110. struct drm_plane *plane = &pipe->plane;
  111. struct drm_device *drm = crtc->dev;
  112. struct tve200_drm_dev_private *priv = drm->dev_private;
  113. const struct drm_display_mode *mode = &cstate->mode;
  114. struct drm_framebuffer *fb = plane->state->fb;
  115. struct drm_connector *connector = priv->connector;
  116. u32 format = fb->format->format;
  117. u32 ctrl1 = 0;
  118. clk_prepare_enable(priv->clk);
  119. /* Function 1 */
  120. ctrl1 |= TVE200_CTRL_CSMODE;
  121. /* Interlace mode for CCIR656: parameterize? */
  122. ctrl1 |= TVE200_CTRL_NONINTERLACE;
  123. /* 32 words per burst */
  124. ctrl1 |= TVE200_CTRL_BURST_32_WORDS;
  125. /* 16 retries */
  126. ctrl1 |= TVE200_CTRL_RETRYCNT_16;
  127. /* NTSC mode: parametrize? */
  128. ctrl1 |= TVE200_CTRL_NTSC;
  129. /* Vsync IRQ at start of Vsync at first */
  130. ctrl1 |= TVE200_VSTSTYPE_VSYNC;
  131. if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
  132. ctrl1 |= TVE200_CTRL_TVCLKP;
  133. if ((mode->hdisplay == 352 && mode->vdisplay == 240) || /* SIF(525) */
  134. (mode->hdisplay == 352 && mode->vdisplay == 288)) { /* CIF(625) */
  135. ctrl1 |= TVE200_CTRL_IPRESOL_CIF;
  136. dev_info(drm->dev, "CIF mode\n");
  137. } else if (mode->hdisplay == 640 && mode->vdisplay == 480) {
  138. ctrl1 |= TVE200_CTRL_IPRESOL_VGA;
  139. dev_info(drm->dev, "VGA mode\n");
  140. } else if ((mode->hdisplay == 720 && mode->vdisplay == 480) ||
  141. (mode->hdisplay == 720 && mode->vdisplay == 576)) {
  142. ctrl1 |= TVE200_CTRL_IPRESOL_D1;
  143. dev_info(drm->dev, "D1 mode\n");
  144. }
  145. if (format & DRM_FORMAT_BIG_ENDIAN) {
  146. ctrl1 |= TVE200_CTRL_BBBP;
  147. format &= ~DRM_FORMAT_BIG_ENDIAN;
  148. }
  149. switch (format) {
  150. case DRM_FORMAT_XRGB8888:
  151. ctrl1 |= TVE200_IPDMOD_RGB888;
  152. break;
  153. case DRM_FORMAT_RGB565:
  154. ctrl1 |= TVE200_IPDMOD_RGB565;
  155. break;
  156. case DRM_FORMAT_XRGB1555:
  157. ctrl1 |= TVE200_IPDMOD_RGB555;
  158. break;
  159. case DRM_FORMAT_XBGR8888:
  160. ctrl1 |= TVE200_IPDMOD_RGB888 | TVE200_BGR;
  161. break;
  162. case DRM_FORMAT_BGR565:
  163. ctrl1 |= TVE200_IPDMOD_RGB565 | TVE200_BGR;
  164. break;
  165. case DRM_FORMAT_XBGR1555:
  166. ctrl1 |= TVE200_IPDMOD_RGB555 | TVE200_BGR;
  167. break;
  168. case DRM_FORMAT_YUYV:
  169. ctrl1 |= TVE200_IPDMOD_YUV422;
  170. ctrl1 |= TVE200_CTRL_YCBCRODR_CR0Y1CB0Y0;
  171. break;
  172. case DRM_FORMAT_YVYU:
  173. ctrl1 |= TVE200_IPDMOD_YUV422;
  174. ctrl1 |= TVE200_CTRL_YCBCRODR_CB0Y1CR0Y0;
  175. break;
  176. case DRM_FORMAT_UYVY:
  177. ctrl1 |= TVE200_IPDMOD_YUV422;
  178. ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CR0Y0CB0;
  179. break;
  180. case DRM_FORMAT_VYUY:
  181. ctrl1 |= TVE200_IPDMOD_YUV422;
  182. ctrl1 |= TVE200_CTRL_YCBCRODR_Y1CB0Y0CR0;
  183. break;
  184. case DRM_FORMAT_YUV420:
  185. ctrl1 |= TVE200_CTRL_YUV420;
  186. ctrl1 |= TVE200_IPDMOD_YUV420;
  187. break;
  188. default:
  189. dev_err(drm->dev, "Unknown FB format 0x%08x\n",
  190. fb->format->format);
  191. break;
  192. }
  193. ctrl1 |= TVE200_TVEEN;
  194. /* Turn it on */
  195. writel(ctrl1, priv->regs + TVE200_CTRL);
  196. drm_crtc_vblank_on(crtc);
  197. }
  198. static void tve200_display_disable(struct drm_simple_display_pipe *pipe)
  199. {
  200. struct drm_crtc *crtc = &pipe->crtc;
  201. struct drm_device *drm = crtc->dev;
  202. struct tve200_drm_dev_private *priv = drm->dev_private;
  203. drm_crtc_vblank_off(crtc);
  204. /* Disable and Power Down */
  205. writel(0, priv->regs + TVE200_CTRL);
  206. clk_disable_unprepare(priv->clk);
  207. }
  208. static void tve200_display_update(struct drm_simple_display_pipe *pipe,
  209. struct drm_plane_state *old_pstate)
  210. {
  211. struct drm_crtc *crtc = &pipe->crtc;
  212. struct drm_device *drm = crtc->dev;
  213. struct tve200_drm_dev_private *priv = drm->dev_private;
  214. struct drm_pending_vblank_event *event = crtc->state->event;
  215. struct drm_plane *plane = &pipe->plane;
  216. struct drm_plane_state *pstate = plane->state;
  217. struct drm_framebuffer *fb = pstate->fb;
  218. if (fb) {
  219. /* For RGB, the Y component is used as base address */
  220. writel(drm_fb_cma_get_gem_addr(fb, pstate, 0),
  221. priv->regs + TVE200_Y_FRAME_BASE_ADDR);
  222. /* For three plane YUV we need two more addresses */
  223. if (fb->format->format == DRM_FORMAT_YUV420) {
  224. writel(drm_fb_cma_get_gem_addr(fb, pstate, 1),
  225. priv->regs + TVE200_U_FRAME_BASE_ADDR);
  226. writel(drm_fb_cma_get_gem_addr(fb, pstate, 2),
  227. priv->regs + TVE200_V_FRAME_BASE_ADDR);
  228. }
  229. }
  230. if (event) {
  231. crtc->state->event = NULL;
  232. spin_lock_irq(&crtc->dev->event_lock);
  233. if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
  234. drm_crtc_arm_vblank_event(crtc, event);
  235. else
  236. drm_crtc_send_vblank_event(crtc, event);
  237. spin_unlock_irq(&crtc->dev->event_lock);
  238. }
  239. }
  240. static int tve200_display_enable_vblank(struct drm_simple_display_pipe *pipe)
  241. {
  242. struct drm_crtc *crtc = &pipe->crtc;
  243. struct drm_device *drm = crtc->dev;
  244. struct tve200_drm_dev_private *priv = drm->dev_private;
  245. writel(TVE200_INT_V_STATUS, priv->regs + TVE200_INT_EN);
  246. return 0;
  247. }
  248. static void tve200_display_disable_vblank(struct drm_simple_display_pipe *pipe)
  249. {
  250. struct drm_crtc *crtc = &pipe->crtc;
  251. struct drm_device *drm = crtc->dev;
  252. struct tve200_drm_dev_private *priv = drm->dev_private;
  253. writel(0, priv->regs + TVE200_INT_EN);
  254. }
  255. static int tve200_display_prepare_fb(struct drm_simple_display_pipe *pipe,
  256. struct drm_plane_state *plane_state)
  257. {
  258. return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
  259. }
  260. static const struct drm_simple_display_pipe_funcs tve200_display_funcs = {
  261. .check = tve200_display_check,
  262. .enable = tve200_display_enable,
  263. .disable = tve200_display_disable,
  264. .update = tve200_display_update,
  265. .prepare_fb = tve200_display_prepare_fb,
  266. .enable_vblank = tve200_display_enable_vblank,
  267. .disable_vblank = tve200_display_disable_vblank,
  268. };
  269. int tve200_display_init(struct drm_device *drm)
  270. {
  271. struct tve200_drm_dev_private *priv = drm->dev_private;
  272. int ret;
  273. static const u32 formats[] = {
  274. DRM_FORMAT_XRGB8888,
  275. DRM_FORMAT_XBGR8888,
  276. DRM_FORMAT_RGB565,
  277. DRM_FORMAT_BGR565,
  278. DRM_FORMAT_XRGB1555,
  279. DRM_FORMAT_XBGR1555,
  280. /*
  281. * The controller actually supports any YCbCr ordering,
  282. * for packed YCbCr. This just lists the orderings that
  283. * DRM supports.
  284. */
  285. DRM_FORMAT_YUYV,
  286. DRM_FORMAT_YVYU,
  287. DRM_FORMAT_UYVY,
  288. DRM_FORMAT_VYUY,
  289. /* This uses three planes */
  290. DRM_FORMAT_YUV420,
  291. };
  292. ret = drm_simple_display_pipe_init(drm, &priv->pipe,
  293. &tve200_display_funcs,
  294. formats, ARRAY_SIZE(formats),
  295. NULL,
  296. priv->connector);
  297. if (ret)
  298. return ret;
  299. return 0;
  300. }