162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * camss-vfe-170.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Qualcomm MSM Camera Subsystem - VFE (Video Front End) Module v170
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (C) 2020-2021 Linaro Ltd.
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/interrupt.h>
1162306a36Sopenharmony_ci#include <linux/io.h>
1262306a36Sopenharmony_ci#include <linux/iopoll.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include "camss.h"
1562306a36Sopenharmony_ci#include "camss-vfe.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define VFE_HW_VERSION				(0x000)
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#define VFE_GLOBAL_RESET_CMD			(0x018)
2062306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_CORE		BIT(0)
2162306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_CAMIF		BIT(1)
2262306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_BUS		BIT(2)
2362306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_BUS_BDG	BIT(3)
2462306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_REGISTER	BIT(4)
2562306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_PM		BIT(5)
2662306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_BUS_MISR	BIT(6)
2762306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_TESTGEN	BIT(7)
2862306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_DSP		BIT(8)
2962306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_IDLE_CGC	BIT(9)
3062306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_RDI0		BIT(10)
3162306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_RDI1		BIT(11)
3262306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_RDI2		BIT(12)
3362306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_RDI3		BIT(13)
3462306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_VFE_DOMAIN	BIT(30)
3562306a36Sopenharmony_ci#define		GLOBAL_RESET_CMD_RESET_BYPASS	BIT(31)
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#define VFE_CORE_CFG				(0x050)
3862306a36Sopenharmony_ci#define		CFG_PIXEL_PATTERN_YCBYCR	(0x4)
3962306a36Sopenharmony_ci#define		CFG_PIXEL_PATTERN_YCRYCB	(0x5)
4062306a36Sopenharmony_ci#define		CFG_PIXEL_PATTERN_CBYCRY	(0x6)
4162306a36Sopenharmony_ci#define		CFG_PIXEL_PATTERN_CRYCBY	(0x7)
4262306a36Sopenharmony_ci#define		CFG_COMPOSITE_REG_UPDATE_EN	BIT(4)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#define VFE_IRQ_CMD				(0x058)
4562306a36Sopenharmony_ci#define		CMD_GLOBAL_CLEAR		BIT(0)
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define VFE_IRQ_MASK_0					(0x05c)
4862306a36Sopenharmony_ci#define		MASK_0_CAMIF_SOF			BIT(0)
4962306a36Sopenharmony_ci#define		MASK_0_CAMIF_EOF			BIT(1)
5062306a36Sopenharmony_ci#define		MASK_0_RDI_REG_UPDATE(n)		BIT((n) + 5)
5162306a36Sopenharmony_ci#define		MASK_0_IMAGE_MASTER_n_PING_PONG(n)	BIT((n) + 8)
5262306a36Sopenharmony_ci#define		MASK_0_IMAGE_COMPOSITE_DONE_n(n)	BIT((n) + 25)
5362306a36Sopenharmony_ci#define		MASK_0_RESET_ACK			BIT(31)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define VFE_IRQ_MASK_1					(0x060)
5662306a36Sopenharmony_ci#define		MASK_1_CAMIF_ERROR			BIT(0)
5762306a36Sopenharmony_ci#define		MASK_1_VIOLATION			BIT(7)
5862306a36Sopenharmony_ci#define		MASK_1_BUS_BDG_HALT_ACK			BIT(8)
5962306a36Sopenharmony_ci#define		MASK_1_IMAGE_MASTER_n_BUS_OVERFLOW(n)	BIT((n) + 9)
6062306a36Sopenharmony_ci#define		MASK_1_RDI_SOF(n)			BIT((n) + 29)
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define VFE_IRQ_CLEAR_0					(0x064)
6362306a36Sopenharmony_ci#define VFE_IRQ_CLEAR_1					(0x068)
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci#define VFE_IRQ_STATUS_0				(0x06c)
6662306a36Sopenharmony_ci#define		STATUS_0_CAMIF_SOF			BIT(0)
6762306a36Sopenharmony_ci#define		STATUS_0_RDI_REG_UPDATE(n)		BIT((n) + 5)
6862306a36Sopenharmony_ci#define		STATUS_0_IMAGE_MASTER_PING_PONG(n)	BIT((n) + 8)
6962306a36Sopenharmony_ci#define		STATUS_0_IMAGE_COMPOSITE_DONE(n)	BIT((n) + 25)
7062306a36Sopenharmony_ci#define		STATUS_0_RESET_ACK			BIT(31)
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define VFE_IRQ_STATUS_1				(0x070)
7362306a36Sopenharmony_ci#define		STATUS_1_VIOLATION			BIT(7)
7462306a36Sopenharmony_ci#define		STATUS_1_BUS_BDG_HALT_ACK		BIT(8)
7562306a36Sopenharmony_ci#define		STATUS_1_RDI_SOF(n)			BIT((n) + 27)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define VFE_VIOLATION_STATUS			(0x07c)
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci#define VFE_CAMIF_CMD				(0x478)
8062306a36Sopenharmony_ci#define		CMD_CLEAR_CAMIF_STATUS		BIT(2)
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define VFE_CAMIF_CFG				(0x47c)
8362306a36Sopenharmony_ci#define		CFG_VSYNC_SYNC_EDGE		(0)
8462306a36Sopenharmony_ci#define			VSYNC_ACTIVE_HIGH	(0)
8562306a36Sopenharmony_ci#define			VSYNC_ACTIVE_LOW	(1)
8662306a36Sopenharmony_ci#define		CFG_HSYNC_SYNC_EDGE		(1)
8762306a36Sopenharmony_ci#define			HSYNC_ACTIVE_HIGH	(0)
8862306a36Sopenharmony_ci#define			HSYNC_ACTIVE_LOW	(1)
8962306a36Sopenharmony_ci#define		CFG_VFE_SUBSAMPLE_ENABLE	BIT(4)
9062306a36Sopenharmony_ci#define		CFG_BUS_SUBSAMPLE_ENABLE	BIT(5)
9162306a36Sopenharmony_ci#define		CFG_VFE_OUTPUT_EN		BIT(6)
9262306a36Sopenharmony_ci#define		CFG_BUS_OUTPUT_EN		BIT(7)
9362306a36Sopenharmony_ci#define		CFG_BINNING_EN			BIT(9)
9462306a36Sopenharmony_ci#define		CFG_FRAME_BASED_EN		BIT(10)
9562306a36Sopenharmony_ci#define		CFG_RAW_CROP_EN			BIT(22)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci#define VFE_REG_UPDATE_CMD			(0x4ac)
9862306a36Sopenharmony_ci#define		REG_UPDATE_RDI(n)		BIT(1 + (n))
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci#define VFE_BUS_IRQ_MASK(n)		(0x2044 + (n) * 4)
10162306a36Sopenharmony_ci#define VFE_BUS_IRQ_CLEAR(n)		(0x2050 + (n) * 4)
10262306a36Sopenharmony_ci#define VFE_BUS_IRQ_STATUS(n)		(0x205c + (n) * 4)
10362306a36Sopenharmony_ci#define		STATUS0_COMP_RESET_DONE		BIT(0)
10462306a36Sopenharmony_ci#define		STATUS0_COMP_REG_UPDATE0_DONE	BIT(1)
10562306a36Sopenharmony_ci#define		STATUS0_COMP_REG_UPDATE1_DONE	BIT(2)
10662306a36Sopenharmony_ci#define		STATUS0_COMP_REG_UPDATE2_DONE	BIT(3)
10762306a36Sopenharmony_ci#define		STATUS0_COMP_REG_UPDATE3_DONE	BIT(4)
10862306a36Sopenharmony_ci#define		STATUS0_COMP_REG_UPDATE_DONE(n)	BIT((n) + 1)
10962306a36Sopenharmony_ci#define		STATUS0_COMP0_BUF_DONE		BIT(5)
11062306a36Sopenharmony_ci#define		STATUS0_COMP1_BUF_DONE		BIT(6)
11162306a36Sopenharmony_ci#define		STATUS0_COMP2_BUF_DONE		BIT(7)
11262306a36Sopenharmony_ci#define		STATUS0_COMP3_BUF_DONE		BIT(8)
11362306a36Sopenharmony_ci#define		STATUS0_COMP4_BUF_DONE		BIT(9)
11462306a36Sopenharmony_ci#define		STATUS0_COMP5_BUF_DONE		BIT(10)
11562306a36Sopenharmony_ci#define		STATUS0_COMP_BUF_DONE(n)	BIT((n) + 5)
11662306a36Sopenharmony_ci#define		STATUS0_COMP_ERROR		BIT(11)
11762306a36Sopenharmony_ci#define		STATUS0_COMP_OVERWRITE		BIT(12)
11862306a36Sopenharmony_ci#define		STATUS0_OVERFLOW		BIT(13)
11962306a36Sopenharmony_ci#define		STATUS0_VIOLATION		BIT(14)
12062306a36Sopenharmony_ci/* WM_CLIENT_BUF_DONE defined for buffers 0:19 */
12162306a36Sopenharmony_ci#define		STATUS1_WM_CLIENT_BUF_DONE(n)		BIT(n)
12262306a36Sopenharmony_ci#define		STATUS1_EARLY_DONE			BIT(24)
12362306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP0_BUF_DONE		BIT(0)
12462306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP1_BUF_DONE		BIT(1)
12562306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP2_BUF_DONE		BIT(2)
12662306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP3_BUF_DONE		BIT(3)
12762306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP4_BUF_DONE		BIT(4)
12862306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP5_BUF_DONE		BIT(5)
12962306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP_BUF_DONE(n)		BIT(n)
13062306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP_ERROR			BIT(6)
13162306a36Sopenharmony_ci#define		STATUS2_DUAL_COMP_OVERWRITE		BIT(7)
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci#define VFE_BUS_IRQ_CLEAR_GLOBAL		(0x2068)
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci#define VFE_BUS_WM_DEBUG_STATUS_CFG		(0x226c)
13662306a36Sopenharmony_ci#define		DEBUG_STATUS_CFG_STATUS0(n)	BIT(n)
13762306a36Sopenharmony_ci#define		DEBUG_STATUS_CFG_STATUS1(n)	BIT(8 + (n))
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#define VFE_BUS_WM_ADDR_SYNC_FRAME_HEADER	(0x2080)
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#define VFE_BUS_WM_ADDR_SYNC_NO_SYNC		(0x2084)
14262306a36Sopenharmony_ci#define		BUS_VER2_MAX_CLIENTS (24)
14362306a36Sopenharmony_ci#define		WM_ADDR_NO_SYNC_DEFAULT_VAL \
14462306a36Sopenharmony_ci				((1 << BUS_VER2_MAX_CLIENTS) - 1)
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#define VFE_BUS_WM_CGC_OVERRIDE			(0x200c)
14762306a36Sopenharmony_ci#define		WM_CGC_OVERRIDE_ALL		(0xFFFFF)
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci#define VFE_BUS_WM_TEST_BUS_CTRL		(0x211c)
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci#define VFE_BUS_WM_STATUS0(n)			(0x2200 + (n) * 0x100)
15262306a36Sopenharmony_ci#define VFE_BUS_WM_STATUS1(n)			(0x2204 + (n) * 0x100)
15362306a36Sopenharmony_ci#define VFE_BUS_WM_CFG(n)			(0x2208 + (n) * 0x100)
15462306a36Sopenharmony_ci#define		WM_CFG_EN			(0)
15562306a36Sopenharmony_ci#define		WM_CFG_MODE			(1)
15662306a36Sopenharmony_ci#define			MODE_QCOM_PLAIN	(0)
15762306a36Sopenharmony_ci#define			MODE_MIPI_RAW	(1)
15862306a36Sopenharmony_ci#define		WM_CFG_VIRTUALFRAME		(2)
15962306a36Sopenharmony_ci#define VFE_BUS_WM_HEADER_ADDR(n)		(0x220c + (n) * 0x100)
16062306a36Sopenharmony_ci#define VFE_BUS_WM_HEADER_CFG(n)		(0x2210 + (n) * 0x100)
16162306a36Sopenharmony_ci#define VFE_BUS_WM_IMAGE_ADDR(n)		(0x2214 + (n) * 0x100)
16262306a36Sopenharmony_ci#define VFE_BUS_WM_IMAGE_ADDR_OFFSET(n)		(0x2218 + (n) * 0x100)
16362306a36Sopenharmony_ci#define VFE_BUS_WM_BUFFER_WIDTH_CFG(n)		(0x221c + (n) * 0x100)
16462306a36Sopenharmony_ci#define		WM_BUFFER_DEFAULT_WIDTH		(0xFF01)
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci#define VFE_BUS_WM_BUFFER_HEIGHT_CFG(n)		(0x2220 + (n) * 0x100)
16762306a36Sopenharmony_ci#define VFE_BUS_WM_PACKER_CFG(n)		(0x2224 + (n) * 0x100)
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci#define VFE_BUS_WM_STRIDE(n)			(0x2228 + (n) * 0x100)
17062306a36Sopenharmony_ci#define		WM_STRIDE_DEFAULT_STRIDE	(0xFF01)
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci#define VFE_BUS_WM_IRQ_SUBSAMPLE_PERIOD(n)	(0x2248 + (n) * 0x100)
17362306a36Sopenharmony_ci#define VFE_BUS_WM_IRQ_SUBSAMPLE_PATTERN(n)	(0x224c + (n) * 0x100)
17462306a36Sopenharmony_ci#define VFE_BUS_WM_FRAMEDROP_PERIOD(n)		(0x2250 + (n) * 0x100)
17562306a36Sopenharmony_ci#define VFE_BUS_WM_FRAMEDROP_PATTERN(n)		(0x2254 + (n) * 0x100)
17662306a36Sopenharmony_ci#define VFE_BUS_WM_FRAME_INC(n)			(0x2258 + (n) * 0x100)
17762306a36Sopenharmony_ci#define VFE_BUS_WM_BURST_LIMIT(n)		(0x225c + (n) * 0x100)
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cistatic u32 vfe_hw_version(struct vfe_device *vfe)
18062306a36Sopenharmony_ci{
18162306a36Sopenharmony_ci	u32 hw_version = readl_relaxed(vfe->base + VFE_HW_VERSION);
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	u32 gen = (hw_version >> 28) & 0xF;
18462306a36Sopenharmony_ci	u32 rev = (hw_version >> 16) & 0xFFF;
18562306a36Sopenharmony_ci	u32 step = hw_version & 0xFFFF;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	dev_dbg(vfe->camss->dev, "VFE HW Version = %u.%u.%u\n",
18862306a36Sopenharmony_ci		gen, rev, step);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	return hw_version;
19162306a36Sopenharmony_ci}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistatic inline void vfe_reg_set(struct vfe_device *vfe, u32 reg, u32 set_bits)
19462306a36Sopenharmony_ci{
19562306a36Sopenharmony_ci	u32 bits = readl_relaxed(vfe->base + reg);
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	writel_relaxed(bits | set_bits, vfe->base + reg);
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic void vfe_global_reset(struct vfe_device *vfe)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci	u32 reset_bits = GLOBAL_RESET_CMD_CORE		|
20362306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_CAMIF		|
20462306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_BUS		|
20562306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_BUS_BDG	|
20662306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_REGISTER	|
20762306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_TESTGEN	|
20862306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_DSP		|
20962306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_IDLE_CGC	|
21062306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_RDI0		|
21162306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_RDI1		|
21262306a36Sopenharmony_ci			 GLOBAL_RESET_CMD_RDI2;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	writel_relaxed(BIT(31), vfe->base + VFE_IRQ_MASK_0);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	/* Make sure IRQ mask has been written before resetting */
21762306a36Sopenharmony_ci	wmb();
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	writel_relaxed(reset_bits, vfe->base + VFE_GLOBAL_RESET_CMD);
22062306a36Sopenharmony_ci}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_cistatic void vfe_wm_start(struct vfe_device *vfe, u8 wm, struct vfe_line *line)
22362306a36Sopenharmony_ci{
22462306a36Sopenharmony_ci	u32 val;
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	/*Set Debug Registers*/
22762306a36Sopenharmony_ci	val = DEBUG_STATUS_CFG_STATUS0(1) |
22862306a36Sopenharmony_ci	      DEBUG_STATUS_CFG_STATUS0(7);
22962306a36Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_BUS_WM_DEBUG_STATUS_CFG);
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	/* BUS_WM_INPUT_IF_ADDR_SYNC_FRAME_HEADER */
23262306a36Sopenharmony_ci	writel_relaxed(0, vfe->base + VFE_BUS_WM_ADDR_SYNC_FRAME_HEADER);
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	/* no clock gating at bus input */
23562306a36Sopenharmony_ci	val = WM_CGC_OVERRIDE_ALL;
23662306a36Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_BUS_WM_CGC_OVERRIDE);
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	writel_relaxed(0x0, vfe->base + VFE_BUS_WM_TEST_BUS_CTRL);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	/* if addr_no_sync has default value then config the addr no sync reg */
24162306a36Sopenharmony_ci	val = WM_ADDR_NO_SYNC_DEFAULT_VAL;
24262306a36Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_BUS_WM_ADDR_SYNC_NO_SYNC);
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	writel_relaxed(0xf, vfe->base + VFE_BUS_WM_BURST_LIMIT(wm));
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	val = WM_BUFFER_DEFAULT_WIDTH;
24762306a36Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_BUS_WM_BUFFER_WIDTH_CFG(wm));
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	val = 0;
25062306a36Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_BUS_WM_BUFFER_HEIGHT_CFG(wm));
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	val = 0;
25362306a36Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_BUS_WM_PACKER_CFG(wm)); // XXX 1 for PLAIN8?
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	/* Configure stride for RDIs */
25662306a36Sopenharmony_ci	val = WM_STRIDE_DEFAULT_STRIDE;
25762306a36Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_BUS_WM_STRIDE(wm));
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	/* Enable WM */
26062306a36Sopenharmony_ci	val = 1 << WM_CFG_EN |
26162306a36Sopenharmony_ci	      MODE_MIPI_RAW << WM_CFG_MODE;
26262306a36Sopenharmony_ci	writel_relaxed(val, vfe->base + VFE_BUS_WM_CFG(wm));
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_cistatic void vfe_wm_stop(struct vfe_device *vfe, u8 wm)
26662306a36Sopenharmony_ci{
26762306a36Sopenharmony_ci	/* Disable WM */
26862306a36Sopenharmony_ci	writel_relaxed(0, vfe->base + VFE_BUS_WM_CFG(wm));
26962306a36Sopenharmony_ci}
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_cistatic void vfe_wm_update(struct vfe_device *vfe, u8 wm, u32 addr,
27262306a36Sopenharmony_ci			  struct vfe_line *line)
27362306a36Sopenharmony_ci{
27462306a36Sopenharmony_ci	struct v4l2_pix_format_mplane *pix =
27562306a36Sopenharmony_ci		&line->video_out.active_fmt.fmt.pix_mp;
27662306a36Sopenharmony_ci	u32 stride = pix->plane_fmt[0].bytesperline;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	writel_relaxed(addr, vfe->base + VFE_BUS_WM_IMAGE_ADDR(wm));
27962306a36Sopenharmony_ci	writel_relaxed(stride * pix->height, vfe->base + VFE_BUS_WM_FRAME_INC(wm));
28062306a36Sopenharmony_ci}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_cistatic void vfe_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id)
28362306a36Sopenharmony_ci{
28462306a36Sopenharmony_ci	vfe->reg_update |= REG_UPDATE_RDI(line_id);
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci	/* Enforce ordering between previous reg writes and reg update */
28762306a36Sopenharmony_ci	wmb();
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	writel_relaxed(vfe->reg_update, vfe->base + VFE_REG_UPDATE_CMD);
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	/* Enforce ordering between reg update and subsequent reg writes */
29262306a36Sopenharmony_ci	wmb();
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_cistatic inline void vfe_reg_update_clear(struct vfe_device *vfe,
29662306a36Sopenharmony_ci					enum vfe_line_id line_id)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	vfe->reg_update &= ~REG_UPDATE_RDI(line_id);
29962306a36Sopenharmony_ci}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_cistatic void vfe_enable_irq_common(struct vfe_device *vfe)
30262306a36Sopenharmony_ci{
30362306a36Sopenharmony_ci	vfe_reg_set(vfe, VFE_IRQ_MASK_0, ~0u);
30462306a36Sopenharmony_ci	vfe_reg_set(vfe, VFE_IRQ_MASK_1, ~0u);
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(0));
30762306a36Sopenharmony_ci	writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(1));
30862306a36Sopenharmony_ci	writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(2));
30962306a36Sopenharmony_ci}
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_cistatic void vfe_isr_halt_ack(struct vfe_device *vfe)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	complete(&vfe->halt_complete);
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_cistatic void vfe_isr_read(struct vfe_device *vfe, u32 *status0, u32 *status1)
31762306a36Sopenharmony_ci{
31862306a36Sopenharmony_ci	*status0 = readl_relaxed(vfe->base + VFE_IRQ_STATUS_0);
31962306a36Sopenharmony_ci	*status1 = readl_relaxed(vfe->base + VFE_IRQ_STATUS_1);
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	writel_relaxed(*status0, vfe->base + VFE_IRQ_CLEAR_0);
32262306a36Sopenharmony_ci	writel_relaxed(*status1, vfe->base + VFE_IRQ_CLEAR_1);
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	/* Enforce ordering between IRQ Clear and Global IRQ Clear */
32562306a36Sopenharmony_ci	wmb();
32662306a36Sopenharmony_ci	writel_relaxed(CMD_GLOBAL_CLEAR, vfe->base + VFE_IRQ_CMD);
32762306a36Sopenharmony_ci}
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_cistatic void vfe_violation_read(struct vfe_device *vfe)
33062306a36Sopenharmony_ci{
33162306a36Sopenharmony_ci	u32 violation = readl_relaxed(vfe->base + VFE_VIOLATION_STATUS);
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	pr_err_ratelimited("VFE: violation = 0x%08x\n", violation);
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci/*
33762306a36Sopenharmony_ci * vfe_isr - VFE module interrupt handler
33862306a36Sopenharmony_ci * @irq: Interrupt line
33962306a36Sopenharmony_ci * @dev: VFE device
34062306a36Sopenharmony_ci *
34162306a36Sopenharmony_ci * Return IRQ_HANDLED on success
34262306a36Sopenharmony_ci */
34362306a36Sopenharmony_cistatic irqreturn_t vfe_isr(int irq, void *dev)
34462306a36Sopenharmony_ci{
34562306a36Sopenharmony_ci	struct vfe_device *vfe = dev;
34662306a36Sopenharmony_ci	u32 status0, status1, vfe_bus_status[3];
34762306a36Sopenharmony_ci	int i, wm;
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	status0 = readl_relaxed(vfe->base + VFE_IRQ_STATUS_0);
35062306a36Sopenharmony_ci	status1 = readl_relaxed(vfe->base + VFE_IRQ_STATUS_1);
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	writel_relaxed(status0, vfe->base + VFE_IRQ_CLEAR_0);
35362306a36Sopenharmony_ci	writel_relaxed(status1, vfe->base + VFE_IRQ_CLEAR_1);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	for (i = VFE_LINE_RDI0; i <= VFE_LINE_RDI2; i++) {
35662306a36Sopenharmony_ci		vfe_bus_status[i] = readl_relaxed(vfe->base + VFE_BUS_IRQ_STATUS(i));
35762306a36Sopenharmony_ci		writel_relaxed(vfe_bus_status[i], vfe->base + VFE_BUS_IRQ_CLEAR(i));
35862306a36Sopenharmony_ci	}
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	/* Enforce ordering between IRQ reading and interpretation */
36162306a36Sopenharmony_ci	wmb();
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	writel_relaxed(CMD_GLOBAL_CLEAR, vfe->base + VFE_IRQ_CMD);
36462306a36Sopenharmony_ci	writel_relaxed(1, vfe->base + VFE_BUS_IRQ_CLEAR_GLOBAL);
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	if (status0 & STATUS_0_RESET_ACK)
36762306a36Sopenharmony_ci		vfe->isr_ops.reset_ack(vfe);
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	for (i = VFE_LINE_RDI0; i <= VFE_LINE_RDI2; i++)
37062306a36Sopenharmony_ci		if (status0 & STATUS_0_RDI_REG_UPDATE(i))
37162306a36Sopenharmony_ci			vfe->isr_ops.reg_update(vfe, i);
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	for (i = VFE_LINE_RDI0; i <= VFE_LINE_RDI2; i++)
37462306a36Sopenharmony_ci		if (status0 & STATUS_1_RDI_SOF(i))
37562306a36Sopenharmony_ci			vfe->isr_ops.sof(vfe, i);
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	for (i = 0; i < MSM_VFE_COMPOSITE_IRQ_NUM; i++)
37862306a36Sopenharmony_ci		if (vfe_bus_status[0] & STATUS0_COMP_BUF_DONE(i))
37962306a36Sopenharmony_ci			vfe->isr_ops.comp_done(vfe, i);
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci	for (wm = 0; wm < MSM_VFE_IMAGE_MASTERS_NUM; wm++)
38262306a36Sopenharmony_ci		if (status0 & BIT(9))
38362306a36Sopenharmony_ci			if (vfe_bus_status[1] & STATUS1_WM_CLIENT_BUF_DONE(wm))
38462306a36Sopenharmony_ci				vfe->isr_ops.wm_done(vfe, wm);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	return IRQ_HANDLED;
38762306a36Sopenharmony_ci}
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci/*
39062306a36Sopenharmony_ci * vfe_halt - Trigger halt on VFE module and wait to complete
39162306a36Sopenharmony_ci * @vfe: VFE device
39262306a36Sopenharmony_ci *
39362306a36Sopenharmony_ci * Return 0 on success or a negative error code otherwise
39462306a36Sopenharmony_ci */
39562306a36Sopenharmony_cistatic int vfe_halt(struct vfe_device *vfe)
39662306a36Sopenharmony_ci{
39762306a36Sopenharmony_ci	/* rely on vfe_disable_output() to stop the VFE */
39862306a36Sopenharmony_ci	return 0;
39962306a36Sopenharmony_ci}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_cistatic int vfe_get_output(struct vfe_line *line)
40262306a36Sopenharmony_ci{
40362306a36Sopenharmony_ci	struct vfe_device *vfe = to_vfe(line);
40462306a36Sopenharmony_ci	struct vfe_output *output;
40562306a36Sopenharmony_ci	unsigned long flags;
40662306a36Sopenharmony_ci	int wm_idx;
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci	spin_lock_irqsave(&vfe->output_lock, flags);
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	output = &line->output;
41162306a36Sopenharmony_ci	if (output->state > VFE_OUTPUT_RESERVED) {
41262306a36Sopenharmony_ci		dev_err(vfe->camss->dev, "Output is running\n");
41362306a36Sopenharmony_ci		goto error;
41462306a36Sopenharmony_ci	}
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	output->wm_num = 1;
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci	wm_idx = vfe_reserve_wm(vfe, line->id);
41962306a36Sopenharmony_ci	if (wm_idx < 0) {
42062306a36Sopenharmony_ci		dev_err(vfe->camss->dev, "Can not reserve wm\n");
42162306a36Sopenharmony_ci		goto error_get_wm;
42262306a36Sopenharmony_ci	}
42362306a36Sopenharmony_ci	output->wm_idx[0] = wm_idx;
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	output->drop_update_idx = 0;
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	spin_unlock_irqrestore(&vfe->output_lock, flags);
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci	return 0;
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_cierror_get_wm:
43262306a36Sopenharmony_ci	vfe_release_wm(vfe, output->wm_idx[0]);
43362306a36Sopenharmony_ci	output->state = VFE_OUTPUT_OFF;
43462306a36Sopenharmony_cierror:
43562306a36Sopenharmony_ci	spin_unlock_irqrestore(&vfe->output_lock, flags);
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	return -EINVAL;
43862306a36Sopenharmony_ci}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_cistatic int vfe_enable_output(struct vfe_line *line)
44162306a36Sopenharmony_ci{
44262306a36Sopenharmony_ci	struct vfe_device *vfe = to_vfe(line);
44362306a36Sopenharmony_ci	struct vfe_output *output = &line->output;
44462306a36Sopenharmony_ci	const struct vfe_hw_ops *ops = vfe->ops;
44562306a36Sopenharmony_ci	struct media_entity *sensor;
44662306a36Sopenharmony_ci	unsigned long flags;
44762306a36Sopenharmony_ci	unsigned int frame_skip = 0;
44862306a36Sopenharmony_ci	unsigned int i;
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	sensor = camss_find_sensor(&line->subdev.entity);
45162306a36Sopenharmony_ci	if (sensor) {
45262306a36Sopenharmony_ci		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
45562306a36Sopenharmony_ci		/* Max frame skip is 29 frames */
45662306a36Sopenharmony_ci		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
45762306a36Sopenharmony_ci			frame_skip = VFE_FRAME_DROP_VAL - 1;
45862306a36Sopenharmony_ci	}
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	spin_lock_irqsave(&vfe->output_lock, flags);
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	ops->reg_update_clear(vfe, line->id);
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci	if (output->state > VFE_OUTPUT_RESERVED) {
46562306a36Sopenharmony_ci		dev_err(vfe->camss->dev, "Output is not in reserved state %d\n",
46662306a36Sopenharmony_ci			output->state);
46762306a36Sopenharmony_ci		spin_unlock_irqrestore(&vfe->output_lock, flags);
46862306a36Sopenharmony_ci		return -EINVAL;
46962306a36Sopenharmony_ci	}
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci	WARN_ON(output->gen2.active_num);
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	output->state = VFE_OUTPUT_ON;
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	output->sequence = 0;
47662306a36Sopenharmony_ci	output->wait_reg_update = 0;
47762306a36Sopenharmony_ci	reinit_completion(&output->reg_update);
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci	vfe_wm_start(vfe, output->wm_idx[0], line);
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	for (i = 0; i < 2; i++) {
48262306a36Sopenharmony_ci		output->buf[i] = vfe_buf_get_pending(output);
48362306a36Sopenharmony_ci		if (!output->buf[i])
48462306a36Sopenharmony_ci			break;
48562306a36Sopenharmony_ci		output->gen2.active_num++;
48662306a36Sopenharmony_ci		vfe_wm_update(vfe, output->wm_idx[0], output->buf[i]->addr[0], line);
48762306a36Sopenharmony_ci	}
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci	ops->reg_update(vfe, line->id);
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	spin_unlock_irqrestore(&vfe->output_lock, flags);
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci	return 0;
49462306a36Sopenharmony_ci}
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_cistatic void vfe_disable_output(struct vfe_line *line)
49762306a36Sopenharmony_ci{
49862306a36Sopenharmony_ci	struct vfe_device *vfe = to_vfe(line);
49962306a36Sopenharmony_ci	struct vfe_output *output = &line->output;
50062306a36Sopenharmony_ci	unsigned long flags;
50162306a36Sopenharmony_ci	unsigned int i;
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	spin_lock_irqsave(&vfe->output_lock, flags);
50462306a36Sopenharmony_ci	for (i = 0; i < output->wm_num; i++)
50562306a36Sopenharmony_ci		vfe_wm_stop(vfe, output->wm_idx[i]);
50662306a36Sopenharmony_ci	output->gen2.active_num = 0;
50762306a36Sopenharmony_ci	spin_unlock_irqrestore(&vfe->output_lock, flags);
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci	vfe_reset(vfe);
51062306a36Sopenharmony_ci}
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci/*
51362306a36Sopenharmony_ci * vfe_enable - Enable streaming on VFE line
51462306a36Sopenharmony_ci * @line: VFE line
51562306a36Sopenharmony_ci *
51662306a36Sopenharmony_ci * Return 0 on success or a negative error code otherwise
51762306a36Sopenharmony_ci */
51862306a36Sopenharmony_cistatic int vfe_enable(struct vfe_line *line)
51962306a36Sopenharmony_ci{
52062306a36Sopenharmony_ci	struct vfe_device *vfe = to_vfe(line);
52162306a36Sopenharmony_ci	int ret;
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci	mutex_lock(&vfe->stream_lock);
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	if (!vfe->stream_count)
52662306a36Sopenharmony_ci		vfe_enable_irq_common(vfe);
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci	vfe->stream_count++;
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	mutex_unlock(&vfe->stream_lock);
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci	ret = vfe_get_output(line);
53362306a36Sopenharmony_ci	if (ret < 0)
53462306a36Sopenharmony_ci		goto error_get_output;
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci	ret = vfe_enable_output(line);
53762306a36Sopenharmony_ci	if (ret < 0)
53862306a36Sopenharmony_ci		goto error_enable_output;
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ci	vfe->was_streaming = 1;
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci	return 0;
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_cierror_enable_output:
54562306a36Sopenharmony_ci	vfe_put_output(line);
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_cierror_get_output:
54862306a36Sopenharmony_ci	mutex_lock(&vfe->stream_lock);
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci	vfe->stream_count--;
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_ci	mutex_unlock(&vfe->stream_lock);
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	return ret;
55562306a36Sopenharmony_ci}
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci/*
55862306a36Sopenharmony_ci * vfe_disable - Disable streaming on VFE line
55962306a36Sopenharmony_ci * @line: VFE line
56062306a36Sopenharmony_ci *
56162306a36Sopenharmony_ci * Return 0 on success or a negative error code otherwise
56262306a36Sopenharmony_ci */
56362306a36Sopenharmony_cistatic int vfe_disable(struct vfe_line *line)
56462306a36Sopenharmony_ci{
56562306a36Sopenharmony_ci	struct vfe_device *vfe = to_vfe(line);
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci	vfe_disable_output(line);
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	vfe_put_output(line);
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci	mutex_lock(&vfe->stream_lock);
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	vfe->stream_count--;
57462306a36Sopenharmony_ci
57562306a36Sopenharmony_ci	mutex_unlock(&vfe->stream_lock);
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ci	return 0;
57862306a36Sopenharmony_ci}
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci/*
58162306a36Sopenharmony_ci * vfe_isr_sof - Process start of frame interrupt
58262306a36Sopenharmony_ci * @vfe: VFE Device
58362306a36Sopenharmony_ci * @line_id: VFE line
58462306a36Sopenharmony_ci */
58562306a36Sopenharmony_cistatic void vfe_isr_sof(struct vfe_device *vfe, enum vfe_line_id line_id)
58662306a36Sopenharmony_ci{
58762306a36Sopenharmony_ci	/* nop */
58862306a36Sopenharmony_ci}
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci/*
59162306a36Sopenharmony_ci * vfe_isr_reg_update - Process reg update interrupt
59262306a36Sopenharmony_ci * @vfe: VFE Device
59362306a36Sopenharmony_ci * @line_id: VFE line
59462306a36Sopenharmony_ci */
59562306a36Sopenharmony_cistatic void vfe_isr_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id)
59662306a36Sopenharmony_ci{
59762306a36Sopenharmony_ci	struct vfe_output *output;
59862306a36Sopenharmony_ci	unsigned long flags;
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci	spin_lock_irqsave(&vfe->output_lock, flags);
60162306a36Sopenharmony_ci	vfe->ops->reg_update_clear(vfe, line_id);
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	output = &vfe->line[line_id].output;
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	if (output->wait_reg_update) {
60662306a36Sopenharmony_ci		output->wait_reg_update = 0;
60762306a36Sopenharmony_ci		complete(&output->reg_update);
60862306a36Sopenharmony_ci	}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	spin_unlock_irqrestore(&vfe->output_lock, flags);
61162306a36Sopenharmony_ci}
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci/*
61462306a36Sopenharmony_ci * vfe_isr_wm_done - Process write master done interrupt
61562306a36Sopenharmony_ci * @vfe: VFE Device
61662306a36Sopenharmony_ci * @wm: Write master id
61762306a36Sopenharmony_ci */
61862306a36Sopenharmony_cistatic void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm)
61962306a36Sopenharmony_ci{
62062306a36Sopenharmony_ci	struct vfe_line *line = &vfe->line[vfe->wm_output_map[wm]];
62162306a36Sopenharmony_ci	struct camss_buffer *ready_buf;
62262306a36Sopenharmony_ci	struct vfe_output *output;
62362306a36Sopenharmony_ci	unsigned long flags;
62462306a36Sopenharmony_ci	u32 index;
62562306a36Sopenharmony_ci	u64 ts = ktime_get_ns();
62662306a36Sopenharmony_ci
62762306a36Sopenharmony_ci	spin_lock_irqsave(&vfe->output_lock, flags);
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci	if (vfe->wm_output_map[wm] == VFE_LINE_NONE) {
63062306a36Sopenharmony_ci		dev_err_ratelimited(vfe->camss->dev,
63162306a36Sopenharmony_ci				    "Received wm done for unmapped index\n");
63262306a36Sopenharmony_ci		goto out_unlock;
63362306a36Sopenharmony_ci	}
63462306a36Sopenharmony_ci	output = &vfe->line[vfe->wm_output_map[wm]].output;
63562306a36Sopenharmony_ci
63662306a36Sopenharmony_ci	ready_buf = output->buf[0];
63762306a36Sopenharmony_ci	if (!ready_buf) {
63862306a36Sopenharmony_ci		dev_err_ratelimited(vfe->camss->dev,
63962306a36Sopenharmony_ci				    "Missing ready buf %d!\n", output->state);
64062306a36Sopenharmony_ci		goto out_unlock;
64162306a36Sopenharmony_ci	}
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	ready_buf->vb.vb2_buf.timestamp = ts;
64462306a36Sopenharmony_ci	ready_buf->vb.sequence = output->sequence++;
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci	index = 0;
64762306a36Sopenharmony_ci	output->buf[0] = output->buf[1];
64862306a36Sopenharmony_ci	if (output->buf[0])
64962306a36Sopenharmony_ci		index = 1;
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci	output->buf[index] = vfe_buf_get_pending(output);
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_ci	if (output->buf[index])
65462306a36Sopenharmony_ci		vfe_wm_update(vfe, output->wm_idx[0], output->buf[index]->addr[0], line);
65562306a36Sopenharmony_ci	else
65662306a36Sopenharmony_ci		output->gen2.active_num--;
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci	spin_unlock_irqrestore(&vfe->output_lock, flags);
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci	vb2_buffer_done(&ready_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci	return;
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ciout_unlock:
66562306a36Sopenharmony_ci	spin_unlock_irqrestore(&vfe->output_lock, flags);
66662306a36Sopenharmony_ci}
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci/*
66962306a36Sopenharmony_ci * vfe_pm_domain_off - Disable power domains specific to this VFE.
67062306a36Sopenharmony_ci * @vfe: VFE Device
67162306a36Sopenharmony_ci */
67262306a36Sopenharmony_cistatic void vfe_pm_domain_off(struct vfe_device *vfe)
67362306a36Sopenharmony_ci{
67462306a36Sopenharmony_ci	struct camss *camss = vfe->camss;
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci	if (vfe->id >= camss->vfe_num)
67762306a36Sopenharmony_ci		return;
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	device_link_del(camss->genpd_link[vfe->id]);
68062306a36Sopenharmony_ci}
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_ci/*
68362306a36Sopenharmony_ci * vfe_pm_domain_on - Enable power domains specific to this VFE.
68462306a36Sopenharmony_ci * @vfe: VFE Device
68562306a36Sopenharmony_ci */
68662306a36Sopenharmony_cistatic int vfe_pm_domain_on(struct vfe_device *vfe)
68762306a36Sopenharmony_ci{
68862306a36Sopenharmony_ci	struct camss *camss = vfe->camss;
68962306a36Sopenharmony_ci	enum vfe_line_id id = vfe->id;
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci	if (id >= camss->vfe_num)
69262306a36Sopenharmony_ci		return 0;
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_ci	camss->genpd_link[id] = device_link_add(camss->dev, camss->genpd[id],
69562306a36Sopenharmony_ci						DL_FLAG_STATELESS |
69662306a36Sopenharmony_ci						DL_FLAG_PM_RUNTIME |
69762306a36Sopenharmony_ci						DL_FLAG_RPM_ACTIVE);
69862306a36Sopenharmony_ci	if (!camss->genpd_link[id])
69962306a36Sopenharmony_ci		return -EINVAL;
70062306a36Sopenharmony_ci
70162306a36Sopenharmony_ci	return 0;
70262306a36Sopenharmony_ci}
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ci/*
70562306a36Sopenharmony_ci * vfe_queue_buffer - Add empty buffer
70662306a36Sopenharmony_ci * @vid: Video device structure
70762306a36Sopenharmony_ci * @buf: Buffer to be enqueued
70862306a36Sopenharmony_ci *
70962306a36Sopenharmony_ci * Add an empty buffer - depending on the current number of buffers it will be
71062306a36Sopenharmony_ci * put in pending buffer queue or directly given to the hardware to be filled.
71162306a36Sopenharmony_ci *
71262306a36Sopenharmony_ci * Return 0 on success or a negative error code otherwise
71362306a36Sopenharmony_ci */
71462306a36Sopenharmony_cistatic int vfe_queue_buffer(struct camss_video *vid,
71562306a36Sopenharmony_ci			    struct camss_buffer *buf)
71662306a36Sopenharmony_ci{
71762306a36Sopenharmony_ci	struct vfe_line *line = container_of(vid, struct vfe_line, video_out);
71862306a36Sopenharmony_ci	struct vfe_device *vfe = to_vfe(line);
71962306a36Sopenharmony_ci	struct vfe_output *output;
72062306a36Sopenharmony_ci	unsigned long flags;
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	output = &line->output;
72362306a36Sopenharmony_ci
72462306a36Sopenharmony_ci	spin_lock_irqsave(&vfe->output_lock, flags);
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci	if (output->state == VFE_OUTPUT_ON && output->gen2.active_num < 2) {
72762306a36Sopenharmony_ci		output->buf[output->gen2.active_num++] = buf;
72862306a36Sopenharmony_ci		vfe_wm_update(vfe, output->wm_idx[0], buf->addr[0], line);
72962306a36Sopenharmony_ci	} else {
73062306a36Sopenharmony_ci		vfe_buf_add_pending(output, buf);
73162306a36Sopenharmony_ci	}
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci	spin_unlock_irqrestore(&vfe->output_lock, flags);
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci	return 0;
73662306a36Sopenharmony_ci}
73762306a36Sopenharmony_ci
73862306a36Sopenharmony_cistatic const struct vfe_isr_ops vfe_isr_ops_170 = {
73962306a36Sopenharmony_ci	.reset_ack = vfe_isr_reset_ack,
74062306a36Sopenharmony_ci	.halt_ack = vfe_isr_halt_ack,
74162306a36Sopenharmony_ci	.reg_update = vfe_isr_reg_update,
74262306a36Sopenharmony_ci	.sof = vfe_isr_sof,
74362306a36Sopenharmony_ci	.comp_done = vfe_isr_comp_done,
74462306a36Sopenharmony_ci	.wm_done = vfe_isr_wm_done,
74562306a36Sopenharmony_ci};
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_cistatic const struct camss_video_ops vfe_video_ops_170 = {
74862306a36Sopenharmony_ci	.queue_buffer = vfe_queue_buffer,
74962306a36Sopenharmony_ci	.flush_buffers = vfe_flush_buffers,
75062306a36Sopenharmony_ci};
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_cistatic void vfe_subdev_init(struct device *dev, struct vfe_device *vfe)
75362306a36Sopenharmony_ci{
75462306a36Sopenharmony_ci	vfe->isr_ops = vfe_isr_ops_170;
75562306a36Sopenharmony_ci	vfe->video_ops = vfe_video_ops_170;
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ci	vfe->line_num = VFE_LINE_NUM_GEN2;
75862306a36Sopenharmony_ci}
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ciconst struct vfe_hw_ops vfe_ops_170 = {
76162306a36Sopenharmony_ci	.global_reset = vfe_global_reset,
76262306a36Sopenharmony_ci	.hw_version = vfe_hw_version,
76362306a36Sopenharmony_ci	.isr_read = vfe_isr_read,
76462306a36Sopenharmony_ci	.isr = vfe_isr,
76562306a36Sopenharmony_ci	.pm_domain_off = vfe_pm_domain_off,
76662306a36Sopenharmony_ci	.pm_domain_on = vfe_pm_domain_on,
76762306a36Sopenharmony_ci	.reg_update_clear = vfe_reg_update_clear,
76862306a36Sopenharmony_ci	.reg_update = vfe_reg_update,
76962306a36Sopenharmony_ci	.subdev_init = vfe_subdev_init,
77062306a36Sopenharmony_ci	.vfe_disable = vfe_disable,
77162306a36Sopenharmony_ci	.vfe_enable = vfe_enable,
77262306a36Sopenharmony_ci	.vfe_halt = vfe_halt,
77362306a36Sopenharmony_ci	.violation_read = vfe_violation_read,
77462306a36Sopenharmony_ci};
775