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