tve200_display.c 10 KB

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