162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/* Copyright (C) 2017 Intel Corporation */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#ifndef __IPU3_CIO2_H
562306a36Sopenharmony_ci#define __IPU3_CIO2_H
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/bits.h>
862306a36Sopenharmony_ci#include <linux/dma-mapping.h>
962306a36Sopenharmony_ci#include <linux/kernel.h>
1062306a36Sopenharmony_ci#include <linux/mutex.h>
1162306a36Sopenharmony_ci#include <linux/types.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <asm/page.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <media/media-device.h>
1662306a36Sopenharmony_ci#include <media/media-entity.h>
1762306a36Sopenharmony_ci#include <media/v4l2-async.h>
1862306a36Sopenharmony_ci#include <media/v4l2-dev.h>
1962306a36Sopenharmony_ci#include <media/v4l2-device.h>
2062306a36Sopenharmony_ci#include <media/v4l2-subdev.h>
2162306a36Sopenharmony_ci#include <media/videobuf2-core.h>
2262306a36Sopenharmony_ci#include <media/videobuf2-v4l2.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistruct cio2_fbpt_entry;		/* defined here, after the first usage */
2562306a36Sopenharmony_cistruct pci_dev;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define CIO2_NAME					"ipu3-cio2"
2862306a36Sopenharmony_ci#define CIO2_DEVICE_NAME				"Intel IPU3 CIO2"
2962306a36Sopenharmony_ci#define CIO2_ENTITY_NAME				"ipu3-csi2"
3062306a36Sopenharmony_ci#define CIO2_PCI_ID					0x9d32
3162306a36Sopenharmony_ci#define CIO2_PCI_BAR					0
3262306a36Sopenharmony_ci#define CIO2_DMA_MASK					DMA_BIT_MASK(39)
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#define CIO2_IMAGE_MAX_WIDTH				4224U
3562306a36Sopenharmony_ci#define CIO2_IMAGE_MAX_HEIGHT				3136U
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/* 32MB = 8xFBPT_entry */
3862306a36Sopenharmony_ci#define CIO2_MAX_LOPS					8
3962306a36Sopenharmony_ci#define CIO2_MAX_BUFFERS			(PAGE_SIZE / 16 / CIO2_MAX_LOPS)
4062306a36Sopenharmony_ci#define CIO2_LOP_ENTRIES			(PAGE_SIZE / sizeof(u32))
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define CIO2_PAD_SINK					0U
4362306a36Sopenharmony_ci#define CIO2_PAD_SOURCE					1U
4462306a36Sopenharmony_ci#define CIO2_PADS					2U
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define CIO2_NUM_DMA_CHAN				20U
4762306a36Sopenharmony_ci#define CIO2_NUM_PORTS					4U /* DPHYs */
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/* 1 for each sensor */
5062306a36Sopenharmony_ci#define CIO2_QUEUES					CIO2_NUM_PORTS
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci/* Register and bit field definitions */
5362306a36Sopenharmony_ci#define CIO2_REG_PIPE_BASE(n)			((n) * 0x0400)	/* n = 0..3 */
5462306a36Sopenharmony_ci#define CIO2_REG_CSIRX_BASE				0x000
5562306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_BASE				0x100
5662306a36Sopenharmony_ci#define CIO2_REG_PIXELGEN_BAS				0x200
5762306a36Sopenharmony_ci#define CIO2_REG_IRQCTRL_BASE				0x300
5862306a36Sopenharmony_ci#define CIO2_REG_GPREG_BASE				0x1000
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci/* base register: CIO2_REG_PIPE_BASE(pipe) * CIO2_REG_CSIRX_BASE */
6162306a36Sopenharmony_ci#define CIO2_REG_CSIRX_ENABLE			(CIO2_REG_CSIRX_BASE + 0x0)
6262306a36Sopenharmony_ci#define CIO2_REG_CSIRX_NOF_ENABLED_LANES	(CIO2_REG_CSIRX_BASE + 0x4)
6362306a36Sopenharmony_ci#define CIO2_REG_CSIRX_SP_IF_CONFIG		(CIO2_REG_CSIRX_BASE + 0x10)
6462306a36Sopenharmony_ci#define CIO2_REG_CSIRX_LP_IF_CONFIG		(CIO2_REG_CSIRX_BASE + 0x14)
6562306a36Sopenharmony_ci#define CIO2_CSIRX_IF_CONFIG_FILTEROUT			0x00
6662306a36Sopenharmony_ci#define CIO2_CSIRX_IF_CONFIG_FILTEROUT_VC_INACTIVE	0x01
6762306a36Sopenharmony_ci#define CIO2_CSIRX_IF_CONFIG_PASS			0x02
6862306a36Sopenharmony_ci#define CIO2_CSIRX_IF_CONFIG_FLAG_ERROR			BIT(2)
6962306a36Sopenharmony_ci#define CIO2_REG_CSIRX_STATUS			(CIO2_REG_CSIRX_BASE + 0x18)
7062306a36Sopenharmony_ci#define CIO2_REG_CSIRX_STATUS_DLANE_HS		(CIO2_REG_CSIRX_BASE + 0x1c)
7162306a36Sopenharmony_ci#define CIO2_CSIRX_STATUS_DLANE_HS_MASK			0xff
7262306a36Sopenharmony_ci#define CIO2_REG_CSIRX_STATUS_DLANE_LP		(CIO2_REG_CSIRX_BASE + 0x20)
7362306a36Sopenharmony_ci#define CIO2_CSIRX_STATUS_DLANE_LP_MASK			0xffffff
7462306a36Sopenharmony_ci/* Termination enable and settle in 0.0625ns units, lane=0..3 or -1 for clock */
7562306a36Sopenharmony_ci#define CIO2_REG_CSIRX_DLY_CNT_TERMEN(lane) \
7662306a36Sopenharmony_ci				(CIO2_REG_CSIRX_BASE + 0x2c + 8 * (lane))
7762306a36Sopenharmony_ci#define CIO2_REG_CSIRX_DLY_CNT_SETTLE(lane) \
7862306a36Sopenharmony_ci				(CIO2_REG_CSIRX_BASE + 0x30 + 8 * (lane))
7962306a36Sopenharmony_ci/* base register: CIO2_REG_PIPE_BASE(pipe) * CIO2_REG_MIPIBE_BASE */
8062306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_ENABLE		(CIO2_REG_MIPIBE_BASE + 0x0)
8162306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_STATUS		(CIO2_REG_MIPIBE_BASE + 0x4)
8262306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_COMP_FORMAT(vc) \
8362306a36Sopenharmony_ci				(CIO2_REG_MIPIBE_BASE + 0x8 + 0x4 * (vc))
8462306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_FORCE_RAW8	(CIO2_REG_MIPIBE_BASE + 0x20)
8562306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_FORCE_RAW8_ENABLE		BIT(0)
8662306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_FORCE_RAW8_USE_TYPEID		BIT(1)
8762306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_FORCE_RAW8_TYPEID_SHIFT		2U
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_IRQ_STATUS	(CIO2_REG_MIPIBE_BASE + 0x24)
9062306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_IRQ_CLEAR	(CIO2_REG_MIPIBE_BASE + 0x28)
9162306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_GLOBAL_LUT_DISREGARD (CIO2_REG_MIPIBE_BASE + 0x68)
9262306a36Sopenharmony_ci#define CIO2_MIPIBE_GLOBAL_LUT_DISREGARD		1U
9362306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_PKT_STALL_STATUS (CIO2_REG_MIPIBE_BASE + 0x6c)
9462306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_PARSE_GSP_THROUGH_LP_LUT_REG_IDX \
9562306a36Sopenharmony_ci					(CIO2_REG_MIPIBE_BASE + 0x70)
9662306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_SP_LUT_ENTRY(vc) \
9762306a36Sopenharmony_ci				       (CIO2_REG_MIPIBE_BASE + 0x74 + 4 * (vc))
9862306a36Sopenharmony_ci#define CIO2_REG_MIPIBE_LP_LUT_ENTRY(m)	/* m = 0..15 */ \
9962306a36Sopenharmony_ci					(CIO2_REG_MIPIBE_BASE + 0x84 + 4 * (m))
10062306a36Sopenharmony_ci#define CIO2_MIPIBE_LP_LUT_ENTRY_DISREGARD		1U
10162306a36Sopenharmony_ci#define CIO2_MIPIBE_LP_LUT_ENTRY_SID_SHIFT		1U
10262306a36Sopenharmony_ci#define CIO2_MIPIBE_LP_LUT_ENTRY_VC_SHIFT		5U
10362306a36Sopenharmony_ci#define CIO2_MIPIBE_LP_LUT_ENTRY_FORMAT_TYPE_SHIFT	7U
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci/* base register: CIO2_REG_PIPE_BASE(pipe) * CIO2_REG_IRQCTRL_BASE */
10662306a36Sopenharmony_ci/* IRQ registers are 18-bit wide, see cio2_irq_error for bit definitions */
10762306a36Sopenharmony_ci#define CIO2_REG_IRQCTRL_EDGE		(CIO2_REG_IRQCTRL_BASE + 0x00)
10862306a36Sopenharmony_ci#define CIO2_REG_IRQCTRL_MASK		(CIO2_REG_IRQCTRL_BASE + 0x04)
10962306a36Sopenharmony_ci#define CIO2_REG_IRQCTRL_STATUS		(CIO2_REG_IRQCTRL_BASE + 0x08)
11062306a36Sopenharmony_ci#define CIO2_REG_IRQCTRL_CLEAR		(CIO2_REG_IRQCTRL_BASE + 0x0c)
11162306a36Sopenharmony_ci#define CIO2_REG_IRQCTRL_ENABLE		(CIO2_REG_IRQCTRL_BASE + 0x10)
11262306a36Sopenharmony_ci#define CIO2_REG_IRQCTRL_LEVEL_NOT_PULSE	(CIO2_REG_IRQCTRL_BASE + 0x14)
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci#define CIO2_REG_GPREG_SRST		(CIO2_REG_GPREG_BASE + 0x0)
11562306a36Sopenharmony_ci#define CIO2_GPREG_SRST_ALL				0xffff	/* Reset all */
11662306a36Sopenharmony_ci#define CIO2_REG_FB_HPLL_FREQ		(CIO2_REG_GPREG_BASE + 0x08)
11762306a36Sopenharmony_ci#define CIO2_REG_ISCLK_RATIO		(CIO2_REG_GPREG_BASE + 0xc)
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci#define CIO2_REG_CGC					0x1400
12062306a36Sopenharmony_ci#define CIO2_CGC_CSI2_TGE				BIT(0)
12162306a36Sopenharmony_ci#define CIO2_CGC_PRIM_TGE				BIT(1)
12262306a36Sopenharmony_ci#define CIO2_CGC_SIDE_TGE				BIT(2)
12362306a36Sopenharmony_ci#define CIO2_CGC_XOSC_TGE				BIT(3)
12462306a36Sopenharmony_ci#define CIO2_CGC_MPLL_SHUTDOWN_EN			BIT(4)
12562306a36Sopenharmony_ci#define CIO2_CGC_D3I3_TGE				BIT(5)
12662306a36Sopenharmony_ci#define CIO2_CGC_CSI2_INTERFRAME_TGE			BIT(6)
12762306a36Sopenharmony_ci#define CIO2_CGC_CSI2_PORT_DCGE				BIT(8)
12862306a36Sopenharmony_ci#define CIO2_CGC_CSI2_DCGE				BIT(9)
12962306a36Sopenharmony_ci#define CIO2_CGC_SIDE_DCGE				BIT(10)
13062306a36Sopenharmony_ci#define CIO2_CGC_PRIM_DCGE				BIT(11)
13162306a36Sopenharmony_ci#define CIO2_CGC_ROSC_DCGE				BIT(12)
13262306a36Sopenharmony_ci#define CIO2_CGC_XOSC_DCGE				BIT(13)
13362306a36Sopenharmony_ci#define CIO2_CGC_FLIS_DCGE				BIT(14)
13462306a36Sopenharmony_ci#define CIO2_CGC_CLKGATE_HOLDOFF_SHIFT			20U
13562306a36Sopenharmony_ci#define CIO2_CGC_CSI_CLKGATE_HOLDOFF_SHIFT		24U
13662306a36Sopenharmony_ci#define CIO2_REG_D0I3C					0x1408
13762306a36Sopenharmony_ci#define CIO2_D0I3C_I3					BIT(2)	/* Set D0I3 */
13862306a36Sopenharmony_ci#define CIO2_D0I3C_RR					BIT(3)	/* Restore? */
13962306a36Sopenharmony_ci#define CIO2_REG_SWRESET				0x140c
14062306a36Sopenharmony_ci#define CIO2_SWRESET_SWRESET				1U
14162306a36Sopenharmony_ci#define CIO2_REG_SENSOR_ACTIVE				0x1410
14262306a36Sopenharmony_ci#define CIO2_REG_INT_STS				0x1414
14362306a36Sopenharmony_ci#define CIO2_REG_INT_STS_EXT_OE				0x1418
14462306a36Sopenharmony_ci#define CIO2_INT_EXT_OE_DMAOE_SHIFT			0U
14562306a36Sopenharmony_ci#define CIO2_INT_EXT_OE_DMAOE_MASK			0x7ffff
14662306a36Sopenharmony_ci#define CIO2_INT_EXT_OE_OES_SHIFT			24U
14762306a36Sopenharmony_ci#define CIO2_INT_EXT_OE_OES_MASK	(0xf << CIO2_INT_EXT_OE_OES_SHIFT)
14862306a36Sopenharmony_ci#define CIO2_REG_INT_EN					0x1420
14962306a36Sopenharmony_ci#define CIO2_REG_INT_EN_IRQ				(1 << 24)
15062306a36Sopenharmony_ci#define CIO2_REG_INT_EN_IOS(dma)	(1U << (((dma) >> 1U) + 12U))
15162306a36Sopenharmony_ci/*
15262306a36Sopenharmony_ci * Interrupt on completion bit, Eg. DMA 0-3 maps to bit 0-3,
15362306a36Sopenharmony_ci * DMA4 & DMA5 map to bit 4 ... DMA18 & DMA19 map to bit 11 Et cetera
15462306a36Sopenharmony_ci */
15562306a36Sopenharmony_ci#define CIO2_INT_IOC(dma)	(1U << ((dma) < 4U ? (dma) : ((dma) >> 1U) + 2U))
15662306a36Sopenharmony_ci#define CIO2_INT_IOC_SHIFT				0
15762306a36Sopenharmony_ci#define CIO2_INT_IOC_MASK		(0x7ff << CIO2_INT_IOC_SHIFT)
15862306a36Sopenharmony_ci#define CIO2_INT_IOS_IOLN(dma)		(1U << (((dma) >> 1U) + 12U))
15962306a36Sopenharmony_ci#define CIO2_INT_IOS_IOLN_SHIFT				12
16062306a36Sopenharmony_ci#define CIO2_INT_IOS_IOLN_MASK		(0x3ff << CIO2_INT_IOS_IOLN_SHIFT)
16162306a36Sopenharmony_ci#define CIO2_INT_IOIE					BIT(22)
16262306a36Sopenharmony_ci#define CIO2_INT_IOOE					BIT(23)
16362306a36Sopenharmony_ci#define CIO2_INT_IOIRQ					BIT(24)
16462306a36Sopenharmony_ci#define CIO2_REG_INT_EN_EXT_OE				0x1424
16562306a36Sopenharmony_ci#define CIO2_REG_DMA_DBG				0x1448
16662306a36Sopenharmony_ci#define CIO2_REG_DMA_DBG_DMA_INDEX_SHIFT		0U
16762306a36Sopenharmony_ci#define CIO2_REG_PBM_ARB_CTRL				0x1460
16862306a36Sopenharmony_ci#define CIO2_PBM_ARB_CTRL_LANES_DIV			0U /* 4-4-2-2 lanes */
16962306a36Sopenharmony_ci#define CIO2_PBM_ARB_CTRL_LANES_DIV_SHIFT		0U
17062306a36Sopenharmony_ci#define CIO2_PBM_ARB_CTRL_LE_EN				BIT(7)
17162306a36Sopenharmony_ci#define CIO2_PBM_ARB_CTRL_PLL_POST_SHTDN		2U
17262306a36Sopenharmony_ci#define CIO2_PBM_ARB_CTRL_PLL_POST_SHTDN_SHIFT		8U
17362306a36Sopenharmony_ci#define CIO2_PBM_ARB_CTRL_PLL_AHD_WK_UP			480U
17462306a36Sopenharmony_ci#define CIO2_PBM_ARB_CTRL_PLL_AHD_WK_UP_SHIFT		16U
17562306a36Sopenharmony_ci#define CIO2_REG_PBM_WMCTRL1				0x1464
17662306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL1_MIN_2CK_SHIFT			0U
17762306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL1_MID1_2CK_SHIFT			8U
17862306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL1_MID2_2CK_SHIFT			16U
17962306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL1_TS_COUNT_DISABLE		BIT(31)
18062306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL1_MIN_2CK	(4 << CIO2_PBM_WMCTRL1_MIN_2CK_SHIFT)
18162306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL1_MID1_2CK	(16 << CIO2_PBM_WMCTRL1_MID1_2CK_SHIFT)
18262306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL1_MID2_2CK	(21 << CIO2_PBM_WMCTRL1_MID2_2CK_SHIFT)
18362306a36Sopenharmony_ci#define CIO2_REG_PBM_WMCTRL2				0x1468
18462306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_HWM_2CK			40U
18562306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_HWM_2CK_SHIFT			0U
18662306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_LWM_2CK			22U
18762306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_LWM_2CK_SHIFT			8U
18862306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_OBFFWM_2CK			2U
18962306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_OBFFWM_2CK_SHIFT		16U
19062306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_TRANSDYN			1U
19162306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_TRANSDYN_SHIFT			24U
19262306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_DYNWMEN			BIT(28)
19362306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_OBFF_MEM_EN			BIT(29)
19462306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_OBFF_CPU_EN			BIT(30)
19562306a36Sopenharmony_ci#define CIO2_PBM_WMCTRL2_DRAINNOW			BIT(31)
19662306a36Sopenharmony_ci#define CIO2_REG_PBM_TS_COUNT				0x146c
19762306a36Sopenharmony_ci#define CIO2_REG_PBM_FOPN_ABORT				0x1474
19862306a36Sopenharmony_ci/* below n = 0..3 */
19962306a36Sopenharmony_ci#define CIO2_PBM_FOPN_ABORT(n)				(0x1 << 8U * (n))
20062306a36Sopenharmony_ci#define CIO2_PBM_FOPN_FORCE_ABORT(n)			(0x2 << 8U * (n))
20162306a36Sopenharmony_ci#define CIO2_PBM_FOPN_FRAMEOPEN(n)			(0x8 << 8U * (n))
20262306a36Sopenharmony_ci#define CIO2_REG_LTRCTRL				0x1480
20362306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRDYNEN				BIT(16)
20462306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSTABLETIME_SHIFT		8U
20562306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSTABLETIME_MASK			0xff
20662306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSEL1S3				BIT(7)
20762306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSEL1S2				BIT(6)
20862306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSEL1S1				BIT(5)
20962306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSEL1S0				BIT(4)
21062306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSEL2S3				BIT(3)
21162306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSEL2S2				BIT(2)
21262306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSEL2S1				BIT(1)
21362306a36Sopenharmony_ci#define CIO2_LTRCTRL_LTRSEL2S0				BIT(0)
21462306a36Sopenharmony_ci#define CIO2_REG_LTRVAL23				0x1484
21562306a36Sopenharmony_ci#define CIO2_REG_LTRVAL01				0x1488
21662306a36Sopenharmony_ci#define CIO2_LTRVAL02_VAL_SHIFT				0U
21762306a36Sopenharmony_ci#define CIO2_LTRVAL02_SCALE_SHIFT			10U
21862306a36Sopenharmony_ci#define CIO2_LTRVAL13_VAL_SHIFT				16U
21962306a36Sopenharmony_ci#define CIO2_LTRVAL13_SCALE_SHIFT			26U
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci#define CIO2_LTRVAL0_VAL				175U
22262306a36Sopenharmony_ci/* Value times 1024 ns */
22362306a36Sopenharmony_ci#define CIO2_LTRVAL0_SCALE				2U
22462306a36Sopenharmony_ci#define CIO2_LTRVAL1_VAL				90U
22562306a36Sopenharmony_ci#define CIO2_LTRVAL1_SCALE				2U
22662306a36Sopenharmony_ci#define CIO2_LTRVAL2_VAL				90U
22762306a36Sopenharmony_ci#define CIO2_LTRVAL2_SCALE				2U
22862306a36Sopenharmony_ci#define CIO2_LTRVAL3_VAL				90U
22962306a36Sopenharmony_ci#define CIO2_LTRVAL3_SCALE				2U
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci#define CIO2_REG_CDMABA(n)		(0x1500 + 0x10 * (n))	/* n = 0..19 */
23262306a36Sopenharmony_ci#define CIO2_REG_CDMARI(n)		(0x1504 + 0x10 * (n))
23362306a36Sopenharmony_ci#define CIO2_CDMARI_FBPT_RP_SHIFT			0U
23462306a36Sopenharmony_ci#define CIO2_CDMARI_FBPT_RP_MASK			0xff
23562306a36Sopenharmony_ci#define CIO2_REG_CDMAC0(n)		(0x1508 + 0x10 * (n))
23662306a36Sopenharmony_ci#define CIO2_CDMAC0_FBPT_LEN_SHIFT			0U
23762306a36Sopenharmony_ci#define CIO2_CDMAC0_FBPT_WIDTH_SHIFT			8U
23862306a36Sopenharmony_ci#define CIO2_CDMAC0_FBPT_NS				BIT(25)
23962306a36Sopenharmony_ci#define CIO2_CDMAC0_DMA_INTR_ON_FS			BIT(26)
24062306a36Sopenharmony_ci#define CIO2_CDMAC0_DMA_INTR_ON_FE			BIT(27)
24162306a36Sopenharmony_ci#define CIO2_CDMAC0_FBPT_UPDATE_FIFO_FULL		BIT(28)
24262306a36Sopenharmony_ci#define CIO2_CDMAC0_FBPT_FIFO_FULL_FIX_DIS		BIT(29)
24362306a36Sopenharmony_ci#define CIO2_CDMAC0_DMA_EN				BIT(30)
24462306a36Sopenharmony_ci#define CIO2_CDMAC0_DMA_HALTED				BIT(31)
24562306a36Sopenharmony_ci#define CIO2_REG_CDMAC1(n)		(0x150c + 0x10 * (n))
24662306a36Sopenharmony_ci#define CIO2_CDMAC1_LINENUMINT_SHIFT			0U
24762306a36Sopenharmony_ci#define CIO2_CDMAC1_LINENUMUPDATE_SHIFT			16U
24862306a36Sopenharmony_ci/* n = 0..3 */
24962306a36Sopenharmony_ci#define CIO2_REG_PXM_PXF_FMT_CFG0(n)	(0x1700 + 0x30 * (n))
25062306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_SID0_SHIFT			0U
25162306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_SID1_SHIFT			16U
25262306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_PCK_64B			(0 << 0)
25362306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_PCK_32B			(1 << 0)
25462306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_BPP_08			(0 << 2)
25562306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_BPP_10			(1 << 2)
25662306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_BPP_12			(2 << 2)
25762306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_BPP_14			(3 << 2)
25862306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_SPEC_4PPC			(0 << 4)
25962306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_SPEC_3PPC_RGBA		(1 << 4)
26062306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_SPEC_3PPC_ARGB		(2 << 4)
26162306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_SPEC_PLANAR2		(3 << 4)
26262306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_SPEC_PLANAR3		(4 << 4)
26362306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_SPEC_NV16			(5 << 4)
26462306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_PSWAP4_1ST_AB		(1 << 7)
26562306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_PSWAP4_1ST_CD		(1 << 8)
26662306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_PSWAP4_2ND_AC		(1 << 9)
26762306a36Sopenharmony_ci#define CIO2_PXM_PXF_FMT_CFG_PSWAP4_2ND_BD		(1 << 10)
26862306a36Sopenharmony_ci#define CIO2_REG_INT_STS_EXT_IE				0x17e4
26962306a36Sopenharmony_ci#define CIO2_REG_INT_EN_EXT_IE				0x17e8
27062306a36Sopenharmony_ci#define CIO2_INT_EXT_IE_ECC_RE(n)			(0x01 << (8U * (n)))
27162306a36Sopenharmony_ci#define CIO2_INT_EXT_IE_DPHY_NR(n)			(0x02 << (8U * (n)))
27262306a36Sopenharmony_ci#define CIO2_INT_EXT_IE_ECC_NR(n)			(0x04 << (8U * (n)))
27362306a36Sopenharmony_ci#define CIO2_INT_EXT_IE_CRCERR(n)			(0x08 << (8U * (n)))
27462306a36Sopenharmony_ci#define CIO2_INT_EXT_IE_INTERFRAMEDATA(n)		(0x10 << (8U * (n)))
27562306a36Sopenharmony_ci#define CIO2_INT_EXT_IE_PKT2SHORT(n)			(0x20 << (8U * (n)))
27662306a36Sopenharmony_ci#define CIO2_INT_EXT_IE_PKT2LONG(n)			(0x40 << (8U * (n)))
27762306a36Sopenharmony_ci#define CIO2_INT_EXT_IE_IRQ(n)				(0x80 << (8U * (n)))
27862306a36Sopenharmony_ci#define CIO2_REG_PXM_FRF_CFG(n)				(0x1720 + 0x30 * (n))
27962306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_FNSEL				BIT(0)
28062306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_FN_RST				BIT(1)
28162306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_ABORT				BIT(2)
28262306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_CRC_TH_SHIFT			3U
28362306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_MSK_ECC_DPHY_NR		BIT(8)
28462306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_MSK_ECC_RE			BIT(9)
28562306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_MSK_ECC_DPHY_NE		BIT(10)
28662306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_EVEN_ODD_MODE_SHIFT		11U
28762306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_MASK_CRC_THRES			BIT(13)
28862306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_MASK_CSI_ACCEPT		BIT(14)
28962306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_CIOHC_FS_MODE			BIT(15)
29062306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_CIOHC_FRST_FRM_SHIFT		16U
29162306a36Sopenharmony_ci#define CIO2_REG_PXM_SID2BID0(n)			(0x1724 + 0x30 * (n))
29262306a36Sopenharmony_ci#define CIO2_FB_HPLL_FREQ				0x2
29362306a36Sopenharmony_ci#define CIO2_ISCLK_RATIO				0xc
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci#define CIO2_IRQCTRL_MASK				0x3ffff
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci#define CIO2_INT_EN_EXT_OE_MASK				0x8f0fffff
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci#define CIO2_CGC_CLKGATE_HOLDOFF			3U
30062306a36Sopenharmony_ci#define CIO2_CGC_CSI_CLKGATE_HOLDOFF			5U
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci#define CIO2_PXM_FRF_CFG_CRC_TH				16
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci#define CIO2_INT_EN_EXT_IE_MASK				0xffffffff
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci#define CIO2_DMA_CHAN					0U
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_CLANE_IDX			-1
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_TERMEN_CLANE_A		0
31162306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_TERMEN_CLANE_B		0
31262306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_SETTLE_CLANE_A		95
31362306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_SETTLE_CLANE_B		-8
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_TERMEN_DLANE_A		0
31662306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_TERMEN_DLANE_B		0
31762306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_SETTLE_DLANE_A		85
31862306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_SETTLE_DLANE_B		-2
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_TERMEN_DEFAULT		0x4
32162306a36Sopenharmony_ci#define CIO2_CSIRX_DLY_CNT_SETTLE_DEFAULT		0x570
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci#define CIO2_PMCSR_OFFSET				4U
32462306a36Sopenharmony_ci#define CIO2_PMCSR_D0D3_SHIFT				2U
32562306a36Sopenharmony_ci#define CIO2_PMCSR_D3					0x3
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_cistruct cio2_csi2_timing {
32862306a36Sopenharmony_ci	s32 clk_termen;
32962306a36Sopenharmony_ci	s32 clk_settle;
33062306a36Sopenharmony_ci	s32 dat_termen;
33162306a36Sopenharmony_ci	s32 dat_settle;
33262306a36Sopenharmony_ci};
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_cistruct cio2_buffer {
33562306a36Sopenharmony_ci	struct vb2_v4l2_buffer vbb;
33662306a36Sopenharmony_ci	u32 *lop[CIO2_MAX_LOPS];
33762306a36Sopenharmony_ci	dma_addr_t lop_bus_addr[CIO2_MAX_LOPS];
33862306a36Sopenharmony_ci	unsigned int offset;
33962306a36Sopenharmony_ci};
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci#define to_cio2_buffer(vb)	container_of(vb, struct cio2_buffer, vbb.vb2_buf)
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_cistruct csi2_bus_info {
34462306a36Sopenharmony_ci	u32 port;
34562306a36Sopenharmony_ci	u32 lanes;
34662306a36Sopenharmony_ci};
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_cistruct cio2_queue {
34962306a36Sopenharmony_ci	/* mutex to be used by vb2_queue */
35062306a36Sopenharmony_ci	struct mutex lock;
35162306a36Sopenharmony_ci	struct media_pipeline pipe;
35262306a36Sopenharmony_ci	struct csi2_bus_info csi2;
35362306a36Sopenharmony_ci	struct v4l2_subdev *sensor;
35462306a36Sopenharmony_ci	void __iomem *csi_rx_base;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	/* Subdev, /dev/v4l-subdevX */
35762306a36Sopenharmony_ci	struct v4l2_subdev subdev;
35862306a36Sopenharmony_ci	struct mutex subdev_lock; /* Serialise acces to subdev_fmt field */
35962306a36Sopenharmony_ci	struct media_pad subdev_pads[CIO2_PADS];
36062306a36Sopenharmony_ci	struct v4l2_mbus_framefmt subdev_fmt;
36162306a36Sopenharmony_ci	atomic_t frame_sequence;
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	/* Video device, /dev/videoX */
36462306a36Sopenharmony_ci	struct video_device vdev;
36562306a36Sopenharmony_ci	struct media_pad vdev_pad;
36662306a36Sopenharmony_ci	struct v4l2_pix_format_mplane format;
36762306a36Sopenharmony_ci	struct vb2_queue vbq;
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	/* Buffer queue handling */
37062306a36Sopenharmony_ci	struct cio2_fbpt_entry *fbpt;	/* Frame buffer pointer table */
37162306a36Sopenharmony_ci	dma_addr_t fbpt_bus_addr;
37262306a36Sopenharmony_ci	struct cio2_buffer *bufs[CIO2_MAX_BUFFERS];
37362306a36Sopenharmony_ci	unsigned int bufs_first;	/* Index of the first used entry */
37462306a36Sopenharmony_ci	unsigned int bufs_next;	/* Index of the first unused entry */
37562306a36Sopenharmony_ci	atomic_t bufs_queued;
37662306a36Sopenharmony_ci};
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_cistruct cio2_device {
37962306a36Sopenharmony_ci	struct pci_dev *pci_dev;
38062306a36Sopenharmony_ci	void __iomem *base;
38162306a36Sopenharmony_ci	struct v4l2_device v4l2_dev;
38262306a36Sopenharmony_ci	struct cio2_queue queue[CIO2_QUEUES];
38362306a36Sopenharmony_ci	struct cio2_queue *cur_queue;
38462306a36Sopenharmony_ci	/* mutex to be used by video_device */
38562306a36Sopenharmony_ci	struct mutex lock;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	bool streaming;
38862306a36Sopenharmony_ci	struct v4l2_async_notifier notifier;
38962306a36Sopenharmony_ci	struct media_device media_dev;
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci	/*
39262306a36Sopenharmony_ci	 * Safety net to catch DMA fetch ahead
39362306a36Sopenharmony_ci	 * when reaching the end of LOP
39462306a36Sopenharmony_ci	 */
39562306a36Sopenharmony_ci	void *dummy_page;
39662306a36Sopenharmony_ci	/* DMA handle of dummy_page */
39762306a36Sopenharmony_ci	dma_addr_t dummy_page_bus_addr;
39862306a36Sopenharmony_ci	/* single List of Pointers (LOP) page */
39962306a36Sopenharmony_ci	u32 *dummy_lop;
40062306a36Sopenharmony_ci	/* DMA handle of dummy_lop */
40162306a36Sopenharmony_ci	dma_addr_t dummy_lop_bus_addr;
40262306a36Sopenharmony_ci};
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci#define to_cio2_device(n)	container_of(n, struct cio2_device, notifier)
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci/**************** Virtual channel ****************/
40762306a36Sopenharmony_ci/*
40862306a36Sopenharmony_ci * This should come from sensor driver. No
40962306a36Sopenharmony_ci * driver interface nor requirement yet.
41062306a36Sopenharmony_ci */
41162306a36Sopenharmony_ci#define SENSOR_VIR_CH_DFLT		0
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci/**************** FBPT operations ****************/
41462306a36Sopenharmony_ci#define CIO2_FBPT_SIZE			(CIO2_MAX_BUFFERS * CIO2_MAX_LOPS * \
41562306a36Sopenharmony_ci					 sizeof(struct cio2_fbpt_entry))
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci#define CIO2_FBPT_SUBENTRY_UNIT		4
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci/* cio2 fbpt first_entry ctrl status */
42062306a36Sopenharmony_ci#define CIO2_FBPT_CTRL_VALID		BIT(0)
42162306a36Sopenharmony_ci#define CIO2_FBPT_CTRL_IOC		BIT(1)
42262306a36Sopenharmony_ci#define CIO2_FBPT_CTRL_IOS		BIT(2)
42362306a36Sopenharmony_ci#define CIO2_FBPT_CTRL_SUCCXFAIL	BIT(3)
42462306a36Sopenharmony_ci#define CIO2_FBPT_CTRL_CMPLCODE_SHIFT	4
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci/*
42762306a36Sopenharmony_ci * Frame Buffer Pointer Table(FBPT) entry
42862306a36Sopenharmony_ci * each entry describe an output buffer and consists of
42962306a36Sopenharmony_ci * several sub-entries
43062306a36Sopenharmony_ci */
43162306a36Sopenharmony_cistruct __packed cio2_fbpt_entry {
43262306a36Sopenharmony_ci	union {
43362306a36Sopenharmony_ci		struct __packed {
43462306a36Sopenharmony_ci			u32 ctrl; /* status ctrl */
43562306a36Sopenharmony_ci			u16 cur_line_num; /* current line # written to DDR */
43662306a36Sopenharmony_ci			u16 frame_num; /* updated by DMA upon FE */
43762306a36Sopenharmony_ci			u32 first_page_offset; /* offset for 1st page in LOP */
43862306a36Sopenharmony_ci		} first_entry;
43962306a36Sopenharmony_ci		/* Second entry per buffer */
44062306a36Sopenharmony_ci		struct __packed {
44162306a36Sopenharmony_ci			u32 timestamp;
44262306a36Sopenharmony_ci			u32 num_of_bytes;
44362306a36Sopenharmony_ci			/* the number of bytes for write on last page */
44462306a36Sopenharmony_ci			u16 last_page_available_bytes;
44562306a36Sopenharmony_ci			/* the number of pages allocated for this buf */
44662306a36Sopenharmony_ci			u16 num_of_pages;
44762306a36Sopenharmony_ci		} second_entry;
44862306a36Sopenharmony_ci	};
44962306a36Sopenharmony_ci	u32 lop_page_addr;	/* Points to list of pointers (LOP) table */
45062306a36Sopenharmony_ci};
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_cistatic inline struct cio2_queue *file_to_cio2_queue(struct file *file)
45362306a36Sopenharmony_ci{
45462306a36Sopenharmony_ci	return container_of(video_devdata(file), struct cio2_queue, vdev);
45562306a36Sopenharmony_ci}
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_cistatic inline struct cio2_queue *vb2q_to_cio2_queue(struct vb2_queue *vq)
45862306a36Sopenharmony_ci{
45962306a36Sopenharmony_ci	return container_of(vq, struct cio2_queue, vbq);
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci#endif
463