xref: /linux/drivers/gpu/drm/exynos/exynos_mixer.c (revision 1b8e5747a9b65928a5a126d128847b29128cae34)
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 
17d8408326SSeung-Woo Kim #include "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"
3922b21ae6SJoonyoung Shim 
40d8408326SSeung-Woo Kim #define get_mixer_context(dev)	platform_get_drvdata(to_platform_device(dev))
41d8408326SSeung-Woo Kim 
4222b21ae6SJoonyoung Shim struct hdmi_win_data {
4322b21ae6SJoonyoung Shim 	dma_addr_t		dma_addr;
4422b21ae6SJoonyoung Shim 	void __iomem		*vaddr;
4522b21ae6SJoonyoung Shim 	dma_addr_t		chroma_dma_addr;
4622b21ae6SJoonyoung Shim 	void __iomem		*chroma_vaddr;
4722b21ae6SJoonyoung Shim 	uint32_t		pixel_format;
4822b21ae6SJoonyoung Shim 	unsigned int		bpp;
4922b21ae6SJoonyoung Shim 	unsigned int		crtc_x;
5022b21ae6SJoonyoung Shim 	unsigned int		crtc_y;
5122b21ae6SJoonyoung Shim 	unsigned int		crtc_width;
5222b21ae6SJoonyoung Shim 	unsigned int		crtc_height;
5322b21ae6SJoonyoung Shim 	unsigned int		fb_x;
5422b21ae6SJoonyoung Shim 	unsigned int		fb_y;
5522b21ae6SJoonyoung Shim 	unsigned int		fb_width;
5622b21ae6SJoonyoung Shim 	unsigned int		fb_height;
578dcb96b6SSeung-Woo Kim 	unsigned int		src_width;
588dcb96b6SSeung-Woo Kim 	unsigned int		src_height;
5922b21ae6SJoonyoung Shim 	unsigned int		mode_width;
6022b21ae6SJoonyoung Shim 	unsigned int		mode_height;
6122b21ae6SJoonyoung Shim 	unsigned int		scan_flags;
6222b21ae6SJoonyoung Shim };
6322b21ae6SJoonyoung Shim 
6422b21ae6SJoonyoung Shim struct mixer_resources {
6522b21ae6SJoonyoung Shim 	int			irq;
6622b21ae6SJoonyoung Shim 	void __iomem		*mixer_regs;
6722b21ae6SJoonyoung Shim 	void __iomem		*vp_regs;
6822b21ae6SJoonyoung Shim 	spinlock_t		reg_slock;
6922b21ae6SJoonyoung Shim 	struct clk		*mixer;
7022b21ae6SJoonyoung Shim 	struct clk		*vp;
7122b21ae6SJoonyoung Shim 	struct clk		*sclk_mixer;
7222b21ae6SJoonyoung Shim 	struct clk		*sclk_hdmi;
7322b21ae6SJoonyoung Shim 	struct clk		*sclk_dac;
7422b21ae6SJoonyoung Shim };
7522b21ae6SJoonyoung Shim 
761e123441SRahul Sharma enum mixer_version_id {
771e123441SRahul Sharma 	MXR_VER_0_0_0_16,
781e123441SRahul Sharma 	MXR_VER_16_0_33_0,
791e123441SRahul Sharma };
801e123441SRahul Sharma 
8122b21ae6SJoonyoung Shim struct mixer_context {
82cf8fc4f1SJoonyoung Shim 	struct device		*dev;
8322b21ae6SJoonyoung Shim 	int			pipe;
8422b21ae6SJoonyoung Shim 	bool			interlace;
85cf8fc4f1SJoonyoung Shim 	bool			powered;
86*1b8e5747SRahul Sharma 	bool			vp_enabled;
87cf8fc4f1SJoonyoung Shim 	u32			int_en;
8822b21ae6SJoonyoung Shim 
89cf8fc4f1SJoonyoung Shim 	struct mutex		mixer_mutex;
9022b21ae6SJoonyoung Shim 	struct mixer_resources	mixer_res;
91a634dd54SJoonyoung Shim 	struct hdmi_win_data	win_data[MIXER_WIN_NR];
921e123441SRahul Sharma 	enum mixer_version_id	mxr_ver;
931e123441SRahul Sharma };
941e123441SRahul Sharma 
951e123441SRahul Sharma struct mixer_drv_data {
961e123441SRahul Sharma 	enum mixer_version_id	version;
97*1b8e5747SRahul Sharma 	bool					is_vp_enabled;
9822b21ae6SJoonyoung Shim };
9922b21ae6SJoonyoung Shim 
100d8408326SSeung-Woo Kim static const u8 filter_y_horiz_tap8[] = {
101d8408326SSeung-Woo Kim 	0,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
102d8408326SSeung-Woo Kim 	-1,	-1,	-1,	-1,	-1,	0,	0,	0,
103d8408326SSeung-Woo Kim 	0,	2,	4,	5,	6,	6,	6,	6,
104d8408326SSeung-Woo Kim 	6,	5,	5,	4,	3,	2,	1,	1,
105d8408326SSeung-Woo Kim 	0,	-6,	-12,	-16,	-18,	-20,	-21,	-20,
106d8408326SSeung-Woo Kim 	-20,	-18,	-16,	-13,	-10,	-8,	-5,	-2,
107d8408326SSeung-Woo Kim 	127,	126,	125,	121,	114,	107,	99,	89,
108d8408326SSeung-Woo Kim 	79,	68,	57,	46,	35,	25,	16,	8,
109d8408326SSeung-Woo Kim };
110d8408326SSeung-Woo Kim 
111d8408326SSeung-Woo Kim static const u8 filter_y_vert_tap4[] = {
112d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
113d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
114d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
115d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
116d8408326SSeung-Woo Kim 	0,	5,	11,	19,	27,	37,	48,	59,
117d8408326SSeung-Woo Kim 	70,	81,	92,	102,	111,	118,	124,	126,
118d8408326SSeung-Woo Kim 	0,	0,	-1,	-1,	-2,	-3,	-4,	-5,
119d8408326SSeung-Woo Kim 	-6,	-7,	-8,	-8,	-8,	-8,	-6,	-3,
120d8408326SSeung-Woo Kim };
121d8408326SSeung-Woo Kim 
122d8408326SSeung-Woo Kim static const u8 filter_cr_horiz_tap4[] = {
123d8408326SSeung-Woo Kim 	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
124d8408326SSeung-Woo Kim 	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
125d8408326SSeung-Woo Kim 	127,	126,	124,	118,	111,	102,	92,	81,
126d8408326SSeung-Woo Kim 	70,	59,	48,	37,	27,	19,	11,	5,
127d8408326SSeung-Woo Kim };
128d8408326SSeung-Woo Kim 
129d8408326SSeung-Woo Kim static inline u32 vp_reg_read(struct mixer_resources *res, u32 reg_id)
130d8408326SSeung-Woo Kim {
131d8408326SSeung-Woo Kim 	return readl(res->vp_regs + reg_id);
132d8408326SSeung-Woo Kim }
133d8408326SSeung-Woo Kim 
134d8408326SSeung-Woo Kim static inline void vp_reg_write(struct mixer_resources *res, u32 reg_id,
135d8408326SSeung-Woo Kim 				 u32 val)
136d8408326SSeung-Woo Kim {
137d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
138d8408326SSeung-Woo Kim }
139d8408326SSeung-Woo Kim 
140d8408326SSeung-Woo Kim static inline void vp_reg_writemask(struct mixer_resources *res, u32 reg_id,
141d8408326SSeung-Woo Kim 				 u32 val, u32 mask)
142d8408326SSeung-Woo Kim {
143d8408326SSeung-Woo Kim 	u32 old = vp_reg_read(res, reg_id);
144d8408326SSeung-Woo Kim 
145d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
146d8408326SSeung-Woo Kim 	writel(val, res->vp_regs + reg_id);
147d8408326SSeung-Woo Kim }
148d8408326SSeung-Woo Kim 
149d8408326SSeung-Woo Kim static inline u32 mixer_reg_read(struct mixer_resources *res, u32 reg_id)
150d8408326SSeung-Woo Kim {
151d8408326SSeung-Woo Kim 	return readl(res->mixer_regs + reg_id);
152d8408326SSeung-Woo Kim }
153d8408326SSeung-Woo Kim 
154d8408326SSeung-Woo Kim static inline void mixer_reg_write(struct mixer_resources *res, u32 reg_id,
155d8408326SSeung-Woo Kim 				 u32 val)
156d8408326SSeung-Woo Kim {
157d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
158d8408326SSeung-Woo Kim }
159d8408326SSeung-Woo Kim 
160d8408326SSeung-Woo Kim static inline void mixer_reg_writemask(struct mixer_resources *res,
161d8408326SSeung-Woo Kim 				 u32 reg_id, u32 val, u32 mask)
162d8408326SSeung-Woo Kim {
163d8408326SSeung-Woo Kim 	u32 old = mixer_reg_read(res, reg_id);
164d8408326SSeung-Woo Kim 
165d8408326SSeung-Woo Kim 	val = (val & mask) | (old & ~mask);
166d8408326SSeung-Woo Kim 	writel(val, res->mixer_regs + reg_id);
167d8408326SSeung-Woo Kim }
168d8408326SSeung-Woo Kim 
169d8408326SSeung-Woo Kim static void mixer_regs_dump(struct mixer_context *ctx)
170d8408326SSeung-Woo Kim {
171d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
172d8408326SSeung-Woo Kim do { \
173d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
174d8408326SSeung-Woo Kim 		(u32)readl(ctx->mixer_res.mixer_regs + reg_id)); \
175d8408326SSeung-Woo Kim } while (0)
176d8408326SSeung-Woo Kim 
177d8408326SSeung-Woo Kim 	DUMPREG(MXR_STATUS);
178d8408326SSeung-Woo Kim 	DUMPREG(MXR_CFG);
179d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_EN);
180d8408326SSeung-Woo Kim 	DUMPREG(MXR_INT_STATUS);
181d8408326SSeung-Woo Kim 
182d8408326SSeung-Woo Kim 	DUMPREG(MXR_LAYER_CFG);
183d8408326SSeung-Woo Kim 	DUMPREG(MXR_VIDEO_CFG);
184d8408326SSeung-Woo Kim 
185d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_CFG);
186d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_BASE);
187d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SPAN);
188d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_WH);
189d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_SXY);
190d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC0_DXY);
191d8408326SSeung-Woo Kim 
192d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_CFG);
193d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_BASE);
194d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SPAN);
195d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_WH);
196d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_SXY);
197d8408326SSeung-Woo Kim 	DUMPREG(MXR_GRAPHIC1_DXY);
198d8408326SSeung-Woo Kim #undef DUMPREG
199d8408326SSeung-Woo Kim }
200d8408326SSeung-Woo Kim 
201d8408326SSeung-Woo Kim static void vp_regs_dump(struct mixer_context *ctx)
202d8408326SSeung-Woo Kim {
203d8408326SSeung-Woo Kim #define DUMPREG(reg_id) \
204d8408326SSeung-Woo Kim do { \
205d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS(#reg_id " = %08x\n", \
206d8408326SSeung-Woo Kim 		(u32) readl(ctx->mixer_res.vp_regs + reg_id)); \
207d8408326SSeung-Woo Kim } while (0)
208d8408326SSeung-Woo Kim 
209d8408326SSeung-Woo Kim 	DUMPREG(VP_ENABLE);
210d8408326SSeung-Woo Kim 	DUMPREG(VP_SRESET);
211d8408326SSeung-Woo Kim 	DUMPREG(VP_SHADOW_UPDATE);
212d8408326SSeung-Woo Kim 	DUMPREG(VP_FIELD_ID);
213d8408326SSeung-Woo Kim 	DUMPREG(VP_MODE);
214d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_Y);
215d8408326SSeung-Woo Kim 	DUMPREG(VP_IMG_SIZE_C);
216d8408326SSeung-Woo Kim 	DUMPREG(VP_PER_RATE_CTRL);
217d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_Y_PTR);
218d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_Y_PTR);
219d8408326SSeung-Woo Kim 	DUMPREG(VP_TOP_C_PTR);
220d8408326SSeung-Woo Kim 	DUMPREG(VP_BOT_C_PTR);
221d8408326SSeung-Woo Kim 	DUMPREG(VP_ENDIAN_MODE);
222d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_H_POSITION);
223d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_V_POSITION);
224d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_WIDTH);
225d8408326SSeung-Woo Kim 	DUMPREG(VP_SRC_HEIGHT);
226d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_H_POSITION);
227d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_V_POSITION);
228d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_WIDTH);
229d8408326SSeung-Woo Kim 	DUMPREG(VP_DST_HEIGHT);
230d8408326SSeung-Woo Kim 	DUMPREG(VP_H_RATIO);
231d8408326SSeung-Woo Kim 	DUMPREG(VP_V_RATIO);
232d8408326SSeung-Woo Kim 
233d8408326SSeung-Woo Kim #undef DUMPREG
234d8408326SSeung-Woo Kim }
235d8408326SSeung-Woo Kim 
236d8408326SSeung-Woo Kim static inline void vp_filter_set(struct mixer_resources *res,
237d8408326SSeung-Woo Kim 		int reg_id, const u8 *data, unsigned int size)
238d8408326SSeung-Woo Kim {
239d8408326SSeung-Woo Kim 	/* assure 4-byte align */
240d8408326SSeung-Woo Kim 	BUG_ON(size & 3);
241d8408326SSeung-Woo Kim 	for (; size; size -= 4, reg_id += 4, data += 4) {
242d8408326SSeung-Woo Kim 		u32 val = (data[0] << 24) |  (data[1] << 16) |
243d8408326SSeung-Woo Kim 			(data[2] << 8) | data[3];
244d8408326SSeung-Woo Kim 		vp_reg_write(res, reg_id, val);
245d8408326SSeung-Woo Kim 	}
246d8408326SSeung-Woo Kim }
247d8408326SSeung-Woo Kim 
248d8408326SSeung-Woo Kim static void vp_default_filter(struct mixer_resources *res)
249d8408326SSeung-Woo Kim {
250d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY8_Y0_LL,
251e25e1b66SSachin Kamat 		filter_y_horiz_tap8, sizeof(filter_y_horiz_tap8));
252d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_Y0_LL,
253e25e1b66SSachin Kamat 		filter_y_vert_tap4, sizeof(filter_y_vert_tap4));
254d8408326SSeung-Woo Kim 	vp_filter_set(res, VP_POLY4_C0_LL,
255e25e1b66SSachin Kamat 		filter_cr_horiz_tap4, sizeof(filter_cr_horiz_tap4));
256d8408326SSeung-Woo Kim }
257d8408326SSeung-Woo Kim 
258d8408326SSeung-Woo Kim static void mixer_vsync_set_update(struct mixer_context *ctx, bool enable)
259d8408326SSeung-Woo Kim {
260d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
261d8408326SSeung-Woo Kim 
262d8408326SSeung-Woo Kim 	/* block update on vsync */
263d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, enable ?
264d8408326SSeung-Woo Kim 			MXR_STATUS_SYNC_ENABLE : 0, MXR_STATUS_SYNC_ENABLE);
265d8408326SSeung-Woo Kim 
266*1b8e5747SRahul Sharma 	if (ctx->vp_enabled)
267d8408326SSeung-Woo Kim 		vp_reg_write(res, VP_SHADOW_UPDATE, enable ?
268d8408326SSeung-Woo Kim 			VP_SHADOW_UPDATE_ENABLE : 0);
269d8408326SSeung-Woo Kim }
270d8408326SSeung-Woo Kim 
271d8408326SSeung-Woo Kim static void mixer_cfg_scan(struct mixer_context *ctx, unsigned int height)
272d8408326SSeung-Woo Kim {
273d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
274d8408326SSeung-Woo Kim 	u32 val;
275d8408326SSeung-Woo Kim 
276d8408326SSeung-Woo Kim 	/* choosing between interlace and progressive mode */
277d8408326SSeung-Woo Kim 	val = (ctx->interlace ? MXR_CFG_SCAN_INTERLACE :
278d8408326SSeung-Woo Kim 				MXR_CFG_SCAN_PROGRASSIVE);
279d8408326SSeung-Woo Kim 
280d8408326SSeung-Woo Kim 	/* choosing between porper HD and SD mode */
281d8408326SSeung-Woo Kim 	if (height == 480)
282d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD;
283d8408326SSeung-Woo Kim 	else if (height == 576)
284d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD;
285d8408326SSeung-Woo Kim 	else if (height == 720)
286d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
287d8408326SSeung-Woo Kim 	else if (height == 1080)
288d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_HD_1080 | MXR_CFG_SCAN_HD;
289d8408326SSeung-Woo Kim 	else
290d8408326SSeung-Woo Kim 		val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
291d8408326SSeung-Woo Kim 
292d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_SCAN_MASK);
293d8408326SSeung-Woo Kim }
294d8408326SSeung-Woo Kim 
295d8408326SSeung-Woo Kim static void mixer_cfg_rgb_fmt(struct mixer_context *ctx, unsigned int height)
296d8408326SSeung-Woo Kim {
297d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
298d8408326SSeung-Woo Kim 	u32 val;
299d8408326SSeung-Woo Kim 
300d8408326SSeung-Woo Kim 	if (height == 480) {
301d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
302d8408326SSeung-Woo Kim 	} else if (height == 576) {
303d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB601_0_255;
304d8408326SSeung-Woo Kim 	} else if (height == 720) {
305d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
306d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
307d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
308d8408326SSeung-Woo Kim 				(32 << 0));
309d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
310d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
311d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
312d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
313d8408326SSeung-Woo Kim 	} else if (height == 1080) {
314d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
315d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
316d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
317d8408326SSeung-Woo Kim 				(32 << 0));
318d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
319d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
320d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
321d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
322d8408326SSeung-Woo Kim 	} else {
323d8408326SSeung-Woo Kim 		val = MXR_CFG_RGB709_16_235;
324d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_Y,
325d8408326SSeung-Woo Kim 				(1 << 30) | (94 << 20) | (314 << 10) |
326d8408326SSeung-Woo Kim 				(32 << 0));
327d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CB,
328d8408326SSeung-Woo Kim 				(972 << 20) | (851 << 10) | (225 << 0));
329d8408326SSeung-Woo Kim 		mixer_reg_write(res, MXR_CM_COEFF_CR,
330d8408326SSeung-Woo Kim 				(225 << 20) | (820 << 10) | (1004 << 0));
331d8408326SSeung-Woo Kim 	}
332d8408326SSeung-Woo Kim 
333d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
334d8408326SSeung-Woo Kim }
335d8408326SSeung-Woo Kim 
336d8408326SSeung-Woo Kim static void mixer_cfg_layer(struct mixer_context *ctx, int win, bool enable)
337d8408326SSeung-Woo Kim {
338d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
339d8408326SSeung-Woo Kim 	u32 val = enable ? ~0 : 0;
340d8408326SSeung-Woo Kim 
341d8408326SSeung-Woo Kim 	switch (win) {
342d8408326SSeung-Woo Kim 	case 0:
343d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
344d8408326SSeung-Woo Kim 		break;
345d8408326SSeung-Woo Kim 	case 1:
346d8408326SSeung-Woo Kim 		mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
347d8408326SSeung-Woo Kim 		break;
348d8408326SSeung-Woo Kim 	case 2:
349*1b8e5747SRahul Sharma 		if (ctx->vp_enabled) {
350d8408326SSeung-Woo Kim 			vp_reg_writemask(res, VP_ENABLE, val, VP_ENABLE_ON);
351*1b8e5747SRahul Sharma 			mixer_reg_writemask(res, MXR_CFG, val,
352*1b8e5747SRahul Sharma 				MXR_CFG_VP_ENABLE);
353*1b8e5747SRahul Sharma 		}
354d8408326SSeung-Woo Kim 		break;
355d8408326SSeung-Woo Kim 	}
356d8408326SSeung-Woo Kim }
357d8408326SSeung-Woo Kim 
358d8408326SSeung-Woo Kim static void mixer_run(struct mixer_context *ctx)
359d8408326SSeung-Woo Kim {
360d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
361d8408326SSeung-Woo Kim 
362d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
363d8408326SSeung-Woo Kim 
364d8408326SSeung-Woo Kim 	mixer_regs_dump(ctx);
365d8408326SSeung-Woo Kim }
366d8408326SSeung-Woo Kim 
367d8408326SSeung-Woo Kim static void vp_video_buffer(struct mixer_context *ctx, int win)
368d8408326SSeung-Woo Kim {
369d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
370d8408326SSeung-Woo Kim 	unsigned long flags;
371d8408326SSeung-Woo Kim 	struct hdmi_win_data *win_data;
372d8408326SSeung-Woo Kim 	unsigned int x_ratio, y_ratio;
373d8408326SSeung-Woo Kim 	unsigned int buf_num;
374d8408326SSeung-Woo Kim 	dma_addr_t luma_addr[2], chroma_addr[2];
375d8408326SSeung-Woo Kim 	bool tiled_mode = false;
376d8408326SSeung-Woo Kim 	bool crcb_mode = false;
377d8408326SSeung-Woo Kim 	u32 val;
378d8408326SSeung-Woo Kim 
379d8408326SSeung-Woo Kim 	win_data = &ctx->win_data[win];
380d8408326SSeung-Woo Kim 
381d8408326SSeung-Woo Kim 	switch (win_data->pixel_format) {
382d8408326SSeung-Woo Kim 	case DRM_FORMAT_NV12MT:
383d8408326SSeung-Woo Kim 		tiled_mode = true;
384363b06aaSVille Syrjälä 	case DRM_FORMAT_NV12:
385d8408326SSeung-Woo Kim 		crcb_mode = false;
386d8408326SSeung-Woo Kim 		buf_num = 2;
387d8408326SSeung-Woo Kim 		break;
388d8408326SSeung-Woo Kim 	/* TODO: single buffer format NV12, NV21 */
389d8408326SSeung-Woo Kim 	default:
390d8408326SSeung-Woo Kim 		/* ignore pixel format at disable time */
391d8408326SSeung-Woo Kim 		if (!win_data->dma_addr)
392d8408326SSeung-Woo Kim 			break;
393d8408326SSeung-Woo Kim 
394d8408326SSeung-Woo Kim 		DRM_ERROR("pixel format for vp is wrong [%d].\n",
395d8408326SSeung-Woo Kim 				win_data->pixel_format);
396d8408326SSeung-Woo Kim 		return;
397d8408326SSeung-Woo Kim 	}
398d8408326SSeung-Woo Kim 
399d8408326SSeung-Woo Kim 	/* scaling feature: (src << 16) / dst */
4008dcb96b6SSeung-Woo Kim 	x_ratio = (win_data->src_width << 16) / win_data->crtc_width;
4018dcb96b6SSeung-Woo Kim 	y_ratio = (win_data->src_height << 16) / win_data->crtc_height;
402d8408326SSeung-Woo Kim 
403d8408326SSeung-Woo Kim 	if (buf_num == 2) {
404d8408326SSeung-Woo Kim 		luma_addr[0] = win_data->dma_addr;
405d8408326SSeung-Woo Kim 		chroma_addr[0] = win_data->chroma_dma_addr;
406d8408326SSeung-Woo Kim 	} else {
407d8408326SSeung-Woo Kim 		luma_addr[0] = win_data->dma_addr;
408d8408326SSeung-Woo Kim 		chroma_addr[0] = win_data->dma_addr
4098dcb96b6SSeung-Woo Kim 			+ (win_data->fb_width * win_data->fb_height);
410d8408326SSeung-Woo Kim 	}
411d8408326SSeung-Woo Kim 
412d8408326SSeung-Woo Kim 	if (win_data->scan_flags & DRM_MODE_FLAG_INTERLACE) {
413d8408326SSeung-Woo Kim 		ctx->interlace = true;
414d8408326SSeung-Woo Kim 		if (tiled_mode) {
415d8408326SSeung-Woo Kim 			luma_addr[1] = luma_addr[0] + 0x40;
416d8408326SSeung-Woo Kim 			chroma_addr[1] = chroma_addr[0] + 0x40;
417d8408326SSeung-Woo Kim 		} else {
4188dcb96b6SSeung-Woo Kim 			luma_addr[1] = luma_addr[0] + win_data->fb_width;
4198dcb96b6SSeung-Woo Kim 			chroma_addr[1] = chroma_addr[0] + win_data->fb_width;
420d8408326SSeung-Woo Kim 		}
421d8408326SSeung-Woo Kim 	} else {
422d8408326SSeung-Woo Kim 		ctx->interlace = false;
423d8408326SSeung-Woo Kim 		luma_addr[1] = 0;
424d8408326SSeung-Woo Kim 		chroma_addr[1] = 0;
425d8408326SSeung-Woo Kim 	}
426d8408326SSeung-Woo Kim 
427d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
428d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, false);
429d8408326SSeung-Woo Kim 
430d8408326SSeung-Woo Kim 	/* interlace or progressive scan mode */
431d8408326SSeung-Woo Kim 	val = (ctx->interlace ? ~0 : 0);
432d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_LINE_SKIP);
433d8408326SSeung-Woo Kim 
434d8408326SSeung-Woo Kim 	/* setup format */
435d8408326SSeung-Woo Kim 	val = (crcb_mode ? VP_MODE_NV21 : VP_MODE_NV12);
436d8408326SSeung-Woo Kim 	val |= (tiled_mode ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
437d8408326SSeung-Woo Kim 	vp_reg_writemask(res, VP_MODE, val, VP_MODE_FMT_MASK);
438d8408326SSeung-Woo Kim 
439d8408326SSeung-Woo Kim 	/* setting size of input image */
4408dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_IMG_SIZE_Y, VP_IMG_HSIZE(win_data->fb_width) |
4418dcb96b6SSeung-Woo Kim 		VP_IMG_VSIZE(win_data->fb_height));
442d8408326SSeung-Woo Kim 	/* chroma height has to reduced by 2 to avoid chroma distorions */
4438dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_IMG_SIZE_C, VP_IMG_HSIZE(win_data->fb_width) |
4448dcb96b6SSeung-Woo Kim 		VP_IMG_VSIZE(win_data->fb_height / 2));
445d8408326SSeung-Woo Kim 
4468dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_WIDTH, win_data->src_width);
4478dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_HEIGHT, win_data->src_height);
448d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_H_POSITION,
4498dcb96b6SSeung-Woo Kim 			VP_SRC_H_POSITION_VAL(win_data->fb_x));
4508dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_SRC_V_POSITION, win_data->fb_y);
451d8408326SSeung-Woo Kim 
4528dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_DST_WIDTH, win_data->crtc_width);
4538dcb96b6SSeung-Woo Kim 	vp_reg_write(res, VP_DST_H_POSITION, win_data->crtc_x);
454d8408326SSeung-Woo Kim 	if (ctx->interlace) {
4558dcb96b6SSeung-Woo Kim 		vp_reg_write(res, VP_DST_HEIGHT, win_data->crtc_height / 2);
4568dcb96b6SSeung-Woo Kim 		vp_reg_write(res, VP_DST_V_POSITION, win_data->crtc_y / 2);
457d8408326SSeung-Woo Kim 	} else {
4588dcb96b6SSeung-Woo Kim 		vp_reg_write(res, VP_DST_HEIGHT, win_data->crtc_height);
4598dcb96b6SSeung-Woo Kim 		vp_reg_write(res, VP_DST_V_POSITION, win_data->crtc_y);
460d8408326SSeung-Woo Kim 	}
461d8408326SSeung-Woo Kim 
462d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_H_RATIO, x_ratio);
463d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_V_RATIO, y_ratio);
464d8408326SSeung-Woo Kim 
465d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
466d8408326SSeung-Woo Kim 
467d8408326SSeung-Woo Kim 	/* set buffer address to vp */
468d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_Y_PTR, luma_addr[0]);
469d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_Y_PTR, luma_addr[1]);
470d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_TOP_C_PTR, chroma_addr[0]);
471d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_BOT_C_PTR, chroma_addr[1]);
472d8408326SSeung-Woo Kim 
4738dcb96b6SSeung-Woo Kim 	mixer_cfg_scan(ctx, win_data->mode_height);
4748dcb96b6SSeung-Woo Kim 	mixer_cfg_rgb_fmt(ctx, win_data->mode_height);
475d8408326SSeung-Woo Kim 	mixer_cfg_layer(ctx, win, true);
476d8408326SSeung-Woo Kim 	mixer_run(ctx);
477d8408326SSeung-Woo Kim 
478d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, true);
479d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
480d8408326SSeung-Woo Kim 
481d8408326SSeung-Woo Kim 	vp_regs_dump(ctx);
482d8408326SSeung-Woo Kim }
483d8408326SSeung-Woo Kim 
484d8408326SSeung-Woo Kim static void mixer_graph_buffer(struct mixer_context *ctx, int win)
485d8408326SSeung-Woo Kim {
486d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
487d8408326SSeung-Woo Kim 	unsigned long flags;
488d8408326SSeung-Woo Kim 	struct hdmi_win_data *win_data;
489d8408326SSeung-Woo Kim 	unsigned int x_ratio, y_ratio;
490d8408326SSeung-Woo Kim 	unsigned int src_x_offset, src_y_offset, dst_x_offset, dst_y_offset;
491d8408326SSeung-Woo Kim 	dma_addr_t dma_addr;
492d8408326SSeung-Woo Kim 	unsigned int fmt;
493d8408326SSeung-Woo Kim 	u32 val;
494d8408326SSeung-Woo Kim 
495d8408326SSeung-Woo Kim 	win_data = &ctx->win_data[win];
496d8408326SSeung-Woo Kim 
497d8408326SSeung-Woo Kim 	#define RGB565 4
498d8408326SSeung-Woo Kim 	#define ARGB1555 5
499d8408326SSeung-Woo Kim 	#define ARGB4444 6
500d8408326SSeung-Woo Kim 	#define ARGB8888 7
501d8408326SSeung-Woo Kim 
502d8408326SSeung-Woo Kim 	switch (win_data->bpp) {
503d8408326SSeung-Woo Kim 	case 16:
504d8408326SSeung-Woo Kim 		fmt = ARGB4444;
505d8408326SSeung-Woo Kim 		break;
506d8408326SSeung-Woo Kim 	case 32:
507d8408326SSeung-Woo Kim 		fmt = ARGB8888;
508d8408326SSeung-Woo Kim 		break;
509d8408326SSeung-Woo Kim 	default:
510d8408326SSeung-Woo Kim 		fmt = ARGB8888;
511d8408326SSeung-Woo Kim 	}
512d8408326SSeung-Woo Kim 
513d8408326SSeung-Woo Kim 	/* 2x scaling feature */
514d8408326SSeung-Woo Kim 	x_ratio = 0;
515d8408326SSeung-Woo Kim 	y_ratio = 0;
516d8408326SSeung-Woo Kim 
517d8408326SSeung-Woo Kim 	dst_x_offset = win_data->crtc_x;
518d8408326SSeung-Woo Kim 	dst_y_offset = win_data->crtc_y;
519d8408326SSeung-Woo Kim 
520d8408326SSeung-Woo Kim 	/* converting dma address base and source offset */
5218dcb96b6SSeung-Woo Kim 	dma_addr = win_data->dma_addr
5228dcb96b6SSeung-Woo Kim 		+ (win_data->fb_x * win_data->bpp >> 3)
5238dcb96b6SSeung-Woo Kim 		+ (win_data->fb_y * win_data->fb_width * win_data->bpp >> 3);
524d8408326SSeung-Woo Kim 	src_x_offset = 0;
525d8408326SSeung-Woo Kim 	src_y_offset = 0;
526d8408326SSeung-Woo Kim 
527d8408326SSeung-Woo Kim 	if (win_data->scan_flags & DRM_MODE_FLAG_INTERLACE)
528d8408326SSeung-Woo Kim 		ctx->interlace = true;
529d8408326SSeung-Woo Kim 	else
530d8408326SSeung-Woo Kim 		ctx->interlace = false;
531d8408326SSeung-Woo Kim 
532d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
533d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, false);
534d8408326SSeung-Woo Kim 
535d8408326SSeung-Woo Kim 	/* setup format */
536d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_GRAPHIC_CFG(win),
537d8408326SSeung-Woo Kim 		MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
538d8408326SSeung-Woo Kim 
539d8408326SSeung-Woo Kim 	/* setup geometry */
5408dcb96b6SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_SPAN(win), win_data->fb_width);
541d8408326SSeung-Woo Kim 
5428dcb96b6SSeung-Woo Kim 	val  = MXR_GRP_WH_WIDTH(win_data->crtc_width);
5438dcb96b6SSeung-Woo Kim 	val |= MXR_GRP_WH_HEIGHT(win_data->crtc_height);
544d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_H_SCALE(x_ratio);
545d8408326SSeung-Woo Kim 	val |= MXR_GRP_WH_V_SCALE(y_ratio);
546d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_WH(win), val);
547d8408326SSeung-Woo Kim 
548d8408326SSeung-Woo Kim 	/* setup offsets in source image */
549d8408326SSeung-Woo Kim 	val  = MXR_GRP_SXY_SX(src_x_offset);
550d8408326SSeung-Woo Kim 	val |= MXR_GRP_SXY_SY(src_y_offset);
551d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_SXY(win), val);
552d8408326SSeung-Woo Kim 
553d8408326SSeung-Woo Kim 	/* setup offsets in display image */
554d8408326SSeung-Woo Kim 	val  = MXR_GRP_DXY_DX(dst_x_offset);
555d8408326SSeung-Woo Kim 	val |= MXR_GRP_DXY_DY(dst_y_offset);
556d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_DXY(win), val);
557d8408326SSeung-Woo Kim 
558d8408326SSeung-Woo Kim 	/* set buffer address to mixer */
559d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_GRAPHIC_BASE(win), dma_addr);
560d8408326SSeung-Woo Kim 
5618dcb96b6SSeung-Woo Kim 	mixer_cfg_scan(ctx, win_data->mode_height);
5628dcb96b6SSeung-Woo Kim 	mixer_cfg_rgb_fmt(ctx, win_data->mode_height);
563d8408326SSeung-Woo Kim 	mixer_cfg_layer(ctx, win, true);
564d8408326SSeung-Woo Kim 	mixer_run(ctx);
565d8408326SSeung-Woo Kim 
566d8408326SSeung-Woo Kim 	mixer_vsync_set_update(ctx, true);
567d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
568d8408326SSeung-Woo Kim }
569d8408326SSeung-Woo Kim 
570d8408326SSeung-Woo Kim static void vp_win_reset(struct mixer_context *ctx)
571d8408326SSeung-Woo Kim {
572d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
573d8408326SSeung-Woo Kim 	int tries = 100;
574d8408326SSeung-Woo Kim 
575d8408326SSeung-Woo Kim 	vp_reg_write(res, VP_SRESET, VP_SRESET_PROCESSING);
576d8408326SSeung-Woo Kim 	for (tries = 100; tries; --tries) {
577d8408326SSeung-Woo Kim 		/* waiting until VP_SRESET_PROCESSING is 0 */
578d8408326SSeung-Woo Kim 		if (~vp_reg_read(res, VP_SRESET) & VP_SRESET_PROCESSING)
579d8408326SSeung-Woo Kim 			break;
580d8408326SSeung-Woo Kim 		mdelay(10);
581d8408326SSeung-Woo Kim 	}
582d8408326SSeung-Woo Kim 	WARN(tries == 0, "failed to reset Video Processor\n");
583d8408326SSeung-Woo Kim }
584d8408326SSeung-Woo Kim 
585cf8fc4f1SJoonyoung Shim static void mixer_win_reset(struct mixer_context *ctx)
586cf8fc4f1SJoonyoung Shim {
587cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
588cf8fc4f1SJoonyoung Shim 	unsigned long flags;
589cf8fc4f1SJoonyoung Shim 	u32 val; /* value stored to register */
590cf8fc4f1SJoonyoung Shim 
591cf8fc4f1SJoonyoung Shim 	spin_lock_irqsave(&res->reg_slock, flags);
592cf8fc4f1SJoonyoung Shim 	mixer_vsync_set_update(ctx, false);
593cf8fc4f1SJoonyoung Shim 
594cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
595cf8fc4f1SJoonyoung Shim 
596cf8fc4f1SJoonyoung Shim 	/* set output in RGB888 mode */
597cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
598cf8fc4f1SJoonyoung Shim 
599cf8fc4f1SJoonyoung Shim 	/* 16 beat burst in DMA */
600cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_STATUS, MXR_STATUS_16_BURST,
601cf8fc4f1SJoonyoung Shim 		MXR_STATUS_BURST_MASK);
602cf8fc4f1SJoonyoung Shim 
603cf8fc4f1SJoonyoung Shim 	/* setting default layer priority: layer1 > layer0 > video
604cf8fc4f1SJoonyoung Shim 	 * because typical usage scenario would be
605cf8fc4f1SJoonyoung Shim 	 * layer1 - OSD
606cf8fc4f1SJoonyoung Shim 	 * layer0 - framebuffer
607cf8fc4f1SJoonyoung Shim 	 * video - video overlay
608cf8fc4f1SJoonyoung Shim 	 */
609cf8fc4f1SJoonyoung Shim 	val = MXR_LAYER_CFG_GRP1_VAL(3);
610cf8fc4f1SJoonyoung Shim 	val |= MXR_LAYER_CFG_GRP0_VAL(2);
611*1b8e5747SRahul Sharma 	if (ctx->vp_enabled)
612cf8fc4f1SJoonyoung Shim 		val |= MXR_LAYER_CFG_VP_VAL(1);
613cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_LAYER_CFG, val);
614cf8fc4f1SJoonyoung Shim 
615cf8fc4f1SJoonyoung Shim 	/* setting background color */
616cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR0, 0x008080);
617cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR1, 0x008080);
618cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_BG_COLOR2, 0x008080);
619cf8fc4f1SJoonyoung Shim 
620cf8fc4f1SJoonyoung Shim 	/* setting graphical layers */
621cf8fc4f1SJoonyoung Shim 	val  = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
622cf8fc4f1SJoonyoung Shim 	val |= MXR_GRP_CFG_WIN_BLEND_EN;
6235736603bSSeung-Woo Kim 	val |= MXR_GRP_CFG_BLEND_PRE_MUL;
6245736603bSSeung-Woo Kim 	val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
625cf8fc4f1SJoonyoung Shim 	val |= MXR_GRP_CFG_ALPHA_VAL(0xff); /* non-transparent alpha */
626cf8fc4f1SJoonyoung Shim 
627cf8fc4f1SJoonyoung Shim 	/* the same configuration for both layers */
628cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_GRAPHIC_CFG(0), val);
629cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_GRAPHIC_CFG(1), val);
630cf8fc4f1SJoonyoung Shim 
6315736603bSSeung-Woo Kim 	/* setting video layers */
6325736603bSSeung-Woo Kim 	val = MXR_GRP_CFG_ALPHA_VAL(0);
6335736603bSSeung-Woo Kim 	mixer_reg_write(res, MXR_VIDEO_CFG, val);
6345736603bSSeung-Woo Kim 
635*1b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
636cf8fc4f1SJoonyoung Shim 		/* configuration of Video Processor Registers */
637cf8fc4f1SJoonyoung Shim 		vp_win_reset(ctx);
638cf8fc4f1SJoonyoung Shim 		vp_default_filter(res);
639*1b8e5747SRahul Sharma 	}
640cf8fc4f1SJoonyoung Shim 
641cf8fc4f1SJoonyoung Shim 	/* disable all layers */
642cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
643cf8fc4f1SJoonyoung Shim 	mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
644*1b8e5747SRahul Sharma 	if (ctx->vp_enabled)
645cf8fc4f1SJoonyoung Shim 		mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
646cf8fc4f1SJoonyoung Shim 
647cf8fc4f1SJoonyoung Shim 	mixer_vsync_set_update(ctx, true);
648cf8fc4f1SJoonyoung Shim 	spin_unlock_irqrestore(&res->reg_slock, flags);
649cf8fc4f1SJoonyoung Shim }
650cf8fc4f1SJoonyoung Shim 
651cf8fc4f1SJoonyoung Shim static void mixer_poweron(struct mixer_context *ctx)
652cf8fc4f1SJoonyoung Shim {
653cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
654cf8fc4f1SJoonyoung Shim 
655cf8fc4f1SJoonyoung Shim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
656cf8fc4f1SJoonyoung Shim 
657cf8fc4f1SJoonyoung Shim 	mutex_lock(&ctx->mixer_mutex);
658cf8fc4f1SJoonyoung Shim 	if (ctx->powered) {
659cf8fc4f1SJoonyoung Shim 		mutex_unlock(&ctx->mixer_mutex);
660cf8fc4f1SJoonyoung Shim 		return;
661cf8fc4f1SJoonyoung Shim 	}
662cf8fc4f1SJoonyoung Shim 	ctx->powered = true;
663cf8fc4f1SJoonyoung Shim 	mutex_unlock(&ctx->mixer_mutex);
664cf8fc4f1SJoonyoung Shim 
665cf8fc4f1SJoonyoung Shim 	pm_runtime_get_sync(ctx->dev);
666cf8fc4f1SJoonyoung Shim 
667cf8fc4f1SJoonyoung Shim 	clk_enable(res->mixer);
668*1b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
669cf8fc4f1SJoonyoung Shim 		clk_enable(res->vp);
670cf8fc4f1SJoonyoung Shim 		clk_enable(res->sclk_mixer);
671*1b8e5747SRahul Sharma 	}
672cf8fc4f1SJoonyoung Shim 
673cf8fc4f1SJoonyoung Shim 	mixer_reg_write(res, MXR_INT_EN, ctx->int_en);
674cf8fc4f1SJoonyoung Shim 	mixer_win_reset(ctx);
675cf8fc4f1SJoonyoung Shim }
676cf8fc4f1SJoonyoung Shim 
677cf8fc4f1SJoonyoung Shim static void mixer_poweroff(struct mixer_context *ctx)
678cf8fc4f1SJoonyoung Shim {
679cf8fc4f1SJoonyoung Shim 	struct mixer_resources *res = &ctx->mixer_res;
680cf8fc4f1SJoonyoung Shim 
681cf8fc4f1SJoonyoung Shim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
682cf8fc4f1SJoonyoung Shim 
683cf8fc4f1SJoonyoung Shim 	mutex_lock(&ctx->mixer_mutex);
684cf8fc4f1SJoonyoung Shim 	if (!ctx->powered)
685cf8fc4f1SJoonyoung Shim 		goto out;
686cf8fc4f1SJoonyoung Shim 	mutex_unlock(&ctx->mixer_mutex);
687cf8fc4f1SJoonyoung Shim 
688cf8fc4f1SJoonyoung Shim 	ctx->int_en = mixer_reg_read(res, MXR_INT_EN);
689cf8fc4f1SJoonyoung Shim 
690cf8fc4f1SJoonyoung Shim 	clk_disable(res->mixer);
691*1b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
692cf8fc4f1SJoonyoung Shim 		clk_disable(res->vp);
693cf8fc4f1SJoonyoung Shim 		clk_disable(res->sclk_mixer);
694*1b8e5747SRahul Sharma 	}
695cf8fc4f1SJoonyoung Shim 
696cf8fc4f1SJoonyoung Shim 	pm_runtime_put_sync(ctx->dev);
697cf8fc4f1SJoonyoung Shim 
698cf8fc4f1SJoonyoung Shim 	mutex_lock(&ctx->mixer_mutex);
699cf8fc4f1SJoonyoung Shim 	ctx->powered = false;
700cf8fc4f1SJoonyoung Shim 
701cf8fc4f1SJoonyoung Shim out:
702cf8fc4f1SJoonyoung Shim 	mutex_unlock(&ctx->mixer_mutex);
703cf8fc4f1SJoonyoung Shim }
704cf8fc4f1SJoonyoung Shim 
705d8408326SSeung-Woo Kim static int mixer_enable_vblank(void *ctx, int pipe)
706d8408326SSeung-Woo Kim {
707d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
708d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
709d8408326SSeung-Woo Kim 
710d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
711d8408326SSeung-Woo Kim 
712d8408326SSeung-Woo Kim 	mixer_ctx->pipe = pipe;
713d8408326SSeung-Woo Kim 
714d8408326SSeung-Woo Kim 	/* enable vsync interrupt */
715d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_INT_EN, MXR_INT_EN_VSYNC,
716d8408326SSeung-Woo Kim 			MXR_INT_EN_VSYNC);
717d8408326SSeung-Woo Kim 
718d8408326SSeung-Woo Kim 	return 0;
719d8408326SSeung-Woo Kim }
720d8408326SSeung-Woo Kim 
721d8408326SSeung-Woo Kim static void mixer_disable_vblank(void *ctx)
722d8408326SSeung-Woo Kim {
723d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
724d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
725d8408326SSeung-Woo Kim 
726d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
727d8408326SSeung-Woo Kim 
728d8408326SSeung-Woo Kim 	/* disable vsync interrupt */
729d8408326SSeung-Woo Kim 	mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
730d8408326SSeung-Woo Kim }
731d8408326SSeung-Woo Kim 
732cf8fc4f1SJoonyoung Shim static void mixer_dpms(void *ctx, int mode)
733cf8fc4f1SJoonyoung Shim {
734cf8fc4f1SJoonyoung Shim 	struct mixer_context *mixer_ctx = ctx;
735cf8fc4f1SJoonyoung Shim 
736cf8fc4f1SJoonyoung Shim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
737cf8fc4f1SJoonyoung Shim 
738cf8fc4f1SJoonyoung Shim 	switch (mode) {
739cf8fc4f1SJoonyoung Shim 	case DRM_MODE_DPMS_ON:
740cf8fc4f1SJoonyoung Shim 		mixer_poweron(mixer_ctx);
741cf8fc4f1SJoonyoung Shim 		break;
742cf8fc4f1SJoonyoung Shim 	case DRM_MODE_DPMS_STANDBY:
743cf8fc4f1SJoonyoung Shim 	case DRM_MODE_DPMS_SUSPEND:
744cf8fc4f1SJoonyoung Shim 	case DRM_MODE_DPMS_OFF:
745cf8fc4f1SJoonyoung Shim 		mixer_poweroff(mixer_ctx);
746cf8fc4f1SJoonyoung Shim 		break;
747cf8fc4f1SJoonyoung Shim 	default:
748cf8fc4f1SJoonyoung Shim 		DRM_DEBUG_KMS("unknown dpms mode: %d\n", mode);
749cf8fc4f1SJoonyoung Shim 		break;
750cf8fc4f1SJoonyoung Shim 	}
751cf8fc4f1SJoonyoung Shim }
752cf8fc4f1SJoonyoung Shim 
7533d05859fSInki Dae static void mixer_wait_for_vblank(void *ctx)
7543d05859fSInki Dae {
7553d05859fSInki Dae 	struct mixer_context *mixer_ctx = ctx;
7563d05859fSInki Dae 	struct mixer_resources *res = &mixer_ctx->mixer_res;
7573d05859fSInki Dae 	int ret;
7583d05859fSInki Dae 
7593d05859fSInki Dae 	ret = wait_for((mixer_reg_read(res, MXR_INT_STATUS) &
7603d05859fSInki Dae 				MXR_INT_STATUS_VSYNC), 50);
7613d05859fSInki Dae 	if (ret < 0)
7623d05859fSInki Dae 		DRM_DEBUG_KMS("vblank wait timed out.\n");
7633d05859fSInki Dae }
7643d05859fSInki Dae 
765d8408326SSeung-Woo Kim static void mixer_win_mode_set(void *ctx,
766d8408326SSeung-Woo Kim 			      struct exynos_drm_overlay *overlay)
767d8408326SSeung-Woo Kim {
768d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
769d8408326SSeung-Woo Kim 	struct hdmi_win_data *win_data;
770d8408326SSeung-Woo Kim 	int win;
771d8408326SSeung-Woo Kim 
772d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
773d8408326SSeung-Woo Kim 
774d8408326SSeung-Woo Kim 	if (!overlay) {
775d8408326SSeung-Woo Kim 		DRM_ERROR("overlay is NULL\n");
776d8408326SSeung-Woo Kim 		return;
777d8408326SSeung-Woo Kim 	}
778d8408326SSeung-Woo Kim 
779d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("set [%d]x[%d] at (%d,%d) to [%d]x[%d] at (%d,%d)\n",
780d8408326SSeung-Woo Kim 				 overlay->fb_width, overlay->fb_height,
781d8408326SSeung-Woo Kim 				 overlay->fb_x, overlay->fb_y,
782d8408326SSeung-Woo Kim 				 overlay->crtc_width, overlay->crtc_height,
783d8408326SSeung-Woo Kim 				 overlay->crtc_x, overlay->crtc_y);
784d8408326SSeung-Woo Kim 
785d8408326SSeung-Woo Kim 	win = overlay->zpos;
786d8408326SSeung-Woo Kim 	if (win == DEFAULT_ZPOS)
787a2ee151bSJoonyoung Shim 		win = MIXER_DEFAULT_WIN;
788d8408326SSeung-Woo Kim 
789a634dd54SJoonyoung Shim 	if (win < 0 || win > MIXER_WIN_NR) {
790cf8fc4f1SJoonyoung Shim 		DRM_ERROR("mixer window[%d] is wrong\n", win);
791d8408326SSeung-Woo Kim 		return;
792d8408326SSeung-Woo Kim 	}
793d8408326SSeung-Woo Kim 
794d8408326SSeung-Woo Kim 	win_data = &mixer_ctx->win_data[win];
795d8408326SSeung-Woo Kim 
796d8408326SSeung-Woo Kim 	win_data->dma_addr = overlay->dma_addr[0];
797d8408326SSeung-Woo Kim 	win_data->vaddr = overlay->vaddr[0];
798d8408326SSeung-Woo Kim 	win_data->chroma_dma_addr = overlay->dma_addr[1];
799d8408326SSeung-Woo Kim 	win_data->chroma_vaddr = overlay->vaddr[1];
800d8408326SSeung-Woo Kim 	win_data->pixel_format = overlay->pixel_format;
801d8408326SSeung-Woo Kim 	win_data->bpp = overlay->bpp;
802d8408326SSeung-Woo Kim 
803d8408326SSeung-Woo Kim 	win_data->crtc_x = overlay->crtc_x;
804d8408326SSeung-Woo Kim 	win_data->crtc_y = overlay->crtc_y;
805d8408326SSeung-Woo Kim 	win_data->crtc_width = overlay->crtc_width;
806d8408326SSeung-Woo Kim 	win_data->crtc_height = overlay->crtc_height;
807d8408326SSeung-Woo Kim 
808d8408326SSeung-Woo Kim 	win_data->fb_x = overlay->fb_x;
809d8408326SSeung-Woo Kim 	win_data->fb_y = overlay->fb_y;
810d8408326SSeung-Woo Kim 	win_data->fb_width = overlay->fb_width;
811d8408326SSeung-Woo Kim 	win_data->fb_height = overlay->fb_height;
8128dcb96b6SSeung-Woo Kim 	win_data->src_width = overlay->src_width;
8138dcb96b6SSeung-Woo Kim 	win_data->src_height = overlay->src_height;
814d8408326SSeung-Woo Kim 
815d8408326SSeung-Woo Kim 	win_data->mode_width = overlay->mode_width;
816d8408326SSeung-Woo Kim 	win_data->mode_height = overlay->mode_height;
817d8408326SSeung-Woo Kim 
818d8408326SSeung-Woo Kim 	win_data->scan_flags = overlay->scan_flag;
819d8408326SSeung-Woo Kim }
820d8408326SSeung-Woo Kim 
821cf8fc4f1SJoonyoung Shim static void mixer_win_commit(void *ctx, int win)
822d8408326SSeung-Woo Kim {
823d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
824d8408326SSeung-Woo Kim 
825d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
826d8408326SSeung-Woo Kim 
827*1b8e5747SRahul Sharma 	if (win > 1 && mixer_ctx->vp_enabled)
828d8408326SSeung-Woo Kim 		vp_video_buffer(mixer_ctx, win);
829d8408326SSeung-Woo Kim 	else
830d8408326SSeung-Woo Kim 		mixer_graph_buffer(mixer_ctx, win);
831d8408326SSeung-Woo Kim }
832d8408326SSeung-Woo Kim 
833cf8fc4f1SJoonyoung Shim static void mixer_win_disable(void *ctx, int win)
834d8408326SSeung-Woo Kim {
835d8408326SSeung-Woo Kim 	struct mixer_context *mixer_ctx = ctx;
836d8408326SSeung-Woo Kim 	struct mixer_resources *res = &mixer_ctx->mixer_res;
837d8408326SSeung-Woo Kim 	unsigned long flags;
838d8408326SSeung-Woo Kim 
839d8408326SSeung-Woo Kim 	DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
840d8408326SSeung-Woo Kim 
841d8408326SSeung-Woo Kim 	spin_lock_irqsave(&res->reg_slock, flags);
842d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, false);
843d8408326SSeung-Woo Kim 
844d8408326SSeung-Woo Kim 	mixer_cfg_layer(mixer_ctx, win, false);
845d8408326SSeung-Woo Kim 
846d8408326SSeung-Woo Kim 	mixer_vsync_set_update(mixer_ctx, true);
847d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&res->reg_slock, flags);
848d8408326SSeung-Woo Kim }
849d8408326SSeung-Woo Kim 
850578b6065SJoonyoung Shim static struct exynos_mixer_ops mixer_ops = {
851578b6065SJoonyoung Shim 	/* manager */
852d8408326SSeung-Woo Kim 	.enable_vblank		= mixer_enable_vblank,
853d8408326SSeung-Woo Kim 	.disable_vblank		= mixer_disable_vblank,
854cf8fc4f1SJoonyoung Shim 	.dpms			= mixer_dpms,
855578b6065SJoonyoung Shim 
856578b6065SJoonyoung Shim 	/* overlay */
8573d05859fSInki Dae 	.wait_for_vblank	= mixer_wait_for_vblank,
858d8408326SSeung-Woo Kim 	.win_mode_set		= mixer_win_mode_set,
859d8408326SSeung-Woo Kim 	.win_commit		= mixer_win_commit,
860d8408326SSeung-Woo Kim 	.win_disable		= mixer_win_disable,
861d8408326SSeung-Woo Kim };
862d8408326SSeung-Woo Kim 
863d8408326SSeung-Woo Kim /* for pageflip event */
864d8408326SSeung-Woo Kim static void mixer_finish_pageflip(struct drm_device *drm_dev, int crtc)
865d8408326SSeung-Woo Kim {
866d8408326SSeung-Woo Kim 	struct exynos_drm_private *dev_priv = drm_dev->dev_private;
867d8408326SSeung-Woo Kim 	struct drm_pending_vblank_event *e, *t;
868d8408326SSeung-Woo Kim 	struct timeval now;
869d8408326SSeung-Woo Kim 	unsigned long flags;
870d8408326SSeung-Woo Kim 	bool is_checked = false;
871d8408326SSeung-Woo Kim 
872d8408326SSeung-Woo Kim 	spin_lock_irqsave(&drm_dev->event_lock, flags);
873d8408326SSeung-Woo Kim 
874d8408326SSeung-Woo Kim 	list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
875d8408326SSeung-Woo Kim 			base.link) {
876d8408326SSeung-Woo Kim 		/* if event's pipe isn't same as crtc then ignore it. */
877d8408326SSeung-Woo Kim 		if (crtc != e->pipe)
878d8408326SSeung-Woo Kim 			continue;
879d8408326SSeung-Woo Kim 
880d8408326SSeung-Woo Kim 		is_checked = true;
881d8408326SSeung-Woo Kim 		do_gettimeofday(&now);
882d8408326SSeung-Woo Kim 		e->event.sequence = 0;
883d8408326SSeung-Woo Kim 		e->event.tv_sec = now.tv_sec;
884d8408326SSeung-Woo Kim 		e->event.tv_usec = now.tv_usec;
885d8408326SSeung-Woo Kim 
886d8408326SSeung-Woo Kim 		list_move_tail(&e->base.link, &e->base.file_priv->event_list);
887d8408326SSeung-Woo Kim 		wake_up_interruptible(&e->base.file_priv->event_wait);
888d8408326SSeung-Woo Kim 	}
889d8408326SSeung-Woo Kim 
890d8408326SSeung-Woo Kim 	if (is_checked)
891c5614ae3SInki Dae 		/*
892c5614ae3SInki Dae 		 * call drm_vblank_put only in case that drm_vblank_get was
893c5614ae3SInki Dae 		 * called.
894c5614ae3SInki Dae 		 */
895c5614ae3SInki Dae 		if (atomic_read(&drm_dev->vblank_refcount[crtc]) > 0)
896d8408326SSeung-Woo Kim 			drm_vblank_put(drm_dev, crtc);
897d8408326SSeung-Woo Kim 
898d8408326SSeung-Woo Kim 	spin_unlock_irqrestore(&drm_dev->event_lock, flags);
899d8408326SSeung-Woo Kim }
900d8408326SSeung-Woo Kim 
901d8408326SSeung-Woo Kim static irqreturn_t mixer_irq_handler(int irq, void *arg)
902d8408326SSeung-Woo Kim {
903d8408326SSeung-Woo Kim 	struct exynos_drm_hdmi_context *drm_hdmi_ctx = arg;
904f9309d1bSJoonyoung Shim 	struct mixer_context *ctx = drm_hdmi_ctx->ctx;
905d8408326SSeung-Woo Kim 	struct mixer_resources *res = &ctx->mixer_res;
9068379e482SSeung-Woo Kim 	u32 val, base, shadow;
907d8408326SSeung-Woo Kim 
908d8408326SSeung-Woo Kim 	spin_lock(&res->reg_slock);
909d8408326SSeung-Woo Kim 
910d8408326SSeung-Woo Kim 	/* read interrupt status for handling and clearing flags for VSYNC */
911d8408326SSeung-Woo Kim 	val = mixer_reg_read(res, MXR_INT_STATUS);
912d8408326SSeung-Woo Kim 
913d8408326SSeung-Woo Kim 	/* handling VSYNC */
914d8408326SSeung-Woo Kim 	if (val & MXR_INT_STATUS_VSYNC) {
915d8408326SSeung-Woo Kim 		/* interlace scan need to check shadow register */
916d8408326SSeung-Woo Kim 		if (ctx->interlace) {
9178379e482SSeung-Woo Kim 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
9188379e482SSeung-Woo Kim 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
9198379e482SSeung-Woo Kim 			if (base != shadow)
920d8408326SSeung-Woo Kim 				goto out;
921d8408326SSeung-Woo Kim 
9228379e482SSeung-Woo Kim 			base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
9238379e482SSeung-Woo Kim 			shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
9248379e482SSeung-Woo Kim 			if (base != shadow)
925d8408326SSeung-Woo Kim 				goto out;
926d8408326SSeung-Woo Kim 		}
927d8408326SSeung-Woo Kim 
928d8408326SSeung-Woo Kim 		drm_handle_vblank(drm_hdmi_ctx->drm_dev, ctx->pipe);
929d8408326SSeung-Woo Kim 		mixer_finish_pageflip(drm_hdmi_ctx->drm_dev, ctx->pipe);
930d8408326SSeung-Woo Kim 	}
931d8408326SSeung-Woo Kim 
932d8408326SSeung-Woo Kim out:
933d8408326SSeung-Woo Kim 	/* clear interrupts */
934d8408326SSeung-Woo Kim 	if (~val & MXR_INT_EN_VSYNC) {
935d8408326SSeung-Woo Kim 		/* vsync interrupt use different bit for read and clear */
936d8408326SSeung-Woo Kim 		val &= ~MXR_INT_EN_VSYNC;
937d8408326SSeung-Woo Kim 		val |= MXR_INT_CLEAR_VSYNC;
938d8408326SSeung-Woo Kim 	}
939d8408326SSeung-Woo Kim 	mixer_reg_write(res, MXR_INT_STATUS, val);
940d8408326SSeung-Woo Kim 
941d8408326SSeung-Woo Kim 	spin_unlock(&res->reg_slock);
942d8408326SSeung-Woo Kim 
943d8408326SSeung-Woo Kim 	return IRQ_HANDLED;
944d8408326SSeung-Woo Kim }
945d8408326SSeung-Woo Kim 
946d8408326SSeung-Woo Kim static int __devinit mixer_resources_init(struct exynos_drm_hdmi_context *ctx,
947d8408326SSeung-Woo Kim 				 struct platform_device *pdev)
948d8408326SSeung-Woo Kim {
949f9309d1bSJoonyoung Shim 	struct mixer_context *mixer_ctx = ctx->ctx;
950d8408326SSeung-Woo Kim 	struct device *dev = &pdev->dev;
951d8408326SSeung-Woo Kim 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
952d8408326SSeung-Woo Kim 	struct resource *res;
953d8408326SSeung-Woo Kim 	int ret;
954d8408326SSeung-Woo Kim 
955d8408326SSeung-Woo Kim 	spin_lock_init(&mixer_res->reg_slock);
956d8408326SSeung-Woo Kim 
957d8408326SSeung-Woo Kim 	mixer_res->mixer = clk_get(dev, "mixer");
958d8408326SSeung-Woo Kim 	if (IS_ERR_OR_NULL(mixer_res->mixer)) {
959d8408326SSeung-Woo Kim 		dev_err(dev, "failed to get clock 'mixer'\n");
960d8408326SSeung-Woo Kim 		ret = -ENODEV;
961d8408326SSeung-Woo Kim 		goto fail;
962d8408326SSeung-Woo Kim 	}
963*1b8e5747SRahul Sharma 
964d8408326SSeung-Woo Kim 	mixer_res->sclk_hdmi = clk_get(dev, "sclk_hdmi");
965d8408326SSeung-Woo Kim 	if (IS_ERR_OR_NULL(mixer_res->sclk_hdmi)) {
966d8408326SSeung-Woo Kim 		dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
967d8408326SSeung-Woo Kim 		ret = -ENODEV;
968d8408326SSeung-Woo Kim 		goto fail;
969d8408326SSeung-Woo Kim 	}
970*1b8e5747SRahul Sharma 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
971d8408326SSeung-Woo Kim 	if (res == NULL) {
972d8408326SSeung-Woo Kim 		dev_err(dev, "get memory resource failed.\n");
973d8408326SSeung-Woo Kim 		ret = -ENXIO;
974d8408326SSeung-Woo Kim 		goto fail;
975d8408326SSeung-Woo Kim 	}
976d8408326SSeung-Woo Kim 
9779416dfa7SSachin Kamat 	mixer_res->mixer_regs = devm_ioremap(&pdev->dev, res->start,
9789416dfa7SSachin Kamat 							resource_size(res));
979d8408326SSeung-Woo Kim 	if (mixer_res->mixer_regs == NULL) {
980d8408326SSeung-Woo Kim 		dev_err(dev, "register mapping failed.\n");
981d8408326SSeung-Woo Kim 		ret = -ENXIO;
982d8408326SSeung-Woo Kim 		goto fail;
983d8408326SSeung-Woo Kim 	}
984d8408326SSeung-Woo Kim 
985*1b8e5747SRahul Sharma 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
986d8408326SSeung-Woo Kim 	if (res == NULL) {
987d8408326SSeung-Woo Kim 		dev_err(dev, "get interrupt resource failed.\n");
988d8408326SSeung-Woo Kim 		ret = -ENXIO;
9899416dfa7SSachin Kamat 		goto fail;
990d8408326SSeung-Woo Kim 	}
991d8408326SSeung-Woo Kim 
9929416dfa7SSachin Kamat 	ret = devm_request_irq(&pdev->dev, res->start, mixer_irq_handler,
9939416dfa7SSachin Kamat 							0, "drm_mixer", ctx);
994d8408326SSeung-Woo Kim 	if (ret) {
995d8408326SSeung-Woo Kim 		dev_err(dev, "request interrupt failed.\n");
9969416dfa7SSachin Kamat 		goto fail;
997d8408326SSeung-Woo Kim 	}
998d8408326SSeung-Woo Kim 	mixer_res->irq = res->start;
999d8408326SSeung-Woo Kim 
1000d8408326SSeung-Woo Kim 	return 0;
1001d8408326SSeung-Woo Kim 
1002d8408326SSeung-Woo Kim fail:
1003d8408326SSeung-Woo Kim 	if (!IS_ERR_OR_NULL(mixer_res->sclk_hdmi))
1004d8408326SSeung-Woo Kim 		clk_put(mixer_res->sclk_hdmi);
1005d8408326SSeung-Woo Kim 	if (!IS_ERR_OR_NULL(mixer_res->mixer))
1006d8408326SSeung-Woo Kim 		clk_put(mixer_res->mixer);
1007d8408326SSeung-Woo Kim 	return ret;
1008d8408326SSeung-Woo Kim }
1009d8408326SSeung-Woo Kim 
1010*1b8e5747SRahul Sharma static int __devinit vp_resources_init(struct exynos_drm_hdmi_context *ctx,
1011*1b8e5747SRahul Sharma 				 struct platform_device *pdev)
1012*1b8e5747SRahul Sharma {
1013*1b8e5747SRahul Sharma 	struct mixer_context *mixer_ctx = ctx->ctx;
1014*1b8e5747SRahul Sharma 	struct device *dev = &pdev->dev;
1015*1b8e5747SRahul Sharma 	struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
1016*1b8e5747SRahul Sharma 	struct resource *res;
1017*1b8e5747SRahul Sharma 	int ret;
1018*1b8e5747SRahul Sharma 
1019*1b8e5747SRahul Sharma 	mixer_res->vp = clk_get(dev, "vp");
1020*1b8e5747SRahul Sharma 	if (IS_ERR_OR_NULL(mixer_res->vp)) {
1021*1b8e5747SRahul Sharma 		dev_err(dev, "failed to get clock 'vp'\n");
1022*1b8e5747SRahul Sharma 		ret = -ENODEV;
1023*1b8e5747SRahul Sharma 		goto fail;
1024*1b8e5747SRahul Sharma 	}
1025*1b8e5747SRahul Sharma 	mixer_res->sclk_mixer = clk_get(dev, "sclk_mixer");
1026*1b8e5747SRahul Sharma 	if (IS_ERR_OR_NULL(mixer_res->sclk_mixer)) {
1027*1b8e5747SRahul Sharma 		dev_err(dev, "failed to get clock 'sclk_mixer'\n");
1028*1b8e5747SRahul Sharma 		ret = -ENODEV;
1029*1b8e5747SRahul Sharma 		goto fail;
1030*1b8e5747SRahul Sharma 	}
1031*1b8e5747SRahul Sharma 	mixer_res->sclk_dac = clk_get(dev, "sclk_dac");
1032*1b8e5747SRahul Sharma 	if (IS_ERR_OR_NULL(mixer_res->sclk_dac)) {
1033*1b8e5747SRahul Sharma 		dev_err(dev, "failed to get clock 'sclk_dac'\n");
1034*1b8e5747SRahul Sharma 		ret = -ENODEV;
1035*1b8e5747SRahul Sharma 		goto fail;
1036*1b8e5747SRahul Sharma 	}
1037*1b8e5747SRahul Sharma 
1038*1b8e5747SRahul Sharma 	if (mixer_res->sclk_hdmi)
1039*1b8e5747SRahul Sharma 		clk_set_parent(mixer_res->sclk_mixer, mixer_res->sclk_hdmi);
1040*1b8e5747SRahul Sharma 
1041*1b8e5747SRahul Sharma 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1042*1b8e5747SRahul Sharma 	if (res == NULL) {
1043*1b8e5747SRahul Sharma 		dev_err(dev, "get memory resource failed.\n");
1044*1b8e5747SRahul Sharma 		ret = -ENXIO;
1045*1b8e5747SRahul Sharma 		goto fail;
1046*1b8e5747SRahul Sharma 	}
1047*1b8e5747SRahul Sharma 
1048*1b8e5747SRahul Sharma 	mixer_res->vp_regs = devm_ioremap(&pdev->dev, res->start,
1049*1b8e5747SRahul Sharma 							resource_size(res));
1050*1b8e5747SRahul Sharma 	if (mixer_res->vp_regs == NULL) {
1051*1b8e5747SRahul Sharma 		dev_err(dev, "register mapping failed.\n");
1052*1b8e5747SRahul Sharma 		ret = -ENXIO;
1053*1b8e5747SRahul Sharma 		goto fail;
1054*1b8e5747SRahul Sharma 	}
1055*1b8e5747SRahul Sharma 
1056*1b8e5747SRahul Sharma 	return 0;
1057*1b8e5747SRahul Sharma 
1058*1b8e5747SRahul Sharma fail:
1059*1b8e5747SRahul Sharma 	if (!IS_ERR_OR_NULL(mixer_res->sclk_dac))
1060*1b8e5747SRahul Sharma 		clk_put(mixer_res->sclk_dac);
1061*1b8e5747SRahul Sharma 	if (!IS_ERR_OR_NULL(mixer_res->sclk_mixer))
1062*1b8e5747SRahul Sharma 		clk_put(mixer_res->sclk_mixer);
1063*1b8e5747SRahul Sharma 	if (!IS_ERR_OR_NULL(mixer_res->vp))
1064*1b8e5747SRahul Sharma 		clk_put(mixer_res->vp);
1065*1b8e5747SRahul Sharma 	return ret;
1066*1b8e5747SRahul Sharma }
1067*1b8e5747SRahul Sharma 
10681e123441SRahul Sharma static struct mixer_drv_data exynos4_mxr_drv_data = {
10691e123441SRahul Sharma 	.version = MXR_VER_0_0_0_16,
1070*1b8e5747SRahul Sharma 	.is_vp_enabled = 1,
10711e123441SRahul Sharma };
10721e123441SRahul Sharma 
10731e123441SRahul Sharma static struct platform_device_id mixer_driver_types[] = {
10741e123441SRahul Sharma 	{
10751e123441SRahul Sharma 		.name		= "s5p-mixer",
10761e123441SRahul Sharma 		.driver_data	= (unsigned long)&exynos4_mxr_drv_data,
10771e123441SRahul Sharma 	}, {
10781e123441SRahul Sharma 		/* end node */
10791e123441SRahul Sharma 	}
10801e123441SRahul Sharma };
10811e123441SRahul Sharma 
1082d8408326SSeung-Woo Kim static int __devinit mixer_probe(struct platform_device *pdev)
1083d8408326SSeung-Woo Kim {
1084d8408326SSeung-Woo Kim 	struct device *dev = &pdev->dev;
1085d8408326SSeung-Woo Kim 	struct exynos_drm_hdmi_context *drm_hdmi_ctx;
1086d8408326SSeung-Woo Kim 	struct mixer_context *ctx;
10871e123441SRahul Sharma 	struct mixer_drv_data *drv;
1088d8408326SSeung-Woo Kim 	int ret;
1089d8408326SSeung-Woo Kim 
1090d8408326SSeung-Woo Kim 	dev_info(dev, "probe start\n");
1091d8408326SSeung-Woo Kim 
10929416dfa7SSachin Kamat 	drm_hdmi_ctx = devm_kzalloc(&pdev->dev, sizeof(*drm_hdmi_ctx),
10939416dfa7SSachin Kamat 								GFP_KERNEL);
1094d8408326SSeung-Woo Kim 	if (!drm_hdmi_ctx) {
1095d8408326SSeung-Woo Kim 		DRM_ERROR("failed to allocate common hdmi context.\n");
1096d8408326SSeung-Woo Kim 		return -ENOMEM;
1097d8408326SSeung-Woo Kim 	}
1098d8408326SSeung-Woo Kim 
10999416dfa7SSachin Kamat 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1100d8408326SSeung-Woo Kim 	if (!ctx) {
1101d8408326SSeung-Woo Kim 		DRM_ERROR("failed to alloc mixer context.\n");
1102d8408326SSeung-Woo Kim 		return -ENOMEM;
1103d8408326SSeung-Woo Kim 	}
1104d8408326SSeung-Woo Kim 
1105cf8fc4f1SJoonyoung Shim 	mutex_init(&ctx->mixer_mutex);
1106cf8fc4f1SJoonyoung Shim 
11071e123441SRahul Sharma 	drv = (struct mixer_drv_data *)platform_get_device_id(
11081e123441SRahul Sharma 			pdev)->driver_data;
1109cf8fc4f1SJoonyoung Shim 	ctx->dev = &pdev->dev;
1110d8408326SSeung-Woo Kim 	drm_hdmi_ctx->ctx = (void *)ctx;
1111*1b8e5747SRahul Sharma 	ctx->vp_enabled = drv->is_vp_enabled;
11121e123441SRahul Sharma 	ctx->mxr_ver = drv->version;
1113d8408326SSeung-Woo Kim 
1114d8408326SSeung-Woo Kim 	platform_set_drvdata(pdev, drm_hdmi_ctx);
1115d8408326SSeung-Woo Kim 
1116d8408326SSeung-Woo Kim 	/* acquire resources: regs, irqs, clocks */
1117d8408326SSeung-Woo Kim 	ret = mixer_resources_init(drm_hdmi_ctx, pdev);
1118*1b8e5747SRahul Sharma 	if (ret) {
1119*1b8e5747SRahul Sharma 		DRM_ERROR("mixer_resources_init failed\n");
1120d8408326SSeung-Woo Kim 		goto fail;
1121*1b8e5747SRahul Sharma 	}
1122*1b8e5747SRahul Sharma 
1123*1b8e5747SRahul Sharma 	if (ctx->vp_enabled) {
1124*1b8e5747SRahul Sharma 		/* acquire vp resources: regs, irqs, clocks */
1125*1b8e5747SRahul Sharma 		ret = vp_resources_init(drm_hdmi_ctx, pdev);
1126*1b8e5747SRahul Sharma 		if (ret) {
1127*1b8e5747SRahul Sharma 			DRM_ERROR("vp_resources_init failed\n");
1128*1b8e5747SRahul Sharma 			goto fail;
1129*1b8e5747SRahul Sharma 		}
1130*1b8e5747SRahul Sharma 	}
1131d8408326SSeung-Woo Kim 
1132d8408326SSeung-Woo Kim 	/* register specific callback point to common hdmi. */
1133578b6065SJoonyoung Shim 	exynos_mixer_ops_register(&mixer_ops);
1134d8408326SSeung-Woo Kim 
1135cf8fc4f1SJoonyoung Shim 	pm_runtime_enable(dev);
1136d8408326SSeung-Woo Kim 
1137d8408326SSeung-Woo Kim 	return 0;
1138d8408326SSeung-Woo Kim 
1139d8408326SSeung-Woo Kim 
1140d8408326SSeung-Woo Kim fail:
1141d8408326SSeung-Woo Kim 	dev_info(dev, "probe failed\n");
1142d8408326SSeung-Woo Kim 	return ret;
1143d8408326SSeung-Woo Kim }
1144d8408326SSeung-Woo Kim 
1145d8408326SSeung-Woo Kim static int mixer_remove(struct platform_device *pdev)
1146d8408326SSeung-Woo Kim {
11479416dfa7SSachin Kamat 	dev_info(&pdev->dev, "remove successful\n");
1148d8408326SSeung-Woo Kim 
1149cf8fc4f1SJoonyoung Shim 	pm_runtime_disable(&pdev->dev);
1150cf8fc4f1SJoonyoung Shim 
1151d8408326SSeung-Woo Kim 	return 0;
1152d8408326SSeung-Woo Kim }
1153d8408326SSeung-Woo Kim 
1154ab27af85SJoonyoung Shim #ifdef CONFIG_PM_SLEEP
1155ab27af85SJoonyoung Shim static int mixer_suspend(struct device *dev)
1156ab27af85SJoonyoung Shim {
1157ab27af85SJoonyoung Shim 	struct exynos_drm_hdmi_context *drm_hdmi_ctx = get_mixer_context(dev);
1158ab27af85SJoonyoung Shim 	struct mixer_context *ctx = drm_hdmi_ctx->ctx;
1159ab27af85SJoonyoung Shim 
1160ab27af85SJoonyoung Shim 	mixer_poweroff(ctx);
1161ab27af85SJoonyoung Shim 
1162ab27af85SJoonyoung Shim 	return 0;
1163ab27af85SJoonyoung Shim }
1164ab27af85SJoonyoung Shim #endif
1165ab27af85SJoonyoung Shim 
1166ab27af85SJoonyoung Shim static SIMPLE_DEV_PM_OPS(mixer_pm_ops, mixer_suspend, NULL);
1167ab27af85SJoonyoung Shim 
1168d8408326SSeung-Woo Kim struct platform_driver mixer_driver = {
1169d8408326SSeung-Woo Kim 	.driver = {
1170d8408326SSeung-Woo Kim 		.name = "s5p-mixer",
1171d8408326SSeung-Woo Kim 		.owner = THIS_MODULE,
1172ab27af85SJoonyoung Shim 		.pm = &mixer_pm_ops,
1173d8408326SSeung-Woo Kim 	},
1174d8408326SSeung-Woo Kim 	.probe = mixer_probe,
1175d8408326SSeung-Woo Kim 	.remove = __devexit_p(mixer_remove),
11761e123441SRahul Sharma 	.id_table	= mixer_driver_types,
1177d8408326SSeung-Woo Kim };
1178