exynos_drm_fimd.c 27 KB

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