exynos_drm_fimd.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /* exynos_drm_fimd.c
  2. *
  3. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  4. * Authors:
  5. * Joonyoung Shim <jy0922.shim@samsung.com>
  6. * Inki Dae <inki.dae@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 <linux/kernel.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/component.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/regmap.h>
  24. #include <video/of_display_timing.h>
  25. #include <video/of_videomode.h>
  26. #include <video/samsung_fimd.h>
  27. #include <drm/exynos_drm.h>
  28. #include "exynos_drm_drv.h"
  29. #include "exynos_drm_fbdev.h"
  30. #include "exynos_drm_crtc.h"
  31. #include "exynos_drm_plane.h"
  32. #include "exynos_drm_iommu.h"
  33. /*
  34. * FIMD stands for Fully Interactive Mobile Display and
  35. * as a display controller, it transfers contents drawn on memory
  36. * to a LCD Panel through Display Interfaces such as RGB or
  37. * CPU Interface.
  38. */
  39. #define MIN_FB_WIDTH_FOR_16WORD_BURST 128
  40. /* position control register for hardware window 0, 2 ~ 4.*/
  41. #define VIDOSD_A(win) (VIDOSD_BASE + 0x00 + (win) * 16)
  42. #define VIDOSD_B(win) (VIDOSD_BASE + 0x04 + (win) * 16)
  43. /*
  44. * size control register for hardware windows 0 and alpha control register
  45. * for hardware windows 1 ~ 4
  46. */
  47. #define VIDOSD_C(win) (VIDOSD_BASE + 0x08 + (win) * 16)
  48. /* size control register for hardware windows 1 ~ 2. */
  49. #define VIDOSD_D(win) (VIDOSD_BASE + 0x0C + (win) * 16)
  50. #define VIDWnALPHA0(win) (VIDW_ALPHA + 0x00 + (win) * 8)
  51. #define VIDWnALPHA1(win) (VIDW_ALPHA + 0x04 + (win) * 8)
  52. #define VIDWx_BUF_START(win, buf) (VIDW_BUF_START(buf) + (win) * 8)
  53. #define VIDWx_BUF_START_S(win, buf) (VIDW_BUF_START_S(buf) + (win) * 8)
  54. #define VIDWx_BUF_END(win, buf) (VIDW_BUF_END(buf) + (win) * 8)
  55. #define VIDWx_BUF_SIZE(win, buf) (VIDW_BUF_SIZE(buf) + (win) * 4)
  56. /* color key control register for hardware window 1 ~ 4. */
  57. #define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
  58. /* color key value register for hardware window 1 ~ 4. */
  59. #define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
  60. /* I80 / RGB trigger control register */
  61. #define TRIGCON 0x1A4
  62. #define TRGMODE_I80_RGB_ENABLE_I80 (1 << 0)
  63. #define SWTRGCMD_I80_RGB_ENABLE (1 << 1)
  64. /* display mode change control register except exynos4 */
  65. #define VIDOUT_CON 0x000
  66. #define VIDOUT_CON_F_I80_LDI0 (0x2 << 8)
  67. /* I80 interface control for main LDI register */
  68. #define I80IFCONFAx(x) (0x1B0 + (x) * 4)
  69. #define I80IFCONFBx(x) (0x1B8 + (x) * 4)
  70. #define LCD_CS_SETUP(x) ((x) << 16)
  71. #define LCD_WR_SETUP(x) ((x) << 12)
  72. #define LCD_WR_ACTIVE(x) ((x) << 8)
  73. #define LCD_WR_HOLD(x) ((x) << 4)
  74. #define I80IFEN_ENABLE (1 << 0)
  75. /* FIMD has totally five hardware windows. */
  76. #define WINDOWS_NR 5
  77. #define CURSOR_WIN 4
  78. struct fimd_driver_data {
  79. unsigned int timing_base;
  80. unsigned int lcdblk_offset;
  81. unsigned int lcdblk_vt_shift;
  82. unsigned int lcdblk_bypass_shift;
  83. unsigned int has_shadowcon:1;
  84. unsigned int has_clksel:1;
  85. unsigned int has_limited_fmt:1;
  86. unsigned int has_vidoutcon:1;
  87. unsigned int has_vtsel:1;
  88. };
  89. static struct fimd_driver_data s3c64xx_fimd_driver_data = {
  90. .timing_base = 0x0,
  91. .has_clksel = 1,
  92. .has_limited_fmt = 1,
  93. };
  94. static struct fimd_driver_data exynos3_fimd_driver_data = {
  95. .timing_base = 0x20000,
  96. .lcdblk_offset = 0x210,
  97. .lcdblk_bypass_shift = 1,
  98. .has_shadowcon = 1,
  99. .has_vidoutcon = 1,
  100. };
  101. static struct fimd_driver_data exynos4_fimd_driver_data = {
  102. .timing_base = 0x0,
  103. .lcdblk_offset = 0x210,
  104. .lcdblk_vt_shift = 10,
  105. .lcdblk_bypass_shift = 1,
  106. .has_shadowcon = 1,
  107. .has_vtsel = 1,
  108. };
  109. static struct fimd_driver_data exynos4415_fimd_driver_data = {
  110. .timing_base = 0x20000,
  111. .lcdblk_offset = 0x210,
  112. .lcdblk_vt_shift = 10,
  113. .lcdblk_bypass_shift = 1,
  114. .has_shadowcon = 1,
  115. .has_vidoutcon = 1,
  116. .has_vtsel = 1,
  117. };
  118. static struct fimd_driver_data exynos5_fimd_driver_data = {
  119. .timing_base = 0x20000,
  120. .lcdblk_offset = 0x214,
  121. .lcdblk_vt_shift = 24,
  122. .lcdblk_bypass_shift = 15,
  123. .has_shadowcon = 1,
  124. .has_vidoutcon = 1,
  125. .has_vtsel = 1,
  126. };
  127. struct fimd_context {
  128. struct device *dev;
  129. struct drm_device *drm_dev;
  130. struct exynos_drm_crtc *crtc;
  131. struct exynos_drm_plane planes[WINDOWS_NR];
  132. struct clk *bus_clk;
  133. struct clk *lcd_clk;
  134. void __iomem *regs;
  135. struct regmap *sysreg;
  136. unsigned long irq_flags;
  137. u32 vidcon0;
  138. u32 vidcon1;
  139. u32 vidout_con;
  140. u32 i80ifcon;
  141. bool i80_if;
  142. bool suspended;
  143. int pipe;
  144. wait_queue_head_t wait_vsync_queue;
  145. atomic_t wait_vsync_event;
  146. atomic_t win_updated;
  147. atomic_t triggering;
  148. struct exynos_drm_panel_info panel;
  149. struct fimd_driver_data *driver_data;
  150. struct drm_encoder *encoder;
  151. };
  152. static const struct of_device_id fimd_driver_dt_match[] = {
  153. { .compatible = "samsung,s3c6400-fimd",
  154. .data = &s3c64xx_fimd_driver_data },
  155. { .compatible = "samsung,exynos3250-fimd",
  156. .data = &exynos3_fimd_driver_data },
  157. { .compatible = "samsung,exynos4210-fimd",
  158. .data = &exynos4_fimd_driver_data },
  159. { .compatible = "samsung,exynos4415-fimd",
  160. .data = &exynos4415_fimd_driver_data },
  161. { .compatible = "samsung,exynos5250-fimd",
  162. .data = &exynos5_fimd_driver_data },
  163. {},
  164. };
  165. MODULE_DEVICE_TABLE(of, fimd_driver_dt_match);
  166. static const uint32_t fimd_formats[] = {
  167. DRM_FORMAT_C8,
  168. DRM_FORMAT_XRGB1555,
  169. DRM_FORMAT_RGB565,
  170. DRM_FORMAT_XRGB8888,
  171. DRM_FORMAT_ARGB8888,
  172. };
  173. static inline struct fimd_driver_data *drm_fimd_get_driver_data(
  174. struct platform_device *pdev)
  175. {
  176. const struct of_device_id *of_id =
  177. of_match_device(fimd_driver_dt_match, &pdev->dev);
  178. return (struct fimd_driver_data *)of_id->data;
  179. }
  180. static int fimd_enable_vblank(struct exynos_drm_crtc *crtc)
  181. {
  182. struct fimd_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_I80IFDONE;
  191. val |= VIDINTCON0_INT_SYSMAINCON;
  192. val &= ~VIDINTCON0_INT_SYSSUBCON;
  193. } else {
  194. val |= VIDINTCON0_INT_FRAME;
  195. val &= ~VIDINTCON0_FRAMESEL0_MASK;
  196. val |= VIDINTCON0_FRAMESEL0_VSYNC;
  197. val &= ~VIDINTCON0_FRAMESEL1_MASK;
  198. val |= VIDINTCON0_FRAMESEL1_NONE;
  199. }
  200. writel(val, ctx->regs + VIDINTCON0);
  201. }
  202. return 0;
  203. }
  204. static void fimd_disable_vblank(struct exynos_drm_crtc *crtc)
  205. {
  206. struct fimd_context *ctx = crtc->ctx;
  207. u32 val;
  208. if (ctx->suspended)
  209. return;
  210. if (test_and_clear_bit(0, &ctx->irq_flags)) {
  211. val = readl(ctx->regs + VIDINTCON0);
  212. val &= ~VIDINTCON0_INT_ENABLE;
  213. if (ctx->i80_if) {
  214. val &= ~VIDINTCON0_INT_I80IFDONE;
  215. val &= ~VIDINTCON0_INT_SYSMAINCON;
  216. val &= ~VIDINTCON0_INT_SYSSUBCON;
  217. } else
  218. val &= ~VIDINTCON0_INT_FRAME;
  219. writel(val, ctx->regs + VIDINTCON0);
  220. }
  221. }
  222. static void fimd_wait_for_vblank(struct exynos_drm_crtc *crtc)
  223. {
  224. struct fimd_context *ctx = crtc->ctx;
  225. if (ctx->suspended)
  226. return;
  227. atomic_set(&ctx->wait_vsync_event, 1);
  228. /*
  229. * wait for FIMD to signal VSYNC interrupt or return after
  230. * timeout which is set to 50ms (refresh rate of 20).
  231. */
  232. if (!wait_event_timeout(ctx->wait_vsync_queue,
  233. !atomic_read(&ctx->wait_vsync_event),
  234. HZ/20))
  235. DRM_DEBUG_KMS("vblank wait timed out.\n");
  236. }
  237. static void fimd_enable_video_output(struct fimd_context *ctx, unsigned int win,
  238. bool enable)
  239. {
  240. u32 val = readl(ctx->regs + WINCON(win));
  241. if (enable)
  242. val |= WINCONx_ENWIN;
  243. else
  244. val &= ~WINCONx_ENWIN;
  245. writel(val, ctx->regs + WINCON(win));
  246. }
  247. static void fimd_enable_shadow_channel_path(struct fimd_context *ctx,
  248. unsigned int win,
  249. bool enable)
  250. {
  251. u32 val = readl(ctx->regs + SHADOWCON);
  252. if (enable)
  253. val |= SHADOWCON_CHx_ENABLE(win);
  254. else
  255. val &= ~SHADOWCON_CHx_ENABLE(win);
  256. writel(val, ctx->regs + SHADOWCON);
  257. }
  258. static void fimd_clear_channels(struct exynos_drm_crtc *crtc)
  259. {
  260. struct fimd_context *ctx = crtc->ctx;
  261. unsigned int win, ch_enabled = 0;
  262. DRM_DEBUG_KMS("%s\n", __FILE__);
  263. /* Hardware is in unknown state, so ensure it gets enabled properly */
  264. pm_runtime_get_sync(ctx->dev);
  265. clk_prepare_enable(ctx->bus_clk);
  266. clk_prepare_enable(ctx->lcd_clk);
  267. /* Check if any channel is enabled. */
  268. for (win = 0; win < WINDOWS_NR; win++) {
  269. u32 val = readl(ctx->regs + WINCON(win));
  270. if (val & WINCONx_ENWIN) {
  271. fimd_enable_video_output(ctx, win, false);
  272. if (ctx->driver_data->has_shadowcon)
  273. fimd_enable_shadow_channel_path(ctx, win,
  274. false);
  275. ch_enabled = 1;
  276. }
  277. }
  278. /* Wait for vsync, as disable channel takes effect at next vsync */
  279. if (ch_enabled) {
  280. int pipe = ctx->pipe;
  281. /* ensure that vblank interrupt won't be reported to core */
  282. ctx->suspended = false;
  283. ctx->pipe = -1;
  284. fimd_enable_vblank(ctx->crtc);
  285. fimd_wait_for_vblank(ctx->crtc);
  286. fimd_disable_vblank(ctx->crtc);
  287. ctx->suspended = true;
  288. ctx->pipe = pipe;
  289. }
  290. clk_disable_unprepare(ctx->lcd_clk);
  291. clk_disable_unprepare(ctx->bus_clk);
  292. pm_runtime_put(ctx->dev);
  293. }
  294. static u32 fimd_calc_clkdiv(struct fimd_context *ctx,
  295. const struct drm_display_mode *mode)
  296. {
  297. unsigned long ideal_clk = mode->htotal * mode->vtotal * mode->vrefresh;
  298. u32 clkdiv;
  299. if (ctx->i80_if) {
  300. /*
  301. * The frame done interrupt should be occurred prior to the
  302. * next TE signal.
  303. */
  304. ideal_clk *= 2;
  305. }
  306. /* Find the clock divider value that gets us closest to ideal_clk */
  307. clkdiv = DIV_ROUND_UP(clk_get_rate(ctx->lcd_clk), ideal_clk);
  308. return (clkdiv < 0x100) ? clkdiv : 0xff;
  309. }
  310. static void fimd_commit(struct exynos_drm_crtc *crtc)
  311. {
  312. struct fimd_context *ctx = crtc->ctx;
  313. struct drm_display_mode *mode = &crtc->base.state->adjusted_mode;
  314. struct fimd_driver_data *driver_data = ctx->driver_data;
  315. void *timing_base = ctx->regs + driver_data->timing_base;
  316. u32 val, clkdiv;
  317. if (ctx->suspended)
  318. return;
  319. /* nothing to do if we haven't set the mode yet */
  320. if (mode->htotal == 0 || mode->vtotal == 0)
  321. return;
  322. if (ctx->i80_if) {
  323. val = ctx->i80ifcon | I80IFEN_ENABLE;
  324. writel(val, timing_base + I80IFCONFAx(0));
  325. /* disable auto frame rate */
  326. writel(0, timing_base + I80IFCONFBx(0));
  327. /* set video type selection to I80 interface */
  328. if (driver_data->has_vtsel && ctx->sysreg &&
  329. regmap_update_bits(ctx->sysreg,
  330. driver_data->lcdblk_offset,
  331. 0x3 << driver_data->lcdblk_vt_shift,
  332. 0x1 << driver_data->lcdblk_vt_shift)) {
  333. DRM_ERROR("Failed to update sysreg for I80 i/f.\n");
  334. return;
  335. }
  336. } else {
  337. int vsync_len, vbpd, vfpd, hsync_len, hbpd, hfpd;
  338. u32 vidcon1;
  339. /* setup polarity values */
  340. vidcon1 = ctx->vidcon1;
  341. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  342. vidcon1 |= VIDCON1_INV_VSYNC;
  343. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  344. vidcon1 |= VIDCON1_INV_HSYNC;
  345. writel(vidcon1, ctx->regs + driver_data->timing_base + VIDCON1);
  346. /* setup vertical timing values. */
  347. vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
  348. vbpd = mode->crtc_vtotal - mode->crtc_vsync_end;
  349. vfpd = mode->crtc_vsync_start - mode->crtc_vdisplay;
  350. val = VIDTCON0_VBPD(vbpd - 1) |
  351. VIDTCON0_VFPD(vfpd - 1) |
  352. VIDTCON0_VSPW(vsync_len - 1);
  353. writel(val, ctx->regs + driver_data->timing_base + VIDTCON0);
  354. /* setup horizontal timing values. */
  355. hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
  356. hbpd = mode->crtc_htotal - mode->crtc_hsync_end;
  357. hfpd = mode->crtc_hsync_start - mode->crtc_hdisplay;
  358. val = VIDTCON1_HBPD(hbpd - 1) |
  359. VIDTCON1_HFPD(hfpd - 1) |
  360. VIDTCON1_HSPW(hsync_len - 1);
  361. writel(val, ctx->regs + driver_data->timing_base + VIDTCON1);
  362. }
  363. if (driver_data->has_vidoutcon)
  364. writel(ctx->vidout_con, timing_base + VIDOUT_CON);
  365. /* set bypass selection */
  366. if (ctx->sysreg && regmap_update_bits(ctx->sysreg,
  367. driver_data->lcdblk_offset,
  368. 0x1 << driver_data->lcdblk_bypass_shift,
  369. 0x1 << driver_data->lcdblk_bypass_shift)) {
  370. DRM_ERROR("Failed to update sysreg for bypass setting.\n");
  371. return;
  372. }
  373. /* setup horizontal and vertical display size. */
  374. val = VIDTCON2_LINEVAL(mode->vdisplay - 1) |
  375. VIDTCON2_HOZVAL(mode->hdisplay - 1) |
  376. VIDTCON2_LINEVAL_E(mode->vdisplay - 1) |
  377. VIDTCON2_HOZVAL_E(mode->hdisplay - 1);
  378. writel(val, ctx->regs + driver_data->timing_base + VIDTCON2);
  379. /*
  380. * fields of register with prefix '_F' would be updated
  381. * at vsync(same as dma start)
  382. */
  383. val = ctx->vidcon0;
  384. val |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  385. if (ctx->driver_data->has_clksel)
  386. val |= VIDCON0_CLKSEL_LCD;
  387. clkdiv = fimd_calc_clkdiv(ctx, mode);
  388. if (clkdiv > 1)
  389. val |= VIDCON0_CLKVAL_F(clkdiv - 1) | VIDCON0_CLKDIR;
  390. writel(val, ctx->regs + VIDCON0);
  391. }
  392. static void fimd_win_set_pixfmt(struct fimd_context *ctx, unsigned int win,
  393. struct drm_framebuffer *fb)
  394. {
  395. unsigned long val;
  396. val = WINCONx_ENWIN;
  397. /*
  398. * In case of s3c64xx, window 0 doesn't support alpha channel.
  399. * So the request format is ARGB8888 then change it to XRGB8888.
  400. */
  401. if (ctx->driver_data->has_limited_fmt && !win) {
  402. if (fb->pixel_format == DRM_FORMAT_ARGB8888)
  403. fb->pixel_format = DRM_FORMAT_XRGB8888;
  404. }
  405. switch (fb->pixel_format) {
  406. case DRM_FORMAT_C8:
  407. val |= WINCON0_BPPMODE_8BPP_PALETTE;
  408. val |= WINCONx_BURSTLEN_8WORD;
  409. val |= WINCONx_BYTSWP;
  410. break;
  411. case DRM_FORMAT_XRGB1555:
  412. val |= WINCON0_BPPMODE_16BPP_1555;
  413. val |= WINCONx_HAWSWP;
  414. val |= WINCONx_BURSTLEN_16WORD;
  415. break;
  416. case DRM_FORMAT_RGB565:
  417. val |= WINCON0_BPPMODE_16BPP_565;
  418. val |= WINCONx_HAWSWP;
  419. val |= WINCONx_BURSTLEN_16WORD;
  420. break;
  421. case DRM_FORMAT_XRGB8888:
  422. val |= WINCON0_BPPMODE_24BPP_888;
  423. val |= WINCONx_WSWP;
  424. val |= WINCONx_BURSTLEN_16WORD;
  425. break;
  426. case DRM_FORMAT_ARGB8888:
  427. val |= WINCON1_BPPMODE_25BPP_A1888
  428. | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
  429. val |= WINCONx_WSWP;
  430. val |= WINCONx_BURSTLEN_16WORD;
  431. break;
  432. default:
  433. DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
  434. val |= WINCON0_BPPMODE_24BPP_888;
  435. val |= WINCONx_WSWP;
  436. val |= WINCONx_BURSTLEN_16WORD;
  437. break;
  438. }
  439. DRM_DEBUG_KMS("bpp = %d\n", fb->bits_per_pixel);
  440. /*
  441. * In case of exynos, setting dma-burst to 16Word causes permanent
  442. * tearing for very small buffers, e.g. cursor buffer. Burst Mode
  443. * switching which is based on plane size is not recommended as
  444. * plane size varies alot towards the end of the screen and rapid
  445. * movement causes unstable DMA which results into iommu crash/tear.
  446. */
  447. if (fb->width < MIN_FB_WIDTH_FOR_16WORD_BURST) {
  448. val &= ~WINCONx_BURSTLEN_MASK;
  449. val |= WINCONx_BURSTLEN_4WORD;
  450. }
  451. writel(val, ctx->regs + WINCON(win));
  452. /* hardware window 0 doesn't support alpha channel. */
  453. if (win != 0) {
  454. /* OSD alpha */
  455. val = VIDISD14C_ALPHA0_R(0xf) |
  456. VIDISD14C_ALPHA0_G(0xf) |
  457. VIDISD14C_ALPHA0_B(0xf) |
  458. VIDISD14C_ALPHA1_R(0xf) |
  459. VIDISD14C_ALPHA1_G(0xf) |
  460. VIDISD14C_ALPHA1_B(0xf);
  461. writel(val, ctx->regs + VIDOSD_C(win));
  462. val = VIDW_ALPHA_R(0xf) | VIDW_ALPHA_G(0xf) |
  463. VIDW_ALPHA_G(0xf);
  464. writel(val, ctx->regs + VIDWnALPHA0(win));
  465. writel(val, ctx->regs + VIDWnALPHA1(win));
  466. }
  467. }
  468. static void fimd_win_set_colkey(struct fimd_context *ctx, unsigned int win)
  469. {
  470. unsigned int keycon0 = 0, keycon1 = 0;
  471. keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
  472. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  473. keycon1 = WxKEYCON1_COLVAL(0xffffffff);
  474. writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
  475. writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
  476. }
  477. /**
  478. * shadow_protect_win() - disable updating values from shadow registers at vsync
  479. *
  480. * @win: window to protect registers for
  481. * @protect: 1 to protect (disable updates)
  482. */
  483. static void fimd_shadow_protect_win(struct fimd_context *ctx,
  484. unsigned int win, bool protect)
  485. {
  486. u32 reg, bits, val;
  487. /*
  488. * SHADOWCON/PRTCON register is used for enabling timing.
  489. *
  490. * for example, once only width value of a register is set,
  491. * if the dma is started then fimd hardware could malfunction so
  492. * with protect window setting, the register fields with prefix '_F'
  493. * wouldn't be updated at vsync also but updated once unprotect window
  494. * is set.
  495. */
  496. if (ctx->driver_data->has_shadowcon) {
  497. reg = SHADOWCON;
  498. bits = SHADOWCON_WINx_PROTECT(win);
  499. } else {
  500. reg = PRTCON;
  501. bits = PRTCON_PROTECT;
  502. }
  503. val = readl(ctx->regs + reg);
  504. if (protect)
  505. val |= bits;
  506. else
  507. val &= ~bits;
  508. writel(val, ctx->regs + reg);
  509. }
  510. static void fimd_atomic_begin(struct exynos_drm_crtc *crtc,
  511. struct exynos_drm_plane *plane)
  512. {
  513. struct fimd_context *ctx = crtc->ctx;
  514. if (ctx->suspended)
  515. return;
  516. fimd_shadow_protect_win(ctx, plane->zpos, true);
  517. }
  518. static void fimd_atomic_flush(struct exynos_drm_crtc *crtc,
  519. struct exynos_drm_plane *plane)
  520. {
  521. struct fimd_context *ctx = crtc->ctx;
  522. if (ctx->suspended)
  523. return;
  524. fimd_shadow_protect_win(ctx, plane->zpos, false);
  525. }
  526. static void fimd_update_plane(struct exynos_drm_crtc *crtc,
  527. struct exynos_drm_plane *plane)
  528. {
  529. struct fimd_context *ctx = crtc->ctx;
  530. struct drm_plane_state *state = plane->base.state;
  531. dma_addr_t dma_addr;
  532. unsigned long val, size, offset;
  533. unsigned int last_x, last_y, buf_offsize, line_size;
  534. unsigned int win = plane->zpos;
  535. unsigned int bpp = state->fb->bits_per_pixel >> 3;
  536. unsigned int pitch = state->fb->pitches[0];
  537. if (ctx->suspended)
  538. return;
  539. offset = plane->src_x * bpp;
  540. offset += plane->src_y * pitch;
  541. /* buffer start address */
  542. dma_addr = plane->dma_addr[0] + offset;
  543. val = (unsigned long)dma_addr;
  544. writel(val, ctx->regs + VIDWx_BUF_START(win, 0));
  545. /* buffer end address */
  546. size = pitch * plane->crtc_h;
  547. val = (unsigned long)(dma_addr + size);
  548. writel(val, ctx->regs + VIDWx_BUF_END(win, 0));
  549. DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
  550. (unsigned long)dma_addr, val, size);
  551. DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
  552. plane->crtc_w, plane->crtc_h);
  553. /* buffer size */
  554. buf_offsize = pitch - (plane->crtc_w * bpp);
  555. line_size = plane->crtc_w * bpp;
  556. val = VIDW_BUF_SIZE_OFFSET(buf_offsize) |
  557. VIDW_BUF_SIZE_PAGEWIDTH(line_size) |
  558. VIDW_BUF_SIZE_OFFSET_E(buf_offsize) |
  559. VIDW_BUF_SIZE_PAGEWIDTH_E(line_size);
  560. writel(val, ctx->regs + VIDWx_BUF_SIZE(win, 0));
  561. /* OSD position */
  562. val = VIDOSDxA_TOPLEFT_X(plane->crtc_x) |
  563. VIDOSDxA_TOPLEFT_Y(plane->crtc_y) |
  564. VIDOSDxA_TOPLEFT_X_E(plane->crtc_x) |
  565. VIDOSDxA_TOPLEFT_Y_E(plane->crtc_y);
  566. writel(val, ctx->regs + VIDOSD_A(win));
  567. last_x = plane->crtc_x + plane->crtc_w;
  568. if (last_x)
  569. last_x--;
  570. last_y = plane->crtc_y + plane->crtc_h;
  571. if (last_y)
  572. last_y--;
  573. val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y) |
  574. VIDOSDxB_BOTRIGHT_X_E(last_x) | VIDOSDxB_BOTRIGHT_Y_E(last_y);
  575. writel(val, ctx->regs + VIDOSD_B(win));
  576. DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
  577. plane->crtc_x, plane->crtc_y, last_x, last_y);
  578. /* OSD size */
  579. if (win != 3 && win != 4) {
  580. u32 offset = VIDOSD_D(win);
  581. if (win == 0)
  582. offset = VIDOSD_C(win);
  583. val = plane->crtc_w * plane->crtc_h;
  584. writel(val, ctx->regs + offset);
  585. DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val);
  586. }
  587. fimd_win_set_pixfmt(ctx, win, state->fb);
  588. /* hardware window 0 doesn't support color key. */
  589. if (win != 0)
  590. fimd_win_set_colkey(ctx, win);
  591. fimd_enable_video_output(ctx, win, true);
  592. if (ctx->driver_data->has_shadowcon)
  593. fimd_enable_shadow_channel_path(ctx, win, true);
  594. if (ctx->i80_if)
  595. atomic_set(&ctx->win_updated, 1);
  596. }
  597. static void fimd_disable_plane(struct exynos_drm_crtc *crtc,
  598. struct exynos_drm_plane *plane)
  599. {
  600. struct fimd_context *ctx = crtc->ctx;
  601. unsigned int win = plane->zpos;
  602. if (ctx->suspended)
  603. return;
  604. fimd_enable_video_output(ctx, win, false);
  605. if (ctx->driver_data->has_shadowcon)
  606. fimd_enable_shadow_channel_path(ctx, win, false);
  607. }
  608. static void fimd_enable(struct exynos_drm_crtc *crtc)
  609. {
  610. struct fimd_context *ctx = crtc->ctx;
  611. int ret;
  612. if (!ctx->suspended)
  613. return;
  614. ctx->suspended = false;
  615. pm_runtime_get_sync(ctx->dev);
  616. ret = clk_prepare_enable(ctx->bus_clk);
  617. if (ret < 0) {
  618. DRM_ERROR("Failed to prepare_enable the bus clk [%d]\n", ret);
  619. return;
  620. }
  621. ret = clk_prepare_enable(ctx->lcd_clk);
  622. if (ret < 0) {
  623. DRM_ERROR("Failed to prepare_enable the lcd clk [%d]\n", ret);
  624. return;
  625. }
  626. /* if vblank was enabled status, enable it again. */
  627. if (test_and_clear_bit(0, &ctx->irq_flags))
  628. fimd_enable_vblank(ctx->crtc);
  629. fimd_commit(ctx->crtc);
  630. }
  631. static void fimd_disable(struct exynos_drm_crtc *crtc)
  632. {
  633. struct fimd_context *ctx = crtc->ctx;
  634. int i;
  635. if (ctx->suspended)
  636. return;
  637. /*
  638. * We need to make sure that all windows are disabled before we
  639. * suspend that connector. Otherwise we might try to scan from
  640. * a destroyed buffer later.
  641. */
  642. for (i = 0; i < WINDOWS_NR; i++)
  643. fimd_disable_plane(crtc, &ctx->planes[i]);
  644. fimd_enable_vblank(crtc);
  645. fimd_wait_for_vblank(crtc);
  646. fimd_disable_vblank(crtc);
  647. writel(0, ctx->regs + VIDCON0);
  648. clk_disable_unprepare(ctx->lcd_clk);
  649. clk_disable_unprepare(ctx->bus_clk);
  650. pm_runtime_put_sync(ctx->dev);
  651. ctx->suspended = true;
  652. }
  653. static void fimd_trigger(struct device *dev)
  654. {
  655. struct fimd_context *ctx = dev_get_drvdata(dev);
  656. struct fimd_driver_data *driver_data = ctx->driver_data;
  657. void *timing_base = ctx->regs + driver_data->timing_base;
  658. u32 reg;
  659. /*
  660. * Skips triggering if in triggering state, because multiple triggering
  661. * requests can cause panel reset.
  662. */
  663. if (atomic_read(&ctx->triggering))
  664. return;
  665. /* Enters triggering mode */
  666. atomic_set(&ctx->triggering, 1);
  667. reg = readl(timing_base + TRIGCON);
  668. reg |= (TRGMODE_I80_RGB_ENABLE_I80 | SWTRGCMD_I80_RGB_ENABLE);
  669. writel(reg, timing_base + TRIGCON);
  670. /*
  671. * Exits triggering mode if vblank is not enabled yet, because when the
  672. * VIDINTCON0 register is not set, it can not exit from triggering mode.
  673. */
  674. if (!test_bit(0, &ctx->irq_flags))
  675. atomic_set(&ctx->triggering, 0);
  676. }
  677. static void fimd_te_handler(struct exynos_drm_crtc *crtc)
  678. {
  679. struct fimd_context *ctx = crtc->ctx;
  680. /* Checks the crtc is detached already from encoder */
  681. if (ctx->pipe < 0 || !ctx->drm_dev)
  682. return;
  683. /*
  684. * If there is a page flip request, triggers and handles the page flip
  685. * event so that current fb can be updated into panel GRAM.
  686. */
  687. if (atomic_add_unless(&ctx->win_updated, -1, 0))
  688. fimd_trigger(ctx->dev);
  689. /* Wakes up vsync event queue */
  690. if (atomic_read(&ctx->wait_vsync_event)) {
  691. atomic_set(&ctx->wait_vsync_event, 0);
  692. wake_up(&ctx->wait_vsync_queue);
  693. }
  694. if (test_bit(0, &ctx->irq_flags))
  695. drm_crtc_handle_vblank(&ctx->crtc->base);
  696. }
  697. static void fimd_dp_clock_enable(struct exynos_drm_crtc *crtc, bool enable)
  698. {
  699. struct fimd_context *ctx = crtc->ctx;
  700. u32 val;
  701. /*
  702. * Only Exynos 5250, 5260, 5410 and 542x requires enabling DP/MIE
  703. * clock. On these SoCs the bootloader may enable it but any
  704. * power domain off/on will reset it to disable state.
  705. */
  706. if (ctx->driver_data != &exynos5_fimd_driver_data)
  707. return;
  708. val = enable ? DP_MIE_CLK_DP_ENABLE : DP_MIE_CLK_DISABLE;
  709. writel(val, ctx->regs + DP_MIE_CLKCON);
  710. }
  711. static const struct exynos_drm_crtc_ops fimd_crtc_ops = {
  712. .enable = fimd_enable,
  713. .disable = fimd_disable,
  714. .commit = fimd_commit,
  715. .enable_vblank = fimd_enable_vblank,
  716. .disable_vblank = fimd_disable_vblank,
  717. .wait_for_vblank = fimd_wait_for_vblank,
  718. .atomic_begin = fimd_atomic_begin,
  719. .update_plane = fimd_update_plane,
  720. .disable_plane = fimd_disable_plane,
  721. .atomic_flush = fimd_atomic_flush,
  722. .te_handler = fimd_te_handler,
  723. .clock_enable = fimd_dp_clock_enable,
  724. };
  725. static irqreturn_t fimd_irq_handler(int irq, void *dev_id)
  726. {
  727. struct fimd_context *ctx = (struct fimd_context *)dev_id;
  728. u32 val, clear_bit, start, start_s;
  729. int win;
  730. val = readl(ctx->regs + VIDINTCON1);
  731. clear_bit = ctx->i80_if ? VIDINTCON1_INT_I80 : VIDINTCON1_INT_FRAME;
  732. if (val & clear_bit)
  733. writel(clear_bit, ctx->regs + VIDINTCON1);
  734. /* check the crtc is detached already from encoder */
  735. if (ctx->pipe < 0 || !ctx->drm_dev)
  736. goto out;
  737. if (!ctx->i80_if)
  738. drm_crtc_handle_vblank(&ctx->crtc->base);
  739. for (win = 0 ; win < WINDOWS_NR ; win++) {
  740. struct exynos_drm_plane *plane = &ctx->planes[win];
  741. if (!plane->pending_fb)
  742. continue;
  743. start = readl(ctx->regs + VIDWx_BUF_START(win, 0));
  744. start_s = readl(ctx->regs + VIDWx_BUF_START_S(win, 0));
  745. if (start == start_s)
  746. exynos_drm_crtc_finish_update(ctx->crtc, plane);
  747. }
  748. if (ctx->i80_if) {
  749. /* Exits triggering mode */
  750. atomic_set(&ctx->triggering, 0);
  751. } else {
  752. /* set wait vsync event to zero and wake up queue. */
  753. if (atomic_read(&ctx->wait_vsync_event)) {
  754. atomic_set(&ctx->wait_vsync_event, 0);
  755. wake_up(&ctx->wait_vsync_queue);
  756. }
  757. }
  758. out:
  759. return IRQ_HANDLED;
  760. }
  761. static int fimd_bind(struct device *dev, struct device *master, void *data)
  762. {
  763. struct fimd_context *ctx = dev_get_drvdata(dev);
  764. struct drm_device *drm_dev = data;
  765. struct exynos_drm_private *priv = drm_dev->dev_private;
  766. struct exynos_drm_plane *exynos_plane;
  767. enum drm_plane_type type;
  768. unsigned int zpos;
  769. int ret;
  770. ctx->drm_dev = drm_dev;
  771. ctx->pipe = priv->pipe++;
  772. for (zpos = 0; zpos < WINDOWS_NR; zpos++) {
  773. type = exynos_plane_get_type(zpos, CURSOR_WIN);
  774. ret = exynos_plane_init(drm_dev, &ctx->planes[zpos],
  775. 1 << ctx->pipe, type, fimd_formats,
  776. ARRAY_SIZE(fimd_formats), zpos);
  777. if (ret)
  778. return ret;
  779. }
  780. exynos_plane = &ctx->planes[DEFAULT_WIN];
  781. ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
  782. ctx->pipe, EXYNOS_DISPLAY_TYPE_LCD,
  783. &fimd_crtc_ops, ctx);
  784. if (IS_ERR(ctx->crtc))
  785. return PTR_ERR(ctx->crtc);
  786. if (ctx->encoder)
  787. exynos_dpi_bind(drm_dev, ctx->encoder);
  788. if (is_drm_iommu_supported(drm_dev))
  789. fimd_clear_channels(ctx->crtc);
  790. ret = drm_iommu_attach_device(drm_dev, dev);
  791. if (ret)
  792. priv->pipe--;
  793. return ret;
  794. }
  795. static void fimd_unbind(struct device *dev, struct device *master,
  796. void *data)
  797. {
  798. struct fimd_context *ctx = dev_get_drvdata(dev);
  799. fimd_disable(ctx->crtc);
  800. drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
  801. if (ctx->encoder)
  802. exynos_dpi_remove(ctx->encoder);
  803. }
  804. static const struct component_ops fimd_component_ops = {
  805. .bind = fimd_bind,
  806. .unbind = fimd_unbind,
  807. };
  808. static int fimd_probe(struct platform_device *pdev)
  809. {
  810. struct device *dev = &pdev->dev;
  811. struct fimd_context *ctx;
  812. struct device_node *i80_if_timings;
  813. struct resource *res;
  814. int ret;
  815. if (!dev->of_node)
  816. return -ENODEV;
  817. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  818. if (!ctx)
  819. return -ENOMEM;
  820. ctx->dev = dev;
  821. ctx->suspended = true;
  822. ctx->driver_data = drm_fimd_get_driver_data(pdev);
  823. if (of_property_read_bool(dev->of_node, "samsung,invert-vden"))
  824. ctx->vidcon1 |= VIDCON1_INV_VDEN;
  825. if (of_property_read_bool(dev->of_node, "samsung,invert-vclk"))
  826. ctx->vidcon1 |= VIDCON1_INV_VCLK;
  827. i80_if_timings = of_get_child_by_name(dev->of_node, "i80-if-timings");
  828. if (i80_if_timings) {
  829. u32 val;
  830. ctx->i80_if = true;
  831. if (ctx->driver_data->has_vidoutcon)
  832. ctx->vidout_con |= VIDOUT_CON_F_I80_LDI0;
  833. else
  834. ctx->vidcon0 |= VIDCON0_VIDOUT_I80_LDI0;
  835. /*
  836. * The user manual describes that this "DSI_EN" bit is required
  837. * to enable I80 24-bit data interface.
  838. */
  839. ctx->vidcon0 |= VIDCON0_DSI_EN;
  840. if (of_property_read_u32(i80_if_timings, "cs-setup", &val))
  841. val = 0;
  842. ctx->i80ifcon = LCD_CS_SETUP(val);
  843. if (of_property_read_u32(i80_if_timings, "wr-setup", &val))
  844. val = 0;
  845. ctx->i80ifcon |= LCD_WR_SETUP(val);
  846. if (of_property_read_u32(i80_if_timings, "wr-active", &val))
  847. val = 1;
  848. ctx->i80ifcon |= LCD_WR_ACTIVE(val);
  849. if (of_property_read_u32(i80_if_timings, "wr-hold", &val))
  850. val = 0;
  851. ctx->i80ifcon |= LCD_WR_HOLD(val);
  852. }
  853. of_node_put(i80_if_timings);
  854. ctx->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
  855. "samsung,sysreg");
  856. if (IS_ERR(ctx->sysreg)) {
  857. dev_warn(dev, "failed to get system register.\n");
  858. ctx->sysreg = NULL;
  859. }
  860. ctx->bus_clk = devm_clk_get(dev, "fimd");
  861. if (IS_ERR(ctx->bus_clk)) {
  862. dev_err(dev, "failed to get bus clock\n");
  863. return PTR_ERR(ctx->bus_clk);
  864. }
  865. ctx->lcd_clk = devm_clk_get(dev, "sclk_fimd");
  866. if (IS_ERR(ctx->lcd_clk)) {
  867. dev_err(dev, "failed to get lcd clock\n");
  868. return PTR_ERR(ctx->lcd_clk);
  869. }
  870. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  871. ctx->regs = devm_ioremap_resource(dev, res);
  872. if (IS_ERR(ctx->regs))
  873. return PTR_ERR(ctx->regs);
  874. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  875. ctx->i80_if ? "lcd_sys" : "vsync");
  876. if (!res) {
  877. dev_err(dev, "irq request failed.\n");
  878. return -ENXIO;
  879. }
  880. ret = devm_request_irq(dev, res->start, fimd_irq_handler,
  881. 0, "drm_fimd", ctx);
  882. if (ret) {
  883. dev_err(dev, "irq request failed.\n");
  884. return ret;
  885. }
  886. init_waitqueue_head(&ctx->wait_vsync_queue);
  887. atomic_set(&ctx->wait_vsync_event, 0);
  888. platform_set_drvdata(pdev, ctx);
  889. ctx->encoder = exynos_dpi_probe(dev);
  890. if (IS_ERR(ctx->encoder))
  891. return PTR_ERR(ctx->encoder);
  892. pm_runtime_enable(dev);
  893. ret = component_add(dev, &fimd_component_ops);
  894. if (ret)
  895. goto err_disable_pm_runtime;
  896. return ret;
  897. err_disable_pm_runtime:
  898. pm_runtime_disable(dev);
  899. return ret;
  900. }
  901. static int fimd_remove(struct platform_device *pdev)
  902. {
  903. pm_runtime_disable(&pdev->dev);
  904. component_del(&pdev->dev, &fimd_component_ops);
  905. return 0;
  906. }
  907. struct platform_driver fimd_driver = {
  908. .probe = fimd_probe,
  909. .remove = fimd_remove,
  910. .driver = {
  911. .name = "exynos4-fb",
  912. .owner = THIS_MODULE,
  913. .of_match_table = fimd_driver_dt_match,
  914. },
  915. };