tve200_display.c 10.0 KB

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