xref: /linux/drivers/gpu/drm/exynos/exynos_mixer.c (revision 1261255531088208daeca818e2b486030b5339e5)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d8408326SSeung-Woo Kim /*
3d8408326SSeung-Woo Kim  * Copyright (C) 2011 Samsung Electronics Co.Ltd
4d8408326SSeung-Woo Kim  * Authors:
5d8408326SSeung-Woo Kim  * Seung-Woo Kim <sw0312.kim@samsung.com>
6d8408326SSeung-Woo Kim  *	Inki Dae <inki.dae@samsung.com>
7d8408326SSeung-Woo Kim  *	Joonyoung Shim <jy0922.shim@samsung.com>
8d8408326SSeung-Woo Kim  *
9d8408326SSeung-Woo Kim  * Based on drivers/media/video/s5p-tv/mixer_reg.c
10d8408326SSeung-Woo Kim  */
11d8408326SSeung-Woo Kim 
122bda34d7SSam Ravnborg #include <linux/clk.h>
132bda34d7SSam Ravnborg #include <linux/component.h>
142bda34d7SSam Ravnborg #include <linux/delay.h>
15d8408326SSeung-Woo Kim #include <linux/i2c.h>
16d8408326SSeung-Woo Kim #include <linux/interrupt.h>
17d8408326SSeung-Woo Kim #include <linux/irq.h>
182bda34d7SSam Ravnborg #include <linux/kernel.h>
192bda34d7SSam Ravnborg #include <linux/ktime.h>
203f1c781dSSachin Kamat #include <linux/of.h>
2148f6155aSMarek Szyprowski #include <linux/of_device.h>
222bda34d7SSam Ravnborg #include <linux/platform_device.h>
232bda34d7SSam Ravnborg #include <linux/pm_runtime.h>
242bda34d7SSam Ravnborg #include <linux/regulator/consumer.h>
252bda34d7SSam Ravnborg #include <linux/spinlock.h>
262bda34d7SSam Ravnborg #include <linux/wait.h>
27d8408326SSeung-Woo Kim 
2890bb087fSVille Syrjälä #include <drm/drm_blend.h>
29255490f9SVille Syrjälä #include <drm/drm_edid.h>
302bda34d7SSam Ravnborg #include <drm/drm_fourcc.h>
31720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h>
322bda34d7SSam Ravnborg #include <drm/drm_vblank.h>
33d8408326SSeung-Woo Kim #include <drm/exynos_drm.h>
34d8408326SSeung-Woo Kim 
35663d8766SRahul Sharma #include "exynos_drm_crtc.h"
362bda34d7SSam Ravnborg #include "exynos_drm_drv.h"
370488f50eSMarek Szyprowski #include "exynos_drm_fb.h"
387ee14cdcSGustavo Padovan #include "exynos_drm_plane.h"
392bda34d7SSam Ravnborg #include "regs-mixer.h"
402bda34d7SSam Ravnborg #include "regs-vp.h"
4122b21ae6SJoonyoung Shim 
42f041b257SSean Paul #define MIXER_WIN_NR		3
43fbbb1e1aSMarek Szyprowski #define VP_DEFAULT_WIN		2
44d8408326SSeung-Woo Kim 
452a6e4cd5STobias Jakobi /*
462a6e4cd5STobias Jakobi  * Mixer color space conversion coefficient triplet.
472a6e4cd5STobias Jakobi  * Used for CSC from RGB to YCbCr.
482a6e4cd5STobias Jakobi  * Each coefficient is a 10-bit fixed point number with
492a6e4cd5STobias Jakobi  * sign and no integer part, i.e.
502a6e4cd5STobias Jakobi  * [0:8] = fractional part (representing a value y = x / 2^9)
512a6e4cd5STobias Jakobi  * [9] = sign
522a6e4cd5STobias Jakobi  * Negative values are encoded with two's complement.
532a6e4cd5STobias Jakobi  */
542a6e4cd5STobias Jakobi #define MXR_CSC_C(x) ((int)((x) * 512.0) & 0x3ff)
552a6e4cd5STobias Jakobi #define MXR_CSC_CT(a0, a1, a2) \
562a6e4cd5STobias Jakobi   ((MXR_CSC_C(a0) << 20) | (MXR_CSC_C(a1) << 10) | (MXR_CSC_C(a2) << 0))
572a6e4cd5STobias Jakobi 
582a6e4cd5STobias Jakobi /* YCbCr value, used for mixer background color configuration. */
592a6e4cd5STobias Jakobi #define MXR_YCBCR_VAL(y, cb, cr) (((y) << 16) | ((cb) << 8) | ((cr) << 0))
602a6e4cd5STobias Jakobi 
617a57ca7cSTobias Jakobi /* The pixelformats that are natively supported by the mixer. */
627a57ca7cSTobias Jakobi #define MXR_FORMAT_RGB565	4
637a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB1555	5
647a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB4444	6
657a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB8888	7
667a57ca7cSTobias Jakobi 
671e123441SRahul Sharma enum mixer_version_id {
681e123441SRahul Sharma 	MXR_VER_0_0_0_16,
691e123441SRahul Sharma 	MXR_VER_16_0_33_0,
70def5e095SRahul Sharma 	MXR_VER_128_0_0_184,
711e123441SRahul Sharma };
721e123441SRahul Sharma 
73a44652e8SAndrzej Hajda enum mixer_flag_bits {
74a44652e8SAndrzej Hajda 	MXR_BIT_POWERED,
750df5e4acSAndrzej Hajda 	MXR_BIT_VSYNC,
76adeb6f44STobias Jakobi 	MXR_BIT_INTERLACE,
77adeb6f44STobias Jakobi 	MXR_BIT_VP_ENABLED,
78adeb6f44STobias Jakobi 	MXR_BIT_HAS_SCLK,
79a44652e8SAndrzej Hajda };
80a44652e8SAndrzej Hajda 
81fbbb1e1aSMarek Szyprowski static const uint32_t mixer_formats[] = {
82fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_XRGB4444,
8326a7af3eSTobias Jakobi 	DRM_FORMAT_ARGB4444,
84fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_XRGB1555,
8526a7af3eSTobias Jakobi 	DRM_FORMAT_ARGB1555,
86fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_RGB565,
87fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_XRGB8888,
88fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_ARGB8888,
89fbbb1e1aSMarek Szyprowski };
90fbbb1e1aSMarek Szyprowski 
91fbbb1e1aSMarek Szyprowski static const uint32_t vp_formats[] = {
92fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_NV12,
93fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_NV21,
94fbbb1e1aSMarek Szyprowski };
95fbbb1e1aSMarek Szyprowski 
9622b21ae6SJoonyoung Shim struct mixer_context {
974551789fSSean Paul 	struct platform_device *pdev;
98cf8fc4f1SJoonyoung Shim 	struct device		*dev;
991055b39fSInki Dae 	struct drm_device	*drm_dev;
10007dc3678SMarek Szyprowski 	void			*dma_priv;
10193bca243SGustavo Padovan 	struct exynos_drm_crtc	*crtc;
1027ee14cdcSGustavo Padovan 	struct exynos_drm_plane	planes[MIXER_WIN_NR];
103a44652e8SAndrzej Hajda 	unsigned long		flags;
10422b21ae6SJoonyoung Shim 
105524c59f1SAndrzej Hajda 	int			irq;
106524c59f1SAndrzej Hajda 	void __iomem		*mixer_regs;
107524c59f1SAndrzej Hajda 	void __iomem		*vp_regs;
108524c59f1SAndrzej Hajda 	spinlock_t		reg_slock;
109524c59f1SAndrzej Hajda 	struct clk		*mixer;
110524c59f1SAndrzej Hajda 	struct clk		*vp;
111524c59f1SAndrzej Hajda 	struct clk		*hdmi;
112524c59f1SAndrzej Hajda 	struct clk		*sclk_mixer;
113524c59f1SAndrzej Hajda 	struct clk		*sclk_hdmi;
114524c59f1SAndrzej Hajda 	struct clk		*mout_mixer;
1151e123441SRahul Sharma 	enum mixer_version_id	mxr_ver;
116acc8bf04SAndrzej Hajda 	int			scan_value;
1171e123441SRahul Sharma };
1181e123441SRahul Sharma 
1191e123441SRahul Sharma struct mixer_drv_data {
1201e123441SRahul Sharma 	enum mixer_version_id	version;
1211b8e5747SRahul Sharma 	bool					is_vp_enabled;
122ff830c96SMarek Szyprowski 	bool					has_sclk;
12322b21ae6SJoonyoung Shim };
12422b21ae6SJoonyoung Shim 
125fd2d2fc2SMarek Szyprowski static const struct exynos_drm_plane_config plane_configs[MIXER_WIN_NR] = {
126fd2d2fc2SMarek Szyprowski 	{
127fd2d2fc2SMarek Szyprowski 		.zpos = 0,
128fd2d2fc2SMarek Szyprowski 		.type = DRM_PLANE_TYPE_PRIMARY,
129fd2d2fc2SMarek Szyprowski 		.pixel_formats = mixer_formats,
130fd2d2fc2SMarek Szyprowski 		.num_pixel_formats = ARRAY_SIZE(mixer_formats),
131a2cb911eSMarek Szyprowski 		.capabilities = EXYNOS_DRM_PLANE_CAP_DOUBLE |
132482582c0SChristoph Manszewski 				EXYNOS_DRM_PLANE_CAP_ZPOS |
1336ac99a32SChristoph Manszewski 				EXYNOS_DRM_PLANE_CAP_PIX_BLEND |
1346ac99a32SChristoph Manszewski 				EXYNOS_DRM_PLANE_CAP_WIN_BLEND,
135fd2d2fc2SMarek Szyprowski 	}, {
136fd2d2fc2SMarek Szyprowski 		.zpos = 1,
137fd2d2fc2SMarek Szyprowski 		.type = DRM_PLANE_TYPE_CURSOR,
138fd2d2fc2SMarek Szyprowski 		.pixel_formats = mixer_formats,
139fd2d2fc2SMarek Szyprowski 		.num_pixel_formats = ARRAY_SIZE(mixer_formats),
140a2cb911eSMarek Szyprowski 		.capabilities = EXYNOS_DRM_PLANE_CAP_DOUBLE |
141482582c0SChristoph Manszewski 				EXYNOS_DRM_PLANE_CAP_ZPOS |
1426ac99a32SChristoph Manszewski 				EXYNOS_DRM_PLANE_CAP_PIX_BLEND |
1436ac99a32SChristoph Manszewski 				EXYNOS_DRM_PLANE_CAP_WIN_BLEND,
144fd2d2fc2SMarek Szyprowski 	}, {
145fd2d2fc2SMarek Szyprowski 		.zpos = 2,
146fd2d2fc2SMarek Szyprowski 		.type = DRM_PLANE_TYPE_OVERLAY,
147fd2d2fc2SMarek Szyprowski 		.pixel_formats = vp_formats,
148fd2d2fc2SMarek Szyprowski 		.num_pixel_formats = ARRAY_SIZE(vp_formats),
149a2cb911eSMarek Szyprowski 		.capabilities = EXYNOS_DRM_PLANE_CAP_SCALE |
150f40031c2STobias Jakobi 				EXYNOS_DRM_PLANE_CAP_ZPOS |
1516ac99a32SChristoph Manszewski 				EXYNOS_DRM_PLANE_CAP_TILE |
1526ac99a32SChristoph Manszewski 				EXYNOS_DRM_PLANE_CAP_WIN_BLEND,
153fd2d2fc2SMarek Szyprowski 	},
154fd2d2fc2SMarek Szyprowski };
155fd2d2fc2SMarek Szyprowski 
156d8408326SSeung-Woo Kim static const u8 filter_y_horiz_tap8[] = {
157d8408326SSeung-Woo Kim 	0,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
158d8408326SSeung-Woo Kim 	-1,	-1,	-1,	-1,	-1,	0,	0,	0,
159d8408326SSeung-Woo Kim 	0,	2,	4,	5,	6,	6,	6,	6,
160d8408326SSeung-Woo Kim 	6,	5,	5,	4,	3,	2,	1,	1,
161d8408326SSeung-Woo Kim 	0,	-6,	-12,	-16,	-18,	-20,	-21,	-20,
162d8408326SSeung-Woo Kim 	-20,	-18,	-16,	-13,	-10,	-8,	-5,	-2,
163d8408326SSeung-Woo Kim 	127,	126,	125,	121,	114,	107,	99,	89,
164d8408326SSeung-Woo Kim 	79,	68,	57,	46,	35,	25,	16,	8,
165d8408326SSeung-Woo Kim };
166d8408326SSeung-Woo Kim 
167d8408326SSeung-Woo Kim static const u8 filter_y_vert_tap4[] = {
168d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
169d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
170d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
171d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
172d8408326SSeung-Woo Kim 	0,	5,	11,	19,	27,	37,	48,	59,
173d8408326SSeung-Woo Kim 	70,	81,	92,	102,	111,	118,	124,	126,
174d8408326SSeung-Woo Kim 	0,	0,	-1,	-1,	-2,	-3,	-4,	-5,
175d8408326SSeung-Woo Kim 	-6,	-7,	-8,	-8,	-8,	-8,	-6,	-3,
176d8408326SSeung-Woo Kim };
177d8408326SSeung-Woo Kim 
178d8408326SSeung-Woo Kim static const u8 filter_cr_horiz_tap4[] = {
179d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
180d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
181d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
182d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
183d8408326SSeung-Woo Kim };
184d8408326SSeung-Woo Kim 
185524c59f1SAndrzej Hajda static inline u32 vp_reg_read(struct mixer_context *ctx, u32 reg_id)
186d8408326SSeung-Woo Kim {
187524c59f1SAndrzej Hajda 	return readl(ctx->vp_regs + reg_id);
188d8408326SSeung-Woo Kim }
189d8408326SSeung-Woo Kim 
190524c59f1SAndrzej Hajda static inline void vp_reg_write(struct mixer_context *ctx, u32 reg_id,
191d8408326SSeung-Woo Kim 				 u32 val)
192d8408326SSeung-Woo Kim {
193524c59f1SAndrzej Hajda 	writel(val, ctx->vp_regs + reg_id);
194d8408326SSeung-Woo Kim }
195d8408326SSeung-Woo Kim 
196524c59f1SAndrzej Hajda static inline void vp_reg_writemask(struct mixer_context *ctx, u32 reg_id,
197d8408326SSeung-Woo Kim 				 u32 val, u32 mask)
198d8408326SSeung-Woo Kim {
199524c59f1SAndrzej Hajda 	u32 old = vp_reg_read(ctx, reg_id);
200d8408326SSeung-Woo Kim 
201d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
202524c59f1SAndrzej Hajda 	writel(val, ctx->vp_regs + reg_id);
203d8408326SSeung-Woo Kim }
204d8408326SSeung-Woo Kim 
205524c59f1SAndrzej Hajda static inline u32 mixer_reg_read(struct mixer_context *ctx, u32 reg_id)
206d8408326SSeung-Woo Kim {
207524c59f1SAndrzej Hajda 	return readl(ctx->mixer_regs + reg_id);
208d8408326SSeung-Woo Kim }
209d8408326SSeung-Woo Kim 
210524c59f1SAndrzej Hajda static inline void mixer_reg_write(struct mixer_context *ctx, u32 reg_id,
211d8408326SSeung-Woo Kim 				 u32 val)
212d8408326SSeung-Woo Kim {
213524c59f1SAndrzej Hajda 	writel(val, ctx->mixer_regs + reg_id);
214d8408326SSeung-Woo Kim }
215d8408326SSeung-Woo Kim 
216524c59f1SAndrzej Hajda static inline void mixer_reg_writemask(struct mixer_context *ctx,
217d8408326SSeung-Woo Kim 				 u32 reg_id, u32 val, u32 mask)
218d8408326SSeung-Woo Kim {
219524c59f1SAndrzej Hajda 	u32 old = mixer_reg_read(ctx, reg_id);
220d8408326SSeung-Woo Kim 
221d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
222524c59f1SAndrzej Hajda 	writel(val, ctx->mixer_regs + reg_id);
223d8408326SSeung-Woo Kim }
224d8408326SSeung-Woo Kim 
225d8408326SSeung-Woo Kim static void mixer_regs_dump(struct mixer_context *ctx)
226d8408326SSeung-Woo Kim {
227d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
228d8408326SSeung-Woo Kim do { \
2296be90056SInki Dae 	DRM_DEV_DEBUG_KMS(ctx->dev, #reg_id " = %08x\n", \
230524c59f1SAndrzej Hajda 			 (u32)readl(ctx->mixer_regs + reg_id)); \
231d8408326SSeung-Woo Kim } while (0)
232d8408326SSeung-Woo Kim 
233d8408326SSeung-Woo Kim 	DUMPREG(MXR_STATUS);
234d8408326SSeung-Woo Kim 	DUMPREG(MXR_CFG);
235d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_EN);
236d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_STATUS);
237d8408326SSeung-Woo Kim 
238d8408326SSeung-Woo Kim 	DUMPREG(MXR_LAYER_CFG);
239d8408326SSeung-Woo Kim 	DUMPREG(MXR_VIDEO_CFG);
240d8408326SSeung-Woo Kim 
241d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_CFG);
242d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_BASE);
243d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SPAN);
244d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_WH);
245d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SXY);
246d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_DXY);
247d8408326SSeung-Woo Kim 
248d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_CFG);
249d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_BASE);
250d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SPAN);
251d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_WH);
252d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SXY);
253d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_DXY);
254d8408326SSeung-Woo Kim #undef DUMPREG
255d8408326SSeung-Woo Kim }
256d8408326SSeung-Woo Kim 
257d8408326SSeung-Woo Kim static void vp_regs_dump(struct mixer_context *ctx)
258d8408326SSeung-Woo Kim {
259d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
260d8408326SSeung-Woo Kim do { \
2616be90056SInki Dae 	DRM_DEV_DEBUG_KMS(ctx->dev, #reg_id " = %08x\n", \
262524c59f1SAndrzej Hajda 			 (u32) readl(ctx->vp_regs + reg_id)); \
263d8408326SSeung-Woo Kim } while (0)
264d8408326SSeung-Woo Kim 
265d8408326SSeung-Woo Kim 	DUMPREG(VP_ENABLE);
266d8408326SSeung-Woo Kim 	DUMPREG(VP_SRESET);
267d8408326SSeung-Woo Kim 	DUMPREG(VP_SHADOW_UPDATE);
268d8408326SSeung-Woo Kim 	DUMPREG(VP_FIELD_ID);
269d8408326SSeung-Woo Kim 	DUMPREG(VP_MODE);
270d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_Y);
271d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_C);
272d8408326SSeung-Woo Kim 	DUMPREG(VP_PER_RATE_CTRL);
273d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_Y_PTR);
274d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_Y_PTR);
275d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_C_PTR);
276d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_C_PTR);
277d8408326SSeung-Woo Kim 	DUMPREG(VP_ENDIAN_MODE);
278d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_H_POSITION);
279d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_V_POSITION);
280d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_WIDTH);
281d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_HEIGHT);
282d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_H_POSITION);
283d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_V_POSITION);
284d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_WIDTH);
285d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_HEIGHT);
286d8408326SSeung-Woo Kim 	DUMPREG(VP_H_RATIO);
287d8408326SSeung-Woo Kim 	DUMPREG(VP_V_RATIO);
288d8408326SSeung-Woo Kim 
289d8408326SSeung-Woo Kim #undef DUMPREG
290d8408326SSeung-Woo Kim }
291d8408326SSeung-Woo Kim 
292524c59f1SAndrzej Hajda static inline void vp_filter_set(struct mixer_context *ctx,
293d8408326SSeung-Woo Kim 		int reg_id, const u8 *data, unsigned int size)
294d8408326SSeung-Woo Kim {
295d8408326SSeung-Woo Kim 	/* assure 4-byte align */
296d8408326SSeung-Woo Kim 	BUG_ON(size & 3);
297d8408326SSeung-Woo Kim 	for (; size; size -= 4, reg_id += 4, data += 4) {
298d8408326SSeung-Woo Kim 		u32 val = (data[0] << 24) |  (data[1] << 16) |
299d8408326SSeung-Woo Kim 			(data[2] << 8) | data[3];
300524c59f1SAndrzej Hajda 		vp_reg_write(ctx, reg_id, val);
301d8408326SSeung-Woo Kim 	}
302d8408326SSeung-Woo Kim }
303d8408326SSeung-Woo Kim 
304524c59f1SAndrzej Hajda static void vp_default_filter(struct mixer_context *ctx)
305d8408326SSeung-Woo Kim {
306524c59f1SAndrzej Hajda 	vp_filter_set(ctx, VP_POLY8_Y0_LL,
307e25e1b66SSachin Kamat 		filter_y_horiz_tap8, sizeof(filter_y_horiz_tap8));
308524c59f1SAndrzej Hajda 	vp_filter_set(ctx, VP_POLY4_Y0_LL,
309e25e1b66SSachin Kamat 		filter_y_vert_tap4, sizeof(filter_y_vert_tap4));
310524c59f1SAndrzej Hajda 	vp_filter_set(ctx, VP_POLY4_C0_LL,
311e25e1b66SSachin Kamat 		filter_cr_horiz_tap4, sizeof(filter_cr_horiz_tap4));
312d8408326SSeung-Woo Kim }
313d8408326SSeung-Woo Kim 
314f657a996SMarek Szyprowski static void mixer_cfg_gfx_blend(struct mixer_context *ctx, unsigned int win,
3156ac99a32SChristoph Manszewski 				unsigned int pixel_alpha, unsigned int alpha)
316f657a996SMarek Szyprowski {
3176ac99a32SChristoph Manszewski 	u32 win_alpha = alpha >> 8;
318f657a996SMarek Szyprowski 	u32 val;
319f657a996SMarek Szyprowski 
320f657a996SMarek Szyprowski 	val  = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
321482582c0SChristoph Manszewski 	switch (pixel_alpha) {
322482582c0SChristoph Manszewski 	case DRM_MODE_BLEND_PIXEL_NONE:
323482582c0SChristoph Manszewski 		break;
324482582c0SChristoph Manszewski 	case DRM_MODE_BLEND_COVERAGE:
325482582c0SChristoph Manszewski 		val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
326482582c0SChristoph Manszewski 		break;
327482582c0SChristoph Manszewski 	case DRM_MODE_BLEND_PREMULTI:
328482582c0SChristoph Manszewski 	default:
329f657a996SMarek Szyprowski 		val |= MXR_GRP_CFG_BLEND_PRE_MUL;
330f657a996SMarek Szyprowski 		val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
331482582c0SChristoph Manszewski 		break;
332f657a996SMarek Szyprowski 	}
3336ac99a32SChristoph Manszewski 
3346ac99a32SChristoph Manszewski 	if (alpha != DRM_BLEND_ALPHA_OPAQUE) {
3356ac99a32SChristoph Manszewski 		val |= MXR_GRP_CFG_WIN_BLEND_EN;
3366ac99a32SChristoph Manszewski 		val |= win_alpha;
3376ac99a32SChristoph Manszewski 	}
338524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_GRAPHIC_CFG(win),
339f657a996SMarek Szyprowski 			    val, MXR_GRP_CFG_MISC_MASK);
340f657a996SMarek Szyprowski }
341f657a996SMarek Szyprowski 
3426ac99a32SChristoph Manszewski static void mixer_cfg_vp_blend(struct mixer_context *ctx, unsigned int alpha)
343f657a996SMarek Szyprowski {
3446ac99a32SChristoph Manszewski 	u32 win_alpha = alpha >> 8;
3456ac99a32SChristoph Manszewski 	u32 val = 0;
346f657a996SMarek Szyprowski 
3476ac99a32SChristoph Manszewski 	if (alpha != DRM_BLEND_ALPHA_OPAQUE) {
3486ac99a32SChristoph Manszewski 		val |= MXR_VID_CFG_BLEND_EN;
3496ac99a32SChristoph Manszewski 		val |= win_alpha;
3506ac99a32SChristoph Manszewski 	}
351524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_VIDEO_CFG, val);
352f657a996SMarek Szyprowski }
353f657a996SMarek Szyprowski 
3546a3b45adSAndrzej Hajda static bool mixer_is_synced(struct mixer_context *ctx)
355d8408326SSeung-Woo Kim {
3566a3b45adSAndrzej Hajda 	u32 base, shadow;
357d8408326SSeung-Woo Kim 
3586a3b45adSAndrzej Hajda 	if (ctx->mxr_ver == MXR_VER_16_0_33_0 ||
3596a3b45adSAndrzej Hajda 	    ctx->mxr_ver == MXR_VER_128_0_0_184)
3606a3b45adSAndrzej Hajda 		return !(mixer_reg_read(ctx, MXR_CFG) &
3616a3b45adSAndrzej Hajda 			 MXR_CFG_LAYER_UPDATE_COUNT_MASK);
3626a3b45adSAndrzej Hajda 
3636a3b45adSAndrzej Hajda 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags) &&
3646a3b45adSAndrzej Hajda 	    vp_reg_read(ctx, VP_SHADOW_UPDATE))
3656a3b45adSAndrzej Hajda 		return false;
3666a3b45adSAndrzej Hajda 
3676a3b45adSAndrzej Hajda 	base = mixer_reg_read(ctx, MXR_CFG);
3686a3b45adSAndrzej Hajda 	shadow = mixer_reg_read(ctx, MXR_CFG_S);
3696a3b45adSAndrzej Hajda 	if (base != shadow)
3706a3b45adSAndrzej Hajda 		return false;
3716a3b45adSAndrzej Hajda 
3726a3b45adSAndrzej Hajda 	base = mixer_reg_read(ctx, MXR_GRAPHIC_BASE(0));
3736a3b45adSAndrzej Hajda 	shadow = mixer_reg_read(ctx, MXR_GRAPHIC_BASE_S(0));
3746a3b45adSAndrzej Hajda 	if (base != shadow)
3756a3b45adSAndrzej Hajda 		return false;
3766a3b45adSAndrzej Hajda 
3776a3b45adSAndrzej Hajda 	base = mixer_reg_read(ctx, MXR_GRAPHIC_BASE(1));
3786a3b45adSAndrzej Hajda 	shadow = mixer_reg_read(ctx, MXR_GRAPHIC_BASE_S(1));
3796a3b45adSAndrzej Hajda 	if (base != shadow)
3806a3b45adSAndrzej Hajda 		return false;
3816a3b45adSAndrzej Hajda 
3826a3b45adSAndrzej Hajda 	return true;
3836a3b45adSAndrzej Hajda }
3846a3b45adSAndrzej Hajda 
3856a3b45adSAndrzej Hajda static int mixer_wait_for_sync(struct mixer_context *ctx)
3866a3b45adSAndrzej Hajda {
3876a3b45adSAndrzej Hajda 	ktime_t timeout = ktime_add_us(ktime_get(), 100000);
3886a3b45adSAndrzej Hajda 
3896a3b45adSAndrzej Hajda 	while (!mixer_is_synced(ctx)) {
3906a3b45adSAndrzej Hajda 		usleep_range(1000, 2000);
3916a3b45adSAndrzej Hajda 		if (ktime_compare(ktime_get(), timeout) > 0)
3926a3b45adSAndrzej Hajda 			return -ETIMEDOUT;
3936a3b45adSAndrzej Hajda 	}
3946a3b45adSAndrzej Hajda 	return 0;
3956a3b45adSAndrzej Hajda }
3966a3b45adSAndrzej Hajda 
3976a3b45adSAndrzej Hajda static void mixer_disable_sync(struct mixer_context *ctx)
3986a3b45adSAndrzej Hajda {
3996a3b45adSAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_STATUS, 0, MXR_STATUS_SYNC_ENABLE);
4006a3b45adSAndrzej Hajda }
4016a3b45adSAndrzej Hajda 
4026a3b45adSAndrzej Hajda static void mixer_enable_sync(struct mixer_context *ctx)
4036a3b45adSAndrzej Hajda {
4046a3b45adSAndrzej Hajda 	if (ctx->mxr_ver == MXR_VER_16_0_33_0 ||
4056a3b45adSAndrzej Hajda 	    ctx->mxr_ver == MXR_VER_128_0_0_184)
4066a3b45adSAndrzej Hajda 		mixer_reg_writemask(ctx, MXR_CFG, ~0, MXR_CFG_LAYER_UPDATE);
4076a3b45adSAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_STATUS, ~0, MXR_STATUS_SYNC_ENABLE);
408adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags))
4096a3b45adSAndrzej Hajda 		vp_reg_write(ctx, VP_SHADOW_UPDATE, VP_SHADOW_UPDATE_ENABLE);
410d8408326SSeung-Woo Kim }
411d8408326SSeung-Woo Kim 
4123fc40ca9SAndrzej Hajda static void mixer_cfg_scan(struct mixer_context *ctx, int width, int height)
413d8408326SSeung-Woo Kim {
414d8408326SSeung-Woo Kim 	u32 val;
415d8408326SSeung-Woo Kim 
416d8408326SSeung-Woo Kim 	/* choosing between interlace and progressive mode */
417adeb6f44STobias Jakobi 	val = test_bit(MXR_BIT_INTERLACE, &ctx->flags) ?
418adeb6f44STobias Jakobi 		MXR_CFG_SCAN_INTERLACE : MXR_CFG_SCAN_PROGRESSIVE;
419d8408326SSeung-Woo Kim 
420acc8bf04SAndrzej Hajda 	if (ctx->mxr_ver == MXR_VER_128_0_0_184)
421524c59f1SAndrzej Hajda 		mixer_reg_write(ctx, MXR_RESOLUTION,
4223fc40ca9SAndrzej Hajda 			MXR_MXR_RES_HEIGHT(height) | MXR_MXR_RES_WIDTH(width));
423d8408326SSeung-Woo Kim 	else
424acc8bf04SAndrzej Hajda 		val |= ctx->scan_value;
425d8408326SSeung-Woo Kim 
426524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_CFG, val, MXR_CFG_SCAN_MASK);
427d8408326SSeung-Woo Kim }
428d8408326SSeung-Woo Kim 
42913e810f1SChristoph Manszewski static void mixer_cfg_rgb_fmt(struct mixer_context *ctx, struct drm_display_mode *mode)
430d8408326SSeung-Woo Kim {
43113e810f1SChristoph Manszewski 	enum hdmi_quantization_range range = drm_default_rgb_quant_range(mode);
432d8408326SSeung-Woo Kim 	u32 val;
433d8408326SSeung-Woo Kim 
43413e810f1SChristoph Manszewski 	if (mode->vdisplay < 720) {
43513e810f1SChristoph Manszewski 		val = MXR_CFG_RGB601;
436e9e5ba93SChristoph Manszewski 	} else {
43713e810f1SChristoph Manszewski 		val = MXR_CFG_RGB709;
43813e810f1SChristoph Manszewski 
4392a6e4cd5STobias Jakobi 		/* Configure the BT.709 CSC matrix for full range RGB. */
440524c59f1SAndrzej Hajda 		mixer_reg_write(ctx, MXR_CM_COEFF_Y,
4412a6e4cd5STobias Jakobi 			MXR_CSC_CT( 0.184,  0.614,  0.063) |
4422a6e4cd5STobias Jakobi 			MXR_CM_COEFF_RGB_FULL);
443524c59f1SAndrzej Hajda 		mixer_reg_write(ctx, MXR_CM_COEFF_CB,
4442a6e4cd5STobias Jakobi 			MXR_CSC_CT(-0.102, -0.338,  0.440));
445524c59f1SAndrzej Hajda 		mixer_reg_write(ctx, MXR_CM_COEFF_CR,
4462a6e4cd5STobias Jakobi 			MXR_CSC_CT( 0.440, -0.399, -0.040));
447d8408326SSeung-Woo Kim 	}
448d8408326SSeung-Woo Kim 
44913e810f1SChristoph Manszewski 	if (range == HDMI_QUANTIZATION_RANGE_FULL)
45013e810f1SChristoph Manszewski 		val |= MXR_CFG_QUANT_RANGE_FULL;
45113e810f1SChristoph Manszewski 	else
45213e810f1SChristoph Manszewski 		val |= MXR_CFG_QUANT_RANGE_LIMITED;
45313e810f1SChristoph Manszewski 
454524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
455d8408326SSeung-Woo Kim }
456d8408326SSeung-Woo Kim 
4575b1d5bc6STobias Jakobi static void mixer_cfg_layer(struct mixer_context *ctx, unsigned int win,
458a2cb911eSMarek Szyprowski 			    unsigned int priority, bool enable)
459d8408326SSeung-Woo Kim {
460d8408326SSeung-Woo Kim 	u32 val = enable ? ~0 : 0;
461d8408326SSeung-Woo Kim 
462d8408326SSeung-Woo Kim 	switch (win) {
463d8408326SSeung-Woo Kim 	case 0:
464524c59f1SAndrzej Hajda 		mixer_reg_writemask(ctx, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
465524c59f1SAndrzej Hajda 		mixer_reg_writemask(ctx, MXR_LAYER_CFG,
466a2cb911eSMarek Szyprowski 				    MXR_LAYER_CFG_GRP0_VAL(priority),
467a2cb911eSMarek Szyprowski 				    MXR_LAYER_CFG_GRP0_MASK);
468d8408326SSeung-Woo Kim 		break;
469d8408326SSeung-Woo Kim 	case 1:
470524c59f1SAndrzej Hajda 		mixer_reg_writemask(ctx, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
471524c59f1SAndrzej Hajda 		mixer_reg_writemask(ctx, MXR_LAYER_CFG,
472a2cb911eSMarek Szyprowski 				    MXR_LAYER_CFG_GRP1_VAL(priority),
473a2cb911eSMarek Szyprowski 				    MXR_LAYER_CFG_GRP1_MASK);
474adeb6f44STobias Jakobi 
475d8408326SSeung-Woo Kim 		break;
4765e68fef2SMarek Szyprowski 	case VP_DEFAULT_WIN:
477adeb6f44STobias Jakobi 		if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
478524c59f1SAndrzej Hajda 			vp_reg_writemask(ctx, VP_ENABLE, val, VP_ENABLE_ON);
479524c59f1SAndrzej Hajda 			mixer_reg_writemask(ctx, MXR_CFG, val,
4801b8e5747SRahul Sharma 				MXR_CFG_VP_ENABLE);
481524c59f1SAndrzej Hajda 			mixer_reg_writemask(ctx, MXR_LAYER_CFG,
482a2cb911eSMarek Szyprowski 					    MXR_LAYER_CFG_VP_VAL(priority),
483a2cb911eSMarek Szyprowski 					    MXR_LAYER_CFG_VP_MASK);
4841b8e5747SRahul Sharma 		}
485d8408326SSeung-Woo Kim 		break;
486d8408326SSeung-Woo Kim 	}
487d8408326SSeung-Woo Kim }
488d8408326SSeung-Woo Kim 
489d8408326SSeung-Woo Kim static void mixer_run(struct mixer_context *ctx)
490d8408326SSeung-Woo Kim {
491524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
492d8408326SSeung-Woo Kim }
493d8408326SSeung-Woo Kim 
494381be025SRahul Sharma static void mixer_stop(struct mixer_context *ctx)
495381be025SRahul Sharma {
496381be025SRahul Sharma 	int timeout = 20;
497381be025SRahul Sharma 
498524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_STATUS, 0, MXR_STATUS_REG_RUN);
499381be025SRahul Sharma 
500524c59f1SAndrzej Hajda 	while (!(mixer_reg_read(ctx, MXR_STATUS) & MXR_STATUS_REG_IDLE) &&
501381be025SRahul Sharma 			--timeout)
502381be025SRahul Sharma 		usleep_range(10000, 12000);
503381be025SRahul Sharma }
504381be025SRahul Sharma 
505521d98a3SAndrzej Hajda static void mixer_commit(struct mixer_context *ctx)
506521d98a3SAndrzej Hajda {
507521d98a3SAndrzej Hajda 	struct drm_display_mode *mode = &ctx->crtc->base.state->adjusted_mode;
508521d98a3SAndrzej Hajda 
5093fc40ca9SAndrzej Hajda 	mixer_cfg_scan(ctx, mode->hdisplay, mode->vdisplay);
51013e810f1SChristoph Manszewski 	mixer_cfg_rgb_fmt(ctx, mode);
511521d98a3SAndrzej Hajda 	mixer_run(ctx);
512521d98a3SAndrzej Hajda }
513521d98a3SAndrzej Hajda 
5142eeb2e5eSGustavo Padovan static void vp_video_buffer(struct mixer_context *ctx,
5152eeb2e5eSGustavo Padovan 			    struct exynos_drm_plane *plane)
516d8408326SSeung-Woo Kim {
5170114f404SMarek Szyprowski 	struct exynos_drm_plane_state *state =
5180114f404SMarek Szyprowski 				to_exynos_plane_state(plane->base.state);
5190114f404SMarek Szyprowski 	struct drm_framebuffer *fb = state->base.fb;
520e47726a1SMarek Szyprowski 	unsigned int priority = state->base.normalized_zpos + 1;
521d8408326SSeung-Woo Kim 	unsigned long flags;
522d8408326SSeung-Woo Kim 	dma_addr_t luma_addr[2], chroma_addr[2];
5230f752694STobias Jakobi 	bool is_tiled, is_nv21;
524d8408326SSeung-Woo Kim 	u32 val;
525d8408326SSeung-Woo Kim 
5260f752694STobias Jakobi 	is_nv21 = (fb->format->format == DRM_FORMAT_NV21);
5270f752694STobias Jakobi 	is_tiled = (fb->modifier == DRM_FORMAT_MOD_SAMSUNG_64_32_TILE);
528f40031c2STobias Jakobi 
5290488f50eSMarek Szyprowski 	luma_addr[0] = exynos_drm_fb_dma_addr(fb, 0);
5300488f50eSMarek Szyprowski 	chroma_addr[0] = exynos_drm_fb_dma_addr(fb, 1);
531d8408326SSeung-Woo Kim 
53271469944SAndrzej Hajda 	if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)) {
5330f752694STobias Jakobi 		if (is_tiled) {
534d8408326SSeung-Woo Kim 			luma_addr[1] = luma_addr[0] + 0x40;
535d8408326SSeung-Woo Kim 			chroma_addr[1] = chroma_addr[0] + 0x40;
536d8408326SSeung-Woo Kim 		} else {
5372eeb2e5eSGustavo Padovan 			luma_addr[1] = luma_addr[0] + fb->pitches[0];
5380ccc1c8fSTobias Jakobi 			chroma_addr[1] = chroma_addr[0] + fb->pitches[1];
539d8408326SSeung-Woo Kim 		}
540d8408326SSeung-Woo Kim 	} else {
541d8408326SSeung-Woo Kim 		luma_addr[1] = 0;
542d8408326SSeung-Woo Kim 		chroma_addr[1] = 0;
543d8408326SSeung-Woo Kim 	}
544d8408326SSeung-Woo Kim 
545524c59f1SAndrzej Hajda 	spin_lock_irqsave(&ctx->reg_slock, flags);
546d8408326SSeung-Woo Kim 
547d8408326SSeung-Woo Kim 	/* interlace or progressive scan mode */
548adeb6f44STobias Jakobi 	val = (test_bit(MXR_BIT_INTERLACE, &ctx->flags) ? ~0 : 0);
549524c59f1SAndrzej Hajda 	vp_reg_writemask(ctx, VP_MODE, val, VP_MODE_LINE_SKIP);
550d8408326SSeung-Woo Kim 
551d8408326SSeung-Woo Kim 	/* setup format */
5520f752694STobias Jakobi 	val = (is_nv21 ? VP_MODE_NV21 : VP_MODE_NV12);
5530f752694STobias Jakobi 	val |= (is_tiled ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
554524c59f1SAndrzej Hajda 	vp_reg_writemask(ctx, VP_MODE, val, VP_MODE_FMT_MASK);
555d8408326SSeung-Woo Kim 
556d8408326SSeung-Woo Kim 	/* setting size of input image */
557524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_IMG_SIZE_Y, VP_IMG_HSIZE(fb->pitches[0]) |
5582eeb2e5eSGustavo Padovan 		VP_IMG_VSIZE(fb->height));
559dc500cfbSTobias Jakobi 	/* chroma plane for NV12/NV21 is half the height of the luma plane */
5600ccc1c8fSTobias Jakobi 	vp_reg_write(ctx, VP_IMG_SIZE_C, VP_IMG_HSIZE(fb->pitches[1]) |
5612eeb2e5eSGustavo Padovan 		VP_IMG_VSIZE(fb->height / 2));
562d8408326SSeung-Woo Kim 
563524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_SRC_WIDTH, state->src.w);
564524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_SRC_H_POSITION,
5650114f404SMarek Szyprowski 			VP_SRC_H_POSITION_VAL(state->src.x));
566524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_DST_WIDTH, state->crtc.w);
567524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_DST_H_POSITION, state->crtc.x);
5680ccc1c8fSTobias Jakobi 
569adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)) {
5700ccc1c8fSTobias Jakobi 		vp_reg_write(ctx, VP_SRC_HEIGHT, state->src.h / 2);
5710ccc1c8fSTobias Jakobi 		vp_reg_write(ctx, VP_SRC_V_POSITION, state->src.y / 2);
572524c59f1SAndrzej Hajda 		vp_reg_write(ctx, VP_DST_HEIGHT, state->crtc.h / 2);
573524c59f1SAndrzej Hajda 		vp_reg_write(ctx, VP_DST_V_POSITION, state->crtc.y / 2);
574d8408326SSeung-Woo Kim 	} else {
5750ccc1c8fSTobias Jakobi 		vp_reg_write(ctx, VP_SRC_HEIGHT, state->src.h);
5760ccc1c8fSTobias Jakobi 		vp_reg_write(ctx, VP_SRC_V_POSITION, state->src.y);
577524c59f1SAndrzej Hajda 		vp_reg_write(ctx, VP_DST_HEIGHT, state->crtc.h);
578524c59f1SAndrzej Hajda 		vp_reg_write(ctx, VP_DST_V_POSITION, state->crtc.y);
579d8408326SSeung-Woo Kim 	}
580d8408326SSeung-Woo Kim 
581524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_H_RATIO, state->h_ratio);
582524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_V_RATIO, state->v_ratio);
583d8408326SSeung-Woo Kim 
584524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
585d8408326SSeung-Woo Kim 
586d8408326SSeung-Woo Kim 	/* set buffer address to vp */
587524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_TOP_Y_PTR, luma_addr[0]);
588524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_BOT_Y_PTR, luma_addr[1]);
589524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_TOP_C_PTR, chroma_addr[0]);
590524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_BOT_C_PTR, chroma_addr[1]);
591d8408326SSeung-Woo Kim 
592e47726a1SMarek Szyprowski 	mixer_cfg_layer(ctx, plane->index, priority, true);
5936ac99a32SChristoph Manszewski 	mixer_cfg_vp_blend(ctx, state->base.alpha);
594d8408326SSeung-Woo Kim 
595524c59f1SAndrzej Hajda 	spin_unlock_irqrestore(&ctx->reg_slock, flags);
596d8408326SSeung-Woo Kim 
597c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
598d8408326SSeung-Woo Kim 	vp_regs_dump(ctx);
599d8408326SSeung-Woo Kim }
600d8408326SSeung-Woo Kim 
6012eeb2e5eSGustavo Padovan static void mixer_graph_buffer(struct mixer_context *ctx,
6022eeb2e5eSGustavo Padovan 			       struct exynos_drm_plane *plane)
603d8408326SSeung-Woo Kim {
6040114f404SMarek Szyprowski 	struct exynos_drm_plane_state *state =
6050114f404SMarek Szyprowski 				to_exynos_plane_state(plane->base.state);
6060114f404SMarek Szyprowski 	struct drm_framebuffer *fb = state->base.fb;
607e47726a1SMarek Szyprowski 	unsigned int priority = state->base.normalized_zpos + 1;
608d8408326SSeung-Woo Kim 	unsigned long flags;
60940bdfb0aSMarek Szyprowski 	unsigned int win = plane->index;
6102611015cSTobias Jakobi 	unsigned int x_ratio = 0, y_ratio = 0;
6115dff6905STobias Jakobi 	unsigned int dst_x_offset, dst_y_offset;
612482582c0SChristoph Manszewski 	unsigned int pixel_alpha;
613d8408326SSeung-Woo Kim 	dma_addr_t dma_addr;
614d8408326SSeung-Woo Kim 	unsigned int fmt;
615d8408326SSeung-Woo Kim 	u32 val;
616d8408326SSeung-Woo Kim 
617482582c0SChristoph Manszewski 	if (fb->format->has_alpha)
618482582c0SChristoph Manszewski 		pixel_alpha = state->base.pixel_blend_mode;
619482582c0SChristoph Manszewski 	else
620482582c0SChristoph Manszewski 		pixel_alpha = DRM_MODE_BLEND_PIXEL_NONE;
621482582c0SChristoph Manszewski 
622438b74a5SVille Syrjälä 	switch (fb->format->format) {
6237a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB4444:
62426a7af3eSTobias Jakobi 	case DRM_FORMAT_ARGB4444:
6257a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB4444;
6267a57ca7cSTobias Jakobi 		break;
627d8408326SSeung-Woo Kim 
6287a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB1555:
62926a7af3eSTobias Jakobi 	case DRM_FORMAT_ARGB1555:
6307a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB1555;
631d8408326SSeung-Woo Kim 		break;
6327a57ca7cSTobias Jakobi 
6337a57ca7cSTobias Jakobi 	case DRM_FORMAT_RGB565:
6347a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_RGB565;
635d8408326SSeung-Woo Kim 		break;
6367a57ca7cSTobias Jakobi 
6377a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB8888:
6387a57ca7cSTobias Jakobi 	case DRM_FORMAT_ARGB8888:
6391e60d62fSTobias Jakobi 	default:
6407a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB8888;
6417a57ca7cSTobias Jakobi 		break;
642d8408326SSeung-Woo Kim 	}
643d8408326SSeung-Woo Kim 
644e463b069SMarek Szyprowski 	/* ratio is already checked by common plane code */
645e463b069SMarek Szyprowski 	x_ratio = state->h_ratio == (1 << 15);
646e463b069SMarek Szyprowski 	y_ratio = state->v_ratio == (1 << 15);
647d8408326SSeung-Woo Kim 
6480114f404SMarek Szyprowski 	dst_x_offset = state->crtc.x;
6490114f404SMarek Szyprowski 	dst_y_offset = state->crtc.y;
650d8408326SSeung-Woo Kim 
6515dff6905STobias Jakobi 	/* translate dma address base s.t. the source image offset is zero */
6520488f50eSMarek Szyprowski 	dma_addr = exynos_drm_fb_dma_addr(fb, 0)
653272725c7SVille Syrjälä 		+ (state->src.x * fb->format->cpp[0])
6540114f404SMarek Szyprowski 		+ (state->src.y * fb->pitches[0]);
655d8408326SSeung-Woo Kim 
656524c59f1SAndrzej Hajda 	spin_lock_irqsave(&ctx->reg_slock, flags);
657d8408326SSeung-Woo Kim 
658d8408326SSeung-Woo Kim 	/* setup format */
659524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_GRAPHIC_CFG(win),
660d8408326SSeung-Woo Kim 		MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
661d8408326SSeung-Woo Kim 
662d8408326SSeung-Woo Kim 	/* setup geometry */
663524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_GRAPHIC_SPAN(win),
664272725c7SVille Syrjälä 			fb->pitches[0] / fb->format->cpp[0]);
665d8408326SSeung-Woo Kim 
6660114f404SMarek Szyprowski 	val  = MXR_GRP_WH_WIDTH(state->src.w);
6670114f404SMarek Szyprowski 	val |= MXR_GRP_WH_HEIGHT(state->src.h);
668d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_H_SCALE(x_ratio);
669d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_V_SCALE(y_ratio);
670524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_GRAPHIC_WH(win), val);
671d8408326SSeung-Woo Kim 
672d8408326SSeung-Woo Kim 	/* setup offsets in display image */
673d8408326SSeung-Woo Kim 	val  = MXR_GRP_DXY_DX(dst_x_offset);
674d8408326SSeung-Woo Kim 	val |= MXR_GRP_DXY_DY(dst_y_offset);
675524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_GRAPHIC_DXY(win), val);
676d8408326SSeung-Woo Kim 
677d8408326SSeung-Woo Kim 	/* set buffer address to mixer */
678524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_GRAPHIC_BASE(win), dma_addr);
679d8408326SSeung-Woo Kim 
680e47726a1SMarek Szyprowski 	mixer_cfg_layer(ctx, win, priority, true);
6816ac99a32SChristoph Manszewski 	mixer_cfg_gfx_blend(ctx, win, pixel_alpha, state->base.alpha);
682aaf8b49eSRahul Sharma 
683524c59f1SAndrzej Hajda 	spin_unlock_irqrestore(&ctx->reg_slock, flags);
684c0734fbaSTobias Jakobi 
685c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
686d8408326SSeung-Woo Kim }
687d8408326SSeung-Woo Kim 
688d8408326SSeung-Woo Kim static void vp_win_reset(struct mixer_context *ctx)
689d8408326SSeung-Woo Kim {
690a696394cSTobias Jakobi 	unsigned int tries = 100;
691d8408326SSeung-Woo Kim 
692524c59f1SAndrzej Hajda 	vp_reg_write(ctx, VP_SRESET, VP_SRESET_PROCESSING);
6938646dcb8SDan Carpenter 	while (--tries) {
694d8408326SSeung-Woo Kim 		/* waiting until VP_SRESET_PROCESSING is 0 */
695524c59f1SAndrzej Hajda 		if (~vp_reg_read(ctx, VP_SRESET) & VP_SRESET_PROCESSING)
696d8408326SSeung-Woo Kim 			break;
69702b3de43STomasz Stanislawski 		mdelay(10);
698d8408326SSeung-Woo Kim 	}
699d8408326SSeung-Woo Kim 	WARN(tries == 0, "failed to reset Video Processor\n");
700d8408326SSeung-Woo Kim }
701d8408326SSeung-Woo Kim 
702cf8fc4f1SJoonyoung Shim static void mixer_win_reset(struct mixer_context *ctx)
703cf8fc4f1SJoonyoung Shim {
704cf8fc4f1SJoonyoung Shim 	unsigned long flags;
705cf8fc4f1SJoonyoung Shim 
706524c59f1SAndrzej Hajda 	spin_lock_irqsave(&ctx->reg_slock, flags);
707cf8fc4f1SJoonyoung Shim 
708524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
709cf8fc4f1SJoonyoung Shim 
710cf8fc4f1SJoonyoung Shim 	/* set output in RGB888 mode */
711524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
712cf8fc4f1SJoonyoung Shim 
713cf8fc4f1SJoonyoung Shim 	/* 16 beat burst in DMA */
714524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_STATUS, MXR_STATUS_16_BURST,
715cf8fc4f1SJoonyoung Shim 		MXR_STATUS_BURST_MASK);
716cf8fc4f1SJoonyoung Shim 
717a2cb911eSMarek Szyprowski 	/* reset default layer priority */
718524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_LAYER_CFG, 0);
719cf8fc4f1SJoonyoung Shim 
7202a6e4cd5STobias Jakobi 	/* set all background colors to RGB (0,0,0) */
721524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_BG_COLOR0, MXR_YCBCR_VAL(0, 128, 128));
722524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_BG_COLOR1, MXR_YCBCR_VAL(0, 128, 128));
723524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_BG_COLOR2, MXR_YCBCR_VAL(0, 128, 128));
724cf8fc4f1SJoonyoung Shim 
725adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
726cf8fc4f1SJoonyoung Shim 		/* configuration of Video Processor Registers */
727cf8fc4f1SJoonyoung Shim 		vp_win_reset(ctx);
728524c59f1SAndrzej Hajda 		vp_default_filter(ctx);
7291b8e5747SRahul Sharma 	}
730cf8fc4f1SJoonyoung Shim 
731cf8fc4f1SJoonyoung Shim 	/* disable all layers */
732524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
733524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
734adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags))
735524c59f1SAndrzej Hajda 		mixer_reg_writemask(ctx, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
736cf8fc4f1SJoonyoung Shim 
7375dff6905STobias Jakobi 	/* set all source image offsets to zero */
738524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_GRAPHIC_SXY(0), 0);
739524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_GRAPHIC_SXY(1), 0);
7405dff6905STobias Jakobi 
741524c59f1SAndrzej Hajda 	spin_unlock_irqrestore(&ctx->reg_slock, flags);
742cf8fc4f1SJoonyoung Shim }
743cf8fc4f1SJoonyoung Shim 
7444551789fSSean Paul static irqreturn_t mixer_irq_handler(int irq, void *arg)
7454551789fSSean Paul {
7464551789fSSean Paul 	struct mixer_context *ctx = arg;
7476a3b45adSAndrzej Hajda 	u32 val;
7484551789fSSean Paul 
749524c59f1SAndrzej Hajda 	spin_lock(&ctx->reg_slock);
7504551789fSSean Paul 
7514551789fSSean Paul 	/* read interrupt status for handling and clearing flags for VSYNC */
752524c59f1SAndrzej Hajda 	val = mixer_reg_read(ctx, MXR_INT_STATUS);
7534551789fSSean Paul 
7544551789fSSean Paul 	/* handling VSYNC */
7554551789fSSean Paul 	if (val & MXR_INT_STATUS_VSYNC) {
75681a464dfSAndrzej Hajda 		/* vsync interrupt use different bit for read and clear */
75781a464dfSAndrzej Hajda 		val |= MXR_INT_CLEAR_VSYNC;
75881a464dfSAndrzej Hajda 		val &= ~MXR_INT_STATUS_VSYNC;
75981a464dfSAndrzej Hajda 
7604551789fSSean Paul 		/* interlace scan need to check shadow register */
7616a3b45adSAndrzej Hajda 		if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)
7626a3b45adSAndrzej Hajda 		    && !mixer_is_synced(ctx))
7632eced8e9SAndrzej Hajda 			goto out;
7642eced8e9SAndrzej Hajda 
765eafd540aSGustavo Padovan 		drm_crtc_handle_vblank(&ctx->crtc->base);
7664551789fSSean Paul 	}
7674551789fSSean Paul 
7684551789fSSean Paul out:
7694551789fSSean Paul 	/* clear interrupts */
770524c59f1SAndrzej Hajda 	mixer_reg_write(ctx, MXR_INT_STATUS, val);
7714551789fSSean Paul 
772524c59f1SAndrzej Hajda 	spin_unlock(&ctx->reg_slock);
7734551789fSSean Paul 
7744551789fSSean Paul 	return IRQ_HANDLED;
7754551789fSSean Paul }
7764551789fSSean Paul 
7774551789fSSean Paul static int mixer_resources_init(struct mixer_context *mixer_ctx)
7784551789fSSean Paul {
7794551789fSSean Paul 	struct device *dev = &mixer_ctx->pdev->dev;
7804551789fSSean Paul 	struct resource *res;
7814551789fSSean Paul 	int ret;
7824551789fSSean Paul 
783524c59f1SAndrzej Hajda 	spin_lock_init(&mixer_ctx->reg_slock);
7844551789fSSean Paul 
785524c59f1SAndrzej Hajda 	mixer_ctx->mixer = devm_clk_get(dev, "mixer");
786524c59f1SAndrzej Hajda 	if (IS_ERR(mixer_ctx->mixer)) {
7874551789fSSean Paul 		dev_err(dev, "failed to get clock 'mixer'\n");
7884551789fSSean Paul 		return -ENODEV;
7894551789fSSean Paul 	}
7904551789fSSean Paul 
791524c59f1SAndrzej Hajda 	mixer_ctx->hdmi = devm_clk_get(dev, "hdmi");
792524c59f1SAndrzej Hajda 	if (IS_ERR(mixer_ctx->hdmi)) {
79304427ec5SMarek Szyprowski 		dev_err(dev, "failed to get clock 'hdmi'\n");
794524c59f1SAndrzej Hajda 		return PTR_ERR(mixer_ctx->hdmi);
79504427ec5SMarek Szyprowski 	}
79604427ec5SMarek Szyprowski 
797524c59f1SAndrzej Hajda 	mixer_ctx->sclk_hdmi = devm_clk_get(dev, "sclk_hdmi");
798524c59f1SAndrzej Hajda 	if (IS_ERR(mixer_ctx->sclk_hdmi)) {
7994551789fSSean Paul 		dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
8004551789fSSean Paul 		return -ENODEV;
8014551789fSSean Paul 	}
8024551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 0);
8034551789fSSean Paul 	if (res == NULL) {
8044551789fSSean Paul 		dev_err(dev, "get memory resource failed.\n");
8054551789fSSean Paul 		return -ENXIO;
8064551789fSSean Paul 	}
8074551789fSSean Paul 
808524c59f1SAndrzej Hajda 	mixer_ctx->mixer_regs = devm_ioremap(dev, res->start,
8094551789fSSean Paul 							resource_size(res));
810524c59f1SAndrzej Hajda 	if (mixer_ctx->mixer_regs == NULL) {
8114551789fSSean Paul 		dev_err(dev, "register mapping failed.\n");
8124551789fSSean Paul 		return -ENXIO;
8134551789fSSean Paul 	}
8144551789fSSean Paul 
815be52abd4SLad Prabhakar 	ret = platform_get_irq(mixer_ctx->pdev, 0);
816be52abd4SLad Prabhakar 	if (ret < 0)
817be52abd4SLad Prabhakar 		return ret;
818be52abd4SLad Prabhakar 	mixer_ctx->irq = ret;
8194551789fSSean Paul 
820be52abd4SLad Prabhakar 	ret = devm_request_irq(dev, mixer_ctx->irq, mixer_irq_handler,
8214551789fSSean Paul 			       0, "drm_mixer", mixer_ctx);
8224551789fSSean Paul 	if (ret) {
8234551789fSSean Paul 		dev_err(dev, "request interrupt failed.\n");
8244551789fSSean Paul 		return ret;
8254551789fSSean Paul 	}
8264551789fSSean Paul 
8274551789fSSean Paul 	return 0;
8284551789fSSean Paul }
8294551789fSSean Paul 
8304551789fSSean Paul static int vp_resources_init(struct mixer_context *mixer_ctx)
8314551789fSSean Paul {
8324551789fSSean Paul 	struct device *dev = &mixer_ctx->pdev->dev;
8334551789fSSean Paul 	struct resource *res;
8344551789fSSean Paul 
835524c59f1SAndrzej Hajda 	mixer_ctx->vp = devm_clk_get(dev, "vp");
836524c59f1SAndrzej Hajda 	if (IS_ERR(mixer_ctx->vp)) {
8374551789fSSean Paul 		dev_err(dev, "failed to get clock 'vp'\n");
8384551789fSSean Paul 		return -ENODEV;
8394551789fSSean Paul 	}
840ff830c96SMarek Szyprowski 
841adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_HAS_SCLK, &mixer_ctx->flags)) {
842524c59f1SAndrzej Hajda 		mixer_ctx->sclk_mixer = devm_clk_get(dev, "sclk_mixer");
843524c59f1SAndrzej Hajda 		if (IS_ERR(mixer_ctx->sclk_mixer)) {
8444551789fSSean Paul 			dev_err(dev, "failed to get clock 'sclk_mixer'\n");
8454551789fSSean Paul 			return -ENODEV;
8464551789fSSean Paul 		}
847524c59f1SAndrzej Hajda 		mixer_ctx->mout_mixer = devm_clk_get(dev, "mout_mixer");
848524c59f1SAndrzej Hajda 		if (IS_ERR(mixer_ctx->mout_mixer)) {
849ff830c96SMarek Szyprowski 			dev_err(dev, "failed to get clock 'mout_mixer'\n");
8504551789fSSean Paul 			return -ENODEV;
8514551789fSSean Paul 		}
8524551789fSSean Paul 
853524c59f1SAndrzej Hajda 		if (mixer_ctx->sclk_hdmi && mixer_ctx->mout_mixer)
854524c59f1SAndrzej Hajda 			clk_set_parent(mixer_ctx->mout_mixer,
855524c59f1SAndrzej Hajda 				       mixer_ctx->sclk_hdmi);
856ff830c96SMarek Szyprowski 	}
8574551789fSSean Paul 
8584551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 1);
8594551789fSSean Paul 	if (res == NULL) {
8604551789fSSean Paul 		dev_err(dev, "get memory resource failed.\n");
8614551789fSSean Paul 		return -ENXIO;
8624551789fSSean Paul 	}
8634551789fSSean Paul 
864524c59f1SAndrzej Hajda 	mixer_ctx->vp_regs = devm_ioremap(dev, res->start,
8654551789fSSean Paul 							resource_size(res));
866524c59f1SAndrzej Hajda 	if (mixer_ctx->vp_regs == NULL) {
8674551789fSSean Paul 		dev_err(dev, "register mapping failed.\n");
8684551789fSSean Paul 		return -ENXIO;
8694551789fSSean Paul 	}
8704551789fSSean Paul 
8714551789fSSean Paul 	return 0;
8724551789fSSean Paul }
8734551789fSSean Paul 
87493bca243SGustavo Padovan static int mixer_initialize(struct mixer_context *mixer_ctx,
875f37cd5e8SInki Dae 			struct drm_device *drm_dev)
8764551789fSSean Paul {
8774551789fSSean Paul 	int ret;
8784551789fSSean Paul 
879eb88e422SGustavo Padovan 	mixer_ctx->drm_dev = drm_dev;
8804551789fSSean Paul 
8814551789fSSean Paul 	/* acquire resources: regs, irqs, clocks */
8824551789fSSean Paul 	ret = mixer_resources_init(mixer_ctx);
8834551789fSSean Paul 	if (ret) {
8846f83d208SInki Dae 		DRM_DEV_ERROR(mixer_ctx->dev,
8856f83d208SInki Dae 			      "mixer_resources_init failed ret=%d\n", ret);
8864551789fSSean Paul 		return ret;
8874551789fSSean Paul 	}
8884551789fSSean Paul 
889adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &mixer_ctx->flags)) {
8904551789fSSean Paul 		/* acquire vp resources: regs, irqs, clocks */
8914551789fSSean Paul 		ret = vp_resources_init(mixer_ctx);
8924551789fSSean Paul 		if (ret) {
8936f83d208SInki Dae 			DRM_DEV_ERROR(mixer_ctx->dev,
8946f83d208SInki Dae 				      "vp_resources_init failed ret=%d\n", ret);
8954551789fSSean Paul 			return ret;
8964551789fSSean Paul 		}
8974551789fSSean Paul 	}
8984551789fSSean Paul 
89907dc3678SMarek Szyprowski 	return exynos_drm_register_dma(drm_dev, mixer_ctx->dev,
90007dc3678SMarek Szyprowski 				       &mixer_ctx->dma_priv);
9011055b39fSInki Dae }
9021055b39fSInki Dae 
90393bca243SGustavo Padovan static void mixer_ctx_remove(struct mixer_context *mixer_ctx)
904d8408326SSeung-Woo Kim {
90507dc3678SMarek Szyprowski 	exynos_drm_unregister_dma(mixer_ctx->drm_dev, mixer_ctx->dev,
90607dc3678SMarek Szyprowski 				  &mixer_ctx->dma_priv);
907f041b257SSean Paul }
908f041b257SSean Paul 
90993bca243SGustavo Padovan static int mixer_enable_vblank(struct exynos_drm_crtc *crtc)
910f041b257SSean Paul {
91193bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
912d8408326SSeung-Woo Kim 
9130df5e4acSAndrzej Hajda 	__set_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
9140df5e4acSAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
915f041b257SSean Paul 		return 0;
916d8408326SSeung-Woo Kim 
917d8408326SSeung-Woo Kim 	/* enable vsync interrupt */
918524c59f1SAndrzej Hajda 	mixer_reg_writemask(mixer_ctx, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
919524c59f1SAndrzej Hajda 	mixer_reg_writemask(mixer_ctx, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
920d8408326SSeung-Woo Kim 
921d8408326SSeung-Woo Kim 	return 0;
922d8408326SSeung-Woo Kim }
923d8408326SSeung-Woo Kim 
92493bca243SGustavo Padovan static void mixer_disable_vblank(struct exynos_drm_crtc *crtc)
925d8408326SSeung-Woo Kim {
92693bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
927d8408326SSeung-Woo Kim 
9280df5e4acSAndrzej Hajda 	__clear_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
9290df5e4acSAndrzej Hajda 
9300df5e4acSAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
931947710c6SAndrzej Hajda 		return;
932947710c6SAndrzej Hajda 
933d8408326SSeung-Woo Kim 	/* disable vsync interrupt */
934524c59f1SAndrzej Hajda 	mixer_reg_writemask(mixer_ctx, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
935524c59f1SAndrzej Hajda 	mixer_reg_writemask(mixer_ctx, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
936d8408326SSeung-Woo Kim }
937d8408326SSeung-Woo Kim 
9383dbaab16SMarek Szyprowski static void mixer_atomic_begin(struct exynos_drm_crtc *crtc)
9393dbaab16SMarek Szyprowski {
9406a3b45adSAndrzej Hajda 	struct mixer_context *ctx = crtc->ctx;
9413dbaab16SMarek Szyprowski 
9426a3b45adSAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &ctx->flags))
9433dbaab16SMarek Szyprowski 		return;
9443dbaab16SMarek Szyprowski 
9456a3b45adSAndrzej Hajda 	if (mixer_wait_for_sync(ctx))
9466a3b45adSAndrzej Hajda 		dev_err(ctx->dev, "timeout waiting for VSYNC\n");
9476a3b45adSAndrzej Hajda 	mixer_disable_sync(ctx);
9483dbaab16SMarek Szyprowski }
9493dbaab16SMarek Szyprowski 
9501e1d1393SGustavo Padovan static void mixer_update_plane(struct exynos_drm_crtc *crtc,
9511e1d1393SGustavo Padovan 			       struct exynos_drm_plane *plane)
952d8408326SSeung-Woo Kim {
95393bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
954d8408326SSeung-Woo Kim 
9556be90056SInki Dae 	DRM_DEV_DEBUG_KMS(mixer_ctx->dev, "win: %d\n", plane->index);
956d8408326SSeung-Woo Kim 
957a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
958dda9012bSShirish S 		return;
959dda9012bSShirish S 
9605e68fef2SMarek Szyprowski 	if (plane->index == VP_DEFAULT_WIN)
9612eeb2e5eSGustavo Padovan 		vp_video_buffer(mixer_ctx, plane);
962d8408326SSeung-Woo Kim 	else
9632eeb2e5eSGustavo Padovan 		mixer_graph_buffer(mixer_ctx, plane);
964d8408326SSeung-Woo Kim }
965d8408326SSeung-Woo Kim 
9661e1d1393SGustavo Padovan static void mixer_disable_plane(struct exynos_drm_crtc *crtc,
9671e1d1393SGustavo Padovan 				struct exynos_drm_plane *plane)
968d8408326SSeung-Woo Kim {
96993bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
970d8408326SSeung-Woo Kim 	unsigned long flags;
971d8408326SSeung-Woo Kim 
9726be90056SInki Dae 	DRM_DEV_DEBUG_KMS(mixer_ctx->dev, "win: %d\n", plane->index);
973d8408326SSeung-Woo Kim 
974a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
975db43fd16SPrathyush K 		return;
976db43fd16SPrathyush K 
977524c59f1SAndrzej Hajda 	spin_lock_irqsave(&mixer_ctx->reg_slock, flags);
978a2cb911eSMarek Szyprowski 	mixer_cfg_layer(mixer_ctx, plane->index, 0, false);
979524c59f1SAndrzej Hajda 	spin_unlock_irqrestore(&mixer_ctx->reg_slock, flags);
9803dbaab16SMarek Szyprowski }
9813dbaab16SMarek Szyprowski 
9823dbaab16SMarek Szyprowski static void mixer_atomic_flush(struct exynos_drm_crtc *crtc)
9833dbaab16SMarek Szyprowski {
9843dbaab16SMarek Szyprowski 	struct mixer_context *mixer_ctx = crtc->ctx;
9853dbaab16SMarek Szyprowski 
9863dbaab16SMarek Szyprowski 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
9873dbaab16SMarek Szyprowski 		return;
988d8408326SSeung-Woo Kim 
9896a3b45adSAndrzej Hajda 	mixer_enable_sync(mixer_ctx);
990a392276dSAndrzej Hajda 	exynos_crtc_handle_event(crtc);
991d8408326SSeung-Woo Kim }
992d8408326SSeung-Woo Kim 
99311f95489SInki Dae static void mixer_atomic_enable(struct exynos_drm_crtc *crtc)
994db43fd16SPrathyush K {
9953cecda03SGustavo Padovan 	struct mixer_context *ctx = crtc->ctx;
996445d3bedSInki Dae 	int ret;
997db43fd16SPrathyush K 
998a44652e8SAndrzej Hajda 	if (test_bit(MXR_BIT_POWERED, &ctx->flags))
999db43fd16SPrathyush K 		return;
1000db43fd16SPrathyush K 
1001445d3bedSInki Dae 	ret = pm_runtime_resume_and_get(ctx->dev);
1002445d3bedSInki Dae 	if (ret < 0) {
1003445d3bedSInki Dae 		dev_err(ctx->dev, "failed to enable MIXER device.\n");
1004445d3bedSInki Dae 		return;
1005445d3bedSInki Dae 	}
1006af65c804SSean Paul 
1007a121d179SAndrzej Hajda 	exynos_drm_pipe_clk_enable(crtc, true);
1008a121d179SAndrzej Hajda 
10096a3b45adSAndrzej Hajda 	mixer_disable_sync(ctx);
10103dbaab16SMarek Szyprowski 
1011524c59f1SAndrzej Hajda 	mixer_reg_writemask(ctx, MXR_STATUS, ~0, MXR_STATUS_SOFT_RESET);
1012d74ed937SRahul Sharma 
10130df5e4acSAndrzej Hajda 	if (test_bit(MXR_BIT_VSYNC, &ctx->flags)) {
1014524c59f1SAndrzej Hajda 		mixer_reg_writemask(ctx, MXR_INT_STATUS, ~0,
1015524c59f1SAndrzej Hajda 					MXR_INT_CLEAR_VSYNC);
1016524c59f1SAndrzej Hajda 		mixer_reg_writemask(ctx, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
10170df5e4acSAndrzej Hajda 	}
1018db43fd16SPrathyush K 	mixer_win_reset(ctx);
1019ccf034a9SGustavo Padovan 
102071469944SAndrzej Hajda 	mixer_commit(ctx);
102171469944SAndrzej Hajda 
10226a3b45adSAndrzej Hajda 	mixer_enable_sync(ctx);
10233dbaab16SMarek Szyprowski 
1024ccf034a9SGustavo Padovan 	set_bit(MXR_BIT_POWERED, &ctx->flags);
1025db43fd16SPrathyush K }
1026db43fd16SPrathyush K 
102711f95489SInki Dae static void mixer_atomic_disable(struct exynos_drm_crtc *crtc)
1028db43fd16SPrathyush K {
10293cecda03SGustavo Padovan 	struct mixer_context *ctx = crtc->ctx;
1030c329f667SJoonyoung Shim 	int i;
1031db43fd16SPrathyush K 
1032a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &ctx->flags))
1033b4bfa3c7SRahul Sharma 		return;
1034db43fd16SPrathyush K 
1035381be025SRahul Sharma 	mixer_stop(ctx);
1036c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
1037c329f667SJoonyoung Shim 
1038c329f667SJoonyoung Shim 	for (i = 0; i < MIXER_WIN_NR; i++)
10391e1d1393SGustavo Padovan 		mixer_disable_plane(crtc, &ctx->planes[i]);
1040db43fd16SPrathyush K 
1041a121d179SAndrzej Hajda 	exynos_drm_pipe_clk_enable(crtc, false);
1042a121d179SAndrzej Hajda 
1043ccf034a9SGustavo Padovan 	pm_runtime_put(ctx->dev);
1044ccf034a9SGustavo Padovan 
1045a44652e8SAndrzej Hajda 	clear_bit(MXR_BIT_POWERED, &ctx->flags);
1046db43fd16SPrathyush K }
1047db43fd16SPrathyush K 
1048*12612555SNathan Huckleberry static enum drm_mode_status mixer_mode_valid(struct exynos_drm_crtc *crtc,
10496ace38a5SAndrzej Hajda 		const struct drm_display_mode *mode)
1050f041b257SSean Paul {
10516ace38a5SAndrzej Hajda 	struct mixer_context *ctx = crtc->ctx;
10526ace38a5SAndrzej Hajda 	u32 w = mode->hdisplay, h = mode->vdisplay;
1053f041b257SSean Paul 
10546be90056SInki Dae 	DRM_DEV_DEBUG_KMS(ctx->dev, "xres=%d, yres=%d, refresh=%d, intl=%d\n",
10550425662fSVille Syrjälä 			  w, h, drm_mode_vrefresh(mode),
10566be90056SInki Dae 			  !!(mode->flags & DRM_MODE_FLAG_INTERLACE));
1057f041b257SSean Paul 
10586ace38a5SAndrzej Hajda 	if (ctx->mxr_ver == MXR_VER_128_0_0_184)
10596ace38a5SAndrzej Hajda 		return MODE_OK;
1060f041b257SSean Paul 
1061f041b257SSean Paul 	if ((w >= 464 && w <= 720 && h >= 261 && h <= 576) ||
1062f041b257SSean Paul 	    (w >= 1024 && w <= 1280 && h >= 576 && h <= 720) ||
1063f041b257SSean Paul 	    (w >= 1664 && w <= 1920 && h >= 936 && h <= 1080))
10646ace38a5SAndrzej Hajda 		return MODE_OK;
1065f041b257SSean Paul 
1066ae58c03eSDaniel Drake 	if ((w == 1024 && h == 768) ||
1067ae58c03eSDaniel Drake 	    (w == 1366 && h == 768) ||
1068ae58c03eSDaniel Drake 	    (w == 1280 && h == 1024))
10690900673eSAndrzej Hajda 		return MODE_OK;
10700900673eSAndrzej Hajda 
10716ace38a5SAndrzej Hajda 	return MODE_BAD;
1072f041b257SSean Paul }
1073f041b257SSean Paul 
1074acc8bf04SAndrzej Hajda static bool mixer_mode_fixup(struct exynos_drm_crtc *crtc,
1075acc8bf04SAndrzej Hajda 		   const struct drm_display_mode *mode,
1076acc8bf04SAndrzej Hajda 		   struct drm_display_mode *adjusted_mode)
1077acc8bf04SAndrzej Hajda {
1078acc8bf04SAndrzej Hajda 	struct mixer_context *ctx = crtc->ctx;
1079acc8bf04SAndrzej Hajda 	int width = mode->hdisplay, height = mode->vdisplay, i;
1080acc8bf04SAndrzej Hajda 
10815a884be5SKrzysztof Wilczynski 	static const struct {
1082acc8bf04SAndrzej Hajda 		int hdisplay, vdisplay, htotal, vtotal, scan_val;
10835a884be5SKrzysztof Wilczynski 	} modes[] = {
1084acc8bf04SAndrzej Hajda 		{ 720, 480, 858, 525, MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD },
1085acc8bf04SAndrzej Hajda 		{ 720, 576, 864, 625, MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD },
1086acc8bf04SAndrzej Hajda 		{ 1280, 720, 1650, 750, MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD },
1087acc8bf04SAndrzej Hajda 		{ 1920, 1080, 2200, 1125, MXR_CFG_SCAN_HD_1080 |
1088acc8bf04SAndrzej Hajda 						MXR_CFG_SCAN_HD }
1089acc8bf04SAndrzej Hajda 	};
1090acc8bf04SAndrzej Hajda 
1091acc8bf04SAndrzej Hajda 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1092acc8bf04SAndrzej Hajda 		__set_bit(MXR_BIT_INTERLACE, &ctx->flags);
1093acc8bf04SAndrzej Hajda 	else
1094acc8bf04SAndrzej Hajda 		__clear_bit(MXR_BIT_INTERLACE, &ctx->flags);
1095acc8bf04SAndrzej Hajda 
1096acc8bf04SAndrzej Hajda 	if (ctx->mxr_ver == MXR_VER_128_0_0_184)
1097acc8bf04SAndrzej Hajda 		return true;
1098acc8bf04SAndrzej Hajda 
1099acc8bf04SAndrzej Hajda 	for (i = 0; i < ARRAY_SIZE(modes); ++i)
1100acc8bf04SAndrzej Hajda 		if (width <= modes[i].hdisplay && height <= modes[i].vdisplay) {
1101acc8bf04SAndrzej Hajda 			ctx->scan_value = modes[i].scan_val;
1102acc8bf04SAndrzej Hajda 			if (width < modes[i].hdisplay ||
1103acc8bf04SAndrzej Hajda 			    height < modes[i].vdisplay) {
1104acc8bf04SAndrzej Hajda 				adjusted_mode->hdisplay = modes[i].hdisplay;
1105acc8bf04SAndrzej Hajda 				adjusted_mode->hsync_start = modes[i].hdisplay;
1106acc8bf04SAndrzej Hajda 				adjusted_mode->hsync_end = modes[i].htotal;
1107acc8bf04SAndrzej Hajda 				adjusted_mode->htotal = modes[i].htotal;
1108acc8bf04SAndrzej Hajda 				adjusted_mode->vdisplay = modes[i].vdisplay;
1109acc8bf04SAndrzej Hajda 				adjusted_mode->vsync_start = modes[i].vdisplay;
1110acc8bf04SAndrzej Hajda 				adjusted_mode->vsync_end = modes[i].vtotal;
1111acc8bf04SAndrzej Hajda 				adjusted_mode->vtotal = modes[i].vtotal;
1112acc8bf04SAndrzej Hajda 			}
1113acc8bf04SAndrzej Hajda 
1114acc8bf04SAndrzej Hajda 			return true;
1115acc8bf04SAndrzej Hajda 		}
1116acc8bf04SAndrzej Hajda 
1117acc8bf04SAndrzej Hajda 	return false;
1118acc8bf04SAndrzej Hajda }
1119acc8bf04SAndrzej Hajda 
1120f3aaf762SKrzysztof Kozlowski static const struct exynos_drm_crtc_ops mixer_crtc_ops = {
112111f95489SInki Dae 	.atomic_enable		= mixer_atomic_enable,
112211f95489SInki Dae 	.atomic_disable		= mixer_atomic_disable,
1123d8408326SSeung-Woo Kim 	.enable_vblank		= mixer_enable_vblank,
1124d8408326SSeung-Woo Kim 	.disable_vblank		= mixer_disable_vblank,
11253dbaab16SMarek Szyprowski 	.atomic_begin		= mixer_atomic_begin,
11269cc7610aSGustavo Padovan 	.update_plane		= mixer_update_plane,
11279cc7610aSGustavo Padovan 	.disable_plane		= mixer_disable_plane,
11283dbaab16SMarek Szyprowski 	.atomic_flush		= mixer_atomic_flush,
11296ace38a5SAndrzej Hajda 	.mode_valid		= mixer_mode_valid,
1130acc8bf04SAndrzej Hajda 	.mode_fixup		= mixer_mode_fixup,
1131f041b257SSean Paul };
11320ea6822fSRahul Sharma 
11335e6cc1c5SArvind Yadav static const struct mixer_drv_data exynos5420_mxr_drv_data = {
1134def5e095SRahul Sharma 	.version = MXR_VER_128_0_0_184,
1135def5e095SRahul Sharma 	.is_vp_enabled = 0,
1136def5e095SRahul Sharma };
1137def5e095SRahul Sharma 
11385e6cc1c5SArvind Yadav static const struct mixer_drv_data exynos5250_mxr_drv_data = {
1139aaf8b49eSRahul Sharma 	.version = MXR_VER_16_0_33_0,
1140aaf8b49eSRahul Sharma 	.is_vp_enabled = 0,
1141aaf8b49eSRahul Sharma };
1142aaf8b49eSRahul Sharma 
11435e6cc1c5SArvind Yadav static const struct mixer_drv_data exynos4212_mxr_drv_data = {
1144ff830c96SMarek Szyprowski 	.version = MXR_VER_0_0_0_16,
1145ff830c96SMarek Szyprowski 	.is_vp_enabled = 1,
1146ff830c96SMarek Szyprowski };
1147ff830c96SMarek Szyprowski 
11485e6cc1c5SArvind Yadav static const struct mixer_drv_data exynos4210_mxr_drv_data = {
11491e123441SRahul Sharma 	.version = MXR_VER_0_0_0_16,
11501b8e5747SRahul Sharma 	.is_vp_enabled = 1,
1151ff830c96SMarek Szyprowski 	.has_sclk = 1,
11521e123441SRahul Sharma };
11531e123441SRahul Sharma 
11545e6cc1c5SArvind Yadav static const struct of_device_id mixer_match_types[] = {
1155aaf8b49eSRahul Sharma 	{
1156ff830c96SMarek Szyprowski 		.compatible = "samsung,exynos4210-mixer",
1157ff830c96SMarek Szyprowski 		.data	= &exynos4210_mxr_drv_data,
1158ff830c96SMarek Szyprowski 	}, {
1159ff830c96SMarek Szyprowski 		.compatible = "samsung,exynos4212-mixer",
1160ff830c96SMarek Szyprowski 		.data	= &exynos4212_mxr_drv_data,
1161ff830c96SMarek Szyprowski 	}, {
1162aaf8b49eSRahul Sharma 		.compatible = "samsung,exynos5-mixer",
1163cc57caf0SRahul Sharma 		.data	= &exynos5250_mxr_drv_data,
1164cc57caf0SRahul Sharma 	}, {
1165cc57caf0SRahul Sharma 		.compatible = "samsung,exynos5250-mixer",
1166cc57caf0SRahul Sharma 		.data	= &exynos5250_mxr_drv_data,
1167aaf8b49eSRahul Sharma 	}, {
1168def5e095SRahul Sharma 		.compatible = "samsung,exynos5420-mixer",
1169def5e095SRahul Sharma 		.data	= &exynos5420_mxr_drv_data,
1170def5e095SRahul Sharma 	}, {
11711e123441SRahul Sharma 		/* end node */
11721e123441SRahul Sharma 	}
11731e123441SRahul Sharma };
117439b58a39SSjoerd Simons MODULE_DEVICE_TABLE(of, mixer_match_types);
11751e123441SRahul Sharma 
1176f37cd5e8SInki Dae static int mixer_bind(struct device *dev, struct device *manager, void *data)
1177d8408326SSeung-Woo Kim {
11788103ef1bSAndrzej Hajda 	struct mixer_context *ctx = dev_get_drvdata(dev);
1179f37cd5e8SInki Dae 	struct drm_device *drm_dev = data;
11807ee14cdcSGustavo Padovan 	struct exynos_drm_plane *exynos_plane;
1181fd2d2fc2SMarek Szyprowski 	unsigned int i;
11826e2a3b66SGustavo Padovan 	int ret;
1183d8408326SSeung-Woo Kim 
1184e2dc3f72SAlban Browaeys 	ret = mixer_initialize(ctx, drm_dev);
1185e2dc3f72SAlban Browaeys 	if (ret)
1186e2dc3f72SAlban Browaeys 		return ret;
1187e2dc3f72SAlban Browaeys 
1188fd2d2fc2SMarek Szyprowski 	for (i = 0; i < MIXER_WIN_NR; i++) {
1189adeb6f44STobias Jakobi 		if (i == VP_DEFAULT_WIN && !test_bit(MXR_BIT_VP_ENABLED,
1190adeb6f44STobias Jakobi 						     &ctx->flags))
1191ab144201SMarek Szyprowski 			continue;
1192ab144201SMarek Szyprowski 
119340bdfb0aSMarek Szyprowski 		ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
11942c82607bSAndrzej Hajda 					&plane_configs[i]);
11957ee14cdcSGustavo Padovan 		if (ret)
11967ee14cdcSGustavo Padovan 			return ret;
11977ee14cdcSGustavo Padovan 	}
11987ee14cdcSGustavo Padovan 
11995d3d0995SGustavo Padovan 	exynos_plane = &ctx->planes[DEFAULT_WIN];
12007ee14cdcSGustavo Padovan 	ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
1201d644951cSAndrzej Hajda 			EXYNOS_DISPLAY_TYPE_HDMI, &mixer_crtc_ops, ctx);
120293bca243SGustavo Padovan 	if (IS_ERR(ctx->crtc)) {
1203e2dc3f72SAlban Browaeys 		mixer_ctx_remove(ctx);
120493bca243SGustavo Padovan 		ret = PTR_ERR(ctx->crtc);
120593bca243SGustavo Padovan 		goto free_ctx;
12068103ef1bSAndrzej Hajda 	}
12078103ef1bSAndrzej Hajda 
12088103ef1bSAndrzej Hajda 	return 0;
120993bca243SGustavo Padovan 
121093bca243SGustavo Padovan free_ctx:
121193bca243SGustavo Padovan 	devm_kfree(dev, ctx);
121293bca243SGustavo Padovan 	return ret;
12138103ef1bSAndrzej Hajda }
12148103ef1bSAndrzej Hajda 
12158103ef1bSAndrzej Hajda static void mixer_unbind(struct device *dev, struct device *master, void *data)
12168103ef1bSAndrzej Hajda {
12178103ef1bSAndrzej Hajda 	struct mixer_context *ctx = dev_get_drvdata(dev);
12188103ef1bSAndrzej Hajda 
121993bca243SGustavo Padovan 	mixer_ctx_remove(ctx);
12208103ef1bSAndrzej Hajda }
12218103ef1bSAndrzej Hajda 
12228103ef1bSAndrzej Hajda static const struct component_ops mixer_component_ops = {
12238103ef1bSAndrzej Hajda 	.bind	= mixer_bind,
12248103ef1bSAndrzej Hajda 	.unbind	= mixer_unbind,
12258103ef1bSAndrzej Hajda };
12268103ef1bSAndrzej Hajda 
12278103ef1bSAndrzej Hajda static int mixer_probe(struct platform_device *pdev)
12288103ef1bSAndrzej Hajda {
12298103ef1bSAndrzej Hajda 	struct device *dev = &pdev->dev;
123048f6155aSMarek Szyprowski 	const struct mixer_drv_data *drv;
12318103ef1bSAndrzej Hajda 	struct mixer_context *ctx;
12328103ef1bSAndrzej Hajda 	int ret;
1233d8408326SSeung-Woo Kim 
1234f041b257SSean Paul 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1235f041b257SSean Paul 	if (!ctx) {
12366f83d208SInki Dae 		DRM_DEV_ERROR(dev, "failed to alloc mixer context.\n");
1237d8408326SSeung-Woo Kim 		return -ENOMEM;
1238f041b257SSean Paul 	}
1239d8408326SSeung-Woo Kim 
124048f6155aSMarek Szyprowski 	drv = of_device_get_match_data(dev);
1241aaf8b49eSRahul Sharma 
12424551789fSSean Paul 	ctx->pdev = pdev;
1243d873ab99SSeung-Woo Kim 	ctx->dev = dev;
12441e123441SRahul Sharma 	ctx->mxr_ver = drv->version;
1245d8408326SSeung-Woo Kim 
1246adeb6f44STobias Jakobi 	if (drv->is_vp_enabled)
1247adeb6f44STobias Jakobi 		__set_bit(MXR_BIT_VP_ENABLED, &ctx->flags);
1248adeb6f44STobias Jakobi 	if (drv->has_sclk)
1249adeb6f44STobias Jakobi 		__set_bit(MXR_BIT_HAS_SCLK, &ctx->flags);
1250adeb6f44STobias Jakobi 
12518103ef1bSAndrzej Hajda 	platform_set_drvdata(pdev, ctx);
1252df5225bcSInki Dae 
12538103ef1bSAndrzej Hajda 	pm_runtime_enable(dev);
1254df5225bcSInki Dae 
1255fda02214SMarek Szyprowski 	ret = component_add(&pdev->dev, &mixer_component_ops);
1256fda02214SMarek Szyprowski 	if (ret)
1257fda02214SMarek Szyprowski 		pm_runtime_disable(dev);
1258fda02214SMarek Szyprowski 
1259df5225bcSInki Dae 	return ret;
1260f37cd5e8SInki Dae }
1261f37cd5e8SInki Dae 
1262d8408326SSeung-Woo Kim static int mixer_remove(struct platform_device *pdev)
1263d8408326SSeung-Woo Kim {
12648103ef1bSAndrzej Hajda 	pm_runtime_disable(&pdev->dev);
12658103ef1bSAndrzej Hajda 
1266df5225bcSInki Dae 	component_del(&pdev->dev, &mixer_component_ops);
1267df5225bcSInki Dae 
1268d8408326SSeung-Woo Kim 	return 0;
1269d8408326SSeung-Woo Kim }
1270d8408326SSeung-Woo Kim 
1271e0fea7e7SArnd Bergmann static int __maybe_unused exynos_mixer_suspend(struct device *dev)
1272ccf034a9SGustavo Padovan {
1273ccf034a9SGustavo Padovan 	struct mixer_context *ctx = dev_get_drvdata(dev);
1274ccf034a9SGustavo Padovan 
1275524c59f1SAndrzej Hajda 	clk_disable_unprepare(ctx->hdmi);
1276524c59f1SAndrzej Hajda 	clk_disable_unprepare(ctx->mixer);
1277adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
1278524c59f1SAndrzej Hajda 		clk_disable_unprepare(ctx->vp);
1279adeb6f44STobias Jakobi 		if (test_bit(MXR_BIT_HAS_SCLK, &ctx->flags))
1280524c59f1SAndrzej Hajda 			clk_disable_unprepare(ctx->sclk_mixer);
1281ccf034a9SGustavo Padovan 	}
1282ccf034a9SGustavo Padovan 
1283ccf034a9SGustavo Padovan 	return 0;
1284ccf034a9SGustavo Padovan }
1285ccf034a9SGustavo Padovan 
1286e0fea7e7SArnd Bergmann static int __maybe_unused exynos_mixer_resume(struct device *dev)
1287ccf034a9SGustavo Padovan {
1288ccf034a9SGustavo Padovan 	struct mixer_context *ctx = dev_get_drvdata(dev);
1289ccf034a9SGustavo Padovan 	int ret;
1290ccf034a9SGustavo Padovan 
1291524c59f1SAndrzej Hajda 	ret = clk_prepare_enable(ctx->mixer);
1292ccf034a9SGustavo Padovan 	if (ret < 0) {
12936f83d208SInki Dae 		DRM_DEV_ERROR(ctx->dev,
12946f83d208SInki Dae 			      "Failed to prepare_enable the mixer clk [%d]\n",
12956f83d208SInki Dae 			      ret);
1296ccf034a9SGustavo Padovan 		return ret;
1297ccf034a9SGustavo Padovan 	}
1298524c59f1SAndrzej Hajda 	ret = clk_prepare_enable(ctx->hdmi);
1299ccf034a9SGustavo Padovan 	if (ret < 0) {
13006f83d208SInki Dae 		DRM_DEV_ERROR(dev,
13016f83d208SInki Dae 			      "Failed to prepare_enable the hdmi clk [%d]\n",
13026f83d208SInki Dae 			      ret);
1303ccf034a9SGustavo Padovan 		return ret;
1304ccf034a9SGustavo Padovan 	}
1305adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
1306524c59f1SAndrzej Hajda 		ret = clk_prepare_enable(ctx->vp);
1307ccf034a9SGustavo Padovan 		if (ret < 0) {
13086f83d208SInki Dae 			DRM_DEV_ERROR(dev,
13096f83d208SInki Dae 				      "Failed to prepare_enable the vp clk [%d]\n",
1310ccf034a9SGustavo Padovan 				      ret);
1311ccf034a9SGustavo Padovan 			return ret;
1312ccf034a9SGustavo Padovan 		}
1313adeb6f44STobias Jakobi 		if (test_bit(MXR_BIT_HAS_SCLK, &ctx->flags)) {
1314524c59f1SAndrzej Hajda 			ret = clk_prepare_enable(ctx->sclk_mixer);
1315ccf034a9SGustavo Padovan 			if (ret < 0) {
13166f83d208SInki Dae 				DRM_DEV_ERROR(dev,
13176f83d208SInki Dae 					   "Failed to prepare_enable the " \
1318ccf034a9SGustavo Padovan 					   "sclk_mixer clk [%d]\n",
1319ccf034a9SGustavo Padovan 					   ret);
1320ccf034a9SGustavo Padovan 				return ret;
1321ccf034a9SGustavo Padovan 			}
1322ccf034a9SGustavo Padovan 		}
1323ccf034a9SGustavo Padovan 	}
1324ccf034a9SGustavo Padovan 
1325ccf034a9SGustavo Padovan 	return 0;
1326ccf034a9SGustavo Padovan }
1327ccf034a9SGustavo Padovan 
1328ccf034a9SGustavo Padovan static const struct dev_pm_ops exynos_mixer_pm_ops = {
1329ccf034a9SGustavo Padovan 	SET_RUNTIME_PM_OPS(exynos_mixer_suspend, exynos_mixer_resume, NULL)
13307e915746SMarek Szyprowski 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
13317e915746SMarek Szyprowski 				pm_runtime_force_resume)
1332ccf034a9SGustavo Padovan };
1333ccf034a9SGustavo Padovan 
1334d8408326SSeung-Woo Kim struct platform_driver mixer_driver = {
1335d8408326SSeung-Woo Kim 	.driver = {
1336aaf8b49eSRahul Sharma 		.name = "exynos-mixer",
1337d8408326SSeung-Woo Kim 		.owner = THIS_MODULE,
1338ccf034a9SGustavo Padovan 		.pm = &exynos_mixer_pm_ops,
1339aaf8b49eSRahul Sharma 		.of_match_table = mixer_match_types,
1340d8408326SSeung-Woo Kim 	},
1341d8408326SSeung-Woo Kim 	.probe = mixer_probe,
134256550d94SGreg Kroah-Hartman 	.remove = mixer_remove,
1343d8408326SSeung-Woo Kim };
1344