blob: 31eafc546d6c06c8232bd0f363d207fd75188ba6 [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
Inki Dae9ac26de2016-04-18 17:59:01 +090031#define IFTYPE_I80 (1 << 0)
32#define I80_HW_TRG (1 << 1)
33#define IFTYPE_HDMI (1 << 2)
34
Andrzej Hajda4f54f21c2015-10-20 11:22:34 +020035static const char * const decon_clks_name[] = {
36 "pclk",
37 "aclk_decon",
38 "aclk_smmu_decon0x",
39 "aclk_xiu_decon0x",
40 "pclk_smmu_decon0x",
41 "sclk_decon_vclk",
42 "sclk_decon_eclk",
43};
44
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +020045enum decon_flag_bits {
46 BIT_CLKS_ENABLED,
47 BIT_IRQS_ENABLED,
48 BIT_WIN_UPDATED,
49 BIT_SUSPENDED
50};
51
Joonyoung Shimc8466a92015-06-12 21:59:00 +090052struct decon_context {
53 struct device *dev;
54 struct drm_device *drm_dev;
55 struct exynos_drm_crtc *crtc;
56 struct exynos_drm_plane planes[WINDOWS_NR];
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +010057 struct exynos_drm_plane_config configs[WINDOWS_NR];
Joonyoung Shimc8466a92015-06-12 21:59:00 +090058 void __iomem *addr;
Andrzej Hajda4f54f21c2015-10-20 11:22:34 +020059 struct clk *clks[ARRAY_SIZE(decon_clks_name)];
Joonyoung Shimc8466a92015-06-12 21:59:00 +090060 int pipe;
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +020061 unsigned long flags;
Inki Dae9ac26de2016-04-18 17:59:01 +090062 unsigned long out_type;
Andrzej Hajdab8182832015-10-20 18:22:41 +090063 int first_win;
Joonyoung Shimc8466a92015-06-12 21:59:00 +090064};
65
Marek Szyprowskifbbb1e12015-08-31 00:53:57 +090066static const uint32_t decon_formats[] = {
67 DRM_FORMAT_XRGB1555,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_ARGB8888,
71};
72
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +010073static const enum drm_plane_type decon_win_types[WINDOWS_NR] = {
74 DRM_PLANE_TYPE_PRIMARY,
75 DRM_PLANE_TYPE_OVERLAY,
76 DRM_PLANE_TYPE_CURSOR,
77};
78
Andrzej Hajdab2192072015-10-20 11:22:37 +020079static inline void decon_set_bits(struct decon_context *ctx, u32 reg, u32 mask,
80 u32 val)
81{
82 val = (val & mask) | (readl(ctx->addr + reg) & ~mask);
83 writel(val, ctx->addr + reg);
84}
85
Joonyoung Shimc8466a92015-06-12 21:59:00 +090086static int decon_enable_vblank(struct exynos_drm_crtc *crtc)
87{
88 struct decon_context *ctx = crtc->ctx;
89 u32 val;
90
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +020091 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +090092 return -EPERM;
93
Marek Szyprowskif3fb3d82016-02-03 13:42:54 +010094 if (!test_and_set_bit(BIT_IRQS_ENABLED, &ctx->flags)) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +090095 val = VIDINTCON0_INTEN;
Inki Dae9ac26de2016-04-18 17:59:01 +090096 if (ctx->out_type & IFTYPE_I80)
Joonyoung Shimc8466a92015-06-12 21:59:00 +090097 val |= VIDINTCON0_FRAMEDONE;
98 else
99 val |= VIDINTCON0_INTFRMEN;
100
101 writel(val, ctx->addr + DECON_VIDINTCON0);
102 }
103
104 return 0;
105}
106
107static void decon_disable_vblank(struct exynos_drm_crtc *crtc)
108{
109 struct decon_context *ctx = crtc->ctx;
110
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200111 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900112 return;
113
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200114 if (test_and_clear_bit(BIT_IRQS_ENABLED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900115 writel(0, ctx->addr + DECON_VIDINTCON0);
116}
117
118static void decon_setup_trigger(struct decon_context *ctx)
119{
Inki Dae9ac26de2016-04-18 17:59:01 +0900120 u32 val = !(ctx->out_type & I80_HW_TRG)
Andrzej Hajdab8182832015-10-20 18:22:41 +0900121 ? TRIGCON_TRIGEN_PER_F | TRIGCON_TRIGEN_F |
122 TRIGCON_TE_AUTO_MASK | TRIGCON_SWTRIGEN
123 : TRIGCON_TRIGEN_PER_F | TRIGCON_TRIGEN_F |
Inki Daeb5bf0f12016-04-12 09:59:11 +0900124 TRIGCON_HWTRIGMASK | TRIGCON_HWTRIGEN;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900125 writel(val, ctx->addr + DECON_TRIGCON);
126}
127
128static void decon_commit(struct exynos_drm_crtc *crtc)
129{
130 struct decon_context *ctx = crtc->ctx;
Andrzej Hajda85de2752015-10-20 11:22:36 +0200131 struct drm_display_mode *m = &crtc->base.mode;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900132 u32 val;
133
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200134 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900135 return;
136
Inki Dae9ac26de2016-04-18 17:59:01 +0900137 if (ctx->out_type & IFTYPE_HDMI) {
Andrzej Hajdab8182832015-10-20 18:22:41 +0900138 m->crtc_hsync_start = m->crtc_hdisplay + 10;
139 m->crtc_hsync_end = m->crtc_htotal - 92;
140 m->crtc_vsync_start = m->crtc_vdisplay + 1;
141 m->crtc_vsync_end = m->crtc_vsync_start + 1;
142 }
143
144 decon_set_bits(ctx, DECON_VIDCON0, VIDCON0_ENVID, 0);
145
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900146 /* enable clock gate */
147 val = CMU_CLKGAGE_MODE_SFR_F | CMU_CLKGAGE_MODE_MEM_F;
148 writel(val, ctx->addr + DECON_CMU);
149
Andrzej Hajdadd65a682016-04-29 15:42:49 +0200150 if (ctx->out_type & (IFTYPE_I80 | I80_HW_TRG))
151 decon_setup_trigger(ctx);
152
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900153 /* lcd on and use command if */
154 val = VIDOUT_LCD_ON;
Inki Dae9ac26de2016-04-18 17:59:01 +0900155 if (ctx->out_type & IFTYPE_I80) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900156 val |= VIDOUT_COMMAND_IF;
Inki Dae9ac26de2016-04-18 17:59:01 +0900157 } else {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900158 val |= VIDOUT_RGB_IF;
Inki Dae9ac26de2016-04-18 17:59:01 +0900159 }
160
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900161 writel(val, ctx->addr + DECON_VIDOUTCON0);
162
Andrzej Hajda85de2752015-10-20 11:22:36 +0200163 val = VIDTCON2_LINEVAL(m->vdisplay - 1) |
164 VIDTCON2_HOZVAL(m->hdisplay - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900165 writel(val, ctx->addr + DECON_VIDTCON2);
166
Inki Dae9ac26de2016-04-18 17:59:01 +0900167 if (!(ctx->out_type & IFTYPE_I80)) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900168 val = VIDTCON00_VBPD_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200169 m->crtc_vtotal - m->crtc_vsync_end - 1) |
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900170 VIDTCON00_VFPD_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200171 m->crtc_vsync_start - m->crtc_vdisplay - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900172 writel(val, ctx->addr + DECON_VIDTCON00);
173
174 val = VIDTCON01_VSPW_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200175 m->crtc_vsync_end - m->crtc_vsync_start - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900176 writel(val, ctx->addr + DECON_VIDTCON01);
177
178 val = VIDTCON10_HBPD_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200179 m->crtc_htotal - m->crtc_hsync_end - 1) |
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900180 VIDTCON10_HFPD_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200181 m->crtc_hsync_start - m->crtc_hdisplay - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900182 writel(val, ctx->addr + DECON_VIDTCON10);
183
184 val = VIDTCON11_HSPW_F(
Andrzej Hajda85de2752015-10-20 11:22:36 +0200185 m->crtc_hsync_end - m->crtc_hsync_start - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900186 writel(val, ctx->addr + DECON_VIDTCON11);
187 }
188
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900189 /* enable output and display signal */
Andrzej Hajdab8182832015-10-20 18:22:41 +0900190 decon_set_bits(ctx, DECON_VIDCON0, VIDCON0_ENVID | VIDCON0_ENVID_F, ~0);
Andrzej Hajda92ead492016-03-23 14:15:16 +0100191
192 decon_set_bits(ctx, DECON_UPDATE, STANDALONE_UPDATE_F, ~0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900193}
194
Gustavo Padovan2eeb2e52015-08-03 14:40:44 +0900195static void decon_win_set_pixfmt(struct decon_context *ctx, unsigned int win,
196 struct drm_framebuffer *fb)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900197{
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900198 unsigned long val;
199
200 val = readl(ctx->addr + DECON_WINCONx(win));
201 val &= ~WINCONx_BPPMODE_MASK;
202
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200203 switch (fb->format->format) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900204 case DRM_FORMAT_XRGB1555:
205 val |= WINCONx_BPPMODE_16BPP_I1555;
206 val |= WINCONx_HAWSWP_F;
207 val |= WINCONx_BURSTLEN_16WORD;
208 break;
209 case DRM_FORMAT_RGB565:
210 val |= WINCONx_BPPMODE_16BPP_565;
211 val |= WINCONx_HAWSWP_F;
212 val |= WINCONx_BURSTLEN_16WORD;
213 break;
214 case DRM_FORMAT_XRGB8888:
215 val |= WINCONx_BPPMODE_24BPP_888;
216 val |= WINCONx_WSWP_F;
217 val |= WINCONx_BURSTLEN_16WORD;
218 break;
219 case DRM_FORMAT_ARGB8888:
220 val |= WINCONx_BPPMODE_32BPP_A8888;
221 val |= WINCONx_WSWP_F | WINCONx_BLD_PIX_F | WINCONx_ALPHA_SEL_F;
222 val |= WINCONx_BURSTLEN_16WORD;
223 break;
224 default:
225 DRM_ERROR("Proper pixel format is not set\n");
226 return;
227 }
228
Ville Syrjälä272725c2016-12-14 23:32:20 +0200229 DRM_DEBUG_KMS("bpp = %u\n", fb->format->cpp[0] * 8);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900230
231 /*
232 * In case of exynos, setting dma-burst to 16Word causes permanent
233 * tearing for very small buffers, e.g. cursor buffer. Burst Mode
234 * switching which is based on plane size is not recommended as
235 * plane size varies a lot towards the end of the screen and rapid
236 * movement causes unstable DMA which results into iommu crash/tear.
237 */
238
Gustavo Padovan2eeb2e52015-08-03 14:40:44 +0900239 if (fb->width < MIN_FB_WIDTH_FOR_16WORD_BURST) {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900240 val &= ~WINCONx_BURSTLEN_MASK;
241 val |= WINCONx_BURSTLEN_8WORD;
242 }
243
244 writel(val, ctx->addr + DECON_WINCONx(win));
245}
246
247static void decon_shadow_protect_win(struct decon_context *ctx, int win,
248 bool protect)
249{
Andrzej Hajdab2192072015-10-20 11:22:37 +0200250 decon_set_bits(ctx, DECON_SHADOWCON, SHADOWCON_Wx_PROTECT(win),
251 protect ? ~0 : 0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900252}
253
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100254static void decon_atomic_begin(struct exynos_drm_crtc *crtc)
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900255{
256 struct decon_context *ctx = crtc->ctx;
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100257 int i;
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900258
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200259 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900260 return;
261
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100262 for (i = ctx->first_win; i < WINDOWS_NR; i++)
263 decon_shadow_protect_win(ctx, i, true);
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900264}
265
Andrzej Hajdab8182832015-10-20 18:22:41 +0900266#define BIT_VAL(x, e, s) (((x) & ((1 << ((e) - (s) + 1)) - 1)) << (s))
267#define COORDINATE_X(x) BIT_VAL((x), 23, 12)
268#define COORDINATE_Y(x) BIT_VAL((x), 11, 0)
269
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900270static void decon_update_plane(struct exynos_drm_crtc *crtc,
271 struct exynos_drm_plane *plane)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900272{
Marek Szyprowski0114f402015-11-30 14:53:22 +0100273 struct exynos_drm_plane_state *state =
274 to_exynos_plane_state(plane->base.state);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900275 struct decon_context *ctx = crtc->ctx;
Marek Szyprowski0114f402015-11-30 14:53:22 +0100276 struct drm_framebuffer *fb = state->base.fb;
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100277 unsigned int win = plane->index;
Ville Syrjälä272725c2016-12-14 23:32:20 +0200278 unsigned int bpp = fb->format->cpp[0];
Marek Szyprowski0488f502015-11-30 14:53:21 +0100279 unsigned int pitch = fb->pitches[0];
280 dma_addr_t dma_addr = exynos_drm_fb_dma_addr(fb, 0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900281 u32 val;
282
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200283 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900284 return;
285
Marek Szyprowski0114f402015-11-30 14:53:22 +0100286 val = COORDINATE_X(state->crtc.x) | COORDINATE_Y(state->crtc.y);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900287 writel(val, ctx->addr + DECON_VIDOSDxA(win));
288
Marek Szyprowski0114f402015-11-30 14:53:22 +0100289 val = COORDINATE_X(state->crtc.x + state->crtc.w - 1) |
290 COORDINATE_Y(state->crtc.y + state->crtc.h - 1);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900291 writel(val, ctx->addr + DECON_VIDOSDxB(win));
292
293 val = VIDOSD_Wx_ALPHA_R_F(0x0) | VIDOSD_Wx_ALPHA_G_F(0x0) |
294 VIDOSD_Wx_ALPHA_B_F(0x0);
295 writel(val, ctx->addr + DECON_VIDOSDxC(win));
296
297 val = VIDOSD_Wx_ALPHA_R_F(0x0) | VIDOSD_Wx_ALPHA_G_F(0x0) |
298 VIDOSD_Wx_ALPHA_B_F(0x0);
299 writel(val, ctx->addr + DECON_VIDOSDxD(win));
300
Marek Szyprowski0488f502015-11-30 14:53:21 +0100301 writel(dma_addr, ctx->addr + DECON_VIDW0xADD0B0(win));
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900302
Marek Szyprowski0114f402015-11-30 14:53:22 +0100303 val = dma_addr + pitch * state->src.h;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900304 writel(val, ctx->addr + DECON_VIDW0xADD1B0(win));
305
Inki Dae9ac26de2016-04-18 17:59:01 +0900306 if (!(ctx->out_type & IFTYPE_HDMI))
Marek Szyprowski0114f402015-11-30 14:53:22 +0100307 val = BIT_VAL(pitch - state->crtc.w * bpp, 27, 14)
308 | BIT_VAL(state->crtc.w * bpp, 13, 0);
Andrzej Hajdab8182832015-10-20 18:22:41 +0900309 else
Marek Szyprowski0114f402015-11-30 14:53:22 +0100310 val = BIT_VAL(pitch - state->crtc.w * bpp, 29, 15)
311 | BIT_VAL(state->crtc.w * bpp, 14, 0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900312 writel(val, ctx->addr + DECON_VIDW0xADD2(win));
313
Marek Szyprowski0488f502015-11-30 14:53:21 +0100314 decon_win_set_pixfmt(ctx, win, fb);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900315
316 /* window enable */
Andrzej Hajdab2192072015-10-20 11:22:37 +0200317 decon_set_bits(ctx, DECON_WINCONx(win), WINCONx_ENWIN_F, ~0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900318}
319
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900320static void decon_disable_plane(struct exynos_drm_crtc *crtc,
321 struct exynos_drm_plane *plane)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900322{
323 struct decon_context *ctx = crtc->ctx;
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100324 unsigned int win = plane->index;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900325
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200326 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900327 return;
328
Andrzej Hajdab2192072015-10-20 11:22:37 +0200329 decon_set_bits(ctx, DECON_WINCONx(win), WINCONx_ENWIN_F, 0);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900330}
331
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100332static void decon_atomic_flush(struct exynos_drm_crtc *crtc)
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900333{
334 struct decon_context *ctx = crtc->ctx;
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100335 int i;
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900336
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200337 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900338 return;
339
Marek Szyprowskid29c2c12016-01-05 13:52:51 +0100340 for (i = ctx->first_win; i < WINDOWS_NR; i++)
341 decon_shadow_protect_win(ctx, i, false);
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900342
Andrzej Hajda92ead492016-03-23 14:15:16 +0100343 /* standalone update */
344 decon_set_bits(ctx, DECON_UPDATE, STANDALONE_UPDATE_F, ~0);
345
Inki Dae9ac26de2016-04-18 17:59:01 +0900346 if (ctx->out_type & IFTYPE_I80)
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200347 set_bit(BIT_WIN_UPDATED, &ctx->flags);
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900348}
349
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900350static void decon_swreset(struct decon_context *ctx)
351{
352 unsigned int tries;
353
354 writel(0, ctx->addr + DECON_VIDCON0);
355 for (tries = 2000; tries; --tries) {
356 if (~readl(ctx->addr + DECON_VIDCON0) & VIDCON0_STOP_STATUS)
357 break;
358 udelay(10);
359 }
360
361 WARN(tries == 0, "failed to disable DECON\n");
362
363 writel(VIDCON0_SWRESET, ctx->addr + DECON_VIDCON0);
364 for (tries = 2000; tries; --tries) {
365 if (~readl(ctx->addr + DECON_VIDCON0) & VIDCON0_SWRESET)
366 break;
367 udelay(10);
368 }
369
370 WARN(tries == 0, "failed to software reset DECON\n");
Andrzej Hajdab8182832015-10-20 18:22:41 +0900371
Inki Dae9ac26de2016-04-18 17:59:01 +0900372 if (!(ctx->out_type & IFTYPE_HDMI))
Andrzej Hajdab8182832015-10-20 18:22:41 +0900373 return;
374
375 writel(VIDCON0_CLKVALUP | VIDCON0_VLCKFREE, ctx->addr + DECON_VIDCON0);
376 decon_set_bits(ctx, DECON_CMU,
377 CMU_CLKGAGE_MODE_SFR_F | CMU_CLKGAGE_MODE_MEM_F, ~0);
378 writel(VIDCON1_VCLK_RUN_VDEN_DISABLE, ctx->addr + DECON_VIDCON1);
379 writel(CRCCTRL_CRCEN | CRCCTRL_CRCSTART_F | CRCCTRL_CRCCLKEN,
380 ctx->addr + DECON_CRCCTRL);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900381}
382
383static void decon_enable(struct exynos_drm_crtc *crtc)
384{
385 struct decon_context *ctx = crtc->ctx;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900386
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200387 if (!test_and_clear_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900388 return;
389
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900390 pm_runtime_get_sync(ctx->dev);
391
Andrzej Hajdac60230e2016-03-23 14:26:00 +0100392 exynos_drm_pipe_clk_enable(crtc, true);
393
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200394 set_bit(BIT_CLKS_ENABLED, &ctx->flags);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900395
Andrzej Hajdae87b3c62016-03-23 14:15:17 +0100396 decon_swreset(ctx);
397
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900398 /* if vblank was enabled status, enable it again. */
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200399 if (test_and_clear_bit(BIT_IRQS_ENABLED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900400 decon_enable_vblank(ctx->crtc);
401
402 decon_commit(ctx->crtc);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900403}
404
405static void decon_disable(struct exynos_drm_crtc *crtc)
406{
407 struct decon_context *ctx = crtc->ctx;
408 int i;
409
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200410 if (test_bit(BIT_SUSPENDED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900411 return;
412
413 /*
414 * We need to make sure that all windows are disabled before we
415 * suspend that connector. Otherwise we might try to scan from
416 * a destroyed buffer later.
417 */
Andrzej Hajdab8182832015-10-20 18:22:41 +0900418 for (i = ctx->first_win; i < WINDOWS_NR; i++)
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900419 decon_disable_plane(crtc, &ctx->planes[i]);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900420
421 decon_swreset(ctx);
422
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200423 clear_bit(BIT_CLKS_ENABLED, &ctx->flags);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900424
Andrzej Hajdac60230e2016-03-23 14:26:00 +0100425 exynos_drm_pipe_clk_enable(crtc, false);
426
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900427 pm_runtime_put_sync(ctx->dev);
428
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200429 set_bit(BIT_SUSPENDED, &ctx->flags);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900430}
431
Andrzej Hajda9844d6e2016-02-11 12:55:46 +0100432static void decon_te_irq_handler(struct exynos_drm_crtc *crtc)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900433{
434 struct decon_context *ctx = crtc->ctx;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900435
Andrzej Hajda3f4c8e52016-04-29 15:42:48 +0200436 if (!test_bit(BIT_CLKS_ENABLED, &ctx->flags) ||
437 (ctx->out_type & I80_HW_TRG))
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}
443
444static void decon_clear_channels(struct exynos_drm_crtc *crtc)
445{
446 struct decon_context *ctx = crtc->ctx;
447 int win, i, ret;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900448
449 DRM_DEBUG_KMS("%s\n", __FILE__);
450
451 for (i = 0; i < ARRAY_SIZE(decon_clks_name); i++) {
452 ret = clk_prepare_enable(ctx->clks[i]);
453 if (ret < 0)
454 goto err;
455 }
456
457 for (win = 0; win < WINDOWS_NR; win++) {
Andrzej Hajdab2192072015-10-20 11:22:37 +0200458 decon_shadow_protect_win(ctx, win, true);
459 decon_set_bits(ctx, DECON_WINCONx(win), WINCONx_ENWIN_F, 0);
460 decon_shadow_protect_win(ctx, win, false);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900461 }
Andrzej Hajda92ead492016-03-23 14:15:16 +0100462
463 decon_set_bits(ctx, DECON_UPDATE, STANDALONE_UPDATE_F, ~0);
464
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900465 /* TODO: wait for possible vsync */
466 msleep(50);
467
468err:
469 while (--i >= 0)
470 clk_disable_unprepare(ctx->clks[i]);
471}
472
Bhumika Goyalfc36ec72017-01-09 23:24:53 +0530473static const struct exynos_drm_crtc_ops decon_crtc_ops = {
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900474 .enable = decon_enable,
475 .disable = decon_disable,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900476 .enable_vblank = decon_enable_vblank,
477 .disable_vblank = decon_disable_vblank,
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900478 .atomic_begin = decon_atomic_begin,
Gustavo Padovan9cc76102015-08-03 14:38:05 +0900479 .update_plane = decon_update_plane,
480 .disable_plane = decon_disable_plane,
Hyungwon Hwangcc5a7b32015-08-27 18:21:14 +0900481 .atomic_flush = decon_atomic_flush,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900482 .te_handler = decon_te_irq_handler,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900483};
484
485static int decon_bind(struct device *dev, struct device *master, void *data)
486{
487 struct decon_context *ctx = dev_get_drvdata(dev);
488 struct drm_device *drm_dev = data;
489 struct exynos_drm_private *priv = drm_dev->dev_private;
490 struct exynos_drm_plane *exynos_plane;
Andrzej Hajdab8182832015-10-20 18:22:41 +0900491 enum exynos_drm_output_type out_type;
Andrzej Hajdab8182832015-10-20 18:22:41 +0900492 unsigned int win;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900493 int ret;
494
495 ctx->drm_dev = drm_dev;
496 ctx->pipe = priv->pipe++;
497
Andrzej Hajdab8182832015-10-20 18:22:41 +0900498 for (win = ctx->first_win; win < WINDOWS_NR; win++) {
499 int tmp = (win == ctx->first_win) ? 0 : win;
500
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100501 ctx->configs[win].pixel_formats = decon_formats;
502 ctx->configs[win].num_pixel_formats = ARRAY_SIZE(decon_formats);
503 ctx->configs[win].zpos = win;
504 ctx->configs[win].type = decon_win_types[tmp];
505
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100506 ret = exynos_plane_init(drm_dev, &ctx->planes[win], win,
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100507 1 << ctx->pipe, &ctx->configs[win]);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900508 if (ret)
509 return ret;
510 }
511
Andrzej Hajdab8182832015-10-20 18:22:41 +0900512 exynos_plane = &ctx->planes[ctx->first_win];
Inki Dae9ac26de2016-04-18 17:59:01 +0900513 out_type = (ctx->out_type & IFTYPE_HDMI) ? EXYNOS_DISPLAY_TYPE_HDMI
Andrzej Hajdab8182832015-10-20 18:22:41 +0900514 : EXYNOS_DISPLAY_TYPE_LCD;
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900515 ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
Andrzej Hajdab8182832015-10-20 18:22:41 +0900516 ctx->pipe, out_type,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900517 &decon_crtc_ops, ctx);
518 if (IS_ERR(ctx->crtc)) {
519 ret = PTR_ERR(ctx->crtc);
520 goto err;
521 }
522
Joonyoung Shimeb7a3fc2015-07-02 21:49:39 +0900523 decon_clear_channels(ctx->crtc);
524
525 ret = drm_iommu_attach_device(drm_dev, dev);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900526 if (ret)
527 goto err;
528
529 return ret;
530err:
531 priv->pipe--;
532 return ret;
533}
534
535static void decon_unbind(struct device *dev, struct device *master, void *data)
536{
537 struct decon_context *ctx = dev_get_drvdata(dev);
538
539 decon_disable(ctx->crtc);
540
541 /* detach this sub driver from iommu mapping if supported. */
Joonyoung Shimbf566082015-07-02 21:49:38 +0900542 drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900543}
544
545static const struct component_ops decon_component_ops = {
546 .bind = decon_bind,
547 .unbind = decon_unbind,
548};
549
Andrzej Hajdab8182832015-10-20 18:22:41 +0900550static irqreturn_t decon_irq_handler(int irq, void *dev_id)
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900551{
552 struct decon_context *ctx = dev_id;
553 u32 val;
554
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200555 if (!test_bit(BIT_CLKS_ENABLED, &ctx->flags))
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900556 goto out;
557
558 val = readl(ctx->addr + DECON_VIDINTCON1);
Andrzej Hajdab8182832015-10-20 18:22:41 +0900559 val &= VIDINTCON1_INTFRMDONEPEND | VIDINTCON1_INTFRMPEND;
560
561 if (val) {
Andrzej Hajdab8182832015-10-20 18:22:41 +0900562 writel(val, ctx->addr + DECON_VIDINTCON1);
Andrzej Hajdab0bb3d02016-04-29 15:42:47 +0200563 drm_crtc_handle_vblank(&ctx->crtc->base);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900564 }
565
566out:
567 return IRQ_HANDLED;
568}
569
Gustavo Padovanebf3fd42015-11-02 20:54:55 +0900570#ifdef CONFIG_PM
571static int exynos5433_decon_suspend(struct device *dev)
572{
573 struct decon_context *ctx = dev_get_drvdata(dev);
Andrzej Hajda92c96ff2016-02-11 12:25:04 +0100574 int i = ARRAY_SIZE(decon_clks_name);
Gustavo Padovanebf3fd42015-11-02 20:54:55 +0900575
Andrzej Hajda92c96ff2016-02-11 12:25:04 +0100576 while (--i >= 0)
Gustavo Padovanebf3fd42015-11-02 20:54:55 +0900577 clk_disable_unprepare(ctx->clks[i]);
578
579 return 0;
580}
581
582static int exynos5433_decon_resume(struct device *dev)
583{
584 struct decon_context *ctx = dev_get_drvdata(dev);
585 int i, ret;
586
587 for (i = 0; i < ARRAY_SIZE(decon_clks_name); i++) {
588 ret = clk_prepare_enable(ctx->clks[i]);
589 if (ret < 0)
590 goto err;
591 }
592
593 return 0;
594
595err:
596 while (--i >= 0)
597 clk_disable_unprepare(ctx->clks[i]);
598
599 return ret;
600}
601#endif
602
603static const struct dev_pm_ops exynos5433_decon_pm_ops = {
604 SET_RUNTIME_PM_OPS(exynos5433_decon_suspend, exynos5433_decon_resume,
605 NULL)
606};
607
Andrzej Hajdab8182832015-10-20 18:22:41 +0900608static const struct of_device_id exynos5433_decon_driver_dt_match[] = {
609 {
610 .compatible = "samsung,exynos5433-decon",
Inki Dae9ac26de2016-04-18 17:59:01 +0900611 .data = (void *)I80_HW_TRG
Andrzej Hajdab8182832015-10-20 18:22:41 +0900612 },
613 {
614 .compatible = "samsung,exynos5433-decon-tv",
Inki Dae9ac26de2016-04-18 17:59:01 +0900615 .data = (void *)(I80_HW_TRG | IFTYPE_HDMI)
Andrzej Hajdab8182832015-10-20 18:22:41 +0900616 },
617 {},
618};
619MODULE_DEVICE_TABLE(of, exynos5433_decon_driver_dt_match);
620
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900621static int exynos5433_decon_probe(struct platform_device *pdev)
622{
623 struct device *dev = &pdev->dev;
624 struct decon_context *ctx;
625 struct resource *res;
626 int ret;
627 int i;
628
629 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
630 if (!ctx)
631 return -ENOMEM;
632
Andrzej Hajda7b6bb6e2015-10-20 11:22:38 +0200633 __set_bit(BIT_SUSPENDED, &ctx->flags);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900634 ctx->dev = dev;
Inki Dae9ac26de2016-04-18 17:59:01 +0900635 ctx->out_type = (unsigned long)of_device_get_match_data(dev);
Andrzej Hajdab8182832015-10-20 18:22:41 +0900636
Inki Dae9ac26de2016-04-18 17:59:01 +0900637 if (ctx->out_type & IFTYPE_HDMI) {
Andrzej Hajdab8182832015-10-20 18:22:41 +0900638 ctx->first_win = 1;
Inki Dae9ac26de2016-04-18 17:59:01 +0900639 } else if (of_get_child_by_name(dev->of_node, "i80-if-timings")) {
Andrzej Hajdadd65a682016-04-29 15:42:49 +0200640 ctx->out_type |= IFTYPE_I80;
Inki Dae9ac26de2016-04-18 17:59:01 +0900641 }
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900642
643 for (i = 0; i < ARRAY_SIZE(decon_clks_name); i++) {
644 struct clk *clk;
645
646 clk = devm_clk_get(ctx->dev, decon_clks_name[i]);
647 if (IS_ERR(clk))
648 return PTR_ERR(clk);
649
650 ctx->clks[i] = clk;
651 }
652
653 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
654 if (!res) {
655 dev_err(dev, "cannot find IO resource\n");
656 return -ENXIO;
657 }
658
659 ctx->addr = devm_ioremap_resource(dev, res);
660 if (IS_ERR(ctx->addr)) {
661 dev_err(dev, "ioremap failed\n");
662 return PTR_ERR(ctx->addr);
663 }
664
665 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
Inki Dae9ac26de2016-04-18 17:59:01 +0900666 (ctx->out_type & IFTYPE_I80) ? "lcd_sys" : "vsync");
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900667 if (!res) {
668 dev_err(dev, "cannot find IRQ resource\n");
669 return -ENXIO;
670 }
671
Andrzej Hajdab8182832015-10-20 18:22:41 +0900672 ret = devm_request_irq(dev, res->start, decon_irq_handler, 0,
673 "drm_decon", ctx);
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900674 if (ret < 0) {
675 dev_err(dev, "lcd_sys irq request failed\n");
676 return ret;
677 }
678
679 platform_set_drvdata(pdev, ctx);
680
681 pm_runtime_enable(dev);
682
683 ret = component_add(dev, &decon_component_ops);
684 if (ret)
685 goto err_disable_pm_runtime;
686
687 return 0;
688
689err_disable_pm_runtime:
690 pm_runtime_disable(dev);
691
692 return ret;
693}
694
695static int exynos5433_decon_remove(struct platform_device *pdev)
696{
697 pm_runtime_disable(&pdev->dev);
698
699 component_del(&pdev->dev, &decon_component_ops);
700
701 return 0;
702}
703
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900704struct platform_driver exynos5433_decon_driver = {
705 .probe = exynos5433_decon_probe,
706 .remove = exynos5433_decon_remove,
707 .driver = {
708 .name = "exynos5433-decon",
Gustavo Padovanebf3fd42015-11-02 20:54:55 +0900709 .pm = &exynos5433_decon_pm_ops,
Joonyoung Shimc8466a92015-06-12 21:59:00 +0900710 .of_match_table = exynos5433_decon_driver_dt_match,
711 },
712};