exynos7_drm_decon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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_drv.h"
  29. #include "exynos_drm_fbdev.h"
  30. #include "exynos_drm_iommu.h"
  31. /*
  32. * DECON stands for Display and Enhancement controller.
  33. */
  34. #define DECON_DEFAULT_FRAMERATE 60
  35. #define MIN_FB_WIDTH_FOR_16WORD_BURST 128
  36. #define WINDOWS_NR 2
  37. struct decon_win_data {
  38. unsigned int ovl_x;
  39. unsigned int ovl_y;
  40. unsigned int offset_x;
  41. unsigned int offset_y;
  42. unsigned int ovl_width;
  43. unsigned int ovl_height;
  44. unsigned int fb_width;
  45. unsigned int fb_height;
  46. unsigned int bpp;
  47. unsigned int pixel_format;
  48. dma_addr_t dma_addr;
  49. bool enabled;
  50. bool resume;
  51. };
  52. struct decon_context {
  53. struct device *dev;
  54. struct drm_device *drm_dev;
  55. struct exynos_drm_crtc *crtc;
  56. struct clk *pclk;
  57. struct clk *aclk;
  58. struct clk *eclk;
  59. struct clk *vclk;
  60. void __iomem *regs;
  61. struct decon_win_data win_data[WINDOWS_NR];
  62. unsigned int default_win;
  63. unsigned long irq_flags;
  64. bool i80_if;
  65. bool suspended;
  66. int pipe;
  67. wait_queue_head_t wait_vsync_queue;
  68. atomic_t wait_vsync_event;
  69. struct exynos_drm_panel_info panel;
  70. struct exynos_drm_display *display;
  71. };
  72. static const struct of_device_id decon_driver_dt_match[] = {
  73. {.compatible = "samsung,exynos7-decon"},
  74. {},
  75. };
  76. MODULE_DEVICE_TABLE(of, decon_driver_dt_match);
  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_channel(struct decon_context *ctx)
  93. {
  94. 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. unsigned int state = ctx->suspended;
  108. ctx->suspended = 0;
  109. decon_wait_for_vblank(ctx->crtc);
  110. ctx->suspended = state;
  111. }
  112. }
  113. static int decon_ctx_initialize(struct decon_context *ctx,
  114. struct drm_device *drm_dev)
  115. {
  116. struct exynos_drm_private *priv = drm_dev->dev_private;
  117. ctx->drm_dev = drm_dev;
  118. ctx->pipe = priv->pipe++;
  119. /* attach this sub driver to iommu mapping if supported. */
  120. if (is_drm_iommu_supported(ctx->drm_dev)) {
  121. int ret;
  122. /*
  123. * If any channel is already active, iommu will throw
  124. * a PAGE FAULT when enabled. So clear any channel if enabled.
  125. */
  126. decon_clear_channel(ctx);
  127. ret = drm_iommu_attach_device(ctx->drm_dev, ctx->dev);
  128. if (ret) {
  129. DRM_ERROR("drm_iommu_attach failed.\n");
  130. return ret;
  131. }
  132. }
  133. return 0;
  134. }
  135. static void decon_ctx_remove(struct decon_context *ctx)
  136. {
  137. /* detach this sub driver from iommu mapping if supported. */
  138. if (is_drm_iommu_supported(ctx->drm_dev))
  139. drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
  140. }
  141. static u32 decon_calc_clkdiv(struct decon_context *ctx,
  142. const struct drm_display_mode *mode)
  143. {
  144. unsigned long ideal_clk = mode->htotal * mode->vtotal * mode->vrefresh;
  145. u32 clkdiv;
  146. /* Find the clock divider value that gets us closest to ideal_clk */
  147. clkdiv = DIV_ROUND_UP(clk_get_rate(ctx->vclk), ideal_clk);
  148. return (clkdiv < 0x100) ? clkdiv : 0xff;
  149. }
  150. static bool decon_mode_fixup(struct exynos_drm_crtc *crtc,
  151. const struct drm_display_mode *mode,
  152. struct drm_display_mode *adjusted_mode)
  153. {
  154. if (adjusted_mode->vrefresh == 0)
  155. adjusted_mode->vrefresh = DECON_DEFAULT_FRAMERATE;
  156. return true;
  157. }
  158. static void decon_commit(struct exynos_drm_crtc *crtc)
  159. {
  160. struct decon_context *ctx = crtc->ctx;
  161. struct drm_display_mode *mode = &crtc->base.mode;
  162. u32 val, clkdiv;
  163. if (ctx->suspended)
  164. return;
  165. /* nothing to do if we haven't set the mode yet */
  166. if (mode->htotal == 0 || mode->vtotal == 0)
  167. return;
  168. if (!ctx->i80_if) {
  169. int vsync_len, vbpd, vfpd, hsync_len, hbpd, hfpd;
  170. /* setup vertical timing values. */
  171. vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
  172. vbpd = mode->crtc_vtotal - mode->crtc_vsync_end;
  173. vfpd = mode->crtc_vsync_start - mode->crtc_vdisplay;
  174. val = VIDTCON0_VBPD(vbpd - 1) | VIDTCON0_VFPD(vfpd - 1);
  175. writel(val, ctx->regs + VIDTCON0);
  176. val = VIDTCON1_VSPW(vsync_len - 1);
  177. writel(val, ctx->regs + VIDTCON1);
  178. /* setup horizontal timing values. */
  179. hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
  180. hbpd = mode->crtc_htotal - mode->crtc_hsync_end;
  181. hfpd = mode->crtc_hsync_start - mode->crtc_hdisplay;
  182. /* setup horizontal timing values. */
  183. val = VIDTCON2_HBPD(hbpd - 1) | VIDTCON2_HFPD(hfpd - 1);
  184. writel(val, ctx->regs + VIDTCON2);
  185. val = VIDTCON3_HSPW(hsync_len - 1);
  186. writel(val, ctx->regs + VIDTCON3);
  187. }
  188. /* setup horizontal and vertical display size. */
  189. val = VIDTCON4_LINEVAL(mode->vdisplay - 1) |
  190. VIDTCON4_HOZVAL(mode->hdisplay - 1);
  191. writel(val, ctx->regs + VIDTCON4);
  192. writel(mode->vdisplay - 1, ctx->regs + LINECNT_OP_THRESHOLD);
  193. /*
  194. * fields of register with prefix '_F' would be updated
  195. * at vsync(same as dma start)
  196. */
  197. val = VIDCON0_ENVID | VIDCON0_ENVID_F;
  198. writel(val, ctx->regs + VIDCON0);
  199. clkdiv = decon_calc_clkdiv(ctx, mode);
  200. if (clkdiv > 1) {
  201. val = VCLKCON1_CLKVAL_NUM_VCLK(clkdiv - 1);
  202. writel(val, ctx->regs + VCLKCON1);
  203. writel(val, ctx->regs + VCLKCON2);
  204. }
  205. val = readl(ctx->regs + DECON_UPDATE);
  206. val |= DECON_UPDATE_STANDALONE_F;
  207. writel(val, ctx->regs + DECON_UPDATE);
  208. }
  209. static int decon_enable_vblank(struct exynos_drm_crtc *crtc)
  210. {
  211. struct decon_context *ctx = crtc->ctx;
  212. u32 val;
  213. if (ctx->suspended)
  214. return -EPERM;
  215. if (!test_and_set_bit(0, &ctx->irq_flags)) {
  216. val = readl(ctx->regs + VIDINTCON0);
  217. val |= VIDINTCON0_INT_ENABLE;
  218. if (!ctx->i80_if) {
  219. val |= VIDINTCON0_INT_FRAME;
  220. val &= ~VIDINTCON0_FRAMESEL0_MASK;
  221. val |= VIDINTCON0_FRAMESEL0_VSYNC;
  222. }
  223. writel(val, ctx->regs + VIDINTCON0);
  224. }
  225. return 0;
  226. }
  227. static void decon_disable_vblank(struct exynos_drm_crtc *crtc)
  228. {
  229. struct decon_context *ctx = crtc->ctx;
  230. u32 val;
  231. if (ctx->suspended)
  232. return;
  233. if (test_and_clear_bit(0, &ctx->irq_flags)) {
  234. val = readl(ctx->regs + VIDINTCON0);
  235. val &= ~VIDINTCON0_INT_ENABLE;
  236. if (!ctx->i80_if)
  237. val &= ~VIDINTCON0_INT_FRAME;
  238. writel(val, ctx->regs + VIDINTCON0);
  239. }
  240. }
  241. static void decon_win_mode_set(struct exynos_drm_crtc *crtc,
  242. struct exynos_drm_plane *plane)
  243. {
  244. struct decon_context *ctx = crtc->ctx;
  245. struct decon_win_data *win_data;
  246. int win, padding;
  247. if (!plane) {
  248. DRM_ERROR("plane is NULL\n");
  249. return;
  250. }
  251. win = plane->zpos;
  252. if (win == DEFAULT_ZPOS)
  253. win = ctx->default_win;
  254. if (win < 0 || win >= WINDOWS_NR)
  255. return;
  256. win_data = &ctx->win_data[win];
  257. padding = (plane->pitch / (plane->bpp >> 3)) - plane->fb_width;
  258. win_data->offset_x = plane->fb_x;
  259. win_data->offset_y = plane->fb_y;
  260. win_data->fb_width = plane->fb_width + padding;
  261. win_data->fb_height = plane->fb_height;
  262. win_data->ovl_x = plane->crtc_x;
  263. win_data->ovl_y = plane->crtc_y;
  264. win_data->ovl_width = plane->crtc_width;
  265. win_data->ovl_height = plane->crtc_height;
  266. win_data->dma_addr = plane->dma_addr[0];
  267. win_data->bpp = plane->bpp;
  268. win_data->pixel_format = plane->pixel_format;
  269. DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
  270. win_data->offset_x, win_data->offset_y);
  271. DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
  272. win_data->ovl_width, win_data->ovl_height);
  273. DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
  274. DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
  275. plane->fb_width, plane->crtc_width);
  276. }
  277. static void decon_win_set_pixfmt(struct decon_context *ctx, unsigned int win)
  278. {
  279. struct decon_win_data *win_data = &ctx->win_data[win];
  280. unsigned long val;
  281. val = readl(ctx->regs + WINCON(win));
  282. val &= ~WINCONx_BPPMODE_MASK;
  283. switch (win_data->pixel_format) {
  284. case DRM_FORMAT_RGB565:
  285. val |= WINCONx_BPPMODE_16BPP_565;
  286. val |= WINCONx_BURSTLEN_16WORD;
  287. break;
  288. case DRM_FORMAT_XRGB8888:
  289. val |= WINCONx_BPPMODE_24BPP_xRGB;
  290. val |= WINCONx_BURSTLEN_16WORD;
  291. break;
  292. case DRM_FORMAT_XBGR8888:
  293. val |= WINCONx_BPPMODE_24BPP_xBGR;
  294. val |= WINCONx_BURSTLEN_16WORD;
  295. break;
  296. case DRM_FORMAT_RGBX8888:
  297. val |= WINCONx_BPPMODE_24BPP_RGBx;
  298. val |= WINCONx_BURSTLEN_16WORD;
  299. break;
  300. case DRM_FORMAT_BGRX8888:
  301. val |= WINCONx_BPPMODE_24BPP_BGRx;
  302. val |= WINCONx_BURSTLEN_16WORD;
  303. break;
  304. case DRM_FORMAT_ARGB8888:
  305. val |= WINCONx_BPPMODE_32BPP_ARGB | WINCONx_BLD_PIX |
  306. WINCONx_ALPHA_SEL;
  307. val |= WINCONx_BURSTLEN_16WORD;
  308. break;
  309. case DRM_FORMAT_ABGR8888:
  310. val |= WINCONx_BPPMODE_32BPP_ABGR | WINCONx_BLD_PIX |
  311. WINCONx_ALPHA_SEL;
  312. val |= WINCONx_BURSTLEN_16WORD;
  313. break;
  314. case DRM_FORMAT_RGBA8888:
  315. val |= WINCONx_BPPMODE_32BPP_RGBA | WINCONx_BLD_PIX |
  316. WINCONx_ALPHA_SEL;
  317. val |= WINCONx_BURSTLEN_16WORD;
  318. break;
  319. case DRM_FORMAT_BGRA8888:
  320. val |= WINCONx_BPPMODE_32BPP_BGRA | WINCONx_BLD_PIX |
  321. WINCONx_ALPHA_SEL;
  322. val |= WINCONx_BURSTLEN_16WORD;
  323. break;
  324. default:
  325. DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
  326. val |= WINCONx_BPPMODE_24BPP_xRGB;
  327. val |= WINCONx_BURSTLEN_16WORD;
  328. break;
  329. }
  330. DRM_DEBUG_KMS("bpp = %d\n", win_data->bpp);
  331. /*
  332. * In case of exynos, setting dma-burst to 16Word causes permanent
  333. * tearing for very small buffers, e.g. cursor buffer. Burst Mode
  334. * switching which is based on plane size is not recommended as
  335. * plane size varies a lot towards the end of the screen and rapid
  336. * movement causes unstable DMA which results into iommu crash/tear.
  337. */
  338. if (win_data->fb_width < MIN_FB_WIDTH_FOR_16WORD_BURST) {
  339. val &= ~WINCONx_BURSTLEN_MASK;
  340. val |= WINCONx_BURSTLEN_8WORD;
  341. }
  342. writel(val, ctx->regs + WINCON(win));
  343. }
  344. static void decon_win_set_colkey(struct decon_context *ctx, unsigned int win)
  345. {
  346. unsigned int keycon0 = 0, keycon1 = 0;
  347. keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
  348. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  349. keycon1 = WxKEYCON1_COLVAL(0xffffffff);
  350. writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
  351. writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
  352. }
  353. /**
  354. * shadow_protect_win() - disable updating values from shadow registers at vsync
  355. *
  356. * @win: window to protect registers for
  357. * @protect: 1 to protect (disable updates)
  358. */
  359. static void decon_shadow_protect_win(struct decon_context *ctx,
  360. int win, bool protect)
  361. {
  362. u32 bits, val;
  363. bits = SHADOWCON_WINx_PROTECT(win);
  364. val = readl(ctx->regs + SHADOWCON);
  365. if (protect)
  366. val |= bits;
  367. else
  368. val &= ~bits;
  369. writel(val, ctx->regs + SHADOWCON);
  370. }
  371. static void decon_win_commit(struct exynos_drm_crtc *crtc, int zpos)
  372. {
  373. struct decon_context *ctx = crtc->ctx;
  374. struct drm_display_mode *mode = &crtc->base.mode;
  375. struct decon_win_data *win_data;
  376. int win = zpos;
  377. unsigned long val, alpha;
  378. unsigned int last_x;
  379. unsigned int last_y;
  380. if (ctx->suspended)
  381. return;
  382. if (win == DEFAULT_ZPOS)
  383. win = ctx->default_win;
  384. if (win < 0 || win >= WINDOWS_NR)
  385. return;
  386. win_data = &ctx->win_data[win];
  387. /* If suspended, enable this on resume */
  388. if (ctx->suspended) {
  389. win_data->resume = true;
  390. return;
  391. }
  392. /*
  393. * SHADOWCON/PRTCON register is used for enabling timing.
  394. *
  395. * for example, once only width value of a register is set,
  396. * if the dma is started then decon hardware could malfunction so
  397. * with protect window setting, the register fields with prefix '_F'
  398. * wouldn't be updated at vsync also but updated once unprotect window
  399. * is set.
  400. */
  401. /* protect windows */
  402. decon_shadow_protect_win(ctx, win, true);
  403. /* buffer start address */
  404. val = (unsigned long)win_data->dma_addr;
  405. writel(val, ctx->regs + VIDW_BUF_START(win));
  406. /* buffer size */
  407. writel(win_data->fb_width, ctx->regs + VIDW_WHOLE_X(win));
  408. writel(win_data->fb_height, ctx->regs + VIDW_WHOLE_Y(win));
  409. /* offset from the start of the buffer to read */
  410. writel(win_data->offset_x, ctx->regs + VIDW_OFFSET_X(win));
  411. writel(win_data->offset_y, ctx->regs + VIDW_OFFSET_Y(win));
  412. DRM_DEBUG_KMS("start addr = 0x%lx\n",
  413. (unsigned long)win_data->dma_addr);
  414. DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
  415. win_data->ovl_width, win_data->ovl_height);
  416. /*
  417. * OSD position.
  418. * In case the window layout goes of LCD layout, DECON fails.
  419. */
  420. if ((win_data->ovl_x + win_data->ovl_width) > mode->hdisplay)
  421. win_data->ovl_x = mode->hdisplay - win_data->ovl_width;
  422. if ((win_data->ovl_y + win_data->ovl_height) > mode->vdisplay)
  423. win_data->ovl_y = mode->vdisplay - win_data->ovl_height;
  424. val = VIDOSDxA_TOPLEFT_X(win_data->ovl_x) |
  425. VIDOSDxA_TOPLEFT_Y(win_data->ovl_y);
  426. writel(val, ctx->regs + VIDOSD_A(win));
  427. last_x = win_data->ovl_x + win_data->ovl_width;
  428. if (last_x)
  429. last_x--;
  430. last_y = win_data->ovl_y + win_data->ovl_height;
  431. if (last_y)
  432. last_y--;
  433. val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y);
  434. writel(val, ctx->regs + VIDOSD_B(win));
  435. DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
  436. win_data->ovl_x, win_data->ovl_y, last_x, last_y);
  437. /* OSD alpha */
  438. alpha = VIDOSDxC_ALPHA0_R_F(0x0) |
  439. VIDOSDxC_ALPHA0_G_F(0x0) |
  440. VIDOSDxC_ALPHA0_B_F(0x0);
  441. writel(alpha, ctx->regs + VIDOSD_C(win));
  442. alpha = VIDOSDxD_ALPHA1_R_F(0xff) |
  443. VIDOSDxD_ALPHA1_G_F(0xff) |
  444. VIDOSDxD_ALPHA1_B_F(0xff);
  445. writel(alpha, ctx->regs + VIDOSD_D(win));
  446. decon_win_set_pixfmt(ctx, win);
  447. /* hardware window 0 doesn't support color key. */
  448. if (win != 0)
  449. decon_win_set_colkey(ctx, win);
  450. /* wincon */
  451. val = readl(ctx->regs + WINCON(win));
  452. val |= WINCONx_TRIPLE_BUF_MODE;
  453. val |= WINCONx_ENWIN;
  454. writel(val, ctx->regs + WINCON(win));
  455. /* Enable DMA channel and unprotect windows */
  456. decon_shadow_protect_win(ctx, win, false);
  457. val = readl(ctx->regs + DECON_UPDATE);
  458. val |= DECON_UPDATE_STANDALONE_F;
  459. writel(val, ctx->regs + DECON_UPDATE);
  460. win_data->enabled = true;
  461. }
  462. static void decon_win_disable(struct exynos_drm_crtc *crtc, int zpos)
  463. {
  464. struct decon_context *ctx = crtc->ctx;
  465. struct decon_win_data *win_data;
  466. int win = zpos;
  467. u32 val;
  468. if (win == DEFAULT_ZPOS)
  469. win = ctx->default_win;
  470. if (win < 0 || win >= WINDOWS_NR)
  471. return;
  472. win_data = &ctx->win_data[win];
  473. if (ctx->suspended) {
  474. /* do not resume this window*/
  475. win_data->resume = false;
  476. return;
  477. }
  478. /* protect windows */
  479. decon_shadow_protect_win(ctx, win, true);
  480. /* wincon */
  481. val = readl(ctx->regs + WINCON(win));
  482. val &= ~WINCONx_ENWIN;
  483. writel(val, ctx->regs + WINCON(win));
  484. /* unprotect windows */
  485. decon_shadow_protect_win(ctx, win, false);
  486. val = readl(ctx->regs + DECON_UPDATE);
  487. val |= DECON_UPDATE_STANDALONE_F;
  488. writel(val, ctx->regs + DECON_UPDATE);
  489. win_data->enabled = false;
  490. }
  491. static void decon_window_suspend(struct decon_context *ctx)
  492. {
  493. struct decon_win_data *win_data;
  494. int i;
  495. for (i = 0; i < WINDOWS_NR; i++) {
  496. win_data = &ctx->win_data[i];
  497. win_data->resume = win_data->enabled;
  498. if (win_data->enabled)
  499. decon_win_disable(ctx->crtc, i);
  500. }
  501. }
  502. static void decon_window_resume(struct decon_context *ctx)
  503. {
  504. struct decon_win_data *win_data;
  505. int i;
  506. for (i = 0; i < WINDOWS_NR; i++) {
  507. win_data = &ctx->win_data[i];
  508. win_data->enabled = win_data->resume;
  509. win_data->resume = false;
  510. }
  511. }
  512. static void decon_apply(struct decon_context *ctx)
  513. {
  514. struct decon_win_data *win_data;
  515. int i;
  516. for (i = 0; i < WINDOWS_NR; i++) {
  517. win_data = &ctx->win_data[i];
  518. if (win_data->enabled)
  519. decon_win_commit(ctx->crtc, i);
  520. else
  521. decon_win_disable(ctx->crtc, i);
  522. }
  523. decon_commit(ctx->crtc);
  524. }
  525. static void decon_init(struct decon_context *ctx)
  526. {
  527. u32 val;
  528. writel(VIDCON0_SWRESET, ctx->regs + VIDCON0);
  529. val = VIDOUTCON0_DISP_IF_0_ON;
  530. if (!ctx->i80_if)
  531. val |= VIDOUTCON0_RGBIF;
  532. writel(val, ctx->regs + VIDOUTCON0);
  533. writel(VCLKCON0_CLKVALUP | VCLKCON0_VCLKFREE, ctx->regs + VCLKCON0);
  534. if (!ctx->i80_if)
  535. writel(VIDCON1_VCLK_HOLD, ctx->regs + VIDCON1(0));
  536. }
  537. static int decon_poweron(struct decon_context *ctx)
  538. {
  539. int ret;
  540. if (!ctx->suspended)
  541. return 0;
  542. ctx->suspended = false;
  543. pm_runtime_get_sync(ctx->dev);
  544. ret = clk_prepare_enable(ctx->pclk);
  545. if (ret < 0) {
  546. DRM_ERROR("Failed to prepare_enable the pclk [%d]\n", ret);
  547. goto pclk_err;
  548. }
  549. ret = clk_prepare_enable(ctx->aclk);
  550. if (ret < 0) {
  551. DRM_ERROR("Failed to prepare_enable the aclk [%d]\n", ret);
  552. goto aclk_err;
  553. }
  554. ret = clk_prepare_enable(ctx->eclk);
  555. if (ret < 0) {
  556. DRM_ERROR("Failed to prepare_enable the eclk [%d]\n", ret);
  557. goto eclk_err;
  558. }
  559. ret = clk_prepare_enable(ctx->vclk);
  560. if (ret < 0) {
  561. DRM_ERROR("Failed to prepare_enable the vclk [%d]\n", ret);
  562. goto vclk_err;
  563. }
  564. decon_init(ctx);
  565. /* if vblank was enabled status, enable it again. */
  566. if (test_and_clear_bit(0, &ctx->irq_flags)) {
  567. ret = decon_enable_vblank(ctx->crtc);
  568. if (ret) {
  569. DRM_ERROR("Failed to re-enable vblank [%d]\n", ret);
  570. goto err;
  571. }
  572. }
  573. decon_window_resume(ctx);
  574. decon_apply(ctx);
  575. return 0;
  576. err:
  577. clk_disable_unprepare(ctx->vclk);
  578. vclk_err:
  579. clk_disable_unprepare(ctx->eclk);
  580. eclk_err:
  581. clk_disable_unprepare(ctx->aclk);
  582. aclk_err:
  583. clk_disable_unprepare(ctx->pclk);
  584. pclk_err:
  585. ctx->suspended = true;
  586. return ret;
  587. }
  588. static int decon_poweroff(struct decon_context *ctx)
  589. {
  590. if (ctx->suspended)
  591. return 0;
  592. /*
  593. * We need to make sure that all windows are disabled before we
  594. * suspend that connector. Otherwise we might try to scan from
  595. * a destroyed buffer later.
  596. */
  597. decon_window_suspend(ctx);
  598. clk_disable_unprepare(ctx->vclk);
  599. clk_disable_unprepare(ctx->eclk);
  600. clk_disable_unprepare(ctx->aclk);
  601. clk_disable_unprepare(ctx->pclk);
  602. pm_runtime_put_sync(ctx->dev);
  603. ctx->suspended = true;
  604. return 0;
  605. }
  606. static void decon_dpms(struct exynos_drm_crtc *crtc, int mode)
  607. {
  608. DRM_DEBUG_KMS("%s, %d\n", __FILE__, mode);
  609. switch (mode) {
  610. case DRM_MODE_DPMS_ON:
  611. decon_poweron(crtc->ctx);
  612. break;
  613. case DRM_MODE_DPMS_STANDBY:
  614. case DRM_MODE_DPMS_SUSPEND:
  615. case DRM_MODE_DPMS_OFF:
  616. decon_poweroff(crtc->ctx);
  617. break;
  618. default:
  619. DRM_DEBUG_KMS("unspecified mode %d\n", mode);
  620. break;
  621. }
  622. }
  623. static struct exynos_drm_crtc_ops decon_crtc_ops = {
  624. .dpms = decon_dpms,
  625. .mode_fixup = decon_mode_fixup,
  626. .commit = decon_commit,
  627. .enable_vblank = decon_enable_vblank,
  628. .disable_vblank = decon_disable_vblank,
  629. .wait_for_vblank = decon_wait_for_vblank,
  630. .win_mode_set = decon_win_mode_set,
  631. .win_commit = decon_win_commit,
  632. .win_disable = decon_win_disable,
  633. };
  634. static irqreturn_t decon_irq_handler(int irq, void *dev_id)
  635. {
  636. struct decon_context *ctx = (struct decon_context *)dev_id;
  637. u32 val, clear_bit;
  638. val = readl(ctx->regs + VIDINTCON1);
  639. clear_bit = ctx->i80_if ? VIDINTCON1_INT_I80 : VIDINTCON1_INT_FRAME;
  640. if (val & clear_bit)
  641. writel(clear_bit, ctx->regs + VIDINTCON1);
  642. /* check the crtc is detached already from encoder */
  643. if (ctx->pipe < 0 || !ctx->drm_dev)
  644. goto out;
  645. if (!ctx->i80_if) {
  646. drm_handle_vblank(ctx->drm_dev, ctx->pipe);
  647. exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
  648. /* set wait vsync event to zero and wake up queue. */
  649. if (atomic_read(&ctx->wait_vsync_event)) {
  650. atomic_set(&ctx->wait_vsync_event, 0);
  651. wake_up(&ctx->wait_vsync_queue);
  652. }
  653. }
  654. out:
  655. return IRQ_HANDLED;
  656. }
  657. static int decon_bind(struct device *dev, struct device *master, void *data)
  658. {
  659. struct decon_context *ctx = dev_get_drvdata(dev);
  660. struct drm_device *drm_dev = data;
  661. int ret;
  662. ret = decon_ctx_initialize(ctx, drm_dev);
  663. if (ret) {
  664. DRM_ERROR("decon_ctx_initialize failed.\n");
  665. return ret;
  666. }
  667. ctx->crtc = exynos_drm_crtc_create(drm_dev, ctx->pipe,
  668. EXYNOS_DISPLAY_TYPE_LCD,
  669. &decon_crtc_ops, ctx);
  670. if (IS_ERR(ctx->crtc)) {
  671. decon_ctx_remove(ctx);
  672. return PTR_ERR(ctx->crtc);
  673. }
  674. if (ctx->display)
  675. exynos_drm_create_enc_conn(drm_dev, ctx->display);
  676. return 0;
  677. }
  678. static void decon_unbind(struct device *dev, struct device *master,
  679. void *data)
  680. {
  681. struct decon_context *ctx = dev_get_drvdata(dev);
  682. decon_dpms(ctx->crtc, DRM_MODE_DPMS_OFF);
  683. if (ctx->display)
  684. exynos_dpi_remove(ctx->display);
  685. decon_ctx_remove(ctx);
  686. }
  687. static const struct component_ops decon_component_ops = {
  688. .bind = decon_bind,
  689. .unbind = decon_unbind,
  690. };
  691. static int decon_probe(struct platform_device *pdev)
  692. {
  693. struct device *dev = &pdev->dev;
  694. struct decon_context *ctx;
  695. struct device_node *i80_if_timings;
  696. struct resource *res;
  697. int ret;
  698. if (!dev->of_node)
  699. return -ENODEV;
  700. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  701. if (!ctx)
  702. return -ENOMEM;
  703. ret = exynos_drm_component_add(dev, EXYNOS_DEVICE_TYPE_CRTC,
  704. EXYNOS_DISPLAY_TYPE_LCD);
  705. if (ret)
  706. return ret;
  707. ctx->dev = dev;
  708. ctx->suspended = true;
  709. i80_if_timings = of_get_child_by_name(dev->of_node, "i80-if-timings");
  710. if (i80_if_timings)
  711. ctx->i80_if = true;
  712. of_node_put(i80_if_timings);
  713. ctx->regs = of_iomap(dev->of_node, 0);
  714. if (!ctx->regs) {
  715. ret = -ENOMEM;
  716. goto err_del_component;
  717. }
  718. ctx->pclk = devm_clk_get(dev, "pclk_decon0");
  719. if (IS_ERR(ctx->pclk)) {
  720. dev_err(dev, "failed to get bus clock pclk\n");
  721. ret = PTR_ERR(ctx->pclk);
  722. goto err_iounmap;
  723. }
  724. ctx->aclk = devm_clk_get(dev, "aclk_decon0");
  725. if (IS_ERR(ctx->aclk)) {
  726. dev_err(dev, "failed to get bus clock aclk\n");
  727. ret = PTR_ERR(ctx->aclk);
  728. goto err_iounmap;
  729. }
  730. ctx->eclk = devm_clk_get(dev, "decon0_eclk");
  731. if (IS_ERR(ctx->eclk)) {
  732. dev_err(dev, "failed to get eclock\n");
  733. ret = PTR_ERR(ctx->eclk);
  734. goto err_iounmap;
  735. }
  736. ctx->vclk = devm_clk_get(dev, "decon0_vclk");
  737. if (IS_ERR(ctx->vclk)) {
  738. dev_err(dev, "failed to get vclock\n");
  739. ret = PTR_ERR(ctx->vclk);
  740. goto err_iounmap;
  741. }
  742. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  743. ctx->i80_if ? "lcd_sys" : "vsync");
  744. if (!res) {
  745. dev_err(dev, "irq request failed.\n");
  746. ret = -ENXIO;
  747. goto err_iounmap;
  748. }
  749. ret = devm_request_irq(dev, res->start, decon_irq_handler,
  750. 0, "drm_decon", ctx);
  751. if (ret) {
  752. dev_err(dev, "irq request failed.\n");
  753. goto err_iounmap;
  754. }
  755. init_waitqueue_head(&ctx->wait_vsync_queue);
  756. atomic_set(&ctx->wait_vsync_event, 0);
  757. platform_set_drvdata(pdev, ctx);
  758. ctx->display = exynos_dpi_probe(dev);
  759. if (IS_ERR(ctx->display)) {
  760. ret = PTR_ERR(ctx->display);
  761. goto err_iounmap;
  762. }
  763. pm_runtime_enable(dev);
  764. ret = component_add(dev, &decon_component_ops);
  765. if (ret)
  766. goto err_disable_pm_runtime;
  767. return ret;
  768. err_disable_pm_runtime:
  769. pm_runtime_disable(dev);
  770. err_iounmap:
  771. iounmap(ctx->regs);
  772. err_del_component:
  773. exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CRTC);
  774. return ret;
  775. }
  776. static int decon_remove(struct platform_device *pdev)
  777. {
  778. struct decon_context *ctx = dev_get_drvdata(&pdev->dev);
  779. pm_runtime_disable(&pdev->dev);
  780. iounmap(ctx->regs);
  781. component_del(&pdev->dev, &decon_component_ops);
  782. exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC);
  783. return 0;
  784. }
  785. struct platform_driver decon_driver = {
  786. .probe = decon_probe,
  787. .remove = decon_remove,
  788. .driver = {
  789. .name = "exynos-decon",
  790. .of_match_table = decon_driver_dt_match,
  791. },
  792. };