exynos7_drm_decon.c 20 KB

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