18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2011 Samsung Electronics Co.Ltd
48c2ecf20Sopenharmony_ci * Authors:
58c2ecf20Sopenharmony_ci * Seung-Woo Kim <sw0312.kim@samsung.com>
68c2ecf20Sopenharmony_ci *	Inki Dae <inki.dae@samsung.com>
78c2ecf20Sopenharmony_ci *	Joonyoung Shim <jy0922.shim@samsung.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Based on drivers/media/video/s5p-tv/mixer_reg.c
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/component.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/i2c.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/irq.h>
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci#include <linux/ktime.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/of_device.h>
228c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
238c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
248c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
258c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
268c2ecf20Sopenharmony_ci#include <linux/wait.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <drm/drm_fourcc.h>
298c2ecf20Sopenharmony_ci#include <drm/drm_vblank.h>
308c2ecf20Sopenharmony_ci#include <drm/exynos_drm.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "exynos_drm_crtc.h"
338c2ecf20Sopenharmony_ci#include "exynos_drm_drv.h"
348c2ecf20Sopenharmony_ci#include "exynos_drm_fb.h"
358c2ecf20Sopenharmony_ci#include "exynos_drm_plane.h"
368c2ecf20Sopenharmony_ci#include "regs-mixer.h"
378c2ecf20Sopenharmony_ci#include "regs-vp.h"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define MIXER_WIN_NR		3
408c2ecf20Sopenharmony_ci#define VP_DEFAULT_WIN		2
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/*
438c2ecf20Sopenharmony_ci * Mixer color space conversion coefficient triplet.
448c2ecf20Sopenharmony_ci * Used for CSC from RGB to YCbCr.
458c2ecf20Sopenharmony_ci * Each coefficient is a 10-bit fixed point number with
468c2ecf20Sopenharmony_ci * sign and no integer part, i.e.
478c2ecf20Sopenharmony_ci * [0:8] = fractional part (representing a value y = x / 2^9)
488c2ecf20Sopenharmony_ci * [9] = sign
498c2ecf20Sopenharmony_ci * Negative values are encoded with two's complement.
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci#define MXR_CSC_C(x) ((int)((x) * 512.0) & 0x3ff)
528c2ecf20Sopenharmony_ci#define MXR_CSC_CT(a0, a1, a2) \
538c2ecf20Sopenharmony_ci  ((MXR_CSC_C(a0) << 20) | (MXR_CSC_C(a1) << 10) | (MXR_CSC_C(a2) << 0))
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* YCbCr value, used for mixer background color configuration. */
568c2ecf20Sopenharmony_ci#define MXR_YCBCR_VAL(y, cb, cr) (((y) << 16) | ((cb) << 8) | ((cr) << 0))
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* The pixelformats that are natively supported by the mixer. */
598c2ecf20Sopenharmony_ci#define MXR_FORMAT_RGB565	4
608c2ecf20Sopenharmony_ci#define MXR_FORMAT_ARGB1555	5
618c2ecf20Sopenharmony_ci#define MXR_FORMAT_ARGB4444	6
628c2ecf20Sopenharmony_ci#define MXR_FORMAT_ARGB8888	7
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cienum mixer_version_id {
658c2ecf20Sopenharmony_ci	MXR_VER_0_0_0_16,
668c2ecf20Sopenharmony_ci	MXR_VER_16_0_33_0,
678c2ecf20Sopenharmony_ci	MXR_VER_128_0_0_184,
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cienum mixer_flag_bits {
718c2ecf20Sopenharmony_ci	MXR_BIT_POWERED,
728c2ecf20Sopenharmony_ci	MXR_BIT_VSYNC,
738c2ecf20Sopenharmony_ci	MXR_BIT_INTERLACE,
748c2ecf20Sopenharmony_ci	MXR_BIT_VP_ENABLED,
758c2ecf20Sopenharmony_ci	MXR_BIT_HAS_SCLK,
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic const uint32_t mixer_formats[] = {
798c2ecf20Sopenharmony_ci	DRM_FORMAT_XRGB4444,
808c2ecf20Sopenharmony_ci	DRM_FORMAT_ARGB4444,
818c2ecf20Sopenharmony_ci	DRM_FORMAT_XRGB1555,
828c2ecf20Sopenharmony_ci	DRM_FORMAT_ARGB1555,
838c2ecf20Sopenharmony_ci	DRM_FORMAT_RGB565,
848c2ecf20Sopenharmony_ci	DRM_FORMAT_XRGB8888,
858c2ecf20Sopenharmony_ci	DRM_FORMAT_ARGB8888,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic const uint32_t vp_formats[] = {
898c2ecf20Sopenharmony_ci	DRM_FORMAT_NV12,
908c2ecf20Sopenharmony_ci	DRM_FORMAT_NV21,
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistruct mixer_context {
948c2ecf20Sopenharmony_ci	struct platform_device *pdev;
958c2ecf20Sopenharmony_ci	struct device		*dev;
968c2ecf20Sopenharmony_ci	struct drm_device	*drm_dev;
978c2ecf20Sopenharmony_ci	void			*dma_priv;
988c2ecf20Sopenharmony_ci	struct exynos_drm_crtc	*crtc;
998c2ecf20Sopenharmony_ci	struct exynos_drm_plane	planes[MIXER_WIN_NR];
1008c2ecf20Sopenharmony_ci	unsigned long		flags;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	int			irq;
1038c2ecf20Sopenharmony_ci	void __iomem		*mixer_regs;
1048c2ecf20Sopenharmony_ci	void __iomem		*vp_regs;
1058c2ecf20Sopenharmony_ci	spinlock_t		reg_slock;
1068c2ecf20Sopenharmony_ci	struct clk		*mixer;
1078c2ecf20Sopenharmony_ci	struct clk		*vp;
1088c2ecf20Sopenharmony_ci	struct clk		*hdmi;
1098c2ecf20Sopenharmony_ci	struct clk		*sclk_mixer;
1108c2ecf20Sopenharmony_ci	struct clk		*sclk_hdmi;
1118c2ecf20Sopenharmony_ci	struct clk		*mout_mixer;
1128c2ecf20Sopenharmony_ci	enum mixer_version_id	mxr_ver;
1138c2ecf20Sopenharmony_ci	int			scan_value;
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistruct mixer_drv_data {
1178c2ecf20Sopenharmony_ci	enum mixer_version_id	version;
1188c2ecf20Sopenharmony_ci	bool					is_vp_enabled;
1198c2ecf20Sopenharmony_ci	bool					has_sclk;
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const struct exynos_drm_plane_config plane_configs[MIXER_WIN_NR] = {
1238c2ecf20Sopenharmony_ci	{
1248c2ecf20Sopenharmony_ci		.zpos = 0,
1258c2ecf20Sopenharmony_ci		.type = DRM_PLANE_TYPE_PRIMARY,
1268c2ecf20Sopenharmony_ci		.pixel_formats = mixer_formats,
1278c2ecf20Sopenharmony_ci		.num_pixel_formats = ARRAY_SIZE(mixer_formats),
1288c2ecf20Sopenharmony_ci		.capabilities = EXYNOS_DRM_PLANE_CAP_DOUBLE |
1298c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_ZPOS |
1308c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_PIX_BLEND |
1318c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_WIN_BLEND,
1328c2ecf20Sopenharmony_ci	}, {
1338c2ecf20Sopenharmony_ci		.zpos = 1,
1348c2ecf20Sopenharmony_ci		.type = DRM_PLANE_TYPE_CURSOR,
1358c2ecf20Sopenharmony_ci		.pixel_formats = mixer_formats,
1368c2ecf20Sopenharmony_ci		.num_pixel_formats = ARRAY_SIZE(mixer_formats),
1378c2ecf20Sopenharmony_ci		.capabilities = EXYNOS_DRM_PLANE_CAP_DOUBLE |
1388c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_ZPOS |
1398c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_PIX_BLEND |
1408c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_WIN_BLEND,
1418c2ecf20Sopenharmony_ci	}, {
1428c2ecf20Sopenharmony_ci		.zpos = 2,
1438c2ecf20Sopenharmony_ci		.type = DRM_PLANE_TYPE_OVERLAY,
1448c2ecf20Sopenharmony_ci		.pixel_formats = vp_formats,
1458c2ecf20Sopenharmony_ci		.num_pixel_formats = ARRAY_SIZE(vp_formats),
1468c2ecf20Sopenharmony_ci		.capabilities = EXYNOS_DRM_PLANE_CAP_SCALE |
1478c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_ZPOS |
1488c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_TILE |
1498c2ecf20Sopenharmony_ci				EXYNOS_DRM_PLANE_CAP_WIN_BLEND,
1508c2ecf20Sopenharmony_ci	},
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic const u8 filter_y_horiz_tap8[] = {
1548c2ecf20Sopenharmony_ci	0,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
1558c2ecf20Sopenharmony_ci	-1,	-1,	-1,	-1,	-1,	0,	0,	0,
1568c2ecf20Sopenharmony_ci	0,	2,	4,	5,	6,	6,	6,	6,
1578c2ecf20Sopenharmony_ci	6,	5,	5,	4,	3,	2,	1,	1,
1588c2ecf20Sopenharmony_ci	0,	-6,	-12,	-16,	-18,	-20,	-21,	-20,
1598c2ecf20Sopenharmony_ci	-20,	-18,	-16,	-13,	-10,	-8,	-5,	-2,
1608c2ecf20Sopenharmony_ci	127,	126,	125,	121,	114,	107,	99,	89,
1618c2ecf20Sopenharmony_ci	79,	68,	57,	46,	35,	25,	16,	8,
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic const u8 filter_y_vert_tap4[] = {
1658c2ecf20Sopenharmony_ci	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
1668c2ecf20Sopenharmony_ci	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
1678c2ecf20Sopenharmony_ci	127,	126,	124,	118,	111,	102,	92,	81,
1688c2ecf20Sopenharmony_ci	70,	59,	48,	37,	27,	19,	11,	5,
1698c2ecf20Sopenharmony_ci	0,	5,	11,	19,	27,	37,	48,	59,
1708c2ecf20Sopenharmony_ci	70,	81,	92,	102,	111,	118,	124,	126,
1718c2ecf20Sopenharmony_ci	0,	0,	-1,	-1,	-2,	-3,	-4,	-5,
1728c2ecf20Sopenharmony_ci	-6,	-7,	-8,	-8,	-8,	-8,	-6,	-3,
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic const u8 filter_cr_horiz_tap4[] = {
1768c2ecf20Sopenharmony_ci	0,	-3,	-6,	-8,	-8,	-8,	-8,	-7,
1778c2ecf20Sopenharmony_ci	-6,	-5,	-4,	-3,	-2,	-1,	-1,	0,
1788c2ecf20Sopenharmony_ci	127,	126,	124,	118,	111,	102,	92,	81,
1798c2ecf20Sopenharmony_ci	70,	59,	48,	37,	27,	19,	11,	5,
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic inline u32 vp_reg_read(struct mixer_context *ctx, u32 reg_id)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	return readl(ctx->vp_regs + reg_id);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic inline void vp_reg_write(struct mixer_context *ctx, u32 reg_id,
1888c2ecf20Sopenharmony_ci				 u32 val)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	writel(val, ctx->vp_regs + reg_id);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic inline void vp_reg_writemask(struct mixer_context *ctx, u32 reg_id,
1948c2ecf20Sopenharmony_ci				 u32 val, u32 mask)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	u32 old = vp_reg_read(ctx, reg_id);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	val = (val & mask) | (old & ~mask);
1998c2ecf20Sopenharmony_ci	writel(val, ctx->vp_regs + reg_id);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic inline u32 mixer_reg_read(struct mixer_context *ctx, u32 reg_id)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	return readl(ctx->mixer_regs + reg_id);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic inline void mixer_reg_write(struct mixer_context *ctx, u32 reg_id,
2088c2ecf20Sopenharmony_ci				 u32 val)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	writel(val, ctx->mixer_regs + reg_id);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic inline void mixer_reg_writemask(struct mixer_context *ctx,
2148c2ecf20Sopenharmony_ci				 u32 reg_id, u32 val, u32 mask)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	u32 old = mixer_reg_read(ctx, reg_id);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	val = (val & mask) | (old & ~mask);
2198c2ecf20Sopenharmony_ci	writel(val, ctx->mixer_regs + reg_id);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic void mixer_regs_dump(struct mixer_context *ctx)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci#define DUMPREG(reg_id) \
2258c2ecf20Sopenharmony_cido { \
2268c2ecf20Sopenharmony_ci	DRM_DEV_DEBUG_KMS(ctx->dev, #reg_id " = %08x\n", \
2278c2ecf20Sopenharmony_ci			 (u32)readl(ctx->mixer_regs + reg_id)); \
2288c2ecf20Sopenharmony_ci} while (0)
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	DUMPREG(MXR_STATUS);
2318c2ecf20Sopenharmony_ci	DUMPREG(MXR_CFG);
2328c2ecf20Sopenharmony_ci	DUMPREG(MXR_INT_EN);
2338c2ecf20Sopenharmony_ci	DUMPREG(MXR_INT_STATUS);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	DUMPREG(MXR_LAYER_CFG);
2368c2ecf20Sopenharmony_ci	DUMPREG(MXR_VIDEO_CFG);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC0_CFG);
2398c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC0_BASE);
2408c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC0_SPAN);
2418c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC0_WH);
2428c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC0_SXY);
2438c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC0_DXY);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC1_CFG);
2468c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC1_BASE);
2478c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC1_SPAN);
2488c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC1_WH);
2498c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC1_SXY);
2508c2ecf20Sopenharmony_ci	DUMPREG(MXR_GRAPHIC1_DXY);
2518c2ecf20Sopenharmony_ci#undef DUMPREG
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic void vp_regs_dump(struct mixer_context *ctx)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci#define DUMPREG(reg_id) \
2578c2ecf20Sopenharmony_cido { \
2588c2ecf20Sopenharmony_ci	DRM_DEV_DEBUG_KMS(ctx->dev, #reg_id " = %08x\n", \
2598c2ecf20Sopenharmony_ci			 (u32) readl(ctx->vp_regs + reg_id)); \
2608c2ecf20Sopenharmony_ci} while (0)
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	DUMPREG(VP_ENABLE);
2638c2ecf20Sopenharmony_ci	DUMPREG(VP_SRESET);
2648c2ecf20Sopenharmony_ci	DUMPREG(VP_SHADOW_UPDATE);
2658c2ecf20Sopenharmony_ci	DUMPREG(VP_FIELD_ID);
2668c2ecf20Sopenharmony_ci	DUMPREG(VP_MODE);
2678c2ecf20Sopenharmony_ci	DUMPREG(VP_IMG_SIZE_Y);
2688c2ecf20Sopenharmony_ci	DUMPREG(VP_IMG_SIZE_C);
2698c2ecf20Sopenharmony_ci	DUMPREG(VP_PER_RATE_CTRL);
2708c2ecf20Sopenharmony_ci	DUMPREG(VP_TOP_Y_PTR);
2718c2ecf20Sopenharmony_ci	DUMPREG(VP_BOT_Y_PTR);
2728c2ecf20Sopenharmony_ci	DUMPREG(VP_TOP_C_PTR);
2738c2ecf20Sopenharmony_ci	DUMPREG(VP_BOT_C_PTR);
2748c2ecf20Sopenharmony_ci	DUMPREG(VP_ENDIAN_MODE);
2758c2ecf20Sopenharmony_ci	DUMPREG(VP_SRC_H_POSITION);
2768c2ecf20Sopenharmony_ci	DUMPREG(VP_SRC_V_POSITION);
2778c2ecf20Sopenharmony_ci	DUMPREG(VP_SRC_WIDTH);
2788c2ecf20Sopenharmony_ci	DUMPREG(VP_SRC_HEIGHT);
2798c2ecf20Sopenharmony_ci	DUMPREG(VP_DST_H_POSITION);
2808c2ecf20Sopenharmony_ci	DUMPREG(VP_DST_V_POSITION);
2818c2ecf20Sopenharmony_ci	DUMPREG(VP_DST_WIDTH);
2828c2ecf20Sopenharmony_ci	DUMPREG(VP_DST_HEIGHT);
2838c2ecf20Sopenharmony_ci	DUMPREG(VP_H_RATIO);
2848c2ecf20Sopenharmony_ci	DUMPREG(VP_V_RATIO);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci#undef DUMPREG
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic inline void vp_filter_set(struct mixer_context *ctx,
2908c2ecf20Sopenharmony_ci		int reg_id, const u8 *data, unsigned int size)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	/* assure 4-byte align */
2938c2ecf20Sopenharmony_ci	BUG_ON(size & 3);
2948c2ecf20Sopenharmony_ci	for (; size; size -= 4, reg_id += 4, data += 4) {
2958c2ecf20Sopenharmony_ci		u32 val = (data[0] << 24) |  (data[1] << 16) |
2968c2ecf20Sopenharmony_ci			(data[2] << 8) | data[3];
2978c2ecf20Sopenharmony_ci		vp_reg_write(ctx, reg_id, val);
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic void vp_default_filter(struct mixer_context *ctx)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	vp_filter_set(ctx, VP_POLY8_Y0_LL,
3048c2ecf20Sopenharmony_ci		filter_y_horiz_tap8, sizeof(filter_y_horiz_tap8));
3058c2ecf20Sopenharmony_ci	vp_filter_set(ctx, VP_POLY4_Y0_LL,
3068c2ecf20Sopenharmony_ci		filter_y_vert_tap4, sizeof(filter_y_vert_tap4));
3078c2ecf20Sopenharmony_ci	vp_filter_set(ctx, VP_POLY4_C0_LL,
3088c2ecf20Sopenharmony_ci		filter_cr_horiz_tap4, sizeof(filter_cr_horiz_tap4));
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic void mixer_cfg_gfx_blend(struct mixer_context *ctx, unsigned int win,
3128c2ecf20Sopenharmony_ci				unsigned int pixel_alpha, unsigned int alpha)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	u32 win_alpha = alpha >> 8;
3158c2ecf20Sopenharmony_ci	u32 val;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	val  = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
3188c2ecf20Sopenharmony_ci	switch (pixel_alpha) {
3198c2ecf20Sopenharmony_ci	case DRM_MODE_BLEND_PIXEL_NONE:
3208c2ecf20Sopenharmony_ci		break;
3218c2ecf20Sopenharmony_ci	case DRM_MODE_BLEND_COVERAGE:
3228c2ecf20Sopenharmony_ci		val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
3238c2ecf20Sopenharmony_ci		break;
3248c2ecf20Sopenharmony_ci	case DRM_MODE_BLEND_PREMULTI:
3258c2ecf20Sopenharmony_ci	default:
3268c2ecf20Sopenharmony_ci		val |= MXR_GRP_CFG_BLEND_PRE_MUL;
3278c2ecf20Sopenharmony_ci		val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
3288c2ecf20Sopenharmony_ci		break;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	if (alpha != DRM_BLEND_ALPHA_OPAQUE) {
3328c2ecf20Sopenharmony_ci		val |= MXR_GRP_CFG_WIN_BLEND_EN;
3338c2ecf20Sopenharmony_ci		val |= win_alpha;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_GRAPHIC_CFG(win),
3368c2ecf20Sopenharmony_ci			    val, MXR_GRP_CFG_MISC_MASK);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic void mixer_cfg_vp_blend(struct mixer_context *ctx, unsigned int alpha)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	u32 win_alpha = alpha >> 8;
3428c2ecf20Sopenharmony_ci	u32 val = 0;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (alpha != DRM_BLEND_ALPHA_OPAQUE) {
3458c2ecf20Sopenharmony_ci		val |= MXR_VID_CFG_BLEND_EN;
3468c2ecf20Sopenharmony_ci		val |= win_alpha;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_VIDEO_CFG, val);
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic bool mixer_is_synced(struct mixer_context *ctx)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	u32 base, shadow;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (ctx->mxr_ver == MXR_VER_16_0_33_0 ||
3568c2ecf20Sopenharmony_ci	    ctx->mxr_ver == MXR_VER_128_0_0_184)
3578c2ecf20Sopenharmony_ci		return !(mixer_reg_read(ctx, MXR_CFG) &
3588c2ecf20Sopenharmony_ci			 MXR_CFG_LAYER_UPDATE_COUNT_MASK);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags) &&
3618c2ecf20Sopenharmony_ci	    vp_reg_read(ctx, VP_SHADOW_UPDATE))
3628c2ecf20Sopenharmony_ci		return false;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	base = mixer_reg_read(ctx, MXR_CFG);
3658c2ecf20Sopenharmony_ci	shadow = mixer_reg_read(ctx, MXR_CFG_S);
3668c2ecf20Sopenharmony_ci	if (base != shadow)
3678c2ecf20Sopenharmony_ci		return false;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	base = mixer_reg_read(ctx, MXR_GRAPHIC_BASE(0));
3708c2ecf20Sopenharmony_ci	shadow = mixer_reg_read(ctx, MXR_GRAPHIC_BASE_S(0));
3718c2ecf20Sopenharmony_ci	if (base != shadow)
3728c2ecf20Sopenharmony_ci		return false;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	base = mixer_reg_read(ctx, MXR_GRAPHIC_BASE(1));
3758c2ecf20Sopenharmony_ci	shadow = mixer_reg_read(ctx, MXR_GRAPHIC_BASE_S(1));
3768c2ecf20Sopenharmony_ci	if (base != shadow)
3778c2ecf20Sopenharmony_ci		return false;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	return true;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic int mixer_wait_for_sync(struct mixer_context *ctx)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	ktime_t timeout = ktime_add_us(ktime_get(), 100000);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	while (!mixer_is_synced(ctx)) {
3878c2ecf20Sopenharmony_ci		usleep_range(1000, 2000);
3888c2ecf20Sopenharmony_ci		if (ktime_compare(ktime_get(), timeout) > 0)
3898c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci	return 0;
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic void mixer_disable_sync(struct mixer_context *ctx)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_STATUS, 0, MXR_STATUS_SYNC_ENABLE);
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic void mixer_enable_sync(struct mixer_context *ctx)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	if (ctx->mxr_ver == MXR_VER_16_0_33_0 ||
4028c2ecf20Sopenharmony_ci	    ctx->mxr_ver == MXR_VER_128_0_0_184)
4038c2ecf20Sopenharmony_ci		mixer_reg_writemask(ctx, MXR_CFG, ~0, MXR_CFG_LAYER_UPDATE);
4048c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_STATUS, ~0, MXR_STATUS_SYNC_ENABLE);
4058c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags))
4068c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_SHADOW_UPDATE, VP_SHADOW_UPDATE_ENABLE);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic void mixer_cfg_scan(struct mixer_context *ctx, int width, int height)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	u32 val;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	/* choosing between interlace and progressive mode */
4148c2ecf20Sopenharmony_ci	val = test_bit(MXR_BIT_INTERLACE, &ctx->flags) ?
4158c2ecf20Sopenharmony_ci		MXR_CFG_SCAN_INTERLACE : MXR_CFG_SCAN_PROGRESSIVE;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (ctx->mxr_ver == MXR_VER_128_0_0_184)
4188c2ecf20Sopenharmony_ci		mixer_reg_write(ctx, MXR_RESOLUTION,
4198c2ecf20Sopenharmony_ci			MXR_MXR_RES_HEIGHT(height) | MXR_MXR_RES_WIDTH(width));
4208c2ecf20Sopenharmony_ci	else
4218c2ecf20Sopenharmony_ci		val |= ctx->scan_value;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_CFG, val, MXR_CFG_SCAN_MASK);
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic void mixer_cfg_rgb_fmt(struct mixer_context *ctx, struct drm_display_mode *mode)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	enum hdmi_quantization_range range = drm_default_rgb_quant_range(mode);
4298c2ecf20Sopenharmony_ci	u32 val;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	if (mode->vdisplay < 720) {
4328c2ecf20Sopenharmony_ci		val = MXR_CFG_RGB601;
4338c2ecf20Sopenharmony_ci	} else {
4348c2ecf20Sopenharmony_ci		val = MXR_CFG_RGB709;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		/* Configure the BT.709 CSC matrix for full range RGB. */
4378c2ecf20Sopenharmony_ci		mixer_reg_write(ctx, MXR_CM_COEFF_Y,
4388c2ecf20Sopenharmony_ci			MXR_CSC_CT( 0.184,  0.614,  0.063) |
4398c2ecf20Sopenharmony_ci			MXR_CM_COEFF_RGB_FULL);
4408c2ecf20Sopenharmony_ci		mixer_reg_write(ctx, MXR_CM_COEFF_CB,
4418c2ecf20Sopenharmony_ci			MXR_CSC_CT(-0.102, -0.338,  0.440));
4428c2ecf20Sopenharmony_ci		mixer_reg_write(ctx, MXR_CM_COEFF_CR,
4438c2ecf20Sopenharmony_ci			MXR_CSC_CT( 0.440, -0.399, -0.040));
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	if (range == HDMI_QUANTIZATION_RANGE_FULL)
4478c2ecf20Sopenharmony_ci		val |= MXR_CFG_QUANT_RANGE_FULL;
4488c2ecf20Sopenharmony_ci	else
4498c2ecf20Sopenharmony_ci		val |= MXR_CFG_QUANT_RANGE_LIMITED;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic void mixer_cfg_layer(struct mixer_context *ctx, unsigned int win,
4558c2ecf20Sopenharmony_ci			    unsigned int priority, bool enable)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	u32 val = enable ? ~0 : 0;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	switch (win) {
4608c2ecf20Sopenharmony_ci	case 0:
4618c2ecf20Sopenharmony_ci		mixer_reg_writemask(ctx, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
4628c2ecf20Sopenharmony_ci		mixer_reg_writemask(ctx, MXR_LAYER_CFG,
4638c2ecf20Sopenharmony_ci				    MXR_LAYER_CFG_GRP0_VAL(priority),
4648c2ecf20Sopenharmony_ci				    MXR_LAYER_CFG_GRP0_MASK);
4658c2ecf20Sopenharmony_ci		break;
4668c2ecf20Sopenharmony_ci	case 1:
4678c2ecf20Sopenharmony_ci		mixer_reg_writemask(ctx, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
4688c2ecf20Sopenharmony_ci		mixer_reg_writemask(ctx, MXR_LAYER_CFG,
4698c2ecf20Sopenharmony_ci				    MXR_LAYER_CFG_GRP1_VAL(priority),
4708c2ecf20Sopenharmony_ci				    MXR_LAYER_CFG_GRP1_MASK);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci		break;
4738c2ecf20Sopenharmony_ci	case VP_DEFAULT_WIN:
4748c2ecf20Sopenharmony_ci		if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
4758c2ecf20Sopenharmony_ci			vp_reg_writemask(ctx, VP_ENABLE, val, VP_ENABLE_ON);
4768c2ecf20Sopenharmony_ci			mixer_reg_writemask(ctx, MXR_CFG, val,
4778c2ecf20Sopenharmony_ci				MXR_CFG_VP_ENABLE);
4788c2ecf20Sopenharmony_ci			mixer_reg_writemask(ctx, MXR_LAYER_CFG,
4798c2ecf20Sopenharmony_ci					    MXR_LAYER_CFG_VP_VAL(priority),
4808c2ecf20Sopenharmony_ci					    MXR_LAYER_CFG_VP_MASK);
4818c2ecf20Sopenharmony_ci		}
4828c2ecf20Sopenharmony_ci		break;
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic void mixer_run(struct mixer_context *ctx)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_cistatic void mixer_stop(struct mixer_context *ctx)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci	int timeout = 20;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_STATUS, 0, MXR_STATUS_REG_RUN);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	while (!(mixer_reg_read(ctx, MXR_STATUS) & MXR_STATUS_REG_IDLE) &&
4988c2ecf20Sopenharmony_ci			--timeout)
4998c2ecf20Sopenharmony_ci		usleep_range(10000, 12000);
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistatic void mixer_commit(struct mixer_context *ctx)
5038c2ecf20Sopenharmony_ci{
5048c2ecf20Sopenharmony_ci	struct drm_display_mode *mode = &ctx->crtc->base.state->adjusted_mode;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	mixer_cfg_scan(ctx, mode->hdisplay, mode->vdisplay);
5078c2ecf20Sopenharmony_ci	mixer_cfg_rgb_fmt(ctx, mode);
5088c2ecf20Sopenharmony_ci	mixer_run(ctx);
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic void vp_video_buffer(struct mixer_context *ctx,
5128c2ecf20Sopenharmony_ci			    struct exynos_drm_plane *plane)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	struct exynos_drm_plane_state *state =
5158c2ecf20Sopenharmony_ci				to_exynos_plane_state(plane->base.state);
5168c2ecf20Sopenharmony_ci	struct drm_framebuffer *fb = state->base.fb;
5178c2ecf20Sopenharmony_ci	unsigned int priority = state->base.normalized_zpos + 1;
5188c2ecf20Sopenharmony_ci	unsigned long flags;
5198c2ecf20Sopenharmony_ci	dma_addr_t luma_addr[2], chroma_addr[2];
5208c2ecf20Sopenharmony_ci	bool is_tiled, is_nv21;
5218c2ecf20Sopenharmony_ci	u32 val;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	is_nv21 = (fb->format->format == DRM_FORMAT_NV21);
5248c2ecf20Sopenharmony_ci	is_tiled = (fb->modifier == DRM_FORMAT_MOD_SAMSUNG_64_32_TILE);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	luma_addr[0] = exynos_drm_fb_dma_addr(fb, 0);
5278c2ecf20Sopenharmony_ci	chroma_addr[0] = exynos_drm_fb_dma_addr(fb, 1);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)) {
5308c2ecf20Sopenharmony_ci		if (is_tiled) {
5318c2ecf20Sopenharmony_ci			luma_addr[1] = luma_addr[0] + 0x40;
5328c2ecf20Sopenharmony_ci			chroma_addr[1] = chroma_addr[0] + 0x40;
5338c2ecf20Sopenharmony_ci		} else {
5348c2ecf20Sopenharmony_ci			luma_addr[1] = luma_addr[0] + fb->pitches[0];
5358c2ecf20Sopenharmony_ci			chroma_addr[1] = chroma_addr[0] + fb->pitches[1];
5368c2ecf20Sopenharmony_ci		}
5378c2ecf20Sopenharmony_ci	} else {
5388c2ecf20Sopenharmony_ci		luma_addr[1] = 0;
5398c2ecf20Sopenharmony_ci		chroma_addr[1] = 0;
5408c2ecf20Sopenharmony_ci	}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->reg_slock, flags);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	/* interlace or progressive scan mode */
5458c2ecf20Sopenharmony_ci	val = (test_bit(MXR_BIT_INTERLACE, &ctx->flags) ? ~0 : 0);
5468c2ecf20Sopenharmony_ci	vp_reg_writemask(ctx, VP_MODE, val, VP_MODE_LINE_SKIP);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/* setup format */
5498c2ecf20Sopenharmony_ci	val = (is_nv21 ? VP_MODE_NV21 : VP_MODE_NV12);
5508c2ecf20Sopenharmony_ci	val |= (is_tiled ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
5518c2ecf20Sopenharmony_ci	vp_reg_writemask(ctx, VP_MODE, val, VP_MODE_FMT_MASK);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	/* setting size of input image */
5548c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_IMG_SIZE_Y, VP_IMG_HSIZE(fb->pitches[0]) |
5558c2ecf20Sopenharmony_ci		VP_IMG_VSIZE(fb->height));
5568c2ecf20Sopenharmony_ci	/* chroma plane for NV12/NV21 is half the height of the luma plane */
5578c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_IMG_SIZE_C, VP_IMG_HSIZE(fb->pitches[1]) |
5588c2ecf20Sopenharmony_ci		VP_IMG_VSIZE(fb->height / 2));
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_SRC_WIDTH, state->src.w);
5618c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_SRC_H_POSITION,
5628c2ecf20Sopenharmony_ci			VP_SRC_H_POSITION_VAL(state->src.x));
5638c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_DST_WIDTH, state->crtc.w);
5648c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_DST_H_POSITION, state->crtc.x);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)) {
5678c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_SRC_HEIGHT, state->src.h / 2);
5688c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_SRC_V_POSITION, state->src.y / 2);
5698c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_DST_HEIGHT, state->crtc.h / 2);
5708c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_DST_V_POSITION, state->crtc.y / 2);
5718c2ecf20Sopenharmony_ci	} else {
5728c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_SRC_HEIGHT, state->src.h);
5738c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_SRC_V_POSITION, state->src.y);
5748c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_DST_HEIGHT, state->crtc.h);
5758c2ecf20Sopenharmony_ci		vp_reg_write(ctx, VP_DST_V_POSITION, state->crtc.y);
5768c2ecf20Sopenharmony_ci	}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_H_RATIO, state->h_ratio);
5798c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_V_RATIO, state->v_ratio);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	/* set buffer address to vp */
5848c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_TOP_Y_PTR, luma_addr[0]);
5858c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_BOT_Y_PTR, luma_addr[1]);
5868c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_TOP_C_PTR, chroma_addr[0]);
5878c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_BOT_C_PTR, chroma_addr[1]);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	mixer_cfg_layer(ctx, plane->index, priority, true);
5908c2ecf20Sopenharmony_ci	mixer_cfg_vp_blend(ctx, state->base.alpha);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->reg_slock, flags);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	mixer_regs_dump(ctx);
5958c2ecf20Sopenharmony_ci	vp_regs_dump(ctx);
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_cistatic void mixer_graph_buffer(struct mixer_context *ctx,
5998c2ecf20Sopenharmony_ci			       struct exynos_drm_plane *plane)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci	struct exynos_drm_plane_state *state =
6028c2ecf20Sopenharmony_ci				to_exynos_plane_state(plane->base.state);
6038c2ecf20Sopenharmony_ci	struct drm_framebuffer *fb = state->base.fb;
6048c2ecf20Sopenharmony_ci	unsigned int priority = state->base.normalized_zpos + 1;
6058c2ecf20Sopenharmony_ci	unsigned long flags;
6068c2ecf20Sopenharmony_ci	unsigned int win = plane->index;
6078c2ecf20Sopenharmony_ci	unsigned int x_ratio = 0, y_ratio = 0;
6088c2ecf20Sopenharmony_ci	unsigned int dst_x_offset, dst_y_offset;
6098c2ecf20Sopenharmony_ci	unsigned int pixel_alpha;
6108c2ecf20Sopenharmony_ci	dma_addr_t dma_addr;
6118c2ecf20Sopenharmony_ci	unsigned int fmt;
6128c2ecf20Sopenharmony_ci	u32 val;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (fb->format->has_alpha)
6158c2ecf20Sopenharmony_ci		pixel_alpha = state->base.pixel_blend_mode;
6168c2ecf20Sopenharmony_ci	else
6178c2ecf20Sopenharmony_ci		pixel_alpha = DRM_MODE_BLEND_PIXEL_NONE;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	switch (fb->format->format) {
6208c2ecf20Sopenharmony_ci	case DRM_FORMAT_XRGB4444:
6218c2ecf20Sopenharmony_ci	case DRM_FORMAT_ARGB4444:
6228c2ecf20Sopenharmony_ci		fmt = MXR_FORMAT_ARGB4444;
6238c2ecf20Sopenharmony_ci		break;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	case DRM_FORMAT_XRGB1555:
6268c2ecf20Sopenharmony_ci	case DRM_FORMAT_ARGB1555:
6278c2ecf20Sopenharmony_ci		fmt = MXR_FORMAT_ARGB1555;
6288c2ecf20Sopenharmony_ci		break;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	case DRM_FORMAT_RGB565:
6318c2ecf20Sopenharmony_ci		fmt = MXR_FORMAT_RGB565;
6328c2ecf20Sopenharmony_ci		break;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	case DRM_FORMAT_XRGB8888:
6358c2ecf20Sopenharmony_ci	case DRM_FORMAT_ARGB8888:
6368c2ecf20Sopenharmony_ci	default:
6378c2ecf20Sopenharmony_ci		fmt = MXR_FORMAT_ARGB8888;
6388c2ecf20Sopenharmony_ci		break;
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	/* ratio is already checked by common plane code */
6428c2ecf20Sopenharmony_ci	x_ratio = state->h_ratio == (1 << 15);
6438c2ecf20Sopenharmony_ci	y_ratio = state->v_ratio == (1 << 15);
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	dst_x_offset = state->crtc.x;
6468c2ecf20Sopenharmony_ci	dst_y_offset = state->crtc.y;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	/* translate dma address base s.t. the source image offset is zero */
6498c2ecf20Sopenharmony_ci	dma_addr = exynos_drm_fb_dma_addr(fb, 0)
6508c2ecf20Sopenharmony_ci		+ (state->src.x * fb->format->cpp[0])
6518c2ecf20Sopenharmony_ci		+ (state->src.y * fb->pitches[0]);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->reg_slock, flags);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	/* setup format */
6568c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_GRAPHIC_CFG(win),
6578c2ecf20Sopenharmony_ci		MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	/* setup geometry */
6608c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_GRAPHIC_SPAN(win),
6618c2ecf20Sopenharmony_ci			fb->pitches[0] / fb->format->cpp[0]);
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	val  = MXR_GRP_WH_WIDTH(state->src.w);
6648c2ecf20Sopenharmony_ci	val |= MXR_GRP_WH_HEIGHT(state->src.h);
6658c2ecf20Sopenharmony_ci	val |= MXR_GRP_WH_H_SCALE(x_ratio);
6668c2ecf20Sopenharmony_ci	val |= MXR_GRP_WH_V_SCALE(y_ratio);
6678c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_GRAPHIC_WH(win), val);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	/* setup offsets in display image */
6708c2ecf20Sopenharmony_ci	val  = MXR_GRP_DXY_DX(dst_x_offset);
6718c2ecf20Sopenharmony_ci	val |= MXR_GRP_DXY_DY(dst_y_offset);
6728c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_GRAPHIC_DXY(win), val);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/* set buffer address to mixer */
6758c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_GRAPHIC_BASE(win), dma_addr);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	mixer_cfg_layer(ctx, win, priority, true);
6788c2ecf20Sopenharmony_ci	mixer_cfg_gfx_blend(ctx, win, pixel_alpha, state->base.alpha);
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->reg_slock, flags);
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	mixer_regs_dump(ctx);
6838c2ecf20Sopenharmony_ci}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_cistatic void vp_win_reset(struct mixer_context *ctx)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	unsigned int tries = 100;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	vp_reg_write(ctx, VP_SRESET, VP_SRESET_PROCESSING);
6908c2ecf20Sopenharmony_ci	while (--tries) {
6918c2ecf20Sopenharmony_ci		/* waiting until VP_SRESET_PROCESSING is 0 */
6928c2ecf20Sopenharmony_ci		if (~vp_reg_read(ctx, VP_SRESET) & VP_SRESET_PROCESSING)
6938c2ecf20Sopenharmony_ci			break;
6948c2ecf20Sopenharmony_ci		mdelay(10);
6958c2ecf20Sopenharmony_ci	}
6968c2ecf20Sopenharmony_ci	WARN(tries == 0, "failed to reset Video Processor\n");
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_cistatic void mixer_win_reset(struct mixer_context *ctx)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	unsigned long flags;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->reg_slock, flags);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	/* set output in RGB888 mode */
7088c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* 16 beat burst in DMA */
7118c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_STATUS, MXR_STATUS_16_BURST,
7128c2ecf20Sopenharmony_ci		MXR_STATUS_BURST_MASK);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	/* reset default layer priority */
7158c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_LAYER_CFG, 0);
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	/* set all background colors to RGB (0,0,0) */
7188c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_BG_COLOR0, MXR_YCBCR_VAL(0, 128, 128));
7198c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_BG_COLOR1, MXR_YCBCR_VAL(0, 128, 128));
7208c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_BG_COLOR2, MXR_YCBCR_VAL(0, 128, 128));
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
7238c2ecf20Sopenharmony_ci		/* configuration of Video Processor Registers */
7248c2ecf20Sopenharmony_ci		vp_win_reset(ctx);
7258c2ecf20Sopenharmony_ci		vp_default_filter(ctx);
7268c2ecf20Sopenharmony_ci	}
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	/* disable all layers */
7298c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
7308c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
7318c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags))
7328c2ecf20Sopenharmony_ci		mixer_reg_writemask(ctx, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	/* set all source image offsets to zero */
7358c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_GRAPHIC_SXY(0), 0);
7368c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_GRAPHIC_SXY(1), 0);
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->reg_slock, flags);
7398c2ecf20Sopenharmony_ci}
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_cistatic irqreturn_t mixer_irq_handler(int irq, void *arg)
7428c2ecf20Sopenharmony_ci{
7438c2ecf20Sopenharmony_ci	struct mixer_context *ctx = arg;
7448c2ecf20Sopenharmony_ci	u32 val;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	spin_lock(&ctx->reg_slock);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	/* read interrupt status for handling and clearing flags for VSYNC */
7498c2ecf20Sopenharmony_ci	val = mixer_reg_read(ctx, MXR_INT_STATUS);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	/* handling VSYNC */
7528c2ecf20Sopenharmony_ci	if (val & MXR_INT_STATUS_VSYNC) {
7538c2ecf20Sopenharmony_ci		/* vsync interrupt use different bit for read and clear */
7548c2ecf20Sopenharmony_ci		val |= MXR_INT_CLEAR_VSYNC;
7558c2ecf20Sopenharmony_ci		val &= ~MXR_INT_STATUS_VSYNC;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci		/* interlace scan need to check shadow register */
7588c2ecf20Sopenharmony_ci		if (test_bit(MXR_BIT_INTERLACE, &ctx->flags)
7598c2ecf20Sopenharmony_ci		    && !mixer_is_synced(ctx))
7608c2ecf20Sopenharmony_ci			goto out;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci		drm_crtc_handle_vblank(&ctx->crtc->base);
7638c2ecf20Sopenharmony_ci	}
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ciout:
7668c2ecf20Sopenharmony_ci	/* clear interrupts */
7678c2ecf20Sopenharmony_ci	mixer_reg_write(ctx, MXR_INT_STATUS, val);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	spin_unlock(&ctx->reg_slock);
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic int mixer_resources_init(struct mixer_context *mixer_ctx)
7758c2ecf20Sopenharmony_ci{
7768c2ecf20Sopenharmony_ci	struct device *dev = &mixer_ctx->pdev->dev;
7778c2ecf20Sopenharmony_ci	struct resource *res;
7788c2ecf20Sopenharmony_ci	int ret;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	spin_lock_init(&mixer_ctx->reg_slock);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	mixer_ctx->mixer = devm_clk_get(dev, "mixer");
7838c2ecf20Sopenharmony_ci	if (IS_ERR(mixer_ctx->mixer)) {
7848c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get clock 'mixer'\n");
7858c2ecf20Sopenharmony_ci		return -ENODEV;
7868c2ecf20Sopenharmony_ci	}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	mixer_ctx->hdmi = devm_clk_get(dev, "hdmi");
7898c2ecf20Sopenharmony_ci	if (IS_ERR(mixer_ctx->hdmi)) {
7908c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get clock 'hdmi'\n");
7918c2ecf20Sopenharmony_ci		return PTR_ERR(mixer_ctx->hdmi);
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	mixer_ctx->sclk_hdmi = devm_clk_get(dev, "sclk_hdmi");
7958c2ecf20Sopenharmony_ci	if (IS_ERR(mixer_ctx->sclk_hdmi)) {
7968c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
7978c2ecf20Sopenharmony_ci		return -ENODEV;
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 0);
8008c2ecf20Sopenharmony_ci	if (res == NULL) {
8018c2ecf20Sopenharmony_ci		dev_err(dev, "get memory resource failed.\n");
8028c2ecf20Sopenharmony_ci		return -ENXIO;
8038c2ecf20Sopenharmony_ci	}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	mixer_ctx->mixer_regs = devm_ioremap(dev, res->start,
8068c2ecf20Sopenharmony_ci							resource_size(res));
8078c2ecf20Sopenharmony_ci	if (mixer_ctx->mixer_regs == NULL) {
8088c2ecf20Sopenharmony_ci		dev_err(dev, "register mapping failed.\n");
8098c2ecf20Sopenharmony_ci		return -ENXIO;
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_IRQ, 0);
8138c2ecf20Sopenharmony_ci	if (res == NULL) {
8148c2ecf20Sopenharmony_ci		dev_err(dev, "get interrupt resource failed.\n");
8158c2ecf20Sopenharmony_ci		return -ENXIO;
8168c2ecf20Sopenharmony_ci	}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	ret = devm_request_irq(dev, res->start, mixer_irq_handler,
8198c2ecf20Sopenharmony_ci						0, "drm_mixer", mixer_ctx);
8208c2ecf20Sopenharmony_ci	if (ret) {
8218c2ecf20Sopenharmony_ci		dev_err(dev, "request interrupt failed.\n");
8228c2ecf20Sopenharmony_ci		return ret;
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci	mixer_ctx->irq = res->start;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	return 0;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_cistatic int vp_resources_init(struct mixer_context *mixer_ctx)
8308c2ecf20Sopenharmony_ci{
8318c2ecf20Sopenharmony_ci	struct device *dev = &mixer_ctx->pdev->dev;
8328c2ecf20Sopenharmony_ci	struct resource *res;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	mixer_ctx->vp = devm_clk_get(dev, "vp");
8358c2ecf20Sopenharmony_ci	if (IS_ERR(mixer_ctx->vp)) {
8368c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get clock 'vp'\n");
8378c2ecf20Sopenharmony_ci		return -ENODEV;
8388c2ecf20Sopenharmony_ci	}
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_HAS_SCLK, &mixer_ctx->flags)) {
8418c2ecf20Sopenharmony_ci		mixer_ctx->sclk_mixer = devm_clk_get(dev, "sclk_mixer");
8428c2ecf20Sopenharmony_ci		if (IS_ERR(mixer_ctx->sclk_mixer)) {
8438c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get clock 'sclk_mixer'\n");
8448c2ecf20Sopenharmony_ci			return -ENODEV;
8458c2ecf20Sopenharmony_ci		}
8468c2ecf20Sopenharmony_ci		mixer_ctx->mout_mixer = devm_clk_get(dev, "mout_mixer");
8478c2ecf20Sopenharmony_ci		if (IS_ERR(mixer_ctx->mout_mixer)) {
8488c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get clock 'mout_mixer'\n");
8498c2ecf20Sopenharmony_ci			return -ENODEV;
8508c2ecf20Sopenharmony_ci		}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci		if (mixer_ctx->sclk_hdmi && mixer_ctx->mout_mixer)
8538c2ecf20Sopenharmony_ci			clk_set_parent(mixer_ctx->mout_mixer,
8548c2ecf20Sopenharmony_ci				       mixer_ctx->sclk_hdmi);
8558c2ecf20Sopenharmony_ci	}
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 1);
8588c2ecf20Sopenharmony_ci	if (res == NULL) {
8598c2ecf20Sopenharmony_ci		dev_err(dev, "get memory resource failed.\n");
8608c2ecf20Sopenharmony_ci		return -ENXIO;
8618c2ecf20Sopenharmony_ci	}
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	mixer_ctx->vp_regs = devm_ioremap(dev, res->start,
8648c2ecf20Sopenharmony_ci							resource_size(res));
8658c2ecf20Sopenharmony_ci	if (mixer_ctx->vp_regs == NULL) {
8668c2ecf20Sopenharmony_ci		dev_err(dev, "register mapping failed.\n");
8678c2ecf20Sopenharmony_ci		return -ENXIO;
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	return 0;
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic int mixer_initialize(struct mixer_context *mixer_ctx,
8748c2ecf20Sopenharmony_ci			struct drm_device *drm_dev)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	int ret;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	mixer_ctx->drm_dev = drm_dev;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	/* acquire resources: regs, irqs, clocks */
8818c2ecf20Sopenharmony_ci	ret = mixer_resources_init(mixer_ctx);
8828c2ecf20Sopenharmony_ci	if (ret) {
8838c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(mixer_ctx->dev,
8848c2ecf20Sopenharmony_ci			      "mixer_resources_init failed ret=%d\n", ret);
8858c2ecf20Sopenharmony_ci		return ret;
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_VP_ENABLED, &mixer_ctx->flags)) {
8898c2ecf20Sopenharmony_ci		/* acquire vp resources: regs, irqs, clocks */
8908c2ecf20Sopenharmony_ci		ret = vp_resources_init(mixer_ctx);
8918c2ecf20Sopenharmony_ci		if (ret) {
8928c2ecf20Sopenharmony_ci			DRM_DEV_ERROR(mixer_ctx->dev,
8938c2ecf20Sopenharmony_ci				      "vp_resources_init failed ret=%d\n", ret);
8948c2ecf20Sopenharmony_ci			return ret;
8958c2ecf20Sopenharmony_ci		}
8968c2ecf20Sopenharmony_ci	}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	return exynos_drm_register_dma(drm_dev, mixer_ctx->dev,
8998c2ecf20Sopenharmony_ci				       &mixer_ctx->dma_priv);
9008c2ecf20Sopenharmony_ci}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_cistatic void mixer_ctx_remove(struct mixer_context *mixer_ctx)
9038c2ecf20Sopenharmony_ci{
9048c2ecf20Sopenharmony_ci	exynos_drm_unregister_dma(mixer_ctx->drm_dev, mixer_ctx->dev,
9058c2ecf20Sopenharmony_ci				  &mixer_ctx->dma_priv);
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_cistatic int mixer_enable_vblank(struct exynos_drm_crtc *crtc)
9098c2ecf20Sopenharmony_ci{
9108c2ecf20Sopenharmony_ci	struct mixer_context *mixer_ctx = crtc->ctx;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	__set_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
9138c2ecf20Sopenharmony_ci	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
9148c2ecf20Sopenharmony_ci		return 0;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	/* enable vsync interrupt */
9178c2ecf20Sopenharmony_ci	mixer_reg_writemask(mixer_ctx, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
9188c2ecf20Sopenharmony_ci	mixer_reg_writemask(mixer_ctx, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	return 0;
9218c2ecf20Sopenharmony_ci}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_cistatic void mixer_disable_vblank(struct exynos_drm_crtc *crtc)
9248c2ecf20Sopenharmony_ci{
9258c2ecf20Sopenharmony_ci	struct mixer_context *mixer_ctx = crtc->ctx;
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	__clear_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
9308c2ecf20Sopenharmony_ci		return;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	/* disable vsync interrupt */
9338c2ecf20Sopenharmony_ci	mixer_reg_writemask(mixer_ctx, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
9348c2ecf20Sopenharmony_ci	mixer_reg_writemask(mixer_ctx, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
9358c2ecf20Sopenharmony_ci}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_cistatic void mixer_atomic_begin(struct exynos_drm_crtc *crtc)
9388c2ecf20Sopenharmony_ci{
9398c2ecf20Sopenharmony_ci	struct mixer_context *ctx = crtc->ctx;
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	if (!test_bit(MXR_BIT_POWERED, &ctx->flags))
9428c2ecf20Sopenharmony_ci		return;
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	if (mixer_wait_for_sync(ctx))
9458c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "timeout waiting for VSYNC\n");
9468c2ecf20Sopenharmony_ci	mixer_disable_sync(ctx);
9478c2ecf20Sopenharmony_ci}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_cistatic void mixer_update_plane(struct exynos_drm_crtc *crtc,
9508c2ecf20Sopenharmony_ci			       struct exynos_drm_plane *plane)
9518c2ecf20Sopenharmony_ci{
9528c2ecf20Sopenharmony_ci	struct mixer_context *mixer_ctx = crtc->ctx;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	DRM_DEV_DEBUG_KMS(mixer_ctx->dev, "win: %d\n", plane->index);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
9578c2ecf20Sopenharmony_ci		return;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	if (plane->index == VP_DEFAULT_WIN)
9608c2ecf20Sopenharmony_ci		vp_video_buffer(mixer_ctx, plane);
9618c2ecf20Sopenharmony_ci	else
9628c2ecf20Sopenharmony_ci		mixer_graph_buffer(mixer_ctx, plane);
9638c2ecf20Sopenharmony_ci}
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_cistatic void mixer_disable_plane(struct exynos_drm_crtc *crtc,
9668c2ecf20Sopenharmony_ci				struct exynos_drm_plane *plane)
9678c2ecf20Sopenharmony_ci{
9688c2ecf20Sopenharmony_ci	struct mixer_context *mixer_ctx = crtc->ctx;
9698c2ecf20Sopenharmony_ci	unsigned long flags;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	DRM_DEV_DEBUG_KMS(mixer_ctx->dev, "win: %d\n", plane->index);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
9748c2ecf20Sopenharmony_ci		return;
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	spin_lock_irqsave(&mixer_ctx->reg_slock, flags);
9778c2ecf20Sopenharmony_ci	mixer_cfg_layer(mixer_ctx, plane->index, 0, false);
9788c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&mixer_ctx->reg_slock, flags);
9798c2ecf20Sopenharmony_ci}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_cistatic void mixer_atomic_flush(struct exynos_drm_crtc *crtc)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	struct mixer_context *mixer_ctx = crtc->ctx;
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
9868c2ecf20Sopenharmony_ci		return;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	mixer_enable_sync(mixer_ctx);
9898c2ecf20Sopenharmony_ci	exynos_crtc_handle_event(crtc);
9908c2ecf20Sopenharmony_ci}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_cistatic void mixer_atomic_enable(struct exynos_drm_crtc *crtc)
9938c2ecf20Sopenharmony_ci{
9948c2ecf20Sopenharmony_ci	struct mixer_context *ctx = crtc->ctx;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_POWERED, &ctx->flags))
9978c2ecf20Sopenharmony_ci		return;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	pm_runtime_get_sync(ctx->dev);
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	exynos_drm_pipe_clk_enable(crtc, true);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	mixer_disable_sync(ctx);
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	mixer_reg_writemask(ctx, MXR_STATUS, ~0, MXR_STATUS_SOFT_RESET);
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_VSYNC, &ctx->flags)) {
10088c2ecf20Sopenharmony_ci		mixer_reg_writemask(ctx, MXR_INT_STATUS, ~0,
10098c2ecf20Sopenharmony_ci					MXR_INT_CLEAR_VSYNC);
10108c2ecf20Sopenharmony_ci		mixer_reg_writemask(ctx, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
10118c2ecf20Sopenharmony_ci	}
10128c2ecf20Sopenharmony_ci	mixer_win_reset(ctx);
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	mixer_commit(ctx);
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	mixer_enable_sync(ctx);
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	set_bit(MXR_BIT_POWERED, &ctx->flags);
10198c2ecf20Sopenharmony_ci}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_cistatic void mixer_atomic_disable(struct exynos_drm_crtc *crtc)
10228c2ecf20Sopenharmony_ci{
10238c2ecf20Sopenharmony_ci	struct mixer_context *ctx = crtc->ctx;
10248c2ecf20Sopenharmony_ci	int i;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	if (!test_bit(MXR_BIT_POWERED, &ctx->flags))
10278c2ecf20Sopenharmony_ci		return;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	mixer_stop(ctx);
10308c2ecf20Sopenharmony_ci	mixer_regs_dump(ctx);
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	for (i = 0; i < MIXER_WIN_NR; i++)
10338c2ecf20Sopenharmony_ci		mixer_disable_plane(crtc, &ctx->planes[i]);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	exynos_drm_pipe_clk_enable(crtc, false);
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	pm_runtime_put(ctx->dev);
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	clear_bit(MXR_BIT_POWERED, &ctx->flags);
10408c2ecf20Sopenharmony_ci}
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_cistatic int mixer_mode_valid(struct exynos_drm_crtc *crtc,
10438c2ecf20Sopenharmony_ci		const struct drm_display_mode *mode)
10448c2ecf20Sopenharmony_ci{
10458c2ecf20Sopenharmony_ci	struct mixer_context *ctx = crtc->ctx;
10468c2ecf20Sopenharmony_ci	u32 w = mode->hdisplay, h = mode->vdisplay;
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	DRM_DEV_DEBUG_KMS(ctx->dev, "xres=%d, yres=%d, refresh=%d, intl=%d\n",
10498c2ecf20Sopenharmony_ci			  w, h, drm_mode_vrefresh(mode),
10508c2ecf20Sopenharmony_ci			  !!(mode->flags & DRM_MODE_FLAG_INTERLACE));
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	if (ctx->mxr_ver == MXR_VER_128_0_0_184)
10538c2ecf20Sopenharmony_ci		return MODE_OK;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	if ((w >= 464 && w <= 720 && h >= 261 && h <= 576) ||
10568c2ecf20Sopenharmony_ci	    (w >= 1024 && w <= 1280 && h >= 576 && h <= 720) ||
10578c2ecf20Sopenharmony_ci	    (w >= 1664 && w <= 1920 && h >= 936 && h <= 1080))
10588c2ecf20Sopenharmony_ci		return MODE_OK;
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	if ((w == 1024 && h == 768) ||
10618c2ecf20Sopenharmony_ci	    (w == 1366 && h == 768) ||
10628c2ecf20Sopenharmony_ci	    (w == 1280 && h == 1024))
10638c2ecf20Sopenharmony_ci		return MODE_OK;
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	return MODE_BAD;
10668c2ecf20Sopenharmony_ci}
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_cistatic bool mixer_mode_fixup(struct exynos_drm_crtc *crtc,
10698c2ecf20Sopenharmony_ci		   const struct drm_display_mode *mode,
10708c2ecf20Sopenharmony_ci		   struct drm_display_mode *adjusted_mode)
10718c2ecf20Sopenharmony_ci{
10728c2ecf20Sopenharmony_ci	struct mixer_context *ctx = crtc->ctx;
10738c2ecf20Sopenharmony_ci	int width = mode->hdisplay, height = mode->vdisplay, i;
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	static const struct {
10768c2ecf20Sopenharmony_ci		int hdisplay, vdisplay, htotal, vtotal, scan_val;
10778c2ecf20Sopenharmony_ci	} modes[] = {
10788c2ecf20Sopenharmony_ci		{ 720, 480, 858, 525, MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD },
10798c2ecf20Sopenharmony_ci		{ 720, 576, 864, 625, MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD },
10808c2ecf20Sopenharmony_ci		{ 1280, 720, 1650, 750, MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD },
10818c2ecf20Sopenharmony_ci		{ 1920, 1080, 2200, 1125, MXR_CFG_SCAN_HD_1080 |
10828c2ecf20Sopenharmony_ci						MXR_CFG_SCAN_HD }
10838c2ecf20Sopenharmony_ci	};
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10868c2ecf20Sopenharmony_ci		__set_bit(MXR_BIT_INTERLACE, &ctx->flags);
10878c2ecf20Sopenharmony_ci	else
10888c2ecf20Sopenharmony_ci		__clear_bit(MXR_BIT_INTERLACE, &ctx->flags);
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	if (ctx->mxr_ver == MXR_VER_128_0_0_184)
10918c2ecf20Sopenharmony_ci		return true;
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(modes); ++i)
10948c2ecf20Sopenharmony_ci		if (width <= modes[i].hdisplay && height <= modes[i].vdisplay) {
10958c2ecf20Sopenharmony_ci			ctx->scan_value = modes[i].scan_val;
10968c2ecf20Sopenharmony_ci			if (width < modes[i].hdisplay ||
10978c2ecf20Sopenharmony_ci			    height < modes[i].vdisplay) {
10988c2ecf20Sopenharmony_ci				adjusted_mode->hdisplay = modes[i].hdisplay;
10998c2ecf20Sopenharmony_ci				adjusted_mode->hsync_start = modes[i].hdisplay;
11008c2ecf20Sopenharmony_ci				adjusted_mode->hsync_end = modes[i].htotal;
11018c2ecf20Sopenharmony_ci				adjusted_mode->htotal = modes[i].htotal;
11028c2ecf20Sopenharmony_ci				adjusted_mode->vdisplay = modes[i].vdisplay;
11038c2ecf20Sopenharmony_ci				adjusted_mode->vsync_start = modes[i].vdisplay;
11048c2ecf20Sopenharmony_ci				adjusted_mode->vsync_end = modes[i].vtotal;
11058c2ecf20Sopenharmony_ci				adjusted_mode->vtotal = modes[i].vtotal;
11068c2ecf20Sopenharmony_ci			}
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci			return true;
11098c2ecf20Sopenharmony_ci		}
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	return false;
11128c2ecf20Sopenharmony_ci}
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_cistatic const struct exynos_drm_crtc_ops mixer_crtc_ops = {
11158c2ecf20Sopenharmony_ci	.atomic_enable		= mixer_atomic_enable,
11168c2ecf20Sopenharmony_ci	.atomic_disable		= mixer_atomic_disable,
11178c2ecf20Sopenharmony_ci	.enable_vblank		= mixer_enable_vblank,
11188c2ecf20Sopenharmony_ci	.disable_vblank		= mixer_disable_vblank,
11198c2ecf20Sopenharmony_ci	.atomic_begin		= mixer_atomic_begin,
11208c2ecf20Sopenharmony_ci	.update_plane		= mixer_update_plane,
11218c2ecf20Sopenharmony_ci	.disable_plane		= mixer_disable_plane,
11228c2ecf20Sopenharmony_ci	.atomic_flush		= mixer_atomic_flush,
11238c2ecf20Sopenharmony_ci	.mode_valid		= mixer_mode_valid,
11248c2ecf20Sopenharmony_ci	.mode_fixup		= mixer_mode_fixup,
11258c2ecf20Sopenharmony_ci};
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_cistatic const struct mixer_drv_data exynos5420_mxr_drv_data = {
11288c2ecf20Sopenharmony_ci	.version = MXR_VER_128_0_0_184,
11298c2ecf20Sopenharmony_ci	.is_vp_enabled = 0,
11308c2ecf20Sopenharmony_ci};
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_cistatic const struct mixer_drv_data exynos5250_mxr_drv_data = {
11338c2ecf20Sopenharmony_ci	.version = MXR_VER_16_0_33_0,
11348c2ecf20Sopenharmony_ci	.is_vp_enabled = 0,
11358c2ecf20Sopenharmony_ci};
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_cistatic const struct mixer_drv_data exynos4212_mxr_drv_data = {
11388c2ecf20Sopenharmony_ci	.version = MXR_VER_0_0_0_16,
11398c2ecf20Sopenharmony_ci	.is_vp_enabled = 1,
11408c2ecf20Sopenharmony_ci};
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_cistatic const struct mixer_drv_data exynos4210_mxr_drv_data = {
11438c2ecf20Sopenharmony_ci	.version = MXR_VER_0_0_0_16,
11448c2ecf20Sopenharmony_ci	.is_vp_enabled = 1,
11458c2ecf20Sopenharmony_ci	.has_sclk = 1,
11468c2ecf20Sopenharmony_ci};
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_cistatic const struct of_device_id mixer_match_types[] = {
11498c2ecf20Sopenharmony_ci	{
11508c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos4210-mixer",
11518c2ecf20Sopenharmony_ci		.data	= &exynos4210_mxr_drv_data,
11528c2ecf20Sopenharmony_ci	}, {
11538c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos4212-mixer",
11548c2ecf20Sopenharmony_ci		.data	= &exynos4212_mxr_drv_data,
11558c2ecf20Sopenharmony_ci	}, {
11568c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos5-mixer",
11578c2ecf20Sopenharmony_ci		.data	= &exynos5250_mxr_drv_data,
11588c2ecf20Sopenharmony_ci	}, {
11598c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos5250-mixer",
11608c2ecf20Sopenharmony_ci		.data	= &exynos5250_mxr_drv_data,
11618c2ecf20Sopenharmony_ci	}, {
11628c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos5420-mixer",
11638c2ecf20Sopenharmony_ci		.data	= &exynos5420_mxr_drv_data,
11648c2ecf20Sopenharmony_ci	}, {
11658c2ecf20Sopenharmony_ci		/* end node */
11668c2ecf20Sopenharmony_ci	}
11678c2ecf20Sopenharmony_ci};
11688c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mixer_match_types);
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_cistatic int mixer_bind(struct device *dev, struct device *manager, void *data)
11718c2ecf20Sopenharmony_ci{
11728c2ecf20Sopenharmony_ci	struct mixer_context *ctx = dev_get_drvdata(dev);
11738c2ecf20Sopenharmony_ci	struct drm_device *drm_dev = data;
11748c2ecf20Sopenharmony_ci	struct exynos_drm_plane *exynos_plane;
11758c2ecf20Sopenharmony_ci	unsigned int i;
11768c2ecf20Sopenharmony_ci	int ret;
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci	ret = mixer_initialize(ctx, drm_dev);
11798c2ecf20Sopenharmony_ci	if (ret)
11808c2ecf20Sopenharmony_ci		return ret;
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	for (i = 0; i < MIXER_WIN_NR; i++) {
11838c2ecf20Sopenharmony_ci		if (i == VP_DEFAULT_WIN && !test_bit(MXR_BIT_VP_ENABLED,
11848c2ecf20Sopenharmony_ci						     &ctx->flags))
11858c2ecf20Sopenharmony_ci			continue;
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci		ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
11888c2ecf20Sopenharmony_ci					&plane_configs[i]);
11898c2ecf20Sopenharmony_ci		if (ret)
11908c2ecf20Sopenharmony_ci			return ret;
11918c2ecf20Sopenharmony_ci	}
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	exynos_plane = &ctx->planes[DEFAULT_WIN];
11948c2ecf20Sopenharmony_ci	ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
11958c2ecf20Sopenharmony_ci			EXYNOS_DISPLAY_TYPE_HDMI, &mixer_crtc_ops, ctx);
11968c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->crtc)) {
11978c2ecf20Sopenharmony_ci		mixer_ctx_remove(ctx);
11988c2ecf20Sopenharmony_ci		ret = PTR_ERR(ctx->crtc);
11998c2ecf20Sopenharmony_ci		goto free_ctx;
12008c2ecf20Sopenharmony_ci	}
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	return 0;
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cifree_ctx:
12058c2ecf20Sopenharmony_ci	devm_kfree(dev, ctx);
12068c2ecf20Sopenharmony_ci	return ret;
12078c2ecf20Sopenharmony_ci}
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_cistatic void mixer_unbind(struct device *dev, struct device *master, void *data)
12108c2ecf20Sopenharmony_ci{
12118c2ecf20Sopenharmony_ci	struct mixer_context *ctx = dev_get_drvdata(dev);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	mixer_ctx_remove(ctx);
12148c2ecf20Sopenharmony_ci}
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_cistatic const struct component_ops mixer_component_ops = {
12178c2ecf20Sopenharmony_ci	.bind	= mixer_bind,
12188c2ecf20Sopenharmony_ci	.unbind	= mixer_unbind,
12198c2ecf20Sopenharmony_ci};
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_cistatic int mixer_probe(struct platform_device *pdev)
12228c2ecf20Sopenharmony_ci{
12238c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
12248c2ecf20Sopenharmony_ci	const struct mixer_drv_data *drv;
12258c2ecf20Sopenharmony_ci	struct mixer_context *ctx;
12268c2ecf20Sopenharmony_ci	int ret;
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
12298c2ecf20Sopenharmony_ci	if (!ctx) {
12308c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(dev, "failed to alloc mixer context.\n");
12318c2ecf20Sopenharmony_ci		return -ENOMEM;
12328c2ecf20Sopenharmony_ci	}
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	drv = of_device_get_match_data(dev);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	ctx->pdev = pdev;
12378c2ecf20Sopenharmony_ci	ctx->dev = dev;
12388c2ecf20Sopenharmony_ci	ctx->mxr_ver = drv->version;
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	if (drv->is_vp_enabled)
12418c2ecf20Sopenharmony_ci		__set_bit(MXR_BIT_VP_ENABLED, &ctx->flags);
12428c2ecf20Sopenharmony_ci	if (drv->has_sclk)
12438c2ecf20Sopenharmony_ci		__set_bit(MXR_BIT_HAS_SCLK, &ctx->flags);
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ctx);
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	pm_runtime_enable(dev);
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	ret = component_add(&pdev->dev, &mixer_component_ops);
12508c2ecf20Sopenharmony_ci	if (ret)
12518c2ecf20Sopenharmony_ci		pm_runtime_disable(dev);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	return ret;
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_cistatic int mixer_remove(struct platform_device *pdev)
12578c2ecf20Sopenharmony_ci{
12588c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	component_del(&pdev->dev, &mixer_component_ops);
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	return 0;
12638c2ecf20Sopenharmony_ci}
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_cistatic int __maybe_unused exynos_mixer_suspend(struct device *dev)
12668c2ecf20Sopenharmony_ci{
12678c2ecf20Sopenharmony_ci	struct mixer_context *ctx = dev_get_drvdata(dev);
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	clk_disable_unprepare(ctx->hdmi);
12708c2ecf20Sopenharmony_ci	clk_disable_unprepare(ctx->mixer);
12718c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
12728c2ecf20Sopenharmony_ci		clk_disable_unprepare(ctx->vp);
12738c2ecf20Sopenharmony_ci		if (test_bit(MXR_BIT_HAS_SCLK, &ctx->flags))
12748c2ecf20Sopenharmony_ci			clk_disable_unprepare(ctx->sclk_mixer);
12758c2ecf20Sopenharmony_ci	}
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	return 0;
12788c2ecf20Sopenharmony_ci}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_cistatic int __maybe_unused exynos_mixer_resume(struct device *dev)
12818c2ecf20Sopenharmony_ci{
12828c2ecf20Sopenharmony_ci	struct mixer_context *ctx = dev_get_drvdata(dev);
12838c2ecf20Sopenharmony_ci	int ret;
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(ctx->mixer);
12868c2ecf20Sopenharmony_ci	if (ret < 0) {
12878c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(ctx->dev,
12888c2ecf20Sopenharmony_ci			      "Failed to prepare_enable the mixer clk [%d]\n",
12898c2ecf20Sopenharmony_ci			      ret);
12908c2ecf20Sopenharmony_ci		return ret;
12918c2ecf20Sopenharmony_ci	}
12928c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(ctx->hdmi);
12938c2ecf20Sopenharmony_ci	if (ret < 0) {
12948c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(dev,
12958c2ecf20Sopenharmony_ci			      "Failed to prepare_enable the hdmi clk [%d]\n",
12968c2ecf20Sopenharmony_ci			      ret);
12978c2ecf20Sopenharmony_ci		return ret;
12988c2ecf20Sopenharmony_ci	}
12998c2ecf20Sopenharmony_ci	if (test_bit(MXR_BIT_VP_ENABLED, &ctx->flags)) {
13008c2ecf20Sopenharmony_ci		ret = clk_prepare_enable(ctx->vp);
13018c2ecf20Sopenharmony_ci		if (ret < 0) {
13028c2ecf20Sopenharmony_ci			DRM_DEV_ERROR(dev,
13038c2ecf20Sopenharmony_ci				      "Failed to prepare_enable the vp clk [%d]\n",
13048c2ecf20Sopenharmony_ci				      ret);
13058c2ecf20Sopenharmony_ci			return ret;
13068c2ecf20Sopenharmony_ci		}
13078c2ecf20Sopenharmony_ci		if (test_bit(MXR_BIT_HAS_SCLK, &ctx->flags)) {
13088c2ecf20Sopenharmony_ci			ret = clk_prepare_enable(ctx->sclk_mixer);
13098c2ecf20Sopenharmony_ci			if (ret < 0) {
13108c2ecf20Sopenharmony_ci				DRM_DEV_ERROR(dev,
13118c2ecf20Sopenharmony_ci					   "Failed to prepare_enable the " \
13128c2ecf20Sopenharmony_ci					   "sclk_mixer clk [%d]\n",
13138c2ecf20Sopenharmony_ci					   ret);
13148c2ecf20Sopenharmony_ci				return ret;
13158c2ecf20Sopenharmony_ci			}
13168c2ecf20Sopenharmony_ci		}
13178c2ecf20Sopenharmony_ci	}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	return 0;
13208c2ecf20Sopenharmony_ci}
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_cistatic const struct dev_pm_ops exynos_mixer_pm_ops = {
13238c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(exynos_mixer_suspend, exynos_mixer_resume, NULL)
13248c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
13258c2ecf20Sopenharmony_ci				pm_runtime_force_resume)
13268c2ecf20Sopenharmony_ci};
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_cistruct platform_driver mixer_driver = {
13298c2ecf20Sopenharmony_ci	.driver = {
13308c2ecf20Sopenharmony_ci		.name = "exynos-mixer",
13318c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,
13328c2ecf20Sopenharmony_ci		.pm = &exynos_mixer_pm_ops,
13338c2ecf20Sopenharmony_ci		.of_match_table = mixer_match_types,
13348c2ecf20Sopenharmony_ci	},
13358c2ecf20Sopenharmony_ci	.probe = mixer_probe,
13368c2ecf20Sopenharmony_ci	.remove = mixer_remove,
13378c2ecf20Sopenharmony_ci};
1338