blob: bfb258135c74b0a5c54c04e2d7441f2a2840821d [file] [log] [blame]
Joonyoung Shimc8466a92015-06-12 21:59:00 +09001/* drivers/gpu/drm/exynos5433_drm_decon.c
2 *
3 * Copyright (C) 2015 Samsung Electronics Co.Ltd
4 * Authors:
5 * Joonyoung Shim <jy0922.shim@samsung.com>
6 * Hyungwon Hwang <human.hwang@samsung.com>
7 *
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
11 */
12
13#include <linux/platform_device.h>
14#include <linux/clk.h>
15#include <linux/component.h>
Andrzej Hajdab8182832015-10-20 18:22:41 +090016#include <linux/of_device.h>
Joonyoung Shimc8466a92015-06-12 21:59:00 +090017#include <linux/of_gpio.h>
18#include <linux/pm_runtime.h>
19
20#include <video/exynos5433_decon.h>
21
22#include "exynos_drm_drv.h"
23#include "exynos_drm_crtc.h"
Marek Szyprowski0488f502015-11-30 14:53:21 +010024#include "exynos_drm_fb.h"
Joonyoung Shimc8466a92015-06-12 21:59:00 +090025#include "exynos_drm_plane.h"
26#include "exynos_drm_iommu.h"
27
28#define WINDOWS_NR 3
29#define MIN_FB_WIDTH_FOR_16WORD_BURST 128
30
Andrzej Hajda4f54f21c2015-10-20 11:22:34 +020031static const char * const decon_clks_name[] = {
32 "pclk",
33 "aclk_decon",
34 "aclk_smmu_decon0x",
35 "aclk_xiu_decon0x",
36 "pclk_smmu_decon0x",
37 "sclk_decon_vclk",
38 "sclk_decon_eclk",
39};
40
Andrzej Hajdab8182832015-10-20 18:22:41 +090041enum decon_iftype {
42 IFTYPE_RGB,
43 IFTYPE_I80,
44 IFTYPE_HDMI
45};
46
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +020047enum decon_flag_bits {
48 BIT_CLKS_ENABLED,
49 BIT_IRQS_ENABLED,
50 BIT_WIN_UPDATED,
51 BIT_SUSPENDED
52};
53
Joonyoung Shimc8466a92015-06-12 21:59:00 +090054struct decon_context {
55 struct device *dev;
56 struct drm_device *drm_dev;
57 struct exynos_drm_crtc *crtc;
58 struct exynos_drm_plane planes[WINDOWS_NR];
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +010059 struct exynos_drm_plane_config configs[WINDOWS_NR];
Joonyoung Shimc8466a92015-06-12 21:59:00 +090060 void __iomem *addr;
Andrzej Hajda4f54f21c2015-10-20 11:22:34 +020061 struct clk *clks[ARRAY_SIZE(decon_clks_name)];
Joonyoung Shimc8466a92015-06-12 21:59:00 +090062 int pipe;
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +020063 unsigned long flags;
Andrzej Hajdab8182832015-10-20 18:22:41 +090064 enum decon_iftype out_type;
65 int first_win;
Joonyoung Shimc8466a92015-06-12 21:59:00 +090066};
67
Marek Szyprowskifbbb1e12015-08-31 00:53:57 +090068static const uint32_t decon_formats[] = {
69 DRM_FORMAT_XRGB1555,
70 DRM_FORMAT_RGB565,
71 DRM_FORMAT_XRGB8888,
72 DRM_FORMAT_ARGB8888,
73};
74
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +010075static const enum drm_plane_type decon_win_types[WINDOWS_NR] = {
76 DRM_PLANE_TYPE_PRIMARY,
77 DRM_PLANE_TYPE_OVERLAY,
78 DRM_PLANE_TYPE_CURSOR,
79};
80
Andrzej Hajdab2192072015-10-20 11:22:37 +020081static inline void decon_set_bits(struct decon_context *ctx, u32 reg, u32 mask,
82 u32 val)
83{
84 val = (val & mask) | (readl(ctx->addr + reg) & ~mask);
85 writel(val, ctx->addr + reg);
86}
87
Joonyoung Shimc8466a92015-06-12 21:59:00 +090088static int decon_enable_vblank(struct exynos_drm_crtc *crtc)
89{
90 struct decon_context *ctx = crtc->ctx;
91 u32 val;
92
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +020093 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +090094 return -EPERM;
95
Marek Szyprowskif3fb3d82016-02-03 13:42:54 +010096 if (!test_and_set_bit(BIT_IRQS_ENABLED, &ctx->flags)) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +090097 val = VIDINTCON0_INTEN;
Andrzej Hajdab8182832015-10-20 18:22:41 +090098 if (ctx->out_type == IFTYPE_I80)
Joonyoung Shimc8466a92015-06-12 21:59:00 +090099 val |= VIDINTCON0_FRAMEDONE;
100 else
101 val |= VIDINTCON0_INTFRMEN;
102
103 writel(val, ctx->addr + DECON_VIDINTCON0);
104 }
105
106 return 0;
107}
108
109static void decon_disable_vblank(struct exynos_drm_crtc *crtc)
110{
111 struct decon_context *ctx = crtc->ctx;
112
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200113 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900114 return;
115
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200116 if (test_and_clear_bit(BIT_IRQS_ENABLED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900117 writel(0, ctx->addr + DECON_VIDINTCON0);
118}
119
120static void decon_setup_trigger(struct decon_context *ctx)
121{
Andrzej Hajdab8182832015-10-20 18:22:41 +0900122 u32 val = (ctx->out_type != IFTYPE_HDMI)
123 ? TRIGCON_TRIGEN_PER_F | TRIGCON_TRIGEN_F |
124 TRIGCON_TE_AUTO_MASK | TRIGCON_SWTRIGEN
125 : TRIGCON_TRIGEN_PER_F | TRIGCON_TRIGEN_F |
126 TRIGCON_HWTRIGMASK_I80_RGB | TRIGCON_HWTRIGEN_I80_RGB;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900127 writel(val, ctx->addr + DECON_TRIGCON);
128}
129
130static void decon_commit(struct exynos_drm_crtc *crtc)
131{
132 struct decon_context *ctx = crtc->ctx;
Andrzej Hajda85de2752015-10-20 11:22:36 +0200133 struct drm_display_mode *m = &crtc->base.mode;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900134 u32 val;
135
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200136 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900137 return;
138
Andrzej Hajdab8182832015-10-20 18:22:41 +0900139 if (ctx->out_type == IFTYPE_HDMI) {
140 m->crtc_hsync_start = m->crtc_hdisplay + 10;
141 m->crtc_hsync_end = m->crtc_htotal - 92;
142 m->crtc_vsync_start = m->crtc_vdisplay + 1;
143 m->crtc_vsync_end = m->crtc_vsync_start + 1;
144 }
145
146 decon_set_bits(ctx, DECON_VIDCON0, VIDCON0_ENVID, 0);
147
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900148 /* enable clock gate */
149 val = CMU_CLKGAGE_MODE_SFR_F | CMU_CLKGAGE_MODE_MEM_F;
150 writel(val, ctx->addr + DECON_CMU);
151
152 /* lcd on and use command if */
153 val = VIDOUT_LCD_ON;
Andrzej Hajdab8182832015-10-20 18:22:41 +0900154 if (ctx->out_type == IFTYPE_I80)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900155 val |= VIDOUT_COMMAND_IF;
156 else
157 val |= VIDOUT_RGB_IF;
158 writel(val, ctx->addr + DECON_VIDOUTCON0);
159
Andrzej Hajda85de2752015-10-20 11:22:36 +0200160 val = VIDTCON2_LINEVAL(m->vdisplay - 1) |
161 VIDTCON2_HOZVAL(m->hdisplay - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900162 writel(val, ctx->addr + DECON_VIDTCON2);
163
Andrzej Hajdab8182832015-10-20 18:22:41 +0900164 if (ctx->out_type != IFTYPE_I80) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900165 val = VIDTCON00_VBPD_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200166 m->crtc_vtotal - m->crtc_vsync_end - 1) |
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900167 VIDTCON00_VFPD_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200168 m->crtc_vsync_start - m->crtc_vdisplay - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900169 writel(val, ctx->addr + DECON_VIDTCON00);
170
171 val = VIDTCON01_VSPW_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200172 m->crtc_vsync_end - m->crtc_vsync_start - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900173 writel(val, ctx->addr + DECON_VIDTCON01);
174
175 val = VIDTCON10_HBPD_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200176 m->crtc_htotal - m->crtc_hsync_end - 1) |
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900177 VIDTCON10_HFPD_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200178 m->crtc_hsync_start - m->crtc_hdisplay - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900179 writel(val, ctx->addr + DECON_VIDTCON10);
180
181 val = VIDTCON11_HSPW_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200182 m->crtc_hsync_end - m->crtc_hsync_start - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900183 writel(val, ctx->addr + DECON_VIDTCON11);
184 }
185
186 decon_setup_trigger(ctx);
187
188 /* enable output and display signal */
Andrzej Hajdab8182832015-10-20 18:22:41 +0900189 decon_set_bits(ctx, DECON_VIDCON0, VIDCON0_ENVID | VIDCON0_ENVID_F, ~0);
Andrzej Hajda92ead492016-03-23 14:15:16 +0100190
191 decon_set_bits(ctx, DECON_UPDATE, STANDALONE_UPDATE_F, ~0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900192}
193
Gustavo Padovan2eeb2e52015-08-03 14:40:44 +0900194static void decon_win_set_pixfmt(struct decon_context *ctx, unsigned int win,
195 struct drm_framebuffer *fb)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900196{
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900197 unsigned long val;
198
199 val = readl(ctx->addr + DECON_WINCONx(win));
200 val &= ~WINCONx_BPPMODE_MASK;
201
Gustavo Padovan2eeb2e52015-08-03 14:40:44 +0900202 switch (fb->pixel_format) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900203 case DRM_FORMAT_XRGB1555:
204 val |= WINCONx_BPPMODE_16BPP_I1555;
205 val |= WINCONx_HAWSWP_F;
206 val |= WINCONx_BURSTLEN_16WORD;
207 break;
208 case DRM_FORMAT_RGB565:
209 val |= WINCONx_BPPMODE_16BPP_565;
210 val |= WINCONx_HAWSWP_F;
211 val |= WINCONx_BURSTLEN_16WORD;
212 break;
213 case DRM_FORMAT_XRGB8888:
214 val |= WINCONx_BPPMODE_24BPP_888;
215 val |= WINCONx_WSWP_F;
216 val |= WINCONx_BURSTLEN_16WORD;
217 break;
218 case DRM_FORMAT_ARGB8888:
219 val |= WINCONx_BPPMODE_32BPP_A8888;
220 val |= WINCONx_WSWP_F | WINCONx_BLD_PIX_F | WINCONx_ALPHA_SEL_F;
221 val |= WINCONx_BURSTLEN_16WORD;
222 break;
223 default:
224 DRM_ERROR("Proper pixel format is not set\n");
225 return;
226 }
227
Gustavo Padovan2eeb2e52015-08-03 14:40:44 +0900228 DRM_DEBUG_KMS("bpp = %u\n", fb->bits_per_pixel);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900229
230 /*
231 * In case of exynos, setting dma-burst to 16Word causes permanent
232 * tearing for very small buffers, e.g. cursor buffer. Burst Mode
233 * switching which is based on plane size is not recommended as
234 * plane size varies a lot towards the end of the screen and rapid
235 * movement causes unstable DMA which results into iommu crash/tear.
236 */
237
Gustavo Padovan2eeb2e52015-08-03 14:40:44 +0900238 if (fb->width < MIN_FB_WIDTH_FOR_16WORD_BURST) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900239 val &= ~WINCONx_BURSTLEN_MASK;
240 val |= WINCONx_BURSTLEN_8WORD;
241 }
242
243 writel(val, ctx->addr + DECON_WINCONx(win));
244}
245
246static void decon_shadow_protect_win(struct decon_context *ctx, int win,
247 bool protect)
248{
Andrzej Hajdab2192072015-10-20 11:22:37 +0200249 decon_set_bits(ctx, DECON_SHADOWCON, SHADOWCON_Wx_PROTECT(win),
250 protect ? ~0 : 0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900251}
252
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100253static void decon_atomic_begin(struct exynos_drm_crtc *crtc)
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900254{
255 struct decon_context *ctx = crtc->ctx;
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100256 int i;
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900257
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200258 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900259 return;
260
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100261 for (i = ctx->first_win; i < WINDOWS_NR; i++)
262 decon_shadow_protect_win(ctx, i, true);
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900263}
264
Andrzej Hajdab8182832015-10-20 18:22:41 +0900265#define BIT_VAL(x, e, s) (((x) & ((1 << ((e) - (s) + 1)) - 1)) << (s))
266#define COORDINATE_X(x) BIT_VAL((x), 23, 12)
267#define COORDINATE_Y(x) BIT_VAL((x), 11, 0)
268
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900269static void decon_update_plane(struct exynos_drm_crtc *crtc,
270 struct exynos_drm_plane *plane)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900271{
Marek Szyprowski0114f402015-11-30 14:53:22 +0100272 struct exynos_drm_plane_state *state =
273 to_exynos_plane_state(plane->base.state);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900274 struct decon_context *ctx = crtc->ctx;
Marek Szyprowski0114f402015-11-30 14:53:22 +0100275 struct drm_framebuffer *fb = state->base.fb;
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100276 unsigned int win = plane->index;
Marek Szyprowski0488f502015-11-30 14:53:21 +0100277 unsigned int bpp = fb->bits_per_pixel >> 3;
278 unsigned int pitch = fb->pitches[0];
279 dma_addr_t dma_addr = exynos_drm_fb_dma_addr(fb, 0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900280 u32 val;
281
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200282 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900283 return;
284
Marek Szyprowski0114f402015-11-30 14:53:22 +0100285 val = COORDINATE_X(state->crtc.x) | COORDINATE_Y(state->crtc.y);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900286 writel(val, ctx->addr + DECON_VIDOSDxA(win));
287
Marek Szyprowski0114f402015-11-30 14:53:22 +0100288 val = COORDINATE_X(state->crtc.x + state->crtc.w - 1) |
289 COORDINATE_Y(state->crtc.y + state->crtc.h - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900290 writel(val, ctx->addr + DECON_VIDOSDxB(win));
291
292 val = VIDOSD_Wx_ALPHA_R_F(0x0) | VIDOSD_Wx_ALPHA_G_F(0x0) |
293 VIDOSD_Wx_ALPHA_B_F(0x0);
294 writel(val, ctx->addr + DECON_VIDOSDxC(win));
295
296 val = VIDOSD_Wx_ALPHA_R_F(0x0) | VIDOSD_Wx_ALPHA_G_F(0x0) |
297 VIDOSD_Wx_ALPHA_B_F(0x0);
298 writel(val, ctx->addr + DECON_VIDOSDxD(win));
299
Marek Szyprowski0488f502015-11-30 14:53:21 +0100300 writel(dma_addr, ctx->addr + DECON_VIDW0xADD0B0(win));
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900301
Marek Szyprowski0114f402015-11-30 14:53:22 +0100302 val = dma_addr + pitch * state->src.h;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900303 writel(val, ctx->addr + DECON_VIDW0xADD1B0(win));
304
Andrzej Hajdab8182832015-10-20 18:22:41 +0900305 if (ctx->out_type != IFTYPE_HDMI)
Marek Szyprowski0114f402015-11-30 14:53:22 +0100306 val = BIT_VAL(pitch - state->crtc.w * bpp, 27, 14)
307 | BIT_VAL(state->crtc.w * bpp, 13, 0);
Andrzej Hajdab8182832015-10-20 18:22:41 +0900308 else
Marek Szyprowski0114f402015-11-30 14:53:22 +0100309 val = BIT_VAL(pitch - state->crtc.w * bpp, 29, 15)
310 | BIT_VAL(state->crtc.w * bpp, 14, 0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900311 writel(val, ctx->addr + DECON_VIDW0xADD2(win));
312
Marek Szyprowski0488f502015-11-30 14:53:21 +0100313 decon_win_set_pixfmt(ctx, win, fb);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900314
315 /* window enable */
Andrzej Hajdab2192072015-10-20 11:22:37 +0200316 decon_set_bits(ctx, DECON_WINCONx(win), WINCONx_ENWIN_F, ~0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900317}
318
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900319static void decon_disable_plane(struct exynos_drm_crtc *crtc,
320 struct exynos_drm_plane *plane)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900321{
322 struct decon_context *ctx = crtc->ctx;
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100323 unsigned int win = plane->index;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900324
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200325 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900326 return;
327
328 decon_shadow_protect_win(ctx, win, true);
329
330 /* window disable */
Andrzej Hajdab2192072015-10-20 11:22:37 +0200331 decon_set_bits(ctx, DECON_WINCONx(win), WINCONx_ENWIN_F, 0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900332
333 decon_shadow_protect_win(ctx, win, false);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900334}
335
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100336static void decon_atomic_flush(struct exynos_drm_crtc *crtc)
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900337{
338 struct decon_context *ctx = crtc->ctx;
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100339 int i;
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900340
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200341 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900342 return;
343
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100344 for (i = ctx->first_win; i < WINDOWS_NR; i++)
345 decon_shadow_protect_win(ctx, i, false);
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900346
Andrzej Hajda92ead492016-03-23 14:15:16 +0100347 /* standalone update */
348 decon_set_bits(ctx, DECON_UPDATE, STANDALONE_UPDATE_F, ~0);
349
Andrzej Hajdab8182832015-10-20 18:22:41 +0900350 if (ctx->out_type == IFTYPE_I80)
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200351 set_bit(BIT_WIN_UPDATED, &ctx->flags);
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900352}
353
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900354static void decon_swreset(struct decon_context *ctx)
355{
356 unsigned int tries;
357
358 writel(0, ctx->addr + DECON_VIDCON0);
359 for (tries = 2000; tries; --tries) {
360 if (~readl(ctx->addr + DECON_VIDCON0) & VIDCON0_STOP_STATUS)
361 break;
362 udelay(10);
363 }
364
365 WARN(tries == 0, "failed to disable DECON\n");
366
367 writel(VIDCON0_SWRESET, ctx->addr + DECON_VIDCON0);
368 for (tries = 2000; tries; --tries) {
369 if (~readl(ctx->addr + DECON_VIDCON0) & VIDCON0_SWRESET)
370 break;
371 udelay(10);
372 }
373
374 WARN(tries == 0, "failed to software reset DECON\n");
Andrzej Hajdab8182832015-10-20 18:22:41 +0900375
376 if (ctx->out_type != IFTYPE_HDMI)
377 return;
378
379 writel(VIDCON0_CLKVALUP | VIDCON0_VLCKFREE, ctx->addr + DECON_VIDCON0);
380 decon_set_bits(ctx, DECON_CMU,
381 CMU_CLKGAGE_MODE_SFR_F | CMU_CLKGAGE_MODE_MEM_F, ~0);
382 writel(VIDCON1_VCLK_RUN_VDEN_DISABLE, ctx->addr + DECON_VIDCON1);
383 writel(CRCCTRL_CRCEN | CRCCTRL_CRCSTART_F | CRCCTRL_CRCCLKEN,
384 ctx->addr + DECON_CRCCTRL);
385 decon_setup_trigger(ctx);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900386}
387
388static void decon_enable(struct exynos_drm_crtc *crtc)
389{
390 struct decon_context *ctx = crtc->ctx;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900391
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200392 if (!test_and_clear_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900393 return;
394
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900395 pm_runtime_get_sync(ctx->dev);
396
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200397 set_bit(BIT_CLKS_ENABLED, &ctx->flags);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900398
Andrzej Hajdae87b3c62016-03-23 14:15:17 +0100399 decon_swreset(ctx);
400
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900401 /* if vblank was enabled status, enable it again. */
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200402 if (test_and_clear_bit(BIT_IRQS_ENABLED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900403 decon_enable_vblank(ctx->crtc);
404
405 decon_commit(ctx->crtc);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900406}
407
408static void decon_disable(struct exynos_drm_crtc *crtc)
409{
410 struct decon_context *ctx = crtc->ctx;
411 int i;
412
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200413 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900414 return;
415
416 /*
417 * We need to make sure that all windows are disabled before we
418 * suspend that connector. Otherwise we might try to scan from
419 * a destroyed buffer later.
420 */
Andrzej Hajdab8182832015-10-20 18:22:41 +0900421 for (i = ctx->first_win; i < WINDOWS_NR; i++)
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900422 decon_disable_plane(crtc, &ctx->planes[i]);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900423
424 decon_swreset(ctx);
425
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200426 clear_bit(BIT_CLKS_ENABLED, &ctx->flags);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900427
428 pm_runtime_put_sync(ctx->dev);
429
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200430 set_bit(BIT_SUSPENDED, &ctx->flags);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900431}
432
Andrzej Hajda9844d6e2016-02-11 12:55:46 +0100433static void decon_te_irq_handler(struct exynos_drm_crtc *crtc)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900434{
435 struct decon_context *ctx = crtc->ctx;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900436
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200437 if (!test_bit(BIT_CLKS_ENABLED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900438 return;
439
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200440 if (test_and_clear_bit(BIT_WIN_UPDATED, &ctx->flags))
Andrzej Hajdab2192072015-10-20 11:22:37 +0200441 decon_set_bits(ctx, DECON_TRIGCON, TRIGCON_SWTRIGCMD, ~0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900442
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300443 drm_crtc_handle_vblank(&ctx->crtc->base);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900444}
445
446static void decon_clear_channels(struct exynos_drm_crtc *crtc)
447{
448 struct decon_context *ctx = crtc->ctx;
449 int win, i, ret;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900450
451 DRM_DEBUG_KMS("%s\n", __FILE__);
452
453 for (i = 0; i < ARRAY_SIZE(decon_clks_name); i++) {
454 ret = clk_prepare_enable(ctx->clks[i]);
455 if (ret < 0)
456 goto err;
457 }
458
459 for (win = 0; win < WINDOWS_NR; win++) {
Andrzej Hajdab2192072015-10-20 11:22:37 +0200460 decon_shadow_protect_win(ctx, win, true);
461 decon_set_bits(ctx, DECON_WINCONx(win), WINCONx_ENWIN_F, 0);
462 decon_shadow_protect_win(ctx, win, false);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900463 }
Andrzej Hajda92ead492016-03-23 14:15:16 +0100464
465 decon_set_bits(ctx, DECON_UPDATE, STANDALONE_UPDATE_F, ~0);
466
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900467 /* TODO: wait for possible vsync */
468 msleep(50);
469
470err:
471 while (--i >= 0)
472 clk_disable_unprepare(ctx->clks[i]);
473}
474
475static struct exynos_drm_crtc_ops decon_crtc_ops = {
476 .enable = decon_enable,
477 .disable = decon_disable,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900478 .enable_vblank = decon_enable_vblank,
479 .disable_vblank = decon_disable_vblank,
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900480 .atomic_begin = decon_atomic_begin,
Gustavo Padovan9cc76102015-08-03 14:38:05 +0900481 .update_plane = decon_update_plane,
482 .disable_plane = decon_disable_plane,
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900483 .atomic_flush = decon_atomic_flush,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900484 .te_handler = decon_te_irq_handler,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900485};
486
487static int decon_bind(struct device *dev, struct device *master, void *data)
488{
489 struct decon_context *ctx = dev_get_drvdata(dev);
490 struct drm_device *drm_dev = data;
491 struct exynos_drm_private *priv = drm_dev->dev_private;
492 struct exynos_drm_plane *exynos_plane;
Andrzej Hajdab8182832015-10-20 18:22:41 +0900493 enum exynos_drm_output_type out_type;
Andrzej Hajdab8182832015-10-20 18:22:41 +0900494 unsigned int win;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900495 int ret;
496
497 ctx->drm_dev = drm_dev;
498 ctx->pipe = priv->pipe++;
499
Andrzej Hajdab8182832015-10-20 18:22:41 +0900500 for (win = ctx->first_win; win < WINDOWS_NR; win++) {
501 int tmp = (win == ctx->first_win) ? 0 : win;
502
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100503 ctx->configs[win].pixel_formats = decon_formats;
504 ctx->configs[win].num_pixel_formats = ARRAY_SIZE(decon_formats);
505 ctx->configs[win].zpos = win;
506 ctx->configs[win].type = decon_win_types[tmp];
507
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100508 ret = exynos_plane_init(drm_dev, &ctx->planes[win], win,
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100509 1 << ctx->pipe, &ctx->configs[win]);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900510 if (ret)
511 return ret;
512 }
513
Andrzej Hajdab8182832015-10-20 18:22:41 +0900514 exynos_plane = &ctx->planes[ctx->first_win];
515 out_type = (ctx->out_type == IFTYPE_HDMI) ? EXYNOS_DISPLAY_TYPE_HDMI
516 : EXYNOS_DISPLAY_TYPE_LCD;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900517 ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
Andrzej Hajdab8182832015-10-20 18:22:41 +0900518 ctx->pipe, out_type,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900519 &decon_crtc_ops, ctx);
520 if (IS_ERR(ctx->crtc)) {
521 ret = PTR_ERR(ctx->crtc);
522 goto err;
523 }
524
Joonyoung Shimeb7a3fc2015-07-02 21:49:39 +0900525 decon_clear_channels(ctx->crtc);
526
527 ret = drm_iommu_attach_device(drm_dev, dev);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900528 if (ret)
529 goto err;
530
531 return ret;
532err:
533 priv->pipe--;
534 return ret;
535}
536
537static void decon_unbind(struct device *dev, struct device *master, void *data)
538{
539 struct decon_context *ctx = dev_get_drvdata(dev);
540
541 decon_disable(ctx->crtc);
542
543 /* detach this sub driver from iommu mapping if supported. */
Joonyoung Shimbf566082015-07-02 21:49:38 +0900544 drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900545}
546
547static const struct component_ops decon_component_ops = {
548 .bind = decon_bind,
549 .unbind = decon_unbind,
550};
551
Andrzej Hajdab8182832015-10-20 18:22:41 +0900552static irqreturn_t decon_irq_handler(int irq, void *dev_id)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900553{
554 struct decon_context *ctx = dev_id;
555 u32 val;
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300556 int win;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900557
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200558 if (!test_bit(BIT_CLKS_ENABLED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900559 goto out;
560
561 val = readl(ctx->addr + DECON_VIDINTCON1);
Andrzej Hajdab8182832015-10-20 18:22:41 +0900562 val &= VIDINTCON1_INTFRMDONEPEND | VIDINTCON1_INTFRMPEND;
563
564 if (val) {
565 for (win = ctx->first_win; win < WINDOWS_NR ; win++) {
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300566 struct exynos_drm_plane *plane = &ctx->planes[win];
567
568 if (!plane->pending_fb)
569 continue;
570
571 exynos_drm_crtc_finish_update(ctx->crtc, plane);
572 }
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900573
574 /* clear */
Andrzej Hajdab8182832015-10-20 18:22:41 +0900575 writel(val, ctx->addr + DECON_VIDINTCON1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900576 }
577
578out:
579 return IRQ_HANDLED;
580}
581
Gustavo Padovanebf3fd42015-11-02 20:54:55 +0900582#ifdef CONFIG_PM
583static int exynos5433_decon_suspend(struct device *dev)
584{
585 struct decon_context *ctx = dev_get_drvdata(dev);
Andrzej Hajda92c96ff2016-02-11 12:25:04 +0100586 int i = ARRAY_SIZE(decon_clks_name);
Gustavo Padovanebf3fd42015-11-02 20:54:55 +0900587
Andrzej Hajda92c96ff2016-02-11 12:25:04 +0100588 while (--i >= 0)
Gustavo Padovanebf3fd42015-11-02 20:54:55 +0900589 clk_disable_unprepare(ctx->clks[i]);
590
591 return 0;
592}
593
594static int exynos5433_decon_resume(struct device *dev)
595{
596 struct decon_context *ctx = dev_get_drvdata(dev);
597 int i, ret;
598
599 for (i = 0; i < ARRAY_SIZE(decon_clks_name); i++) {
600 ret = clk_prepare_enable(ctx->clks[i]);
601 if (ret < 0)
602 goto err;
603 }
604
605 return 0;
606
607err:
608 while (--i >= 0)
609 clk_disable_unprepare(ctx->clks[i]);
610
611 return ret;
612}
613#endif
614
615static const struct dev_pm_ops exynos5433_decon_pm_ops = {
616 SET_RUNTIME_PM_OPS(exynos5433_decon_suspend, exynos5433_decon_resume,
617 NULL)
618};
619
Andrzej Hajdab8182832015-10-20 18:22:41 +0900620static const struct of_device_id exynos5433_decon_driver_dt_match[] = {
621 {
622 .compatible = "samsung,exynos5433-decon",
623 .data = (void *)IFTYPE_RGB
624 },
625 {
626 .compatible = "samsung,exynos5433-decon-tv",
627 .data = (void *)IFTYPE_HDMI
628 },
629 {},
630};
631MODULE_DEVICE_TABLE(of, exynos5433_decon_driver_dt_match);
632
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900633static int exynos5433_decon_probe(struct platform_device *pdev)
634{
Andrzej Hajdab8182832015-10-20 18:22:41 +0900635 const struct of_device_id *of_id;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900636 struct device *dev = &pdev->dev;
637 struct decon_context *ctx;
638 struct resource *res;
639 int ret;
640 int i;
641
642 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
643 if (!ctx)
644 return -ENOMEM;
645
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200646 __set_bit(BIT_SUSPENDED, &ctx->flags);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900647 ctx->dev = dev;
Andrzej Hajdab8182832015-10-20 18:22:41 +0900648
649 of_id = of_match_device(exynos5433_decon_driver_dt_match, &pdev->dev);
650 ctx->out_type = (enum decon_iftype)of_id->data;
651
652 if (ctx->out_type == IFTYPE_HDMI)
653 ctx->first_win = 1;
654 else if (of_get_child_by_name(dev->of_node, "i80-if-timings"))
655 ctx->out_type = IFTYPE_I80;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900656
657 for (i = 0; i < ARRAY_SIZE(decon_clks_name); i++) {
658 struct clk *clk;
659
660 clk = devm_clk_get(ctx->dev, decon_clks_name[i]);
661 if (IS_ERR(clk))
662 return PTR_ERR(clk);
663
664 ctx->clks[i] = clk;
665 }
666
667 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
668 if (!res) {
669 dev_err(dev, "cannot find IO resource\n");
670 return -ENXIO;
671 }
672
673 ctx->addr = devm_ioremap_resource(dev, res);
674 if (IS_ERR(ctx->addr)) {
675 dev_err(dev, "ioremap failed\n");
676 return PTR_ERR(ctx->addr);
677 }
678
679 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
Andrzej Hajdab8182832015-10-20 18:22:41 +0900680 (ctx->out_type == IFTYPE_I80) ? "lcd_sys" : "vsync");
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900681 if (!res) {
682 dev_err(dev, "cannot find IRQ resource\n");
683 return -ENXIO;
684 }
685
Andrzej Hajdab8182832015-10-20 18:22:41 +0900686 ret = devm_request_irq(dev, res->start, decon_irq_handler, 0,
687 "drm_decon", ctx);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900688 if (ret < 0) {
689 dev_err(dev, "lcd_sys irq request failed\n");
690 return ret;
691 }
692
693 platform_set_drvdata(pdev, ctx);
694
695 pm_runtime_enable(dev);
696
697 ret = component_add(dev, &decon_component_ops);
698 if (ret)
699 goto err_disable_pm_runtime;
700
701 return 0;
702
703err_disable_pm_runtime:
704 pm_runtime_disable(dev);
705
706 return ret;
707}
708
709static int exynos5433_decon_remove(struct platform_device *pdev)
710{
711 pm_runtime_disable(&pdev->dev);
712
713 component_del(&pdev->dev, &decon_component_ops);
714
715 return 0;
716}
717
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900718struct platform_driver exynos5433_decon_driver = {
719 .probe = exynos5433_decon_probe,
720 .remove = exynos5433_decon_remove,
721 .driver = {
722 .name = "exynos5433-decon",
Gustavo Padovanebf3fd42015-11-02 20:54:55 +0900723 .pm = &exynos5433_decon_pm_ops,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900724 .of_match_table = exynos5433_decon_driver_dt_match,
725 },
726};