exynos7_drm_decon.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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 "exynos_drm_crtc.h"
  27. #include "exynos_drm_plane.h"
  28. #include "exynos_drm_drv.h"
  29. #include "exynos_drm_fb.h"
  30. #include "exynos_drm_iommu.h"
  31. #include "regs-decon7.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. default:
  257. val |= WINCONx_BPPMODE_32BPP_BGRA | WINCONx_BLD_PIX |
  258. WINCONx_ALPHA_SEL;
  259. val |= WINCONx_BURSTLEN_16WORD;
  260. break;
  261. }
  262. DRM_DEBUG_KMS("cpp = %d\n", fb->format->cpp[0]);
  263. /*
  264. * In case of exynos, setting dma-burst to 16Word causes permanent
  265. * tearing for very small buffers, e.g. cursor buffer. Burst Mode
  266. * switching which is based on plane size is not recommended as
  267. * plane size varies a lot towards the end of the screen and rapid
  268. * movement causes unstable DMA which results into iommu crash/tear.
  269. */
  270. padding = (fb->pitches[0] / fb->format->cpp[0]) - fb->width;
  271. if (fb->width + padding < MIN_FB_WIDTH_FOR_16WORD_BURST) {
  272. val &= ~WINCONx_BURSTLEN_MASK;
  273. val |= WINCONx_BURSTLEN_8WORD;
  274. }
  275. writel(val, ctx->regs + WINCON(win));
  276. }
  277. static void decon_win_set_colkey(struct decon_context *ctx, unsigned int win)
  278. {
  279. unsigned int keycon0 = 0, keycon1 = 0;
  280. keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
  281. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  282. keycon1 = WxKEYCON1_COLVAL(0xffffffff);
  283. writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
  284. writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
  285. }
  286. /**
  287. * shadow_protect_win() - disable updating values from shadow registers at vsync
  288. *
  289. * @win: window to protect registers for
  290. * @protect: 1 to protect (disable updates)
  291. */
  292. static void decon_shadow_protect_win(struct decon_context *ctx,
  293. unsigned int win, bool protect)
  294. {
  295. u32 bits, val;
  296. bits = SHADOWCON_WINx_PROTECT(win);
  297. val = readl(ctx->regs + SHADOWCON);
  298. if (protect)
  299. val |= bits;
  300. else
  301. val &= ~bits;
  302. writel(val, ctx->regs + SHADOWCON);
  303. }
  304. static void decon_atomic_begin(struct exynos_drm_crtc *crtc)
  305. {
  306. struct decon_context *ctx = crtc->ctx;
  307. int i;
  308. if (ctx->suspended)
  309. return;
  310. for (i = 0; i < WINDOWS_NR; i++)
  311. decon_shadow_protect_win(ctx, i, true);
  312. }
  313. static void decon_update_plane(struct exynos_drm_crtc *crtc,
  314. struct exynos_drm_plane *plane)
  315. {
  316. struct exynos_drm_plane_state *state =
  317. to_exynos_plane_state(plane->base.state);
  318. struct decon_context *ctx = crtc->ctx;
  319. struct drm_framebuffer *fb = state->base.fb;
  320. int padding;
  321. unsigned long val, alpha;
  322. unsigned int last_x;
  323. unsigned int last_y;
  324. unsigned int win = plane->index;
  325. unsigned int cpp = fb->format->cpp[0];
  326. unsigned int pitch = fb->pitches[0];
  327. if (ctx->suspended)
  328. return;
  329. /*
  330. * SHADOWCON/PRTCON register is used for enabling timing.
  331. *
  332. * for example, once only width value of a register is set,
  333. * if the dma is started then decon hardware could malfunction so
  334. * with protect window setting, the register fields with prefix '_F'
  335. * wouldn't be updated at vsync also but updated once unprotect window
  336. * is set.
  337. */
  338. /* buffer start address */
  339. val = (unsigned long)exynos_drm_fb_dma_addr(fb, 0);
  340. writel(val, ctx->regs + VIDW_BUF_START(win));
  341. padding = (pitch / cpp) - fb->width;
  342. /* buffer size */
  343. writel(fb->width + padding, ctx->regs + VIDW_WHOLE_X(win));
  344. writel(fb->height, ctx->regs + VIDW_WHOLE_Y(win));
  345. /* offset from the start of the buffer to read */
  346. writel(state->src.x, ctx->regs + VIDW_OFFSET_X(win));
  347. writel(state->src.y, ctx->regs + VIDW_OFFSET_Y(win));
  348. DRM_DEBUG_KMS("start addr = 0x%lx\n",
  349. (unsigned long)val);
  350. DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
  351. state->crtc.w, state->crtc.h);
  352. val = VIDOSDxA_TOPLEFT_X(state->crtc.x) |
  353. VIDOSDxA_TOPLEFT_Y(state->crtc.y);
  354. writel(val, ctx->regs + VIDOSD_A(win));
  355. last_x = state->crtc.x + state->crtc.w;
  356. if (last_x)
  357. last_x--;
  358. last_y = state->crtc.y + state->crtc.h;
  359. if (last_y)
  360. last_y--;
  361. val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y);
  362. writel(val, ctx->regs + VIDOSD_B(win));
  363. DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
  364. state->crtc.x, state->crtc.y, last_x, last_y);
  365. /* OSD alpha */
  366. alpha = VIDOSDxC_ALPHA0_R_F(0x0) |
  367. VIDOSDxC_ALPHA0_G_F(0x0) |
  368. VIDOSDxC_ALPHA0_B_F(0x0);
  369. writel(alpha, ctx->regs + VIDOSD_C(win));
  370. alpha = VIDOSDxD_ALPHA1_R_F(0xff) |
  371. VIDOSDxD_ALPHA1_G_F(0xff) |
  372. VIDOSDxD_ALPHA1_B_F(0xff);
  373. writel(alpha, ctx->regs + VIDOSD_D(win));
  374. decon_win_set_pixfmt(ctx, win, fb);
  375. /* hardware window 0 doesn't support color key. */
  376. if (win != 0)
  377. decon_win_set_colkey(ctx, win);
  378. /* wincon */
  379. val = readl(ctx->regs + WINCON(win));
  380. val |= WINCONx_TRIPLE_BUF_MODE;
  381. val |= WINCONx_ENWIN;
  382. writel(val, ctx->regs + WINCON(win));
  383. /* Enable DMA channel and unprotect windows */
  384. decon_shadow_protect_win(ctx, win, false);
  385. val = readl(ctx->regs + DECON_UPDATE);
  386. val |= DECON_UPDATE_STANDALONE_F;
  387. writel(val, ctx->regs + DECON_UPDATE);
  388. }
  389. static void decon_disable_plane(struct exynos_drm_crtc *crtc,
  390. struct exynos_drm_plane *plane)
  391. {
  392. struct decon_context *ctx = crtc->ctx;
  393. unsigned int win = plane->index;
  394. u32 val;
  395. if (ctx->suspended)
  396. return;
  397. /* protect windows */
  398. decon_shadow_protect_win(ctx, win, true);
  399. /* wincon */
  400. val = readl(ctx->regs + WINCON(win));
  401. val &= ~WINCONx_ENWIN;
  402. writel(val, ctx->regs + WINCON(win));
  403. val = readl(ctx->regs + DECON_UPDATE);
  404. val |= DECON_UPDATE_STANDALONE_F;
  405. writel(val, ctx->regs + DECON_UPDATE);
  406. }
  407. static void decon_atomic_flush(struct exynos_drm_crtc *crtc)
  408. {
  409. struct decon_context *ctx = crtc->ctx;
  410. int i;
  411. if (ctx->suspended)
  412. return;
  413. for (i = 0; i < WINDOWS_NR; i++)
  414. decon_shadow_protect_win(ctx, i, false);
  415. exynos_crtc_handle_event(crtc);
  416. }
  417. static void decon_init(struct decon_context *ctx)
  418. {
  419. u32 val;
  420. writel(VIDCON0_SWRESET, ctx->regs + VIDCON0);
  421. val = VIDOUTCON0_DISP_IF_0_ON;
  422. if (!ctx->i80_if)
  423. val |= VIDOUTCON0_RGBIF;
  424. writel(val, ctx->regs + VIDOUTCON0);
  425. writel(VCLKCON0_CLKVALUP | VCLKCON0_VCLKFREE, ctx->regs + VCLKCON0);
  426. if (!ctx->i80_if)
  427. writel(VIDCON1_VCLK_HOLD, ctx->regs + VIDCON1(0));
  428. }
  429. static void decon_enable(struct exynos_drm_crtc *crtc)
  430. {
  431. struct decon_context *ctx = crtc->ctx;
  432. if (!ctx->suspended)
  433. return;
  434. pm_runtime_get_sync(ctx->dev);
  435. decon_init(ctx);
  436. /* if vblank was enabled status, enable it again. */
  437. if (test_and_clear_bit(0, &ctx->irq_flags))
  438. decon_enable_vblank(ctx->crtc);
  439. decon_commit(ctx->crtc);
  440. ctx->suspended = false;
  441. }
  442. static void decon_disable(struct exynos_drm_crtc *crtc)
  443. {
  444. struct decon_context *ctx = crtc->ctx;
  445. int i;
  446. if (ctx->suspended)
  447. return;
  448. /*
  449. * We need to make sure that all windows are disabled before we
  450. * suspend that connector. Otherwise we might try to scan from
  451. * a destroyed buffer later.
  452. */
  453. for (i = 0; i < WINDOWS_NR; i++)
  454. decon_disable_plane(crtc, &ctx->planes[i]);
  455. pm_runtime_put_sync(ctx->dev);
  456. ctx->suspended = true;
  457. }
  458. static const struct exynos_drm_crtc_ops decon_crtc_ops = {
  459. .enable = decon_enable,
  460. .disable = decon_disable,
  461. .enable_vblank = decon_enable_vblank,
  462. .disable_vblank = decon_disable_vblank,
  463. .atomic_begin = decon_atomic_begin,
  464. .update_plane = decon_update_plane,
  465. .disable_plane = decon_disable_plane,
  466. .atomic_flush = decon_atomic_flush,
  467. };
  468. static irqreturn_t decon_irq_handler(int irq, void *dev_id)
  469. {
  470. struct decon_context *ctx = (struct decon_context *)dev_id;
  471. u32 val, clear_bit;
  472. val = readl(ctx->regs + VIDINTCON1);
  473. clear_bit = ctx->i80_if ? VIDINTCON1_INT_I80 : VIDINTCON1_INT_FRAME;
  474. if (val & clear_bit)
  475. writel(clear_bit, ctx->regs + VIDINTCON1);
  476. /* check the crtc is detached already from encoder */
  477. if (!ctx->drm_dev)
  478. goto out;
  479. if (!ctx->i80_if) {
  480. drm_crtc_handle_vblank(&ctx->crtc->base);
  481. /* set wait vsync event to zero and wake up queue. */
  482. if (atomic_read(&ctx->wait_vsync_event)) {
  483. atomic_set(&ctx->wait_vsync_event, 0);
  484. wake_up(&ctx->wait_vsync_queue);
  485. }
  486. }
  487. out:
  488. return IRQ_HANDLED;
  489. }
  490. static int decon_bind(struct device *dev, struct device *master, void *data)
  491. {
  492. struct decon_context *ctx = dev_get_drvdata(dev);
  493. struct drm_device *drm_dev = data;
  494. struct exynos_drm_plane *exynos_plane;
  495. unsigned int i;
  496. int ret;
  497. ret = decon_ctx_initialize(ctx, drm_dev);
  498. if (ret) {
  499. DRM_ERROR("decon_ctx_initialize failed.\n");
  500. return ret;
  501. }
  502. for (i = 0; i < WINDOWS_NR; i++) {
  503. ctx->configs[i].pixel_formats = decon_formats;
  504. ctx->configs[i].num_pixel_formats = ARRAY_SIZE(decon_formats);
  505. ctx->configs[i].zpos = i;
  506. ctx->configs[i].type = decon_win_types[i];
  507. ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
  508. &ctx->configs[i]);
  509. if (ret)
  510. return ret;
  511. }
  512. exynos_plane = &ctx->planes[DEFAULT_WIN];
  513. ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
  514. EXYNOS_DISPLAY_TYPE_LCD, &decon_crtc_ops, ctx);
  515. if (IS_ERR(ctx->crtc)) {
  516. decon_ctx_remove(ctx);
  517. return PTR_ERR(ctx->crtc);
  518. }
  519. if (ctx->encoder)
  520. exynos_dpi_bind(drm_dev, ctx->encoder);
  521. return 0;
  522. }
  523. static void decon_unbind(struct device *dev, struct device *master,
  524. void *data)
  525. {
  526. struct decon_context *ctx = dev_get_drvdata(dev);
  527. decon_disable(ctx->crtc);
  528. if (ctx->encoder)
  529. exynos_dpi_remove(ctx->encoder);
  530. decon_ctx_remove(ctx);
  531. }
  532. static const struct component_ops decon_component_ops = {
  533. .bind = decon_bind,
  534. .unbind = decon_unbind,
  535. };
  536. static int decon_probe(struct platform_device *pdev)
  537. {
  538. struct device *dev = &pdev->dev;
  539. struct decon_context *ctx;
  540. struct device_node *i80_if_timings;
  541. struct resource *res;
  542. int ret;
  543. if (!dev->of_node)
  544. return -ENODEV;
  545. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  546. if (!ctx)
  547. return -ENOMEM;
  548. ctx->dev = dev;
  549. ctx->suspended = true;
  550. i80_if_timings = of_get_child_by_name(dev->of_node, "i80-if-timings");
  551. if (i80_if_timings)
  552. ctx->i80_if = true;
  553. of_node_put(i80_if_timings);
  554. ctx->regs = of_iomap(dev->of_node, 0);
  555. if (!ctx->regs)
  556. return -ENOMEM;
  557. ctx->pclk = devm_clk_get(dev, "pclk_decon0");
  558. if (IS_ERR(ctx->pclk)) {
  559. dev_err(dev, "failed to get bus clock pclk\n");
  560. ret = PTR_ERR(ctx->pclk);
  561. goto err_iounmap;
  562. }
  563. ctx->aclk = devm_clk_get(dev, "aclk_decon0");
  564. if (IS_ERR(ctx->aclk)) {
  565. dev_err(dev, "failed to get bus clock aclk\n");
  566. ret = PTR_ERR(ctx->aclk);
  567. goto err_iounmap;
  568. }
  569. ctx->eclk = devm_clk_get(dev, "decon0_eclk");
  570. if (IS_ERR(ctx->eclk)) {
  571. dev_err(dev, "failed to get eclock\n");
  572. ret = PTR_ERR(ctx->eclk);
  573. goto err_iounmap;
  574. }
  575. ctx->vclk = devm_clk_get(dev, "decon0_vclk");
  576. if (IS_ERR(ctx->vclk)) {
  577. dev_err(dev, "failed to get vclock\n");
  578. ret = PTR_ERR(ctx->vclk);
  579. goto err_iounmap;
  580. }
  581. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  582. ctx->i80_if ? "lcd_sys" : "vsync");
  583. if (!res) {
  584. dev_err(dev, "irq request failed.\n");
  585. ret = -ENXIO;
  586. goto err_iounmap;
  587. }
  588. ret = devm_request_irq(dev, res->start, decon_irq_handler,
  589. 0, "drm_decon", ctx);
  590. if (ret) {
  591. dev_err(dev, "irq request failed.\n");
  592. goto err_iounmap;
  593. }
  594. init_waitqueue_head(&ctx->wait_vsync_queue);
  595. atomic_set(&ctx->wait_vsync_event, 0);
  596. platform_set_drvdata(pdev, ctx);
  597. ctx->encoder = exynos_dpi_probe(dev);
  598. if (IS_ERR(ctx->encoder)) {
  599. ret = PTR_ERR(ctx->encoder);
  600. goto err_iounmap;
  601. }
  602. pm_runtime_enable(dev);
  603. ret = component_add(dev, &decon_component_ops);
  604. if (ret)
  605. goto err_disable_pm_runtime;
  606. return ret;
  607. err_disable_pm_runtime:
  608. pm_runtime_disable(dev);
  609. err_iounmap:
  610. iounmap(ctx->regs);
  611. return ret;
  612. }
  613. static int decon_remove(struct platform_device *pdev)
  614. {
  615. struct decon_context *ctx = dev_get_drvdata(&pdev->dev);
  616. pm_runtime_disable(&pdev->dev);
  617. iounmap(ctx->regs);
  618. component_del(&pdev->dev, &decon_component_ops);
  619. return 0;
  620. }
  621. #ifdef CONFIG_PM
  622. static int exynos7_decon_suspend(struct device *dev)
  623. {
  624. struct decon_context *ctx = dev_get_drvdata(dev);
  625. clk_disable_unprepare(ctx->vclk);
  626. clk_disable_unprepare(ctx->eclk);
  627. clk_disable_unprepare(ctx->aclk);
  628. clk_disable_unprepare(ctx->pclk);
  629. return 0;
  630. }
  631. static int exynos7_decon_resume(struct device *dev)
  632. {
  633. struct decon_context *ctx = dev_get_drvdata(dev);
  634. int ret;
  635. ret = clk_prepare_enable(ctx->pclk);
  636. if (ret < 0) {
  637. DRM_ERROR("Failed to prepare_enable the pclk [%d]\n", ret);
  638. return ret;
  639. }
  640. ret = clk_prepare_enable(ctx->aclk);
  641. if (ret < 0) {
  642. DRM_ERROR("Failed to prepare_enable the aclk [%d]\n", ret);
  643. return ret;
  644. }
  645. ret = clk_prepare_enable(ctx->eclk);
  646. if (ret < 0) {
  647. DRM_ERROR("Failed to prepare_enable the eclk [%d]\n", ret);
  648. return ret;
  649. }
  650. ret = clk_prepare_enable(ctx->vclk);
  651. if (ret < 0) {
  652. DRM_ERROR("Failed to prepare_enable the vclk [%d]\n", ret);
  653. return ret;
  654. }
  655. return 0;
  656. }
  657. #endif
  658. static const struct dev_pm_ops exynos7_decon_pm_ops = {
  659. SET_RUNTIME_PM_OPS(exynos7_decon_suspend, exynos7_decon_resume,
  660. NULL)
  661. };
  662. struct platform_driver decon_driver = {
  663. .probe = decon_probe,
  664. .remove = decon_remove,
  665. .driver = {
  666. .name = "exynos-decon",
  667. .pm = &exynos7_decon_pm_ops,
  668. .of_match_table = decon_driver_dt_match,
  669. },
  670. };