xref: /linux/drivers/gpu/drm/exynos/exynos_mixer.c (revision 8137a2e21a3465d91d86b49cfebcc370438b77bd)
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/module.h>
27d8408326SSeung-Woo Kim #include <linux/platform_device.h>
28d8408326SSeung-Woo Kim #include <linux/interrupt.h>
29d8408326SSeung-Woo Kim #include <linux/irq.h>
30d8408326SSeung-Woo Kim #include <linux/delay.h>
31d8408326SSeung-Woo Kim #include <linux/pm_runtime.h>
32d8408326SSeung-Woo Kim #include <linux/clk.h>
33d8408326SSeung-Woo Kim #include <linux/regulator/consumer.h>
34d8408326SSeung-Woo Kim 
35d8408326SSeung-Woo Kim #include <drm/exynos_drm.h>
36d8408326SSeung-Woo Kim 
37d8408326SSeung-Woo Kim #include "exynos_drm_drv.h"
38d8408326SSeung-Woo Kim #include "exynos_drm_hdmi.h"
391055b39fSInki Dae #include "exynos_drm_iommu.h"
4022b21ae6SJoonyoung Shim 
41d8408326SSeung-Woo Kim #define get_mixer_context(dev)	platform_get_drvdata(to_platform_device(dev))
42d8408326SSeung-Woo Kim 
4322b21ae6SJoonyoung Shim struct hdmi_win_data {
4422b21ae6SJoonyoung Shim 	dma_addr_t		dma_addr;
4522b21ae6SJoonyoung Shim 	void __iomem		*vaddr;
4622b21ae6SJoonyoung Shim 	dma_addr_t		chroma_dma_addr;
4722b21ae6SJoonyoung Shim 	void __iomem		*chroma_vaddr;
4822b21ae6SJoonyoung Shim 	uint32_t		pixel_format;
4922b21ae6SJoonyoung Shim 	unsigned int		bpp;
5022b21ae6SJoonyoung Shim 	unsigned int		crtc_x;
5122b21ae6SJoonyoung Shim 	unsigned int		crtc_y;
5222b21ae6SJoonyoung Shim 	unsigned int		crtc_width;
5322b21ae6SJoonyoung Shim 	unsigned int		crtc_height;
5422b21ae6SJoonyoung Shim 	unsigned int		fb_x;
5522b21ae6SJoonyoung Shim 	unsigned int		fb_y;
5622b21ae6SJoonyoung Shim 	unsigned int		fb_width;
5722b21ae6SJoonyoung Shim 	unsigned int		fb_height;
588dcb96b6SSeung-Woo Kim 	unsigned int		src_width;
598dcb96b6SSeung-Woo Kim 	unsigned int		src_height;
6022b21ae6SJoonyoung Shim 	unsigned int		mode_width;
6122b21ae6SJoonyoung Shim 	unsigned int		mode_height;
6222b21ae6SJoonyoung Shim 	unsigned int		scan_flags;
6322b21ae6SJoonyoung Shim };
6422b21ae6SJoonyoung Shim 
6522b21ae6SJoonyoung Shim struct mixer_resources {
6622b21ae6SJoonyoung Shim 	int			irq;
6722b21ae6SJoonyoung Shim 	void __iomem		*mixer_regs;
6822b21ae6SJoonyoung Shim 	void __iomem		*vp_regs;
6922b21ae6SJoonyoung Shim 	spinlock_t		reg_slock;
7022b21ae6SJoonyoung Shim 	struct clk		*mixer;
7122b21ae6SJoonyoung Shim 	struct clk		*vp;
7222b21ae6SJoonyoung Shim 	struct clk		*sclk_mixer;
7322b21ae6SJoonyoung Shim 	struct clk		*sclk_hdmi;
7422b21ae6SJoonyoung Shim 	struct clk		*sclk_dac;
7522b21ae6SJoonyoung Shim };
7622b21ae6SJoonyoung Shim 
771e123441SRahul Sharma enum mixer_version_id {
781e123441SRahul Sharma 	MXR_VER_0_0_0_16,
791e123441SRahul Sharma 	MXR_VER_16_0_33_0,
801e123441SRahul Sharma };
811e123441SRahul Sharma 
8222b21ae6SJoonyoung Shim struct mixer_context {
83cf8fc4f1SJoonyoung Shim 	struct device		*dev;
841055b39fSInki Dae 	struct drm_device	*drm_dev;
8522b21ae6SJoonyoung Shim 	int			pipe;
8622b21ae6SJoonyoung Shim 	bool			interlace;
87cf8fc4f1SJoonyoung Shim 	bool			powered;
881b8e5747SRahul Sharma 	bool			vp_enabled;
89cf8fc4f1SJoonyoung Shim 	u32			int_en;
9022b21ae6SJoonyoung Shim 
91cf8fc4f1SJoonyoung Shim 	struct mutex		mixer_mutex;
9222b21ae6SJoonyoung Shim 	struct mixer_resources	mixer_res;
93a634dd54SJoonyoung Shim 	struct hdmi_win_data	win_data[MIXER_WIN_NR];
941e123441SRahul Sharma 	enum mixer_version_id	mxr_ver;
951055b39fSInki Dae 	void			*parent_ctx;
961e123441SRahul Sharma };
971e123441SRahul Sharma 
981e123441SRahul Sharma struct mixer_drv_data {
991e123441SRahul Sharma 	enum mixer_version_id	version;
1001b8e5747SRahul Sharma 	bool					is_vp_enabled;
10122b21ae6SJoonyoung Shim };
10222b21ae6SJoonyoung Shim 
103d8408326SSeung-Woo Kim static const u8 filter_y_horiz_tap8[] = {
104d8408326SSeung-Woo Kim 	0,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
105d8408326SSeung-Woo Kim 	-1,	-1,	-1,	-1,	-1,	0,	0,	0,
106d8408326SSeung-Woo Kim 	0,	2,	4,	5,	6,	6,	6,	6,
107d8408326SSeung-Woo Kim 	6,	5,	5,	4,	3,	2,	1,	1,
108d8408326SSeung-Woo Kim 	0,	-6,	-12,	-16,	-18,	-20,	-21,	-20,
109d8408326SSeung-Woo Kim 	-20,	-18,	-16,	-13,	-10,	-8,	-5,	-2,
110d8408326SSeung-Woo Kim 	127,	126,	125,	121,	114,	107,	99,	89,
111d8408326SSeung-Woo Kim 	79,	68,	57,	46,	35,	25,	16,	8,
112d8408326SSeung-Woo Kim };
113d8408326SSeung-Woo Kim 
114d8408326SSeung-Woo Kim static const u8 filter_y_vert_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 	0,	5,	11,	19,	27,	37,	48,	59,
120d8408326SSeung-Woo Kim 	70,	81,	92,	102,	111,	118,	124,	126,
121d8408326SSeung-Woo Kim 	0,	0,	-1,	-1,	-2,	-3,	-4,	-5,
122d8408326SSeung-Woo Kim 	-6,	-7,	-8,	-8,	-8,	-8,	-6,	-3,
123d8408326SSeung-Woo Kim };
124d8408326SSeung-Woo Kim 
125d8408326SSeung-Woo Kim static const u8 filter_cr_horiz_tap4[] = {
126d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
127d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
128d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
129d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
130d8408326SSeung-Woo Kim };
131d8408326SSeung-Woo Kim 
132d8408326SSeung-Woo Kim static inline u32 vp_reg_read(struct mixer_resources *res, u32 reg_id)
133d8408326SSeung-Woo Kim {
134d8408326SSeung-Woo Kim 	return readl(res->vp_regs + reg_id);
135d8408326SSeung-Woo Kim }
136d8408326SSeung-Woo Kim 
137d8408326SSeung-Woo Kim static inline void vp_reg_write(struct mixer_resources *res, u32 reg_id,
138d8408326SSeung-Woo Kim 				 u32 val)
139d8408326SSeung-Woo Kim {
140d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
141d8408326SSeung-Woo Kim }
142d8408326SSeung-Woo Kim 
143d8408326SSeung-Woo Kim static inline void vp_reg_writemask(struct mixer_resources *res, u32 reg_id,
144d8408326SSeung-Woo Kim 				 u32 val, u32 mask)
145d8408326SSeung-Woo Kim {
146d8408326SSeung-Woo Kim 	u32 old = vp_reg_read(res, reg_id);
147d8408326SSeung-Woo Kim 
148d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
149d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
150d8408326SSeung-Woo Kim }
151d8408326SSeung-Woo Kim 
152d8408326SSeung-Woo Kim static inline u32 mixer_reg_read(struct mixer_resources *res, u32 reg_id)
153d8408326SSeung-Woo Kim {
154d8408326SSeung-Woo Kim 	return readl(res->mixer_regs + reg_id);
155d8408326SSeung-Woo Kim }
156d8408326SSeung-Woo Kim 
157d8408326SSeung-Woo Kim static inline void mixer_reg_write(struct mixer_resources *res, u32 reg_id,
158d8408326SSeung-Woo Kim 				 u32 val)
159d8408326SSeung-Woo Kim {
160d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
161d8408326SSeung-Woo Kim }
162d8408326SSeung-Woo Kim 
163d8408326SSeung-Woo Kim static inline void mixer_reg_writemask(struct mixer_resources *res,
164d8408326SSeung-Woo Kim 				 u32 reg_id, u32 val, u32 mask)
165d8408326SSeung-Woo Kim {
166d8408326SSeung-Woo Kim 	u32 old = mixer_reg_read(res, reg_id);
167d8408326SSeung-Woo Kim 
168d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
169d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
170d8408326SSeung-Woo Kim }
171d8408326SSeung-Woo Kim 
172d8408326SSeung-Woo Kim static void mixer_regs_dump(struct mixer_context *ctx)
173d8408326SSeung-Woo Kim {
174d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
175d8408326SSeung-Woo Kim do { \
176d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
177d8408326SSeung-Woo Kim 		(u32)readl(ctx->mixer_res.mixer_regs + reg_id)); \
178d8408326SSeung-Woo Kim } while (0)
179d8408326SSeung-Woo Kim 
180d8408326SSeung-Woo Kim 	DUMPREG(MXR_STATUS);
181d8408326SSeung-Woo Kim 	DUMPREG(MXR_CFG);
182d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_EN);
183d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_STATUS);
184d8408326SSeung-Woo Kim 
185d8408326SSeung-Woo Kim 	DUMPREG(MXR_LAYER_CFG);
186d8408326SSeung-Woo Kim 	DUMPREG(MXR_VIDEO_CFG);
187d8408326SSeung-Woo Kim 
188d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_CFG);
189d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_BASE);
190d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SPAN);
191d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_WH);
192d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SXY);
193d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_DXY);
194d8408326SSeung-Woo Kim 
195d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_CFG);
196d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_BASE);
197d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SPAN);
198d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_WH);
199d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SXY);
200d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_DXY);
201d8408326SSeung-Woo Kim #undef DUMPREG
202d8408326SSeung-Woo Kim }
203d8408326SSeung-Woo Kim 
204d8408326SSeung-Woo Kim static void vp_regs_dump(struct mixer_context *ctx)
205d8408326SSeung-Woo Kim {
206d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
207d8408326SSeung-Woo Kim do { \
208d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
209d8408326SSeung-Woo Kim 		(u32) readl(ctx->mixer_res.vp_regs + reg_id)); \
210d8408326SSeung-Woo Kim } while (0)
211d8408326SSeung-Woo Kim 
212d8408326SSeung-Woo Kim 	DUMPREG(VP_ENABLE);
213d8408326SSeung-Woo Kim 	DUMPREG(VP_SRESET);
214d8408326SSeung-Woo Kim 	DUMPREG(VP_SHADOW_UPDATE);
215d8408326SSeung-Woo Kim 	DUMPREG(VP_FIELD_ID);
216d8408326SSeung-Woo Kim 	DUMPREG(VP_MODE);
217d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_Y);
218d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_C);
219d8408326SSeung-Woo Kim 	DUMPREG(VP_PER_RATE_CTRL);
220d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_Y_PTR);
221d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_Y_PTR);
222d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_C_PTR);
223d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_C_PTR);
224d8408326SSeung-Woo Kim 	DUMPREG(VP_ENDIAN_MODE);
225d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_H_POSITION);
226d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_V_POSITION);
227d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_WIDTH);
228d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_HEIGHT);
229d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_H_POSITION);
230d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_V_POSITION);
231d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_WIDTH);
232d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_HEIGHT);
233d8408326SSeung-Woo Kim 	DUMPREG(VP_H_RATIO);
234d8408326SSeung-Woo Kim 	DUMPREG(VP_V_RATIO);
235d8408326SSeung-Woo Kim 
236d8408326SSeung-Woo Kim #undef DUMPREG
237d8408326SSeung-Woo Kim }
238d8408326SSeung-Woo Kim 
239d8408326SSeung-Woo Kim static inline void vp_filter_set(struct mixer_resources *res,
240d8408326SSeung-Woo Kim 		int reg_id, const u8 *data, unsigned int size)
241d8408326SSeung-Woo Kim {
242d8408326SSeung-Woo Kim 	/* assure 4-byte align */
243d8408326SSeung-Woo Kim 	BUG_ON(size & 3);
244d8408326SSeung-Woo Kim 	for (; size; size -= 4, reg_id += 4, data += 4) {
245d8408326SSeung-Woo Kim 		u32 val = (data[0] << 24) |  (data[1] << 16) |
246d8408326SSeung-Woo Kim 			(data[2] << 8) | data[3];
247d8408326SSeung-Woo Kim 		vp_reg_write(res, reg_id, val);
248d8408326SSeung-Woo Kim 	}
249d8408326SSeung-Woo Kim }
250d8408326SSeung-Woo Kim 
251d8408326SSeung-Woo Kim static void vp_default_filter(struct mixer_resources *res)
252d8408326SSeung-Woo Kim {
253d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY8_Y0_LL,
254e25e1b66SSachin Kamat 		filter_y_horiz_tap8, sizeof(filter_y_horiz_tap8));
255d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_Y0_LL,
256e25e1b66SSachin Kamat 		filter_y_vert_tap4, sizeof(filter_y_vert_tap4));
257d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_C0_LL,
258e25e1b66SSachin Kamat 		filter_cr_horiz_tap4, sizeof(filter_cr_horiz_tap4));
259d8408326SSeung-Woo Kim }
260d8408326SSeung-Woo Kim 
261d8408326SSeung-Woo Kim static void mixer_vsync_set_update(struct mixer_context *ctx, bool enable)
262d8408326SSeung-Woo Kim {
263d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
264d8408326SSeung-Woo Kim 
265d8408326SSeung-Woo Kim 	/* block update on vsync */
266d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, enable ?
267d8408326SSeung-Woo Kim 			MXR_STATUS_SYNC_ENABLE : 0, MXR_STATUS_SYNC_ENABLE);
268d8408326SSeung-Woo Kim 
2691b8e5747SRahul Sharma 	if (ctx->vp_enabled)
270d8408326SSeung-Woo Kim 		vp_reg_write(res, VP_SHADOW_UPDATE, enable ?
271d8408326SSeung-Woo Kim 			VP_SHADOW_UPDATE_ENABLE : 0);
272d8408326SSeung-Woo Kim }
273d8408326SSeung-Woo Kim 
274d8408326SSeung-Woo Kim static void mixer_cfg_scan(struct mixer_context *ctx, unsigned int height)
275d8408326SSeung-Woo Kim {
276d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
277d8408326SSeung-Woo Kim 	u32 val;
278d8408326SSeung-Woo Kim 
279d8408326SSeung-Woo Kim 	/* choosing between interlace and progressive mode */
280d8408326SSeung-Woo Kim 	val = (ctx->interlace ? MXR_CFG_SCAN_INTERLACE :
281d8408326SSeung-Woo Kim 				MXR_CFG_SCAN_PROGRASSIVE);
282d8408326SSeung-Woo Kim 
283d8408326SSeung-Woo Kim 	/* choosing between porper HD and SD mode */
284d8408326SSeung-Woo Kim 	if (height == 480)
285d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD;
286d8408326SSeung-Woo Kim 	else if (height == 576)
287d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD;
288d8408326SSeung-Woo Kim 	else if (height == 720)
289d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
290d8408326SSeung-Woo Kim 	else if (height == 1080)
291d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_HD_1080 | MXR_CFG_SCAN_HD;
292d8408326SSeung-Woo Kim 	else
293d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
294d8408326SSeung-Woo Kim 
295d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_SCAN_MASK);
296d8408326SSeung-Woo Kim }
297d8408326SSeung-Woo Kim 
298d8408326SSeung-Woo Kim static void mixer_cfg_rgb_fmt(struct mixer_context *ctx, unsigned int height)
299d8408326SSeung-Woo Kim {
300d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
301d8408326SSeung-Woo Kim 	u32 val;
302d8408326SSeung-Woo Kim 
303d8408326SSeung-Woo Kim 	if (height == 480) {
304d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
305d8408326SSeung-Woo Kim 	} else if (height == 576) {
306d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
307d8408326SSeung-Woo Kim 	} else if (height == 720) {
308d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
309d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
310d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
311d8408326SSeung-Woo Kim 				(32 << 0));
312d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
313d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
314d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
315d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
316d8408326SSeung-Woo Kim 	} else if (height == 1080) {
317d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
318d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
319d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
320d8408326SSeung-Woo Kim 				(32 << 0));
321d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
322d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
323d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
324d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
325d8408326SSeung-Woo Kim 	} else {
326d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
327d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
328d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
329d8408326SSeung-Woo Kim 				(32 << 0));
330d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
331d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
332d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
333d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
334d8408326SSeung-Woo Kim 	}
335d8408326SSeung-Woo Kim 
336d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
337d8408326SSeung-Woo Kim }
338d8408326SSeung-Woo Kim 
339d8408326SSeung-Woo Kim static void mixer_cfg_layer(struct mixer_context *ctx, int win, bool enable)
340d8408326SSeung-Woo Kim {
341d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
342d8408326SSeung-Woo Kim 	u32 val = enable ? ~0 : 0;
343d8408326SSeung-Woo Kim 
344d8408326SSeung-Woo Kim 	switch (win) {
345d8408326SSeung-Woo Kim 	case 0:
346d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
347d8408326SSeung-Woo Kim 		break;
348d8408326SSeung-Woo Kim 	case 1:
349d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
350d8408326SSeung-Woo Kim 		break;
351d8408326SSeung-Woo Kim 	case 2:
3521b8e5747SRahul Sharma 		if (ctx->vp_enabled) {
353d8408326SSeung-Woo Kim 			vp_reg_writemask(res, VP_ENABLE, val, VP_ENABLE_ON);
3541b8e5747SRahul Sharma 			mixer_reg_writemask(res, MXR_CFG, val,
3551b8e5747SRahul Sharma 				MXR_CFG_VP_ENABLE);
3561b8e5747SRahul Sharma 		}
357d8408326SSeung-Woo Kim 		break;
358d8408326SSeung-Woo Kim 	}
359d8408326SSeung-Woo Kim }
360d8408326SSeung-Woo Kim 
361d8408326SSeung-Woo Kim static void mixer_run(struct mixer_context *ctx)
362d8408326SSeung-Woo Kim {
363d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
364d8408326SSeung-Woo Kim 
365d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
366d8408326SSeung-Woo Kim 
367d8408326SSeung-Woo Kim 	mixer_regs_dump(ctx);
368d8408326SSeung-Woo Kim }
369d8408326SSeung-Woo Kim 
370d8408326SSeung-Woo Kim static void vp_video_buffer(struct mixer_context *ctx, int win)
371d8408326SSeung-Woo Kim {
372d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
373d8408326SSeung-Woo Kim 	unsigned long flags;
374d8408326SSeung-Woo Kim 	struct hdmi_win_data *win_data;
375d8408326SSeung-Woo Kim 	unsigned int x_ratio, y_ratio;
376d8408326SSeung-Woo Kim 	unsigned int buf_num;
377d8408326SSeung-Woo Kim 	dma_addr_t luma_addr[2], chroma_addr[2];
378d8408326SSeung-Woo Kim 	bool tiled_mode = false;
379d8408326SSeung-Woo Kim 	bool crcb_mode = false;
380d8408326SSeung-Woo Kim 	u32 val;
381d8408326SSeung-Woo Kim 
382d8408326SSeung-Woo Kim 	win_data = &ctx->win_data[win];
383d8408326SSeung-Woo Kim 
384d8408326SSeung-Woo Kim 	switch (win_data->pixel_format) {
385d8408326SSeung-Woo Kim 	case DRM_FORMAT_NV12MT:
386d8408326SSeung-Woo Kim 		tiled_mode = true;
387363b06aaSVille Syrjälä 	case DRM_FORMAT_NV12:
388d8408326SSeung-Woo Kim 		crcb_mode = false;
389d8408326SSeung-Woo Kim 		buf_num = 2;
390d8408326SSeung-Woo Kim 		break;
391d8408326SSeung-Woo Kim 	/* TODO: single buffer format NV12, NV21 */
392d8408326SSeung-Woo Kim 	default:
393d8408326SSeung-Woo Kim 		/* ignore pixel format at disable time */
394d8408326SSeung-Woo Kim 		if (!win_data->dma_addr)
395d8408326SSeung-Woo Kim 			break;
396d8408326SSeung-Woo Kim 
397d8408326SSeung-Woo Kim 		DRM_ERROR("pixel format for vp is wrong [%d].\n",
398d8408326SSeung-Woo Kim 				win_data->pixel_format);
399d8408326SSeung-Woo Kim 		return;
400d8408326SSeung-Woo Kim 	}
401d8408326SSeung-Woo Kim 
402d8408326SSeung-Woo Kim 	/* scaling feature: (src << 16) / dst */
4038dcb96b6SSeung-Woo Kim 	x_ratio = (win_data->src_width << 16) / win_data->crtc_width;
4048dcb96b6SSeung-Woo Kim 	y_ratio = (win_data->src_height << 16) / win_data->crtc_height;
405d8408326SSeung-Woo Kim 
406d8408326SSeung-Woo Kim 	if (buf_num == 2) {
407d8408326SSeung-Woo Kim 		luma_addr[0] = win_data->dma_addr;
408d8408326SSeung-Woo Kim 		chroma_addr[0] = win_data->chroma_dma_addr;
409d8408326SSeung-Woo Kim 	} else {
410d8408326SSeung-Woo Kim 		luma_addr[0] = win_data->dma_addr;
411d8408326SSeung-Woo Kim 		chroma_addr[0] = win_data->dma_addr
4128dcb96b6SSeung-Woo Kim 			+ (win_data->fb_width * win_data->fb_height);
413d8408326SSeung-Woo Kim 	}
414d8408326SSeung-Woo Kim 
415d8408326SSeung-Woo Kim 	if (win_data->scan_flags & DRM_MODE_FLAG_INTERLACE) {
416d8408326SSeung-Woo Kim 		ctx->interlace = true;
417d8408326SSeung-Woo Kim 		if (tiled_mode) {
418d8408326SSeung-Woo Kim 			luma_addr[1] = luma_addr[0] + 0x40;
419d8408326SSeung-Woo Kim 			chroma_addr[1] = chroma_addr[0] + 0x40;
420d8408326SSeung-Woo Kim 		} else {
4218dcb96b6SSeung-Woo Kim 			luma_addr[1] = luma_addr[0] + win_data->fb_width;
4228dcb96b6SSeung-Woo Kim 			chroma_addr[1] = chroma_addr[0] + win_data->fb_width;
423d8408326SSeung-Woo Kim 		}
424d8408326SSeung-Woo Kim 	} else {
425d8408326SSeung-Woo Kim 		ctx->interlace = false;
426d8408326SSeung-Woo Kim 		luma_addr[1] = 0;
427d8408326SSeung-Woo Kim 		chroma_addr[1] = 0;
428d8408326SSeung-Woo Kim 	}
429d8408326SSeung-Woo Kim 
430d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
431d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, false);
432d8408326SSeung-Woo Kim 
433d8408326SSeung-Woo Kim 	/* interlace or progressive scan mode */
434d8408326SSeung-Woo Kim 	val = (ctx->interlace ? ~0 : 0);
435d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_LINE_SKIP);
436d8408326SSeung-Woo Kim 
437d8408326SSeung-Woo Kim 	/* setup format */
438d8408326SSeung-Woo Kim 	val = (crcb_mode ? VP_MODE_NV21 : VP_MODE_NV12);
439d8408326SSeung-Woo Kim 	val |= (tiled_mode ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
440d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_FMT_MASK);
441d8408326SSeung-Woo Kim 
442d8408326SSeung-Woo Kim 	/* setting size of input image */
4438dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_IMG_SIZE_Y, VP_IMG_HSIZE(win_data->fb_width) |
4448dcb96b6SSeung-Woo Kim 		VP_IMG_VSIZE(win_data->fb_height));
445d8408326SSeung-Woo Kim 	/* chroma height has to reduced by 2 to avoid chroma distorions */
4468dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_IMG_SIZE_C, VP_IMG_HSIZE(win_data->fb_width) |
4478dcb96b6SSeung-Woo Kim 		VP_IMG_VSIZE(win_data->fb_height / 2));
448d8408326SSeung-Woo Kim 
4498dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_WIDTH, win_data->src_width);
4508dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_HEIGHT, win_data->src_height);
451d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_H_POSITION,
4528dcb96b6SSeung-Woo Kim 			VP_SRC_H_POSITION_VAL(win_data->fb_x));
4538dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_V_POSITION, win_data->fb_y);
454d8408326SSeung-Woo Kim 
4558dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_DST_WIDTH, win_data->crtc_width);
4568dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_DST_H_POSITION, win_data->crtc_x);
457d8408326SSeung-Woo Kim 	if (ctx->interlace) {
4588dcb96b6SSeung-Woo Kim 		vp_reg_write(res, VP_DST_HEIGHT, win_data->crtc_height / 2);
4598dcb96b6SSeung-Woo Kim 		vp_reg_write(res, VP_DST_V_POSITION, win_data->crtc_y / 2);
460d8408326SSeung-Woo Kim 	} else {
4618dcb96b6SSeung-Woo Kim 		vp_reg_write(res, VP_DST_HEIGHT, win_data->crtc_height);
4628dcb96b6SSeung-Woo Kim 		vp_reg_write(res, VP_DST_V_POSITION, win_data->crtc_y);
463d8408326SSeung-Woo Kim 	}
464d8408326SSeung-Woo Kim 
465d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_H_RATIO, x_ratio);
466d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_V_RATIO, y_ratio);
467d8408326SSeung-Woo Kim 
468d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
469d8408326SSeung-Woo Kim 
470d8408326SSeung-Woo Kim 	/* set buffer address to vp */
471d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_Y_PTR, luma_addr[0]);
472d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_Y_PTR, luma_addr[1]);
473d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_C_PTR, chroma_addr[0]);
474d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_C_PTR, chroma_addr[1]);
475d8408326SSeung-Woo Kim 
4768dcb96b6SSeung-Woo Kim 	mixer_cfg_scan(ctx, win_data->mode_height);
4778dcb96b6SSeung-Woo Kim 	mixer_cfg_rgb_fmt(ctx, win_data->mode_height);
478d8408326SSeung-Woo Kim 	mixer_cfg_layer(ctx, win, true);
479d8408326SSeung-Woo Kim 	mixer_run(ctx);
480d8408326SSeung-Woo Kim 
481d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, true);
482d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
483d8408326SSeung-Woo Kim 
484d8408326SSeung-Woo Kim 	vp_regs_dump(ctx);
485d8408326SSeung-Woo Kim }
486d8408326SSeung-Woo Kim 
487aaf8b49eSRahul Sharma static void mixer_layer_update(struct mixer_context *ctx)
488aaf8b49eSRahul Sharma {
489aaf8b49eSRahul Sharma 	struct mixer_resources *res = &ctx->mixer_res;
490aaf8b49eSRahul Sharma 	u32 val;
491aaf8b49eSRahul Sharma 
492aaf8b49eSRahul Sharma 	val = mixer_reg_read(res, MXR_CFG);
493aaf8b49eSRahul Sharma 
494aaf8b49eSRahul Sharma 	/* allow one update per vsync only */
495aaf8b49eSRahul Sharma 	if (!(val & MXR_CFG_LAYER_UPDATE_COUNT_MASK))
496aaf8b49eSRahul Sharma 		mixer_reg_writemask(res, MXR_CFG, ~0, MXR_CFG_LAYER_UPDATE);
497aaf8b49eSRahul Sharma }
498aaf8b49eSRahul Sharma 
499d8408326SSeung-Woo Kim static void mixer_graph_buffer(struct mixer_context *ctx, int win)
500d8408326SSeung-Woo Kim {
501d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
502d8408326SSeung-Woo Kim 	unsigned long flags;
503d8408326SSeung-Woo Kim 	struct hdmi_win_data *win_data;
504d8408326SSeung-Woo Kim 	unsigned int x_ratio, y_ratio;
505d8408326SSeung-Woo Kim 	unsigned int src_x_offset, src_y_offset, dst_x_offset, dst_y_offset;
506d8408326SSeung-Woo Kim 	dma_addr_t dma_addr;
507d8408326SSeung-Woo Kim 	unsigned int fmt;
508d8408326SSeung-Woo Kim 	u32 val;
509d8408326SSeung-Woo Kim 
510d8408326SSeung-Woo Kim 	win_data = &ctx->win_data[win];
511d8408326SSeung-Woo Kim 
512d8408326SSeung-Woo Kim 	#define RGB565 4
513d8408326SSeung-Woo Kim 	#define ARGB1555 5
514d8408326SSeung-Woo Kim 	#define ARGB4444 6
515d8408326SSeung-Woo Kim 	#define ARGB8888 7
516d8408326SSeung-Woo Kim 
517d8408326SSeung-Woo Kim 	switch (win_data->bpp) {
518d8408326SSeung-Woo Kim 	case 16:
519d8408326SSeung-Woo Kim 		fmt = ARGB4444;
520d8408326SSeung-Woo Kim 		break;
521d8408326SSeung-Woo Kim 	case 32:
522d8408326SSeung-Woo Kim 		fmt = ARGB8888;
523d8408326SSeung-Woo Kim 		break;
524d8408326SSeung-Woo Kim 	default:
525d8408326SSeung-Woo Kim 		fmt = ARGB8888;
526d8408326SSeung-Woo Kim 	}
527d8408326SSeung-Woo Kim 
528d8408326SSeung-Woo Kim 	/* 2x scaling feature */
529d8408326SSeung-Woo Kim 	x_ratio = 0;
530d8408326SSeung-Woo Kim 	y_ratio = 0;
531d8408326SSeung-Woo Kim 
532d8408326SSeung-Woo Kim 	dst_x_offset = win_data->crtc_x;
533d8408326SSeung-Woo Kim 	dst_y_offset = win_data->crtc_y;
534d8408326SSeung-Woo Kim 
535d8408326SSeung-Woo Kim 	/* converting dma address base and source offset */
5368dcb96b6SSeung-Woo Kim 	dma_addr = win_data->dma_addr
5378dcb96b6SSeung-Woo Kim 		+ (win_data->fb_x * win_data->bpp >> 3)
5388dcb96b6SSeung-Woo Kim 		+ (win_data->fb_y * win_data->fb_width * win_data->bpp >> 3);
539d8408326SSeung-Woo Kim 	src_x_offset = 0;
540d8408326SSeung-Woo Kim 	src_y_offset = 0;
541d8408326SSeung-Woo Kim 
542d8408326SSeung-Woo Kim 	if (win_data->scan_flags & DRM_MODE_FLAG_INTERLACE)
543d8408326SSeung-Woo Kim 		ctx->interlace = true;
544d8408326SSeung-Woo Kim 	else
545d8408326SSeung-Woo Kim 		ctx->interlace = false;
546d8408326SSeung-Woo Kim 
547d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
548d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, false);
549d8408326SSeung-Woo Kim 
550d8408326SSeung-Woo Kim 	/* setup format */
551d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_GRAPHIC_CFG(win),
552d8408326SSeung-Woo Kim 		MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
553d8408326SSeung-Woo Kim 
554d8408326SSeung-Woo Kim 	/* setup geometry */
5558dcb96b6SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_SPAN(win), win_data->fb_width);
556d8408326SSeung-Woo Kim 
5578dcb96b6SSeung-Woo Kim 	val  = MXR_GRP_WH_WIDTH(win_data->crtc_width);
5588dcb96b6SSeung-Woo Kim 	val |= MXR_GRP_WH_HEIGHT(win_data->crtc_height);
559d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_H_SCALE(x_ratio);
560d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_V_SCALE(y_ratio);
561d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_WH(win), val);
562d8408326SSeung-Woo Kim 
563d8408326SSeung-Woo Kim 	/* setup offsets in source image */
564d8408326SSeung-Woo Kim 	val  = MXR_GRP_SXY_SX(src_x_offset);
565d8408326SSeung-Woo Kim 	val |= MXR_GRP_SXY_SY(src_y_offset);
566d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_SXY(win), val);
567d8408326SSeung-Woo Kim 
568d8408326SSeung-Woo Kim 	/* setup offsets in display image */
569d8408326SSeung-Woo Kim 	val  = MXR_GRP_DXY_DX(dst_x_offset);
570d8408326SSeung-Woo Kim 	val |= MXR_GRP_DXY_DY(dst_y_offset);
571d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_DXY(win), val);
572d8408326SSeung-Woo Kim 
573d8408326SSeung-Woo Kim 	/* set buffer address to mixer */
574d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_BASE(win), dma_addr);
575d8408326SSeung-Woo Kim 
5768dcb96b6SSeung-Woo Kim 	mixer_cfg_scan(ctx, win_data->mode_height);
5778dcb96b6SSeung-Woo Kim 	mixer_cfg_rgb_fmt(ctx, win_data->mode_height);
578d8408326SSeung-Woo Kim 	mixer_cfg_layer(ctx, win, true);
579aaf8b49eSRahul Sharma 
580aaf8b49eSRahul Sharma 	/* layer update mandatory for mixer 16.0.33.0 */
581aaf8b49eSRahul Sharma 	if (ctx->mxr_ver == MXR_VER_16_0_33_0)
582aaf8b49eSRahul Sharma 		mixer_layer_update(ctx);
583aaf8b49eSRahul Sharma 
584d8408326SSeung-Woo Kim 	mixer_run(ctx);
585d8408326SSeung-Woo Kim 
586d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, true);
587d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
588d8408326SSeung-Woo Kim }
589d8408326SSeung-Woo Kim 
590d8408326SSeung-Woo Kim static void vp_win_reset(struct mixer_context *ctx)
591d8408326SSeung-Woo Kim {
592d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
593d8408326SSeung-Woo Kim 	int tries = 100;
594d8408326SSeung-Woo Kim 
595d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRESET, VP_SRESET_PROCESSING);
596d8408326SSeung-Woo Kim 	for (tries = 100; tries; --tries) {
597d8408326SSeung-Woo Kim 		/* waiting until VP_SRESET_PROCESSING is 0 */
598d8408326SSeung-Woo Kim 		if (~vp_reg_read(res, VP_SRESET) & VP_SRESET_PROCESSING)
599d8408326SSeung-Woo Kim 			break;
600d8408326SSeung-Woo Kim 		mdelay(10);
601d8408326SSeung-Woo Kim 	}
602d8408326SSeung-Woo Kim 	WARN(tries == 0, "failed to reset Video Processor\n");
603d8408326SSeung-Woo Kim }
604d8408326SSeung-Woo Kim 
605cf8fc4f1SJoonyoung Shim static void mixer_win_reset(struct mixer_context *ctx)
606cf8fc4f1SJoonyoung Shim {
607cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
608cf8fc4f1SJoonyoung Shim 	unsigned long flags;
609cf8fc4f1SJoonyoung Shim 	u32 val; /* value stored to register */
610cf8fc4f1SJoonyoung Shim 
611cf8fc4f1SJoonyoung Shim 	spin_lock_irqsave(&res->reg_slock, flags);
612cf8fc4f1SJoonyoung Shim 	mixer_vsync_set_update(ctx, false);
613cf8fc4f1SJoonyoung Shim 
614cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
615cf8fc4f1SJoonyoung Shim 
616cf8fc4f1SJoonyoung Shim 	/* set output in RGB888 mode */
617cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
618cf8fc4f1SJoonyoung Shim 
619cf8fc4f1SJoonyoung Shim 	/* 16 beat burst in DMA */
620cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_STATUS, MXR_STATUS_16_BURST,
621cf8fc4f1SJoonyoung Shim 		MXR_STATUS_BURST_MASK);
622cf8fc4f1SJoonyoung Shim 
623cf8fc4f1SJoonyoung Shim 	/* setting default layer priority: layer1 > layer0 > video
624cf8fc4f1SJoonyoung Shim 	 * because typical usage scenario would be
625cf8fc4f1SJoonyoung Shim 	 * layer1 - OSD
626cf8fc4f1SJoonyoung Shim 	 * layer0 - framebuffer
627cf8fc4f1SJoonyoung Shim 	 * video - video overlay
628cf8fc4f1SJoonyoung Shim 	 */
629cf8fc4f1SJoonyoung Shim 	val = MXR_LAYER_CFG_GRP1_VAL(3);
630cf8fc4f1SJoonyoung Shim 	val |= MXR_LAYER_CFG_GRP0_VAL(2);
6311b8e5747SRahul Sharma 	if (ctx->vp_enabled)
632cf8fc4f1SJoonyoung Shim 		val |= MXR_LAYER_CFG_VP_VAL(1);
633cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_LAYER_CFG, val);
634cf8fc4f1SJoonyoung Shim 
635cf8fc4f1SJoonyoung Shim 	/* setting background color */
636cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR0, 0x008080);
637cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR1, 0x008080);
638cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR2, 0x008080);
639cf8fc4f1SJoonyoung Shim 
640cf8fc4f1SJoonyoung Shim 	/* setting graphical layers */
641cf8fc4f1SJoonyoung Shim 	val  = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
642cf8fc4f1SJoonyoung Shim 	val |= MXR_GRP_CFG_WIN_BLEND_EN;
6435736603bSSeung-Woo Kim 	val |= MXR_GRP_CFG_BLEND_PRE_MUL;
6445736603bSSeung-Woo Kim 	val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
645cf8fc4f1SJoonyoung Shim 	val |= MXR_GRP_CFG_ALPHA_VAL(0xff); /* non-transparent alpha */
646cf8fc4f1SJoonyoung Shim 
647cf8fc4f1SJoonyoung Shim 	/* the same configuration for both layers */
648cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_GRAPHIC_CFG(0), val);
649cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_GRAPHIC_CFG(1), val);
650cf8fc4f1SJoonyoung Shim 
6515736603bSSeung-Woo Kim 	/* setting video layers */
6525736603bSSeung-Woo Kim 	val = MXR_GRP_CFG_ALPHA_VAL(0);
6535736603bSSeung-Woo Kim 	mixer_reg_write(res, MXR_VIDEO_CFG, val);
6545736603bSSeung-Woo Kim 
6551b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
656cf8fc4f1SJoonyoung Shim 		/* configuration of Video Processor Registers */
657cf8fc4f1SJoonyoung Shim 		vp_win_reset(ctx);
658cf8fc4f1SJoonyoung Shim 		vp_default_filter(res);
6591b8e5747SRahul Sharma 	}
660cf8fc4f1SJoonyoung Shim 
661cf8fc4f1SJoonyoung Shim 	/* disable all layers */
662cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
663cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
6641b8e5747SRahul Sharma 	if (ctx->vp_enabled)
665cf8fc4f1SJoonyoung Shim 		mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
666cf8fc4f1SJoonyoung Shim 
667cf8fc4f1SJoonyoung Shim 	mixer_vsync_set_update(ctx, true);
668cf8fc4f1SJoonyoung Shim 	spin_unlock_irqrestore(&res->reg_slock, flags);
669cf8fc4f1SJoonyoung Shim }
670cf8fc4f1SJoonyoung Shim 
6711055b39fSInki Dae static int mixer_iommu_on(void *ctx, bool enable)
6721055b39fSInki Dae {
6731055b39fSInki Dae 	struct exynos_drm_hdmi_context *drm_hdmi_ctx;
6741055b39fSInki Dae 	struct mixer_context *mdata = ctx;
6751055b39fSInki Dae 	struct drm_device *drm_dev;
6761055b39fSInki Dae 
6771055b39fSInki Dae 	drm_hdmi_ctx = mdata->parent_ctx;
6781055b39fSInki Dae 	drm_dev = drm_hdmi_ctx->drm_dev;
6791055b39fSInki Dae 
6801055b39fSInki Dae 	if (is_drm_iommu_supported(drm_dev)) {
6811055b39fSInki Dae 		if (enable)
6821055b39fSInki Dae 			return drm_iommu_attach_device(drm_dev, mdata->dev);
6831055b39fSInki Dae 
6841055b39fSInki Dae 		drm_iommu_detach_device(drm_dev, mdata->dev);
6851055b39fSInki Dae 	}
6861055b39fSInki Dae 	return 0;
6871055b39fSInki Dae }
6881055b39fSInki Dae 
689cf8fc4f1SJoonyoung Shim static void mixer_poweron(struct mixer_context *ctx)
690cf8fc4f1SJoonyoung Shim {
691cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
692cf8fc4f1SJoonyoung Shim 
693cf8fc4f1SJoonyoung Shim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
694cf8fc4f1SJoonyoung Shim 
695cf8fc4f1SJoonyoung Shim 	mutex_lock(&ctx->mixer_mutex);
696cf8fc4f1SJoonyoung Shim 	if (ctx->powered) {
697cf8fc4f1SJoonyoung Shim 		mutex_unlock(&ctx->mixer_mutex);
698cf8fc4f1SJoonyoung Shim 		return;
699cf8fc4f1SJoonyoung Shim 	}
700cf8fc4f1SJoonyoung Shim 	ctx->powered = true;
701cf8fc4f1SJoonyoung Shim 	mutex_unlock(&ctx->mixer_mutex);
702cf8fc4f1SJoonyoung Shim 
703cf8fc4f1SJoonyoung Shim 	pm_runtime_get_sync(ctx->dev);
704cf8fc4f1SJoonyoung Shim 
705cf8fc4f1SJoonyoung Shim 	clk_enable(res->mixer);
7061b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
707cf8fc4f1SJoonyoung Shim 		clk_enable(res->vp);
708cf8fc4f1SJoonyoung Shim 		clk_enable(res->sclk_mixer);
7091b8e5747SRahul Sharma 	}
710cf8fc4f1SJoonyoung Shim 
711cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_INT_EN, ctx->int_en);
712cf8fc4f1SJoonyoung Shim 	mixer_win_reset(ctx);
713cf8fc4f1SJoonyoung Shim }
714cf8fc4f1SJoonyoung Shim 
715cf8fc4f1SJoonyoung Shim static void mixer_poweroff(struct mixer_context *ctx)
716cf8fc4f1SJoonyoung Shim {
717cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
718cf8fc4f1SJoonyoung Shim 
719cf8fc4f1SJoonyoung Shim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
720cf8fc4f1SJoonyoung Shim 
721cf8fc4f1SJoonyoung Shim 	mutex_lock(&ctx->mixer_mutex);
722cf8fc4f1SJoonyoung Shim 	if (!ctx->powered)
723cf8fc4f1SJoonyoung Shim 		goto out;
724cf8fc4f1SJoonyoung Shim 	mutex_unlock(&ctx->mixer_mutex);
725cf8fc4f1SJoonyoung Shim 
726cf8fc4f1SJoonyoung Shim 	ctx->int_en = mixer_reg_read(res, MXR_INT_EN);
727cf8fc4f1SJoonyoung Shim 
728cf8fc4f1SJoonyoung Shim 	clk_disable(res->mixer);
7291b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
730cf8fc4f1SJoonyoung Shim 		clk_disable(res->vp);
731cf8fc4f1SJoonyoung Shim 		clk_disable(res->sclk_mixer);
7321b8e5747SRahul Sharma 	}
733cf8fc4f1SJoonyoung Shim 
734cf8fc4f1SJoonyoung Shim 	pm_runtime_put_sync(ctx->dev);
735cf8fc4f1SJoonyoung Shim 
736cf8fc4f1SJoonyoung Shim 	mutex_lock(&ctx->mixer_mutex);
737cf8fc4f1SJoonyoung Shim 	ctx->powered = false;
738cf8fc4f1SJoonyoung Shim 
739cf8fc4f1SJoonyoung Shim out:
740cf8fc4f1SJoonyoung Shim 	mutex_unlock(&ctx->mixer_mutex);
741cf8fc4f1SJoonyoung Shim }
742cf8fc4f1SJoonyoung Shim 
743d8408326SSeung-Woo Kim static int mixer_enable_vblank(void *ctx, int pipe)
744d8408326SSeung-Woo Kim {
745d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
746d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
747d8408326SSeung-Woo Kim 
748d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
749d8408326SSeung-Woo Kim 
750d8408326SSeung-Woo Kim 	mixer_ctx->pipe = pipe;
751d8408326SSeung-Woo Kim 
752d8408326SSeung-Woo Kim 	/* enable vsync interrupt */
753d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_INT_EN, MXR_INT_EN_VSYNC,
754d8408326SSeung-Woo Kim 			MXR_INT_EN_VSYNC);
755d8408326SSeung-Woo Kim 
756d8408326SSeung-Woo Kim 	return 0;
757d8408326SSeung-Woo Kim }
758d8408326SSeung-Woo Kim 
759d8408326SSeung-Woo Kim static void mixer_disable_vblank(void *ctx)
760d8408326SSeung-Woo Kim {
761d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
762d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
763d8408326SSeung-Woo Kim 
764d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
765d8408326SSeung-Woo Kim 
766d8408326SSeung-Woo Kim 	/* disable vsync interrupt */
767d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
768d8408326SSeung-Woo Kim }
769d8408326SSeung-Woo Kim 
770cf8fc4f1SJoonyoung Shim static void mixer_dpms(void *ctx, int mode)
771cf8fc4f1SJoonyoung Shim {
772cf8fc4f1SJoonyoung Shim 	struct mixer_context *mixer_ctx = ctx;
773cf8fc4f1SJoonyoung Shim 
774cf8fc4f1SJoonyoung Shim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
775cf8fc4f1SJoonyoung Shim 
776cf8fc4f1SJoonyoung Shim 	switch (mode) {
777cf8fc4f1SJoonyoung Shim 	case DRM_MODE_DPMS_ON:
778cf8fc4f1SJoonyoung Shim 		mixer_poweron(mixer_ctx);
779cf8fc4f1SJoonyoung Shim 		break;
780cf8fc4f1SJoonyoung Shim 	case DRM_MODE_DPMS_STANDBY:
781cf8fc4f1SJoonyoung Shim 	case DRM_MODE_DPMS_SUSPEND:
782cf8fc4f1SJoonyoung Shim 	case DRM_MODE_DPMS_OFF:
783cf8fc4f1SJoonyoung Shim 		mixer_poweroff(mixer_ctx);
784cf8fc4f1SJoonyoung Shim 		break;
785cf8fc4f1SJoonyoung Shim 	default:
786cf8fc4f1SJoonyoung Shim 		DRM_DEBUG_KMS("unknown dpms mode: %d\n", mode);
787cf8fc4f1SJoonyoung Shim 		break;
788cf8fc4f1SJoonyoung Shim 	}
789cf8fc4f1SJoonyoung Shim }
790cf8fc4f1SJoonyoung Shim 
791d8408326SSeung-Woo Kim static void mixer_win_mode_set(void *ctx,
792d8408326SSeung-Woo Kim 			      struct exynos_drm_overlay *overlay)
793d8408326SSeung-Woo Kim {
794d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
795d8408326SSeung-Woo Kim 	struct hdmi_win_data *win_data;
796d8408326SSeung-Woo Kim 	int win;
797d8408326SSeung-Woo Kim 
798d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
799d8408326SSeung-Woo Kim 
800d8408326SSeung-Woo Kim 	if (!overlay) {
801d8408326SSeung-Woo Kim 		DRM_ERROR("overlay is NULL\n");
802d8408326SSeung-Woo Kim 		return;
803d8408326SSeung-Woo Kim 	}
804d8408326SSeung-Woo Kim 
805d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("set [%d]x[%d] at (%d,%d) to [%d]x[%d] at (%d,%d)\n",
806d8408326SSeung-Woo Kim 				 overlay->fb_width, overlay->fb_height,
807d8408326SSeung-Woo Kim 				 overlay->fb_x, overlay->fb_y,
808d8408326SSeung-Woo Kim 				 overlay->crtc_width, overlay->crtc_height,
809d8408326SSeung-Woo Kim 				 overlay->crtc_x, overlay->crtc_y);
810d8408326SSeung-Woo Kim 
811d8408326SSeung-Woo Kim 	win = overlay->zpos;
812d8408326SSeung-Woo Kim 	if (win == DEFAULT_ZPOS)
813a2ee151bSJoonyoung Shim 		win = MIXER_DEFAULT_WIN;
814d8408326SSeung-Woo Kim 
815a634dd54SJoonyoung Shim 	if (win < 0 || win > MIXER_WIN_NR) {
816cf8fc4f1SJoonyoung Shim 		DRM_ERROR("mixer window[%d] is wrong\n", win);
817d8408326SSeung-Woo Kim 		return;
818d8408326SSeung-Woo Kim 	}
819d8408326SSeung-Woo Kim 
820d8408326SSeung-Woo Kim 	win_data = &mixer_ctx->win_data[win];
821d8408326SSeung-Woo Kim 
822d8408326SSeung-Woo Kim 	win_data->dma_addr = overlay->dma_addr[0];
823d8408326SSeung-Woo Kim 	win_data->vaddr = overlay->vaddr[0];
824d8408326SSeung-Woo Kim 	win_data->chroma_dma_addr = overlay->dma_addr[1];
825d8408326SSeung-Woo Kim 	win_data->chroma_vaddr = overlay->vaddr[1];
826d8408326SSeung-Woo Kim 	win_data->pixel_format = overlay->pixel_format;
827d8408326SSeung-Woo Kim 	win_data->bpp = overlay->bpp;
828d8408326SSeung-Woo Kim 
829d8408326SSeung-Woo Kim 	win_data->crtc_x = overlay->crtc_x;
830d8408326SSeung-Woo Kim 	win_data->crtc_y = overlay->crtc_y;
831d8408326SSeung-Woo Kim 	win_data->crtc_width = overlay->crtc_width;
832d8408326SSeung-Woo Kim 	win_data->crtc_height = overlay->crtc_height;
833d8408326SSeung-Woo Kim 
834d8408326SSeung-Woo Kim 	win_data->fb_x = overlay->fb_x;
835d8408326SSeung-Woo Kim 	win_data->fb_y = overlay->fb_y;
836d8408326SSeung-Woo Kim 	win_data->fb_width = overlay->fb_width;
837d8408326SSeung-Woo Kim 	win_data->fb_height = overlay->fb_height;
8388dcb96b6SSeung-Woo Kim 	win_data->src_width = overlay->src_width;
8398dcb96b6SSeung-Woo Kim 	win_data->src_height = overlay->src_height;
840d8408326SSeung-Woo Kim 
841d8408326SSeung-Woo Kim 	win_data->mode_width = overlay->mode_width;
842d8408326SSeung-Woo Kim 	win_data->mode_height = overlay->mode_height;
843d8408326SSeung-Woo Kim 
844d8408326SSeung-Woo Kim 	win_data->scan_flags = overlay->scan_flag;
845d8408326SSeung-Woo Kim }
846d8408326SSeung-Woo Kim 
847cf8fc4f1SJoonyoung Shim static void mixer_win_commit(void *ctx, int win)
848d8408326SSeung-Woo Kim {
849d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
850d8408326SSeung-Woo Kim 
851d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
852d8408326SSeung-Woo Kim 
8531b8e5747SRahul Sharma 	if (win > 1 && mixer_ctx->vp_enabled)
854d8408326SSeung-Woo Kim 		vp_video_buffer(mixer_ctx, win);
855d8408326SSeung-Woo Kim 	else
856d8408326SSeung-Woo Kim 		mixer_graph_buffer(mixer_ctx, win);
857d8408326SSeung-Woo Kim }
858d8408326SSeung-Woo Kim 
859cf8fc4f1SJoonyoung Shim static void mixer_win_disable(void *ctx, int win)
860d8408326SSeung-Woo Kim {
861d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
862d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
863d8408326SSeung-Woo Kim 	unsigned long flags;
864d8408326SSeung-Woo Kim 
865d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
866d8408326SSeung-Woo Kim 
867d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
868d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, false);
869d8408326SSeung-Woo Kim 
870d8408326SSeung-Woo Kim 	mixer_cfg_layer(mixer_ctx, win, false);
871d8408326SSeung-Woo Kim 
872d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, true);
873d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
874d8408326SSeung-Woo Kim }
875d8408326SSeung-Woo Kim 
876*8137a2e2SPrathyush K static void mixer_wait_for_vblank(void *ctx)
877*8137a2e2SPrathyush K {
878*8137a2e2SPrathyush K 	struct mixer_context *mixer_ctx = ctx;
879*8137a2e2SPrathyush K 	struct mixer_resources *res = &mixer_ctx->mixer_res;
880*8137a2e2SPrathyush K 	int ret;
881*8137a2e2SPrathyush K 
882*8137a2e2SPrathyush K 	ret = wait_for((mixer_reg_read(res, MXR_INT_STATUS) &
883*8137a2e2SPrathyush K 				MXR_INT_STATUS_VSYNC), 50);
884*8137a2e2SPrathyush K 	if (ret < 0)
885*8137a2e2SPrathyush K 		DRM_DEBUG_KMS("vblank wait timed out.\n");
886*8137a2e2SPrathyush K }
887*8137a2e2SPrathyush K 
888578b6065SJoonyoung Shim static struct exynos_mixer_ops mixer_ops = {
889578b6065SJoonyoung Shim 	/* manager */
8901055b39fSInki Dae 	.iommu_on		= mixer_iommu_on,
891d8408326SSeung-Woo Kim 	.enable_vblank		= mixer_enable_vblank,
892d8408326SSeung-Woo Kim 	.disable_vblank		= mixer_disable_vblank,
893*8137a2e2SPrathyush K 	.wait_for_vblank	= mixer_wait_for_vblank,
894cf8fc4f1SJoonyoung Shim 	.dpms			= mixer_dpms,
895578b6065SJoonyoung Shim 
896578b6065SJoonyoung Shim 	/* overlay */
897d8408326SSeung-Woo Kim 	.win_mode_set		= mixer_win_mode_set,
898d8408326SSeung-Woo Kim 	.win_commit		= mixer_win_commit,
899d8408326SSeung-Woo Kim 	.win_disable		= mixer_win_disable,
900d8408326SSeung-Woo Kim };
901d8408326SSeung-Woo Kim 
902d8408326SSeung-Woo Kim /* for pageflip event */
903d8408326SSeung-Woo Kim static void mixer_finish_pageflip(struct drm_device *drm_dev, int crtc)
904d8408326SSeung-Woo Kim {
905d8408326SSeung-Woo Kim 	struct exynos_drm_private *dev_priv = drm_dev->dev_private;
906d8408326SSeung-Woo Kim 	struct drm_pending_vblank_event *e, *t;
907d8408326SSeung-Woo Kim 	struct timeval now;
908d8408326SSeung-Woo Kim 	unsigned long flags;
909d8408326SSeung-Woo Kim 
910d8408326SSeung-Woo Kim 	spin_lock_irqsave(&drm_dev->event_lock, flags);
911d8408326SSeung-Woo Kim 
912d8408326SSeung-Woo Kim 	list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
913d8408326SSeung-Woo Kim 			base.link) {
914d8408326SSeung-Woo Kim 		/* if event's pipe isn't same as crtc then ignore it. */
915d8408326SSeung-Woo Kim 		if (crtc != e->pipe)
916d8408326SSeung-Woo Kim 			continue;
917d8408326SSeung-Woo Kim 
918d8408326SSeung-Woo Kim 		do_gettimeofday(&now);
919d8408326SSeung-Woo Kim 		e->event.sequence = 0;
920d8408326SSeung-Woo Kim 		e->event.tv_sec = now.tv_sec;
921d8408326SSeung-Woo Kim 		e->event.tv_usec = now.tv_usec;
922d8408326SSeung-Woo Kim 
923d8408326SSeung-Woo Kim 		list_move_tail(&e->base.link, &e->base.file_priv->event_list);
924d8408326SSeung-Woo Kim 		wake_up_interruptible(&e->base.file_priv->event_wait);
925d8408326SSeung-Woo Kim 		drm_vblank_put(drm_dev, crtc);
926e1f48ee5SImre Deak 	}
927d8408326SSeung-Woo Kim 
928d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&drm_dev->event_lock, flags);
929d8408326SSeung-Woo Kim }
930d8408326SSeung-Woo Kim 
931d8408326SSeung-Woo Kim static irqreturn_t mixer_irq_handler(int irq, void *arg)
932d8408326SSeung-Woo Kim {
933d8408326SSeung-Woo Kim 	struct exynos_drm_hdmi_context *drm_hdmi_ctx = arg;
934f9309d1bSJoonyoung Shim 	struct mixer_context *ctx = drm_hdmi_ctx->ctx;
935d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
9368379e482SSeung-Woo Kim 	u32 val, base, shadow;
937d8408326SSeung-Woo Kim 
938d8408326SSeung-Woo Kim 	spin_lock(&res->reg_slock);
939d8408326SSeung-Woo Kim 
940d8408326SSeung-Woo Kim 	/* read interrupt status for handling and clearing flags for VSYNC */
941d8408326SSeung-Woo Kim 	val = mixer_reg_read(res, MXR_INT_STATUS);
942d8408326SSeung-Woo Kim 
943d8408326SSeung-Woo Kim 	/* handling VSYNC */
944d8408326SSeung-Woo Kim 	if (val & MXR_INT_STATUS_VSYNC) {
945d8408326SSeung-Woo Kim 		/* interlace scan need to check shadow register */
946d8408326SSeung-Woo Kim 		if (ctx->interlace) {
9478379e482SSeung-Woo Kim 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
9488379e482SSeung-Woo Kim 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
9498379e482SSeung-Woo Kim 			if (base != shadow)
950d8408326SSeung-Woo Kim 				goto out;
951d8408326SSeung-Woo Kim 
9528379e482SSeung-Woo Kim 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
9538379e482SSeung-Woo Kim 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
9548379e482SSeung-Woo Kim 			if (base != shadow)
955d8408326SSeung-Woo Kim 				goto out;
956d8408326SSeung-Woo Kim 		}
957d8408326SSeung-Woo Kim 
958d8408326SSeung-Woo Kim 		drm_handle_vblank(drm_hdmi_ctx->drm_dev, ctx->pipe);
959d8408326SSeung-Woo Kim 		mixer_finish_pageflip(drm_hdmi_ctx->drm_dev, ctx->pipe);
960d8408326SSeung-Woo Kim 	}
961d8408326SSeung-Woo Kim 
962d8408326SSeung-Woo Kim out:
963d8408326SSeung-Woo Kim 	/* clear interrupts */
964d8408326SSeung-Woo Kim 	if (~val & MXR_INT_EN_VSYNC) {
965d8408326SSeung-Woo Kim 		/* vsync interrupt use different bit for read and clear */
966d8408326SSeung-Woo Kim 		val &= ~MXR_INT_EN_VSYNC;
967d8408326SSeung-Woo Kim 		val |= MXR_INT_CLEAR_VSYNC;
968d8408326SSeung-Woo Kim 	}
969d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_INT_STATUS, val);
970d8408326SSeung-Woo Kim 
971d8408326SSeung-Woo Kim 	spin_unlock(&res->reg_slock);
972d8408326SSeung-Woo Kim 
973d8408326SSeung-Woo Kim 	return IRQ_HANDLED;
974d8408326SSeung-Woo Kim }
975d8408326SSeung-Woo Kim 
976d8408326SSeung-Woo Kim static int __devinit mixer_resources_init(struct exynos_drm_hdmi_context *ctx,
977d8408326SSeung-Woo Kim 				 struct platform_device *pdev)
978d8408326SSeung-Woo Kim {
979f9309d1bSJoonyoung Shim 	struct mixer_context *mixer_ctx = ctx->ctx;
980d8408326SSeung-Woo Kim 	struct device *dev = &pdev->dev;
981d8408326SSeung-Woo Kim 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
982d8408326SSeung-Woo Kim 	struct resource *res;
983d8408326SSeung-Woo Kim 	int ret;
984d8408326SSeung-Woo Kim 
985d8408326SSeung-Woo Kim 	spin_lock_init(&mixer_res->reg_slock);
986d8408326SSeung-Woo Kim 
98737f50861SSachin Kamat 	mixer_res->mixer = devm_clk_get(dev, "mixer");
988d8408326SSeung-Woo Kim 	if (IS_ERR_OR_NULL(mixer_res->mixer)) {
989d8408326SSeung-Woo Kim 		dev_err(dev, "failed to get clock 'mixer'\n");
99037f50861SSachin Kamat 		return -ENODEV;
991d8408326SSeung-Woo Kim 	}
9921b8e5747SRahul Sharma 
99337f50861SSachin Kamat 	mixer_res->sclk_hdmi = devm_clk_get(dev, "sclk_hdmi");
994d8408326SSeung-Woo Kim 	if (IS_ERR_OR_NULL(mixer_res->sclk_hdmi)) {
995d8408326SSeung-Woo Kim 		dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
99637f50861SSachin Kamat 		return -ENODEV;
997d8408326SSeung-Woo Kim 	}
9981b8e5747SRahul Sharma 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
999d8408326SSeung-Woo Kim 	if (res == NULL) {
1000d8408326SSeung-Woo Kim 		dev_err(dev, "get memory resource failed.\n");
100137f50861SSachin Kamat 		return -ENXIO;
1002d8408326SSeung-Woo Kim 	}
1003d8408326SSeung-Woo Kim 
10049416dfa7SSachin Kamat 	mixer_res->mixer_regs = devm_ioremap(&pdev->dev, res->start,
10059416dfa7SSachin Kamat 							resource_size(res));
1006d8408326SSeung-Woo Kim 	if (mixer_res->mixer_regs == NULL) {
1007d8408326SSeung-Woo Kim 		dev_err(dev, "register mapping failed.\n");
100837f50861SSachin Kamat 		return -ENXIO;
1009d8408326SSeung-Woo Kim 	}
1010d8408326SSeung-Woo Kim 
10111b8e5747SRahul Sharma 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1012d8408326SSeung-Woo Kim 	if (res == NULL) {
1013d8408326SSeung-Woo Kim 		dev_err(dev, "get interrupt resource failed.\n");
101437f50861SSachin Kamat 		return -ENXIO;
1015d8408326SSeung-Woo Kim 	}
1016d8408326SSeung-Woo Kim 
10179416dfa7SSachin Kamat 	ret = devm_request_irq(&pdev->dev, res->start, mixer_irq_handler,
10189416dfa7SSachin Kamat 							0, "drm_mixer", ctx);
1019d8408326SSeung-Woo Kim 	if (ret) {
1020d8408326SSeung-Woo Kim 		dev_err(dev, "request interrupt failed.\n");
102137f50861SSachin Kamat 		return ret;
1022d8408326SSeung-Woo Kim 	}
1023d8408326SSeung-Woo Kim 	mixer_res->irq = res->start;
1024d8408326SSeung-Woo Kim 
1025d8408326SSeung-Woo Kim 	return 0;
1026d8408326SSeung-Woo Kim }
1027d8408326SSeung-Woo Kim 
10281b8e5747SRahul Sharma static int __devinit vp_resources_init(struct exynos_drm_hdmi_context *ctx,
10291b8e5747SRahul Sharma 				 struct platform_device *pdev)
10301b8e5747SRahul Sharma {
10311b8e5747SRahul Sharma 	struct mixer_context *mixer_ctx = ctx->ctx;
10321b8e5747SRahul Sharma 	struct device *dev = &pdev->dev;
10331b8e5747SRahul Sharma 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
10341b8e5747SRahul Sharma 	struct resource *res;
10351b8e5747SRahul Sharma 
103637f50861SSachin Kamat 	mixer_res->vp = devm_clk_get(dev, "vp");
10371b8e5747SRahul Sharma 	if (IS_ERR_OR_NULL(mixer_res->vp)) {
10381b8e5747SRahul Sharma 		dev_err(dev, "failed to get clock 'vp'\n");
103937f50861SSachin Kamat 		return -ENODEV;
10401b8e5747SRahul Sharma 	}
104137f50861SSachin Kamat 	mixer_res->sclk_mixer = devm_clk_get(dev, "sclk_mixer");
10421b8e5747SRahul Sharma 	if (IS_ERR_OR_NULL(mixer_res->sclk_mixer)) {
10431b8e5747SRahul Sharma 		dev_err(dev, "failed to get clock 'sclk_mixer'\n");
104437f50861SSachin Kamat 		return -ENODEV;
10451b8e5747SRahul Sharma 	}
104637f50861SSachin Kamat 	mixer_res->sclk_dac = devm_clk_get(dev, "sclk_dac");
10471b8e5747SRahul Sharma 	if (IS_ERR_OR_NULL(mixer_res->sclk_dac)) {
10481b8e5747SRahul Sharma 		dev_err(dev, "failed to get clock 'sclk_dac'\n");
104937f50861SSachin Kamat 		return -ENODEV;
10501b8e5747SRahul Sharma 	}
10511b8e5747SRahul Sharma 
10521b8e5747SRahul Sharma 	if (mixer_res->sclk_hdmi)
10531b8e5747SRahul Sharma 		clk_set_parent(mixer_res->sclk_mixer, mixer_res->sclk_hdmi);
10541b8e5747SRahul Sharma 
10551b8e5747SRahul Sharma 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
10561b8e5747SRahul Sharma 	if (res == NULL) {
10571b8e5747SRahul Sharma 		dev_err(dev, "get memory resource failed.\n");
105837f50861SSachin Kamat 		return -ENXIO;
10591b8e5747SRahul Sharma 	}
10601b8e5747SRahul Sharma 
10611b8e5747SRahul Sharma 	mixer_res->vp_regs = devm_ioremap(&pdev->dev, res->start,
10621b8e5747SRahul Sharma 							resource_size(res));
10631b8e5747SRahul Sharma 	if (mixer_res->vp_regs == NULL) {
10641b8e5747SRahul Sharma 		dev_err(dev, "register mapping failed.\n");
106537f50861SSachin Kamat 		return -ENXIO;
10661b8e5747SRahul Sharma 	}
10671b8e5747SRahul Sharma 
10681b8e5747SRahul Sharma 	return 0;
10691b8e5747SRahul Sharma }
10701b8e5747SRahul Sharma 
1071aaf8b49eSRahul Sharma static struct mixer_drv_data exynos5_mxr_drv_data = {
1072aaf8b49eSRahul Sharma 	.version = MXR_VER_16_0_33_0,
1073aaf8b49eSRahul Sharma 	.is_vp_enabled = 0,
1074aaf8b49eSRahul Sharma };
1075aaf8b49eSRahul Sharma 
10761e123441SRahul Sharma static struct mixer_drv_data exynos4_mxr_drv_data = {
10771e123441SRahul Sharma 	.version = MXR_VER_0_0_0_16,
10781b8e5747SRahul Sharma 	.is_vp_enabled = 1,
10791e123441SRahul Sharma };
10801e123441SRahul Sharma 
10811e123441SRahul Sharma static struct platform_device_id mixer_driver_types[] = {
10821e123441SRahul Sharma 	{
10831e123441SRahul Sharma 		.name		= "s5p-mixer",
10841e123441SRahul Sharma 		.driver_data	= (unsigned long)&exynos4_mxr_drv_data,
10851e123441SRahul Sharma 	}, {
1086aaf8b49eSRahul Sharma 		.name		= "exynos5-mixer",
1087aaf8b49eSRahul Sharma 		.driver_data	= (unsigned long)&exynos5_mxr_drv_data,
1088aaf8b49eSRahul Sharma 	}, {
1089aaf8b49eSRahul Sharma 		/* end node */
1090aaf8b49eSRahul Sharma 	}
1091aaf8b49eSRahul Sharma };
1092aaf8b49eSRahul Sharma 
1093aaf8b49eSRahul Sharma static struct of_device_id mixer_match_types[] = {
1094aaf8b49eSRahul Sharma 	{
1095aaf8b49eSRahul Sharma 		.compatible = "samsung,exynos5-mixer",
1096aaf8b49eSRahul Sharma 		.data	= &exynos5_mxr_drv_data,
1097aaf8b49eSRahul Sharma 	}, {
10981e123441SRahul Sharma 		/* end node */
10991e123441SRahul Sharma 	}
11001e123441SRahul Sharma };
11011e123441SRahul Sharma 
1102d8408326SSeung-Woo Kim static int __devinit mixer_probe(struct platform_device *pdev)
1103d8408326SSeung-Woo Kim {
1104d8408326SSeung-Woo Kim 	struct device *dev = &pdev->dev;
1105d8408326SSeung-Woo Kim 	struct exynos_drm_hdmi_context *drm_hdmi_ctx;
1106d8408326SSeung-Woo Kim 	struct mixer_context *ctx;
11071e123441SRahul Sharma 	struct mixer_drv_data *drv;
1108d8408326SSeung-Woo Kim 	int ret;
1109d8408326SSeung-Woo Kim 
1110d8408326SSeung-Woo Kim 	dev_info(dev, "probe start\n");
1111d8408326SSeung-Woo Kim 
11129416dfa7SSachin Kamat 	drm_hdmi_ctx = devm_kzalloc(&pdev->dev, sizeof(*drm_hdmi_ctx),
11139416dfa7SSachin Kamat 								GFP_KERNEL);
1114d8408326SSeung-Woo Kim 	if (!drm_hdmi_ctx) {
1115d8408326SSeung-Woo Kim 		DRM_ERROR("failed to allocate common hdmi context.\n");
1116d8408326SSeung-Woo Kim 		return -ENOMEM;
1117d8408326SSeung-Woo Kim 	}
1118d8408326SSeung-Woo Kim 
11199416dfa7SSachin Kamat 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1120d8408326SSeung-Woo Kim 	if (!ctx) {
1121d8408326SSeung-Woo Kim 		DRM_ERROR("failed to alloc mixer context.\n");
1122d8408326SSeung-Woo Kim 		return -ENOMEM;
1123d8408326SSeung-Woo Kim 	}
1124d8408326SSeung-Woo Kim 
1125cf8fc4f1SJoonyoung Shim 	mutex_init(&ctx->mixer_mutex);
1126cf8fc4f1SJoonyoung Shim 
1127aaf8b49eSRahul Sharma 	if (dev->of_node) {
1128aaf8b49eSRahul Sharma 		const struct of_device_id *match;
1129aaf8b49eSRahul Sharma 		match = of_match_node(of_match_ptr(mixer_match_types),
1130aaf8b49eSRahul Sharma 							  pdev->dev.of_node);
11312cdc53b3SRahul Sharma 		drv = (struct mixer_drv_data *)match->data;
1132aaf8b49eSRahul Sharma 	} else {
1133aaf8b49eSRahul Sharma 		drv = (struct mixer_drv_data *)
1134aaf8b49eSRahul Sharma 			platform_get_device_id(pdev)->driver_data;
1135aaf8b49eSRahul Sharma 	}
1136aaf8b49eSRahul Sharma 
1137cf8fc4f1SJoonyoung Shim 	ctx->dev = &pdev->dev;
11381055b39fSInki Dae 	ctx->parent_ctx = (void *)drm_hdmi_ctx;
1139d8408326SSeung-Woo Kim 	drm_hdmi_ctx->ctx = (void *)ctx;
11401b8e5747SRahul Sharma 	ctx->vp_enabled = drv->is_vp_enabled;
11411e123441SRahul Sharma 	ctx->mxr_ver = drv->version;
1142d8408326SSeung-Woo Kim 
1143d8408326SSeung-Woo Kim 	platform_set_drvdata(pdev, drm_hdmi_ctx);
1144d8408326SSeung-Woo Kim 
1145d8408326SSeung-Woo Kim 	/* acquire resources: regs, irqs, clocks */
1146d8408326SSeung-Woo Kim 	ret = mixer_resources_init(drm_hdmi_ctx, pdev);
11471b8e5747SRahul Sharma 	if (ret) {
11481b8e5747SRahul Sharma 		DRM_ERROR("mixer_resources_init failed\n");
1149d8408326SSeung-Woo Kim 		goto fail;
11501b8e5747SRahul Sharma 	}
11511b8e5747SRahul Sharma 
11521b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
11531b8e5747SRahul Sharma 		/* acquire vp resources: regs, irqs, clocks */
11541b8e5747SRahul Sharma 		ret = vp_resources_init(drm_hdmi_ctx, pdev);
11551b8e5747SRahul Sharma 		if (ret) {
11561b8e5747SRahul Sharma 			DRM_ERROR("vp_resources_init failed\n");
11571b8e5747SRahul Sharma 			goto fail;
11581b8e5747SRahul Sharma 		}
11591b8e5747SRahul Sharma 	}
1160d8408326SSeung-Woo Kim 
1161768c3059SRahul Sharma 	/* attach mixer driver to common hdmi. */
1162768c3059SRahul Sharma 	exynos_mixer_drv_attach(drm_hdmi_ctx);
1163d8408326SSeung-Woo Kim 
1164d8408326SSeung-Woo Kim 	/* register specific callback point to common hdmi. */
1165578b6065SJoonyoung Shim 	exynos_mixer_ops_register(&mixer_ops);
1166d8408326SSeung-Woo Kim 
1167cf8fc4f1SJoonyoung Shim 	pm_runtime_enable(dev);
1168d8408326SSeung-Woo Kim 
1169d8408326SSeung-Woo Kim 	return 0;
1170d8408326SSeung-Woo Kim 
1171d8408326SSeung-Woo Kim 
1172d8408326SSeung-Woo Kim fail:
1173d8408326SSeung-Woo Kim 	dev_info(dev, "probe failed\n");
1174d8408326SSeung-Woo Kim 	return ret;
1175d8408326SSeung-Woo Kim }
1176d8408326SSeung-Woo Kim 
1177d8408326SSeung-Woo Kim static int mixer_remove(struct platform_device *pdev)
1178d8408326SSeung-Woo Kim {
11799416dfa7SSachin Kamat 	dev_info(&pdev->dev, "remove successful\n");
1180d8408326SSeung-Woo Kim 
1181cf8fc4f1SJoonyoung Shim 	pm_runtime_disable(&pdev->dev);
1182cf8fc4f1SJoonyoung Shim 
1183d8408326SSeung-Woo Kim 	return 0;
1184d8408326SSeung-Woo Kim }
1185d8408326SSeung-Woo Kim 
1186ab27af85SJoonyoung Shim #ifdef CONFIG_PM_SLEEP
1187ab27af85SJoonyoung Shim static int mixer_suspend(struct device *dev)
1188ab27af85SJoonyoung Shim {
1189ab27af85SJoonyoung Shim 	struct exynos_drm_hdmi_context *drm_hdmi_ctx = get_mixer_context(dev);
1190ab27af85SJoonyoung Shim 	struct mixer_context *ctx = drm_hdmi_ctx->ctx;
1191ab27af85SJoonyoung Shim 
1192ab27af85SJoonyoung Shim 	mixer_poweroff(ctx);
1193ab27af85SJoonyoung Shim 
1194ab27af85SJoonyoung Shim 	return 0;
1195ab27af85SJoonyoung Shim }
1196ab27af85SJoonyoung Shim #endif
1197ab27af85SJoonyoung Shim 
1198ab27af85SJoonyoung Shim static SIMPLE_DEV_PM_OPS(mixer_pm_ops, mixer_suspend, NULL);
1199ab27af85SJoonyoung Shim 
1200d8408326SSeung-Woo Kim struct platform_driver mixer_driver = {
1201d8408326SSeung-Woo Kim 	.driver = {
1202aaf8b49eSRahul Sharma 		.name = "exynos-mixer",
1203d8408326SSeung-Woo Kim 		.owner = THIS_MODULE,
1204ab27af85SJoonyoung Shim 		.pm = &mixer_pm_ops,
1205aaf8b49eSRahul Sharma 		.of_match_table = mixer_match_types,
1206d8408326SSeung-Woo Kim 	},
1207d8408326SSeung-Woo Kim 	.probe = mixer_probe,
1208d8408326SSeung-Woo Kim 	.remove = __devexit_p(mixer_remove),
12091e123441SRahul Sharma 	.id_table	= mixer_driver_types,
1210d8408326SSeung-Woo Kim };
1211