xref: /openbmc/linux/drivers/media/i2c/st-mipid02.c (revision 234489ac)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
4  *
5  * Copyright (C) STMicroelectronics SA 2019
6  * Authors: Mickael Guene <mickael.guene@st.com>
7  *          for STMicroelectronics.
8  *
9  *
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/module.h>
17 #include <linux/of_graph.h>
18 #include <linux/regulator/consumer.h>
19 #include <media/v4l2-async.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-subdev.h>
24 
25 #define MIPID02_CLK_LANE_WR_REG1			0x01
26 #define MIPID02_CLK_LANE_REG1				0x02
27 #define MIPID02_CLK_LANE_REG3				0x04
28 #define MIPID02_DATA_LANE0_REG1				0x05
29 #define MIPID02_DATA_LANE0_REG2				0x06
30 #define MIPID02_DATA_LANE1_REG1				0x09
31 #define MIPID02_DATA_LANE1_REG2				0x0a
32 #define MIPID02_MODE_REG1				0x14
33 #define MIPID02_MODE_REG2				0x15
34 #define MIPID02_DATA_ID_RREG				0x17
35 #define MIPID02_DATA_SELECTION_CTRL			0x19
36 #define MIPID02_PIX_WIDTH_CTRL				0x1e
37 #define MIPID02_PIX_WIDTH_CTRL_EMB			0x1f
38 
39 /* Bits definition for MIPID02_CLK_LANE_REG1 */
40 #define CLK_ENABLE					BIT(0)
41 /* Bits definition for MIPID02_CLK_LANE_REG3 */
42 #define CLK_MIPI_CSI					BIT(1)
43 /* Bits definition for MIPID02_DATA_LANE0_REG1 */
44 #define DATA_ENABLE					BIT(0)
45 /* Bits definition for MIPID02_DATA_LANEx_REG2 */
46 #define DATA_MIPI_CSI					BIT(0)
47 /* Bits definition for MIPID02_MODE_REG1 */
48 #define MODE_DATA_SWAP					BIT(2)
49 #define MODE_NO_BYPASS					BIT(6)
50 /* Bits definition for MIPID02_MODE_REG2 */
51 #define MODE_HSYNC_ACTIVE_HIGH				BIT(1)
52 #define MODE_VSYNC_ACTIVE_HIGH				BIT(2)
53 #define MODE_PCLK_SAMPLE_RISING				BIT(3)
54 /* Bits definition for MIPID02_DATA_SELECTION_CTRL */
55 #define SELECTION_MANUAL_DATA				BIT(2)
56 #define SELECTION_MANUAL_WIDTH				BIT(3)
57 
58 static const u32 mipid02_supported_fmt_codes[] = {
59 	MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
60 	MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
61 	MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
62 	MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
63 	MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SGBRG12_1X12,
64 	MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SRGGB12_1X12,
65 	MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YVYU8_1X16,
66 	MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_VYUY8_1X16,
67 	MEDIA_BUS_FMT_RGB565_1X16, MEDIA_BUS_FMT_BGR888_1X24,
68 	MEDIA_BUS_FMT_RGB565_2X8_LE, MEDIA_BUS_FMT_RGB565_2X8_BE,
69 	MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YVYU8_2X8,
70 	MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_VYUY8_2X8,
71 	MEDIA_BUS_FMT_JPEG_1X8
72 };
73 
74 /* regulator supplies */
75 static const char * const mipid02_supply_name[] = {
76 	"VDDE", /* 1.8V digital I/O supply */
77 	"VDDIN", /* 1V8 voltage regulator supply */
78 };
79 
80 #define MIPID02_NUM_SUPPLIES		ARRAY_SIZE(mipid02_supply_name)
81 
82 #define MIPID02_SINK_0			0
83 #define MIPID02_SINK_1			1
84 #define MIPID02_SOURCE			2
85 #define MIPID02_PAD_NB			3
86 
87 struct mipid02_dev {
88 	struct i2c_client *i2c_client;
89 	struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
90 	struct v4l2_subdev sd;
91 	struct media_pad pad[MIPID02_PAD_NB];
92 	struct clk *xclk;
93 	struct gpio_desc *reset_gpio;
94 	/* endpoints info */
95 	struct v4l2_fwnode_endpoint rx;
96 	u64 link_frequency;
97 	struct v4l2_fwnode_endpoint tx;
98 	/* remote source */
99 	struct v4l2_async_notifier notifier;
100 	struct v4l2_subdev *s_subdev;
101 	/* registers */
102 	struct {
103 		u8 clk_lane_reg1;
104 		u8 data_lane0_reg1;
105 		u8 data_lane1_reg1;
106 		u8 mode_reg1;
107 		u8 mode_reg2;
108 		u8 data_selection_ctrl;
109 		u8 data_id_rreg;
110 		u8 pix_width_ctrl;
111 		u8 pix_width_ctrl_emb;
112 	} r;
113 	/* lock to protect all members below */
114 	struct mutex lock;
115 	bool streaming;
116 	struct v4l2_mbus_framefmt fmt;
117 };
118 
119 static int bpp_from_code(__u32 code)
120 {
121 	switch (code) {
122 	case MEDIA_BUS_FMT_SBGGR8_1X8:
123 	case MEDIA_BUS_FMT_SGBRG8_1X8:
124 	case MEDIA_BUS_FMT_SGRBG8_1X8:
125 	case MEDIA_BUS_FMT_SRGGB8_1X8:
126 		return 8;
127 	case MEDIA_BUS_FMT_SBGGR10_1X10:
128 	case MEDIA_BUS_FMT_SGBRG10_1X10:
129 	case MEDIA_BUS_FMT_SGRBG10_1X10:
130 	case MEDIA_BUS_FMT_SRGGB10_1X10:
131 		return 10;
132 	case MEDIA_BUS_FMT_SBGGR12_1X12:
133 	case MEDIA_BUS_FMT_SGBRG12_1X12:
134 	case MEDIA_BUS_FMT_SGRBG12_1X12:
135 	case MEDIA_BUS_FMT_SRGGB12_1X12:
136 		return 12;
137 	case MEDIA_BUS_FMT_YUYV8_1X16:
138 	case MEDIA_BUS_FMT_YVYU8_1X16:
139 	case MEDIA_BUS_FMT_UYVY8_1X16:
140 	case MEDIA_BUS_FMT_VYUY8_1X16:
141 	case MEDIA_BUS_FMT_RGB565_1X16:
142 	case MEDIA_BUS_FMT_YUYV8_2X8:
143 	case MEDIA_BUS_FMT_YVYU8_2X8:
144 	case MEDIA_BUS_FMT_UYVY8_2X8:
145 	case MEDIA_BUS_FMT_VYUY8_2X8:
146 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
147 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
148 		return 16;
149 	case MEDIA_BUS_FMT_BGR888_1X24:
150 		return 24;
151 	default:
152 		return 0;
153 	}
154 }
155 
156 static u8 data_type_from_code(__u32 code)
157 {
158 	switch (code) {
159 	case MEDIA_BUS_FMT_SBGGR8_1X8:
160 	case MEDIA_BUS_FMT_SGBRG8_1X8:
161 	case MEDIA_BUS_FMT_SGRBG8_1X8:
162 	case MEDIA_BUS_FMT_SRGGB8_1X8:
163 		return 0x2a;
164 	case MEDIA_BUS_FMT_SBGGR10_1X10:
165 	case MEDIA_BUS_FMT_SGBRG10_1X10:
166 	case MEDIA_BUS_FMT_SGRBG10_1X10:
167 	case MEDIA_BUS_FMT_SRGGB10_1X10:
168 		return 0x2b;
169 	case MEDIA_BUS_FMT_SBGGR12_1X12:
170 	case MEDIA_BUS_FMT_SGBRG12_1X12:
171 	case MEDIA_BUS_FMT_SGRBG12_1X12:
172 	case MEDIA_BUS_FMT_SRGGB12_1X12:
173 		return 0x2c;
174 	case MEDIA_BUS_FMT_YUYV8_1X16:
175 	case MEDIA_BUS_FMT_YVYU8_1X16:
176 	case MEDIA_BUS_FMT_UYVY8_1X16:
177 	case MEDIA_BUS_FMT_VYUY8_1X16:
178 	case MEDIA_BUS_FMT_YUYV8_2X8:
179 	case MEDIA_BUS_FMT_YVYU8_2X8:
180 	case MEDIA_BUS_FMT_UYVY8_2X8:
181 	case MEDIA_BUS_FMT_VYUY8_2X8:
182 		return 0x1e;
183 	case MEDIA_BUS_FMT_BGR888_1X24:
184 		return 0x24;
185 	case MEDIA_BUS_FMT_RGB565_1X16:
186 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
187 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
188 		return 0x22;
189 	default:
190 		return 0;
191 	}
192 }
193 
194 static void init_format(struct v4l2_mbus_framefmt *fmt)
195 {
196 	fmt->code = MEDIA_BUS_FMT_SBGGR8_1X8;
197 	fmt->field = V4L2_FIELD_NONE;
198 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
199 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
200 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
201 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
202 	fmt->width = 640;
203 	fmt->height = 480;
204 }
205 
206 static __u32 get_fmt_code(__u32 code)
207 {
208 	unsigned int i;
209 
210 	for (i = 0; i < ARRAY_SIZE(mipid02_supported_fmt_codes); i++) {
211 		if (code == mipid02_supported_fmt_codes[i])
212 			return code;
213 	}
214 
215 	return mipid02_supported_fmt_codes[0];
216 }
217 
218 static __u32 serial_to_parallel_code(__u32 serial)
219 {
220 	if (serial == MEDIA_BUS_FMT_RGB565_1X16)
221 		return MEDIA_BUS_FMT_RGB565_2X8_LE;
222 	if (serial == MEDIA_BUS_FMT_YUYV8_1X16)
223 		return MEDIA_BUS_FMT_YUYV8_2X8;
224 	if (serial == MEDIA_BUS_FMT_YVYU8_1X16)
225 		return MEDIA_BUS_FMT_YVYU8_2X8;
226 	if (serial == MEDIA_BUS_FMT_UYVY8_1X16)
227 		return MEDIA_BUS_FMT_UYVY8_2X8;
228 	if (serial == MEDIA_BUS_FMT_VYUY8_1X16)
229 		return MEDIA_BUS_FMT_VYUY8_2X8;
230 	if (serial == MEDIA_BUS_FMT_BGR888_1X24)
231 		return MEDIA_BUS_FMT_BGR888_3X8;
232 
233 	return serial;
234 }
235 
236 static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
237 {
238 	return container_of(sd, struct mipid02_dev, sd);
239 }
240 
241 static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
242 {
243 	struct i2c_client *client = bridge->i2c_client;
244 	struct i2c_msg msg[2];
245 	u8 buf[2];
246 	int ret;
247 
248 	buf[0] = reg >> 8;
249 	buf[1] = reg & 0xff;
250 
251 	msg[0].addr = client->addr;
252 	msg[0].flags = client->flags;
253 	msg[0].buf = buf;
254 	msg[0].len = sizeof(buf);
255 
256 	msg[1].addr = client->addr;
257 	msg[1].flags = client->flags | I2C_M_RD;
258 	msg[1].buf = val;
259 	msg[1].len = 1;
260 
261 	ret = i2c_transfer(client->adapter, msg, 2);
262 	if (ret < 0) {
263 		dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
264 			    __func__, client->addr, reg, ret);
265 		return ret;
266 	}
267 
268 	return 0;
269 }
270 
271 static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
272 {
273 	struct i2c_client *client = bridge->i2c_client;
274 	struct i2c_msg msg;
275 	u8 buf[3];
276 	int ret;
277 
278 	buf[0] = reg >> 8;
279 	buf[1] = reg & 0xff;
280 	buf[2] = val;
281 
282 	msg.addr = client->addr;
283 	msg.flags = client->flags;
284 	msg.buf = buf;
285 	msg.len = sizeof(buf);
286 
287 	ret = i2c_transfer(client->adapter, &msg, 1);
288 	if (ret < 0) {
289 		dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
290 			    __func__, reg, ret);
291 		return ret;
292 	}
293 
294 	return 0;
295 }
296 
297 static int mipid02_get_regulators(struct mipid02_dev *bridge)
298 {
299 	unsigned int i;
300 
301 	for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
302 		bridge->supplies[i].supply = mipid02_supply_name[i];
303 
304 	return devm_regulator_bulk_get(&bridge->i2c_client->dev,
305 				       MIPID02_NUM_SUPPLIES,
306 				       bridge->supplies);
307 }
308 
309 static void mipid02_apply_reset(struct mipid02_dev *bridge)
310 {
311 	gpiod_set_value_cansleep(bridge->reset_gpio, 0);
312 	usleep_range(5000, 10000);
313 	gpiod_set_value_cansleep(bridge->reset_gpio, 1);
314 	usleep_range(5000, 10000);
315 	gpiod_set_value_cansleep(bridge->reset_gpio, 0);
316 	usleep_range(5000, 10000);
317 }
318 
319 static int mipid02_set_power_on(struct mipid02_dev *bridge)
320 {
321 	struct i2c_client *client = bridge->i2c_client;
322 	int ret;
323 
324 	ret = clk_prepare_enable(bridge->xclk);
325 	if (ret) {
326 		dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
327 		return ret;
328 	}
329 
330 	ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
331 				    bridge->supplies);
332 	if (ret) {
333 		dev_err(&client->dev, "%s: failed to enable regulators\n",
334 			    __func__);
335 		goto xclk_off;
336 	}
337 
338 	if (bridge->reset_gpio) {
339 		dev_dbg(&client->dev, "apply reset");
340 		mipid02_apply_reset(bridge);
341 	} else {
342 		dev_dbg(&client->dev, "don't apply reset");
343 		usleep_range(5000, 10000);
344 	}
345 
346 	return 0;
347 
348 xclk_off:
349 	clk_disable_unprepare(bridge->xclk);
350 	return ret;
351 }
352 
353 static void mipid02_set_power_off(struct mipid02_dev *bridge)
354 {
355 	regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
356 	clk_disable_unprepare(bridge->xclk);
357 }
358 
359 static int mipid02_detect(struct mipid02_dev *bridge)
360 {
361 	u8 reg;
362 
363 	/*
364 	 * There is no version registers. Just try to read register
365 	 * MIPID02_CLK_LANE_WR_REG1.
366 	 */
367 	return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
368 }
369 
370 static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
371 						    struct v4l2_subdev *subdev)
372 {
373 	struct v4l2_querymenu qm = {.id = V4L2_CID_LINK_FREQ, };
374 	struct v4l2_ctrl *ctrl;
375 	int ret;
376 
377 	ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_LINK_FREQ);
378 	if (!ctrl)
379 		return 0;
380 	qm.index = v4l2_ctrl_g_ctrl(ctrl);
381 
382 	ret = v4l2_querymenu(subdev->ctrl_handler, &qm);
383 	if (ret)
384 		return 0;
385 
386 	return qm.value;
387 }
388 
389 static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
390 						     struct v4l2_subdev *subdev)
391 {
392 	struct v4l2_fwnode_endpoint *ep = &bridge->rx;
393 	struct v4l2_ctrl *ctrl;
394 	u32 pixel_clock;
395 	u32 bpp = bpp_from_code(bridge->fmt.code);
396 
397 	ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
398 	if (!ctrl)
399 		return 0;
400 	pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
401 
402 	return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
403 }
404 
405 /*
406  * We need to know link frequency to setup clk_lane_reg1 timings. Link frequency
407  * will be computed using connected device V4L2_CID_PIXEL_RATE, bit per pixel
408  * and number of lanes.
409  */
410 static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge)
411 {
412 	struct i2c_client *client = bridge->i2c_client;
413 	struct v4l2_subdev *subdev = bridge->s_subdev;
414 	u32 link_freq;
415 
416 	link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, subdev);
417 	if (!link_freq) {
418 		link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge,
419 								      subdev);
420 		if (!link_freq) {
421 			dev_err(&client->dev, "Failed to get link frequency");
422 			return -EINVAL;
423 		}
424 	}
425 
426 	dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
427 	bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
428 
429 	return 0;
430 }
431 
432 static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
433 {
434 	struct i2c_client *client = bridge->i2c_client;
435 	struct v4l2_fwnode_endpoint *ep = &bridge->rx;
436 	bool *polarities = ep->bus.mipi_csi2.lane_polarities;
437 
438 	/* midid02 doesn't support clock lane remapping */
439 	if (ep->bus.mipi_csi2.clock_lane != 0) {
440 		dev_err(&client->dev, "clk lane must be map to lane 0\n");
441 		return -EINVAL;
442 	}
443 	bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
444 
445 	return 0;
446 }
447 
448 static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
449 					bool are_lanes_swap, bool *polarities)
450 {
451 	bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
452 
453 	if (nb == 1 && are_lanes_swap)
454 		return 0;
455 
456 	/*
457 	 * data lane 0 as pin swap polarity reversed compared to clock and
458 	 * data lane 1
459 	 */
460 	if (!are_pin_swap)
461 		bridge->r.data_lane0_reg1 = 1 << 1;
462 	bridge->r.data_lane0_reg1 |= DATA_ENABLE;
463 
464 	return 0;
465 }
466 
467 static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
468 					bool are_lanes_swap, bool *polarities)
469 {
470 	bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
471 
472 	if (nb == 1 && !are_lanes_swap)
473 		return 0;
474 
475 	if (are_pin_swap)
476 		bridge->r.data_lane1_reg1 = 1 << 1;
477 	bridge->r.data_lane1_reg1 |= DATA_ENABLE;
478 
479 	return 0;
480 }
481 
482 static int mipid02_configure_from_rx(struct mipid02_dev *bridge)
483 {
484 	struct v4l2_fwnode_endpoint *ep = &bridge->rx;
485 	bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
486 	bool *polarities = ep->bus.mipi_csi2.lane_polarities;
487 	int nb = ep->bus.mipi_csi2.num_data_lanes;
488 	int ret;
489 
490 	ret = mipid02_configure_clk_lane(bridge);
491 	if (ret)
492 		return ret;
493 
494 	ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
495 					   polarities);
496 	if (ret)
497 		return ret;
498 
499 	ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
500 					   polarities);
501 	if (ret)
502 		return ret;
503 
504 	bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
505 	bridge->r.mode_reg1 |= (nb - 1) << 1;
506 
507 	return mipid02_configure_from_rx_speed(bridge);
508 }
509 
510 static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
511 {
512 	struct v4l2_fwnode_endpoint *ep = &bridge->tx;
513 
514 	bridge->r.data_selection_ctrl = SELECTION_MANUAL_WIDTH;
515 	bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
516 	bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
517 	if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
518 		bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
519 	if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
520 		bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
521 	if (ep->bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
522 		bridge->r.mode_reg2 |= MODE_PCLK_SAMPLE_RISING;
523 
524 	return 0;
525 }
526 
527 static int mipid02_configure_from_code(struct mipid02_dev *bridge)
528 {
529 	u8 data_type;
530 
531 	bridge->r.data_id_rreg = 0;
532 
533 	if (bridge->fmt.code != MEDIA_BUS_FMT_JPEG_1X8) {
534 		bridge->r.data_selection_ctrl |= SELECTION_MANUAL_DATA;
535 
536 		data_type = data_type_from_code(bridge->fmt.code);
537 		if (!data_type)
538 			return -EINVAL;
539 		bridge->r.data_id_rreg = data_type;
540 	}
541 
542 	return 0;
543 }
544 
545 static int mipid02_stream_disable(struct mipid02_dev *bridge)
546 {
547 	struct i2c_client *client = bridge->i2c_client;
548 	int ret;
549 
550 	/* Disable all lanes */
551 	ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
552 	if (ret)
553 		goto error;
554 	ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
555 	if (ret)
556 		goto error;
557 	ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
558 	if (ret)
559 		goto error;
560 error:
561 	if (ret)
562 		dev_err(&client->dev, "failed to stream off %d", ret);
563 
564 	return ret;
565 }
566 
567 static int mipid02_stream_enable(struct mipid02_dev *bridge)
568 {
569 	struct i2c_client *client = bridge->i2c_client;
570 	int ret = -EINVAL;
571 
572 	if (!bridge->s_subdev)
573 		goto error;
574 
575 	memset(&bridge->r, 0, sizeof(bridge->r));
576 	/* build registers content */
577 	ret = mipid02_configure_from_rx(bridge);
578 	if (ret)
579 		goto error;
580 	ret = mipid02_configure_from_tx(bridge);
581 	if (ret)
582 		goto error;
583 	ret = mipid02_configure_from_code(bridge);
584 	if (ret)
585 		goto error;
586 
587 	/* write mipi registers */
588 	ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
589 		bridge->r.clk_lane_reg1);
590 	if (ret)
591 		goto error;
592 	ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
593 	if (ret)
594 		goto error;
595 	ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
596 		bridge->r.data_lane0_reg1);
597 	if (ret)
598 		goto error;
599 	ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
600 		DATA_MIPI_CSI);
601 	if (ret)
602 		goto error;
603 	ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
604 		bridge->r.data_lane1_reg1);
605 	if (ret)
606 		goto error;
607 	ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
608 		DATA_MIPI_CSI);
609 	if (ret)
610 		goto error;
611 	ret = mipid02_write_reg(bridge, MIPID02_MODE_REG1,
612 		MODE_NO_BYPASS | bridge->r.mode_reg1);
613 	if (ret)
614 		goto error;
615 	ret = mipid02_write_reg(bridge, MIPID02_MODE_REG2,
616 		bridge->r.mode_reg2);
617 	if (ret)
618 		goto error;
619 	ret = mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
620 		bridge->r.data_id_rreg);
621 	if (ret)
622 		goto error;
623 	ret = mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
624 		bridge->r.data_selection_ctrl);
625 	if (ret)
626 		goto error;
627 	ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
628 		bridge->r.pix_width_ctrl);
629 	if (ret)
630 		goto error;
631 	ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
632 		bridge->r.pix_width_ctrl_emb);
633 	if (ret)
634 		goto error;
635 
636 	return 0;
637 
638 error:
639 	dev_err(&client->dev, "failed to stream on %d", ret);
640 	mipid02_stream_disable(bridge);
641 
642 	return ret;
643 }
644 
645 static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
646 {
647 	struct mipid02_dev *bridge = to_mipid02_dev(sd);
648 	struct i2c_client *client = bridge->i2c_client;
649 	int ret = 0;
650 
651 	dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
652 		    enable, bridge->streaming);
653 	mutex_lock(&bridge->lock);
654 
655 	if (bridge->streaming == enable)
656 		goto out;
657 
658 	ret = enable ? mipid02_stream_enable(bridge) :
659 		       mipid02_stream_disable(bridge);
660 	if (!ret)
661 		bridge->streaming = enable;
662 
663 out:
664 	dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
665 		    bridge->streaming, ret);
666 	mutex_unlock(&bridge->lock);
667 
668 	return ret;
669 }
670 
671 static int mipid02_enum_mbus_code(struct v4l2_subdev *sd,
672 				 struct v4l2_subdev_state *sd_state,
673 				 struct v4l2_subdev_mbus_code_enum *code)
674 {
675 	struct mipid02_dev *bridge = to_mipid02_dev(sd);
676 	int ret = 0;
677 
678 	switch (code->pad) {
679 	case MIPID02_SINK_0:
680 		if (code->index >= ARRAY_SIZE(mipid02_supported_fmt_codes))
681 			ret = -EINVAL;
682 		else
683 			code->code = mipid02_supported_fmt_codes[code->index];
684 		break;
685 	case MIPID02_SOURCE:
686 		if (code->index == 0)
687 			code->code = serial_to_parallel_code(bridge->fmt.code);
688 		else
689 			ret = -EINVAL;
690 		break;
691 	default:
692 		ret = -EINVAL;
693 	}
694 
695 	return ret;
696 }
697 
698 static int mipid02_get_fmt(struct v4l2_subdev *sd,
699 			   struct v4l2_subdev_state *sd_state,
700 			   struct v4l2_subdev_format *format)
701 {
702 	struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
703 	struct mipid02_dev *bridge = to_mipid02_dev(sd);
704 	struct i2c_client *client = bridge->i2c_client;
705 	struct v4l2_mbus_framefmt *fmt;
706 
707 	dev_dbg(&client->dev, "%s probe %d", __func__, format->pad);
708 
709 	if (format->pad >= MIPID02_PAD_NB)
710 		return -EINVAL;
711 	/* second CSI-2 pad not yet supported */
712 	if (format->pad == MIPID02_SINK_1)
713 		return -EINVAL;
714 
715 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
716 		fmt = v4l2_subdev_get_try_format(&bridge->sd, sd_state,
717 						 format->pad);
718 	else
719 		fmt = &bridge->fmt;
720 
721 	mutex_lock(&bridge->lock);
722 
723 	*mbus_fmt = *fmt;
724 	/* code may need to be converted for source */
725 	if (format->pad == MIPID02_SOURCE)
726 		mbus_fmt->code = serial_to_parallel_code(mbus_fmt->code);
727 
728 	mutex_unlock(&bridge->lock);
729 
730 	return 0;
731 }
732 
733 static void mipid02_set_fmt_source(struct v4l2_subdev *sd,
734 				   struct v4l2_subdev_state *sd_state,
735 				   struct v4l2_subdev_format *format)
736 {
737 	struct mipid02_dev *bridge = to_mipid02_dev(sd);
738 
739 	/* source pad mirror active sink pad */
740 	format->format = bridge->fmt;
741 	/* but code may need to be converted */
742 	format->format.code = serial_to_parallel_code(format->format.code);
743 
744 	/* only apply format for V4L2_SUBDEV_FORMAT_TRY case */
745 	if (format->which != V4L2_SUBDEV_FORMAT_TRY)
746 		return;
747 
748 	*v4l2_subdev_get_try_format(sd, sd_state, format->pad) = format->format;
749 }
750 
751 static void mipid02_set_fmt_sink(struct v4l2_subdev *sd,
752 				 struct v4l2_subdev_state *sd_state,
753 				 struct v4l2_subdev_format *format)
754 {
755 	struct mipid02_dev *bridge = to_mipid02_dev(sd);
756 	struct v4l2_mbus_framefmt *fmt;
757 
758 	format->format.code = get_fmt_code(format->format.code);
759 
760 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
761 		fmt = v4l2_subdev_get_try_format(sd, sd_state, format->pad);
762 	else
763 		fmt = &bridge->fmt;
764 
765 	*fmt = format->format;
766 }
767 
768 static int mipid02_set_fmt(struct v4l2_subdev *sd,
769 			   struct v4l2_subdev_state *sd_state,
770 			   struct v4l2_subdev_format *format)
771 {
772 	struct mipid02_dev *bridge = to_mipid02_dev(sd);
773 	struct i2c_client *client = bridge->i2c_client;
774 	int ret = 0;
775 
776 	dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
777 
778 	if (format->pad >= MIPID02_PAD_NB)
779 		return -EINVAL;
780 	/* second CSI-2 pad not yet supported */
781 	if (format->pad == MIPID02_SINK_1)
782 		return -EINVAL;
783 
784 	mutex_lock(&bridge->lock);
785 
786 	if (bridge->streaming) {
787 		ret = -EBUSY;
788 		goto error;
789 	}
790 
791 	if (format->pad == MIPID02_SOURCE)
792 		mipid02_set_fmt_source(sd, sd_state, format);
793 	else
794 		mipid02_set_fmt_sink(sd, sd_state, format);
795 
796 error:
797 	mutex_unlock(&bridge->lock);
798 
799 	return ret;
800 }
801 
802 static const struct v4l2_subdev_video_ops mipid02_video_ops = {
803 	.s_stream = mipid02_s_stream,
804 };
805 
806 static const struct v4l2_subdev_pad_ops mipid02_pad_ops = {
807 	.enum_mbus_code = mipid02_enum_mbus_code,
808 	.get_fmt = mipid02_get_fmt,
809 	.set_fmt = mipid02_set_fmt,
810 };
811 
812 static const struct v4l2_subdev_ops mipid02_subdev_ops = {
813 	.video = &mipid02_video_ops,
814 	.pad = &mipid02_pad_ops,
815 };
816 
817 static const struct media_entity_operations mipid02_subdev_entity_ops = {
818 	.link_validate = v4l2_subdev_link_validate,
819 };
820 
821 static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
822 			       struct v4l2_subdev *s_subdev,
823 			       struct v4l2_async_subdev *asd)
824 {
825 	struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
826 	struct i2c_client *client = bridge->i2c_client;
827 	int source_pad;
828 	int ret;
829 
830 	dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
831 
832 	source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
833 						 s_subdev->fwnode,
834 						 MEDIA_PAD_FL_SOURCE);
835 	if (source_pad < 0) {
836 		dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
837 			s_subdev->name);
838 		return source_pad;
839 	}
840 
841 	ret = media_create_pad_link(&s_subdev->entity, source_pad,
842 				    &bridge->sd.entity, 0,
843 				    MEDIA_LNK_FL_ENABLED |
844 				    MEDIA_LNK_FL_IMMUTABLE);
845 	if (ret) {
846 		dev_err(&client->dev, "Couldn't create media link %d", ret);
847 		return ret;
848 	}
849 
850 	bridge->s_subdev = s_subdev;
851 
852 	return 0;
853 }
854 
855 static void mipid02_async_unbind(struct v4l2_async_notifier *notifier,
856 				 struct v4l2_subdev *s_subdev,
857 				 struct v4l2_async_subdev *asd)
858 {
859 	struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
860 
861 	bridge->s_subdev = NULL;
862 }
863 
864 static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
865 	.bound		= mipid02_async_bound,
866 	.unbind		= mipid02_async_unbind,
867 };
868 
869 static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
870 {
871 	struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_CSI2_DPHY };
872 	struct i2c_client *client = bridge->i2c_client;
873 	struct v4l2_async_subdev *asd;
874 	struct device_node *ep_node;
875 	int ret;
876 
877 	/* parse rx (endpoint 0) */
878 	ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
879 						0, 0);
880 	if (!ep_node) {
881 		dev_err(&client->dev, "unable to find port0 ep");
882 		ret = -EINVAL;
883 		goto error;
884 	}
885 
886 	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
887 	if (ret) {
888 		dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
889 			ret);
890 		goto error_of_node_put;
891 	}
892 
893 	/* do some sanity checks */
894 	if (ep.bus.mipi_csi2.num_data_lanes > 2) {
895 		dev_err(&client->dev, "max supported data lanes is 2 / got %d",
896 			ep.bus.mipi_csi2.num_data_lanes);
897 		ret = -EINVAL;
898 		goto error_of_node_put;
899 	}
900 
901 	/* register it for later use */
902 	bridge->rx = ep;
903 
904 	/* register async notifier so we get noticed when sensor is connected */
905 	v4l2_async_nf_init(&bridge->notifier);
906 	asd = v4l2_async_nf_add_fwnode_remote(&bridge->notifier,
907 					      of_fwnode_handle(ep_node),
908 					      struct v4l2_async_subdev);
909 	of_node_put(ep_node);
910 
911 	if (IS_ERR(asd)) {
912 		dev_err(&client->dev, "fail to register asd to notifier %ld",
913 			PTR_ERR(asd));
914 		return PTR_ERR(asd);
915 	}
916 	bridge->notifier.ops = &mipid02_notifier_ops;
917 
918 	ret = v4l2_async_subdev_nf_register(&bridge->sd, &bridge->notifier);
919 	if (ret)
920 		v4l2_async_nf_cleanup(&bridge->notifier);
921 
922 	return ret;
923 
924 error_of_node_put:
925 	of_node_put(ep_node);
926 error:
927 
928 	return ret;
929 }
930 
931 static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
932 {
933 	struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_PARALLEL };
934 	struct i2c_client *client = bridge->i2c_client;
935 	struct device_node *ep_node;
936 	int ret;
937 
938 	/* parse tx (endpoint 2) */
939 	ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
940 						2, 0);
941 	if (!ep_node) {
942 		dev_err(&client->dev, "unable to find port1 ep");
943 		ret = -EINVAL;
944 		goto error;
945 	}
946 
947 	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
948 	if (ret) {
949 		dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
950 		goto error_of_node_put;
951 	}
952 
953 	of_node_put(ep_node);
954 	bridge->tx = ep;
955 
956 	return 0;
957 
958 error_of_node_put:
959 	of_node_put(ep_node);
960 error:
961 
962 	return -EINVAL;
963 }
964 
965 static int mipid02_probe(struct i2c_client *client)
966 {
967 	struct device *dev = &client->dev;
968 	struct mipid02_dev *bridge;
969 	u32 clk_freq;
970 	int ret;
971 
972 	bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
973 	if (!bridge)
974 		return -ENOMEM;
975 
976 	init_format(&bridge->fmt);
977 
978 	bridge->i2c_client = client;
979 	v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
980 
981 	/* got and check clock */
982 	bridge->xclk = devm_clk_get(dev, "xclk");
983 	if (IS_ERR(bridge->xclk)) {
984 		dev_err(dev, "failed to get xclk\n");
985 		return PTR_ERR(bridge->xclk);
986 	}
987 
988 	clk_freq = clk_get_rate(bridge->xclk);
989 	if (clk_freq < 6000000 || clk_freq > 27000000) {
990 		dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
991 			clk_freq);
992 		return -EINVAL;
993 	}
994 
995 	bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
996 						     GPIOD_OUT_HIGH);
997 
998 	if (IS_ERR(bridge->reset_gpio)) {
999 		dev_err(dev, "failed to get reset GPIO\n");
1000 		return PTR_ERR(bridge->reset_gpio);
1001 	}
1002 
1003 	ret = mipid02_get_regulators(bridge);
1004 	if (ret) {
1005 		dev_err(dev, "failed to get regulators %d", ret);
1006 		return ret;
1007 	}
1008 
1009 	mutex_init(&bridge->lock);
1010 	bridge->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1011 	bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
1012 	bridge->sd.entity.ops = &mipid02_subdev_entity_ops;
1013 	bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
1014 	bridge->pad[1].flags = MEDIA_PAD_FL_SINK;
1015 	bridge->pad[2].flags = MEDIA_PAD_FL_SOURCE;
1016 	ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
1017 				     bridge->pad);
1018 	if (ret) {
1019 		dev_err(&client->dev, "pads init failed %d", ret);
1020 		goto mutex_cleanup;
1021 	}
1022 
1023 	/* enable clock, power and reset device if available */
1024 	ret = mipid02_set_power_on(bridge);
1025 	if (ret)
1026 		goto entity_cleanup;
1027 
1028 	ret = mipid02_detect(bridge);
1029 	if (ret) {
1030 		dev_err(&client->dev, "failed to detect mipid02 %d", ret);
1031 		goto power_off;
1032 	}
1033 
1034 	ret = mipid02_parse_tx_ep(bridge);
1035 	if (ret) {
1036 		dev_err(&client->dev, "failed to parse tx %d", ret);
1037 		goto power_off;
1038 	}
1039 
1040 	ret = mipid02_parse_rx_ep(bridge);
1041 	if (ret) {
1042 		dev_err(&client->dev, "failed to parse rx %d", ret);
1043 		goto power_off;
1044 	}
1045 
1046 	ret = v4l2_async_register_subdev(&bridge->sd);
1047 	if (ret < 0) {
1048 		dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
1049 			    ret);
1050 		goto unregister_notifier;
1051 	}
1052 
1053 	dev_info(&client->dev, "mipid02 device probe successfully");
1054 
1055 	return 0;
1056 
1057 unregister_notifier:
1058 	v4l2_async_nf_unregister(&bridge->notifier);
1059 	v4l2_async_nf_cleanup(&bridge->notifier);
1060 power_off:
1061 	mipid02_set_power_off(bridge);
1062 entity_cleanup:
1063 	media_entity_cleanup(&bridge->sd.entity);
1064 mutex_cleanup:
1065 	mutex_destroy(&bridge->lock);
1066 
1067 	return ret;
1068 }
1069 
1070 static void mipid02_remove(struct i2c_client *client)
1071 {
1072 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1073 	struct mipid02_dev *bridge = to_mipid02_dev(sd);
1074 
1075 	v4l2_async_nf_unregister(&bridge->notifier);
1076 	v4l2_async_nf_cleanup(&bridge->notifier);
1077 	v4l2_async_unregister_subdev(&bridge->sd);
1078 	mipid02_set_power_off(bridge);
1079 	media_entity_cleanup(&bridge->sd.entity);
1080 	mutex_destroy(&bridge->lock);
1081 }
1082 
1083 static const struct of_device_id mipid02_dt_ids[] = {
1084 	{ .compatible = "st,st-mipid02" },
1085 	{ /* sentinel */ }
1086 };
1087 MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
1088 
1089 static struct i2c_driver mipid02_i2c_driver = {
1090 	.driver = {
1091 		.name  = "st-mipid02",
1092 		.of_match_table = mipid02_dt_ids,
1093 	},
1094 	.probe_new = mipid02_probe,
1095 	.remove = mipid02_remove,
1096 };
1097 
1098 module_i2c_driver(mipid02_i2c_driver);
1099 
1100 MODULE_AUTHOR("Mickael Guene <mickael.guene@st.com>");
1101 MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
1102 MODULE_LICENSE("GPL v2");
1103