1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
5 * Copyright (C) STMicroelectronics SA 2019
6 * Authors: Mickael Guene <mickael.guene@st.com>
7 * for STMicroelectronics.
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>
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
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)
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
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 */
76 #define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
78 #define MIPID02_SINK_0 0
79 #define MIPID02_SINK_1 1
80 #define MIPID02_SOURCE 2
81 #define MIPID02_PAD_NB 3
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
];
89 struct gpio_desc
*reset_gpio
;
91 struct v4l2_fwnode_endpoint rx
;
93 struct v4l2_fwnode_endpoint tx
;
95 struct v4l2_async_subdev asd
;
96 struct v4l2_async_notifier notifier
;
97 struct v4l2_subdev
*s_subdev
;
105 u8 data_selection_ctrl
;
108 u8 pix_width_ctrl_emb
;
110 /* lock to protect all members below */
113 struct v4l2_mbus_framefmt fmt
;
116 static int bpp_from_code(__u32 code
)
119 case MEDIA_BUS_FMT_SBGGR8_1X8
:
120 case MEDIA_BUS_FMT_SGBRG8_1X8
:
121 case MEDIA_BUS_FMT_SGRBG8_1X8
:
122 case MEDIA_BUS_FMT_SRGGB8_1X8
:
124 case MEDIA_BUS_FMT_SBGGR10_1X10
:
125 case MEDIA_BUS_FMT_SGBRG10_1X10
:
126 case MEDIA_BUS_FMT_SGRBG10_1X10
:
127 case MEDIA_BUS_FMT_SRGGB10_1X10
:
129 case MEDIA_BUS_FMT_SBGGR12_1X12
:
130 case MEDIA_BUS_FMT_SGBRG12_1X12
:
131 case MEDIA_BUS_FMT_SGRBG12_1X12
:
132 case MEDIA_BUS_FMT_SRGGB12_1X12
:
134 case MEDIA_BUS_FMT_UYVY8_1X16
:
135 case MEDIA_BUS_FMT_YUYV8_2X8
:
136 case MEDIA_BUS_FMT_UYVY8_2X8
:
137 case MEDIA_BUS_FMT_RGB565_2X8_LE
:
138 case MEDIA_BUS_FMT_RGB565_2X8_BE
:
140 case MEDIA_BUS_FMT_BGR888_1X24
:
147 static u8
data_type_from_code(__u32 code
)
150 case MEDIA_BUS_FMT_SBGGR8_1X8
:
151 case MEDIA_BUS_FMT_SGBRG8_1X8
:
152 case MEDIA_BUS_FMT_SGRBG8_1X8
:
153 case MEDIA_BUS_FMT_SRGGB8_1X8
:
155 case MEDIA_BUS_FMT_SBGGR10_1X10
:
156 case MEDIA_BUS_FMT_SGBRG10_1X10
:
157 case MEDIA_BUS_FMT_SGRBG10_1X10
:
158 case MEDIA_BUS_FMT_SRGGB10_1X10
:
160 case MEDIA_BUS_FMT_SBGGR12_1X12
:
161 case MEDIA_BUS_FMT_SGBRG12_1X12
:
162 case MEDIA_BUS_FMT_SGRBG12_1X12
:
163 case MEDIA_BUS_FMT_SRGGB12_1X12
:
165 case MEDIA_BUS_FMT_UYVY8_1X16
:
166 case MEDIA_BUS_FMT_YUYV8_2X8
:
167 case MEDIA_BUS_FMT_UYVY8_2X8
:
169 case MEDIA_BUS_FMT_BGR888_1X24
:
171 case MEDIA_BUS_FMT_RGB565_2X8_LE
:
172 case MEDIA_BUS_FMT_RGB565_2X8_BE
:
179 static void init_format(struct v4l2_mbus_framefmt
*fmt
)
181 fmt
->code
= MEDIA_BUS_FMT_SBGGR8_1X8
;
182 fmt
->field
= V4L2_FIELD_NONE
;
183 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
184 fmt
->ycbcr_enc
= V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB
);
185 fmt
->quantization
= V4L2_QUANTIZATION_FULL_RANGE
;
186 fmt
->xfer_func
= V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB
);
191 static __u32
get_fmt_code(__u32 code
)
195 for (i
= 0; i
< ARRAY_SIZE(mipid02_supported_fmt_codes
); i
++) {
196 if (code
== mipid02_supported_fmt_codes
[i
])
200 return mipid02_supported_fmt_codes
[0];
203 static __u32
serial_to_parallel_code(__u32 serial
)
205 if (serial
== MEDIA_BUS_FMT_UYVY8_1X16
)
206 return MEDIA_BUS_FMT_UYVY8_2X8
;
207 if (serial
== MEDIA_BUS_FMT_BGR888_1X24
)
208 return MEDIA_BUS_FMT_BGR888_3X8
;
213 static inline struct mipid02_dev
*to_mipid02_dev(struct v4l2_subdev
*sd
)
215 return container_of(sd
, struct mipid02_dev
, sd
);
218 static int mipid02_read_reg(struct mipid02_dev
*bridge
, u16 reg
, u8
*val
)
220 struct i2c_client
*client
= bridge
->i2c_client
;
221 struct i2c_msg msg
[2];
228 msg
[0].addr
= client
->addr
;
229 msg
[0].flags
= client
->flags
;
231 msg
[0].len
= sizeof(buf
);
233 msg
[1].addr
= client
->addr
;
234 msg
[1].flags
= client
->flags
| I2C_M_RD
;
238 ret
= i2c_transfer(client
->adapter
, msg
, 2);
240 dev_dbg(&client
->dev
, "%s: %x i2c_transfer, reg: %x => %d\n",
241 __func__
, client
->addr
, reg
, ret
);
248 static int mipid02_write_reg(struct mipid02_dev
*bridge
, u16 reg
, u8 val
)
250 struct i2c_client
*client
= bridge
->i2c_client
;
259 msg
.addr
= client
->addr
;
260 msg
.flags
= client
->flags
;
262 msg
.len
= sizeof(buf
);
264 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
266 dev_dbg(&client
->dev
, "%s: i2c_transfer, reg: %x => %d\n",
274 static int mipid02_get_regulators(struct mipid02_dev
*bridge
)
278 for (i
= 0; i
< MIPID02_NUM_SUPPLIES
; i
++)
279 bridge
->supplies
[i
].supply
= mipid02_supply_name
[i
];
281 return devm_regulator_bulk_get(&bridge
->i2c_client
->dev
,
282 MIPID02_NUM_SUPPLIES
,
286 static void mipid02_apply_reset(struct mipid02_dev
*bridge
)
288 gpiod_set_value_cansleep(bridge
->reset_gpio
, 0);
289 usleep_range(5000, 10000);
290 gpiod_set_value_cansleep(bridge
->reset_gpio
, 1);
291 usleep_range(5000, 10000);
292 gpiod_set_value_cansleep(bridge
->reset_gpio
, 0);
293 usleep_range(5000, 10000);
296 static int mipid02_set_power_on(struct mipid02_dev
*bridge
)
298 struct i2c_client
*client
= bridge
->i2c_client
;
301 ret
= clk_prepare_enable(bridge
->xclk
);
303 dev_err(&client
->dev
, "%s: failed to enable clock\n", __func__
);
307 ret
= regulator_bulk_enable(MIPID02_NUM_SUPPLIES
,
310 dev_err(&client
->dev
, "%s: failed to enable regulators\n",
315 if (bridge
->reset_gpio
) {
316 dev_dbg(&client
->dev
, "apply reset");
317 mipid02_apply_reset(bridge
);
319 dev_dbg(&client
->dev
, "don't apply reset");
320 usleep_range(5000, 10000);
326 clk_disable_unprepare(bridge
->xclk
);
330 static void mipid02_set_power_off(struct mipid02_dev
*bridge
)
332 regulator_bulk_disable(MIPID02_NUM_SUPPLIES
, bridge
->supplies
);
333 clk_disable_unprepare(bridge
->xclk
);
336 static int mipid02_detect(struct mipid02_dev
*bridge
)
341 * There is no version registers. Just try to read register
342 * MIPID02_CLK_LANE_WR_REG1.
344 return mipid02_read_reg(bridge
, MIPID02_CLK_LANE_WR_REG1
, ®
);
347 static u32
mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev
*bridge
,
348 struct v4l2_subdev
*subdev
)
350 struct v4l2_querymenu qm
= {.id
= V4L2_CID_LINK_FREQ
, };
351 struct v4l2_ctrl
*ctrl
;
354 ctrl
= v4l2_ctrl_find(subdev
->ctrl_handler
, V4L2_CID_LINK_FREQ
);
357 qm
.index
= v4l2_ctrl_g_ctrl(ctrl
);
359 ret
= v4l2_querymenu(subdev
->ctrl_handler
, &qm
);
366 static u32
mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev
*bridge
,
367 struct v4l2_subdev
*subdev
)
369 struct v4l2_fwnode_endpoint
*ep
= &bridge
->rx
;
370 struct v4l2_ctrl
*ctrl
;
372 u32 bpp
= bpp_from_code(bridge
->fmt
.code
);
374 ctrl
= v4l2_ctrl_find(subdev
->ctrl_handler
, V4L2_CID_PIXEL_RATE
);
377 pixel_clock
= v4l2_ctrl_g_ctrl_int64(ctrl
);
379 return pixel_clock
* bpp
/ (2 * ep
->bus
.mipi_csi2
.num_data_lanes
);
383 * We need to know link frequency to setup clk_lane_reg1 timings. Link frequency
384 * will be computed using connected device V4L2_CID_PIXEL_RATE, bit per pixel
385 * and number of lanes.
387 static int mipid02_configure_from_rx_speed(struct mipid02_dev
*bridge
)
389 struct i2c_client
*client
= bridge
->i2c_client
;
390 struct v4l2_subdev
*subdev
= bridge
->s_subdev
;
393 link_freq
= mipid02_get_link_freq_from_cid_link_freq(bridge
, subdev
);
395 link_freq
= mipid02_get_link_freq_from_cid_pixel_rate(bridge
,
398 dev_err(&client
->dev
, "Failed to get link frequency");
403 dev_dbg(&client
->dev
, "detect link_freq = %d Hz", link_freq
);
404 bridge
->r
.clk_lane_reg1
|= (2000000000 / link_freq
) << 2;
409 static int mipid02_configure_clk_lane(struct mipid02_dev
*bridge
)
411 struct i2c_client
*client
= bridge
->i2c_client
;
412 struct v4l2_fwnode_endpoint
*ep
= &bridge
->rx
;
413 bool *polarities
= ep
->bus
.mipi_csi2
.lane_polarities
;
415 /* midid02 doesn't support clock lane remapping */
416 if (ep
->bus
.mipi_csi2
.clock_lane
!= 0) {
417 dev_err(&client
->dev
, "clk lane must be map to lane 0\n");
420 bridge
->r
.clk_lane_reg1
|= (polarities
[0] << 1) | CLK_ENABLE
;
425 static int mipid02_configure_data0_lane(struct mipid02_dev
*bridge
, int nb
,
426 bool are_lanes_swap
, bool *polarities
)
428 bool are_pin_swap
= are_lanes_swap
? polarities
[2] : polarities
[1];
430 if (nb
== 1 && are_lanes_swap
)
434 * data lane 0 as pin swap polarity reversed compared to clock and
438 bridge
->r
.data_lane0_reg1
= 1 << 1;
439 bridge
->r
.data_lane0_reg1
|= DATA_ENABLE
;
444 static int mipid02_configure_data1_lane(struct mipid02_dev
*bridge
, int nb
,
445 bool are_lanes_swap
, bool *polarities
)
447 bool are_pin_swap
= are_lanes_swap
? polarities
[1] : polarities
[2];
449 if (nb
== 1 && !are_lanes_swap
)
453 bridge
->r
.data_lane1_reg1
= 1 << 1;
454 bridge
->r
.data_lane1_reg1
|= DATA_ENABLE
;
459 static int mipid02_configure_from_rx(struct mipid02_dev
*bridge
)
461 struct v4l2_fwnode_endpoint
*ep
= &bridge
->rx
;
462 bool are_lanes_swap
= ep
->bus
.mipi_csi2
.data_lanes
[0] == 2;
463 bool *polarities
= ep
->bus
.mipi_csi2
.lane_polarities
;
464 int nb
= ep
->bus
.mipi_csi2
.num_data_lanes
;
467 ret
= mipid02_configure_clk_lane(bridge
);
471 ret
= mipid02_configure_data0_lane(bridge
, nb
, are_lanes_swap
,
476 ret
= mipid02_configure_data1_lane(bridge
, nb
, are_lanes_swap
,
481 bridge
->r
.mode_reg1
|= are_lanes_swap
? MODE_DATA_SWAP
: 0;
482 bridge
->r
.mode_reg1
|= (nb
- 1) << 1;
484 return mipid02_configure_from_rx_speed(bridge
);
487 static int mipid02_configure_from_tx(struct mipid02_dev
*bridge
)
489 struct v4l2_fwnode_endpoint
*ep
= &bridge
->tx
;
491 bridge
->r
.data_selection_ctrl
= SELECTION_MANUAL_WIDTH
;
492 bridge
->r
.pix_width_ctrl
= ep
->bus
.parallel
.bus_width
;
493 bridge
->r
.pix_width_ctrl_emb
= ep
->bus
.parallel
.bus_width
;
494 if (ep
->bus
.parallel
.flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
)
495 bridge
->r
.mode_reg2
|= MODE_HSYNC_ACTIVE_HIGH
;
496 if (ep
->bus
.parallel
.flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
)
497 bridge
->r
.mode_reg2
|= MODE_VSYNC_ACTIVE_HIGH
;
502 static int mipid02_configure_from_code(struct mipid02_dev
*bridge
)
506 bridge
->r
.data_id_rreg
= 0;
508 if (bridge
->fmt
.code
!= MEDIA_BUS_FMT_JPEG_1X8
) {
509 bridge
->r
.data_selection_ctrl
|= SELECTION_MANUAL_DATA
;
511 data_type
= data_type_from_code(bridge
->fmt
.code
);
514 bridge
->r
.data_id_rreg
= data_type
;
520 static int mipid02_stream_disable(struct mipid02_dev
*bridge
)
522 struct i2c_client
*client
= bridge
->i2c_client
;
525 /* Disable all lanes */
526 ret
= mipid02_write_reg(bridge
, MIPID02_CLK_LANE_REG1
, 0);
529 ret
= mipid02_write_reg(bridge
, MIPID02_DATA_LANE0_REG1
, 0);
532 ret
= mipid02_write_reg(bridge
, MIPID02_DATA_LANE1_REG1
, 0);
537 dev_err(&client
->dev
, "failed to stream off %d", ret
);
542 static int mipid02_stream_enable(struct mipid02_dev
*bridge
)
544 struct i2c_client
*client
= bridge
->i2c_client
;
547 if (!bridge
->s_subdev
)
550 memset(&bridge
->r
, 0, sizeof(bridge
->r
));
551 /* build registers content */
552 ret
= mipid02_configure_from_rx(bridge
);
555 ret
= mipid02_configure_from_tx(bridge
);
558 ret
= mipid02_configure_from_code(bridge
);
562 /* write mipi registers */
563 ret
= mipid02_write_reg(bridge
, MIPID02_CLK_LANE_REG1
,
564 bridge
->r
.clk_lane_reg1
);
567 ret
= mipid02_write_reg(bridge
, MIPID02_CLK_LANE_REG3
, CLK_MIPI_CSI
);
570 ret
= mipid02_write_reg(bridge
, MIPID02_DATA_LANE0_REG1
,
571 bridge
->r
.data_lane0_reg1
);
574 ret
= mipid02_write_reg(bridge
, MIPID02_DATA_LANE0_REG2
,
578 ret
= mipid02_write_reg(bridge
, MIPID02_DATA_LANE1_REG1
,
579 bridge
->r
.data_lane1_reg1
);
582 ret
= mipid02_write_reg(bridge
, MIPID02_DATA_LANE1_REG2
,
586 ret
= mipid02_write_reg(bridge
, MIPID02_MODE_REG1
,
587 MODE_NO_BYPASS
| bridge
->r
.mode_reg1
);
590 ret
= mipid02_write_reg(bridge
, MIPID02_MODE_REG2
,
591 bridge
->r
.mode_reg2
);
594 ret
= mipid02_write_reg(bridge
, MIPID02_DATA_ID_RREG
,
595 bridge
->r
.data_id_rreg
);
598 ret
= mipid02_write_reg(bridge
, MIPID02_DATA_SELECTION_CTRL
,
599 bridge
->r
.data_selection_ctrl
);
602 ret
= mipid02_write_reg(bridge
, MIPID02_PIX_WIDTH_CTRL
,
603 bridge
->r
.pix_width_ctrl
);
606 ret
= mipid02_write_reg(bridge
, MIPID02_PIX_WIDTH_CTRL_EMB
,
607 bridge
->r
.pix_width_ctrl_emb
);
614 dev_err(&client
->dev
, "failed to stream on %d", ret
);
615 mipid02_stream_disable(bridge
);
620 static int mipid02_s_stream(struct v4l2_subdev
*sd
, int enable
)
622 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
623 struct i2c_client
*client
= bridge
->i2c_client
;
626 dev_dbg(&client
->dev
, "%s : requested %d / current = %d", __func__
,
627 enable
, bridge
->streaming
);
628 mutex_lock(&bridge
->lock
);
630 if (bridge
->streaming
== enable
)
633 ret
= enable
? mipid02_stream_enable(bridge
) :
634 mipid02_stream_disable(bridge
);
636 bridge
->streaming
= enable
;
639 dev_dbg(&client
->dev
, "%s current now = %d / %d", __func__
,
640 bridge
->streaming
, ret
);
641 mutex_unlock(&bridge
->lock
);
646 static int mipid02_enum_mbus_code(struct v4l2_subdev
*sd
,
647 struct v4l2_subdev_pad_config
*cfg
,
648 struct v4l2_subdev_mbus_code_enum
*code
)
650 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
655 if (code
->index
>= ARRAY_SIZE(mipid02_supported_fmt_codes
))
658 code
->code
= mipid02_supported_fmt_codes
[code
->index
];
661 if (code
->index
== 0)
662 code
->code
= serial_to_parallel_code(bridge
->fmt
.code
);
673 static int mipid02_get_fmt(struct v4l2_subdev
*sd
,
674 struct v4l2_subdev_pad_config
*cfg
,
675 struct v4l2_subdev_format
*format
)
677 struct v4l2_mbus_framefmt
*mbus_fmt
= &format
->format
;
678 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
679 struct i2c_client
*client
= bridge
->i2c_client
;
680 struct v4l2_mbus_framefmt
*fmt
;
682 dev_dbg(&client
->dev
, "%s probe %d", __func__
, format
->pad
);
684 if (format
->pad
>= MIPID02_PAD_NB
)
686 /* second CSI-2 pad not yet supported */
687 if (format
->pad
== MIPID02_SINK_1
)
690 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
)
691 fmt
= v4l2_subdev_get_try_format(&bridge
->sd
, cfg
, format
->pad
);
695 mutex_lock(&bridge
->lock
);
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
);
702 mutex_unlock(&bridge
->lock
);
707 static void mipid02_set_fmt_source(struct v4l2_subdev
*sd
,
708 struct v4l2_subdev_pad_config
*cfg
,
709 struct v4l2_subdev_format
*format
)
711 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
713 /* source pad mirror active sink pad */
714 format
->format
= bridge
->fmt
;
715 /* but code may need to be converted */
716 format
->format
.code
= serial_to_parallel_code(format
->format
.code
);
718 /* only apply format for V4L2_SUBDEV_FORMAT_TRY case */
719 if (format
->which
!= V4L2_SUBDEV_FORMAT_TRY
)
722 *v4l2_subdev_get_try_format(sd
, cfg
, format
->pad
) = format
->format
;
725 static void mipid02_set_fmt_sink(struct v4l2_subdev
*sd
,
726 struct v4l2_subdev_pad_config
*cfg
,
727 struct v4l2_subdev_format
*format
)
729 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
730 struct v4l2_mbus_framefmt
*fmt
;
732 format
->format
.code
= get_fmt_code(format
->format
.code
);
734 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
)
735 fmt
= v4l2_subdev_get_try_format(sd
, cfg
, format
->pad
);
739 *fmt
= format
->format
;
742 static int mipid02_set_fmt(struct v4l2_subdev
*sd
,
743 struct v4l2_subdev_pad_config
*cfg
,
744 struct v4l2_subdev_format
*format
)
746 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
747 struct i2c_client
*client
= bridge
->i2c_client
;
750 dev_dbg(&client
->dev
, "%s for %d", __func__
, format
->pad
);
752 if (format
->pad
>= MIPID02_PAD_NB
)
754 /* second CSI-2 pad not yet supported */
755 if (format
->pad
== MIPID02_SINK_1
)
758 mutex_lock(&bridge
->lock
);
760 if (bridge
->streaming
) {
765 if (format
->pad
== MIPID02_SOURCE
)
766 mipid02_set_fmt_source(sd
, cfg
, format
);
768 mipid02_set_fmt_sink(sd
, cfg
, format
);
771 mutex_unlock(&bridge
->lock
);
776 static const struct v4l2_subdev_video_ops mipid02_video_ops
= {
777 .s_stream
= mipid02_s_stream
,
780 static const struct v4l2_subdev_pad_ops mipid02_pad_ops
= {
781 .enum_mbus_code
= mipid02_enum_mbus_code
,
782 .get_fmt
= mipid02_get_fmt
,
783 .set_fmt
= mipid02_set_fmt
,
786 static const struct v4l2_subdev_ops mipid02_subdev_ops
= {
787 .video
= &mipid02_video_ops
,
788 .pad
= &mipid02_pad_ops
,
791 static const struct media_entity_operations mipid02_subdev_entity_ops
= {
792 .link_validate
= v4l2_subdev_link_validate
,
795 static int mipid02_async_bound(struct v4l2_async_notifier
*notifier
,
796 struct v4l2_subdev
*s_subdev
,
797 struct v4l2_async_subdev
*asd
)
799 struct mipid02_dev
*bridge
= to_mipid02_dev(notifier
->sd
);
800 struct i2c_client
*client
= bridge
->i2c_client
;
804 dev_dbg(&client
->dev
, "sensor_async_bound call %p", s_subdev
);
806 source_pad
= media_entity_get_fwnode_pad(&s_subdev
->entity
,
808 MEDIA_PAD_FL_SOURCE
);
809 if (source_pad
< 0) {
810 dev_err(&client
->dev
, "Couldn't find output pad for subdev %s\n",
815 ret
= media_create_pad_link(&s_subdev
->entity
, source_pad
,
816 &bridge
->sd
.entity
, 0,
817 MEDIA_LNK_FL_ENABLED
|
818 MEDIA_LNK_FL_IMMUTABLE
);
820 dev_err(&client
->dev
, "Couldn't create media link %d", ret
);
824 bridge
->s_subdev
= s_subdev
;
829 static void mipid02_async_unbind(struct v4l2_async_notifier
*notifier
,
830 struct v4l2_subdev
*s_subdev
,
831 struct v4l2_async_subdev
*asd
)
833 struct mipid02_dev
*bridge
= to_mipid02_dev(notifier
->sd
);
835 bridge
->s_subdev
= NULL
;
838 static const struct v4l2_async_notifier_operations mipid02_notifier_ops
= {
839 .bound
= mipid02_async_bound
,
840 .unbind
= mipid02_async_unbind
,
843 static int mipid02_parse_rx_ep(struct mipid02_dev
*bridge
)
845 struct v4l2_fwnode_endpoint ep
= { .bus_type
= V4L2_MBUS_CSI2_DPHY
};
846 struct i2c_client
*client
= bridge
->i2c_client
;
847 struct device_node
*ep_node
;
850 /* parse rx (endpoint 0) */
851 ep_node
= of_graph_get_endpoint_by_regs(bridge
->i2c_client
->dev
.of_node
,
854 dev_err(&client
->dev
, "unable to find port0 ep");
859 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node
), &ep
);
861 dev_err(&client
->dev
, "Could not parse v4l2 endpoint %d\n",
863 goto error_of_node_put
;
866 /* do some sanity checks */
867 if (ep
.bus
.mipi_csi2
.num_data_lanes
> 2) {
868 dev_err(&client
->dev
, "max supported data lanes is 2 / got %d",
869 ep
.bus
.mipi_csi2
.num_data_lanes
);
871 goto error_of_node_put
;
874 /* register it for later use */
877 /* register async notifier so we get noticed when sensor is connected */
878 bridge
->asd
.match
.fwnode
=
879 fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node
));
880 bridge
->asd
.match_type
= V4L2_ASYNC_MATCH_FWNODE
;
881 of_node_put(ep_node
);
883 v4l2_async_notifier_init(&bridge
->notifier
);
884 ret
= v4l2_async_notifier_add_subdev(&bridge
->notifier
, &bridge
->asd
);
886 dev_err(&client
->dev
, "fail to register asd to notifier %d",
888 fwnode_handle_put(bridge
->asd
.match
.fwnode
);
891 bridge
->notifier
.ops
= &mipid02_notifier_ops
;
893 ret
= v4l2_async_subdev_notifier_register(&bridge
->sd
,
896 v4l2_async_notifier_cleanup(&bridge
->notifier
);
901 of_node_put(ep_node
);
907 static int mipid02_parse_tx_ep(struct mipid02_dev
*bridge
)
909 struct v4l2_fwnode_endpoint ep
= { .bus_type
= V4L2_MBUS_PARALLEL
};
910 struct i2c_client
*client
= bridge
->i2c_client
;
911 struct device_node
*ep_node
;
914 /* parse tx (endpoint 2) */
915 ep_node
= of_graph_get_endpoint_by_regs(bridge
->i2c_client
->dev
.of_node
,
918 dev_err(&client
->dev
, "unable to find port1 ep");
923 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node
), &ep
);
925 dev_err(&client
->dev
, "Could not parse v4l2 endpoint\n");
926 goto error_of_node_put
;
929 of_node_put(ep_node
);
935 of_node_put(ep_node
);
941 static int mipid02_probe(struct i2c_client
*client
)
943 struct device
*dev
= &client
->dev
;
944 struct mipid02_dev
*bridge
;
948 bridge
= devm_kzalloc(dev
, sizeof(*bridge
), GFP_KERNEL
);
952 init_format(&bridge
->fmt
);
954 bridge
->i2c_client
= client
;
955 v4l2_i2c_subdev_init(&bridge
->sd
, client
, &mipid02_subdev_ops
);
957 /* got and check clock */
958 bridge
->xclk
= devm_clk_get(dev
, "xclk");
959 if (IS_ERR(bridge
->xclk
)) {
960 dev_err(dev
, "failed to get xclk\n");
961 return PTR_ERR(bridge
->xclk
);
964 clk_freq
= clk_get_rate(bridge
->xclk
);
965 if (clk_freq
< 6000000 || clk_freq
> 27000000) {
966 dev_err(dev
, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
971 bridge
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
974 if (IS_ERR(bridge
->reset_gpio
)) {
975 dev_err(dev
, "failed to get reset GPIO\n");
976 return PTR_ERR(bridge
->reset_gpio
);
979 ret
= mipid02_get_regulators(bridge
);
981 dev_err(dev
, "failed to get regulators %d", ret
);
985 mutex_init(&bridge
->lock
);
986 bridge
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
987 bridge
->sd
.entity
.function
= MEDIA_ENT_F_VID_IF_BRIDGE
;
988 bridge
->sd
.entity
.ops
= &mipid02_subdev_entity_ops
;
989 bridge
->pad
[0].flags
= MEDIA_PAD_FL_SINK
;
990 bridge
->pad
[1].flags
= MEDIA_PAD_FL_SINK
;
991 bridge
->pad
[2].flags
= MEDIA_PAD_FL_SOURCE
;
992 ret
= media_entity_pads_init(&bridge
->sd
.entity
, MIPID02_PAD_NB
,
995 dev_err(&client
->dev
, "pads init failed %d", ret
);
999 /* enable clock, power and reset device if available */
1000 ret
= mipid02_set_power_on(bridge
);
1002 goto entity_cleanup
;
1004 ret
= mipid02_detect(bridge
);
1006 dev_err(&client
->dev
, "failed to detect mipid02 %d", ret
);
1010 ret
= mipid02_parse_tx_ep(bridge
);
1012 dev_err(&client
->dev
, "failed to parse tx %d", ret
);
1016 ret
= mipid02_parse_rx_ep(bridge
);
1018 dev_err(&client
->dev
, "failed to parse rx %d", ret
);
1022 ret
= v4l2_async_register_subdev(&bridge
->sd
);
1024 dev_err(&client
->dev
, "v4l2_async_register_subdev failed %d",
1026 goto unregister_notifier
;
1029 dev_info(&client
->dev
, "mipid02 device probe successfully");
1033 unregister_notifier
:
1034 v4l2_async_notifier_unregister(&bridge
->notifier
);
1035 v4l2_async_notifier_cleanup(&bridge
->notifier
);
1037 mipid02_set_power_off(bridge
);
1039 media_entity_cleanup(&bridge
->sd
.entity
);
1041 mutex_destroy(&bridge
->lock
);
1046 static int mipid02_remove(struct i2c_client
*client
)
1048 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1049 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
1051 v4l2_async_notifier_unregister(&bridge
->notifier
);
1052 v4l2_async_notifier_cleanup(&bridge
->notifier
);
1053 v4l2_async_unregister_subdev(&bridge
->sd
);
1054 mipid02_set_power_off(bridge
);
1055 media_entity_cleanup(&bridge
->sd
.entity
);
1056 mutex_destroy(&bridge
->lock
);
1061 static const struct of_device_id mipid02_dt_ids
[] = {
1062 { .compatible
= "st,st-mipid02" },
1065 MODULE_DEVICE_TABLE(of
, mipid02_dt_ids
);
1067 static struct i2c_driver mipid02_i2c_driver
= {
1069 .name
= "st-mipid02",
1070 .of_match_table
= mipid02_dt_ids
,
1072 .probe_new
= mipid02_probe
,
1073 .remove
= mipid02_remove
,
1076 module_i2c_driver(mipid02_i2c_driver
);
1078 MODULE_AUTHOR("Mickael Guene <mickael.guene@st.com>");
1079 MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
1080 MODULE_LICENSE("GPL v2");