xref: /linux/drivers/gpu/drm/exynos/exynos_mixer.c (revision 7ee14cdcbc4f813b9c5875d6e8e3daef71c366b3)
1d8408326SSeung-Woo Kim /*
2d8408326SSeung-Woo Kim  * Copyright (C) 2011 Samsung Electronics Co.Ltd
3d8408326SSeung-Woo Kim  * Authors:
4d8408326SSeung-Woo Kim  * Seung-Woo Kim <sw0312.kim@samsung.com>
5d8408326SSeung-Woo Kim  *	Inki Dae <inki.dae@samsung.com>
6d8408326SSeung-Woo Kim  *	Joonyoung Shim <jy0922.shim@samsung.com>
7d8408326SSeung-Woo Kim  *
8d8408326SSeung-Woo Kim  * Based on drivers/media/video/s5p-tv/mixer_reg.c
9d8408326SSeung-Woo Kim  *
10d8408326SSeung-Woo Kim  * This program is free software; you can redistribute  it and/or modify it
11d8408326SSeung-Woo Kim  * under  the terms of  the GNU General  Public License as published by the
12d8408326SSeung-Woo Kim  * Free Software Foundation;  either version 2 of the  License, or (at your
13d8408326SSeung-Woo Kim  * option) any later version.
14d8408326SSeung-Woo Kim  *
15d8408326SSeung-Woo Kim  */
16d8408326SSeung-Woo Kim 
17760285e7SDavid Howells #include <drm/drmP.h>
18d8408326SSeung-Woo Kim 
19d8408326SSeung-Woo Kim #include "regs-mixer.h"
20d8408326SSeung-Woo Kim #include "regs-vp.h"
21d8408326SSeung-Woo Kim 
22d8408326SSeung-Woo Kim #include <linux/kernel.h>
23d8408326SSeung-Woo Kim #include <linux/spinlock.h>
24d8408326SSeung-Woo Kim #include <linux/wait.h>
25d8408326SSeung-Woo Kim #include <linux/i2c.h>
26d8408326SSeung-Woo Kim #include <linux/platform_device.h>
27d8408326SSeung-Woo Kim #include <linux/interrupt.h>
28d8408326SSeung-Woo Kim #include <linux/irq.h>
29d8408326SSeung-Woo Kim #include <linux/delay.h>
30d8408326SSeung-Woo Kim #include <linux/pm_runtime.h>
31d8408326SSeung-Woo Kim #include <linux/clk.h>
32d8408326SSeung-Woo Kim #include <linux/regulator/consumer.h>
333f1c781dSSachin Kamat #include <linux/of.h>
34f37cd5e8SInki Dae #include <linux/component.h>
35d8408326SSeung-Woo Kim 
36d8408326SSeung-Woo Kim #include <drm/exynos_drm.h>
37d8408326SSeung-Woo Kim 
38d8408326SSeung-Woo Kim #include "exynos_drm_drv.h"
39663d8766SRahul Sharma #include "exynos_drm_crtc.h"
40*7ee14cdcSGustavo Padovan #include "exynos_drm_plane.h"
411055b39fSInki Dae #include "exynos_drm_iommu.h"
42f041b257SSean Paul #include "exynos_mixer.h"
4322b21ae6SJoonyoung Shim 
44f041b257SSean Paul #define MIXER_WIN_NR		3
45f041b257SSean Paul #define MIXER_DEFAULT_WIN	0
46d8408326SSeung-Woo Kim 
4722b21ae6SJoonyoung Shim struct mixer_resources {
4822b21ae6SJoonyoung Shim 	int			irq;
4922b21ae6SJoonyoung Shim 	void __iomem		*mixer_regs;
5022b21ae6SJoonyoung Shim 	void __iomem		*vp_regs;
5122b21ae6SJoonyoung Shim 	spinlock_t		reg_slock;
5222b21ae6SJoonyoung Shim 	struct clk		*mixer;
5322b21ae6SJoonyoung Shim 	struct clk		*vp;
5404427ec5SMarek Szyprowski 	struct clk		*hdmi;
5522b21ae6SJoonyoung Shim 	struct clk		*sclk_mixer;
5622b21ae6SJoonyoung Shim 	struct clk		*sclk_hdmi;
57ff830c96SMarek Szyprowski 	struct clk		*mout_mixer;
5822b21ae6SJoonyoung Shim };
5922b21ae6SJoonyoung Shim 
601e123441SRahul Sharma enum mixer_version_id {
611e123441SRahul Sharma 	MXR_VER_0_0_0_16,
621e123441SRahul Sharma 	MXR_VER_16_0_33_0,
63def5e095SRahul Sharma 	MXR_VER_128_0_0_184,
641e123441SRahul Sharma };
651e123441SRahul Sharma 
6622b21ae6SJoonyoung Shim struct mixer_context {
674551789fSSean Paul 	struct platform_device *pdev;
68cf8fc4f1SJoonyoung Shim 	struct device		*dev;
691055b39fSInki Dae 	struct drm_device	*drm_dev;
7093bca243SGustavo Padovan 	struct exynos_drm_crtc	*crtc;
71*7ee14cdcSGustavo Padovan 	struct exynos_drm_plane	planes[MIXER_WIN_NR];
7222b21ae6SJoonyoung Shim 	int			pipe;
7322b21ae6SJoonyoung Shim 	bool			interlace;
74cf8fc4f1SJoonyoung Shim 	bool			powered;
751b8e5747SRahul Sharma 	bool			vp_enabled;
76ff830c96SMarek Szyprowski 	bool			has_sclk;
77cf8fc4f1SJoonyoung Shim 	u32			int_en;
7822b21ae6SJoonyoung Shim 
79cf8fc4f1SJoonyoung Shim 	struct mutex		mixer_mutex;
8022b21ae6SJoonyoung Shim 	struct mixer_resources	mixer_res;
811e123441SRahul Sharma 	enum mixer_version_id	mxr_ver;
826e95d5e6SPrathyush K 	wait_queue_head_t	wait_vsync_queue;
836e95d5e6SPrathyush K 	atomic_t		wait_vsync_event;
841e123441SRahul Sharma };
851e123441SRahul Sharma 
861e123441SRahul Sharma struct mixer_drv_data {
871e123441SRahul Sharma 	enum mixer_version_id	version;
881b8e5747SRahul Sharma 	bool					is_vp_enabled;
89ff830c96SMarek Szyprowski 	bool					has_sclk;
9022b21ae6SJoonyoung Shim };
9122b21ae6SJoonyoung Shim 
92d8408326SSeung-Woo Kim static const u8 filter_y_horiz_tap8[] = {
93d8408326SSeung-Woo Kim 	0,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
94d8408326SSeung-Woo Kim 	-1,	-1,	-1,	-1,	-1,	0,	0,	0,
95d8408326SSeung-Woo Kim 	0,	2,	4,	5,	6,	6,	6,	6,
96d8408326SSeung-Woo Kim 	6,	5,	5,	4,	3,	2,	1,	1,
97d8408326SSeung-Woo Kim 	0,	-6,	-12,	-16,	-18,	-20,	-21,	-20,
98d8408326SSeung-Woo Kim 	-20,	-18,	-16,	-13,	-10,	-8,	-5,	-2,
99d8408326SSeung-Woo Kim 	127,	126,	125,	121,	114,	107,	99,	89,
100d8408326SSeung-Woo Kim 	79,	68,	57,	46,	35,	25,	16,	8,
101d8408326SSeung-Woo Kim };
102d8408326SSeung-Woo Kim 
103d8408326SSeung-Woo Kim static const u8 filter_y_vert_tap4[] = {
104d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
105d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
106d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
107d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
108d8408326SSeung-Woo Kim 	0,	5,	11,	19,	27,	37,	48,	59,
109d8408326SSeung-Woo Kim 	70,	81,	92,	102,	111,	118,	124,	126,
110d8408326SSeung-Woo Kim 	0,	0,	-1,	-1,	-2,	-3,	-4,	-5,
111d8408326SSeung-Woo Kim 	-6,	-7,	-8,	-8,	-8,	-8,	-6,	-3,
112d8408326SSeung-Woo Kim };
113d8408326SSeung-Woo Kim 
114d8408326SSeung-Woo Kim static const u8 filter_cr_horiz_tap4[] = {
115d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
116d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
117d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
118d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
119d8408326SSeung-Woo Kim };
120d8408326SSeung-Woo Kim 
121d8408326SSeung-Woo Kim static inline u32 vp_reg_read(struct mixer_resources *res, u32 reg_id)
122d8408326SSeung-Woo Kim {
123d8408326SSeung-Woo Kim 	return readl(res->vp_regs + reg_id);
124d8408326SSeung-Woo Kim }
125d8408326SSeung-Woo Kim 
126d8408326SSeung-Woo Kim static inline void vp_reg_write(struct mixer_resources *res, u32 reg_id,
127d8408326SSeung-Woo Kim 				 u32 val)
128d8408326SSeung-Woo Kim {
129d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
130d8408326SSeung-Woo Kim }
131d8408326SSeung-Woo Kim 
132d8408326SSeung-Woo Kim static inline void vp_reg_writemask(struct mixer_resources *res, u32 reg_id,
133d8408326SSeung-Woo Kim 				 u32 val, u32 mask)
134d8408326SSeung-Woo Kim {
135d8408326SSeung-Woo Kim 	u32 old = vp_reg_read(res, reg_id);
136d8408326SSeung-Woo Kim 
137d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
138d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
139d8408326SSeung-Woo Kim }
140d8408326SSeung-Woo Kim 
141d8408326SSeung-Woo Kim static inline u32 mixer_reg_read(struct mixer_resources *res, u32 reg_id)
142d8408326SSeung-Woo Kim {
143d8408326SSeung-Woo Kim 	return readl(res->mixer_regs + reg_id);
144d8408326SSeung-Woo Kim }
145d8408326SSeung-Woo Kim 
146d8408326SSeung-Woo Kim static inline void mixer_reg_write(struct mixer_resources *res, u32 reg_id,
147d8408326SSeung-Woo Kim 				 u32 val)
148d8408326SSeung-Woo Kim {
149d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
150d8408326SSeung-Woo Kim }
151d8408326SSeung-Woo Kim 
152d8408326SSeung-Woo Kim static inline void mixer_reg_writemask(struct mixer_resources *res,
153d8408326SSeung-Woo Kim 				 u32 reg_id, u32 val, u32 mask)
154d8408326SSeung-Woo Kim {
155d8408326SSeung-Woo Kim 	u32 old = mixer_reg_read(res, reg_id);
156d8408326SSeung-Woo Kim 
157d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
158d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
159d8408326SSeung-Woo Kim }
160d8408326SSeung-Woo Kim 
161d8408326SSeung-Woo Kim static void mixer_regs_dump(struct mixer_context *ctx)
162d8408326SSeung-Woo Kim {
163d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
164d8408326SSeung-Woo Kim do { \
165d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
166d8408326SSeung-Woo Kim 		(u32)readl(ctx->mixer_res.mixer_regs + reg_id)); \
167d8408326SSeung-Woo Kim } while (0)
168d8408326SSeung-Woo Kim 
169d8408326SSeung-Woo Kim 	DUMPREG(MXR_STATUS);
170d8408326SSeung-Woo Kim 	DUMPREG(MXR_CFG);
171d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_EN);
172d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_STATUS);
173d8408326SSeung-Woo Kim 
174d8408326SSeung-Woo Kim 	DUMPREG(MXR_LAYER_CFG);
175d8408326SSeung-Woo Kim 	DUMPREG(MXR_VIDEO_CFG);
176d8408326SSeung-Woo Kim 
177d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_CFG);
178d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_BASE);
179d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SPAN);
180d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_WH);
181d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SXY);
182d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_DXY);
183d8408326SSeung-Woo Kim 
184d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_CFG);
185d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_BASE);
186d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SPAN);
187d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_WH);
188d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SXY);
189d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_DXY);
190d8408326SSeung-Woo Kim #undef DUMPREG
191d8408326SSeung-Woo Kim }
192d8408326SSeung-Woo Kim 
193d8408326SSeung-Woo Kim static void vp_regs_dump(struct mixer_context *ctx)
194d8408326SSeung-Woo Kim {
195d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
196d8408326SSeung-Woo Kim do { \
197d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
198d8408326SSeung-Woo Kim 		(u32) readl(ctx->mixer_res.vp_regs + reg_id)); \
199d8408326SSeung-Woo Kim } while (0)
200d8408326SSeung-Woo Kim 
201d8408326SSeung-Woo Kim 	DUMPREG(VP_ENABLE);
202d8408326SSeung-Woo Kim 	DUMPREG(VP_SRESET);
203d8408326SSeung-Woo Kim 	DUMPREG(VP_SHADOW_UPDATE);
204d8408326SSeung-Woo Kim 	DUMPREG(VP_FIELD_ID);
205d8408326SSeung-Woo Kim 	DUMPREG(VP_MODE);
206d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_Y);
207d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_C);
208d8408326SSeung-Woo Kim 	DUMPREG(VP_PER_RATE_CTRL);
209d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_Y_PTR);
210d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_Y_PTR);
211d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_C_PTR);
212d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_C_PTR);
213d8408326SSeung-Woo Kim 	DUMPREG(VP_ENDIAN_MODE);
214d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_H_POSITION);
215d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_V_POSITION);
216d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_WIDTH);
217d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_HEIGHT);
218d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_H_POSITION);
219d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_V_POSITION);
220d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_WIDTH);
221d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_HEIGHT);
222d8408326SSeung-Woo Kim 	DUMPREG(VP_H_RATIO);
223d8408326SSeung-Woo Kim 	DUMPREG(VP_V_RATIO);
224d8408326SSeung-Woo Kim 
225d8408326SSeung-Woo Kim #undef DUMPREG
226d8408326SSeung-Woo Kim }
227d8408326SSeung-Woo Kim 
228d8408326SSeung-Woo Kim static inline void vp_filter_set(struct mixer_resources *res,
229d8408326SSeung-Woo Kim 		int reg_id, const u8 *data, unsigned int size)
230d8408326SSeung-Woo Kim {
231d8408326SSeung-Woo Kim 	/* assure 4-byte align */
232d8408326SSeung-Woo Kim 	BUG_ON(size & 3);
233d8408326SSeung-Woo Kim 	for (; size; size -= 4, reg_id += 4, data += 4) {
234d8408326SSeung-Woo Kim 		u32 val = (data[0] << 24) |  (data[1] << 16) |
235d8408326SSeung-Woo Kim 			(data[2] << 8) | data[3];
236d8408326SSeung-Woo Kim 		vp_reg_write(res, reg_id, val);
237d8408326SSeung-Woo Kim 	}
238d8408326SSeung-Woo Kim }
239d8408326SSeung-Woo Kim 
240d8408326SSeung-Woo Kim static void vp_default_filter(struct mixer_resources *res)
241d8408326SSeung-Woo Kim {
242d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY8_Y0_LL,
243e25e1b66SSachin Kamat 		filter_y_horiz_tap8, sizeof(filter_y_horiz_tap8));
244d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_Y0_LL,
245e25e1b66SSachin Kamat 		filter_y_vert_tap4, sizeof(filter_y_vert_tap4));
246d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_C0_LL,
247e25e1b66SSachin Kamat 		filter_cr_horiz_tap4, sizeof(filter_cr_horiz_tap4));
248d8408326SSeung-Woo Kim }
249d8408326SSeung-Woo Kim 
250d8408326SSeung-Woo Kim static void mixer_vsync_set_update(struct mixer_context *ctx, bool enable)
251d8408326SSeung-Woo Kim {
252d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
253d8408326SSeung-Woo Kim 
254d8408326SSeung-Woo Kim 	/* block update on vsync */
255d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, enable ?
256d8408326SSeung-Woo Kim 			MXR_STATUS_SYNC_ENABLE : 0, MXR_STATUS_SYNC_ENABLE);
257d8408326SSeung-Woo Kim 
2581b8e5747SRahul Sharma 	if (ctx->vp_enabled)
259d8408326SSeung-Woo Kim 		vp_reg_write(res, VP_SHADOW_UPDATE, enable ?
260d8408326SSeung-Woo Kim 			VP_SHADOW_UPDATE_ENABLE : 0);
261d8408326SSeung-Woo Kim }
262d8408326SSeung-Woo Kim 
263d8408326SSeung-Woo Kim static void mixer_cfg_scan(struct mixer_context *ctx, unsigned int height)
264d8408326SSeung-Woo Kim {
265d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
266d8408326SSeung-Woo Kim 	u32 val;
267d8408326SSeung-Woo Kim 
268d8408326SSeung-Woo Kim 	/* choosing between interlace and progressive mode */
269d8408326SSeung-Woo Kim 	val = (ctx->interlace ? MXR_CFG_SCAN_INTERLACE :
270d8408326SSeung-Woo Kim 				MXR_CFG_SCAN_PROGRASSIVE);
271d8408326SSeung-Woo Kim 
272def5e095SRahul Sharma 	if (ctx->mxr_ver != MXR_VER_128_0_0_184) {
273def5e095SRahul Sharma 		/* choosing between proper HD and SD mode */
27429630743SRahul Sharma 		if (height <= 480)
275d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD;
27629630743SRahul Sharma 		else if (height <= 576)
277d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD;
27829630743SRahul Sharma 		else if (height <= 720)
279d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
28029630743SRahul Sharma 		else if (height <= 1080)
281d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_1080 | MXR_CFG_SCAN_HD;
282d8408326SSeung-Woo Kim 		else
283d8408326SSeung-Woo Kim 			val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
284def5e095SRahul Sharma 	}
285d8408326SSeung-Woo Kim 
286d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_SCAN_MASK);
287d8408326SSeung-Woo Kim }
288d8408326SSeung-Woo Kim 
289d8408326SSeung-Woo Kim static void mixer_cfg_rgb_fmt(struct mixer_context *ctx, unsigned int height)
290d8408326SSeung-Woo Kim {
291d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
292d8408326SSeung-Woo Kim 	u32 val;
293d8408326SSeung-Woo Kim 
294d8408326SSeung-Woo Kim 	if (height == 480) {
295d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
296d8408326SSeung-Woo Kim 	} else if (height == 576) {
297d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
298d8408326SSeung-Woo Kim 	} else if (height == 720) {
299d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
300d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
301d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
302d8408326SSeung-Woo Kim 				(32 << 0));
303d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
304d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
305d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
306d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
307d8408326SSeung-Woo Kim 	} else if (height == 1080) {
308d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
309d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
310d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
311d8408326SSeung-Woo Kim 				(32 << 0));
312d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
313d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
314d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
315d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
316d8408326SSeung-Woo Kim 	} else {
317d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
318d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
319d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
320d8408326SSeung-Woo Kim 				(32 << 0));
321d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
322d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
323d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
324d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
325d8408326SSeung-Woo Kim 	}
326d8408326SSeung-Woo Kim 
327d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
328d8408326SSeung-Woo Kim }
329d8408326SSeung-Woo Kim 
330d8408326SSeung-Woo Kim static void mixer_cfg_layer(struct mixer_context *ctx, int win, bool enable)
331d8408326SSeung-Woo Kim {
332d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
333d8408326SSeung-Woo Kim 	u32 val = enable ? ~0 : 0;
334d8408326SSeung-Woo Kim 
335d8408326SSeung-Woo Kim 	switch (win) {
336d8408326SSeung-Woo Kim 	case 0:
337d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
338d8408326SSeung-Woo Kim 		break;
339d8408326SSeung-Woo Kim 	case 1:
340d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
341d8408326SSeung-Woo Kim 		break;
342d8408326SSeung-Woo Kim 	case 2:
3431b8e5747SRahul Sharma 		if (ctx->vp_enabled) {
344d8408326SSeung-Woo Kim 			vp_reg_writemask(res, VP_ENABLE, val, VP_ENABLE_ON);
3451b8e5747SRahul Sharma 			mixer_reg_writemask(res, MXR_CFG, val,
3461b8e5747SRahul Sharma 				MXR_CFG_VP_ENABLE);
347f1e716d8SJoonyoung Shim 
348f1e716d8SJoonyoung Shim 			/* control blending of graphic layer 0 */
349f1e716d8SJoonyoung Shim 			mixer_reg_writemask(res, MXR_GRAPHIC_CFG(0), val,
350f1e716d8SJoonyoung Shim 					MXR_GRP_CFG_BLEND_PRE_MUL |
351f1e716d8SJoonyoung Shim 					MXR_GRP_CFG_PIXEL_BLEND_EN);
3521b8e5747SRahul Sharma 		}
353d8408326SSeung-Woo Kim 		break;
354d8408326SSeung-Woo Kim 	}
355d8408326SSeung-Woo Kim }
356d8408326SSeung-Woo Kim 
357d8408326SSeung-Woo Kim static void mixer_run(struct mixer_context *ctx)
358d8408326SSeung-Woo Kim {
359d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
360d8408326SSeung-Woo Kim 
361d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
362d8408326SSeung-Woo Kim 
363d8408326SSeung-Woo Kim 	mixer_regs_dump(ctx);
364d8408326SSeung-Woo Kim }
365d8408326SSeung-Woo Kim 
366381be025SRahul Sharma static void mixer_stop(struct mixer_context *ctx)
367381be025SRahul Sharma {
368381be025SRahul Sharma 	struct mixer_resources *res = &ctx->mixer_res;
369381be025SRahul Sharma 	int timeout = 20;
370381be025SRahul Sharma 
371381be025SRahul Sharma 	mixer_reg_writemask(res, MXR_STATUS, 0, MXR_STATUS_REG_RUN);
372381be025SRahul Sharma 
373381be025SRahul Sharma 	while (!(mixer_reg_read(res, MXR_STATUS) & MXR_STATUS_REG_IDLE) &&
374381be025SRahul Sharma 			--timeout)
375381be025SRahul Sharma 		usleep_range(10000, 12000);
376381be025SRahul Sharma 
377381be025SRahul Sharma 	mixer_regs_dump(ctx);
378381be025SRahul Sharma }
379381be025SRahul Sharma 
380d8408326SSeung-Woo Kim static void vp_video_buffer(struct mixer_context *ctx, int win)
381d8408326SSeung-Woo Kim {
382d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
383d8408326SSeung-Woo Kim 	unsigned long flags;
384*7ee14cdcSGustavo Padovan 	struct exynos_drm_plane *plane;
385d8408326SSeung-Woo Kim 	unsigned int x_ratio, y_ratio;
386782953ecSYoungJun Cho 	unsigned int buf_num = 1;
387d8408326SSeung-Woo Kim 	dma_addr_t luma_addr[2], chroma_addr[2];
388d8408326SSeung-Woo Kim 	bool tiled_mode = false;
389d8408326SSeung-Woo Kim 	bool crcb_mode = false;
390d8408326SSeung-Woo Kim 	u32 val;
391d8408326SSeung-Woo Kim 
392*7ee14cdcSGustavo Padovan 	plane = &ctx->planes[win];
393d8408326SSeung-Woo Kim 
394*7ee14cdcSGustavo Padovan 	switch (plane->pixel_format) {
395363b06aaSVille Syrjälä 	case DRM_FORMAT_NV12:
396d8408326SSeung-Woo Kim 		crcb_mode = false;
397d8408326SSeung-Woo Kim 		buf_num = 2;
398d8408326SSeung-Woo Kim 		break;
399d8408326SSeung-Woo Kim 	/* TODO: single buffer format NV12, NV21 */
400d8408326SSeung-Woo Kim 	default:
401d8408326SSeung-Woo Kim 		/* ignore pixel format at disable time */
402*7ee14cdcSGustavo Padovan 		if (!plane->dma_addr[0])
403d8408326SSeung-Woo Kim 			break;
404d8408326SSeung-Woo Kim 
405d8408326SSeung-Woo Kim 		DRM_ERROR("pixel format for vp is wrong [%d].\n",
406*7ee14cdcSGustavo Padovan 				plane->pixel_format);
407d8408326SSeung-Woo Kim 		return;
408d8408326SSeung-Woo Kim 	}
409d8408326SSeung-Woo Kim 
410d8408326SSeung-Woo Kim 	/* scaling feature: (src << 16) / dst */
411*7ee14cdcSGustavo Padovan 	x_ratio = (plane->src_width << 16) / plane->crtc_width;
412*7ee14cdcSGustavo Padovan 	y_ratio = (plane->src_height << 16) / plane->crtc_height;
413d8408326SSeung-Woo Kim 
414d8408326SSeung-Woo Kim 	if (buf_num == 2) {
415*7ee14cdcSGustavo Padovan 		luma_addr[0] = plane->dma_addr[0];
416*7ee14cdcSGustavo Padovan 		chroma_addr[0] = plane->dma_addr[1];
417d8408326SSeung-Woo Kim 	} else {
418*7ee14cdcSGustavo Padovan 		luma_addr[0] = plane->dma_addr[0];
419*7ee14cdcSGustavo Padovan 		chroma_addr[0] = plane->dma_addr[0]
420*7ee14cdcSGustavo Padovan 			+ (plane->pitch * plane->fb_height);
421d8408326SSeung-Woo Kim 	}
422d8408326SSeung-Woo Kim 
423*7ee14cdcSGustavo Padovan 	if (plane->scan_flag & DRM_MODE_FLAG_INTERLACE) {
424d8408326SSeung-Woo Kim 		ctx->interlace = true;
425d8408326SSeung-Woo Kim 		if (tiled_mode) {
426d8408326SSeung-Woo Kim 			luma_addr[1] = luma_addr[0] + 0x40;
427d8408326SSeung-Woo Kim 			chroma_addr[1] = chroma_addr[0] + 0x40;
428d8408326SSeung-Woo Kim 		} else {
429*7ee14cdcSGustavo Padovan 			luma_addr[1] = luma_addr[0] + plane->pitch;
430*7ee14cdcSGustavo Padovan 			chroma_addr[1] = chroma_addr[0] + plane->pitch;
431d8408326SSeung-Woo Kim 		}
432d8408326SSeung-Woo Kim 	} else {
433d8408326SSeung-Woo Kim 		ctx->interlace = false;
434d8408326SSeung-Woo Kim 		luma_addr[1] = 0;
435d8408326SSeung-Woo Kim 		chroma_addr[1] = 0;
436d8408326SSeung-Woo Kim 	}
437d8408326SSeung-Woo Kim 
438d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
439d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, false);
440d8408326SSeung-Woo Kim 
441d8408326SSeung-Woo Kim 	/* interlace or progressive scan mode */
442d8408326SSeung-Woo Kim 	val = (ctx->interlace ? ~0 : 0);
443d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_LINE_SKIP);
444d8408326SSeung-Woo Kim 
445d8408326SSeung-Woo Kim 	/* setup format */
446d8408326SSeung-Woo Kim 	val = (crcb_mode ? VP_MODE_NV21 : VP_MODE_NV12);
447d8408326SSeung-Woo Kim 	val |= (tiled_mode ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
448d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_FMT_MASK);
449d8408326SSeung-Woo Kim 
450d8408326SSeung-Woo Kim 	/* setting size of input image */
451*7ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_IMG_SIZE_Y, VP_IMG_HSIZE(plane->pitch) |
452*7ee14cdcSGustavo Padovan 		VP_IMG_VSIZE(plane->fb_height));
453d8408326SSeung-Woo Kim 	/* chroma height has to reduced by 2 to avoid chroma distorions */
454*7ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_IMG_SIZE_C, VP_IMG_HSIZE(plane->pitch) |
455*7ee14cdcSGustavo Padovan 		VP_IMG_VSIZE(plane->fb_height / 2));
456d8408326SSeung-Woo Kim 
457*7ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_SRC_WIDTH, plane->src_width);
458*7ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_SRC_HEIGHT, plane->src_height);
459d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_H_POSITION,
460*7ee14cdcSGustavo Padovan 			VP_SRC_H_POSITION_VAL(plane->fb_x));
461*7ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_SRC_V_POSITION, plane->fb_y);
462d8408326SSeung-Woo Kim 
463*7ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_DST_WIDTH, plane->crtc_width);
464*7ee14cdcSGustavo Padovan 	vp_reg_write(res, VP_DST_H_POSITION, plane->crtc_x);
465d8408326SSeung-Woo Kim 	if (ctx->interlace) {
466*7ee14cdcSGustavo Padovan 		vp_reg_write(res, VP_DST_HEIGHT, plane->crtc_height / 2);
467*7ee14cdcSGustavo Padovan 		vp_reg_write(res, VP_DST_V_POSITION, plane->crtc_y / 2);
468d8408326SSeung-Woo Kim 	} else {
469*7ee14cdcSGustavo Padovan 		vp_reg_write(res, VP_DST_HEIGHT, plane->crtc_height);
470*7ee14cdcSGustavo Padovan 		vp_reg_write(res, VP_DST_V_POSITION, plane->crtc_y);
471d8408326SSeung-Woo Kim 	}
472d8408326SSeung-Woo Kim 
473d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_H_RATIO, x_ratio);
474d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_V_RATIO, y_ratio);
475d8408326SSeung-Woo Kim 
476d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
477d8408326SSeung-Woo Kim 
478d8408326SSeung-Woo Kim 	/* set buffer address to vp */
479d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_Y_PTR, luma_addr[0]);
480d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_Y_PTR, luma_addr[1]);
481d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_C_PTR, chroma_addr[0]);
482d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_C_PTR, chroma_addr[1]);
483d8408326SSeung-Woo Kim 
484*7ee14cdcSGustavo Padovan 	mixer_cfg_scan(ctx, plane->mode_height);
485*7ee14cdcSGustavo Padovan 	mixer_cfg_rgb_fmt(ctx, plane->mode_height);
486d8408326SSeung-Woo Kim 	mixer_cfg_layer(ctx, win, true);
487d8408326SSeung-Woo Kim 	mixer_run(ctx);
488d8408326SSeung-Woo Kim 
489d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, true);
490d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
491d8408326SSeung-Woo Kim 
492d8408326SSeung-Woo Kim 	vp_regs_dump(ctx);
493d8408326SSeung-Woo Kim }
494d8408326SSeung-Woo Kim 
495aaf8b49eSRahul Sharma static void mixer_layer_update(struct mixer_context *ctx)
496aaf8b49eSRahul Sharma {
497aaf8b49eSRahul Sharma 	struct mixer_resources *res = &ctx->mixer_res;
498aaf8b49eSRahul Sharma 
499aaf8b49eSRahul Sharma 	mixer_reg_writemask(res, MXR_CFG, ~0, MXR_CFG_LAYER_UPDATE);
500aaf8b49eSRahul Sharma }
501aaf8b49eSRahul Sharma 
502d8408326SSeung-Woo Kim static void mixer_graph_buffer(struct mixer_context *ctx, int win)
503d8408326SSeung-Woo Kim {
504d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
505d8408326SSeung-Woo Kim 	unsigned long flags;
506*7ee14cdcSGustavo Padovan 	struct exynos_drm_plane *plane;
507d8408326SSeung-Woo Kim 	unsigned int x_ratio, y_ratio;
508d8408326SSeung-Woo Kim 	unsigned int src_x_offset, src_y_offset, dst_x_offset, dst_y_offset;
509d8408326SSeung-Woo Kim 	dma_addr_t dma_addr;
510d8408326SSeung-Woo Kim 	unsigned int fmt;
511d8408326SSeung-Woo Kim 	u32 val;
512d8408326SSeung-Woo Kim 
513*7ee14cdcSGustavo Padovan 	plane = &ctx->planes[win];
514d8408326SSeung-Woo Kim 
515d8408326SSeung-Woo Kim 	#define RGB565 4
516d8408326SSeung-Woo Kim 	#define ARGB1555 5
517d8408326SSeung-Woo Kim 	#define ARGB4444 6
518d8408326SSeung-Woo Kim 	#define ARGB8888 7
519d8408326SSeung-Woo Kim 
520*7ee14cdcSGustavo Padovan 	switch (plane->bpp) {
521d8408326SSeung-Woo Kim 	case 16:
522d8408326SSeung-Woo Kim 		fmt = ARGB4444;
523d8408326SSeung-Woo Kim 		break;
524d8408326SSeung-Woo Kim 	case 32:
525d8408326SSeung-Woo Kim 		fmt = ARGB8888;
526d8408326SSeung-Woo Kim 		break;
527d8408326SSeung-Woo Kim 	default:
528d8408326SSeung-Woo Kim 		fmt = ARGB8888;
529d8408326SSeung-Woo Kim 	}
530d8408326SSeung-Woo Kim 
531d8408326SSeung-Woo Kim 	/* 2x scaling feature */
532d8408326SSeung-Woo Kim 	x_ratio = 0;
533d8408326SSeung-Woo Kim 	y_ratio = 0;
534d8408326SSeung-Woo Kim 
535*7ee14cdcSGustavo Padovan 	dst_x_offset = plane->crtc_x;
536*7ee14cdcSGustavo Padovan 	dst_y_offset = plane->crtc_y;
537d8408326SSeung-Woo Kim 
538d8408326SSeung-Woo Kim 	/* converting dma address base and source offset */
539*7ee14cdcSGustavo Padovan 	dma_addr = plane->dma_addr[0]
540*7ee14cdcSGustavo Padovan 		+ (plane->fb_x * plane->bpp >> 3)
541*7ee14cdcSGustavo Padovan 		+ (plane->fb_y * plane->pitch);
542d8408326SSeung-Woo Kim 	src_x_offset = 0;
543d8408326SSeung-Woo Kim 	src_y_offset = 0;
544d8408326SSeung-Woo Kim 
545*7ee14cdcSGustavo Padovan 	if (plane->scan_flag & DRM_MODE_FLAG_INTERLACE)
546d8408326SSeung-Woo Kim 		ctx->interlace = true;
547d8408326SSeung-Woo Kim 	else
548d8408326SSeung-Woo Kim 		ctx->interlace = false;
549d8408326SSeung-Woo Kim 
550d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
551d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, false);
552d8408326SSeung-Woo Kim 
553d8408326SSeung-Woo Kim 	/* setup format */
554d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_GRAPHIC_CFG(win),
555d8408326SSeung-Woo Kim 		MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
556d8408326SSeung-Woo Kim 
557d8408326SSeung-Woo Kim 	/* setup geometry */
558adacb228SDaniel Stone 	mixer_reg_write(res, MXR_GRAPHIC_SPAN(win),
559*7ee14cdcSGustavo Padovan 			plane->pitch / (plane->bpp >> 3));
560d8408326SSeung-Woo Kim 
561def5e095SRahul Sharma 	/* setup display size */
562def5e095SRahul Sharma 	if (ctx->mxr_ver == MXR_VER_128_0_0_184 &&
563def5e095SRahul Sharma 		win == MIXER_DEFAULT_WIN) {
564*7ee14cdcSGustavo Padovan 		val  = MXR_MXR_RES_HEIGHT(plane->mode_height);
565*7ee14cdcSGustavo Padovan 		val |= MXR_MXR_RES_WIDTH(plane->mode_width);
566def5e095SRahul Sharma 		mixer_reg_write(res, MXR_RESOLUTION, val);
567def5e095SRahul Sharma 	}
568def5e095SRahul Sharma 
569*7ee14cdcSGustavo Padovan 	val  = MXR_GRP_WH_WIDTH(plane->crtc_width);
570*7ee14cdcSGustavo Padovan 	val |= MXR_GRP_WH_HEIGHT(plane->crtc_height);
571d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_H_SCALE(x_ratio);
572d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_V_SCALE(y_ratio);
573d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_WH(win), val);
574d8408326SSeung-Woo Kim 
575d8408326SSeung-Woo Kim 	/* setup offsets in source image */
576d8408326SSeung-Woo Kim 	val  = MXR_GRP_SXY_SX(src_x_offset);
577d8408326SSeung-Woo Kim 	val |= MXR_GRP_SXY_SY(src_y_offset);
578d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_SXY(win), val);
579d8408326SSeung-Woo Kim 
580d8408326SSeung-Woo Kim 	/* setup offsets in display image */
581d8408326SSeung-Woo Kim 	val  = MXR_GRP_DXY_DX(dst_x_offset);
582d8408326SSeung-Woo Kim 	val |= MXR_GRP_DXY_DY(dst_y_offset);
583d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_DXY(win), val);
584d8408326SSeung-Woo Kim 
585d8408326SSeung-Woo Kim 	/* set buffer address to mixer */
586d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_BASE(win), dma_addr);
587d8408326SSeung-Woo Kim 
588*7ee14cdcSGustavo Padovan 	mixer_cfg_scan(ctx, plane->mode_height);
589*7ee14cdcSGustavo Padovan 	mixer_cfg_rgb_fmt(ctx, plane->mode_height);
590d8408326SSeung-Woo Kim 	mixer_cfg_layer(ctx, win, true);
591aaf8b49eSRahul Sharma 
592aaf8b49eSRahul Sharma 	/* layer update mandatory for mixer 16.0.33.0 */
593def5e095SRahul Sharma 	if (ctx->mxr_ver == MXR_VER_16_0_33_0 ||
594def5e095SRahul Sharma 		ctx->mxr_ver == MXR_VER_128_0_0_184)
595aaf8b49eSRahul Sharma 		mixer_layer_update(ctx);
596aaf8b49eSRahul Sharma 
597d8408326SSeung-Woo Kim 	mixer_run(ctx);
598d8408326SSeung-Woo Kim 
599d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, true);
600d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
601d8408326SSeung-Woo Kim }
602d8408326SSeung-Woo Kim 
603d8408326SSeung-Woo Kim static void vp_win_reset(struct mixer_context *ctx)
604d8408326SSeung-Woo Kim {
605d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
606d8408326SSeung-Woo Kim 	int tries = 100;
607d8408326SSeung-Woo Kim 
608d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRESET, VP_SRESET_PROCESSING);
609d8408326SSeung-Woo Kim 	for (tries = 100; tries; --tries) {
610d8408326SSeung-Woo Kim 		/* waiting until VP_SRESET_PROCESSING is 0 */
611d8408326SSeung-Woo Kim 		if (~vp_reg_read(res, VP_SRESET) & VP_SRESET_PROCESSING)
612d8408326SSeung-Woo Kim 			break;
61309760ea3SSean Paul 		usleep_range(10000, 12000);
614d8408326SSeung-Woo Kim 	}
615d8408326SSeung-Woo Kim 	WARN(tries == 0, "failed to reset Video Processor\n");
616d8408326SSeung-Woo Kim }
617d8408326SSeung-Woo Kim 
618cf8fc4f1SJoonyoung Shim static void mixer_win_reset(struct mixer_context *ctx)
619cf8fc4f1SJoonyoung Shim {
620cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
621cf8fc4f1SJoonyoung Shim 	unsigned long flags;
622cf8fc4f1SJoonyoung Shim 	u32 val; /* value stored to register */
623cf8fc4f1SJoonyoung Shim 
624cf8fc4f1SJoonyoung Shim 	spin_lock_irqsave(&res->reg_slock, flags);
625cf8fc4f1SJoonyoung Shim 	mixer_vsync_set_update(ctx, false);
626cf8fc4f1SJoonyoung Shim 
627cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
628cf8fc4f1SJoonyoung Shim 
629cf8fc4f1SJoonyoung Shim 	/* set output in RGB888 mode */
630cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
631cf8fc4f1SJoonyoung Shim 
632cf8fc4f1SJoonyoung Shim 	/* 16 beat burst in DMA */
633cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_STATUS, MXR_STATUS_16_BURST,
634cf8fc4f1SJoonyoung Shim 		MXR_STATUS_BURST_MASK);
635cf8fc4f1SJoonyoung Shim 
636cf8fc4f1SJoonyoung Shim 	/* setting default layer priority: layer1 > layer0 > video
637cf8fc4f1SJoonyoung Shim 	 * because typical usage scenario would be
638cf8fc4f1SJoonyoung Shim 	 * layer1 - OSD
639cf8fc4f1SJoonyoung Shim 	 * layer0 - framebuffer
640cf8fc4f1SJoonyoung Shim 	 * video - video overlay
641cf8fc4f1SJoonyoung Shim 	 */
642cf8fc4f1SJoonyoung Shim 	val = MXR_LAYER_CFG_GRP1_VAL(3);
643cf8fc4f1SJoonyoung Shim 	val |= MXR_LAYER_CFG_GRP0_VAL(2);
6441b8e5747SRahul Sharma 	if (ctx->vp_enabled)
645cf8fc4f1SJoonyoung Shim 		val |= MXR_LAYER_CFG_VP_VAL(1);
646cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_LAYER_CFG, val);
647cf8fc4f1SJoonyoung Shim 
648cf8fc4f1SJoonyoung Shim 	/* setting background color */
649cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR0, 0x008080);
650cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR1, 0x008080);
651cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR2, 0x008080);
652cf8fc4f1SJoonyoung Shim 
653cf8fc4f1SJoonyoung Shim 	/* setting graphical layers */
654cf8fc4f1SJoonyoung Shim 	val  = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
655cf8fc4f1SJoonyoung Shim 	val |= MXR_GRP_CFG_WIN_BLEND_EN;
656cf8fc4f1SJoonyoung Shim 	val |= MXR_GRP_CFG_ALPHA_VAL(0xff); /* non-transparent alpha */
657cf8fc4f1SJoonyoung Shim 
6580377f4edSSean Paul 	/* Don't blend layer 0 onto the mixer background */
659cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_GRAPHIC_CFG(0), val);
6600377f4edSSean Paul 
6610377f4edSSean Paul 	/* Blend layer 1 into layer 0 */
6620377f4edSSean Paul 	val |= MXR_GRP_CFG_BLEND_PRE_MUL;
6630377f4edSSean Paul 	val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
664cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_GRAPHIC_CFG(1), val);
665cf8fc4f1SJoonyoung Shim 
6665736603bSSeung-Woo Kim 	/* setting video layers */
6675736603bSSeung-Woo Kim 	val = MXR_GRP_CFG_ALPHA_VAL(0);
6685736603bSSeung-Woo Kim 	mixer_reg_write(res, MXR_VIDEO_CFG, val);
6695736603bSSeung-Woo Kim 
6701b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
671cf8fc4f1SJoonyoung Shim 		/* configuration of Video Processor Registers */
672cf8fc4f1SJoonyoung Shim 		vp_win_reset(ctx);
673cf8fc4f1SJoonyoung Shim 		vp_default_filter(res);
6741b8e5747SRahul Sharma 	}
675cf8fc4f1SJoonyoung Shim 
676cf8fc4f1SJoonyoung Shim 	/* disable all layers */
677cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
678cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
6791b8e5747SRahul Sharma 	if (ctx->vp_enabled)
680cf8fc4f1SJoonyoung Shim 		mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
681cf8fc4f1SJoonyoung Shim 
682cf8fc4f1SJoonyoung Shim 	mixer_vsync_set_update(ctx, true);
683cf8fc4f1SJoonyoung Shim 	spin_unlock_irqrestore(&res->reg_slock, flags);
684cf8fc4f1SJoonyoung Shim }
685cf8fc4f1SJoonyoung Shim 
6864551789fSSean Paul static irqreturn_t mixer_irq_handler(int irq, void *arg)
6874551789fSSean Paul {
6884551789fSSean Paul 	struct mixer_context *ctx = arg;
6894551789fSSean Paul 	struct mixer_resources *res = &ctx->mixer_res;
6904551789fSSean Paul 	u32 val, base, shadow;
6914551789fSSean Paul 
6924551789fSSean Paul 	spin_lock(&res->reg_slock);
6934551789fSSean Paul 
6944551789fSSean Paul 	/* read interrupt status for handling and clearing flags for VSYNC */
6954551789fSSean Paul 	val = mixer_reg_read(res, MXR_INT_STATUS);
6964551789fSSean Paul 
6974551789fSSean Paul 	/* handling VSYNC */
6984551789fSSean Paul 	if (val & MXR_INT_STATUS_VSYNC) {
6994551789fSSean Paul 		/* interlace scan need to check shadow register */
7004551789fSSean Paul 		if (ctx->interlace) {
7014551789fSSean Paul 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
7024551789fSSean Paul 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
7034551789fSSean Paul 			if (base != shadow)
7044551789fSSean Paul 				goto out;
7054551789fSSean Paul 
7064551789fSSean Paul 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
7074551789fSSean Paul 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
7084551789fSSean Paul 			if (base != shadow)
7094551789fSSean Paul 				goto out;
7104551789fSSean Paul 		}
7114551789fSSean Paul 
7124551789fSSean Paul 		drm_handle_vblank(ctx->drm_dev, ctx->pipe);
7134551789fSSean Paul 		exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
7144551789fSSean Paul 
7154551789fSSean Paul 		/* set wait vsync event to zero and wake up queue. */
7164551789fSSean Paul 		if (atomic_read(&ctx->wait_vsync_event)) {
7174551789fSSean Paul 			atomic_set(&ctx->wait_vsync_event, 0);
7184551789fSSean Paul 			wake_up(&ctx->wait_vsync_queue);
7194551789fSSean Paul 		}
7204551789fSSean Paul 	}
7214551789fSSean Paul 
7224551789fSSean Paul out:
7234551789fSSean Paul 	/* clear interrupts */
7244551789fSSean Paul 	if (~val & MXR_INT_EN_VSYNC) {
7254551789fSSean Paul 		/* vsync interrupt use different bit for read and clear */
7264551789fSSean Paul 		val &= ~MXR_INT_EN_VSYNC;
7274551789fSSean Paul 		val |= MXR_INT_CLEAR_VSYNC;
7284551789fSSean Paul 	}
7294551789fSSean Paul 	mixer_reg_write(res, MXR_INT_STATUS, val);
7304551789fSSean Paul 
7314551789fSSean Paul 	spin_unlock(&res->reg_slock);
7324551789fSSean Paul 
7334551789fSSean Paul 	return IRQ_HANDLED;
7344551789fSSean Paul }
7354551789fSSean Paul 
7364551789fSSean Paul static int mixer_resources_init(struct mixer_context *mixer_ctx)
7374551789fSSean Paul {
7384551789fSSean Paul 	struct device *dev = &mixer_ctx->pdev->dev;
7394551789fSSean Paul 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
7404551789fSSean Paul 	struct resource *res;
7414551789fSSean Paul 	int ret;
7424551789fSSean Paul 
7434551789fSSean Paul 	spin_lock_init(&mixer_res->reg_slock);
7444551789fSSean Paul 
7454551789fSSean Paul 	mixer_res->mixer = devm_clk_get(dev, "mixer");
7464551789fSSean Paul 	if (IS_ERR(mixer_res->mixer)) {
7474551789fSSean Paul 		dev_err(dev, "failed to get clock 'mixer'\n");
7484551789fSSean Paul 		return -ENODEV;
7494551789fSSean Paul 	}
7504551789fSSean Paul 
75104427ec5SMarek Szyprowski 	mixer_res->hdmi = devm_clk_get(dev, "hdmi");
75204427ec5SMarek Szyprowski 	if (IS_ERR(mixer_res->hdmi)) {
75304427ec5SMarek Szyprowski 		dev_err(dev, "failed to get clock 'hdmi'\n");
75404427ec5SMarek Szyprowski 		return PTR_ERR(mixer_res->hdmi);
75504427ec5SMarek Szyprowski 	}
75604427ec5SMarek Szyprowski 
7574551789fSSean Paul 	mixer_res->sclk_hdmi = devm_clk_get(dev, "sclk_hdmi");
7584551789fSSean Paul 	if (IS_ERR(mixer_res->sclk_hdmi)) {
7594551789fSSean Paul 		dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
7604551789fSSean Paul 		return -ENODEV;
7614551789fSSean Paul 	}
7624551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 0);
7634551789fSSean Paul 	if (res == NULL) {
7644551789fSSean Paul 		dev_err(dev, "get memory resource failed.\n");
7654551789fSSean Paul 		return -ENXIO;
7664551789fSSean Paul 	}
7674551789fSSean Paul 
7684551789fSSean Paul 	mixer_res->mixer_regs = devm_ioremap(dev, res->start,
7694551789fSSean Paul 							resource_size(res));
7704551789fSSean Paul 	if (mixer_res->mixer_regs == NULL) {
7714551789fSSean Paul 		dev_err(dev, "register mapping failed.\n");
7724551789fSSean Paul 		return -ENXIO;
7734551789fSSean Paul 	}
7744551789fSSean Paul 
7754551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_IRQ, 0);
7764551789fSSean Paul 	if (res == NULL) {
7774551789fSSean Paul 		dev_err(dev, "get interrupt resource failed.\n");
7784551789fSSean Paul 		return -ENXIO;
7794551789fSSean Paul 	}
7804551789fSSean Paul 
7814551789fSSean Paul 	ret = devm_request_irq(dev, res->start, mixer_irq_handler,
7824551789fSSean Paul 						0, "drm_mixer", mixer_ctx);
7834551789fSSean Paul 	if (ret) {
7844551789fSSean Paul 		dev_err(dev, "request interrupt failed.\n");
7854551789fSSean Paul 		return ret;
7864551789fSSean Paul 	}
7874551789fSSean Paul 	mixer_res->irq = res->start;
7884551789fSSean Paul 
7894551789fSSean Paul 	return 0;
7904551789fSSean Paul }
7914551789fSSean Paul 
7924551789fSSean Paul static int vp_resources_init(struct mixer_context *mixer_ctx)
7934551789fSSean Paul {
7944551789fSSean Paul 	struct device *dev = &mixer_ctx->pdev->dev;
7954551789fSSean Paul 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
7964551789fSSean Paul 	struct resource *res;
7974551789fSSean Paul 
7984551789fSSean Paul 	mixer_res->vp = devm_clk_get(dev, "vp");
7994551789fSSean Paul 	if (IS_ERR(mixer_res->vp)) {
8004551789fSSean Paul 		dev_err(dev, "failed to get clock 'vp'\n");
8014551789fSSean Paul 		return -ENODEV;
8024551789fSSean Paul 	}
803ff830c96SMarek Szyprowski 
804ff830c96SMarek Szyprowski 	if (mixer_ctx->has_sclk) {
8054551789fSSean Paul 		mixer_res->sclk_mixer = devm_clk_get(dev, "sclk_mixer");
8064551789fSSean Paul 		if (IS_ERR(mixer_res->sclk_mixer)) {
8074551789fSSean Paul 			dev_err(dev, "failed to get clock 'sclk_mixer'\n");
8084551789fSSean Paul 			return -ENODEV;
8094551789fSSean Paul 		}
810ff830c96SMarek Szyprowski 		mixer_res->mout_mixer = devm_clk_get(dev, "mout_mixer");
811ff830c96SMarek Szyprowski 		if (IS_ERR(mixer_res->mout_mixer)) {
812ff830c96SMarek Szyprowski 			dev_err(dev, "failed to get clock 'mout_mixer'\n");
8134551789fSSean Paul 			return -ENODEV;
8144551789fSSean Paul 		}
8154551789fSSean Paul 
816ff830c96SMarek Szyprowski 		if (mixer_res->sclk_hdmi && mixer_res->mout_mixer)
817ff830c96SMarek Szyprowski 			clk_set_parent(mixer_res->mout_mixer,
818ff830c96SMarek Szyprowski 				       mixer_res->sclk_hdmi);
819ff830c96SMarek Szyprowski 	}
8204551789fSSean Paul 
8214551789fSSean Paul 	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 1);
8224551789fSSean Paul 	if (res == NULL) {
8234551789fSSean Paul 		dev_err(dev, "get memory resource failed.\n");
8244551789fSSean Paul 		return -ENXIO;
8254551789fSSean Paul 	}
8264551789fSSean Paul 
8274551789fSSean Paul 	mixer_res->vp_regs = devm_ioremap(dev, res->start,
8284551789fSSean Paul 							resource_size(res));
8294551789fSSean Paul 	if (mixer_res->vp_regs == NULL) {
8304551789fSSean Paul 		dev_err(dev, "register mapping failed.\n");
8314551789fSSean Paul 		return -ENXIO;
8324551789fSSean Paul 	}
8334551789fSSean Paul 
8344551789fSSean Paul 	return 0;
8354551789fSSean Paul }
8364551789fSSean Paul 
83793bca243SGustavo Padovan static int mixer_initialize(struct mixer_context *mixer_ctx,
838f37cd5e8SInki Dae 			struct drm_device *drm_dev)
8394551789fSSean Paul {
8404551789fSSean Paul 	int ret;
841f37cd5e8SInki Dae 	struct exynos_drm_private *priv;
842f37cd5e8SInki Dae 	priv = drm_dev->dev_private;
8434551789fSSean Paul 
844eb88e422SGustavo Padovan 	mixer_ctx->drm_dev = drm_dev;
8458a326eddSGustavo Padovan 	mixer_ctx->pipe = priv->pipe++;
8464551789fSSean Paul 
8474551789fSSean Paul 	/* acquire resources: regs, irqs, clocks */
8484551789fSSean Paul 	ret = mixer_resources_init(mixer_ctx);
8494551789fSSean Paul 	if (ret) {
8504551789fSSean Paul 		DRM_ERROR("mixer_resources_init failed ret=%d\n", ret);
8514551789fSSean Paul 		return ret;
8524551789fSSean Paul 	}
8534551789fSSean Paul 
8544551789fSSean Paul 	if (mixer_ctx->vp_enabled) {
8554551789fSSean Paul 		/* acquire vp resources: regs, irqs, clocks */
8564551789fSSean Paul 		ret = vp_resources_init(mixer_ctx);
8574551789fSSean Paul 		if (ret) {
8584551789fSSean Paul 			DRM_ERROR("vp_resources_init failed ret=%d\n", ret);
8594551789fSSean Paul 			return ret;
8604551789fSSean Paul 		}
8614551789fSSean Paul 	}
8624551789fSSean Paul 
863f041b257SSean Paul 	if (!is_drm_iommu_supported(mixer_ctx->drm_dev))
8641055b39fSInki Dae 		return 0;
865f041b257SSean Paul 
866f041b257SSean Paul 	return drm_iommu_attach_device(mixer_ctx->drm_dev, mixer_ctx->dev);
8671055b39fSInki Dae }
8681055b39fSInki Dae 
86993bca243SGustavo Padovan static void mixer_ctx_remove(struct mixer_context *mixer_ctx)
870d8408326SSeung-Woo Kim {
871f041b257SSean Paul 	if (is_drm_iommu_supported(mixer_ctx->drm_dev))
872f041b257SSean Paul 		drm_iommu_detach_device(mixer_ctx->drm_dev, mixer_ctx->dev);
873f041b257SSean Paul }
874f041b257SSean Paul 
87593bca243SGustavo Padovan static int mixer_enable_vblank(struct exynos_drm_crtc *crtc)
876f041b257SSean Paul {
87793bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
878d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
879d8408326SSeung-Woo Kim 
880f041b257SSean Paul 	if (!mixer_ctx->powered) {
881f041b257SSean Paul 		mixer_ctx->int_en |= MXR_INT_EN_VSYNC;
882f041b257SSean Paul 		return 0;
883f041b257SSean Paul 	}
884d8408326SSeung-Woo Kim 
885d8408326SSeung-Woo Kim 	/* enable vsync interrupt */
886d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_INT_EN, MXR_INT_EN_VSYNC,
887d8408326SSeung-Woo Kim 			MXR_INT_EN_VSYNC);
888d8408326SSeung-Woo Kim 
889d8408326SSeung-Woo Kim 	return 0;
890d8408326SSeung-Woo Kim }
891d8408326SSeung-Woo Kim 
89293bca243SGustavo Padovan static void mixer_disable_vblank(struct exynos_drm_crtc *crtc)
893d8408326SSeung-Woo Kim {
89493bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
895d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
896d8408326SSeung-Woo Kim 
897d8408326SSeung-Woo Kim 	/* disable vsync interrupt */
898d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
899d8408326SSeung-Woo Kim }
900d8408326SSeung-Woo Kim 
90193bca243SGustavo Padovan static void mixer_win_commit(struct exynos_drm_crtc *crtc, int zpos)
902d8408326SSeung-Woo Kim {
90393bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
904f041b257SSean Paul 	int win = zpos == DEFAULT_ZPOS ? MIXER_DEFAULT_WIN : zpos;
905d8408326SSeung-Woo Kim 
906cbc4c33dSYoungJun Cho 	DRM_DEBUG_KMS("win: %d\n", win);
907d8408326SSeung-Woo Kim 
908dda9012bSShirish S 	mutex_lock(&mixer_ctx->mixer_mutex);
909dda9012bSShirish S 	if (!mixer_ctx->powered) {
910dda9012bSShirish S 		mutex_unlock(&mixer_ctx->mixer_mutex);
911dda9012bSShirish S 		return;
912dda9012bSShirish S 	}
913dda9012bSShirish S 	mutex_unlock(&mixer_ctx->mixer_mutex);
914dda9012bSShirish S 
9151b8e5747SRahul Sharma 	if (win > 1 && mixer_ctx->vp_enabled)
916d8408326SSeung-Woo Kim 		vp_video_buffer(mixer_ctx, win);
917d8408326SSeung-Woo Kim 	else
918d8408326SSeung-Woo Kim 		mixer_graph_buffer(mixer_ctx, win);
919db43fd16SPrathyush K 
920*7ee14cdcSGustavo Padovan 	mixer_ctx->planes[win].enabled = true;
921d8408326SSeung-Woo Kim }
922d8408326SSeung-Woo Kim 
92393bca243SGustavo Padovan static void mixer_win_disable(struct exynos_drm_crtc *crtc, int zpos)
924d8408326SSeung-Woo Kim {
92593bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
926d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
927f041b257SSean Paul 	int win = zpos == DEFAULT_ZPOS ? MIXER_DEFAULT_WIN : zpos;
928d8408326SSeung-Woo Kim 	unsigned long flags;
929d8408326SSeung-Woo Kim 
930cbc4c33dSYoungJun Cho 	DRM_DEBUG_KMS("win: %d\n", win);
931d8408326SSeung-Woo Kim 
932db43fd16SPrathyush K 	mutex_lock(&mixer_ctx->mixer_mutex);
933db43fd16SPrathyush K 	if (!mixer_ctx->powered) {
934db43fd16SPrathyush K 		mutex_unlock(&mixer_ctx->mixer_mutex);
935*7ee14cdcSGustavo Padovan 		mixer_ctx->planes[win].resume = false;
936db43fd16SPrathyush K 		return;
937db43fd16SPrathyush K 	}
938db43fd16SPrathyush K 	mutex_unlock(&mixer_ctx->mixer_mutex);
939db43fd16SPrathyush K 
940d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
941d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, false);
942d8408326SSeung-Woo Kim 
943d8408326SSeung-Woo Kim 	mixer_cfg_layer(mixer_ctx, win, false);
944d8408326SSeung-Woo Kim 
945d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, true);
946d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
947db43fd16SPrathyush K 
948*7ee14cdcSGustavo Padovan 	mixer_ctx->planes[win].enabled = false;
949d8408326SSeung-Woo Kim }
950d8408326SSeung-Woo Kim 
95193bca243SGustavo Padovan static void mixer_wait_for_vblank(struct exynos_drm_crtc *crtc)
9520ea6822fSRahul Sharma {
95393bca243SGustavo Padovan 	struct mixer_context *mixer_ctx = crtc->ctx;
9547c4c5584SJoonyoung Shim 	int err;
9558137a2e2SPrathyush K 
9566e95d5e6SPrathyush K 	mutex_lock(&mixer_ctx->mixer_mutex);
9576e95d5e6SPrathyush K 	if (!mixer_ctx->powered) {
9586e95d5e6SPrathyush K 		mutex_unlock(&mixer_ctx->mixer_mutex);
9596e95d5e6SPrathyush K 		return;
9606e95d5e6SPrathyush K 	}
9616e95d5e6SPrathyush K 	mutex_unlock(&mixer_ctx->mixer_mutex);
9626e95d5e6SPrathyush K 
96393bca243SGustavo Padovan 	err = drm_vblank_get(mixer_ctx->drm_dev, mixer_ctx->pipe);
9647c4c5584SJoonyoung Shim 	if (err < 0) {
9657c4c5584SJoonyoung Shim 		DRM_DEBUG_KMS("failed to acquire vblank counter\n");
9667c4c5584SJoonyoung Shim 		return;
9677c4c5584SJoonyoung Shim 	}
9685d39b9eeSRahul Sharma 
9696e95d5e6SPrathyush K 	atomic_set(&mixer_ctx->wait_vsync_event, 1);
9706e95d5e6SPrathyush K 
9716e95d5e6SPrathyush K 	/*
9726e95d5e6SPrathyush K 	 * wait for MIXER to signal VSYNC interrupt or return after
9736e95d5e6SPrathyush K 	 * timeout which is set to 50ms (refresh rate of 20).
9746e95d5e6SPrathyush K 	 */
9756e95d5e6SPrathyush K 	if (!wait_event_timeout(mixer_ctx->wait_vsync_queue,
9766e95d5e6SPrathyush K 				!atomic_read(&mixer_ctx->wait_vsync_event),
977bfd8303aSDaniel Vetter 				HZ/20))
9788137a2e2SPrathyush K 		DRM_DEBUG_KMS("vblank wait timed out.\n");
9795d39b9eeSRahul Sharma 
98093bca243SGustavo Padovan 	drm_vblank_put(mixer_ctx->drm_dev, mixer_ctx->pipe);
9818137a2e2SPrathyush K }
9828137a2e2SPrathyush K 
98392dc7a04SJoonyoung Shim static void mixer_window_suspend(struct mixer_context *ctx)
984db43fd16SPrathyush K {
985*7ee14cdcSGustavo Padovan 	struct exynos_drm_plane *plane;
986db43fd16SPrathyush K 	int i;
987db43fd16SPrathyush K 
988db43fd16SPrathyush K 	for (i = 0; i < MIXER_WIN_NR; i++) {
989*7ee14cdcSGustavo Padovan 		plane = &ctx->planes[i];
990*7ee14cdcSGustavo Padovan 		plane->resume = plane->enabled;
99192dc7a04SJoonyoung Shim 		mixer_win_disable(ctx->crtc, i);
992db43fd16SPrathyush K 	}
99392dc7a04SJoonyoung Shim 	mixer_wait_for_vblank(ctx->crtc);
994db43fd16SPrathyush K }
995db43fd16SPrathyush K 
99692dc7a04SJoonyoung Shim static void mixer_window_resume(struct mixer_context *ctx)
997db43fd16SPrathyush K {
998*7ee14cdcSGustavo Padovan 	struct exynos_drm_plane *plane;
999db43fd16SPrathyush K 	int i;
1000db43fd16SPrathyush K 
1001db43fd16SPrathyush K 	for (i = 0; i < MIXER_WIN_NR; i++) {
1002*7ee14cdcSGustavo Padovan 		plane = &ctx->planes[i];
1003*7ee14cdcSGustavo Padovan 		plane->enabled = plane->resume;
1004*7ee14cdcSGustavo Padovan 		plane->resume = false;
1005*7ee14cdcSGustavo Padovan 		if (plane->enabled)
100692dc7a04SJoonyoung Shim 			mixer_win_commit(ctx->crtc, i);
1007db43fd16SPrathyush K 	}
1008db43fd16SPrathyush K }
1009db43fd16SPrathyush K 
101092dc7a04SJoonyoung Shim static void mixer_poweron(struct mixer_context *ctx)
1011db43fd16SPrathyush K {
1012db43fd16SPrathyush K 	struct mixer_resources *res = &ctx->mixer_res;
1013db43fd16SPrathyush K 
1014db43fd16SPrathyush K 	mutex_lock(&ctx->mixer_mutex);
1015db43fd16SPrathyush K 	if (ctx->powered) {
1016db43fd16SPrathyush K 		mutex_unlock(&ctx->mixer_mutex);
1017db43fd16SPrathyush K 		return;
1018db43fd16SPrathyush K 	}
1019b4bfa3c7SRahul Sharma 
1020db43fd16SPrathyush K 	mutex_unlock(&ctx->mixer_mutex);
1021db43fd16SPrathyush K 
1022af65c804SSean Paul 	pm_runtime_get_sync(ctx->dev);
1023af65c804SSean Paul 
10240bfb1f8bSSean Paul 	clk_prepare_enable(res->mixer);
102504427ec5SMarek Szyprowski 	clk_prepare_enable(res->hdmi);
1026db43fd16SPrathyush K 	if (ctx->vp_enabled) {
10270bfb1f8bSSean Paul 		clk_prepare_enable(res->vp);
1028ff830c96SMarek Szyprowski 		if (ctx->has_sclk)
10290bfb1f8bSSean Paul 			clk_prepare_enable(res->sclk_mixer);
1030db43fd16SPrathyush K 	}
1031db43fd16SPrathyush K 
1032b4bfa3c7SRahul Sharma 	mutex_lock(&ctx->mixer_mutex);
1033b4bfa3c7SRahul Sharma 	ctx->powered = true;
1034b4bfa3c7SRahul Sharma 	mutex_unlock(&ctx->mixer_mutex);
1035b4bfa3c7SRahul Sharma 
1036d74ed937SRahul Sharma 	mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_SOFT_RESET);
1037d74ed937SRahul Sharma 
1038db43fd16SPrathyush K 	mixer_reg_write(res, MXR_INT_EN, ctx->int_en);
1039db43fd16SPrathyush K 	mixer_win_reset(ctx);
1040db43fd16SPrathyush K 
104192dc7a04SJoonyoung Shim 	mixer_window_resume(ctx);
1042db43fd16SPrathyush K }
1043db43fd16SPrathyush K 
104492dc7a04SJoonyoung Shim static void mixer_poweroff(struct mixer_context *ctx)
1045db43fd16SPrathyush K {
1046db43fd16SPrathyush K 	struct mixer_resources *res = &ctx->mixer_res;
1047db43fd16SPrathyush K 
1048db43fd16SPrathyush K 	mutex_lock(&ctx->mixer_mutex);
1049b4bfa3c7SRahul Sharma 	if (!ctx->powered) {
1050b4bfa3c7SRahul Sharma 		mutex_unlock(&ctx->mixer_mutex);
1051b4bfa3c7SRahul Sharma 		return;
1052b4bfa3c7SRahul Sharma 	}
1053db43fd16SPrathyush K 	mutex_unlock(&ctx->mixer_mutex);
1054db43fd16SPrathyush K 
1055381be025SRahul Sharma 	mixer_stop(ctx);
105692dc7a04SJoonyoung Shim 	mixer_window_suspend(ctx);
1057db43fd16SPrathyush K 
1058db43fd16SPrathyush K 	ctx->int_en = mixer_reg_read(res, MXR_INT_EN);
1059db43fd16SPrathyush K 
1060b4bfa3c7SRahul Sharma 	mutex_lock(&ctx->mixer_mutex);
1061b4bfa3c7SRahul Sharma 	ctx->powered = false;
1062b4bfa3c7SRahul Sharma 	mutex_unlock(&ctx->mixer_mutex);
1063b4bfa3c7SRahul Sharma 
106404427ec5SMarek Szyprowski 	clk_disable_unprepare(res->hdmi);
10650bfb1f8bSSean Paul 	clk_disable_unprepare(res->mixer);
1066db43fd16SPrathyush K 	if (ctx->vp_enabled) {
10670bfb1f8bSSean Paul 		clk_disable_unprepare(res->vp);
1068ff830c96SMarek Szyprowski 		if (ctx->has_sclk)
10690bfb1f8bSSean Paul 			clk_disable_unprepare(res->sclk_mixer);
1070db43fd16SPrathyush K 	}
1071db43fd16SPrathyush K 
1072af65c804SSean Paul 	pm_runtime_put_sync(ctx->dev);
1073db43fd16SPrathyush K }
1074db43fd16SPrathyush K 
107593bca243SGustavo Padovan static void mixer_dpms(struct exynos_drm_crtc *crtc, int mode)
1076db43fd16SPrathyush K {
1077db43fd16SPrathyush K 	switch (mode) {
1078db43fd16SPrathyush K 	case DRM_MODE_DPMS_ON:
107992dc7a04SJoonyoung Shim 		mixer_poweron(crtc->ctx);
1080db43fd16SPrathyush K 		break;
1081db43fd16SPrathyush K 	case DRM_MODE_DPMS_STANDBY:
1082db43fd16SPrathyush K 	case DRM_MODE_DPMS_SUSPEND:
1083db43fd16SPrathyush K 	case DRM_MODE_DPMS_OFF:
108492dc7a04SJoonyoung Shim 		mixer_poweroff(crtc->ctx);
1085db43fd16SPrathyush K 		break;
1086db43fd16SPrathyush K 	default:
1087db43fd16SPrathyush K 		DRM_DEBUG_KMS("unknown dpms mode: %d\n", mode);
1088db43fd16SPrathyush K 		break;
1089db43fd16SPrathyush K 	}
1090db43fd16SPrathyush K }
1091db43fd16SPrathyush K 
1092f041b257SSean Paul /* Only valid for Mixer version 16.0.33.0 */
1093f041b257SSean Paul int mixer_check_mode(struct drm_display_mode *mode)
1094f041b257SSean Paul {
1095f041b257SSean Paul 	u32 w, h;
1096f041b257SSean Paul 
1097f041b257SSean Paul 	w = mode->hdisplay;
1098f041b257SSean Paul 	h = mode->vdisplay;
1099f041b257SSean Paul 
1100f041b257SSean Paul 	DRM_DEBUG_KMS("xres=%d, yres=%d, refresh=%d, intl=%d\n",
1101f041b257SSean Paul 		mode->hdisplay, mode->vdisplay, mode->vrefresh,
1102f041b257SSean Paul 		(mode->flags & DRM_MODE_FLAG_INTERLACE) ? 1 : 0);
1103f041b257SSean Paul 
1104f041b257SSean Paul 	if ((w >= 464 && w <= 720 && h >= 261 && h <= 576) ||
1105f041b257SSean Paul 		(w >= 1024 && w <= 1280 && h >= 576 && h <= 720) ||
1106f041b257SSean Paul 		(w >= 1664 && w <= 1920 && h >= 936 && h <= 1080))
1107f041b257SSean Paul 		return 0;
1108f041b257SSean Paul 
1109f041b257SSean Paul 	return -EINVAL;
1110f041b257SSean Paul }
1111f041b257SSean Paul 
111293bca243SGustavo Padovan static struct exynos_drm_crtc_ops mixer_crtc_ops = {
1113f041b257SSean Paul 	.dpms			= mixer_dpms,
1114d8408326SSeung-Woo Kim 	.enable_vblank		= mixer_enable_vblank,
1115d8408326SSeung-Woo Kim 	.disable_vblank		= mixer_disable_vblank,
11168137a2e2SPrathyush K 	.wait_for_vblank	= mixer_wait_for_vblank,
1117d8408326SSeung-Woo Kim 	.win_commit		= mixer_win_commit,
1118d8408326SSeung-Woo Kim 	.win_disable		= mixer_win_disable,
1119f041b257SSean Paul };
11200ea6822fSRahul Sharma 
1121def5e095SRahul Sharma static struct mixer_drv_data exynos5420_mxr_drv_data = {
1122def5e095SRahul Sharma 	.version = MXR_VER_128_0_0_184,
1123def5e095SRahul Sharma 	.is_vp_enabled = 0,
1124def5e095SRahul Sharma };
1125def5e095SRahul Sharma 
1126cc57caf0SRahul Sharma static struct mixer_drv_data exynos5250_mxr_drv_data = {
1127aaf8b49eSRahul Sharma 	.version = MXR_VER_16_0_33_0,
1128aaf8b49eSRahul Sharma 	.is_vp_enabled = 0,
1129aaf8b49eSRahul Sharma };
1130aaf8b49eSRahul Sharma 
1131ff830c96SMarek Szyprowski static struct mixer_drv_data exynos4212_mxr_drv_data = {
1132ff830c96SMarek Szyprowski 	.version = MXR_VER_0_0_0_16,
1133ff830c96SMarek Szyprowski 	.is_vp_enabled = 1,
1134ff830c96SMarek Szyprowski };
1135ff830c96SMarek Szyprowski 
1136cc57caf0SRahul Sharma static struct mixer_drv_data exynos4210_mxr_drv_data = {
11371e123441SRahul Sharma 	.version = MXR_VER_0_0_0_16,
11381b8e5747SRahul Sharma 	.is_vp_enabled = 1,
1139ff830c96SMarek Szyprowski 	.has_sclk = 1,
11401e123441SRahul Sharma };
11411e123441SRahul Sharma 
11421e123441SRahul Sharma static struct platform_device_id mixer_driver_types[] = {
11431e123441SRahul Sharma 	{
11441e123441SRahul Sharma 		.name		= "s5p-mixer",
1145cc57caf0SRahul Sharma 		.driver_data	= (unsigned long)&exynos4210_mxr_drv_data,
11461e123441SRahul Sharma 	}, {
1147aaf8b49eSRahul Sharma 		.name		= "exynos5-mixer",
1148cc57caf0SRahul Sharma 		.driver_data	= (unsigned long)&exynos5250_mxr_drv_data,
1149aaf8b49eSRahul Sharma 	}, {
1150aaf8b49eSRahul Sharma 		/* end node */
1151aaf8b49eSRahul Sharma 	}
1152aaf8b49eSRahul Sharma };
1153aaf8b49eSRahul Sharma 
1154aaf8b49eSRahul Sharma static 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;
1180*7ee14cdcSGustavo Padovan 	struct exynos_drm_plane *exynos_plane;
1181*7ee14cdcSGustavo Padovan 	enum drm_plane_type type;
1182*7ee14cdcSGustavo Padovan 	int zpos, ret;
1183d8408326SSeung-Woo Kim 
1184e2dc3f72SAlban Browaeys 	ret = mixer_initialize(ctx, drm_dev);
1185e2dc3f72SAlban Browaeys 	if (ret)
1186e2dc3f72SAlban Browaeys 		return ret;
1187e2dc3f72SAlban Browaeys 
1188*7ee14cdcSGustavo Padovan 	for (zpos = 0; zpos < MIXER_WIN_NR; zpos++) {
1189*7ee14cdcSGustavo Padovan 		type = (zpos == MIXER_DEFAULT_WIN) ? DRM_PLANE_TYPE_PRIMARY :
1190*7ee14cdcSGustavo Padovan 						DRM_PLANE_TYPE_OVERLAY;
1191*7ee14cdcSGustavo Padovan 		ret = exynos_plane_init(drm_dev, &ctx->planes[zpos],
1192*7ee14cdcSGustavo Padovan 					1 << ctx->pipe, type);
1193*7ee14cdcSGustavo Padovan 		if (ret)
1194*7ee14cdcSGustavo Padovan 			return ret;
1195*7ee14cdcSGustavo Padovan 	}
1196*7ee14cdcSGustavo Padovan 
1197*7ee14cdcSGustavo Padovan 	exynos_plane = &ctx->planes[MIXER_DEFAULT_WIN];
1198*7ee14cdcSGustavo Padovan 	ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
1199*7ee14cdcSGustavo Padovan 					   ctx->pipe, EXYNOS_DISPLAY_TYPE_HDMI,
120093bca243SGustavo Padovan 					   &mixer_crtc_ops, ctx);
120193bca243SGustavo Padovan 	if (IS_ERR(ctx->crtc)) {
1202e2dc3f72SAlban Browaeys 		mixer_ctx_remove(ctx);
120393bca243SGustavo Padovan 		ret = PTR_ERR(ctx->crtc);
120493bca243SGustavo Padovan 		goto free_ctx;
12058103ef1bSAndrzej Hajda 	}
12068103ef1bSAndrzej Hajda 
12078103ef1bSAndrzej Hajda 	return 0;
120893bca243SGustavo Padovan 
120993bca243SGustavo Padovan free_ctx:
121093bca243SGustavo Padovan 	devm_kfree(dev, ctx);
121193bca243SGustavo Padovan 	return ret;
12128103ef1bSAndrzej Hajda }
12138103ef1bSAndrzej Hajda 
12148103ef1bSAndrzej Hajda static void mixer_unbind(struct device *dev, struct device *master, void *data)
12158103ef1bSAndrzej Hajda {
12168103ef1bSAndrzej Hajda 	struct mixer_context *ctx = dev_get_drvdata(dev);
12178103ef1bSAndrzej Hajda 
121893bca243SGustavo Padovan 	mixer_ctx_remove(ctx);
12198103ef1bSAndrzej Hajda }
12208103ef1bSAndrzej Hajda 
12218103ef1bSAndrzej Hajda static const struct component_ops mixer_component_ops = {
12228103ef1bSAndrzej Hajda 	.bind	= mixer_bind,
12238103ef1bSAndrzej Hajda 	.unbind	= mixer_unbind,
12248103ef1bSAndrzej Hajda };
12258103ef1bSAndrzej Hajda 
12268103ef1bSAndrzej Hajda static int mixer_probe(struct platform_device *pdev)
12278103ef1bSAndrzej Hajda {
12288103ef1bSAndrzej Hajda 	struct device *dev = &pdev->dev;
12298103ef1bSAndrzej Hajda 	struct mixer_drv_data *drv;
12308103ef1bSAndrzej Hajda 	struct mixer_context *ctx;
12318103ef1bSAndrzej Hajda 	int ret;
1232d8408326SSeung-Woo Kim 
1233f041b257SSean Paul 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1234f041b257SSean Paul 	if (!ctx) {
1235f041b257SSean Paul 		DRM_ERROR("failed to alloc mixer context.\n");
1236d8408326SSeung-Woo Kim 		return -ENOMEM;
1237f041b257SSean Paul 	}
1238d8408326SSeung-Woo Kim 
1239cf8fc4f1SJoonyoung Shim 	mutex_init(&ctx->mixer_mutex);
1240cf8fc4f1SJoonyoung Shim 
1241aaf8b49eSRahul Sharma 	if (dev->of_node) {
1242aaf8b49eSRahul Sharma 		const struct of_device_id *match;
12438103ef1bSAndrzej Hajda 
1244e436b09dSSachin Kamat 		match = of_match_node(mixer_match_types, dev->of_node);
12452cdc53b3SRahul Sharma 		drv = (struct mixer_drv_data *)match->data;
1246aaf8b49eSRahul Sharma 	} else {
1247aaf8b49eSRahul Sharma 		drv = (struct mixer_drv_data *)
1248aaf8b49eSRahul Sharma 			platform_get_device_id(pdev)->driver_data;
1249aaf8b49eSRahul Sharma 	}
1250aaf8b49eSRahul Sharma 
12514551789fSSean Paul 	ctx->pdev = pdev;
1252d873ab99SSeung-Woo Kim 	ctx->dev = dev;
12531b8e5747SRahul Sharma 	ctx->vp_enabled = drv->is_vp_enabled;
1254ff830c96SMarek Szyprowski 	ctx->has_sclk = drv->has_sclk;
12551e123441SRahul Sharma 	ctx->mxr_ver = drv->version;
125657ed0f7bSDaniel Vetter 	init_waitqueue_head(&ctx->wait_vsync_queue);
12576e95d5e6SPrathyush K 	atomic_set(&ctx->wait_vsync_event, 0);
1258d8408326SSeung-Woo Kim 
12598103ef1bSAndrzej Hajda 	platform_set_drvdata(pdev, ctx);
1260df5225bcSInki Dae 
1261df5225bcSInki Dae 	ret = exynos_drm_component_add(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC,
12625d1741adSGustavo Padovan 					EXYNOS_DISPLAY_TYPE_HDMI);
1263df5225bcSInki Dae 	if (ret)
1264df5225bcSInki Dae 		return ret;
1265df5225bcSInki Dae 
1266df5225bcSInki Dae 	ret = component_add(&pdev->dev, &mixer_component_ops);
12678103ef1bSAndrzej Hajda 	if (ret) {
1268df5225bcSInki Dae 		exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC);
12698103ef1bSAndrzej Hajda 		return ret;
12708103ef1bSAndrzej Hajda 	}
12718103ef1bSAndrzej Hajda 
12728103ef1bSAndrzej Hajda 	pm_runtime_enable(dev);
1273df5225bcSInki Dae 
1274df5225bcSInki Dae 	return ret;
1275f37cd5e8SInki Dae }
1276f37cd5e8SInki Dae 
1277d8408326SSeung-Woo Kim static int mixer_remove(struct platform_device *pdev)
1278d8408326SSeung-Woo Kim {
12798103ef1bSAndrzej Hajda 	pm_runtime_disable(&pdev->dev);
12808103ef1bSAndrzej Hajda 
1281df5225bcSInki Dae 	component_del(&pdev->dev, &mixer_component_ops);
1282df5225bcSInki Dae 	exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC);
1283df5225bcSInki Dae 
1284d8408326SSeung-Woo Kim 	return 0;
1285d8408326SSeung-Woo Kim }
1286d8408326SSeung-Woo Kim 
1287d8408326SSeung-Woo Kim struct platform_driver mixer_driver = {
1288d8408326SSeung-Woo Kim 	.driver = {
1289aaf8b49eSRahul Sharma 		.name = "exynos-mixer",
1290d8408326SSeung-Woo Kim 		.owner = THIS_MODULE,
1291aaf8b49eSRahul Sharma 		.of_match_table = mixer_match_types,
1292d8408326SSeung-Woo Kim 	},
1293d8408326SSeung-Woo Kim 	.probe = mixer_probe,
129456550d94SGreg Kroah-Hartman 	.remove = mixer_remove,
12951e123441SRahul Sharma 	.id_table	= mixer_driver_types,
1296d8408326SSeung-Woo Kim };
1297