xref: /linux/drivers/gpu/drm/exynos/exynos_mixer.c (revision 2eeb2e5e6634dbb97bdda81f65f70e4b037028d5)
1d8408326SSeung-Woo Kim /*
2d8408326SSeung-Woo Kim  * Copyright (C) 2011 Samsung Electronics Co.Ltd
3d8408326SSeung-Woo Kim  * Authors:
4d8408326SSeung-Woo Kim  * Seung-Woo Kim <sw0312.kim@samsung.com>
5d8408326SSeung-Woo Kim  *	Inki Dae <inki.dae@samsung.com>
6d8408326SSeung-Woo Kim  *	Joonyoung Shim <jy0922.shim@samsung.com>
7d8408326SSeung-Woo Kim  *
8d8408326SSeung-Woo Kim  * Based on drivers/media/video/s5p-tv/mixer_reg.c
9d8408326SSeung-Woo Kim  *
10d8408326SSeung-Woo Kim  * This program is free software; you can redistribute  it and/or modify it
11d8408326SSeung-Woo Kim  * under  the terms of  the GNU General  Public License as published by the
12d8408326SSeung-Woo Kim  * Free Software Foundation;  either version 2 of the  License, or (at your
13d8408326SSeung-Woo Kim  * option) any later version.
14d8408326SSeung-Woo Kim  *
15d8408326SSeung-Woo Kim  */
16d8408326SSeung-Woo Kim 
17760285e7SDavid Howells #include <drm/drmP.h>
18d8408326SSeung-Woo Kim 
19d8408326SSeung-Woo Kim #include "regs-mixer.h"
20d8408326SSeung-Woo Kim #include "regs-vp.h"
21d8408326SSeung-Woo Kim 
22d8408326SSeung-Woo Kim #include <linux/kernel.h>
23d8408326SSeung-Woo Kim #include <linux/spinlock.h>
24d8408326SSeung-Woo Kim #include <linux/wait.h>
25d8408326SSeung-Woo Kim #include <linux/i2c.h>
26d8408326SSeung-Woo Kim #include <linux/platform_device.h>
27d8408326SSeung-Woo Kim #include <linux/interrupt.h>
28d8408326SSeung-Woo Kim #include <linux/irq.h>
29d8408326SSeung-Woo Kim #include <linux/delay.h>
30d8408326SSeung-Woo Kim #include <linux/pm_runtime.h>
31d8408326SSeung-Woo Kim #include <linux/clk.h>
32d8408326SSeung-Woo Kim #include <linux/regulator/consumer.h>
333f1c781dSSachin Kamat #include <linux/of.h>
34f37cd5e8SInki Dae #include <linux/component.h>
35d8408326SSeung-Woo Kim 
36d8408326SSeung-Woo Kim #include <drm/exynos_drm.h>
37d8408326SSeung-Woo Kim 
38d8408326SSeung-Woo Kim #include "exynos_drm_drv.h"
39663d8766SRahul Sharma #include "exynos_drm_crtc.h"
407ee14cdcSGustavo Padovan #include "exynos_drm_plane.h"
411055b39fSInki Dae #include "exynos_drm_iommu.h"
42f041b257SSean Paul #include "exynos_mixer.h"
4322b21ae6SJoonyoung Shim 
44f041b257SSean Paul #define MIXER_WIN_NR		3
45f041b257SSean Paul #define MIXER_DEFAULT_WIN	0
46d8408326SSeung-Woo Kim 
477a57ca7cSTobias Jakobi /* The pixelformats that are natively supported by the mixer. */
487a57ca7cSTobias Jakobi #define MXR_FORMAT_RGB565	4
497a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB1555	5
507a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB4444	6
517a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB8888	7
527a57ca7cSTobias Jakobi 
5322b21ae6SJoonyoung Shim struct mixer_resources {
5422b21ae6SJoonyoung Shim 	int			irq;
5522b21ae6SJoonyoung Shim 	void __iomem		*mixer_regs;
5622b21ae6SJoonyoung Shim 	void __iomem		*vp_regs;
5722b21ae6SJoonyoung Shim 	spinlock_t		reg_slock;
5822b21ae6SJoonyoung Shim 	struct clk		*mixer;
5922b21ae6SJoonyoung Shim 	struct clk		*vp;
6004427ec5SMarek Szyprowski 	struct clk		*hdmi;
6122b21ae6SJoonyoung Shim 	struct clk		*sclk_mixer;
6222b21ae6SJoonyoung Shim 	struct clk		*sclk_hdmi;
63ff830c96SMarek Szyprowski 	struct clk		*mout_mixer;
6422b21ae6SJoonyoung Shim };
6522b21ae6SJoonyoung Shim 
661e123441SRahul Sharma enum mixer_version_id {
671e123441SRahul Sharma 	MXR_VER_0_0_0_16,
681e123441SRahul Sharma 	MXR_VER_16_0_33_0,
69def5e095SRahul Sharma 	MXR_VER_128_0_0_184,
701e123441SRahul Sharma };
711e123441SRahul Sharma 
72a44652e8SAndrzej Hajda enum mixer_flag_bits {
73a44652e8SAndrzej Hajda 	MXR_BIT_POWERED,
740df5e4acSAndrzej Hajda 	MXR_BIT_VSYNC,
75a44652e8SAndrzej Hajda };
76a44652e8SAndrzej Hajda 
7722b21ae6SJoonyoung Shim struct mixer_context {
784551789fSSean Paul 	struct platform_device *pdev;
79cf8fc4f1SJoonyoung Shim 	struct device		*dev;
801055b39fSInki Dae 	struct drm_device	*drm_dev;
8193bca243SGustavo Padovan 	struct exynos_drm_crtc	*crtc;
827ee14cdcSGustavo Padovan 	struct exynos_drm_plane	planes[MIXER_WIN_NR];
8322b21ae6SJoonyoung Shim 	int			pipe;
84a44652e8SAndrzej Hajda 	unsigned long		flags;
8522b21ae6SJoonyoung Shim 	bool			interlace;
861b8e5747SRahul Sharma 	bool			vp_enabled;
87ff830c96SMarek Szyprowski 	bool			has_sclk;
8822b21ae6SJoonyoung Shim 
8922b21ae6SJoonyoung Shim 	struct mixer_resources	mixer_res;
901e123441SRahul Sharma 	enum mixer_version_id	mxr_ver;
916e95d5e6SPrathyush K 	wait_queue_head_t	wait_vsync_queue;
926e95d5e6SPrathyush K 	atomic_t		wait_vsync_event;
931e123441SRahul Sharma };
941e123441SRahul Sharma 
951e123441SRahul Sharma struct mixer_drv_data {
961e123441SRahul Sharma 	enum mixer_version_id	version;
971b8e5747SRahul Sharma 	bool					is_vp_enabled;
98ff830c96SMarek Szyprowski 	bool					has_sclk;
9922b21ae6SJoonyoung Shim };
10022b21ae6SJoonyoung Shim 
101d8408326SSeung-Woo Kim static const u8 filter_y_horiz_tap8[] = {
102d8408326SSeung-Woo Kim 	0,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
103d8408326SSeung-Woo Kim 	-1,	-1,	-1,	-1,	-1,	0,	0,	0,
104d8408326SSeung-Woo Kim 	0,	2,	4,	5,	6,	6,	6,	6,
105d8408326SSeung-Woo Kim 	6,	5,	5,	4,	3,	2,	1,	1,
106d8408326SSeung-Woo Kim 	0,	-6,	-12,	-16,	-18,	-20,	-21,	-20,
107d8408326SSeung-Woo Kim 	-20,	-18,	-16,	-13,	-10,	-8,	-5,	-2,
108d8408326SSeung-Woo Kim 	127,	126,	125,	121,	114,	107,	99,	89,
109d8408326SSeung-Woo Kim 	79,	68,	57,	46,	35,	25,	16,	8,
110d8408326SSeung-Woo Kim };
111d8408326SSeung-Woo Kim 
112d8408326SSeung-Woo Kim static const u8 filter_y_vert_tap4[] = {
113d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
114d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
115d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
116d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
117d8408326SSeung-Woo Kim 	0,	5,	11,	19,	27,	37,	48,	59,
118d8408326SSeung-Woo Kim 	70,	81,	92,	102,	111,	118,	124,	126,
119d8408326SSeung-Woo Kim 	0,	0,	-1,	-1,	-2,	-3,	-4,	-5,
120d8408326SSeung-Woo Kim 	-6,	-7,	-8,	-8,	-8,	-8,	-6,	-3,
121d8408326SSeung-Woo Kim };
122d8408326SSeung-Woo Kim 
123d8408326SSeung-Woo Kim static const u8 filter_cr_horiz_tap4[] = {
124d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
125d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
126d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
127d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
128d8408326SSeung-Woo Kim };
129d8408326SSeung-Woo Kim 
130d8408326SSeung-Woo Kim static inline u32 vp_reg_read(struct mixer_resources *res, u32 reg_id)
131d8408326SSeung-Woo Kim {
132d8408326SSeung-Woo Kim 	return readl(res->vp_regs + reg_id);
133d8408326SSeung-Woo Kim }
134d8408326SSeung-Woo Kim 
135d8408326SSeung-Woo Kim static inline void vp_reg_write(struct mixer_resources *res, u32 reg_id,
136d8408326SSeung-Woo Kim 				 u32 val)
137d8408326SSeung-Woo Kim {
138d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
139d8408326SSeung-Woo Kim }
140d8408326SSeung-Woo Kim 
141d8408326SSeung-Woo Kim static inline void vp_reg_writemask(struct mixer_resources *res, u32 reg_id,
142d8408326SSeung-Woo Kim 				 u32 val, u32 mask)
143d8408326SSeung-Woo Kim {
144d8408326SSeung-Woo Kim 	u32 old = vp_reg_read(res, reg_id);
145d8408326SSeung-Woo Kim 
146d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
147d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
148d8408326SSeung-Woo Kim }
149d8408326SSeung-Woo Kim 
150d8408326SSeung-Woo Kim static inline u32 mixer_reg_read(struct mixer_resources *res, u32 reg_id)
151d8408326SSeung-Woo Kim {
152d8408326SSeung-Woo Kim 	return readl(res->mixer_regs + reg_id);
153d8408326SSeung-Woo Kim }
154d8408326SSeung-Woo Kim 
155d8408326SSeung-Woo Kim static inline void mixer_reg_write(struct mixer_resources *res, u32 reg_id,
156d8408326SSeung-Woo Kim 				 u32 val)
157d8408326SSeung-Woo Kim {
158d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
159d8408326SSeung-Woo Kim }
160d8408326SSeung-Woo Kim 
161d8408326SSeung-Woo Kim static inline void mixer_reg_writemask(struct mixer_resources *res,
162d8408326SSeung-Woo Kim 				 u32 reg_id, u32 val, u32 mask)
163d8408326SSeung-Woo Kim {
164d8408326SSeung-Woo Kim 	u32 old = mixer_reg_read(res, reg_id);
165d8408326SSeung-Woo Kim 
166d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
167d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
168d8408326SSeung-Woo Kim }
169d8408326SSeung-Woo Kim 
170d8408326SSeung-Woo Kim static void mixer_regs_dump(struct mixer_context *ctx)
171d8408326SSeung-Woo Kim {
172d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
173d8408326SSeung-Woo Kim do { \
174d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
175d8408326SSeung-Woo Kim 		(u32)readl(ctx->mixer_res.mixer_regs + reg_id)); \
176d8408326SSeung-Woo Kim } while (0)
177d8408326SSeung-Woo Kim 
178d8408326SSeung-Woo Kim 	DUMPREG(MXR_STATUS);
179d8408326SSeung-Woo Kim 	DUMPREG(MXR_CFG);
180d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_EN);
181d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_STATUS);
182d8408326SSeung-Woo Kim 
183d8408326SSeung-Woo Kim 	DUMPREG(MXR_LAYER_CFG);
184d8408326SSeung-Woo Kim 	DUMPREG(MXR_VIDEO_CFG);
185d8408326SSeung-Woo Kim 
186d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_CFG);
187d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_BASE);
188d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SPAN);
189d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_WH);
190d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SXY);
191d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_DXY);
192d8408326SSeung-Woo Kim 
193d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_CFG);
194d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_BASE);
195d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SPAN);
196d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_WH);
197d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SXY);
198d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_DXY);
199d8408326SSeung-Woo Kim #undef DUMPREG
200d8408326SSeung-Woo Kim }
201d8408326SSeung-Woo Kim 
202d8408326SSeung-Woo Kim static void vp_regs_dump(struct mixer_context *ctx)
203d8408326SSeung-Woo Kim {
204d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
205d8408326SSeung-Woo Kim do { \
206d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
207d8408326SSeung-Woo Kim 		(u32) readl(ctx->mixer_res.vp_regs + reg_id)); \
208d8408326SSeung-Woo Kim } while (0)
209d8408326SSeung-Woo Kim 
210d8408326SSeung-Woo Kim 	DUMPREG(VP_ENABLE);
211d8408326SSeung-Woo Kim 	DUMPREG(VP_SRESET);
212d8408326SSeung-Woo Kim 	DUMPREG(VP_SHADOW_UPDATE);
213d8408326SSeung-Woo Kim 	DUMPREG(VP_FIELD_ID);
214d8408326SSeung-Woo Kim 	DUMPREG(VP_MODE);
215d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_Y);
216d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_C);
217d8408326SSeung-Woo Kim 	DUMPREG(VP_PER_RATE_CTRL);
218d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_Y_PTR);
219d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_Y_PTR);
220d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_C_PTR);
221d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_C_PTR);
222d8408326SSeung-Woo Kim 	DUMPREG(VP_ENDIAN_MODE);
223d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_H_POSITION);
224d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_V_POSITION);
225d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_WIDTH);
226d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_HEIGHT);
227d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_H_POSITION);
228d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_V_POSITION);
229d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_WIDTH);
230d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_HEIGHT);
231d8408326SSeung-Woo Kim 	DUMPREG(VP_H_RATIO);
232d8408326SSeung-Woo Kim 	DUMPREG(VP_V_RATIO);
233d8408326SSeung-Woo Kim 
234d8408326SSeung-Woo Kim #undef DUMPREG
235d8408326SSeung-Woo Kim }
236d8408326SSeung-Woo Kim 
237d8408326SSeung-Woo Kim static inline void vp_filter_set(struct mixer_resources *res,
238d8408326SSeung-Woo Kim 		int reg_id, const u8 *data, unsigned int size)
239d8408326SSeung-Woo Kim {
240d8408326SSeung-Woo Kim 	/* assure 4-byte align */
241d8408326SSeung-Woo Kim 	BUG_ON(size & 3);
242d8408326SSeung-Woo Kim 	for (; size; size -= 4, reg_id += 4, data += 4) {
243d8408326SSeung-Woo Kim 		u32 val = (data[0] << 24) |  (data[1] << 16) |
244d8408326SSeung-Woo Kim 			(data[2] << 8) | data[3];
245d8408326SSeung-Woo Kim 		vp_reg_write(res, reg_id, val);
246d8408326SSeung-Woo Kim 	}
247d8408326SSeung-Woo Kim }
248d8408326SSeung-Woo Kim 
249d8408326SSeung-Woo Kim static void vp_default_filter(struct mixer_resources *res)
250d8408326SSeung-Woo Kim {
251d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY8_Y0_LL,
252e25e1b66SSachin Kamat 		filter_y_horiz_tap8, sizeof(filter_y_horiz_tap8));
253d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_Y0_LL,
254e25e1b66SSachin Kamat 		filter_y_vert_tap4, sizeof(filter_y_vert_tap4));
255d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_C0_LL,
256e25e1b66SSachin Kamat 		filter_cr_horiz_tap4, sizeof(filter_cr_horiz_tap4));
257d8408326SSeung-Woo Kim }
258d8408326SSeung-Woo Kim 
259d8408326SSeung-Woo Kim static void mixer_vsync_set_update(struct mixer_context *ctx, bool enable)
260d8408326SSeung-Woo Kim {
261d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
262d8408326SSeung-Woo Kim 
263d8408326SSeung-Woo Kim 	/* block update on vsync */
264d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, enable ?
265d8408326SSeung-Woo Kim 			MXR_STATUS_SYNC_ENABLE : 0, MXR_STATUS_SYNC_ENABLE);
266d8408326SSeung-Woo Kim 
2671b8e5747SRahul Sharma 	if (ctx->vp_enabled)
268d8408326SSeung-Woo Kim 		vp_reg_write(res, VP_SHADOW_UPDATE, enable ?
269d8408326SSeung-Woo Kim 			VP_SHADOW_UPDATE_ENABLE : 0);
270d8408326SSeung-Woo Kim }
271d8408326SSeung-Woo Kim 
272d8408326SSeung-Woo Kim static void mixer_cfg_scan(struct mixer_context *ctx, unsigned int height)
273d8408326SSeung-Woo Kim {
274d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
275d8408326SSeung-Woo Kim 	u32 val;
276d8408326SSeung-Woo Kim 
277d8408326SSeung-Woo Kim 	/* choosing between interlace and progressive mode */
278d8408326SSeung-Woo Kim 	val = (ctx->interlace ? MXR_CFG_SCAN_INTERLACE :
2791e6d459dSTobias Jakobi 				MXR_CFG_SCAN_PROGRESSIVE);
280d8408326SSeung-Woo Kim 
281def5e095SRahul Sharma 	if (ctx->mxr_ver != MXR_VER_128_0_0_184) {
282def5e095SRahul Sharma 		/* choosing between proper HD and SD mode */
28329630743SRahul Sharma 		if (height <= 480)
284d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD;
28529630743SRahul Sharma 		else if (height <= 576)
286d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD;
28729630743SRahul Sharma 		else if (height <= 720)
288d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
28929630743SRahul Sharma 		else if (height <= 1080)
290d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_1080 | MXR_CFG_SCAN_HD;
291d8408326SSeung-Woo Kim 		else
292d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
293def5e095SRahul Sharma 	}
294d8408326SSeung-Woo Kim 
295d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_SCAN_MASK);
296d8408326SSeung-Woo Kim }
297d8408326SSeung-Woo Kim 
298d8408326SSeung-Woo Kim static void mixer_cfg_rgb_fmt(struct mixer_context *ctx, unsigned int height)
299d8408326SSeung-Woo Kim {
300d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
301d8408326SSeung-Woo Kim 	u32 val;
302d8408326SSeung-Woo Kim 
303d8408326SSeung-Woo Kim 	if (height == 480) {
304d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
305d8408326SSeung-Woo Kim 	} else if (height == 576) {
306d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
307d8408326SSeung-Woo Kim 	} else if (height == 720) {
308d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
309d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
310d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
311d8408326SSeung-Woo Kim 				(32 << 0));
312d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
313d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
314d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
315d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
316d8408326SSeung-Woo Kim 	} else if (height == 1080) {
317d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
318d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
319d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
320d8408326SSeung-Woo Kim 				(32 << 0));
321d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
322d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
323d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
324d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
325d8408326SSeung-Woo Kim 	} else {
326d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
327d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
328d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
329d8408326SSeung-Woo Kim 				(32 << 0));
330d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
331d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
332d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
333d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
334d8408326SSeung-Woo Kim 	}
335d8408326SSeung-Woo Kim 
336d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
337d8408326SSeung-Woo Kim }
338d8408326SSeung-Woo Kim 
3395b1d5bc6STobias Jakobi static void mixer_cfg_layer(struct mixer_context *ctx, unsigned int win,
3405b1d5bc6STobias Jakobi 				bool enable)
341d8408326SSeung-Woo Kim {
342d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
343d8408326SSeung-Woo Kim 	u32 val = enable ? ~0 : 0;
344d8408326SSeung-Woo Kim 
345d8408326SSeung-Woo Kim 	switch (win) {
346d8408326SSeung-Woo Kim 	case 0:
347d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
348d8408326SSeung-Woo Kim 		break;
349d8408326SSeung-Woo Kim 	case 1:
350d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
351d8408326SSeung-Woo Kim 		break;
352d8408326SSeung-Woo Kim 	case 2:
3531b8e5747SRahul Sharma 		if (ctx->vp_enabled) {
354d8408326SSeung-Woo Kim 			vp_reg_writemask(res, VP_ENABLE, val, VP_ENABLE_ON);
3551b8e5747SRahul Sharma 			mixer_reg_writemask(res, MXR_CFG, val,
3561b8e5747SRahul Sharma 				MXR_CFG_VP_ENABLE);
357f1e716d8SJoonyoung Shim 
358f1e716d8SJoonyoung Shim 			/* control blending of graphic layer 0 */
359f1e716d8SJoonyoung Shim 			mixer_reg_writemask(res, MXR_GRAPHIC_CFG(0), val,
360f1e716d8SJoonyoung Shim 					MXR_GRP_CFG_BLEND_PRE_MUL |
361f1e716d8SJoonyoung Shim 					MXR_GRP_CFG_PIXEL_BLEND_EN);
3621b8e5747SRahul Sharma 		}
363d8408326SSeung-Woo Kim 		break;
364d8408326SSeung-Woo Kim 	}
365d8408326SSeung-Woo Kim }
366d8408326SSeung-Woo Kim 
367d8408326SSeung-Woo Kim static void mixer_run(struct mixer_context *ctx)
368d8408326SSeung-Woo Kim {
369d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
370d8408326SSeung-Woo Kim 
371d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
372d8408326SSeung-Woo Kim }
373d8408326SSeung-Woo Kim 
374381be025SRahul Sharma static void mixer_stop(struct mixer_context *ctx)
375381be025SRahul Sharma {
376381be025SRahul Sharma 	struct mixer_resources *res = &ctx->mixer_res;
377381be025SRahul Sharma 	int timeout = 20;
378381be025SRahul Sharma 
379381be025SRahul Sharma 	mixer_reg_writemask(res, MXR_STATUS, 0, MXR_STATUS_REG_RUN);
380381be025SRahul Sharma 
381381be025SRahul Sharma 	while (!(mixer_reg_read(res, MXR_STATUS) & MXR_STATUS_REG_IDLE) &&
382381be025SRahul Sharma 			--timeout)
383381be025SRahul Sharma 		usleep_range(10000, 12000);
384381be025SRahul Sharma }
385381be025SRahul Sharma 
386*2eeb2e5eSGustavo Padovan static void vp_video_buffer(struct mixer_context *ctx,
387*2eeb2e5eSGustavo Padovan 			    struct exynos_drm_plane *plane)
388d8408326SSeung-Woo Kim {
389d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
390*2eeb2e5eSGustavo Padovan 	struct drm_plane_state *state = plane->base.state;
391*2eeb2e5eSGustavo Padovan 	struct drm_framebuffer *fb = state->fb;
392*2eeb2e5eSGustavo Padovan 	struct drm_display_mode *mode = &state->crtc->mode;
393d8408326SSeung-Woo Kim 	unsigned long flags;
394d8408326SSeung-Woo Kim 	dma_addr_t luma_addr[2], chroma_addr[2];
395d8408326SSeung-Woo Kim 	bool tiled_mode = false;
396d8408326SSeung-Woo Kim 	bool crcb_mode = false;
397d8408326SSeung-Woo Kim 	u32 val;
398d8408326SSeung-Woo Kim 
399*2eeb2e5eSGustavo Padovan 	switch (fb->pixel_format) {
400363b06aaSVille Syrjälä 	case DRM_FORMAT_NV12:
401d8408326SSeung-Woo Kim 		crcb_mode = false;
402d8408326SSeung-Woo Kim 		break;
4038f2590f8STobias Jakobi 	case DRM_FORMAT_NV21:
4048f2590f8STobias Jakobi 		crcb_mode = true;
4058f2590f8STobias Jakobi 		break;
406d8408326SSeung-Woo Kim 	default:
407d8408326SSeung-Woo Kim 		DRM_ERROR("pixel format for vp is wrong [%d].\n",
408*2eeb2e5eSGustavo Padovan 				fb->pixel_format);
409d8408326SSeung-Woo Kim 		return;
410d8408326SSeung-Woo Kim 	}
411d8408326SSeung-Woo Kim 
4127ee14cdcSGustavo Padovan 	luma_addr[0] = plane->dma_addr[0];
4137ee14cdcSGustavo Padovan 	chroma_addr[0] = plane->dma_addr[1];
414d8408326SSeung-Woo Kim 
415*2eeb2e5eSGustavo Padovan 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
416d8408326SSeung-Woo Kim 		ctx->interlace = true;
417d8408326SSeung-Woo Kim 		if (tiled_mode) {
418d8408326SSeung-Woo Kim 			luma_addr[1] = luma_addr[0] + 0x40;
419d8408326SSeung-Woo Kim 			chroma_addr[1] = chroma_addr[0] + 0x40;
420d8408326SSeung-Woo Kim 		} else {
421*2eeb2e5eSGustavo Padovan 			luma_addr[1] = luma_addr[0] + fb->pitches[0];
422*2eeb2e5eSGustavo Padovan 			chroma_addr[1] = chroma_addr[0] + fb->pitches[0];
423d8408326SSeung-Woo Kim 		}
424d8408326SSeung-Woo Kim 	} else {
425d8408326SSeung-Woo Kim 		ctx->interlace = false;
426d8408326SSeung-Woo Kim 		luma_addr[1] = 0;
427d8408326SSeung-Woo Kim 		chroma_addr[1] = 0;
428d8408326SSeung-Woo Kim 	}
429d8408326SSeung-Woo Kim 
430d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
431d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, false);
432d8408326SSeung-Woo Kim 
433d8408326SSeung-Woo Kim 	/* interlace or progressive scan mode */
434d8408326SSeung-Woo Kim 	val = (ctx->interlace ? ~0 : 0);
435d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_LINE_SKIP);
436d8408326SSeung-Woo Kim 
437d8408326SSeung-Woo Kim 	/* setup format */
438d8408326SSeung-Woo Kim 	val = (crcb_mode ? VP_MODE_NV21 : VP_MODE_NV12);
439d8408326SSeung-Woo Kim 	val |= (tiled_mode ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
440d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_FMT_MASK);
441d8408326SSeung-Woo Kim 
442d8408326SSeung-Woo Kim 	/* setting size of input image */
443*2eeb2e5eSGustavo Padovan 	vp_reg_write(res, VP_IMG_SIZE_Y, VP_IMG_HSIZE(fb->pitches[0]) |
444*2eeb2e5eSGustavo Padovan 		VP_IMG_VSIZE(fb->height));
445d8408326SSeung-Woo Kim 	/* chroma height has to reduced by 2 to avoid chroma distorions */
446*2eeb2e5eSGustavo Padovan 	vp_reg_write(res, VP_IMG_SIZE_C, VP_IMG_HSIZE(fb->pitches[0]) |
447*2eeb2e5eSGustavo Padovan 		VP_IMG_VSIZE(fb->height / 2));
448d8408326SSeung-Woo Kim 
4497ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_SRC_WIDTH, plane->src_width);
4507ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_SRC_HEIGHT, plane->src_height);
451d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_H_POSITION,
452cb8a3db2SJoonyoung Shim 			VP_SRC_H_POSITION_VAL(plane->src_x));
453cb8a3db2SJoonyoung Shim 	vp_reg_write(res, VP_SRC_V_POSITION, plane->src_y);
454d8408326SSeung-Woo Kim 
4557ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_DST_WIDTH, plane->crtc_width);
4567ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_DST_H_POSITION, plane->crtc_x);
457d8408326SSeung-Woo Kim 	if (ctx->interlace) {
4587ee14cdcSGustavo Padovan 		vp_reg_write(res, VP_DST_HEIGHT, plane->crtc_height / 2);
4597ee14cdcSGustavo Padovan 		vp_reg_write(res, VP_DST_V_POSITION, plane->crtc_y / 2);
460d8408326SSeung-Woo Kim 	} else {
4617ee14cdcSGustavo Padovan 		vp_reg_write(res, VP_DST_HEIGHT, plane->crtc_height);
4627ee14cdcSGustavo Padovan 		vp_reg_write(res, VP_DST_V_POSITION, plane->crtc_y);
463d8408326SSeung-Woo Kim 	}
464d8408326SSeung-Woo Kim 
4653cabaf7eSJoonyoung Shim 	vp_reg_write(res, VP_H_RATIO, plane->h_ratio);
4663cabaf7eSJoonyoung Shim 	vp_reg_write(res, VP_V_RATIO, plane->v_ratio);
467d8408326SSeung-Woo Kim 
468d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
469d8408326SSeung-Woo Kim 
470d8408326SSeung-Woo Kim 	/* set buffer address to vp */
471d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_Y_PTR, luma_addr[0]);
472d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_Y_PTR, luma_addr[1]);
473d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_C_PTR, chroma_addr[0]);
474d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_C_PTR, chroma_addr[1]);
475d8408326SSeung-Woo Kim 
476*2eeb2e5eSGustavo Padovan 	mixer_cfg_scan(ctx, mode->vdisplay);
477*2eeb2e5eSGustavo Padovan 	mixer_cfg_rgb_fmt(ctx, mode->vdisplay);
478*2eeb2e5eSGustavo Padovan 	mixer_cfg_layer(ctx, plane->zpos, true);
479d8408326SSeung-Woo Kim 	mixer_run(ctx);
480d8408326SSeung-Woo Kim 
481d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, true);
482d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
483d8408326SSeung-Woo Kim 
484c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
485d8408326SSeung-Woo Kim 	vp_regs_dump(ctx);
486d8408326SSeung-Woo Kim }
487d8408326SSeung-Woo Kim 
488aaf8b49eSRahul Sharma static void mixer_layer_update(struct mixer_context *ctx)
489aaf8b49eSRahul Sharma {
490aaf8b49eSRahul Sharma 	struct mixer_resources *res = &ctx->mixer_res;
491aaf8b49eSRahul Sharma 
492aaf8b49eSRahul Sharma 	mixer_reg_writemask(res, MXR_CFG, ~0, MXR_CFG_LAYER_UPDATE);
493aaf8b49eSRahul Sharma }
494aaf8b49eSRahul Sharma 
4952611015cSTobias Jakobi static int mixer_setup_scale(const struct exynos_drm_plane *plane,
4962611015cSTobias Jakobi 		unsigned int *x_ratio, unsigned int *y_ratio)
4972611015cSTobias Jakobi {
4982611015cSTobias Jakobi 	if (plane->crtc_width != plane->src_width) {
4992611015cSTobias Jakobi 		if (plane->crtc_width == 2 * plane->src_width)
5002611015cSTobias Jakobi 			*x_ratio = 1;
5012611015cSTobias Jakobi 		else
5022611015cSTobias Jakobi 			goto fail;
5032611015cSTobias Jakobi 	}
5042611015cSTobias Jakobi 
5052611015cSTobias Jakobi 	if (plane->crtc_height != plane->src_height) {
5062611015cSTobias Jakobi 		if (plane->crtc_height == 2 * plane->src_height)
5072611015cSTobias Jakobi 			*y_ratio = 1;
5082611015cSTobias Jakobi 		else
5092611015cSTobias Jakobi 			goto fail;
5102611015cSTobias Jakobi 	}
5112611015cSTobias Jakobi 
5122611015cSTobias Jakobi 	return 0;
5132611015cSTobias Jakobi 
5142611015cSTobias Jakobi fail:
5152611015cSTobias Jakobi 	DRM_DEBUG_KMS("only 2x width/height scaling of plane supported\n");
5162611015cSTobias Jakobi 	return -ENOTSUPP;
5172611015cSTobias Jakobi }
5182611015cSTobias Jakobi 
519*2eeb2e5eSGustavo Padovan static void mixer_graph_buffer(struct mixer_context *ctx,
520*2eeb2e5eSGustavo Padovan 			       struct exynos_drm_plane *plane)
521d8408326SSeung-Woo Kim {
522d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
523*2eeb2e5eSGustavo Padovan 	struct drm_plane_state *state = plane->base.state;
524*2eeb2e5eSGustavo Padovan 	struct drm_framebuffer *fb = state->fb;
525*2eeb2e5eSGustavo Padovan 	struct drm_display_mode *mode = &state->crtc->mode;
526d8408326SSeung-Woo Kim 	unsigned long flags;
527*2eeb2e5eSGustavo Padovan 	unsigned int win = plane->zpos;
5282611015cSTobias Jakobi 	unsigned int x_ratio = 0, y_ratio = 0;
529d8408326SSeung-Woo Kim 	unsigned int src_x_offset, src_y_offset, dst_x_offset, dst_y_offset;
530d8408326SSeung-Woo Kim 	dma_addr_t dma_addr;
531d8408326SSeung-Woo Kim 	unsigned int fmt;
532d8408326SSeung-Woo Kim 	u32 val;
533d8408326SSeung-Woo Kim 
534*2eeb2e5eSGustavo Padovan 	switch (fb->pixel_format) {
5357a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB4444:
5367a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB4444;
5377a57ca7cSTobias Jakobi 		break;
538d8408326SSeung-Woo Kim 
5397a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB1555:
5407a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB1555;
541d8408326SSeung-Woo Kim 		break;
5427a57ca7cSTobias Jakobi 
5437a57ca7cSTobias Jakobi 	case DRM_FORMAT_RGB565:
5447a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_RGB565;
545d8408326SSeung-Woo Kim 		break;
5467a57ca7cSTobias Jakobi 
5477a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB8888:
5487a57ca7cSTobias Jakobi 	case DRM_FORMAT_ARGB8888:
5497a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB8888;
5507a57ca7cSTobias Jakobi 		break;
5517a57ca7cSTobias Jakobi 
552d8408326SSeung-Woo Kim 	default:
5537a57ca7cSTobias Jakobi 		DRM_DEBUG_KMS("pixelformat unsupported by mixer\n");
5547a57ca7cSTobias Jakobi 		return;
555d8408326SSeung-Woo Kim 	}
556d8408326SSeung-Woo Kim 
5572611015cSTobias Jakobi 	/* check if mixer supports requested scaling setup */
5582611015cSTobias Jakobi 	if (mixer_setup_scale(plane, &x_ratio, &y_ratio))
5592611015cSTobias Jakobi 		return;
560d8408326SSeung-Woo Kim 
5617ee14cdcSGustavo Padovan 	dst_x_offset = plane->crtc_x;
5627ee14cdcSGustavo Padovan 	dst_y_offset = plane->crtc_y;
563d8408326SSeung-Woo Kim 
564d8408326SSeung-Woo Kim 	/* converting dma address base and source offset */
5657ee14cdcSGustavo Padovan 	dma_addr = plane->dma_addr[0]
566*2eeb2e5eSGustavo Padovan 		+ (plane->src_x * fb->bits_per_pixel >> 3)
567*2eeb2e5eSGustavo Padovan 		+ (plane->src_y * fb->pitches[0]);
568d8408326SSeung-Woo Kim 	src_x_offset = 0;
569d8408326SSeung-Woo Kim 	src_y_offset = 0;
570d8408326SSeung-Woo Kim 
571*2eeb2e5eSGustavo Padovan 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
572d8408326SSeung-Woo Kim 		ctx->interlace = true;
573d8408326SSeung-Woo Kim 	else
574d8408326SSeung-Woo Kim 		ctx->interlace = false;
575d8408326SSeung-Woo Kim 
576d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
577d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, false);
578d8408326SSeung-Woo Kim 
579d8408326SSeung-Woo Kim 	/* setup format */
580d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_GRAPHIC_CFG(win),
581d8408326SSeung-Woo Kim 		MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
582d8408326SSeung-Woo Kim 
583d8408326SSeung-Woo Kim 	/* setup geometry */
584adacb228SDaniel Stone 	mixer_reg_write(res, MXR_GRAPHIC_SPAN(win),
585*2eeb2e5eSGustavo Padovan 			fb->pitches[0] / (fb->bits_per_pixel >> 3));
586d8408326SSeung-Woo Kim 
587def5e095SRahul Sharma 	/* setup display size */
588def5e095SRahul Sharma 	if (ctx->mxr_ver == MXR_VER_128_0_0_184 &&
589def5e095SRahul Sharma 		win == MIXER_DEFAULT_WIN) {
590*2eeb2e5eSGustavo Padovan 		val  = MXR_MXR_RES_HEIGHT(mode->vdisplay);
591*2eeb2e5eSGustavo Padovan 		val |= MXR_MXR_RES_WIDTH(mode->hdisplay);
592def5e095SRahul Sharma 		mixer_reg_write(res, MXR_RESOLUTION, val);
593def5e095SRahul Sharma 	}
594def5e095SRahul Sharma 
5952611015cSTobias Jakobi 	val  = MXR_GRP_WH_WIDTH(plane->src_width);
5962611015cSTobias Jakobi 	val |= MXR_GRP_WH_HEIGHT(plane->src_height);
597d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_H_SCALE(x_ratio);
598d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_V_SCALE(y_ratio);
599d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_WH(win), val);
600d8408326SSeung-Woo Kim 
601d8408326SSeung-Woo Kim 	/* setup offsets in source image */
602d8408326SSeung-Woo Kim 	val  = MXR_GRP_SXY_SX(src_x_offset);
603d8408326SSeung-Woo Kim 	val |= MXR_GRP_SXY_SY(src_y_offset);
604d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_SXY(win), val);
605d8408326SSeung-Woo Kim 
606d8408326SSeung-Woo Kim 	/* setup offsets in display image */
607d8408326SSeung-Woo Kim 	val  = MXR_GRP_DXY_DX(dst_x_offset);
608d8408326SSeung-Woo Kim 	val |= MXR_GRP_DXY_DY(dst_y_offset);
609d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_DXY(win), val);
610d8408326SSeung-Woo Kim 
611d8408326SSeung-Woo Kim 	/* set buffer address to mixer */
612d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_BASE(win), dma_addr);
613d8408326SSeung-Woo Kim 
614*2eeb2e5eSGustavo Padovan 	mixer_cfg_scan(ctx, mode->vdisplay);
615*2eeb2e5eSGustavo Padovan 	mixer_cfg_rgb_fmt(ctx, mode->vdisplay);
616d8408326SSeung-Woo Kim 	mixer_cfg_layer(ctx, win, true);
617aaf8b49eSRahul Sharma 
618aaf8b49eSRahul Sharma 	/* layer update mandatory for mixer 16.0.33.0 */
619def5e095SRahul Sharma 	if (ctx->mxr_ver == MXR_VER_16_0_33_0 ||
620def5e095SRahul Sharma 		ctx->mxr_ver == MXR_VER_128_0_0_184)
621aaf8b49eSRahul Sharma 		mixer_layer_update(ctx);
622aaf8b49eSRahul Sharma 
623d8408326SSeung-Woo Kim 	mixer_run(ctx);
624d8408326SSeung-Woo Kim 
625d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, true);
626d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
627c0734fbaSTobias Jakobi 
628c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
629d8408326SSeung-Woo Kim }
630d8408326SSeung-Woo Kim 
631d8408326SSeung-Woo Kim static void vp_win_reset(struct mixer_context *ctx)
632d8408326SSeung-Woo Kim {
633d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
634d8408326SSeung-Woo Kim 	int tries = 100;
635d8408326SSeung-Woo Kim 
636d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRESET, VP_SRESET_PROCESSING);
637d8408326SSeung-Woo Kim 	for (tries = 100; tries; --tries) {
638d8408326SSeung-Woo Kim 		/* waiting until VP_SRESET_PROCESSING is 0 */
639d8408326SSeung-Woo Kim 		if (~vp_reg_read(res, VP_SRESET) & VP_SRESET_PROCESSING)
640d8408326SSeung-Woo Kim 			break;
64109760ea3SSean Paul 		usleep_range(10000, 12000);
642d8408326SSeung-Woo Kim 	}
643d8408326SSeung-Woo Kim 	WARN(tries == 0, "failed to reset Video Processor\n");
644d8408326SSeung-Woo Kim }
645d8408326SSeung-Woo Kim 
646cf8fc4f1SJoonyoung Shim static void mixer_win_reset(struct mixer_context *ctx)
647cf8fc4f1SJoonyoung Shim {
648cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
649cf8fc4f1SJoonyoung Shim 	unsigned long flags;
650cf8fc4f1SJoonyoung Shim 	u32 val; /* value stored to register */
651cf8fc4f1SJoonyoung Shim 
652cf8fc4f1SJoonyoung Shim 	spin_lock_irqsave(&res->reg_slock, flags);
653cf8fc4f1SJoonyoung Shim 	mixer_vsync_set_update(ctx, false);
654cf8fc4f1SJoonyoung Shim 
655cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
656cf8fc4f1SJoonyoung Shim 
657cf8fc4f1SJoonyoung Shim 	/* set output in RGB888 mode */
658cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
659cf8fc4f1SJoonyoung Shim 
660cf8fc4f1SJoonyoung Shim 	/* 16 beat burst in DMA */
661cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_STATUS, MXR_STATUS_16_BURST,
662cf8fc4f1SJoonyoung Shim 		MXR_STATUS_BURST_MASK);
663cf8fc4f1SJoonyoung Shim 
664cf8fc4f1SJoonyoung Shim 	/* setting default layer priority: layer1 > layer0 > video
665cf8fc4f1SJoonyoung Shim 	 * because typical usage scenario would be
666cf8fc4f1SJoonyoung Shim 	 * layer1 - OSD
667cf8fc4f1SJoonyoung Shim 	 * layer0 - framebuffer
668cf8fc4f1SJoonyoung Shim 	 * video - video overlay
669cf8fc4f1SJoonyoung Shim 	 */
670cf8fc4f1SJoonyoung Shim 	val = MXR_LAYER_CFG_GRP1_VAL(3);
671cf8fc4f1SJoonyoung Shim 	val |= MXR_LAYER_CFG_GRP0_VAL(2);
6721b8e5747SRahul Sharma 	if (ctx->vp_enabled)
673cf8fc4f1SJoonyoung Shim 		val |= MXR_LAYER_CFG_VP_VAL(1);
674cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_LAYER_CFG, val);
675cf8fc4f1SJoonyoung Shim 
676cf8fc4f1SJoonyoung Shim 	/* setting background color */
677cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR0, 0x008080);
678cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR1, 0x008080);
679cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR2, 0x008080);
680cf8fc4f1SJoonyoung Shim 
681cf8fc4f1SJoonyoung Shim 	/* setting graphical layers */
682cf8fc4f1SJoonyoung Shim 	val  = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
683cf8fc4f1SJoonyoung Shim 	val |= MXR_GRP_CFG_WIN_BLEND_EN;
684cf8fc4f1SJoonyoung Shim 	val |= MXR_GRP_CFG_ALPHA_VAL(0xff); /* non-transparent alpha */
685cf8fc4f1SJoonyoung Shim 
6860377f4edSSean Paul 	/* Don't blend layer 0 onto the mixer background */
687cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_GRAPHIC_CFG(0), val);
6880377f4edSSean Paul 
6890377f4edSSean Paul 	/* Blend layer 1 into layer 0 */
6900377f4edSSean Paul 	val |= MXR_GRP_CFG_BLEND_PRE_MUL;
6910377f4edSSean Paul 	val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
692cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_GRAPHIC_CFG(1), val);
693cf8fc4f1SJoonyoung Shim 
6945736603bSSeung-Woo Kim 	/* setting video layers */
6955736603bSSeung-Woo Kim 	val = MXR_GRP_CFG_ALPHA_VAL(0);
6965736603bSSeung-Woo Kim 	mixer_reg_write(res, MXR_VIDEO_CFG, val);
6975736603bSSeung-Woo Kim 
6981b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
699cf8fc4f1SJoonyoung Shim 		/* configuration of Video Processor Registers */
700cf8fc4f1SJoonyoung Shim 		vp_win_reset(ctx);
701cf8fc4f1SJoonyoung Shim 		vp_default_filter(res);
7021b8e5747SRahul Sharma 	}
703cf8fc4f1SJoonyoung Shim 
704cf8fc4f1SJoonyoung Shim 	/* disable all layers */
705cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
706cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
7071b8e5747SRahul Sharma 	if (ctx->vp_enabled)
708cf8fc4f1SJoonyoung Shim 		mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
709cf8fc4f1SJoonyoung Shim 
710cf8fc4f1SJoonyoung Shim 	mixer_vsync_set_update(ctx, true);
711cf8fc4f1SJoonyoung Shim 	spin_unlock_irqrestore(&res->reg_slock, flags);
712cf8fc4f1SJoonyoung Shim }
713cf8fc4f1SJoonyoung Shim 
7144551789fSSean Paul static irqreturn_t mixer_irq_handler(int irq, void *arg)
7154551789fSSean Paul {
7164551789fSSean Paul 	struct mixer_context *ctx = arg;
7174551789fSSean Paul 	struct mixer_resources *res = &ctx->mixer_res;
7184551789fSSean Paul 	u32 val, base, shadow;
7194551789fSSean Paul 
7204551789fSSean Paul 	spin_lock(&res->reg_slock);
7214551789fSSean Paul 
7224551789fSSean Paul 	/* read interrupt status for handling and clearing flags for VSYNC */
7234551789fSSean Paul 	val = mixer_reg_read(res, MXR_INT_STATUS);
7244551789fSSean Paul 
7254551789fSSean Paul 	/* handling VSYNC */
7264551789fSSean Paul 	if (val & MXR_INT_STATUS_VSYNC) {
72781a464dfSAndrzej Hajda 		/* vsync interrupt use different bit for read and clear */
72881a464dfSAndrzej Hajda 		val |= MXR_INT_CLEAR_VSYNC;
72981a464dfSAndrzej Hajda 		val &= ~MXR_INT_STATUS_VSYNC;
73081a464dfSAndrzej Hajda 
7314551789fSSean Paul 		/* interlace scan need to check shadow register */
7324551789fSSean Paul 		if (ctx->interlace) {
7334551789fSSean Paul 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
7344551789fSSean Paul 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
7354551789fSSean Paul 			if (base != shadow)
7364551789fSSean Paul 				goto out;
7374551789fSSean Paul 
7384551789fSSean Paul 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
7394551789fSSean Paul 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
7404551789fSSean Paul 			if (base != shadow)
7414551789fSSean Paul 				goto out;
7424551789fSSean Paul 		}
7434551789fSSean Paul 
744eafd540aSGustavo Padovan 		drm_crtc_handle_vblank(&ctx->crtc->base);
745eafd540aSGustavo Padovan 		exynos_drm_crtc_finish_pageflip(ctx->crtc);
7464551789fSSean Paul 
7474551789fSSean Paul 		/* set wait vsync event to zero and wake up queue. */
7484551789fSSean Paul 		if (atomic_read(&ctx->wait_vsync_event)) {
7494551789fSSean Paul 			atomic_set(&ctx->wait_vsync_event, 0);
7504551789fSSean Paul 			wake_up(&ctx->wait_vsync_queue);
7514551789fSSean Paul 		}
7524551789fSSean Paul 	}
7534551789fSSean Paul 
7544551789fSSean Paul out:
7554551789fSSean Paul 	/* clear interrupts */
7564551789fSSean Paul 	mixer_reg_write(res, MXR_INT_STATUS, val);
7574551789fSSean Paul 
7584551789fSSean Paul 	spin_unlock(&res->reg_slock);
7594551789fSSean Paul 
7604551789fSSean Paul 	return IRQ_HANDLED;
7614551789fSSean Paul }
7624551789fSSean Paul 
7634551789fSSean Paul static int mixer_resources_init(struct mixer_context *mixer_ctx)
7644551789fSSean Paul {
7654551789fSSean Paul 	struct device *dev = &mixer_ctx->pdev->dev;
7664551789fSSean Paul 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
7674551789fSSean Paul 	struct resource *res;
7684551789fSSean Paul 	int ret;
7694551789fSSean Paul 
7704551789fSSean Paul 	spin_lock_init(&mixer_res->reg_slock);
7714551789fSSean Paul 
7724551789fSSean Paul 	mixer_res->mixer = devm_clk_get(dev, "mixer");
7734551789fSSean Paul 	if (IS_ERR(mixer_res->mixer)) {
7744551789fSSean Paul 		dev_err(dev, "failed to get clock 'mixer'\n");
7754551789fSSean Paul 		return -ENODEV;
7764551789fSSean Paul 	}
7774551789fSSean Paul 
77804427ec5SMarek Szyprowski 	mixer_res->hdmi = devm_clk_get(dev, "hdmi");
77904427ec5SMarek Szyprowski 	if (IS_ERR(mixer_res->hdmi)) {
78004427ec5SMarek Szyprowski 		dev_err(dev, "failed to get clock 'hdmi'\n");
78104427ec5SMarek Szyprowski 		return PTR_ERR(mixer_res->hdmi);
78204427ec5SMarek Szyprowski 	}
78304427ec5SMarek Szyprowski 
7844551789fSSean Paul 	mixer_res->sclk_hdmi = devm_clk_get(dev, "sclk_hdmi");
7854551789fSSean Paul 	if (IS_ERR(mixer_res->sclk_hdmi)) {
7864551789fSSean Paul 		dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
7874551789fSSean Paul 		return -ENODEV;
7884551789fSSean Paul 	}
7894551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 0);
7904551789fSSean Paul 	if (res == NULL) {
7914551789fSSean Paul 		dev_err(dev, "get memory resource failed.\n");
7924551789fSSean Paul 		return -ENXIO;
7934551789fSSean Paul 	}
7944551789fSSean Paul 
7954551789fSSean Paul 	mixer_res->mixer_regs = devm_ioremap(dev, res->start,
7964551789fSSean Paul 							resource_size(res));
7974551789fSSean Paul 	if (mixer_res->mixer_regs == NULL) {
7984551789fSSean Paul 		dev_err(dev, "register mapping failed.\n");
7994551789fSSean Paul 		return -ENXIO;
8004551789fSSean Paul 	}
8014551789fSSean Paul 
8024551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_IRQ, 0);
8034551789fSSean Paul 	if (res == NULL) {
8044551789fSSean Paul 		dev_err(dev, "get interrupt resource failed.\n");
8054551789fSSean Paul 		return -ENXIO;
8064551789fSSean Paul 	}
8074551789fSSean Paul 
8084551789fSSean Paul 	ret = devm_request_irq(dev, res->start, mixer_irq_handler,
8094551789fSSean Paul 						0, "drm_mixer", mixer_ctx);
8104551789fSSean Paul 	if (ret) {
8114551789fSSean Paul 		dev_err(dev, "request interrupt failed.\n");
8124551789fSSean Paul 		return ret;
8134551789fSSean Paul 	}
8144551789fSSean Paul 	mixer_res->irq = res->start;
8154551789fSSean Paul 
8164551789fSSean Paul 	return 0;
8174551789fSSean Paul }
8184551789fSSean Paul 
8194551789fSSean Paul static int vp_resources_init(struct mixer_context *mixer_ctx)
8204551789fSSean Paul {
8214551789fSSean Paul 	struct device *dev = &mixer_ctx->pdev->dev;
8224551789fSSean Paul 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
8234551789fSSean Paul 	struct resource *res;
8244551789fSSean Paul 
8254551789fSSean Paul 	mixer_res->vp = devm_clk_get(dev, "vp");
8264551789fSSean Paul 	if (IS_ERR(mixer_res->vp)) {
8274551789fSSean Paul 		dev_err(dev, "failed to get clock 'vp'\n");
8284551789fSSean Paul 		return -ENODEV;
8294551789fSSean Paul 	}
830ff830c96SMarek Szyprowski 
831ff830c96SMarek Szyprowski 	if (mixer_ctx->has_sclk) {
8324551789fSSean Paul 		mixer_res->sclk_mixer = devm_clk_get(dev, "sclk_mixer");
8334551789fSSean Paul 		if (IS_ERR(mixer_res->sclk_mixer)) {
8344551789fSSean Paul 			dev_err(dev, "failed to get clock 'sclk_mixer'\n");
8354551789fSSean Paul 			return -ENODEV;
8364551789fSSean Paul 		}
837ff830c96SMarek Szyprowski 		mixer_res->mout_mixer = devm_clk_get(dev, "mout_mixer");
838ff830c96SMarek Szyprowski 		if (IS_ERR(mixer_res->mout_mixer)) {
839ff830c96SMarek Szyprowski 			dev_err(dev, "failed to get clock 'mout_mixer'\n");
8404551789fSSean Paul 			return -ENODEV;
8414551789fSSean Paul 		}
8424551789fSSean Paul 
843ff830c96SMarek Szyprowski 		if (mixer_res->sclk_hdmi && mixer_res->mout_mixer)
844ff830c96SMarek Szyprowski 			clk_set_parent(mixer_res->mout_mixer,
845ff830c96SMarek Szyprowski 				       mixer_res->sclk_hdmi);
846ff830c96SMarek Szyprowski 	}
8474551789fSSean Paul 
8484551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 1);
8494551789fSSean Paul 	if (res == NULL) {
8504551789fSSean Paul 		dev_err(dev, "get memory resource failed.\n");
8514551789fSSean Paul 		return -ENXIO;
8524551789fSSean Paul 	}
8534551789fSSean Paul 
8544551789fSSean Paul 	mixer_res->vp_regs = devm_ioremap(dev, res->start,
8554551789fSSean Paul 							resource_size(res));
8564551789fSSean Paul 	if (mixer_res->vp_regs == NULL) {
8574551789fSSean Paul 		dev_err(dev, "register mapping failed.\n");
8584551789fSSean Paul 		return -ENXIO;
8594551789fSSean Paul 	}
8604551789fSSean Paul 
8614551789fSSean Paul 	return 0;
8624551789fSSean Paul }
8634551789fSSean Paul 
86493bca243SGustavo Padovan static int mixer_initialize(struct mixer_context *mixer_ctx,
865f37cd5e8SInki Dae 			struct drm_device *drm_dev)
8664551789fSSean Paul {
8674551789fSSean Paul 	int ret;
868f37cd5e8SInki Dae 	struct exynos_drm_private *priv;
869f37cd5e8SInki Dae 	priv = drm_dev->dev_private;
8704551789fSSean Paul 
871eb88e422SGustavo Padovan 	mixer_ctx->drm_dev = drm_dev;
8728a326eddSGustavo Padovan 	mixer_ctx->pipe = priv->pipe++;
8734551789fSSean Paul 
8744551789fSSean Paul 	/* acquire resources: regs, irqs, clocks */
8754551789fSSean Paul 	ret = mixer_resources_init(mixer_ctx);
8764551789fSSean Paul 	if (ret) {
8774551789fSSean Paul 		DRM_ERROR("mixer_resources_init failed ret=%d\n", ret);
8784551789fSSean Paul 		return ret;
8794551789fSSean Paul 	}
8804551789fSSean Paul 
8814551789fSSean Paul 	if (mixer_ctx->vp_enabled) {
8824551789fSSean Paul 		/* acquire vp resources: regs, irqs, clocks */
8834551789fSSean Paul 		ret = vp_resources_init(mixer_ctx);
8844551789fSSean Paul 		if (ret) {
8854551789fSSean Paul 			DRM_ERROR("vp_resources_init failed ret=%d\n", ret);
8864551789fSSean Paul 			return ret;
8874551789fSSean Paul 		}
8884551789fSSean Paul 	}
8894551789fSSean Paul 
890eb7a3fc7SJoonyoung Shim 	ret = drm_iommu_attach_device(drm_dev, mixer_ctx->dev);
891fc2e013fSHyungwon Hwang 	if (ret)
892fc2e013fSHyungwon Hwang 		priv->pipe--;
893f041b257SSean Paul 
894fc2e013fSHyungwon Hwang 	return ret;
8951055b39fSInki Dae }
8961055b39fSInki Dae 
89793bca243SGustavo Padovan static void mixer_ctx_remove(struct mixer_context *mixer_ctx)
898d8408326SSeung-Woo Kim {
899f041b257SSean Paul 	drm_iommu_detach_device(mixer_ctx->drm_dev, mixer_ctx->dev);
900f041b257SSean Paul }
901f041b257SSean Paul 
90293bca243SGustavo Padovan static int mixer_enable_vblank(struct exynos_drm_crtc *crtc)
903f041b257SSean Paul {
90493bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
905d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
906d8408326SSeung-Woo Kim 
9070df5e4acSAndrzej Hajda 	__set_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
9080df5e4acSAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
909f041b257SSean Paul 		return 0;
910d8408326SSeung-Woo Kim 
911d8408326SSeung-Woo Kim 	/* enable vsync interrupt */
912fc073248SAndrzej Hajda 	mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
913fc073248SAndrzej Hajda 	mixer_reg_writemask(res, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
914d8408326SSeung-Woo Kim 
915d8408326SSeung-Woo Kim 	return 0;
916d8408326SSeung-Woo Kim }
917d8408326SSeung-Woo Kim 
91893bca243SGustavo Padovan static void mixer_disable_vblank(struct exynos_drm_crtc *crtc)
919d8408326SSeung-Woo Kim {
92093bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
921d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
922d8408326SSeung-Woo Kim 
9230df5e4acSAndrzej Hajda 	__clear_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
9240df5e4acSAndrzej Hajda 
9250df5e4acSAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
926947710c6SAndrzej Hajda 		return;
927947710c6SAndrzej Hajda 
928d8408326SSeung-Woo Kim 	/* disable vsync interrupt */
929fc073248SAndrzej Hajda 	mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
930d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
931d8408326SSeung-Woo Kim }
932d8408326SSeung-Woo Kim 
9331e1d1393SGustavo Padovan static void mixer_update_plane(struct exynos_drm_crtc *crtc,
9341e1d1393SGustavo Padovan 			       struct exynos_drm_plane *plane)
935d8408326SSeung-Woo Kim {
93693bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
937d8408326SSeung-Woo Kim 
9381e1d1393SGustavo Padovan 	DRM_DEBUG_KMS("win: %d\n", plane->zpos);
939d8408326SSeung-Woo Kim 
940a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
941dda9012bSShirish S 		return;
942dda9012bSShirish S 
9431e1d1393SGustavo Padovan 	if (plane->zpos > 1 && mixer_ctx->vp_enabled)
944*2eeb2e5eSGustavo Padovan 		vp_video_buffer(mixer_ctx, plane);
945d8408326SSeung-Woo Kim 	else
946*2eeb2e5eSGustavo Padovan 		mixer_graph_buffer(mixer_ctx, plane);
947d8408326SSeung-Woo Kim }
948d8408326SSeung-Woo Kim 
9491e1d1393SGustavo Padovan static void mixer_disable_plane(struct exynos_drm_crtc *crtc,
9501e1d1393SGustavo Padovan 				struct exynos_drm_plane *plane)
951d8408326SSeung-Woo Kim {
95293bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
953d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
954d8408326SSeung-Woo Kim 	unsigned long flags;
955d8408326SSeung-Woo Kim 
9561e1d1393SGustavo Padovan 	DRM_DEBUG_KMS("win: %d\n", plane->zpos);
957d8408326SSeung-Woo Kim 
958a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
959db43fd16SPrathyush K 		return;
960db43fd16SPrathyush K 
961d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
962d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, false);
963d8408326SSeung-Woo Kim 
9641e1d1393SGustavo Padovan 	mixer_cfg_layer(mixer_ctx, plane->zpos, false);
965d8408326SSeung-Woo Kim 
966d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, true);
967d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
968d8408326SSeung-Woo Kim }
969d8408326SSeung-Woo Kim 
97093bca243SGustavo Padovan static void mixer_wait_for_vblank(struct exynos_drm_crtc *crtc)
9710ea6822fSRahul Sharma {
97293bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
9737c4c5584SJoonyoung Shim 	int err;
9748137a2e2SPrathyush K 
975a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
9766e95d5e6SPrathyush K 		return;
9776e95d5e6SPrathyush K 
97893bca243SGustavo Padovan 	err = drm_vblank_get(mixer_ctx->drm_dev, mixer_ctx->pipe);
9797c4c5584SJoonyoung Shim 	if (err < 0) {
9807c4c5584SJoonyoung Shim 		DRM_DEBUG_KMS("failed to acquire vblank counter\n");
9817c4c5584SJoonyoung Shim 		return;
9827c4c5584SJoonyoung Shim 	}
9835d39b9eeSRahul Sharma 
9846e95d5e6SPrathyush K 	atomic_set(&mixer_ctx->wait_vsync_event, 1);
9856e95d5e6SPrathyush K 
9866e95d5e6SPrathyush K 	/*
9876e95d5e6SPrathyush K 	 * wait for MIXER to signal VSYNC interrupt or return after
9886e95d5e6SPrathyush K 	 * timeout which is set to 50ms (refresh rate of 20).
9896e95d5e6SPrathyush K 	 */
9906e95d5e6SPrathyush K 	if (!wait_event_timeout(mixer_ctx->wait_vsync_queue,
9916e95d5e6SPrathyush K 				!atomic_read(&mixer_ctx->wait_vsync_event),
992bfd8303aSDaniel Vetter 				HZ/20))
9938137a2e2SPrathyush K 		DRM_DEBUG_KMS("vblank wait timed out.\n");
9945d39b9eeSRahul Sharma 
99593bca243SGustavo Padovan 	drm_vblank_put(mixer_ctx->drm_dev, mixer_ctx->pipe);
9968137a2e2SPrathyush K }
9978137a2e2SPrathyush K 
9983cecda03SGustavo Padovan static void mixer_enable(struct exynos_drm_crtc *crtc)
999db43fd16SPrathyush K {
10003cecda03SGustavo Padovan 	struct mixer_context *ctx = crtc->ctx;
1001db43fd16SPrathyush K 	struct mixer_resources *res = &ctx->mixer_res;
100238000dbbSGustavo Padovan 	int ret;
1003db43fd16SPrathyush K 
1004a44652e8SAndrzej Hajda 	if (test_bit(MXR_BIT_POWERED, &ctx->flags))
1005db43fd16SPrathyush K 		return;
1006db43fd16SPrathyush K 
1007af65c804SSean Paul 	pm_runtime_get_sync(ctx->dev);
1008af65c804SSean Paul 
100938000dbbSGustavo Padovan 	ret = clk_prepare_enable(res->mixer);
101038000dbbSGustavo Padovan 	if (ret < 0) {
101138000dbbSGustavo Padovan 		DRM_ERROR("Failed to prepare_enable the mixer clk [%d]\n", ret);
101238000dbbSGustavo Padovan 		return;
101338000dbbSGustavo Padovan 	}
101438000dbbSGustavo Padovan 	ret = clk_prepare_enable(res->hdmi);
101538000dbbSGustavo Padovan 	if (ret < 0) {
101638000dbbSGustavo Padovan 		DRM_ERROR("Failed to prepare_enable the hdmi clk [%d]\n", ret);
101738000dbbSGustavo Padovan 		return;
101838000dbbSGustavo Padovan 	}
1019db43fd16SPrathyush K 	if (ctx->vp_enabled) {
102038000dbbSGustavo Padovan 		ret = clk_prepare_enable(res->vp);
102138000dbbSGustavo Padovan 		if (ret < 0) {
102238000dbbSGustavo Padovan 			DRM_ERROR("Failed to prepare_enable the vp clk [%d]\n",
102338000dbbSGustavo Padovan 				  ret);
102438000dbbSGustavo Padovan 			return;
102538000dbbSGustavo Padovan 		}
102638000dbbSGustavo Padovan 		if (ctx->has_sclk) {
102738000dbbSGustavo Padovan 			ret = clk_prepare_enable(res->sclk_mixer);
102838000dbbSGustavo Padovan 			if (ret < 0) {
102938000dbbSGustavo Padovan 				DRM_ERROR("Failed to prepare_enable the " \
103038000dbbSGustavo Padovan 					   "sclk_mixer clk [%d]\n",
103138000dbbSGustavo Padovan 					  ret);
103238000dbbSGustavo Padovan 				return;
103338000dbbSGustavo Padovan 			}
103438000dbbSGustavo Padovan 		}
1035db43fd16SPrathyush K 	}
1036db43fd16SPrathyush K 
1037a44652e8SAndrzej Hajda 	set_bit(MXR_BIT_POWERED, &ctx->flags);
1038b4bfa3c7SRahul Sharma 
1039d74ed937SRahul Sharma 	mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_SOFT_RESET);
1040d74ed937SRahul Sharma 
10410df5e4acSAndrzej Hajda 	if (test_bit(MXR_BIT_VSYNC, &ctx->flags)) {
1042fc073248SAndrzej Hajda 		mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
10430df5e4acSAndrzej Hajda 		mixer_reg_writemask(res, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
10440df5e4acSAndrzej Hajda 	}
1045db43fd16SPrathyush K 	mixer_win_reset(ctx);
1046db43fd16SPrathyush K }
1047db43fd16SPrathyush K 
10483cecda03SGustavo Padovan static void mixer_disable(struct exynos_drm_crtc *crtc)
1049db43fd16SPrathyush K {
10503cecda03SGustavo Padovan 	struct mixer_context *ctx = crtc->ctx;
1051db43fd16SPrathyush K 	struct mixer_resources *res = &ctx->mixer_res;
1052c329f667SJoonyoung Shim 	int i;
1053db43fd16SPrathyush K 
1054a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &ctx->flags))
1055b4bfa3c7SRahul Sharma 		return;
1056db43fd16SPrathyush K 
1057381be025SRahul Sharma 	mixer_stop(ctx);
1058c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
1059c329f667SJoonyoung Shim 
1060c329f667SJoonyoung Shim 	for (i = 0; i < MIXER_WIN_NR; i++)
10611e1d1393SGustavo Padovan 		mixer_disable_plane(crtc, &ctx->planes[i]);
1062db43fd16SPrathyush K 
1063a44652e8SAndrzej Hajda 	clear_bit(MXR_BIT_POWERED, &ctx->flags);
1064b4bfa3c7SRahul Sharma 
106504427ec5SMarek Szyprowski 	clk_disable_unprepare(res->hdmi);
10660bfb1f8bSSean Paul 	clk_disable_unprepare(res->mixer);
1067db43fd16SPrathyush K 	if (ctx->vp_enabled) {
10680bfb1f8bSSean Paul 		clk_disable_unprepare(res->vp);
1069ff830c96SMarek Szyprowski 		if (ctx->has_sclk)
10700bfb1f8bSSean Paul 			clk_disable_unprepare(res->sclk_mixer);
1071db43fd16SPrathyush K 	}
1072db43fd16SPrathyush K 
1073af65c804SSean Paul 	pm_runtime_put_sync(ctx->dev);
1074db43fd16SPrathyush K }
1075db43fd16SPrathyush K 
1076f041b257SSean Paul /* Only valid for Mixer version 16.0.33.0 */
1077f041b257SSean Paul int mixer_check_mode(struct drm_display_mode *mode)
1078f041b257SSean Paul {
1079f041b257SSean Paul 	u32 w, h;
1080f041b257SSean Paul 
1081f041b257SSean Paul 	w = mode->hdisplay;
1082f041b257SSean Paul 	h = mode->vdisplay;
1083f041b257SSean Paul 
1084f041b257SSean Paul 	DRM_DEBUG_KMS("xres=%d, yres=%d, refresh=%d, intl=%d\n",
1085f041b257SSean Paul 		mode->hdisplay, mode->vdisplay, mode->vrefresh,
1086f041b257SSean Paul 		(mode->flags & DRM_MODE_FLAG_INTERLACE) ? 1 : 0);
1087f041b257SSean Paul 
1088f041b257SSean Paul 	if ((w >= 464 && w <= 720 && h >= 261 && h <= 576) ||
1089f041b257SSean Paul 		(w >= 1024 && w <= 1280 && h >= 576 && h <= 720) ||
1090f041b257SSean Paul 		(w >= 1664 && w <= 1920 && h >= 936 && h <= 1080))
1091f041b257SSean Paul 		return 0;
1092f041b257SSean Paul 
1093f041b257SSean Paul 	return -EINVAL;
1094f041b257SSean Paul }
1095f041b257SSean Paul 
1096f3aaf762SKrzysztof Kozlowski static const struct exynos_drm_crtc_ops mixer_crtc_ops = {
10973cecda03SGustavo Padovan 	.enable			= mixer_enable,
10983cecda03SGustavo Padovan 	.disable		= mixer_disable,
1099d8408326SSeung-Woo Kim 	.enable_vblank		= mixer_enable_vblank,
1100d8408326SSeung-Woo Kim 	.disable_vblank		= mixer_disable_vblank,
11018137a2e2SPrathyush K 	.wait_for_vblank	= mixer_wait_for_vblank,
11029cc7610aSGustavo Padovan 	.update_plane		= mixer_update_plane,
11039cc7610aSGustavo Padovan 	.disable_plane		= mixer_disable_plane,
1104f041b257SSean Paul };
11050ea6822fSRahul Sharma 
1106def5e095SRahul Sharma static struct mixer_drv_data exynos5420_mxr_drv_data = {
1107def5e095SRahul Sharma 	.version = MXR_VER_128_0_0_184,
1108def5e095SRahul Sharma 	.is_vp_enabled = 0,
1109def5e095SRahul Sharma };
1110def5e095SRahul Sharma 
1111cc57caf0SRahul Sharma static struct mixer_drv_data exynos5250_mxr_drv_data = {
1112aaf8b49eSRahul Sharma 	.version = MXR_VER_16_0_33_0,
1113aaf8b49eSRahul Sharma 	.is_vp_enabled = 0,
1114aaf8b49eSRahul Sharma };
1115aaf8b49eSRahul Sharma 
1116ff830c96SMarek Szyprowski static struct mixer_drv_data exynos4212_mxr_drv_data = {
1117ff830c96SMarek Szyprowski 	.version = MXR_VER_0_0_0_16,
1118ff830c96SMarek Szyprowski 	.is_vp_enabled = 1,
1119ff830c96SMarek Szyprowski };
1120ff830c96SMarek Szyprowski 
1121cc57caf0SRahul Sharma static struct mixer_drv_data exynos4210_mxr_drv_data = {
11221e123441SRahul Sharma 	.version = MXR_VER_0_0_0_16,
11231b8e5747SRahul Sharma 	.is_vp_enabled = 1,
1124ff830c96SMarek Szyprowski 	.has_sclk = 1,
11251e123441SRahul Sharma };
11261e123441SRahul Sharma 
1127d6b16302SKrzysztof Kozlowski static const struct platform_device_id mixer_driver_types[] = {
11281e123441SRahul Sharma 	{
11291e123441SRahul Sharma 		.name		= "s5p-mixer",
1130cc57caf0SRahul Sharma 		.driver_data	= (unsigned long)&exynos4210_mxr_drv_data,
11311e123441SRahul Sharma 	}, {
1132aaf8b49eSRahul Sharma 		.name		= "exynos5-mixer",
1133cc57caf0SRahul Sharma 		.driver_data	= (unsigned long)&exynos5250_mxr_drv_data,
1134aaf8b49eSRahul Sharma 	}, {
1135aaf8b49eSRahul Sharma 		/* end node */
1136aaf8b49eSRahul Sharma 	}
1137aaf8b49eSRahul Sharma };
1138aaf8b49eSRahul Sharma 
1139aaf8b49eSRahul Sharma static struct of_device_id mixer_match_types[] = {
1140aaf8b49eSRahul Sharma 	{
1141ff830c96SMarek Szyprowski 		.compatible = "samsung,exynos4210-mixer",
1142ff830c96SMarek Szyprowski 		.data	= &exynos4210_mxr_drv_data,
1143ff830c96SMarek Szyprowski 	}, {
1144ff830c96SMarek Szyprowski 		.compatible = "samsung,exynos4212-mixer",
1145ff830c96SMarek Szyprowski 		.data	= &exynos4212_mxr_drv_data,
1146ff830c96SMarek Szyprowski 	}, {
1147aaf8b49eSRahul Sharma 		.compatible = "samsung,exynos5-mixer",
1148cc57caf0SRahul Sharma 		.data	= &exynos5250_mxr_drv_data,
1149cc57caf0SRahul Sharma 	}, {
1150cc57caf0SRahul Sharma 		.compatible = "samsung,exynos5250-mixer",
1151cc57caf0SRahul Sharma 		.data	= &exynos5250_mxr_drv_data,
1152aaf8b49eSRahul Sharma 	}, {
1153def5e095SRahul Sharma 		.compatible = "samsung,exynos5420-mixer",
1154def5e095SRahul Sharma 		.data	= &exynos5420_mxr_drv_data,
1155def5e095SRahul Sharma 	}, {
11561e123441SRahul Sharma 		/* end node */
11571e123441SRahul Sharma 	}
11581e123441SRahul Sharma };
115939b58a39SSjoerd Simons MODULE_DEVICE_TABLE(of, mixer_match_types);
11601e123441SRahul Sharma 
1161f37cd5e8SInki Dae static int mixer_bind(struct device *dev, struct device *manager, void *data)
1162d8408326SSeung-Woo Kim {
11638103ef1bSAndrzej Hajda 	struct mixer_context *ctx = dev_get_drvdata(dev);
1164f37cd5e8SInki Dae 	struct drm_device *drm_dev = data;
11657ee14cdcSGustavo Padovan 	struct exynos_drm_plane *exynos_plane;
11667ee14cdcSGustavo Padovan 	enum drm_plane_type type;
11676e2a3b66SGustavo Padovan 	unsigned int zpos;
11686e2a3b66SGustavo Padovan 	int ret;
1169d8408326SSeung-Woo Kim 
1170e2dc3f72SAlban Browaeys 	ret = mixer_initialize(ctx, drm_dev);
1171e2dc3f72SAlban Browaeys 	if (ret)
1172e2dc3f72SAlban Browaeys 		return ret;
1173e2dc3f72SAlban Browaeys 
11747ee14cdcSGustavo Padovan 	for (zpos = 0; zpos < MIXER_WIN_NR; zpos++) {
11757ee14cdcSGustavo Padovan 		type = (zpos == MIXER_DEFAULT_WIN) ? DRM_PLANE_TYPE_PRIMARY :
11767ee14cdcSGustavo Padovan 						DRM_PLANE_TYPE_OVERLAY;
11777ee14cdcSGustavo Padovan 		ret = exynos_plane_init(drm_dev, &ctx->planes[zpos],
11786e2a3b66SGustavo Padovan 					1 << ctx->pipe, type, zpos);
11797ee14cdcSGustavo Padovan 		if (ret)
11807ee14cdcSGustavo Padovan 			return ret;
11817ee14cdcSGustavo Padovan 	}
11827ee14cdcSGustavo Padovan 
11837ee14cdcSGustavo Padovan 	exynos_plane = &ctx->planes[MIXER_DEFAULT_WIN];
11847ee14cdcSGustavo Padovan 	ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
11857ee14cdcSGustavo Padovan 					   ctx->pipe, EXYNOS_DISPLAY_TYPE_HDMI,
118693bca243SGustavo Padovan 					   &mixer_crtc_ops, ctx);
118793bca243SGustavo Padovan 	if (IS_ERR(ctx->crtc)) {
1188e2dc3f72SAlban Browaeys 		mixer_ctx_remove(ctx);
118993bca243SGustavo Padovan 		ret = PTR_ERR(ctx->crtc);
119093bca243SGustavo Padovan 		goto free_ctx;
11918103ef1bSAndrzej Hajda 	}
11928103ef1bSAndrzej Hajda 
11938103ef1bSAndrzej Hajda 	return 0;
119493bca243SGustavo Padovan 
119593bca243SGustavo Padovan free_ctx:
119693bca243SGustavo Padovan 	devm_kfree(dev, ctx);
119793bca243SGustavo Padovan 	return ret;
11988103ef1bSAndrzej Hajda }
11998103ef1bSAndrzej Hajda 
12008103ef1bSAndrzej Hajda static void mixer_unbind(struct device *dev, struct device *master, void *data)
12018103ef1bSAndrzej Hajda {
12028103ef1bSAndrzej Hajda 	struct mixer_context *ctx = dev_get_drvdata(dev);
12038103ef1bSAndrzej Hajda 
120493bca243SGustavo Padovan 	mixer_ctx_remove(ctx);
12058103ef1bSAndrzej Hajda }
12068103ef1bSAndrzej Hajda 
12078103ef1bSAndrzej Hajda static const struct component_ops mixer_component_ops = {
12088103ef1bSAndrzej Hajda 	.bind	= mixer_bind,
12098103ef1bSAndrzej Hajda 	.unbind	= mixer_unbind,
12108103ef1bSAndrzej Hajda };
12118103ef1bSAndrzej Hajda 
12128103ef1bSAndrzej Hajda static int mixer_probe(struct platform_device *pdev)
12138103ef1bSAndrzej Hajda {
12148103ef1bSAndrzej Hajda 	struct device *dev = &pdev->dev;
12158103ef1bSAndrzej Hajda 	struct mixer_drv_data *drv;
12168103ef1bSAndrzej Hajda 	struct mixer_context *ctx;
12178103ef1bSAndrzej Hajda 	int ret;
1218d8408326SSeung-Woo Kim 
1219f041b257SSean Paul 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1220f041b257SSean Paul 	if (!ctx) {
1221f041b257SSean Paul 		DRM_ERROR("failed to alloc mixer context.\n");
1222d8408326SSeung-Woo Kim 		return -ENOMEM;
1223f041b257SSean Paul 	}
1224d8408326SSeung-Woo Kim 
1225aaf8b49eSRahul Sharma 	if (dev->of_node) {
1226aaf8b49eSRahul Sharma 		const struct of_device_id *match;
12278103ef1bSAndrzej Hajda 
1228e436b09dSSachin Kamat 		match = of_match_node(mixer_match_types, dev->of_node);
12292cdc53b3SRahul Sharma 		drv = (struct mixer_drv_data *)match->data;
1230aaf8b49eSRahul Sharma 	} else {
1231aaf8b49eSRahul Sharma 		drv = (struct mixer_drv_data *)
1232aaf8b49eSRahul Sharma 			platform_get_device_id(pdev)->driver_data;
1233aaf8b49eSRahul Sharma 	}
1234aaf8b49eSRahul Sharma 
12354551789fSSean Paul 	ctx->pdev = pdev;
1236d873ab99SSeung-Woo Kim 	ctx->dev = dev;
12371b8e5747SRahul Sharma 	ctx->vp_enabled = drv->is_vp_enabled;
1238ff830c96SMarek Szyprowski 	ctx->has_sclk = drv->has_sclk;
12391e123441SRahul Sharma 	ctx->mxr_ver = drv->version;
124057ed0f7bSDaniel Vetter 	init_waitqueue_head(&ctx->wait_vsync_queue);
12416e95d5e6SPrathyush K 	atomic_set(&ctx->wait_vsync_event, 0);
1242d8408326SSeung-Woo Kim 
12438103ef1bSAndrzej Hajda 	platform_set_drvdata(pdev, ctx);
1244df5225bcSInki Dae 
1245df5225bcSInki Dae 	ret = component_add(&pdev->dev, &mixer_component_ops);
124686650408SAndrzej Hajda 	if (!ret)
12478103ef1bSAndrzej Hajda 		pm_runtime_enable(dev);
1248df5225bcSInki Dae 
1249df5225bcSInki Dae 	return ret;
1250f37cd5e8SInki Dae }
1251f37cd5e8SInki Dae 
1252d8408326SSeung-Woo Kim static int mixer_remove(struct platform_device *pdev)
1253d8408326SSeung-Woo Kim {
12548103ef1bSAndrzej Hajda 	pm_runtime_disable(&pdev->dev);
12558103ef1bSAndrzej Hajda 
1256df5225bcSInki Dae 	component_del(&pdev->dev, &mixer_component_ops);
1257df5225bcSInki Dae 
1258d8408326SSeung-Woo Kim 	return 0;
1259d8408326SSeung-Woo Kim }
1260d8408326SSeung-Woo Kim 
1261d8408326SSeung-Woo Kim struct platform_driver mixer_driver = {
1262d8408326SSeung-Woo Kim 	.driver = {
1263aaf8b49eSRahul Sharma 		.name = "exynos-mixer",
1264d8408326SSeung-Woo Kim 		.owner = THIS_MODULE,
1265aaf8b49eSRahul Sharma 		.of_match_table = mixer_match_types,
1266d8408326SSeung-Woo Kim 	},
1267d8408326SSeung-Woo Kim 	.probe = mixer_probe,
126856550d94SGreg Kroah-Hartman 	.remove = mixer_remove,
12691e123441SRahul Sharma 	.id_table	= mixer_driver_types,
1270d8408326SSeung-Woo Kim };
1271