exynos_drm_fimd.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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 <video/of_display_timing.h>
  22. #include <video/of_videomode.h>
  23. #include <video/samsung_fimd.h>
  24. #include <drm/exynos_drm.h>
  25. #include "exynos_drm_drv.h"
  26. #include "exynos_drm_fbdev.h"
  27. #include "exynos_drm_crtc.h"
  28. #include "exynos_drm_iommu.h"
  29. /*
  30. * FIMD stands for Fully Interactive Mobile Display and
  31. * as a display controller, it transfers contents drawn on memory
  32. * to a LCD Panel through Display Interfaces such as RGB or
  33. * CPU Interface.
  34. */
  35. #define FIMD_DEFAULT_FRAMERATE 60
  36. /* position control register for hardware window 0, 2 ~ 4.*/
  37. #define VIDOSD_A(win) (VIDOSD_BASE + 0x00 + (win) * 16)
  38. #define VIDOSD_B(win) (VIDOSD_BASE + 0x04 + (win) * 16)
  39. /*
  40. * size control register for hardware windows 0 and alpha control register
  41. * for hardware windows 1 ~ 4
  42. */
  43. #define VIDOSD_C(win) (VIDOSD_BASE + 0x08 + (win) * 16)
  44. /* size control register for hardware windows 1 ~ 2. */
  45. #define VIDOSD_D(win) (VIDOSD_BASE + 0x0C + (win) * 16)
  46. #define VIDWx_BUF_START(win, buf) (VIDW_BUF_START(buf) + (win) * 8)
  47. #define VIDWx_BUF_END(win, buf) (VIDW_BUF_END(buf) + (win) * 8)
  48. #define VIDWx_BUF_SIZE(win, buf) (VIDW_BUF_SIZE(buf) + (win) * 4)
  49. /* color key control register for hardware window 1 ~ 4. */
  50. #define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
  51. /* color key value register for hardware window 1 ~ 4. */
  52. #define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
  53. /* FIMD has totally five hardware windows. */
  54. #define WINDOWS_NR 5
  55. #define get_fimd_manager(mgr) platform_get_drvdata(to_platform_device(dev))
  56. struct fimd_driver_data {
  57. unsigned int timing_base;
  58. unsigned int has_shadowcon:1;
  59. unsigned int has_clksel:1;
  60. unsigned int has_limited_fmt:1;
  61. };
  62. static struct fimd_driver_data s3c64xx_fimd_driver_data = {
  63. .timing_base = 0x0,
  64. .has_clksel = 1,
  65. .has_limited_fmt = 1,
  66. };
  67. static struct fimd_driver_data exynos4_fimd_driver_data = {
  68. .timing_base = 0x0,
  69. .has_shadowcon = 1,
  70. };
  71. static struct fimd_driver_data exynos5_fimd_driver_data = {
  72. .timing_base = 0x20000,
  73. .has_shadowcon = 1,
  74. };
  75. struct fimd_win_data {
  76. unsigned int offset_x;
  77. unsigned int offset_y;
  78. unsigned int ovl_width;
  79. unsigned int ovl_height;
  80. unsigned int fb_width;
  81. unsigned int fb_height;
  82. unsigned int bpp;
  83. unsigned int pixel_format;
  84. dma_addr_t dma_addr;
  85. unsigned int buf_offsize;
  86. unsigned int line_size; /* bytes */
  87. bool enabled;
  88. bool resume;
  89. };
  90. struct fimd_context {
  91. struct exynos_drm_subdrv subdrv;
  92. struct device *dev;
  93. struct drm_device *drm_dev;
  94. int irq;
  95. struct drm_crtc *crtc;
  96. struct clk *bus_clk;
  97. struct clk *lcd_clk;
  98. void __iomem *regs;
  99. struct fimd_win_data win_data[WINDOWS_NR];
  100. unsigned int clkdiv;
  101. unsigned int default_win;
  102. unsigned long irq_flags;
  103. u32 vidcon0;
  104. u32 vidcon1;
  105. bool suspended;
  106. struct mutex lock;
  107. wait_queue_head_t wait_vsync_queue;
  108. atomic_t wait_vsync_event;
  109. struct exynos_drm_panel_info panel;
  110. struct fimd_driver_data *driver_data;
  111. };
  112. static const struct of_device_id fimd_driver_dt_match[] = {
  113. { .compatible = "samsung,s3c6400-fimd",
  114. .data = &s3c64xx_fimd_driver_data },
  115. { .compatible = "samsung,exynos4210-fimd",
  116. .data = &exynos4_fimd_driver_data },
  117. { .compatible = "samsung,exynos5250-fimd",
  118. .data = &exynos5_fimd_driver_data },
  119. {},
  120. };
  121. static inline struct fimd_driver_data *drm_fimd_get_driver_data(
  122. struct platform_device *pdev)
  123. {
  124. const struct of_device_id *of_id =
  125. of_match_device(fimd_driver_dt_match, &pdev->dev);
  126. return (struct fimd_driver_data *)of_id->data;
  127. }
  128. static bool fimd_display_is_connected(struct device *dev)
  129. {
  130. /* TODO. */
  131. return true;
  132. }
  133. static void *fimd_get_panel(struct device *dev)
  134. {
  135. struct exynos_drm_manager *mgr = get_fimd_manager(dev);
  136. struct fimd_context *ctx = mgr->ctx;
  137. return &ctx->panel;
  138. }
  139. static int fimd_check_mode(struct device *dev, struct drm_display_mode *mode)
  140. {
  141. /* TODO. */
  142. return 0;
  143. }
  144. static struct exynos_drm_display_ops fimd_display_ops = {
  145. .type = EXYNOS_DISPLAY_TYPE_LCD,
  146. .is_connected = fimd_display_is_connected,
  147. .get_panel = fimd_get_panel,
  148. .check_mode = fimd_check_mode,
  149. };
  150. static int fimd_mgr_initialize(struct exynos_drm_manager *mgr,
  151. struct drm_device *drm_dev)
  152. {
  153. struct fimd_context *ctx = mgr->ctx;
  154. ctx->drm_dev = drm_dev;
  155. return 0;
  156. }
  157. static void fimd_dpms(struct exynos_drm_manager *mgr, int mode)
  158. {
  159. struct fimd_context *ctx = mgr->ctx;
  160. DRM_DEBUG_KMS("%d\n", mode);
  161. mutex_lock(&ctx->lock);
  162. switch (mode) {
  163. case DRM_MODE_DPMS_ON:
  164. /*
  165. * enable fimd hardware only if suspended status.
  166. *
  167. * P.S. fimd_dpms function would be called at booting time so
  168. * clk_enable could be called double time.
  169. */
  170. if (ctx->suspended)
  171. pm_runtime_get_sync(ctx->dev);
  172. break;
  173. case DRM_MODE_DPMS_STANDBY:
  174. case DRM_MODE_DPMS_SUSPEND:
  175. case DRM_MODE_DPMS_OFF:
  176. if (!ctx->suspended)
  177. pm_runtime_put_sync(ctx->dev);
  178. break;
  179. default:
  180. DRM_DEBUG_KMS("unspecified mode %d\n", mode);
  181. break;
  182. }
  183. mutex_unlock(&ctx->lock);
  184. }
  185. static void fimd_apply(struct exynos_drm_manager *mgr)
  186. {
  187. struct fimd_context *ctx = mgr->ctx;
  188. struct exynos_drm_manager_ops *mgr_ops = mgr->ops;
  189. struct fimd_win_data *win_data;
  190. int i;
  191. for (i = 0; i < WINDOWS_NR; i++) {
  192. win_data = &ctx->win_data[i];
  193. if (win_data->enabled && (mgr_ops && mgr_ops->win_commit))
  194. mgr_ops->win_commit(mgr, i);
  195. }
  196. if (mgr_ops && mgr_ops->commit)
  197. mgr_ops->commit(mgr);
  198. }
  199. static void fimd_commit(struct exynos_drm_manager *mgr)
  200. {
  201. struct fimd_context *ctx = mgr->ctx;
  202. struct exynos_drm_panel_info *panel = &ctx->panel;
  203. struct videomode *vm = &panel->vm;
  204. struct fimd_driver_data *driver_data;
  205. u32 val;
  206. driver_data = ctx->driver_data;
  207. if (ctx->suspended)
  208. return;
  209. /* setup polarity values from machine code. */
  210. writel(ctx->vidcon1, ctx->regs + driver_data->timing_base + VIDCON1);
  211. /* setup vertical timing values. */
  212. val = VIDTCON0_VBPD(vm->vback_porch - 1) |
  213. VIDTCON0_VFPD(vm->vfront_porch - 1) |
  214. VIDTCON0_VSPW(vm->vsync_len - 1);
  215. writel(val, ctx->regs + driver_data->timing_base + VIDTCON0);
  216. /* setup horizontal timing values. */
  217. val = VIDTCON1_HBPD(vm->hback_porch - 1) |
  218. VIDTCON1_HFPD(vm->hfront_porch - 1) |
  219. VIDTCON1_HSPW(vm->hsync_len - 1);
  220. writel(val, ctx->regs + driver_data->timing_base + VIDTCON1);
  221. /* setup horizontal and vertical display size. */
  222. val = VIDTCON2_LINEVAL(vm->vactive - 1) |
  223. VIDTCON2_HOZVAL(vm->hactive - 1) |
  224. VIDTCON2_LINEVAL_E(vm->vactive - 1) |
  225. VIDTCON2_HOZVAL_E(vm->hactive - 1);
  226. writel(val, ctx->regs + driver_data->timing_base + VIDTCON2);
  227. /* setup clock source, clock divider, enable dma. */
  228. val = ctx->vidcon0;
  229. val &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
  230. if (ctx->driver_data->has_clksel) {
  231. val &= ~VIDCON0_CLKSEL_MASK;
  232. val |= VIDCON0_CLKSEL_LCD;
  233. }
  234. if (ctx->clkdiv > 1)
  235. val |= VIDCON0_CLKVAL_F(ctx->clkdiv - 1) | VIDCON0_CLKDIR;
  236. else
  237. val &= ~VIDCON0_CLKDIR; /* 1:1 clock */
  238. /*
  239. * fields of register with prefix '_F' would be updated
  240. * at vsync(same as dma start)
  241. */
  242. val |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  243. writel(val, ctx->regs + VIDCON0);
  244. }
  245. static int fimd_enable_vblank(struct exynos_drm_manager *mgr)
  246. {
  247. struct fimd_context *ctx = mgr->ctx;
  248. u32 val;
  249. if (ctx->suspended)
  250. return -EPERM;
  251. if (!test_and_set_bit(0, &ctx->irq_flags)) {
  252. val = readl(ctx->regs + VIDINTCON0);
  253. val |= VIDINTCON0_INT_ENABLE;
  254. val |= VIDINTCON0_INT_FRAME;
  255. val &= ~VIDINTCON0_FRAMESEL0_MASK;
  256. val |= VIDINTCON0_FRAMESEL0_VSYNC;
  257. val &= ~VIDINTCON0_FRAMESEL1_MASK;
  258. val |= VIDINTCON0_FRAMESEL1_NONE;
  259. writel(val, ctx->regs + VIDINTCON0);
  260. }
  261. return 0;
  262. }
  263. static void fimd_disable_vblank(struct exynos_drm_manager *mgr)
  264. {
  265. struct fimd_context *ctx = mgr->ctx;
  266. u32 val;
  267. if (ctx->suspended)
  268. return;
  269. if (test_and_clear_bit(0, &ctx->irq_flags)) {
  270. val = readl(ctx->regs + VIDINTCON0);
  271. val &= ~VIDINTCON0_INT_FRAME;
  272. val &= ~VIDINTCON0_INT_ENABLE;
  273. writel(val, ctx->regs + VIDINTCON0);
  274. }
  275. }
  276. static void fimd_wait_for_vblank(struct exynos_drm_manager *mgr)
  277. {
  278. struct fimd_context *ctx = mgr->ctx;
  279. if (ctx->suspended)
  280. return;
  281. atomic_set(&ctx->wait_vsync_event, 1);
  282. /*
  283. * wait for FIMD to signal VSYNC interrupt or return after
  284. * timeout which is set to 50ms (refresh rate of 20).
  285. */
  286. if (!wait_event_timeout(ctx->wait_vsync_queue,
  287. !atomic_read(&ctx->wait_vsync_event),
  288. HZ/20))
  289. DRM_DEBUG_KMS("vblank wait timed out.\n");
  290. }
  291. static void fimd_win_mode_set(struct exynos_drm_manager *mgr,
  292. struct exynos_drm_overlay *overlay)
  293. {
  294. struct fimd_context *ctx = mgr->ctx;
  295. struct fimd_win_data *win_data;
  296. int win;
  297. unsigned long offset;
  298. if (!overlay) {
  299. DRM_ERROR("overlay is NULL\n");
  300. return;
  301. }
  302. win = overlay->zpos;
  303. if (win == DEFAULT_ZPOS)
  304. win = ctx->default_win;
  305. if (win < 0 || win >= WINDOWS_NR)
  306. return;
  307. offset = overlay->fb_x * (overlay->bpp >> 3);
  308. offset += overlay->fb_y * overlay->pitch;
  309. DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, overlay->pitch);
  310. win_data = &ctx->win_data[win];
  311. win_data->offset_x = overlay->crtc_x;
  312. win_data->offset_y = overlay->crtc_y;
  313. win_data->ovl_width = overlay->crtc_width;
  314. win_data->ovl_height = overlay->crtc_height;
  315. win_data->fb_width = overlay->fb_width;
  316. win_data->fb_height = overlay->fb_height;
  317. win_data->dma_addr = overlay->dma_addr[0] + offset;
  318. win_data->bpp = overlay->bpp;
  319. win_data->pixel_format = overlay->pixel_format;
  320. win_data->buf_offsize = (overlay->fb_width - overlay->crtc_width) *
  321. (overlay->bpp >> 3);
  322. win_data->line_size = overlay->crtc_width * (overlay->bpp >> 3);
  323. DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
  324. win_data->offset_x, win_data->offset_y);
  325. DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
  326. win_data->ovl_width, win_data->ovl_height);
  327. DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
  328. DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
  329. overlay->fb_width, overlay->crtc_width);
  330. }
  331. static void fimd_win_set_pixfmt(struct fimd_context *ctx, unsigned int win)
  332. {
  333. struct fimd_win_data *win_data = &ctx->win_data[win];
  334. unsigned long val;
  335. val = WINCONx_ENWIN;
  336. /*
  337. * In case of s3c64xx, window 0 doesn't support alpha channel.
  338. * So the request format is ARGB8888 then change it to XRGB8888.
  339. */
  340. if (ctx->driver_data->has_limited_fmt && !win) {
  341. if (win_data->pixel_format == DRM_FORMAT_ARGB8888)
  342. win_data->pixel_format = DRM_FORMAT_XRGB8888;
  343. }
  344. switch (win_data->pixel_format) {
  345. case DRM_FORMAT_C8:
  346. val |= WINCON0_BPPMODE_8BPP_PALETTE;
  347. val |= WINCONx_BURSTLEN_8WORD;
  348. val |= WINCONx_BYTSWP;
  349. break;
  350. case DRM_FORMAT_XRGB1555:
  351. val |= WINCON0_BPPMODE_16BPP_1555;
  352. val |= WINCONx_HAWSWP;
  353. val |= WINCONx_BURSTLEN_16WORD;
  354. break;
  355. case DRM_FORMAT_RGB565:
  356. val |= WINCON0_BPPMODE_16BPP_565;
  357. val |= WINCONx_HAWSWP;
  358. val |= WINCONx_BURSTLEN_16WORD;
  359. break;
  360. case DRM_FORMAT_XRGB8888:
  361. val |= WINCON0_BPPMODE_24BPP_888;
  362. val |= WINCONx_WSWP;
  363. val |= WINCONx_BURSTLEN_16WORD;
  364. break;
  365. case DRM_FORMAT_ARGB8888:
  366. val |= WINCON1_BPPMODE_25BPP_A1888
  367. | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
  368. val |= WINCONx_WSWP;
  369. val |= WINCONx_BURSTLEN_16WORD;
  370. break;
  371. default:
  372. DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
  373. val |= WINCON0_BPPMODE_24BPP_888;
  374. val |= WINCONx_WSWP;
  375. val |= WINCONx_BURSTLEN_16WORD;
  376. break;
  377. }
  378. DRM_DEBUG_KMS("bpp = %d\n", win_data->bpp);
  379. writel(val, ctx->regs + WINCON(win));
  380. }
  381. static void fimd_win_set_colkey(struct fimd_context *ctx, unsigned int win)
  382. {
  383. unsigned int keycon0 = 0, keycon1 = 0;
  384. keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
  385. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  386. keycon1 = WxKEYCON1_COLVAL(0xffffffff);
  387. writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
  388. writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
  389. }
  390. /**
  391. * shadow_protect_win() - disable updating values from shadow registers at vsync
  392. *
  393. * @win: window to protect registers for
  394. * @protect: 1 to protect (disable updates)
  395. */
  396. static void fimd_shadow_protect_win(struct fimd_context *ctx,
  397. int win, bool protect)
  398. {
  399. u32 reg, bits, val;
  400. if (ctx->driver_data->has_shadowcon) {
  401. reg = SHADOWCON;
  402. bits = SHADOWCON_WINx_PROTECT(win);
  403. } else {
  404. reg = PRTCON;
  405. bits = PRTCON_PROTECT;
  406. }
  407. val = readl(ctx->regs + reg);
  408. if (protect)
  409. val |= bits;
  410. else
  411. val &= ~bits;
  412. writel(val, ctx->regs + reg);
  413. }
  414. static void fimd_win_commit(struct exynos_drm_manager *mgr, int zpos)
  415. {
  416. struct fimd_context *ctx = mgr->ctx;
  417. struct fimd_win_data *win_data;
  418. int win = zpos;
  419. unsigned long val, alpha, size;
  420. unsigned int last_x;
  421. unsigned int last_y;
  422. if (ctx->suspended)
  423. return;
  424. if (win == DEFAULT_ZPOS)
  425. win = ctx->default_win;
  426. if (win < 0 || win >= WINDOWS_NR)
  427. return;
  428. win_data = &ctx->win_data[win];
  429. /*
  430. * SHADOWCON/PRTCON register is used for enabling timing.
  431. *
  432. * for example, once only width value of a register is set,
  433. * if the dma is started then fimd hardware could malfunction so
  434. * with protect window setting, the register fields with prefix '_F'
  435. * wouldn't be updated at vsync also but updated once unprotect window
  436. * is set.
  437. */
  438. /* protect windows */
  439. fimd_shadow_protect_win(ctx, win, true);
  440. /* buffer start address */
  441. val = (unsigned long)win_data->dma_addr;
  442. writel(val, ctx->regs + VIDWx_BUF_START(win, 0));
  443. /* buffer end address */
  444. size = win_data->fb_width * win_data->ovl_height * (win_data->bpp >> 3);
  445. val = (unsigned long)(win_data->dma_addr + size);
  446. writel(val, ctx->regs + VIDWx_BUF_END(win, 0));
  447. DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
  448. (unsigned long)win_data->dma_addr, val, size);
  449. DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
  450. win_data->ovl_width, win_data->ovl_height);
  451. /* buffer size */
  452. val = VIDW_BUF_SIZE_OFFSET(win_data->buf_offsize) |
  453. VIDW_BUF_SIZE_PAGEWIDTH(win_data->line_size) |
  454. VIDW_BUF_SIZE_OFFSET_E(win_data->buf_offsize) |
  455. VIDW_BUF_SIZE_PAGEWIDTH_E(win_data->line_size);
  456. writel(val, ctx->regs + VIDWx_BUF_SIZE(win, 0));
  457. /* OSD position */
  458. val = VIDOSDxA_TOPLEFT_X(win_data->offset_x) |
  459. VIDOSDxA_TOPLEFT_Y(win_data->offset_y) |
  460. VIDOSDxA_TOPLEFT_X_E(win_data->offset_x) |
  461. VIDOSDxA_TOPLEFT_Y_E(win_data->offset_y);
  462. writel(val, ctx->regs + VIDOSD_A(win));
  463. last_x = win_data->offset_x + win_data->ovl_width;
  464. if (last_x)
  465. last_x--;
  466. last_y = win_data->offset_y + win_data->ovl_height;
  467. if (last_y)
  468. last_y--;
  469. val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y) |
  470. VIDOSDxB_BOTRIGHT_X_E(last_x) | VIDOSDxB_BOTRIGHT_Y_E(last_y);
  471. writel(val, ctx->regs + VIDOSD_B(win));
  472. DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
  473. win_data->offset_x, win_data->offset_y, last_x, last_y);
  474. /* hardware window 0 doesn't support alpha channel. */
  475. if (win != 0) {
  476. /* OSD alpha */
  477. alpha = VIDISD14C_ALPHA1_R(0xf) |
  478. VIDISD14C_ALPHA1_G(0xf) |
  479. VIDISD14C_ALPHA1_B(0xf);
  480. writel(alpha, ctx->regs + VIDOSD_C(win));
  481. }
  482. /* OSD size */
  483. if (win != 3 && win != 4) {
  484. u32 offset = VIDOSD_D(win);
  485. if (win == 0)
  486. offset = VIDOSD_C(win);
  487. val = win_data->ovl_width * win_data->ovl_height;
  488. writel(val, ctx->regs + offset);
  489. DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val);
  490. }
  491. fimd_win_set_pixfmt(ctx, win);
  492. /* hardware window 0 doesn't support color key. */
  493. if (win != 0)
  494. fimd_win_set_colkey(ctx, win);
  495. /* wincon */
  496. val = readl(ctx->regs + WINCON(win));
  497. val |= WINCONx_ENWIN;
  498. writel(val, ctx->regs + WINCON(win));
  499. /* Enable DMA channel and unprotect windows */
  500. fimd_shadow_protect_win(ctx, win, false);
  501. if (ctx->driver_data->has_shadowcon) {
  502. val = readl(ctx->regs + SHADOWCON);
  503. val |= SHADOWCON_CHx_ENABLE(win);
  504. writel(val, ctx->regs + SHADOWCON);
  505. }
  506. win_data->enabled = true;
  507. }
  508. static void fimd_win_disable(struct exynos_drm_manager *mgr, int zpos)
  509. {
  510. struct fimd_context *ctx = mgr->ctx;
  511. struct fimd_win_data *win_data;
  512. int win = zpos;
  513. u32 val;
  514. if (win == DEFAULT_ZPOS)
  515. win = ctx->default_win;
  516. if (win < 0 || win >= WINDOWS_NR)
  517. return;
  518. win_data = &ctx->win_data[win];
  519. if (ctx->suspended) {
  520. /* do not resume this window*/
  521. win_data->resume = false;
  522. return;
  523. }
  524. /* protect windows */
  525. fimd_shadow_protect_win(ctx, win, true);
  526. /* wincon */
  527. val = readl(ctx->regs + WINCON(win));
  528. val &= ~WINCONx_ENWIN;
  529. writel(val, ctx->regs + WINCON(win));
  530. /* unprotect windows */
  531. if (ctx->driver_data->has_shadowcon) {
  532. val = readl(ctx->regs + SHADOWCON);
  533. val &= ~SHADOWCON_CHx_ENABLE(win);
  534. writel(val, ctx->regs + SHADOWCON);
  535. }
  536. fimd_shadow_protect_win(ctx, win, false);
  537. win_data->enabled = false;
  538. }
  539. static struct exynos_drm_manager_ops fimd_manager_ops = {
  540. .initialize = fimd_mgr_initialize,
  541. .dpms = fimd_dpms,
  542. .commit = fimd_commit,
  543. .enable_vblank = fimd_enable_vblank,
  544. .disable_vblank = fimd_disable_vblank,
  545. .wait_for_vblank = fimd_wait_for_vblank,
  546. .win_mode_set = fimd_win_mode_set,
  547. .win_commit = fimd_win_commit,
  548. .win_disable = fimd_win_disable,
  549. };
  550. static struct exynos_drm_manager fimd_manager = {
  551. .pipe = -1,
  552. .ops = &fimd_manager_ops,
  553. .display_ops = &fimd_display_ops,
  554. };
  555. static irqreturn_t fimd_irq_handler(int irq, void *dev_id)
  556. {
  557. struct fimd_context *ctx = (struct fimd_context *)dev_id;
  558. struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
  559. struct exynos_drm_manager *manager = subdrv->manager;
  560. u32 val;
  561. val = readl(ctx->regs + VIDINTCON1);
  562. if (val & VIDINTCON1_INT_FRAME)
  563. /* VSYNC interrupt */
  564. writel(VIDINTCON1_INT_FRAME, ctx->regs + VIDINTCON1);
  565. /* check the crtc is detached already from encoder */
  566. if (manager->pipe < 0 || !ctx->drm_dev)
  567. goto out;
  568. drm_handle_vblank(ctx->drm_dev, manager->pipe);
  569. exynos_drm_crtc_finish_pageflip(ctx->drm_dev, manager->pipe);
  570. /* set wait vsync event to zero and wake up queue. */
  571. if (atomic_read(&ctx->wait_vsync_event)) {
  572. atomic_set(&ctx->wait_vsync_event, 0);
  573. wake_up(&ctx->wait_vsync_queue);
  574. }
  575. out:
  576. return IRQ_HANDLED;
  577. }
  578. static int fimd_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
  579. {
  580. /*
  581. * enable drm irq mode.
  582. * - with irq_enabled = true, we can use the vblank feature.
  583. *
  584. * P.S. note that we wouldn't use drm irq handler but
  585. * just specific driver own one instead because
  586. * drm framework supports only one irq handler.
  587. */
  588. drm_dev->irq_enabled = true;
  589. /*
  590. * with vblank_disable_allowed = true, vblank interrupt will be disabled
  591. * by drm timer once a current process gives up ownership of
  592. * vblank event.(after drm_vblank_put function is called)
  593. */
  594. drm_dev->vblank_disable_allowed = true;
  595. /* attach this sub driver to iommu mapping if supported. */
  596. if (is_drm_iommu_supported(drm_dev))
  597. drm_iommu_attach_device(drm_dev, dev);
  598. return 0;
  599. }
  600. static void fimd_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
  601. {
  602. /* detach this sub driver from iommu mapping if supported. */
  603. if (is_drm_iommu_supported(drm_dev))
  604. drm_iommu_detach_device(drm_dev, dev);
  605. }
  606. static int fimd_configure_clocks(struct fimd_context *ctx, struct device *dev)
  607. {
  608. struct videomode *vm = &ctx->panel.vm;
  609. unsigned long clk;
  610. ctx->bus_clk = devm_clk_get(dev, "fimd");
  611. if (IS_ERR(ctx->bus_clk)) {
  612. dev_err(dev, "failed to get bus clock\n");
  613. return PTR_ERR(ctx->bus_clk);
  614. }
  615. ctx->lcd_clk = devm_clk_get(dev, "sclk_fimd");
  616. if (IS_ERR(ctx->lcd_clk)) {
  617. dev_err(dev, "failed to get lcd clock\n");
  618. return PTR_ERR(ctx->lcd_clk);
  619. }
  620. clk = clk_get_rate(ctx->lcd_clk);
  621. if (clk == 0) {
  622. dev_err(dev, "error getting sclk_fimd clock rate\n");
  623. return -EINVAL;
  624. }
  625. if (vm->pixelclock == 0) {
  626. unsigned long c;
  627. c = vm->hactive + vm->hback_porch + vm->hfront_porch +
  628. vm->hsync_len;
  629. c *= vm->vactive + vm->vback_porch + vm->vfront_porch +
  630. vm->vsync_len;
  631. vm->pixelclock = c * FIMD_DEFAULT_FRAMERATE;
  632. if (vm->pixelclock == 0) {
  633. dev_err(dev, "incorrect display timings\n");
  634. return -EINVAL;
  635. }
  636. dev_warn(dev, "pixel clock recalculated to %luHz (%dHz frame rate)\n",
  637. vm->pixelclock, FIMD_DEFAULT_FRAMERATE);
  638. }
  639. ctx->clkdiv = DIV_ROUND_UP(clk, vm->pixelclock);
  640. if (ctx->clkdiv > 256) {
  641. dev_warn(dev, "calculated pixel clock divider too high (%u), lowered to 256\n",
  642. ctx->clkdiv);
  643. ctx->clkdiv = 256;
  644. }
  645. vm->pixelclock = clk / ctx->clkdiv;
  646. DRM_DEBUG_KMS("pixel clock = %lu, clkdiv = %d\n", vm->pixelclock,
  647. ctx->clkdiv);
  648. return 0;
  649. }
  650. static void fimd_clear_win(struct fimd_context *ctx, int win)
  651. {
  652. writel(0, ctx->regs + WINCON(win));
  653. writel(0, ctx->regs + VIDOSD_A(win));
  654. writel(0, ctx->regs + VIDOSD_B(win));
  655. writel(0, ctx->regs + VIDOSD_C(win));
  656. if (win == 1 || win == 2)
  657. writel(0, ctx->regs + VIDOSD_D(win));
  658. fimd_shadow_protect_win(ctx, win, false);
  659. }
  660. static int fimd_clock(struct fimd_context *ctx, bool enable)
  661. {
  662. if (enable) {
  663. int ret;
  664. ret = clk_prepare_enable(ctx->bus_clk);
  665. if (ret < 0)
  666. return ret;
  667. ret = clk_prepare_enable(ctx->lcd_clk);
  668. if (ret < 0) {
  669. clk_disable_unprepare(ctx->bus_clk);
  670. return ret;
  671. }
  672. } else {
  673. clk_disable_unprepare(ctx->lcd_clk);
  674. clk_disable_unprepare(ctx->bus_clk);
  675. }
  676. return 0;
  677. }
  678. static void fimd_window_suspend(struct device *dev)
  679. {
  680. struct exynos_drm_manager *mgr = get_fimd_manager(dev);
  681. struct fimd_context *ctx = mgr->ctx;
  682. struct fimd_win_data *win_data;
  683. int i;
  684. for (i = 0; i < WINDOWS_NR; i++) {
  685. win_data = &ctx->win_data[i];
  686. win_data->resume = win_data->enabled;
  687. fimd_win_disable(mgr, i);
  688. }
  689. fimd_wait_for_vblank(mgr);
  690. }
  691. static void fimd_window_resume(struct device *dev)
  692. {
  693. struct exynos_drm_manager *mgr = get_fimd_manager(dev);
  694. struct fimd_context *ctx = mgr->ctx;
  695. struct fimd_win_data *win_data;
  696. int i;
  697. for (i = 0; i < WINDOWS_NR; i++) {
  698. win_data = &ctx->win_data[i];
  699. win_data->enabled = win_data->resume;
  700. win_data->resume = false;
  701. }
  702. }
  703. static int fimd_activate(struct exynos_drm_manager *mgr, bool enable)
  704. {
  705. struct fimd_context *ctx = mgr->ctx;
  706. struct device *dev = ctx->subdrv.dev;
  707. if (enable) {
  708. int ret;
  709. ret = fimd_clock(ctx, true);
  710. if (ret < 0)
  711. return ret;
  712. ctx->suspended = false;
  713. /* if vblank was enabled status, enable it again. */
  714. if (test_and_clear_bit(0, &ctx->irq_flags))
  715. fimd_enable_vblank(mgr);
  716. fimd_window_resume(dev);
  717. fimd_apply(mgr);
  718. } else {
  719. fimd_window_suspend(dev);
  720. fimd_clock(ctx, false);
  721. ctx->suspended = true;
  722. }
  723. return 0;
  724. }
  725. static int fimd_get_platform_data(struct fimd_context *ctx, struct device *dev)
  726. {
  727. struct videomode *vm;
  728. int ret;
  729. vm = &ctx->panel.vm;
  730. ret = of_get_videomode(dev->of_node, vm, OF_USE_NATIVE_MODE);
  731. if (ret) {
  732. DRM_ERROR("failed: of_get_videomode() : %d\n", ret);
  733. return ret;
  734. }
  735. if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
  736. ctx->vidcon1 |= VIDCON1_INV_VSYNC;
  737. if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
  738. ctx->vidcon1 |= VIDCON1_INV_HSYNC;
  739. if (vm->flags & DISPLAY_FLAGS_DE_LOW)
  740. ctx->vidcon1 |= VIDCON1_INV_VDEN;
  741. if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
  742. ctx->vidcon1 |= VIDCON1_INV_VCLK;
  743. return 0;
  744. }
  745. static int fimd_probe(struct platform_device *pdev)
  746. {
  747. struct device *dev = &pdev->dev;
  748. struct fimd_context *ctx;
  749. struct exynos_drm_subdrv *subdrv;
  750. struct resource *res;
  751. int win;
  752. int ret = -EINVAL;
  753. if (!dev->of_node)
  754. return -ENODEV;
  755. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  756. if (!ctx)
  757. return -ENOMEM;
  758. ctx->dev = dev;
  759. ret = fimd_get_platform_data(ctx, dev);
  760. if (ret)
  761. return ret;
  762. ret = fimd_configure_clocks(ctx, dev);
  763. if (ret)
  764. return ret;
  765. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  766. ctx->regs = devm_ioremap_resource(dev, res);
  767. if (IS_ERR(ctx->regs))
  768. return PTR_ERR(ctx->regs);
  769. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "vsync");
  770. if (!res) {
  771. dev_err(dev, "irq request failed.\n");
  772. return -ENXIO;
  773. }
  774. ctx->irq = res->start;
  775. ret = devm_request_irq(dev, ctx->irq, fimd_irq_handler,
  776. 0, "drm_fimd", ctx);
  777. if (ret) {
  778. dev_err(dev, "irq request failed.\n");
  779. return ret;
  780. }
  781. ctx->driver_data = drm_fimd_get_driver_data(pdev);
  782. init_waitqueue_head(&ctx->wait_vsync_queue);
  783. atomic_set(&ctx->wait_vsync_event, 0);
  784. fimd_manager.ctx = ctx;
  785. subdrv = &ctx->subdrv;
  786. subdrv->dev = dev;
  787. subdrv->manager = &fimd_manager;
  788. subdrv->probe = fimd_subdrv_probe;
  789. subdrv->remove = fimd_subdrv_remove;
  790. mutex_init(&ctx->lock);
  791. platform_set_drvdata(pdev, &fimd_manager);
  792. pm_runtime_enable(dev);
  793. pm_runtime_get_sync(dev);
  794. for (win = 0; win < WINDOWS_NR; win++)
  795. fimd_clear_win(ctx, win);
  796. exynos_drm_subdrv_register(subdrv);
  797. return 0;
  798. }
  799. static int fimd_remove(struct platform_device *pdev)
  800. {
  801. struct device *dev = &pdev->dev;
  802. struct exynos_drm_manager *mgr = platform_get_drvdata(pdev);
  803. struct fimd_context *ctx = mgr->ctx;
  804. exynos_drm_subdrv_unregister(&ctx->subdrv);
  805. if (ctx->suspended)
  806. goto out;
  807. pm_runtime_set_suspended(dev);
  808. pm_runtime_put_sync(dev);
  809. out:
  810. pm_runtime_disable(dev);
  811. return 0;
  812. }
  813. #ifdef CONFIG_PM_SLEEP
  814. static int fimd_suspend(struct device *dev)
  815. {
  816. struct exynos_drm_manager *mgr = get_fimd_manager(dev);
  817. /*
  818. * do not use pm_runtime_suspend(). if pm_runtime_suspend() is
  819. * called here, an error would be returned by that interface
  820. * because the usage_count of pm runtime is more than 1.
  821. */
  822. if (!pm_runtime_suspended(dev))
  823. return fimd_activate(mgr, false);
  824. return 0;
  825. }
  826. static int fimd_resume(struct device *dev)
  827. {
  828. struct exynos_drm_manager *mgr = get_fimd_manager(dev);
  829. /*
  830. * if entered to sleep when lcd panel was on, the usage_count
  831. * of pm runtime would still be 1 so in this case, fimd driver
  832. * should be on directly not drawing on pm runtime interface.
  833. */
  834. if (pm_runtime_suspended(dev))
  835. return 0;
  836. return fimd_activate(mgr, true);
  837. }
  838. #endif
  839. #ifdef CONFIG_PM_RUNTIME
  840. static int fimd_runtime_suspend(struct device *dev)
  841. {
  842. struct exynos_drm_manager *mgr = get_fimd_manager(dev);
  843. return fimd_activate(mgr, false);
  844. }
  845. static int fimd_runtime_resume(struct device *dev)
  846. {
  847. struct exynos_drm_manager *mgr = get_fimd_manager(dev);
  848. return fimd_activate(mgr, true);
  849. }
  850. #endif
  851. static const struct dev_pm_ops fimd_pm_ops = {
  852. SET_SYSTEM_SLEEP_PM_OPS(fimd_suspend, fimd_resume)
  853. SET_RUNTIME_PM_OPS(fimd_runtime_suspend, fimd_runtime_resume, NULL)
  854. };
  855. struct platform_driver fimd_driver = {
  856. .probe = fimd_probe,
  857. .remove = fimd_remove,
  858. .driver = {
  859. .name = "exynos4-fb",
  860. .owner = THIS_MODULE,
  861. .pm = &fimd_pm_ops,
  862. .of_match_table = fimd_driver_dt_match,
  863. },
  864. };