1 /* drivers/gpu/drm/exynos5433_drm_decon.c
3 * Copyright (C) 2015 Samsung Electronics Co.Ltd
5 * Joonyoung Shim <jy0922.shim@samsung.com>
6 * Hyungwon Hwang <human.hwang@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundationr
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
15 #include <linux/component.h>
16 #include <linux/of_device.h>
17 #include <linux/of_gpio.h>
18 #include <linux/pm_runtime.h>
20 #include <video/exynos5433_decon.h>
22 #include "exynos_drm_drv.h"
23 #include "exynos_drm_crtc.h"
24 #include "exynos_drm_plane.h"
25 #include "exynos_drm_iommu.h"
29 #define MIN_FB_WIDTH_FOR_16WORD_BURST 128
31 static const char * const decon_clks_name
[] = {
47 enum decon_flag_bits
{
54 struct decon_context
{
56 struct drm_device
*drm_dev
;
57 struct exynos_drm_crtc
*crtc
;
58 struct exynos_drm_plane planes
[WINDOWS_NR
];
60 struct clk
*clks
[ARRAY_SIZE(decon_clks_name
)];
63 enum decon_iftype out_type
;
67 static const uint32_t decon_formats
[] = {
74 static inline void decon_set_bits(struct decon_context
*ctx
, u32 reg
, u32 mask
,
77 val
= (val
& mask
) | (readl(ctx
->addr
+ reg
) & ~mask
);
78 writel(val
, ctx
->addr
+ reg
);
81 static int decon_enable_vblank(struct exynos_drm_crtc
*crtc
)
83 struct decon_context
*ctx
= crtc
->ctx
;
86 if (test_bit(BIT_SUSPENDED
, &ctx
->flags
))
89 if (test_and_set_bit(BIT_IRQS_ENABLED
, &ctx
->flags
)) {
90 val
= VIDINTCON0_INTEN
;
91 if (ctx
->out_type
== IFTYPE_I80
)
92 val
|= VIDINTCON0_FRAMEDONE
;
94 val
|= VIDINTCON0_INTFRMEN
;
96 writel(val
, ctx
->addr
+ DECON_VIDINTCON0
);
102 static void decon_disable_vblank(struct exynos_drm_crtc
*crtc
)
104 struct decon_context
*ctx
= crtc
->ctx
;
106 if (test_bit(BIT_SUSPENDED
, &ctx
->flags
))
109 if (test_and_clear_bit(BIT_IRQS_ENABLED
, &ctx
->flags
))
110 writel(0, ctx
->addr
+ DECON_VIDINTCON0
);
113 static void decon_setup_trigger(struct decon_context
*ctx
)
115 u32 val
= (ctx
->out_type
!= IFTYPE_HDMI
)
116 ? TRIGCON_TRIGEN_PER_F
| TRIGCON_TRIGEN_F
|
117 TRIGCON_TE_AUTO_MASK
| TRIGCON_SWTRIGEN
118 : TRIGCON_TRIGEN_PER_F
| TRIGCON_TRIGEN_F
|
119 TRIGCON_HWTRIGMASK_I80_RGB
| TRIGCON_HWTRIGEN_I80_RGB
;
120 writel(val
, ctx
->addr
+ DECON_TRIGCON
);
123 static void decon_commit(struct exynos_drm_crtc
*crtc
)
125 struct decon_context
*ctx
= crtc
->ctx
;
126 struct drm_display_mode
*m
= &crtc
->base
.mode
;
129 if (test_bit(BIT_SUSPENDED
, &ctx
->flags
))
132 if (ctx
->out_type
== IFTYPE_HDMI
) {
133 m
->crtc_hsync_start
= m
->crtc_hdisplay
+ 10;
134 m
->crtc_hsync_end
= m
->crtc_htotal
- 92;
135 m
->crtc_vsync_start
= m
->crtc_vdisplay
+ 1;
136 m
->crtc_vsync_end
= m
->crtc_vsync_start
+ 1;
139 decon_set_bits(ctx
, DECON_VIDCON0
, VIDCON0_ENVID
, 0);
141 /* enable clock gate */
142 val
= CMU_CLKGAGE_MODE_SFR_F
| CMU_CLKGAGE_MODE_MEM_F
;
143 writel(val
, ctx
->addr
+ DECON_CMU
);
145 /* lcd on and use command if */
147 if (ctx
->out_type
== IFTYPE_I80
)
148 val
|= VIDOUT_COMMAND_IF
;
150 val
|= VIDOUT_RGB_IF
;
151 writel(val
, ctx
->addr
+ DECON_VIDOUTCON0
);
153 val
= VIDTCON2_LINEVAL(m
->vdisplay
- 1) |
154 VIDTCON2_HOZVAL(m
->hdisplay
- 1);
155 writel(val
, ctx
->addr
+ DECON_VIDTCON2
);
157 if (ctx
->out_type
!= IFTYPE_I80
) {
158 val
= VIDTCON00_VBPD_F(
159 m
->crtc_vtotal
- m
->crtc_vsync_end
- 1) |
161 m
->crtc_vsync_start
- m
->crtc_vdisplay
- 1);
162 writel(val
, ctx
->addr
+ DECON_VIDTCON00
);
164 val
= VIDTCON01_VSPW_F(
165 m
->crtc_vsync_end
- m
->crtc_vsync_start
- 1);
166 writel(val
, ctx
->addr
+ DECON_VIDTCON01
);
168 val
= VIDTCON10_HBPD_F(
169 m
->crtc_htotal
- m
->crtc_hsync_end
- 1) |
171 m
->crtc_hsync_start
- m
->crtc_hdisplay
- 1);
172 writel(val
, ctx
->addr
+ DECON_VIDTCON10
);
174 val
= VIDTCON11_HSPW_F(
175 m
->crtc_hsync_end
- m
->crtc_hsync_start
- 1);
176 writel(val
, ctx
->addr
+ DECON_VIDTCON11
);
179 decon_setup_trigger(ctx
);
181 /* enable output and display signal */
182 decon_set_bits(ctx
, DECON_VIDCON0
, VIDCON0_ENVID
| VIDCON0_ENVID_F
, ~0);
185 static void decon_win_set_pixfmt(struct decon_context
*ctx
, unsigned int win
,
186 struct drm_framebuffer
*fb
)
190 val
= readl(ctx
->addr
+ DECON_WINCONx(win
));
191 val
&= ~WINCONx_BPPMODE_MASK
;
193 switch (fb
->pixel_format
) {
194 case DRM_FORMAT_XRGB1555
:
195 val
|= WINCONx_BPPMODE_16BPP_I1555
;
196 val
|= WINCONx_HAWSWP_F
;
197 val
|= WINCONx_BURSTLEN_16WORD
;
199 case DRM_FORMAT_RGB565
:
200 val
|= WINCONx_BPPMODE_16BPP_565
;
201 val
|= WINCONx_HAWSWP_F
;
202 val
|= WINCONx_BURSTLEN_16WORD
;
204 case DRM_FORMAT_XRGB8888
:
205 val
|= WINCONx_BPPMODE_24BPP_888
;
206 val
|= WINCONx_WSWP_F
;
207 val
|= WINCONx_BURSTLEN_16WORD
;
209 case DRM_FORMAT_ARGB8888
:
210 val
|= WINCONx_BPPMODE_32BPP_A8888
;
211 val
|= WINCONx_WSWP_F
| WINCONx_BLD_PIX_F
| WINCONx_ALPHA_SEL_F
;
212 val
|= WINCONx_BURSTLEN_16WORD
;
215 DRM_ERROR("Proper pixel format is not set\n");
219 DRM_DEBUG_KMS("bpp = %u\n", fb
->bits_per_pixel
);
222 * In case of exynos, setting dma-burst to 16Word causes permanent
223 * tearing for very small buffers, e.g. cursor buffer. Burst Mode
224 * switching which is based on plane size is not recommended as
225 * plane size varies a lot towards the end of the screen and rapid
226 * movement causes unstable DMA which results into iommu crash/tear.
229 if (fb
->width
< MIN_FB_WIDTH_FOR_16WORD_BURST
) {
230 val
&= ~WINCONx_BURSTLEN_MASK
;
231 val
|= WINCONx_BURSTLEN_8WORD
;
234 writel(val
, ctx
->addr
+ DECON_WINCONx(win
));
237 static void decon_shadow_protect_win(struct decon_context
*ctx
, int win
,
240 decon_set_bits(ctx
, DECON_SHADOWCON
, SHADOWCON_Wx_PROTECT(win
),
244 static void decon_atomic_begin(struct exynos_drm_crtc
*crtc
,
245 struct exynos_drm_plane
*plane
)
247 struct decon_context
*ctx
= crtc
->ctx
;
249 if (test_bit(BIT_SUSPENDED
, &ctx
->flags
))
252 decon_shadow_protect_win(ctx
, plane
->zpos
, true);
255 #define BIT_VAL(x, e, s) (((x) & ((1 << ((e) - (s) + 1)) - 1)) << (s))
256 #define COORDINATE_X(x) BIT_VAL((x), 23, 12)
257 #define COORDINATE_Y(x) BIT_VAL((x), 11, 0)
259 static void decon_update_plane(struct exynos_drm_crtc
*crtc
,
260 struct exynos_drm_plane
*plane
)
262 struct decon_context
*ctx
= crtc
->ctx
;
263 struct drm_plane_state
*state
= plane
->base
.state
;
264 unsigned int win
= plane
->zpos
;
265 unsigned int bpp
= state
->fb
->bits_per_pixel
>> 3;
266 unsigned int pitch
= state
->fb
->pitches
[0];
269 if (test_bit(BIT_SUSPENDED
, &ctx
->flags
))
272 val
= COORDINATE_X(plane
->crtc_x
) | COORDINATE_Y(plane
->crtc_y
);
273 writel(val
, ctx
->addr
+ DECON_VIDOSDxA(win
));
275 val
= COORDINATE_X(plane
->crtc_x
+ plane
->crtc_w
- 1) |
276 COORDINATE_Y(plane
->crtc_y
+ plane
->crtc_h
- 1);
277 writel(val
, ctx
->addr
+ DECON_VIDOSDxB(win
));
279 val
= VIDOSD_Wx_ALPHA_R_F(0x0) | VIDOSD_Wx_ALPHA_G_F(0x0) |
280 VIDOSD_Wx_ALPHA_B_F(0x0);
281 writel(val
, ctx
->addr
+ DECON_VIDOSDxC(win
));
283 val
= VIDOSD_Wx_ALPHA_R_F(0x0) | VIDOSD_Wx_ALPHA_G_F(0x0) |
284 VIDOSD_Wx_ALPHA_B_F(0x0);
285 writel(val
, ctx
->addr
+ DECON_VIDOSDxD(win
));
287 writel(plane
->dma_addr
[0], ctx
->addr
+ DECON_VIDW0xADD0B0(win
));
289 val
= plane
->dma_addr
[0] + pitch
* plane
->crtc_h
;
290 writel(val
, ctx
->addr
+ DECON_VIDW0xADD1B0(win
));
292 if (ctx
->out_type
!= IFTYPE_HDMI
)
293 val
= BIT_VAL(pitch
- plane
->crtc_w
* bpp
, 27, 14)
294 | BIT_VAL(plane
->crtc_w
* bpp
, 13, 0);
296 val
= BIT_VAL(pitch
- plane
->crtc_w
* bpp
, 29, 15)
297 | BIT_VAL(plane
->crtc_w
* bpp
, 14, 0);
298 writel(val
, ctx
->addr
+ DECON_VIDW0xADD2(win
));
300 decon_win_set_pixfmt(ctx
, win
, state
->fb
);
303 decon_set_bits(ctx
, DECON_WINCONx(win
), WINCONx_ENWIN_F
, ~0);
305 /* standalone update */
306 decon_set_bits(ctx
, DECON_UPDATE
, STANDALONE_UPDATE_F
, ~0);
309 static void decon_disable_plane(struct exynos_drm_crtc
*crtc
,
310 struct exynos_drm_plane
*plane
)
312 struct decon_context
*ctx
= crtc
->ctx
;
313 unsigned int win
= plane
->zpos
;
315 if (test_bit(BIT_SUSPENDED
, &ctx
->flags
))
318 decon_shadow_protect_win(ctx
, win
, true);
321 decon_set_bits(ctx
, DECON_WINCONx(win
), WINCONx_ENWIN_F
, 0);
323 decon_shadow_protect_win(ctx
, win
, false);
325 /* standalone update */
326 decon_set_bits(ctx
, DECON_UPDATE
, STANDALONE_UPDATE_F
, ~0);
329 static void decon_atomic_flush(struct exynos_drm_crtc
*crtc
,
330 struct exynos_drm_plane
*plane
)
332 struct decon_context
*ctx
= crtc
->ctx
;
334 if (test_bit(BIT_SUSPENDED
, &ctx
->flags
))
337 decon_shadow_protect_win(ctx
, plane
->zpos
, false);
339 if (ctx
->out_type
== IFTYPE_I80
)
340 set_bit(BIT_WIN_UPDATED
, &ctx
->flags
);
343 static void decon_swreset(struct decon_context
*ctx
)
347 writel(0, ctx
->addr
+ DECON_VIDCON0
);
348 for (tries
= 2000; tries
; --tries
) {
349 if (~readl(ctx
->addr
+ DECON_VIDCON0
) & VIDCON0_STOP_STATUS
)
354 WARN(tries
== 0, "failed to disable DECON\n");
356 writel(VIDCON0_SWRESET
, ctx
->addr
+ DECON_VIDCON0
);
357 for (tries
= 2000; tries
; --tries
) {
358 if (~readl(ctx
->addr
+ DECON_VIDCON0
) & VIDCON0_SWRESET
)
363 WARN(tries
== 0, "failed to software reset DECON\n");
365 if (ctx
->out_type
!= IFTYPE_HDMI
)
368 writel(VIDCON0_CLKVALUP
| VIDCON0_VLCKFREE
, ctx
->addr
+ DECON_VIDCON0
);
369 decon_set_bits(ctx
, DECON_CMU
,
370 CMU_CLKGAGE_MODE_SFR_F
| CMU_CLKGAGE_MODE_MEM_F
, ~0);
371 writel(VIDCON1_VCLK_RUN_VDEN_DISABLE
, ctx
->addr
+ DECON_VIDCON1
);
372 writel(CRCCTRL_CRCEN
| CRCCTRL_CRCSTART_F
| CRCCTRL_CRCCLKEN
,
373 ctx
->addr
+ DECON_CRCCTRL
);
374 decon_setup_trigger(ctx
);
377 static void decon_enable(struct exynos_drm_crtc
*crtc
)
379 struct decon_context
*ctx
= crtc
->ctx
;
383 if (!test_and_clear_bit(BIT_SUSPENDED
, &ctx
->flags
))
386 pm_runtime_get_sync(ctx
->dev
);
388 for (i
= 0; i
< ARRAY_SIZE(decon_clks_name
); i
++) {
389 ret
= clk_prepare_enable(ctx
->clks
[i
]);
394 set_bit(BIT_CLKS_ENABLED
, &ctx
->flags
);
396 /* if vblank was enabled status, enable it again. */
397 if (test_and_clear_bit(BIT_IRQS_ENABLED
, &ctx
->flags
))
398 decon_enable_vblank(ctx
->crtc
);
400 decon_commit(ctx
->crtc
);
405 clk_disable_unprepare(ctx
->clks
[i
]);
407 set_bit(BIT_SUSPENDED
, &ctx
->flags
);
410 static void decon_disable(struct exynos_drm_crtc
*crtc
)
412 struct decon_context
*ctx
= crtc
->ctx
;
415 if (test_bit(BIT_SUSPENDED
, &ctx
->flags
))
419 * We need to make sure that all windows are disabled before we
420 * suspend that connector. Otherwise we might try to scan from
421 * a destroyed buffer later.
423 for (i
= ctx
->first_win
; i
< WINDOWS_NR
; i
++)
424 decon_disable_plane(crtc
, &ctx
->planes
[i
]);
428 for (i
= 0; i
< ARRAY_SIZE(decon_clks_name
); i
++)
429 clk_disable_unprepare(ctx
->clks
[i
]);
431 clear_bit(BIT_CLKS_ENABLED
, &ctx
->flags
);
433 pm_runtime_put_sync(ctx
->dev
);
435 set_bit(BIT_SUSPENDED
, &ctx
->flags
);
438 void decon_te_irq_handler(struct exynos_drm_crtc
*crtc
)
440 struct decon_context
*ctx
= crtc
->ctx
;
442 if (!test_bit(BIT_CLKS_ENABLED
, &ctx
->flags
))
445 if (test_and_clear_bit(BIT_WIN_UPDATED
, &ctx
->flags
))
446 decon_set_bits(ctx
, DECON_TRIGCON
, TRIGCON_SWTRIGCMD
, ~0);
448 drm_crtc_handle_vblank(&ctx
->crtc
->base
);
451 static void decon_clear_channels(struct exynos_drm_crtc
*crtc
)
453 struct decon_context
*ctx
= crtc
->ctx
;
456 DRM_DEBUG_KMS("%s\n", __FILE__
);
458 for (i
= 0; i
< ARRAY_SIZE(decon_clks_name
); i
++) {
459 ret
= clk_prepare_enable(ctx
->clks
[i
]);
464 for (win
= 0; win
< WINDOWS_NR
; win
++) {
465 decon_shadow_protect_win(ctx
, win
, true);
466 decon_set_bits(ctx
, DECON_WINCONx(win
), WINCONx_ENWIN_F
, 0);
467 decon_shadow_protect_win(ctx
, win
, false);
468 decon_set_bits(ctx
, DECON_UPDATE
, STANDALONE_UPDATE_F
, ~0);
470 /* TODO: wait for possible vsync */
475 clk_disable_unprepare(ctx
->clks
[i
]);
478 static struct exynos_drm_crtc_ops decon_crtc_ops
= {
479 .enable
= decon_enable
,
480 .disable
= decon_disable
,
481 .commit
= decon_commit
,
482 .enable_vblank
= decon_enable_vblank
,
483 .disable_vblank
= decon_disable_vblank
,
484 .atomic_begin
= decon_atomic_begin
,
485 .update_plane
= decon_update_plane
,
486 .disable_plane
= decon_disable_plane
,
487 .atomic_flush
= decon_atomic_flush
,
488 .te_handler
= decon_te_irq_handler
,
491 static int decon_bind(struct device
*dev
, struct device
*master
, void *data
)
493 struct decon_context
*ctx
= dev_get_drvdata(dev
);
494 struct drm_device
*drm_dev
= data
;
495 struct exynos_drm_private
*priv
= drm_dev
->dev_private
;
496 struct exynos_drm_plane
*exynos_plane
;
497 enum exynos_drm_output_type out_type
;
498 enum drm_plane_type type
;
502 ctx
->drm_dev
= drm_dev
;
503 ctx
->pipe
= priv
->pipe
++;
505 for (win
= ctx
->first_win
; win
< WINDOWS_NR
; win
++) {
506 int tmp
= (win
== ctx
->first_win
) ? 0 : win
;
508 type
= exynos_plane_get_type(tmp
, CURSOR_WIN
);
509 ret
= exynos_plane_init(drm_dev
, &ctx
->planes
[win
],
510 1 << ctx
->pipe
, type
, decon_formats
,
511 ARRAY_SIZE(decon_formats
), win
);
516 exynos_plane
= &ctx
->planes
[ctx
->first_win
];
517 out_type
= (ctx
->out_type
== IFTYPE_HDMI
) ? EXYNOS_DISPLAY_TYPE_HDMI
518 : EXYNOS_DISPLAY_TYPE_LCD
;
519 ctx
->crtc
= exynos_drm_crtc_create(drm_dev
, &exynos_plane
->base
,
521 &decon_crtc_ops
, ctx
);
522 if (IS_ERR(ctx
->crtc
)) {
523 ret
= PTR_ERR(ctx
->crtc
);
527 decon_clear_channels(ctx
->crtc
);
529 ret
= drm_iommu_attach_device(drm_dev
, dev
);
539 static void decon_unbind(struct device
*dev
, struct device
*master
, void *data
)
541 struct decon_context
*ctx
= dev_get_drvdata(dev
);
543 decon_disable(ctx
->crtc
);
545 /* detach this sub driver from iommu mapping if supported. */
546 drm_iommu_detach_device(ctx
->drm_dev
, ctx
->dev
);
549 static const struct component_ops decon_component_ops
= {
551 .unbind
= decon_unbind
,
554 static irqreturn_t
decon_irq_handler(int irq
, void *dev_id
)
556 struct decon_context
*ctx
= dev_id
;
560 if (!test_bit(BIT_CLKS_ENABLED
, &ctx
->flags
))
563 val
= readl(ctx
->addr
+ DECON_VIDINTCON1
);
564 val
&= VIDINTCON1_INTFRMDONEPEND
| VIDINTCON1_INTFRMPEND
;
567 for (win
= ctx
->first_win
; win
< WINDOWS_NR
; win
++) {
568 struct exynos_drm_plane
*plane
= &ctx
->planes
[win
];
570 if (!plane
->pending_fb
)
573 exynos_drm_crtc_finish_update(ctx
->crtc
, plane
);
577 writel(val
, ctx
->addr
+ DECON_VIDINTCON1
);
584 static const struct of_device_id exynos5433_decon_driver_dt_match
[] = {
586 .compatible
= "samsung,exynos5433-decon",
587 .data
= (void *)IFTYPE_RGB
590 .compatible
= "samsung,exynos5433-decon-tv",
591 .data
= (void *)IFTYPE_HDMI
595 MODULE_DEVICE_TABLE(of
, exynos5433_decon_driver_dt_match
);
597 static int exynos5433_decon_probe(struct platform_device
*pdev
)
599 const struct of_device_id
*of_id
;
600 struct device
*dev
= &pdev
->dev
;
601 struct decon_context
*ctx
;
602 struct resource
*res
;
606 ctx
= devm_kzalloc(dev
, sizeof(*ctx
), GFP_KERNEL
);
610 __set_bit(BIT_SUSPENDED
, &ctx
->flags
);
613 of_id
= of_match_device(exynos5433_decon_driver_dt_match
, &pdev
->dev
);
614 ctx
->out_type
= (enum decon_iftype
)of_id
->data
;
616 if (ctx
->out_type
== IFTYPE_HDMI
)
618 else if (of_get_child_by_name(dev
->of_node
, "i80-if-timings"))
619 ctx
->out_type
= IFTYPE_I80
;
621 for (i
= 0; i
< ARRAY_SIZE(decon_clks_name
); i
++) {
624 clk
= devm_clk_get(ctx
->dev
, decon_clks_name
[i
]);
631 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
633 dev_err(dev
, "cannot find IO resource\n");
637 ctx
->addr
= devm_ioremap_resource(dev
, res
);
638 if (IS_ERR(ctx
->addr
)) {
639 dev_err(dev
, "ioremap failed\n");
640 return PTR_ERR(ctx
->addr
);
643 res
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
,
644 (ctx
->out_type
== IFTYPE_I80
) ? "lcd_sys" : "vsync");
646 dev_err(dev
, "cannot find IRQ resource\n");
650 ret
= devm_request_irq(dev
, res
->start
, decon_irq_handler
, 0,
653 dev_err(dev
, "lcd_sys irq request failed\n");
657 platform_set_drvdata(pdev
, ctx
);
659 pm_runtime_enable(dev
);
661 ret
= component_add(dev
, &decon_component_ops
);
663 goto err_disable_pm_runtime
;
667 err_disable_pm_runtime
:
668 pm_runtime_disable(dev
);
673 static int exynos5433_decon_remove(struct platform_device
*pdev
)
675 pm_runtime_disable(&pdev
->dev
);
677 component_del(&pdev
->dev
, &decon_component_ops
);
682 struct platform_driver exynos5433_decon_driver
= {
683 .probe
= exynos5433_decon_probe
,
684 .remove
= exynos5433_decon_remove
,
686 .name
= "exynos5433-decon",
687 .of_match_table
= exynos5433_decon_driver_dt_match
,