18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * camss-vfe-4-1.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Qualcomm MSM Camera Subsystem - VFE (Video Front End) Module v4.1
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
88c2ecf20Sopenharmony_ci * Copyright (C) 2015-2018 Linaro Ltd.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/io.h>
138c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "camss-vfe.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define VFE_0_HW_VERSION		0x000
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD		0x00c
208c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_CORE	BIT(0)
218c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_CAMIF	BIT(1)
228c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_BUS	BIT(2)
238c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_BUS_BDG	BIT(3)
248c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_REGISTER	BIT(4)
258c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_TIMER	BIT(5)
268c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_PM	BIT(6)
278c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_BUS_MISR	BIT(7)
288c2ecf20Sopenharmony_ci#define VFE_0_GLOBAL_RESET_CMD_TESTGEN	BIT(8)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define VFE_0_MODULE_CFG		0x018
318c2ecf20Sopenharmony_ci#define VFE_0_MODULE_CFG_DEMUX			BIT(2)
328c2ecf20Sopenharmony_ci#define VFE_0_MODULE_CFG_CHROMA_UPSAMPLE	BIT(3)
338c2ecf20Sopenharmony_ci#define VFE_0_MODULE_CFG_SCALE_ENC		BIT(23)
348c2ecf20Sopenharmony_ci#define VFE_0_MODULE_CFG_CROP_ENC		BIT(27)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define VFE_0_CORE_CFG			0x01c
378c2ecf20Sopenharmony_ci#define VFE_0_CORE_CFG_PIXEL_PATTERN_YCBYCR	0x4
388c2ecf20Sopenharmony_ci#define VFE_0_CORE_CFG_PIXEL_PATTERN_YCRYCB	0x5
398c2ecf20Sopenharmony_ci#define VFE_0_CORE_CFG_PIXEL_PATTERN_CBYCRY	0x6
408c2ecf20Sopenharmony_ci#define VFE_0_CORE_CFG_PIXEL_PATTERN_CRYCBY	0x7
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define VFE_0_IRQ_CMD			0x024
438c2ecf20Sopenharmony_ci#define VFE_0_IRQ_CMD_GLOBAL_CLEAR	BIT(0)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_0		0x028
468c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_0_CAMIF_SOF			BIT(0)
478c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_0_CAMIF_EOF			BIT(1)
488c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_0_RDIn_REG_UPDATE(n)		BIT((n) + 5)
498c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_0_line_n_REG_UPDATE(n)		\
508c2ecf20Sopenharmony_ci	((n) == VFE_LINE_PIX ? BIT(4) : VFE_0_IRQ_MASK_0_RDIn_REG_UPDATE(n))
518c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_0_IMAGE_MASTER_n_PING_PONG(n)	BIT((n) + 8)
528c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_0_IMAGE_COMPOSITE_DONE_n(n)	BIT((n) + 25)
538c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_0_RESET_ACK			BIT(31)
548c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_1		0x02c
558c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_1_CAMIF_ERROR			BIT(0)
568c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_1_VIOLATION			BIT(7)
578c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_1_BUS_BDG_HALT_ACK		BIT(8)
588c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_1_IMAGE_MASTER_n_BUS_OVERFLOW(n)	BIT((n) + 9)
598c2ecf20Sopenharmony_ci#define VFE_0_IRQ_MASK_1_RDIn_SOF(n)			BIT((n) + 29)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define VFE_0_IRQ_CLEAR_0		0x030
628c2ecf20Sopenharmony_ci#define VFE_0_IRQ_CLEAR_1		0x034
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_0		0x038
658c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_0_CAMIF_SOF			BIT(0)
668c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_0_RDIn_REG_UPDATE(n)		BIT((n) + 5)
678c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_0_line_n_REG_UPDATE(n)		\
688c2ecf20Sopenharmony_ci	((n) == VFE_LINE_PIX ? BIT(4) : VFE_0_IRQ_STATUS_0_RDIn_REG_UPDATE(n))
698c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_0_IMAGE_MASTER_n_PING_PONG(n)	BIT((n) + 8)
708c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_0_IMAGE_COMPOSITE_DONE_n(n)	BIT((n) + 25)
718c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_0_RESET_ACK			BIT(31)
728c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_1		0x03c
738c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_1_VIOLATION			BIT(7)
748c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_1_BUS_BDG_HALT_ACK		BIT(8)
758c2ecf20Sopenharmony_ci#define VFE_0_IRQ_STATUS_1_RDIn_SOF(n)			BIT((n) + 29)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define VFE_0_IRQ_COMPOSITE_MASK_0	0x40
788c2ecf20Sopenharmony_ci#define VFE_0_VIOLATION_STATUS		0x48
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define VFE_0_BUS_CMD			0x4c
818c2ecf20Sopenharmony_ci#define VFE_0_BUS_CMD_Mx_RLD_CMD(x)	BIT(x)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define VFE_0_BUS_CFG			0x050
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define VFE_0_BUS_XBAR_CFG_x(x)		(0x58 + 0x4 * ((x) / 2))
868c2ecf20Sopenharmony_ci#define VFE_0_BUS_XBAR_CFG_x_M_PAIR_STREAM_EN			BIT(1)
878c2ecf20Sopenharmony_ci#define VFE_0_BUS_XBAR_CFG_x_M_PAIR_STREAM_SWAP_INTER_INTRA	(0x3 << 4)
888c2ecf20Sopenharmony_ci#define VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_SHIFT		8
898c2ecf20Sopenharmony_ci#define VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_LUMA		0
908c2ecf20Sopenharmony_ci#define VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI0	5
918c2ecf20Sopenharmony_ci#define VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI1	6
928c2ecf20Sopenharmony_ci#define VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI2	7
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_CFG(n)		(0x06c + 0x24 * (n))
958c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_CFG_WR_PATH_SHIFT	0
968c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_CFG_FRM_BASED_SHIFT	1
978c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_PING_ADDR(n)	(0x070 + 0x24 * (n))
988c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_PONG_ADDR(n)	(0x074 + 0x24 * (n))
998c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_ADDR_CFG(n)		(0x078 + 0x24 * (n))
1008c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_ADDR_CFG_FRM_DROP_PER_SHIFT	2
1018c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_ADDR_CFG_FRM_DROP_PER_MASK	(0x1f << 2)
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_UB_CFG(n)		(0x07c + 0x24 * (n))
1048c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_UB_CFG_OFFSET_SHIFT	16
1058c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_IMAGE_SIZE(n)	(0x080 + 0x24 * (n))
1068c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_BUFFER_CFG(n)	(0x084 + 0x24 * (n))
1078c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_FRAMEDROP_PATTERN(n)	\
1088c2ecf20Sopenharmony_ci							(0x088 + 0x24 * (n))
1098c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_IRQ_SUBSAMPLE_PATTERN(n)	\
1108c2ecf20Sopenharmony_ci							(0x08c + 0x24 * (n))
1118c2ecf20Sopenharmony_ci#define VFE_0_BUS_IMAGE_MASTER_n_WR_IRQ_SUBSAMPLE_PATTERN_DEF	0xffffffff
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define VFE_0_BUS_PING_PONG_STATUS	0x268
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_CMD		0x2c0
1168c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_CMD_HALT_REQ	1
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_0		0x2c4
1198c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_0_CFG	0xaaa5aaa5
1208c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_1		0x2c8
1218c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_2		0x2cc
1228c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_3		0x2d0
1238c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_4		0x2d4
1248c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_5		0x2d8
1258c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_6		0x2dc
1268c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_7		0x2e0
1278c2ecf20Sopenharmony_ci#define VFE_0_BUS_BDG_QOS_CFG_7_CFG	0x0001aaa5
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#define VFE_0_RDI_CFG_x(x)		(0x2e8 + (0x4 * (x)))
1308c2ecf20Sopenharmony_ci#define VFE_0_RDI_CFG_x_RDI_STREAM_SEL_SHIFT	28
1318c2ecf20Sopenharmony_ci#define VFE_0_RDI_CFG_x_RDI_STREAM_SEL_MASK	(0xf << 28)
1328c2ecf20Sopenharmony_ci#define VFE_0_RDI_CFG_x_RDI_M0_SEL_SHIFT	4
1338c2ecf20Sopenharmony_ci#define VFE_0_RDI_CFG_x_RDI_M0_SEL_MASK		(0xf << 4)
1348c2ecf20Sopenharmony_ci#define VFE_0_RDI_CFG_x_RDI_EN_BIT		BIT(2)
1358c2ecf20Sopenharmony_ci#define VFE_0_RDI_CFG_x_MIPI_EN_BITS		0x3
1368c2ecf20Sopenharmony_ci#define VFE_0_RDI_CFG_x_RDI_Mr_FRAME_BASED_EN(r)	BIT(16 + (r))
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_CMD				0x2f4
1398c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_CMD_DISABLE_FRAME_BOUNDARY	0
1408c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_CMD_ENABLE_FRAME_BOUNDARY	1
1418c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_CMD_NO_CHANGE		3
1428c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_CMD_CLEAR_CAMIF_STATUS	BIT(2)
1438c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_CFG				0x2f8
1448c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_CFG_VFE_OUTPUT_EN		BIT(6)
1458c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_FRAME_CFG			0x300
1468c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_WINDOW_WIDTH_CFG		0x304
1478c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_WINDOW_HEIGHT_CFG		0x308
1488c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_SUBSAMPLE_CFG_0		0x30c
1498c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_IRQ_SUBSAMPLE_PATTERN	0x314
1508c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_STATUS			0x31c
1518c2ecf20Sopenharmony_ci#define VFE_0_CAMIF_STATUS_HALT			BIT(31)
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci#define VFE_0_REG_UPDATE			0x378
1548c2ecf20Sopenharmony_ci#define VFE_0_REG_UPDATE_RDIn(n)		BIT(1 + (n))
1558c2ecf20Sopenharmony_ci#define VFE_0_REG_UPDATE_line_n(n)		\
1568c2ecf20Sopenharmony_ci			((n) == VFE_LINE_PIX ? 1 : VFE_0_REG_UPDATE_RDIn(n))
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_CFG				0x424
1598c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_CFG_PERIOD			0x3
1608c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_GAIN_0			0x428
1618c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_GAIN_0_CH0_EVEN		(0x80 << 0)
1628c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_GAIN_0_CH0_ODD		(0x80 << 16)
1638c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_GAIN_1			0x42c
1648c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_GAIN_1_CH1			(0x80 << 0)
1658c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_GAIN_1_CH2			(0x80 << 16)
1668c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_EVEN_CFG			0x438
1678c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_EVEN_CFG_PATTERN_YUYV	0x9cac
1688c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_EVEN_CFG_PATTERN_YVYU	0xac9c
1698c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_EVEN_CFG_PATTERN_UYVY	0xc9ca
1708c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_EVEN_CFG_PATTERN_VYUY	0xcac9
1718c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_ODD_CFG			0x43c
1728c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_ODD_CFG_PATTERN_YUYV	0x9cac
1738c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_ODD_CFG_PATTERN_YVYU	0xac9c
1748c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_ODD_CFG_PATTERN_UYVY	0xc9ca
1758c2ecf20Sopenharmony_ci#define VFE_0_DEMUX_ODD_CFG_PATTERN_VYUY	0xcac9
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_Y_CFG			0x75c
1788c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_Y_H_IMAGE_SIZE		0x760
1798c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_Y_H_PHASE		0x764
1808c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_Y_V_IMAGE_SIZE		0x76c
1818c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_Y_V_PHASE		0x770
1828c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_CBCR_CFG		0x778
1838c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_CBCR_H_IMAGE_SIZE	0x77c
1848c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_CBCR_H_PHASE		0x780
1858c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_CBCR_V_IMAGE_SIZE	0x790
1868c2ecf20Sopenharmony_ci#define VFE_0_SCALE_ENC_CBCR_V_PHASE		0x794
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci#define VFE_0_CROP_ENC_Y_WIDTH			0x854
1898c2ecf20Sopenharmony_ci#define VFE_0_CROP_ENC_Y_HEIGHT			0x858
1908c2ecf20Sopenharmony_ci#define VFE_0_CROP_ENC_CBCR_WIDTH		0x85c
1918c2ecf20Sopenharmony_ci#define VFE_0_CROP_ENC_CBCR_HEIGHT		0x860
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#define VFE_0_CLAMP_ENC_MAX_CFG			0x874
1948c2ecf20Sopenharmony_ci#define VFE_0_CLAMP_ENC_MAX_CFG_CH0		(0xff << 0)
1958c2ecf20Sopenharmony_ci#define VFE_0_CLAMP_ENC_MAX_CFG_CH1		(0xff << 8)
1968c2ecf20Sopenharmony_ci#define VFE_0_CLAMP_ENC_MAX_CFG_CH2		(0xff << 16)
1978c2ecf20Sopenharmony_ci#define VFE_0_CLAMP_ENC_MIN_CFG			0x878
1988c2ecf20Sopenharmony_ci#define VFE_0_CLAMP_ENC_MIN_CFG_CH0		(0x0 << 0)
1998c2ecf20Sopenharmony_ci#define VFE_0_CLAMP_ENC_MIN_CFG_CH1		(0x0 << 8)
2008c2ecf20Sopenharmony_ci#define VFE_0_CLAMP_ENC_MIN_CFG_CH2		(0x0 << 16)
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci#define VFE_0_CGC_OVERRIDE_1			0x974
2038c2ecf20Sopenharmony_ci#define VFE_0_CGC_OVERRIDE_1_IMAGE_Mx_CGC_OVERRIDE(x)	BIT(x)
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci#define CAMIF_TIMEOUT_SLEEP_US 1000
2068c2ecf20Sopenharmony_ci#define CAMIF_TIMEOUT_ALL_US 1000000
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci#define MSM_VFE_VFE0_UB_SIZE 1023
2098c2ecf20Sopenharmony_ci#define MSM_VFE_VFE0_UB_SIZE_RDI (MSM_VFE_VFE0_UB_SIZE / 3)
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic void vfe_hw_version_read(struct vfe_device *vfe, struct device *dev)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	u32 hw_version = readl_relaxed(vfe->base + VFE_0_HW_VERSION);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	dev_dbg(dev, "VFE HW Version = 0x%08x\n", hw_version);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic u16 vfe_get_ub_size(u8 vfe_id)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	if (vfe_id == 0)
2218c2ecf20Sopenharmony_ci		return MSM_VFE_VFE0_UB_SIZE_RDI;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return 0;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic inline void vfe_reg_clr(struct vfe_device *vfe, u32 reg, u32 clr_bits)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	u32 bits = readl_relaxed(vfe->base + reg);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	writel_relaxed(bits & ~clr_bits, vfe->base + reg);
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic inline void vfe_reg_set(struct vfe_device *vfe, u32 reg, u32 set_bits)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	u32 bits = readl_relaxed(vfe->base + reg);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	writel_relaxed(bits | set_bits, vfe->base + reg);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic void vfe_global_reset(struct vfe_device *vfe)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	u32 reset_bits = VFE_0_GLOBAL_RESET_CMD_TESTGEN		|
2438c2ecf20Sopenharmony_ci			 VFE_0_GLOBAL_RESET_CMD_BUS_MISR	|
2448c2ecf20Sopenharmony_ci			 VFE_0_GLOBAL_RESET_CMD_PM		|
2458c2ecf20Sopenharmony_ci			 VFE_0_GLOBAL_RESET_CMD_TIMER		|
2468c2ecf20Sopenharmony_ci			 VFE_0_GLOBAL_RESET_CMD_REGISTER	|
2478c2ecf20Sopenharmony_ci			 VFE_0_GLOBAL_RESET_CMD_BUS_BDG		|
2488c2ecf20Sopenharmony_ci			 VFE_0_GLOBAL_RESET_CMD_BUS		|
2498c2ecf20Sopenharmony_ci			 VFE_0_GLOBAL_RESET_CMD_CAMIF		|
2508c2ecf20Sopenharmony_ci			 VFE_0_GLOBAL_RESET_CMD_CORE;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	writel_relaxed(reset_bits, vfe->base + VFE_0_GLOBAL_RESET_CMD);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic void vfe_halt_request(struct vfe_device *vfe)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	writel_relaxed(VFE_0_BUS_BDG_CMD_HALT_REQ,
2588c2ecf20Sopenharmony_ci		       vfe->base + VFE_0_BUS_BDG_CMD);
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic void vfe_halt_clear(struct vfe_device *vfe)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	writel_relaxed(0x0, vfe->base + VFE_0_BUS_BDG_CMD);
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic void vfe_wm_enable(struct vfe_device *vfe, u8 wm, u8 enable)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	if (enable)
2698c2ecf20Sopenharmony_ci		vfe_reg_set(vfe, VFE_0_BUS_IMAGE_MASTER_n_WR_CFG(wm),
2708c2ecf20Sopenharmony_ci			    1 << VFE_0_BUS_IMAGE_MASTER_n_WR_CFG_WR_PATH_SHIFT);
2718c2ecf20Sopenharmony_ci	else
2728c2ecf20Sopenharmony_ci		vfe_reg_clr(vfe, VFE_0_BUS_IMAGE_MASTER_n_WR_CFG(wm),
2738c2ecf20Sopenharmony_ci			    1 << VFE_0_BUS_IMAGE_MASTER_n_WR_CFG_WR_PATH_SHIFT);
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic void vfe_wm_frame_based(struct vfe_device *vfe, u8 wm, u8 enable)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	if (enable)
2798c2ecf20Sopenharmony_ci		vfe_reg_set(vfe, VFE_0_BUS_IMAGE_MASTER_n_WR_CFG(wm),
2808c2ecf20Sopenharmony_ci			1 << VFE_0_BUS_IMAGE_MASTER_n_WR_CFG_FRM_BASED_SHIFT);
2818c2ecf20Sopenharmony_ci	else
2828c2ecf20Sopenharmony_ci		vfe_reg_clr(vfe, VFE_0_BUS_IMAGE_MASTER_n_WR_CFG(wm),
2838c2ecf20Sopenharmony_ci			1 << VFE_0_BUS_IMAGE_MASTER_n_WR_CFG_FRM_BASED_SHIFT);
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci#define CALC_WORD(width, M, N) (((width) * (M) + (N) - 1) / (N))
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic int vfe_word_per_line(u32 format, u32 pixel_per_line)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	int val = 0;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	switch (format) {
2938c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
2948c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
2958c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
2968c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV61:
2978c2ecf20Sopenharmony_ci		val = CALC_WORD(pixel_per_line, 1, 8);
2988c2ecf20Sopenharmony_ci		break;
2998c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUYV:
3008c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YVYU:
3018c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_UYVY:
3028c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_VYUY:
3038c2ecf20Sopenharmony_ci		val = CALC_WORD(pixel_per_line, 2, 8);
3048c2ecf20Sopenharmony_ci		break;
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return val;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic void vfe_get_wm_sizes(struct v4l2_pix_format_mplane *pix, u8 plane,
3118c2ecf20Sopenharmony_ci			     u16 *width, u16 *height, u16 *bytesperline)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	switch (pix->pixelformat) {
3148c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV12:
3158c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV21:
3168c2ecf20Sopenharmony_ci		*width = pix->width;
3178c2ecf20Sopenharmony_ci		*height = pix->height;
3188c2ecf20Sopenharmony_ci		*bytesperline = pix->plane_fmt[0].bytesperline;
3198c2ecf20Sopenharmony_ci		if (plane == 1)
3208c2ecf20Sopenharmony_ci			*height /= 2;
3218c2ecf20Sopenharmony_ci		break;
3228c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV16:
3238c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_NV61:
3248c2ecf20Sopenharmony_ci		*width = pix->width;
3258c2ecf20Sopenharmony_ci		*height = pix->height;
3268c2ecf20Sopenharmony_ci		*bytesperline = pix->plane_fmt[0].bytesperline;
3278c2ecf20Sopenharmony_ci		break;
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_cistatic void vfe_wm_line_based(struct vfe_device *vfe, u32 wm,
3328c2ecf20Sopenharmony_ci			      struct v4l2_pix_format_mplane *pix,
3338c2ecf20Sopenharmony_ci			      u8 plane, u32 enable)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	u32 reg;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	if (enable) {
3388c2ecf20Sopenharmony_ci		u16 width = 0, height = 0, bytesperline = 0, wpl;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci		vfe_get_wm_sizes(pix, plane, &width, &height, &bytesperline);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci		wpl = vfe_word_per_line(pix->pixelformat, width);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci		reg = height - 1;
3458c2ecf20Sopenharmony_ci		reg |= ((wpl + 1) / 2 - 1) << 16;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci		writel_relaxed(reg, vfe->base +
3488c2ecf20Sopenharmony_ci			       VFE_0_BUS_IMAGE_MASTER_n_WR_IMAGE_SIZE(wm));
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci		wpl = vfe_word_per_line(pix->pixelformat, bytesperline);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci		reg = 0x3;
3538c2ecf20Sopenharmony_ci		reg |= (height - 1) << 4;
3548c2ecf20Sopenharmony_ci		reg |= wpl << 16;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci		writel_relaxed(reg, vfe->base +
3578c2ecf20Sopenharmony_ci			       VFE_0_BUS_IMAGE_MASTER_n_WR_BUFFER_CFG(wm));
3588c2ecf20Sopenharmony_ci	} else {
3598c2ecf20Sopenharmony_ci		writel_relaxed(0, vfe->base +
3608c2ecf20Sopenharmony_ci			       VFE_0_BUS_IMAGE_MASTER_n_WR_IMAGE_SIZE(wm));
3618c2ecf20Sopenharmony_ci		writel_relaxed(0, vfe->base +
3628c2ecf20Sopenharmony_ci			       VFE_0_BUS_IMAGE_MASTER_n_WR_BUFFER_CFG(wm));
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic void vfe_wm_set_framedrop_period(struct vfe_device *vfe, u8 wm, u8 per)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	u32 reg;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	reg = readl_relaxed(vfe->base +
3718c2ecf20Sopenharmony_ci			    VFE_0_BUS_IMAGE_MASTER_n_WR_ADDR_CFG(wm));
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	reg &= ~(VFE_0_BUS_IMAGE_MASTER_n_WR_ADDR_CFG_FRM_DROP_PER_MASK);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	reg |= (per << VFE_0_BUS_IMAGE_MASTER_n_WR_ADDR_CFG_FRM_DROP_PER_SHIFT)
3768c2ecf20Sopenharmony_ci		& VFE_0_BUS_IMAGE_MASTER_n_WR_ADDR_CFG_FRM_DROP_PER_MASK;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	writel_relaxed(reg,
3798c2ecf20Sopenharmony_ci		       vfe->base + VFE_0_BUS_IMAGE_MASTER_n_WR_ADDR_CFG(wm));
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic void vfe_wm_set_framedrop_pattern(struct vfe_device *vfe, u8 wm,
3838c2ecf20Sopenharmony_ci					 u32 pattern)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	writel_relaxed(pattern,
3868c2ecf20Sopenharmony_ci	       vfe->base + VFE_0_BUS_IMAGE_MASTER_n_WR_FRAMEDROP_PATTERN(wm));
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic void vfe_wm_set_ub_cfg(struct vfe_device *vfe, u8 wm,
3908c2ecf20Sopenharmony_ci			      u16 offset, u16 depth)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	u32 reg;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	reg = (offset << VFE_0_BUS_IMAGE_MASTER_n_WR_UB_CFG_OFFSET_SHIFT) |
3958c2ecf20Sopenharmony_ci		depth;
3968c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_BUS_IMAGE_MASTER_n_WR_UB_CFG(wm));
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic void vfe_bus_reload_wm(struct vfe_device *vfe, u8 wm)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	wmb();
4028c2ecf20Sopenharmony_ci	writel_relaxed(VFE_0_BUS_CMD_Mx_RLD_CMD(wm), vfe->base + VFE_0_BUS_CMD);
4038c2ecf20Sopenharmony_ci	wmb();
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic void vfe_wm_set_ping_addr(struct vfe_device *vfe, u8 wm, u32 addr)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	writel_relaxed(addr,
4098c2ecf20Sopenharmony_ci		       vfe->base + VFE_0_BUS_IMAGE_MASTER_n_WR_PING_ADDR(wm));
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic void vfe_wm_set_pong_addr(struct vfe_device *vfe, u8 wm, u32 addr)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	writel_relaxed(addr,
4158c2ecf20Sopenharmony_ci		       vfe->base + VFE_0_BUS_IMAGE_MASTER_n_WR_PONG_ADDR(wm));
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic int vfe_wm_get_ping_pong_status(struct vfe_device *vfe, u8 wm)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	u32 reg;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	reg = readl_relaxed(vfe->base + VFE_0_BUS_PING_PONG_STATUS);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return (reg >> wm) & 0x1;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic void vfe_bus_enable_wr_if(struct vfe_device *vfe, u8 enable)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	if (enable)
4308c2ecf20Sopenharmony_ci		writel_relaxed(0x10000009, vfe->base + VFE_0_BUS_CFG);
4318c2ecf20Sopenharmony_ci	else
4328c2ecf20Sopenharmony_ci		writel_relaxed(0, vfe->base + VFE_0_BUS_CFG);
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic void vfe_bus_connect_wm_to_rdi(struct vfe_device *vfe, u8 wm,
4368c2ecf20Sopenharmony_ci				      enum vfe_line_id id)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	u32 reg;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	reg = VFE_0_RDI_CFG_x_MIPI_EN_BITS;
4418c2ecf20Sopenharmony_ci	reg |= VFE_0_RDI_CFG_x_RDI_Mr_FRAME_BASED_EN(id);
4428c2ecf20Sopenharmony_ci	vfe_reg_set(vfe, VFE_0_RDI_CFG_x(0), reg);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	reg = VFE_0_RDI_CFG_x_RDI_EN_BIT;
4458c2ecf20Sopenharmony_ci	reg |= ((3 * id) << VFE_0_RDI_CFG_x_RDI_STREAM_SEL_SHIFT) &
4468c2ecf20Sopenharmony_ci		VFE_0_RDI_CFG_x_RDI_STREAM_SEL_MASK;
4478c2ecf20Sopenharmony_ci	vfe_reg_set(vfe, VFE_0_RDI_CFG_x(id), reg);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	switch (id) {
4508c2ecf20Sopenharmony_ci	case VFE_LINE_RDI0:
4518c2ecf20Sopenharmony_ci	default:
4528c2ecf20Sopenharmony_ci		reg = VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI0 <<
4538c2ecf20Sopenharmony_ci		      VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_SHIFT;
4548c2ecf20Sopenharmony_ci		break;
4558c2ecf20Sopenharmony_ci	case VFE_LINE_RDI1:
4568c2ecf20Sopenharmony_ci		reg = VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI1 <<
4578c2ecf20Sopenharmony_ci		      VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_SHIFT;
4588c2ecf20Sopenharmony_ci		break;
4598c2ecf20Sopenharmony_ci	case VFE_LINE_RDI2:
4608c2ecf20Sopenharmony_ci		reg = VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI2 <<
4618c2ecf20Sopenharmony_ci		      VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_SHIFT;
4628c2ecf20Sopenharmony_ci		break;
4638c2ecf20Sopenharmony_ci	}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	if (wm % 2 == 1)
4668c2ecf20Sopenharmony_ci		reg <<= 16;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	vfe_reg_set(vfe, VFE_0_BUS_XBAR_CFG_x(wm), reg);
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_cistatic void vfe_wm_set_subsample(struct vfe_device *vfe, u8 wm)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	writel_relaxed(VFE_0_BUS_IMAGE_MASTER_n_WR_IRQ_SUBSAMPLE_PATTERN_DEF,
4748c2ecf20Sopenharmony_ci		       vfe->base +
4758c2ecf20Sopenharmony_ci		       VFE_0_BUS_IMAGE_MASTER_n_WR_IRQ_SUBSAMPLE_PATTERN(wm));
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_cistatic void vfe_bus_disconnect_wm_from_rdi(struct vfe_device *vfe, u8 wm,
4798c2ecf20Sopenharmony_ci					   enum vfe_line_id id)
4808c2ecf20Sopenharmony_ci{
4818c2ecf20Sopenharmony_ci	u32 reg;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	reg = VFE_0_RDI_CFG_x_RDI_Mr_FRAME_BASED_EN(id);
4848c2ecf20Sopenharmony_ci	vfe_reg_clr(vfe, VFE_0_RDI_CFG_x(0), reg);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	reg = VFE_0_RDI_CFG_x_RDI_EN_BIT;
4878c2ecf20Sopenharmony_ci	vfe_reg_clr(vfe, VFE_0_RDI_CFG_x(id), reg);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	switch (id) {
4908c2ecf20Sopenharmony_ci	case VFE_LINE_RDI0:
4918c2ecf20Sopenharmony_ci	default:
4928c2ecf20Sopenharmony_ci		reg = VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI0 <<
4938c2ecf20Sopenharmony_ci		      VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_SHIFT;
4948c2ecf20Sopenharmony_ci		break;
4958c2ecf20Sopenharmony_ci	case VFE_LINE_RDI1:
4968c2ecf20Sopenharmony_ci		reg = VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI1 <<
4978c2ecf20Sopenharmony_ci		      VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_SHIFT;
4988c2ecf20Sopenharmony_ci		break;
4998c2ecf20Sopenharmony_ci	case VFE_LINE_RDI2:
5008c2ecf20Sopenharmony_ci		reg = VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_VAL_RDI2 <<
5018c2ecf20Sopenharmony_ci		      VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_SHIFT;
5028c2ecf20Sopenharmony_ci		break;
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	if (wm % 2 == 1)
5068c2ecf20Sopenharmony_ci		reg <<= 16;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	vfe_reg_clr(vfe, VFE_0_BUS_XBAR_CFG_x(wm), reg);
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic void vfe_set_xbar_cfg(struct vfe_device *vfe, struct vfe_output *output,
5128c2ecf20Sopenharmony_ci			     u8 enable)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	struct vfe_line *line = container_of(output, struct vfe_line, output);
5158c2ecf20Sopenharmony_ci	u32 p = line->video_out.active_fmt.fmt.pix_mp.pixelformat;
5168c2ecf20Sopenharmony_ci	u32 reg;
5178c2ecf20Sopenharmony_ci	unsigned int i;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	for (i = 0; i < output->wm_num; i++) {
5208c2ecf20Sopenharmony_ci		if (i == 0) {
5218c2ecf20Sopenharmony_ci			reg = VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_LUMA <<
5228c2ecf20Sopenharmony_ci				VFE_0_BUS_XBAR_CFG_x_M_SINGLE_STREAM_SEL_SHIFT;
5238c2ecf20Sopenharmony_ci		} else if (i == 1) {
5248c2ecf20Sopenharmony_ci			reg = VFE_0_BUS_XBAR_CFG_x_M_PAIR_STREAM_EN;
5258c2ecf20Sopenharmony_ci			if (p == V4L2_PIX_FMT_NV12 || p == V4L2_PIX_FMT_NV16)
5268c2ecf20Sopenharmony_ci				reg |= VFE_0_BUS_XBAR_CFG_x_M_PAIR_STREAM_SWAP_INTER_INTRA;
5278c2ecf20Sopenharmony_ci		} else {
5288c2ecf20Sopenharmony_ci			/* On current devices output->wm_num is always <= 2 */
5298c2ecf20Sopenharmony_ci			break;
5308c2ecf20Sopenharmony_ci		}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		if (output->wm_idx[i] % 2 == 1)
5338c2ecf20Sopenharmony_ci			reg <<= 16;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci		if (enable)
5368c2ecf20Sopenharmony_ci			vfe_reg_set(vfe,
5378c2ecf20Sopenharmony_ci				    VFE_0_BUS_XBAR_CFG_x(output->wm_idx[i]),
5388c2ecf20Sopenharmony_ci				    reg);
5398c2ecf20Sopenharmony_ci		else
5408c2ecf20Sopenharmony_ci			vfe_reg_clr(vfe,
5418c2ecf20Sopenharmony_ci				    VFE_0_BUS_XBAR_CFG_x(output->wm_idx[i]),
5428c2ecf20Sopenharmony_ci				    reg);
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci}
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_cistatic void vfe_set_realign_cfg(struct vfe_device *vfe, struct vfe_line *line,
5478c2ecf20Sopenharmony_ci				u8 enable)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	/* empty */
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_cistatic void vfe_set_rdi_cid(struct vfe_device *vfe, enum vfe_line_id id, u8 cid)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	vfe_reg_clr(vfe, VFE_0_RDI_CFG_x(id),
5548c2ecf20Sopenharmony_ci		    VFE_0_RDI_CFG_x_RDI_M0_SEL_MASK);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	vfe_reg_set(vfe, VFE_0_RDI_CFG_x(id),
5578c2ecf20Sopenharmony_ci		    cid << VFE_0_RDI_CFG_x_RDI_M0_SEL_SHIFT);
5588c2ecf20Sopenharmony_ci}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_cistatic void vfe_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	vfe->reg_update |= VFE_0_REG_UPDATE_line_n(line_id);
5638c2ecf20Sopenharmony_ci	wmb();
5648c2ecf20Sopenharmony_ci	writel_relaxed(vfe->reg_update, vfe->base + VFE_0_REG_UPDATE);
5658c2ecf20Sopenharmony_ci	wmb();
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic inline void vfe_reg_update_clear(struct vfe_device *vfe,
5698c2ecf20Sopenharmony_ci					enum vfe_line_id line_id)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	vfe->reg_update &= ~VFE_0_REG_UPDATE_line_n(line_id);
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic void vfe_enable_irq_wm_line(struct vfe_device *vfe, u8 wm,
5758c2ecf20Sopenharmony_ci				   enum vfe_line_id line_id, u8 enable)
5768c2ecf20Sopenharmony_ci{
5778c2ecf20Sopenharmony_ci	u32 irq_en0 = VFE_0_IRQ_MASK_0_IMAGE_MASTER_n_PING_PONG(wm) |
5788c2ecf20Sopenharmony_ci		      VFE_0_IRQ_MASK_0_line_n_REG_UPDATE(line_id);
5798c2ecf20Sopenharmony_ci	u32 irq_en1 = VFE_0_IRQ_MASK_1_IMAGE_MASTER_n_BUS_OVERFLOW(wm) |
5808c2ecf20Sopenharmony_ci		      VFE_0_IRQ_MASK_1_RDIn_SOF(line_id);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	if (enable) {
5838c2ecf20Sopenharmony_ci		vfe_reg_set(vfe, VFE_0_IRQ_MASK_0, irq_en0);
5848c2ecf20Sopenharmony_ci		vfe_reg_set(vfe, VFE_0_IRQ_MASK_1, irq_en1);
5858c2ecf20Sopenharmony_ci	} else {
5868c2ecf20Sopenharmony_ci		vfe_reg_clr(vfe, VFE_0_IRQ_MASK_0, irq_en0);
5878c2ecf20Sopenharmony_ci		vfe_reg_clr(vfe, VFE_0_IRQ_MASK_1, irq_en1);
5888c2ecf20Sopenharmony_ci	}
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_cistatic void vfe_enable_irq_pix_line(struct vfe_device *vfe, u8 comp,
5928c2ecf20Sopenharmony_ci				    enum vfe_line_id line_id, u8 enable)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	struct vfe_output *output = &vfe->line[line_id].output;
5958c2ecf20Sopenharmony_ci	unsigned int i;
5968c2ecf20Sopenharmony_ci	u32 irq_en0;
5978c2ecf20Sopenharmony_ci	u32 irq_en1;
5988c2ecf20Sopenharmony_ci	u32 comp_mask = 0;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	irq_en0 = VFE_0_IRQ_MASK_0_CAMIF_SOF;
6018c2ecf20Sopenharmony_ci	irq_en0 |= VFE_0_IRQ_MASK_0_CAMIF_EOF;
6028c2ecf20Sopenharmony_ci	irq_en0 |= VFE_0_IRQ_MASK_0_IMAGE_COMPOSITE_DONE_n(comp);
6038c2ecf20Sopenharmony_ci	irq_en0 |= VFE_0_IRQ_MASK_0_line_n_REG_UPDATE(line_id);
6048c2ecf20Sopenharmony_ci	irq_en1 = VFE_0_IRQ_MASK_1_CAMIF_ERROR;
6058c2ecf20Sopenharmony_ci	for (i = 0; i < output->wm_num; i++) {
6068c2ecf20Sopenharmony_ci		irq_en1 |= VFE_0_IRQ_MASK_1_IMAGE_MASTER_n_BUS_OVERFLOW(
6078c2ecf20Sopenharmony_ci							output->wm_idx[i]);
6088c2ecf20Sopenharmony_ci		comp_mask |= (1 << output->wm_idx[i]) << comp * 8;
6098c2ecf20Sopenharmony_ci	}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	if (enable) {
6128c2ecf20Sopenharmony_ci		vfe_reg_set(vfe, VFE_0_IRQ_MASK_0, irq_en0);
6138c2ecf20Sopenharmony_ci		vfe_reg_set(vfe, VFE_0_IRQ_MASK_1, irq_en1);
6148c2ecf20Sopenharmony_ci		vfe_reg_set(vfe, VFE_0_IRQ_COMPOSITE_MASK_0, comp_mask);
6158c2ecf20Sopenharmony_ci	} else {
6168c2ecf20Sopenharmony_ci		vfe_reg_clr(vfe, VFE_0_IRQ_MASK_0, irq_en0);
6178c2ecf20Sopenharmony_ci		vfe_reg_clr(vfe, VFE_0_IRQ_MASK_1, irq_en1);
6188c2ecf20Sopenharmony_ci		vfe_reg_clr(vfe, VFE_0_IRQ_COMPOSITE_MASK_0, comp_mask);
6198c2ecf20Sopenharmony_ci	}
6208c2ecf20Sopenharmony_ci}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_cistatic void vfe_enable_irq_common(struct vfe_device *vfe)
6238c2ecf20Sopenharmony_ci{
6248c2ecf20Sopenharmony_ci	u32 irq_en0 = VFE_0_IRQ_MASK_0_RESET_ACK;
6258c2ecf20Sopenharmony_ci	u32 irq_en1 = VFE_0_IRQ_MASK_1_VIOLATION |
6268c2ecf20Sopenharmony_ci		      VFE_0_IRQ_MASK_1_BUS_BDG_HALT_ACK;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	vfe_reg_set(vfe, VFE_0_IRQ_MASK_0, irq_en0);
6298c2ecf20Sopenharmony_ci	vfe_reg_set(vfe, VFE_0_IRQ_MASK_1, irq_en1);
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic void vfe_set_demux_cfg(struct vfe_device *vfe, struct vfe_line *line)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	u32 val, even_cfg, odd_cfg;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	writel_relaxed(VFE_0_DEMUX_CFG_PERIOD, vfe->base + VFE_0_DEMUX_CFG);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	val = VFE_0_DEMUX_GAIN_0_CH0_EVEN | VFE_0_DEMUX_GAIN_0_CH0_ODD;
6398c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_DEMUX_GAIN_0);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	val = VFE_0_DEMUX_GAIN_1_CH1 | VFE_0_DEMUX_GAIN_1_CH2;
6428c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_DEMUX_GAIN_1);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	switch (line->fmt[MSM_VFE_PAD_SINK].code) {
6458c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_YUYV8_2X8:
6468c2ecf20Sopenharmony_ci		even_cfg = VFE_0_DEMUX_EVEN_CFG_PATTERN_YUYV;
6478c2ecf20Sopenharmony_ci		odd_cfg = VFE_0_DEMUX_ODD_CFG_PATTERN_YUYV;
6488c2ecf20Sopenharmony_ci		break;
6498c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_YVYU8_2X8:
6508c2ecf20Sopenharmony_ci		even_cfg = VFE_0_DEMUX_EVEN_CFG_PATTERN_YVYU;
6518c2ecf20Sopenharmony_ci		odd_cfg = VFE_0_DEMUX_ODD_CFG_PATTERN_YVYU;
6528c2ecf20Sopenharmony_ci		break;
6538c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_UYVY8_2X8:
6548c2ecf20Sopenharmony_ci	default:
6558c2ecf20Sopenharmony_ci		even_cfg = VFE_0_DEMUX_EVEN_CFG_PATTERN_UYVY;
6568c2ecf20Sopenharmony_ci		odd_cfg = VFE_0_DEMUX_ODD_CFG_PATTERN_UYVY;
6578c2ecf20Sopenharmony_ci		break;
6588c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_VYUY8_2X8:
6598c2ecf20Sopenharmony_ci		even_cfg = VFE_0_DEMUX_EVEN_CFG_PATTERN_VYUY;
6608c2ecf20Sopenharmony_ci		odd_cfg = VFE_0_DEMUX_ODD_CFG_PATTERN_VYUY;
6618c2ecf20Sopenharmony_ci		break;
6628c2ecf20Sopenharmony_ci	}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	writel_relaxed(even_cfg, vfe->base + VFE_0_DEMUX_EVEN_CFG);
6658c2ecf20Sopenharmony_ci	writel_relaxed(odd_cfg, vfe->base + VFE_0_DEMUX_ODD_CFG);
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic inline u8 vfe_calc_interp_reso(u16 input, u16 output)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	if (input / output >= 16)
6718c2ecf20Sopenharmony_ci		return 0;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	if (input / output >= 8)
6748c2ecf20Sopenharmony_ci		return 1;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	if (input / output >= 4)
6778c2ecf20Sopenharmony_ci		return 2;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	return 3;
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_cistatic void vfe_set_scale_cfg(struct vfe_device *vfe, struct vfe_line *line)
6838c2ecf20Sopenharmony_ci{
6848c2ecf20Sopenharmony_ci	u32 p = line->video_out.active_fmt.fmt.pix_mp.pixelformat;
6858c2ecf20Sopenharmony_ci	u32 reg;
6868c2ecf20Sopenharmony_ci	u16 input, output;
6878c2ecf20Sopenharmony_ci	u8 interp_reso;
6888c2ecf20Sopenharmony_ci	u32 phase_mult;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	writel_relaxed(0x3, vfe->base + VFE_0_SCALE_ENC_Y_CFG);
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	input = line->fmt[MSM_VFE_PAD_SINK].width;
6938c2ecf20Sopenharmony_ci	output = line->compose.width;
6948c2ecf20Sopenharmony_ci	reg = (output << 16) | input;
6958c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_SCALE_ENC_Y_H_IMAGE_SIZE);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	interp_reso = vfe_calc_interp_reso(input, output);
6988c2ecf20Sopenharmony_ci	phase_mult = input * (1 << (13 + interp_reso)) / output;
6998c2ecf20Sopenharmony_ci	reg = (interp_reso << 20) | phase_mult;
7008c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_SCALE_ENC_Y_H_PHASE);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	input = line->fmt[MSM_VFE_PAD_SINK].height;
7038c2ecf20Sopenharmony_ci	output = line->compose.height;
7048c2ecf20Sopenharmony_ci	reg = (output << 16) | input;
7058c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_SCALE_ENC_Y_V_IMAGE_SIZE);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	interp_reso = vfe_calc_interp_reso(input, output);
7088c2ecf20Sopenharmony_ci	phase_mult = input * (1 << (13 + interp_reso)) / output;
7098c2ecf20Sopenharmony_ci	reg = (interp_reso << 20) | phase_mult;
7108c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_SCALE_ENC_Y_V_PHASE);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	writel_relaxed(0x3, vfe->base + VFE_0_SCALE_ENC_CBCR_CFG);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	input = line->fmt[MSM_VFE_PAD_SINK].width;
7158c2ecf20Sopenharmony_ci	output = line->compose.width / 2;
7168c2ecf20Sopenharmony_ci	reg = (output << 16) | input;
7178c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_SCALE_ENC_CBCR_H_IMAGE_SIZE);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	interp_reso = vfe_calc_interp_reso(input, output);
7208c2ecf20Sopenharmony_ci	phase_mult = input * (1 << (13 + interp_reso)) / output;
7218c2ecf20Sopenharmony_ci	reg = (interp_reso << 20) | phase_mult;
7228c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_SCALE_ENC_CBCR_H_PHASE);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	input = line->fmt[MSM_VFE_PAD_SINK].height;
7258c2ecf20Sopenharmony_ci	output = line->compose.height;
7268c2ecf20Sopenharmony_ci	if (p == V4L2_PIX_FMT_NV12 || p == V4L2_PIX_FMT_NV21)
7278c2ecf20Sopenharmony_ci		output = line->compose.height / 2;
7288c2ecf20Sopenharmony_ci	reg = (output << 16) | input;
7298c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_SCALE_ENC_CBCR_V_IMAGE_SIZE);
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	interp_reso = vfe_calc_interp_reso(input, output);
7328c2ecf20Sopenharmony_ci	phase_mult = input * (1 << (13 + interp_reso)) / output;
7338c2ecf20Sopenharmony_ci	reg = (interp_reso << 20) | phase_mult;
7348c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_SCALE_ENC_CBCR_V_PHASE);
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_cistatic void vfe_set_crop_cfg(struct vfe_device *vfe, struct vfe_line *line)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	u32 p = line->video_out.active_fmt.fmt.pix_mp.pixelformat;
7408c2ecf20Sopenharmony_ci	u32 reg;
7418c2ecf20Sopenharmony_ci	u16 first, last;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	first = line->crop.left;
7448c2ecf20Sopenharmony_ci	last = line->crop.left + line->crop.width - 1;
7458c2ecf20Sopenharmony_ci	reg = (first << 16) | last;
7468c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_CROP_ENC_Y_WIDTH);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	first = line->crop.top;
7498c2ecf20Sopenharmony_ci	last = line->crop.top + line->crop.height - 1;
7508c2ecf20Sopenharmony_ci	reg = (first << 16) | last;
7518c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_CROP_ENC_Y_HEIGHT);
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	first = line->crop.left / 2;
7548c2ecf20Sopenharmony_ci	last = line->crop.left / 2 + line->crop.width / 2 - 1;
7558c2ecf20Sopenharmony_ci	reg = (first << 16) | last;
7568c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_CROP_ENC_CBCR_WIDTH);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	first = line->crop.top;
7598c2ecf20Sopenharmony_ci	last = line->crop.top + line->crop.height - 1;
7608c2ecf20Sopenharmony_ci	if (p == V4L2_PIX_FMT_NV12 || p == V4L2_PIX_FMT_NV21) {
7618c2ecf20Sopenharmony_ci		first = line->crop.top / 2;
7628c2ecf20Sopenharmony_ci		last = line->crop.top / 2 + line->crop.height / 2 - 1;
7638c2ecf20Sopenharmony_ci	}
7648c2ecf20Sopenharmony_ci	reg = (first << 16) | last;
7658c2ecf20Sopenharmony_ci	writel_relaxed(reg, vfe->base + VFE_0_CROP_ENC_CBCR_HEIGHT);
7668c2ecf20Sopenharmony_ci}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_cistatic void vfe_set_clamp_cfg(struct vfe_device *vfe)
7698c2ecf20Sopenharmony_ci{
7708c2ecf20Sopenharmony_ci	u32 val = VFE_0_CLAMP_ENC_MAX_CFG_CH0 |
7718c2ecf20Sopenharmony_ci		VFE_0_CLAMP_ENC_MAX_CFG_CH1 |
7728c2ecf20Sopenharmony_ci		VFE_0_CLAMP_ENC_MAX_CFG_CH2;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CLAMP_ENC_MAX_CFG);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	val = VFE_0_CLAMP_ENC_MIN_CFG_CH0 |
7778c2ecf20Sopenharmony_ci		VFE_0_CLAMP_ENC_MIN_CFG_CH1 |
7788c2ecf20Sopenharmony_ci		VFE_0_CLAMP_ENC_MIN_CFG_CH2;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CLAMP_ENC_MIN_CFG);
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_cistatic void vfe_set_qos(struct vfe_device *vfe)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	u32 val = VFE_0_BUS_BDG_QOS_CFG_0_CFG;
7868c2ecf20Sopenharmony_ci	u32 val7 = VFE_0_BUS_BDG_QOS_CFG_7_CFG;
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_BUS_BDG_QOS_CFG_0);
7898c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_BUS_BDG_QOS_CFG_1);
7908c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_BUS_BDG_QOS_CFG_2);
7918c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_BUS_BDG_QOS_CFG_3);
7928c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_BUS_BDG_QOS_CFG_4);
7938c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_BUS_BDG_QOS_CFG_5);
7948c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_BUS_BDG_QOS_CFG_6);
7958c2ecf20Sopenharmony_ci	writel_relaxed(val7, vfe->base + VFE_0_BUS_BDG_QOS_CFG_7);
7968c2ecf20Sopenharmony_ci}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_cistatic void vfe_set_ds(struct vfe_device *vfe)
7998c2ecf20Sopenharmony_ci{
8008c2ecf20Sopenharmony_ci	/* empty */
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic void vfe_set_cgc_override(struct vfe_device *vfe, u8 wm, u8 enable)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	u32 val = VFE_0_CGC_OVERRIDE_1_IMAGE_Mx_CGC_OVERRIDE(wm);
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	if (enable)
8088c2ecf20Sopenharmony_ci		vfe_reg_set(vfe, VFE_0_CGC_OVERRIDE_1, val);
8098c2ecf20Sopenharmony_ci	else
8108c2ecf20Sopenharmony_ci		vfe_reg_clr(vfe, VFE_0_CGC_OVERRIDE_1, val);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	wmb();
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_cistatic void vfe_set_camif_cfg(struct vfe_device *vfe, struct vfe_line *line)
8168c2ecf20Sopenharmony_ci{
8178c2ecf20Sopenharmony_ci	u32 val;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	switch (line->fmt[MSM_VFE_PAD_SINK].code) {
8208c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_YUYV8_2X8:
8218c2ecf20Sopenharmony_ci		val = VFE_0_CORE_CFG_PIXEL_PATTERN_YCBYCR;
8228c2ecf20Sopenharmony_ci		break;
8238c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_YVYU8_2X8:
8248c2ecf20Sopenharmony_ci		val = VFE_0_CORE_CFG_PIXEL_PATTERN_YCRYCB;
8258c2ecf20Sopenharmony_ci		break;
8268c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_UYVY8_2X8:
8278c2ecf20Sopenharmony_ci	default:
8288c2ecf20Sopenharmony_ci		val = VFE_0_CORE_CFG_PIXEL_PATTERN_CBYCRY;
8298c2ecf20Sopenharmony_ci		break;
8308c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_VYUY8_2X8:
8318c2ecf20Sopenharmony_ci		val = VFE_0_CORE_CFG_PIXEL_PATTERN_CRYCBY;
8328c2ecf20Sopenharmony_ci		break;
8338c2ecf20Sopenharmony_ci	}
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CORE_CFG);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	val = line->fmt[MSM_VFE_PAD_SINK].width * 2;
8388c2ecf20Sopenharmony_ci	val |= line->fmt[MSM_VFE_PAD_SINK].height << 16;
8398c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CAMIF_FRAME_CFG);
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	val = line->fmt[MSM_VFE_PAD_SINK].width * 2 - 1;
8428c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CAMIF_WINDOW_WIDTH_CFG);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	val = line->fmt[MSM_VFE_PAD_SINK].height - 1;
8458c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CAMIF_WINDOW_HEIGHT_CFG);
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	val = 0xffffffff;
8488c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CAMIF_SUBSAMPLE_CFG_0);
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	val = 0xffffffff;
8518c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CAMIF_IRQ_SUBSAMPLE_PATTERN);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	val = VFE_0_RDI_CFG_x_MIPI_EN_BITS;
8548c2ecf20Sopenharmony_ci	vfe_reg_set(vfe, VFE_0_RDI_CFG_x(0), val);
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	val = VFE_0_CAMIF_CFG_VFE_OUTPUT_EN;
8578c2ecf20Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_0_CAMIF_CFG);
8588c2ecf20Sopenharmony_ci}
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_cistatic void vfe_set_camif_cmd(struct vfe_device *vfe, u8 enable)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	u32 cmd;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	cmd = VFE_0_CAMIF_CMD_CLEAR_CAMIF_STATUS | VFE_0_CAMIF_CMD_NO_CHANGE;
8658c2ecf20Sopenharmony_ci	writel_relaxed(cmd, vfe->base + VFE_0_CAMIF_CMD);
8668c2ecf20Sopenharmony_ci	wmb();
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	if (enable)
8698c2ecf20Sopenharmony_ci		cmd = VFE_0_CAMIF_CMD_ENABLE_FRAME_BOUNDARY;
8708c2ecf20Sopenharmony_ci	else
8718c2ecf20Sopenharmony_ci		cmd = VFE_0_CAMIF_CMD_DISABLE_FRAME_BOUNDARY;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	writel_relaxed(cmd, vfe->base + VFE_0_CAMIF_CMD);
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_cistatic void vfe_set_module_cfg(struct vfe_device *vfe, u8 enable)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	u32 val = VFE_0_MODULE_CFG_DEMUX |
8798c2ecf20Sopenharmony_ci		  VFE_0_MODULE_CFG_CHROMA_UPSAMPLE |
8808c2ecf20Sopenharmony_ci		  VFE_0_MODULE_CFG_SCALE_ENC |
8818c2ecf20Sopenharmony_ci		  VFE_0_MODULE_CFG_CROP_ENC;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	if (enable)
8848c2ecf20Sopenharmony_ci		writel_relaxed(val, vfe->base + VFE_0_MODULE_CFG);
8858c2ecf20Sopenharmony_ci	else
8868c2ecf20Sopenharmony_ci		writel_relaxed(0x0, vfe->base + VFE_0_MODULE_CFG);
8878c2ecf20Sopenharmony_ci}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_cistatic int vfe_camif_wait_for_stop(struct vfe_device *vfe, struct device *dev)
8908c2ecf20Sopenharmony_ci{
8918c2ecf20Sopenharmony_ci	u32 val;
8928c2ecf20Sopenharmony_ci	int ret;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	ret = readl_poll_timeout(vfe->base + VFE_0_CAMIF_STATUS,
8958c2ecf20Sopenharmony_ci				 val,
8968c2ecf20Sopenharmony_ci				 (val & VFE_0_CAMIF_STATUS_HALT),
8978c2ecf20Sopenharmony_ci				 CAMIF_TIMEOUT_SLEEP_US,
8988c2ecf20Sopenharmony_ci				 CAMIF_TIMEOUT_ALL_US);
8998c2ecf20Sopenharmony_ci	if (ret < 0)
9008c2ecf20Sopenharmony_ci		dev_err(dev, "%s: camif stop timeout\n", __func__);
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	return ret;
9038c2ecf20Sopenharmony_ci}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_cistatic void vfe_isr_read(struct vfe_device *vfe, u32 *value0, u32 *value1)
9068c2ecf20Sopenharmony_ci{
9078c2ecf20Sopenharmony_ci	*value0 = readl_relaxed(vfe->base + VFE_0_IRQ_STATUS_0);
9088c2ecf20Sopenharmony_ci	*value1 = readl_relaxed(vfe->base + VFE_0_IRQ_STATUS_1);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	writel_relaxed(*value0, vfe->base + VFE_0_IRQ_CLEAR_0);
9118c2ecf20Sopenharmony_ci	writel_relaxed(*value1, vfe->base + VFE_0_IRQ_CLEAR_1);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	wmb();
9148c2ecf20Sopenharmony_ci	writel_relaxed(VFE_0_IRQ_CMD_GLOBAL_CLEAR, vfe->base + VFE_0_IRQ_CMD);
9158c2ecf20Sopenharmony_ci}
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_cistatic void vfe_violation_read(struct vfe_device *vfe)
9188c2ecf20Sopenharmony_ci{
9198c2ecf20Sopenharmony_ci	u32 violation = readl_relaxed(vfe->base + VFE_0_VIOLATION_STATUS);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	pr_err_ratelimited("VFE: violation = 0x%08x\n", violation);
9228c2ecf20Sopenharmony_ci}
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci/*
9258c2ecf20Sopenharmony_ci * vfe_isr - ISPIF module interrupt handler
9268c2ecf20Sopenharmony_ci * @irq: Interrupt line
9278c2ecf20Sopenharmony_ci * @dev: VFE device
9288c2ecf20Sopenharmony_ci *
9298c2ecf20Sopenharmony_ci * Return IRQ_HANDLED on success
9308c2ecf20Sopenharmony_ci */
9318c2ecf20Sopenharmony_cistatic irqreturn_t vfe_isr(int irq, void *dev)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	struct vfe_device *vfe = dev;
9348c2ecf20Sopenharmony_ci	u32 value0, value1;
9358c2ecf20Sopenharmony_ci	int i, j;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	vfe->ops->isr_read(vfe, &value0, &value1);
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	trace_printk("VFE: status0 = 0x%08x, status1 = 0x%08x\n",
9408c2ecf20Sopenharmony_ci		     value0, value1);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	if (value0 & VFE_0_IRQ_STATUS_0_RESET_ACK)
9438c2ecf20Sopenharmony_ci		vfe->isr_ops.reset_ack(vfe);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	if (value1 & VFE_0_IRQ_STATUS_1_VIOLATION)
9468c2ecf20Sopenharmony_ci		vfe->ops->violation_read(vfe);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	if (value1 & VFE_0_IRQ_STATUS_1_BUS_BDG_HALT_ACK)
9498c2ecf20Sopenharmony_ci		vfe->isr_ops.halt_ack(vfe);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++)
9528c2ecf20Sopenharmony_ci		if (value0 & VFE_0_IRQ_STATUS_0_line_n_REG_UPDATE(i))
9538c2ecf20Sopenharmony_ci			vfe->isr_ops.reg_update(vfe, i);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	if (value0 & VFE_0_IRQ_STATUS_0_CAMIF_SOF)
9568c2ecf20Sopenharmony_ci		vfe->isr_ops.sof(vfe, VFE_LINE_PIX);
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	for (i = VFE_LINE_RDI0; i <= VFE_LINE_RDI2; i++)
9598c2ecf20Sopenharmony_ci		if (value1 & VFE_0_IRQ_STATUS_1_RDIn_SOF(i))
9608c2ecf20Sopenharmony_ci			vfe->isr_ops.sof(vfe, i);
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	for (i = 0; i < MSM_VFE_COMPOSITE_IRQ_NUM; i++)
9638c2ecf20Sopenharmony_ci		if (value0 & VFE_0_IRQ_STATUS_0_IMAGE_COMPOSITE_DONE_n(i)) {
9648c2ecf20Sopenharmony_ci			vfe->isr_ops.comp_done(vfe, i);
9658c2ecf20Sopenharmony_ci			for (j = 0; j < ARRAY_SIZE(vfe->wm_output_map); j++)
9668c2ecf20Sopenharmony_ci				if (vfe->wm_output_map[j] == VFE_LINE_PIX)
9678c2ecf20Sopenharmony_ci					value0 &= ~VFE_0_IRQ_MASK_0_IMAGE_MASTER_n_PING_PONG(j);
9688c2ecf20Sopenharmony_ci		}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	for (i = 0; i < MSM_VFE_IMAGE_MASTERS_NUM; i++)
9718c2ecf20Sopenharmony_ci		if (value0 & VFE_0_IRQ_STATUS_0_IMAGE_MASTER_n_PING_PONG(i))
9728c2ecf20Sopenharmony_ci			vfe->isr_ops.wm_done(vfe, i);
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9758c2ecf20Sopenharmony_ci}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ciconst struct vfe_hw_ops vfe_ops_4_1 = {
9788c2ecf20Sopenharmony_ci	.hw_version_read = vfe_hw_version_read,
9798c2ecf20Sopenharmony_ci	.get_ub_size = vfe_get_ub_size,
9808c2ecf20Sopenharmony_ci	.global_reset = vfe_global_reset,
9818c2ecf20Sopenharmony_ci	.halt_request = vfe_halt_request,
9828c2ecf20Sopenharmony_ci	.halt_clear = vfe_halt_clear,
9838c2ecf20Sopenharmony_ci	.wm_enable = vfe_wm_enable,
9848c2ecf20Sopenharmony_ci	.wm_frame_based = vfe_wm_frame_based,
9858c2ecf20Sopenharmony_ci	.wm_line_based = vfe_wm_line_based,
9868c2ecf20Sopenharmony_ci	.wm_set_framedrop_period = vfe_wm_set_framedrop_period,
9878c2ecf20Sopenharmony_ci	.wm_set_framedrop_pattern = vfe_wm_set_framedrop_pattern,
9888c2ecf20Sopenharmony_ci	.wm_set_ub_cfg = vfe_wm_set_ub_cfg,
9898c2ecf20Sopenharmony_ci	.bus_reload_wm = vfe_bus_reload_wm,
9908c2ecf20Sopenharmony_ci	.wm_set_ping_addr = vfe_wm_set_ping_addr,
9918c2ecf20Sopenharmony_ci	.wm_set_pong_addr = vfe_wm_set_pong_addr,
9928c2ecf20Sopenharmony_ci	.wm_get_ping_pong_status = vfe_wm_get_ping_pong_status,
9938c2ecf20Sopenharmony_ci	.bus_enable_wr_if = vfe_bus_enable_wr_if,
9948c2ecf20Sopenharmony_ci	.bus_connect_wm_to_rdi = vfe_bus_connect_wm_to_rdi,
9958c2ecf20Sopenharmony_ci	.wm_set_subsample = vfe_wm_set_subsample,
9968c2ecf20Sopenharmony_ci	.bus_disconnect_wm_from_rdi = vfe_bus_disconnect_wm_from_rdi,
9978c2ecf20Sopenharmony_ci	.set_xbar_cfg = vfe_set_xbar_cfg,
9988c2ecf20Sopenharmony_ci	.set_realign_cfg = vfe_set_realign_cfg,
9998c2ecf20Sopenharmony_ci	.set_rdi_cid = vfe_set_rdi_cid,
10008c2ecf20Sopenharmony_ci	.reg_update = vfe_reg_update,
10018c2ecf20Sopenharmony_ci	.reg_update_clear = vfe_reg_update_clear,
10028c2ecf20Sopenharmony_ci	.enable_irq_wm_line = vfe_enable_irq_wm_line,
10038c2ecf20Sopenharmony_ci	.enable_irq_pix_line = vfe_enable_irq_pix_line,
10048c2ecf20Sopenharmony_ci	.enable_irq_common = vfe_enable_irq_common,
10058c2ecf20Sopenharmony_ci	.set_demux_cfg = vfe_set_demux_cfg,
10068c2ecf20Sopenharmony_ci	.set_scale_cfg = vfe_set_scale_cfg,
10078c2ecf20Sopenharmony_ci	.set_crop_cfg = vfe_set_crop_cfg,
10088c2ecf20Sopenharmony_ci	.set_clamp_cfg = vfe_set_clamp_cfg,
10098c2ecf20Sopenharmony_ci	.set_qos = vfe_set_qos,
10108c2ecf20Sopenharmony_ci	.set_ds = vfe_set_ds,
10118c2ecf20Sopenharmony_ci	.set_cgc_override = vfe_set_cgc_override,
10128c2ecf20Sopenharmony_ci	.set_camif_cfg = vfe_set_camif_cfg,
10138c2ecf20Sopenharmony_ci	.set_camif_cmd = vfe_set_camif_cmd,
10148c2ecf20Sopenharmony_ci	.set_module_cfg = vfe_set_module_cfg,
10158c2ecf20Sopenharmony_ci	.camif_wait_for_stop = vfe_camif_wait_for_stop,
10168c2ecf20Sopenharmony_ci	.isr_read = vfe_isr_read,
10178c2ecf20Sopenharmony_ci	.violation_read = vfe_violation_read,
10188c2ecf20Sopenharmony_ci	.isr = vfe_isr,
10198c2ecf20Sopenharmony_ci};
1020