1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Samsung CSIS MIPI CSI-2 receiver driver.
4  *
5  * The Samsung CSIS IP is a MIPI CSI-2 receiver found in various NXP i.MX7 and
6  * i.MX8 SoCs. The i.MX7 features version 3.3 of the IP, while i.MX8 features
7  * version 3.6.3.
8  *
9  * Copyright (C) 2019 Linaro Ltd
10  * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. All Rights Reserved.
11  * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
12  *
13  */
14 
15 #include <linux/clk.h>
16 #include <linux/debugfs.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/reset.h>
30 #include <linux/spinlock.h>
31 
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-fwnode.h>
35 #include <media/v4l2-mc.h>
36 #include <media/v4l2-subdev.h>
37 
38 #define CSIS_DRIVER_NAME			"imx-mipi-csis"
39 
40 #define CSIS_PAD_SINK				0
41 #define CSIS_PAD_SOURCE				1
42 #define CSIS_PADS_NUM				2
43 
44 #define MIPI_CSIS_DEF_PIX_WIDTH			640
45 #define MIPI_CSIS_DEF_PIX_HEIGHT		480
46 
47 /* Register map definition */
48 
49 /* CSIS common control */
50 #define MIPI_CSIS_CMN_CTRL			0x04
51 #define MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW	BIT(16)
52 #define MIPI_CSIS_CMN_CTRL_INTER_MODE		BIT(10)
53 #define MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL	BIT(2)
54 #define MIPI_CSIS_CMN_CTRL_RESET		BIT(1)
55 #define MIPI_CSIS_CMN_CTRL_ENABLE		BIT(0)
56 
57 #define MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET	8
58 #define MIPI_CSIS_CMN_CTRL_LANE_NR_MASK		(3 << 8)
59 
60 /* CSIS clock control */
61 #define MIPI_CSIS_CLK_CTRL			0x08
62 #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH3(x)	((x) << 28)
63 #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH2(x)	((x) << 24)
64 #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH1(x)	((x) << 20)
65 #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(x)	((x) << 16)
66 #define MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK	(0xf << 4)
67 #define MIPI_CSIS_CLK_CTRL_WCLK_SRC		BIT(0)
68 
69 /* CSIS Interrupt mask */
70 #define MIPI_CSIS_INT_MSK			0x10
71 #define MIPI_CSIS_INT_MSK_EVEN_BEFORE		BIT(31)
72 #define MIPI_CSIS_INT_MSK_EVEN_AFTER		BIT(30)
73 #define MIPI_CSIS_INT_MSK_ODD_BEFORE		BIT(29)
74 #define MIPI_CSIS_INT_MSK_ODD_AFTER		BIT(28)
75 #define MIPI_CSIS_INT_MSK_FRAME_START		BIT(24)
76 #define MIPI_CSIS_INT_MSK_FRAME_END		BIT(20)
77 #define MIPI_CSIS_INT_MSK_ERR_SOT_HS		BIT(16)
78 #define MIPI_CSIS_INT_MSK_ERR_LOST_FS		BIT(12)
79 #define MIPI_CSIS_INT_MSK_ERR_LOST_FE		BIT(8)
80 #define MIPI_CSIS_INT_MSK_ERR_OVER		BIT(4)
81 #define MIPI_CSIS_INT_MSK_ERR_WRONG_CFG		BIT(3)
82 #define MIPI_CSIS_INT_MSK_ERR_ECC		BIT(2)
83 #define MIPI_CSIS_INT_MSK_ERR_CRC		BIT(1)
84 #define MIPI_CSIS_INT_MSK_ERR_UNKNOWN		BIT(0)
85 
86 /* CSIS Interrupt source */
87 #define MIPI_CSIS_INT_SRC			0x14
88 #define MIPI_CSIS_INT_SRC_EVEN_BEFORE		BIT(31)
89 #define MIPI_CSIS_INT_SRC_EVEN_AFTER		BIT(30)
90 #define MIPI_CSIS_INT_SRC_EVEN			BIT(30)
91 #define MIPI_CSIS_INT_SRC_ODD_BEFORE		BIT(29)
92 #define MIPI_CSIS_INT_SRC_ODD_AFTER		BIT(28)
93 #define MIPI_CSIS_INT_SRC_ODD			(0x3 << 28)
94 #define MIPI_CSIS_INT_SRC_NON_IMAGE_DATA	(0xf << 28)
95 #define MIPI_CSIS_INT_SRC_FRAME_START		BIT(24)
96 #define MIPI_CSIS_INT_SRC_FRAME_END		BIT(20)
97 #define MIPI_CSIS_INT_SRC_ERR_SOT_HS		BIT(16)
98 #define MIPI_CSIS_INT_SRC_ERR_LOST_FS		BIT(12)
99 #define MIPI_CSIS_INT_SRC_ERR_LOST_FE		BIT(8)
100 #define MIPI_CSIS_INT_SRC_ERR_OVER		BIT(4)
101 #define MIPI_CSIS_INT_SRC_ERR_WRONG_CFG		BIT(3)
102 #define MIPI_CSIS_INT_SRC_ERR_ECC		BIT(2)
103 #define MIPI_CSIS_INT_SRC_ERR_CRC		BIT(1)
104 #define MIPI_CSIS_INT_SRC_ERR_UNKNOWN		BIT(0)
105 #define MIPI_CSIS_INT_SRC_ERRORS		0xfffff
106 
107 /* D-PHY status control */
108 #define MIPI_CSIS_DPHY_STATUS			0x20
109 #define MIPI_CSIS_DPHY_STATUS_ULPS_DAT		BIT(8)
110 #define MIPI_CSIS_DPHY_STATUS_STOPSTATE_DAT	BIT(4)
111 #define MIPI_CSIS_DPHY_STATUS_ULPS_CLK		BIT(1)
112 #define MIPI_CSIS_DPHY_STATUS_STOPSTATE_CLK	BIT(0)
113 
114 /* D-PHY common control */
115 #define MIPI_CSIS_DPHY_CMN_CTRL			0x24
116 #define MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(n)	((n) << 24)
117 #define MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE_MASK	GENMASK(31, 24)
118 #define MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(n)	((n) << 22)
119 #define MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE_MASK	GENMASK(23, 22)
120 #define MIPI_CSIS_DPHY_CMN_CTRL_DPDN_SWAP_CLK	BIT(6)
121 #define MIPI_CSIS_DPHY_CMN_CTRL_DPDN_SWAP_DAT	BIT(5)
122 #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE_DAT	BIT(1)
123 #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE_CLK	BIT(0)
124 #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE		(0x1f << 0)
125 
126 /* D-PHY Master and Slave Control register Low */
127 #define MIPI_CSIS_DPHY_BCTRL_L			0x30
128 #define MIPI_CSIS_DPHY_BCTRL_L_USER_DATA_PATTERN_LOW(n)		(((n) & 3U) << 30)
129 #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV		(0 << 28)
130 #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_724MV		(1 << 28)
131 #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_733MV		(2 << 28)
132 #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_706MV		(3 << 28)
133 #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ		(0 << 27)
134 #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_1_5MHZ		(1 << 27)
135 #define MIPI_CSIS_DPHY_BCTRL_L_VREG12_EXTPWR_EN_CTL		BIT(26)
136 #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V		(0 << 24)
137 #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_23V		(1 << 24)
138 #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_17V		(2 << 24)
139 #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_26V		(3 << 24)
140 #define MIPI_CSIS_DPHY_BCTRL_L_REG_1P2_LVL_SEL			BIT(23)
141 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV		(0 << 21)
142 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_100MV		(1 << 21)
143 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_120MV		(2 << 21)
144 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_140MV		(3 << 21)
145 #define MIPI_CSIS_DPHY_BCTRL_L_VREF_SRC_SEL			BIT(20)
146 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV		(0 << 18)
147 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_743MV		(1 << 18)
148 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_650MV		(2 << 18)
149 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_682MV		(3 << 18)
150 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_PULSE_REJECT		BIT(17)
151 #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_0	(0 << 15)
152 #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_15P	(1 << 15)
153 #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_30P	(3 << 15)
154 #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_UP		BIT(14)
155 #define MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV			(0 << 13)
156 #define MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_70MV			(1 << 13)
157 #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_EN			BIT(12)
158 #define MIPI_CSIS_DPHY_BCTRL_L_ERRCONTENTION_LP_EN		BIT(11)
159 #define MIPI_CSIS_DPHY_BCTRL_L_TXTRIGGER_CLK_EN			BIT(10)
160 #define MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(n)			(((n) * 25 / 1000000) << 0)
161 
162 /* D-PHY Master and Slave Control register High */
163 #define MIPI_CSIS_DPHY_BCTRL_H			0x34
164 /* D-PHY Slave Control register Low */
165 #define MIPI_CSIS_DPHY_SCTRL_L			0x38
166 /* D-PHY Slave Control register High */
167 #define MIPI_CSIS_DPHY_SCTRL_H			0x3c
168 
169 /* ISP Configuration register */
170 #define MIPI_CSIS_ISP_CONFIG_CH(n)		(0x40 + (n) * 0x10)
171 #define MIPI_CSIS_ISPCFG_MEM_FULL_GAP_MSK	(0xff << 24)
172 #define MIPI_CSIS_ISPCFG_MEM_FULL_GAP(x)	((x) << 24)
173 #define MIPI_CSIS_ISPCFG_PIXEL_MODE_SINGLE	(0 << 12)
174 #define MIPI_CSIS_ISPCFG_PIXEL_MODE_DUAL	(1 << 12)
175 #define MIPI_CSIS_ISPCFG_PIXEL_MODE_QUAD	(2 << 12)	/* i.MX8M[MNP] only */
176 #define MIPI_CSIS_ISPCFG_PIXEL_MASK		(3 << 12)
177 #define MIPI_CSIS_ISPCFG_ALIGN_32BIT		BIT(11)
178 #define MIPI_CSIS_ISPCFG_FMT(fmt)		((fmt) << 2)
179 #define MIPI_CSIS_ISPCFG_FMT_MASK		(0x3f << 2)
180 
181 /* ISP Image Resolution register */
182 #define MIPI_CSIS_ISP_RESOL_CH(n)		(0x44 + (n) * 0x10)
183 #define CSIS_MAX_PIX_WIDTH			0xffff
184 #define CSIS_MAX_PIX_HEIGHT			0xffff
185 
186 /* ISP SYNC register */
187 #define MIPI_CSIS_ISP_SYNC_CH(n)		(0x48 + (n) * 0x10)
188 #define MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET	18
189 #define MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET	12
190 #define MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET	0
191 
192 /* ISP shadow registers */
193 #define MIPI_CSIS_SDW_CONFIG_CH(n)		(0x80 + (n) * 0x10)
194 #define MIPI_CSIS_SDW_RESOL_CH(n)		(0x84 + (n) * 0x10)
195 #define MIPI_CSIS_SDW_SYNC_CH(n)		(0x88 + (n) * 0x10)
196 
197 /* Debug control register */
198 #define MIPI_CSIS_DBG_CTRL			0xc0
199 #define MIPI_CSIS_DBG_INTR_MSK			0xc4
200 #define MIPI_CSIS_DBG_INTR_MSK_DT_NOT_SUPPORT	BIT(25)
201 #define MIPI_CSIS_DBG_INTR_MSK_DT_IGNORE	BIT(24)
202 #define MIPI_CSIS_DBG_INTR_MSK_ERR_FRAME_SIZE	BIT(20)
203 #define MIPI_CSIS_DBG_INTR_MSK_TRUNCATED_FRAME	BIT(16)
204 #define MIPI_CSIS_DBG_INTR_MSK_EARLY_FE		BIT(12)
205 #define MIPI_CSIS_DBG_INTR_MSK_EARLY_FS		BIT(8)
206 #define MIPI_CSIS_DBG_INTR_MSK_CAM_VSYNC_FALL	BIT(4)
207 #define MIPI_CSIS_DBG_INTR_MSK_CAM_VSYNC_RISE	BIT(0)
208 #define MIPI_CSIS_DBG_INTR_SRC			0xc8
209 #define MIPI_CSIS_DBG_INTR_SRC_DT_NOT_SUPPORT	BIT(25)
210 #define MIPI_CSIS_DBG_INTR_SRC_DT_IGNORE	BIT(24)
211 #define MIPI_CSIS_DBG_INTR_SRC_ERR_FRAME_SIZE	BIT(20)
212 #define MIPI_CSIS_DBG_INTR_SRC_TRUNCATED_FRAME	BIT(16)
213 #define MIPI_CSIS_DBG_INTR_SRC_EARLY_FE		BIT(12)
214 #define MIPI_CSIS_DBG_INTR_SRC_EARLY_FS		BIT(8)
215 #define MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_FALL	BIT(4)
216 #define MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_RISE	BIT(0)
217 
218 #define MIPI_CSIS_FRAME_COUNTER_CH(n)		(0x0100 + (n) * 4)
219 
220 /* Non-image packet data buffers */
221 #define MIPI_CSIS_PKTDATA_ODD			0x2000
222 #define MIPI_CSIS_PKTDATA_EVEN			0x3000
223 #define MIPI_CSIS_PKTDATA_SIZE			SZ_4K
224 
225 #define DEFAULT_SCLK_CSIS_FREQ			166000000UL
226 
227 /* MIPI CSI-2 Data Types */
228 #define MIPI_CSI2_DATA_TYPE_YUV420_8		0x18
229 #define MIPI_CSI2_DATA_TYPE_YUV420_10		0x19
230 #define MIPI_CSI2_DATA_TYPE_LE_YUV420_8		0x1a
231 #define MIPI_CSI2_DATA_TYPE_CS_YUV420_8		0x1c
232 #define MIPI_CSI2_DATA_TYPE_CS_YUV420_10	0x1d
233 #define MIPI_CSI2_DATA_TYPE_YUV422_8		0x1e
234 #define MIPI_CSI2_DATA_TYPE_YUV422_10		0x1f
235 #define MIPI_CSI2_DATA_TYPE_RGB565		0x22
236 #define MIPI_CSI2_DATA_TYPE_RGB666		0x23
237 #define MIPI_CSI2_DATA_TYPE_RGB888		0x24
238 #define MIPI_CSI2_DATA_TYPE_RAW6		0x28
239 #define MIPI_CSI2_DATA_TYPE_RAW7		0x29
240 #define MIPI_CSI2_DATA_TYPE_RAW8		0x2a
241 #define MIPI_CSI2_DATA_TYPE_RAW10		0x2b
242 #define MIPI_CSI2_DATA_TYPE_RAW12		0x2c
243 #define MIPI_CSI2_DATA_TYPE_RAW14		0x2d
244 #define MIPI_CSI2_DATA_TYPE_USER(x)		(0x30 + (x))
245 
246 enum {
247 	ST_POWERED	= 1,
248 	ST_STREAMING	= 2,
249 	ST_SUSPENDED	= 4,
250 };
251 
252 struct mipi_csis_event {
253 	bool debug;
254 	u32 mask;
255 	const char * const name;
256 	unsigned int counter;
257 };
258 
259 static const struct mipi_csis_event mipi_csis_events[] = {
260 	/* Errors */
261 	{ false, MIPI_CSIS_INT_SRC_ERR_SOT_HS,		"SOT Error" },
262 	{ false, MIPI_CSIS_INT_SRC_ERR_LOST_FS,		"Lost Frame Start Error" },
263 	{ false, MIPI_CSIS_INT_SRC_ERR_LOST_FE,		"Lost Frame End Error" },
264 	{ false, MIPI_CSIS_INT_SRC_ERR_OVER,		"FIFO Overflow Error" },
265 	{ false, MIPI_CSIS_INT_SRC_ERR_WRONG_CFG,	"Wrong Configuration Error" },
266 	{ false, MIPI_CSIS_INT_SRC_ERR_ECC,		"ECC Error" },
267 	{ false, MIPI_CSIS_INT_SRC_ERR_CRC,		"CRC Error" },
268 	{ false, MIPI_CSIS_INT_SRC_ERR_UNKNOWN,		"Unknown Error" },
269 	{ true, MIPI_CSIS_DBG_INTR_SRC_DT_NOT_SUPPORT,	"Data Type Not Supported" },
270 	{ true, MIPI_CSIS_DBG_INTR_SRC_DT_IGNORE,	"Data Type Ignored" },
271 	{ true, MIPI_CSIS_DBG_INTR_SRC_ERR_FRAME_SIZE,	"Frame Size Error" },
272 	{ true, MIPI_CSIS_DBG_INTR_SRC_TRUNCATED_FRAME,	"Truncated Frame" },
273 	{ true, MIPI_CSIS_DBG_INTR_SRC_EARLY_FE,	"Early Frame End" },
274 	{ true, MIPI_CSIS_DBG_INTR_SRC_EARLY_FS,	"Early Frame Start" },
275 	/* Non-image data receive events */
276 	{ false, MIPI_CSIS_INT_SRC_EVEN_BEFORE,		"Non-image data before even frame" },
277 	{ false, MIPI_CSIS_INT_SRC_EVEN_AFTER,		"Non-image data after even frame" },
278 	{ false, MIPI_CSIS_INT_SRC_ODD_BEFORE,		"Non-image data before odd frame" },
279 	{ false, MIPI_CSIS_INT_SRC_ODD_AFTER,		"Non-image data after odd frame" },
280 	/* Frame start/end */
281 	{ false, MIPI_CSIS_INT_SRC_FRAME_START,		"Frame Start" },
282 	{ false, MIPI_CSIS_INT_SRC_FRAME_END,		"Frame End" },
283 	{ true, MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_FALL,	"VSYNC Falling Edge" },
284 	{ true, MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_RISE,	"VSYNC Rising Edge" },
285 };
286 
287 #define MIPI_CSIS_NUM_EVENTS ARRAY_SIZE(mipi_csis_events)
288 
289 enum mipi_csis_clk {
290 	MIPI_CSIS_CLK_PCLK,
291 	MIPI_CSIS_CLK_WRAP,
292 	MIPI_CSIS_CLK_PHY,
293 	MIPI_CSIS_CLK_AXI,
294 };
295 
296 static const char * const mipi_csis_clk_id[] = {
297 	"pclk",
298 	"wrap",
299 	"phy",
300 	"axi",
301 };
302 
303 enum mipi_csis_version {
304 	MIPI_CSIS_V3_3,
305 	MIPI_CSIS_V3_6_3,
306 };
307 
308 struct mipi_csis_info {
309 	enum mipi_csis_version version;
310 	unsigned int num_clocks;
311 };
312 
313 struct csi_state {
314 	struct device *dev;
315 	void __iomem *regs;
316 	struct clk_bulk_data *clks;
317 	struct reset_control *mrst;
318 	struct regulator *mipi_phy_regulator;
319 	const struct mipi_csis_info *info;
320 
321 	struct v4l2_subdev sd;
322 	struct media_pad pads[CSIS_PADS_NUM];
323 	struct v4l2_async_notifier notifier;
324 	struct v4l2_subdev *src_sd;
325 
326 	struct v4l2_mbus_config_mipi_csi2 bus;
327 	u32 clk_frequency;
328 	u32 hs_settle;
329 	u32 clk_settle;
330 
331 	struct mutex lock;	/* Protect csis_fmt, format_mbus and state */
332 	const struct csis_pix_format *csis_fmt;
333 	struct v4l2_mbus_framefmt format_mbus[CSIS_PADS_NUM];
334 	u32 state;
335 
336 	spinlock_t slock;	/* Protect events */
337 	struct mipi_csis_event events[MIPI_CSIS_NUM_EVENTS];
338 	struct dentry *debugfs_root;
339 	struct {
340 		bool enable;
341 		u32 hs_settle;
342 		u32 clk_settle;
343 	} debug;
344 };
345 
346 /* -----------------------------------------------------------------------------
347  * Format helpers
348  */
349 
350 struct csis_pix_format {
351 	u32 code;
352 	u32 output;
353 	u32 data_type;
354 	u8 width;
355 };
356 
357 static const struct csis_pix_format mipi_csis_formats[] = {
358 	/* YUV formats. */
359 	{
360 		.code = MEDIA_BUS_FMT_UYVY8_1X16,
361 		.output = MEDIA_BUS_FMT_UYVY8_1X16,
362 		.data_type = MIPI_CSI2_DATA_TYPE_YUV422_8,
363 		.width = 16,
364 	},
365 	/* RGB formats. */
366 	{
367 		.code = MEDIA_BUS_FMT_RGB565_1X16,
368 		.output = MEDIA_BUS_FMT_RGB565_1X16,
369 		.data_type = MIPI_CSI2_DATA_TYPE_RGB565,
370 		.width = 16,
371 	}, {
372 		.code = MEDIA_BUS_FMT_BGR888_1X24,
373 		.output = MEDIA_BUS_FMT_RGB888_1X24,
374 		.data_type = MIPI_CSI2_DATA_TYPE_RGB888,
375 		.width = 24,
376 	},
377 	/* RAW (Bayer and greyscale) formats. */
378 	{
379 		.code = MEDIA_BUS_FMT_SBGGR8_1X8,
380 		.output = MEDIA_BUS_FMT_SBGGR8_1X8,
381 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
382 		.width = 8,
383 	}, {
384 		.code = MEDIA_BUS_FMT_SGBRG8_1X8,
385 		.output = MEDIA_BUS_FMT_SGBRG8_1X8,
386 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
387 		.width = 8,
388 	}, {
389 		.code = MEDIA_BUS_FMT_SGRBG8_1X8,
390 		.output = MEDIA_BUS_FMT_SGRBG8_1X8,
391 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
392 		.width = 8,
393 	}, {
394 		.code = MEDIA_BUS_FMT_SRGGB8_1X8,
395 		.output = MEDIA_BUS_FMT_SRGGB8_1X8,
396 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
397 		.width = 8,
398 	}, {
399 		.code = MEDIA_BUS_FMT_Y8_1X8,
400 		.output = MEDIA_BUS_FMT_Y8_1X8,
401 		.data_type = MIPI_CSI2_DATA_TYPE_RAW8,
402 		.width = 8,
403 	}, {
404 		.code = MEDIA_BUS_FMT_SBGGR10_1X10,
405 		.output = MEDIA_BUS_FMT_SBGGR10_1X10,
406 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
407 		.width = 10,
408 	}, {
409 		.code = MEDIA_BUS_FMT_SGBRG10_1X10,
410 		.output = MEDIA_BUS_FMT_SGBRG10_1X10,
411 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
412 		.width = 10,
413 	}, {
414 		.code = MEDIA_BUS_FMT_SGRBG10_1X10,
415 		.output = MEDIA_BUS_FMT_SGRBG10_1X10,
416 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
417 		.width = 10,
418 	}, {
419 		.code = MEDIA_BUS_FMT_SRGGB10_1X10,
420 		.output = MEDIA_BUS_FMT_SRGGB10_1X10,
421 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
422 		.width = 10,
423 	}, {
424 		.code = MEDIA_BUS_FMT_Y10_1X10,
425 		.output = MEDIA_BUS_FMT_Y10_1X10,
426 		.data_type = MIPI_CSI2_DATA_TYPE_RAW10,
427 		.width = 10,
428 	}, {
429 		.code = MEDIA_BUS_FMT_SBGGR12_1X12,
430 		.output = MEDIA_BUS_FMT_SBGGR12_1X12,
431 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
432 		.width = 12,
433 	}, {
434 		.code = MEDIA_BUS_FMT_SGBRG12_1X12,
435 		.output = MEDIA_BUS_FMT_SGBRG12_1X12,
436 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
437 		.width = 12,
438 	}, {
439 		.code = MEDIA_BUS_FMT_SGRBG12_1X12,
440 		.output = MEDIA_BUS_FMT_SGRBG12_1X12,
441 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
442 		.width = 12,
443 	}, {
444 		.code = MEDIA_BUS_FMT_SRGGB12_1X12,
445 		.output = MEDIA_BUS_FMT_SRGGB12_1X12,
446 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
447 		.width = 12,
448 	}, {
449 		.code = MEDIA_BUS_FMT_Y12_1X12,
450 		.output = MEDIA_BUS_FMT_Y12_1X12,
451 		.data_type = MIPI_CSI2_DATA_TYPE_RAW12,
452 		.width = 12,
453 	}, {
454 		.code = MEDIA_BUS_FMT_SBGGR14_1X14,
455 		.output = MEDIA_BUS_FMT_SBGGR14_1X14,
456 		.data_type = MIPI_CSI2_DATA_TYPE_RAW14,
457 		.width = 14,
458 	}, {
459 		.code = MEDIA_BUS_FMT_SGBRG14_1X14,
460 		.output = MEDIA_BUS_FMT_SGBRG14_1X14,
461 		.data_type = MIPI_CSI2_DATA_TYPE_RAW14,
462 		.width = 14,
463 	}, {
464 		.code = MEDIA_BUS_FMT_SGRBG14_1X14,
465 		.output = MEDIA_BUS_FMT_SGRBG14_1X14,
466 		.data_type = MIPI_CSI2_DATA_TYPE_RAW14,
467 		.width = 14,
468 	}, {
469 		.code = MEDIA_BUS_FMT_SRGGB14_1X14,
470 		.output = MEDIA_BUS_FMT_SRGGB14_1X14,
471 		.data_type = MIPI_CSI2_DATA_TYPE_RAW14,
472 		.width = 14,
473 	}
474 };
475 
476 static const struct csis_pix_format *find_csis_format(u32 code)
477 {
478 	unsigned int i;
479 
480 	for (i = 0; i < ARRAY_SIZE(mipi_csis_formats); i++)
481 		if (code == mipi_csis_formats[i].code)
482 			return &mipi_csis_formats[i];
483 	return NULL;
484 }
485 
486 /* -----------------------------------------------------------------------------
487  * Hardware configuration
488  */
489 
490 static inline u32 mipi_csis_read(struct csi_state *state, u32 reg)
491 {
492 	return readl(state->regs + reg);
493 }
494 
495 static inline void mipi_csis_write(struct csi_state *state, u32 reg, u32 val)
496 {
497 	writel(val, state->regs + reg);
498 }
499 
500 static void mipi_csis_enable_interrupts(struct csi_state *state, bool on)
501 {
502 	mipi_csis_write(state, MIPI_CSIS_INT_MSK, on ? 0xffffffff : 0);
503 	mipi_csis_write(state, MIPI_CSIS_DBG_INTR_MSK, on ? 0xffffffff : 0);
504 }
505 
506 static void mipi_csis_sw_reset(struct csi_state *state)
507 {
508 	u32 val = mipi_csis_read(state, MIPI_CSIS_CMN_CTRL);
509 
510 	mipi_csis_write(state, MIPI_CSIS_CMN_CTRL,
511 			val | MIPI_CSIS_CMN_CTRL_RESET);
512 	usleep_range(10, 20);
513 }
514 
515 static void mipi_csis_system_enable(struct csi_state *state, int on)
516 {
517 	u32 val, mask;
518 
519 	val = mipi_csis_read(state, MIPI_CSIS_CMN_CTRL);
520 	if (on)
521 		val |= MIPI_CSIS_CMN_CTRL_ENABLE;
522 	else
523 		val &= ~MIPI_CSIS_CMN_CTRL_ENABLE;
524 	mipi_csis_write(state, MIPI_CSIS_CMN_CTRL, val);
525 
526 	val = mipi_csis_read(state, MIPI_CSIS_DPHY_CMN_CTRL);
527 	val &= ~MIPI_CSIS_DPHY_CMN_CTRL_ENABLE;
528 	if (on) {
529 		mask = (1 << (state->bus.num_data_lanes + 1)) - 1;
530 		val |= (mask & MIPI_CSIS_DPHY_CMN_CTRL_ENABLE);
531 	}
532 	mipi_csis_write(state, MIPI_CSIS_DPHY_CMN_CTRL, val);
533 }
534 
535 /* Called with the state.lock mutex held */
536 static void __mipi_csis_set_format(struct csi_state *state)
537 {
538 	struct v4l2_mbus_framefmt *mf = &state->format_mbus[CSIS_PAD_SINK];
539 	u32 val;
540 
541 	/* Color format */
542 	val = mipi_csis_read(state, MIPI_CSIS_ISP_CONFIG_CH(0));
543 	val &= ~(MIPI_CSIS_ISPCFG_ALIGN_32BIT | MIPI_CSIS_ISPCFG_FMT_MASK
544 		| MIPI_CSIS_ISPCFG_PIXEL_MASK);
545 
546 	/*
547 	 * YUV 4:2:2 can be transferred with 8 or 16 bits per clock sample
548 	 * (referred to in the documentation as single and dual pixel modes
549 	 * respectively, although the 8-bit mode transfers half a pixel per
550 	 * clock sample and the 16-bit mode one pixel). While both mode work
551 	 * when the CSIS is connected to a receiver that supports either option,
552 	 * single pixel mode requires clock rates twice as high. As all SoCs
553 	 * that integrate the CSIS can operate in 16-bit bit mode, and some do
554 	 * not support 8-bit mode (this is the case of the i.MX8MP), use dual
555 	 * pixel mode unconditionally.
556 	 *
557 	 * TODO: Verify which other formats require DUAL (or QUAD) modes.
558 	 */
559 	if (state->csis_fmt->data_type == MIPI_CSI2_DATA_TYPE_YUV422_8)
560 		val |= MIPI_CSIS_ISPCFG_PIXEL_MODE_DUAL;
561 
562 	val |= MIPI_CSIS_ISPCFG_FMT(state->csis_fmt->data_type);
563 	mipi_csis_write(state, MIPI_CSIS_ISP_CONFIG_CH(0), val);
564 
565 	/* Pixel resolution */
566 	val = mf->width | (mf->height << 16);
567 	mipi_csis_write(state, MIPI_CSIS_ISP_RESOL_CH(0), val);
568 }
569 
570 static int mipi_csis_calculate_params(struct csi_state *state)
571 {
572 	s64 link_freq;
573 	u32 lane_rate;
574 
575 	/* Calculate the line rate from the pixel rate. */
576 	link_freq = v4l2_get_link_freq(state->src_sd->ctrl_handler,
577 				       state->csis_fmt->width,
578 				       state->bus.num_data_lanes * 2);
579 	if (link_freq < 0) {
580 		dev_err(state->dev, "Unable to obtain link frequency: %d\n",
581 			(int)link_freq);
582 		return link_freq;
583 	}
584 
585 	lane_rate = link_freq * 2;
586 
587 	if (lane_rate < 80000000 || lane_rate > 1500000000) {
588 		dev_dbg(state->dev, "Out-of-bound lane rate %u\n", lane_rate);
589 		return -EINVAL;
590 	}
591 
592 	/*
593 	 * The HSSETTLE counter value is document in a table, but can also
594 	 * easily be calculated. Hardcode the CLKSETTLE value to 0 for now
595 	 * (which is documented as corresponding to CSI-2 v0.87 to v1.00) until
596 	 * we figure out how to compute it correctly.
597 	 */
598 	state->hs_settle = (lane_rate - 5000000) / 45000000;
599 	state->clk_settle = 0;
600 
601 	dev_dbg(state->dev, "lane rate %u, Tclk_settle %u, Ths_settle %u\n",
602 		lane_rate, state->clk_settle, state->hs_settle);
603 
604 	if (state->debug.hs_settle < 0xff) {
605 		dev_dbg(state->dev, "overriding Ths_settle with %u\n",
606 			state->debug.hs_settle);
607 		state->hs_settle = state->debug.hs_settle;
608 	}
609 
610 	if (state->debug.clk_settle < 4) {
611 		dev_dbg(state->dev, "overriding Tclk_settle with %u\n",
612 			state->debug.clk_settle);
613 		state->clk_settle = state->debug.clk_settle;
614 	}
615 
616 	return 0;
617 }
618 
619 static void mipi_csis_set_params(struct csi_state *state)
620 {
621 	int lanes = state->bus.num_data_lanes;
622 	u32 val;
623 
624 	val = mipi_csis_read(state, MIPI_CSIS_CMN_CTRL);
625 	val &= ~MIPI_CSIS_CMN_CTRL_LANE_NR_MASK;
626 	val |= (lanes - 1) << MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET;
627 	if (state->info->version == MIPI_CSIS_V3_3)
628 		val |= MIPI_CSIS_CMN_CTRL_INTER_MODE;
629 	mipi_csis_write(state, MIPI_CSIS_CMN_CTRL, val);
630 
631 	__mipi_csis_set_format(state);
632 
633 	mipi_csis_write(state, MIPI_CSIS_DPHY_CMN_CTRL,
634 			MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(state->hs_settle) |
635 			MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(state->clk_settle));
636 
637 	val = (0 << MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET)
638 	    | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET)
639 	    | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET);
640 	mipi_csis_write(state, MIPI_CSIS_ISP_SYNC_CH(0), val);
641 
642 	val = mipi_csis_read(state, MIPI_CSIS_CLK_CTRL);
643 	val |= MIPI_CSIS_CLK_CTRL_WCLK_SRC;
644 	val |= MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(15);
645 	val &= ~MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK;
646 	mipi_csis_write(state, MIPI_CSIS_CLK_CTRL, val);
647 
648 	mipi_csis_write(state, MIPI_CSIS_DPHY_BCTRL_L,
649 			MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV |
650 			MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ |
651 			MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V |
652 			MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV |
653 			MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV |
654 			MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV |
655 			MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(20000000));
656 	mipi_csis_write(state, MIPI_CSIS_DPHY_BCTRL_H, 0);
657 
658 	/* Update the shadow register. */
659 	val = mipi_csis_read(state, MIPI_CSIS_CMN_CTRL);
660 	mipi_csis_write(state, MIPI_CSIS_CMN_CTRL,
661 			val | MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW |
662 			MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL);
663 }
664 
665 static int mipi_csis_clk_enable(struct csi_state *state)
666 {
667 	return clk_bulk_prepare_enable(state->info->num_clocks, state->clks);
668 }
669 
670 static void mipi_csis_clk_disable(struct csi_state *state)
671 {
672 	clk_bulk_disable_unprepare(state->info->num_clocks, state->clks);
673 }
674 
675 static int mipi_csis_clk_get(struct csi_state *state)
676 {
677 	unsigned int i;
678 	int ret;
679 
680 	state->clks = devm_kcalloc(state->dev, state->info->num_clocks,
681 				   sizeof(*state->clks), GFP_KERNEL);
682 
683 	if (!state->clks)
684 		return -ENOMEM;
685 
686 	for (i = 0; i < state->info->num_clocks; i++)
687 		state->clks[i].id = mipi_csis_clk_id[i];
688 
689 	ret = devm_clk_bulk_get(state->dev, state->info->num_clocks,
690 				state->clks);
691 	if (ret < 0)
692 		return ret;
693 
694 	/* Set clock rate */
695 	ret = clk_set_rate(state->clks[MIPI_CSIS_CLK_WRAP].clk,
696 			   state->clk_frequency);
697 	if (ret < 0)
698 		dev_err(state->dev, "set rate=%d failed: %d\n",
699 			state->clk_frequency, ret);
700 
701 	return ret;
702 }
703 
704 static void mipi_csis_start_stream(struct csi_state *state)
705 {
706 	mipi_csis_sw_reset(state);
707 	mipi_csis_set_params(state);
708 	mipi_csis_system_enable(state, true);
709 	mipi_csis_enable_interrupts(state, true);
710 }
711 
712 static void mipi_csis_stop_stream(struct csi_state *state)
713 {
714 	mipi_csis_enable_interrupts(state, false);
715 	mipi_csis_system_enable(state, false);
716 }
717 
718 static irqreturn_t mipi_csis_irq_handler(int irq, void *dev_id)
719 {
720 	struct csi_state *state = dev_id;
721 	unsigned long flags;
722 	unsigned int i;
723 	u32 status;
724 	u32 dbg_status;
725 
726 	status = mipi_csis_read(state, MIPI_CSIS_INT_SRC);
727 	dbg_status = mipi_csis_read(state, MIPI_CSIS_DBG_INTR_SRC);
728 
729 	spin_lock_irqsave(&state->slock, flags);
730 
731 	/* Update the event/error counters */
732 	if ((status & MIPI_CSIS_INT_SRC_ERRORS) || state->debug.enable) {
733 		for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++) {
734 			struct mipi_csis_event *event = &state->events[i];
735 
736 			if ((!event->debug && (status & event->mask)) ||
737 			    (event->debug && (dbg_status & event->mask)))
738 				event->counter++;
739 		}
740 	}
741 	spin_unlock_irqrestore(&state->slock, flags);
742 
743 	mipi_csis_write(state, MIPI_CSIS_INT_SRC, status);
744 	mipi_csis_write(state, MIPI_CSIS_DBG_INTR_SRC, dbg_status);
745 
746 	return IRQ_HANDLED;
747 }
748 
749 /* -----------------------------------------------------------------------------
750  * PHY regulator and reset
751  */
752 
753 static int mipi_csis_phy_enable(struct csi_state *state)
754 {
755 	if (state->info->version != MIPI_CSIS_V3_3)
756 		return 0;
757 
758 	return regulator_enable(state->mipi_phy_regulator);
759 }
760 
761 static int mipi_csis_phy_disable(struct csi_state *state)
762 {
763 	if (state->info->version != MIPI_CSIS_V3_3)
764 		return 0;
765 
766 	return regulator_disable(state->mipi_phy_regulator);
767 }
768 
769 static void mipi_csis_phy_reset(struct csi_state *state)
770 {
771 	if (state->info->version != MIPI_CSIS_V3_3)
772 		return;
773 
774 	reset_control_assert(state->mrst);
775 	msleep(20);
776 	reset_control_deassert(state->mrst);
777 }
778 
779 static int mipi_csis_phy_init(struct csi_state *state)
780 {
781 	if (state->info->version != MIPI_CSIS_V3_3)
782 		return 0;
783 
784 	/* Get MIPI PHY reset and regulator. */
785 	state->mrst = devm_reset_control_get_exclusive(state->dev, NULL);
786 	if (IS_ERR(state->mrst))
787 		return PTR_ERR(state->mrst);
788 
789 	state->mipi_phy_regulator = devm_regulator_get(state->dev, "phy");
790 	if (IS_ERR(state->mipi_phy_regulator))
791 		return PTR_ERR(state->mipi_phy_regulator);
792 
793 	return regulator_set_voltage(state->mipi_phy_regulator, 1000000,
794 				     1000000);
795 }
796 
797 /* -----------------------------------------------------------------------------
798  * Debug
799  */
800 
801 static void mipi_csis_clear_counters(struct csi_state *state)
802 {
803 	unsigned long flags;
804 	unsigned int i;
805 
806 	spin_lock_irqsave(&state->slock, flags);
807 	for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++)
808 		state->events[i].counter = 0;
809 	spin_unlock_irqrestore(&state->slock, flags);
810 }
811 
812 static void mipi_csis_log_counters(struct csi_state *state, bool non_errors)
813 {
814 	unsigned int num_events = non_errors ? MIPI_CSIS_NUM_EVENTS
815 				: MIPI_CSIS_NUM_EVENTS - 8;
816 	unsigned long flags;
817 	unsigned int i;
818 
819 	spin_lock_irqsave(&state->slock, flags);
820 
821 	for (i = 0; i < num_events; ++i) {
822 		if (state->events[i].counter > 0 || state->debug.enable)
823 			dev_info(state->dev, "%s events: %d\n",
824 				 state->events[i].name,
825 				 state->events[i].counter);
826 	}
827 	spin_unlock_irqrestore(&state->slock, flags);
828 }
829 
830 static int mipi_csis_dump_regs(struct csi_state *state)
831 {
832 	static const struct {
833 		u32 offset;
834 		const char * const name;
835 	} registers[] = {
836 		{ MIPI_CSIS_CMN_CTRL, "CMN_CTRL" },
837 		{ MIPI_CSIS_CLK_CTRL, "CLK_CTRL" },
838 		{ MIPI_CSIS_INT_MSK, "INT_MSK" },
839 		{ MIPI_CSIS_DPHY_STATUS, "DPHY_STATUS" },
840 		{ MIPI_CSIS_DPHY_CMN_CTRL, "DPHY_CMN_CTRL" },
841 		{ MIPI_CSIS_DPHY_SCTRL_L, "DPHY_SCTRL_L" },
842 		{ MIPI_CSIS_DPHY_SCTRL_H, "DPHY_SCTRL_H" },
843 		{ MIPI_CSIS_ISP_CONFIG_CH(0), "ISP_CONFIG_CH0" },
844 		{ MIPI_CSIS_ISP_RESOL_CH(0), "ISP_RESOL_CH0" },
845 		{ MIPI_CSIS_SDW_CONFIG_CH(0), "SDW_CONFIG_CH0" },
846 		{ MIPI_CSIS_SDW_RESOL_CH(0), "SDW_RESOL_CH0" },
847 		{ MIPI_CSIS_DBG_CTRL, "DBG_CTRL" },
848 		{ MIPI_CSIS_FRAME_COUNTER_CH(0), "FRAME_COUNTER_CH0" },
849 	};
850 
851 	unsigned int i;
852 	u32 cfg;
853 
854 	dev_info(state->dev, "--- REGISTERS ---\n");
855 
856 	for (i = 0; i < ARRAY_SIZE(registers); i++) {
857 		cfg = mipi_csis_read(state, registers[i].offset);
858 		dev_info(state->dev, "%14s: 0x%08x\n", registers[i].name, cfg);
859 	}
860 
861 	return 0;
862 }
863 
864 static int mipi_csis_dump_regs_show(struct seq_file *m, void *private)
865 {
866 	struct csi_state *state = m->private;
867 
868 	return mipi_csis_dump_regs(state);
869 }
870 DEFINE_SHOW_ATTRIBUTE(mipi_csis_dump_regs);
871 
872 static void mipi_csis_debugfs_init(struct csi_state *state)
873 {
874 	state->debug.hs_settle = UINT_MAX;
875 	state->debug.clk_settle = UINT_MAX;
876 
877 	state->debugfs_root = debugfs_create_dir(dev_name(state->dev), NULL);
878 
879 	debugfs_create_bool("debug_enable", 0600, state->debugfs_root,
880 			    &state->debug.enable);
881 	debugfs_create_file("dump_regs", 0600, state->debugfs_root, state,
882 			    &mipi_csis_dump_regs_fops);
883 	debugfs_create_u32("tclk_settle", 0600, state->debugfs_root,
884 			   &state->debug.clk_settle);
885 	debugfs_create_u32("ths_settle", 0600, state->debugfs_root,
886 			   &state->debug.hs_settle);
887 }
888 
889 static void mipi_csis_debugfs_exit(struct csi_state *state)
890 {
891 	debugfs_remove_recursive(state->debugfs_root);
892 }
893 
894 /* -----------------------------------------------------------------------------
895  * V4L2 subdev operations
896  */
897 
898 static struct csi_state *mipi_sd_to_csis_state(struct v4l2_subdev *sdev)
899 {
900 	return container_of(sdev, struct csi_state, sd);
901 }
902 
903 static int mipi_csis_s_stream(struct v4l2_subdev *sd, int enable)
904 {
905 	struct csi_state *state = mipi_sd_to_csis_state(sd);
906 	int ret;
907 
908 	if (enable) {
909 		ret = mipi_csis_calculate_params(state);
910 		if (ret < 0)
911 			return ret;
912 
913 		mipi_csis_clear_counters(state);
914 
915 		ret = pm_runtime_resume_and_get(state->dev);
916 		if (ret < 0)
917 			return ret;
918 
919 		ret = v4l2_subdev_call(state->src_sd, core, s_power, 1);
920 		if (ret < 0 && ret != -ENOIOCTLCMD)
921 			goto done;
922 	}
923 
924 	mutex_lock(&state->lock);
925 
926 	if (enable) {
927 		if (state->state & ST_SUSPENDED) {
928 			ret = -EBUSY;
929 			goto unlock;
930 		}
931 
932 		mipi_csis_start_stream(state);
933 		ret = v4l2_subdev_call(state->src_sd, video, s_stream, 1);
934 		if (ret < 0)
935 			goto unlock;
936 
937 		mipi_csis_log_counters(state, true);
938 
939 		state->state |= ST_STREAMING;
940 	} else {
941 		v4l2_subdev_call(state->src_sd, video, s_stream, 0);
942 		ret = v4l2_subdev_call(state->src_sd, core, s_power, 0);
943 		if (ret == -ENOIOCTLCMD)
944 			ret = 0;
945 		mipi_csis_stop_stream(state);
946 		state->state &= ~ST_STREAMING;
947 		if (state->debug.enable)
948 			mipi_csis_log_counters(state, true);
949 	}
950 
951 unlock:
952 	mutex_unlock(&state->lock);
953 
954 done:
955 	if (!enable || ret < 0)
956 		pm_runtime_put(state->dev);
957 
958 	return ret;
959 }
960 
961 static struct v4l2_mbus_framefmt *
962 mipi_csis_get_format(struct csi_state *state,
963 		     struct v4l2_subdev_state *sd_state,
964 		     enum v4l2_subdev_format_whence which,
965 		     unsigned int pad)
966 {
967 	if (which == V4L2_SUBDEV_FORMAT_TRY)
968 		return v4l2_subdev_get_try_format(&state->sd, sd_state, pad);
969 
970 	return &state->format_mbus[pad];
971 }
972 
973 static int mipi_csis_init_cfg(struct v4l2_subdev *sd,
974 			      struct v4l2_subdev_state *sd_state)
975 {
976 	struct csi_state *state = mipi_sd_to_csis_state(sd);
977 	struct v4l2_mbus_framefmt *fmt_sink;
978 	struct v4l2_mbus_framefmt *fmt_source;
979 	enum v4l2_subdev_format_whence which;
980 
981 	which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
982 	fmt_sink = mipi_csis_get_format(state, sd_state, which, CSIS_PAD_SINK);
983 
984 	fmt_sink->code = MEDIA_BUS_FMT_UYVY8_1X16;
985 	fmt_sink->width = MIPI_CSIS_DEF_PIX_WIDTH;
986 	fmt_sink->height = MIPI_CSIS_DEF_PIX_HEIGHT;
987 	fmt_sink->field = V4L2_FIELD_NONE;
988 
989 	fmt_sink->colorspace = V4L2_COLORSPACE_SMPTE170M;
990 	fmt_sink->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt_sink->colorspace);
991 	fmt_sink->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt_sink->colorspace);
992 	fmt_sink->quantization =
993 		V4L2_MAP_QUANTIZATION_DEFAULT(false, fmt_sink->colorspace,
994 					      fmt_sink->ycbcr_enc);
995 
996 	/*
997 	 * When called from mipi_csis_subdev_init() to initialize the active
998 	 * configuration, cfg is NULL, which indicates there's no source pad
999 	 * configuration to set.
1000 	 */
1001 	if (!sd_state)
1002 		return 0;
1003 
1004 	fmt_source = mipi_csis_get_format(state, sd_state, which,
1005 					  CSIS_PAD_SOURCE);
1006 	*fmt_source = *fmt_sink;
1007 
1008 	return 0;
1009 }
1010 
1011 static int mipi_csis_get_fmt(struct v4l2_subdev *sd,
1012 			     struct v4l2_subdev_state *sd_state,
1013 			     struct v4l2_subdev_format *sdformat)
1014 {
1015 	struct csi_state *state = mipi_sd_to_csis_state(sd);
1016 	struct v4l2_mbus_framefmt *fmt;
1017 
1018 	fmt = mipi_csis_get_format(state, sd_state, sdformat->which,
1019 				   sdformat->pad);
1020 
1021 	mutex_lock(&state->lock);
1022 	sdformat->format = *fmt;
1023 	mutex_unlock(&state->lock);
1024 
1025 	return 0;
1026 }
1027 
1028 static int mipi_csis_enum_mbus_code(struct v4l2_subdev *sd,
1029 				    struct v4l2_subdev_state *sd_state,
1030 				    struct v4l2_subdev_mbus_code_enum *code)
1031 {
1032 	struct csi_state *state = mipi_sd_to_csis_state(sd);
1033 
1034 	/*
1035 	 * The CSIS can't transcode in any way, the source format is identical
1036 	 * to the sink format.
1037 	 */
1038 	if (code->pad == CSIS_PAD_SOURCE) {
1039 		struct v4l2_mbus_framefmt *fmt;
1040 
1041 		if (code->index > 0)
1042 			return -EINVAL;
1043 
1044 		fmt = mipi_csis_get_format(state, sd_state, code->which,
1045 					   code->pad);
1046 		code->code = fmt->code;
1047 		return 0;
1048 	}
1049 
1050 	if (code->pad != CSIS_PAD_SINK)
1051 		return -EINVAL;
1052 
1053 	if (code->index >= ARRAY_SIZE(mipi_csis_formats))
1054 		return -EINVAL;
1055 
1056 	code->code = mipi_csis_formats[code->index].code;
1057 
1058 	return 0;
1059 }
1060 
1061 static int mipi_csis_set_fmt(struct v4l2_subdev *sd,
1062 			     struct v4l2_subdev_state *sd_state,
1063 			     struct v4l2_subdev_format *sdformat)
1064 {
1065 	struct csi_state *state = mipi_sd_to_csis_state(sd);
1066 	struct csis_pix_format const *csis_fmt;
1067 	struct v4l2_mbus_framefmt *fmt;
1068 	unsigned int align;
1069 
1070 	/*
1071 	 * The CSIS can't transcode in any way, the source format can't be
1072 	 * modified.
1073 	 */
1074 	if (sdformat->pad == CSIS_PAD_SOURCE)
1075 		return mipi_csis_get_fmt(sd, sd_state, sdformat);
1076 
1077 	if (sdformat->pad != CSIS_PAD_SINK)
1078 		return -EINVAL;
1079 
1080 	/*
1081 	 * Validate the media bus code and clamp and align the size.
1082 	 *
1083 	 * The total number of bits per line must be a multiple of 8. We thus
1084 	 * need to align the width for formats that are not multiples of 8
1085 	 * bits.
1086 	 */
1087 	csis_fmt = find_csis_format(sdformat->format.code);
1088 	if (!csis_fmt)
1089 		csis_fmt = &mipi_csis_formats[0];
1090 
1091 	switch (csis_fmt->width % 8) {
1092 	case 0:
1093 		align = 0;
1094 		break;
1095 	case 4:
1096 		align = 1;
1097 		break;
1098 	case 2:
1099 	case 6:
1100 		align = 2;
1101 		break;
1102 	default:
1103 		/* 1, 3, 5, 7 */
1104 		align = 3;
1105 		break;
1106 	}
1107 
1108 	v4l_bound_align_image(&sdformat->format.width, 1,
1109 			      CSIS_MAX_PIX_WIDTH, align,
1110 			      &sdformat->format.height, 1,
1111 			      CSIS_MAX_PIX_HEIGHT, 0, 0);
1112 
1113 	fmt = mipi_csis_get_format(state, sd_state, sdformat->which,
1114 				   sdformat->pad);
1115 
1116 	mutex_lock(&state->lock);
1117 
1118 	fmt->code = csis_fmt->code;
1119 	fmt->width = sdformat->format.width;
1120 	fmt->height = sdformat->format.height;
1121 	fmt->colorspace = sdformat->format.colorspace;
1122 	fmt->quantization = sdformat->format.quantization;
1123 	fmt->xfer_func = sdformat->format.xfer_func;
1124 	fmt->ycbcr_enc = sdformat->format.ycbcr_enc;
1125 
1126 	sdformat->format = *fmt;
1127 
1128 	/* Propagate the format from sink to source. */
1129 	fmt = mipi_csis_get_format(state, sd_state, sdformat->which,
1130 				   CSIS_PAD_SOURCE);
1131 	*fmt = sdformat->format;
1132 
1133 	/* The format on the source pad might change due to unpacking. */
1134 	fmt->code = csis_fmt->output;
1135 
1136 	/* Store the CSIS format descriptor for active formats. */
1137 	if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1138 		state->csis_fmt = csis_fmt;
1139 
1140 	mutex_unlock(&state->lock);
1141 
1142 	return 0;
1143 }
1144 
1145 static int mipi_csis_log_status(struct v4l2_subdev *sd)
1146 {
1147 	struct csi_state *state = mipi_sd_to_csis_state(sd);
1148 
1149 	mutex_lock(&state->lock);
1150 	mipi_csis_log_counters(state, true);
1151 	if (state->debug.enable && (state->state & ST_POWERED))
1152 		mipi_csis_dump_regs(state);
1153 	mutex_unlock(&state->lock);
1154 
1155 	return 0;
1156 }
1157 
1158 static const struct v4l2_subdev_core_ops mipi_csis_core_ops = {
1159 	.log_status	= mipi_csis_log_status,
1160 };
1161 
1162 static const struct v4l2_subdev_video_ops mipi_csis_video_ops = {
1163 	.s_stream	= mipi_csis_s_stream,
1164 };
1165 
1166 static const struct v4l2_subdev_pad_ops mipi_csis_pad_ops = {
1167 	.init_cfg		= mipi_csis_init_cfg,
1168 	.enum_mbus_code		= mipi_csis_enum_mbus_code,
1169 	.get_fmt		= mipi_csis_get_fmt,
1170 	.set_fmt		= mipi_csis_set_fmt,
1171 };
1172 
1173 static const struct v4l2_subdev_ops mipi_csis_subdev_ops = {
1174 	.core	= &mipi_csis_core_ops,
1175 	.video	= &mipi_csis_video_ops,
1176 	.pad	= &mipi_csis_pad_ops,
1177 };
1178 
1179 /* -----------------------------------------------------------------------------
1180  * Media entity operations
1181  */
1182 
1183 static int mipi_csis_link_setup(struct media_entity *entity,
1184 				const struct media_pad *local_pad,
1185 				const struct media_pad *remote_pad, u32 flags)
1186 {
1187 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1188 	struct csi_state *state = mipi_sd_to_csis_state(sd);
1189 	struct v4l2_subdev *remote_sd;
1190 
1191 	dev_dbg(state->dev, "link setup %s -> %s", remote_pad->entity->name,
1192 		local_pad->entity->name);
1193 
1194 	/* We only care about the link to the source. */
1195 	if (!(local_pad->flags & MEDIA_PAD_FL_SINK))
1196 		return 0;
1197 
1198 	remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity);
1199 
1200 	if (flags & MEDIA_LNK_FL_ENABLED) {
1201 		if (state->src_sd)
1202 			return -EBUSY;
1203 
1204 		state->src_sd = remote_sd;
1205 	} else {
1206 		state->src_sd = NULL;
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 static const struct media_entity_operations mipi_csis_entity_ops = {
1213 	.link_setup	= mipi_csis_link_setup,
1214 	.link_validate	= v4l2_subdev_link_validate,
1215 	.get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
1216 };
1217 
1218 /* -----------------------------------------------------------------------------
1219  * Async subdev notifier
1220  */
1221 
1222 static struct csi_state *
1223 mipi_notifier_to_csis_state(struct v4l2_async_notifier *n)
1224 {
1225 	return container_of(n, struct csi_state, notifier);
1226 }
1227 
1228 static int mipi_csis_notify_bound(struct v4l2_async_notifier *notifier,
1229 				  struct v4l2_subdev *sd,
1230 				  struct v4l2_async_subdev *asd)
1231 {
1232 	struct csi_state *state = mipi_notifier_to_csis_state(notifier);
1233 	struct media_pad *sink = &state->sd.entity.pads[CSIS_PAD_SINK];
1234 
1235 	return v4l2_create_fwnode_links_to_pad(sd, sink, 0);
1236 }
1237 
1238 static const struct v4l2_async_notifier_operations mipi_csis_notify_ops = {
1239 	.bound = mipi_csis_notify_bound,
1240 };
1241 
1242 static int mipi_csis_async_register(struct csi_state *state)
1243 {
1244 	struct v4l2_fwnode_endpoint vep = {
1245 		.bus_type = V4L2_MBUS_CSI2_DPHY,
1246 	};
1247 	struct v4l2_async_subdev *asd;
1248 	struct fwnode_handle *ep;
1249 	unsigned int i;
1250 	int ret;
1251 
1252 	v4l2_async_nf_init(&state->notifier);
1253 
1254 	ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(state->dev), 0, 0,
1255 					     FWNODE_GRAPH_ENDPOINT_NEXT);
1256 	if (!ep)
1257 		return -ENOTCONN;
1258 
1259 	ret = v4l2_fwnode_endpoint_parse(ep, &vep);
1260 	if (ret)
1261 		goto err_parse;
1262 
1263 	for (i = 0; i < vep.bus.mipi_csi2.num_data_lanes; ++i) {
1264 		if (vep.bus.mipi_csi2.data_lanes[i] != i + 1) {
1265 			dev_err(state->dev,
1266 				"data lanes reordering is not supported");
1267 			ret = -EINVAL;
1268 			goto err_parse;
1269 		}
1270 	}
1271 
1272 	state->bus = vep.bus.mipi_csi2;
1273 
1274 	dev_dbg(state->dev, "data lanes: %d\n", state->bus.num_data_lanes);
1275 	dev_dbg(state->dev, "flags: 0x%08x\n", state->bus.flags);
1276 
1277 	asd = v4l2_async_nf_add_fwnode_remote(&state->notifier, ep,
1278 					      struct v4l2_async_subdev);
1279 	if (IS_ERR(asd)) {
1280 		ret = PTR_ERR(asd);
1281 		goto err_parse;
1282 	}
1283 
1284 	fwnode_handle_put(ep);
1285 
1286 	state->notifier.ops = &mipi_csis_notify_ops;
1287 
1288 	ret = v4l2_async_subdev_nf_register(&state->sd, &state->notifier);
1289 	if (ret)
1290 		return ret;
1291 
1292 	return v4l2_async_register_subdev(&state->sd);
1293 
1294 err_parse:
1295 	fwnode_handle_put(ep);
1296 
1297 	return ret;
1298 }
1299 
1300 /* -----------------------------------------------------------------------------
1301  * Suspend/resume
1302  */
1303 
1304 static int mipi_csis_pm_suspend(struct device *dev, bool runtime)
1305 {
1306 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1307 	struct csi_state *state = mipi_sd_to_csis_state(sd);
1308 	int ret = 0;
1309 
1310 	mutex_lock(&state->lock);
1311 	if (state->state & ST_POWERED) {
1312 		mipi_csis_stop_stream(state);
1313 		ret = mipi_csis_phy_disable(state);
1314 		if (ret)
1315 			goto unlock;
1316 		mipi_csis_clk_disable(state);
1317 		state->state &= ~ST_POWERED;
1318 		if (!runtime)
1319 			state->state |= ST_SUSPENDED;
1320 	}
1321 
1322 unlock:
1323 	mutex_unlock(&state->lock);
1324 
1325 	return ret ? -EAGAIN : 0;
1326 }
1327 
1328 static int mipi_csis_pm_resume(struct device *dev, bool runtime)
1329 {
1330 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1331 	struct csi_state *state = mipi_sd_to_csis_state(sd);
1332 	int ret = 0;
1333 
1334 	mutex_lock(&state->lock);
1335 	if (!runtime && !(state->state & ST_SUSPENDED))
1336 		goto unlock;
1337 
1338 	if (!(state->state & ST_POWERED)) {
1339 		ret = mipi_csis_phy_enable(state);
1340 		if (ret)
1341 			goto unlock;
1342 
1343 		state->state |= ST_POWERED;
1344 		mipi_csis_clk_enable(state);
1345 	}
1346 	if (state->state & ST_STREAMING)
1347 		mipi_csis_start_stream(state);
1348 
1349 	state->state &= ~ST_SUSPENDED;
1350 
1351 unlock:
1352 	mutex_unlock(&state->lock);
1353 
1354 	return ret ? -EAGAIN : 0;
1355 }
1356 
1357 static int __maybe_unused mipi_csis_suspend(struct device *dev)
1358 {
1359 	return mipi_csis_pm_suspend(dev, false);
1360 }
1361 
1362 static int __maybe_unused mipi_csis_resume(struct device *dev)
1363 {
1364 	return mipi_csis_pm_resume(dev, false);
1365 }
1366 
1367 static int __maybe_unused mipi_csis_runtime_suspend(struct device *dev)
1368 {
1369 	return mipi_csis_pm_suspend(dev, true);
1370 }
1371 
1372 static int __maybe_unused mipi_csis_runtime_resume(struct device *dev)
1373 {
1374 	return mipi_csis_pm_resume(dev, true);
1375 }
1376 
1377 static const struct dev_pm_ops mipi_csis_pm_ops = {
1378 	SET_RUNTIME_PM_OPS(mipi_csis_runtime_suspend, mipi_csis_runtime_resume,
1379 			   NULL)
1380 	SET_SYSTEM_SLEEP_PM_OPS(mipi_csis_suspend, mipi_csis_resume)
1381 };
1382 
1383 /* -----------------------------------------------------------------------------
1384  * Probe/remove & platform driver
1385  */
1386 
1387 static int mipi_csis_subdev_init(struct csi_state *state)
1388 {
1389 	struct v4l2_subdev *sd = &state->sd;
1390 
1391 	v4l2_subdev_init(sd, &mipi_csis_subdev_ops);
1392 	sd->owner = THIS_MODULE;
1393 	snprintf(sd->name, sizeof(sd->name), "csis-%s",
1394 		 dev_name(state->dev));
1395 
1396 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1397 	sd->ctrl_handler = NULL;
1398 
1399 	sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
1400 	sd->entity.ops = &mipi_csis_entity_ops;
1401 
1402 	sd->dev = state->dev;
1403 
1404 	state->csis_fmt = &mipi_csis_formats[0];
1405 	mipi_csis_init_cfg(sd, NULL);
1406 
1407 	state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK
1408 					 | MEDIA_PAD_FL_MUST_CONNECT;
1409 	state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE
1410 					   | MEDIA_PAD_FL_MUST_CONNECT;
1411 	return media_entity_pads_init(&sd->entity, CSIS_PADS_NUM,
1412 				      state->pads);
1413 }
1414 
1415 static int mipi_csis_parse_dt(struct csi_state *state)
1416 {
1417 	struct device_node *node = state->dev->of_node;
1418 
1419 	if (of_property_read_u32(node, "clock-frequency",
1420 				 &state->clk_frequency))
1421 		state->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
1422 
1423 	return 0;
1424 }
1425 
1426 static int mipi_csis_probe(struct platform_device *pdev)
1427 {
1428 	struct device *dev = &pdev->dev;
1429 	struct csi_state *state;
1430 	int irq;
1431 	int ret;
1432 
1433 	state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
1434 	if (!state)
1435 		return -ENOMEM;
1436 
1437 	mutex_init(&state->lock);
1438 	spin_lock_init(&state->slock);
1439 
1440 	state->dev = dev;
1441 	state->info = of_device_get_match_data(dev);
1442 
1443 	memcpy(state->events, mipi_csis_events, sizeof(state->events));
1444 
1445 	/* Parse DT properties. */
1446 	ret = mipi_csis_parse_dt(state);
1447 	if (ret < 0) {
1448 		dev_err(dev, "Failed to parse device tree: %d\n", ret);
1449 		return ret;
1450 	}
1451 
1452 	/* Acquire resources. */
1453 	state->regs = devm_platform_ioremap_resource(pdev, 0);
1454 	if (IS_ERR(state->regs))
1455 		return PTR_ERR(state->regs);
1456 
1457 	irq = platform_get_irq(pdev, 0);
1458 	if (irq < 0)
1459 		return irq;
1460 
1461 	ret = mipi_csis_phy_init(state);
1462 	if (ret < 0)
1463 		return ret;
1464 
1465 	ret = mipi_csis_clk_get(state);
1466 	if (ret < 0)
1467 		return ret;
1468 
1469 	/* Reset PHY and enable the clocks. */
1470 	mipi_csis_phy_reset(state);
1471 
1472 	ret = mipi_csis_clk_enable(state);
1473 	if (ret < 0) {
1474 		dev_err(state->dev, "failed to enable clocks: %d\n", ret);
1475 		return ret;
1476 	}
1477 
1478 	/* Now that the hardware is initialized, request the interrupt. */
1479 	ret = devm_request_irq(dev, irq, mipi_csis_irq_handler, 0,
1480 			       dev_name(dev), state);
1481 	if (ret) {
1482 		dev_err(dev, "Interrupt request failed\n");
1483 		goto disable_clock;
1484 	}
1485 
1486 	/* Initialize and register the subdev. */
1487 	ret = mipi_csis_subdev_init(state);
1488 	if (ret < 0)
1489 		goto disable_clock;
1490 
1491 	platform_set_drvdata(pdev, &state->sd);
1492 
1493 	ret = mipi_csis_async_register(state);
1494 	if (ret < 0) {
1495 		dev_err(dev, "async register failed: %d\n", ret);
1496 		goto cleanup;
1497 	}
1498 
1499 	/* Initialize debugfs. */
1500 	mipi_csis_debugfs_init(state);
1501 
1502 	/* Enable runtime PM. */
1503 	pm_runtime_enable(dev);
1504 	if (!pm_runtime_enabled(dev)) {
1505 		ret = mipi_csis_pm_resume(dev, true);
1506 		if (ret < 0)
1507 			goto unregister_all;
1508 	}
1509 
1510 	dev_info(dev, "lanes: %d, freq: %u\n",
1511 		 state->bus.num_data_lanes, state->clk_frequency);
1512 
1513 	return 0;
1514 
1515 unregister_all:
1516 	mipi_csis_debugfs_exit(state);
1517 cleanup:
1518 	media_entity_cleanup(&state->sd.entity);
1519 	v4l2_async_nf_unregister(&state->notifier);
1520 	v4l2_async_nf_cleanup(&state->notifier);
1521 	v4l2_async_unregister_subdev(&state->sd);
1522 disable_clock:
1523 	mipi_csis_clk_disable(state);
1524 	mutex_destroy(&state->lock);
1525 
1526 	return ret;
1527 }
1528 
1529 static int mipi_csis_remove(struct platform_device *pdev)
1530 {
1531 	struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1532 	struct csi_state *state = mipi_sd_to_csis_state(sd);
1533 
1534 	mipi_csis_debugfs_exit(state);
1535 	v4l2_async_nf_unregister(&state->notifier);
1536 	v4l2_async_nf_cleanup(&state->notifier);
1537 	v4l2_async_unregister_subdev(&state->sd);
1538 
1539 	pm_runtime_disable(&pdev->dev);
1540 	mipi_csis_pm_suspend(&pdev->dev, true);
1541 	mipi_csis_clk_disable(state);
1542 	media_entity_cleanup(&state->sd.entity);
1543 	mutex_destroy(&state->lock);
1544 	pm_runtime_set_suspended(&pdev->dev);
1545 
1546 	return 0;
1547 }
1548 
1549 static const struct of_device_id mipi_csis_of_match[] = {
1550 	{
1551 		.compatible = "fsl,imx7-mipi-csi2",
1552 		.data = &(const struct mipi_csis_info){
1553 			.version = MIPI_CSIS_V3_3,
1554 			.num_clocks = 3,
1555 		},
1556 	}, {
1557 		.compatible = "fsl,imx8mm-mipi-csi2",
1558 		.data = &(const struct mipi_csis_info){
1559 			.version = MIPI_CSIS_V3_6_3,
1560 			.num_clocks = 4,
1561 		},
1562 	},
1563 	{ /* sentinel */ },
1564 };
1565 MODULE_DEVICE_TABLE(of, mipi_csis_of_match);
1566 
1567 static struct platform_driver mipi_csis_driver = {
1568 	.probe		= mipi_csis_probe,
1569 	.remove		= mipi_csis_remove,
1570 	.driver		= {
1571 		.of_match_table = mipi_csis_of_match,
1572 		.name		= CSIS_DRIVER_NAME,
1573 		.pm		= &mipi_csis_pm_ops,
1574 	},
1575 };
1576 
1577 module_platform_driver(mipi_csis_driver);
1578 
1579 MODULE_DESCRIPTION("i.MX7 & i.MX8 MIPI CSI-2 receiver driver");
1580 MODULE_LICENSE("GPL v2");
1581 MODULE_ALIAS("platform:imx-mipi-csi2");
1582