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/pm_runtime.h>
18 #include <linux/of_graph.h>
19 #include <linux/regulator/consumer.h>
20 #include <media/mipi-csi2.h>
21 #include <media/v4l2-async.h>
22 #include <media/v4l2-cci.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-fwnode.h>
26 #include <media/v4l2-subdev.h>
28 #define MIPID02_CLK_LANE_WR_REG1 CCI_REG8(0x01)
29 #define MIPID02_CLK_LANE_REG1 CCI_REG8(0x02)
30 #define MIPID02_CLK_LANE_REG3 CCI_REG8(0x04)
31 #define MIPID02_DATA_LANE0_REG1 CCI_REG8(0x05)
32 #define MIPID02_DATA_LANE0_REG2 CCI_REG8(0x06)
33 #define MIPID02_DATA_LANE1_REG1 CCI_REG8(0x09)
34 #define MIPID02_DATA_LANE1_REG2 CCI_REG8(0x0a)
35 #define MIPID02_MODE_REG1 CCI_REG8(0x14)
36 #define MIPID02_MODE_REG2 CCI_REG8(0x15)
37 #define MIPID02_DATA_ID_RREG CCI_REG8(0x17)
38 #define MIPID02_DATA_SELECTION_CTRL CCI_REG8(0x19)
39 #define MIPID02_PIX_WIDTH_CTRL CCI_REG8(0x1e)
40 #define MIPID02_PIX_WIDTH_CTRL_EMB CCI_REG8(0x1f)
42 /* Bits definition for MIPID02_CLK_LANE_REG1 */
43 #define CLK_ENABLE BIT(0)
44 /* Bits definition for MIPID02_CLK_LANE_REG3 */
45 #define CLK_MIPI_CSI BIT(1)
46 /* Bits definition for MIPID02_DATA_LANE0_REG1 */
47 #define DATA_ENABLE BIT(0)
48 /* Bits definition for MIPID02_DATA_LANEx_REG2 */
49 #define DATA_MIPI_CSI BIT(0)
50 /* Bits definition for MIPID02_MODE_REG1 */
51 #define MODE_DATA_SWAP BIT(2)
52 #define MODE_NO_BYPASS BIT(6)
53 /* Bits definition for MIPID02_MODE_REG2 */
54 #define MODE_HSYNC_ACTIVE_HIGH BIT(1)
55 #define MODE_VSYNC_ACTIVE_HIGH BIT(2)
56 #define MODE_PCLK_SAMPLE_RISING BIT(3)
57 /* Bits definition for MIPID02_DATA_SELECTION_CTRL */
58 #define SELECTION_MANUAL_DATA BIT(2)
59 #define SELECTION_MANUAL_WIDTH BIT(3)
61 static const u32 mipid02_supported_fmt_codes
[] = {
62 MEDIA_BUS_FMT_SBGGR8_1X8
, MEDIA_BUS_FMT_SGBRG8_1X8
,
63 MEDIA_BUS_FMT_SGRBG8_1X8
, MEDIA_BUS_FMT_SRGGB8_1X8
,
64 MEDIA_BUS_FMT_SBGGR10_1X10
, MEDIA_BUS_FMT_SGBRG10_1X10
,
65 MEDIA_BUS_FMT_SGRBG10_1X10
, MEDIA_BUS_FMT_SRGGB10_1X10
,
66 MEDIA_BUS_FMT_SBGGR12_1X12
, MEDIA_BUS_FMT_SGBRG12_1X12
,
67 MEDIA_BUS_FMT_SGRBG12_1X12
, MEDIA_BUS_FMT_SRGGB12_1X12
,
68 MEDIA_BUS_FMT_YUYV8_1X16
, MEDIA_BUS_FMT_YVYU8_1X16
,
69 MEDIA_BUS_FMT_UYVY8_1X16
, MEDIA_BUS_FMT_VYUY8_1X16
,
70 MEDIA_BUS_FMT_RGB565_1X16
, MEDIA_BUS_FMT_BGR888_1X24
,
71 MEDIA_BUS_FMT_Y8_1X8
, MEDIA_BUS_FMT_JPEG_1X8
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 */
80 #define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
82 #define MIPID02_SINK_0 0
83 #define MIPID02_SINK_1 1
84 #define MIPID02_SOURCE 2
85 #define MIPID02_PAD_NB 3
88 struct i2c_client
*i2c_client
;
89 struct regulator_bulk_data supplies
[MIPID02_NUM_SUPPLIES
];
90 struct v4l2_subdev sd
;
91 struct regmap
*regmap
;
92 struct media_pad pad
[MIPID02_PAD_NB
];
94 struct gpio_desc
*reset_gpio
;
96 struct v4l2_fwnode_endpoint rx
;
97 struct v4l2_fwnode_endpoint tx
;
99 struct v4l2_async_notifier notifier
;
100 struct v4l2_subdev
*s_subdev
;
109 u8 data_selection_ctrl
;
112 u8 pix_width_ctrl_emb
;
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
:
123 case MEDIA_BUS_FMT_Y8_1X8
:
125 case MEDIA_BUS_FMT_SBGGR10_1X10
:
126 case MEDIA_BUS_FMT_SGBRG10_1X10
:
127 case MEDIA_BUS_FMT_SGRBG10_1X10
:
128 case MEDIA_BUS_FMT_SRGGB10_1X10
:
130 case MEDIA_BUS_FMT_SBGGR12_1X12
:
131 case MEDIA_BUS_FMT_SGBRG12_1X12
:
132 case MEDIA_BUS_FMT_SGRBG12_1X12
:
133 case MEDIA_BUS_FMT_SRGGB12_1X12
:
135 case MEDIA_BUS_FMT_YUYV8_1X16
:
136 case MEDIA_BUS_FMT_YVYU8_1X16
:
137 case MEDIA_BUS_FMT_UYVY8_1X16
:
138 case MEDIA_BUS_FMT_VYUY8_1X16
:
139 case MEDIA_BUS_FMT_RGB565_1X16
:
141 case MEDIA_BUS_FMT_BGR888_1X24
:
148 static u8
data_type_from_code(__u32 code
)
151 case MEDIA_BUS_FMT_SBGGR8_1X8
:
152 case MEDIA_BUS_FMT_SGBRG8_1X8
:
153 case MEDIA_BUS_FMT_SGRBG8_1X8
:
154 case MEDIA_BUS_FMT_SRGGB8_1X8
:
155 case MEDIA_BUS_FMT_Y8_1X8
:
156 return MIPI_CSI2_DT_RAW8
;
157 case MEDIA_BUS_FMT_SBGGR10_1X10
:
158 case MEDIA_BUS_FMT_SGBRG10_1X10
:
159 case MEDIA_BUS_FMT_SGRBG10_1X10
:
160 case MEDIA_BUS_FMT_SRGGB10_1X10
:
161 return MIPI_CSI2_DT_RAW10
;
162 case MEDIA_BUS_FMT_SBGGR12_1X12
:
163 case MEDIA_BUS_FMT_SGBRG12_1X12
:
164 case MEDIA_BUS_FMT_SGRBG12_1X12
:
165 case MEDIA_BUS_FMT_SRGGB12_1X12
:
166 return MIPI_CSI2_DT_RAW12
;
167 case MEDIA_BUS_FMT_YUYV8_1X16
:
168 case MEDIA_BUS_FMT_YVYU8_1X16
:
169 case MEDIA_BUS_FMT_UYVY8_1X16
:
170 case MEDIA_BUS_FMT_VYUY8_1X16
:
171 return MIPI_CSI2_DT_YUV422_8B
;
172 case MEDIA_BUS_FMT_BGR888_1X24
:
173 return MIPI_CSI2_DT_RGB888
;
174 case MEDIA_BUS_FMT_RGB565_1X16
:
175 return MIPI_CSI2_DT_RGB565
;
181 static __u32
get_fmt_code(__u32 code
)
185 for (i
= 0; i
< ARRAY_SIZE(mipid02_supported_fmt_codes
); i
++) {
186 if (code
== mipid02_supported_fmt_codes
[i
])
190 return mipid02_supported_fmt_codes
[0];
193 static __u32
serial_to_parallel_code(__u32 serial
)
195 if (serial
== MEDIA_BUS_FMT_RGB565_1X16
)
196 return MEDIA_BUS_FMT_RGB565_2X8_LE
;
197 if (serial
== MEDIA_BUS_FMT_YUYV8_1X16
)
198 return MEDIA_BUS_FMT_YUYV8_2X8
;
199 if (serial
== MEDIA_BUS_FMT_YVYU8_1X16
)
200 return MEDIA_BUS_FMT_YVYU8_2X8
;
201 if (serial
== MEDIA_BUS_FMT_UYVY8_1X16
)
202 return MEDIA_BUS_FMT_UYVY8_2X8
;
203 if (serial
== MEDIA_BUS_FMT_VYUY8_1X16
)
204 return MEDIA_BUS_FMT_VYUY8_2X8
;
205 if (serial
== MEDIA_BUS_FMT_BGR888_1X24
)
206 return MEDIA_BUS_FMT_BGR888_3X8
;
211 static inline struct mipid02_dev
*to_mipid02_dev(struct v4l2_subdev
*sd
)
213 return container_of(sd
, struct mipid02_dev
, sd
);
216 static int mipid02_get_regulators(struct mipid02_dev
*bridge
)
220 for (i
= 0; i
< MIPID02_NUM_SUPPLIES
; i
++)
221 bridge
->supplies
[i
].supply
= mipid02_supply_name
[i
];
223 return devm_regulator_bulk_get(&bridge
->i2c_client
->dev
,
224 MIPID02_NUM_SUPPLIES
,
228 static void mipid02_apply_reset(struct mipid02_dev
*bridge
)
230 gpiod_set_value_cansleep(bridge
->reset_gpio
, 0);
231 usleep_range(5000, 10000);
232 gpiod_set_value_cansleep(bridge
->reset_gpio
, 1);
233 usleep_range(5000, 10000);
234 gpiod_set_value_cansleep(bridge
->reset_gpio
, 0);
235 usleep_range(5000, 10000);
238 static int mipid02_set_power_on(struct device
*dev
)
240 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
241 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
242 struct i2c_client
*client
= bridge
->i2c_client
;
245 ret
= clk_prepare_enable(bridge
->xclk
);
247 dev_err(&client
->dev
, "%s: failed to enable clock\n", __func__
);
251 ret
= regulator_bulk_enable(MIPID02_NUM_SUPPLIES
,
254 dev_err(&client
->dev
, "%s: failed to enable regulators\n",
259 if (bridge
->reset_gpio
) {
260 dev_dbg(&client
->dev
, "apply reset");
261 mipid02_apply_reset(bridge
);
263 dev_dbg(&client
->dev
, "don't apply reset");
264 usleep_range(5000, 10000);
270 clk_disable_unprepare(bridge
->xclk
);
274 static int mipid02_set_power_off(struct device
*dev
)
276 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
277 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
279 regulator_bulk_disable(MIPID02_NUM_SUPPLIES
, bridge
->supplies
);
280 clk_disable_unprepare(bridge
->xclk
);
285 static int mipid02_detect(struct mipid02_dev
*bridge
)
290 * There is no version registers. Just try to read register
291 * MIPID02_CLK_LANE_WR_REG1.
293 return cci_read(bridge
->regmap
, MIPID02_CLK_LANE_WR_REG1
, ®
, NULL
);
297 * We need to know link frequency to setup clk_lane_reg1 timings. Link frequency
298 * will be retrieve from connected device via v4l2_get_link_freq, bit per pixel
299 * and number of lanes.
301 static int mipid02_configure_from_rx_speed(struct mipid02_dev
*bridge
,
302 struct v4l2_mbus_framefmt
*fmt
)
304 struct i2c_client
*client
= bridge
->i2c_client
;
305 struct v4l2_subdev
*subdev
= bridge
->s_subdev
;
306 struct v4l2_fwnode_endpoint
*ep
= &bridge
->rx
;
307 u32 bpp
= bpp_from_code(fmt
->code
);
309 * clk_lane_reg1 requires 4 times the unit interval time, and bitrate
310 * is twice the link frequency, hence ui_4 = 1000000000 * 4 / 2
312 u64 ui_4
= 2000000000;
315 link_freq
= v4l2_get_link_freq(subdev
->ctrl_handler
, bpp
,
316 2 * ep
->bus
.mipi_csi2
.num_data_lanes
);
318 dev_err(&client
->dev
, "Failed to get link frequency");
322 dev_dbg(&client
->dev
, "detect link_freq = %lld Hz", link_freq
);
323 ui_4
= div64_u64(ui_4
, link_freq
);
324 bridge
->r
.clk_lane_reg1
|= ui_4
<< 2;
329 static int mipid02_configure_clk_lane(struct mipid02_dev
*bridge
)
331 struct i2c_client
*client
= bridge
->i2c_client
;
332 struct v4l2_fwnode_endpoint
*ep
= &bridge
->rx
;
333 bool *polarities
= ep
->bus
.mipi_csi2
.lane_polarities
;
335 /* midid02 doesn't support clock lane remapping */
336 if (ep
->bus
.mipi_csi2
.clock_lane
!= 0) {
337 dev_err(&client
->dev
, "clk lane must be map to lane 0\n");
340 bridge
->r
.clk_lane_reg1
|= (polarities
[0] << 1) | CLK_ENABLE
;
345 static int mipid02_configure_data0_lane(struct mipid02_dev
*bridge
, int nb
,
346 bool are_lanes_swap
, bool *polarities
)
348 bool are_pin_swap
= are_lanes_swap
? polarities
[2] : polarities
[1];
350 if (nb
== 1 && are_lanes_swap
)
354 * data lane 0 as pin swap polarity reversed compared to clock and
358 bridge
->r
.data_lane0_reg1
= 1 << 1;
359 bridge
->r
.data_lane0_reg1
|= DATA_ENABLE
;
364 static int mipid02_configure_data1_lane(struct mipid02_dev
*bridge
, int nb
,
365 bool are_lanes_swap
, bool *polarities
)
367 bool are_pin_swap
= are_lanes_swap
? polarities
[1] : polarities
[2];
369 if (nb
== 1 && !are_lanes_swap
)
373 bridge
->r
.data_lane1_reg1
= 1 << 1;
374 bridge
->r
.data_lane1_reg1
|= DATA_ENABLE
;
379 static int mipid02_configure_from_rx(struct mipid02_dev
*bridge
,
380 struct v4l2_mbus_framefmt
*fmt
)
382 struct v4l2_fwnode_endpoint
*ep
= &bridge
->rx
;
383 bool are_lanes_swap
= ep
->bus
.mipi_csi2
.data_lanes
[0] == 2;
384 bool *polarities
= ep
->bus
.mipi_csi2
.lane_polarities
;
385 int nb
= ep
->bus
.mipi_csi2
.num_data_lanes
;
388 ret
= mipid02_configure_clk_lane(bridge
);
392 ret
= mipid02_configure_data0_lane(bridge
, nb
, are_lanes_swap
,
397 ret
= mipid02_configure_data1_lane(bridge
, nb
, are_lanes_swap
,
402 bridge
->r
.mode_reg1
|= are_lanes_swap
? MODE_DATA_SWAP
: 0;
403 bridge
->r
.mode_reg1
|= (nb
- 1) << 1;
405 return mipid02_configure_from_rx_speed(bridge
, fmt
);
408 static int mipid02_configure_from_tx(struct mipid02_dev
*bridge
)
410 struct v4l2_fwnode_endpoint
*ep
= &bridge
->tx
;
412 bridge
->r
.data_selection_ctrl
= SELECTION_MANUAL_WIDTH
;
413 bridge
->r
.pix_width_ctrl
= ep
->bus
.parallel
.bus_width
;
414 bridge
->r
.pix_width_ctrl_emb
= ep
->bus
.parallel
.bus_width
;
415 if (ep
->bus
.parallel
.flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
)
416 bridge
->r
.mode_reg2
|= MODE_HSYNC_ACTIVE_HIGH
;
417 if (ep
->bus
.parallel
.flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
)
418 bridge
->r
.mode_reg2
|= MODE_VSYNC_ACTIVE_HIGH
;
419 if (ep
->bus
.parallel
.flags
& V4L2_MBUS_PCLK_SAMPLE_RISING
)
420 bridge
->r
.mode_reg2
|= MODE_PCLK_SAMPLE_RISING
;
425 static int mipid02_configure_from_code(struct mipid02_dev
*bridge
,
426 struct v4l2_mbus_framefmt
*fmt
)
430 bridge
->r
.data_id_rreg
= 0;
432 if (fmt
->code
!= MEDIA_BUS_FMT_JPEG_1X8
) {
433 bridge
->r
.data_selection_ctrl
|= SELECTION_MANUAL_DATA
;
435 data_type
= data_type_from_code(fmt
->code
);
438 bridge
->r
.data_id_rreg
= data_type
;
444 static int mipid02_disable_streams(struct v4l2_subdev
*sd
,
445 struct v4l2_subdev_state
*state
, u32 pad
,
448 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
449 struct i2c_client
*client
= bridge
->i2c_client
;
452 if (!bridge
->s_subdev
)
455 ret
= v4l2_subdev_disable_streams(bridge
->s_subdev
,
456 bridge
->s_subdev_pad_id
, BIT(0));
460 /* Disable all lanes */
461 cci_write(bridge
->regmap
, MIPID02_CLK_LANE_REG1
, 0, &ret
);
462 cci_write(bridge
->regmap
, MIPID02_DATA_LANE0_REG1
, 0, &ret
);
463 cci_write(bridge
->regmap
, MIPID02_DATA_LANE1_REG1
, 0, &ret
);
467 pm_runtime_mark_last_busy(&client
->dev
);
468 pm_runtime_put_autosuspend(&client
->dev
);
472 dev_err(&client
->dev
, "failed to stream off %d", ret
);
477 static int mipid02_enable_streams(struct v4l2_subdev
*sd
,
478 struct v4l2_subdev_state
*state
, u32 pad
,
481 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
482 struct i2c_client
*client
= bridge
->i2c_client
;
483 struct v4l2_mbus_framefmt
*fmt
;
486 if (!bridge
->s_subdev
)
489 memset(&bridge
->r
, 0, sizeof(bridge
->r
));
491 fmt
= v4l2_subdev_state_get_format(state
, MIPID02_SINK_0
);
493 /* build registers content */
494 ret
= mipid02_configure_from_rx(bridge
, fmt
);
497 ret
= mipid02_configure_from_tx(bridge
);
500 ret
= mipid02_configure_from_code(bridge
, fmt
);
504 ret
= pm_runtime_resume_and_get(&client
->dev
);
508 /* write mipi registers */
509 cci_write(bridge
->regmap
, MIPID02_CLK_LANE_REG1
,
510 bridge
->r
.clk_lane_reg1
, &ret
);
511 cci_write(bridge
->regmap
, MIPID02_CLK_LANE_REG3
, CLK_MIPI_CSI
, &ret
);
512 cci_write(bridge
->regmap
, MIPID02_DATA_LANE0_REG1
,
513 bridge
->r
.data_lane0_reg1
, &ret
);
514 cci_write(bridge
->regmap
, MIPID02_DATA_LANE0_REG2
, DATA_MIPI_CSI
, &ret
);
515 cci_write(bridge
->regmap
, MIPID02_DATA_LANE1_REG1
,
516 bridge
->r
.data_lane1_reg1
, &ret
);
517 cci_write(bridge
->regmap
, MIPID02_DATA_LANE1_REG2
, DATA_MIPI_CSI
, &ret
);
518 cci_write(bridge
->regmap
, MIPID02_MODE_REG1
,
519 MODE_NO_BYPASS
| bridge
->r
.mode_reg1
, &ret
);
520 cci_write(bridge
->regmap
, MIPID02_MODE_REG2
, bridge
->r
.mode_reg2
, &ret
);
521 cci_write(bridge
->regmap
, MIPID02_DATA_ID_RREG
, bridge
->r
.data_id_rreg
,
523 cci_write(bridge
->regmap
, MIPID02_DATA_SELECTION_CTRL
,
524 bridge
->r
.data_selection_ctrl
, &ret
);
525 cci_write(bridge
->regmap
, MIPID02_PIX_WIDTH_CTRL
,
526 bridge
->r
.pix_width_ctrl
, &ret
);
527 cci_write(bridge
->regmap
, MIPID02_PIX_WIDTH_CTRL_EMB
,
528 bridge
->r
.pix_width_ctrl_emb
, &ret
);
532 ret
= v4l2_subdev_enable_streams(bridge
->s_subdev
,
533 bridge
->s_subdev_pad_id
, BIT(0));
540 cci_write(bridge
->regmap
, MIPID02_CLK_LANE_REG1
, 0, &ret
);
541 cci_write(bridge
->regmap
, MIPID02_DATA_LANE0_REG1
, 0, &ret
);
542 cci_write(bridge
->regmap
, MIPID02_DATA_LANE1_REG1
, 0, &ret
);
544 pm_runtime_mark_last_busy(&client
->dev
);
545 pm_runtime_put_autosuspend(&client
->dev
);
549 static const struct v4l2_mbus_framefmt default_fmt
= {
550 .code
= MEDIA_BUS_FMT_SBGGR8_1X8
,
551 .field
= V4L2_FIELD_NONE
,
552 .colorspace
= V4L2_COLORSPACE_SRGB
,
553 .ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
,
554 .quantization
= V4L2_QUANTIZATION_FULL_RANGE
,
555 .xfer_func
= V4L2_XFER_FUNC_DEFAULT
,
560 static int mipid02_init_state(struct v4l2_subdev
*sd
,
561 struct v4l2_subdev_state
*state
)
563 *v4l2_subdev_state_get_format(state
, MIPID02_SINK_0
) = default_fmt
;
564 /* MIPID02_SINK_1 isn't supported yet */
565 *v4l2_subdev_state_get_format(state
, MIPID02_SOURCE
) = default_fmt
;
570 static int mipid02_enum_mbus_code(struct v4l2_subdev
*sd
,
571 struct v4l2_subdev_state
*sd_state
,
572 struct v4l2_subdev_mbus_code_enum
*code
)
574 struct v4l2_mbus_framefmt
*sink_fmt
;
579 if (code
->index
>= ARRAY_SIZE(mipid02_supported_fmt_codes
))
582 code
->code
= mipid02_supported_fmt_codes
[code
->index
];
585 if (code
->index
== 0) {
586 sink_fmt
= v4l2_subdev_state_get_format(sd_state
,
588 code
->code
= serial_to_parallel_code(sink_fmt
->code
);
600 static int mipid02_set_fmt(struct v4l2_subdev
*sd
,
601 struct v4l2_subdev_state
*sd_state
,
602 struct v4l2_subdev_format
*fmt
)
604 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
605 struct i2c_client
*client
= bridge
->i2c_client
;
606 struct v4l2_mbus_framefmt
*pad_fmt
;
608 dev_dbg(&client
->dev
, "%s for %d", __func__
, fmt
->pad
);
610 /* second CSI-2 pad not yet supported */
611 if (fmt
->pad
== MIPID02_SINK_1
)
614 pad_fmt
= v4l2_subdev_state_get_format(sd_state
, fmt
->pad
);
615 fmt
->format
.code
= get_fmt_code(fmt
->format
.code
);
617 /* code may need to be converted */
618 if (fmt
->pad
== MIPID02_SOURCE
)
619 fmt
->format
.code
= serial_to_parallel_code(fmt
->format
.code
);
621 *pad_fmt
= fmt
->format
;
623 /* Propagate the format to the source pad in case of sink pad update */
624 if (fmt
->pad
== MIPID02_SINK_0
) {
625 pad_fmt
= v4l2_subdev_state_get_format(sd_state
,
627 *pad_fmt
= fmt
->format
;
628 pad_fmt
->code
= serial_to_parallel_code(fmt
->format
.code
);
634 static const struct v4l2_subdev_video_ops mipid02_video_ops
= {
635 .s_stream
= v4l2_subdev_s_stream_helper
,
638 static const struct v4l2_subdev_pad_ops mipid02_pad_ops
= {
639 .enum_mbus_code
= mipid02_enum_mbus_code
,
640 .get_fmt
= v4l2_subdev_get_fmt
,
641 .set_fmt
= mipid02_set_fmt
,
642 .enable_streams
= mipid02_enable_streams
,
643 .disable_streams
= mipid02_disable_streams
,
646 static const struct v4l2_subdev_ops mipid02_subdev_ops
= {
647 .video
= &mipid02_video_ops
,
648 .pad
= &mipid02_pad_ops
,
651 static const struct v4l2_subdev_internal_ops mipid02_subdev_internal_ops
= {
652 .init_state
= mipid02_init_state
,
655 static const struct media_entity_operations mipid02_subdev_entity_ops
= {
656 .link_validate
= v4l2_subdev_link_validate
,
659 static int mipid02_async_bound(struct v4l2_async_notifier
*notifier
,
660 struct v4l2_subdev
*s_subdev
,
661 struct v4l2_async_connection
*asd
)
663 struct mipid02_dev
*bridge
= to_mipid02_dev(notifier
->sd
);
664 struct i2c_client
*client
= bridge
->i2c_client
;
668 dev_dbg(&client
->dev
, "sensor_async_bound call %p", s_subdev
);
670 source_pad
= media_entity_get_fwnode_pad(&s_subdev
->entity
,
672 MEDIA_PAD_FL_SOURCE
);
673 if (source_pad
< 0) {
674 dev_err(&client
->dev
, "Couldn't find output pad for subdev %s\n",
679 ret
= media_create_pad_link(&s_subdev
->entity
, source_pad
,
680 &bridge
->sd
.entity
, 0,
681 MEDIA_LNK_FL_ENABLED
|
682 MEDIA_LNK_FL_IMMUTABLE
);
684 dev_err(&client
->dev
, "Couldn't create media link %d", ret
);
688 bridge
->s_subdev
= s_subdev
;
689 bridge
->s_subdev_pad_id
= source_pad
;
694 static void mipid02_async_unbind(struct v4l2_async_notifier
*notifier
,
695 struct v4l2_subdev
*s_subdev
,
696 struct v4l2_async_connection
*asd
)
698 struct mipid02_dev
*bridge
= to_mipid02_dev(notifier
->sd
);
700 bridge
->s_subdev
= NULL
;
703 static const struct v4l2_async_notifier_operations mipid02_notifier_ops
= {
704 .bound
= mipid02_async_bound
,
705 .unbind
= mipid02_async_unbind
,
708 static int mipid02_parse_rx_ep(struct mipid02_dev
*bridge
)
710 struct v4l2_fwnode_endpoint ep
= { .bus_type
= V4L2_MBUS_CSI2_DPHY
};
711 struct i2c_client
*client
= bridge
->i2c_client
;
712 struct v4l2_async_connection
*asd
;
713 struct device_node
*ep_node
;
716 /* parse rx (endpoint 0) */
717 ep_node
= of_graph_get_endpoint_by_regs(bridge
->i2c_client
->dev
.of_node
,
720 dev_err(&client
->dev
, "unable to find port0 ep");
725 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node
), &ep
);
727 dev_err(&client
->dev
, "Could not parse v4l2 endpoint %d\n",
729 goto error_of_node_put
;
732 /* do some sanity checks */
733 if (ep
.bus
.mipi_csi2
.num_data_lanes
> 2) {
734 dev_err(&client
->dev
, "max supported data lanes is 2 / got %d",
735 ep
.bus
.mipi_csi2
.num_data_lanes
);
737 goto error_of_node_put
;
740 /* register it for later use */
743 /* register async notifier so we get noticed when sensor is connected */
744 v4l2_async_subdev_nf_init(&bridge
->notifier
, &bridge
->sd
);
745 asd
= v4l2_async_nf_add_fwnode_remote(&bridge
->notifier
,
746 of_fwnode_handle(ep_node
),
747 struct v4l2_async_connection
);
748 of_node_put(ep_node
);
751 dev_err(&client
->dev
, "fail to register asd to notifier %ld",
755 bridge
->notifier
.ops
= &mipid02_notifier_ops
;
757 ret
= v4l2_async_nf_register(&bridge
->notifier
);
759 v4l2_async_nf_cleanup(&bridge
->notifier
);
764 of_node_put(ep_node
);
770 static int mipid02_parse_tx_ep(struct mipid02_dev
*bridge
)
772 struct v4l2_fwnode_endpoint ep
= { .bus_type
= V4L2_MBUS_PARALLEL
};
773 struct i2c_client
*client
= bridge
->i2c_client
;
774 struct device_node
*ep_node
;
777 /* parse tx (endpoint 2) */
778 ep_node
= of_graph_get_endpoint_by_regs(bridge
->i2c_client
->dev
.of_node
,
781 dev_err(&client
->dev
, "unable to find port1 ep");
786 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node
), &ep
);
788 dev_err(&client
->dev
, "Could not parse v4l2 endpoint\n");
789 goto error_of_node_put
;
792 of_node_put(ep_node
);
798 of_node_put(ep_node
);
804 static int mipid02_probe(struct i2c_client
*client
)
806 struct device
*dev
= &client
->dev
;
807 struct mipid02_dev
*bridge
;
811 bridge
= devm_kzalloc(dev
, sizeof(*bridge
), GFP_KERNEL
);
815 bridge
->i2c_client
= client
;
816 v4l2_i2c_subdev_init(&bridge
->sd
, client
, &mipid02_subdev_ops
);
818 /* got and check clock */
819 bridge
->xclk
= devm_clk_get(dev
, "xclk");
820 if (IS_ERR(bridge
->xclk
)) {
821 dev_err(dev
, "failed to get xclk\n");
822 return PTR_ERR(bridge
->xclk
);
825 clk_freq
= clk_get_rate(bridge
->xclk
);
826 if (clk_freq
< 6000000 || clk_freq
> 27000000) {
827 dev_err(dev
, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
832 bridge
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
835 if (IS_ERR(bridge
->reset_gpio
)) {
836 dev_err(dev
, "failed to get reset GPIO\n");
837 return PTR_ERR(bridge
->reset_gpio
);
840 ret
= mipid02_get_regulators(bridge
);
842 dev_err(dev
, "failed to get regulators %d", ret
);
846 /* Initialise the regmap for further cci access */
847 bridge
->regmap
= devm_cci_regmap_init_i2c(client
, 16);
848 if (IS_ERR(bridge
->regmap
))
849 return dev_err_probe(dev
, PTR_ERR(bridge
->regmap
),
850 "failed to get cci regmap\n");
852 bridge
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
853 bridge
->sd
.entity
.function
= MEDIA_ENT_F_VID_IF_BRIDGE
;
854 bridge
->sd
.internal_ops
= &mipid02_subdev_internal_ops
;
855 bridge
->sd
.entity
.ops
= &mipid02_subdev_entity_ops
;
856 bridge
->pad
[0].flags
= MEDIA_PAD_FL_SINK
;
857 bridge
->pad
[1].flags
= MEDIA_PAD_FL_SINK
;
858 bridge
->pad
[2].flags
= MEDIA_PAD_FL_SOURCE
;
859 ret
= media_entity_pads_init(&bridge
->sd
.entity
, MIPID02_PAD_NB
,
862 dev_err(&client
->dev
, "pads init failed %d", ret
);
866 ret
= v4l2_subdev_init_finalize(&bridge
->sd
);
868 dev_err(dev
, "subdev init error: %d\n", ret
);
872 /* enable clock, power and reset device if available */
873 ret
= mipid02_set_power_on(&client
->dev
);
877 ret
= mipid02_detect(bridge
);
879 dev_err(&client
->dev
, "failed to detect mipid02 %d", ret
);
883 ret
= mipid02_parse_tx_ep(bridge
);
885 dev_err(&client
->dev
, "failed to parse tx %d", ret
);
889 ret
= mipid02_parse_rx_ep(bridge
);
891 dev_err(&client
->dev
, "failed to parse rx %d", ret
);
895 /* Enable runtime PM and turn off the device */
896 pm_runtime_set_active(dev
);
897 pm_runtime_get_noresume(&client
->dev
);
898 pm_runtime_enable(dev
);
900 pm_runtime_set_autosuspend_delay(&client
->dev
, 1000);
901 pm_runtime_use_autosuspend(&client
->dev
);
902 pm_runtime_put_autosuspend(&client
->dev
);
904 ret
= v4l2_async_register_subdev(&bridge
->sd
);
906 dev_err(&client
->dev
, "v4l2_async_register_subdev failed %d",
908 goto unregister_notifier
;
911 dev_info(&client
->dev
, "mipid02 device probe successfully");
916 v4l2_async_nf_unregister(&bridge
->notifier
);
917 v4l2_async_nf_cleanup(&bridge
->notifier
);
918 pm_runtime_disable(&client
->dev
);
919 pm_runtime_set_suspended(&client
->dev
);
921 mipid02_set_power_off(&client
->dev
);
923 media_entity_cleanup(&bridge
->sd
.entity
);
928 static void mipid02_remove(struct i2c_client
*client
)
930 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
931 struct mipid02_dev
*bridge
= to_mipid02_dev(sd
);
933 v4l2_async_nf_unregister(&bridge
->notifier
);
934 v4l2_async_nf_cleanup(&bridge
->notifier
);
935 v4l2_async_unregister_subdev(&bridge
->sd
);
937 pm_runtime_disable(&client
->dev
);
938 if (!pm_runtime_status_suspended(&client
->dev
))
939 mipid02_set_power_off(&client
->dev
);
940 pm_runtime_set_suspended(&client
->dev
);
941 media_entity_cleanup(&bridge
->sd
.entity
);
944 static const struct of_device_id mipid02_dt_ids
[] = {
945 { .compatible
= "st,st-mipid02" },
948 MODULE_DEVICE_TABLE(of
, mipid02_dt_ids
);
950 static const struct dev_pm_ops mipid02_pm_ops
= {
951 RUNTIME_PM_OPS(mipid02_set_power_off
, mipid02_set_power_on
, NULL
)
954 static struct i2c_driver mipid02_i2c_driver
= {
956 .name
= "st-mipid02",
957 .of_match_table
= mipid02_dt_ids
,
958 .pm
= pm_ptr(&mipid02_pm_ops
),
960 .probe
= mipid02_probe
,
961 .remove
= mipid02_remove
,
964 module_i2c_driver(mipid02_i2c_driver
);
966 MODULE_AUTHOR("Mickael Guene <mickael.guene@st.com>");
967 MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
968 MODULE_LICENSE("GPL v2");