xref: /linux/drivers/gpu/drm/exynos/exynos_mixer.c (revision 3fc40ca9695c6a10c528f80306b1731128eca8c1)
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>
3448f6155aSMarek Szyprowski #include <linux/of_device.h>
35f37cd5e8SInki Dae #include <linux/component.h>
36d8408326SSeung-Woo Kim 
37d8408326SSeung-Woo Kim #include <drm/exynos_drm.h>
38d8408326SSeung-Woo Kim 
39d8408326SSeung-Woo Kim #include "exynos_drm_drv.h"
40663d8766SRahul Sharma #include "exynos_drm_crtc.h"
410488f50eSMarek Szyprowski #include "exynos_drm_fb.h"
427ee14cdcSGustavo Padovan #include "exynos_drm_plane.h"
431055b39fSInki Dae #include "exynos_drm_iommu.h"
4422b21ae6SJoonyoung Shim 
45f041b257SSean Paul #define MIXER_WIN_NR		3
46fbbb1e1aSMarek Szyprowski #define VP_DEFAULT_WIN		2
47d8408326SSeung-Woo Kim 
482a6e4cd5STobias Jakobi /*
492a6e4cd5STobias Jakobi  * Mixer color space conversion coefficient triplet.
502a6e4cd5STobias Jakobi  * Used for CSC from RGB to YCbCr.
512a6e4cd5STobias Jakobi  * Each coefficient is a 10-bit fixed point number with
522a6e4cd5STobias Jakobi  * sign and no integer part, i.e.
532a6e4cd5STobias Jakobi  * [0:8] = fractional part (representing a value y = x / 2^9)
542a6e4cd5STobias Jakobi  * [9] = sign
552a6e4cd5STobias Jakobi  * Negative values are encoded with two's complement.
562a6e4cd5STobias Jakobi  */
572a6e4cd5STobias Jakobi #define MXR_CSC_C(x) ((int)((x) * 512.0) & 0x3ff)
582a6e4cd5STobias Jakobi #define MXR_CSC_CT(a0, a1, a2) \
592a6e4cd5STobias Jakobi   ((MXR_CSC_C(a0) << 20) | (MXR_CSC_C(a1) << 10) | (MXR_CSC_C(a2) << 0))
602a6e4cd5STobias Jakobi 
612a6e4cd5STobias Jakobi /* YCbCr value, used for mixer background color configuration. */
622a6e4cd5STobias Jakobi #define MXR_YCBCR_VAL(y, cb, cr) (((y) << 16) | ((cb) << 8) | ((cr) << 0))
632a6e4cd5STobias Jakobi 
647a57ca7cSTobias Jakobi /* The pixelformats that are natively supported by the mixer. */
657a57ca7cSTobias Jakobi #define MXR_FORMAT_RGB565	4
667a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB1555	5
677a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB4444	6
687a57ca7cSTobias Jakobi #define MXR_FORMAT_ARGB8888	7
697a57ca7cSTobias Jakobi 
7022b21ae6SJoonyoung Shim struct mixer_resources {
7122b21ae6SJoonyoung Shim 	int			irq;
7222b21ae6SJoonyoung Shim 	void __iomem		*mixer_regs;
7322b21ae6SJoonyoung Shim 	void __iomem		*vp_regs;
7422b21ae6SJoonyoung Shim 	spinlock_t		reg_slock;
7522b21ae6SJoonyoung Shim 	struct clk		*mixer;
7622b21ae6SJoonyoung Shim 	struct clk		*vp;
7704427ec5SMarek Szyprowski 	struct clk		*hdmi;
7822b21ae6SJoonyoung Shim 	struct clk		*sclk_mixer;
7922b21ae6SJoonyoung Shim 	struct clk		*sclk_hdmi;
80ff830c96SMarek Szyprowski 	struct clk		*mout_mixer;
8122b21ae6SJoonyoung Shim };
8222b21ae6SJoonyoung Shim 
831e123441SRahul Sharma enum mixer_version_id {
841e123441SRahul Sharma 	MXR_VER_0_0_0_16,
851e123441SRahul Sharma 	MXR_VER_16_0_33_0,
86def5e095SRahul Sharma 	MXR_VER_128_0_0_184,
871e123441SRahul Sharma };
881e123441SRahul Sharma 
89a44652e8SAndrzej Hajda enum mixer_flag_bits {
90a44652e8SAndrzej Hajda 	MXR_BIT_POWERED,
910df5e4acSAndrzej Hajda 	MXR_BIT_VSYNC,
92adeb6f44STobias Jakobi 	MXR_BIT_INTERLACE,
93adeb6f44STobias Jakobi 	MXR_BIT_VP_ENABLED,
94adeb6f44STobias Jakobi 	MXR_BIT_HAS_SCLK,
95a44652e8SAndrzej Hajda };
96a44652e8SAndrzej Hajda 
97fbbb1e1aSMarek Szyprowski static const uint32_t mixer_formats[] = {
98fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_XRGB4444,
9926a7af3eSTobias Jakobi 	DRM_FORMAT_ARGB4444,
100fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_XRGB1555,
10126a7af3eSTobias Jakobi 	DRM_FORMAT_ARGB1555,
102fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_RGB565,
103fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_XRGB8888,
104fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_ARGB8888,
105fbbb1e1aSMarek Szyprowski };
106fbbb1e1aSMarek Szyprowski 
107fbbb1e1aSMarek Szyprowski static const uint32_t vp_formats[] = {
108fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_NV12,
109fbbb1e1aSMarek Szyprowski 	DRM_FORMAT_NV21,
110fbbb1e1aSMarek Szyprowski };
111fbbb1e1aSMarek Szyprowski 
11222b21ae6SJoonyoung Shim struct mixer_context {
1134551789fSSean Paul 	struct platform_device *pdev;
114cf8fc4f1SJoonyoung Shim 	struct device		*dev;
1151055b39fSInki Dae 	struct drm_device	*drm_dev;
11693bca243SGustavo Padovan 	struct exynos_drm_crtc	*crtc;
1177ee14cdcSGustavo Padovan 	struct exynos_drm_plane	planes[MIXER_WIN_NR];
118a44652e8SAndrzej Hajda 	unsigned long		flags;
11922b21ae6SJoonyoung Shim 
12022b21ae6SJoonyoung Shim 	struct mixer_resources	mixer_res;
1211e123441SRahul Sharma 	enum mixer_version_id	mxr_ver;
1221e123441SRahul Sharma };
1231e123441SRahul Sharma 
1241e123441SRahul Sharma struct mixer_drv_data {
1251e123441SRahul Sharma 	enum mixer_version_id	version;
1261b8e5747SRahul Sharma 	bool					is_vp_enabled;
127ff830c96SMarek Szyprowski 	bool					has_sclk;
12822b21ae6SJoonyoung Shim };
12922b21ae6SJoonyoung Shim 
130fd2d2fc2SMarek Szyprowski static const struct exynos_drm_plane_config plane_configs[MIXER_WIN_NR] = {
131fd2d2fc2SMarek Szyprowski 	{
132fd2d2fc2SMarek Szyprowski 		.zpos = 0,
133fd2d2fc2SMarek Szyprowski 		.type = DRM_PLANE_TYPE_PRIMARY,
134fd2d2fc2SMarek Szyprowski 		.pixel_formats = mixer_formats,
135fd2d2fc2SMarek Szyprowski 		.num_pixel_formats = ARRAY_SIZE(mixer_formats),
136a2cb911eSMarek Szyprowski 		.capabilities = EXYNOS_DRM_PLANE_CAP_DOUBLE |
137a2cb911eSMarek Szyprowski 				EXYNOS_DRM_PLANE_CAP_ZPOS,
138fd2d2fc2SMarek Szyprowski 	}, {
139fd2d2fc2SMarek Szyprowski 		.zpos = 1,
140fd2d2fc2SMarek Szyprowski 		.type = DRM_PLANE_TYPE_CURSOR,
141fd2d2fc2SMarek Szyprowski 		.pixel_formats = mixer_formats,
142fd2d2fc2SMarek Szyprowski 		.num_pixel_formats = ARRAY_SIZE(mixer_formats),
143a2cb911eSMarek Szyprowski 		.capabilities = EXYNOS_DRM_PLANE_CAP_DOUBLE |
144a2cb911eSMarek Szyprowski 				EXYNOS_DRM_PLANE_CAP_ZPOS,
145fd2d2fc2SMarek Szyprowski 	}, {
146fd2d2fc2SMarek Szyprowski 		.zpos = 2,
147fd2d2fc2SMarek Szyprowski 		.type = DRM_PLANE_TYPE_OVERLAY,
148fd2d2fc2SMarek Szyprowski 		.pixel_formats = vp_formats,
149fd2d2fc2SMarek Szyprowski 		.num_pixel_formats = ARRAY_SIZE(vp_formats),
150a2cb911eSMarek Szyprowski 		.capabilities = EXYNOS_DRM_PLANE_CAP_SCALE |
151f40031c2STobias Jakobi 				EXYNOS_DRM_PLANE_CAP_ZPOS |
152f40031c2STobias Jakobi 				EXYNOS_DRM_PLANE_CAP_TILE,
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 
185f657a996SMarek Szyprowski static inline bool is_alpha_format(unsigned int pixel_format)
186f657a996SMarek Szyprowski {
187f657a996SMarek Szyprowski 	switch (pixel_format) {
188f657a996SMarek Szyprowski 	case DRM_FORMAT_ARGB8888:
18926a7af3eSTobias Jakobi 	case DRM_FORMAT_ARGB1555:
19026a7af3eSTobias Jakobi 	case DRM_FORMAT_ARGB4444:
191f657a996SMarek Szyprowski 		return true;
192f657a996SMarek Szyprowski 	default:
193f657a996SMarek Szyprowski 		return false;
194f657a996SMarek Szyprowski 	}
195f657a996SMarek Szyprowski }
196f657a996SMarek Szyprowski 
197d8408326SSeung-Woo Kim static inline u32 vp_reg_read(struct mixer_resources *res, u32 reg_id)
198d8408326SSeung-Woo Kim {
199d8408326SSeung-Woo Kim 	return readl(res->vp_regs + reg_id);
200d8408326SSeung-Woo Kim }
201d8408326SSeung-Woo Kim 
202d8408326SSeung-Woo Kim static inline void vp_reg_write(struct mixer_resources *res, u32 reg_id,
203d8408326SSeung-Woo Kim 				 u32 val)
204d8408326SSeung-Woo Kim {
205d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
206d8408326SSeung-Woo Kim }
207d8408326SSeung-Woo Kim 
208d8408326SSeung-Woo Kim static inline void vp_reg_writemask(struct mixer_resources *res, u32 reg_id,
209d8408326SSeung-Woo Kim 				 u32 val, u32 mask)
210d8408326SSeung-Woo Kim {
211d8408326SSeung-Woo Kim 	u32 old = vp_reg_read(res, reg_id);
212d8408326SSeung-Woo Kim 
213d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
214d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
215d8408326SSeung-Woo Kim }
216d8408326SSeung-Woo Kim 
217d8408326SSeung-Woo Kim static inline u32 mixer_reg_read(struct mixer_resources *res, u32 reg_id)
218d8408326SSeung-Woo Kim {
219d8408326SSeung-Woo Kim 	return readl(res->mixer_regs + reg_id);
220d8408326SSeung-Woo Kim }
221d8408326SSeung-Woo Kim 
222d8408326SSeung-Woo Kim static inline void mixer_reg_write(struct mixer_resources *res, u32 reg_id,
223d8408326SSeung-Woo Kim 				 u32 val)
224d8408326SSeung-Woo Kim {
225d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
226d8408326SSeung-Woo Kim }
227d8408326SSeung-Woo Kim 
228d8408326SSeung-Woo Kim static inline void mixer_reg_writemask(struct mixer_resources *res,
229d8408326SSeung-Woo Kim 				 u32 reg_id, u32 val, u32 mask)
230d8408326SSeung-Woo Kim {
231d8408326SSeung-Woo Kim 	u32 old = mixer_reg_read(res, reg_id);
232d8408326SSeung-Woo Kim 
233d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
234d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
235d8408326SSeung-Woo Kim }
236d8408326SSeung-Woo Kim 
237d8408326SSeung-Woo Kim static void mixer_regs_dump(struct mixer_context *ctx)
238d8408326SSeung-Woo Kim {
239d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
240d8408326SSeung-Woo Kim do { \
241d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
242d8408326SSeung-Woo Kim 		(u32)readl(ctx->mixer_res.mixer_regs + reg_id)); \
243d8408326SSeung-Woo Kim } while (0)
244d8408326SSeung-Woo Kim 
245d8408326SSeung-Woo Kim 	DUMPREG(MXR_STATUS);
246d8408326SSeung-Woo Kim 	DUMPREG(MXR_CFG);
247d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_EN);
248d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_STATUS);
249d8408326SSeung-Woo Kim 
250d8408326SSeung-Woo Kim 	DUMPREG(MXR_LAYER_CFG);
251d8408326SSeung-Woo Kim 	DUMPREG(MXR_VIDEO_CFG);
252d8408326SSeung-Woo Kim 
253d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_CFG);
254d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_BASE);
255d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SPAN);
256d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_WH);
257d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SXY);
258d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_DXY);
259d8408326SSeung-Woo Kim 
260d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_CFG);
261d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_BASE);
262d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SPAN);
263d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_WH);
264d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SXY);
265d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_DXY);
266d8408326SSeung-Woo Kim #undef DUMPREG
267d8408326SSeung-Woo Kim }
268d8408326SSeung-Woo Kim 
269d8408326SSeung-Woo Kim static void vp_regs_dump(struct mixer_context *ctx)
270d8408326SSeung-Woo Kim {
271d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
272d8408326SSeung-Woo Kim do { \
273d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
274d8408326SSeung-Woo Kim 		(u32) readl(ctx->mixer_res.vp_regs + reg_id)); \
275d8408326SSeung-Woo Kim } while (0)
276d8408326SSeung-Woo Kim 
277d8408326SSeung-Woo Kim 	DUMPREG(VP_ENABLE);
278d8408326SSeung-Woo Kim 	DUMPREG(VP_SRESET);
279d8408326SSeung-Woo Kim 	DUMPREG(VP_SHADOW_UPDATE);
280d8408326SSeung-Woo Kim 	DUMPREG(VP_FIELD_ID);
281d8408326SSeung-Woo Kim 	DUMPREG(VP_MODE);
282d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_Y);
283d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_C);
284d8408326SSeung-Woo Kim 	DUMPREG(VP_PER_RATE_CTRL);
285d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_Y_PTR);
286d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_Y_PTR);
287d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_C_PTR);
288d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_C_PTR);
289d8408326SSeung-Woo Kim 	DUMPREG(VP_ENDIAN_MODE);
290d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_H_POSITION);
291d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_V_POSITION);
292d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_WIDTH);
293d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_HEIGHT);
294d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_H_POSITION);
295d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_V_POSITION);
296d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_WIDTH);
297d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_HEIGHT);
298d8408326SSeung-Woo Kim 	DUMPREG(VP_H_RATIO);
299d8408326SSeung-Woo Kim 	DUMPREG(VP_V_RATIO);
300d8408326SSeung-Woo Kim 
301d8408326SSeung-Woo Kim #undef DUMPREG
302d8408326SSeung-Woo Kim }
303d8408326SSeung-Woo Kim 
304d8408326SSeung-Woo Kim static inline void vp_filter_set(struct mixer_resources *res,
305d8408326SSeung-Woo Kim 		int reg_id, const u8 *data, unsigned int size)
306d8408326SSeung-Woo Kim {
307d8408326SSeung-Woo Kim 	/* assure 4-byte align */
308d8408326SSeung-Woo Kim 	BUG_ON(size & 3);
309d8408326SSeung-Woo Kim 	for (; size; size -= 4, reg_id += 4, data += 4) {
310d8408326SSeung-Woo Kim 		u32 val = (data[0] << 24) |  (data[1] << 16) |
311d8408326SSeung-Woo Kim 			(data[2] << 8) | data[3];
312d8408326SSeung-Woo Kim 		vp_reg_write(res, reg_id, val);
313d8408326SSeung-Woo Kim 	}
314d8408326SSeung-Woo Kim }
315d8408326SSeung-Woo Kim 
316d8408326SSeung-Woo Kim static void vp_default_filter(struct mixer_resources *res)
317d8408326SSeung-Woo Kim {
318d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY8_Y0_LL,
319e25e1b66SSachin Kamat 		filter_y_horiz_tap8, sizeof(filter_y_horiz_tap8));
320d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_Y0_LL,
321e25e1b66SSachin Kamat 		filter_y_vert_tap4, sizeof(filter_y_vert_tap4));
322d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_C0_LL,
323e25e1b66SSachin Kamat 		filter_cr_horiz_tap4, sizeof(filter_cr_horiz_tap4));
324d8408326SSeung-Woo Kim }
325d8408326SSeung-Woo Kim 
326f657a996SMarek Szyprowski static void mixer_cfg_gfx_blend(struct mixer_context *ctx, unsigned int win,
327f657a996SMarek Szyprowski 				bool alpha)
328f657a996SMarek Szyprowski {
329f657a996SMarek Szyprowski 	struct mixer_resources *res = &ctx->mixer_res;
330f657a996SMarek Szyprowski 	u32 val;
331f657a996SMarek Szyprowski 
332f657a996SMarek Szyprowski 	val  = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
333f657a996SMarek Szyprowski 	if (alpha) {
334f657a996SMarek Szyprowski 		/* blending based on pixel alpha */
335f657a996SMarek Szyprowski 		val |= MXR_GRP_CFG_BLEND_PRE_MUL;
336f657a996SMarek Szyprowski 		val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
337f657a996SMarek Szyprowski 	}
338f657a996SMarek Szyprowski 	mixer_reg_writemask(res, MXR_GRAPHIC_CFG(win),
339f657a996SMarek Szyprowski 			    val, MXR_GRP_CFG_MISC_MASK);
340f657a996SMarek Szyprowski }
341f657a996SMarek Szyprowski 
342f657a996SMarek Szyprowski static void mixer_cfg_vp_blend(struct mixer_context *ctx)
343f657a996SMarek Szyprowski {
344f657a996SMarek Szyprowski 	struct mixer_resources *res = &ctx->mixer_res;
345f657a996SMarek Szyprowski 	u32 val;
346f657a996SMarek Szyprowski 
347f657a996SMarek Szyprowski 	/*
348f657a996SMarek Szyprowski 	 * No blending at the moment since the NV12/NV21 pixelformats don't
349f657a996SMarek Szyprowski 	 * have an alpha channel. However the mixer supports a global alpha
350f657a996SMarek Szyprowski 	 * value for a layer. Once this functionality is exposed, we can
351f657a996SMarek Szyprowski 	 * support blending of the video layer through this.
352f657a996SMarek Szyprowski 	 */
353f657a996SMarek Szyprowski 	val = 0;
354f657a996SMarek Szyprowski 	mixer_reg_write(res, MXR_VIDEO_CFG, val);
355f657a996SMarek Szyprowski }
356f657a996SMarek Szyprowski 
357d8408326SSeung-Woo Kim static void mixer_vsync_set_update(struct mixer_context *ctx, bool enable)
358d8408326SSeung-Woo Kim {
359d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
360d8408326SSeung-Woo Kim 
361d8408326SSeung-Woo Kim 	/* block update on vsync */
362d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, enable ?
363d8408326SSeung-Woo Kim 			MXR_STATUS_SYNC_ENABLE : 0, MXR_STATUS_SYNC_ENABLE);
364d8408326SSeung-Woo Kim 
365adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags))
366d8408326SSeung-Woo Kim 		vp_reg_write(res, VP_SHADOW_UPDATE, enable ?
367d8408326SSeung-Woo Kim 			VP_SHADOW_UPDATE_ENABLE : 0);
368d8408326SSeung-Woo Kim }
369d8408326SSeung-Woo Kim 
370*3fc40ca9SAndrzej Hajda static void mixer_cfg_scan(struct mixer_context *ctx, int width, int height)
371d8408326SSeung-Woo Kim {
372d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
373d8408326SSeung-Woo Kim 	u32 val;
374d8408326SSeung-Woo Kim 
375d8408326SSeung-Woo Kim 	/* choosing between interlace and progressive mode */
376adeb6f44STobias Jakobi 	val = test_bit(MXR_BIT_INTERLACE, &ctx->flags) ?
377adeb6f44STobias Jakobi 		MXR_CFG_SCAN_INTERLACE : MXR_CFG_SCAN_PROGRESSIVE;
378d8408326SSeung-Woo Kim 
379*3fc40ca9SAndrzej Hajda 	/* setup display size */
380*3fc40ca9SAndrzej Hajda 	if (ctx->mxr_ver == MXR_VER_128_0_0_184) {
381*3fc40ca9SAndrzej Hajda 		mixer_reg_write(&ctx->mixer_res, MXR_RESOLUTION,
382*3fc40ca9SAndrzej Hajda 			MXR_MXR_RES_HEIGHT(height) | MXR_MXR_RES_WIDTH(width));
383*3fc40ca9SAndrzej Hajda 	} else {
384def5e095SRahul Sharma 		/* choosing between proper HD and SD mode */
38529630743SRahul Sharma 		if (height <= 480)
386d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD;
38729630743SRahul Sharma 		else if (height <= 576)
388d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD;
38929630743SRahul Sharma 		else if (height <= 720)
390d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
39129630743SRahul Sharma 		else if (height <= 1080)
392d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_1080 | MXR_CFG_SCAN_HD;
393d8408326SSeung-Woo Kim 		else
394d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
395def5e095SRahul Sharma 	}
396d8408326SSeung-Woo Kim 
397d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_SCAN_MASK);
398d8408326SSeung-Woo Kim }
399d8408326SSeung-Woo Kim 
400d8408326SSeung-Woo Kim static void mixer_cfg_rgb_fmt(struct mixer_context *ctx, unsigned int height)
401d8408326SSeung-Woo Kim {
402d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
403d8408326SSeung-Woo Kim 	u32 val;
404d8408326SSeung-Woo Kim 
4052a39db01STobias Jakobi 	switch (height) {
4062a39db01STobias Jakobi 	case 480:
4072a39db01STobias Jakobi 	case 576:
408d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
4092a39db01STobias Jakobi 		break;
4102a39db01STobias Jakobi 	case 720:
4112a39db01STobias Jakobi 	case 1080:
4122a39db01STobias Jakobi 	default:
413d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
4142a6e4cd5STobias Jakobi 		/* Configure the BT.709 CSC matrix for full range RGB. */
415d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
4162a6e4cd5STobias Jakobi 			MXR_CSC_CT( 0.184,  0.614,  0.063) |
4172a6e4cd5STobias Jakobi 			MXR_CM_COEFF_RGB_FULL);
418d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
4192a6e4cd5STobias Jakobi 			MXR_CSC_CT(-0.102, -0.338,  0.440));
420d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
4212a6e4cd5STobias Jakobi 			MXR_CSC_CT( 0.440, -0.399, -0.040));
4222a39db01STobias Jakobi 		break;
423d8408326SSeung-Woo Kim 	}
424d8408326SSeung-Woo Kim 
425d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
426d8408326SSeung-Woo Kim }
427d8408326SSeung-Woo Kim 
4285b1d5bc6STobias Jakobi static void mixer_cfg_layer(struct mixer_context *ctx, unsigned int win,
429a2cb911eSMarek Szyprowski 			    unsigned int priority, bool enable)
430d8408326SSeung-Woo Kim {
431d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
432d8408326SSeung-Woo Kim 	u32 val = enable ? ~0 : 0;
433d8408326SSeung-Woo Kim 
434d8408326SSeung-Woo Kim 	switch (win) {
435d8408326SSeung-Woo Kim 	case 0:
436d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
437a2cb911eSMarek Szyprowski 		mixer_reg_writemask(res, MXR_LAYER_CFG,
438a2cb911eSMarek Szyprowski 				    MXR_LAYER_CFG_GRP0_VAL(priority),
439a2cb911eSMarek Szyprowski 				    MXR_LAYER_CFG_GRP0_MASK);
440d8408326SSeung-Woo Kim 		break;
441d8408326SSeung-Woo Kim 	case 1:
442d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
443a2cb911eSMarek Szyprowski 		mixer_reg_writemask(res, MXR_LAYER_CFG,
444a2cb911eSMarek Szyprowski 				    MXR_LAYER_CFG_GRP1_VAL(priority),
445a2cb911eSMarek Szyprowski 				    MXR_LAYER_CFG_GRP1_MASK);
446adeb6f44STobias Jakobi 
447d8408326SSeung-Woo Kim 		break;
4485e68fef2SMarek Szyprowski 	case VP_DEFAULT_WIN:
449adeb6f44STobias Jakobi 		if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
450d8408326SSeung-Woo Kim 			vp_reg_writemask(res, VP_ENABLE, val, VP_ENABLE_ON);
4511b8e5747SRahul Sharma 			mixer_reg_writemask(res, MXR_CFG, val,
4521b8e5747SRahul Sharma 				MXR_CFG_VP_ENABLE);
453a2cb911eSMarek Szyprowski 			mixer_reg_writemask(res, MXR_LAYER_CFG,
454a2cb911eSMarek Szyprowski 					    MXR_LAYER_CFG_VP_VAL(priority),
455a2cb911eSMarek Szyprowski 					    MXR_LAYER_CFG_VP_MASK);
4561b8e5747SRahul Sharma 		}
457d8408326SSeung-Woo Kim 		break;
458d8408326SSeung-Woo Kim 	}
459d8408326SSeung-Woo Kim }
460d8408326SSeung-Woo Kim 
461d8408326SSeung-Woo Kim static void mixer_run(struct mixer_context *ctx)
462d8408326SSeung-Woo Kim {
463d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
464d8408326SSeung-Woo Kim 
465d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
466d8408326SSeung-Woo Kim }
467d8408326SSeung-Woo Kim 
468381be025SRahul Sharma static void mixer_stop(struct mixer_context *ctx)
469381be025SRahul Sharma {
470381be025SRahul Sharma 	struct mixer_resources *res = &ctx->mixer_res;
471381be025SRahul Sharma 	int timeout = 20;
472381be025SRahul Sharma 
473381be025SRahul Sharma 	mixer_reg_writemask(res, MXR_STATUS, 0, MXR_STATUS_REG_RUN);
474381be025SRahul Sharma 
475381be025SRahul Sharma 	while (!(mixer_reg_read(res, MXR_STATUS) & MXR_STATUS_REG_IDLE) &&
476381be025SRahul Sharma 			--timeout)
477381be025SRahul Sharma 		usleep_range(10000, 12000);
478381be025SRahul Sharma }
479381be025SRahul Sharma 
480521d98a3SAndrzej Hajda static void mixer_commit(struct mixer_context *ctx)
481521d98a3SAndrzej Hajda {
482521d98a3SAndrzej Hajda 	struct drm_display_mode *mode = &ctx->crtc->base.state->adjusted_mode;
483521d98a3SAndrzej Hajda 
48471469944SAndrzej Hajda 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
48571469944SAndrzej Hajda 		__set_bit(MXR_BIT_INTERLACE, &ctx->flags);
48671469944SAndrzej Hajda 	else
48771469944SAndrzej Hajda 		__clear_bit(MXR_BIT_INTERLACE, &ctx->flags);
48871469944SAndrzej Hajda 
489*3fc40ca9SAndrzej Hajda 	mixer_cfg_scan(ctx, mode->hdisplay, mode->vdisplay);
490521d98a3SAndrzej Hajda 	mixer_cfg_rgb_fmt(ctx, mode->vdisplay);
491521d98a3SAndrzej Hajda 	mixer_run(ctx);
492521d98a3SAndrzej Hajda }
493521d98a3SAndrzej Hajda 
4942eeb2e5eSGustavo Padovan static void vp_video_buffer(struct mixer_context *ctx,
4952eeb2e5eSGustavo Padovan 			    struct exynos_drm_plane *plane)
496d8408326SSeung-Woo Kim {
4970114f404SMarek Szyprowski 	struct exynos_drm_plane_state *state =
4980114f404SMarek Szyprowski 				to_exynos_plane_state(plane->base.state);
499d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
5000114f404SMarek Szyprowski 	struct drm_framebuffer *fb = state->base.fb;
501e47726a1SMarek Szyprowski 	unsigned int priority = state->base.normalized_zpos + 1;
502d8408326SSeung-Woo Kim 	unsigned long flags;
503d8408326SSeung-Woo Kim 	dma_addr_t luma_addr[2], chroma_addr[2];
5040f752694STobias Jakobi 	bool is_tiled, is_nv21;
505d8408326SSeung-Woo Kim 	u32 val;
506d8408326SSeung-Woo Kim 
5070f752694STobias Jakobi 	is_nv21 = (fb->format->format == DRM_FORMAT_NV21);
5080f752694STobias Jakobi 	is_tiled = (fb->modifier == DRM_FORMAT_MOD_SAMSUNG_64_32_TILE);
509f40031c2STobias Jakobi 
5100488f50eSMarek Szyprowski 	luma_addr[0] = exynos_drm_fb_dma_addr(fb, 0);
5110488f50eSMarek Szyprowski 	chroma_addr[0] = exynos_drm_fb_dma_addr(fb, 1);
512d8408326SSeung-Woo Kim 
51371469944SAndrzej Hajda 	if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)) {
5140f752694STobias Jakobi 		if (is_tiled) {
515d8408326SSeung-Woo Kim 			luma_addr[1] = luma_addr[0] + 0x40;
516d8408326SSeung-Woo Kim 			chroma_addr[1] = chroma_addr[0] + 0x40;
517d8408326SSeung-Woo Kim 		} else {
5182eeb2e5eSGustavo Padovan 			luma_addr[1] = luma_addr[0] + fb->pitches[0];
5192eeb2e5eSGustavo Padovan 			chroma_addr[1] = chroma_addr[0] + fb->pitches[0];
520d8408326SSeung-Woo Kim 		}
521d8408326SSeung-Woo Kim 	} else {
522d8408326SSeung-Woo Kim 		luma_addr[1] = 0;
523d8408326SSeung-Woo Kim 		chroma_addr[1] = 0;
524d8408326SSeung-Woo Kim 	}
525d8408326SSeung-Woo Kim 
526d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
527d8408326SSeung-Woo Kim 
528d8408326SSeung-Woo Kim 	/* interlace or progressive scan mode */
529adeb6f44STobias Jakobi 	val = (test_bit(MXR_BIT_INTERLACE, &ctx->flags) ? ~0 : 0);
530d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_LINE_SKIP);
531d8408326SSeung-Woo Kim 
532d8408326SSeung-Woo Kim 	/* setup format */
5330f752694STobias Jakobi 	val = (is_nv21 ? VP_MODE_NV21 : VP_MODE_NV12);
5340f752694STobias Jakobi 	val |= (is_tiled ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
535d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_FMT_MASK);
536d8408326SSeung-Woo Kim 
537d8408326SSeung-Woo Kim 	/* setting size of input image */
5382eeb2e5eSGustavo Padovan 	vp_reg_write(res, VP_IMG_SIZE_Y, VP_IMG_HSIZE(fb->pitches[0]) |
5392eeb2e5eSGustavo Padovan 		VP_IMG_VSIZE(fb->height));
540dc500cfbSTobias Jakobi 	/* chroma plane for NV12/NV21 is half the height of the luma plane */
5412eeb2e5eSGustavo Padovan 	vp_reg_write(res, VP_IMG_SIZE_C, VP_IMG_HSIZE(fb->pitches[0]) |
5422eeb2e5eSGustavo Padovan 		VP_IMG_VSIZE(fb->height / 2));
543d8408326SSeung-Woo Kim 
5440114f404SMarek Szyprowski 	vp_reg_write(res, VP_SRC_WIDTH, state->src.w);
5450114f404SMarek Szyprowski 	vp_reg_write(res, VP_SRC_HEIGHT, state->src.h);
546d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_H_POSITION,
5470114f404SMarek Szyprowski 			VP_SRC_H_POSITION_VAL(state->src.x));
5480114f404SMarek Szyprowski 	vp_reg_write(res, VP_SRC_V_POSITION, state->src.y);
549d8408326SSeung-Woo Kim 
5500114f404SMarek Szyprowski 	vp_reg_write(res, VP_DST_WIDTH, state->crtc.w);
5510114f404SMarek Szyprowski 	vp_reg_write(res, VP_DST_H_POSITION, state->crtc.x);
552adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)) {
5530114f404SMarek Szyprowski 		vp_reg_write(res, VP_DST_HEIGHT, state->crtc.h / 2);
5540114f404SMarek Szyprowski 		vp_reg_write(res, VP_DST_V_POSITION, state->crtc.y / 2);
555d8408326SSeung-Woo Kim 	} else {
5560114f404SMarek Szyprowski 		vp_reg_write(res, VP_DST_HEIGHT, state->crtc.h);
5570114f404SMarek Szyprowski 		vp_reg_write(res, VP_DST_V_POSITION, state->crtc.y);
558d8408326SSeung-Woo Kim 	}
559d8408326SSeung-Woo Kim 
5600114f404SMarek Szyprowski 	vp_reg_write(res, VP_H_RATIO, state->h_ratio);
5610114f404SMarek Szyprowski 	vp_reg_write(res, VP_V_RATIO, state->v_ratio);
562d8408326SSeung-Woo Kim 
563d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
564d8408326SSeung-Woo Kim 
565d8408326SSeung-Woo Kim 	/* set buffer address to vp */
566d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_Y_PTR, luma_addr[0]);
567d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_Y_PTR, luma_addr[1]);
568d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_C_PTR, chroma_addr[0]);
569d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_C_PTR, chroma_addr[1]);
570d8408326SSeung-Woo Kim 
571e47726a1SMarek Szyprowski 	mixer_cfg_layer(ctx, plane->index, priority, true);
572f657a996SMarek Szyprowski 	mixer_cfg_vp_blend(ctx);
573d8408326SSeung-Woo Kim 
574d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
575d8408326SSeung-Woo Kim 
576c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
577d8408326SSeung-Woo Kim 	vp_regs_dump(ctx);
578d8408326SSeung-Woo Kim }
579d8408326SSeung-Woo Kim 
580aaf8b49eSRahul Sharma static void mixer_layer_update(struct mixer_context *ctx)
581aaf8b49eSRahul Sharma {
582aaf8b49eSRahul Sharma 	struct mixer_resources *res = &ctx->mixer_res;
583aaf8b49eSRahul Sharma 
584aaf8b49eSRahul Sharma 	mixer_reg_writemask(res, MXR_CFG, ~0, MXR_CFG_LAYER_UPDATE);
585aaf8b49eSRahul Sharma }
586aaf8b49eSRahul Sharma 
5872eeb2e5eSGustavo Padovan static void mixer_graph_buffer(struct mixer_context *ctx,
5882eeb2e5eSGustavo Padovan 			       struct exynos_drm_plane *plane)
589d8408326SSeung-Woo Kim {
5900114f404SMarek Szyprowski 	struct exynos_drm_plane_state *state =
5910114f404SMarek Szyprowski 				to_exynos_plane_state(plane->base.state);
592d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
5930114f404SMarek Szyprowski 	struct drm_framebuffer *fb = state->base.fb;
594e47726a1SMarek Szyprowski 	unsigned int priority = state->base.normalized_zpos + 1;
595d8408326SSeung-Woo Kim 	unsigned long flags;
59640bdfb0aSMarek Szyprowski 	unsigned int win = plane->index;
5972611015cSTobias Jakobi 	unsigned int x_ratio = 0, y_ratio = 0;
5985dff6905STobias Jakobi 	unsigned int dst_x_offset, dst_y_offset;
599d8408326SSeung-Woo Kim 	dma_addr_t dma_addr;
600d8408326SSeung-Woo Kim 	unsigned int fmt;
601d8408326SSeung-Woo Kim 	u32 val;
602d8408326SSeung-Woo Kim 
603438b74a5SVille Syrjälä 	switch (fb->format->format) {
6047a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB4444:
60526a7af3eSTobias Jakobi 	case DRM_FORMAT_ARGB4444:
6067a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB4444;
6077a57ca7cSTobias Jakobi 		break;
608d8408326SSeung-Woo Kim 
6097a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB1555:
61026a7af3eSTobias Jakobi 	case DRM_FORMAT_ARGB1555:
6117a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB1555;
612d8408326SSeung-Woo Kim 		break;
6137a57ca7cSTobias Jakobi 
6147a57ca7cSTobias Jakobi 	case DRM_FORMAT_RGB565:
6157a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_RGB565;
616d8408326SSeung-Woo Kim 		break;
6177a57ca7cSTobias Jakobi 
6187a57ca7cSTobias Jakobi 	case DRM_FORMAT_XRGB8888:
6197a57ca7cSTobias Jakobi 	case DRM_FORMAT_ARGB8888:
6201e60d62fSTobias Jakobi 	default:
6217a57ca7cSTobias Jakobi 		fmt = MXR_FORMAT_ARGB8888;
6227a57ca7cSTobias Jakobi 		break;
623d8408326SSeung-Woo Kim 	}
624d8408326SSeung-Woo Kim 
625e463b069SMarek Szyprowski 	/* ratio is already checked by common plane code */
626e463b069SMarek Szyprowski 	x_ratio = state->h_ratio == (1 << 15);
627e463b069SMarek Szyprowski 	y_ratio = state->v_ratio == (1 << 15);
628d8408326SSeung-Woo Kim 
6290114f404SMarek Szyprowski 	dst_x_offset = state->crtc.x;
6300114f404SMarek Szyprowski 	dst_y_offset = state->crtc.y;
631d8408326SSeung-Woo Kim 
6325dff6905STobias Jakobi 	/* translate dma address base s.t. the source image offset is zero */
6330488f50eSMarek Szyprowski 	dma_addr = exynos_drm_fb_dma_addr(fb, 0)
634272725c7SVille Syrjälä 		+ (state->src.x * fb->format->cpp[0])
6350114f404SMarek Szyprowski 		+ (state->src.y * fb->pitches[0]);
636d8408326SSeung-Woo Kim 
637d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
638d8408326SSeung-Woo Kim 
639d8408326SSeung-Woo Kim 	/* setup format */
640d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_GRAPHIC_CFG(win),
641d8408326SSeung-Woo Kim 		MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
642d8408326SSeung-Woo Kim 
643d8408326SSeung-Woo Kim 	/* setup geometry */
644adacb228SDaniel Stone 	mixer_reg_write(res, MXR_GRAPHIC_SPAN(win),
645272725c7SVille Syrjälä 			fb->pitches[0] / fb->format->cpp[0]);
646d8408326SSeung-Woo Kim 
6470114f404SMarek Szyprowski 	val  = MXR_GRP_WH_WIDTH(state->src.w);
6480114f404SMarek Szyprowski 	val |= MXR_GRP_WH_HEIGHT(state->src.h);
649d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_H_SCALE(x_ratio);
650d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_V_SCALE(y_ratio);
651d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_WH(win), val);
652d8408326SSeung-Woo Kim 
653d8408326SSeung-Woo Kim 	/* setup offsets in display image */
654d8408326SSeung-Woo Kim 	val  = MXR_GRP_DXY_DX(dst_x_offset);
655d8408326SSeung-Woo Kim 	val |= MXR_GRP_DXY_DY(dst_y_offset);
656d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_DXY(win), val);
657d8408326SSeung-Woo Kim 
658d8408326SSeung-Woo Kim 	/* set buffer address to mixer */
659d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_BASE(win), dma_addr);
660d8408326SSeung-Woo Kim 
661e47726a1SMarek Szyprowski 	mixer_cfg_layer(ctx, win, priority, true);
662438b74a5SVille Syrjälä 	mixer_cfg_gfx_blend(ctx, win, is_alpha_format(fb->format->format));
663aaf8b49eSRahul Sharma 
664aaf8b49eSRahul Sharma 	/* layer update mandatory for mixer 16.0.33.0 */
665def5e095SRahul Sharma 	if (ctx->mxr_ver == MXR_VER_16_0_33_0 ||
666def5e095SRahul Sharma 		ctx->mxr_ver == MXR_VER_128_0_0_184)
667aaf8b49eSRahul Sharma 		mixer_layer_update(ctx);
668aaf8b49eSRahul Sharma 
669d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
670c0734fbaSTobias Jakobi 
671c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
672d8408326SSeung-Woo Kim }
673d8408326SSeung-Woo Kim 
674d8408326SSeung-Woo Kim static void vp_win_reset(struct mixer_context *ctx)
675d8408326SSeung-Woo Kim {
676d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
677a696394cSTobias Jakobi 	unsigned int tries = 100;
678d8408326SSeung-Woo Kim 
679d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRESET, VP_SRESET_PROCESSING);
6808646dcb8SDan Carpenter 	while (--tries) {
681d8408326SSeung-Woo Kim 		/* waiting until VP_SRESET_PROCESSING is 0 */
682d8408326SSeung-Woo Kim 		if (~vp_reg_read(res, VP_SRESET) & VP_SRESET_PROCESSING)
683d8408326SSeung-Woo Kim 			break;
68402b3de43STomasz Stanislawski 		mdelay(10);
685d8408326SSeung-Woo Kim 	}
686d8408326SSeung-Woo Kim 	WARN(tries == 0, "failed to reset Video Processor\n");
687d8408326SSeung-Woo Kim }
688d8408326SSeung-Woo Kim 
689cf8fc4f1SJoonyoung Shim static void mixer_win_reset(struct mixer_context *ctx)
690cf8fc4f1SJoonyoung Shim {
691cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
692cf8fc4f1SJoonyoung Shim 	unsigned long flags;
693cf8fc4f1SJoonyoung Shim 
694cf8fc4f1SJoonyoung Shim 	spin_lock_irqsave(&res->reg_slock, flags);
695cf8fc4f1SJoonyoung Shim 
696cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
697cf8fc4f1SJoonyoung Shim 
698cf8fc4f1SJoonyoung Shim 	/* set output in RGB888 mode */
699cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
700cf8fc4f1SJoonyoung Shim 
701cf8fc4f1SJoonyoung Shim 	/* 16 beat burst in DMA */
702cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_STATUS, MXR_STATUS_16_BURST,
703cf8fc4f1SJoonyoung Shim 		MXR_STATUS_BURST_MASK);
704cf8fc4f1SJoonyoung Shim 
705a2cb911eSMarek Szyprowski 	/* reset default layer priority */
706a2cb911eSMarek Szyprowski 	mixer_reg_write(res, MXR_LAYER_CFG, 0);
707cf8fc4f1SJoonyoung Shim 
7082a6e4cd5STobias Jakobi 	/* set all background colors to RGB (0,0,0) */
7092a6e4cd5STobias Jakobi 	mixer_reg_write(res, MXR_BG_COLOR0, MXR_YCBCR_VAL(0, 128, 128));
7102a6e4cd5STobias Jakobi 	mixer_reg_write(res, MXR_BG_COLOR1, MXR_YCBCR_VAL(0, 128, 128));
7112a6e4cd5STobias Jakobi 	mixer_reg_write(res, MXR_BG_COLOR2, MXR_YCBCR_VAL(0, 128, 128));
712cf8fc4f1SJoonyoung Shim 
713adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
714cf8fc4f1SJoonyoung Shim 		/* configuration of Video Processor Registers */
715cf8fc4f1SJoonyoung Shim 		vp_win_reset(ctx);
716cf8fc4f1SJoonyoung Shim 		vp_default_filter(res);
7171b8e5747SRahul Sharma 	}
718cf8fc4f1SJoonyoung Shim 
719cf8fc4f1SJoonyoung Shim 	/* disable all layers */
720cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
721cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
722adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags))
723cf8fc4f1SJoonyoung Shim 		mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
724cf8fc4f1SJoonyoung Shim 
7255dff6905STobias Jakobi 	/* set all source image offsets to zero */
7265dff6905STobias Jakobi 	mixer_reg_write(res, MXR_GRAPHIC_SXY(0), 0);
7275dff6905STobias Jakobi 	mixer_reg_write(res, MXR_GRAPHIC_SXY(1), 0);
7285dff6905STobias Jakobi 
729cf8fc4f1SJoonyoung Shim 	spin_unlock_irqrestore(&res->reg_slock, flags);
730cf8fc4f1SJoonyoung Shim }
731cf8fc4f1SJoonyoung Shim 
7324551789fSSean Paul static irqreturn_t mixer_irq_handler(int irq, void *arg)
7334551789fSSean Paul {
7344551789fSSean Paul 	struct mixer_context *ctx = arg;
7354551789fSSean Paul 	struct mixer_resources *res = &ctx->mixer_res;
7364551789fSSean Paul 	u32 val, base, shadow;
7374551789fSSean Paul 
7384551789fSSean Paul 	spin_lock(&res->reg_slock);
7394551789fSSean Paul 
7404551789fSSean Paul 	/* read interrupt status for handling and clearing flags for VSYNC */
7414551789fSSean Paul 	val = mixer_reg_read(res, MXR_INT_STATUS);
7424551789fSSean Paul 
7434551789fSSean Paul 	/* handling VSYNC */
7444551789fSSean Paul 	if (val & MXR_INT_STATUS_VSYNC) {
74581a464dfSAndrzej Hajda 		/* vsync interrupt use different bit for read and clear */
74681a464dfSAndrzej Hajda 		val |= MXR_INT_CLEAR_VSYNC;
74781a464dfSAndrzej Hajda 		val &= ~MXR_INT_STATUS_VSYNC;
74881a464dfSAndrzej Hajda 
7494551789fSSean Paul 		/* interlace scan need to check shadow register */
750adeb6f44STobias Jakobi 		if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)) {
7514551789fSSean Paul 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
7524551789fSSean Paul 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
7534551789fSSean Paul 			if (base != shadow)
7544551789fSSean Paul 				goto out;
7554551789fSSean Paul 
7564551789fSSean Paul 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
7574551789fSSean Paul 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
7584551789fSSean Paul 			if (base != shadow)
7594551789fSSean Paul 				goto out;
7604551789fSSean Paul 		}
7614551789fSSean Paul 
762eafd540aSGustavo Padovan 		drm_crtc_handle_vblank(&ctx->crtc->base);
7634551789fSSean Paul 	}
7644551789fSSean Paul 
7654551789fSSean Paul out:
7664551789fSSean Paul 	/* clear interrupts */
7674551789fSSean Paul 	mixer_reg_write(res, MXR_INT_STATUS, val);
7684551789fSSean Paul 
7694551789fSSean Paul 	spin_unlock(&res->reg_slock);
7704551789fSSean Paul 
7714551789fSSean Paul 	return IRQ_HANDLED;
7724551789fSSean Paul }
7734551789fSSean Paul 
7744551789fSSean Paul static int mixer_resources_init(struct mixer_context *mixer_ctx)
7754551789fSSean Paul {
7764551789fSSean Paul 	struct device *dev = &mixer_ctx->pdev->dev;
7774551789fSSean Paul 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
7784551789fSSean Paul 	struct resource *res;
7794551789fSSean Paul 	int ret;
7804551789fSSean Paul 
7814551789fSSean Paul 	spin_lock_init(&mixer_res->reg_slock);
7824551789fSSean Paul 
7834551789fSSean Paul 	mixer_res->mixer = devm_clk_get(dev, "mixer");
7844551789fSSean Paul 	if (IS_ERR(mixer_res->mixer)) {
7854551789fSSean Paul 		dev_err(dev, "failed to get clock 'mixer'\n");
7864551789fSSean Paul 		return -ENODEV;
7874551789fSSean Paul 	}
7884551789fSSean Paul 
78904427ec5SMarek Szyprowski 	mixer_res->hdmi = devm_clk_get(dev, "hdmi");
79004427ec5SMarek Szyprowski 	if (IS_ERR(mixer_res->hdmi)) {
79104427ec5SMarek Szyprowski 		dev_err(dev, "failed to get clock 'hdmi'\n");
79204427ec5SMarek Szyprowski 		return PTR_ERR(mixer_res->hdmi);
79304427ec5SMarek Szyprowski 	}
79404427ec5SMarek Szyprowski 
7954551789fSSean Paul 	mixer_res->sclk_hdmi = devm_clk_get(dev, "sclk_hdmi");
7964551789fSSean Paul 	if (IS_ERR(mixer_res->sclk_hdmi)) {
7974551789fSSean Paul 		dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
7984551789fSSean Paul 		return -ENODEV;
7994551789fSSean Paul 	}
8004551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 0);
8014551789fSSean Paul 	if (res == NULL) {
8024551789fSSean Paul 		dev_err(dev, "get memory resource failed.\n");
8034551789fSSean Paul 		return -ENXIO;
8044551789fSSean Paul 	}
8054551789fSSean Paul 
8064551789fSSean Paul 	mixer_res->mixer_regs = devm_ioremap(dev, res->start,
8074551789fSSean Paul 							resource_size(res));
8084551789fSSean Paul 	if (mixer_res->mixer_regs == NULL) {
8094551789fSSean Paul 		dev_err(dev, "register mapping failed.\n");
8104551789fSSean Paul 		return -ENXIO;
8114551789fSSean Paul 	}
8124551789fSSean Paul 
8134551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_IRQ, 0);
8144551789fSSean Paul 	if (res == NULL) {
8154551789fSSean Paul 		dev_err(dev, "get interrupt resource failed.\n");
8164551789fSSean Paul 		return -ENXIO;
8174551789fSSean Paul 	}
8184551789fSSean Paul 
8194551789fSSean Paul 	ret = devm_request_irq(dev, res->start, mixer_irq_handler,
8204551789fSSean Paul 						0, "drm_mixer", mixer_ctx);
8214551789fSSean Paul 	if (ret) {
8224551789fSSean Paul 		dev_err(dev, "request interrupt failed.\n");
8234551789fSSean Paul 		return ret;
8244551789fSSean Paul 	}
8254551789fSSean Paul 	mixer_res->irq = res->start;
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 mixer_resources *mixer_res = &mixer_ctx->mixer_res;
8344551789fSSean Paul 	struct resource *res;
8354551789fSSean Paul 
8364551789fSSean Paul 	mixer_res->vp = devm_clk_get(dev, "vp");
8374551789fSSean Paul 	if (IS_ERR(mixer_res->vp)) {
8384551789fSSean Paul 		dev_err(dev, "failed to get clock 'vp'\n");
8394551789fSSean Paul 		return -ENODEV;
8404551789fSSean Paul 	}
841ff830c96SMarek Szyprowski 
842adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_HAS_SCLK, &mixer_ctx->flags)) {
8434551789fSSean Paul 		mixer_res->sclk_mixer = devm_clk_get(dev, "sclk_mixer");
8444551789fSSean Paul 		if (IS_ERR(mixer_res->sclk_mixer)) {
8454551789fSSean Paul 			dev_err(dev, "failed to get clock 'sclk_mixer'\n");
8464551789fSSean Paul 			return -ENODEV;
8474551789fSSean Paul 		}
848ff830c96SMarek Szyprowski 		mixer_res->mout_mixer = devm_clk_get(dev, "mout_mixer");
849ff830c96SMarek Szyprowski 		if (IS_ERR(mixer_res->mout_mixer)) {
850ff830c96SMarek Szyprowski 			dev_err(dev, "failed to get clock 'mout_mixer'\n");
8514551789fSSean Paul 			return -ENODEV;
8524551789fSSean Paul 		}
8534551789fSSean Paul 
854ff830c96SMarek Szyprowski 		if (mixer_res->sclk_hdmi && mixer_res->mout_mixer)
855ff830c96SMarek Szyprowski 			clk_set_parent(mixer_res->mout_mixer,
856ff830c96SMarek Szyprowski 				       mixer_res->sclk_hdmi);
857ff830c96SMarek Szyprowski 	}
8584551789fSSean Paul 
8594551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 1);
8604551789fSSean Paul 	if (res == NULL) {
8614551789fSSean Paul 		dev_err(dev, "get memory resource failed.\n");
8624551789fSSean Paul 		return -ENXIO;
8634551789fSSean Paul 	}
8644551789fSSean Paul 
8654551789fSSean Paul 	mixer_res->vp_regs = devm_ioremap(dev, res->start,
8664551789fSSean Paul 							resource_size(res));
8674551789fSSean Paul 	if (mixer_res->vp_regs == NULL) {
8684551789fSSean Paul 		dev_err(dev, "register mapping failed.\n");
8694551789fSSean Paul 		return -ENXIO;
8704551789fSSean Paul 	}
8714551789fSSean Paul 
8724551789fSSean Paul 	return 0;
8734551789fSSean Paul }
8744551789fSSean Paul 
87593bca243SGustavo Padovan static int mixer_initialize(struct mixer_context *mixer_ctx,
876f37cd5e8SInki Dae 			struct drm_device *drm_dev)
8774551789fSSean Paul {
8784551789fSSean Paul 	int ret;
879f37cd5e8SInki Dae 	struct exynos_drm_private *priv;
880f37cd5e8SInki Dae 	priv = drm_dev->dev_private;
8814551789fSSean Paul 
882eb88e422SGustavo Padovan 	mixer_ctx->drm_dev = drm_dev;
8834551789fSSean Paul 
8844551789fSSean Paul 	/* acquire resources: regs, irqs, clocks */
8854551789fSSean Paul 	ret = mixer_resources_init(mixer_ctx);
8864551789fSSean Paul 	if (ret) {
8874551789fSSean Paul 		DRM_ERROR("mixer_resources_init failed ret=%d\n", ret);
8884551789fSSean Paul 		return ret;
8894551789fSSean Paul 	}
8904551789fSSean Paul 
891adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &mixer_ctx->flags)) {
8924551789fSSean Paul 		/* acquire vp resources: regs, irqs, clocks */
8934551789fSSean Paul 		ret = vp_resources_init(mixer_ctx);
8944551789fSSean Paul 		if (ret) {
8954551789fSSean Paul 			DRM_ERROR("vp_resources_init failed ret=%d\n", ret);
8964551789fSSean Paul 			return ret;
8974551789fSSean Paul 		}
8984551789fSSean Paul 	}
8994551789fSSean Paul 
900f44d3d2fSAndrzej Hajda 	return drm_iommu_attach_device(drm_dev, mixer_ctx->dev);
9011055b39fSInki Dae }
9021055b39fSInki Dae 
90393bca243SGustavo Padovan static void mixer_ctx_remove(struct mixer_context *mixer_ctx)
904d8408326SSeung-Woo Kim {
905f041b257SSean Paul 	drm_iommu_detach_device(mixer_ctx->drm_dev, mixer_ctx->dev);
906f041b257SSean Paul }
907f041b257SSean Paul 
90893bca243SGustavo Padovan static int mixer_enable_vblank(struct exynos_drm_crtc *crtc)
909f041b257SSean Paul {
91093bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
911d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
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 */
918fc073248SAndrzej Hajda 	mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
919fc073248SAndrzej Hajda 	mixer_reg_writemask(res, 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 	struct mixer_resources *res = &mixer_ctx->mixer_res;
928d8408326SSeung-Woo Kim 
9290df5e4acSAndrzej Hajda 	__clear_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
9300df5e4acSAndrzej Hajda 
9310df5e4acSAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
932947710c6SAndrzej Hajda 		return;
933947710c6SAndrzej Hajda 
934d8408326SSeung-Woo Kim 	/* disable vsync interrupt */
935fc073248SAndrzej Hajda 	mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
936d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
937d8408326SSeung-Woo Kim }
938d8408326SSeung-Woo Kim 
9393dbaab16SMarek Szyprowski static void mixer_atomic_begin(struct exynos_drm_crtc *crtc)
9403dbaab16SMarek Szyprowski {
9413dbaab16SMarek Szyprowski 	struct mixer_context *mixer_ctx = crtc->ctx;
9423dbaab16SMarek Szyprowski 
9433dbaab16SMarek Szyprowski 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
9443dbaab16SMarek Szyprowski 		return;
9453dbaab16SMarek Szyprowski 
9463dbaab16SMarek Szyprowski 	mixer_vsync_set_update(mixer_ctx, false);
9473dbaab16SMarek Szyprowski }
9483dbaab16SMarek Szyprowski 
9491e1d1393SGustavo Padovan static void mixer_update_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 
95440bdfb0aSMarek Szyprowski 	DRM_DEBUG_KMS("win: %d\n", plane->index);
955d8408326SSeung-Woo Kim 
956a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
957dda9012bSShirish S 		return;
958dda9012bSShirish S 
9595e68fef2SMarek Szyprowski 	if (plane->index == VP_DEFAULT_WIN)
9602eeb2e5eSGustavo Padovan 		vp_video_buffer(mixer_ctx, plane);
961d8408326SSeung-Woo Kim 	else
9622eeb2e5eSGustavo Padovan 		mixer_graph_buffer(mixer_ctx, plane);
963d8408326SSeung-Woo Kim }
964d8408326SSeung-Woo Kim 
9651e1d1393SGustavo Padovan static void mixer_disable_plane(struct exynos_drm_crtc *crtc,
9661e1d1393SGustavo Padovan 				struct exynos_drm_plane *plane)
967d8408326SSeung-Woo Kim {
96893bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
969d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
970d8408326SSeung-Woo Kim 	unsigned long flags;
971d8408326SSeung-Woo Kim 
97240bdfb0aSMarek Szyprowski 	DRM_DEBUG_KMS("win: %d\n", plane->index);
973d8408326SSeung-Woo Kim 
974a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
975db43fd16SPrathyush K 		return;
976db43fd16SPrathyush K 
977d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
978a2cb911eSMarek Szyprowski 	mixer_cfg_layer(mixer_ctx, plane->index, 0, false);
9793dbaab16SMarek Szyprowski 	spin_unlock_irqrestore(&res->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 
989d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, true);
990a392276dSAndrzej Hajda 	exynos_crtc_handle_event(crtc);
991d8408326SSeung-Woo Kim }
992d8408326SSeung-Woo Kim 
9933cecda03SGustavo Padovan static void mixer_enable(struct exynos_drm_crtc *crtc)
994db43fd16SPrathyush K {
9953cecda03SGustavo Padovan 	struct mixer_context *ctx = crtc->ctx;
996db43fd16SPrathyush K 	struct mixer_resources *res = &ctx->mixer_res;
997db43fd16SPrathyush K 
998a44652e8SAndrzej Hajda 	if (test_bit(MXR_BIT_POWERED, &ctx->flags))
999db43fd16SPrathyush K 		return;
1000db43fd16SPrathyush K 
1001af65c804SSean Paul 	pm_runtime_get_sync(ctx->dev);
1002af65c804SSean Paul 
1003a121d179SAndrzej Hajda 	exynos_drm_pipe_clk_enable(crtc, true);
1004a121d179SAndrzej Hajda 
10053dbaab16SMarek Szyprowski 	mixer_vsync_set_update(ctx, false);
10063dbaab16SMarek Szyprowski 
1007d74ed937SRahul Sharma 	mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_SOFT_RESET);
1008d74ed937SRahul Sharma 
10090df5e4acSAndrzej Hajda 	if (test_bit(MXR_BIT_VSYNC, &ctx->flags)) {
1010fc073248SAndrzej Hajda 		mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
10110df5e4acSAndrzej Hajda 		mixer_reg_writemask(res, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
10120df5e4acSAndrzej Hajda 	}
1013db43fd16SPrathyush K 	mixer_win_reset(ctx);
1014ccf034a9SGustavo Padovan 
101571469944SAndrzej Hajda 	mixer_commit(ctx);
101671469944SAndrzej Hajda 
10173dbaab16SMarek Szyprowski 	mixer_vsync_set_update(ctx, true);
10183dbaab16SMarek Szyprowski 
1019ccf034a9SGustavo Padovan 	set_bit(MXR_BIT_POWERED, &ctx->flags);
1020db43fd16SPrathyush K }
1021db43fd16SPrathyush K 
10223cecda03SGustavo Padovan static void mixer_disable(struct exynos_drm_crtc *crtc)
1023db43fd16SPrathyush K {
10243cecda03SGustavo Padovan 	struct mixer_context *ctx = crtc->ctx;
1025c329f667SJoonyoung Shim 	int i;
1026db43fd16SPrathyush K 
1027a44652e8SAndrzej Hajda 	if (!test_bit(MXR_BIT_POWERED, &ctx->flags))
1028b4bfa3c7SRahul Sharma 		return;
1029db43fd16SPrathyush K 
1030381be025SRahul Sharma 	mixer_stop(ctx);
1031c0734fbaSTobias Jakobi 	mixer_regs_dump(ctx);
1032c329f667SJoonyoung Shim 
1033c329f667SJoonyoung Shim 	for (i = 0; i < MIXER_WIN_NR; i++)
10341e1d1393SGustavo Padovan 		mixer_disable_plane(crtc, &ctx->planes[i]);
1035db43fd16SPrathyush K 
1036a121d179SAndrzej Hajda 	exynos_drm_pipe_clk_enable(crtc, false);
1037a121d179SAndrzej Hajda 
1038ccf034a9SGustavo Padovan 	pm_runtime_put(ctx->dev);
1039ccf034a9SGustavo Padovan 
1040a44652e8SAndrzej Hajda 	clear_bit(MXR_BIT_POWERED, &ctx->flags);
1041db43fd16SPrathyush K }
1042db43fd16SPrathyush K 
1043f041b257SSean Paul /* Only valid for Mixer version 16.0.33.0 */
10443ae24362SAndrzej Hajda static int mixer_atomic_check(struct exynos_drm_crtc *crtc,
10453ae24362SAndrzej Hajda 		       struct drm_crtc_state *state)
1046f041b257SSean Paul {
10473ae24362SAndrzej Hajda 	struct drm_display_mode *mode = &state->adjusted_mode;
1048f041b257SSean Paul 	u32 w, h;
1049f041b257SSean Paul 
1050f041b257SSean Paul 	w = mode->hdisplay;
1051f041b257SSean Paul 	h = mode->vdisplay;
1052f041b257SSean Paul 
1053f041b257SSean Paul 	DRM_DEBUG_KMS("xres=%d, yres=%d, refresh=%d, intl=%d\n",
1054f041b257SSean Paul 		mode->hdisplay, mode->vdisplay, mode->vrefresh,
1055f041b257SSean Paul 		(mode->flags & DRM_MODE_FLAG_INTERLACE) ? 1 : 0);
1056f041b257SSean Paul 
1057f041b257SSean Paul 	if ((w >= 464 && w <= 720 && h >= 261 && h <= 576) ||
1058f041b257SSean Paul 		(w >= 1024 && w <= 1280 && h >= 576 && h <= 720) ||
1059f041b257SSean Paul 		(w >= 1664 && w <= 1920 && h >= 936 && h <= 1080))
1060f041b257SSean Paul 		return 0;
1061f041b257SSean Paul 
1062f041b257SSean Paul 	return -EINVAL;
1063f041b257SSean Paul }
1064f041b257SSean Paul 
1065f3aaf762SKrzysztof Kozlowski static const struct exynos_drm_crtc_ops mixer_crtc_ops = {
10663cecda03SGustavo Padovan 	.enable			= mixer_enable,
10673cecda03SGustavo Padovan 	.disable		= mixer_disable,
1068d8408326SSeung-Woo Kim 	.enable_vblank		= mixer_enable_vblank,
1069d8408326SSeung-Woo Kim 	.disable_vblank		= mixer_disable_vblank,
10703dbaab16SMarek Szyprowski 	.atomic_begin		= mixer_atomic_begin,
10719cc7610aSGustavo Padovan 	.update_plane		= mixer_update_plane,
10729cc7610aSGustavo Padovan 	.disable_plane		= mixer_disable_plane,
10733dbaab16SMarek Szyprowski 	.atomic_flush		= mixer_atomic_flush,
10743ae24362SAndrzej Hajda 	.atomic_check		= mixer_atomic_check,
1075f041b257SSean Paul };
10760ea6822fSRahul Sharma 
10775e6cc1c5SArvind Yadav static const struct mixer_drv_data exynos5420_mxr_drv_data = {
1078def5e095SRahul Sharma 	.version = MXR_VER_128_0_0_184,
1079def5e095SRahul Sharma 	.is_vp_enabled = 0,
1080def5e095SRahul Sharma };
1081def5e095SRahul Sharma 
10825e6cc1c5SArvind Yadav static const struct mixer_drv_data exynos5250_mxr_drv_data = {
1083aaf8b49eSRahul Sharma 	.version = MXR_VER_16_0_33_0,
1084aaf8b49eSRahul Sharma 	.is_vp_enabled = 0,
1085aaf8b49eSRahul Sharma };
1086aaf8b49eSRahul Sharma 
10875e6cc1c5SArvind Yadav static const struct mixer_drv_data exynos4212_mxr_drv_data = {
1088ff830c96SMarek Szyprowski 	.version = MXR_VER_0_0_0_16,
1089ff830c96SMarek Szyprowski 	.is_vp_enabled = 1,
1090ff830c96SMarek Szyprowski };
1091ff830c96SMarek Szyprowski 
10925e6cc1c5SArvind Yadav static const struct mixer_drv_data exynos4210_mxr_drv_data = {
10931e123441SRahul Sharma 	.version = MXR_VER_0_0_0_16,
10941b8e5747SRahul Sharma 	.is_vp_enabled = 1,
1095ff830c96SMarek Szyprowski 	.has_sclk = 1,
10961e123441SRahul Sharma };
10971e123441SRahul Sharma 
10985e6cc1c5SArvind Yadav static const struct of_device_id mixer_match_types[] = {
1099aaf8b49eSRahul Sharma 	{
1100ff830c96SMarek Szyprowski 		.compatible = "samsung,exynos4210-mixer",
1101ff830c96SMarek Szyprowski 		.data	= &exynos4210_mxr_drv_data,
1102ff830c96SMarek Szyprowski 	}, {
1103ff830c96SMarek Szyprowski 		.compatible = "samsung,exynos4212-mixer",
1104ff830c96SMarek Szyprowski 		.data	= &exynos4212_mxr_drv_data,
1105ff830c96SMarek Szyprowski 	}, {
1106aaf8b49eSRahul Sharma 		.compatible = "samsung,exynos5-mixer",
1107cc57caf0SRahul Sharma 		.data	= &exynos5250_mxr_drv_data,
1108cc57caf0SRahul Sharma 	}, {
1109cc57caf0SRahul Sharma 		.compatible = "samsung,exynos5250-mixer",
1110cc57caf0SRahul Sharma 		.data	= &exynos5250_mxr_drv_data,
1111aaf8b49eSRahul Sharma 	}, {
1112def5e095SRahul Sharma 		.compatible = "samsung,exynos5420-mixer",
1113def5e095SRahul Sharma 		.data	= &exynos5420_mxr_drv_data,
1114def5e095SRahul Sharma 	}, {
11151e123441SRahul Sharma 		/* end node */
11161e123441SRahul Sharma 	}
11171e123441SRahul Sharma };
111839b58a39SSjoerd Simons MODULE_DEVICE_TABLE(of, mixer_match_types);
11191e123441SRahul Sharma 
1120f37cd5e8SInki Dae static int mixer_bind(struct device *dev, struct device *manager, void *data)
1121d8408326SSeung-Woo Kim {
11228103ef1bSAndrzej Hajda 	struct mixer_context *ctx = dev_get_drvdata(dev);
1123f37cd5e8SInki Dae 	struct drm_device *drm_dev = data;
11247ee14cdcSGustavo Padovan 	struct exynos_drm_plane *exynos_plane;
1125fd2d2fc2SMarek Szyprowski 	unsigned int i;
11266e2a3b66SGustavo Padovan 	int ret;
1127d8408326SSeung-Woo Kim 
1128e2dc3f72SAlban Browaeys 	ret = mixer_initialize(ctx, drm_dev);
1129e2dc3f72SAlban Browaeys 	if (ret)
1130e2dc3f72SAlban Browaeys 		return ret;
1131e2dc3f72SAlban Browaeys 
1132fd2d2fc2SMarek Szyprowski 	for (i = 0; i < MIXER_WIN_NR; i++) {
1133adeb6f44STobias Jakobi 		if (i == VP_DEFAULT_WIN && !test_bit(MXR_BIT_VP_ENABLED,
1134adeb6f44STobias Jakobi 						     &ctx->flags))
1135ab144201SMarek Szyprowski 			continue;
1136ab144201SMarek Szyprowski 
113740bdfb0aSMarek Szyprowski 		ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
11382c82607bSAndrzej Hajda 					&plane_configs[i]);
11397ee14cdcSGustavo Padovan 		if (ret)
11407ee14cdcSGustavo Padovan 			return ret;
11417ee14cdcSGustavo Padovan 	}
11427ee14cdcSGustavo Padovan 
11435d3d0995SGustavo Padovan 	exynos_plane = &ctx->planes[DEFAULT_WIN];
11447ee14cdcSGustavo Padovan 	ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
1145d644951cSAndrzej Hajda 			EXYNOS_DISPLAY_TYPE_HDMI, &mixer_crtc_ops, ctx);
114693bca243SGustavo Padovan 	if (IS_ERR(ctx->crtc)) {
1147e2dc3f72SAlban Browaeys 		mixer_ctx_remove(ctx);
114893bca243SGustavo Padovan 		ret = PTR_ERR(ctx->crtc);
114993bca243SGustavo Padovan 		goto free_ctx;
11508103ef1bSAndrzej Hajda 	}
11518103ef1bSAndrzej Hajda 
11528103ef1bSAndrzej Hajda 	return 0;
115393bca243SGustavo Padovan 
115493bca243SGustavo Padovan free_ctx:
115593bca243SGustavo Padovan 	devm_kfree(dev, ctx);
115693bca243SGustavo Padovan 	return ret;
11578103ef1bSAndrzej Hajda }
11588103ef1bSAndrzej Hajda 
11598103ef1bSAndrzej Hajda static void mixer_unbind(struct device *dev, struct device *master, void *data)
11608103ef1bSAndrzej Hajda {
11618103ef1bSAndrzej Hajda 	struct mixer_context *ctx = dev_get_drvdata(dev);
11628103ef1bSAndrzej Hajda 
116393bca243SGustavo Padovan 	mixer_ctx_remove(ctx);
11648103ef1bSAndrzej Hajda }
11658103ef1bSAndrzej Hajda 
11668103ef1bSAndrzej Hajda static const struct component_ops mixer_component_ops = {
11678103ef1bSAndrzej Hajda 	.bind	= mixer_bind,
11688103ef1bSAndrzej Hajda 	.unbind	= mixer_unbind,
11698103ef1bSAndrzej Hajda };
11708103ef1bSAndrzej Hajda 
11718103ef1bSAndrzej Hajda static int mixer_probe(struct platform_device *pdev)
11728103ef1bSAndrzej Hajda {
11738103ef1bSAndrzej Hajda 	struct device *dev = &pdev->dev;
117448f6155aSMarek Szyprowski 	const struct mixer_drv_data *drv;
11758103ef1bSAndrzej Hajda 	struct mixer_context *ctx;
11768103ef1bSAndrzej Hajda 	int ret;
1177d8408326SSeung-Woo Kim 
1178f041b257SSean Paul 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1179f041b257SSean Paul 	if (!ctx) {
1180f041b257SSean Paul 		DRM_ERROR("failed to alloc mixer context.\n");
1181d8408326SSeung-Woo Kim 		return -ENOMEM;
1182f041b257SSean Paul 	}
1183d8408326SSeung-Woo Kim 
118448f6155aSMarek Szyprowski 	drv = of_device_get_match_data(dev);
1185aaf8b49eSRahul Sharma 
11864551789fSSean Paul 	ctx->pdev = pdev;
1187d873ab99SSeung-Woo Kim 	ctx->dev = dev;
11881e123441SRahul Sharma 	ctx->mxr_ver = drv->version;
1189d8408326SSeung-Woo Kim 
1190adeb6f44STobias Jakobi 	if (drv->is_vp_enabled)
1191adeb6f44STobias Jakobi 		__set_bit(MXR_BIT_VP_ENABLED, &ctx->flags);
1192adeb6f44STobias Jakobi 	if (drv->has_sclk)
1193adeb6f44STobias Jakobi 		__set_bit(MXR_BIT_HAS_SCLK, &ctx->flags);
1194adeb6f44STobias Jakobi 
11958103ef1bSAndrzej Hajda 	platform_set_drvdata(pdev, ctx);
1196df5225bcSInki Dae 
1197df5225bcSInki Dae 	ret = component_add(&pdev->dev, &mixer_component_ops);
119886650408SAndrzej Hajda 	if (!ret)
11998103ef1bSAndrzej Hajda 		pm_runtime_enable(dev);
1200df5225bcSInki Dae 
1201df5225bcSInki Dae 	return ret;
1202f37cd5e8SInki Dae }
1203f37cd5e8SInki Dae 
1204d8408326SSeung-Woo Kim static int mixer_remove(struct platform_device *pdev)
1205d8408326SSeung-Woo Kim {
12068103ef1bSAndrzej Hajda 	pm_runtime_disable(&pdev->dev);
12078103ef1bSAndrzej Hajda 
1208df5225bcSInki Dae 	component_del(&pdev->dev, &mixer_component_ops);
1209df5225bcSInki Dae 
1210d8408326SSeung-Woo Kim 	return 0;
1211d8408326SSeung-Woo Kim }
1212d8408326SSeung-Woo Kim 
1213e0fea7e7SArnd Bergmann static int __maybe_unused exynos_mixer_suspend(struct device *dev)
1214ccf034a9SGustavo Padovan {
1215ccf034a9SGustavo Padovan 	struct mixer_context *ctx = dev_get_drvdata(dev);
1216ccf034a9SGustavo Padovan 	struct mixer_resources *res = &ctx->mixer_res;
1217ccf034a9SGustavo Padovan 
1218ccf034a9SGustavo Padovan 	clk_disable_unprepare(res->hdmi);
1219ccf034a9SGustavo Padovan 	clk_disable_unprepare(res->mixer);
1220adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
1221ccf034a9SGustavo Padovan 		clk_disable_unprepare(res->vp);
1222adeb6f44STobias Jakobi 		if (test_bit(MXR_BIT_HAS_SCLK, &ctx->flags))
1223ccf034a9SGustavo Padovan 			clk_disable_unprepare(res->sclk_mixer);
1224ccf034a9SGustavo Padovan 	}
1225ccf034a9SGustavo Padovan 
1226ccf034a9SGustavo Padovan 	return 0;
1227ccf034a9SGustavo Padovan }
1228ccf034a9SGustavo Padovan 
1229e0fea7e7SArnd Bergmann static int __maybe_unused exynos_mixer_resume(struct device *dev)
1230ccf034a9SGustavo Padovan {
1231ccf034a9SGustavo Padovan 	struct mixer_context *ctx = dev_get_drvdata(dev);
1232ccf034a9SGustavo Padovan 	struct mixer_resources *res = &ctx->mixer_res;
1233ccf034a9SGustavo Padovan 	int ret;
1234ccf034a9SGustavo Padovan 
1235ccf034a9SGustavo Padovan 	ret = clk_prepare_enable(res->mixer);
1236ccf034a9SGustavo Padovan 	if (ret < 0) {
1237ccf034a9SGustavo Padovan 		DRM_ERROR("Failed to prepare_enable the mixer clk [%d]\n", ret);
1238ccf034a9SGustavo Padovan 		return ret;
1239ccf034a9SGustavo Padovan 	}
1240ccf034a9SGustavo Padovan 	ret = clk_prepare_enable(res->hdmi);
1241ccf034a9SGustavo Padovan 	if (ret < 0) {
1242ccf034a9SGustavo Padovan 		DRM_ERROR("Failed to prepare_enable the hdmi clk [%d]\n", ret);
1243ccf034a9SGustavo Padovan 		return ret;
1244ccf034a9SGustavo Padovan 	}
1245adeb6f44STobias Jakobi 	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
1246ccf034a9SGustavo Padovan 		ret = clk_prepare_enable(res->vp);
1247ccf034a9SGustavo Padovan 		if (ret < 0) {
1248ccf034a9SGustavo Padovan 			DRM_ERROR("Failed to prepare_enable the vp clk [%d]\n",
1249ccf034a9SGustavo Padovan 				  ret);
1250ccf034a9SGustavo Padovan 			return ret;
1251ccf034a9SGustavo Padovan 		}
1252adeb6f44STobias Jakobi 		if (test_bit(MXR_BIT_HAS_SCLK, &ctx->flags)) {
1253ccf034a9SGustavo Padovan 			ret = clk_prepare_enable(res->sclk_mixer);
1254ccf034a9SGustavo Padovan 			if (ret < 0) {
1255ccf034a9SGustavo Padovan 				DRM_ERROR("Failed to prepare_enable the " \
1256ccf034a9SGustavo Padovan 					   "sclk_mixer clk [%d]\n",
1257ccf034a9SGustavo Padovan 					  ret);
1258ccf034a9SGustavo Padovan 				return ret;
1259ccf034a9SGustavo Padovan 			}
1260ccf034a9SGustavo Padovan 		}
1261ccf034a9SGustavo Padovan 	}
1262ccf034a9SGustavo Padovan 
1263ccf034a9SGustavo Padovan 	return 0;
1264ccf034a9SGustavo Padovan }
1265ccf034a9SGustavo Padovan 
1266ccf034a9SGustavo Padovan static const struct dev_pm_ops exynos_mixer_pm_ops = {
1267ccf034a9SGustavo Padovan 	SET_RUNTIME_PM_OPS(exynos_mixer_suspend, exynos_mixer_resume, NULL)
1268ccf034a9SGustavo Padovan };
1269ccf034a9SGustavo Padovan 
1270d8408326SSeung-Woo Kim struct platform_driver mixer_driver = {
1271d8408326SSeung-Woo Kim 	.driver = {
1272aaf8b49eSRahul Sharma 		.name = "exynos-mixer",
1273d8408326SSeung-Woo Kim 		.owner = THIS_MODULE,
1274ccf034a9SGustavo Padovan 		.pm = &exynos_mixer_pm_ops,
1275aaf8b49eSRahul Sharma 		.of_match_table = mixer_match_types,
1276d8408326SSeung-Woo Kim 	},
1277d8408326SSeung-Woo Kim 	.probe = mixer_probe,
127856550d94SGreg Kroah-Hartman 	.remove = mixer_remove,
1279d8408326SSeung-Woo Kim };
1280