3 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Joonyoung Shim <jy0922.shim@samsung.com>
6 * Inki Dae <inki.dae@samsung.com>
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.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/clk.h>
20 #include <linux/of_device.h>
21 #include <linux/pm_runtime.h>
23 #include <video/of_display_timing.h>
24 #include <video/samsung_fimd.h>
25 #include <drm/exynos_drm.h>
27 #include "exynos_drm_drv.h"
28 #include "exynos_drm_fbdev.h"
29 #include "exynos_drm_crtc.h"
30 #include "exynos_drm_iommu.h"
33 * FIMD is stand for Fully Interactive Mobile Display and
34 * as a display controller, it transfers contents drawn on memory
35 * to a LCD Panel through Display Interfaces such as RGB or
39 /* position control register for hardware window 0, 2 ~ 4.*/
40 #define VIDOSD_A(win) (VIDOSD_BASE + 0x00 + (win) * 16)
41 #define VIDOSD_B(win) (VIDOSD_BASE + 0x04 + (win) * 16)
43 * size control register for hardware windows 0 and alpha control register
44 * for hardware windows 1 ~ 4
46 #define VIDOSD_C(win) (VIDOSD_BASE + 0x08 + (win) * 16)
47 /* size control register for hardware windows 1 ~ 2. */
48 #define VIDOSD_D(win) (VIDOSD_BASE + 0x0C + (win) * 16)
50 #define VIDWx_BUF_START(win, buf) (VIDW_BUF_START(buf) + (win) * 8)
51 #define VIDWx_BUF_END(win, buf) (VIDW_BUF_END(buf) + (win) * 8)
52 #define VIDWx_BUF_SIZE(win, buf) (VIDW_BUF_SIZE(buf) + (win) * 4)
54 /* color key control register for hardware window 1 ~ 4. */
55 #define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
56 /* color key value register for hardware window 1 ~ 4. */
57 #define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
59 /* FIMD has totally five hardware windows. */
62 #define get_fimd_context(dev) platform_get_drvdata(to_platform_device(dev))
64 struct fimd_driver_data
{
65 unsigned int timing_base
;
67 unsigned int has_shadowcon
:1;
68 unsigned int has_clksel
:1;
71 static struct fimd_driver_data s3c64xx_fimd_driver_data
= {
76 static struct fimd_driver_data exynos4_fimd_driver_data
= {
81 static struct fimd_driver_data exynos5_fimd_driver_data
= {
82 .timing_base
= 0x20000,
86 struct fimd_win_data
{
87 unsigned int offset_x
;
88 unsigned int offset_y
;
89 unsigned int ovl_width
;
90 unsigned int ovl_height
;
91 unsigned int fb_width
;
92 unsigned int fb_height
;
95 unsigned int buf_offsize
;
96 unsigned int line_size
; /* bytes */
101 struct fimd_context
{
102 struct exynos_drm_subdrv subdrv
;
104 struct drm_crtc
*crtc
;
108 struct fimd_win_data win_data
[WINDOWS_NR
];
110 unsigned int default_win
;
111 unsigned long irq_flags
;
116 wait_queue_head_t wait_vsync_queue
;
117 atomic_t wait_vsync_event
;
119 struct exynos_drm_panel_info
*panel
;
120 struct fimd_driver_data
*driver_data
;
124 static const struct of_device_id fimd_driver_dt_match
[] = {
125 { .compatible
= "samsung,s3c6400-fimd",
126 .data
= &s3c64xx_fimd_driver_data
},
127 { .compatible
= "samsung,exynos4210-fimd",
128 .data
= &exynos4_fimd_driver_data
},
129 { .compatible
= "samsung,exynos5250-fimd",
130 .data
= &exynos5_fimd_driver_data
},
133 MODULE_DEVICE_TABLE(of
, fimd_driver_dt_match
);
136 static inline struct fimd_driver_data
*drm_fimd_get_driver_data(
137 struct platform_device
*pdev
)
140 const struct of_device_id
*of_id
=
141 of_match_device(fimd_driver_dt_match
, &pdev
->dev
);
144 return (struct fimd_driver_data
*)of_id
->data
;
147 return (struct fimd_driver_data
*)
148 platform_get_device_id(pdev
)->driver_data
;
151 static bool fimd_display_is_connected(struct device
*dev
)
158 static void *fimd_get_panel(struct device
*dev
)
160 struct fimd_context
*ctx
= get_fimd_context(dev
);
165 static int fimd_check_mode(struct device
*dev
, struct drm_display_mode
*mode
)
172 static int fimd_display_power_on(struct device
*dev
, int mode
)
179 static struct exynos_drm_display_ops fimd_display_ops
= {
180 .type
= EXYNOS_DISPLAY_TYPE_LCD
,
181 .is_connected
= fimd_display_is_connected
,
182 .get_panel
= fimd_get_panel
,
183 .check_mode
= fimd_check_mode
,
184 .power_on
= fimd_display_power_on
,
187 static void fimd_dpms(struct device
*subdrv_dev
, int mode
)
189 struct fimd_context
*ctx
= get_fimd_context(subdrv_dev
);
191 DRM_DEBUG_KMS("%d\n", mode
);
193 mutex_lock(&ctx
->lock
);
196 case DRM_MODE_DPMS_ON
:
198 * enable fimd hardware only if suspended status.
200 * P.S. fimd_dpms function would be called at booting time so
201 * clk_enable could be called double time.
204 pm_runtime_get_sync(subdrv_dev
);
206 case DRM_MODE_DPMS_STANDBY
:
207 case DRM_MODE_DPMS_SUSPEND
:
208 case DRM_MODE_DPMS_OFF
:
210 pm_runtime_put_sync(subdrv_dev
);
213 DRM_DEBUG_KMS("unspecified mode %d\n", mode
);
217 mutex_unlock(&ctx
->lock
);
220 static void fimd_apply(struct device
*subdrv_dev
)
222 struct fimd_context
*ctx
= get_fimd_context(subdrv_dev
);
223 struct exynos_drm_manager
*mgr
= ctx
->subdrv
.manager
;
224 struct exynos_drm_manager_ops
*mgr_ops
= mgr
->ops
;
225 struct exynos_drm_overlay_ops
*ovl_ops
= mgr
->overlay_ops
;
226 struct fimd_win_data
*win_data
;
229 for (i
= 0; i
< WINDOWS_NR
; i
++) {
230 win_data
= &ctx
->win_data
[i
];
231 if (win_data
->enabled
&& (ovl_ops
&& ovl_ops
->commit
))
232 ovl_ops
->commit(subdrv_dev
, i
);
235 if (mgr_ops
&& mgr_ops
->commit
)
236 mgr_ops
->commit(subdrv_dev
);
239 static void fimd_commit(struct device
*dev
)
241 struct fimd_context
*ctx
= get_fimd_context(dev
);
242 struct exynos_drm_panel_info
*panel
= ctx
->panel
;
243 struct fb_videomode
*timing
= &panel
->timing
;
244 struct fimd_driver_data
*driver_data
;
247 driver_data
= ctx
->driver_data
;
251 /* setup polarity values from machine code. */
252 writel(ctx
->vidcon1
, ctx
->regs
+ driver_data
->timing_base
+ VIDCON1
);
254 /* setup vertical timing values. */
255 val
= VIDTCON0_VBPD(timing
->upper_margin
- 1) |
256 VIDTCON0_VFPD(timing
->lower_margin
- 1) |
257 VIDTCON0_VSPW(timing
->vsync_len
- 1);
258 writel(val
, ctx
->regs
+ driver_data
->timing_base
+ VIDTCON0
);
260 /* setup horizontal timing values. */
261 val
= VIDTCON1_HBPD(timing
->left_margin
- 1) |
262 VIDTCON1_HFPD(timing
->right_margin
- 1) |
263 VIDTCON1_HSPW(timing
->hsync_len
- 1);
264 writel(val
, ctx
->regs
+ driver_data
->timing_base
+ VIDTCON1
);
266 /* setup horizontal and vertical display size. */
267 val
= VIDTCON2_LINEVAL(timing
->yres
- 1) |
268 VIDTCON2_HOZVAL(timing
->xres
- 1) |
269 VIDTCON2_LINEVAL_E(timing
->yres
- 1) |
270 VIDTCON2_HOZVAL_E(timing
->xres
- 1);
271 writel(val
, ctx
->regs
+ driver_data
->timing_base
+ VIDTCON2
);
273 /* setup clock source, clock divider, enable dma. */
275 val
&= ~(VIDCON0_CLKVAL_F_MASK
| VIDCON0_CLKDIR
);
277 if (ctx
->driver_data
->has_clksel
) {
278 val
&= ~VIDCON0_CLKSEL_MASK
;
279 val
|= VIDCON0_CLKSEL_LCD
;
283 val
|= VIDCON0_CLKVAL_F(ctx
->clkdiv
- 1) | VIDCON0_CLKDIR
;
285 val
&= ~VIDCON0_CLKDIR
; /* 1:1 clock */
288 * fields of register with prefix '_F' would be updated
289 * at vsync(same as dma start)
291 val
|= VIDCON0_ENVID
| VIDCON0_ENVID_F
;
292 writel(val
, ctx
->regs
+ VIDCON0
);
295 static int fimd_enable_vblank(struct device
*dev
)
297 struct fimd_context
*ctx
= get_fimd_context(dev
);
303 if (!test_and_set_bit(0, &ctx
->irq_flags
)) {
304 val
= readl(ctx
->regs
+ VIDINTCON0
);
306 val
|= VIDINTCON0_INT_ENABLE
;
307 val
|= VIDINTCON0_INT_FRAME
;
309 val
&= ~VIDINTCON0_FRAMESEL0_MASK
;
310 val
|= VIDINTCON0_FRAMESEL0_VSYNC
;
311 val
&= ~VIDINTCON0_FRAMESEL1_MASK
;
312 val
|= VIDINTCON0_FRAMESEL1_NONE
;
314 writel(val
, ctx
->regs
+ VIDINTCON0
);
320 static void fimd_disable_vblank(struct device
*dev
)
322 struct fimd_context
*ctx
= get_fimd_context(dev
);
328 if (test_and_clear_bit(0, &ctx
->irq_flags
)) {
329 val
= readl(ctx
->regs
+ VIDINTCON0
);
331 val
&= ~VIDINTCON0_INT_FRAME
;
332 val
&= ~VIDINTCON0_INT_ENABLE
;
334 writel(val
, ctx
->regs
+ VIDINTCON0
);
338 static void fimd_wait_for_vblank(struct device
*dev
)
340 struct fimd_context
*ctx
= get_fimd_context(dev
);
345 atomic_set(&ctx
->wait_vsync_event
, 1);
348 * wait for FIMD to signal VSYNC interrupt or return after
349 * timeout which is set to 50ms (refresh rate of 20).
351 if (!wait_event_timeout(ctx
->wait_vsync_queue
,
352 !atomic_read(&ctx
->wait_vsync_event
),
354 DRM_DEBUG_KMS("vblank wait timed out.\n");
357 static struct exynos_drm_manager_ops fimd_manager_ops
= {
360 .commit
= fimd_commit
,
361 .enable_vblank
= fimd_enable_vblank
,
362 .disable_vblank
= fimd_disable_vblank
,
363 .wait_for_vblank
= fimd_wait_for_vblank
,
366 static void fimd_win_mode_set(struct device
*dev
,
367 struct exynos_drm_overlay
*overlay
)
369 struct fimd_context
*ctx
= get_fimd_context(dev
);
370 struct fimd_win_data
*win_data
;
372 unsigned long offset
;
375 dev_err(dev
, "overlay is NULL\n");
380 if (win
== DEFAULT_ZPOS
)
381 win
= ctx
->default_win
;
383 if (win
< 0 || win
>= WINDOWS_NR
)
386 offset
= overlay
->fb_x
* (overlay
->bpp
>> 3);
387 offset
+= overlay
->fb_y
* overlay
->pitch
;
389 DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset
, overlay
->pitch
);
391 win_data
= &ctx
->win_data
[win
];
393 win_data
->offset_x
= overlay
->crtc_x
;
394 win_data
->offset_y
= overlay
->crtc_y
;
395 win_data
->ovl_width
= overlay
->crtc_width
;
396 win_data
->ovl_height
= overlay
->crtc_height
;
397 win_data
->fb_width
= overlay
->fb_width
;
398 win_data
->fb_height
= overlay
->fb_height
;
399 win_data
->dma_addr
= overlay
->dma_addr
[0] + offset
;
400 win_data
->bpp
= overlay
->bpp
;
401 win_data
->buf_offsize
= (overlay
->fb_width
- overlay
->crtc_width
) *
403 win_data
->line_size
= overlay
->crtc_width
* (overlay
->bpp
>> 3);
405 DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
406 win_data
->offset_x
, win_data
->offset_y
);
407 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
408 win_data
->ovl_width
, win_data
->ovl_height
);
409 DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data
->dma_addr
);
410 DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
411 overlay
->fb_width
, overlay
->crtc_width
);
414 static void fimd_win_set_pixfmt(struct device
*dev
, unsigned int win
)
416 struct fimd_context
*ctx
= get_fimd_context(dev
);
417 struct fimd_win_data
*win_data
= &ctx
->win_data
[win
];
422 switch (win_data
->bpp
) {
424 val
|= WINCON0_BPPMODE_1BPP
;
425 val
|= WINCONx_BITSWP
;
426 val
|= WINCONx_BURSTLEN_4WORD
;
429 val
|= WINCON0_BPPMODE_2BPP
;
430 val
|= WINCONx_BITSWP
;
431 val
|= WINCONx_BURSTLEN_8WORD
;
434 val
|= WINCON0_BPPMODE_4BPP
;
435 val
|= WINCONx_BITSWP
;
436 val
|= WINCONx_BURSTLEN_8WORD
;
439 val
|= WINCON0_BPPMODE_8BPP_PALETTE
;
440 val
|= WINCONx_BURSTLEN_8WORD
;
441 val
|= WINCONx_BYTSWP
;
444 val
|= WINCON0_BPPMODE_16BPP_565
;
445 val
|= WINCONx_HAWSWP
;
446 val
|= WINCONx_BURSTLEN_16WORD
;
449 val
|= WINCON0_BPPMODE_24BPP_888
;
451 val
|= WINCONx_BURSTLEN_16WORD
;
454 val
|= WINCON1_BPPMODE_28BPP_A4888
455 | WINCON1_BLD_PIX
| WINCON1_ALPHA_SEL
;
457 val
|= WINCONx_BURSTLEN_16WORD
;
460 DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
462 val
|= WINCON0_BPPMODE_24BPP_888
;
464 val
|= WINCONx_BURSTLEN_16WORD
;
468 DRM_DEBUG_KMS("bpp = %d\n", win_data
->bpp
);
470 writel(val
, ctx
->regs
+ WINCON(win
));
473 static void fimd_win_set_colkey(struct device
*dev
, unsigned int win
)
475 struct fimd_context
*ctx
= get_fimd_context(dev
);
476 unsigned int keycon0
= 0, keycon1
= 0;
478 keycon0
= ~(WxKEYCON0_KEYBL_EN
| WxKEYCON0_KEYEN_F
|
479 WxKEYCON0_DIRCON
) | WxKEYCON0_COMPKEY(0);
481 keycon1
= WxKEYCON1_COLVAL(0xffffffff);
483 writel(keycon0
, ctx
->regs
+ WKEYCON0_BASE(win
));
484 writel(keycon1
, ctx
->regs
+ WKEYCON1_BASE(win
));
488 * shadow_protect_win() - disable updating values from shadow registers at vsync
490 * @win: window to protect registers for
491 * @protect: 1 to protect (disable updates)
493 static void fimd_shadow_protect_win(struct fimd_context
*ctx
,
494 int win
, bool protect
)
498 if (ctx
->driver_data
->has_shadowcon
) {
500 bits
= SHADOWCON_WINx_PROTECT(win
);
503 bits
= PRTCON_PROTECT
;
506 val
= readl(ctx
->regs
+ reg
);
511 writel(val
, ctx
->regs
+ reg
);
514 static void fimd_win_commit(struct device
*dev
, int zpos
)
516 struct fimd_context
*ctx
= get_fimd_context(dev
);
517 struct fimd_win_data
*win_data
;
519 unsigned long val
, alpha
, size
;
526 if (win
== DEFAULT_ZPOS
)
527 win
= ctx
->default_win
;
529 if (win
< 0 || win
>= WINDOWS_NR
)
532 win_data
= &ctx
->win_data
[win
];
535 * SHADOWCON/PRTCON register is used for enabling timing.
537 * for example, once only width value of a register is set,
538 * if the dma is started then fimd hardware could malfunction so
539 * with protect window setting, the register fields with prefix '_F'
540 * wouldn't be updated at vsync also but updated once unprotect window
544 /* protect windows */
545 fimd_shadow_protect_win(ctx
, win
, true);
547 /* buffer start address */
548 val
= (unsigned long)win_data
->dma_addr
;
549 writel(val
, ctx
->regs
+ VIDWx_BUF_START(win
, 0));
551 /* buffer end address */
552 size
= win_data
->fb_width
* win_data
->ovl_height
* (win_data
->bpp
>> 3);
553 val
= (unsigned long)(win_data
->dma_addr
+ size
);
554 writel(val
, ctx
->regs
+ VIDWx_BUF_END(win
, 0));
556 DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
557 (unsigned long)win_data
->dma_addr
, val
, size
);
558 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
559 win_data
->ovl_width
, win_data
->ovl_height
);
562 val
= VIDW_BUF_SIZE_OFFSET(win_data
->buf_offsize
) |
563 VIDW_BUF_SIZE_PAGEWIDTH(win_data
->line_size
) |
564 VIDW_BUF_SIZE_OFFSET_E(win_data
->buf_offsize
) |
565 VIDW_BUF_SIZE_PAGEWIDTH_E(win_data
->line_size
);
566 writel(val
, ctx
->regs
+ VIDWx_BUF_SIZE(win
, 0));
569 val
= VIDOSDxA_TOPLEFT_X(win_data
->offset_x
) |
570 VIDOSDxA_TOPLEFT_Y(win_data
->offset_y
) |
571 VIDOSDxA_TOPLEFT_X_E(win_data
->offset_x
) |
572 VIDOSDxA_TOPLEFT_Y_E(win_data
->offset_y
);
573 writel(val
, ctx
->regs
+ VIDOSD_A(win
));
575 last_x
= win_data
->offset_x
+ win_data
->ovl_width
;
578 last_y
= win_data
->offset_y
+ win_data
->ovl_height
;
582 val
= VIDOSDxB_BOTRIGHT_X(last_x
) | VIDOSDxB_BOTRIGHT_Y(last_y
) |
583 VIDOSDxB_BOTRIGHT_X_E(last_x
) | VIDOSDxB_BOTRIGHT_Y_E(last_y
);
585 writel(val
, ctx
->regs
+ VIDOSD_B(win
));
587 DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
588 win_data
->offset_x
, win_data
->offset_y
, last_x
, last_y
);
590 /* hardware window 0 doesn't support alpha channel. */
593 alpha
= VIDISD14C_ALPHA1_R(0xf) |
594 VIDISD14C_ALPHA1_G(0xf) |
595 VIDISD14C_ALPHA1_B(0xf);
597 writel(alpha
, ctx
->regs
+ VIDOSD_C(win
));
601 if (win
!= 3 && win
!= 4) {
602 u32 offset
= VIDOSD_D(win
);
604 offset
= VIDOSD_C(win
);
605 val
= win_data
->ovl_width
* win_data
->ovl_height
;
606 writel(val
, ctx
->regs
+ offset
);
608 DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val
);
611 fimd_win_set_pixfmt(dev
, win
);
613 /* hardware window 0 doesn't support color key. */
615 fimd_win_set_colkey(dev
, win
);
618 val
= readl(ctx
->regs
+ WINCON(win
));
619 val
|= WINCONx_ENWIN
;
620 writel(val
, ctx
->regs
+ WINCON(win
));
622 /* Enable DMA channel and unprotect windows */
623 fimd_shadow_protect_win(ctx
, win
, false);
625 if (ctx
->driver_data
->has_shadowcon
) {
626 val
= readl(ctx
->regs
+ SHADOWCON
);
627 val
|= SHADOWCON_CHx_ENABLE(win
);
628 writel(val
, ctx
->regs
+ SHADOWCON
);
631 win_data
->enabled
= true;
634 static void fimd_win_disable(struct device
*dev
, int zpos
)
636 struct fimd_context
*ctx
= get_fimd_context(dev
);
637 struct fimd_win_data
*win_data
;
641 if (win
== DEFAULT_ZPOS
)
642 win
= ctx
->default_win
;
644 if (win
< 0 || win
>= WINDOWS_NR
)
647 win_data
= &ctx
->win_data
[win
];
649 if (ctx
->suspended
) {
650 /* do not resume this window*/
651 win_data
->resume
= false;
655 /* protect windows */
656 fimd_shadow_protect_win(ctx
, win
, true);
659 val
= readl(ctx
->regs
+ WINCON(win
));
660 val
&= ~WINCONx_ENWIN
;
661 writel(val
, ctx
->regs
+ WINCON(win
));
663 /* unprotect windows */
664 if (ctx
->driver_data
->has_shadowcon
) {
665 val
= readl(ctx
->regs
+ SHADOWCON
);
666 val
&= ~SHADOWCON_CHx_ENABLE(win
);
667 writel(val
, ctx
->regs
+ SHADOWCON
);
670 fimd_shadow_protect_win(ctx
, win
, false);
672 win_data
->enabled
= false;
675 static struct exynos_drm_overlay_ops fimd_overlay_ops
= {
676 .mode_set
= fimd_win_mode_set
,
677 .commit
= fimd_win_commit
,
678 .disable
= fimd_win_disable
,
681 static struct exynos_drm_manager fimd_manager
= {
683 .ops
= &fimd_manager_ops
,
684 .overlay_ops
= &fimd_overlay_ops
,
685 .display_ops
= &fimd_display_ops
,
688 static irqreturn_t
fimd_irq_handler(int irq
, void *dev_id
)
690 struct fimd_context
*ctx
= (struct fimd_context
*)dev_id
;
691 struct exynos_drm_subdrv
*subdrv
= &ctx
->subdrv
;
692 struct drm_device
*drm_dev
= subdrv
->drm_dev
;
693 struct exynos_drm_manager
*manager
= subdrv
->manager
;
696 val
= readl(ctx
->regs
+ VIDINTCON1
);
698 if (val
& VIDINTCON1_INT_FRAME
)
699 /* VSYNC interrupt */
700 writel(VIDINTCON1_INT_FRAME
, ctx
->regs
+ VIDINTCON1
);
702 /* check the crtc is detached already from encoder */
703 if (manager
->pipe
< 0)
706 drm_handle_vblank(drm_dev
, manager
->pipe
);
707 exynos_drm_crtc_finish_pageflip(drm_dev
, manager
->pipe
);
709 /* set wait vsync event to zero and wake up queue. */
710 if (atomic_read(&ctx
->wait_vsync_event
)) {
711 atomic_set(&ctx
->wait_vsync_event
, 0);
712 DRM_WAKEUP(&ctx
->wait_vsync_queue
);
718 static int fimd_subdrv_probe(struct drm_device
*drm_dev
, struct device
*dev
)
721 * enable drm irq mode.
722 * - with irq_enabled = 1, we can use the vblank feature.
724 * P.S. note that we wouldn't use drm irq handler but
725 * just specific driver own one instead because
726 * drm framework supports only one irq handler.
728 drm_dev
->irq_enabled
= 1;
731 * with vblank_disable_allowed = 1, vblank interrupt will be disabled
732 * by drm timer once a current process gives up ownership of
733 * vblank event.(after drm_vblank_put function is called)
735 drm_dev
->vblank_disable_allowed
= 1;
737 /* attach this sub driver to iommu mapping if supported. */
738 if (is_drm_iommu_supported(drm_dev
))
739 drm_iommu_attach_device(drm_dev
, dev
);
744 static void fimd_subdrv_remove(struct drm_device
*drm_dev
, struct device
*dev
)
746 /* detach this sub driver from iommu mapping if supported. */
747 if (is_drm_iommu_supported(drm_dev
))
748 drm_iommu_detach_device(drm_dev
, dev
);
751 static int fimd_calc_clkdiv(struct fimd_context
*ctx
,
752 struct fb_videomode
*timing
)
754 unsigned long clk
= clk_get_rate(ctx
->lcd_clk
);
757 u32 best_framerate
= 0;
760 retrace
= timing
->left_margin
+ timing
->hsync_len
+
761 timing
->right_margin
+ timing
->xres
;
762 retrace
*= timing
->upper_margin
+ timing
->vsync_len
+
763 timing
->lower_margin
+ timing
->yres
;
765 /* default framerate is 60Hz */
766 if (!timing
->refresh
)
767 timing
->refresh
= 60;
771 for (clkdiv
= 1; clkdiv
< 0x100; clkdiv
++) {
774 /* get best framerate */
775 framerate
= clk
/ clkdiv
;
776 tmp
= timing
->refresh
- framerate
;
778 best_framerate
= framerate
;
782 best_framerate
= framerate
;
783 else if (tmp
< (best_framerate
- framerate
))
784 best_framerate
= framerate
;
792 static void fimd_clear_win(struct fimd_context
*ctx
, int win
)
794 writel(0, ctx
->regs
+ WINCON(win
));
795 writel(0, ctx
->regs
+ VIDOSD_A(win
));
796 writel(0, ctx
->regs
+ VIDOSD_B(win
));
797 writel(0, ctx
->regs
+ VIDOSD_C(win
));
799 if (win
== 1 || win
== 2)
800 writel(0, ctx
->regs
+ VIDOSD_D(win
));
802 fimd_shadow_protect_win(ctx
, win
, false);
805 static int fimd_clock(struct fimd_context
*ctx
, bool enable
)
810 ret
= clk_prepare_enable(ctx
->bus_clk
);
814 ret
= clk_prepare_enable(ctx
->lcd_clk
);
816 clk_disable_unprepare(ctx
->bus_clk
);
820 clk_disable_unprepare(ctx
->lcd_clk
);
821 clk_disable_unprepare(ctx
->bus_clk
);
827 static void fimd_window_suspend(struct device
*dev
)
829 struct fimd_context
*ctx
= get_fimd_context(dev
);
830 struct fimd_win_data
*win_data
;
833 for (i
= 0; i
< WINDOWS_NR
; i
++) {
834 win_data
= &ctx
->win_data
[i
];
835 win_data
->resume
= win_data
->enabled
;
836 fimd_win_disable(dev
, i
);
838 fimd_wait_for_vblank(dev
);
841 static void fimd_window_resume(struct device
*dev
)
843 struct fimd_context
*ctx
= get_fimd_context(dev
);
844 struct fimd_win_data
*win_data
;
847 for (i
= 0; i
< WINDOWS_NR
; i
++) {
848 win_data
= &ctx
->win_data
[i
];
849 win_data
->enabled
= win_data
->resume
;
850 win_data
->resume
= false;
854 static int fimd_activate(struct fimd_context
*ctx
, bool enable
)
856 struct device
*dev
= ctx
->subdrv
.dev
;
860 ret
= fimd_clock(ctx
, true);
864 ctx
->suspended
= false;
866 /* if vblank was enabled status, enable it again. */
867 if (test_and_clear_bit(0, &ctx
->irq_flags
))
868 fimd_enable_vblank(dev
);
870 fimd_window_resume(dev
);
872 fimd_window_suspend(dev
);
874 fimd_clock(ctx
, false);
875 ctx
->suspended
= true;
881 static int fimd_probe(struct platform_device
*pdev
)
883 struct device
*dev
= &pdev
->dev
;
884 struct fimd_context
*ctx
;
885 struct exynos_drm_subdrv
*subdrv
;
886 struct exynos_drm_fimd_pdata
*pdata
;
887 struct exynos_drm_panel_info
*panel
;
888 struct resource
*res
;
893 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
895 DRM_ERROR("memory allocation for pdata failed\n");
899 ret
= of_get_fb_videomode(dev
->of_node
, &pdata
->panel
.timing
,
902 DRM_ERROR("failed: of_get_fb_videomode() : %d\n", ret
);
906 pdata
= dev
->platform_data
;
908 DRM_ERROR("no platform data specified\n");
913 panel
= &pdata
->panel
;
915 dev_err(dev
, "panel is null.\n");
919 ctx
= devm_kzalloc(dev
, sizeof(*ctx
), GFP_KERNEL
);
923 ctx
->bus_clk
= devm_clk_get(dev
, "fimd");
924 if (IS_ERR(ctx
->bus_clk
)) {
925 dev_err(dev
, "failed to get bus clock\n");
926 return PTR_ERR(ctx
->bus_clk
);
929 ctx
->lcd_clk
= devm_clk_get(dev
, "sclk_fimd");
930 if (IS_ERR(ctx
->lcd_clk
)) {
931 dev_err(dev
, "failed to get lcd clock\n");
932 return PTR_ERR(ctx
->lcd_clk
);
935 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
937 ctx
->regs
= devm_ioremap_resource(dev
, res
);
938 if (IS_ERR(ctx
->regs
))
939 return PTR_ERR(ctx
->regs
);
941 res
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "vsync");
943 dev_err(dev
, "irq request failed.\n");
947 ctx
->irq
= res
->start
;
949 ret
= devm_request_irq(dev
, ctx
->irq
, fimd_irq_handler
,
952 dev_err(dev
, "irq request failed.\n");
956 ctx
->driver_data
= drm_fimd_get_driver_data(pdev
);
957 ctx
->vidcon0
= pdata
->vidcon0
;
958 ctx
->vidcon1
= pdata
->vidcon1
;
959 ctx
->default_win
= pdata
->default_win
;
961 DRM_INIT_WAITQUEUE(&ctx
->wait_vsync_queue
);
962 atomic_set(&ctx
->wait_vsync_event
, 0);
964 subdrv
= &ctx
->subdrv
;
967 subdrv
->manager
= &fimd_manager
;
968 subdrv
->probe
= fimd_subdrv_probe
;
969 subdrv
->remove
= fimd_subdrv_remove
;
971 mutex_init(&ctx
->lock
);
973 platform_set_drvdata(pdev
, ctx
);
975 pm_runtime_enable(dev
);
976 pm_runtime_get_sync(dev
);
978 ctx
->clkdiv
= fimd_calc_clkdiv(ctx
, &panel
->timing
);
979 panel
->timing
.pixclock
= clk_get_rate(ctx
->lcd_clk
) / ctx
->clkdiv
;
981 DRM_DEBUG_KMS("pixel clock = %d, clkdiv = %d\n",
982 panel
->timing
.pixclock
, ctx
->clkdiv
);
984 for (win
= 0; win
< WINDOWS_NR
; win
++)
985 fimd_clear_win(ctx
, win
);
987 exynos_drm_subdrv_register(subdrv
);
992 static int fimd_remove(struct platform_device
*pdev
)
994 struct device
*dev
= &pdev
->dev
;
995 struct fimd_context
*ctx
= platform_get_drvdata(pdev
);
997 exynos_drm_subdrv_unregister(&ctx
->subdrv
);
1002 pm_runtime_set_suspended(dev
);
1003 pm_runtime_put_sync(dev
);
1006 pm_runtime_disable(dev
);
1011 #ifdef CONFIG_PM_SLEEP
1012 static int fimd_suspend(struct device
*dev
)
1014 struct fimd_context
*ctx
= get_fimd_context(dev
);
1017 * do not use pm_runtime_suspend(). if pm_runtime_suspend() is
1018 * called here, an error would be returned by that interface
1019 * because the usage_count of pm runtime is more than 1.
1021 if (!pm_runtime_suspended(dev
))
1022 return fimd_activate(ctx
, false);
1027 static int fimd_resume(struct device
*dev
)
1029 struct fimd_context
*ctx
= get_fimd_context(dev
);
1032 * if entered to sleep when lcd panel was on, the usage_count
1033 * of pm runtime would still be 1 so in this case, fimd driver
1034 * should be on directly not drawing on pm runtime interface.
1036 if (!pm_runtime_suspended(dev
)) {
1039 ret
= fimd_activate(ctx
, true);
1044 * in case of dpms on(standby), fimd_apply function will
1045 * be called by encoder's dpms callback to update fimd's
1046 * registers but in case of sleep wakeup, it's not.
1047 * so fimd_apply function should be called at here.
1056 #ifdef CONFIG_PM_RUNTIME
1057 static int fimd_runtime_suspend(struct device
*dev
)
1059 struct fimd_context
*ctx
= get_fimd_context(dev
);
1061 return fimd_activate(ctx
, false);
1064 static int fimd_runtime_resume(struct device
*dev
)
1066 struct fimd_context
*ctx
= get_fimd_context(dev
);
1068 return fimd_activate(ctx
, true);
1072 static struct platform_device_id fimd_driver_ids
[] = {
1074 .name
= "s3c64xx-fb",
1075 .driver_data
= (unsigned long)&s3c64xx_fimd_driver_data
,
1077 .name
= "exynos4-fb",
1078 .driver_data
= (unsigned long)&exynos4_fimd_driver_data
,
1080 .name
= "exynos5-fb",
1081 .driver_data
= (unsigned long)&exynos5_fimd_driver_data
,
1085 MODULE_DEVICE_TABLE(platform
, fimd_driver_ids
);
1087 static const struct dev_pm_ops fimd_pm_ops
= {
1088 SET_SYSTEM_SLEEP_PM_OPS(fimd_suspend
, fimd_resume
)
1089 SET_RUNTIME_PM_OPS(fimd_runtime_suspend
, fimd_runtime_resume
, NULL
)
1092 struct platform_driver fimd_driver
= {
1093 .probe
= fimd_probe
,
1094 .remove
= fimd_remove
,
1095 .id_table
= fimd_driver_ids
,
1097 .name
= "exynos4-fb",
1098 .owner
= THIS_MODULE
,
1100 .of_match_table
= of_match_ptr(fimd_driver_dt_match
),