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