exynos7_drm_decon.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /* drivers/gpu/drm/exynos/exynos7_drm_decon.c
  2. *
  3. * Copyright (C) 2014 Samsung Electronics Co.Ltd
  4. * Authors:
  5. * Akshu Agarwal <akshua@gmail.com>
  6. * Ajay Kumar <ajaykumar.rs@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/exynos_drm.h>
  16. #include <linux/clk.h>
  17. #include <linux/component.h>
  18. #include <linux/kernel.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <video/of_display_timing.h>
  25. #include <video/of_videomode.h>
  26. #include <video/exynos7_decon.h>
  27. #include "exynos_drm_crtc.h"
  28. #include "exynos_drm_plane.h"
  29. #include "exynos_drm_drv.h"
  30. #include "exynos_drm_fb.h"
  31. #include "exynos_drm_fbdev.h"
  32. #include "exynos_drm_iommu.h"
  33. /*
  34. * DECON stands for Display and Enhancement controller.
  35. */
  36. #define MIN_FB_WIDTH_FOR_16WORD_BURST 128
  37. #define WINDOWS_NR 2
  38. struct decon_context {
  39. struct device *dev;
  40. struct drm_device *drm_dev;
  41. struct exynos_drm_crtc *crtc;
  42. struct exynos_drm_plane planes[WINDOWS_NR];
  43. struct exynos_drm_plane_config configs[WINDOWS_NR];
  44. struct clk *pclk;
  45. struct clk *aclk;
  46. struct clk *eclk;
  47. struct clk *vclk;
  48. void __iomem *regs;
  49. unsigned long irq_flags;
  50. bool i80_if;
  51. bool suspended;
  52. int pipe;
  53. wait_queue_head_t wait_vsync_queue;
  54. atomic_t wait_vsync_event;
  55. struct exynos_drm_panel_info panel;
  56. struct drm_encoder *encoder;
  57. };
  58. static const struct of_device_id decon_driver_dt_match[] = {
  59. {.compatible = "samsung,exynos7-decon"},
  60. {},
  61. };
  62. MODULE_DEVICE_TABLE(of, decon_driver_dt_match);
  63. static const uint32_t decon_formats[] = {
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_RGBX8888,
  68. DRM_FORMAT_BGRX8888,
  69. DRM_FORMAT_ARGB8888,
  70. DRM_FORMAT_ABGR8888,
  71. DRM_FORMAT_RGBA8888,
  72. DRM_FORMAT_BGRA8888,
  73. };
  74. static const enum drm_plane_type decon_win_types[WINDOWS_NR] = {
  75. DRM_PLANE_TYPE_PRIMARY,
  76. DRM_PLANE_TYPE_CURSOR,
  77. };
  78. static void decon_wait_for_vblank(struct exynos_drm_crtc *crtc)
  79. {
  80. struct decon_context *ctx = crtc->ctx;
  81. if (ctx->suspended)
  82. return;
  83. atomic_set(&ctx->wait_vsync_event, 1);
  84. /*
  85. * wait for DECON to signal VSYNC interrupt or return after
  86. * timeout which is set to 50ms (refresh rate of 20).
  87. */
  88. if (!wait_event_timeout(ctx->wait_vsync_queue,
  89. !atomic_read(&ctx->wait_vsync_event),
  90. HZ/20))
  91. DRM_DEBUG_KMS("vblank wait timed out.\n");
  92. }
  93. static void decon_clear_channels(struct exynos_drm_crtc *crtc)
  94. {
  95. struct decon_context *ctx = crtc->ctx;
  96. unsigned int win, ch_enabled = 0;
  97. DRM_DEBUG_KMS("%s\n", __FILE__);
  98. /* Check if any channel is enabled. */
  99. for (win = 0; win < WINDOWS_NR; win++) {
  100. u32 val = readl(ctx->regs + WINCON(win));
  101. if (val & WINCONx_ENWIN) {
  102. val &= ~WINCONx_ENWIN;
  103. writel(val, ctx->regs + WINCON(win));
  104. ch_enabled = 1;
  105. }
  106. }
  107. /* Wait for vsync, as disable channel takes effect at next vsync */
  108. if (ch_enabled)
  109. decon_wait_for_vblank(ctx->crtc);
  110. }
  111. static int decon_ctx_initialize(struct decon_context *ctx,
  112. struct drm_device *drm_dev)
  113. {
  114. struct exynos_drm_private *priv = drm_dev->dev_private;
  115. int ret;
  116. ctx->drm_dev = drm_dev;
  117. ctx->pipe = priv->pipe++;
  118. decon_clear_channels(ctx->crtc);
  119. ret = drm_iommu_attach_device(drm_dev, ctx->dev);
  120. if (ret)
  121. priv->pipe--;
  122. return ret;
  123. }
  124. static void decon_ctx_remove(struct decon_context *ctx)
  125. {
  126. /* detach this sub driver from iommu mapping if supported. */
  127. drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
  128. }
  129. static u32 decon_calc_clkdiv(struct decon_context *ctx,
  130. const struct drm_display_mode *mode)
  131. {
  132. unsigned long ideal_clk = mode->htotal * mode->vtotal * mode->vrefresh;
  133. u32 clkdiv;
  134. /* Find the clock divider value that gets us closest to ideal_clk */
  135. clkdiv = DIV_ROUND_UP(clk_get_rate(ctx->vclk), ideal_clk);
  136. return (clkdiv < 0x100) ? clkdiv : 0xff;
  137. }
  138. static void decon_commit(struct exynos_drm_crtc *crtc)
  139. {
  140. struct decon_context *ctx = crtc->ctx;
  141. struct drm_display_mode *mode = &crtc->base.state->adjusted_mode;
  142. u32 val, clkdiv;
  143. if (ctx->suspended)
  144. return;
  145. /* nothing to do if we haven't set the mode yet */
  146. if (mode->htotal == 0 || mode->vtotal == 0)
  147. return;
  148. if (!ctx->i80_if) {
  149. int vsync_len, vbpd, vfpd, hsync_len, hbpd, hfpd;
  150. /* setup vertical timing values. */
  151. vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
  152. vbpd = mode->crtc_vtotal - mode->crtc_vsync_end;
  153. vfpd = mode->crtc_vsync_start - mode->crtc_vdisplay;
  154. val = VIDTCON0_VBPD(vbpd - 1) | VIDTCON0_VFPD(vfpd - 1);
  155. writel(val, ctx->regs + VIDTCON0);
  156. val = VIDTCON1_VSPW(vsync_len - 1);
  157. writel(val, ctx->regs + VIDTCON1);
  158. /* setup horizontal timing values. */
  159. hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
  160. hbpd = mode->crtc_htotal - mode->crtc_hsync_end;
  161. hfpd = mode->crtc_hsync_start - mode->crtc_hdisplay;
  162. /* setup horizontal timing values. */
  163. val = VIDTCON2_HBPD(hbpd - 1) | VIDTCON2_HFPD(hfpd - 1);
  164. writel(val, ctx->regs + VIDTCON2);
  165. val = VIDTCON3_HSPW(hsync_len - 1);
  166. writel(val, ctx->regs + VIDTCON3);
  167. }
  168. /* setup horizontal and vertical display size. */
  169. val = VIDTCON4_LINEVAL(mode->vdisplay - 1) |
  170. VIDTCON4_HOZVAL(mode->hdisplay - 1);
  171. writel(val, ctx->regs + VIDTCON4);
  172. writel(mode->vdisplay - 1, ctx->regs + LINECNT_OP_THRESHOLD);
  173. /*
  174. * fields of register with prefix '_F' would be updated
  175. * at vsync(same as dma start)
  176. */
  177. val = VIDCON0_ENVID | VIDCON0_ENVID_F;
  178. writel(val, ctx->regs + VIDCON0);
  179. clkdiv = decon_calc_clkdiv(ctx, mode);
  180. if (clkdiv > 1) {
  181. val = VCLKCON1_CLKVAL_NUM_VCLK(clkdiv - 1);
  182. writel(val, ctx->regs + VCLKCON1);
  183. writel(val, ctx->regs + VCLKCON2);
  184. }
  185. val = readl(ctx->regs + DECON_UPDATE);
  186. val |= DECON_UPDATE_STANDALONE_F;
  187. writel(val, ctx->regs + DECON_UPDATE);
  188. }
  189. static int decon_enable_vblank(struct exynos_drm_crtc *crtc)
  190. {
  191. struct decon_context *ctx = crtc->ctx;
  192. u32 val;
  193. if (ctx->suspended)
  194. return -EPERM;
  195. if (!test_and_set_bit(0, &ctx->irq_flags)) {
  196. val = readl(ctx->regs + VIDINTCON0);
  197. val |= VIDINTCON0_INT_ENABLE;
  198. if (!ctx->i80_if) {
  199. val |= VIDINTCON0_INT_FRAME;
  200. val &= ~VIDINTCON0_FRAMESEL0_MASK;
  201. val |= VIDINTCON0_FRAMESEL0_VSYNC;
  202. }
  203. writel(val, ctx->regs + VIDINTCON0);
  204. }
  205. return 0;
  206. }
  207. static void decon_disable_vblank(struct exynos_drm_crtc *crtc)
  208. {
  209. struct decon_context *ctx = crtc->ctx;
  210. u32 val;
  211. if (ctx->suspended)
  212. return;
  213. if (test_and_clear_bit(0, &ctx->irq_flags)) {
  214. val = readl(ctx->regs + VIDINTCON0);
  215. val &= ~VIDINTCON0_INT_ENABLE;
  216. if (!ctx->i80_if)
  217. val &= ~VIDINTCON0_INT_FRAME;
  218. writel(val, ctx->regs + VIDINTCON0);
  219. }
  220. }
  221. static void decon_win_set_pixfmt(struct decon_context *ctx, unsigned int win,
  222. struct drm_framebuffer *fb)
  223. {
  224. unsigned long val;
  225. int padding;
  226. val = readl(ctx->regs + WINCON(win));
  227. val &= ~WINCONx_BPPMODE_MASK;
  228. switch (fb->pixel_format) {
  229. case DRM_FORMAT_RGB565:
  230. val |= WINCONx_BPPMODE_16BPP_565;
  231. val |= WINCONx_BURSTLEN_16WORD;
  232. break;
  233. case DRM_FORMAT_XRGB8888:
  234. val |= WINCONx_BPPMODE_24BPP_xRGB;
  235. val |= WINCONx_BURSTLEN_16WORD;
  236. break;
  237. case DRM_FORMAT_XBGR8888:
  238. val |= WINCONx_BPPMODE_24BPP_xBGR;
  239. val |= WINCONx_BURSTLEN_16WORD;
  240. break;
  241. case DRM_FORMAT_RGBX8888:
  242. val |= WINCONx_BPPMODE_24BPP_RGBx;
  243. val |= WINCONx_BURSTLEN_16WORD;
  244. break;
  245. case DRM_FORMAT_BGRX8888:
  246. val |= WINCONx_BPPMODE_24BPP_BGRx;
  247. val |= WINCONx_BURSTLEN_16WORD;
  248. break;
  249. case DRM_FORMAT_ARGB8888:
  250. val |= WINCONx_BPPMODE_32BPP_ARGB | WINCONx_BLD_PIX |
  251. WINCONx_ALPHA_SEL;
  252. val |= WINCONx_BURSTLEN_16WORD;
  253. break;
  254. case DRM_FORMAT_ABGR8888:
  255. val |= WINCONx_BPPMODE_32BPP_ABGR | WINCONx_BLD_PIX |
  256. WINCONx_ALPHA_SEL;
  257. val |= WINCONx_BURSTLEN_16WORD;
  258. break;
  259. case DRM_FORMAT_RGBA8888:
  260. val |= WINCONx_BPPMODE_32BPP_RGBA | WINCONx_BLD_PIX |
  261. WINCONx_ALPHA_SEL;
  262. val |= WINCONx_BURSTLEN_16WORD;
  263. break;
  264. case DRM_FORMAT_BGRA8888:
  265. val |= WINCONx_BPPMODE_32BPP_BGRA | WINCONx_BLD_PIX |
  266. WINCONx_ALPHA_SEL;
  267. val |= WINCONx_BURSTLEN_16WORD;
  268. break;
  269. default:
  270. DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
  271. val |= WINCONx_BPPMODE_24BPP_xRGB;
  272. val |= WINCONx_BURSTLEN_16WORD;
  273. break;
  274. }
  275. DRM_DEBUG_KMS("bpp = %d\n", fb->bits_per_pixel);
  276. /*
  277. * In case of exynos, setting dma-burst to 16Word causes permanent
  278. * tearing for very small buffers, e.g. cursor buffer. Burst Mode
  279. * switching which is based on plane size is not recommended as
  280. * plane size varies a lot towards the end of the screen and rapid
  281. * movement causes unstable DMA which results into iommu crash/tear.
  282. */
  283. padding = (fb->pitches[0] / (fb->bits_per_pixel >> 3)) - fb->width;
  284. if (fb->width + padding < MIN_FB_WIDTH_FOR_16WORD_BURST) {
  285. val &= ~WINCONx_BURSTLEN_MASK;
  286. val |= WINCONx_BURSTLEN_8WORD;
  287. }
  288. writel(val, ctx->regs + WINCON(win));
  289. }
  290. static void decon_win_set_colkey(struct decon_context *ctx, unsigned int win)
  291. {
  292. unsigned int keycon0 = 0, keycon1 = 0;
  293. keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
  294. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  295. keycon1 = WxKEYCON1_COLVAL(0xffffffff);
  296. writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
  297. writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
  298. }
  299. /**
  300. * shadow_protect_win() - disable updating values from shadow registers at vsync
  301. *
  302. * @win: window to protect registers for
  303. * @protect: 1 to protect (disable updates)
  304. */
  305. static void decon_shadow_protect_win(struct decon_context *ctx,
  306. unsigned int win, bool protect)
  307. {
  308. u32 bits, val;
  309. bits = SHADOWCON_WINx_PROTECT(win);
  310. val = readl(ctx->regs + SHADOWCON);
  311. if (protect)
  312. val |= bits;
  313. else
  314. val &= ~bits;
  315. writel(val, ctx->regs + SHADOWCON);
  316. }
  317. static void decon_atomic_begin(struct exynos_drm_crtc *crtc)
  318. {
  319. struct decon_context *ctx = crtc->ctx;
  320. int i;
  321. if (ctx->suspended)
  322. return;
  323. for (i = 0; i < WINDOWS_NR; i++)
  324. decon_shadow_protect_win(ctx, i, true);
  325. }
  326. static void decon_update_plane(struct exynos_drm_crtc *crtc,
  327. struct exynos_drm_plane *plane)
  328. {
  329. struct exynos_drm_plane_state *state =
  330. to_exynos_plane_state(plane->base.state);
  331. struct decon_context *ctx = crtc->ctx;
  332. struct drm_framebuffer *fb = state->base.fb;
  333. int padding;
  334. unsigned long val, alpha;
  335. unsigned int last_x;
  336. unsigned int last_y;
  337. unsigned int win = plane->index;
  338. unsigned int bpp = fb->bits_per_pixel >> 3;
  339. unsigned int pitch = fb->pitches[0];
  340. if (ctx->suspended)
  341. return;
  342. /*
  343. * SHADOWCON/PRTCON register is used for enabling timing.
  344. *
  345. * for example, once only width value of a register is set,
  346. * if the dma is started then decon hardware could malfunction so
  347. * with protect window setting, the register fields with prefix '_F'
  348. * wouldn't be updated at vsync also but updated once unprotect window
  349. * is set.
  350. */
  351. /* buffer start address */
  352. val = (unsigned long)exynos_drm_fb_dma_addr(fb, 0);
  353. writel(val, ctx->regs + VIDW_BUF_START(win));
  354. padding = (pitch / bpp) - fb->width;
  355. /* buffer size */
  356. writel(fb->width + padding, ctx->regs + VIDW_WHOLE_X(win));
  357. writel(fb->height, ctx->regs + VIDW_WHOLE_Y(win));
  358. /* offset from the start of the buffer to read */
  359. writel(state->src.x, ctx->regs + VIDW_OFFSET_X(win));
  360. writel(state->src.y, ctx->regs + VIDW_OFFSET_Y(win));
  361. DRM_DEBUG_KMS("start addr = 0x%lx\n",
  362. (unsigned long)val);
  363. DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
  364. state->crtc.w, state->crtc.h);
  365. val = VIDOSDxA_TOPLEFT_X(state->crtc.x) |
  366. VIDOSDxA_TOPLEFT_Y(state->crtc.y);
  367. writel(val, ctx->regs + VIDOSD_A(win));
  368. last_x = state->crtc.x + state->crtc.w;
  369. if (last_x)
  370. last_x--;
  371. last_y = state->crtc.y + state->crtc.h;
  372. if (last_y)
  373. last_y--;
  374. val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y);
  375. writel(val, ctx->regs + VIDOSD_B(win));
  376. DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
  377. state->crtc.x, state->crtc.y, last_x, last_y);
  378. /* OSD alpha */
  379. alpha = VIDOSDxC_ALPHA0_R_F(0x0) |
  380. VIDOSDxC_ALPHA0_G_F(0x0) |
  381. VIDOSDxC_ALPHA0_B_F(0x0);
  382. writel(alpha, ctx->regs + VIDOSD_C(win));
  383. alpha = VIDOSDxD_ALPHA1_R_F(0xff) |
  384. VIDOSDxD_ALPHA1_G_F(0xff) |
  385. VIDOSDxD_ALPHA1_B_F(0xff);
  386. writel(alpha, ctx->regs + VIDOSD_D(win));
  387. decon_win_set_pixfmt(ctx, win, fb);
  388. /* hardware window 0 doesn't support color key. */
  389. if (win != 0)
  390. decon_win_set_colkey(ctx, win);
  391. /* wincon */
  392. val = readl(ctx->regs + WINCON(win));
  393. val |= WINCONx_TRIPLE_BUF_MODE;
  394. val |= WINCONx_ENWIN;
  395. writel(val, ctx->regs + WINCON(win));
  396. /* Enable DMA channel and unprotect windows */
  397. decon_shadow_protect_win(ctx, win, false);
  398. val = readl(ctx->regs + DECON_UPDATE);
  399. val |= DECON_UPDATE_STANDALONE_F;
  400. writel(val, ctx->regs + DECON_UPDATE);
  401. }
  402. static void decon_disable_plane(struct exynos_drm_crtc *crtc,
  403. struct exynos_drm_plane *plane)
  404. {
  405. struct decon_context *ctx = crtc->ctx;
  406. unsigned int win = plane->index;
  407. u32 val;
  408. if (ctx->suspended)
  409. return;
  410. /* protect windows */
  411. decon_shadow_protect_win(ctx, win, true);
  412. /* wincon */
  413. val = readl(ctx->regs + WINCON(win));
  414. val &= ~WINCONx_ENWIN;
  415. writel(val, ctx->regs + WINCON(win));
  416. val = readl(ctx->regs + DECON_UPDATE);
  417. val |= DECON_UPDATE_STANDALONE_F;
  418. writel(val, ctx->regs + DECON_UPDATE);
  419. }
  420. static void decon_atomic_flush(struct exynos_drm_crtc *crtc)
  421. {
  422. struct decon_context *ctx = crtc->ctx;
  423. int i;
  424. if (ctx->suspended)
  425. return;
  426. for (i = 0; i < WINDOWS_NR; i++)
  427. decon_shadow_protect_win(ctx, i, false);
  428. }
  429. static void decon_init(struct decon_context *ctx)
  430. {
  431. u32 val;
  432. writel(VIDCON0_SWRESET, ctx->regs + VIDCON0);
  433. val = VIDOUTCON0_DISP_IF_0_ON;
  434. if (!ctx->i80_if)
  435. val |= VIDOUTCON0_RGBIF;
  436. writel(val, ctx->regs + VIDOUTCON0);
  437. writel(VCLKCON0_CLKVALUP | VCLKCON0_VCLKFREE, ctx->regs + VCLKCON0);
  438. if (!ctx->i80_if)
  439. writel(VIDCON1_VCLK_HOLD, ctx->regs + VIDCON1(0));
  440. }
  441. static void decon_enable(struct exynos_drm_crtc *crtc)
  442. {
  443. struct decon_context *ctx = crtc->ctx;
  444. if (!ctx->suspended)
  445. return;
  446. pm_runtime_get_sync(ctx->dev);
  447. decon_init(ctx);
  448. /* if vblank was enabled status, enable it again. */
  449. if (test_and_clear_bit(0, &ctx->irq_flags))
  450. decon_enable_vblank(ctx->crtc);
  451. decon_commit(ctx->crtc);
  452. ctx->suspended = false;
  453. }
  454. static void decon_disable(struct exynos_drm_crtc *crtc)
  455. {
  456. struct decon_context *ctx = crtc->ctx;
  457. int i;
  458. if (ctx->suspended)
  459. return;
  460. /*
  461. * We need to make sure that all windows are disabled before we
  462. * suspend that connector. Otherwise we might try to scan from
  463. * a destroyed buffer later.
  464. */
  465. for (i = 0; i < WINDOWS_NR; i++)
  466. decon_disable_plane(crtc, &ctx->planes[i]);
  467. pm_runtime_put_sync(ctx->dev);
  468. ctx->suspended = true;
  469. }
  470. static const struct exynos_drm_crtc_ops decon_crtc_ops = {
  471. .enable = decon_enable,
  472. .disable = decon_disable,
  473. .commit = decon_commit,
  474. .enable_vblank = decon_enable_vblank,
  475. .disable_vblank = decon_disable_vblank,
  476. .wait_for_vblank = decon_wait_for_vblank,
  477. .atomic_begin = decon_atomic_begin,
  478. .update_plane = decon_update_plane,
  479. .disable_plane = decon_disable_plane,
  480. .atomic_flush = decon_atomic_flush,
  481. };
  482. static irqreturn_t decon_irq_handler(int irq, void *dev_id)
  483. {
  484. struct decon_context *ctx = (struct decon_context *)dev_id;
  485. u32 val, clear_bit;
  486. int win;
  487. val = readl(ctx->regs + VIDINTCON1);
  488. clear_bit = ctx->i80_if ? VIDINTCON1_INT_I80 : VIDINTCON1_INT_FRAME;
  489. if (val & clear_bit)
  490. writel(clear_bit, ctx->regs + VIDINTCON1);
  491. /* check the crtc is detached already from encoder */
  492. if (ctx->pipe < 0 || !ctx->drm_dev)
  493. goto out;
  494. if (!ctx->i80_if) {
  495. drm_crtc_handle_vblank(&ctx->crtc->base);
  496. for (win = 0 ; win < WINDOWS_NR ; win++) {
  497. struct exynos_drm_plane *plane = &ctx->planes[win];
  498. if (!plane->pending_fb)
  499. continue;
  500. exynos_drm_crtc_finish_update(ctx->crtc, plane);
  501. }
  502. /* set wait vsync event to zero and wake up queue. */
  503. if (atomic_read(&ctx->wait_vsync_event)) {
  504. atomic_set(&ctx->wait_vsync_event, 0);
  505. wake_up(&ctx->wait_vsync_queue);
  506. }
  507. }
  508. out:
  509. return IRQ_HANDLED;
  510. }
  511. static int decon_bind(struct device *dev, struct device *master, void *data)
  512. {
  513. struct decon_context *ctx = dev_get_drvdata(dev);
  514. struct drm_device *drm_dev = data;
  515. struct exynos_drm_plane *exynos_plane;
  516. unsigned int i;
  517. int ret;
  518. ret = decon_ctx_initialize(ctx, drm_dev);
  519. if (ret) {
  520. DRM_ERROR("decon_ctx_initialize failed.\n");
  521. return ret;
  522. }
  523. for (i = 0; i < WINDOWS_NR; i++) {
  524. ctx->configs[i].pixel_formats = decon_formats;
  525. ctx->configs[i].num_pixel_formats = ARRAY_SIZE(decon_formats);
  526. ctx->configs[i].zpos = i;
  527. ctx->configs[i].type = decon_win_types[i];
  528. ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
  529. 1 << ctx->pipe, &ctx->configs[i]);
  530. if (ret)
  531. return ret;
  532. }
  533. exynos_plane = &ctx->planes[DEFAULT_WIN];
  534. ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
  535. ctx->pipe, EXYNOS_DISPLAY_TYPE_LCD,
  536. &decon_crtc_ops, ctx);
  537. if (IS_ERR(ctx->crtc)) {
  538. decon_ctx_remove(ctx);
  539. return PTR_ERR(ctx->crtc);
  540. }
  541. if (ctx->encoder)
  542. exynos_dpi_bind(drm_dev, ctx->encoder);
  543. return 0;
  544. }
  545. static void decon_unbind(struct device *dev, struct device *master,
  546. void *data)
  547. {
  548. struct decon_context *ctx = dev_get_drvdata(dev);
  549. decon_disable(ctx->crtc);
  550. if (ctx->encoder)
  551. exynos_dpi_remove(ctx->encoder);
  552. decon_ctx_remove(ctx);
  553. }
  554. static const struct component_ops decon_component_ops = {
  555. .bind = decon_bind,
  556. .unbind = decon_unbind,
  557. };
  558. static int decon_probe(struct platform_device *pdev)
  559. {
  560. struct device *dev = &pdev->dev;
  561. struct decon_context *ctx;
  562. struct device_node *i80_if_timings;
  563. struct resource *res;
  564. int ret;
  565. if (!dev->of_node)
  566. return -ENODEV;
  567. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  568. if (!ctx)
  569. return -ENOMEM;
  570. ctx->dev = dev;
  571. ctx->suspended = true;
  572. i80_if_timings = of_get_child_by_name(dev->of_node, "i80-if-timings");
  573. if (i80_if_timings)
  574. ctx->i80_if = true;
  575. of_node_put(i80_if_timings);
  576. ctx->regs = of_iomap(dev->of_node, 0);
  577. if (!ctx->regs)
  578. return -ENOMEM;
  579. ctx->pclk = devm_clk_get(dev, "pclk_decon0");
  580. if (IS_ERR(ctx->pclk)) {
  581. dev_err(dev, "failed to get bus clock pclk\n");
  582. ret = PTR_ERR(ctx->pclk);
  583. goto err_iounmap;
  584. }
  585. ctx->aclk = devm_clk_get(dev, "aclk_decon0");
  586. if (IS_ERR(ctx->aclk)) {
  587. dev_err(dev, "failed to get bus clock aclk\n");
  588. ret = PTR_ERR(ctx->aclk);
  589. goto err_iounmap;
  590. }
  591. ctx->eclk = devm_clk_get(dev, "decon0_eclk");
  592. if (IS_ERR(ctx->eclk)) {
  593. dev_err(dev, "failed to get eclock\n");
  594. ret = PTR_ERR(ctx->eclk);
  595. goto err_iounmap;
  596. }
  597. ctx->vclk = devm_clk_get(dev, "decon0_vclk");
  598. if (IS_ERR(ctx->vclk)) {
  599. dev_err(dev, "failed to get vclock\n");
  600. ret = PTR_ERR(ctx->vclk);
  601. goto err_iounmap;
  602. }
  603. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  604. ctx->i80_if ? "lcd_sys" : "vsync");
  605. if (!res) {
  606. dev_err(dev, "irq request failed.\n");
  607. ret = -ENXIO;
  608. goto err_iounmap;
  609. }
  610. ret = devm_request_irq(dev, res->start, decon_irq_handler,
  611. 0, "drm_decon", ctx);
  612. if (ret) {
  613. dev_err(dev, "irq request failed.\n");
  614. goto err_iounmap;
  615. }
  616. init_waitqueue_head(&ctx->wait_vsync_queue);
  617. atomic_set(&ctx->wait_vsync_event, 0);
  618. platform_set_drvdata(pdev, ctx);
  619. ctx->encoder = exynos_dpi_probe(dev);
  620. if (IS_ERR(ctx->encoder)) {
  621. ret = PTR_ERR(ctx->encoder);
  622. goto err_iounmap;
  623. }
  624. pm_runtime_enable(dev);
  625. ret = component_add(dev, &decon_component_ops);
  626. if (ret)
  627. goto err_disable_pm_runtime;
  628. return ret;
  629. err_disable_pm_runtime:
  630. pm_runtime_disable(dev);
  631. err_iounmap:
  632. iounmap(ctx->regs);
  633. return ret;
  634. }
  635. static int decon_remove(struct platform_device *pdev)
  636. {
  637. struct decon_context *ctx = dev_get_drvdata(&pdev->dev);
  638. pm_runtime_disable(&pdev->dev);
  639. iounmap(ctx->regs);
  640. component_del(&pdev->dev, &decon_component_ops);
  641. return 0;
  642. }
  643. #ifdef CONFIG_PM
  644. static int exynos7_decon_suspend(struct device *dev)
  645. {
  646. struct decon_context *ctx = dev_get_drvdata(dev);
  647. clk_disable_unprepare(ctx->vclk);
  648. clk_disable_unprepare(ctx->eclk);
  649. clk_disable_unprepare(ctx->aclk);
  650. clk_disable_unprepare(ctx->pclk);
  651. return 0;
  652. }
  653. static int exynos7_decon_resume(struct device *dev)
  654. {
  655. struct decon_context *ctx = dev_get_drvdata(dev);
  656. int ret;
  657. ret = clk_prepare_enable(ctx->pclk);
  658. if (ret < 0) {
  659. DRM_ERROR("Failed to prepare_enable the pclk [%d]\n", ret);
  660. return ret;
  661. }
  662. ret = clk_prepare_enable(ctx->aclk);
  663. if (ret < 0) {
  664. DRM_ERROR("Failed to prepare_enable the aclk [%d]\n", ret);
  665. return ret;
  666. }
  667. ret = clk_prepare_enable(ctx->eclk);
  668. if (ret < 0) {
  669. DRM_ERROR("Failed to prepare_enable the eclk [%d]\n", ret);
  670. return ret;
  671. }
  672. ret = clk_prepare_enable(ctx->vclk);
  673. if (ret < 0) {
  674. DRM_ERROR("Failed to prepare_enable the vclk [%d]\n", ret);
  675. return ret;
  676. }
  677. return 0;
  678. }
  679. #endif
  680. static const struct dev_pm_ops exynos7_decon_pm_ops = {
  681. SET_RUNTIME_PM_OPS(exynos7_decon_suspend, exynos7_decon_resume,
  682. NULL)
  683. };
  684. struct platform_driver decon_driver = {
  685. .probe = decon_probe,
  686. .remove = decon_remove,
  687. .driver = {
  688. .name = "exynos-decon",
  689. .pm = &exynos7_decon_pm_ops,
  690. .of_match_table = decon_driver_dt_match,
  691. },
  692. };