xref: /openbmc/linux/drivers/media/platform/nxp/imx-mipi-csis.c (revision 1188f7f111c61394ec56beb8e30322305a8220b6)
146fb9995SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0
246fb9995SMauro Carvalho Chehab /*
346fb9995SMauro Carvalho Chehab  * Samsung CSIS MIPI CSI-2 receiver driver.
446fb9995SMauro Carvalho Chehab  *
546fb9995SMauro Carvalho Chehab  * The Samsung CSIS IP is a MIPI CSI-2 receiver found in various NXP i.MX7 and
646fb9995SMauro Carvalho Chehab  * i.MX8 SoCs. The i.MX7 features version 3.3 of the IP, while i.MX8 features
746fb9995SMauro Carvalho Chehab  * version 3.6.3.
846fb9995SMauro Carvalho Chehab  *
946fb9995SMauro Carvalho Chehab  * Copyright (C) 2019 Linaro Ltd
1046fb9995SMauro Carvalho Chehab  * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. All Rights Reserved.
1146fb9995SMauro Carvalho Chehab  * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
1246fb9995SMauro Carvalho Chehab  *
1346fb9995SMauro Carvalho Chehab  */
1446fb9995SMauro Carvalho Chehab 
1546fb9995SMauro Carvalho Chehab #include <linux/clk.h>
1646fb9995SMauro Carvalho Chehab #include <linux/debugfs.h>
1746fb9995SMauro Carvalho Chehab #include <linux/delay.h>
1846fb9995SMauro Carvalho Chehab #include <linux/errno.h>
1946fb9995SMauro Carvalho Chehab #include <linux/interrupt.h>
2046fb9995SMauro Carvalho Chehab #include <linux/io.h>
2146fb9995SMauro Carvalho Chehab #include <linux/kernel.h>
2246fb9995SMauro Carvalho Chehab #include <linux/module.h>
2346fb9995SMauro Carvalho Chehab #include <linux/mutex.h>
2446fb9995SMauro Carvalho Chehab #include <linux/of.h>
2546fb9995SMauro Carvalho Chehab #include <linux/platform_device.h>
2646fb9995SMauro Carvalho Chehab #include <linux/pm_runtime.h>
2746fb9995SMauro Carvalho Chehab #include <linux/regulator/consumer.h>
2846fb9995SMauro Carvalho Chehab #include <linux/reset.h>
2946fb9995SMauro Carvalho Chehab #include <linux/spinlock.h>
3046fb9995SMauro Carvalho Chehab 
3146fb9995SMauro Carvalho Chehab #include <media/v4l2-common.h>
3246fb9995SMauro Carvalho Chehab #include <media/v4l2-device.h>
3346fb9995SMauro Carvalho Chehab #include <media/v4l2-fwnode.h>
3446fb9995SMauro Carvalho Chehab #include <media/v4l2-mc.h>
3546fb9995SMauro Carvalho Chehab #include <media/v4l2-subdev.h>
3646fb9995SMauro Carvalho Chehab 
3746fb9995SMauro Carvalho Chehab #define CSIS_DRIVER_NAME			"imx-mipi-csis"
3846fb9995SMauro Carvalho Chehab 
3946fb9995SMauro Carvalho Chehab #define CSIS_PAD_SINK				0
4046fb9995SMauro Carvalho Chehab #define CSIS_PAD_SOURCE				1
4146fb9995SMauro Carvalho Chehab #define CSIS_PADS_NUM				2
4246fb9995SMauro Carvalho Chehab 
4346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DEF_PIX_WIDTH			640
4446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DEF_PIX_HEIGHT		480
4546fb9995SMauro Carvalho Chehab 
4646fb9995SMauro Carvalho Chehab /* Register map definition */
4746fb9995SMauro Carvalho Chehab 
48a1c046d3SLaurent Pinchart /* CSIS version */
49a1c046d3SLaurent Pinchart #define MIPI_CSIS_VERSION			0x00
50a1c046d3SLaurent Pinchart #define MIPI_CSIS_VERSION_IMX7D			0x03030505
51a1c046d3SLaurent Pinchart #define MIPI_CSIS_VERSION_IMX8MP		0x03060301
52a1c046d3SLaurent Pinchart 
5346fb9995SMauro Carvalho Chehab /* CSIS common control */
5446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CMN_CTRL			0x04
5546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW	BIT(16)
5646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CMN_CTRL_INTER_MODE		BIT(10)
5746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL	BIT(2)
5846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CMN_CTRL_RESET		BIT(1)
5946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CMN_CTRL_ENABLE		BIT(0)
6046fb9995SMauro Carvalho Chehab 
6146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET	8
6246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CMN_CTRL_LANE_NR_MASK		(3 << 8)
6346fb9995SMauro Carvalho Chehab 
6446fb9995SMauro Carvalho Chehab /* CSIS clock control */
6546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CLK_CTRL			0x08
6646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH3(x)	((x) << 28)
6746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH2(x)	((x) << 24)
6846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH1(x)	((x) << 20)
6946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(x)	((x) << 16)
7046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK	(0xf << 4)
7146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_CLK_CTRL_WCLK_SRC		BIT(0)
7246fb9995SMauro Carvalho Chehab 
7346fb9995SMauro Carvalho Chehab /* CSIS Interrupt mask */
7446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK			0x10
7546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_EVEN_BEFORE		BIT(31)
7646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_EVEN_AFTER		BIT(30)
7746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ODD_BEFORE		BIT(29)
7846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ODD_AFTER		BIT(28)
7946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_FRAME_START		BIT(24)
8046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_FRAME_END		BIT(20)
8146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ERR_SOT_HS		BIT(16)
8246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ERR_LOST_FS		BIT(12)
8346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ERR_LOST_FE		BIT(8)
8446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ERR_OVER		BIT(4)
8546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ERR_WRONG_CFG		BIT(3)
8646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ERR_ECC		BIT(2)
8746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ERR_CRC		BIT(1)
8846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_MSK_ERR_UNKNOWN		BIT(0)
8946fb9995SMauro Carvalho Chehab 
9046fb9995SMauro Carvalho Chehab /* CSIS Interrupt source */
9146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC			0x14
9246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_EVEN_BEFORE		BIT(31)
9346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_EVEN_AFTER		BIT(30)
9446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_EVEN			BIT(30)
9546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ODD_BEFORE		BIT(29)
9646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ODD_AFTER		BIT(28)
9746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ODD			(0x3 << 28)
9846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_NON_IMAGE_DATA	(0xf << 28)
9946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_FRAME_START		BIT(24)
10046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_FRAME_END		BIT(20)
10146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERR_SOT_HS		BIT(16)
10246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERR_LOST_FS		BIT(12)
10346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERR_LOST_FE		BIT(8)
10446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERR_OVER		BIT(4)
10546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERR_WRONG_CFG		BIT(3)
10646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERR_ECC		BIT(2)
10746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERR_CRC		BIT(1)
10846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERR_UNKNOWN		BIT(0)
10946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_INT_SRC_ERRORS		0xfffff
11046fb9995SMauro Carvalho Chehab 
11146fb9995SMauro Carvalho Chehab /* D-PHY status control */
11246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_STATUS			0x20
11346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_STATUS_ULPS_DAT		BIT(8)
11446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_STATUS_STOPSTATE_DAT	BIT(4)
11546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_STATUS_ULPS_CLK		BIT(1)
11646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_STATUS_STOPSTATE_CLK	BIT(0)
11746fb9995SMauro Carvalho Chehab 
11846fb9995SMauro Carvalho Chehab /* D-PHY common control */
11946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL			0x24
12046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(n)	((n) << 24)
12146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE_MASK	GENMASK(31, 24)
12246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(n)	((n) << 22)
12346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE_MASK	GENMASK(23, 22)
12446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_DPDN_SWAP_CLK	BIT(6)
12546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_DPDN_SWAP_DAT	BIT(5)
12646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE_DAT	BIT(1)
12746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE_CLK	BIT(0)
12846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE		(0x1f << 0)
12946fb9995SMauro Carvalho Chehab 
13046fb9995SMauro Carvalho Chehab /* D-PHY Master and Slave Control register Low */
13146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L			0x30
13246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_USER_DATA_PATTERN_LOW(n)		(((n) & 3U) << 30)
13346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV		(0 << 28)
13446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_724MV		(1 << 28)
13546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_733MV		(2 << 28)
13646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_706MV		(3 << 28)
13746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ		(0 << 27)
13846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_1_5MHZ		(1 << 27)
13946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_VREG12_EXTPWR_EN_CTL		BIT(26)
14046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V		(0 << 24)
14146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_23V		(1 << 24)
14246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_17V		(2 << 24)
14346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_26V		(3 << 24)
14446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_REG_1P2_LVL_SEL			BIT(23)
14546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV		(0 << 21)
14646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_100MV		(1 << 21)
14746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_120MV		(2 << 21)
14846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_140MV		(3 << 21)
14946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_VREF_SRC_SEL			BIT(20)
15046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV		(0 << 18)
15146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_743MV		(1 << 18)
15246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_650MV		(2 << 18)
15346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_682MV		(3 << 18)
15446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_PULSE_REJECT		BIT(17)
15546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_0	(0 << 15)
15646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_15P	(1 << 15)
15746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_30P	(3 << 15)
15846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_UP		BIT(14)
15946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV			(0 << 13)
16046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_70MV			(1 << 13)
16146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_EN			BIT(12)
16246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_ERRCONTENTION_LP_EN		BIT(11)
16346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_TXTRIGGER_CLK_EN			BIT(10)
16446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(n)			(((n) * 25 / 1000000) << 0)
16546fb9995SMauro Carvalho Chehab 
16646fb9995SMauro Carvalho Chehab /* D-PHY Master and Slave Control register High */
16746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_BCTRL_H			0x34
16846fb9995SMauro Carvalho Chehab /* D-PHY Slave Control register Low */
16946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_SCTRL_L			0x38
17046fb9995SMauro Carvalho Chehab /* D-PHY Slave Control register High */
17146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DPHY_SCTRL_H			0x3c
17246fb9995SMauro Carvalho Chehab 
17346fb9995SMauro Carvalho Chehab /* ISP Configuration register */
17446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISP_CONFIG_CH(n)		(0x40 + (n) * 0x10)
17546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_MEM_FULL_GAP_MSK	(0xff << 24)
17646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_MEM_FULL_GAP(x)	((x) << 24)
17746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_PIXEL_MODE_SINGLE	(0 << 12)
17846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_PIXEL_MODE_DUAL	(1 << 12)
17946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_PIXEL_MODE_QUAD	(2 << 12)	/* i.MX8M[MNP] only */
18046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_PIXEL_MASK		(3 << 12)
18146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_ALIGN_32BIT		BIT(11)
18246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_FMT(fmt)		((fmt) << 2)
18346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISPCFG_FMT_MASK		(0x3f << 2)
18446fb9995SMauro Carvalho Chehab 
18546fb9995SMauro Carvalho Chehab /* ISP Image Resolution register */
18646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISP_RESOL_CH(n)		(0x44 + (n) * 0x10)
18746fb9995SMauro Carvalho Chehab #define CSIS_MAX_PIX_WIDTH			0xffff
18846fb9995SMauro Carvalho Chehab #define CSIS_MAX_PIX_HEIGHT			0xffff
18946fb9995SMauro Carvalho Chehab 
19046fb9995SMauro Carvalho Chehab /* ISP SYNC register */
19146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISP_SYNC_CH(n)		(0x48 + (n) * 0x10)
19246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET	18
19346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET	12
19446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET	0
19546fb9995SMauro Carvalho Chehab 
19646fb9995SMauro Carvalho Chehab /* ISP shadow registers */
19746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_SDW_CONFIG_CH(n)		(0x80 + (n) * 0x10)
19846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_SDW_RESOL_CH(n)		(0x84 + (n) * 0x10)
19946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_SDW_SYNC_CH(n)		(0x88 + (n) * 0x10)
20046fb9995SMauro Carvalho Chehab 
20146fb9995SMauro Carvalho Chehab /* Debug control register */
20246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_CTRL			0xc0
20346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK			0xc4
20446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK_DT_NOT_SUPPORT	BIT(25)
20546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK_DT_IGNORE	BIT(24)
20646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK_ERR_FRAME_SIZE	BIT(20)
20746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK_TRUNCATED_FRAME	BIT(16)
20846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK_EARLY_FE		BIT(12)
20946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK_EARLY_FS		BIT(8)
21046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK_CAM_VSYNC_FALL	BIT(4)
21146fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_MSK_CAM_VSYNC_RISE	BIT(0)
21246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC			0xc8
21346fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC_DT_NOT_SUPPORT	BIT(25)
21446fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC_DT_IGNORE	BIT(24)
21546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC_ERR_FRAME_SIZE	BIT(20)
21646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC_TRUNCATED_FRAME	BIT(16)
21746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC_EARLY_FE		BIT(12)
21846fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC_EARLY_FS		BIT(8)
21946fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_FALL	BIT(4)
22046fb9995SMauro Carvalho Chehab #define MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_RISE	BIT(0)
22146fb9995SMauro Carvalho Chehab 
22246fb9995SMauro Carvalho Chehab #define MIPI_CSIS_FRAME_COUNTER_CH(n)		(0x0100 + (n) * 4)
22346fb9995SMauro Carvalho Chehab 
22446fb9995SMauro Carvalho Chehab /* Non-image packet data buffers */
22546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_PKTDATA_ODD			0x2000
22646fb9995SMauro Carvalho Chehab #define MIPI_CSIS_PKTDATA_EVEN			0x3000
22746fb9995SMauro Carvalho Chehab #define MIPI_CSIS_PKTDATA_SIZE			SZ_4K
22846fb9995SMauro Carvalho Chehab 
22946fb9995SMauro Carvalho Chehab #define DEFAULT_SCLK_CSIS_FREQ			166000000UL
23046fb9995SMauro Carvalho Chehab 
23146fb9995SMauro Carvalho Chehab /* MIPI CSI-2 Data Types */
23246fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_YUV420_8		0x18
23346fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_YUV420_10		0x19
23446fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_LE_YUV420_8		0x1a
23546fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_CS_YUV420_8		0x1c
23646fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_CS_YUV420_10	0x1d
23746fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_YUV422_8		0x1e
23846fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_YUV422_10		0x1f
23946fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RGB565		0x22
24046fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RGB666		0x23
24146fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RGB888		0x24
24246fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RAW6		0x28
24346fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RAW7		0x29
24446fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RAW8		0x2a
24546fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RAW10		0x2b
24646fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RAW12		0x2c
24746fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_RAW14		0x2d
24846fb9995SMauro Carvalho Chehab #define MIPI_CSI2_DATA_TYPE_USER(x)		(0x30 + (x))
24946fb9995SMauro Carvalho Chehab 
25046fb9995SMauro Carvalho Chehab struct mipi_csis_event {
25146fb9995SMauro Carvalho Chehab 	bool debug;
25246fb9995SMauro Carvalho Chehab 	u32 mask;
25346fb9995SMauro Carvalho Chehab 	const char * const name;
25446fb9995SMauro Carvalho Chehab 	unsigned int counter;
25546fb9995SMauro Carvalho Chehab };
25646fb9995SMauro Carvalho Chehab 
25746fb9995SMauro Carvalho Chehab static const struct mipi_csis_event mipi_csis_events[] = {
25846fb9995SMauro Carvalho Chehab 	/* Errors */
25946fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ERR_SOT_HS,		"SOT Error" },
26046fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ERR_LOST_FS,		"Lost Frame Start Error" },
26146fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ERR_LOST_FE,		"Lost Frame End Error" },
26246fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ERR_OVER,		"FIFO Overflow Error" },
26346fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ERR_WRONG_CFG,	"Wrong Configuration Error" },
26446fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ERR_ECC,		"ECC Error" },
26546fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ERR_CRC,		"CRC Error" },
26646fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ERR_UNKNOWN,		"Unknown Error" },
26746fb9995SMauro Carvalho Chehab 	{ true, MIPI_CSIS_DBG_INTR_SRC_DT_NOT_SUPPORT,	"Data Type Not Supported" },
26846fb9995SMauro Carvalho Chehab 	{ true, MIPI_CSIS_DBG_INTR_SRC_DT_IGNORE,	"Data Type Ignored" },
26946fb9995SMauro Carvalho Chehab 	{ true, MIPI_CSIS_DBG_INTR_SRC_ERR_FRAME_SIZE,	"Frame Size Error" },
27046fb9995SMauro Carvalho Chehab 	{ true, MIPI_CSIS_DBG_INTR_SRC_TRUNCATED_FRAME,	"Truncated Frame" },
27146fb9995SMauro Carvalho Chehab 	{ true, MIPI_CSIS_DBG_INTR_SRC_EARLY_FE,	"Early Frame End" },
27246fb9995SMauro Carvalho Chehab 	{ true, MIPI_CSIS_DBG_INTR_SRC_EARLY_FS,	"Early Frame Start" },
27346fb9995SMauro Carvalho Chehab 	/* Non-image data receive events */
27446fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_EVEN_BEFORE,		"Non-image data before even frame" },
27546fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_EVEN_AFTER,		"Non-image data after even frame" },
27646fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ODD_BEFORE,		"Non-image data before odd frame" },
27746fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_ODD_AFTER,		"Non-image data after odd frame" },
27846fb9995SMauro Carvalho Chehab 	/* Frame start/end */
27946fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_FRAME_START,		"Frame Start" },
28046fb9995SMauro Carvalho Chehab 	{ false, MIPI_CSIS_INT_SRC_FRAME_END,		"Frame End" },
28146fb9995SMauro Carvalho Chehab 	{ true, MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_FALL,	"VSYNC Falling Edge" },
28246fb9995SMauro Carvalho Chehab 	{ true, MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_RISE,	"VSYNC Rising Edge" },
28346fb9995SMauro Carvalho Chehab };
28446fb9995SMauro Carvalho Chehab 
28546fb9995SMauro Carvalho Chehab #define MIPI_CSIS_NUM_EVENTS ARRAY_SIZE(mipi_csis_events)
28646fb9995SMauro Carvalho Chehab 
28746fb9995SMauro Carvalho Chehab enum mipi_csis_clk {
28846fb9995SMauro Carvalho Chehab 	MIPI_CSIS_CLK_PCLK,
28946fb9995SMauro Carvalho Chehab 	MIPI_CSIS_CLK_WRAP,
29046fb9995SMauro Carvalho Chehab 	MIPI_CSIS_CLK_PHY,
29146fb9995SMauro Carvalho Chehab 	MIPI_CSIS_CLK_AXI,
29246fb9995SMauro Carvalho Chehab };
29346fb9995SMauro Carvalho Chehab 
29446fb9995SMauro Carvalho Chehab static const char * const mipi_csis_clk_id[] = {
29546fb9995SMauro Carvalho Chehab 	"pclk",
29646fb9995SMauro Carvalho Chehab 	"wrap",
29746fb9995SMauro Carvalho Chehab 	"phy",
29846fb9995SMauro Carvalho Chehab 	"axi",
29946fb9995SMauro Carvalho Chehab };
30046fb9995SMauro Carvalho Chehab 
30146fb9995SMauro Carvalho Chehab enum mipi_csis_version {
30246fb9995SMauro Carvalho Chehab 	MIPI_CSIS_V3_3,
30346fb9995SMauro Carvalho Chehab 	MIPI_CSIS_V3_6_3,
30446fb9995SMauro Carvalho Chehab };
30546fb9995SMauro Carvalho Chehab 
30646fb9995SMauro Carvalho Chehab struct mipi_csis_info {
30746fb9995SMauro Carvalho Chehab 	enum mipi_csis_version version;
30846fb9995SMauro Carvalho Chehab 	unsigned int num_clocks;
30946fb9995SMauro Carvalho Chehab };
31046fb9995SMauro Carvalho Chehab 
311c1cc03eaSLaurent Pinchart struct mipi_csis_device {
31246fb9995SMauro Carvalho Chehab 	struct device *dev;
31346fb9995SMauro Carvalho Chehab 	void __iomem *regs;
31446fb9995SMauro Carvalho Chehab 	struct clk_bulk_data *clks;
31546fb9995SMauro Carvalho Chehab 	struct reset_control *mrst;
31646fb9995SMauro Carvalho Chehab 	struct regulator *mipi_phy_regulator;
31746fb9995SMauro Carvalho Chehab 	const struct mipi_csis_info *info;
31846fb9995SMauro Carvalho Chehab 
31946fb9995SMauro Carvalho Chehab 	struct v4l2_subdev sd;
32046fb9995SMauro Carvalho Chehab 	struct media_pad pads[CSIS_PADS_NUM];
32146fb9995SMauro Carvalho Chehab 	struct v4l2_async_notifier notifier;
32246fb9995SMauro Carvalho Chehab 	struct v4l2_subdev *src_sd;
32346fb9995SMauro Carvalho Chehab 
32446fb9995SMauro Carvalho Chehab 	struct v4l2_mbus_config_mipi_csi2 bus;
32546fb9995SMauro Carvalho Chehab 	u32 clk_frequency;
32646fb9995SMauro Carvalho Chehab 	u32 hs_settle;
32746fb9995SMauro Carvalho Chehab 	u32 clk_settle;
32846fb9995SMauro Carvalho Chehab 
32946fb9995SMauro Carvalho Chehab 	spinlock_t slock;	/* Protect events */
33046fb9995SMauro Carvalho Chehab 	struct mipi_csis_event events[MIPI_CSIS_NUM_EVENTS];
33146fb9995SMauro Carvalho Chehab 	struct dentry *debugfs_root;
33246fb9995SMauro Carvalho Chehab 	struct {
33346fb9995SMauro Carvalho Chehab 		bool enable;
33446fb9995SMauro Carvalho Chehab 		u32 hs_settle;
33546fb9995SMauro Carvalho Chehab 		u32 clk_settle;
33646fb9995SMauro Carvalho Chehab 	} debug;
33746fb9995SMauro Carvalho Chehab };
33846fb9995SMauro Carvalho Chehab 
33946fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
34046fb9995SMauro Carvalho Chehab  * Format helpers
34146fb9995SMauro Carvalho Chehab  */
34246fb9995SMauro Carvalho Chehab 
34346fb9995SMauro Carvalho Chehab struct csis_pix_format {
34446fb9995SMauro Carvalho Chehab 	u32 code;
34546fb9995SMauro Carvalho Chehab 	u32 output;
34646fb9995SMauro Carvalho Chehab 	u32 data_type;
34746fb9995SMauro Carvalho Chehab 	u8 width;
34846fb9995SMauro Carvalho Chehab };
34946fb9995SMauro Carvalho Chehab 
35046fb9995SMauro Carvalho Chehab static const struct csis_pix_format mipi_csis_formats[] = {
35146fb9995SMauro Carvalho Chehab 	/* YUV formats. */
35246fb9995SMauro Carvalho Chehab 	{
35346fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_UYVY8_1X16,
35446fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_UYVY8_1X16,
35546fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_YUV422_8,
35646fb9995SMauro Carvalho Chehab 		.width = 16,
35746fb9995SMauro Carvalho Chehab 	},
35846fb9995SMauro Carvalho Chehab 	/* RGB formats. */
35946fb9995SMauro Carvalho Chehab 	{
36046fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_RGB565_1X16,
36146fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_RGB565_1X16,
36246fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RGB565,
36346fb9995SMauro Carvalho Chehab 		.width = 16,
36446fb9995SMauro Carvalho Chehab 	}, {
36546fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_BGR888_1X24,
36646fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_RGB888_1X24,
36746fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RGB888,
36846fb9995SMauro Carvalho Chehab 		.width = 24,
36946fb9995SMauro Carvalho Chehab 	},
37046fb9995SMauro Carvalho Chehab 	/* RAW (Bayer and greyscale) formats. */
37146fb9995SMauro Carvalho Chehab 	{
37246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SBGGR8_1X8,
37346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SBGGR8_1X8,
37446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
37546fb9995SMauro Carvalho Chehab 		.width = 8,
37646fb9995SMauro Carvalho Chehab 	}, {
37746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SGBRG8_1X8,
37846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SGBRG8_1X8,
37946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
38046fb9995SMauro Carvalho Chehab 		.width = 8,
38146fb9995SMauro Carvalho Chehab 	}, {
38246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SGRBG8_1X8,
38346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SGRBG8_1X8,
38446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
38546fb9995SMauro Carvalho Chehab 		.width = 8,
38646fb9995SMauro Carvalho Chehab 	}, {
38746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SRGGB8_1X8,
38846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SRGGB8_1X8,
38946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
39046fb9995SMauro Carvalho Chehab 		.width = 8,
39146fb9995SMauro Carvalho Chehab 	}, {
39246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_Y8_1X8,
39346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_Y8_1X8,
39446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
39546fb9995SMauro Carvalho Chehab 		.width = 8,
39646fb9995SMauro Carvalho Chehab 	}, {
39746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SBGGR10_1X10,
39846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SBGGR10_1X10,
39946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
40046fb9995SMauro Carvalho Chehab 		.width = 10,
40146fb9995SMauro Carvalho Chehab 	}, {
40246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SGBRG10_1X10,
40346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SGBRG10_1X10,
40446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
40546fb9995SMauro Carvalho Chehab 		.width = 10,
40646fb9995SMauro Carvalho Chehab 	}, {
40746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SGRBG10_1X10,
40846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SGRBG10_1X10,
40946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
41046fb9995SMauro Carvalho Chehab 		.width = 10,
41146fb9995SMauro Carvalho Chehab 	}, {
41246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SRGGB10_1X10,
41346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SRGGB10_1X10,
41446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
41546fb9995SMauro Carvalho Chehab 		.width = 10,
41646fb9995SMauro Carvalho Chehab 	}, {
41746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_Y10_1X10,
41846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_Y10_1X10,
41946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
42046fb9995SMauro Carvalho Chehab 		.width = 10,
42146fb9995SMauro Carvalho Chehab 	}, {
42246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SBGGR12_1X12,
42346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SBGGR12_1X12,
42446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
42546fb9995SMauro Carvalho Chehab 		.width = 12,
42646fb9995SMauro Carvalho Chehab 	}, {
42746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SGBRG12_1X12,
42846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SGBRG12_1X12,
42946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
43046fb9995SMauro Carvalho Chehab 		.width = 12,
43146fb9995SMauro Carvalho Chehab 	}, {
43246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SGRBG12_1X12,
43346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SGRBG12_1X12,
43446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
43546fb9995SMauro Carvalho Chehab 		.width = 12,
43646fb9995SMauro Carvalho Chehab 	}, {
43746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SRGGB12_1X12,
43846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SRGGB12_1X12,
43946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
44046fb9995SMauro Carvalho Chehab 		.width = 12,
44146fb9995SMauro Carvalho Chehab 	}, {
44246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_Y12_1X12,
44346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_Y12_1X12,
44446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
44546fb9995SMauro Carvalho Chehab 		.width = 12,
44646fb9995SMauro Carvalho Chehab 	}, {
44746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SBGGR14_1X14,
44846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SBGGR14_1X14,
44946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW14,
45046fb9995SMauro Carvalho Chehab 		.width = 14,
45146fb9995SMauro Carvalho Chehab 	}, {
45246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SGBRG14_1X14,
45346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SGBRG14_1X14,
45446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW14,
45546fb9995SMauro Carvalho Chehab 		.width = 14,
45646fb9995SMauro Carvalho Chehab 	}, {
45746fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SGRBG14_1X14,
45846fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SGRBG14_1X14,
45946fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW14,
46046fb9995SMauro Carvalho Chehab 		.width = 14,
46146fb9995SMauro Carvalho Chehab 	}, {
46246fb9995SMauro Carvalho Chehab 		.code = MEDIA_BUS_FMT_SRGGB14_1X14,
46346fb9995SMauro Carvalho Chehab 		.output = MEDIA_BUS_FMT_SRGGB14_1X14,
46446fb9995SMauro Carvalho Chehab 		.data_type = MIPI_CSI2_DATA_TYPE_RAW14,
46546fb9995SMauro Carvalho Chehab 		.width = 14,
466d8fdfc66SJacopo Mondi 	},
467d8fdfc66SJacopo Mondi 	/* JPEG */
468d8fdfc66SJacopo Mondi 	{
469d8fdfc66SJacopo Mondi 		.code = MEDIA_BUS_FMT_JPEG_1X8,
470d8fdfc66SJacopo Mondi 		.output = MEDIA_BUS_FMT_JPEG_1X8,
471d8fdfc66SJacopo Mondi 		/*
472d8fdfc66SJacopo Mondi 		 * Map JPEG_1X8 to the RAW8 datatype.
473d8fdfc66SJacopo Mondi 		 *
474d8fdfc66SJacopo Mondi 		 * The CSI-2 specification suggests in Annex A "JPEG8 Data
475d8fdfc66SJacopo Mondi 		 * Format (informative)" to transmit JPEG data using one of the
476d8fdfc66SJacopo Mondi 		 * Data Types aimed to represent arbitrary data, such as the
477d8fdfc66SJacopo Mondi 		 * "User Defined Data Type 1" (0x30).
478d8fdfc66SJacopo Mondi 		 *
479d8fdfc66SJacopo Mondi 		 * However, when configured with a User Defined Data Type, the
480d8fdfc66SJacopo Mondi 		 * CSIS outputs data in quad pixel mode regardless of the mode
481d8fdfc66SJacopo Mondi 		 * selected in the MIPI_CSIS_ISP_CONFIG_CH register. Neither of
482d8fdfc66SJacopo Mondi 		 * the IP cores connected to the CSIS in i.MX SoCs (CSI bridge
483d8fdfc66SJacopo Mondi 		 * or ISI) support quad pixel mode, so this will never work in
484d8fdfc66SJacopo Mondi 		 * practice.
485d8fdfc66SJacopo Mondi 		 *
486d8fdfc66SJacopo Mondi 		 * Some sensors (such as the OV5640) send JPEG data using the
487d8fdfc66SJacopo Mondi 		 * RAW8 data type. This is usable and works, so map the JPEG
488d8fdfc66SJacopo Mondi 		 * format to RAW8. If the CSIS ends up being integrated in an
489d8fdfc66SJacopo Mondi 		 * SoC that can support quad pixel mode, this will have to be
490d8fdfc66SJacopo Mondi 		 * revisited.
491d8fdfc66SJacopo Mondi 		 */
492d8fdfc66SJacopo Mondi 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
493d8fdfc66SJacopo Mondi 		.width = 8,
49446fb9995SMauro Carvalho Chehab 	}
49546fb9995SMauro Carvalho Chehab };
49646fb9995SMauro Carvalho Chehab 
find_csis_format(u32 code)49746fb9995SMauro Carvalho Chehab static const struct csis_pix_format *find_csis_format(u32 code)
49846fb9995SMauro Carvalho Chehab {
49946fb9995SMauro Carvalho Chehab 	unsigned int i;
50046fb9995SMauro Carvalho Chehab 
50146fb9995SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(mipi_csis_formats); i++)
50246fb9995SMauro Carvalho Chehab 		if (code == mipi_csis_formats[i].code)
50346fb9995SMauro Carvalho Chehab 			return &mipi_csis_formats[i];
50446fb9995SMauro Carvalho Chehab 	return NULL;
50546fb9995SMauro Carvalho Chehab }
50646fb9995SMauro Carvalho Chehab 
50746fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
50846fb9995SMauro Carvalho Chehab  * Hardware configuration
50946fb9995SMauro Carvalho Chehab  */
51046fb9995SMauro Carvalho Chehab 
mipi_csis_read(struct mipi_csis_device * csis,u32 reg)511c1cc03eaSLaurent Pinchart static inline u32 mipi_csis_read(struct mipi_csis_device *csis, u32 reg)
51246fb9995SMauro Carvalho Chehab {
513c1cc03eaSLaurent Pinchart 	return readl(csis->regs + reg);
51446fb9995SMauro Carvalho Chehab }
51546fb9995SMauro Carvalho Chehab 
mipi_csis_write(struct mipi_csis_device * csis,u32 reg,u32 val)516c1cc03eaSLaurent Pinchart static inline void mipi_csis_write(struct mipi_csis_device *csis, u32 reg,
517c1cc03eaSLaurent Pinchart 				   u32 val)
51846fb9995SMauro Carvalho Chehab {
519c1cc03eaSLaurent Pinchart 	writel(val, csis->regs + reg);
52046fb9995SMauro Carvalho Chehab }
52146fb9995SMauro Carvalho Chehab 
mipi_csis_enable_interrupts(struct mipi_csis_device * csis,bool on)522c1cc03eaSLaurent Pinchart static void mipi_csis_enable_interrupts(struct mipi_csis_device *csis, bool on)
52346fb9995SMauro Carvalho Chehab {
524c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_INT_MSK, on ? 0xffffffff : 0);
525c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_DBG_INTR_MSK, on ? 0xffffffff : 0);
52646fb9995SMauro Carvalho Chehab }
52746fb9995SMauro Carvalho Chehab 
mipi_csis_sw_reset(struct mipi_csis_device * csis)528c1cc03eaSLaurent Pinchart static void mipi_csis_sw_reset(struct mipi_csis_device *csis)
52946fb9995SMauro Carvalho Chehab {
530c1cc03eaSLaurent Pinchart 	u32 val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
53146fb9995SMauro Carvalho Chehab 
532c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL,
53346fb9995SMauro Carvalho Chehab 			val | MIPI_CSIS_CMN_CTRL_RESET);
53446fb9995SMauro Carvalho Chehab 	usleep_range(10, 20);
53546fb9995SMauro Carvalho Chehab }
53646fb9995SMauro Carvalho Chehab 
mipi_csis_system_enable(struct mipi_csis_device * csis,int on)537c1cc03eaSLaurent Pinchart static void mipi_csis_system_enable(struct mipi_csis_device *csis, int on)
53846fb9995SMauro Carvalho Chehab {
53946fb9995SMauro Carvalho Chehab 	u32 val, mask;
54046fb9995SMauro Carvalho Chehab 
541c1cc03eaSLaurent Pinchart 	val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
54246fb9995SMauro Carvalho Chehab 	if (on)
54346fb9995SMauro Carvalho Chehab 		val |= MIPI_CSIS_CMN_CTRL_ENABLE;
54446fb9995SMauro Carvalho Chehab 	else
54546fb9995SMauro Carvalho Chehab 		val &= ~MIPI_CSIS_CMN_CTRL_ENABLE;
546c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL, val);
54746fb9995SMauro Carvalho Chehab 
548c1cc03eaSLaurent Pinchart 	val = mipi_csis_read(csis, MIPI_CSIS_DPHY_CMN_CTRL);
54946fb9995SMauro Carvalho Chehab 	val &= ~MIPI_CSIS_DPHY_CMN_CTRL_ENABLE;
55046fb9995SMauro Carvalho Chehab 	if (on) {
551c1cc03eaSLaurent Pinchart 		mask = (1 << (csis->bus.num_data_lanes + 1)) - 1;
55246fb9995SMauro Carvalho Chehab 		val |= (mask & MIPI_CSIS_DPHY_CMN_CTRL_ENABLE);
55346fb9995SMauro Carvalho Chehab 	}
554c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_DPHY_CMN_CTRL, val);
55546fb9995SMauro Carvalho Chehab }
55646fb9995SMauro Carvalho Chehab 
__mipi_csis_set_format(struct mipi_csis_device * csis,const struct v4l2_mbus_framefmt * format,const struct csis_pix_format * csis_fmt)5572f03d3cbSLaurent Pinchart static void __mipi_csis_set_format(struct mipi_csis_device *csis,
5582f03d3cbSLaurent Pinchart 				   const struct v4l2_mbus_framefmt *format,
5592f03d3cbSLaurent Pinchart 				   const struct csis_pix_format *csis_fmt)
56046fb9995SMauro Carvalho Chehab {
56146fb9995SMauro Carvalho Chehab 	u32 val;
56246fb9995SMauro Carvalho Chehab 
56346fb9995SMauro Carvalho Chehab 	/* Color format */
564c1cc03eaSLaurent Pinchart 	val = mipi_csis_read(csis, MIPI_CSIS_ISP_CONFIG_CH(0));
56546fb9995SMauro Carvalho Chehab 	val &= ~(MIPI_CSIS_ISPCFG_ALIGN_32BIT | MIPI_CSIS_ISPCFG_FMT_MASK
56646fb9995SMauro Carvalho Chehab 		| MIPI_CSIS_ISPCFG_PIXEL_MASK);
56746fb9995SMauro Carvalho Chehab 
56846fb9995SMauro Carvalho Chehab 	/*
56946fb9995SMauro Carvalho Chehab 	 * YUV 4:2:2 can be transferred with 8 or 16 bits per clock sample
57046fb9995SMauro Carvalho Chehab 	 * (referred to in the documentation as single and dual pixel modes
57146fb9995SMauro Carvalho Chehab 	 * respectively, although the 8-bit mode transfers half a pixel per
57246fb9995SMauro Carvalho Chehab 	 * clock sample and the 16-bit mode one pixel). While both mode work
57346fb9995SMauro Carvalho Chehab 	 * when the CSIS is connected to a receiver that supports either option,
57446fb9995SMauro Carvalho Chehab 	 * single pixel mode requires clock rates twice as high. As all SoCs
57546fb9995SMauro Carvalho Chehab 	 * that integrate the CSIS can operate in 16-bit bit mode, and some do
57646fb9995SMauro Carvalho Chehab 	 * not support 8-bit mode (this is the case of the i.MX8MP), use dual
57746fb9995SMauro Carvalho Chehab 	 * pixel mode unconditionally.
57846fb9995SMauro Carvalho Chehab 	 *
57946fb9995SMauro Carvalho Chehab 	 * TODO: Verify which other formats require DUAL (or QUAD) modes.
58046fb9995SMauro Carvalho Chehab 	 */
5812f03d3cbSLaurent Pinchart 	if (csis_fmt->data_type == MIPI_CSI2_DATA_TYPE_YUV422_8)
58246fb9995SMauro Carvalho Chehab 		val |= MIPI_CSIS_ISPCFG_PIXEL_MODE_DUAL;
58346fb9995SMauro Carvalho Chehab 
5842f03d3cbSLaurent Pinchart 	val |= MIPI_CSIS_ISPCFG_FMT(csis_fmt->data_type);
585c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_ISP_CONFIG_CH(0), val);
58646fb9995SMauro Carvalho Chehab 
58746fb9995SMauro Carvalho Chehab 	/* Pixel resolution */
5882f03d3cbSLaurent Pinchart 	val = format->width | (format->height << 16);
589c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_ISP_RESOL_CH(0), val);
59046fb9995SMauro Carvalho Chehab }
59146fb9995SMauro Carvalho Chehab 
mipi_csis_calculate_params(struct mipi_csis_device * csis,const struct csis_pix_format * csis_fmt)5922f03d3cbSLaurent Pinchart static int mipi_csis_calculate_params(struct mipi_csis_device *csis,
5932f03d3cbSLaurent Pinchart 				      const struct csis_pix_format *csis_fmt)
59446fb9995SMauro Carvalho Chehab {
59546fb9995SMauro Carvalho Chehab 	s64 link_freq;
59646fb9995SMauro Carvalho Chehab 	u32 lane_rate;
59746fb9995SMauro Carvalho Chehab 
59846fb9995SMauro Carvalho Chehab 	/* Calculate the line rate from the pixel rate. */
599c1cc03eaSLaurent Pinchart 	link_freq = v4l2_get_link_freq(csis->src_sd->ctrl_handler,
6002f03d3cbSLaurent Pinchart 				       csis_fmt->width,
601c1cc03eaSLaurent Pinchart 				       csis->bus.num_data_lanes * 2);
60246fb9995SMauro Carvalho Chehab 	if (link_freq < 0) {
603c1cc03eaSLaurent Pinchart 		dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
60446fb9995SMauro Carvalho Chehab 			(int)link_freq);
60546fb9995SMauro Carvalho Chehab 		return link_freq;
60646fb9995SMauro Carvalho Chehab 	}
60746fb9995SMauro Carvalho Chehab 
60846fb9995SMauro Carvalho Chehab 	lane_rate = link_freq * 2;
60946fb9995SMauro Carvalho Chehab 
61046fb9995SMauro Carvalho Chehab 	if (lane_rate < 80000000 || lane_rate > 1500000000) {
611c1cc03eaSLaurent Pinchart 		dev_dbg(csis->dev, "Out-of-bound lane rate %u\n", lane_rate);
61246fb9995SMauro Carvalho Chehab 		return -EINVAL;
61346fb9995SMauro Carvalho Chehab 	}
61446fb9995SMauro Carvalho Chehab 
61546fb9995SMauro Carvalho Chehab 	/*
61646fb9995SMauro Carvalho Chehab 	 * The HSSETTLE counter value is document in a table, but can also
61746fb9995SMauro Carvalho Chehab 	 * easily be calculated. Hardcode the CLKSETTLE value to 0 for now
61846fb9995SMauro Carvalho Chehab 	 * (which is documented as corresponding to CSI-2 v0.87 to v1.00) until
61946fb9995SMauro Carvalho Chehab 	 * we figure out how to compute it correctly.
62046fb9995SMauro Carvalho Chehab 	 */
621c1cc03eaSLaurent Pinchart 	csis->hs_settle = (lane_rate - 5000000) / 45000000;
622c1cc03eaSLaurent Pinchart 	csis->clk_settle = 0;
62346fb9995SMauro Carvalho Chehab 
624c1cc03eaSLaurent Pinchart 	dev_dbg(csis->dev, "lane rate %u, Tclk_settle %u, Ths_settle %u\n",
625c1cc03eaSLaurent Pinchart 		lane_rate, csis->clk_settle, csis->hs_settle);
62646fb9995SMauro Carvalho Chehab 
627c1cc03eaSLaurent Pinchart 	if (csis->debug.hs_settle < 0xff) {
628c1cc03eaSLaurent Pinchart 		dev_dbg(csis->dev, "overriding Ths_settle with %u\n",
629c1cc03eaSLaurent Pinchart 			csis->debug.hs_settle);
630c1cc03eaSLaurent Pinchart 		csis->hs_settle = csis->debug.hs_settle;
63146fb9995SMauro Carvalho Chehab 	}
63246fb9995SMauro Carvalho Chehab 
633c1cc03eaSLaurent Pinchart 	if (csis->debug.clk_settle < 4) {
634c1cc03eaSLaurent Pinchart 		dev_dbg(csis->dev, "overriding Tclk_settle with %u\n",
635c1cc03eaSLaurent Pinchart 			csis->debug.clk_settle);
636c1cc03eaSLaurent Pinchart 		csis->clk_settle = csis->debug.clk_settle;
63746fb9995SMauro Carvalho Chehab 	}
63846fb9995SMauro Carvalho Chehab 
63946fb9995SMauro Carvalho Chehab 	return 0;
64046fb9995SMauro Carvalho Chehab }
64146fb9995SMauro Carvalho Chehab 
mipi_csis_set_params(struct mipi_csis_device * csis,const struct v4l2_mbus_framefmt * format,const struct csis_pix_format * csis_fmt)6422f03d3cbSLaurent Pinchart static void mipi_csis_set_params(struct mipi_csis_device *csis,
6432f03d3cbSLaurent Pinchart 				 const struct v4l2_mbus_framefmt *format,
6442f03d3cbSLaurent Pinchart 				 const struct csis_pix_format *csis_fmt)
64546fb9995SMauro Carvalho Chehab {
646c1cc03eaSLaurent Pinchart 	int lanes = csis->bus.num_data_lanes;
64746fb9995SMauro Carvalho Chehab 	u32 val;
64846fb9995SMauro Carvalho Chehab 
649c1cc03eaSLaurent Pinchart 	val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
65046fb9995SMauro Carvalho Chehab 	val &= ~MIPI_CSIS_CMN_CTRL_LANE_NR_MASK;
65146fb9995SMauro Carvalho Chehab 	val |= (lanes - 1) << MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET;
652c1cc03eaSLaurent Pinchart 	if (csis->info->version == MIPI_CSIS_V3_3)
65346fb9995SMauro Carvalho Chehab 		val |= MIPI_CSIS_CMN_CTRL_INTER_MODE;
654c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL, val);
65546fb9995SMauro Carvalho Chehab 
6562f03d3cbSLaurent Pinchart 	__mipi_csis_set_format(csis, format, csis_fmt);
65746fb9995SMauro Carvalho Chehab 
658c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_DPHY_CMN_CTRL,
659c1cc03eaSLaurent Pinchart 			MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(csis->hs_settle) |
660c1cc03eaSLaurent Pinchart 			MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(csis->clk_settle));
66146fb9995SMauro Carvalho Chehab 
66246fb9995SMauro Carvalho Chehab 	val = (0 << MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET)
66346fb9995SMauro Carvalho Chehab 	    | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET)
66446fb9995SMauro Carvalho Chehab 	    | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET);
665c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_ISP_SYNC_CH(0), val);
66646fb9995SMauro Carvalho Chehab 
667c1cc03eaSLaurent Pinchart 	val = mipi_csis_read(csis, MIPI_CSIS_CLK_CTRL);
66846fb9995SMauro Carvalho Chehab 	val |= MIPI_CSIS_CLK_CTRL_WCLK_SRC;
66946fb9995SMauro Carvalho Chehab 	val |= MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(15);
67046fb9995SMauro Carvalho Chehab 	val &= ~MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK;
671c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_CLK_CTRL, val);
67246fb9995SMauro Carvalho Chehab 
673c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_DPHY_BCTRL_L,
67446fb9995SMauro Carvalho Chehab 			MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV |
67546fb9995SMauro Carvalho Chehab 			MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ |
67646fb9995SMauro Carvalho Chehab 			MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V |
67746fb9995SMauro Carvalho Chehab 			MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV |
67846fb9995SMauro Carvalho Chehab 			MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV |
67946fb9995SMauro Carvalho Chehab 			MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV |
68046fb9995SMauro Carvalho Chehab 			MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(20000000));
681c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_DPHY_BCTRL_H, 0);
68246fb9995SMauro Carvalho Chehab 
68346fb9995SMauro Carvalho Chehab 	/* Update the shadow register. */
684c1cc03eaSLaurent Pinchart 	val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
685c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL,
68646fb9995SMauro Carvalho Chehab 			val | MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW |
68746fb9995SMauro Carvalho Chehab 			MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL);
68846fb9995SMauro Carvalho Chehab }
68946fb9995SMauro Carvalho Chehab 
mipi_csis_clk_enable(struct mipi_csis_device * csis)690c1cc03eaSLaurent Pinchart static int mipi_csis_clk_enable(struct mipi_csis_device *csis)
69146fb9995SMauro Carvalho Chehab {
692c1cc03eaSLaurent Pinchart 	return clk_bulk_prepare_enable(csis->info->num_clocks, csis->clks);
69346fb9995SMauro Carvalho Chehab }
69446fb9995SMauro Carvalho Chehab 
mipi_csis_clk_disable(struct mipi_csis_device * csis)695c1cc03eaSLaurent Pinchart static void mipi_csis_clk_disable(struct mipi_csis_device *csis)
69646fb9995SMauro Carvalho Chehab {
697c1cc03eaSLaurent Pinchart 	clk_bulk_disable_unprepare(csis->info->num_clocks, csis->clks);
69846fb9995SMauro Carvalho Chehab }
69946fb9995SMauro Carvalho Chehab 
mipi_csis_clk_get(struct mipi_csis_device * csis)700c1cc03eaSLaurent Pinchart static int mipi_csis_clk_get(struct mipi_csis_device *csis)
70146fb9995SMauro Carvalho Chehab {
70246fb9995SMauro Carvalho Chehab 	unsigned int i;
70346fb9995SMauro Carvalho Chehab 	int ret;
70446fb9995SMauro Carvalho Chehab 
705c1cc03eaSLaurent Pinchart 	csis->clks = devm_kcalloc(csis->dev, csis->info->num_clocks,
706c1cc03eaSLaurent Pinchart 				  sizeof(*csis->clks), GFP_KERNEL);
70746fb9995SMauro Carvalho Chehab 
708c1cc03eaSLaurent Pinchart 	if (!csis->clks)
70946fb9995SMauro Carvalho Chehab 		return -ENOMEM;
71046fb9995SMauro Carvalho Chehab 
711c1cc03eaSLaurent Pinchart 	for (i = 0; i < csis->info->num_clocks; i++)
712c1cc03eaSLaurent Pinchart 		csis->clks[i].id = mipi_csis_clk_id[i];
71346fb9995SMauro Carvalho Chehab 
714c1cc03eaSLaurent Pinchart 	ret = devm_clk_bulk_get(csis->dev, csis->info->num_clocks,
715c1cc03eaSLaurent Pinchart 				csis->clks);
71646fb9995SMauro Carvalho Chehab 	if (ret < 0)
71746fb9995SMauro Carvalho Chehab 		return ret;
71846fb9995SMauro Carvalho Chehab 
71946fb9995SMauro Carvalho Chehab 	/* Set clock rate */
720c1cc03eaSLaurent Pinchart 	ret = clk_set_rate(csis->clks[MIPI_CSIS_CLK_WRAP].clk,
721c1cc03eaSLaurent Pinchart 			   csis->clk_frequency);
72246fb9995SMauro Carvalho Chehab 	if (ret < 0)
723c1cc03eaSLaurent Pinchart 		dev_err(csis->dev, "set rate=%d failed: %d\n",
724c1cc03eaSLaurent Pinchart 			csis->clk_frequency, ret);
72546fb9995SMauro Carvalho Chehab 
72646fb9995SMauro Carvalho Chehab 	return ret;
72746fb9995SMauro Carvalho Chehab }
72846fb9995SMauro Carvalho Chehab 
mipi_csis_start_stream(struct mipi_csis_device * csis,const struct v4l2_mbus_framefmt * format,const struct csis_pix_format * csis_fmt)7292f03d3cbSLaurent Pinchart static void mipi_csis_start_stream(struct mipi_csis_device *csis,
7302f03d3cbSLaurent Pinchart 				   const struct v4l2_mbus_framefmt *format,
7312f03d3cbSLaurent Pinchart 				   const struct csis_pix_format *csis_fmt)
73246fb9995SMauro Carvalho Chehab {
733c1cc03eaSLaurent Pinchart 	mipi_csis_sw_reset(csis);
7342f03d3cbSLaurent Pinchart 	mipi_csis_set_params(csis, format, csis_fmt);
735c1cc03eaSLaurent Pinchart 	mipi_csis_system_enable(csis, true);
736c1cc03eaSLaurent Pinchart 	mipi_csis_enable_interrupts(csis, true);
73746fb9995SMauro Carvalho Chehab }
73846fb9995SMauro Carvalho Chehab 
mipi_csis_stop_stream(struct mipi_csis_device * csis)739c1cc03eaSLaurent Pinchart static void mipi_csis_stop_stream(struct mipi_csis_device *csis)
74046fb9995SMauro Carvalho Chehab {
741c1cc03eaSLaurent Pinchart 	mipi_csis_enable_interrupts(csis, false);
742c1cc03eaSLaurent Pinchart 	mipi_csis_system_enable(csis, false);
74346fb9995SMauro Carvalho Chehab }
74446fb9995SMauro Carvalho Chehab 
mipi_csis_irq_handler(int irq,void * dev_id)74546fb9995SMauro Carvalho Chehab static irqreturn_t mipi_csis_irq_handler(int irq, void *dev_id)
74646fb9995SMauro Carvalho Chehab {
747c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = dev_id;
74846fb9995SMauro Carvalho Chehab 	unsigned long flags;
74946fb9995SMauro Carvalho Chehab 	unsigned int i;
75046fb9995SMauro Carvalho Chehab 	u32 status;
75146fb9995SMauro Carvalho Chehab 	u32 dbg_status;
75246fb9995SMauro Carvalho Chehab 
753c1cc03eaSLaurent Pinchart 	status = mipi_csis_read(csis, MIPI_CSIS_INT_SRC);
754c1cc03eaSLaurent Pinchart 	dbg_status = mipi_csis_read(csis, MIPI_CSIS_DBG_INTR_SRC);
75546fb9995SMauro Carvalho Chehab 
756c1cc03eaSLaurent Pinchart 	spin_lock_irqsave(&csis->slock, flags);
75746fb9995SMauro Carvalho Chehab 
75846fb9995SMauro Carvalho Chehab 	/* Update the event/error counters */
759c1cc03eaSLaurent Pinchart 	if ((status & MIPI_CSIS_INT_SRC_ERRORS) || csis->debug.enable) {
76046fb9995SMauro Carvalho Chehab 		for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++) {
761c1cc03eaSLaurent Pinchart 			struct mipi_csis_event *event = &csis->events[i];
76246fb9995SMauro Carvalho Chehab 
76346fb9995SMauro Carvalho Chehab 			if ((!event->debug && (status & event->mask)) ||
76446fb9995SMauro Carvalho Chehab 			    (event->debug && (dbg_status & event->mask)))
76546fb9995SMauro Carvalho Chehab 				event->counter++;
76646fb9995SMauro Carvalho Chehab 		}
76746fb9995SMauro Carvalho Chehab 	}
768c1cc03eaSLaurent Pinchart 	spin_unlock_irqrestore(&csis->slock, flags);
76946fb9995SMauro Carvalho Chehab 
770c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_INT_SRC, status);
771c1cc03eaSLaurent Pinchart 	mipi_csis_write(csis, MIPI_CSIS_DBG_INTR_SRC, dbg_status);
77246fb9995SMauro Carvalho Chehab 
77346fb9995SMauro Carvalho Chehab 	return IRQ_HANDLED;
77446fb9995SMauro Carvalho Chehab }
77546fb9995SMauro Carvalho Chehab 
77646fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
77746fb9995SMauro Carvalho Chehab  * PHY regulator and reset
77846fb9995SMauro Carvalho Chehab  */
77946fb9995SMauro Carvalho Chehab 
mipi_csis_phy_enable(struct mipi_csis_device * csis)780c1cc03eaSLaurent Pinchart static int mipi_csis_phy_enable(struct mipi_csis_device *csis)
78146fb9995SMauro Carvalho Chehab {
782c1cc03eaSLaurent Pinchart 	if (csis->info->version != MIPI_CSIS_V3_3)
78346fb9995SMauro Carvalho Chehab 		return 0;
78446fb9995SMauro Carvalho Chehab 
785c1cc03eaSLaurent Pinchart 	return regulator_enable(csis->mipi_phy_regulator);
78646fb9995SMauro Carvalho Chehab }
78746fb9995SMauro Carvalho Chehab 
mipi_csis_phy_disable(struct mipi_csis_device * csis)788c1cc03eaSLaurent Pinchart static int mipi_csis_phy_disable(struct mipi_csis_device *csis)
78946fb9995SMauro Carvalho Chehab {
790c1cc03eaSLaurent Pinchart 	if (csis->info->version != MIPI_CSIS_V3_3)
79146fb9995SMauro Carvalho Chehab 		return 0;
79246fb9995SMauro Carvalho Chehab 
793c1cc03eaSLaurent Pinchart 	return regulator_disable(csis->mipi_phy_regulator);
79446fb9995SMauro Carvalho Chehab }
79546fb9995SMauro Carvalho Chehab 
mipi_csis_phy_reset(struct mipi_csis_device * csis)796c1cc03eaSLaurent Pinchart static void mipi_csis_phy_reset(struct mipi_csis_device *csis)
79746fb9995SMauro Carvalho Chehab {
798c1cc03eaSLaurent Pinchart 	if (csis->info->version != MIPI_CSIS_V3_3)
79946fb9995SMauro Carvalho Chehab 		return;
80046fb9995SMauro Carvalho Chehab 
801c1cc03eaSLaurent Pinchart 	reset_control_assert(csis->mrst);
80246fb9995SMauro Carvalho Chehab 	msleep(20);
803c1cc03eaSLaurent Pinchart 	reset_control_deassert(csis->mrst);
80446fb9995SMauro Carvalho Chehab }
80546fb9995SMauro Carvalho Chehab 
mipi_csis_phy_init(struct mipi_csis_device * csis)806c1cc03eaSLaurent Pinchart static int mipi_csis_phy_init(struct mipi_csis_device *csis)
80746fb9995SMauro Carvalho Chehab {
808c1cc03eaSLaurent Pinchart 	if (csis->info->version != MIPI_CSIS_V3_3)
80946fb9995SMauro Carvalho Chehab 		return 0;
81046fb9995SMauro Carvalho Chehab 
81146fb9995SMauro Carvalho Chehab 	/* Get MIPI PHY reset and regulator. */
812c1cc03eaSLaurent Pinchart 	csis->mrst = devm_reset_control_get_exclusive(csis->dev, NULL);
813c1cc03eaSLaurent Pinchart 	if (IS_ERR(csis->mrst))
814c1cc03eaSLaurent Pinchart 		return PTR_ERR(csis->mrst);
81546fb9995SMauro Carvalho Chehab 
816c1cc03eaSLaurent Pinchart 	csis->mipi_phy_regulator = devm_regulator_get(csis->dev, "phy");
817c1cc03eaSLaurent Pinchart 	if (IS_ERR(csis->mipi_phy_regulator))
818c1cc03eaSLaurent Pinchart 		return PTR_ERR(csis->mipi_phy_regulator);
81946fb9995SMauro Carvalho Chehab 
820c1cc03eaSLaurent Pinchart 	return regulator_set_voltage(csis->mipi_phy_regulator, 1000000,
82146fb9995SMauro Carvalho Chehab 				     1000000);
82246fb9995SMauro Carvalho Chehab }
82346fb9995SMauro Carvalho Chehab 
82446fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
82546fb9995SMauro Carvalho Chehab  * Debug
82646fb9995SMauro Carvalho Chehab  */
82746fb9995SMauro Carvalho Chehab 
mipi_csis_clear_counters(struct mipi_csis_device * csis)828c1cc03eaSLaurent Pinchart static void mipi_csis_clear_counters(struct mipi_csis_device *csis)
82946fb9995SMauro Carvalho Chehab {
83046fb9995SMauro Carvalho Chehab 	unsigned long flags;
83146fb9995SMauro Carvalho Chehab 	unsigned int i;
83246fb9995SMauro Carvalho Chehab 
833c1cc03eaSLaurent Pinchart 	spin_lock_irqsave(&csis->slock, flags);
83446fb9995SMauro Carvalho Chehab 	for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++)
835c1cc03eaSLaurent Pinchart 		csis->events[i].counter = 0;
836c1cc03eaSLaurent Pinchart 	spin_unlock_irqrestore(&csis->slock, flags);
83746fb9995SMauro Carvalho Chehab }
83846fb9995SMauro Carvalho Chehab 
mipi_csis_log_counters(struct mipi_csis_device * csis,bool non_errors)839c1cc03eaSLaurent Pinchart static void mipi_csis_log_counters(struct mipi_csis_device *csis, bool non_errors)
84046fb9995SMauro Carvalho Chehab {
84146fb9995SMauro Carvalho Chehab 	unsigned int num_events = non_errors ? MIPI_CSIS_NUM_EVENTS
84246fb9995SMauro Carvalho Chehab 				: MIPI_CSIS_NUM_EVENTS - 8;
84346fb9995SMauro Carvalho Chehab 	unsigned long flags;
84446fb9995SMauro Carvalho Chehab 	unsigned int i;
84546fb9995SMauro Carvalho Chehab 
846c1cc03eaSLaurent Pinchart 	spin_lock_irqsave(&csis->slock, flags);
84746fb9995SMauro Carvalho Chehab 
84846fb9995SMauro Carvalho Chehab 	for (i = 0; i < num_events; ++i) {
849c1cc03eaSLaurent Pinchart 		if (csis->events[i].counter > 0 || csis->debug.enable)
850c1cc03eaSLaurent Pinchart 			dev_info(csis->dev, "%s events: %d\n",
851c1cc03eaSLaurent Pinchart 				 csis->events[i].name,
852c1cc03eaSLaurent Pinchart 				 csis->events[i].counter);
85346fb9995SMauro Carvalho Chehab 	}
854c1cc03eaSLaurent Pinchart 	spin_unlock_irqrestore(&csis->slock, flags);
85546fb9995SMauro Carvalho Chehab }
85646fb9995SMauro Carvalho Chehab 
mipi_csis_dump_regs(struct mipi_csis_device * csis)857c1cc03eaSLaurent Pinchart static int mipi_csis_dump_regs(struct mipi_csis_device *csis)
85846fb9995SMauro Carvalho Chehab {
85946fb9995SMauro Carvalho Chehab 	static const struct {
86046fb9995SMauro Carvalho Chehab 		u32 offset;
86146fb9995SMauro Carvalho Chehab 		const char * const name;
86246fb9995SMauro Carvalho Chehab 	} registers[] = {
86346fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_CMN_CTRL, "CMN_CTRL" },
86446fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_CLK_CTRL, "CLK_CTRL" },
86546fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_INT_MSK, "INT_MSK" },
86646fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_DPHY_STATUS, "DPHY_STATUS" },
86746fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_DPHY_CMN_CTRL, "DPHY_CMN_CTRL" },
86846fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_DPHY_SCTRL_L, "DPHY_SCTRL_L" },
86946fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_DPHY_SCTRL_H, "DPHY_SCTRL_H" },
87046fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_ISP_CONFIG_CH(0), "ISP_CONFIG_CH0" },
87146fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_ISP_RESOL_CH(0), "ISP_RESOL_CH0" },
87246fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_SDW_CONFIG_CH(0), "SDW_CONFIG_CH0" },
87346fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_SDW_RESOL_CH(0), "SDW_RESOL_CH0" },
87446fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_DBG_CTRL, "DBG_CTRL" },
87546fb9995SMauro Carvalho Chehab 		{ MIPI_CSIS_FRAME_COUNTER_CH(0), "FRAME_COUNTER_CH0" },
87646fb9995SMauro Carvalho Chehab 	};
87746fb9995SMauro Carvalho Chehab 
87846fb9995SMauro Carvalho Chehab 	unsigned int i;
87946fb9995SMauro Carvalho Chehab 	u32 cfg;
88046fb9995SMauro Carvalho Chehab 
881851b270bSJacopo Mondi 	if (!pm_runtime_get_if_in_use(csis->dev))
882851b270bSJacopo Mondi 		return 0;
883851b270bSJacopo Mondi 
884c1cc03eaSLaurent Pinchart 	dev_info(csis->dev, "--- REGISTERS ---\n");
88546fb9995SMauro Carvalho Chehab 
88646fb9995SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(registers); i++) {
887c1cc03eaSLaurent Pinchart 		cfg = mipi_csis_read(csis, registers[i].offset);
888c1cc03eaSLaurent Pinchart 		dev_info(csis->dev, "%14s: 0x%08x\n", registers[i].name, cfg);
88946fb9995SMauro Carvalho Chehab 	}
89046fb9995SMauro Carvalho Chehab 
891851b270bSJacopo Mondi 	pm_runtime_put(csis->dev);
892851b270bSJacopo Mondi 
89346fb9995SMauro Carvalho Chehab 	return 0;
89446fb9995SMauro Carvalho Chehab }
89546fb9995SMauro Carvalho Chehab 
mipi_csis_dump_regs_show(struct seq_file * m,void * private)89646fb9995SMauro Carvalho Chehab static int mipi_csis_dump_regs_show(struct seq_file *m, void *private)
89746fb9995SMauro Carvalho Chehab {
898c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = m->private;
89946fb9995SMauro Carvalho Chehab 
900c1cc03eaSLaurent Pinchart 	return mipi_csis_dump_regs(csis);
90146fb9995SMauro Carvalho Chehab }
90246fb9995SMauro Carvalho Chehab DEFINE_SHOW_ATTRIBUTE(mipi_csis_dump_regs);
90346fb9995SMauro Carvalho Chehab 
mipi_csis_debugfs_init(struct mipi_csis_device * csis)904c1cc03eaSLaurent Pinchart static void mipi_csis_debugfs_init(struct mipi_csis_device *csis)
90546fb9995SMauro Carvalho Chehab {
906c1cc03eaSLaurent Pinchart 	csis->debug.hs_settle = UINT_MAX;
907c1cc03eaSLaurent Pinchart 	csis->debug.clk_settle = UINT_MAX;
90846fb9995SMauro Carvalho Chehab 
909c1cc03eaSLaurent Pinchart 	csis->debugfs_root = debugfs_create_dir(dev_name(csis->dev), NULL);
91046fb9995SMauro Carvalho Chehab 
911c1cc03eaSLaurent Pinchart 	debugfs_create_bool("debug_enable", 0600, csis->debugfs_root,
912c1cc03eaSLaurent Pinchart 			    &csis->debug.enable);
913c1cc03eaSLaurent Pinchart 	debugfs_create_file("dump_regs", 0600, csis->debugfs_root, csis,
91446fb9995SMauro Carvalho Chehab 			    &mipi_csis_dump_regs_fops);
915c1cc03eaSLaurent Pinchart 	debugfs_create_u32("tclk_settle", 0600, csis->debugfs_root,
916c1cc03eaSLaurent Pinchart 			   &csis->debug.clk_settle);
917c1cc03eaSLaurent Pinchart 	debugfs_create_u32("ths_settle", 0600, csis->debugfs_root,
918c1cc03eaSLaurent Pinchart 			   &csis->debug.hs_settle);
91946fb9995SMauro Carvalho Chehab }
92046fb9995SMauro Carvalho Chehab 
mipi_csis_debugfs_exit(struct mipi_csis_device * csis)921c1cc03eaSLaurent Pinchart static void mipi_csis_debugfs_exit(struct mipi_csis_device *csis)
92246fb9995SMauro Carvalho Chehab {
923c1cc03eaSLaurent Pinchart 	debugfs_remove_recursive(csis->debugfs_root);
92446fb9995SMauro Carvalho Chehab }
92546fb9995SMauro Carvalho Chehab 
92646fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
92746fb9995SMauro Carvalho Chehab  * V4L2 subdev operations
92846fb9995SMauro Carvalho Chehab  */
92946fb9995SMauro Carvalho Chehab 
sd_to_mipi_csis_device(struct v4l2_subdev * sdev)930c1cc03eaSLaurent Pinchart static struct mipi_csis_device *sd_to_mipi_csis_device(struct v4l2_subdev *sdev)
93146fb9995SMauro Carvalho Chehab {
932c1cc03eaSLaurent Pinchart 	return container_of(sdev, struct mipi_csis_device, sd);
93346fb9995SMauro Carvalho Chehab }
93446fb9995SMauro Carvalho Chehab 
mipi_csis_s_stream(struct v4l2_subdev * sd,int enable)93546fb9995SMauro Carvalho Chehab static int mipi_csis_s_stream(struct v4l2_subdev *sd, int enable)
93646fb9995SMauro Carvalho Chehab {
937c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
93811927d0fSLaurent Pinchart 	const struct v4l2_mbus_framefmt *format;
93911927d0fSLaurent Pinchart 	const struct csis_pix_format *csis_fmt;
94011927d0fSLaurent Pinchart 	struct v4l2_subdev_state *state;
941df4167d9SJacopo Mondi 	int ret;
94246fb9995SMauro Carvalho Chehab 
943df4167d9SJacopo Mondi 	if (!enable) {
944df4167d9SJacopo Mondi 		v4l2_subdev_call(csis->src_sd, video, s_stream, 0);
945df4167d9SJacopo Mondi 
946df4167d9SJacopo Mondi 		mipi_csis_stop_stream(csis);
947df4167d9SJacopo Mondi 		if (csis->debug.enable)
948df4167d9SJacopo Mondi 			mipi_csis_log_counters(csis, true);
949df4167d9SJacopo Mondi 
950df4167d9SJacopo Mondi 		pm_runtime_put(csis->dev);
951df4167d9SJacopo Mondi 
952df4167d9SJacopo Mondi 		return 0;
953df4167d9SJacopo Mondi 	}
954df4167d9SJacopo Mondi 
95511927d0fSLaurent Pinchart 	state = v4l2_subdev_lock_and_get_active_state(sd);
95611927d0fSLaurent Pinchart 
95711927d0fSLaurent Pinchart 	format = v4l2_subdev_get_pad_format(sd, state, CSIS_PAD_SINK);
95811927d0fSLaurent Pinchart 	csis_fmt = find_csis_format(format->code);
95911927d0fSLaurent Pinchart 
9602f03d3cbSLaurent Pinchart 	ret = mipi_csis_calculate_params(csis, csis_fmt);
96146fb9995SMauro Carvalho Chehab 	if (ret < 0)
96211927d0fSLaurent Pinchart 		goto err_unlock;
96346fb9995SMauro Carvalho Chehab 
964c1cc03eaSLaurent Pinchart 	mipi_csis_clear_counters(csis);
96546fb9995SMauro Carvalho Chehab 
966c1cc03eaSLaurent Pinchart 	ret = pm_runtime_resume_and_get(csis->dev);
96746fb9995SMauro Carvalho Chehab 	if (ret < 0)
96811927d0fSLaurent Pinchart 		goto err_unlock;
96946fb9995SMauro Carvalho Chehab 
9702f03d3cbSLaurent Pinchart 	mipi_csis_start_stream(csis, format, csis_fmt);
97111927d0fSLaurent Pinchart 
972c1cc03eaSLaurent Pinchart 	ret = v4l2_subdev_call(csis->src_sd, video, s_stream, 1);
97346fb9995SMauro Carvalho Chehab 	if (ret < 0)
97411927d0fSLaurent Pinchart 		goto err_stop;
97546fb9995SMauro Carvalho Chehab 
976c1cc03eaSLaurent Pinchart 	mipi_csis_log_counters(csis, true);
977284dd848SLaurent Pinchart 
97811927d0fSLaurent Pinchart 	v4l2_subdev_unlock_state(state);
97946fb9995SMauro Carvalho Chehab 
980df4167d9SJacopo Mondi 	return 0;
981df4167d9SJacopo Mondi 
98211927d0fSLaurent Pinchart err_stop:
983df4167d9SJacopo Mondi 	mipi_csis_stop_stream(csis);
984c1cc03eaSLaurent Pinchart 	pm_runtime_put(csis->dev);
98511927d0fSLaurent Pinchart err_unlock:
98611927d0fSLaurent Pinchart 	v4l2_subdev_unlock_state(state);
98746fb9995SMauro Carvalho Chehab 
98846fb9995SMauro Carvalho Chehab 	return ret;
98946fb9995SMauro Carvalho Chehab }
99046fb9995SMauro Carvalho Chehab 
mipi_csis_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)99146fb9995SMauro Carvalho Chehab static int mipi_csis_enum_mbus_code(struct v4l2_subdev *sd,
99246fb9995SMauro Carvalho Chehab 				    struct v4l2_subdev_state *sd_state,
99346fb9995SMauro Carvalho Chehab 				    struct v4l2_subdev_mbus_code_enum *code)
99446fb9995SMauro Carvalho Chehab {
99546fb9995SMauro Carvalho Chehab 	/*
99646fb9995SMauro Carvalho Chehab 	 * The CSIS can't transcode in any way, the source format is identical
99746fb9995SMauro Carvalho Chehab 	 * to the sink format.
99846fb9995SMauro Carvalho Chehab 	 */
99946fb9995SMauro Carvalho Chehab 	if (code->pad == CSIS_PAD_SOURCE) {
100046fb9995SMauro Carvalho Chehab 		struct v4l2_mbus_framefmt *fmt;
100146fb9995SMauro Carvalho Chehab 
100246fb9995SMauro Carvalho Chehab 		if (code->index > 0)
100346fb9995SMauro Carvalho Chehab 			return -EINVAL;
100446fb9995SMauro Carvalho Chehab 
100511927d0fSLaurent Pinchart 		fmt = v4l2_subdev_get_pad_format(sd, sd_state, code->pad);
100646fb9995SMauro Carvalho Chehab 		code->code = fmt->code;
100746fb9995SMauro Carvalho Chehab 		return 0;
100846fb9995SMauro Carvalho Chehab 	}
100946fb9995SMauro Carvalho Chehab 
101046fb9995SMauro Carvalho Chehab 	if (code->pad != CSIS_PAD_SINK)
101146fb9995SMauro Carvalho Chehab 		return -EINVAL;
101246fb9995SMauro Carvalho Chehab 
101346fb9995SMauro Carvalho Chehab 	if (code->index >= ARRAY_SIZE(mipi_csis_formats))
101446fb9995SMauro Carvalho Chehab 		return -EINVAL;
101546fb9995SMauro Carvalho Chehab 
101646fb9995SMauro Carvalho Chehab 	code->code = mipi_csis_formats[code->index].code;
101746fb9995SMauro Carvalho Chehab 
101846fb9995SMauro Carvalho Chehab 	return 0;
101946fb9995SMauro Carvalho Chehab }
102046fb9995SMauro Carvalho Chehab 
mipi_csis_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * sdformat)102146fb9995SMauro Carvalho Chehab static int mipi_csis_set_fmt(struct v4l2_subdev *sd,
102246fb9995SMauro Carvalho Chehab 			     struct v4l2_subdev_state *sd_state,
102346fb9995SMauro Carvalho Chehab 			     struct v4l2_subdev_format *sdformat)
102446fb9995SMauro Carvalho Chehab {
102546fb9995SMauro Carvalho Chehab 	struct csis_pix_format const *csis_fmt;
102646fb9995SMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *fmt;
102746fb9995SMauro Carvalho Chehab 	unsigned int align;
102846fb9995SMauro Carvalho Chehab 
102946fb9995SMauro Carvalho Chehab 	/*
103046fb9995SMauro Carvalho Chehab 	 * The CSIS can't transcode in any way, the source format can't be
103146fb9995SMauro Carvalho Chehab 	 * modified.
103246fb9995SMauro Carvalho Chehab 	 */
103346fb9995SMauro Carvalho Chehab 	if (sdformat->pad == CSIS_PAD_SOURCE)
103411927d0fSLaurent Pinchart 		return v4l2_subdev_get_fmt(sd, sd_state, sdformat);
103546fb9995SMauro Carvalho Chehab 
103646fb9995SMauro Carvalho Chehab 	if (sdformat->pad != CSIS_PAD_SINK)
103746fb9995SMauro Carvalho Chehab 		return -EINVAL;
103846fb9995SMauro Carvalho Chehab 
103946fb9995SMauro Carvalho Chehab 	/*
104046fb9995SMauro Carvalho Chehab 	 * Validate the media bus code and clamp and align the size.
104146fb9995SMauro Carvalho Chehab 	 *
104246fb9995SMauro Carvalho Chehab 	 * The total number of bits per line must be a multiple of 8. We thus
104346fb9995SMauro Carvalho Chehab 	 * need to align the width for formats that are not multiples of 8
104446fb9995SMauro Carvalho Chehab 	 * bits.
104546fb9995SMauro Carvalho Chehab 	 */
104646fb9995SMauro Carvalho Chehab 	csis_fmt = find_csis_format(sdformat->format.code);
104746fb9995SMauro Carvalho Chehab 	if (!csis_fmt)
104846fb9995SMauro Carvalho Chehab 		csis_fmt = &mipi_csis_formats[0];
104946fb9995SMauro Carvalho Chehab 
105046fb9995SMauro Carvalho Chehab 	switch (csis_fmt->width % 8) {
105146fb9995SMauro Carvalho Chehab 	case 0:
105246fb9995SMauro Carvalho Chehab 		align = 0;
105346fb9995SMauro Carvalho Chehab 		break;
105446fb9995SMauro Carvalho Chehab 	case 4:
105546fb9995SMauro Carvalho Chehab 		align = 1;
105646fb9995SMauro Carvalho Chehab 		break;
105746fb9995SMauro Carvalho Chehab 	case 2:
105846fb9995SMauro Carvalho Chehab 	case 6:
105946fb9995SMauro Carvalho Chehab 		align = 2;
106046fb9995SMauro Carvalho Chehab 		break;
106146fb9995SMauro Carvalho Chehab 	default:
106246fb9995SMauro Carvalho Chehab 		/* 1, 3, 5, 7 */
106346fb9995SMauro Carvalho Chehab 		align = 3;
106446fb9995SMauro Carvalho Chehab 		break;
106546fb9995SMauro Carvalho Chehab 	}
106646fb9995SMauro Carvalho Chehab 
106746fb9995SMauro Carvalho Chehab 	v4l_bound_align_image(&sdformat->format.width, 1,
106846fb9995SMauro Carvalho Chehab 			      CSIS_MAX_PIX_WIDTH, align,
106946fb9995SMauro Carvalho Chehab 			      &sdformat->format.height, 1,
107046fb9995SMauro Carvalho Chehab 			      CSIS_MAX_PIX_HEIGHT, 0, 0);
107146fb9995SMauro Carvalho Chehab 
107211927d0fSLaurent Pinchart 	fmt = v4l2_subdev_get_pad_format(sd, sd_state, sdformat->pad);
107346fb9995SMauro Carvalho Chehab 
107446fb9995SMauro Carvalho Chehab 	fmt->code = csis_fmt->code;
107546fb9995SMauro Carvalho Chehab 	fmt->width = sdformat->format.width;
107646fb9995SMauro Carvalho Chehab 	fmt->height = sdformat->format.height;
107777645c6eSLaurent Pinchart 	fmt->field = V4L2_FIELD_NONE;
107846fb9995SMauro Carvalho Chehab 	fmt->colorspace = sdformat->format.colorspace;
107946fb9995SMauro Carvalho Chehab 	fmt->quantization = sdformat->format.quantization;
108046fb9995SMauro Carvalho Chehab 	fmt->xfer_func = sdformat->format.xfer_func;
108146fb9995SMauro Carvalho Chehab 	fmt->ycbcr_enc = sdformat->format.ycbcr_enc;
108246fb9995SMauro Carvalho Chehab 
108346fb9995SMauro Carvalho Chehab 	sdformat->format = *fmt;
108446fb9995SMauro Carvalho Chehab 
108546fb9995SMauro Carvalho Chehab 	/* Propagate the format from sink to source. */
108611927d0fSLaurent Pinchart 	fmt = v4l2_subdev_get_pad_format(sd, sd_state, CSIS_PAD_SOURCE);
108746fb9995SMauro Carvalho Chehab 	*fmt = sdformat->format;
108846fb9995SMauro Carvalho Chehab 
108946fb9995SMauro Carvalho Chehab 	/* The format on the source pad might change due to unpacking. */
109046fb9995SMauro Carvalho Chehab 	fmt->code = csis_fmt->output;
109146fb9995SMauro Carvalho Chehab 
109246fb9995SMauro Carvalho Chehab 	return 0;
109346fb9995SMauro Carvalho Chehab }
109446fb9995SMauro Carvalho Chehab 
mipi_csis_get_frame_desc(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_frame_desc * fd)1095d200de90SLaurent Pinchart static int mipi_csis_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
1096d200de90SLaurent Pinchart 				    struct v4l2_mbus_frame_desc *fd)
1097d200de90SLaurent Pinchart {
1098d200de90SLaurent Pinchart 	struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[0];
109911927d0fSLaurent Pinchart 	const struct csis_pix_format *csis_fmt;
110011927d0fSLaurent Pinchart 	const struct v4l2_mbus_framefmt *fmt;
110111927d0fSLaurent Pinchart 	struct v4l2_subdev_state *state;
1102d200de90SLaurent Pinchart 
1103d200de90SLaurent Pinchart 	if (pad != CSIS_PAD_SOURCE)
1104d200de90SLaurent Pinchart 		return -EINVAL;
1105d200de90SLaurent Pinchart 
110611927d0fSLaurent Pinchart 	state = v4l2_subdev_lock_and_get_active_state(sd);
110711927d0fSLaurent Pinchart 	fmt = v4l2_subdev_get_pad_format(sd, state, CSIS_PAD_SOURCE);
110811927d0fSLaurent Pinchart 	csis_fmt = find_csis_format(fmt->code);
110911927d0fSLaurent Pinchart 	v4l2_subdev_unlock_state(state);
111011927d0fSLaurent Pinchart 
11113e62aba8SMarek Vasut 	if (!csis_fmt)
11123e62aba8SMarek Vasut 		return -EPIPE;
11133e62aba8SMarek Vasut 
1114d200de90SLaurent Pinchart 	fd->type = V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL;
1115d200de90SLaurent Pinchart 	fd->num_entries = 1;
1116d200de90SLaurent Pinchart 
1117d200de90SLaurent Pinchart 	memset(entry, 0, sizeof(*entry));
1118d200de90SLaurent Pinchart 
1119d200de90SLaurent Pinchart 	entry->flags = 0;
112011927d0fSLaurent Pinchart 	entry->pixelcode = csis_fmt->code;
1121d200de90SLaurent Pinchart 	entry->bus.csi2.vc = 0;
112211927d0fSLaurent Pinchart 	entry->bus.csi2.dt = csis_fmt->data_type;
1123d200de90SLaurent Pinchart 
1124d200de90SLaurent Pinchart 	return 0;
1125d200de90SLaurent Pinchart }
1126d200de90SLaurent Pinchart 
mipi_csis_init_cfg(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state)112777645c6eSLaurent Pinchart static int mipi_csis_init_cfg(struct v4l2_subdev *sd,
112877645c6eSLaurent Pinchart 			      struct v4l2_subdev_state *sd_state)
112977645c6eSLaurent Pinchart {
113077645c6eSLaurent Pinchart 	struct v4l2_subdev_format fmt = {
113177645c6eSLaurent Pinchart 		.pad = CSIS_PAD_SINK,
113277645c6eSLaurent Pinchart 	};
113377645c6eSLaurent Pinchart 
113477645c6eSLaurent Pinchart 	fmt.format.code = mipi_csis_formats[0].code;
113577645c6eSLaurent Pinchart 	fmt.format.width = MIPI_CSIS_DEF_PIX_WIDTH;
113677645c6eSLaurent Pinchart 	fmt.format.height = MIPI_CSIS_DEF_PIX_HEIGHT;
113777645c6eSLaurent Pinchart 
113877645c6eSLaurent Pinchart 	fmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
113977645c6eSLaurent Pinchart 	fmt.format.xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt.format.colorspace);
114077645c6eSLaurent Pinchart 	fmt.format.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt.format.colorspace);
114177645c6eSLaurent Pinchart 	fmt.format.quantization =
114277645c6eSLaurent Pinchart 		V4L2_MAP_QUANTIZATION_DEFAULT(false, fmt.format.colorspace,
114377645c6eSLaurent Pinchart 					      fmt.format.ycbcr_enc);
114477645c6eSLaurent Pinchart 
114577645c6eSLaurent Pinchart 	return mipi_csis_set_fmt(sd, sd_state, &fmt);
114677645c6eSLaurent Pinchart }
114777645c6eSLaurent Pinchart 
mipi_csis_log_status(struct v4l2_subdev * sd)114846fb9995SMauro Carvalho Chehab static int mipi_csis_log_status(struct v4l2_subdev *sd)
114946fb9995SMauro Carvalho Chehab {
1150c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
115146fb9995SMauro Carvalho Chehab 
1152c1cc03eaSLaurent Pinchart 	mipi_csis_log_counters(csis, true);
1153851b270bSJacopo Mondi 	if (csis->debug.enable)
1154c1cc03eaSLaurent Pinchart 		mipi_csis_dump_regs(csis);
115546fb9995SMauro Carvalho Chehab 
115646fb9995SMauro Carvalho Chehab 	return 0;
115746fb9995SMauro Carvalho Chehab }
115846fb9995SMauro Carvalho Chehab 
115946fb9995SMauro Carvalho Chehab static const struct v4l2_subdev_core_ops mipi_csis_core_ops = {
116046fb9995SMauro Carvalho Chehab 	.log_status	= mipi_csis_log_status,
116146fb9995SMauro Carvalho Chehab };
116246fb9995SMauro Carvalho Chehab 
116346fb9995SMauro Carvalho Chehab static const struct v4l2_subdev_video_ops mipi_csis_video_ops = {
116446fb9995SMauro Carvalho Chehab 	.s_stream	= mipi_csis_s_stream,
116546fb9995SMauro Carvalho Chehab };
116646fb9995SMauro Carvalho Chehab 
116746fb9995SMauro Carvalho Chehab static const struct v4l2_subdev_pad_ops mipi_csis_pad_ops = {
116846fb9995SMauro Carvalho Chehab 	.init_cfg		= mipi_csis_init_cfg,
116946fb9995SMauro Carvalho Chehab 	.enum_mbus_code		= mipi_csis_enum_mbus_code,
117011927d0fSLaurent Pinchart 	.get_fmt		= v4l2_subdev_get_fmt,
117146fb9995SMauro Carvalho Chehab 	.set_fmt		= mipi_csis_set_fmt,
1172d200de90SLaurent Pinchart 	.get_frame_desc		= mipi_csis_get_frame_desc,
117346fb9995SMauro Carvalho Chehab };
117446fb9995SMauro Carvalho Chehab 
117546fb9995SMauro Carvalho Chehab static const struct v4l2_subdev_ops mipi_csis_subdev_ops = {
117646fb9995SMauro Carvalho Chehab 	.core	= &mipi_csis_core_ops,
117746fb9995SMauro Carvalho Chehab 	.video	= &mipi_csis_video_ops,
117846fb9995SMauro Carvalho Chehab 	.pad	= &mipi_csis_pad_ops,
117946fb9995SMauro Carvalho Chehab };
118046fb9995SMauro Carvalho Chehab 
118146fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
118246fb9995SMauro Carvalho Chehab  * Media entity operations
118346fb9995SMauro Carvalho Chehab  */
118446fb9995SMauro Carvalho Chehab 
mipi_csis_link_setup(struct media_entity * entity,const struct media_pad * local_pad,const struct media_pad * remote_pad,u32 flags)118546fb9995SMauro Carvalho Chehab static int mipi_csis_link_setup(struct media_entity *entity,
118646fb9995SMauro Carvalho Chehab 				const struct media_pad *local_pad,
118746fb9995SMauro Carvalho Chehab 				const struct media_pad *remote_pad, u32 flags)
118846fb9995SMauro Carvalho Chehab {
118946fb9995SMauro Carvalho Chehab 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1190c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
119146fb9995SMauro Carvalho Chehab 	struct v4l2_subdev *remote_sd;
119246fb9995SMauro Carvalho Chehab 
1193c1cc03eaSLaurent Pinchart 	dev_dbg(csis->dev, "link setup %s -> %s", remote_pad->entity->name,
119446fb9995SMauro Carvalho Chehab 		local_pad->entity->name);
119546fb9995SMauro Carvalho Chehab 
119646fb9995SMauro Carvalho Chehab 	/* We only care about the link to the source. */
119746fb9995SMauro Carvalho Chehab 	if (!(local_pad->flags & MEDIA_PAD_FL_SINK))
119846fb9995SMauro Carvalho Chehab 		return 0;
119946fb9995SMauro Carvalho Chehab 
120046fb9995SMauro Carvalho Chehab 	remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity);
120146fb9995SMauro Carvalho Chehab 
120246fb9995SMauro Carvalho Chehab 	if (flags & MEDIA_LNK_FL_ENABLED) {
1203c1cc03eaSLaurent Pinchart 		if (csis->src_sd)
120446fb9995SMauro Carvalho Chehab 			return -EBUSY;
120546fb9995SMauro Carvalho Chehab 
1206c1cc03eaSLaurent Pinchart 		csis->src_sd = remote_sd;
120746fb9995SMauro Carvalho Chehab 	} else {
1208c1cc03eaSLaurent Pinchart 		csis->src_sd = NULL;
120946fb9995SMauro Carvalho Chehab 	}
121046fb9995SMauro Carvalho Chehab 
121146fb9995SMauro Carvalho Chehab 	return 0;
121246fb9995SMauro Carvalho Chehab }
121346fb9995SMauro Carvalho Chehab 
121446fb9995SMauro Carvalho Chehab static const struct media_entity_operations mipi_csis_entity_ops = {
121546fb9995SMauro Carvalho Chehab 	.link_setup	= mipi_csis_link_setup,
121646fb9995SMauro Carvalho Chehab 	.link_validate	= v4l2_subdev_link_validate,
121746fb9995SMauro Carvalho Chehab 	.get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
121846fb9995SMauro Carvalho Chehab };
121946fb9995SMauro Carvalho Chehab 
122046fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
122146fb9995SMauro Carvalho Chehab  * Async subdev notifier
122246fb9995SMauro Carvalho Chehab  */
122346fb9995SMauro Carvalho Chehab 
1224c1cc03eaSLaurent Pinchart static struct mipi_csis_device *
mipi_notifier_to_csis_state(struct v4l2_async_notifier * n)122546fb9995SMauro Carvalho Chehab mipi_notifier_to_csis_state(struct v4l2_async_notifier *n)
122646fb9995SMauro Carvalho Chehab {
1227c1cc03eaSLaurent Pinchart 	return container_of(n, struct mipi_csis_device, notifier);
122846fb9995SMauro Carvalho Chehab }
122946fb9995SMauro Carvalho Chehab 
mipi_csis_notify_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_connection * asd)123046fb9995SMauro Carvalho Chehab static int mipi_csis_notify_bound(struct v4l2_async_notifier *notifier,
123146fb9995SMauro Carvalho Chehab 				  struct v4l2_subdev *sd,
1232adb2dcd5SSakari Ailus 				  struct v4l2_async_connection *asd)
123346fb9995SMauro Carvalho Chehab {
1234c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = mipi_notifier_to_csis_state(notifier);
1235c1cc03eaSLaurent Pinchart 	struct media_pad *sink = &csis->sd.entity.pads[CSIS_PAD_SINK];
123646fb9995SMauro Carvalho Chehab 
123746fb9995SMauro Carvalho Chehab 	return v4l2_create_fwnode_links_to_pad(sd, sink, 0);
123846fb9995SMauro Carvalho Chehab }
123946fb9995SMauro Carvalho Chehab 
124046fb9995SMauro Carvalho Chehab static const struct v4l2_async_notifier_operations mipi_csis_notify_ops = {
124146fb9995SMauro Carvalho Chehab 	.bound = mipi_csis_notify_bound,
124246fb9995SMauro Carvalho Chehab };
124346fb9995SMauro Carvalho Chehab 
mipi_csis_async_register(struct mipi_csis_device * csis)1244c1cc03eaSLaurent Pinchart static int mipi_csis_async_register(struct mipi_csis_device *csis)
124546fb9995SMauro Carvalho Chehab {
124646fb9995SMauro Carvalho Chehab 	struct v4l2_fwnode_endpoint vep = {
124746fb9995SMauro Carvalho Chehab 		.bus_type = V4L2_MBUS_CSI2_DPHY,
124846fb9995SMauro Carvalho Chehab 	};
1249adb2dcd5SSakari Ailus 	struct v4l2_async_connection *asd;
125046fb9995SMauro Carvalho Chehab 	struct fwnode_handle *ep;
125146fb9995SMauro Carvalho Chehab 	unsigned int i;
125246fb9995SMauro Carvalho Chehab 	int ret;
125346fb9995SMauro Carvalho Chehab 
1254b8ec754aSSakari Ailus 	v4l2_async_subdev_nf_init(&csis->notifier, &csis->sd);
125546fb9995SMauro Carvalho Chehab 
1256c1cc03eaSLaurent Pinchart 	ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csis->dev), 0, 0,
125746fb9995SMauro Carvalho Chehab 					     FWNODE_GRAPH_ENDPOINT_NEXT);
125846fb9995SMauro Carvalho Chehab 	if (!ep)
125946fb9995SMauro Carvalho Chehab 		return -ENOTCONN;
126046fb9995SMauro Carvalho Chehab 
126146fb9995SMauro Carvalho Chehab 	ret = v4l2_fwnode_endpoint_parse(ep, &vep);
126246fb9995SMauro Carvalho Chehab 	if (ret)
126346fb9995SMauro Carvalho Chehab 		goto err_parse;
126446fb9995SMauro Carvalho Chehab 
126546fb9995SMauro Carvalho Chehab 	for (i = 0; i < vep.bus.mipi_csi2.num_data_lanes; ++i) {
126646fb9995SMauro Carvalho Chehab 		if (vep.bus.mipi_csi2.data_lanes[i] != i + 1) {
1267c1cc03eaSLaurent Pinchart 			dev_err(csis->dev,
126846fb9995SMauro Carvalho Chehab 				"data lanes reordering is not supported");
126946fb9995SMauro Carvalho Chehab 			ret = -EINVAL;
127046fb9995SMauro Carvalho Chehab 			goto err_parse;
127146fb9995SMauro Carvalho Chehab 		}
127246fb9995SMauro Carvalho Chehab 	}
127346fb9995SMauro Carvalho Chehab 
1274c1cc03eaSLaurent Pinchart 	csis->bus = vep.bus.mipi_csi2;
127546fb9995SMauro Carvalho Chehab 
1276c1cc03eaSLaurent Pinchart 	dev_dbg(csis->dev, "data lanes: %d\n", csis->bus.num_data_lanes);
1277c1cc03eaSLaurent Pinchart 	dev_dbg(csis->dev, "flags: 0x%08x\n", csis->bus.flags);
127846fb9995SMauro Carvalho Chehab 
1279c1cc03eaSLaurent Pinchart 	asd = v4l2_async_nf_add_fwnode_remote(&csis->notifier, ep,
1280adb2dcd5SSakari Ailus 					      struct v4l2_async_connection);
128146fb9995SMauro Carvalho Chehab 	if (IS_ERR(asd)) {
128246fb9995SMauro Carvalho Chehab 		ret = PTR_ERR(asd);
128346fb9995SMauro Carvalho Chehab 		goto err_parse;
128446fb9995SMauro Carvalho Chehab 	}
128546fb9995SMauro Carvalho Chehab 
128646fb9995SMauro Carvalho Chehab 	fwnode_handle_put(ep);
128746fb9995SMauro Carvalho Chehab 
1288c1cc03eaSLaurent Pinchart 	csis->notifier.ops = &mipi_csis_notify_ops;
128946fb9995SMauro Carvalho Chehab 
1290b8ec754aSSakari Ailus 	ret = v4l2_async_nf_register(&csis->notifier);
129146fb9995SMauro Carvalho Chehab 	if (ret)
129246fb9995SMauro Carvalho Chehab 		return ret;
129346fb9995SMauro Carvalho Chehab 
1294c1cc03eaSLaurent Pinchart 	return v4l2_async_register_subdev(&csis->sd);
129546fb9995SMauro Carvalho Chehab 
129646fb9995SMauro Carvalho Chehab err_parse:
129746fb9995SMauro Carvalho Chehab 	fwnode_handle_put(ep);
129846fb9995SMauro Carvalho Chehab 
129946fb9995SMauro Carvalho Chehab 	return ret;
130046fb9995SMauro Carvalho Chehab }
130146fb9995SMauro Carvalho Chehab 
130246fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
130346fb9995SMauro Carvalho Chehab  * Suspend/resume
130446fb9995SMauro Carvalho Chehab  */
130546fb9995SMauro Carvalho Chehab 
mipi_csis_runtime_suspend(struct device * dev)130624aad87bSLaurent Pinchart static int __maybe_unused mipi_csis_runtime_suspend(struct device *dev)
130746fb9995SMauro Carvalho Chehab {
130846fb9995SMauro Carvalho Chehab 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1309c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1310b6a736e7SLaurent Pinchart 	int ret;
1311c22afddcSLaurent Pinchart 
1312c1cc03eaSLaurent Pinchart 	ret = mipi_csis_phy_disable(csis);
131346fb9995SMauro Carvalho Chehab 	if (ret)
1314b6a736e7SLaurent Pinchart 		return -EAGAIN;
1315c22afddcSLaurent Pinchart 
1316c1cc03eaSLaurent Pinchart 	mipi_csis_clk_disable(csis);
1317c22afddcSLaurent Pinchart 
1318b6a736e7SLaurent Pinchart 	return 0;
131946fb9995SMauro Carvalho Chehab }
132046fb9995SMauro Carvalho Chehab 
mipi_csis_runtime_resume(struct device * dev)132124aad87bSLaurent Pinchart static int __maybe_unused mipi_csis_runtime_resume(struct device *dev)
132246fb9995SMauro Carvalho Chehab {
132346fb9995SMauro Carvalho Chehab 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1324c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1325b6a736e7SLaurent Pinchart 	int ret;
132646fb9995SMauro Carvalho Chehab 
1327c1cc03eaSLaurent Pinchart 	ret = mipi_csis_phy_enable(csis);
132846fb9995SMauro Carvalho Chehab 	if (ret)
1329b6a736e7SLaurent Pinchart 		return -EAGAIN;
133046fb9995SMauro Carvalho Chehab 
1331b6a736e7SLaurent Pinchart 	ret = mipi_csis_clk_enable(csis);
1332b6a736e7SLaurent Pinchart 	if (ret) {
1333b6a736e7SLaurent Pinchart 		mipi_csis_phy_disable(csis);
1334b6a736e7SLaurent Pinchart 		return ret;
1335b6a736e7SLaurent Pinchart 	}
1336c22afddcSLaurent Pinchart 
1337b6a736e7SLaurent Pinchart 	return 0;
133846fb9995SMauro Carvalho Chehab }
133946fb9995SMauro Carvalho Chehab 
134046fb9995SMauro Carvalho Chehab static const struct dev_pm_ops mipi_csis_pm_ops = {
134146fb9995SMauro Carvalho Chehab 	SET_RUNTIME_PM_OPS(mipi_csis_runtime_suspend, mipi_csis_runtime_resume,
134246fb9995SMauro Carvalho Chehab 			   NULL)
134346fb9995SMauro Carvalho Chehab };
134446fb9995SMauro Carvalho Chehab 
134546fb9995SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
134646fb9995SMauro Carvalho Chehab  * Probe/remove & platform driver
134746fb9995SMauro Carvalho Chehab  */
134846fb9995SMauro Carvalho Chehab 
mipi_csis_subdev_init(struct mipi_csis_device * csis)1349c1cc03eaSLaurent Pinchart static int mipi_csis_subdev_init(struct mipi_csis_device *csis)
135046fb9995SMauro Carvalho Chehab {
1351c1cc03eaSLaurent Pinchart 	struct v4l2_subdev *sd = &csis->sd;
135211927d0fSLaurent Pinchart 	int ret;
135346fb9995SMauro Carvalho Chehab 
135446fb9995SMauro Carvalho Chehab 	v4l2_subdev_init(sd, &mipi_csis_subdev_ops);
135546fb9995SMauro Carvalho Chehab 	sd->owner = THIS_MODULE;
135646fb9995SMauro Carvalho Chehab 	snprintf(sd->name, sizeof(sd->name), "csis-%s",
1357c1cc03eaSLaurent Pinchart 		 dev_name(csis->dev));
135846fb9995SMauro Carvalho Chehab 
135946fb9995SMauro Carvalho Chehab 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
136046fb9995SMauro Carvalho Chehab 	sd->ctrl_handler = NULL;
136146fb9995SMauro Carvalho Chehab 
136246fb9995SMauro Carvalho Chehab 	sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
136346fb9995SMauro Carvalho Chehab 	sd->entity.ops = &mipi_csis_entity_ops;
136446fb9995SMauro Carvalho Chehab 
1365c1cc03eaSLaurent Pinchart 	sd->dev = csis->dev;
136646fb9995SMauro Carvalho Chehab 
1367c1cc03eaSLaurent Pinchart 	csis->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK
136846fb9995SMauro Carvalho Chehab 					 | MEDIA_PAD_FL_MUST_CONNECT;
1369c1cc03eaSLaurent Pinchart 	csis->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE
137046fb9995SMauro Carvalho Chehab 					   | MEDIA_PAD_FL_MUST_CONNECT;
137111927d0fSLaurent Pinchart 	ret = media_entity_pads_init(&sd->entity, CSIS_PADS_NUM, csis->pads);
137211927d0fSLaurent Pinchart 	if (ret)
137311927d0fSLaurent Pinchart 		return ret;
137411927d0fSLaurent Pinchart 
137511927d0fSLaurent Pinchart 	ret = v4l2_subdev_init_finalize(sd);
137611927d0fSLaurent Pinchart 	if (ret) {
137711927d0fSLaurent Pinchart 		media_entity_cleanup(&sd->entity);
137811927d0fSLaurent Pinchart 		return ret;
137911927d0fSLaurent Pinchart 	}
138011927d0fSLaurent Pinchart 
138111927d0fSLaurent Pinchart 	return 0;
138246fb9995SMauro Carvalho Chehab }
138346fb9995SMauro Carvalho Chehab 
mipi_csis_parse_dt(struct mipi_csis_device * csis)1384c1cc03eaSLaurent Pinchart static int mipi_csis_parse_dt(struct mipi_csis_device *csis)
138546fb9995SMauro Carvalho Chehab {
1386c1cc03eaSLaurent Pinchart 	struct device_node *node = csis->dev->of_node;
138746fb9995SMauro Carvalho Chehab 
138846fb9995SMauro Carvalho Chehab 	if (of_property_read_u32(node, "clock-frequency",
1389c1cc03eaSLaurent Pinchart 				 &csis->clk_frequency))
1390c1cc03eaSLaurent Pinchart 		csis->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
139146fb9995SMauro Carvalho Chehab 
139246fb9995SMauro Carvalho Chehab 	return 0;
139346fb9995SMauro Carvalho Chehab }
139446fb9995SMauro Carvalho Chehab 
mipi_csis_probe(struct platform_device * pdev)139546fb9995SMauro Carvalho Chehab static int mipi_csis_probe(struct platform_device *pdev)
139646fb9995SMauro Carvalho Chehab {
139746fb9995SMauro Carvalho Chehab 	struct device *dev = &pdev->dev;
1398c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis;
139946fb9995SMauro Carvalho Chehab 	int irq;
140046fb9995SMauro Carvalho Chehab 	int ret;
140146fb9995SMauro Carvalho Chehab 
1402c1cc03eaSLaurent Pinchart 	csis = devm_kzalloc(dev, sizeof(*csis), GFP_KERNEL);
1403c1cc03eaSLaurent Pinchart 	if (!csis)
140446fb9995SMauro Carvalho Chehab 		return -ENOMEM;
140546fb9995SMauro Carvalho Chehab 
1406c1cc03eaSLaurent Pinchart 	spin_lock_init(&csis->slock);
140746fb9995SMauro Carvalho Chehab 
1408c1cc03eaSLaurent Pinchart 	csis->dev = dev;
1409c1cc03eaSLaurent Pinchart 	csis->info = of_device_get_match_data(dev);
141046fb9995SMauro Carvalho Chehab 
1411c1cc03eaSLaurent Pinchart 	memcpy(csis->events, mipi_csis_events, sizeof(csis->events));
141246fb9995SMauro Carvalho Chehab 
141346fb9995SMauro Carvalho Chehab 	/* Parse DT properties. */
1414c1cc03eaSLaurent Pinchart 	ret = mipi_csis_parse_dt(csis);
141546fb9995SMauro Carvalho Chehab 	if (ret < 0) {
141646fb9995SMauro Carvalho Chehab 		dev_err(dev, "Failed to parse device tree: %d\n", ret);
141746fb9995SMauro Carvalho Chehab 		return ret;
141846fb9995SMauro Carvalho Chehab 	}
141946fb9995SMauro Carvalho Chehab 
142046fb9995SMauro Carvalho Chehab 	/* Acquire resources. */
1421c1cc03eaSLaurent Pinchart 	csis->regs = devm_platform_ioremap_resource(pdev, 0);
1422c1cc03eaSLaurent Pinchart 	if (IS_ERR(csis->regs))
1423c1cc03eaSLaurent Pinchart 		return PTR_ERR(csis->regs);
142446fb9995SMauro Carvalho Chehab 
142546fb9995SMauro Carvalho Chehab 	irq = platform_get_irq(pdev, 0);
142646fb9995SMauro Carvalho Chehab 	if (irq < 0)
142746fb9995SMauro Carvalho Chehab 		return irq;
142846fb9995SMauro Carvalho Chehab 
1429c1cc03eaSLaurent Pinchart 	ret = mipi_csis_phy_init(csis);
143046fb9995SMauro Carvalho Chehab 	if (ret < 0)
143146fb9995SMauro Carvalho Chehab 		return ret;
143246fb9995SMauro Carvalho Chehab 
1433c1cc03eaSLaurent Pinchart 	ret = mipi_csis_clk_get(csis);
143446fb9995SMauro Carvalho Chehab 	if (ret < 0)
143546fb9995SMauro Carvalho Chehab 		return ret;
143646fb9995SMauro Carvalho Chehab 
143746fb9995SMauro Carvalho Chehab 	/* Reset PHY and enable the clocks. */
1438c1cc03eaSLaurent Pinchart 	mipi_csis_phy_reset(csis);
143946fb9995SMauro Carvalho Chehab 
144046fb9995SMauro Carvalho Chehab 	/* Now that the hardware is initialized, request the interrupt. */
144146fb9995SMauro Carvalho Chehab 	ret = devm_request_irq(dev, irq, mipi_csis_irq_handler, 0,
1442c1cc03eaSLaurent Pinchart 			       dev_name(dev), csis);
144346fb9995SMauro Carvalho Chehab 	if (ret) {
144446fb9995SMauro Carvalho Chehab 		dev_err(dev, "Interrupt request failed\n");
1445*c9354bffSTomi Valkeinen 		return ret;
144646fb9995SMauro Carvalho Chehab 	}
144746fb9995SMauro Carvalho Chehab 
144846fb9995SMauro Carvalho Chehab 	/* Initialize and register the subdev. */
1449c1cc03eaSLaurent Pinchart 	ret = mipi_csis_subdev_init(csis);
145046fb9995SMauro Carvalho Chehab 	if (ret < 0)
1451*c9354bffSTomi Valkeinen 		return ret;
145246fb9995SMauro Carvalho Chehab 
1453c1cc03eaSLaurent Pinchart 	platform_set_drvdata(pdev, &csis->sd);
145446fb9995SMauro Carvalho Chehab 
1455c1cc03eaSLaurent Pinchart 	ret = mipi_csis_async_register(csis);
145646fb9995SMauro Carvalho Chehab 	if (ret < 0) {
145746fb9995SMauro Carvalho Chehab 		dev_err(dev, "async register failed: %d\n", ret);
1458a42b43f7SLaurent Pinchart 		goto err_cleanup;
145946fb9995SMauro Carvalho Chehab 	}
146046fb9995SMauro Carvalho Chehab 
146146fb9995SMauro Carvalho Chehab 	/* Initialize debugfs. */
1462c1cc03eaSLaurent Pinchart 	mipi_csis_debugfs_init(csis);
146346fb9995SMauro Carvalho Chehab 
146446fb9995SMauro Carvalho Chehab 	/* Enable runtime PM. */
146546fb9995SMauro Carvalho Chehab 	pm_runtime_enable(dev);
146646fb9995SMauro Carvalho Chehab 	if (!pm_runtime_enabled(dev)) {
146724aad87bSLaurent Pinchart 		ret = mipi_csis_runtime_resume(dev);
146846fb9995SMauro Carvalho Chehab 		if (ret < 0)
1469a42b43f7SLaurent Pinchart 			goto err_unregister_all;
147046fb9995SMauro Carvalho Chehab 	}
147146fb9995SMauro Carvalho Chehab 
147246fb9995SMauro Carvalho Chehab 	dev_info(dev, "lanes: %d, freq: %u\n",
1473c1cc03eaSLaurent Pinchart 		 csis->bus.num_data_lanes, csis->clk_frequency);
147446fb9995SMauro Carvalho Chehab 
147546fb9995SMauro Carvalho Chehab 	return 0;
147646fb9995SMauro Carvalho Chehab 
1477a42b43f7SLaurent Pinchart err_unregister_all:
1478c1cc03eaSLaurent Pinchart 	mipi_csis_debugfs_exit(csis);
1479a42b43f7SLaurent Pinchart err_cleanup:
148011927d0fSLaurent Pinchart 	v4l2_subdev_cleanup(&csis->sd);
1481c1cc03eaSLaurent Pinchart 	media_entity_cleanup(&csis->sd.entity);
1482c1cc03eaSLaurent Pinchart 	v4l2_async_nf_unregister(&csis->notifier);
1483c1cc03eaSLaurent Pinchart 	v4l2_async_nf_cleanup(&csis->notifier);
1484c1cc03eaSLaurent Pinchart 	v4l2_async_unregister_subdev(&csis->sd);
148546fb9995SMauro Carvalho Chehab 
148646fb9995SMauro Carvalho Chehab 	return ret;
148746fb9995SMauro Carvalho Chehab }
148846fb9995SMauro Carvalho Chehab 
mipi_csis_remove(struct platform_device * pdev)14899457f2d9SUwe Kleine-König static void mipi_csis_remove(struct platform_device *pdev)
149046fb9995SMauro Carvalho Chehab {
149146fb9995SMauro Carvalho Chehab 	struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1492c1cc03eaSLaurent Pinchart 	struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
149346fb9995SMauro Carvalho Chehab 
1494c1cc03eaSLaurent Pinchart 	mipi_csis_debugfs_exit(csis);
1495c1cc03eaSLaurent Pinchart 	v4l2_async_nf_unregister(&csis->notifier);
1496c1cc03eaSLaurent Pinchart 	v4l2_async_nf_cleanup(&csis->notifier);
1497c1cc03eaSLaurent Pinchart 	v4l2_async_unregister_subdev(&csis->sd);
149846fb9995SMauro Carvalho Chehab 
14990e3535eeSTomi Valkeinen 	if (!pm_runtime_enabled(&pdev->dev))
150024aad87bSLaurent Pinchart 		mipi_csis_runtime_suspend(&pdev->dev);
15010e3535eeSTomi Valkeinen 
15020e3535eeSTomi Valkeinen 	pm_runtime_disable(&pdev->dev);
150311927d0fSLaurent Pinchart 	v4l2_subdev_cleanup(&csis->sd);
1504c1cc03eaSLaurent Pinchart 	media_entity_cleanup(&csis->sd.entity);
150546fb9995SMauro Carvalho Chehab 	pm_runtime_set_suspended(&pdev->dev);
150646fb9995SMauro Carvalho Chehab }
150746fb9995SMauro Carvalho Chehab 
150846fb9995SMauro Carvalho Chehab static const struct of_device_id mipi_csis_of_match[] = {
150946fb9995SMauro Carvalho Chehab 	{
151046fb9995SMauro Carvalho Chehab 		.compatible = "fsl,imx7-mipi-csi2",
151146fb9995SMauro Carvalho Chehab 		.data = &(const struct mipi_csis_info){
151246fb9995SMauro Carvalho Chehab 			.version = MIPI_CSIS_V3_3,
151346fb9995SMauro Carvalho Chehab 			.num_clocks = 3,
151446fb9995SMauro Carvalho Chehab 		},
151546fb9995SMauro Carvalho Chehab 	}, {
151646fb9995SMauro Carvalho Chehab 		.compatible = "fsl,imx8mm-mipi-csi2",
151746fb9995SMauro Carvalho Chehab 		.data = &(const struct mipi_csis_info){
151846fb9995SMauro Carvalho Chehab 			.version = MIPI_CSIS_V3_6_3,
151946fb9995SMauro Carvalho Chehab 			.num_clocks = 4,
152046fb9995SMauro Carvalho Chehab 		},
152146fb9995SMauro Carvalho Chehab 	},
152246fb9995SMauro Carvalho Chehab 	{ /* sentinel */ },
152346fb9995SMauro Carvalho Chehab };
152446fb9995SMauro Carvalho Chehab MODULE_DEVICE_TABLE(of, mipi_csis_of_match);
152546fb9995SMauro Carvalho Chehab 
152646fb9995SMauro Carvalho Chehab static struct platform_driver mipi_csis_driver = {
152746fb9995SMauro Carvalho Chehab 	.probe		= mipi_csis_probe,
15289457f2d9SUwe Kleine-König 	.remove_new	= mipi_csis_remove,
152946fb9995SMauro Carvalho Chehab 	.driver		= {
153046fb9995SMauro Carvalho Chehab 		.of_match_table = mipi_csis_of_match,
153146fb9995SMauro Carvalho Chehab 		.name		= CSIS_DRIVER_NAME,
153246fb9995SMauro Carvalho Chehab 		.pm		= &mipi_csis_pm_ops,
153346fb9995SMauro Carvalho Chehab 	},
153446fb9995SMauro Carvalho Chehab };
153546fb9995SMauro Carvalho Chehab 
153646fb9995SMauro Carvalho Chehab module_platform_driver(mipi_csis_driver);
153746fb9995SMauro Carvalho Chehab 
153846fb9995SMauro Carvalho Chehab MODULE_DESCRIPTION("i.MX7 & i.MX8 MIPI CSI-2 receiver driver");
153946fb9995SMauro Carvalho Chehab MODULE_LICENSE("GPL v2");
154046fb9995SMauro Carvalho Chehab MODULE_ALIAS("platform:imx-mipi-csi2");
1541