exynos7_drm_decon.c 21 KB

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