exynos7_drm_decon.c 20 KB

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