2019-03-12 06:46:19

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v1 0/3] Add support for MIPID02 CSI-2 to PARALLEL bridge I2C device

This patchset introduces support of STMicroelectronics MIPID02 CSI-2 to PARALLEL
I2C bridge. It allows using a CSI-2 sensor with a PARALLEL interface.
Current driver implementation doesn't support CSI-2 second input port usage. It
doesn't support also YUV420, RGB565 and RGB444 input formats.


Mickael Guene (3):
dt-bindings: Document MIPID02 bindings
media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver
media: MAINTAINERS: add entry for STMicroelectronics MIPID02 media
driver

.../bindings/media/i2c/st,st-mipid02.txt | 69 ++
MAINTAINERS | 8 +
drivers/media/i2c/Kconfig | 13 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/st-mipid02.c | 878 +++++++++++++++++++++
5 files changed, 969 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
create mode 100644 drivers/media/i2c/st-mipid02.c

--
2.7.4



2019-03-12 06:46:06

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v1 1/3] dt-bindings: Document MIPID02 bindings

This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
bridge.

Signed-off-by: Mickael Guene <[email protected]>
---

.../bindings/media/i2c/st,st-mipid02.txt | 69 ++++++++++++++++++++++
1 file changed, 69 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt

diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
new file mode 100644
index 0000000..a1855da
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
@@ -0,0 +1,69 @@
+STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
+
+MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
+time. Active port input stream will be de-serialized and its content outputted
+through PARALLEL output port.
+CSI-2 first input port is a dual lane 800Mbps whereas CSI-2 second input port is
+a single lane 800Mbps. Both ports support clock and data lane polarity swap.
+First port also supports data lane swap.
+PARALLEL output port has a maximum width of 12 bits.
+Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
+YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
+
+Required Properties:
+- compatible: should be "st,st-mipid02"
+- clocks: reference to the xclk input clock.
+- clock-names: should be "xclk".
+- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
+- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
+
+Optional Properties:
+- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
+ This is an active low signal to the mipid02.
+
+Required subnodes:
+ - ports: A ports node with one port child node per device input and output
+ port, in accordance with the video interface bindings defined in
+ Documentation/devicetree/bindings/media/video-interfaces.txt. The
+ port nodes are numbered as follows:
+
+ Port Description
+ -----------------------------
+ 0 CSI-2 first input port
+ 1 CSI-2 second input port
+ 2 PARALLEL output
+
+Example:
+
+mipid02: mipid02@14 {
+ compatible = "st,st-mipid02";
+ reg = <0x14>;
+ status = "okay";
+ clocks = <&clk_ext_camera_12>;
+ clock-names = "xclk";
+ VDDE-supply = <&vdd>;
+ VDDIN-supply = <&vdd>;
+ ports {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ port@0 {
+ reg = <0>;
+
+ ep0: endpoint {
+ clock-lanes = <0>;
+ data-lanes = <1 2>;
+ remote-endpoint = <&mipi_csi2_in>;
+ };
+ };
+ port@2 {
+ reg = <2>;
+
+ ep2: endpoint {
+ bus-width = <8>;
+ hsync-active = <0>;
+ vsync-active = <0>;
+ remote-endpoint = <&parallel_out>;
+ };
+ };
+ };
+};
--
2.7.4


2019-03-12 06:47:02

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v1 2/3] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

This V4L2 subdev driver enables STMicroelectronics MIPID02 device.

Signed-off-by: Mickael Guene <[email protected]>
---

drivers/media/i2c/Kconfig | 13 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/st-mipid02.c | 878 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 892 insertions(+)
create mode 100644 drivers/media/i2c/st-mipid02.c

diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 6d32f8d..11a2376 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -1132,6 +1132,19 @@ config VIDEO_I2C
To compile this driver as a module, choose M here: the
module will be called video-i2c

+config VIDEO_ST_MIPID02
+ tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
+ depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
+ depends on MEDIA_CAMERA_SUPPORT
+ depends on MEDIA_CONTROLLER
+ select V4L2_FWNODE
+ help
+ Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
+ It is used to allow usage of CSI-2 sensor with PARALLEL port controller.
+
+ To compile this driver as a module, choose M here: the
+ module will be called st-mipid02.
+
endmenu

endif
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index a64fca8..d8ad9da 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
obj-$(CONFIG_VIDEO_IMX274) += imx274.o
obj-$(CONFIG_VIDEO_IMX319) += imx319.o
obj-$(CONFIG_VIDEO_IMX355) += imx355.o
+obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o

obj-$(CONFIG_SDR_MAX2175) += max2175.o
diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
new file mode 100644
index 0000000..d8710fe
--- /dev/null
+++ b/drivers/media/i2c/st-mipid02.c
@@ -0,0 +1,878 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
+ *
+ * Copyright (C) STMicroelectronics SA 2019
+ * Authors: Mickael Guene <[email protected]>
+ * for STMicroelectronics.
+ *
+ *
+ */
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/of_graph.h>
+#include <linux/regulator/consumer.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+
+#define MIPID02_CLK_LANE_WR_REG1 0x01
+#define MIPID02_CLK_LANE_REG1 0x02
+#define MIPID02_CLK_LANE_REG3 0x04
+#define MIPID02_DATA_LANE0_REG1 0x05
+#define MIPID02_DATA_LANE0_REG2 0x06
+#define MIPID02_DATA_LANE1_REG1 0x09
+#define MIPID02_DATA_LANE1_REG2 0x0a
+#define MIPID02_MODE_REG1 0x14
+#define MIPID02_MODE_REG2 0x15
+#define MIPID02_DATA_ID_RREG 0x17
+#define MIPID02_DATA_SELECTION_CTRL 0x19
+#define MIPID02_PIX_WIDTH_CTRL 0x1e
+#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
+
+/* Bits definition for MIPID02_CLK_LANE_REG1 */
+#define CLK_ENABLE BIT(0)
+/* Bits definition for MIPID02_CLK_LANE_REG3 */
+#define CLK_MIPI_CSI BIT(1)
+/* Bits definition for MIPID02_DATA_LANE0_REG1 */
+#define DATA_ENABLE BIT(0)
+/* Bits definition for MIPID02_DATA_LANEx_REG2 */
+#define DATA_MIPI_CSI BIT(0)
+/* Bits definition for MIPID02_MODE_REG1 */
+#define MODE_DATA_SWAP BIT(2)
+#define MODE_NO_BYPASS BIT(6)
+/* Bits definition for MIPID02_MODE_REG2 */
+#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
+#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
+/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
+#define SELECTION_MANUAL_DATA BIT(2)
+#define SELECTION_MANUAL_WIDTH BIT(3)
+
+/* regulator supplies */
+static const char * const mipid02_supply_name[] = {
+ "VDDE", /* 1.8V digital I/O supply */
+ "VDDIN", /* 1V8 voltage regulator supply */
+};
+
+#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
+#define MIPID02_PAD_NB 2
+
+struct mipid02_dev {
+ struct i2c_client *i2c_client;
+ struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
+ struct v4l2_subdev sd;
+ struct media_pad pad[MIPID02_PAD_NB];
+ struct clk *xclk;
+ struct gpio_desc *reset_gpio;
+ /* Protect streaming */
+ struct mutex lock;
+ bool streaming;
+ /* endpoints info */
+ struct v4l2_fwnode_endpoint rx;
+ u64 link_frequency;
+ struct v4l2_fwnode_endpoint tx;
+ /* remote source */
+ struct v4l2_async_subdev asd;
+ struct v4l2_async_notifier notifier;
+ /* registers */
+ struct {
+ u8 clk_lane_reg1;
+ u8 data_lane0_reg1;
+ u8 data_lane1_reg1;
+ u8 mode_reg1;
+ u8 mode_reg2;
+ u8 data_id_rreg;
+ u8 pix_width_ctrl;
+ u8 pix_width_ctrl_emb;
+ } r;
+};
+
+static int bpp_from_code(__u32 code)
+{
+ switch (code) {
+ case MEDIA_BUS_FMT_SBGGR8_1X8:
+ case MEDIA_BUS_FMT_SGBRG8_1X8:
+ case MEDIA_BUS_FMT_SGRBG8_1X8:
+ case MEDIA_BUS_FMT_SRGGB8_1X8:
+ return 8;
+ case MEDIA_BUS_FMT_SBGGR10_1X10:
+ case MEDIA_BUS_FMT_SGBRG10_1X10:
+ case MEDIA_BUS_FMT_SGRBG10_1X10:
+ case MEDIA_BUS_FMT_SRGGB10_1X10:
+ return 10;
+ case MEDIA_BUS_FMT_SBGGR12_1X12:
+ case MEDIA_BUS_FMT_SGBRG12_1X12:
+ case MEDIA_BUS_FMT_SGRBG12_1X12:
+ case MEDIA_BUS_FMT_SRGGB12_1X12:
+ return 12;
+ case MEDIA_BUS_FMT_UYVY8_2X8:
+ return 16;
+ case MEDIA_BUS_FMT_BGR888_1X24:
+ return 24;
+ default:
+ return 0;
+ }
+}
+
+static u8 data_type_from_code(__u32 code)
+{
+ switch (code) {
+ case MEDIA_BUS_FMT_SBGGR8_1X8:
+ case MEDIA_BUS_FMT_SGBRG8_1X8:
+ case MEDIA_BUS_FMT_SGRBG8_1X8:
+ case MEDIA_BUS_FMT_SRGGB8_1X8:
+ return 0x2a;
+ case MEDIA_BUS_FMT_SBGGR10_1X10:
+ case MEDIA_BUS_FMT_SGBRG10_1X10:
+ case MEDIA_BUS_FMT_SGRBG10_1X10:
+ case MEDIA_BUS_FMT_SRGGB10_1X10:
+ return 0x2b;
+ case MEDIA_BUS_FMT_SBGGR12_1X12:
+ case MEDIA_BUS_FMT_SGBRG12_1X12:
+ case MEDIA_BUS_FMT_SGRBG12_1X12:
+ case MEDIA_BUS_FMT_SRGGB12_1X12:
+ return 0x2c;
+ case MEDIA_BUS_FMT_UYVY8_2X8:
+ return 0x1e;
+ case MEDIA_BUS_FMT_BGR888_1X24:
+ return 0x24;
+ default:
+ return 0;
+ }
+}
+
+static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
+{
+ return container_of(sd, struct mipid02_dev, sd);
+}
+
+static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct i2c_msg msg[2];
+ u8 buf[2];
+ int ret;
+
+ buf[0] = reg >> 8;
+ buf[1] = reg & 0xff;
+
+ msg[0].addr = client->addr;
+ msg[0].flags = client->flags;
+ msg[0].buf = buf;
+ msg[0].len = sizeof(buf);
+
+ msg[1].addr = client->addr;
+ msg[1].flags = client->flags | I2C_M_RD;
+ msg[1].buf = val;
+ msg[1].len = 1;
+
+ ret = i2c_transfer(client->adapter, msg, 2);
+ if (ret < 0) {
+ dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
+ __func__, client->addr, reg, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct i2c_msg msg;
+ u8 buf[3];
+ int ret;
+
+ buf[0] = reg >> 8;
+ buf[1] = reg & 0xff;
+ buf[2] = val;
+
+ msg.addr = client->addr;
+ msg.flags = client->flags;
+ msg.buf = buf;
+ msg.len = sizeof(buf);
+
+ ret = i2c_transfer(client->adapter, &msg, 1);
+ if (ret < 0) {
+ dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
+ __func__, reg, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mipid02_get_regulators(struct mipid02_dev *bridge)
+{
+ unsigned int i;
+
+ for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
+ bridge->supplies[i].supply = mipid02_supply_name[i];
+
+ return devm_regulator_bulk_get(&bridge->i2c_client->dev,
+ MIPID02_NUM_SUPPLIES,
+ bridge->supplies);
+}
+
+static void mipid02_apply_reset(struct mipid02_dev *bridge)
+{
+ gpiod_set_value_cansleep(bridge->reset_gpio, 0);
+ usleep_range(5000, 10000);
+ gpiod_set_value_cansleep(bridge->reset_gpio, 1);
+ usleep_range(5000, 10000);
+ gpiod_set_value_cansleep(bridge->reset_gpio, 0);
+}
+
+static int mipid02_set_power_on(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret;
+
+ ret = clk_prepare_enable(bridge->xclk);
+ if (ret) {
+ dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
+ return ret;
+ }
+
+ ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
+ bridge->supplies);
+ if (ret) {
+ dev_err(&client->dev, "%s: failed to enable regulators\n",
+ __func__);
+ goto xclk_off;
+ }
+
+ if (bridge->reset_gpio) {
+ dev_dbg(&client->dev, "apply reset");
+ mipid02_apply_reset(bridge);
+ } else {
+ dev_dbg(&client->dev, "dont apply reset");
+ usleep_range(5000, 10000);
+ }
+
+ return 0;
+
+xclk_off:
+ clk_disable_unprepare(bridge->xclk);
+ return ret;
+}
+
+static void mipid02_set_power_off(struct mipid02_dev *bridge)
+{
+ regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
+ clk_disable_unprepare(bridge->xclk);
+}
+
+static int mipid02_detect(struct mipid02_dev *bridge)
+{
+ u8 reg;
+
+ /*
+ * There is no version registers. Just try to read register
+ * MIPID02_CLK_LANE_WR_REG1.
+ */
+ return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
+}
+
+static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+
+ if (ep->nr_of_link_frequencies != 1)
+ return 0;
+
+ return ep->link_frequencies[0];
+}
+
+static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
+ struct v4l2_subdev *sensor)
+{
+ struct v4l2_ctrl *ctrl;
+
+ ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_LINK_FREQ);
+ if (!ctrl)
+ return 0;
+
+ return v4l2_ctrl_g_ctrl_int64(ctrl);
+}
+
+static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
+ __u32 code,
+ struct v4l2_subdev *sensor)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ struct v4l2_ctrl *ctrl;
+ u32 pixel_clock;
+ u32 bpp = bpp_from_code(code);
+
+ ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_PIXEL_RATE);
+ if (!ctrl)
+ return 0;
+ pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
+
+ return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
+}
+
+/*
+ * We need to know link frequency to setup clk_lane_reg1 timings. So use
+ * following sequence, stop when we got a frequency.
+ * - Get it from device tree. It allow to connect sensor without support of
+ * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
+ * is that is that link frequency must be constant.
+ * - Get it from V4L2_CID_LINK_FREQ.
+ * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
+ * to compute it.
+ */
+static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
+ __u32 code,
+ struct v4l2_subdev *sensor)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ u32 link_freq;
+
+ link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
+ if (link_freq)
+ goto success;
+
+ link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, sensor);
+ if (link_freq)
+ goto success;
+
+ link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, code,
+ sensor);
+ if (link_freq)
+ goto success;
+ dev_err(&client->dev, "Failed to detect link frequency");
+
+ return -EINVAL;
+
+success:
+ dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
+ bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
+
+ return 0;
+}
+
+static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ bool *polarities = ep->bus.mipi_csi2.lane_polarities;
+
+ /* midid02 doesn't support clock lane remapping */
+ if (ep->bus.mipi_csi2.clock_lane != 0) {
+ dev_err(&client->dev, "clk lane must be map to lane 0\n");
+ return -EINVAL;
+ }
+ bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
+ bool are_lanes_swap, bool *polarities)
+{
+ bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
+
+ if (nb == 1 && are_lanes_swap)
+ return 0;
+
+ /*
+ * data lane 0 as pin swap polarity reversed compared to clock and
+ *data lane 1
+ */
+ if (!are_pin_swap)
+ bridge->r.data_lane0_reg1 = 1 << 1;
+ bridge->r.data_lane0_reg1 |= DATA_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
+ bool are_lanes_swap, bool *polarities)
+{
+ bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
+
+ if (nb == 1 && !are_lanes_swap)
+ return 0;
+
+ if (are_pin_swap)
+ bridge->r.data_lane1_reg1 = 1 << 1;
+ bridge->r.data_lane1_reg1 |= DATA_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_from_rx(struct mipid02_dev *bridge, __u32 code,
+ struct v4l2_subdev *sensor)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
+ bool *polarities = ep->bus.mipi_csi2.lane_polarities;
+ int nb = ep->bus.mipi_csi2.num_data_lanes;
+ int ret;
+
+ ret = mipid02_configure_clk_lane(bridge);
+ if (ret)
+ return ret;
+
+ ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
+ polarities);
+ if (ret)
+ return ret;
+
+ ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
+ polarities);
+ if (ret)
+ return ret;
+
+ bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
+ bridge->r.mode_reg1 |= (nb - 1) << 1;
+
+ return mipid02_configure_from_rx_speed(bridge, code, sensor);
+}
+
+static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->tx;
+
+ bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
+ bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
+ if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
+ bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
+ if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
+ bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
+
+ return 0;
+}
+
+static int mipid02_configure_from_code(struct mipid02_dev *bridge, __u32 code)
+{
+ u8 data_type;
+
+ bridge->r.data_id_rreg = 0;
+ data_type = data_type_from_code(code);
+ if (!data_type)
+ return -EINVAL;
+ bridge->r.data_id_rreg = data_type;
+
+ return 0;
+}
+
+static __u32 mipid02_get_source_code(struct mipid02_dev *bridge,
+ struct v4l2_subdev *sensor)
+{
+ int ret;
+ struct v4l2_subdev_format fmt = {
+ .which = V4L2_SUBDEV_FORMAT_ACTIVE,
+ };
+
+ ret = v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt);
+
+ return ret ? MEDIA_BUS_FMT_SBGGR8_1X8 : fmt.format.code;
+}
+
+static int mipid02_stream_disable(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret = 0;
+
+ /* Disable all lanes */
+ ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
+ if (ret)
+ dev_err(&client->dev, "failed to stream off %d", ret);
+
+ return ret;
+}
+
+static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
+{
+ struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
+ struct media_entity *entity;
+
+ if (!mdev)
+ return NULL;
+
+ media_device_for_each_entity(entity, mdev)
+ if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
+ return media_entity_to_v4l2_subdev(entity);
+
+ return NULL;
+}
+
+static int mipid02_stream_enable(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_subdev *sensor;
+ __u32 code;
+ int ret = 0;
+
+ sensor = mipid02_find_sensor(bridge);
+ if (!sensor)
+ goto error;
+
+ dev_dbg(&client->dev, "use sensor '%s'", sensor->name);
+ memset(&bridge->r, 0, sizeof(bridge->r));
+ /* build registers content */
+ code = mipid02_get_source_code(bridge, sensor);
+ ret |= mipid02_configure_from_rx(bridge, code, sensor);
+ ret |= mipid02_configure_from_tx(bridge);
+ ret |= mipid02_configure_from_code(bridge, code);
+
+ /* write mipi registers */
+ ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
+ bridge->r.clk_lane_reg1);
+ ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
+ bridge->r.data_lane0_reg1);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
+ DATA_MIPI_CSI);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
+ bridge->r.data_lane1_reg1);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
+ DATA_MIPI_CSI);
+ ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG1,
+ MODE_NO_BYPASS | bridge->r.mode_reg1);
+ ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG2,
+ bridge->r.mode_reg2);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
+ bridge->r.data_id_rreg);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
+ SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
+ ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
+ bridge->r.pix_width_ctrl);
+ ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
+ bridge->r.pix_width_ctrl_emb);
+
+ if (ret)
+ goto error;
+
+ return 0;
+
+error:
+ dev_err(&client->dev, "failed to stream on %d", ret);
+ mipid02_stream_disable(bridge);
+
+ return ret;
+}
+
+static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int ret = 0;
+
+ dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
+ enable, bridge->streaming);
+ mutex_lock(&bridge->lock);
+
+ if (bridge->streaming == enable)
+ goto out;
+
+ ret = enable ? mipid02_stream_enable(bridge) :
+ mipid02_stream_disable(bridge);
+ if (!ret)
+ bridge->streaming = enable;
+
+out:
+ dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
+ bridge->streaming, ret);
+ mutex_unlock(&bridge->lock);
+
+ return ret;
+}
+
+static const struct v4l2_subdev_video_ops mipid02_video_ops = {
+ .s_stream = mipid02_s_stream,
+};
+
+static const struct v4l2_subdev_ops mipid02_subdev_ops = {
+ .video = &mipid02_video_ops,
+};
+
+static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
+ struct v4l2_subdev *s_subdev,
+ struct v4l2_async_subdev *asd)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int source_pad;
+
+ dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
+
+ source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
+ s_subdev->fwnode,
+ MEDIA_PAD_FL_SOURCE);
+ if (source_pad < 0) {
+ dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
+ s_subdev->name);
+ return source_pad;
+ }
+
+ dev_dbg(&client->dev, "Bnd %s pad: %d\n", s_subdev->name, source_pad);
+
+ return media_create_pad_link(&s_subdev->entity, source_pad,
+ &bridge->sd.entity, 0,
+ MEDIA_LNK_FL_ENABLED |
+ MEDIA_LNK_FL_IMMUTABLE);
+}
+
+static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
+ .bound = mipid02_async_bound,
+};
+
+static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint ep;
+ struct i2c_client *client = bridge->i2c_client;
+ struct device_node *ep_node;
+ int ret;
+
+ memset(&ep, 0, sizeof(ep));
+ ep.bus_type = V4L2_MBUS_CSI2_DPHY;
+ /* parse rx (endpoint 0) */
+ ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
+ 0, 0);
+ if (!ep_node) {
+ dev_err(&client->dev, "unable to find port0 ep");
+ ret = -EINVAL;
+ goto error;
+ }
+
+ ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
+ if (ret) {
+ dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
+ ret);
+ goto error_of_node_put;
+ }
+
+ /* do some sanity checks */
+ if (ep.bus.mipi_csi2.num_data_lanes > 2) {
+ dev_err(&client->dev, "max supported data lanes is 2 / got %d",
+ ep.bus.mipi_csi2.num_data_lanes);
+ ret = -EINVAL;
+ goto error_v4l2_fwnode_endpoint_free;
+ }
+
+ /* register it for later use */
+ bridge->rx = ep;
+ bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
+ &bridge->link_frequency : NULL;
+ v4l2_fwnode_endpoint_free(&ep);
+
+ /* register async notifier so we get noticed when sensor is connected */
+ bridge->asd.match.fwnode =
+ fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
+ bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
+ of_node_put(ep_node);
+
+ v4l2_async_notifier_init(&bridge->notifier);
+ ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
+ if (ret) {
+ dev_err(&client->dev, "fail to register asd to notifier %d",
+ ret);
+ goto error_fwnode_handle_put;
+ }
+ bridge->notifier.ops = &mipid02_notifier_ops;
+
+ ret = v4l2_async_subdev_notifier_register(&bridge->sd,
+ &bridge->notifier);
+ if (ret)
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+
+ return ret;
+
+error_v4l2_fwnode_endpoint_free:
+ v4l2_fwnode_endpoint_free(&ep);
+error_of_node_put:
+ of_node_put(ep_node);
+error:
+
+ return ret;
+
+error_fwnode_handle_put:
+ fwnode_handle_put(bridge->asd.match.fwnode);
+
+ return ret;
+}
+
+static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_fwnode_endpoint ep;
+ struct device_node *ep_node;
+ int ret;
+
+ memset(&ep, 0, sizeof(ep));
+ ep.bus_type = V4L2_MBUS_PARALLEL;
+ /* parse tx (endpoint 2) */
+ ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
+ 2, 0);
+ if (!ep_node) {
+ dev_err(&client->dev, "unable to find port1 ep");
+ ret = -EINVAL;
+ goto error;
+ }
+
+ ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
+ if (ret) {
+ dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
+ goto error_of_node_put;
+ }
+
+ of_node_put(ep_node);
+ bridge->tx = ep;
+
+ return 0;
+
+error_of_node_put:
+ of_node_put(ep_node);
+error:
+
+ return -EINVAL;
+}
+
+static int mipid02_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ struct device *dev = &client->dev;
+ struct mipid02_dev *bridge;
+ u32 clk_freq;
+ int ret;
+
+ bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
+ if (!bridge)
+ return -ENOMEM;
+
+ bridge->i2c_client = client;
+ v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
+
+ /* got and check clock */
+ bridge->xclk = devm_clk_get(dev, "xclk");
+ if (IS_ERR(bridge->xclk)) {
+ dev_err(dev, "failed to get xclk\n");
+ return PTR_ERR(bridge->xclk);
+ }
+
+ clk_freq = clk_get_rate(bridge->xclk);
+ if (clk_freq < 6000000 || clk_freq > 27000000) {
+ dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
+ clk_freq);
+ return -EINVAL;
+ }
+
+ bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
+ GPIOD_OUT_HIGH);
+
+ ret = mipid02_get_regulators(bridge);
+ if (ret) {
+ dev_err(dev, "failed to get regulators %d", ret);
+ return ret;
+ }
+
+ mutex_init(&bridge->lock);
+ bridge->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
+ bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
+ bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
+ bridge->pad[1].flags = MEDIA_PAD_FL_SOURCE;
+ ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
+ bridge->pad);
+ if (ret) {
+ dev_err(&client->dev, "pads init failed %d", ret);
+ return ret;
+ }
+
+ /* enable clock, power and reset device if available */
+ ret = mipid02_set_power_on(bridge);
+ if (ret)
+ goto entity_cleanup;
+
+ ret = mipid02_detect(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to detect mipid02 %d", ret);
+ goto power_off;
+ }
+
+ ret = mipid02_parse_tx_ep(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to parse tx %d", ret);
+ goto power_off;
+ }
+
+ ret = mipid02_parse_rx_ep(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to parse rx %d", ret);
+ goto power_off;
+ }
+
+ ret = v4l2_async_register_subdev(&bridge->sd);
+ if (ret < 0) {
+ dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
+ ret);
+ goto unregister_notifier;
+ }
+
+ dev_info(&client->dev, "mipid02 device probe successfully");
+
+ return 0;
+
+unregister_notifier:
+ v4l2_async_notifier_unregister(&bridge->notifier);
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+power_off:
+ mipid02_set_power_off(bridge);
+entity_cleanup:
+ media_entity_cleanup(&bridge->sd.entity);
+
+ return ret;
+}
+
+static int mipid02_remove(struct i2c_client *client)
+{
+ struct v4l2_subdev *sd = i2c_get_clientdata(client);
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+
+ v4l2_async_notifier_unregister(&bridge->notifier);
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+ v4l2_async_unregister_subdev(&bridge->sd);
+ mipid02_set_power_off(bridge);
+ media_entity_cleanup(&bridge->sd.entity);
+
+ return 0;
+}
+
+static const struct i2c_device_id mipid02_id[] = {
+ {"st-mipid02", 0},
+ {},
+};
+MODULE_DEVICE_TABLE(i2c, mipid02_id);
+
+static const struct of_device_id mipid02_dt_ids[] = {
+ { .compatible = "st,st-mipid02" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
+
+static struct i2c_driver mipid02_i2c_driver = {
+ .driver = {
+ .name = "st-mipid02",
+ .of_match_table = mipid02_dt_ids,
+ },
+ .id_table = mipid02_id,
+ .probe = mipid02_probe,
+ .remove = mipid02_remove,
+};
+
+module_i2c_driver(mipid02_i2c_driver);
+
+MODULE_AUTHOR("Mickael Guene <[email protected]>");
+MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
+MODULE_LICENSE("GPL v2");
--
2.7.4


2019-03-12 06:47:19

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v1 3/3] media: MAINTAINERS: add entry for STMicroelectronics MIPID02 media driver

Add maintainer entry for the STMicroelectronics MIPID02 CSI-2 to PARALLEL
bridge driver and dt-bindings.

Signed-off-by: Mickael Guene <[email protected]>
---

MAINTAINERS | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 1c6ecae..4bd36b1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14424,6 +14424,14 @@ S: Maintained
F: drivers/iio/imu/st_lsm6dsx/
F: Documentation/devicetree/bindings/iio/imu/st_lsm6dsx.txt

+ST MIPID02 CSI-2 TO PARALLEL BRIDGE DRIVER
+M: Mickael Guene <[email protected]>
+L: [email protected]
+T: git git://linuxtv.org/media_tree.git
+S: Maintained
+F: drivers/media/i2c/st-mipid02.c
+F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
+
ST STM32 I2C/SMBUS DRIVER
M: Pierre-Yves MORDRET <[email protected]>
L: [email protected]
--
2.7.4


2019-03-16 21:58:50

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v1 3/3] media: MAINTAINERS: add entry for STMicroelectronics MIPID02 media driver

On Tue, Mar 12, 2019 at 07:44:05AM +0100, Mickael Guene wrote:
> Add maintainer entry for the STMicroelectronics MIPID02 CSI-2 to PARALLEL
> bridge driver and dt-bindings.
>
> Signed-off-by: Mickael Guene <[email protected]>
> ---
>
> MAINTAINERS | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 1c6ecae..4bd36b1 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -14424,6 +14424,14 @@ S: Maintained
> F: drivers/iio/imu/st_lsm6dsx/
> F: Documentation/devicetree/bindings/iio/imu/st_lsm6dsx.txt
>
> +ST MIPID02 CSI-2 TO PARALLEL BRIDGE DRIVER
> +M: Mickael Guene <[email protected]>
> +L: [email protected]
> +T: git git://linuxtv.org/media_tree.git
> +S: Maintained
> +F: drivers/media/i2c/st-mipid02.c
> +F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt

Could you squash this to the first patch, so that there are no files added
without them being listed here?

--
Sakari Ailus

2019-03-16 21:58:50

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v1 1/3] dt-bindings: Document MIPID02 bindings

Hi Mickael,

Thanks for the patchset.

On Tue, Mar 12, 2019 at 07:44:03AM +0100, Mickael Guene wrote:
> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
> bridge.
>
> Signed-off-by: Mickael Guene <[email protected]>
> ---
>
> .../bindings/media/i2c/st,st-mipid02.txt | 69 ++++++++++++++++++++++
> 1 file changed, 69 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>
> diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> new file mode 100644
> index 0000000..a1855da
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> @@ -0,0 +1,69 @@
> +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
> +
> +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
> +time. Active port input stream will be de-serialized and its content outputted
> +through PARALLEL output port.
> +CSI-2 first input port is a dual lane 800Mbps whereas CSI-2 second input port is

800 Mbps per lane (or total)?

> +a single lane 800Mbps. Both ports support clock and data lane polarity swap.
> +First port also supports data lane swap.
> +PARALLEL output port has a maximum width of 12 bits.
> +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
> +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
> +
> +Required Properties:
> +- compatible: should be "st,st-mipid02"
> +- clocks: reference to the xclk input clock.
> +- clock-names: should be "xclk".
> +- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
> +- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.

Perhaps Rob can confirm, but AFAIR the custom is to use lower case letters.

> +
> +Optional Properties:
> +- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
> + This is an active low signal to the mipid02.
> +
> +Required subnodes:
> + - ports: A ports node with one port child node per device input and output
> + port, in accordance with the video interface bindings defined in
> + Documentation/devicetree/bindings/media/video-interfaces.txt. The
> + port nodes are numbered as follows:
> +
> + Port Description
> + -----------------------------
> + 0 CSI-2 first input port
> + 1 CSI-2 second input port
> + 2 PARALLEL output

Please document which endpoint properties are relevant. From the above
description I'd presume this to be at least clock-lanes (1st input),
data-lanes, lane-polarities (for CSI-2) as well as bus-width for the
parallel bus.

> +
> +Example:
> +
> +mipid02: mipid02@14 {

The node should be a generic name. "csi2rx" is used by a few devices now.

> + compatible = "st,st-mipid02";
> + reg = <0x14>;
> + status = "okay";
> + clocks = <&clk_ext_camera_12>;
> + clock-names = "xclk";
> + VDDE-supply = <&vdd>;
> + VDDIN-supply = <&vdd>;
> + ports {
> + #address-cells = <1>;
> + #size-cells = <0>;
> + port@0 {
> + reg = <0>;
> +
> + ep0: endpoint {
> + clock-lanes = <0>;
> + data-lanes = <1 2>;
> + remote-endpoint = <&mipi_csi2_in>;
> + };
> + };
> + port@2 {
> + reg = <2>;
> +
> + ep2: endpoint {
> + bus-width = <8>;
> + hsync-active = <0>;
> + vsync-active = <0>;
> + remote-endpoint = <&parallel_out>;
> + };
> + };
> + };
> +};

--
Kind regards,

Sakari Ailus

2019-03-16 22:17:18

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v1 2/3] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Mickael,

Thanks for the patchset.

On Tue, Mar 12, 2019 at 07:44:04AM +0100, Mickael Guene wrote:
> This V4L2 subdev driver enables STMicroelectronics MIPID02 device.
>
> Signed-off-by: Mickael Guene <[email protected]>
> ---
>
> drivers/media/i2c/Kconfig | 13 +
> drivers/media/i2c/Makefile | 1 +
> drivers/media/i2c/st-mipid02.c | 878 +++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 892 insertions(+)
> create mode 100644 drivers/media/i2c/st-mipid02.c
>
> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
> index 6d32f8d..11a2376 100644
> --- a/drivers/media/i2c/Kconfig
> +++ b/drivers/media/i2c/Kconfig
> @@ -1132,6 +1132,19 @@ config VIDEO_I2C
> To compile this driver as a module, choose M here: the
> module will be called video-i2c
>
> +config VIDEO_ST_MIPID02
> + tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
> + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
> + depends on MEDIA_CAMERA_SUPPORT
> + depends on MEDIA_CONTROLLER
> + select V4L2_FWNODE
> + help
> + Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
> + It is used to allow usage of CSI-2 sensor with PARALLEL port controller.

Over 80, please wrap.

> +
> + To compile this driver as a module, choose M here: the
> + module will be called st-mipid02.
> +
> endmenu
>
> endif
> diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
> index a64fca8..d8ad9da 100644
> --- a/drivers/media/i2c/Makefile
> +++ b/drivers/media/i2c/Makefile
> @@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
> obj-$(CONFIG_VIDEO_IMX274) += imx274.o
> obj-$(CONFIG_VIDEO_IMX319) += imx319.o
> obj-$(CONFIG_VIDEO_IMX355) += imx355.o
> +obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o
>
> obj-$(CONFIG_SDR_MAX2175) += max2175.o
> diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
> new file mode 100644
> index 0000000..d8710fe
> --- /dev/null
> +++ b/drivers/media/i2c/st-mipid02.c
> @@ -0,0 +1,878 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
> + *
> + * Copyright (C) STMicroelectronics SA 2019
> + * Authors: Mickael Guene <[email protected]>
> + * for STMicroelectronics.
> + *
> + *
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/delay.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/i2c.h>
> +#include <linux/module.h>
> +#include <linux/of_graph.h>
> +#include <linux/regulator/consumer.h>
> +#include <media/v4l2-async.h>
> +#include <media/v4l2-ctrls.h>
> +#include <media/v4l2-device.h>
> +#include <media/v4l2-fwnode.h>
> +#include <media/v4l2-subdev.h>
> +
> +#define MIPID02_CLK_LANE_WR_REG1 0x01
> +#define MIPID02_CLK_LANE_REG1 0x02
> +#define MIPID02_CLK_LANE_REG3 0x04
> +#define MIPID02_DATA_LANE0_REG1 0x05
> +#define MIPID02_DATA_LANE0_REG2 0x06
> +#define MIPID02_DATA_LANE1_REG1 0x09
> +#define MIPID02_DATA_LANE1_REG2 0x0a
> +#define MIPID02_MODE_REG1 0x14
> +#define MIPID02_MODE_REG2 0x15
> +#define MIPID02_DATA_ID_RREG 0x17
> +#define MIPID02_DATA_SELECTION_CTRL 0x19
> +#define MIPID02_PIX_WIDTH_CTRL 0x1e
> +#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
> +
> +/* Bits definition for MIPID02_CLK_LANE_REG1 */
> +#define CLK_ENABLE BIT(0)
> +/* Bits definition for MIPID02_CLK_LANE_REG3 */
> +#define CLK_MIPI_CSI BIT(1)
> +/* Bits definition for MIPID02_DATA_LANE0_REG1 */
> +#define DATA_ENABLE BIT(0)
> +/* Bits definition for MIPID02_DATA_LANEx_REG2 */
> +#define DATA_MIPI_CSI BIT(0)
> +/* Bits definition for MIPID02_MODE_REG1 */
> +#define MODE_DATA_SWAP BIT(2)
> +#define MODE_NO_BYPASS BIT(6)
> +/* Bits definition for MIPID02_MODE_REG2 */
> +#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
> +#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
> +/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
> +#define SELECTION_MANUAL_DATA BIT(2)
> +#define SELECTION_MANUAL_WIDTH BIT(3)
> +
> +/* regulator supplies */
> +static const char * const mipid02_supply_name[] = {
> + "VDDE", /* 1.8V digital I/O supply */
> + "VDDIN", /* 1V8 voltage regulator supply */
> +};
> +
> +#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
> +#define MIPID02_PAD_NB 2
> +
> +struct mipid02_dev {
> + struct i2c_client *i2c_client;
> + struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
> + struct v4l2_subdev sd;
> + struct media_pad pad[MIPID02_PAD_NB];
> + struct clk *xclk;
> + struct gpio_desc *reset_gpio;
> + /* Protect streaming */
> + struct mutex lock;
> + bool streaming;
> + /* endpoints info */
> + struct v4l2_fwnode_endpoint rx;
> + u64 link_frequency;
> + struct v4l2_fwnode_endpoint tx;
> + /* remote source */
> + struct v4l2_async_subdev asd;
> + struct v4l2_async_notifier notifier;
> + /* registers */
> + struct {
> + u8 clk_lane_reg1;
> + u8 data_lane0_reg1;
> + u8 data_lane1_reg1;
> + u8 mode_reg1;
> + u8 mode_reg2;
> + u8 data_id_rreg;
> + u8 pix_width_ctrl;
> + u8 pix_width_ctrl_emb;
> + } r;
> +};
> +
> +static int bpp_from_code(__u32 code)
> +{
> + switch (code) {
> + case MEDIA_BUS_FMT_SBGGR8_1X8:
> + case MEDIA_BUS_FMT_SGBRG8_1X8:
> + case MEDIA_BUS_FMT_SGRBG8_1X8:
> + case MEDIA_BUS_FMT_SRGGB8_1X8:
> + return 8;
> + case MEDIA_BUS_FMT_SBGGR10_1X10:
> + case MEDIA_BUS_FMT_SGBRG10_1X10:
> + case MEDIA_BUS_FMT_SGRBG10_1X10:
> + case MEDIA_BUS_FMT_SRGGB10_1X10:
> + return 10;
> + case MEDIA_BUS_FMT_SBGGR12_1X12:
> + case MEDIA_BUS_FMT_SGBRG12_1X12:
> + case MEDIA_BUS_FMT_SGRBG12_1X12:
> + case MEDIA_BUS_FMT_SRGGB12_1X12:
> + return 12;
> + case MEDIA_BUS_FMT_UYVY8_2X8:
> + return 16;
> + case MEDIA_BUS_FMT_BGR888_1X24:
> + return 24;
> + default:
> + return 0;
> + }
> +}
> +
> +static u8 data_type_from_code(__u32 code)
> +{
> + switch (code) {
> + case MEDIA_BUS_FMT_SBGGR8_1X8:
> + case MEDIA_BUS_FMT_SGBRG8_1X8:
> + case MEDIA_BUS_FMT_SGRBG8_1X8:
> + case MEDIA_BUS_FMT_SRGGB8_1X8:
> + return 0x2a;
> + case MEDIA_BUS_FMT_SBGGR10_1X10:
> + case MEDIA_BUS_FMT_SGBRG10_1X10:
> + case MEDIA_BUS_FMT_SGRBG10_1X10:
> + case MEDIA_BUS_FMT_SRGGB10_1X10:
> + return 0x2b;
> + case MEDIA_BUS_FMT_SBGGR12_1X12:
> + case MEDIA_BUS_FMT_SGBRG12_1X12:
> + case MEDIA_BUS_FMT_SGRBG12_1X12:
> + case MEDIA_BUS_FMT_SRGGB12_1X12:
> + return 0x2c;
> + case MEDIA_BUS_FMT_UYVY8_2X8:
> + return 0x1e;
> + case MEDIA_BUS_FMT_BGR888_1X24:
> + return 0x24;
> + default:
> + return 0;
> + }
> +}
> +
> +static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
> +{
> + return container_of(sd, struct mipid02_dev, sd);
> +}
> +
> +static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct i2c_msg msg[2];
> + u8 buf[2];
> + int ret;
> +
> + buf[0] = reg >> 8;
> + buf[1] = reg & 0xff;
> +
> + msg[0].addr = client->addr;
> + msg[0].flags = client->flags;
> + msg[0].buf = buf;
> + msg[0].len = sizeof(buf);
> +
> + msg[1].addr = client->addr;
> + msg[1].flags = client->flags | I2C_M_RD;
> + msg[1].buf = val;
> + msg[1].len = 1;
> +
> + ret = i2c_transfer(client->adapter, msg, 2);
> + if (ret < 0) {
> + dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
> + __func__, client->addr, reg, ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct i2c_msg msg;
> + u8 buf[3];
> + int ret;
> +
> + buf[0] = reg >> 8;
> + buf[1] = reg & 0xff;
> + buf[2] = val;
> +
> + msg.addr = client->addr;
> + msg.flags = client->flags;
> + msg.buf = buf;
> + msg.len = sizeof(buf);
> +
> + ret = i2c_transfer(client->adapter, &msg, 1);
> + if (ret < 0) {
> + dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
> + __func__, reg, ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int mipid02_get_regulators(struct mipid02_dev *bridge)
> +{
> + unsigned int i;
> +
> + for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
> + bridge->supplies[i].supply = mipid02_supply_name[i];
> +
> + return devm_regulator_bulk_get(&bridge->i2c_client->dev,
> + MIPID02_NUM_SUPPLIES,
> + bridge->supplies);
> +}
> +
> +static void mipid02_apply_reset(struct mipid02_dev *bridge)
> +{
> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
> + usleep_range(5000, 10000);
> + gpiod_set_value_cansleep(bridge->reset_gpio, 1);
> + usleep_range(5000, 10000);
> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
> +}
> +
> +static int mipid02_set_power_on(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + int ret;
> +
> + ret = clk_prepare_enable(bridge->xclk);
> + if (ret) {
> + dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
> + return ret;
> + }
> +
> + ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
> + bridge->supplies);
> + if (ret) {
> + dev_err(&client->dev, "%s: failed to enable regulators\n",
> + __func__);
> + goto xclk_off;
> + }
> +
> + if (bridge->reset_gpio) {
> + dev_dbg(&client->dev, "apply reset");
> + mipid02_apply_reset(bridge);
> + } else {
> + dev_dbg(&client->dev, "dont apply reset");
> + usleep_range(5000, 10000);
> + }
> +
> + return 0;
> +
> +xclk_off:
> + clk_disable_unprepare(bridge->xclk);
> + return ret;
> +}
> +
> +static void mipid02_set_power_off(struct mipid02_dev *bridge)
> +{
> + regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
> + clk_disable_unprepare(bridge->xclk);
> +}
> +
> +static int mipid02_detect(struct mipid02_dev *bridge)
> +{
> + u8 reg;
> +
> + /*
> + * There is no version registers. Just try to read register
> + * MIPID02_CLK_LANE_WR_REG1.
> + */
> + return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
> +}
> +
> +static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> +
> + if (ep->nr_of_link_frequencies != 1)
> + return 0;
> +
> + return ep->link_frequencies[0];
> +}
> +
> +static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
> + struct v4l2_subdev *sensor)
> +{
> + struct v4l2_ctrl *ctrl;
> +
> + ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_LINK_FREQ);
> + if (!ctrl)
> + return 0;
> +
> + return v4l2_ctrl_g_ctrl_int64(ctrl);
> +}
> +
> +static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
> + __u32 code,
> + struct v4l2_subdev *sensor)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + struct v4l2_ctrl *ctrl;
> + u32 pixel_clock;
> + u32 bpp = bpp_from_code(code);
> +
> + ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_PIXEL_RATE);
> + if (!ctrl)
> + return 0;
> + pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
> +
> + return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
> +}
> +
> +/*
> + * We need to know link frequency to setup clk_lane_reg1 timings. So use
> + * following sequence, stop when we got a frequency.
> + * - Get it from device tree. It allow to connect sensor without support of
> + * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
> + * is that is that link frequency must be constant.
> + * - Get it from V4L2_CID_LINK_FREQ.
> + * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
> + * to compute it.
> + */
> +static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
> + __u32 code,
> + struct v4l2_subdev *sensor)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + u32 link_freq;
> +
> + link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
> + if (link_freq)
> + goto success;
> +
> + link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, sensor);
> + if (link_freq)
> + goto success;
> +
> + link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, code,
> + sensor);
> + if (link_freq)
> + goto success;
> + dev_err(&client->dev, "Failed to detect link frequency");
> +
> + return -EINVAL;
> +
> +success:
> + dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
> + bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
> +
> + /* midid02 doesn't support clock lane remapping */
> + if (ep->bus.mipi_csi2.clock_lane != 0) {
> + dev_err(&client->dev, "clk lane must be map to lane 0\n");
> + return -EINVAL;
> + }
> + bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
> + bool are_lanes_swap, bool *polarities)
> +{
> + bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
> +
> + if (nb == 1 && are_lanes_swap)
> + return 0;
> +
> + /*
> + * data lane 0 as pin swap polarity reversed compared to clock and
> + *data lane 1
> + */
> + if (!are_pin_swap)
> + bridge->r.data_lane0_reg1 = 1 << 1;
> + bridge->r.data_lane0_reg1 |= DATA_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
> + bool are_lanes_swap, bool *polarities)
> +{
> + bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
> +
> + if (nb == 1 && !are_lanes_swap)
> + return 0;
> +
> + if (are_pin_swap)
> + bridge->r.data_lane1_reg1 = 1 << 1;
> + bridge->r.data_lane1_reg1 |= DATA_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_from_rx(struct mipid02_dev *bridge, __u32 code,
> + struct v4l2_subdev *sensor)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
> + int nb = ep->bus.mipi_csi2.num_data_lanes;
> + int ret;
> +
> + ret = mipid02_configure_clk_lane(bridge);
> + if (ret)
> + return ret;
> +
> + ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
> + polarities);
> + if (ret)
> + return ret;
> +
> + ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
> + polarities);
> + if (ret)
> + return ret;
> +
> + bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
> + bridge->r.mode_reg1 |= (nb - 1) << 1;
> +
> + return mipid02_configure_from_rx_speed(bridge, code, sensor);
> +}
> +
> +static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->tx;
> +
> + bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
> + bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
> + if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
> + bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
> + if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
> + bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_from_code(struct mipid02_dev *bridge, __u32 code)
> +{
> + u8 data_type;
> +
> + bridge->r.data_id_rreg = 0;
> + data_type = data_type_from_code(code);
> + if (!data_type)
> + return -EINVAL;
> + bridge->r.data_id_rreg = data_type;
> +
> + return 0;
> +}
> +
> +static __u32 mipid02_get_source_code(struct mipid02_dev *bridge,
> + struct v4l2_subdev *sensor)
> +{
> + int ret;

Please declare temporary variables, return values etc. as last. That
generally is the case elsewhere in the file.

> + struct v4l2_subdev_format fmt = {
> + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
> + };
> +
> + ret = v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt);
> +
> + return ret ? MEDIA_BUS_FMT_SBGGR8_1X8 : fmt.format.code;
> +}
> +
> +static int mipid02_stream_disable(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + int ret = 0;

No need to initialise here.

> +
> + /* Disable all lanes */
> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
> + if (ret)
> + dev_err(&client->dev, "failed to stream off %d", ret);
> +
> + return ret;
> +}
> +
> +static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
> +{
> + struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
> + struct media_entity *entity;
> +
> + if (!mdev)
> + return NULL;
> +
> + media_device_for_each_entity(entity, mdev)
> + if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
> + return media_entity_to_v4l2_subdev(entity);

Hmm. Could you instead use the link state to determine which of the
receivers is active? You'll need one more pad, and then you'd had 1:1
mapping between ports and pads.

> +
> + return NULL;
> +}
> +
> +static int mipid02_stream_enable(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_subdev *sensor;
> + __u32 code;
> + int ret = 0;

No need to initialise.

> +
> + sensor = mipid02_find_sensor(bridge);
> + if (!sensor)
> + goto error;
> +
> + dev_dbg(&client->dev, "use sensor '%s'", sensor->name);
> + memset(&bridge->r, 0, sizeof(bridge->r));
> + /* build registers content */
> + code = mipid02_get_source_code(bridge, sensor);
> + ret |= mipid02_configure_from_rx(bridge, code, sensor);
> + ret |= mipid02_configure_from_tx(bridge);
> + ret |= mipid02_configure_from_code(bridge, code);
> +
> + /* write mipi registers */
> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
> + bridge->r.clk_lane_reg1);
> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
> + bridge->r.data_lane0_reg1);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
> + DATA_MIPI_CSI);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
> + bridge->r.data_lane1_reg1);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
> + DATA_MIPI_CSI);
> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG1,
> + MODE_NO_BYPASS | bridge->r.mode_reg1);
> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG2,
> + bridge->r.mode_reg2);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
> + bridge->r.data_id_rreg);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
> + bridge->r.pix_width_ctrl);
> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
> + bridge->r.pix_width_ctrl_emb);

Be careful with the error codes. ret will be returned by the s_stream
callback below.

> +
> + if (ret)
> + goto error;
> +
> + return 0;
> +
> +error:
> + dev_err(&client->dev, "failed to stream on %d", ret);
> + mipid02_stream_disable(bridge);
> +
> + return ret;
> +}
> +
> +static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
> +{
> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> + struct i2c_client *client = bridge->i2c_client;
> + int ret = 0;
> +
> + dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
> + enable, bridge->streaming);
> + mutex_lock(&bridge->lock);
> +
> + if (bridge->streaming == enable)
> + goto out;
> +
> + ret = enable ? mipid02_stream_enable(bridge) :
> + mipid02_stream_disable(bridge);
> + if (!ret)
> + bridge->streaming = enable;
> +
> +out:
> + dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
> + bridge->streaming, ret);
> + mutex_unlock(&bridge->lock);
> +
> + return ret;
> +}
> +
> +static const struct v4l2_subdev_video_ops mipid02_video_ops = {
> + .s_stream = mipid02_s_stream,
> +};
> +
> +static const struct v4l2_subdev_ops mipid02_subdev_ops = {
> + .video = &mipid02_video_ops,
> +};
> +
> +static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
> + struct v4l2_subdev *s_subdev,
> + struct v4l2_async_subdev *asd)
> +{
> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
> + struct i2c_client *client = bridge->i2c_client;
> + int source_pad;
> +
> + dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
> +
> + source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
> + s_subdev->fwnode,
> + MEDIA_PAD_FL_SOURCE);
> + if (source_pad < 0) {
> + dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
> + s_subdev->name);
> + return source_pad;
> + }
> +
> + dev_dbg(&client->dev, "Bnd %s pad: %d\n", s_subdev->name, source_pad);

How about simply "Bound"...? In case you need this.

> +
> + return media_create_pad_link(&s_subdev->entity, source_pad,
> + &bridge->sd.entity, 0,
> + MEDIA_LNK_FL_ENABLED |
> + MEDIA_LNK_FL_IMMUTABLE);
> +}
> +
> +static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
> + .bound = mipid02_async_bound,
> +};
> +
> +static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint ep;
> + struct i2c_client *client = bridge->i2c_client;
> + struct device_node *ep_node;
> + int ret;
> +
> + memset(&ep, 0, sizeof(ep));
> + ep.bus_type = V4L2_MBUS_CSI2_DPHY;
> + /* parse rx (endpoint 0) */
> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
> + 0, 0);
> + if (!ep_node) {
> + dev_err(&client->dev, "unable to find port0 ep");
> + ret = -EINVAL;
> + goto error;
> + }
> +
> + ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
> + if (ret) {
> + dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
> + ret);
> + goto error_of_node_put;
> + }
> +
> + /* do some sanity checks */
> + if (ep.bus.mipi_csi2.num_data_lanes > 2) {
> + dev_err(&client->dev, "max supported data lanes is 2 / got %d",
> + ep.bus.mipi_csi2.num_data_lanes);
> + ret = -EINVAL;
> + goto error_v4l2_fwnode_endpoint_free;
> + }
> +
> + /* register it for later use */
> + bridge->rx = ep;
> + bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
> + &bridge->link_frequency : NULL;
> + v4l2_fwnode_endpoint_free(&ep);
> +
> + /* register async notifier so we get noticed when sensor is connected */
> + bridge->asd.match.fwnode =
> + fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
> + bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
> + of_node_put(ep_node);
> +
> + v4l2_async_notifier_init(&bridge->notifier);
> + ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
> + if (ret) {
> + dev_err(&client->dev, "fail to register asd to notifier %d",
> + ret);
> + goto error_fwnode_handle_put;
> + }
> + bridge->notifier.ops = &mipid02_notifier_ops;
> +
> + ret = v4l2_async_subdev_notifier_register(&bridge->sd,
> + &bridge->notifier);
> + if (ret)
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> +
> + return ret;
> +
> +error_v4l2_fwnode_endpoint_free:
> + v4l2_fwnode_endpoint_free(&ep);
> +error_of_node_put:
> + of_node_put(ep_node);
> +error:
> +
> + return ret;
> +
> +error_fwnode_handle_put:
> + fwnode_handle_put(bridge->asd.match.fwnode);
> +
> + return ret;
> +}
> +
> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_fwnode_endpoint ep;
> + struct device_node *ep_node;
> + int ret;
> +
> + memset(&ep, 0, sizeof(ep));
> + ep.bus_type = V4L2_MBUS_PARALLEL;

You can set the field in variable declaration, and omit memset. The same in
the function above.

> + /* parse tx (endpoint 2) */
> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
> + 2, 0);
> + if (!ep_node) {
> + dev_err(&client->dev, "unable to find port1 ep");
> + ret = -EINVAL;
> + goto error;
> + }
> +
> + ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
> + if (ret) {
> + dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
> + goto error_of_node_put;
> + }
> +
> + of_node_put(ep_node);
> + bridge->tx = ep;
> +
> + return 0;
> +
> +error_of_node_put:
> + of_node_put(ep_node);
> +error:
> +
> + return -EINVAL;
> +}
> +
> +static int mipid02_probe(struct i2c_client *client,
> + const struct i2c_device_id *id)
> +{
> + struct device *dev = &client->dev;
> + struct mipid02_dev *bridge;
> + u32 clk_freq;
> + int ret;
> +
> + bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
> + if (!bridge)
> + return -ENOMEM;
> +
> + bridge->i2c_client = client;
> + v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
> +
> + /* got and check clock */
> + bridge->xclk = devm_clk_get(dev, "xclk");
> + if (IS_ERR(bridge->xclk)) {
> + dev_err(dev, "failed to get xclk\n");
> + return PTR_ERR(bridge->xclk);
> + }
> +
> + clk_freq = clk_get_rate(bridge->xclk);
> + if (clk_freq < 6000000 || clk_freq > 27000000) {
> + dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
> + clk_freq);
> + return -EINVAL;
> + }
> +
> + bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
> + GPIOD_OUT_HIGH);
> +
> + ret = mipid02_get_regulators(bridge);
> + if (ret) {
> + dev_err(dev, "failed to get regulators %d", ret);
> + return ret;
> + }
> +
> + mutex_init(&bridge->lock);
> + bridge->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
> + bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
> + bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
> + bridge->pad[1].flags = MEDIA_PAD_FL_SOURCE;
> + ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
> + bridge->pad);
> + if (ret) {
> + dev_err(&client->dev, "pads init failed %d", ret);
> + return ret;
> + }
> +
> + /* enable clock, power and reset device if available */
> + ret = mipid02_set_power_on(bridge);
> + if (ret)
> + goto entity_cleanup;
> +
> + ret = mipid02_detect(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to detect mipid02 %d", ret);
> + goto power_off;
> + }
> +
> + ret = mipid02_parse_tx_ep(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to parse tx %d", ret);
> + goto power_off;
> + }
> +
> + ret = mipid02_parse_rx_ep(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to parse rx %d", ret);
> + goto power_off;
> + }
> +
> + ret = v4l2_async_register_subdev(&bridge->sd);
> + if (ret < 0) {
> + dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
> + ret);
> + goto unregister_notifier;
> + }
> +
> + dev_info(&client->dev, "mipid02 device probe successfully");
> +
> + return 0;
> +
> +unregister_notifier:
> + v4l2_async_notifier_unregister(&bridge->notifier);
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> +power_off:
> + mipid02_set_power_off(bridge);
> +entity_cleanup:
> + media_entity_cleanup(&bridge->sd.entity);
> +
> + return ret;
> +}
> +
> +static int mipid02_remove(struct i2c_client *client)
> +{
> + struct v4l2_subdev *sd = i2c_get_clientdata(client);
> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> +
> + v4l2_async_notifier_unregister(&bridge->notifier);
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> + v4l2_async_unregister_subdev(&bridge->sd);
> + mipid02_set_power_off(bridge);
> + media_entity_cleanup(&bridge->sd.entity);
> +
> + return 0;
> +}
> +
> +static const struct i2c_device_id mipid02_id[] = {
> + {"st-mipid02", 0},

Could you add whitespaces inside braces?

> + {},
> +};
> +MODULE_DEVICE_TABLE(i2c, mipid02_id);
> +
> +static const struct of_device_id mipid02_dt_ids[] = {
> + { .compatible = "st,st-mipid02" },
> + { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
> +
> +static struct i2c_driver mipid02_i2c_driver = {
> + .driver = {
> + .name = "st-mipid02",
> + .of_match_table = mipid02_dt_ids,
> + },
> + .id_table = mipid02_id,
> + .probe = mipid02_probe,
> + .remove = mipid02_remove,
> +};
> +
> +module_i2c_driver(mipid02_i2c_driver);
> +
> +MODULE_AUTHOR("Mickael Guene <[email protected]>");
> +MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
> +MODULE_LICENSE("GPL v2");

--
Kind regards,

Sakari Ailus

2019-03-18 08:31:34

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v1 1/3] dt-bindings: Document MIPID02 bindings

Hi Sakari,

Thanks for your review. Find my comments below.

On 3/16/19 22:46, Sakari Ailus wrote:
> Hi Mickael,
>
> Thanks for the patchset.
>
> On Tue, Mar 12, 2019 at 07:44:03AM +0100, Mickael Guene wrote:
>> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
>> bridge.
>>
>> Signed-off-by: Mickael Guene <[email protected]>
>> ---
>>
>> .../bindings/media/i2c/st,st-mipid02.txt | 69 ++++++++++++++++++++++
>> 1 file changed, 69 insertions(+)
>> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>>
>> diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>> new file mode 100644
>> index 0000000..a1855da
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>> @@ -0,0 +1,69 @@
>> +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
>> +
>> +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
>> +time. Active port input stream will be de-serialized and its content outputted
>> +through PARALLEL output port.
>> +CSI-2 first input port is a dual lane 800Mbps whereas CSI-2 second input port is
>
> 800 Mbps per lane (or total)?
>
800 Mbps per lane. I will document it.
>> +a single lane 800Mbps. Both ports support clock and data lane polarity swap.
>> +First port also supports data lane swap.
>> +PARALLEL output port has a maximum width of 12 bits.
>> +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
>> +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
>> +
>> +Required Properties:
>> +- compatible: should be "st,st-mipid02"
>> +- clocks: reference to the xclk input clock.
>> +- clock-names: should be "xclk".
>> +- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
>> +- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
>
> Perhaps Rob can confirm, but AFAIR the custom is to use lower case letters.
>
It seems there is a 50-50 ratio between upper and lower case usage in
Documentation/devicetree/bindings/media/i2. I will wait Rob's answer to change
it or not.
>> +
>> +Optional Properties:
>> +- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
>> + This is an active low signal to the mipid02.
>> +
>> +Required subnodes:
>> + - ports: A ports node with one port child node per device input and output
>> + port, in accordance with the video interface bindings defined in
>> + Documentation/devicetree/bindings/media/video-interfaces.txt. The
>> + port nodes are numbered as follows:
>> +
>> + Port Description
>> + -----------------------------
>> + 0 CSI-2 first input port
>> + 1 CSI-2 second input port
>> + 2 PARALLEL output
>
> Please document which endpoint properties are relevant. From the above
> description I'd presume this to be at least clock-lanes (1st input),
> data-lanes, lane-polarities (for CSI-2) as well as bus-width for the
> parallel bus.
>
ok. I will add documentation.
>> +
>> +Example:
>> +
>> +mipid02: mipid02@14 {
>
> The node should be a generic name. "csi2rx" is used by a few devices now.
>
If I understand you well, you would prefer:
csi2rx: mipid02@14 {
I show no usage of csi2rx node naming except for MIPI-CSI2 RX controller.
>> + compatible = "st,st-mipid02";
>> + reg = <0x14>;
>> + status = "okay";
>> + clocks = <&clk_ext_camera_12>;
>> + clock-names = "xclk";
>> + VDDE-supply = <&vdd>;
>> + VDDIN-supply = <&vdd>;
>> + ports {
>> + #address-cells = <1>;
>> + #size-cells = <0>;
>> + port@0 {
>> + reg = <0>;
>> +
>> + ep0: endpoint {
>> + clock-lanes = <0>;
>> + data-lanes = <1 2>;
>> + remote-endpoint = <&mipi_csi2_in>;
>> + };
>> + };
>> + port@2 {
>> + reg = <2>;
>> +
>> + ep2: endpoint {
>> + bus-width = <8>;
>> + hsync-active = <0>;
>> + vsync-active = <0>;
>> + remote-endpoint = <&parallel_out>;
>> + };
>> + };
>> + };
>> +};
>

2019-03-18 08:51:07

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v1 1/3] dt-bindings: Document MIPID02 bindings

On Mon, Mar 18, 2019 at 08:28:36AM +0000, Mickael GUENE wrote:
> Hi Sakari,
>
> Thanks for your review. Find my comments below.
>
> On 3/16/19 22:46, Sakari Ailus wrote:
> > Hi Mickael,
> >
> > Thanks for the patchset.
> >
> > On Tue, Mar 12, 2019 at 07:44:03AM +0100, Mickael Guene wrote:
> >> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
> >> bridge.
> >>
> >> Signed-off-by: Mickael Guene <[email protected]>
> >> ---
> >>
> >> .../bindings/media/i2c/st,st-mipid02.txt | 69 ++++++++++++++++++++++
> >> 1 file changed, 69 insertions(+)
> >> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> >>
> >> diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> >> new file mode 100644
> >> index 0000000..a1855da
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> >> @@ -0,0 +1,69 @@
> >> +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
> >> +
> >> +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
> >> +time. Active port input stream will be de-serialized and its content outputted
> >> +through PARALLEL output port.
> >> +CSI-2 first input port is a dual lane 800Mbps whereas CSI-2 second input port is
> >
> > 800 Mbps per lane (or total)?
> >
> 800 Mbps per lane. I will document it.
> >> +a single lane 800Mbps. Both ports support clock and data lane polarity swap.
> >> +First port also supports data lane swap.
> >> +PARALLEL output port has a maximum width of 12 bits.
> >> +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
> >> +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
> >> +
> >> +Required Properties:
> >> +- compatible: should be "st,st-mipid02"
> >> +- clocks: reference to the xclk input clock.
> >> +- clock-names: should be "xclk".
> >> +- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
> >> +- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
> >
> > Perhaps Rob can confirm, but AFAIR the custom is to use lower case letters.
> >
> It seems there is a 50-50 ratio between upper and lower case usage in
> Documentation/devicetree/bindings/media/i2. I will wait Rob's answer to change
> it or not.
> >> +
> >> +Optional Properties:
> >> +- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
> >> + This is an active low signal to the mipid02.
> >> +
> >> +Required subnodes:
> >> + - ports: A ports node with one port child node per device input and output
> >> + port, in accordance with the video interface bindings defined in
> >> + Documentation/devicetree/bindings/media/video-interfaces.txt. The
> >> + port nodes are numbered as follows:
> >> +
> >> + Port Description
> >> + -----------------------------
> >> + 0 CSI-2 first input port
> >> + 1 CSI-2 second input port
> >> + 2 PARALLEL output
> >
> > Please document which endpoint properties are relevant. From the above
> > description I'd presume this to be at least clock-lanes (1st input),
> > data-lanes, lane-polarities (for CSI-2) as well as bus-width for the
> > parallel bus.
> >
> ok. I will add documentation.
> >> +
> >> +Example:
> >> +
> >> +mipid02: mipid02@14 {
> >
> > The node should be a generic name. "csi2rx" is used by a few devices now.
> >
> If I understand you well, you would prefer:
> csi2rx: mipid02@14 {
> I show no usage of csi2rx node naming except for MIPI-CSI2 RX controller.

The other way around. :)

The label can be more or less anything AFAIK.

--
Sakari Ailus

2019-03-18 09:10:30

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v1 1/3] dt-bindings: Document MIPID02 bindings

Sakari,

>>>> +
>>>> +Example:
>>>> +
>>>> +mipid02: mipid02@14 {
>>>
>>> The node should be a generic name. "csi2rx" is used by a few devices now.
>>>
>> If I understand you well, you would prefer:
>> csi2rx: mipid02@14 {
>> I show no usage of csi2rx node naming except for MIPI-CSI2 RX controller.
>
> The other way around. :)
>
> The label can be more or less anything AFAIK.
>
Ok got it !!! So something like 'mipid02: bridge@14 {' should be ok ?

Thx

2019-03-18 09:59:25

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v1 2/3] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Sakari,

Thanks for your review. Find my comments below.

On 3/16/19 23:14, Sakari Ailus wrote:
> Hi Mickael,
>
> Thanks for the patchset.
>
> On Tue, Mar 12, 2019 at 07:44:04AM +0100, Mickael Guene wrote:
>> This V4L2 subdev driver enables STMicroelectronics MIPID02 device.
>>
>> Signed-off-by: Mickael Guene <[email protected]>
>> ---
>>
>> drivers/media/i2c/Kconfig | 13 +
>> drivers/media/i2c/Makefile | 1 +
>> drivers/media/i2c/st-mipid02.c | 878 +++++++++++++++++++++++++++++++++++++++++
>> 3 files changed, 892 insertions(+)
>> create mode 100644 drivers/media/i2c/st-mipid02.c
>>
>> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
>> index 6d32f8d..11a2376 100644
>> --- a/drivers/media/i2c/Kconfig
>> +++ b/drivers/media/i2c/Kconfig
>> @@ -1132,6 +1132,19 @@ config VIDEO_I2C
>> To compile this driver as a module, choose M here: the
>> module will be called video-i2c
>>
>> +config VIDEO_ST_MIPID02
>> + tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
>> + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
>> + depends on MEDIA_CAMERA_SUPPORT
>> + depends on MEDIA_CONTROLLER
>> + select V4L2_FWNODE
>> + help
>> + Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
>> + It is used to allow usage of CSI-2 sensor with PARALLEL port controller.
>
> Over 80, please wrap.
>
ok
>> +
>> + To compile this driver as a module, choose M here: the
>> + module will be called st-mipid02.
>> +
>> endmenu
>>
>> endif
>> diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
>> index a64fca8..d8ad9da 100644
>> --- a/drivers/media/i2c/Makefile
>> +++ b/drivers/media/i2c/Makefile
>> @@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
>> obj-$(CONFIG_VIDEO_IMX274) += imx274.o
>> obj-$(CONFIG_VIDEO_IMX319) += imx319.o
>> obj-$(CONFIG_VIDEO_IMX355) += imx355.o
>> +obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o
>>
>> obj-$(CONFIG_SDR_MAX2175) += max2175.o
>> diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
>> new file mode 100644
>> index 0000000..d8710fe
>> --- /dev/null
>> +++ b/drivers/media/i2c/st-mipid02.c
>> @@ -0,0 +1,878 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
>> + *
>> + * Copyright (C) STMicroelectronics SA 2019
>> + * Authors: Mickael Guene <[email protected]>
>> + * for STMicroelectronics.
>> + *
>> + *
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/delay.h>
>> +#include <linux/gpio/consumer.h>
>> +#include <linux/i2c.h>
>> +#include <linux/module.h>
>> +#include <linux/of_graph.h>
>> +#include <linux/regulator/consumer.h>
>> +#include <media/v4l2-async.h>
>> +#include <media/v4l2-ctrls.h>
>> +#include <media/v4l2-device.h>
>> +#include <media/v4l2-fwnode.h>
>> +#include <media/v4l2-subdev.h>
>> +
>> +#define MIPID02_CLK_LANE_WR_REG1 0x01
>> +#define MIPID02_CLK_LANE_REG1 0x02
>> +#define MIPID02_CLK_LANE_REG3 0x04
>> +#define MIPID02_DATA_LANE0_REG1 0x05
>> +#define MIPID02_DATA_LANE0_REG2 0x06
>> +#define MIPID02_DATA_LANE1_REG1 0x09
>> +#define MIPID02_DATA_LANE1_REG2 0x0a
>> +#define MIPID02_MODE_REG1 0x14
>> +#define MIPID02_MODE_REG2 0x15
>> +#define MIPID02_DATA_ID_RREG 0x17
>> +#define MIPID02_DATA_SELECTION_CTRL 0x19
>> +#define MIPID02_PIX_WIDTH_CTRL 0x1e
>> +#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
>> +
>> +/* Bits definition for MIPID02_CLK_LANE_REG1 */
>> +#define CLK_ENABLE BIT(0)
>> +/* Bits definition for MIPID02_CLK_LANE_REG3 */
>> +#define CLK_MIPI_CSI BIT(1)
>> +/* Bits definition for MIPID02_DATA_LANE0_REG1 */
>> +#define DATA_ENABLE BIT(0)
>> +/* Bits definition for MIPID02_DATA_LANEx_REG2 */
>> +#define DATA_MIPI_CSI BIT(0)
>> +/* Bits definition for MIPID02_MODE_REG1 */
>> +#define MODE_DATA_SWAP BIT(2)
>> +#define MODE_NO_BYPASS BIT(6)
>> +/* Bits definition for MIPID02_MODE_REG2 */
>> +#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
>> +#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
>> +/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
>> +#define SELECTION_MANUAL_DATA BIT(2)
>> +#define SELECTION_MANUAL_WIDTH BIT(3)
>> +
>> +/* regulator supplies */
>> +static const char * const mipid02_supply_name[] = {
>> + "VDDE", /* 1.8V digital I/O supply */
>> + "VDDIN", /* 1V8 voltage regulator supply */
>> +};
>> +
>> +#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
>> +#define MIPID02_PAD_NB 2
>> +
>> +struct mipid02_dev {
>> + struct i2c_client *i2c_client;
>> + struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
>> + struct v4l2_subdev sd;
>> + struct media_pad pad[MIPID02_PAD_NB];
>> + struct clk *xclk;
>> + struct gpio_desc *reset_gpio;
>> + /* Protect streaming */
>> + struct mutex lock;
>> + bool streaming;
>> + /* endpoints info */
>> + struct v4l2_fwnode_endpoint rx;
>> + u64 link_frequency;
>> + struct v4l2_fwnode_endpoint tx;
>> + /* remote source */
>> + struct v4l2_async_subdev asd;
>> + struct v4l2_async_notifier notifier;
>> + /* registers */
>> + struct {
>> + u8 clk_lane_reg1;
>> + u8 data_lane0_reg1;
>> + u8 data_lane1_reg1;
>> + u8 mode_reg1;
>> + u8 mode_reg2;
>> + u8 data_id_rreg;
>> + u8 pix_width_ctrl;
>> + u8 pix_width_ctrl_emb;
>> + } r;
>> +};
>> +
>> +static int bpp_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 8;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 10;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 12;
>> + case MEDIA_BUS_FMT_UYVY8_2X8:
>> + return 16;
>> + case MEDIA_BUS_FMT_BGR888_1X24:
>> + return 24;
>> + default:
>> + return 0;
>> + }
>> +}
>> +
>> +static u8 data_type_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 0x2a;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 0x2b;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 0x2c;
>> + case MEDIA_BUS_FMT_UYVY8_2X8:
>> + return 0x1e;
>> + case MEDIA_BUS_FMT_BGR888_1X24:
>> + return 0x24;
>> + default:
>> + return 0;
>> + }
>> +}
>> +
>> +static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
>> +{
>> + return container_of(sd, struct mipid02_dev, sd);
>> +}
>> +
>> +static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct i2c_msg msg[2];
>> + u8 buf[2];
>> + int ret;
>> +
>> + buf[0] = reg >> 8;
>> + buf[1] = reg & 0xff;
>> +
>> + msg[0].addr = client->addr;
>> + msg[0].flags = client->flags;
>> + msg[0].buf = buf;
>> + msg[0].len = sizeof(buf);
>> +
>> + msg[1].addr = client->addr;
>> + msg[1].flags = client->flags | I2C_M_RD;
>> + msg[1].buf = val;
>> + msg[1].len = 1;
>> +
>> + ret = i2c_transfer(client->adapter, msg, 2);
>> + if (ret < 0) {
>> + dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
>> + __func__, client->addr, reg, ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct i2c_msg msg;
>> + u8 buf[3];
>> + int ret;
>> +
>> + buf[0] = reg >> 8;
>> + buf[1] = reg & 0xff;
>> + buf[2] = val;
>> +
>> + msg.addr = client->addr;
>> + msg.flags = client->flags;
>> + msg.buf = buf;
>> + msg.len = sizeof(buf);
>> +
>> + ret = i2c_transfer(client->adapter, &msg, 1);
>> + if (ret < 0) {
>> + dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
>> + __func__, reg, ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_get_regulators(struct mipid02_dev *bridge)
>> +{
>> + unsigned int i;
>> +
>> + for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
>> + bridge->supplies[i].supply = mipid02_supply_name[i];
>> +
>> + return devm_regulator_bulk_get(&bridge->i2c_client->dev,
>> + MIPID02_NUM_SUPPLIES,
>> + bridge->supplies);
>> +}
>> +
>> +static void mipid02_apply_reset(struct mipid02_dev *bridge)
>> +{
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
>> + usleep_range(5000, 10000);
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 1);
>> + usleep_range(5000, 10000);
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
>> +}
>> +
>> +static int mipid02_set_power_on(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret;
>> +
>> + ret = clk_prepare_enable(bridge->xclk);
>> + if (ret) {
>> + dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
>> + return ret;
>> + }
>> +
>> + ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
>> + bridge->supplies);
>> + if (ret) {
>> + dev_err(&client->dev, "%s: failed to enable regulators\n",
>> + __func__);
>> + goto xclk_off;
>> + }
>> +
>> + if (bridge->reset_gpio) {
>> + dev_dbg(&client->dev, "apply reset");
>> + mipid02_apply_reset(bridge);
>> + } else {
>> + dev_dbg(&client->dev, "dont apply reset");
>> + usleep_range(5000, 10000);
>> + }
>> +
>> + return 0;
>> +
>> +xclk_off:
>> + clk_disable_unprepare(bridge->xclk);
>> + return ret;
>> +}
>> +
>> +static void mipid02_set_power_off(struct mipid02_dev *bridge)
>> +{
>> + regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
>> + clk_disable_unprepare(bridge->xclk);
>> +}
>> +
>> +static int mipid02_detect(struct mipid02_dev *bridge)
>> +{
>> + u8 reg;
>> +
>> + /*
>> + * There is no version registers. Just try to read register
>> + * MIPID02_CLK_LANE_WR_REG1.
>> + */
>> + return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> +
>> + if (ep->nr_of_link_frequencies != 1)
>> + return 0;
>> +
>> + return ep->link_frequencies[0];
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct v4l2_ctrl *ctrl;
>> +
>> + ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_LINK_FREQ);
>> + if (!ctrl)
>> + return 0;
>> +
>> + return v4l2_ctrl_g_ctrl_int64(ctrl);
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
>> + __u32 code,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + struct v4l2_ctrl *ctrl;
>> + u32 pixel_clock;
>> + u32 bpp = bpp_from_code(code);
>> +
>> + ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_PIXEL_RATE);
>> + if (!ctrl)
>> + return 0;
>> + pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
>> +
>> + return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
>> +}
>> +
>> +/*
>> + * We need to know link frequency to setup clk_lane_reg1 timings. So use
>> + * following sequence, stop when we got a frequency.
>> + * - Get it from device tree. It allow to connect sensor without support of
>> + * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
>> + * is that is that link frequency must be constant.
>> + * - Get it from V4L2_CID_LINK_FREQ.
>> + * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
>> + * to compute it.
>> + */
>> +static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
>> + __u32 code,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + u32 link_freq;
>> +
>> + link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
>> + if (link_freq)
>> + goto success;
>> +
>> + link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, sensor);
>> + if (link_freq)
>> + goto success;
>> +
>> + link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, code,
>> + sensor);
>> + if (link_freq)
>> + goto success;
>> + dev_err(&client->dev, "Failed to detect link frequency");
>> +
>> + return -EINVAL;
>> +
>> +success:
>> + dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
>> + bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
>> +
>> + /* midid02 doesn't support clock lane remapping */
>> + if (ep->bus.mipi_csi2.clock_lane != 0) {
>> + dev_err(&client->dev, "clk lane must be map to lane 0\n");
>> + return -EINVAL;
>> + }
>> + bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
>> + bool are_lanes_swap, bool *polarities)
>> +{
>> + bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
>> +
>> + if (nb == 1 && are_lanes_swap)
>> + return 0;
>> +
>> + /*
>> + * data lane 0 as pin swap polarity reversed compared to clock and
>> + *data lane 1
>> + */
>> + if (!are_pin_swap)
>> + bridge->r.data_lane0_reg1 = 1 << 1;
>> + bridge->r.data_lane0_reg1 |= DATA_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
>> + bool are_lanes_swap, bool *polarities)
>> +{
>> + bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
>> +
>> + if (nb == 1 && !are_lanes_swap)
>> + return 0;
>> +
>> + if (are_pin_swap)
>> + bridge->r.data_lane1_reg1 = 1 << 1;
>> + bridge->r.data_lane1_reg1 |= DATA_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_from_rx(struct mipid02_dev *bridge, __u32 code,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
>> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
>> + int nb = ep->bus.mipi_csi2.num_data_lanes;
>> + int ret;
>> +
>> + ret = mipid02_configure_clk_lane(bridge);
>> + if (ret)
>> + return ret;
>> +
>> + ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
>> + polarities);
>> + if (ret)
>> + return ret;
>> +
>> + ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
>> + polarities);
>> + if (ret)
>> + return ret;
>> +
>> + bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
>> + bridge->r.mode_reg1 |= (nb - 1) << 1;
>> +
>> + return mipid02_configure_from_rx_speed(bridge, code, sensor);
>> +}
>> +
>> +static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->tx;
>> +
>> + bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
>> + bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
>> + if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
>> + bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
>> + if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
>> + bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_from_code(struct mipid02_dev *bridge, __u32 code)
>> +{
>> + u8 data_type;
>> +
>> + bridge->r.data_id_rreg = 0;
>> + data_type = data_type_from_code(code);
>> + if (!data_type)
>> + return -EINVAL;
>> + bridge->r.data_id_rreg = data_type;
>> +
>> + return 0;
>> +}
>> +
>> +static __u32 mipid02_get_source_code(struct mipid02_dev *bridge,
>> + struct v4l2_subdev *sensor)
>> +{
>> + int ret;
>
> Please declare temporary variables, return values etc. as last. That
> generally is the case elsewhere in the file.
>
ok
>> + struct v4l2_subdev_format fmt = {
>> + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
>> + };
>> +
>> + ret = v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt);
>> +
>> + return ret ? MEDIA_BUS_FMT_SBGGR8_1X8 : fmt.format.code;
>> +}
>> +
>> +static int mipid02_stream_disable(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret = 0;
>
> No need to initialise here.
>
ok but also need to change from
ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
to
ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
>> +
>> + /* Disable all lanes */
>> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
>> + if (ret)
>> + dev_err(&client->dev, "failed to stream off %d", ret);
>> +
>> + return ret;
>> +}
>> +
>> +static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
>> +{
>> + struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
>> + struct media_entity *entity;
>> +
>> + if (!mdev)
>> + return NULL;
>> +
>> + media_device_for_each_entity(entity, mdev)
>> + if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
>> + return media_entity_to_v4l2_subdev(entity);
>
> Hmm. Could you instead use the link state to determine which of the
> receivers is active? You'll need one more pad, and then you'd had 1:1
> mapping between ports and pads.
>
Goal here is not to detect which of the receivers is active but to find
sensor in case there are others sub-dev in chain (for example a
serializer/deserializer as found in cars).
For the moment the driver doesn't support second input port usage,
this is why there is no such second sink pad yet in the driver.

>> +
>> + return NULL;
>> +}
>> +
>> +static int mipid02_stream_enable(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_subdev *sensor;
>> + __u32 code;
>> + int ret = 0;
>
> No need to initialise.
>
No. I need to initialize but to an error code !!! in case mipid02_find_sensor()
has failed.
So I change 'int ret = 0;' to 'int ret = -EINVAL;' and first ret affectation
from 'ret |= mipid02_configure_from_rx(bridge, code, sensor);' to
'ret = mipid02_configure_from_rx(bridge, code, sensor);'
>> +
>> + sensor = mipid02_find_sensor(bridge);
>> + if (!sensor)
>> + goto error;
>> +
>> + dev_dbg(&client->dev, "use sensor '%s'", sensor->name);
>> + memset(&bridge->r, 0, sizeof(bridge->r));
>> + /* build registers content */
>> + code = mipid02_get_source_code(bridge, sensor);
>> + ret |= mipid02_configure_from_rx(bridge, code, sensor);
>> + ret |= mipid02_configure_from_tx(bridge);
>> + ret |= mipid02_configure_from_code(bridge, code);
>> +
>> + /* write mipi registers */
>> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
>> + bridge->r.clk_lane_reg1);
>> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
>> + bridge->r.data_lane0_reg1);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
>> + DATA_MIPI_CSI);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
>> + bridge->r.data_lane1_reg1);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
>> + DATA_MIPI_CSI);
>> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG1,
>> + MODE_NO_BYPASS | bridge->r.mode_reg1);
>> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG2,
>> + bridge->r.mode_reg2);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
>> + bridge->r.data_id_rreg);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
>> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
>> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
>> + bridge->r.pix_width_ctrl);
>> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
>> + bridge->r.pix_width_ctrl_emb);
>
> Be careful with the error codes. ret will be returned by the s_stream
> callback below.
>
I didn't understand your remark. Can you elaborate a little bit more ?
>> +
>> + if (ret)
>> + goto error;
>> +
>> + return 0;
>> +
>> +error:
>> + dev_err(&client->dev, "failed to stream on %d", ret);
>> + mipid02_stream_disable(bridge);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret = 0;
>> +
>> + dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
>> + enable, bridge->streaming);
>> + mutex_lock(&bridge->lock);
>> +
>> + if (bridge->streaming == enable)
>> + goto out;
>> +
>> + ret = enable ? mipid02_stream_enable(bridge) :
>> + mipid02_stream_disable(bridge);
>> + if (!ret)
>> + bridge->streaming = enable;
>> +
>> +out:
>> + dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
>> + bridge->streaming, ret);
>> + mutex_unlock(&bridge->lock);
>> +
>> + return ret;
>> +}
>> +
>> +static const struct v4l2_subdev_video_ops mipid02_video_ops = {
>> + .s_stream = mipid02_s_stream,
>> +};
>> +
>> +static const struct v4l2_subdev_ops mipid02_subdev_ops = {
>> + .video = &mipid02_video_ops,
>> +};
>> +
>> +static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
>> + struct v4l2_subdev *s_subdev,
>> + struct v4l2_async_subdev *asd)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int source_pad;
>> +
>> + dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
>> +
>> + source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
>> + s_subdev->fwnode,
>> + MEDIA_PAD_FL_SOURCE);
>> + if (source_pad < 0) {
>> + dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
>> + s_subdev->name);
>> + return source_pad;
>> + }
>> +
>> + dev_dbg(&client->dev, "Bnd %s pad: %d\n", s_subdev->name, source_pad);
>
> How about simply "Bound"...? In case you need this.
>
I remove it.
>> +
>> + return media_create_pad_link(&s_subdev->entity, source_pad,
>> + &bridge->sd.entity, 0,
>> + MEDIA_LNK_FL_ENABLED |
>> + MEDIA_LNK_FL_IMMUTABLE);
>> +}
>> +
>> +static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
>> + .bound = mipid02_async_bound,
>> +};
>> +
>> +static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint ep;
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct device_node *ep_node;
>> + int ret;
>> +
>> + memset(&ep, 0, sizeof(ep));
>> + ep.bus_type = V4L2_MBUS_CSI2_DPHY;
>> + /* parse rx (endpoint 0) */
>> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
>> + 0, 0);
>> + if (!ep_node) {
>> + dev_err(&client->dev, "unable to find port0 ep");
>> + ret = -EINVAL;
>> + goto error;
>> + }
>> +
>> + ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
>> + if (ret) {
>> + dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
>> + ret);
>> + goto error_of_node_put;
>> + }
>> +
>> + /* do some sanity checks */
>> + if (ep.bus.mipi_csi2.num_data_lanes > 2) {
>> + dev_err(&client->dev, "max supported data lanes is 2 / got %d",
>> + ep.bus.mipi_csi2.num_data_lanes);
>> + ret = -EINVAL;
>> + goto error_v4l2_fwnode_endpoint_free;
>> + }
>> +
>> + /* register it for later use */
>> + bridge->rx = ep;
>> + bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
>> + &bridge->link_frequency : NULL;
>> + v4l2_fwnode_endpoint_free(&ep);
>> +
>> + /* register async notifier so we get noticed when sensor is connected */
>> + bridge->asd.match.fwnode =
>> + fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
>> + bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
>> + of_node_put(ep_node);
>> +
>> + v4l2_async_notifier_init(&bridge->notifier);
>> + ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
>> + if (ret) {
>> + dev_err(&client->dev, "fail to register asd to notifier %d",
>> + ret);
>> + goto error_fwnode_handle_put;
>> + }
>> + bridge->notifier.ops = &mipid02_notifier_ops;
>> +
>> + ret = v4l2_async_subdev_notifier_register(&bridge->sd,
>> + &bridge->notifier);
>> + if (ret)
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> +
>> + return ret;
>> +
>> +error_v4l2_fwnode_endpoint_free:
>> + v4l2_fwnode_endpoint_free(&ep);
>> +error_of_node_put:
>> + of_node_put(ep_node);
>> +error:
>> +
>> + return ret;
>> +
>> +error_fwnode_handle_put:
>> + fwnode_handle_put(bridge->asd.match.fwnode);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_fwnode_endpoint ep;
>> + struct device_node *ep_node;
>> + int ret;
>> +
>> + memset(&ep, 0, sizeof(ep));
>> + ep.bus_type = V4L2_MBUS_PARALLEL;
>
> You can set the field in variable declaration, and omit memset. The same in
> the function above.
>
According to v4l2_fwnode_endpoint_parse() documentation:
* This function parses the V4L2 fwnode endpoint specific parameters from the
* firmware. The caller is responsible for assigning @vep.bus_type to a valid
* media bus type. The caller may also set the default configuration for the
* endpoint
It seems safer to clear ep else it may select unwanted default configuration
for the endpoint ?

>> + /* parse tx (endpoint 2) */
>> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
>> + 2, 0);
>> + if (!ep_node) {
>> + dev_err(&client->dev, "unable to find port1 ep");
>> + ret = -EINVAL;
>> + goto error;
>> + }
>> +
>> + ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
>> + if (ret) {
>> + dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
>> + goto error_of_node_put;
>> + }
>> +
>> + of_node_put(ep_node);
>> + bridge->tx = ep;
>> +
>> + return 0;
>> +
>> +error_of_node_put:
>> + of_node_put(ep_node);
>> +error:
>> +
>> + return -EINVAL;
>> +}
>> +
>> +static int mipid02_probe(struct i2c_client *client,
>> + const struct i2c_device_id *id)
>> +{
>> + struct device *dev = &client->dev;
>> + struct mipid02_dev *bridge;
>> + u32 clk_freq;
>> + int ret;
>> +
>> + bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
>> + if (!bridge)
>> + return -ENOMEM;
>> +
>> + bridge->i2c_client = client;
>> + v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
>> +
>> + /* got and check clock */
>> + bridge->xclk = devm_clk_get(dev, "xclk");
>> + if (IS_ERR(bridge->xclk)) {
>> + dev_err(dev, "failed to get xclk\n");
>> + return PTR_ERR(bridge->xclk);
>> + }
>> +
>> + clk_freq = clk_get_rate(bridge->xclk);
>> + if (clk_freq < 6000000 || clk_freq > 27000000) {
>> + dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
>> + clk_freq);
>> + return -EINVAL;
>> + }
>> +
>> + bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
>> + GPIOD_OUT_HIGH);
>> +
>> + ret = mipid02_get_regulators(bridge);
>> + if (ret) {
>> + dev_err(dev, "failed to get regulators %d", ret);
>> + return ret;
>> + }
>> +
>> + mutex_init(&bridge->lock);
>> + bridge->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
>> + bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
>> + bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
>> + bridge->pad[1].flags = MEDIA_PAD_FL_SOURCE;
>> + ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
>> + bridge->pad);
>> + if (ret) {
>> + dev_err(&client->dev, "pads init failed %d", ret);
>> + return ret;
>> + }
>> +
>> + /* enable clock, power and reset device if available */
>> + ret = mipid02_set_power_on(bridge);
>> + if (ret)
>> + goto entity_cleanup;
>> +
>> + ret = mipid02_detect(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to detect mipid02 %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = mipid02_parse_tx_ep(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to parse tx %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = mipid02_parse_rx_ep(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to parse rx %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = v4l2_async_register_subdev(&bridge->sd);
>> + if (ret < 0) {
>> + dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
>> + ret);
>> + goto unregister_notifier;
>> + }
>> +
>> + dev_info(&client->dev, "mipid02 device probe successfully");
>> +
>> + return 0;
>> +
>> +unregister_notifier:
>> + v4l2_async_notifier_unregister(&bridge->notifier);
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> +power_off:
>> + mipid02_set_power_off(bridge);
>> +entity_cleanup:
>> + media_entity_cleanup(&bridge->sd.entity);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_remove(struct i2c_client *client)
>> +{
>> + struct v4l2_subdev *sd = i2c_get_clientdata(client);
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> +
>> + v4l2_async_notifier_unregister(&bridge->notifier);
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> + v4l2_async_unregister_subdev(&bridge->sd);
>> + mipid02_set_power_off(bridge);
>> + media_entity_cleanup(&bridge->sd.entity);
>> +
>> + return 0;
>> +}
>> +
>> +static const struct i2c_device_id mipid02_id[] = {
>> + {"st-mipid02", 0},
>
> Could you add whitespaces inside braces?
>
ok
>> + {},
>> +};
>> +MODULE_DEVICE_TABLE(i2c, mipid02_id);
>> +
>> +static const struct of_device_id mipid02_dt_ids[] = {
>> + { .compatible = "st,st-mipid02" },
>> + { /* sentinel */ }
>> +};
>> +MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
>> +
>> +static struct i2c_driver mipid02_i2c_driver = {
>> + .driver = {
>> + .name = "st-mipid02",
>> + .of_match_table = mipid02_dt_ids,
>> + },
>> + .id_table = mipid02_id,
>> + .probe = mipid02_probe,
>> + .remove = mipid02_remove,
>> +};
>> +
>> +module_i2c_driver(mipid02_i2c_driver);
>> +
>> +MODULE_AUTHOR("Mickael Guene <[email protected]>");
>> +MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
>> +MODULE_LICENSE("GPL v2");
>

2019-03-18 10:03:27

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v1 3/3] media: MAINTAINERS: add entry for STMicroelectronics MIPID02 media driver

Sakari,

On 3/16/19 22:47, Sakari Ailus wrote:
> On Tue, Mar 12, 2019 at 07:44:05AM +0100, Mickael Guene wrote:
>> Add maintainer entry for the STMicroelectronics MIPID02 CSI-2 to PARALLEL
>> bridge driver and dt-bindings.
>>
>> Signed-off-by: Mickael Guene <[email protected]>
>> ---
>>
>> MAINTAINERS | 8 ++++++++
>> 1 file changed, 8 insertions(+)
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 1c6ecae..4bd36b1 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -14424,6 +14424,14 @@ S: Maintained
>> F: drivers/iio/imu/st_lsm6dsx/
>> F: Documentation/devicetree/bindings/iio/imu/st_lsm6dsx.txt
>>
>> +ST MIPID02 CSI-2 TO PARALLEL BRIDGE DRIVER
>> +M: Mickael Guene <[email protected]>
>> +L: [email protected]
>> +T: git git://linuxtv.org/media_tree.git
>> +S: Maintained
>> +F: drivers/media/i2c/st-mipid02.c
>> +F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>
> Could you squash this to the first patch, so that there are no files added
> without them being listed here?
>
Ok for the v2 I will squash it with dt-bindings documentation patch.

2019-03-25 07:56:39

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v2 1/2] dt-bindings: Document MIPID02 bindings

This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
bridge.

Signed-off-by: Mickael Guene <[email protected]>
---

Changes in v2:
- Add precision about first CSI-2 port data rate
- Document endpoints supported properties
- Rename 'mipid02@14' into generic 'csi2rx@14' in example

.../bindings/media/i2c/st,st-mipid02.txt | 83 ++++++++++++++++++++++
MAINTAINERS | 7 ++
2 files changed, 90 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt

diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
new file mode 100644
index 0000000..dfeab45
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
@@ -0,0 +1,83 @@
+STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
+
+MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
+time. Active port input stream will be de-serialized and its content outputted
+through PARALLEL output port.
+CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
+input port is a single lane 800Mbps. Both ports support clock and data lane
+polarity swap. First port also supports data lane swap.
+PARALLEL output port has a maximum width of 12 bits.
+Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
+YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
+
+Required Properties:
+- compatible: should be "st,st-mipid02"
+- clocks: reference to the xclk input clock.
+- clock-names: should be "xclk".
+- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
+- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
+
+Optional Properties:
+- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
+ This is an active low signal to the mipid02.
+
+Required subnodes:
+ - ports: A ports node with one port child node per device input and output
+ port, in accordance with the video interface bindings defined in
+ Documentation/devicetree/bindings/media/video-interfaces.txt. The
+ port nodes are numbered as follows:
+
+ Port Description
+ -----------------------------
+ 0 CSI-2 first input port
+ 1 CSI-2 second input port
+ 2 PARALLEL output
+
+Endpoint node optional properties for CSI-2 connection are:
+- bus-type: if present should be 4 - MIPI CSI-2 D-PHY.
+- clock-lanes: should be set to <0> if present (clock lane on hardware lane 0).
+- data-lanes: if present should be <1> for Port 1. for Port 0 dual-lane
+operation should be <1 2> or <2 1>. For Port 0 single-lane operation should be
+<1> or <2>.
+- lane-polarities: any lane can be inverted.
+
+Endpoint node optional properties for PARALLEL connection are:
+- bus-type: if present should be 5 - Parallel.
+- bus-width: shall be set to <6>, <7>, <8>, <10> or <12>.
+- hsync-active: active state of the HSYNC signal, 0/1 for LOW/HIGH respectively.
+- vsync-active: active state of the VSYNC signal, 0/1 for LOW/HIGH respectively.
+
+Example:
+
+mipid02: csi2rx@14 {
+ compatible = "st,st-mipid02";
+ reg = <0x14>;
+ status = "okay";
+ clocks = <&clk_ext_camera_12>;
+ clock-names = "xclk";
+ VDDE-supply = <&vdd>;
+ VDDIN-supply = <&vdd>;
+ ports {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ port@0 {
+ reg = <0>;
+
+ ep0: endpoint {
+ clock-lanes = <0>;
+ data-lanes = <1 2>;
+ remote-endpoint = <&mipi_csi2_in>;
+ };
+ };
+ port@2 {
+ reg = <2>;
+
+ ep2: endpoint {
+ bus-width = <8>;
+ hsync-active = <0>;
+ vsync-active = <0>;
+ remote-endpoint = <&parallel_out>;
+ };
+ };
+ };
+};
diff --git a/MAINTAINERS b/MAINTAINERS
index e17ebf7..74da99d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14668,6 +14668,13 @@ S: Maintained
F: drivers/iio/imu/st_lsm6dsx/
F: Documentation/devicetree/bindings/iio/imu/st_lsm6dsx.txt

+ST MIPID02 CSI-2 TO PARALLEL BRIDGE DRIVER
+M: Mickael Guene <[email protected]>
+L: [email protected]
+T: git git://linuxtv.org/media_tree.git
+S: Maintained
+F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
+
ST STM32 I2C/SMBUS DRIVER
M: Pierre-Yves MORDRET <[email protected]>
L: [email protected]
--
2.7.4


2019-03-25 07:57:05

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v2 0/2] Add support for MIPID02 CSI-2 to PARALLEL bridge I2C device

Hi,

This is the v2 of my MIPID02 series which introduces support of
STMicroelectronics MIPID02 CSI-2 to PARALLEL I2C bridge. It allows using a CSI-2
sensor with a PARALLEL interface. Current driver implementation doesn't support
CSI-2 second input port usage. It doesn't support also YUV420, RGB565 and RGB444
input formats.

Thanks to Sakari for v1 series review.

Changes in v2:
- Add precision about first CSI-2 port data rate
- Document endpoints supported properties
- Rename 'mipid02@14' into generic 'csi2rx@14' in example
- Merge MAINTAINERS patch 3 into patch 1 and 2
- Fix line too long in Kconfig
- Add missing delay after reset release
- Various style fixes
- Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed

Mickael Guene (2):
dt-bindings: Document MIPID02 bindings
media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

.../bindings/media/i2c/st,st-mipid02.txt | 83 ++
MAINTAINERS | 8 +
drivers/media/i2c/Kconfig | 14 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/st-mipid02.c | 877 +++++++++++++++++++++
5 files changed, 983 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
create mode 100644 drivers/media/i2c/st-mipid02.c

--
2.7.4


2019-03-25 07:57:52

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v2 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

This V4L2 subdev driver enables STMicroelectronics MIPID02 device.

Signed-off-by: Mickael Guene <[email protected]>
---

Changes in v2:
- Merge MAINTAINERS patch 3 into patch 1 and 2
- Fix line too long in Kconfig
- Add missing delay after reset release
- Various style fixes
- Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed

MAINTAINERS | 1 +
drivers/media/i2c/Kconfig | 14 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/st-mipid02.c | 877 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 893 insertions(+)
create mode 100644 drivers/media/i2c/st-mipid02.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 74da99d..a14fe81 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14673,6 +14673,7 @@ M: Mickael Guene <[email protected]>
L: [email protected]
T: git git://linuxtv.org/media_tree.git
S: Maintained
+F: drivers/media/i2c/st-mipid02.c
F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt

ST STM32 I2C/SMBUS DRIVER
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 1ef6335..be2470b 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -1135,6 +1135,20 @@ config VIDEO_I2C
To compile this driver as a module, choose M here: the
module will be called video-i2c

+config VIDEO_ST_MIPID02
+ tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
+ depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
+ depends on MEDIA_CAMERA_SUPPORT
+ depends on MEDIA_CONTROLLER
+ select V4L2_FWNODE
+ help
+ Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
+ It is used to allow usage of CSI-2 sensor with PARALLEL port
+ controller.
+
+ To compile this driver as a module, choose M here: the
+ module will be called st-mipid02.
+
endmenu

endif
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index a64fca8..d8ad9da 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
obj-$(CONFIG_VIDEO_IMX274) += imx274.o
obj-$(CONFIG_VIDEO_IMX319) += imx319.o
obj-$(CONFIG_VIDEO_IMX355) += imx355.o
+obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o

obj-$(CONFIG_SDR_MAX2175) += max2175.o
diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
new file mode 100644
index 0000000..61dcb83
--- /dev/null
+++ b/drivers/media/i2c/st-mipid02.c
@@ -0,0 +1,877 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
+ *
+ * Copyright (C) STMicroelectronics SA 2019
+ * Authors: Mickael Guene <[email protected]>
+ * for STMicroelectronics.
+ *
+ *
+ */
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/of_graph.h>
+#include <linux/regulator/consumer.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+
+#define MIPID02_CLK_LANE_WR_REG1 0x01
+#define MIPID02_CLK_LANE_REG1 0x02
+#define MIPID02_CLK_LANE_REG3 0x04
+#define MIPID02_DATA_LANE0_REG1 0x05
+#define MIPID02_DATA_LANE0_REG2 0x06
+#define MIPID02_DATA_LANE1_REG1 0x09
+#define MIPID02_DATA_LANE1_REG2 0x0a
+#define MIPID02_MODE_REG1 0x14
+#define MIPID02_MODE_REG2 0x15
+#define MIPID02_DATA_ID_RREG 0x17
+#define MIPID02_DATA_SELECTION_CTRL 0x19
+#define MIPID02_PIX_WIDTH_CTRL 0x1e
+#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
+
+/* Bits definition for MIPID02_CLK_LANE_REG1 */
+#define CLK_ENABLE BIT(0)
+/* Bits definition for MIPID02_CLK_LANE_REG3 */
+#define CLK_MIPI_CSI BIT(1)
+/* Bits definition for MIPID02_DATA_LANE0_REG1 */
+#define DATA_ENABLE BIT(0)
+/* Bits definition for MIPID02_DATA_LANEx_REG2 */
+#define DATA_MIPI_CSI BIT(0)
+/* Bits definition for MIPID02_MODE_REG1 */
+#define MODE_DATA_SWAP BIT(2)
+#define MODE_NO_BYPASS BIT(6)
+/* Bits definition for MIPID02_MODE_REG2 */
+#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
+#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
+/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
+#define SELECTION_MANUAL_DATA BIT(2)
+#define SELECTION_MANUAL_WIDTH BIT(3)
+
+/* regulator supplies */
+static const char * const mipid02_supply_name[] = {
+ "VDDE", /* 1.8V digital I/O supply */
+ "VDDIN", /* 1V8 voltage regulator supply */
+};
+
+#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
+#define MIPID02_PAD_NB 2
+
+struct mipid02_dev {
+ struct i2c_client *i2c_client;
+ struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
+ struct v4l2_subdev sd;
+ struct media_pad pad[MIPID02_PAD_NB];
+ struct clk *xclk;
+ struct gpio_desc *reset_gpio;
+ /* Protect streaming */
+ struct mutex lock;
+ bool streaming;
+ /* endpoints info */
+ struct v4l2_fwnode_endpoint rx;
+ u64 link_frequency;
+ struct v4l2_fwnode_endpoint tx;
+ /* remote source */
+ struct v4l2_async_subdev asd;
+ struct v4l2_async_notifier notifier;
+ /* registers */
+ struct {
+ u8 clk_lane_reg1;
+ u8 data_lane0_reg1;
+ u8 data_lane1_reg1;
+ u8 mode_reg1;
+ u8 mode_reg2;
+ u8 data_id_rreg;
+ u8 pix_width_ctrl;
+ u8 pix_width_ctrl_emb;
+ } r;
+};
+
+static int bpp_from_code(__u32 code)
+{
+ switch (code) {
+ case MEDIA_BUS_FMT_SBGGR8_1X8:
+ case MEDIA_BUS_FMT_SGBRG8_1X8:
+ case MEDIA_BUS_FMT_SGRBG8_1X8:
+ case MEDIA_BUS_FMT_SRGGB8_1X8:
+ return 8;
+ case MEDIA_BUS_FMT_SBGGR10_1X10:
+ case MEDIA_BUS_FMT_SGBRG10_1X10:
+ case MEDIA_BUS_FMT_SGRBG10_1X10:
+ case MEDIA_BUS_FMT_SRGGB10_1X10:
+ return 10;
+ case MEDIA_BUS_FMT_SBGGR12_1X12:
+ case MEDIA_BUS_FMT_SGBRG12_1X12:
+ case MEDIA_BUS_FMT_SGRBG12_1X12:
+ case MEDIA_BUS_FMT_SRGGB12_1X12:
+ return 12;
+ case MEDIA_BUS_FMT_UYVY8_2X8:
+ return 16;
+ case MEDIA_BUS_FMT_BGR888_1X24:
+ return 24;
+ default:
+ return 0;
+ }
+}
+
+static u8 data_type_from_code(__u32 code)
+{
+ switch (code) {
+ case MEDIA_BUS_FMT_SBGGR8_1X8:
+ case MEDIA_BUS_FMT_SGBRG8_1X8:
+ case MEDIA_BUS_FMT_SGRBG8_1X8:
+ case MEDIA_BUS_FMT_SRGGB8_1X8:
+ return 0x2a;
+ case MEDIA_BUS_FMT_SBGGR10_1X10:
+ case MEDIA_BUS_FMT_SGBRG10_1X10:
+ case MEDIA_BUS_FMT_SGRBG10_1X10:
+ case MEDIA_BUS_FMT_SRGGB10_1X10:
+ return 0x2b;
+ case MEDIA_BUS_FMT_SBGGR12_1X12:
+ case MEDIA_BUS_FMT_SGBRG12_1X12:
+ case MEDIA_BUS_FMT_SGRBG12_1X12:
+ case MEDIA_BUS_FMT_SRGGB12_1X12:
+ return 0x2c;
+ case MEDIA_BUS_FMT_UYVY8_2X8:
+ return 0x1e;
+ case MEDIA_BUS_FMT_BGR888_1X24:
+ return 0x24;
+ default:
+ return 0;
+ }
+}
+
+static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
+{
+ return container_of(sd, struct mipid02_dev, sd);
+}
+
+static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct i2c_msg msg[2];
+ u8 buf[2];
+ int ret;
+
+ buf[0] = reg >> 8;
+ buf[1] = reg & 0xff;
+
+ msg[0].addr = client->addr;
+ msg[0].flags = client->flags;
+ msg[0].buf = buf;
+ msg[0].len = sizeof(buf);
+
+ msg[1].addr = client->addr;
+ msg[1].flags = client->flags | I2C_M_RD;
+ msg[1].buf = val;
+ msg[1].len = 1;
+
+ ret = i2c_transfer(client->adapter, msg, 2);
+ if (ret < 0) {
+ dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
+ __func__, client->addr, reg, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct i2c_msg msg;
+ u8 buf[3];
+ int ret;
+
+ buf[0] = reg >> 8;
+ buf[1] = reg & 0xff;
+ buf[2] = val;
+
+ msg.addr = client->addr;
+ msg.flags = client->flags;
+ msg.buf = buf;
+ msg.len = sizeof(buf);
+
+ ret = i2c_transfer(client->adapter, &msg, 1);
+ if (ret < 0) {
+ dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
+ __func__, reg, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mipid02_get_regulators(struct mipid02_dev *bridge)
+{
+ unsigned int i;
+
+ for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
+ bridge->supplies[i].supply = mipid02_supply_name[i];
+
+ return devm_regulator_bulk_get(&bridge->i2c_client->dev,
+ MIPID02_NUM_SUPPLIES,
+ bridge->supplies);
+}
+
+static void mipid02_apply_reset(struct mipid02_dev *bridge)
+{
+ gpiod_set_value_cansleep(bridge->reset_gpio, 0);
+ usleep_range(5000, 10000);
+ gpiod_set_value_cansleep(bridge->reset_gpio, 1);
+ usleep_range(5000, 10000);
+ gpiod_set_value_cansleep(bridge->reset_gpio, 0);
+ usleep_range(5000, 10000);
+}
+
+static int mipid02_set_power_on(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret;
+
+ ret = clk_prepare_enable(bridge->xclk);
+ if (ret) {
+ dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
+ return ret;
+ }
+
+ ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
+ bridge->supplies);
+ if (ret) {
+ dev_err(&client->dev, "%s: failed to enable regulators\n",
+ __func__);
+ goto xclk_off;
+ }
+
+ if (bridge->reset_gpio) {
+ dev_dbg(&client->dev, "apply reset");
+ mipid02_apply_reset(bridge);
+ } else {
+ dev_dbg(&client->dev, "dont apply reset");
+ usleep_range(5000, 10000);
+ }
+
+ return 0;
+
+xclk_off:
+ clk_disable_unprepare(bridge->xclk);
+ return ret;
+}
+
+static void mipid02_set_power_off(struct mipid02_dev *bridge)
+{
+ regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
+ clk_disable_unprepare(bridge->xclk);
+}
+
+static int mipid02_detect(struct mipid02_dev *bridge)
+{
+ u8 reg;
+
+ /*
+ * There is no version registers. Just try to read register
+ * MIPID02_CLK_LANE_WR_REG1.
+ */
+ return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
+}
+
+static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+
+ if (ep->nr_of_link_frequencies != 1)
+ return 0;
+
+ return ep->link_frequencies[0];
+}
+
+static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
+ struct v4l2_subdev *sensor)
+{
+ struct v4l2_ctrl *ctrl;
+
+ ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_LINK_FREQ);
+ if (!ctrl)
+ return 0;
+
+ return v4l2_ctrl_g_ctrl_int64(ctrl);
+}
+
+static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
+ __u32 code,
+ struct v4l2_subdev *sensor)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ struct v4l2_ctrl *ctrl;
+ u32 pixel_clock;
+ u32 bpp = bpp_from_code(code);
+
+ ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_PIXEL_RATE);
+ if (!ctrl)
+ return 0;
+ pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
+
+ return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
+}
+
+/*
+ * We need to know link frequency to setup clk_lane_reg1 timings. So use
+ * following sequence, stop when we got a frequency.
+ * - Get it from device tree. It allow to connect sensor without support of
+ * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
+ * is that is that link frequency must be constant.
+ * - Get it from V4L2_CID_LINK_FREQ.
+ * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
+ * to compute it.
+ */
+static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
+ __u32 code,
+ struct v4l2_subdev *sensor)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ u32 link_freq;
+
+ link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
+ if (link_freq)
+ goto success;
+
+ link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, sensor);
+ if (link_freq)
+ goto success;
+
+ link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, code,
+ sensor);
+ if (link_freq)
+ goto success;
+ dev_err(&client->dev, "Failed to detect link frequency");
+
+ return -EINVAL;
+
+success:
+ dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
+ bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
+
+ return 0;
+}
+
+static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ bool *polarities = ep->bus.mipi_csi2.lane_polarities;
+
+ /* midid02 doesn't support clock lane remapping */
+ if (ep->bus.mipi_csi2.clock_lane != 0) {
+ dev_err(&client->dev, "clk lane must be map to lane 0\n");
+ return -EINVAL;
+ }
+ bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
+ bool are_lanes_swap, bool *polarities)
+{
+ bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
+
+ if (nb == 1 && are_lanes_swap)
+ return 0;
+
+ /*
+ * data lane 0 as pin swap polarity reversed compared to clock and
+ *data lane 1
+ */
+ if (!are_pin_swap)
+ bridge->r.data_lane0_reg1 = 1 << 1;
+ bridge->r.data_lane0_reg1 |= DATA_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
+ bool are_lanes_swap, bool *polarities)
+{
+ bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
+
+ if (nb == 1 && !are_lanes_swap)
+ return 0;
+
+ if (are_pin_swap)
+ bridge->r.data_lane1_reg1 = 1 << 1;
+ bridge->r.data_lane1_reg1 |= DATA_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_from_rx(struct mipid02_dev *bridge, __u32 code,
+ struct v4l2_subdev *sensor)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
+ bool *polarities = ep->bus.mipi_csi2.lane_polarities;
+ int nb = ep->bus.mipi_csi2.num_data_lanes;
+ int ret;
+
+ ret = mipid02_configure_clk_lane(bridge);
+ if (ret)
+ return ret;
+
+ ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
+ polarities);
+ if (ret)
+ return ret;
+
+ ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
+ polarities);
+ if (ret)
+ return ret;
+
+ bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
+ bridge->r.mode_reg1 |= (nb - 1) << 1;
+
+ return mipid02_configure_from_rx_speed(bridge, code, sensor);
+}
+
+static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->tx;
+
+ bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
+ bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
+ if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
+ bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
+ if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
+ bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
+
+ return 0;
+}
+
+static int mipid02_configure_from_code(struct mipid02_dev *bridge, __u32 code)
+{
+ u8 data_type;
+
+ bridge->r.data_id_rreg = 0;
+ data_type = data_type_from_code(code);
+ if (!data_type)
+ return -EINVAL;
+ bridge->r.data_id_rreg = data_type;
+
+ return 0;
+}
+
+static __u32 mipid02_get_source_code(struct mipid02_dev *bridge,
+ struct v4l2_subdev *sensor)
+{
+ struct v4l2_subdev_format fmt = {
+ .which = V4L2_SUBDEV_FORMAT_ACTIVE,
+ };
+ int ret;
+
+ ret = v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt);
+
+ return ret ? MEDIA_BUS_FMT_SBGGR8_1X8 : fmt.format.code;
+}
+
+static int mipid02_stream_disable(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret;
+
+ /* Disable all lanes */
+ ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
+ if (ret)
+ dev_err(&client->dev, "failed to stream off %d", ret);
+
+ return ret;
+}
+
+static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
+{
+ struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
+ struct media_entity *entity;
+
+ if (!mdev)
+ return NULL;
+
+ media_device_for_each_entity(entity, mdev)
+ if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
+ return media_entity_to_v4l2_subdev(entity);
+
+ return NULL;
+}
+
+static int mipid02_stream_enable(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_subdev *sensor;
+ __u32 code;
+ int ret = -EINVAL;
+
+ sensor = mipid02_find_sensor(bridge);
+ if (!sensor)
+ goto error;
+
+ dev_dbg(&client->dev, "use sensor '%s'", sensor->name);
+ memset(&bridge->r, 0, sizeof(bridge->r));
+ /* build registers content */
+ code = mipid02_get_source_code(bridge, sensor);
+ ret = mipid02_configure_from_rx(bridge, code, sensor);
+ ret |= mipid02_configure_from_tx(bridge);
+ ret |= mipid02_configure_from_code(bridge, code);
+
+ /* write mipi registers */
+ ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
+ bridge->r.clk_lane_reg1);
+ ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
+ bridge->r.data_lane0_reg1);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
+ DATA_MIPI_CSI);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
+ bridge->r.data_lane1_reg1);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
+ DATA_MIPI_CSI);
+ ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG1,
+ MODE_NO_BYPASS | bridge->r.mode_reg1);
+ ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG2,
+ bridge->r.mode_reg2);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
+ bridge->r.data_id_rreg);
+ ret |= mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
+ SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
+ ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
+ bridge->r.pix_width_ctrl);
+ ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
+ bridge->r.pix_width_ctrl_emb);
+
+ if (ret)
+ goto error;
+
+ return 0;
+
+error:
+ dev_err(&client->dev, "failed to stream on %d", ret);
+ mipid02_stream_disable(bridge);
+
+ return ret;
+}
+
+static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int ret = 0;
+
+ dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
+ enable, bridge->streaming);
+ mutex_lock(&bridge->lock);
+
+ if (bridge->streaming == enable)
+ goto out;
+
+ ret = enable ? mipid02_stream_enable(bridge) :
+ mipid02_stream_disable(bridge);
+ if (!ret)
+ bridge->streaming = enable;
+
+out:
+ dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
+ bridge->streaming, ret);
+ mutex_unlock(&bridge->lock);
+
+ return ret;
+}
+
+static const struct v4l2_subdev_video_ops mipid02_video_ops = {
+ .s_stream = mipid02_s_stream,
+};
+
+static const struct v4l2_subdev_ops mipid02_subdev_ops = {
+ .video = &mipid02_video_ops,
+};
+
+static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
+ struct v4l2_subdev *s_subdev,
+ struct v4l2_async_subdev *asd)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int source_pad;
+
+ dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
+
+ source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
+ s_subdev->fwnode,
+ MEDIA_PAD_FL_SOURCE);
+ if (source_pad < 0) {
+ dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
+ s_subdev->name);
+ return source_pad;
+ }
+
+ return media_create_pad_link(&s_subdev->entity, source_pad,
+ &bridge->sd.entity, 0,
+ MEDIA_LNK_FL_ENABLED |
+ MEDIA_LNK_FL_IMMUTABLE);
+}
+
+static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
+ .bound = mipid02_async_bound,
+};
+
+static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint ep;
+ struct i2c_client *client = bridge->i2c_client;
+ struct device_node *ep_node;
+ int ret;
+
+ memset(&ep, 0, sizeof(ep));
+ ep.bus_type = V4L2_MBUS_CSI2_DPHY;
+ /* parse rx (endpoint 0) */
+ ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
+ 0, 0);
+ if (!ep_node) {
+ dev_err(&client->dev, "unable to find port0 ep");
+ ret = -EINVAL;
+ goto error;
+ }
+
+ ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
+ if (ret) {
+ dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
+ ret);
+ goto error_of_node_put;
+ }
+
+ /* do some sanity checks */
+ if (ep.bus.mipi_csi2.num_data_lanes > 2) {
+ dev_err(&client->dev, "max supported data lanes is 2 / got %d",
+ ep.bus.mipi_csi2.num_data_lanes);
+ ret = -EINVAL;
+ goto error_v4l2_fwnode_endpoint_free;
+ }
+
+ /* register it for later use */
+ bridge->rx = ep;
+ bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
+ &bridge->link_frequency : NULL;
+ v4l2_fwnode_endpoint_free(&ep);
+
+ /* register async notifier so we get noticed when sensor is connected */
+ bridge->asd.match.fwnode =
+ fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
+ bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
+ of_node_put(ep_node);
+
+ v4l2_async_notifier_init(&bridge->notifier);
+ ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
+ if (ret) {
+ dev_err(&client->dev, "fail to register asd to notifier %d",
+ ret);
+ goto error_fwnode_handle_put;
+ }
+ bridge->notifier.ops = &mipid02_notifier_ops;
+
+ ret = v4l2_async_subdev_notifier_register(&bridge->sd,
+ &bridge->notifier);
+ if (ret)
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+
+ return ret;
+
+error_v4l2_fwnode_endpoint_free:
+ v4l2_fwnode_endpoint_free(&ep);
+error_of_node_put:
+ of_node_put(ep_node);
+error:
+
+ return ret;
+
+error_fwnode_handle_put:
+ fwnode_handle_put(bridge->asd.match.fwnode);
+
+ return ret;
+}
+
+static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_fwnode_endpoint ep;
+ struct device_node *ep_node;
+ int ret;
+
+ memset(&ep, 0, sizeof(ep));
+ ep.bus_type = V4L2_MBUS_PARALLEL;
+ /* parse tx (endpoint 2) */
+ ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
+ 2, 0);
+ if (!ep_node) {
+ dev_err(&client->dev, "unable to find port1 ep");
+ ret = -EINVAL;
+ goto error;
+ }
+
+ ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
+ if (ret) {
+ dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
+ goto error_of_node_put;
+ }
+
+ of_node_put(ep_node);
+ bridge->tx = ep;
+
+ return 0;
+
+error_of_node_put:
+ of_node_put(ep_node);
+error:
+
+ return -EINVAL;
+}
+
+static int mipid02_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ struct device *dev = &client->dev;
+ struct mipid02_dev *bridge;
+ u32 clk_freq;
+ int ret;
+
+ bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
+ if (!bridge)
+ return -ENOMEM;
+
+ bridge->i2c_client = client;
+ v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
+
+ /* got and check clock */
+ bridge->xclk = devm_clk_get(dev, "xclk");
+ if (IS_ERR(bridge->xclk)) {
+ dev_err(dev, "failed to get xclk\n");
+ return PTR_ERR(bridge->xclk);
+ }
+
+ clk_freq = clk_get_rate(bridge->xclk);
+ if (clk_freq < 6000000 || clk_freq > 27000000) {
+ dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
+ clk_freq);
+ return -EINVAL;
+ }
+
+ bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
+ GPIOD_OUT_HIGH);
+
+ ret = mipid02_get_regulators(bridge);
+ if (ret) {
+ dev_err(dev, "failed to get regulators %d", ret);
+ return ret;
+ }
+
+ mutex_init(&bridge->lock);
+ bridge->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
+ bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
+ bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
+ bridge->pad[1].flags = MEDIA_PAD_FL_SOURCE;
+ ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
+ bridge->pad);
+ if (ret) {
+ dev_err(&client->dev, "pads init failed %d", ret);
+ return ret;
+ }
+
+ /* enable clock, power and reset device if available */
+ ret = mipid02_set_power_on(bridge);
+ if (ret)
+ goto entity_cleanup;
+
+ ret = mipid02_detect(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to detect mipid02 %d", ret);
+ goto power_off;
+ }
+
+ ret = mipid02_parse_tx_ep(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to parse tx %d", ret);
+ goto power_off;
+ }
+
+ ret = mipid02_parse_rx_ep(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to parse rx %d", ret);
+ goto power_off;
+ }
+
+ ret = v4l2_async_register_subdev(&bridge->sd);
+ if (ret < 0) {
+ dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
+ ret);
+ goto unregister_notifier;
+ }
+
+ dev_info(&client->dev, "mipid02 device probe successfully");
+
+ return 0;
+
+unregister_notifier:
+ v4l2_async_notifier_unregister(&bridge->notifier);
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+power_off:
+ mipid02_set_power_off(bridge);
+entity_cleanup:
+ media_entity_cleanup(&bridge->sd.entity);
+
+ return ret;
+}
+
+static int mipid02_remove(struct i2c_client *client)
+{
+ struct v4l2_subdev *sd = i2c_get_clientdata(client);
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+
+ v4l2_async_notifier_unregister(&bridge->notifier);
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+ v4l2_async_unregister_subdev(&bridge->sd);
+ mipid02_set_power_off(bridge);
+ media_entity_cleanup(&bridge->sd.entity);
+
+ return 0;
+}
+
+static const struct i2c_device_id mipid02_id[] = {
+ { "st-mipid02", 0},
+ {},
+};
+MODULE_DEVICE_TABLE(i2c, mipid02_id);
+
+static const struct of_device_id mipid02_dt_ids[] = {
+ { .compatible = "st,st-mipid02" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
+
+static struct i2c_driver mipid02_i2c_driver = {
+ .driver = {
+ .name = "st-mipid02",
+ .of_match_table = mipid02_dt_ids,
+ },
+ .id_table = mipid02_id,
+ .probe = mipid02_probe,
+ .remove = mipid02_remove,
+};
+
+module_i2c_driver(mipid02_i2c_driver);
+
+MODULE_AUTHOR("Mickael Guene <[email protected]>");
+MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
+MODULE_LICENSE("GPL v2");
--
2.7.4


2019-03-25 11:20:16

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v1 2/3] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Mickael,

On Mon, Mar 18, 2019 at 09:57:44AM +0000, Mickael GUENE wrote:
> Hi Sakari,
>
> Thanks for your review. Find my comments below.
>
> On 3/16/19 23:14, Sakari Ailus wrote:
...
> >> +static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
> >> +{
> >> + struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
> >> + struct media_entity *entity;
> >> +
> >> + if (!mdev)
> >> + return NULL;
> >> +
> >> + media_device_for_each_entity(entity, mdev)
> >> + if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
> >> + return media_entity_to_v4l2_subdev(entity);
> >
> > Hmm. Could you instead use the link state to determine which of the
> > receivers is active? You'll need one more pad, and then you'd had 1:1
> > mapping between ports and pads.
> >
> Goal here is not to detect which of the receivers is active but to find
> sensor in case there are others sub-dev in chain (for example a
> serializer/deserializer as found in cars).

You shouldn't make assumptions on the rest of the pipeline beyond the
device that's directly connected. You might not even have a camera there.

> For the moment the driver doesn't support second input port usage,
> this is why there is no such second sink pad yet in the driver.

Could you add the second sink pad now, so that the uAPI remains the same
when you add support for it? Nothing is connected to it but I don't think
it's an issue.

...

> >> +
> >> + sensor = mipid02_find_sensor(bridge);
> >> + if (!sensor)
> >> + goto error;
> >> +
> >> + dev_dbg(&client->dev, "use sensor '%s'", sensor->name);
> >> + memset(&bridge->r, 0, sizeof(bridge->r));
> >> + /* build registers content */
> >> + code = mipid02_get_source_code(bridge, sensor);
> >> + ret |= mipid02_configure_from_rx(bridge, code, sensor);
> >> + ret |= mipid02_configure_from_tx(bridge);
> >> + ret |= mipid02_configure_from_code(bridge, code);
> >> +
> >> + /* write mipi registers */
> >> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
> >> + bridge->r.clk_lane_reg1);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
> >> + bridge->r.data_lane0_reg1);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
> >> + DATA_MIPI_CSI);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
> >> + bridge->r.data_lane1_reg1);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
> >> + DATA_MIPI_CSI);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG1,
> >> + MODE_NO_BYPASS | bridge->r.mode_reg1);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG2,
> >> + bridge->r.mode_reg2);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
> >> + bridge->r.data_id_rreg);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
> >> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
> >> + bridge->r.pix_width_ctrl);
> >> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
> >> + bridge->r.pix_width_ctrl_emb);
> >
> > Be careful with the error codes. ret will be returned by the s_stream
> > callback below.
> >
> I didn't understand your remark. Can you elaborate a little bit more ?

If the functions return different error codes, then ret possibly won't be a
valid error code, or at least it's not going to be what it was intended to
do. You'll need to stop when you encounter an error and then return it to
the caller.

...

> >> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
> >> +{
> >> + struct i2c_client *client = bridge->i2c_client;
> >> + struct v4l2_fwnode_endpoint ep;
> >> + struct device_node *ep_node;
> >> + int ret;
> >> +
> >> + memset(&ep, 0, sizeof(ep));
> >> + ep.bus_type = V4L2_MBUS_PARALLEL;
> >
> > You can set the field in variable declaration, and omit memset. The same in
> > the function above.
> >
> According to v4l2_fwnode_endpoint_parse() documentation:
> * This function parses the V4L2 fwnode endpoint specific parameters from the
> * firmware. The caller is responsible for assigning @vep.bus_type to a valid
> * media bus type. The caller may also set the default configuration for the
> * endpoint
> It seems safer to clear ep else it may select unwanted default configuration
> for the endpoint ?

By setting one of the fields in a struct in declaration, the rest will be
zeroed by the compiler. That's from the C standard.

--
Kind regards,

Sakari Ailus
[email protected]

2019-03-25 11:46:51

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Mickael,

A few comments below. This starts looking pretty good IMO, I missed a few
things the last time around. Please also see my reply on v1.

On Mon, Mar 25, 2019 at 08:55:10AM +0100, Mickael Guene wrote:
> This V4L2 subdev driver enables STMicroelectronics MIPID02 device.
>
> Signed-off-by: Mickael Guene <[email protected]>
> ---
>
> Changes in v2:
> - Merge MAINTAINERS patch 3 into patch 1 and 2
> - Fix line too long in Kconfig
> - Add missing delay after reset release
> - Various style fixes
> - Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed
>
> MAINTAINERS | 1 +
> drivers/media/i2c/Kconfig | 14 +
> drivers/media/i2c/Makefile | 1 +
> drivers/media/i2c/st-mipid02.c | 877 +++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 893 insertions(+)
> create mode 100644 drivers/media/i2c/st-mipid02.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 74da99d..a14fe81 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -14673,6 +14673,7 @@ M: Mickael Guene <[email protected]>
> L: [email protected]
> T: git git://linuxtv.org/media_tree.git
> S: Maintained
> +F: drivers/media/i2c/st-mipid02.c
> F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>
> ST STM32 I2C/SMBUS DRIVER
> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
> index 1ef6335..be2470b 100644
> --- a/drivers/media/i2c/Kconfig
> +++ b/drivers/media/i2c/Kconfig
> @@ -1135,6 +1135,20 @@ config VIDEO_I2C
> To compile this driver as a module, choose M here: the
> module will be called video-i2c
>
> +config VIDEO_ST_MIPID02
> + tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
> + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
> + depends on MEDIA_CAMERA_SUPPORT
> + depends on MEDIA_CONTROLLER
> + select V4L2_FWNODE
> + help
> + Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
> + It is used to allow usage of CSI-2 sensor with PARALLEL port
> + controller.
> +
> + To compile this driver as a module, choose M here: the
> + module will be called st-mipid02.
> +
> endmenu
>
> endif
> diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
> index a64fca8..d8ad9da 100644
> --- a/drivers/media/i2c/Makefile
> +++ b/drivers/media/i2c/Makefile
> @@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
> obj-$(CONFIG_VIDEO_IMX274) += imx274.o
> obj-$(CONFIG_VIDEO_IMX319) += imx319.o
> obj-$(CONFIG_VIDEO_IMX355) += imx355.o
> +obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o
>
> obj-$(CONFIG_SDR_MAX2175) += max2175.o
> diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
> new file mode 100644
> index 0000000..61dcb83
> --- /dev/null
> +++ b/drivers/media/i2c/st-mipid02.c
> @@ -0,0 +1,877 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
> + *
> + * Copyright (C) STMicroelectronics SA 2019
> + * Authors: Mickael Guene <[email protected]>
> + * for STMicroelectronics.
> + *
> + *
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/delay.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/i2c.h>
> +#include <linux/module.h>
> +#include <linux/of_graph.h>
> +#include <linux/regulator/consumer.h>
> +#include <media/v4l2-async.h>
> +#include <media/v4l2-ctrls.h>
> +#include <media/v4l2-device.h>
> +#include <media/v4l2-fwnode.h>
> +#include <media/v4l2-subdev.h>
> +
> +#define MIPID02_CLK_LANE_WR_REG1 0x01
> +#define MIPID02_CLK_LANE_REG1 0x02
> +#define MIPID02_CLK_LANE_REG3 0x04
> +#define MIPID02_DATA_LANE0_REG1 0x05
> +#define MIPID02_DATA_LANE0_REG2 0x06
> +#define MIPID02_DATA_LANE1_REG1 0x09
> +#define MIPID02_DATA_LANE1_REG2 0x0a
> +#define MIPID02_MODE_REG1 0x14
> +#define MIPID02_MODE_REG2 0x15
> +#define MIPID02_DATA_ID_RREG 0x17
> +#define MIPID02_DATA_SELECTION_CTRL 0x19
> +#define MIPID02_PIX_WIDTH_CTRL 0x1e
> +#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
> +
> +/* Bits definition for MIPID02_CLK_LANE_REG1 */
> +#define CLK_ENABLE BIT(0)
> +/* Bits definition for MIPID02_CLK_LANE_REG3 */
> +#define CLK_MIPI_CSI BIT(1)
> +/* Bits definition for MIPID02_DATA_LANE0_REG1 */
> +#define DATA_ENABLE BIT(0)
> +/* Bits definition for MIPID02_DATA_LANEx_REG2 */
> +#define DATA_MIPI_CSI BIT(0)
> +/* Bits definition for MIPID02_MODE_REG1 */
> +#define MODE_DATA_SWAP BIT(2)
> +#define MODE_NO_BYPASS BIT(6)
> +/* Bits definition for MIPID02_MODE_REG2 */
> +#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
> +#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
> +/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
> +#define SELECTION_MANUAL_DATA BIT(2)
> +#define SELECTION_MANUAL_WIDTH BIT(3)
> +
> +/* regulator supplies */
> +static const char * const mipid02_supply_name[] = {
> + "VDDE", /* 1.8V digital I/O supply */
> + "VDDIN", /* 1V8 voltage regulator supply */
> +};
> +
> +#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
> +#define MIPID02_PAD_NB 2
> +
> +struct mipid02_dev {
> + struct i2c_client *i2c_client;
> + struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
> + struct v4l2_subdev sd;
> + struct media_pad pad[MIPID02_PAD_NB];
> + struct clk *xclk;
> + struct gpio_desc *reset_gpio;
> + /* Protect streaming */
> + struct mutex lock;
> + bool streaming;
> + /* endpoints info */
> + struct v4l2_fwnode_endpoint rx;
> + u64 link_frequency;
> + struct v4l2_fwnode_endpoint tx;
> + /* remote source */
> + struct v4l2_async_subdev asd;
> + struct v4l2_async_notifier notifier;
> + /* registers */
> + struct {
> + u8 clk_lane_reg1;
> + u8 data_lane0_reg1;
> + u8 data_lane1_reg1;
> + u8 mode_reg1;
> + u8 mode_reg2;
> + u8 data_id_rreg;
> + u8 pix_width_ctrl;
> + u8 pix_width_ctrl_emb;
> + } r;
> +};
> +
> +static int bpp_from_code(__u32 code)
> +{
> + switch (code) {
> + case MEDIA_BUS_FMT_SBGGR8_1X8:
> + case MEDIA_BUS_FMT_SGBRG8_1X8:
> + case MEDIA_BUS_FMT_SGRBG8_1X8:
> + case MEDIA_BUS_FMT_SRGGB8_1X8:
> + return 8;
> + case MEDIA_BUS_FMT_SBGGR10_1X10:
> + case MEDIA_BUS_FMT_SGBRG10_1X10:
> + case MEDIA_BUS_FMT_SGRBG10_1X10:
> + case MEDIA_BUS_FMT_SRGGB10_1X10:
> + return 10;
> + case MEDIA_BUS_FMT_SBGGR12_1X12:
> + case MEDIA_BUS_FMT_SGBRG12_1X12:
> + case MEDIA_BUS_FMT_SGRBG12_1X12:
> + case MEDIA_BUS_FMT_SRGGB12_1X12:
> + return 12;
> + case MEDIA_BUS_FMT_UYVY8_2X8:
> + return 16;
> + case MEDIA_BUS_FMT_BGR888_1X24:
> + return 24;
> + default:
> + return 0;
> + }
> +}
> +
> +static u8 data_type_from_code(__u32 code)
> +{
> + switch (code) {
> + case MEDIA_BUS_FMT_SBGGR8_1X8:
> + case MEDIA_BUS_FMT_SGBRG8_1X8:
> + case MEDIA_BUS_FMT_SGRBG8_1X8:
> + case MEDIA_BUS_FMT_SRGGB8_1X8:
> + return 0x2a;
> + case MEDIA_BUS_FMT_SBGGR10_1X10:
> + case MEDIA_BUS_FMT_SGBRG10_1X10:
> + case MEDIA_BUS_FMT_SGRBG10_1X10:
> + case MEDIA_BUS_FMT_SRGGB10_1X10:
> + return 0x2b;
> + case MEDIA_BUS_FMT_SBGGR12_1X12:
> + case MEDIA_BUS_FMT_SGBRG12_1X12:
> + case MEDIA_BUS_FMT_SGRBG12_1X12:
> + case MEDIA_BUS_FMT_SRGGB12_1X12:
> + return 0x2c;
> + case MEDIA_BUS_FMT_UYVY8_2X8:
> + return 0x1e;
> + case MEDIA_BUS_FMT_BGR888_1X24:
> + return 0x24;
> + default:
> + return 0;
> + }
> +}
> +
> +static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
> +{
> + return container_of(sd, struct mipid02_dev, sd);
> +}
> +
> +static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct i2c_msg msg[2];
> + u8 buf[2];
> + int ret;
> +
> + buf[0] = reg >> 8;
> + buf[1] = reg & 0xff;
> +
> + msg[0].addr = client->addr;
> + msg[0].flags = client->flags;
> + msg[0].buf = buf;
> + msg[0].len = sizeof(buf);
> +
> + msg[1].addr = client->addr;
> + msg[1].flags = client->flags | I2C_M_RD;
> + msg[1].buf = val;
> + msg[1].len = 1;
> +
> + ret = i2c_transfer(client->adapter, msg, 2);
> + if (ret < 0) {
> + dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
> + __func__, client->addr, reg, ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct i2c_msg msg;
> + u8 buf[3];
> + int ret;
> +
> + buf[0] = reg >> 8;
> + buf[1] = reg & 0xff;
> + buf[2] = val;
> +
> + msg.addr = client->addr;
> + msg.flags = client->flags;
> + msg.buf = buf;
> + msg.len = sizeof(buf);
> +
> + ret = i2c_transfer(client->adapter, &msg, 1);
> + if (ret < 0) {
> + dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
> + __func__, reg, ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int mipid02_get_regulators(struct mipid02_dev *bridge)
> +{
> + unsigned int i;
> +
> + for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
> + bridge->supplies[i].supply = mipid02_supply_name[i];
> +
> + return devm_regulator_bulk_get(&bridge->i2c_client->dev,
> + MIPID02_NUM_SUPPLIES,
> + bridge->supplies);
> +}
> +
> +static void mipid02_apply_reset(struct mipid02_dev *bridge)
> +{
> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
> + usleep_range(5000, 10000);
> + gpiod_set_value_cansleep(bridge->reset_gpio, 1);
> + usleep_range(5000, 10000);
> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
> + usleep_range(5000, 10000);
> +}
> +
> +static int mipid02_set_power_on(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + int ret;
> +
> + ret = clk_prepare_enable(bridge->xclk);
> + if (ret) {
> + dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
> + return ret;
> + }
> +
> + ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
> + bridge->supplies);
> + if (ret) {
> + dev_err(&client->dev, "%s: failed to enable regulators\n",
> + __func__);
> + goto xclk_off;
> + }
> +
> + if (bridge->reset_gpio) {
> + dev_dbg(&client->dev, "apply reset");
> + mipid02_apply_reset(bridge);
> + } else {
> + dev_dbg(&client->dev, "dont apply reset");
> + usleep_range(5000, 10000);
> + }
> +
> + return 0;
> +
> +xclk_off:
> + clk_disable_unprepare(bridge->xclk);
> + return ret;
> +}
> +
> +static void mipid02_set_power_off(struct mipid02_dev *bridge)
> +{
> + regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
> + clk_disable_unprepare(bridge->xclk);
> +}
> +
> +static int mipid02_detect(struct mipid02_dev *bridge)
> +{
> + u8 reg;
> +
> + /*
> + * There is no version registers. Just try to read register
> + * MIPID02_CLK_LANE_WR_REG1.
> + */
> + return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
> +}
> +
> +static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> +
> + if (ep->nr_of_link_frequencies != 1)
> + return 0;
> +
> + return ep->link_frequencies[0];
> +}
> +
> +static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
> + struct v4l2_subdev *sensor)
> +{
> + struct v4l2_ctrl *ctrl;
> +
> + ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_LINK_FREQ);
> + if (!ctrl)
> + return 0;
> +
> + return v4l2_ctrl_g_ctrl_int64(ctrl);
> +}
> +
> +static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
> + __u32 code,
> + struct v4l2_subdev *sensor)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + struct v4l2_ctrl *ctrl;
> + u32 pixel_clock;
> + u32 bpp = bpp_from_code(code);
> +
> + ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_PIXEL_RATE);
> + if (!ctrl)
> + return 0;
> + pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
> +
> + return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
> +}
> +
> +/*
> + * We need to know link frequency to setup clk_lane_reg1 timings. So use
> + * following sequence, stop when we got a frequency.
> + * - Get it from device tree. It allow to connect sensor without support of
> + * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
> + * is that is that link frequency must be constant.
> + * - Get it from V4L2_CID_LINK_FREQ.
> + * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
> + * to compute it.
> + */
> +static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
> + __u32 code,
> + struct v4l2_subdev *sensor)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + u32 link_freq;
> +
> + link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
> + if (link_freq)
> + goto success;
> +
> + link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, sensor);
> + if (link_freq)
> + goto success;
> +
> + link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, code,
> + sensor);
> + if (link_freq)
> + goto success;
> + dev_err(&client->dev, "Failed to detect link frequency");
> +
> + return -EINVAL;
> +
> +success:
> + dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
> + bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
> +
> + /* midid02 doesn't support clock lane remapping */
> + if (ep->bus.mipi_csi2.clock_lane != 0) {
> + dev_err(&client->dev, "clk lane must be map to lane 0\n");
> + return -EINVAL;
> + }
> + bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
> + bool are_lanes_swap, bool *polarities)
> +{
> + bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
> +
> + if (nb == 1 && are_lanes_swap)
> + return 0;
> +
> + /*
> + * data lane 0 as pin swap polarity reversed compared to clock and
> + *data lane 1
> + */
> + if (!are_pin_swap)
> + bridge->r.data_lane0_reg1 = 1 << 1;
> + bridge->r.data_lane0_reg1 |= DATA_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
> + bool are_lanes_swap, bool *polarities)
> +{
> + bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
> +
> + if (nb == 1 && !are_lanes_swap)
> + return 0;
> +
> + if (are_pin_swap)
> + bridge->r.data_lane1_reg1 = 1 << 1;
> + bridge->r.data_lane1_reg1 |= DATA_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_from_rx(struct mipid02_dev *bridge, __u32 code,
> + struct v4l2_subdev *sensor)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
> + int nb = ep->bus.mipi_csi2.num_data_lanes;
> + int ret;
> +
> + ret = mipid02_configure_clk_lane(bridge);
> + if (ret)
> + return ret;
> +
> + ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
> + polarities);
> + if (ret)
> + return ret;
> +
> + ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
> + polarities);
> + if (ret)
> + return ret;
> +
> + bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
> + bridge->r.mode_reg1 |= (nb - 1) << 1;
> +
> + return mipid02_configure_from_rx_speed(bridge, code, sensor);
> +}
> +
> +static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->tx;
> +
> + bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
> + bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
> + if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
> + bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
> + if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
> + bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_from_code(struct mipid02_dev *bridge, __u32 code)
> +{
> + u8 data_type;
> +
> + bridge->r.data_id_rreg = 0;
> + data_type = data_type_from_code(code);
> + if (!data_type)
> + return -EINVAL;
> + bridge->r.data_id_rreg = data_type;
> +
> + return 0;
> +}
> +
> +static __u32 mipid02_get_source_code(struct mipid02_dev *bridge,
> + struct v4l2_subdev *sensor)
> +{
> + struct v4l2_subdev_format fmt = {
> + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
> + };
> + int ret;
> +
> + ret = v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt);
> +
> + return ret ? MEDIA_BUS_FMT_SBGGR8_1X8 : fmt.format.code;
> +}
> +
> +static int mipid02_stream_disable(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + int ret;
> +
> + /* Disable all lanes */
> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
> + if (ret)
> + dev_err(&client->dev, "failed to stream off %d", ret);
> +
> + return ret;
> +}
> +
> +static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
> +{
> + struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
> + struct media_entity *entity;
> +
> + if (!mdev)
> + return NULL;
> +
> + media_device_for_each_entity(entity, mdev)
> + if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
> + return media_entity_to_v4l2_subdev(entity);
> +
> + return NULL;
> +}
> +
> +static int mipid02_stream_enable(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_subdev *sensor;
> + __u32 code;
> + int ret = -EINVAL;
> +
> + sensor = mipid02_find_sensor(bridge);
> + if (!sensor)
> + goto error;
> +
> + dev_dbg(&client->dev, "use sensor '%s'", sensor->name);
> + memset(&bridge->r, 0, sizeof(bridge->r));
> + /* build registers content */
> + code = mipid02_get_source_code(bridge, sensor);
> + ret = mipid02_configure_from_rx(bridge, code, sensor);
> + ret |= mipid02_configure_from_tx(bridge);
> + ret |= mipid02_configure_from_code(bridge, code);
> +
> + /* write mipi registers */
> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
> + bridge->r.clk_lane_reg1);
> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
> + bridge->r.data_lane0_reg1);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
> + DATA_MIPI_CSI);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
> + bridge->r.data_lane1_reg1);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
> + DATA_MIPI_CSI);
> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG1,
> + MODE_NO_BYPASS | bridge->r.mode_reg1);
> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG2,
> + bridge->r.mode_reg2);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
> + bridge->r.data_id_rreg);
> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
> + bridge->r.pix_width_ctrl);
> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
> + bridge->r.pix_width_ctrl_emb);
> +
> + if (ret)
> + goto error;
> +
> + return 0;
> +
> +error:
> + dev_err(&client->dev, "failed to stream on %d", ret);
> + mipid02_stream_disable(bridge);
> +
> + return ret;
> +}
> +
> +static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
> +{
> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> + struct i2c_client *client = bridge->i2c_client;
> + int ret = 0;
> +
> + dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
> + enable, bridge->streaming);
> + mutex_lock(&bridge->lock);
> +
> + if (bridge->streaming == enable)
> + goto out;
> +
> + ret = enable ? mipid02_stream_enable(bridge) :
> + mipid02_stream_disable(bridge);
> + if (!ret)
> + bridge->streaming = enable;
> +
> +out:
> + dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
> + bridge->streaming, ret);
> + mutex_unlock(&bridge->lock);
> +
> + return ret;
> +}
> +
> +static const struct v4l2_subdev_video_ops mipid02_video_ops = {
> + .s_stream = mipid02_s_stream,
> +};
> +
> +static const struct v4l2_subdev_ops mipid02_subdev_ops = {
> + .video = &mipid02_video_ops,

You'll need to add support for set_fmt, get_fmt subdev pad ops and the
link_validate media entity op.

> +};
> +
> +static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
> + struct v4l2_subdev *s_subdev,
> + struct v4l2_async_subdev *asd)
> +{
> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
> + struct i2c_client *client = bridge->i2c_client;
> + int source_pad;
> +
> + dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
> +
> + source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
> + s_subdev->fwnode,
> + MEDIA_PAD_FL_SOURCE);
> + if (source_pad < 0) {
> + dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
> + s_subdev->name);
> + return source_pad;
> + }
> +
> + return media_create_pad_link(&s_subdev->entity, source_pad,
> + &bridge->sd.entity, 0,
> + MEDIA_LNK_FL_ENABLED |
> + MEDIA_LNK_FL_IMMUTABLE);
> +}
> +
> +static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
> + .bound = mipid02_async_bound,
> +};
> +
> +static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint ep;
> + struct i2c_client *client = bridge->i2c_client;
> + struct device_node *ep_node;
> + int ret;
> +
> + memset(&ep, 0, sizeof(ep));
> + ep.bus_type = V4L2_MBUS_CSI2_DPHY;
> + /* parse rx (endpoint 0) */
> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
> + 0, 0);
> + if (!ep_node) {
> + dev_err(&client->dev, "unable to find port0 ep");
> + ret = -EINVAL;
> + goto error;
> + }
> +
> + ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
> + if (ret) {
> + dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
> + ret);
> + goto error_of_node_put;
> + }
> +
> + /* do some sanity checks */
> + if (ep.bus.mipi_csi2.num_data_lanes > 2) {
> + dev_err(&client->dev, "max supported data lanes is 2 / got %d",
> + ep.bus.mipi_csi2.num_data_lanes);
> + ret = -EINVAL;
> + goto error_v4l2_fwnode_endpoint_free;
> + }
> +
> + /* register it for later use */
> + bridge->rx = ep;
> + bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
> + &bridge->link_frequency : NULL;

I think you need to simply ignore the link frequencies here. The
transmitting device can tell the frequency based on its configuration
(based on the link frequencies). You seem to have implemented that already.

> + v4l2_fwnode_endpoint_free(&ep);
> +
> + /* register async notifier so we get noticed when sensor is connected */
> + bridge->asd.match.fwnode =
> + fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
> + bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
> + of_node_put(ep_node);
> +
> + v4l2_async_notifier_init(&bridge->notifier);
> + ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
> + if (ret) {
> + dev_err(&client->dev, "fail to register asd to notifier %d",
> + ret);
> + goto error_fwnode_handle_put;
> + }
> + bridge->notifier.ops = &mipid02_notifier_ops;
> +
> + ret = v4l2_async_subdev_notifier_register(&bridge->sd,
> + &bridge->notifier);
> + if (ret)
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> +
> + return ret;
> +
> +error_v4l2_fwnode_endpoint_free:
> + v4l2_fwnode_endpoint_free(&ep);
> +error_of_node_put:
> + of_node_put(ep_node);
> +error:
> +
> + return ret;
> +
> +error_fwnode_handle_put:
> + fwnode_handle_put(bridge->asd.match.fwnode);
> +
> + return ret;
> +}
> +
> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_fwnode_endpoint ep;
> + struct device_node *ep_node;
> + int ret;
> +
> + memset(&ep, 0, sizeof(ep));
> + ep.bus_type = V4L2_MBUS_PARALLEL;
> + /* parse tx (endpoint 2) */
> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
> + 2, 0);
> + if (!ep_node) {
> + dev_err(&client->dev, "unable to find port1 ep");
> + ret = -EINVAL;
> + goto error;
> + }
> +
> + ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
> + if (ret) {
> + dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
> + goto error_of_node_put;
> + }
> +
> + of_node_put(ep_node);
> + bridge->tx = ep;
> +
> + return 0;
> +
> +error_of_node_put:
> + of_node_put(ep_node);
> +error:
> +
> + return -EINVAL;
> +}
> +
> +static int mipid02_probe(struct i2c_client *client,
> + const struct i2c_device_id *id)
> +{
> + struct device *dev = &client->dev;
> + struct mipid02_dev *bridge;
> + u32 clk_freq;
> + int ret;
> +
> + bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
> + if (!bridge)
> + return -ENOMEM;
> +
> + bridge->i2c_client = client;
> + v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
> +
> + /* got and check clock */
> + bridge->xclk = devm_clk_get(dev, "xclk");
> + if (IS_ERR(bridge->xclk)) {
> + dev_err(dev, "failed to get xclk\n");
> + return PTR_ERR(bridge->xclk);
> + }
> +
> + clk_freq = clk_get_rate(bridge->xclk);
> + if (clk_freq < 6000000 || clk_freq > 27000000) {
> + dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
> + clk_freq);
> + return -EINVAL;
> + }
> +
> + bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
> + GPIOD_OUT_HIGH);
> +
> + ret = mipid02_get_regulators(bridge);
> + if (ret) {
> + dev_err(dev, "failed to get regulators %d", ret);
> + return ret;
> + }
> +
> + mutex_init(&bridge->lock);
> + bridge->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
> + bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
> + bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
> + bridge->pad[1].flags = MEDIA_PAD_FL_SOURCE;
> + ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
> + bridge->pad);
> + if (ret) {
> + dev_err(&client->dev, "pads init failed %d", ret);
> + return ret;
> + }
> +
> + /* enable clock, power and reset device if available */
> + ret = mipid02_set_power_on(bridge);
> + if (ret)
> + goto entity_cleanup;
> +
> + ret = mipid02_detect(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to detect mipid02 %d", ret);
> + goto power_off;
> + }
> +
> + ret = mipid02_parse_tx_ep(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to parse tx %d", ret);
> + goto power_off;
> + }
> +
> + ret = mipid02_parse_rx_ep(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to parse rx %d", ret);
> + goto power_off;
> + }
> +
> + ret = v4l2_async_register_subdev(&bridge->sd);
> + if (ret < 0) {
> + dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
> + ret);
> + goto unregister_notifier;
> + }
> +
> + dev_info(&client->dev, "mipid02 device probe successfully");
> +
> + return 0;
> +
> +unregister_notifier:
> + v4l2_async_notifier_unregister(&bridge->notifier);
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> +power_off:
> + mipid02_set_power_off(bridge);
> +entity_cleanup:
> + media_entity_cleanup(&bridge->sd.entity);
> +
> + return ret;
> +}
> +
> +static int mipid02_remove(struct i2c_client *client)
> +{
> + struct v4l2_subdev *sd = i2c_get_clientdata(client);
> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> +
> + v4l2_async_notifier_unregister(&bridge->notifier);
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> + v4l2_async_unregister_subdev(&bridge->sd);
> + mipid02_set_power_off(bridge);
> + media_entity_cleanup(&bridge->sd.entity);
> +
> + return 0;
> +}
> +
> +static const struct i2c_device_id mipid02_id[] = {
> + { "st-mipid02", 0},
> + {},
> +};
> +MODULE_DEVICE_TABLE(i2c, mipid02_id);
> +
> +static const struct of_device_id mipid02_dt_ids[] = {
> + { .compatible = "st,st-mipid02" },
> + { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
> +
> +static struct i2c_driver mipid02_i2c_driver = {
> + .driver = {
> + .name = "st-mipid02",
> + .of_match_table = mipid02_dt_ids,
> + },
> + .id_table = mipid02_id,
> + .probe = mipid02_probe,
> + .remove = mipid02_remove,
> +};
> +
> +module_i2c_driver(mipid02_i2c_driver);
> +
> +MODULE_AUTHOR("Mickael Guene <[email protected]>");
> +MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
> +MODULE_LICENSE("GPL v2");

--
Kind regards,

Sakari Ailus
[email protected]

2019-03-25 12:16:33

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v1 2/3] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Sakari,

On 3/25/19 12:17, Sakari Ailus wrote:
> Hi Mickael,
>
> On Mon, Mar 18, 2019 at 09:57:44AM +0000, Mickael GUENE wrote:
>> Hi Sakari,
>>
>> Thanks for your review. Find my comments below.
>>
>> On 3/16/19 23:14, Sakari Ailus wrote:
> ...
>>>> +static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
>>>> +{
>>>> + struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
>>>> + struct media_entity *entity;
>>>> +
>>>> + if (!mdev)
>>>> + return NULL;
>>>> +
>>>> + media_device_for_each_entity(entity, mdev)
>>>> + if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
>>>> + return media_entity_to_v4l2_subdev(entity);
>>>
>>> Hmm. Could you instead use the link state to determine which of the
>>> receivers is active? You'll need one more pad, and then you'd had 1:1
>>> mapping between ports and pads.
>>>
>> Goal here is not to detect which of the receivers is active but to find
>> sensor in case there are others sub-dev in chain (for example a
>> serializer/deserializer as found in cars).
>
> You shouldn't make assumptions on the rest of the pipeline beyond the
> device that's directly connected. You might not even have a camera there.
>
I have also seen your answer to '[PATCH v2 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver'
concerning support of set_fmt, get_fmt and link_validate.
My initial idea was to avoid to avoid to implement them and to avoid media ctrl configuration. According
to your remark is seems a bad idea. Right ?
In that case I have to also implement enum_mbus_code ?
I will drop this code and use connected device only to get link speed.

>> For the moment the driver doesn't support second input port usage,
>> this is why there is no such second sink pad yet in the driver.
>
> Could you add the second sink pad now, so that the uAPI remains the same
> when you add support for it? Nothing is connected to it but I don't think
> it's an issue.
>
> ...
>
>>>> +
>>>> + sensor = mipid02_find_sensor(bridge);
>>>> + if (!sensor)
>>>> + goto error;
>>>> +
>>>> + dev_dbg(&client->dev, "use sensor '%s'", sensor->name);
>>>> + memset(&bridge->r, 0, sizeof(bridge->r));
>>>> + /* build registers content */
>>>> + code = mipid02_get_source_code(bridge, sensor);
>>>> + ret |= mipid02_configure_from_rx(bridge, code, sensor);
>>>> + ret |= mipid02_configure_from_tx(bridge);
>>>> + ret |= mipid02_configure_from_code(bridge, code);
>>>> +
>>>> + /* write mipi registers */
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
>>>> + bridge->r.clk_lane_reg1);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
>>>> + bridge->r.data_lane0_reg1);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
>>>> + DATA_MIPI_CSI);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
>>>> + bridge->r.data_lane1_reg1);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
>>>> + DATA_MIPI_CSI);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG1,
>>>> + MODE_NO_BYPASS | bridge->r.mode_reg1);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG2,
>>>> + bridge->r.mode_reg2);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
>>>> + bridge->r.data_id_rreg);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
>>>> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
>>>> + bridge->r.pix_width_ctrl);
>>>> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
>>>> + bridge->r.pix_width_ctrl_emb);
>>>
>>> Be careful with the error codes. ret will be returned by the s_stream
>>> callback below.
>>>
>> I didn't understand your remark. Can you elaborate a little bit more ?
>
> If the functions return different error codes, then ret possibly won't be a
> valid error code, or at least it's not going to be what it was intended to
> do. You'll need to stop when you encounter an error and then return it to
> the caller.
>
> ...
>
Ok
>>>> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
>>>> +{
>>>> + struct i2c_client *client = bridge->i2c_client;
>>>> + struct v4l2_fwnode_endpoint ep;
>>>> + struct device_node *ep_node;
>>>> + int ret;
>>>> +
>>>> + memset(&ep, 0, sizeof(ep));
>>>> + ep.bus_type = V4L2_MBUS_PARALLEL;
>>>
>>> You can set the field in variable declaration, and omit memset. The same in
>>> the function above.
>>>
>> According to v4l2_fwnode_endpoint_parse() documentation:
>> * This function parses the V4L2 fwnode endpoint specific parameters from the
>> * firmware. The caller is responsible for assigning @vep.bus_type to a valid
>> * media bus type. The caller may also set the default configuration for the
>> * endpoint
>> It seems safer to clear ep else it may select unwanted default configuration
>> for the endpoint ?
>
> By setting one of the fields in a struct in declaration, the rest will be
> zeroed by the compiler. That's from the C standard.
>
Ok

2019-03-25 12:24:18

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Sakari,

On 3/25/19 12:44, Sakari Ailus wrote:
> Hi Mickael,
>
> A few comments below. This starts looking pretty good IMO, I missed a few
> things the last time around. Please also see my reply on v1.
>
> On Mon, Mar 25, 2019 at 08:55:10AM +0100, Mickael Guene wrote:
>> This V4L2 subdev driver enables STMicroelectronics MIPID02 device.
>>
>> Signed-off-by: Mickael Guene <[email protected]>
>> ---
>>
>> Changes in v2:
>> - Merge MAINTAINERS patch 3 into patch 1 and 2
>> - Fix line too long in Kconfig
>> - Add missing delay after reset release
>> - Various style fixes
>> - Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed
>>
>> MAINTAINERS | 1 +
>> drivers/media/i2c/Kconfig | 14 +
>> drivers/media/i2c/Makefile | 1 +
>> drivers/media/i2c/st-mipid02.c | 877 +++++++++++++++++++++++++++++++++++++++++
>> 4 files changed, 893 insertions(+)
>> create mode 100644 drivers/media/i2c/st-mipid02.c
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 74da99d..a14fe81 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -14673,6 +14673,7 @@ M: Mickael Guene <[email protected]>
>> L: [email protected]
>> T: git git://linuxtv.org/media_tree.git
>> S: Maintained
>> +F: drivers/media/i2c/st-mipid02.c
>> F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>>
>> ST STM32 I2C/SMBUS DRIVER
>> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
>> index 1ef6335..be2470b 100644
>> --- a/drivers/media/i2c/Kconfig
>> +++ b/drivers/media/i2c/Kconfig
>> @@ -1135,6 +1135,20 @@ config VIDEO_I2C
>> To compile this driver as a module, choose M here: the
>> module will be called video-i2c
>>
>> +config VIDEO_ST_MIPID02
>> + tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
>> + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
>> + depends on MEDIA_CAMERA_SUPPORT
>> + depends on MEDIA_CONTROLLER
>> + select V4L2_FWNODE
>> + help
>> + Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
>> + It is used to allow usage of CSI-2 sensor with PARALLEL port
>> + controller.
>> +
>> + To compile this driver as a module, choose M here: the
>> + module will be called st-mipid02.
>> +
>> endmenu
>>
>> endif
>> diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
>> index a64fca8..d8ad9da 100644
>> --- a/drivers/media/i2c/Makefile
>> +++ b/drivers/media/i2c/Makefile
>> @@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
>> obj-$(CONFIG_VIDEO_IMX274) += imx274.o
>> obj-$(CONFIG_VIDEO_IMX319) += imx319.o
>> obj-$(CONFIG_VIDEO_IMX355) += imx355.o
>> +obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o
>>
>> obj-$(CONFIG_SDR_MAX2175) += max2175.o
>> diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
>> new file mode 100644
>> index 0000000..61dcb83
>> --- /dev/null
>> +++ b/drivers/media/i2c/st-mipid02.c
>> @@ -0,0 +1,877 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
>> + *
>> + * Copyright (C) STMicroelectronics SA 2019
>> + * Authors: Mickael Guene <[email protected]>
>> + * for STMicroelectronics.
>> + *
>> + *
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/delay.h>
>> +#include <linux/gpio/consumer.h>
>> +#include <linux/i2c.h>
>> +#include <linux/module.h>
>> +#include <linux/of_graph.h>
>> +#include <linux/regulator/consumer.h>
>> +#include <media/v4l2-async.h>
>> +#include <media/v4l2-ctrls.h>
>> +#include <media/v4l2-device.h>
>> +#include <media/v4l2-fwnode.h>
>> +#include <media/v4l2-subdev.h>
>> +
>> +#define MIPID02_CLK_LANE_WR_REG1 0x01
>> +#define MIPID02_CLK_LANE_REG1 0x02
>> +#define MIPID02_CLK_LANE_REG3 0x04
>> +#define MIPID02_DATA_LANE0_REG1 0x05
>> +#define MIPID02_DATA_LANE0_REG2 0x06
>> +#define MIPID02_DATA_LANE1_REG1 0x09
>> +#define MIPID02_DATA_LANE1_REG2 0x0a
>> +#define MIPID02_MODE_REG1 0x14
>> +#define MIPID02_MODE_REG2 0x15
>> +#define MIPID02_DATA_ID_RREG 0x17
>> +#define MIPID02_DATA_SELECTION_CTRL 0x19
>> +#define MIPID02_PIX_WIDTH_CTRL 0x1e
>> +#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
>> +
>> +/* Bits definition for MIPID02_CLK_LANE_REG1 */
>> +#define CLK_ENABLE BIT(0)
>> +/* Bits definition for MIPID02_CLK_LANE_REG3 */
>> +#define CLK_MIPI_CSI BIT(1)
>> +/* Bits definition for MIPID02_DATA_LANE0_REG1 */
>> +#define DATA_ENABLE BIT(0)
>> +/* Bits definition for MIPID02_DATA_LANEx_REG2 */
>> +#define DATA_MIPI_CSI BIT(0)
>> +/* Bits definition for MIPID02_MODE_REG1 */
>> +#define MODE_DATA_SWAP BIT(2)
>> +#define MODE_NO_BYPASS BIT(6)
>> +/* Bits definition for MIPID02_MODE_REG2 */
>> +#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
>> +#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
>> +/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
>> +#define SELECTION_MANUAL_DATA BIT(2)
>> +#define SELECTION_MANUAL_WIDTH BIT(3)
>> +
>> +/* regulator supplies */
>> +static const char * const mipid02_supply_name[] = {
>> + "VDDE", /* 1.8V digital I/O supply */
>> + "VDDIN", /* 1V8 voltage regulator supply */
>> +};
>> +
>> +#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
>> +#define MIPID02_PAD_NB 2
>> +
>> +struct mipid02_dev {
>> + struct i2c_client *i2c_client;
>> + struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
>> + struct v4l2_subdev sd;
>> + struct media_pad pad[MIPID02_PAD_NB];
>> + struct clk *xclk;
>> + struct gpio_desc *reset_gpio;
>> + /* Protect streaming */
>> + struct mutex lock;
>> + bool streaming;
>> + /* endpoints info */
>> + struct v4l2_fwnode_endpoint rx;
>> + u64 link_frequency;
>> + struct v4l2_fwnode_endpoint tx;
>> + /* remote source */
>> + struct v4l2_async_subdev asd;
>> + struct v4l2_async_notifier notifier;
>> + /* registers */
>> + struct {
>> + u8 clk_lane_reg1;
>> + u8 data_lane0_reg1;
>> + u8 data_lane1_reg1;
>> + u8 mode_reg1;
>> + u8 mode_reg2;
>> + u8 data_id_rreg;
>> + u8 pix_width_ctrl;
>> + u8 pix_width_ctrl_emb;
>> + } r;
>> +};
>> +
>> +static int bpp_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 8;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 10;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 12;
>> + case MEDIA_BUS_FMT_UYVY8_2X8:
>> + return 16;
>> + case MEDIA_BUS_FMT_BGR888_1X24:
>> + return 24;
>> + default:
>> + return 0;
>> + }
>> +}
>> +
>> +static u8 data_type_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 0x2a;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 0x2b;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 0x2c;
>> + case MEDIA_BUS_FMT_UYVY8_2X8:
>> + return 0x1e;
>> + case MEDIA_BUS_FMT_BGR888_1X24:
>> + return 0x24;
>> + default:
>> + return 0;
>> + }
>> +}
>> +
>> +static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
>> +{
>> + return container_of(sd, struct mipid02_dev, sd);
>> +}
>> +
>> +static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct i2c_msg msg[2];
>> + u8 buf[2];
>> + int ret;
>> +
>> + buf[0] = reg >> 8;
>> + buf[1] = reg & 0xff;
>> +
>> + msg[0].addr = client->addr;
>> + msg[0].flags = client->flags;
>> + msg[0].buf = buf;
>> + msg[0].len = sizeof(buf);
>> +
>> + msg[1].addr = client->addr;
>> + msg[1].flags = client->flags | I2C_M_RD;
>> + msg[1].buf = val;
>> + msg[1].len = 1;
>> +
>> + ret = i2c_transfer(client->adapter, msg, 2);
>> + if (ret < 0) {
>> + dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
>> + __func__, client->addr, reg, ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct i2c_msg msg;
>> + u8 buf[3];
>> + int ret;
>> +
>> + buf[0] = reg >> 8;
>> + buf[1] = reg & 0xff;
>> + buf[2] = val;
>> +
>> + msg.addr = client->addr;
>> + msg.flags = client->flags;
>> + msg.buf = buf;
>> + msg.len = sizeof(buf);
>> +
>> + ret = i2c_transfer(client->adapter, &msg, 1);
>> + if (ret < 0) {
>> + dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
>> + __func__, reg, ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_get_regulators(struct mipid02_dev *bridge)
>> +{
>> + unsigned int i;
>> +
>> + for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
>> + bridge->supplies[i].supply = mipid02_supply_name[i];
>> +
>> + return devm_regulator_bulk_get(&bridge->i2c_client->dev,
>> + MIPID02_NUM_SUPPLIES,
>> + bridge->supplies);
>> +}
>> +
>> +static void mipid02_apply_reset(struct mipid02_dev *bridge)
>> +{
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
>> + usleep_range(5000, 10000);
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 1);
>> + usleep_range(5000, 10000);
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
>> + usleep_range(5000, 10000);
>> +}
>> +
>> +static int mipid02_set_power_on(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret;
>> +
>> + ret = clk_prepare_enable(bridge->xclk);
>> + if (ret) {
>> + dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
>> + return ret;
>> + }
>> +
>> + ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
>> + bridge->supplies);
>> + if (ret) {
>> + dev_err(&client->dev, "%s: failed to enable regulators\n",
>> + __func__);
>> + goto xclk_off;
>> + }
>> +
>> + if (bridge->reset_gpio) {
>> + dev_dbg(&client->dev, "apply reset");
>> + mipid02_apply_reset(bridge);
>> + } else {
>> + dev_dbg(&client->dev, "dont apply reset");
>> + usleep_range(5000, 10000);
>> + }
>> +
>> + return 0;
>> +
>> +xclk_off:
>> + clk_disable_unprepare(bridge->xclk);
>> + return ret;
>> +}
>> +
>> +static void mipid02_set_power_off(struct mipid02_dev *bridge)
>> +{
>> + regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
>> + clk_disable_unprepare(bridge->xclk);
>> +}
>> +
>> +static int mipid02_detect(struct mipid02_dev *bridge)
>> +{
>> + u8 reg;
>> +
>> + /*
>> + * There is no version registers. Just try to read register
>> + * MIPID02_CLK_LANE_WR_REG1.
>> + */
>> + return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> +
>> + if (ep->nr_of_link_frequencies != 1)
>> + return 0;
>> +
>> + return ep->link_frequencies[0];
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct v4l2_ctrl *ctrl;
>> +
>> + ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_LINK_FREQ);
>> + if (!ctrl)
>> + return 0;
>> +
>> + return v4l2_ctrl_g_ctrl_int64(ctrl);
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
>> + __u32 code,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + struct v4l2_ctrl *ctrl;
>> + u32 pixel_clock;
>> + u32 bpp = bpp_from_code(code);
>> +
>> + ctrl = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_PIXEL_RATE);
>> + if (!ctrl)
>> + return 0;
>> + pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
>> +
>> + return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
>> +}
>> +
>> +/*
>> + * We need to know link frequency to setup clk_lane_reg1 timings. So use
>> + * following sequence, stop when we got a frequency.
>> + * - Get it from device tree. It allow to connect sensor without support of
>> + * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
>> + * is that is that link frequency must be constant.
>> + * - Get it from V4L2_CID_LINK_FREQ.
>> + * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
>> + * to compute it.
>> + */
>> +static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
>> + __u32 code,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + u32 link_freq;
>> +
>> + link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
>> + if (link_freq)
>> + goto success;
>> +
>> + link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, sensor);
>> + if (link_freq)
>> + goto success;
>> +
>> + link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, code,
>> + sensor);
>> + if (link_freq)
>> + goto success;
>> + dev_err(&client->dev, "Failed to detect link frequency");
>> +
>> + return -EINVAL;
>> +
>> +success:
>> + dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
>> + bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
>> +
>> + /* midid02 doesn't support clock lane remapping */
>> + if (ep->bus.mipi_csi2.clock_lane != 0) {
>> + dev_err(&client->dev, "clk lane must be map to lane 0\n");
>> + return -EINVAL;
>> + }
>> + bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
>> + bool are_lanes_swap, bool *polarities)
>> +{
>> + bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
>> +
>> + if (nb == 1 && are_lanes_swap)
>> + return 0;
>> +
>> + /*
>> + * data lane 0 as pin swap polarity reversed compared to clock and
>> + *data lane 1
>> + */
>> + if (!are_pin_swap)
>> + bridge->r.data_lane0_reg1 = 1 << 1;
>> + bridge->r.data_lane0_reg1 |= DATA_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
>> + bool are_lanes_swap, bool *polarities)
>> +{
>> + bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
>> +
>> + if (nb == 1 && !are_lanes_swap)
>> + return 0;
>> +
>> + if (are_pin_swap)
>> + bridge->r.data_lane1_reg1 = 1 << 1;
>> + bridge->r.data_lane1_reg1 |= DATA_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_from_rx(struct mipid02_dev *bridge, __u32 code,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
>> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
>> + int nb = ep->bus.mipi_csi2.num_data_lanes;
>> + int ret;
>> +
>> + ret = mipid02_configure_clk_lane(bridge);
>> + if (ret)
>> + return ret;
>> +
>> + ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
>> + polarities);
>> + if (ret)
>> + return ret;
>> +
>> + ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
>> + polarities);
>> + if (ret)
>> + return ret;
>> +
>> + bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
>> + bridge->r.mode_reg1 |= (nb - 1) << 1;
>> +
>> + return mipid02_configure_from_rx_speed(bridge, code, sensor);
>> +}
>> +
>> +static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->tx;
>> +
>> + bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
>> + bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
>> + if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
>> + bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
>> + if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
>> + bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_from_code(struct mipid02_dev *bridge, __u32 code)
>> +{
>> + u8 data_type;
>> +
>> + bridge->r.data_id_rreg = 0;
>> + data_type = data_type_from_code(code);
>> + if (!data_type)
>> + return -EINVAL;
>> + bridge->r.data_id_rreg = data_type;
>> +
>> + return 0;
>> +}
>> +
>> +static __u32 mipid02_get_source_code(struct mipid02_dev *bridge,
>> + struct v4l2_subdev *sensor)
>> +{
>> + struct v4l2_subdev_format fmt = {
>> + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
>> + };
>> + int ret;
>> +
>> + ret = v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt);
>> +
>> + return ret ? MEDIA_BUS_FMT_SBGGR8_1X8 : fmt.format.code;
>> +}
>> +
>> +static int mipid02_stream_disable(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret;
>> +
>> + /* Disable all lanes */
>> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
>> + if (ret)
>> + dev_err(&client->dev, "failed to stream off %d", ret);
>> +
>> + return ret;
>> +}
>> +
>> +static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
>> +{
>> + struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
>> + struct media_entity *entity;
>> +
>> + if (!mdev)
>> + return NULL;
>> +
>> + media_device_for_each_entity(entity, mdev)
>> + if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
>> + return media_entity_to_v4l2_subdev(entity);
>> +
>> + return NULL;
>> +}
>> +
>> +static int mipid02_stream_enable(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_subdev *sensor;
>> + __u32 code;
>> + int ret = -EINVAL;
>> +
>> + sensor = mipid02_find_sensor(bridge);
>> + if (!sensor)
>> + goto error;
>> +
>> + dev_dbg(&client->dev, "use sensor '%s'", sensor->name);
>> + memset(&bridge->r, 0, sizeof(bridge->r));
>> + /* build registers content */
>> + code = mipid02_get_source_code(bridge, sensor);
>> + ret = mipid02_configure_from_rx(bridge, code, sensor);
>> + ret |= mipid02_configure_from_tx(bridge);
>> + ret |= mipid02_configure_from_code(bridge, code);
>> +
>> + /* write mipi registers */
>> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
>> + bridge->r.clk_lane_reg1);
>> + ret |= mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
>> + bridge->r.data_lane0_reg1);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
>> + DATA_MIPI_CSI);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
>> + bridge->r.data_lane1_reg1);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
>> + DATA_MIPI_CSI);
>> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG1,
>> + MODE_NO_BYPASS | bridge->r.mode_reg1);
>> + ret |= mipid02_write_reg(bridge, MIPID02_MODE_REG2,
>> + bridge->r.mode_reg2);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
>> + bridge->r.data_id_rreg);
>> + ret |= mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
>> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
>> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
>> + bridge->r.pix_width_ctrl);
>> + ret |= mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
>> + bridge->r.pix_width_ctrl_emb);
>> +
>> + if (ret)
>> + goto error;
>> +
>> + return 0;
>> +
>> +error:
>> + dev_err(&client->dev, "failed to stream on %d", ret);
>> + mipid02_stream_disable(bridge);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret = 0;
>> +
>> + dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
>> + enable, bridge->streaming);
>> + mutex_lock(&bridge->lock);
>> +
>> + if (bridge->streaming == enable)
>> + goto out;
>> +
>> + ret = enable ? mipid02_stream_enable(bridge) :
>> + mipid02_stream_disable(bridge);
>> + if (!ret)
>> + bridge->streaming = enable;
>> +
>> +out:
>> + dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
>> + bridge->streaming, ret);
>> + mutex_unlock(&bridge->lock);
>> +
>> + return ret;
>> +}
>> +
>> +static const struct v4l2_subdev_video_ops mipid02_video_ops = {
>> + .s_stream = mipid02_s_stream,
>> +};
>> +
>> +static const struct v4l2_subdev_ops mipid02_subdev_ops = {
>> + .video = &mipid02_video_ops,
>
> You'll need to add support for set_fmt, get_fmt subdev pad ops and the
> link_validate media entity op.
>
ok
See my reply to 'Re: [PATCH v1 2/3] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver'
>> +};
>> +
>> +static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
>> + struct v4l2_subdev *s_subdev,
>> + struct v4l2_async_subdev *asd)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int source_pad;
>> +
>> + dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
>> +
>> + source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
>> + s_subdev->fwnode,
>> + MEDIA_PAD_FL_SOURCE);
>> + if (source_pad < 0) {
>> + dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
>> + s_subdev->name);
>> + return source_pad;
>> + }
>> +
>> + return media_create_pad_link(&s_subdev->entity, source_pad,
>> + &bridge->sd.entity, 0,
>> + MEDIA_LNK_FL_ENABLED |
>> + MEDIA_LNK_FL_IMMUTABLE);
>> +}
>> +
>> +static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
>> + .bound = mipid02_async_bound,
>> +};
>> +
>> +static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint ep;
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct device_node *ep_node;
>> + int ret;
>> +
>> + memset(&ep, 0, sizeof(ep));
>> + ep.bus_type = V4L2_MBUS_CSI2_DPHY;
>> + /* parse rx (endpoint 0) */
>> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
>> + 0, 0);
>> + if (!ep_node) {
>> + dev_err(&client->dev, "unable to find port0 ep");
>> + ret = -EINVAL;
>> + goto error;
>> + }
>> +
>> + ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
>> + if (ret) {
>> + dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
>> + ret);
>> + goto error_of_node_put;
>> + }
>> +
>> + /* do some sanity checks */
>> + if (ep.bus.mipi_csi2.num_data_lanes > 2) {
>> + dev_err(&client->dev, "max supported data lanes is 2 / got %d",
>> + ep.bus.mipi_csi2.num_data_lanes);
>> + ret = -EINVAL;
>> + goto error_v4l2_fwnode_endpoint_free;
>> + }
>> +
>> + /* register it for later use */
>> + bridge->rx = ep;
>> + bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
>> + &bridge->link_frequency : NULL;
>
> I think you need to simply ignore the link frequencies here. The
> transmitting device can tell the frequency based on its configuration
> (based on the link frequencies). You seem to have implemented that already.
>
Idea of this was to allow some support for sensor that doesn't implement
V4L2_CID_PIXEL_RATE. Do you think it's useless ?
>> + v4l2_fwnode_endpoint_free(&ep);
>> +
>> + /* register async notifier so we get noticed when sensor is connected */
>> + bridge->asd.match.fwnode =
>> + fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
>> + bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
>> + of_node_put(ep_node);
>> +
>> + v4l2_async_notifier_init(&bridge->notifier);
>> + ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
>> + if (ret) {
>> + dev_err(&client->dev, "fail to register asd to notifier %d",
>> + ret);
>> + goto error_fwnode_handle_put;
>> + }
>> + bridge->notifier.ops = &mipid02_notifier_ops;
>> +
>> + ret = v4l2_async_subdev_notifier_register(&bridge->sd,
>> + &bridge->notifier);
>> + if (ret)
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> +
>> + return ret;
>> +
>> +error_v4l2_fwnode_endpoint_free:
>> + v4l2_fwnode_endpoint_free(&ep);
>> +error_of_node_put:
>> + of_node_put(ep_node);
>> +error:
>> +
>> + return ret;
>> +
>> +error_fwnode_handle_put:
>> + fwnode_handle_put(bridge->asd.match.fwnode);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_fwnode_endpoint ep;
>> + struct device_node *ep_node;
>> + int ret;
>> +
>> + memset(&ep, 0, sizeof(ep));
>> + ep.bus_type = V4L2_MBUS_PARALLEL;
>> + /* parse tx (endpoint 2) */
>> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
>> + 2, 0);
>> + if (!ep_node) {
>> + dev_err(&client->dev, "unable to find port1 ep");
>> + ret = -EINVAL;
>> + goto error;
>> + }
>> +
>> + ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
>> + if (ret) {
>> + dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
>> + goto error_of_node_put;
>> + }
>> +
>> + of_node_put(ep_node);
>> + bridge->tx = ep;
>> +
>> + return 0;
>> +
>> +error_of_node_put:
>> + of_node_put(ep_node);
>> +error:
>> +
>> + return -EINVAL;
>> +}
>> +
>> +static int mipid02_probe(struct i2c_client *client,
>> + const struct i2c_device_id *id)
>> +{
>> + struct device *dev = &client->dev;
>> + struct mipid02_dev *bridge;
>> + u32 clk_freq;
>> + int ret;
>> +
>> + bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
>> + if (!bridge)
>> + return -ENOMEM;
>> +
>> + bridge->i2c_client = client;
>> + v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
>> +
>> + /* got and check clock */
>> + bridge->xclk = devm_clk_get(dev, "xclk");
>> + if (IS_ERR(bridge->xclk)) {
>> + dev_err(dev, "failed to get xclk\n");
>> + return PTR_ERR(bridge->xclk);
>> + }
>> +
>> + clk_freq = clk_get_rate(bridge->xclk);
>> + if (clk_freq < 6000000 || clk_freq > 27000000) {
>> + dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
>> + clk_freq);
>> + return -EINVAL;
>> + }
>> +
>> + bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
>> + GPIOD_OUT_HIGH);
>> +
>> + ret = mipid02_get_regulators(bridge);
>> + if (ret) {
>> + dev_err(dev, "failed to get regulators %d", ret);
>> + return ret;
>> + }
>> +
>> + mutex_init(&bridge->lock);
>> + bridge->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
>> + bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
>> + bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
>> + bridge->pad[1].flags = MEDIA_PAD_FL_SOURCE;
>> + ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
>> + bridge->pad);
>> + if (ret) {
>> + dev_err(&client->dev, "pads init failed %d", ret);
>> + return ret;
>> + }
>> +
>> + /* enable clock, power and reset device if available */
>> + ret = mipid02_set_power_on(bridge);
>> + if (ret)
>> + goto entity_cleanup;
>> +
>> + ret = mipid02_detect(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to detect mipid02 %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = mipid02_parse_tx_ep(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to parse tx %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = mipid02_parse_rx_ep(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to parse rx %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = v4l2_async_register_subdev(&bridge->sd);
>> + if (ret < 0) {
>> + dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
>> + ret);
>> + goto unregister_notifier;
>> + }
>> +
>> + dev_info(&client->dev, "mipid02 device probe successfully");
>> +
>> + return 0;
>> +
>> +unregister_notifier:
>> + v4l2_async_notifier_unregister(&bridge->notifier);
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> +power_off:
>> + mipid02_set_power_off(bridge);
>> +entity_cleanup:
>> + media_entity_cleanup(&bridge->sd.entity);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_remove(struct i2c_client *client)
>> +{
>> + struct v4l2_subdev *sd = i2c_get_clientdata(client);
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> +
>> + v4l2_async_notifier_unregister(&bridge->notifier);
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> + v4l2_async_unregister_subdev(&bridge->sd);
>> + mipid02_set_power_off(bridge);
>> + media_entity_cleanup(&bridge->sd.entity);
>> +
>> + return 0;
>> +}
>> +
>> +static const struct i2c_device_id mipid02_id[] = {
>> + { "st-mipid02", 0},
>> + {},
>> +};
>> +MODULE_DEVICE_TABLE(i2c, mipid02_id);
>> +
>> +static const struct of_device_id mipid02_dt_ids[] = {
>> + { .compatible = "st,st-mipid02" },
>> + { /* sentinel */ }
>> +};
>> +MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
>> +
>> +static struct i2c_driver mipid02_i2c_driver = {
>> + .driver = {
>> + .name = "st-mipid02",
>> + .of_match_table = mipid02_dt_ids,
>> + },
>> + .id_table = mipid02_id,
>> + .probe = mipid02_probe,
>> + .remove = mipid02_remove,
>> +};
>> +
>> +module_i2c_driver(mipid02_i2c_driver);
>> +
>> +MODULE_AUTHOR("Mickael Guene <[email protected]>");
>> +MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
>> +MODULE_LICENSE("GPL v2");
>

2019-03-26 10:04:58

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v3 1/2] dt-bindings: Document MIPID02 bindings

This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
bridge.

Signed-off-by: Mickael Guene <[email protected]>
---

Changes in v3: None
Changes in v2:
- Add precision about first CSI-2 port data rate
- Document endpoints supported properties
- Rename 'mipid02@14' into generic 'csi2rx@14' in example

.../bindings/media/i2c/st,st-mipid02.txt | 83 ++++++++++++++++++++++
MAINTAINERS | 7 ++
2 files changed, 90 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt

diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
new file mode 100644
index 0000000..dfeab45
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
@@ -0,0 +1,83 @@
+STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
+
+MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
+time. Active port input stream will be de-serialized and its content outputted
+through PARALLEL output port.
+CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
+input port is a single lane 800Mbps. Both ports support clock and data lane
+polarity swap. First port also supports data lane swap.
+PARALLEL output port has a maximum width of 12 bits.
+Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
+YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
+
+Required Properties:
+- compatible: should be "st,st-mipid02"
+- clocks: reference to the xclk input clock.
+- clock-names: should be "xclk".
+- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
+- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
+
+Optional Properties:
+- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
+ This is an active low signal to the mipid02.
+
+Required subnodes:
+ - ports: A ports node with one port child node per device input and output
+ port, in accordance with the video interface bindings defined in
+ Documentation/devicetree/bindings/media/video-interfaces.txt. The
+ port nodes are numbered as follows:
+
+ Port Description
+ -----------------------------
+ 0 CSI-2 first input port
+ 1 CSI-2 second input port
+ 2 PARALLEL output
+
+Endpoint node optional properties for CSI-2 connection are:
+- bus-type: if present should be 4 - MIPI CSI-2 D-PHY.
+- clock-lanes: should be set to <0> if present (clock lane on hardware lane 0).
+- data-lanes: if present should be <1> for Port 1. for Port 0 dual-lane
+operation should be <1 2> or <2 1>. For Port 0 single-lane operation should be
+<1> or <2>.
+- lane-polarities: any lane can be inverted.
+
+Endpoint node optional properties for PARALLEL connection are:
+- bus-type: if present should be 5 - Parallel.
+- bus-width: shall be set to <6>, <7>, <8>, <10> or <12>.
+- hsync-active: active state of the HSYNC signal, 0/1 for LOW/HIGH respectively.
+- vsync-active: active state of the VSYNC signal, 0/1 for LOW/HIGH respectively.
+
+Example:
+
+mipid02: csi2rx@14 {
+ compatible = "st,st-mipid02";
+ reg = <0x14>;
+ status = "okay";
+ clocks = <&clk_ext_camera_12>;
+ clock-names = "xclk";
+ VDDE-supply = <&vdd>;
+ VDDIN-supply = <&vdd>;
+ ports {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ port@0 {
+ reg = <0>;
+
+ ep0: endpoint {
+ clock-lanes = <0>;
+ data-lanes = <1 2>;
+ remote-endpoint = <&mipi_csi2_in>;
+ };
+ };
+ port@2 {
+ reg = <2>;
+
+ ep2: endpoint {
+ bus-width = <8>;
+ hsync-active = <0>;
+ vsync-active = <0>;
+ remote-endpoint = <&parallel_out>;
+ };
+ };
+ };
+};
diff --git a/MAINTAINERS b/MAINTAINERS
index e17ebf7..74da99d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14668,6 +14668,13 @@ S: Maintained
F: drivers/iio/imu/st_lsm6dsx/
F: Documentation/devicetree/bindings/iio/imu/st_lsm6dsx.txt

+ST MIPID02 CSI-2 TO PARALLEL BRIDGE DRIVER
+M: Mickael Guene <[email protected]>
+L: [email protected]
+T: git git://linuxtv.org/media_tree.git
+S: Maintained
+F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
+
ST STM32 I2C/SMBUS DRIVER
M: Pierre-Yves MORDRET <[email protected]>
L: [email protected]
--
2.7.4


2019-03-26 10:07:12

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

This V4L2 subdev driver enables STMicroelectronics MIPID02 device.

Signed-off-by: Mickael Guene <[email protected]>
---

Changes in v3:
- Fix potential wrong error code for mipid02_stream_disable and mipid02_stream_enable
- Remove useless memset for ep in mipid02_parse_rx_ep and mipid02_parse_tx_ep
- Add second CSI-2 input pad even if it's not yet supported
- Add support of get_fmt, set_fmt and link_validate and only access subdev connected to mipid02

Changes in v2:
- Merge MAINTAINERS patch 3 into patch 1 and 2
- Fix line too long in Kconfig
- Add missing delay after reset release
- Various style fixes
- Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed

MAINTAINERS | 1 +
drivers/media/i2c/Kconfig | 14 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/st-mipid02.c | 1012 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 1028 insertions(+)
create mode 100644 drivers/media/i2c/st-mipid02.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 74da99d..a14fe81 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14673,6 +14673,7 @@ M: Mickael Guene <[email protected]>
L: [email protected]
T: git git://linuxtv.org/media_tree.git
S: Maintained
+F: drivers/media/i2c/st-mipid02.c
F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt

ST STM32 I2C/SMBUS DRIVER
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 1ef6335..be2470b 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -1135,6 +1135,20 @@ config VIDEO_I2C
To compile this driver as a module, choose M here: the
module will be called video-i2c

+config VIDEO_ST_MIPID02
+ tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
+ depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
+ depends on MEDIA_CAMERA_SUPPORT
+ depends on MEDIA_CONTROLLER
+ select V4L2_FWNODE
+ help
+ Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
+ It is used to allow usage of CSI-2 sensor with PARALLEL port
+ controller.
+
+ To compile this driver as a module, choose M here: the
+ module will be called st-mipid02.
+
endmenu

endif
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index a64fca8..d8ad9da 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
obj-$(CONFIG_VIDEO_IMX274) += imx274.o
obj-$(CONFIG_VIDEO_IMX319) += imx319.o
obj-$(CONFIG_VIDEO_IMX355) += imx355.o
+obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o

obj-$(CONFIG_SDR_MAX2175) += max2175.o
diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
new file mode 100644
index 0000000..5bb9865
--- /dev/null
+++ b/drivers/media/i2c/st-mipid02.c
@@ -0,0 +1,1012 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
+ *
+ * Copyright (C) STMicroelectronics SA 2019
+ * Authors: Mickael Guene <[email protected]>
+ * for STMicroelectronics.
+ *
+ *
+ */
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/of_graph.h>
+#include <linux/regulator/consumer.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+
+#define MIPID02_CLK_LANE_WR_REG1 0x01
+#define MIPID02_CLK_LANE_REG1 0x02
+#define MIPID02_CLK_LANE_REG3 0x04
+#define MIPID02_DATA_LANE0_REG1 0x05
+#define MIPID02_DATA_LANE0_REG2 0x06
+#define MIPID02_DATA_LANE1_REG1 0x09
+#define MIPID02_DATA_LANE1_REG2 0x0a
+#define MIPID02_MODE_REG1 0x14
+#define MIPID02_MODE_REG2 0x15
+#define MIPID02_DATA_ID_RREG 0x17
+#define MIPID02_DATA_SELECTION_CTRL 0x19
+#define MIPID02_PIX_WIDTH_CTRL 0x1e
+#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
+
+/* Bits definition for MIPID02_CLK_LANE_REG1 */
+#define CLK_ENABLE BIT(0)
+/* Bits definition for MIPID02_CLK_LANE_REG3 */
+#define CLK_MIPI_CSI BIT(1)
+/* Bits definition for MIPID02_DATA_LANE0_REG1 */
+#define DATA_ENABLE BIT(0)
+/* Bits definition for MIPID02_DATA_LANEx_REG2 */
+#define DATA_MIPI_CSI BIT(0)
+/* Bits definition for MIPID02_MODE_REG1 */
+#define MODE_DATA_SWAP BIT(2)
+#define MODE_NO_BYPASS BIT(6)
+/* Bits definition for MIPID02_MODE_REG2 */
+#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
+#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
+/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
+#define SELECTION_MANUAL_DATA BIT(2)
+#define SELECTION_MANUAL_WIDTH BIT(3)
+
+static const u32 mipid02_supported_fmt_codes[] = {
+ MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
+ MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
+ MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
+ MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
+ MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SGBRG12_1X12,
+ MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SRGGB12_1X12,
+ MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_BGR888_1X24
+};
+
+/* regulator supplies */
+static const char * const mipid02_supply_name[] = {
+ "VDDE", /* 1.8V digital I/O supply */
+ "VDDIN", /* 1V8 voltage regulator supply */
+};
+
+#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
+#define MIPID02_PAD_NB 3
+
+struct mipid02_dev {
+ struct i2c_client *i2c_client;
+ struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
+ struct v4l2_subdev sd;
+ struct media_pad pad[MIPID02_PAD_NB];
+ struct clk *xclk;
+ struct gpio_desc *reset_gpio;
+ /* endpoints info */
+ struct v4l2_fwnode_endpoint rx;
+ u64 link_frequency;
+ struct v4l2_fwnode_endpoint tx;
+ /* remote source */
+ struct v4l2_async_subdev asd;
+ struct v4l2_async_notifier notifier;
+ struct v4l2_subdev *s_subdev;
+ /* registers */
+ struct {
+ u8 clk_lane_reg1;
+ u8 data_lane0_reg1;
+ u8 data_lane1_reg1;
+ u8 mode_reg1;
+ u8 mode_reg2;
+ u8 data_id_rreg;
+ u8 pix_width_ctrl;
+ u8 pix_width_ctrl_emb;
+ } r;
+ /* lock to protect all members below */
+ struct mutex lock;
+ bool streaming;
+ struct v4l2_mbus_framefmt fmt;
+};
+
+static int bpp_from_code(__u32 code)
+{
+ switch (code) {
+ case MEDIA_BUS_FMT_SBGGR8_1X8:
+ case MEDIA_BUS_FMT_SGBRG8_1X8:
+ case MEDIA_BUS_FMT_SGRBG8_1X8:
+ case MEDIA_BUS_FMT_SRGGB8_1X8:
+ return 8;
+ case MEDIA_BUS_FMT_SBGGR10_1X10:
+ case MEDIA_BUS_FMT_SGBRG10_1X10:
+ case MEDIA_BUS_FMT_SGRBG10_1X10:
+ case MEDIA_BUS_FMT_SRGGB10_1X10:
+ return 10;
+ case MEDIA_BUS_FMT_SBGGR12_1X12:
+ case MEDIA_BUS_FMT_SGBRG12_1X12:
+ case MEDIA_BUS_FMT_SGRBG12_1X12:
+ case MEDIA_BUS_FMT_SRGGB12_1X12:
+ return 12;
+ case MEDIA_BUS_FMT_UYVY8_2X8:
+ return 16;
+ case MEDIA_BUS_FMT_BGR888_1X24:
+ return 24;
+ default:
+ return 0;
+ }
+}
+
+static u8 data_type_from_code(__u32 code)
+{
+ switch (code) {
+ case MEDIA_BUS_FMT_SBGGR8_1X8:
+ case MEDIA_BUS_FMT_SGBRG8_1X8:
+ case MEDIA_BUS_FMT_SGRBG8_1X8:
+ case MEDIA_BUS_FMT_SRGGB8_1X8:
+ return 0x2a;
+ case MEDIA_BUS_FMT_SBGGR10_1X10:
+ case MEDIA_BUS_FMT_SGBRG10_1X10:
+ case MEDIA_BUS_FMT_SGRBG10_1X10:
+ case MEDIA_BUS_FMT_SRGGB10_1X10:
+ return 0x2b;
+ case MEDIA_BUS_FMT_SBGGR12_1X12:
+ case MEDIA_BUS_FMT_SGBRG12_1X12:
+ case MEDIA_BUS_FMT_SGRBG12_1X12:
+ case MEDIA_BUS_FMT_SRGGB12_1X12:
+ return 0x2c;
+ case MEDIA_BUS_FMT_UYVY8_2X8:
+ return 0x1e;
+ case MEDIA_BUS_FMT_BGR888_1X24:
+ return 0x24;
+ default:
+ return 0;
+ }
+}
+
+static __u32 get_fmt_code(__u32 code)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(mipid02_supported_fmt_codes); i++) {
+ if (code == mipid02_supported_fmt_codes[i])
+ return code;
+ }
+
+ return mipid02_supported_fmt_codes[0];
+}
+
+static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
+{
+ return container_of(sd, struct mipid02_dev, sd);
+}
+
+static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct i2c_msg msg[2];
+ u8 buf[2];
+ int ret;
+
+ buf[0] = reg >> 8;
+ buf[1] = reg & 0xff;
+
+ msg[0].addr = client->addr;
+ msg[0].flags = client->flags;
+ msg[0].buf = buf;
+ msg[0].len = sizeof(buf);
+
+ msg[1].addr = client->addr;
+ msg[1].flags = client->flags | I2C_M_RD;
+ msg[1].buf = val;
+ msg[1].len = 1;
+
+ ret = i2c_transfer(client->adapter, msg, 2);
+ if (ret < 0) {
+ dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
+ __func__, client->addr, reg, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct i2c_msg msg;
+ u8 buf[3];
+ int ret;
+
+ buf[0] = reg >> 8;
+ buf[1] = reg & 0xff;
+ buf[2] = val;
+
+ msg.addr = client->addr;
+ msg.flags = client->flags;
+ msg.buf = buf;
+ msg.len = sizeof(buf);
+
+ ret = i2c_transfer(client->adapter, &msg, 1);
+ if (ret < 0) {
+ dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
+ __func__, reg, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mipid02_get_regulators(struct mipid02_dev *bridge)
+{
+ unsigned int i;
+
+ for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
+ bridge->supplies[i].supply = mipid02_supply_name[i];
+
+ return devm_regulator_bulk_get(&bridge->i2c_client->dev,
+ MIPID02_NUM_SUPPLIES,
+ bridge->supplies);
+}
+
+static void mipid02_apply_reset(struct mipid02_dev *bridge)
+{
+ gpiod_set_value_cansleep(bridge->reset_gpio, 0);
+ usleep_range(5000, 10000);
+ gpiod_set_value_cansleep(bridge->reset_gpio, 1);
+ usleep_range(5000, 10000);
+ gpiod_set_value_cansleep(bridge->reset_gpio, 0);
+ usleep_range(5000, 10000);
+}
+
+static int mipid02_set_power_on(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret;
+
+ ret = clk_prepare_enable(bridge->xclk);
+ if (ret) {
+ dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
+ return ret;
+ }
+
+ ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
+ bridge->supplies);
+ if (ret) {
+ dev_err(&client->dev, "%s: failed to enable regulators\n",
+ __func__);
+ goto xclk_off;
+ }
+
+ if (bridge->reset_gpio) {
+ dev_dbg(&client->dev, "apply reset");
+ mipid02_apply_reset(bridge);
+ } else {
+ dev_dbg(&client->dev, "dont apply reset");
+ usleep_range(5000, 10000);
+ }
+
+ return 0;
+
+xclk_off:
+ clk_disable_unprepare(bridge->xclk);
+ return ret;
+}
+
+static void mipid02_set_power_off(struct mipid02_dev *bridge)
+{
+ regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
+ clk_disable_unprepare(bridge->xclk);
+}
+
+static int mipid02_detect(struct mipid02_dev *bridge)
+{
+ u8 reg;
+
+ /*
+ * There is no version registers. Just try to read register
+ * MIPID02_CLK_LANE_WR_REG1.
+ */
+ return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
+}
+
+static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+
+ if (ep->nr_of_link_frequencies != 1)
+ return 0;
+
+ return ep->link_frequencies[0];
+}
+
+static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
+ struct v4l2_subdev *subdev)
+{
+ struct v4l2_ctrl *ctrl;
+
+ ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_LINK_FREQ);
+ if (!ctrl)
+ return 0;
+
+ return v4l2_ctrl_g_ctrl_int64(ctrl);
+}
+
+static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
+ struct v4l2_subdev *subdev)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ struct v4l2_ctrl *ctrl;
+ u32 pixel_clock;
+ u32 bpp = bpp_from_code(bridge->fmt.code);
+
+ ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
+ if (!ctrl)
+ return 0;
+ pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
+
+ return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
+}
+
+/*
+ * We need to know link frequency to setup clk_lane_reg1 timings. So use
+ * following sequence, stop when we got a frequency.
+ * - Get it from device tree. It allow to connect subdev without support of
+ * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
+ * is that is that link frequency must be constant.
+ * - Get it from V4L2_CID_LINK_FREQ.
+ * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
+ * to compute it.
+ */
+static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_subdev *subdev = bridge->s_subdev;
+ u32 link_freq;
+
+ link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
+ if (link_freq)
+ goto success;
+
+ link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, subdev);
+ if (link_freq)
+ goto success;
+
+ link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, subdev);
+ if (link_freq)
+ goto success;
+ dev_err(&client->dev, "Failed to detect link frequency");
+
+ return -EINVAL;
+
+success:
+ dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
+ bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
+
+ return 0;
+}
+
+static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ bool *polarities = ep->bus.mipi_csi2.lane_polarities;
+
+ /* midid02 doesn't support clock lane remapping */
+ if (ep->bus.mipi_csi2.clock_lane != 0) {
+ dev_err(&client->dev, "clk lane must be map to lane 0\n");
+ return -EINVAL;
+ }
+ bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
+ bool are_lanes_swap, bool *polarities)
+{
+ bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
+
+ if (nb == 1 && are_lanes_swap)
+ return 0;
+
+ /*
+ * data lane 0 as pin swap polarity reversed compared to clock and
+ *data lane 1
+ */
+ if (!are_pin_swap)
+ bridge->r.data_lane0_reg1 = 1 << 1;
+ bridge->r.data_lane0_reg1 |= DATA_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
+ bool are_lanes_swap, bool *polarities)
+{
+ bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
+
+ if (nb == 1 && !are_lanes_swap)
+ return 0;
+
+ if (are_pin_swap)
+ bridge->r.data_lane1_reg1 = 1 << 1;
+ bridge->r.data_lane1_reg1 |= DATA_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_from_rx(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
+ bool *polarities = ep->bus.mipi_csi2.lane_polarities;
+ int nb = ep->bus.mipi_csi2.num_data_lanes;
+ int ret;
+
+ ret = mipid02_configure_clk_lane(bridge);
+ if (ret)
+ return ret;
+
+ ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
+ polarities);
+ if (ret)
+ return ret;
+
+ ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
+ polarities);
+ if (ret)
+ return ret;
+
+ bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
+ bridge->r.mode_reg1 |= (nb - 1) << 1;
+
+ return mipid02_configure_from_rx_speed(bridge);
+}
+
+static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->tx;
+
+ bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
+ bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
+ if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
+ bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
+ if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
+ bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
+
+ return 0;
+}
+
+static int mipid02_configure_from_code(struct mipid02_dev *bridge)
+{
+ u8 data_type;
+
+ bridge->r.data_id_rreg = 0;
+ data_type = data_type_from_code(bridge->fmt.code);
+ if (!data_type)
+ return -EINVAL;
+ bridge->r.data_id_rreg = data_type;
+
+ return 0;
+}
+
+static int mipid02_stream_disable(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret;
+
+ /* Disable all lanes */
+ ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
+ if (ret)
+ goto error;
+error:
+ if (ret)
+ dev_err(&client->dev, "failed to stream off %d", ret);
+
+ return ret;
+}
+
+static int mipid02_stream_enable(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret = -EINVAL;
+
+ if (!bridge->s_subdev)
+ goto error;
+
+ memset(&bridge->r, 0, sizeof(bridge->r));
+ /* build registers content */
+ ret = mipid02_configure_from_rx(bridge);
+ if (ret)
+ goto error;
+ ret = mipid02_configure_from_tx(bridge);
+ if (ret)
+ goto error;
+ ret = mipid02_configure_from_code(bridge);
+ if (ret)
+ goto error;
+
+ /* write mipi registers */
+ ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
+ bridge->r.clk_lane_reg1);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
+ bridge->r.data_lane0_reg1);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
+ DATA_MIPI_CSI);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
+ bridge->r.data_lane1_reg1);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
+ DATA_MIPI_CSI);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_MODE_REG1,
+ MODE_NO_BYPASS | bridge->r.mode_reg1);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_MODE_REG2,
+ bridge->r.mode_reg2);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
+ bridge->r.data_id_rreg);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
+ SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
+ bridge->r.pix_width_ctrl);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
+ bridge->r.pix_width_ctrl_emb);
+ if (ret)
+ goto error;
+
+ return 0;
+
+error:
+ dev_err(&client->dev, "failed to stream on %d", ret);
+ mipid02_stream_disable(bridge);
+
+ return ret;
+}
+
+static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int ret = 0;
+
+ dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
+ enable, bridge->streaming);
+ mutex_lock(&bridge->lock);
+
+ if (bridge->streaming == enable)
+ goto out;
+
+ ret = enable ? mipid02_stream_enable(bridge) :
+ mipid02_stream_disable(bridge);
+ if (!ret)
+ bridge->streaming = enable;
+
+out:
+ dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
+ bridge->streaming, ret);
+ mutex_unlock(&bridge->lock);
+
+ return ret;
+}
+
+static int mipid02_get_fmt(struct v4l2_subdev *sd,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_format *format)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_mbus_framefmt *fmt;
+
+ dev_dbg(&client->dev, "%s probe %d", __func__, format->pad);
+
+ if (format->pad >= MIPID02_PAD_NB)
+ return -EINVAL;
+ /* second CSI-2 pad not yet supported */
+ if (format->pad == 1)
+ return -EINVAL;
+
+ if (format->which == V4L2_SUBDEV_FORMAT_TRY)
+ fmt = v4l2_subdev_get_try_format(&bridge->sd, cfg, format->pad);
+ else
+ fmt = &bridge->fmt;
+
+ mutex_lock(&bridge->lock);
+
+ format->format = *fmt;
+
+ mutex_unlock(&bridge->lock);
+
+ return 0;
+}
+
+static int mipid02_set_fmt(struct v4l2_subdev *sd,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_format *format)
+{
+ struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_mbus_framefmt *fmt;
+ int ret;
+
+ dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
+
+ if (format->pad >= MIPID02_PAD_NB)
+ return -EINVAL;
+ /* second CSI-2 pad not yet supported */
+ if (format->pad == 1)
+ return -EINVAL;
+
+ mutex_lock(&bridge->lock);
+
+ if (bridge->streaming) {
+ ret = -EBUSY;
+ goto error;
+ }
+
+ if (format->which == V4L2_SUBDEV_FORMAT_TRY)
+ fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
+ else
+ fmt = &bridge->fmt;
+
+ *fmt = *mbus_fmt;
+ fmt->code = get_fmt_code(mbus_fmt->code);
+ fmt->field = V4L2_FIELD_NONE;
+ fmt->colorspace = V4L2_COLORSPACE_SRGB;
+ fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
+ fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
+ fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
+
+ ret = 0;
+
+error:
+ mutex_unlock(&bridge->lock);
+
+ return ret;
+}
+
+static const struct v4l2_subdev_video_ops mipid02_video_ops = {
+ .s_stream = mipid02_s_stream,
+};
+
+static const struct v4l2_subdev_pad_ops mipid02_pad_ops = {
+ .get_fmt = mipid02_get_fmt,
+ .set_fmt = mipid02_set_fmt,
+};
+
+static const struct v4l2_subdev_ops mipid02_subdev_ops = {
+ .video = &mipid02_video_ops,
+ .pad = &mipid02_pad_ops,
+};
+
+static const struct media_entity_operations mipid02_subdev_entity_ops = {
+ .link_validate = v4l2_subdev_link_validate,
+};
+
+static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
+ struct v4l2_subdev *s_subdev,
+ struct v4l2_async_subdev *asd)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int source_pad;
+ int ret;
+
+ dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
+
+ source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
+ s_subdev->fwnode,
+ MEDIA_PAD_FL_SOURCE);
+ if (source_pad < 0) {
+ dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
+ s_subdev->name);
+ return source_pad;
+ }
+
+ ret = media_create_pad_link(&s_subdev->entity, source_pad,
+ &bridge->sd.entity, 0,
+ MEDIA_LNK_FL_ENABLED |
+ MEDIA_LNK_FL_IMMUTABLE);
+ if (ret) {
+ dev_err(&client->dev, "Couldn't create media link %d", ret);
+ return ret;
+ }
+
+ bridge->s_subdev = s_subdev;
+
+ return 0;
+}
+
+static void mipid02_async_unbind(struct v4l2_async_notifier *notifier,
+ struct v4l2_subdev *s_subdev,
+ struct v4l2_async_subdev *asd)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
+
+ bridge->s_subdev = NULL;
+}
+
+static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
+ .bound = mipid02_async_bound,
+ .unbind = mipid02_async_unbind,
+};
+
+static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_CSI2_DPHY };
+ struct i2c_client *client = bridge->i2c_client;
+ struct device_node *ep_node;
+ int ret;
+
+ /* parse rx (endpoint 0) */
+ ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
+ 0, 0);
+ if (!ep_node) {
+ dev_err(&client->dev, "unable to find port0 ep");
+ ret = -EINVAL;
+ goto error;
+ }
+
+ ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
+ if (ret) {
+ dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
+ ret);
+ goto error_of_node_put;
+ }
+
+ /* do some sanity checks */
+ if (ep.bus.mipi_csi2.num_data_lanes > 2) {
+ dev_err(&client->dev, "max supported data lanes is 2 / got %d",
+ ep.bus.mipi_csi2.num_data_lanes);
+ ret = -EINVAL;
+ goto error_v4l2_fwnode_endpoint_free;
+ }
+
+ /* register it for later use */
+ bridge->rx = ep;
+ bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
+ &bridge->link_frequency : NULL;
+ v4l2_fwnode_endpoint_free(&ep);
+
+ /* register async notifier so we get noticed when sensor is connected */
+ bridge->asd.match.fwnode =
+ fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
+ bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
+ of_node_put(ep_node);
+
+ v4l2_async_notifier_init(&bridge->notifier);
+ ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
+ if (ret) {
+ dev_err(&client->dev, "fail to register asd to notifier %d",
+ ret);
+ goto error_fwnode_handle_put;
+ }
+ bridge->notifier.ops = &mipid02_notifier_ops;
+
+ ret = v4l2_async_subdev_notifier_register(&bridge->sd,
+ &bridge->notifier);
+ if (ret)
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+
+ return ret;
+
+error_v4l2_fwnode_endpoint_free:
+ v4l2_fwnode_endpoint_free(&ep);
+error_of_node_put:
+ of_node_put(ep_node);
+error:
+
+ return ret;
+
+error_fwnode_handle_put:
+ fwnode_handle_put(bridge->asd.match.fwnode);
+
+ return ret;
+}
+
+static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_PARALLEL };
+ struct i2c_client *client = bridge->i2c_client;
+ struct device_node *ep_node;
+ int ret;
+
+ /* parse tx (endpoint 2) */
+ ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
+ 2, 0);
+ if (!ep_node) {
+ dev_err(&client->dev, "unable to find port1 ep");
+ ret = -EINVAL;
+ goto error;
+ }
+
+ ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
+ if (ret) {
+ dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
+ goto error_of_node_put;
+ }
+
+ of_node_put(ep_node);
+ bridge->tx = ep;
+
+ return 0;
+
+error_of_node_put:
+ of_node_put(ep_node);
+error:
+
+ return -EINVAL;
+}
+
+static int mipid02_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ struct device *dev = &client->dev;
+ struct mipid02_dev *bridge;
+ u32 clk_freq;
+ int ret;
+
+ bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
+ if (!bridge)
+ return -ENOMEM;
+
+ bridge->fmt.code = MEDIA_BUS_FMT_SBGGR8_1X8;
+ bridge->fmt.field = V4L2_FIELD_NONE;
+ bridge->fmt.colorspace = V4L2_COLORSPACE_SRGB;
+ bridge->fmt.ycbcr_enc =
+ V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
+ bridge->fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
+ bridge->fmt.xfer_func =
+ V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
+ bridge->fmt.width = 640;
+ bridge->fmt.height = 480;
+
+ bridge->i2c_client = client;
+ v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
+
+ /* got and check clock */
+ bridge->xclk = devm_clk_get(dev, "xclk");
+ if (IS_ERR(bridge->xclk)) {
+ dev_err(dev, "failed to get xclk\n");
+ return PTR_ERR(bridge->xclk);
+ }
+
+ clk_freq = clk_get_rate(bridge->xclk);
+ if (clk_freq < 6000000 || clk_freq > 27000000) {
+ dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
+ clk_freq);
+ return -EINVAL;
+ }
+
+ bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
+ GPIOD_OUT_HIGH);
+
+ ret = mipid02_get_regulators(bridge);
+ if (ret) {
+ dev_err(dev, "failed to get regulators %d", ret);
+ return ret;
+ }
+
+ mutex_init(&bridge->lock);
+ bridge->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+ bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
+ bridge->sd.entity.ops = &mipid02_subdev_entity_ops;
+ bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
+ bridge->pad[1].flags = MEDIA_PAD_FL_SINK;
+ bridge->pad[2].flags = MEDIA_PAD_FL_SOURCE;
+ ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
+ bridge->pad);
+ if (ret) {
+ dev_err(&client->dev, "pads init failed %d", ret);
+ return ret;
+ }
+
+ /* enable clock, power and reset device if available */
+ ret = mipid02_set_power_on(bridge);
+ if (ret)
+ goto entity_cleanup;
+
+ ret = mipid02_detect(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to detect mipid02 %d", ret);
+ goto power_off;
+ }
+
+ ret = mipid02_parse_tx_ep(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to parse tx %d", ret);
+ goto power_off;
+ }
+
+ ret = mipid02_parse_rx_ep(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to parse rx %d", ret);
+ goto power_off;
+ }
+
+ ret = v4l2_async_register_subdev(&bridge->sd);
+ if (ret < 0) {
+ dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
+ ret);
+ goto unregister_notifier;
+ }
+
+ dev_info(&client->dev, "mipid02 device probe successfully");
+
+ return 0;
+
+unregister_notifier:
+ v4l2_async_notifier_unregister(&bridge->notifier);
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+power_off:
+ mipid02_set_power_off(bridge);
+entity_cleanup:
+ media_entity_cleanup(&bridge->sd.entity);
+
+ return ret;
+}
+
+static int mipid02_remove(struct i2c_client *client)
+{
+ struct v4l2_subdev *sd = i2c_get_clientdata(client);
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+
+ v4l2_async_notifier_unregister(&bridge->notifier);
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+ v4l2_async_unregister_subdev(&bridge->sd);
+ mipid02_set_power_off(bridge);
+ media_entity_cleanup(&bridge->sd.entity);
+
+ return 0;
+}
+
+static const struct i2c_device_id mipid02_id[] = {
+ { "st-mipid02", 0},
+ {},
+};
+MODULE_DEVICE_TABLE(i2c, mipid02_id);
+
+static const struct of_device_id mipid02_dt_ids[] = {
+ { .compatible = "st,st-mipid02" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
+
+static struct i2c_driver mipid02_i2c_driver = {
+ .driver = {
+ .name = "st-mipid02",
+ .of_match_table = mipid02_dt_ids,
+ },
+ .id_table = mipid02_id,
+ .probe = mipid02_probe,
+ .remove = mipid02_remove,
+};
+
+module_i2c_driver(mipid02_i2c_driver);
+
+MODULE_AUTHOR("Mickael Guene <[email protected]>");
+MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
+MODULE_LICENSE("GPL v2");
--
2.7.4


2019-03-26 10:07:28

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v3 0/2] Add support for MIPID02 CSI-2 to PARALLEL bridge I2C device

Hi,

This is the v3 of my MIPID02 series which introduces support of
STMicroelectronics MIPID02 CSI-2 to PARALLEL I2C bridge. It allows using a CSI-2
sensor with a PARALLEL interface. Current driver implementation doesn't support
CSI-2 second input port usage. It doesn't support also YUV420, RGB565 and RGB444
input formats.

Thanks to Sakari for review.

Changes in v3:
- Fix potential wrong error code for mipid02_stream_disable and mipid02_stream_enable
- Remove useless memset for ep in mipid02_parse_rx_ep and mipid02_parse_tx_ep
- Add second CSI-2 input pad even if it's not yet supported
- Add support of get_fmt, set_fmt and link_validate and only access subdev connected to mipid02

Changes in v2:
- Add precision about first CSI-2 port data rate
- Document endpoints supported properties
- Rename 'mipid02@14' into generic 'csi2rx@14' in example
- Merge MAINTAINERS patch 3 into patch 1 and 2
- Fix line too long in Kconfig
- Add missing delay after reset release
- Various style fixes
- Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed

Mickael Guene (2):
dt-bindings: Document MIPID02 bindings
media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

.../bindings/media/i2c/st,st-mipid02.txt | 83 ++
MAINTAINERS | 8 +
drivers/media/i2c/Kconfig | 14 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/st-mipid02.c | 1012 ++++++++++++++++++++
5 files changed, 1118 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
create mode 100644 drivers/media/i2c/st-mipid02.c

--
2.7.4


2019-03-26 11:35:33

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Mickael,

Thanks for the update.

On Tue, Mar 26, 2019 at 11:03:40AM +0100, Mickael Guene wrote:
> This V4L2 subdev driver enables STMicroelectronics MIPID02 device.
>
> Signed-off-by: Mickael Guene <[email protected]>
> ---
>
> Changes in v3:
> - Fix potential wrong error code for mipid02_stream_disable and mipid02_stream_enable
> - Remove useless memset for ep in mipid02_parse_rx_ep and mipid02_parse_tx_ep
> - Add second CSI-2 input pad even if it's not yet supported
> - Add support of get_fmt, set_fmt and link_validate and only access subdev connected to mipid02
>
> Changes in v2:
> - Merge MAINTAINERS patch 3 into patch 1 and 2
> - Fix line too long in Kconfig
> - Add missing delay after reset release
> - Various style fixes
> - Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed
>
> MAINTAINERS | 1 +
> drivers/media/i2c/Kconfig | 14 +
> drivers/media/i2c/Makefile | 1 +
> drivers/media/i2c/st-mipid02.c | 1012 ++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 1028 insertions(+)
> create mode 100644 drivers/media/i2c/st-mipid02.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 74da99d..a14fe81 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -14673,6 +14673,7 @@ M: Mickael Guene <[email protected]>
> L: [email protected]
> T: git git://linuxtv.org/media_tree.git
> S: Maintained
> +F: drivers/media/i2c/st-mipid02.c
> F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>
> ST STM32 I2C/SMBUS DRIVER
> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
> index 1ef6335..be2470b 100644
> --- a/drivers/media/i2c/Kconfig
> +++ b/drivers/media/i2c/Kconfig
> @@ -1135,6 +1135,20 @@ config VIDEO_I2C
> To compile this driver as a module, choose M here: the
> module will be called video-i2c
>
> +config VIDEO_ST_MIPID02
> + tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
> + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
> + depends on MEDIA_CAMERA_SUPPORT
> + depends on MEDIA_CONTROLLER
> + select V4L2_FWNODE
> + help
> + Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
> + It is used to allow usage of CSI-2 sensor with PARALLEL port
> + controller.
> +
> + To compile this driver as a module, choose M here: the
> + module will be called st-mipid02.
> +
> endmenu
>
> endif
> diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
> index a64fca8..d8ad9da 100644
> --- a/drivers/media/i2c/Makefile
> +++ b/drivers/media/i2c/Makefile
> @@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
> obj-$(CONFIG_VIDEO_IMX274) += imx274.o
> obj-$(CONFIG_VIDEO_IMX319) += imx319.o
> obj-$(CONFIG_VIDEO_IMX355) += imx355.o
> +obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o
>
> obj-$(CONFIG_SDR_MAX2175) += max2175.o
> diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
> new file mode 100644
> index 0000000..5bb9865
> --- /dev/null
> +++ b/drivers/media/i2c/st-mipid02.c
> @@ -0,0 +1,1012 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
> + *
> + * Copyright (C) STMicroelectronics SA 2019
> + * Authors: Mickael Guene <[email protected]>
> + * for STMicroelectronics.
> + *
> + *
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/delay.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/i2c.h>
> +#include <linux/module.h>
> +#include <linux/of_graph.h>
> +#include <linux/regulator/consumer.h>
> +#include <media/v4l2-async.h>
> +#include <media/v4l2-ctrls.h>
> +#include <media/v4l2-device.h>
> +#include <media/v4l2-fwnode.h>
> +#include <media/v4l2-subdev.h>
> +
> +#define MIPID02_CLK_LANE_WR_REG1 0x01
> +#define MIPID02_CLK_LANE_REG1 0x02
> +#define MIPID02_CLK_LANE_REG3 0x04
> +#define MIPID02_DATA_LANE0_REG1 0x05
> +#define MIPID02_DATA_LANE0_REG2 0x06
> +#define MIPID02_DATA_LANE1_REG1 0x09
> +#define MIPID02_DATA_LANE1_REG2 0x0a
> +#define MIPID02_MODE_REG1 0x14
> +#define MIPID02_MODE_REG2 0x15
> +#define MIPID02_DATA_ID_RREG 0x17
> +#define MIPID02_DATA_SELECTION_CTRL 0x19
> +#define MIPID02_PIX_WIDTH_CTRL 0x1e
> +#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
> +
> +/* Bits definition for MIPID02_CLK_LANE_REG1 */
> +#define CLK_ENABLE BIT(0)
> +/* Bits definition for MIPID02_CLK_LANE_REG3 */
> +#define CLK_MIPI_CSI BIT(1)
> +/* Bits definition for MIPID02_DATA_LANE0_REG1 */
> +#define DATA_ENABLE BIT(0)
> +/* Bits definition for MIPID02_DATA_LANEx_REG2 */
> +#define DATA_MIPI_CSI BIT(0)
> +/* Bits definition for MIPID02_MODE_REG1 */
> +#define MODE_DATA_SWAP BIT(2)
> +#define MODE_NO_BYPASS BIT(6)
> +/* Bits definition for MIPID02_MODE_REG2 */
> +#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
> +#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
> +/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
> +#define SELECTION_MANUAL_DATA BIT(2)
> +#define SELECTION_MANUAL_WIDTH BIT(3)
> +
> +static const u32 mipid02_supported_fmt_codes[] = {
> + MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
> + MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
> + MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
> + MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
> + MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SGBRG12_1X12,
> + MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SRGGB12_1X12,
> + MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_BGR888_1X24
> +};
> +
> +/* regulator supplies */
> +static const char * const mipid02_supply_name[] = {
> + "VDDE", /* 1.8V digital I/O supply */
> + "VDDIN", /* 1V8 voltage regulator supply */
> +};
> +
> +#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
> +#define MIPID02_PAD_NB 3
> +
> +struct mipid02_dev {
> + struct i2c_client *i2c_client;
> + struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
> + struct v4l2_subdev sd;
> + struct media_pad pad[MIPID02_PAD_NB];
> + struct clk *xclk;
> + struct gpio_desc *reset_gpio;
> + /* endpoints info */
> + struct v4l2_fwnode_endpoint rx;
> + u64 link_frequency;
> + struct v4l2_fwnode_endpoint tx;
> + /* remote source */
> + struct v4l2_async_subdev asd;
> + struct v4l2_async_notifier notifier;
> + struct v4l2_subdev *s_subdev;
> + /* registers */
> + struct {
> + u8 clk_lane_reg1;
> + u8 data_lane0_reg1;
> + u8 data_lane1_reg1;
> + u8 mode_reg1;
> + u8 mode_reg2;
> + u8 data_id_rreg;
> + u8 pix_width_ctrl;
> + u8 pix_width_ctrl_emb;
> + } r;
> + /* lock to protect all members below */
> + struct mutex lock;
> + bool streaming;
> + struct v4l2_mbus_framefmt fmt;
> +};
> +
> +static int bpp_from_code(__u32 code)
> +{
> + switch (code) {
> + case MEDIA_BUS_FMT_SBGGR8_1X8:
> + case MEDIA_BUS_FMT_SGBRG8_1X8:
> + case MEDIA_BUS_FMT_SGRBG8_1X8:
> + case MEDIA_BUS_FMT_SRGGB8_1X8:
> + return 8;
> + case MEDIA_BUS_FMT_SBGGR10_1X10:
> + case MEDIA_BUS_FMT_SGBRG10_1X10:
> + case MEDIA_BUS_FMT_SGRBG10_1X10:
> + case MEDIA_BUS_FMT_SRGGB10_1X10:
> + return 10;
> + case MEDIA_BUS_FMT_SBGGR12_1X12:
> + case MEDIA_BUS_FMT_SGBRG12_1X12:
> + case MEDIA_BUS_FMT_SGRBG12_1X12:
> + case MEDIA_BUS_FMT_SRGGB12_1X12:
> + return 12;
> + case MEDIA_BUS_FMT_UYVY8_2X8:

This is good for the parallel bus, but on CSI-2 side you should have
MEDIA_BUS_FMT_UYVY8_1X16 instead. This isn't technically correct for a
serial bus, but the custom is to use the one sample / pixel formats on the
serial busses.

> + return 16;
> + case MEDIA_BUS_FMT_BGR888_1X24:
> + return 24;
> + default:
> + return 0;
> + }
> +}
> +
> +static u8 data_type_from_code(__u32 code)
> +{
> + switch (code) {
> + case MEDIA_BUS_FMT_SBGGR8_1X8:
> + case MEDIA_BUS_FMT_SGBRG8_1X8:
> + case MEDIA_BUS_FMT_SGRBG8_1X8:
> + case MEDIA_BUS_FMT_SRGGB8_1X8:
> + return 0x2a;
> + case MEDIA_BUS_FMT_SBGGR10_1X10:
> + case MEDIA_BUS_FMT_SGBRG10_1X10:
> + case MEDIA_BUS_FMT_SGRBG10_1X10:
> + case MEDIA_BUS_FMT_SRGGB10_1X10:
> + return 0x2b;
> + case MEDIA_BUS_FMT_SBGGR12_1X12:
> + case MEDIA_BUS_FMT_SGBRG12_1X12:
> + case MEDIA_BUS_FMT_SGRBG12_1X12:
> + case MEDIA_BUS_FMT_SRGGB12_1X12:
> + return 0x2c;
> + case MEDIA_BUS_FMT_UYVY8_2X8:
> + return 0x1e;
> + case MEDIA_BUS_FMT_BGR888_1X24:
> + return 0x24;
> + default:
> + return 0;
> + }
> +}
> +
> +static __u32 get_fmt_code(__u32 code)
> +{
> + int i;

unsigned int

> +
> + for (i = 0; i < ARRAY_SIZE(mipid02_supported_fmt_codes); i++) {
> + if (code == mipid02_supported_fmt_codes[i])
> + return code;
> + }
> +
> + return mipid02_supported_fmt_codes[0];
> +}
> +
> +static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
> +{
> + return container_of(sd, struct mipid02_dev, sd);
> +}
> +
> +static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct i2c_msg msg[2];
> + u8 buf[2];
> + int ret;
> +
> + buf[0] = reg >> 8;
> + buf[1] = reg & 0xff;
> +
> + msg[0].addr = client->addr;
> + msg[0].flags = client->flags;
> + msg[0].buf = buf;
> + msg[0].len = sizeof(buf);
> +
> + msg[1].addr = client->addr;
> + msg[1].flags = client->flags | I2C_M_RD;
> + msg[1].buf = val;
> + msg[1].len = 1;
> +
> + ret = i2c_transfer(client->adapter, msg, 2);
> + if (ret < 0) {
> + dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
> + __func__, client->addr, reg, ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct i2c_msg msg;
> + u8 buf[3];
> + int ret;
> +
> + buf[0] = reg >> 8;
> + buf[1] = reg & 0xff;
> + buf[2] = val;
> +
> + msg.addr = client->addr;
> + msg.flags = client->flags;
> + msg.buf = buf;
> + msg.len = sizeof(buf);
> +
> + ret = i2c_transfer(client->adapter, &msg, 1);
> + if (ret < 0) {
> + dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
> + __func__, reg, ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int mipid02_get_regulators(struct mipid02_dev *bridge)
> +{
> + unsigned int i;
> +
> + for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
> + bridge->supplies[i].supply = mipid02_supply_name[i];
> +
> + return devm_regulator_bulk_get(&bridge->i2c_client->dev,
> + MIPID02_NUM_SUPPLIES,
> + bridge->supplies);
> +}
> +
> +static void mipid02_apply_reset(struct mipid02_dev *bridge)
> +{
> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
> + usleep_range(5000, 10000);
> + gpiod_set_value_cansleep(bridge->reset_gpio, 1);
> + usleep_range(5000, 10000);
> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
> + usleep_range(5000, 10000);
> +}
> +
> +static int mipid02_set_power_on(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + int ret;
> +
> + ret = clk_prepare_enable(bridge->xclk);
> + if (ret) {
> + dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
> + return ret;
> + }
> +
> + ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
> + bridge->supplies);
> + if (ret) {
> + dev_err(&client->dev, "%s: failed to enable regulators\n",
> + __func__);
> + goto xclk_off;
> + }
> +
> + if (bridge->reset_gpio) {
> + dev_dbg(&client->dev, "apply reset");
> + mipid02_apply_reset(bridge);
> + } else {
> + dev_dbg(&client->dev, "dont apply reset");

"don't"

> + usleep_range(5000, 10000);
> + }
> +
> + return 0;
> +
> +xclk_off:
> + clk_disable_unprepare(bridge->xclk);
> + return ret;
> +}
> +
> +static void mipid02_set_power_off(struct mipid02_dev *bridge)
> +{
> + regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
> + clk_disable_unprepare(bridge->xclk);
> +}
> +
> +static int mipid02_detect(struct mipid02_dev *bridge)
> +{
> + u8 reg;
> +
> + /*
> + * There is no version registers. Just try to read register
> + * MIPID02_CLK_LANE_WR_REG1.
> + */
> + return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
> +}
> +
> +static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> +
> + if (ep->nr_of_link_frequencies != 1)
> + return 0;
> +
> + return ep->link_frequencies[0];
> +}
> +
> +static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
> + struct v4l2_subdev *subdev)
> +{
> + struct v4l2_ctrl *ctrl;
> +
> + ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_LINK_FREQ);
> + if (!ctrl)
> + return 0;
> +
> + return v4l2_ctrl_g_ctrl_int64(ctrl);

Note that the link frequency control is typically a menu. If you do this,
you get the number of the menu item which is likely not what you want.

> +}
> +
> +static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
> + struct v4l2_subdev *subdev)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + struct v4l2_ctrl *ctrl;
> + u32 pixel_clock;
> + u32 bpp = bpp_from_code(bridge->fmt.code);
> +
> + ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
> + if (!ctrl)
> + return 0;
> + pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
> +
> + return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
> +}
> +
> +/*
> + * We need to know link frequency to setup clk_lane_reg1 timings. So use
> + * following sequence, stop when we got a frequency.
> + * - Get it from device tree. It allow to connect subdev without support of
> + * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
> + * is that is that link frequency must be constant.
> + * - Get it from V4L2_CID_LINK_FREQ.
> + * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
> + * to compute it.
> + */
> +static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_subdev *subdev = bridge->s_subdev;
> + u32 link_freq;
> +
> + link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
> + if (link_freq)
> + goto success;
> +
> + link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, subdev);
> + if (link_freq)
> + goto success;
> +
> + link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, subdev);
> + if (link_freq)
> + goto success;
> + dev_err(&client->dev, "Failed to detect link frequency");
> +
> + return -EINVAL;
> +
> +success:
> + dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
> + bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
> +
> + /* midid02 doesn't support clock lane remapping */
> + if (ep->bus.mipi_csi2.clock_lane != 0) {
> + dev_err(&client->dev, "clk lane must be map to lane 0\n");
> + return -EINVAL;
> + }
> + bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
> + bool are_lanes_swap, bool *polarities)
> +{
> + bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
> +
> + if (nb == 1 && are_lanes_swap)
> + return 0;
> +
> + /*
> + * data lane 0 as pin swap polarity reversed compared to clock and
> + *data lane 1

s/\*/\* /

> + */
> + if (!are_pin_swap)
> + bridge->r.data_lane0_reg1 = 1 << 1;
> + bridge->r.data_lane0_reg1 |= DATA_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
> + bool are_lanes_swap, bool *polarities)
> +{
> + bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
> +
> + if (nb == 1 && !are_lanes_swap)
> + return 0;
> +
> + if (are_pin_swap)
> + bridge->r.data_lane1_reg1 = 1 << 1;
> + bridge->r.data_lane1_reg1 |= DATA_ENABLE;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_from_rx(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
> + bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
> + int nb = ep->bus.mipi_csi2.num_data_lanes;
> + int ret;
> +
> + ret = mipid02_configure_clk_lane(bridge);
> + if (ret)
> + return ret;
> +
> + ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
> + polarities);
> + if (ret)
> + return ret;
> +
> + ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
> + polarities);
> + if (ret)
> + return ret;
> +
> + bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
> + bridge->r.mode_reg1 |= (nb - 1) << 1;
> +
> + return mipid02_configure_from_rx_speed(bridge);
> +}
> +
> +static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint *ep = &bridge->tx;
> +
> + bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
> + bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
> + if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
> + bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
> + if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
> + bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
> +
> + return 0;
> +}
> +
> +static int mipid02_configure_from_code(struct mipid02_dev *bridge)
> +{
> + u8 data_type;
> +
> + bridge->r.data_id_rreg = 0;
> + data_type = data_type_from_code(bridge->fmt.code);
> + if (!data_type)
> + return -EINVAL;
> + bridge->r.data_id_rreg = data_type;
> +
> + return 0;
> +}
> +
> +static int mipid02_stream_disable(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + int ret;
> +
> + /* Disable all lanes */
> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
> + if (ret)
> + goto error;
> +error:
> + if (ret)
> + dev_err(&client->dev, "failed to stream off %d", ret);
> +
> + return ret;
> +}
> +
> +static int mipid02_stream_enable(struct mipid02_dev *bridge)
> +{
> + struct i2c_client *client = bridge->i2c_client;
> + int ret = -EINVAL;
> +
> + if (!bridge->s_subdev)
> + goto error;
> +
> + memset(&bridge->r, 0, sizeof(bridge->r));
> + /* build registers content */
> + ret = mipid02_configure_from_rx(bridge);
> + if (ret)
> + goto error;
> + ret = mipid02_configure_from_tx(bridge);
> + if (ret)
> + goto error;
> + ret = mipid02_configure_from_code(bridge);
> + if (ret)
> + goto error;
> +
> + /* write mipi registers */
> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
> + bridge->r.clk_lane_reg1);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
> + bridge->r.data_lane0_reg1);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
> + DATA_MIPI_CSI);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
> + bridge->r.data_lane1_reg1);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
> + DATA_MIPI_CSI);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_MODE_REG1,
> + MODE_NO_BYPASS | bridge->r.mode_reg1);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_MODE_REG2,
> + bridge->r.mode_reg2);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
> + bridge->r.data_id_rreg);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
> + bridge->r.pix_width_ctrl);
> + if (ret)
> + goto error;
> + ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
> + bridge->r.pix_width_ctrl_emb);
> + if (ret)
> + goto error;
> +
> + return 0;
> +
> +error:
> + dev_err(&client->dev, "failed to stream on %d", ret);
> + mipid02_stream_disable(bridge);
> +
> + return ret;
> +}
> +
> +static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
> +{
> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> + struct i2c_client *client = bridge->i2c_client;
> + int ret = 0;
> +
> + dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
> + enable, bridge->streaming);
> + mutex_lock(&bridge->lock);
> +
> + if (bridge->streaming == enable)
> + goto out;
> +
> + ret = enable ? mipid02_stream_enable(bridge) :
> + mipid02_stream_disable(bridge);
> + if (!ret)
> + bridge->streaming = enable;
> +
> +out:
> + dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
> + bridge->streaming, ret);
> + mutex_unlock(&bridge->lock);
> +
> + return ret;
> +}
> +
> +static int mipid02_get_fmt(struct v4l2_subdev *sd,
> + struct v4l2_subdev_pad_config *cfg,
> + struct v4l2_subdev_format *format)
> +{
> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_mbus_framefmt *fmt;
> +
> + dev_dbg(&client->dev, "%s probe %d", __func__, format->pad);
> +
> + if (format->pad >= MIPID02_PAD_NB)
> + return -EINVAL;
> + /* second CSI-2 pad not yet supported */
> + if (format->pad == 1)
> + return -EINVAL;
> +
> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
> + fmt = v4l2_subdev_get_try_format(&bridge->sd, cfg, format->pad);
> + else
> + fmt = &bridge->fmt;
> +
> + mutex_lock(&bridge->lock);
> +
> + format->format = *fmt;
> +
> + mutex_unlock(&bridge->lock);
> +
> + return 0;
> +}
> +
> +static int mipid02_set_fmt(struct v4l2_subdev *sd,
> + struct v4l2_subdev_pad_config *cfg,
> + struct v4l2_subdev_format *format)
> +{
> + struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> + struct i2c_client *client = bridge->i2c_client;
> + struct v4l2_mbus_framefmt *fmt;
> + int ret;
> +
> + dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
> +
> + if (format->pad >= MIPID02_PAD_NB)
> + return -EINVAL;
> + /* second CSI-2 pad not yet supported */
> + if (format->pad == 1)
> + return -EINVAL;
> +
> + mutex_lock(&bridge->lock);
> +
> + if (bridge->streaming) {
> + ret = -EBUSY;
> + goto error;
> + }
> +
> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
> + fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
> + else
> + fmt = &bridge->fmt;
> +
> + *fmt = *mbus_fmt;

What are the limits of the hardware regarding the size of the image? Aren't
there any?

The format on the sink pad needs to be propagated to the source pad as
well. I presume there's nothing to set on the sink pad for this device, is
there?

Please see:

<URL:https://hverkuil.home.xs4all.nl/spec/uapi/v4l/dev-subdev.html#order-of-configuration-and-format-propagation>

> + fmt->code = get_fmt_code(mbus_fmt->code);
> + fmt->field = V4L2_FIELD_NONE;
> + fmt->colorspace = V4L2_COLORSPACE_SRGB;
> + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
> + fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
> + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
> +
> + ret = 0;
> +
> +error:
> + mutex_unlock(&bridge->lock);
> +
> + return ret;
> +}
> +
> +static const struct v4l2_subdev_video_ops mipid02_video_ops = {
> + .s_stream = mipid02_s_stream,
> +};
> +
> +static const struct v4l2_subdev_pad_ops mipid02_pad_ops = {
> + .get_fmt = mipid02_get_fmt,
> + .set_fmt = mipid02_set_fmt,
> +};
> +
> +static const struct v4l2_subdev_ops mipid02_subdev_ops = {
> + .video = &mipid02_video_ops,
> + .pad = &mipid02_pad_ops,
> +};
> +
> +static const struct media_entity_operations mipid02_subdev_entity_ops = {
> + .link_validate = v4l2_subdev_link_validate,
> +};
> +
> +static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
> + struct v4l2_subdev *s_subdev,
> + struct v4l2_async_subdev *asd)
> +{
> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
> + struct i2c_client *client = bridge->i2c_client;
> + int source_pad;
> + int ret;
> +
> + dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
> +
> + source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
> + s_subdev->fwnode,
> + MEDIA_PAD_FL_SOURCE);
> + if (source_pad < 0) {
> + dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
> + s_subdev->name);
> + return source_pad;
> + }
> +
> + ret = media_create_pad_link(&s_subdev->entity, source_pad,
> + &bridge->sd.entity, 0,
> + MEDIA_LNK_FL_ENABLED |
> + MEDIA_LNK_FL_IMMUTABLE);
> + if (ret) {
> + dev_err(&client->dev, "Couldn't create media link %d", ret);
> + return ret;
> + }
> +
> + bridge->s_subdev = s_subdev;
> +
> + return 0;
> +}
> +
> +static void mipid02_async_unbind(struct v4l2_async_notifier *notifier,
> + struct v4l2_subdev *s_subdev,
> + struct v4l2_async_subdev *asd)
> +{
> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
> +
> + bridge->s_subdev = NULL;
> +}
> +
> +static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
> + .bound = mipid02_async_bound,
> + .unbind = mipid02_async_unbind,
> +};
> +
> +static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_CSI2_DPHY };
> + struct i2c_client *client = bridge->i2c_client;
> + struct device_node *ep_node;
> + int ret;
> +
> + /* parse rx (endpoint 0) */
> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
> + 0, 0);
> + if (!ep_node) {
> + dev_err(&client->dev, "unable to find port0 ep");
> + ret = -EINVAL;
> + goto error;
> + }
> +
> + ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
> + if (ret) {
> + dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
> + ret);
> + goto error_of_node_put;
> + }
> +
> + /* do some sanity checks */
> + if (ep.bus.mipi_csi2.num_data_lanes > 2) {
> + dev_err(&client->dev, "max supported data lanes is 2 / got %d",
> + ep.bus.mipi_csi2.num_data_lanes);
> + ret = -EINVAL;
> + goto error_v4l2_fwnode_endpoint_free;
> + }
> +
> + /* register it for later use */
> + bridge->rx = ep;
> + bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
> + &bridge->link_frequency : NULL;
> + v4l2_fwnode_endpoint_free(&ep);
> +
> + /* register async notifier so we get noticed when sensor is connected */
> + bridge->asd.match.fwnode =
> + fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
> + bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
> + of_node_put(ep_node);
> +
> + v4l2_async_notifier_init(&bridge->notifier);
> + ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
> + if (ret) {
> + dev_err(&client->dev, "fail to register asd to notifier %d",
> + ret);
> + goto error_fwnode_handle_put;
> + }
> + bridge->notifier.ops = &mipid02_notifier_ops;
> +
> + ret = v4l2_async_subdev_notifier_register(&bridge->sd,
> + &bridge->notifier);
> + if (ret)
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> +
> + return ret;
> +
> +error_v4l2_fwnode_endpoint_free:
> + v4l2_fwnode_endpoint_free(&ep);
> +error_of_node_put:
> + of_node_put(ep_node);
> +error:
> +
> + return ret;
> +
> +error_fwnode_handle_put:
> + fwnode_handle_put(bridge->asd.match.fwnode);
> +
> + return ret;
> +}
> +
> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
> +{
> + struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_PARALLEL };
> + struct i2c_client *client = bridge->i2c_client;
> + struct device_node *ep_node;
> + int ret;
> +
> + /* parse tx (endpoint 2) */
> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
> + 2, 0);
> + if (!ep_node) {
> + dev_err(&client->dev, "unable to find port1 ep");
> + ret = -EINVAL;
> + goto error;
> + }
> +
> + ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
> + if (ret) {
> + dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
> + goto error_of_node_put;
> + }
> +
> + of_node_put(ep_node);
> + bridge->tx = ep;
> +
> + return 0;
> +
> +error_of_node_put:
> + of_node_put(ep_node);
> +error:
> +
> + return -EINVAL;
> +}
> +
> +static int mipid02_probe(struct i2c_client *client,
> + const struct i2c_device_id *id)
> +{
> + struct device *dev = &client->dev;
> + struct mipid02_dev *bridge;
> + u32 clk_freq;
> + int ret;
> +
> + bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
> + if (!bridge)
> + return -ENOMEM;
> +
> + bridge->fmt.code = MEDIA_BUS_FMT_SBGGR8_1X8;
> + bridge->fmt.field = V4L2_FIELD_NONE;
> + bridge->fmt.colorspace = V4L2_COLORSPACE_SRGB;
> + bridge->fmt.ycbcr_enc =
> + V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
> + bridge->fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
> + bridge->fmt.xfer_func =
> + V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
> + bridge->fmt.width = 640;
> + bridge->fmt.height = 480;
> +
> + bridge->i2c_client = client;
> + v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
> +
> + /* got and check clock */
> + bridge->xclk = devm_clk_get(dev, "xclk");
> + if (IS_ERR(bridge->xclk)) {
> + dev_err(dev, "failed to get xclk\n");
> + return PTR_ERR(bridge->xclk);
> + }
> +
> + clk_freq = clk_get_rate(bridge->xclk);
> + if (clk_freq < 6000000 || clk_freq > 27000000) {
> + dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
> + clk_freq);
> + return -EINVAL;
> + }
> +
> + bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
> + GPIOD_OUT_HIGH);
> +
> + ret = mipid02_get_regulators(bridge);
> + if (ret) {
> + dev_err(dev, "failed to get regulators %d", ret);
> + return ret;
> + }
> +
> + mutex_init(&bridge->lock);
> + bridge->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
> + bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
> + bridge->sd.entity.ops = &mipid02_subdev_entity_ops;
> + bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
> + bridge->pad[1].flags = MEDIA_PAD_FL_SINK;
> + bridge->pad[2].flags = MEDIA_PAD_FL_SOURCE;
> + ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
> + bridge->pad);
> + if (ret) {
> + dev_err(&client->dev, "pads init failed %d", ret);
> + return ret;
> + }
> +
> + /* enable clock, power and reset device if available */
> + ret = mipid02_set_power_on(bridge);
> + if (ret)
> + goto entity_cleanup;
> +
> + ret = mipid02_detect(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to detect mipid02 %d", ret);
> + goto power_off;
> + }
> +
> + ret = mipid02_parse_tx_ep(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to parse tx %d", ret);
> + goto power_off;
> + }
> +
> + ret = mipid02_parse_rx_ep(bridge);
> + if (ret) {
> + dev_err(&client->dev, "failed to parse rx %d", ret);
> + goto power_off;
> + }
> +
> + ret = v4l2_async_register_subdev(&bridge->sd);
> + if (ret < 0) {
> + dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
> + ret);
> + goto unregister_notifier;
> + }
> +
> + dev_info(&client->dev, "mipid02 device probe successfully");
> +
> + return 0;
> +
> +unregister_notifier:
> + v4l2_async_notifier_unregister(&bridge->notifier);
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> +power_off:
> + mipid02_set_power_off(bridge);
> +entity_cleanup:
> + media_entity_cleanup(&bridge->sd.entity);
> +
> + return ret;
> +}
> +
> +static int mipid02_remove(struct i2c_client *client)
> +{
> + struct v4l2_subdev *sd = i2c_get_clientdata(client);
> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> +
> + v4l2_async_notifier_unregister(&bridge->notifier);
> + v4l2_async_notifier_cleanup(&bridge->notifier);
> + v4l2_async_unregister_subdev(&bridge->sd);
> + mipid02_set_power_off(bridge);
> + media_entity_cleanup(&bridge->sd.entity);
> +
> + return 0;
> +}
> +
> +static const struct i2c_device_id mipid02_id[] = {
> + { "st-mipid02", 0},
> + {},
> +};
> +MODULE_DEVICE_TABLE(i2c, mipid02_id);
> +
> +static const struct of_device_id mipid02_dt_ids[] = {
> + { .compatible = "st,st-mipid02" },
> + { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
> +
> +static struct i2c_driver mipid02_i2c_driver = {
> + .driver = {
> + .name = "st-mipid02",
> + .of_match_table = mipid02_dt_ids,
> + },
> + .id_table = mipid02_id,
> + .probe = mipid02_probe,
> + .remove = mipid02_remove,
> +};
> +
> +module_i2c_driver(mipid02_i2c_driver);
> +
> +MODULE_AUTHOR("Mickael Guene <[email protected]>");
> +MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
> +MODULE_LICENSE("GPL v2");

--
Kind regards,

Sakari Ailus
[email protected]

2019-03-26 11:35:35

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Mickael,

On Mon, Mar 25, 2019 at 12:22:17PM +0000, Mickael GUENE wrote:
...
> >> + /* register it for later use */
> >> + bridge->rx = ep;
> >> + bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
> >> + &bridge->link_frequency : NULL;
> >
> > I think you need to simply ignore the link frequencies here. The
> > transmitting device can tell the frequency based on its configuration
> > (based on the link frequencies). You seem to have implemented that already.
> >
> Idea of this was to allow some support for sensor that doesn't implement
> V4L2_CID_PIXEL_RATE. Do you think it's useless ?

Sensor drivers need to be amended with support for that control.

--
Sakari Ailus
[email protected]

2019-03-26 11:39:24

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v1 2/3] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

On Mon, Mar 25, 2019 at 12:14:05PM +0000, Mickael GUENE wrote:
> Hi Sakari,
>
> On 3/25/19 12:17, Sakari Ailus wrote:
> > Hi Mickael,
> >
> > On Mon, Mar 18, 2019 at 09:57:44AM +0000, Mickael GUENE wrote:
> >> Hi Sakari,
> >>
> >> Thanks for your review. Find my comments below.
> >>
> >> On 3/16/19 23:14, Sakari Ailus wrote:
> > ...
> >>>> +static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
> >>>> +{
> >>>> + struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
> >>>> + struct media_entity *entity;
> >>>> +
> >>>> + if (!mdev)
> >>>> + return NULL;
> >>>> +
> >>>> + media_device_for_each_entity(entity, mdev)
> >>>> + if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
> >>>> + return media_entity_to_v4l2_subdev(entity);
> >>>
> >>> Hmm. Could you instead use the link state to determine which of the
> >>> receivers is active? You'll need one more pad, and then you'd had 1:1
> >>> mapping between ports and pads.
> >>>
> >> Goal here is not to detect which of the receivers is active but to find
> >> sensor in case there are others sub-dev in chain (for example a
> >> serializer/deserializer as found in cars).
> >
> > You shouldn't make assumptions on the rest of the pipeline beyond the
> > device that's directly connected. You might not even have a camera there.
> >
> I have also seen your answer to '[PATCH v2 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver'
> concerning support of set_fmt, get_fmt and link_validate.
> My initial idea was to avoid to avoid to implement them and to avoid media ctrl configuration. According
> to your remark is seems a bad idea. Right ?

Yes, you'll need them. This is how the media controller pipeline works: a
driver for a given device is generally only aware of its direct links and
generally only communicate with drivers for devices directly connected to
them.

> In that case I have to also implement enum_mbus_code ?

Yes, please.

> I will drop this code and use connected device only to get link speed.

Ack.

--
Sakari Ailus
[email protected]

2019-03-26 12:10:06

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v1 2/3] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hello Sakari,

On 3/26/19 12:37, Sakari Ailus wrote:
> On Mon, Mar 25, 2019 at 12:14:05PM +0000, Mickael GUENE wrote:
>> Hi Sakari,
>>
>> On 3/25/19 12:17, Sakari Ailus wrote:
>>> Hi Mickael,
>>>
>>> On Mon, Mar 18, 2019 at 09:57:44AM +0000, Mickael GUENE wrote:
>>>> Hi Sakari,
>>>>
>>>> Thanks for your review. Find my comments below.
>>>>
>>>> On 3/16/19 23:14, Sakari Ailus wrote:
>>> ...
>>>>>> +static struct v4l2_subdev *mipid02_find_sensor(struct mipid02_dev *bridge)
>>>>>> +{
>>>>>> + struct media_device *mdev = bridge->sd.v4l2_dev->mdev;
>>>>>> + struct media_entity *entity;
>>>>>> +
>>>>>> + if (!mdev)
>>>>>> + return NULL;
>>>>>> +
>>>>>> + media_device_for_each_entity(entity, mdev)
>>>>>> + if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
>>>>>> + return media_entity_to_v4l2_subdev(entity);
>>>>>
>>>>> Hmm. Could you instead use the link state to determine which of the
>>>>> receivers is active? You'll need one more pad, and then you'd had 1:1
>>>>> mapping between ports and pads.
>>>>>
>>>> Goal here is not to detect which of the receivers is active but to find
>>>> sensor in case there are others sub-dev in chain (for example a
>>>> serializer/deserializer as found in cars).
>>>
>>> You shouldn't make assumptions on the rest of the pipeline beyond the
>>> device that's directly connected. You might not even have a camera there.
>>>
>> I have also seen your answer to '[PATCH v2 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver'
>> concerning support of set_fmt, get_fmt and link_validate.
>> My initial idea was to avoid to avoid to implement them and to avoid media ctrl configuration. According
>> to your remark is seems a bad idea. Right ?
>
> Yes, you'll need them. This is how the media controller pipeline works: a
> driver for a given device is generally only aware of its direct links and
> generally only communicate with drivers for devices directly connected to
> them.
>
>> In that case I have to also implement enum_mbus_code ?
>
> Yes, please.
>
Ok I will add it in v4
>> I will drop this code and use connected device only to get link speed.
>
> Ack.
>

2019-03-26 12:18:35

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] dt-bindings: Document MIPID02 bindings

Hi Mickael,

On Tue, Mar 26, 2019 at 11:03:39AM +0100, Mickael Guene wrote:
> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
> bridge.
>
> Signed-off-by: Mickael Guene <[email protected]>
> ---
>
> Changes in v3: None
> Changes in v2:
> - Add precision about first CSI-2 port data rate
> - Document endpoints supported properties
> - Rename 'mipid02@14' into generic 'csi2rx@14' in example
>
> .../bindings/media/i2c/st,st-mipid02.txt | 83 ++++++++++++++++++++++
> MAINTAINERS | 7 ++
> 2 files changed, 90 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>
> diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> new file mode 100644
> index 0000000..dfeab45
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> @@ -0,0 +1,83 @@
> +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
> +
> +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
> +time. Active port input stream will be de-serialized and its content outputted
> +through PARALLEL output port.
> +CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
> +input port is a single lane 800Mbps. Both ports support clock and data lane
> +polarity swap. First port also supports data lane swap.
> +PARALLEL output port has a maximum width of 12 bits.
> +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
> +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
> +
> +Required Properties:
> +- compatible: should be "st,st-mipid02"
> +- clocks: reference to the xclk input clock.
> +- clock-names: should be "xclk".
> +- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
> +- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
> +
> +Optional Properties:
> +- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
> + This is an active low signal to the mipid02.
> +
> +Required subnodes:
> + - ports: A ports node with one port child node per device input and output
> + port, in accordance with the video interface bindings defined in
> + Documentation/devicetree/bindings/media/video-interfaces.txt. The
> + port nodes are numbered as follows:
> +
> + Port Description
> + -----------------------------
> + 0 CSI-2 first input port
> + 1 CSI-2 second input port
> + 2 PARALLEL output
> +
> +Endpoint node optional properties for CSI-2 connection are:
> +- bus-type: if present should be 4 - MIPI CSI-2 D-PHY.

You can drop this IMO --- there's just a single valid value so the driver
may know that.

> +- clock-lanes: should be set to <0> if present (clock lane on hardware lane 0).

And please omit this, too, if the clock lane is always 0. Please update the
example, too. The driver doesn't need to check that either IMO, but up to
you.

> +- data-lanes: if present should be <1> for Port 1. for Port 0 dual-lane
> +operation should be <1 2> or <2 1>. For Port 0 single-lane operation should be
> +<1> or <2>.
> +- lane-polarities: any lane can be inverted.
> +
> +Endpoint node optional properties for PARALLEL connection are:
> +- bus-type: if present should be 5 - Parallel.

This, too, can be omitted.

> +- bus-width: shall be set to <6>, <7>, <8>, <10> or <12>.
> +- hsync-active: active state of the HSYNC signal, 0/1 for LOW/HIGH respectively.
> +- vsync-active: active state of the VSYNC signal, 0/1 for LOW/HIGH respectively.

If these are optional, what are the defaults? IMO you could make them
mandatory as well.

> +
> +Example:
> +
> +mipid02: csi2rx@14 {
> + compatible = "st,st-mipid02";
> + reg = <0x14>;
> + status = "okay";
> + clocks = <&clk_ext_camera_12>;
> + clock-names = "xclk";
> + VDDE-supply = <&vdd>;
> + VDDIN-supply = <&vdd>;
> + ports {
> + #address-cells = <1>;
> + #size-cells = <0>;
> + port@0 {
> + reg = <0>;
> +
> + ep0: endpoint {
> + clock-lanes = <0>;
> + data-lanes = <1 2>;
> + remote-endpoint = <&mipi_csi2_in>;
> + };
> + };
> + port@2 {
> + reg = <2>;
> +
> + ep2: endpoint {
> + bus-width = <8>;
> + hsync-active = <0>;
> + vsync-active = <0>;
> + remote-endpoint = <&parallel_out>;
> + };
> + };
> + };
> +};
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e17ebf7..74da99d 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -14668,6 +14668,13 @@ S: Maintained
> F: drivers/iio/imu/st_lsm6dsx/
> F: Documentation/devicetree/bindings/iio/imu/st_lsm6dsx.txt
>
> +ST MIPID02 CSI-2 TO PARALLEL BRIDGE DRIVER
> +M: Mickael Guene <[email protected]>
> +L: [email protected]
> +T: git git://linuxtv.org/media_tree.git
> +S: Maintained
> +F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> +
> ST STM32 I2C/SMBUS DRIVER
> M: Pierre-Yves MORDRET <[email protected]>
> L: [email protected]

--
Kind regards,

Sakari Ailus

2019-03-26 12:35:10

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Sakari,

On 3/26/19 12:34, Sakari Ailus wrote:
> Hi Mickael,
>
> On Mon, Mar 25, 2019 at 12:22:17PM +0000, Mickael GUENE wrote:
> ...
>>>> + /* register it for later use */
>>>> + bridge->rx = ep;
>>>> + bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
>>>> + &bridge->link_frequency : NULL;
>>>
>>> I think you need to simply ignore the link frequencies here. The
>>> transmitting device can tell the frequency based on its configuration
>>> (based on the link frequencies). You seem to have implemented that already.
>>>
>> Idea of this was to allow some support for sensor that doesn't implement
>> V4L2_CID_PIXEL_RATE. Do you think it's useless ?
>
> Sensor drivers need to be amended with support for that control.
>
Ok. I will drop mipid02_get_link_freq_from_rx_ep and mipid02_get_link_freq_from_cid_link_freq
and only use V4L2_CID_PIXEL_RATE to compute link speed.

2019-03-26 12:59:45

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Sakari,

Thanks again for your review.

On 3/26/19 12:33, Sakari Ailus wrote:
> Hi Mickael,
>
> Thanks for the update.
>
> On Tue, Mar 26, 2019 at 11:03:40AM +0100, Mickael Guene wrote:
>> This V4L2 subdev driver enables STMicroelectronics MIPID02 device.
>>
>> Signed-off-by: Mickael Guene <[email protected]>
>> ---
>>
>> Changes in v3:
>> - Fix potential wrong error code for mipid02_stream_disable and mipid02_stream_enable
>> - Remove useless memset for ep in mipid02_parse_rx_ep and mipid02_parse_tx_ep
>> - Add second CSI-2 input pad even if it's not yet supported
>> - Add support of get_fmt, set_fmt and link_validate and only access subdev connected to mipid02
>>
>> Changes in v2:
>> - Merge MAINTAINERS patch 3 into patch 1 and 2
>> - Fix line too long in Kconfig
>> - Add missing delay after reset release
>> - Various style fixes
>> - Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed
>>
>> MAINTAINERS | 1 +
>> drivers/media/i2c/Kconfig | 14 +
>> drivers/media/i2c/Makefile | 1 +
>> drivers/media/i2c/st-mipid02.c | 1012 ++++++++++++++++++++++++++++++++++++++++
>> 4 files changed, 1028 insertions(+)
>> create mode 100644 drivers/media/i2c/st-mipid02.c
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 74da99d..a14fe81 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -14673,6 +14673,7 @@ M: Mickael Guene <[email protected]>
>> L: [email protected]
>> T: git git://linuxtv.org/media_tree.git
>> S: Maintained
>> +F: drivers/media/i2c/st-mipid02.c
>> F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>>
>> ST STM32 I2C/SMBUS DRIVER
>> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
>> index 1ef6335..be2470b 100644
>> --- a/drivers/media/i2c/Kconfig
>> +++ b/drivers/media/i2c/Kconfig
>> @@ -1135,6 +1135,20 @@ config VIDEO_I2C
>> To compile this driver as a module, choose M here: the
>> module will be called video-i2c
>>
>> +config VIDEO_ST_MIPID02
>> + tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
>> + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
>> + depends on MEDIA_CAMERA_SUPPORT
>> + depends on MEDIA_CONTROLLER
>> + select V4L2_FWNODE
>> + help
>> + Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
>> + It is used to allow usage of CSI-2 sensor with PARALLEL port
>> + controller.
>> +
>> + To compile this driver as a module, choose M here: the
>> + module will be called st-mipid02.
>> +
>> endmenu
>>
>> endif
>> diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
>> index a64fca8..d8ad9da 100644
>> --- a/drivers/media/i2c/Makefile
>> +++ b/drivers/media/i2c/Makefile
>> @@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
>> obj-$(CONFIG_VIDEO_IMX274) += imx274.o
>> obj-$(CONFIG_VIDEO_IMX319) += imx319.o
>> obj-$(CONFIG_VIDEO_IMX355) += imx355.o
>> +obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o
>>
>> obj-$(CONFIG_SDR_MAX2175) += max2175.o
>> diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
>> new file mode 100644
>> index 0000000..5bb9865
>> --- /dev/null
>> +++ b/drivers/media/i2c/st-mipid02.c
>> @@ -0,0 +1,1012 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
>> + *
>> + * Copyright (C) STMicroelectronics SA 2019
>> + * Authors: Mickael Guene <[email protected]>
>> + * for STMicroelectronics.
>> + *
>> + *
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/delay.h>
>> +#include <linux/gpio/consumer.h>
>> +#include <linux/i2c.h>
>> +#include <linux/module.h>
>> +#include <linux/of_graph.h>
>> +#include <linux/regulator/consumer.h>
>> +#include <media/v4l2-async.h>
>> +#include <media/v4l2-ctrls.h>
>> +#include <media/v4l2-device.h>
>> +#include <media/v4l2-fwnode.h>
>> +#include <media/v4l2-subdev.h>
>> +
>> +#define MIPID02_CLK_LANE_WR_REG1 0x01
>> +#define MIPID02_CLK_LANE_REG1 0x02
>> +#define MIPID02_CLK_LANE_REG3 0x04
>> +#define MIPID02_DATA_LANE0_REG1 0x05
>> +#define MIPID02_DATA_LANE0_REG2 0x06
>> +#define MIPID02_DATA_LANE1_REG1 0x09
>> +#define MIPID02_DATA_LANE1_REG2 0x0a
>> +#define MIPID02_MODE_REG1 0x14
>> +#define MIPID02_MODE_REG2 0x15
>> +#define MIPID02_DATA_ID_RREG 0x17
>> +#define MIPID02_DATA_SELECTION_CTRL 0x19
>> +#define MIPID02_PIX_WIDTH_CTRL 0x1e
>> +#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
>> +
>> +/* Bits definition for MIPID02_CLK_LANE_REG1 */
>> +#define CLK_ENABLE BIT(0)
>> +/* Bits definition for MIPID02_CLK_LANE_REG3 */
>> +#define CLK_MIPI_CSI BIT(1)
>> +/* Bits definition for MIPID02_DATA_LANE0_REG1 */
>> +#define DATA_ENABLE BIT(0)
>> +/* Bits definition for MIPID02_DATA_LANEx_REG2 */
>> +#define DATA_MIPI_CSI BIT(0)
>> +/* Bits definition for MIPID02_MODE_REG1 */
>> +#define MODE_DATA_SWAP BIT(2)
>> +#define MODE_NO_BYPASS BIT(6)
>> +/* Bits definition for MIPID02_MODE_REG2 */
>> +#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
>> +#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
>> +/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
>> +#define SELECTION_MANUAL_DATA BIT(2)
>> +#define SELECTION_MANUAL_WIDTH BIT(3)
>> +
>> +static const u32 mipid02_supported_fmt_codes[] = {
>> + MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
>> + MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
>> + MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
>> + MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
>> + MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SGBRG12_1X12,
>> + MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SRGGB12_1X12,
>> + MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_BGR888_1X24
>> +};
>> +
>> +/* regulator supplies */
>> +static const char * const mipid02_supply_name[] = {
>> + "VDDE", /* 1.8V digital I/O supply */
>> + "VDDIN", /* 1V8 voltage regulator supply */
>> +};
>> +
>> +#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
>> +#define MIPID02_PAD_NB 3
>> +
>> +struct mipid02_dev {
>> + struct i2c_client *i2c_client;
>> + struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
>> + struct v4l2_subdev sd;
>> + struct media_pad pad[MIPID02_PAD_NB];
>> + struct clk *xclk;
>> + struct gpio_desc *reset_gpio;
>> + /* endpoints info */
>> + struct v4l2_fwnode_endpoint rx;
>> + u64 link_frequency;
>> + struct v4l2_fwnode_endpoint tx;
>> + /* remote source */
>> + struct v4l2_async_subdev asd;
>> + struct v4l2_async_notifier notifier;
>> + struct v4l2_subdev *s_subdev;
>> + /* registers */
>> + struct {
>> + u8 clk_lane_reg1;
>> + u8 data_lane0_reg1;
>> + u8 data_lane1_reg1;
>> + u8 mode_reg1;
>> + u8 mode_reg2;
>> + u8 data_id_rreg;
>> + u8 pix_width_ctrl;
>> + u8 pix_width_ctrl_emb;
>> + } r;
>> + /* lock to protect all members below */
>> + struct mutex lock;
>> + bool streaming;
>> + struct v4l2_mbus_framefmt fmt;
>> +};
>> +
>> +static int bpp_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 8;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 10;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 12;
>> + case MEDIA_BUS_FMT_UYVY8_2X8:
>
> This is good for the parallel bus, but on CSI-2 side you should have
> MEDIA_BUS_FMT_UYVY8_1X16 instead. This isn't technically correct for a
> serial bus, but the custom is to use the one sample / pixel formats on the
> serial busses.
>
ok
>> + return 16;
>> + case MEDIA_BUS_FMT_BGR888_1X24:
>> + return 24;
>> + default:
>> + return 0;
>> + }
>> +}
>> +
>> +static u8 data_type_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 0x2a;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 0x2b;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 0x2c;
>> + case MEDIA_BUS_FMT_UYVY8_2X8:
>> + return 0x1e;
>> + case MEDIA_BUS_FMT_BGR888_1X24:
>> + return 0x24;
>> + default:
>> + return 0;
>> + }
>> +}
>> +
>> +static __u32 get_fmt_code(__u32 code)
>> +{
>> + int i;
>
> unsigned int
>
ok
>> +
>> + for (i = 0; i < ARRAY_SIZE(mipid02_supported_fmt_codes); i++) {
>> + if (code == mipid02_supported_fmt_codes[i])
>> + return code;
>> + }
>> +
>> + return mipid02_supported_fmt_codes[0];
>> +}
>> +
>> +static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
>> +{
>> + return container_of(sd, struct mipid02_dev, sd);
>> +}
>> +
>> +static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct i2c_msg msg[2];
>> + u8 buf[2];
>> + int ret;
>> +
>> + buf[0] = reg >> 8;
>> + buf[1] = reg & 0xff;
>> +
>> + msg[0].addr = client->addr;
>> + msg[0].flags = client->flags;
>> + msg[0].buf = buf;
>> + msg[0].len = sizeof(buf);
>> +
>> + msg[1].addr = client->addr;
>> + msg[1].flags = client->flags | I2C_M_RD;
>> + msg[1].buf = val;
>> + msg[1].len = 1;
>> +
>> + ret = i2c_transfer(client->adapter, msg, 2);
>> + if (ret < 0) {
>> + dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
>> + __func__, client->addr, reg, ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct i2c_msg msg;
>> + u8 buf[3];
>> + int ret;
>> +
>> + buf[0] = reg >> 8;
>> + buf[1] = reg & 0xff;
>> + buf[2] = val;
>> +
>> + msg.addr = client->addr;
>> + msg.flags = client->flags;
>> + msg.buf = buf;
>> + msg.len = sizeof(buf);
>> +
>> + ret = i2c_transfer(client->adapter, &msg, 1);
>> + if (ret < 0) {
>> + dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
>> + __func__, reg, ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_get_regulators(struct mipid02_dev *bridge)
>> +{
>> + unsigned int i;
>> +
>> + for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
>> + bridge->supplies[i].supply = mipid02_supply_name[i];
>> +
>> + return devm_regulator_bulk_get(&bridge->i2c_client->dev,
>> + MIPID02_NUM_SUPPLIES,
>> + bridge->supplies);
>> +}
>> +
>> +static void mipid02_apply_reset(struct mipid02_dev *bridge)
>> +{
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
>> + usleep_range(5000, 10000);
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 1);
>> + usleep_range(5000, 10000);
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
>> + usleep_range(5000, 10000);
>> +}
>> +
>> +static int mipid02_set_power_on(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret;
>> +
>> + ret = clk_prepare_enable(bridge->xclk);
>> + if (ret) {
>> + dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
>> + return ret;
>> + }
>> +
>> + ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
>> + bridge->supplies);
>> + if (ret) {
>> + dev_err(&client->dev, "%s: failed to enable regulators\n",
>> + __func__);
>> + goto xclk_off;
>> + }
>> +
>> + if (bridge->reset_gpio) {
>> + dev_dbg(&client->dev, "apply reset");
>> + mipid02_apply_reset(bridge);
>> + } else {
>> + dev_dbg(&client->dev, "dont apply reset");
>
> "don't"
>
ok
>> + usleep_range(5000, 10000);
>> + }
>> +
>> + return 0;
>> +
>> +xclk_off:
>> + clk_disable_unprepare(bridge->xclk);
>> + return ret;
>> +}
>> +
>> +static void mipid02_set_power_off(struct mipid02_dev *bridge)
>> +{
>> + regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
>> + clk_disable_unprepare(bridge->xclk);
>> +}
>> +
>> +static int mipid02_detect(struct mipid02_dev *bridge)
>> +{
>> + u8 reg;
>> +
>> + /*
>> + * There is no version registers. Just try to read register
>> + * MIPID02_CLK_LANE_WR_REG1.
>> + */
>> + return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_rx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> +
>> + if (ep->nr_of_link_frequencies != 1)
>> + return 0;
>> +
>> + return ep->link_frequencies[0];
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge,
>> + struct v4l2_subdev *subdev)
>> +{
>> + struct v4l2_ctrl *ctrl;
>> +
>> + ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_LINK_FREQ);
>> + if (!ctrl)
>> + return 0;
>> +
>> + return v4l2_ctrl_g_ctrl_int64(ctrl);
>
> Note that the link frequency control is typically a menu. If you do this,
> you get the number of the menu item which is likely not what you want.
>
ok. I have drop it and only use V4L2_CID_PIXEL_RATE.
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
>> + struct v4l2_subdev *subdev)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + struct v4l2_ctrl *ctrl;
>> + u32 pixel_clock;
>> + u32 bpp = bpp_from_code(bridge->fmt.code);
>> +
>> + ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
>> + if (!ctrl)
>> + return 0;
>> + pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
>> +
>> + return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
>> +}
>> +
>> +/*
>> + * We need to know link frequency to setup clk_lane_reg1 timings. So use
>> + * following sequence, stop when we got a frequency.
>> + * - Get it from device tree. It allow to connect subdev without support of
>> + * V4L2_CID_LINK_FREQ or V4L2_CID_PIXEL_RATE or with a broken one. Drawback
>> + * is that is that link frequency must be constant.
>> + * - Get it from V4L2_CID_LINK_FREQ.
>> + * - Get it from V4L2_CID_PIXEL_RATE and use bit per pixel and number of lanes
>> + * to compute it.
>> + */
>> +static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_subdev *subdev = bridge->s_subdev;
>> + u32 link_freq;
>> +
>> + link_freq = mipid02_get_link_freq_from_rx_ep(bridge);
>> + if (link_freq)
>> + goto success;
>> +
>> + link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, subdev);
>> + if (link_freq)
>> + goto success;
>> +
>> + link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, subdev);
>> + if (link_freq)
>> + goto success;
>> + dev_err(&client->dev, "Failed to detect link frequency");
>> +
>> + return -EINVAL;
>> +
>> +success:
>> + dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
>> + bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
>> +
>> + /* midid02 doesn't support clock lane remapping */
>> + if (ep->bus.mipi_csi2.clock_lane != 0) {
>> + dev_err(&client->dev, "clk lane must be map to lane 0\n");
>> + return -EINVAL;
>> + }
>> + bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
>> + bool are_lanes_swap, bool *polarities)
>> +{
>> + bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
>> +
>> + if (nb == 1 && are_lanes_swap)
>> + return 0;
>> +
>> + /*
>> + * data lane 0 as pin swap polarity reversed compared to clock and
>> + *data lane 1
>
> s/\*/\* /
>
ok
>> + */
>> + if (!are_pin_swap)
>> + bridge->r.data_lane0_reg1 = 1 << 1;
>> + bridge->r.data_lane0_reg1 |= DATA_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
>> + bool are_lanes_swap, bool *polarities)
>> +{
>> + bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
>> +
>> + if (nb == 1 && !are_lanes_swap)
>> + return 0;
>> +
>> + if (are_pin_swap)
>> + bridge->r.data_lane1_reg1 = 1 << 1;
>> + bridge->r.data_lane1_reg1 |= DATA_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_from_rx(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
>> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
>> + int nb = ep->bus.mipi_csi2.num_data_lanes;
>> + int ret;
>> +
>> + ret = mipid02_configure_clk_lane(bridge);
>> + if (ret)
>> + return ret;
>> +
>> + ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
>> + polarities);
>> + if (ret)
>> + return ret;
>> +
>> + ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
>> + polarities);
>> + if (ret)
>> + return ret;
>> +
>> + bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
>> + bridge->r.mode_reg1 |= (nb - 1) << 1;
>> +
>> + return mipid02_configure_from_rx_speed(bridge);
>> +}
>> +
>> +static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->tx;
>> +
>> + bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
>> + bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
>> + if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
>> + bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
>> + if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
>> + bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_from_code(struct mipid02_dev *bridge)
>> +{
>> + u8 data_type;
>> +
>> + bridge->r.data_id_rreg = 0;
>> + data_type = data_type_from_code(bridge->fmt.code);
>> + if (!data_type)
>> + return -EINVAL;
>> + bridge->r.data_id_rreg = data_type;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_stream_disable(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret;
>> +
>> + /* Disable all lanes */
>> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
>> + if (ret)
>> + goto error;
>> +error:
>> + if (ret)
>> + dev_err(&client->dev, "failed to stream off %d", ret);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_stream_enable(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret = -EINVAL;
>> +
>> + if (!bridge->s_subdev)
>> + goto error;
>> +
>> + memset(&bridge->r, 0, sizeof(bridge->r));
>> + /* build registers content */
>> + ret = mipid02_configure_from_rx(bridge);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_configure_from_tx(bridge);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_configure_from_code(bridge);
>> + if (ret)
>> + goto error;
>> +
>> + /* write mipi registers */
>> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
>> + bridge->r.clk_lane_reg1);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
>> + bridge->r.data_lane0_reg1);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
>> + DATA_MIPI_CSI);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
>> + bridge->r.data_lane1_reg1);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
>> + DATA_MIPI_CSI);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_MODE_REG1,
>> + MODE_NO_BYPASS | bridge->r.mode_reg1);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_MODE_REG2,
>> + bridge->r.mode_reg2);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
>> + bridge->r.data_id_rreg);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
>> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
>> + bridge->r.pix_width_ctrl);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
>> + bridge->r.pix_width_ctrl_emb);
>> + if (ret)
>> + goto error;
>> +
>> + return 0;
>> +
>> +error:
>> + dev_err(&client->dev, "failed to stream on %d", ret);
>> + mipid02_stream_disable(bridge);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret = 0;
>> +
>> + dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
>> + enable, bridge->streaming);
>> + mutex_lock(&bridge->lock);
>> +
>> + if (bridge->streaming == enable)
>> + goto out;
>> +
>> + ret = enable ? mipid02_stream_enable(bridge) :
>> + mipid02_stream_disable(bridge);
>> + if (!ret)
>> + bridge->streaming = enable;
>> +
>> +out:
>> + dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
>> + bridge->streaming, ret);
>> + mutex_unlock(&bridge->lock);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_get_fmt(struct v4l2_subdev *sd,
>> + struct v4l2_subdev_pad_config *cfg,
>> + struct v4l2_subdev_format *format)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_mbus_framefmt *fmt;
>> +
>> + dev_dbg(&client->dev, "%s probe %d", __func__, format->pad);
>> +
>> + if (format->pad >= MIPID02_PAD_NB)
>> + return -EINVAL;
>> + /* second CSI-2 pad not yet supported */
>> + if (format->pad == 1)
>> + return -EINVAL;
>> +
>> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
>> + fmt = v4l2_subdev_get_try_format(&bridge->sd, cfg, format->pad);
>> + else
>> + fmt = &bridge->fmt;
>> +
>> + mutex_lock(&bridge->lock);
>> +
>> + format->format = *fmt;
>> +
>> + mutex_unlock(&bridge->lock);
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_set_fmt(struct v4l2_subdev *sd,
>> + struct v4l2_subdev_pad_config *cfg,
>> + struct v4l2_subdev_format *format)
>> +{
>> + struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_mbus_framefmt *fmt;
>> + int ret;
>> +
>> + dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
>> +
>> + if (format->pad >= MIPID02_PAD_NB)
>> + return -EINVAL;
>> + /* second CSI-2 pad not yet supported */
>> + if (format->pad == 1)
>> + return -EINVAL;
>> +
>> + mutex_lock(&bridge->lock);
>> +
>> + if (bridge->streaming) {
>> + ret = -EBUSY;
>> + goto error;
>> + }
>> +
>> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
>> + fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
>> + else
>> + fmt = &bridge->fmt;
>> +
>> + *fmt = *mbus_fmt;
>
> What are the limits of the hardware regarding the size of the image? Aren't
> there any?
>
There are no limits for image size.
> The format on the sink pad needs to be propagated to the source pad as
> well. I presume there's nothing to set on the sink pad for this device, is
> there?
User only need to set format code so the driver can configure hardware.
In the mipid02 we have always the same format for sink and source. So I only store
one configuration during set_fmt when called from either pad0 (sink) or pad2 (source).
Is it the correct way to implement it ? or should I only accept set_fmt on pad0 ?
For get_fmt I return stored configuration for pad0 and pad2.

> Please see:
>
> <URL:https://hverkuil.home.xs4all.nl/spec/uapi/v4l/dev-subdev.html#order-of-configuration-and-format-propagation>
>
>> + fmt->code = get_fmt_code(mbus_fmt->code);
>> + fmt->field = V4L2_FIELD_NONE;
>> + fmt->colorspace = V4L2_COLORSPACE_SRGB;
>> + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
>> + fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
>> + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
>> +
>> + ret = 0;
>> +
>> +error:
>> + mutex_unlock(&bridge->lock);
>> +
>> + return ret;
>> +}
>> +
>> +static const struct v4l2_subdev_video_ops mipid02_video_ops = {
>> + .s_stream = mipid02_s_stream,
>> +};
>> +
>> +static const struct v4l2_subdev_pad_ops mipid02_pad_ops = {
>> + .get_fmt = mipid02_get_fmt,
>> + .set_fmt = mipid02_set_fmt,
>> +};
>> +
>> +static const struct v4l2_subdev_ops mipid02_subdev_ops = {
>> + .video = &mipid02_video_ops,
>> + .pad = &mipid02_pad_ops,
>> +};
>> +
>> +static const struct media_entity_operations mipid02_subdev_entity_ops = {
>> + .link_validate = v4l2_subdev_link_validate,
>> +};
>> +
>> +static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
>> + struct v4l2_subdev *s_subdev,
>> + struct v4l2_async_subdev *asd)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int source_pad;
>> + int ret;
>> +
>> + dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
>> +
>> + source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
>> + s_subdev->fwnode,
>> + MEDIA_PAD_FL_SOURCE);
>> + if (source_pad < 0) {
>> + dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
>> + s_subdev->name);
>> + return source_pad;
>> + }
>> +
>> + ret = media_create_pad_link(&s_subdev->entity, source_pad,
>> + &bridge->sd.entity, 0,
>> + MEDIA_LNK_FL_ENABLED |
>> + MEDIA_LNK_FL_IMMUTABLE);
>> + if (ret) {
>> + dev_err(&client->dev, "Couldn't create media link %d", ret);
>> + return ret;
>> + }
>> +
>> + bridge->s_subdev = s_subdev;
>> +
>> + return 0;
>> +}
>> +
>> +static void mipid02_async_unbind(struct v4l2_async_notifier *notifier,
>> + struct v4l2_subdev *s_subdev,
>> + struct v4l2_async_subdev *asd)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
>> +
>> + bridge->s_subdev = NULL;
>> +}
>> +
>> +static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
>> + .bound = mipid02_async_bound,
>> + .unbind = mipid02_async_unbind,
>> +};
>> +
>> +static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_CSI2_DPHY };
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct device_node *ep_node;
>> + int ret;
>> +
>> + /* parse rx (endpoint 0) */
>> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
>> + 0, 0);
>> + if (!ep_node) {
>> + dev_err(&client->dev, "unable to find port0 ep");
>> + ret = -EINVAL;
>> + goto error;
>> + }
>> +
>> + ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
>> + if (ret) {
>> + dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
>> + ret);
>> + goto error_of_node_put;
>> + }
>> +
>> + /* do some sanity checks */
>> + if (ep.bus.mipi_csi2.num_data_lanes > 2) {
>> + dev_err(&client->dev, "max supported data lanes is 2 / got %d",
>> + ep.bus.mipi_csi2.num_data_lanes);
>> + ret = -EINVAL;
>> + goto error_v4l2_fwnode_endpoint_free;
>> + }
>> +
>> + /* register it for later use */
>> + bridge->rx = ep;
>> + bridge->rx.link_frequencies = ep.nr_of_link_frequencies == 1 ?
>> + &bridge->link_frequency : NULL;
>> + v4l2_fwnode_endpoint_free(&ep);
>> +
>> + /* register async notifier so we get noticed when sensor is connected */
>> + bridge->asd.match.fwnode =
>> + fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
>> + bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
>> + of_node_put(ep_node);
>> +
>> + v4l2_async_notifier_init(&bridge->notifier);
>> + ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
>> + if (ret) {
>> + dev_err(&client->dev, "fail to register asd to notifier %d",
>> + ret);
>> + goto error_fwnode_handle_put;
>> + }
>> + bridge->notifier.ops = &mipid02_notifier_ops;
>> +
>> + ret = v4l2_async_subdev_notifier_register(&bridge->sd,
>> + &bridge->notifier);
>> + if (ret)
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> +
>> + return ret;
>> +
>> +error_v4l2_fwnode_endpoint_free:
>> + v4l2_fwnode_endpoint_free(&ep);
>> +error_of_node_put:
>> + of_node_put(ep_node);
>> +error:
>> +
>> + return ret;
>> +
>> +error_fwnode_handle_put:
>> + fwnode_handle_put(bridge->asd.match.fwnode);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_PARALLEL };
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct device_node *ep_node;
>> + int ret;
>> +
>> + /* parse tx (endpoint 2) */
>> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
>> + 2, 0);
>> + if (!ep_node) {
>> + dev_err(&client->dev, "unable to find port1 ep");
>> + ret = -EINVAL;
>> + goto error;
>> + }
>> +
>> + ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
>> + if (ret) {
>> + dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
>> + goto error_of_node_put;
>> + }
>> +
>> + of_node_put(ep_node);
>> + bridge->tx = ep;
>> +
>> + return 0;
>> +
>> +error_of_node_put:
>> + of_node_put(ep_node);
>> +error:
>> +
>> + return -EINVAL;
>> +}
>> +
>> +static int mipid02_probe(struct i2c_client *client,
>> + const struct i2c_device_id *id)
>> +{
>> + struct device *dev = &client->dev;
>> + struct mipid02_dev *bridge;
>> + u32 clk_freq;
>> + int ret;
>> +
>> + bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
>> + if (!bridge)
>> + return -ENOMEM;
>> +
>> + bridge->fmt.code = MEDIA_BUS_FMT_SBGGR8_1X8;
>> + bridge->fmt.field = V4L2_FIELD_NONE;
>> + bridge->fmt.colorspace = V4L2_COLORSPACE_SRGB;
>> + bridge->fmt.ycbcr_enc =
>> + V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
>> + bridge->fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
>> + bridge->fmt.xfer_func =
>> + V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
>> + bridge->fmt.width = 640;
>> + bridge->fmt.height = 480;
>> +
>> + bridge->i2c_client = client;
>> + v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
>> +
>> + /* got and check clock */
>> + bridge->xclk = devm_clk_get(dev, "xclk");
>> + if (IS_ERR(bridge->xclk)) {
>> + dev_err(dev, "failed to get xclk\n");
>> + return PTR_ERR(bridge->xclk);
>> + }
>> +
>> + clk_freq = clk_get_rate(bridge->xclk);
>> + if (clk_freq < 6000000 || clk_freq > 27000000) {
>> + dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
>> + clk_freq);
>> + return -EINVAL;
>> + }
>> +
>> + bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
>> + GPIOD_OUT_HIGH);
>> +
>> + ret = mipid02_get_regulators(bridge);
>> + if (ret) {
>> + dev_err(dev, "failed to get regulators %d", ret);
>> + return ret;
>> + }
>> +
>> + mutex_init(&bridge->lock);
>> + bridge->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
>> + bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
>> + bridge->sd.entity.ops = &mipid02_subdev_entity_ops;
>> + bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
>> + bridge->pad[1].flags = MEDIA_PAD_FL_SINK;
>> + bridge->pad[2].flags = MEDIA_PAD_FL_SOURCE;
>> + ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
>> + bridge->pad);
>> + if (ret) {
>> + dev_err(&client->dev, "pads init failed %d", ret);
>> + return ret;
>> + }
>> +
>> + /* enable clock, power and reset device if available */
>> + ret = mipid02_set_power_on(bridge);
>> + if (ret)
>> + goto entity_cleanup;
>> +
>> + ret = mipid02_detect(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to detect mipid02 %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = mipid02_parse_tx_ep(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to parse tx %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = mipid02_parse_rx_ep(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to parse rx %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = v4l2_async_register_subdev(&bridge->sd);
>> + if (ret < 0) {
>> + dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
>> + ret);
>> + goto unregister_notifier;
>> + }
>> +
>> + dev_info(&client->dev, "mipid02 device probe successfully");
>> +
>> + return 0;
>> +
>> +unregister_notifier:
>> + v4l2_async_notifier_unregister(&bridge->notifier);
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> +power_off:
>> + mipid02_set_power_off(bridge);
>> +entity_cleanup:
>> + media_entity_cleanup(&bridge->sd.entity);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_remove(struct i2c_client *client)
>> +{
>> + struct v4l2_subdev *sd = i2c_get_clientdata(client);
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> +
>> + v4l2_async_notifier_unregister(&bridge->notifier);
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> + v4l2_async_unregister_subdev(&bridge->sd);
>> + mipid02_set_power_off(bridge);
>> + media_entity_cleanup(&bridge->sd.entity);
>> +
>> + return 0;
>> +}
>> +
>> +static const struct i2c_device_id mipid02_id[] = {
>> + { "st-mipid02", 0},
>> + {},
>> +};
>> +MODULE_DEVICE_TABLE(i2c, mipid02_id);
>> +
>> +static const struct of_device_id mipid02_dt_ids[] = {
>> + { .compatible = "st,st-mipid02" },
>> + { /* sentinel */ }
>> +};
>> +MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
>> +
>> +static struct i2c_driver mipid02_i2c_driver = {
>> + .driver = {
>> + .name = "st-mipid02",
>> + .of_match_table = mipid02_dt_ids,
>> + },
>> + .id_table = mipid02_id,
>> + .probe = mipid02_probe,
>> + .remove = mipid02_remove,
>> +};
>> +
>> +module_i2c_driver(mipid02_i2c_driver);
>> +
>> +MODULE_AUTHOR("Mickael Guene <[email protected]>");
>> +MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
>> +MODULE_LICENSE("GPL v2");
>

2019-03-26 13:43:12

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] dt-bindings: Document MIPID02 bindings

Hi Sakari,

On 3/26/19 13:17, Sakari Ailus wrote:
> Hi Mickael,
>
> On Tue, Mar 26, 2019 at 11:03:39AM +0100, Mickael Guene wrote:
>> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
>> bridge.
>>
>> Signed-off-by: Mickael Guene <[email protected]>
>> ---
>>
>> Changes in v3: None
>> Changes in v2:
>> - Add precision about first CSI-2 port data rate
>> - Document endpoints supported properties
>> - Rename 'mipid02@14' into generic 'csi2rx@14' in example
>>
>> .../bindings/media/i2c/st,st-mipid02.txt | 83 ++++++++++++++++++++++
>> MAINTAINERS | 7 ++
>> 2 files changed, 90 insertions(+)
>> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>>
>> diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>> new file mode 100644
>> index 0000000..dfeab45
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>> @@ -0,0 +1,83 @@
>> +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
>> +
>> +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
>> +time. Active port input stream will be de-serialized and its content outputted
>> +through PARALLEL output port.
>> +CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
>> +input port is a single lane 800Mbps. Both ports support clock and data lane
>> +polarity swap. First port also supports data lane swap.
>> +PARALLEL output port has a maximum width of 12 bits.
>> +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
>> +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
>> +
>> +Required Properties:
>> +- compatible: should be "st,st-mipid02"
>> +- clocks: reference to the xclk input clock.
>> +- clock-names: should be "xclk".
>> +- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
>> +- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
>> +
>> +Optional Properties:
>> +- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
>> + This is an active low signal to the mipid02.
>> +
>> +Required subnodes:
>> + - ports: A ports node with one port child node per device input and output
>> + port, in accordance with the video interface bindings defined in
>> + Documentation/devicetree/bindings/media/video-interfaces.txt. The
>> + port nodes are numbered as follows:
>> +
>> + Port Description
>> + -----------------------------
>> + 0 CSI-2 first input port
>> + 1 CSI-2 second input port
>> + 2 PARALLEL output
>> +
>> +Endpoint node optional properties for CSI-2 connection are:
>> +- bus-type: if present should be 4 - MIPI CSI-2 D-PHY.
>
> You can drop this IMO --- there's just a single valid value so the driver
> may know that.
>
ok
>> +- clock-lanes: should be set to <0> if present (clock lane on hardware lane 0).
>
> And please omit this, too, if the clock lane is always 0. Please update the
> example, too. The driver doesn't need to check that either IMO, but up to
> you.
>
ok I will drop it from device tree documentation but I will keep driver check.
I will also make data-lanes mandatory.
>> +- data-lanes: if present should be <1> for Port 1. for Port 0 dual-lane
>> +operation should be <1 2> or <2 1>. For Port 0 single-lane operation should be
>> +<1> or <2>.
>> +- lane-polarities: any lane can be inverted.
>> +
>> +Endpoint node optional properties for PARALLEL connection are:
>> +- bus-type: if present should be 5 - Parallel.
>
> This, too, can be omitted.
>
ok
>> +- bus-width: shall be set to <6>, <7>, <8>, <10> or <12>.
>> +- hsync-active: active state of the HSYNC signal, 0/1 for LOW/HIGH respectively.
>> +- vsync-active: active state of the VSYNC signal, 0/1 for LOW/HIGH respectively.
>
> If these are optional, what are the defaults? IMO you could make them
> mandatory as well.
>
I will make bus-width mandatory
hsync-active and vsync-active will stay optional with LOW being the default.
>> +
>> +Example:
>> +
>> +mipid02: csi2rx@14 {
>> + compatible = "st,st-mipid02";
>> + reg = <0x14>;
>> + status = "okay";
>> + clocks = <&clk_ext_camera_12>;
>> + clock-names = "xclk";
>> + VDDE-supply = <&vdd>;
>> + VDDIN-supply = <&vdd>;
>> + ports {
>> + #address-cells = <1>;
>> + #size-cells = <0>;
>> + port@0 {
>> + reg = <0>;
>> +
>> + ep0: endpoint {
>> + clock-lanes = <0>;
>> + data-lanes = <1 2>;
>> + remote-endpoint = <&mipi_csi2_in>;
>> + };
>> + };
>> + port@2 {
>> + reg = <2>;
>> +
>> + ep2: endpoint {
>> + bus-width = <8>;
>> + hsync-active = <0>;
>> + vsync-active = <0>;
>> + remote-endpoint = <&parallel_out>;
>> + };
>> + };
>> + };
>> +};
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index e17ebf7..74da99d 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -14668,6 +14668,13 @@ S: Maintained
>> F: drivers/iio/imu/st_lsm6dsx/
>> F: Documentation/devicetree/bindings/iio/imu/st_lsm6dsx.txt
>>
>> +ST MIPID02 CSI-2 TO PARALLEL BRIDGE DRIVER
>> +M: Mickael Guene <[email protected]>
>> +L: [email protected]
>> +T: git git://linuxtv.org/media_tree.git
>> +S: Maintained
>> +F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>> +
>> ST STM32 I2C/SMBUS DRIVER
>> M: Pierre-Yves MORDRET <[email protected]>
>> L: [email protected]
>

2019-03-26 13:45:03

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v3 1/2] dt-bindings: Document MIPID02 bindings

Hi Mickael,

On Tue, Mar 26, 2019 at 01:40:18PM +0000, Mickael GUENE wrote:
> Hi Sakari,
>
> On 3/26/19 13:17, Sakari Ailus wrote:
> > Hi Mickael,
> >
> > On Tue, Mar 26, 2019 at 11:03:39AM +0100, Mickael Guene wrote:
> >> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
> >> bridge.
> >>
> >> Signed-off-by: Mickael Guene <[email protected]>
> >> ---
> >>
> >> Changes in v3: None
> >> Changes in v2:
> >> - Add precision about first CSI-2 port data rate
> >> - Document endpoints supported properties
> >> - Rename 'mipid02@14' into generic 'csi2rx@14' in example
> >>
> >> .../bindings/media/i2c/st,st-mipid02.txt | 83 ++++++++++++++++++++++
> >> MAINTAINERS | 7 ++
> >> 2 files changed, 90 insertions(+)
> >> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> >>
> >> diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> >> new file mode 100644
> >> index 0000000..dfeab45
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> >> @@ -0,0 +1,83 @@
> >> +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
> >> +
> >> +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
> >> +time. Active port input stream will be de-serialized and its content outputted
> >> +through PARALLEL output port.
> >> +CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
> >> +input port is a single lane 800Mbps. Both ports support clock and data lane
> >> +polarity swap. First port also supports data lane swap.
> >> +PARALLEL output port has a maximum width of 12 bits.
> >> +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
> >> +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
> >> +
> >> +Required Properties:
> >> +- compatible: should be "st,st-mipid02"
> >> +- clocks: reference to the xclk input clock.
> >> +- clock-names: should be "xclk".
> >> +- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
> >> +- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
> >> +
> >> +Optional Properties:
> >> +- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
> >> + This is an active low signal to the mipid02.
> >> +
> >> +Required subnodes:
> >> + - ports: A ports node with one port child node per device input and output
> >> + port, in accordance with the video interface bindings defined in
> >> + Documentation/devicetree/bindings/media/video-interfaces.txt. The
> >> + port nodes are numbered as follows:
> >> +
> >> + Port Description
> >> + -----------------------------
> >> + 0 CSI-2 first input port
> >> + 1 CSI-2 second input port
> >> + 2 PARALLEL output
> >> +
> >> +Endpoint node optional properties for CSI-2 connection are:
> >> +- bus-type: if present should be 4 - MIPI CSI-2 D-PHY.
> >
> > You can drop this IMO --- there's just a single valid value so the driver
> > may know that.
> >
> ok
> >> +- clock-lanes: should be set to <0> if present (clock lane on hardware lane 0).
> >
> > And please omit this, too, if the clock lane is always 0. Please update the
> > example, too. The driver doesn't need to check that either IMO, but up to
> > you.
> >
> ok I will drop it from device tree documentation but I will keep driver check.
> I will also make data-lanes mandatory.
> >> +- data-lanes: if present should be <1> for Port 1. for Port 0 dual-lane
> >> +operation should be <1 2> or <2 1>. For Port 0 single-lane operation should be
> >> +<1> or <2>.
> >> +- lane-polarities: any lane can be inverted.
> >> +
> >> +Endpoint node optional properties for PARALLEL connection are:
> >> +- bus-type: if present should be 5 - Parallel.
> >
> > This, too, can be omitted.
> >
> ok
> >> +- bus-width: shall be set to <6>, <7>, <8>, <10> or <12>.
> >> +- hsync-active: active state of the HSYNC signal, 0/1 for LOW/HIGH respectively.
> >> +- vsync-active: active state of the VSYNC signal, 0/1 for LOW/HIGH respectively.
> >
> > If these are optional, what are the defaults? IMO you could make them
> > mandatory as well.
> >
> I will make bus-width mandatory
> hsync-active and vsync-active will stay optional with LOW being the default.

The above seems good to me. Thanks!

--
Sakari Ailus

2019-03-26 13:56:00

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Mickael,

On Tue, Mar 26, 2019 at 12:57:03PM +0000, Mickael GUENE wrote:
...
> >> +static int mipid02_set_fmt(struct v4l2_subdev *sd,
> >> + struct v4l2_subdev_pad_config *cfg,
> >> + struct v4l2_subdev_format *format)
> >> +{
> >> + struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
> >> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> >> + struct i2c_client *client = bridge->i2c_client;
> >> + struct v4l2_mbus_framefmt *fmt;
> >> + int ret;
> >> +
> >> + dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
> >> +
> >> + if (format->pad >= MIPID02_PAD_NB)
> >> + return -EINVAL;
> >> + /* second CSI-2 pad not yet supported */
> >> + if (format->pad == 1)
> >> + return -EINVAL;
> >> +
> >> + mutex_lock(&bridge->lock);
> >> +
> >> + if (bridge->streaming) {
> >> + ret = -EBUSY;
> >> + goto error;
> >> + }
> >> +
> >> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
> >> + fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
> >> + else
> >> + fmt = &bridge->fmt;
> >> +
> >> + *fmt = *mbus_fmt;
> >
> > What are the limits of the hardware regarding the size of the image? Aren't
> > there any?
> >
> There are no limits for image size.
> > The format on the sink pad needs to be propagated to the source pad as
> > well. I presume there's nothing to set on the sink pad for this device, is
> > there?
> User only need to set format code so the driver can configure hardware.
> In the mipid02 we have always the same format for sink and source. So I
> only store one configuration during set_fmt when called from either pad0
> (sink) or pad2 (source). Is it the correct way to implement it ? or
> should I only accept set_fmt on pad0 ?
> For get_fmt I return stored configuration for pad0 and pad2.

Only 76 or so characters per line, please.

For pad 0 (sink) the format must be settable freely (as you don't have any
hardware restrictions) and on the pad 2 (source) the driver converts the
format set on the pad 0 according to the hardware functionality. This is
what the link validation from the source pad onwards is based on.

--
Regards,

Sakari Ailus
[email protected]

2019-03-26 14:14:24

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Sakari,

On 3/26/19 14:54, Sakari Ailus wrote:
> Hi Mickael,
>
> On Tue, Mar 26, 2019 at 12:57:03PM +0000, Mickael GUENE wrote:
> ...
>>>> +static int mipid02_set_fmt(struct v4l2_subdev *sd,
>>>> + struct v4l2_subdev_pad_config *cfg,
>>>> + struct v4l2_subdev_format *format)
>>>> +{
>>>> + struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
>>>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>>>> + struct i2c_client *client = bridge->i2c_client;
>>>> + struct v4l2_mbus_framefmt *fmt;
>>>> + int ret;
>>>> +
>>>> + dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
>>>> +
>>>> + if (format->pad >= MIPID02_PAD_NB)
>>>> + return -EINVAL;
>>>> + /* second CSI-2 pad not yet supported */
>>>> + if (format->pad == 1)
>>>> + return -EINVAL;
>>>> +
>>>> + mutex_lock(&bridge->lock);
>>>> +
>>>> + if (bridge->streaming) {
>>>> + ret = -EBUSY;
>>>> + goto error;
>>>> + }
>>>> +
>>>> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
>>>> + fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
>>>> + else
>>>> + fmt = &bridge->fmt;
>>>> +
>>>> + *fmt = *mbus_fmt;
>>>
>>> What are the limits of the hardware regarding the size of the image? Aren't
>>> there any?
>>>
>> There are no limits for image size.
>>> The format on the sink pad needs to be propagated to the source pad as
>>> well. I presume there's nothing to set on the sink pad for this device, is
>>> there?
>> User only need to set format code so the driver can configure hardware.
>> In the mipid02 we have always the same format for sink and source. So I
>> only store one configuration during set_fmt when called from either pad0
>> (sink) or pad2 (source). Is it the correct way to implement it ? or
>> should I only accept set_fmt on pad0 ?
>> For get_fmt I return stored configuration for pad0 and pad2.
>
> Only 76 or so characters per line, please.
>
> For pad 0 (sink) the format must be settable freely (as you don't have any
> hardware restrictions) and on the pad 2 (source) the driver converts the
> format set on the pad 0 according to the hardware functionality. This is
> what the link validation from the source pad onwards is based on.
>
So for a set_fmt on pad 2 I return current configuration ? (as
in my case I have the same configuration for pad 0 and pad2)
And I only update format when set_fmt is called for pad 0 ?

Thanks for your help

Rgs
Mickael

2019-03-26 14:17:26

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

On Tue, Mar 26, 2019 at 02:12:52PM +0000, Mickael GUENE wrote:
> Sakari,
>
> On 3/26/19 14:54, Sakari Ailus wrote:
> > Hi Mickael,
> >
> > On Tue, Mar 26, 2019 at 12:57:03PM +0000, Mickael GUENE wrote:
> > ...
> >>>> +static int mipid02_set_fmt(struct v4l2_subdev *sd,
> >>>> + struct v4l2_subdev_pad_config *cfg,
> >>>> + struct v4l2_subdev_format *format)
> >>>> +{
> >>>> + struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
> >>>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
> >>>> + struct i2c_client *client = bridge->i2c_client;
> >>>> + struct v4l2_mbus_framefmt *fmt;
> >>>> + int ret;
> >>>> +
> >>>> + dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
> >>>> +
> >>>> + if (format->pad >= MIPID02_PAD_NB)
> >>>> + return -EINVAL;
> >>>> + /* second CSI-2 pad not yet supported */
> >>>> + if (format->pad == 1)
> >>>> + return -EINVAL;
> >>>> +
> >>>> + mutex_lock(&bridge->lock);
> >>>> +
> >>>> + if (bridge->streaming) {
> >>>> + ret = -EBUSY;
> >>>> + goto error;
> >>>> + }
> >>>> +
> >>>> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
> >>>> + fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
> >>>> + else
> >>>> + fmt = &bridge->fmt;
> >>>> +
> >>>> + *fmt = *mbus_fmt;
> >>>
> >>> What are the limits of the hardware regarding the size of the image? Aren't
> >>> there any?
> >>>
> >> There are no limits for image size.
> >>> The format on the sink pad needs to be propagated to the source pad as
> >>> well. I presume there's nothing to set on the sink pad for this device, is
> >>> there?
> >> User only need to set format code so the driver can configure hardware.
> >> In the mipid02 we have always the same format for sink and source. So I
> >> only store one configuration during set_fmt when called from either pad0
> >> (sink) or pad2 (source). Is it the correct way to implement it ? or
> >> should I only accept set_fmt on pad0 ?
> >> For get_fmt I return stored configuration for pad0 and pad2.
> >
> > Only 76 or so characters per line, please.
> >
> > For pad 0 (sink) the format must be settable freely (as you don't have any
> > hardware restrictions) and on the pad 2 (source) the driver converts the
> > format set on the pad 0 according to the hardware functionality. This is
> > what the link validation from the source pad onwards is based on.
> >
> So for a set_fmt on pad 2 I return current configuration ? (as
> in my case I have the same configuration for pad 0 and pad2)
> And I only update format when set_fmt is called for pad 0 ?

Correct. Do note that the format is different in cases (uyvy8_1x16 vs. 2x8)
as you're dealing with two different kinds of busses.

--
Sakari Ailus
[email protected]

2019-03-26 14:38:32

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Sakari,

>> +static int bpp_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 8;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 10;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 12;
>> + case MEDIA_BUS_FMT_UYVY8_2X8:
>
> This is good for the parallel bus, but on CSI-2 side you should have
> MEDIA_BUS_FMT_UYVY8_1X16 instead. This isn't technically correct for a
> serial bus, but the custom is to use the one sample / pixel formats on the
> serial busses.
>
Should MEDIA_BUS_FMT_BGR888_1X24 be something like
MEDIA_BUS_FMT_BGR888_3X8 for parallel output bus ?

Rgs
Mickael

2019-03-27 09:57:14

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v4 1/2] dt-bindings: Document MIPID02 bindings

This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
bridge.

Signed-off-by: Mickael Guene <[email protected]>
---

Changes in v4:
- Fix and clarify endpoints properties documentation

Changes in v3: None
Changes in v2:
- Add precision about first CSI-2 port data rate
- Document endpoints supported properties
- Rename 'mipid02@14' into generic 'csi2rx@14' in example

.../bindings/media/i2c/st,st-mipid02.txt | 82 ++++++++++++++++++++++
MAINTAINERS | 7 ++
2 files changed, 89 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt

diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
new file mode 100644
index 0000000..754a175
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
@@ -0,0 +1,82 @@
+STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
+
+MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
+time. Active port input stream will be de-serialized and its content outputted
+through PARALLEL output port.
+CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
+input port is a single lane 800Mbps. Both ports support clock and data lane
+polarity swap. First port also supports data lane swap.
+PARALLEL output port has a maximum width of 12 bits.
+Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
+YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
+
+Required Properties:
+- compatible: should be "st,st-mipid02"
+- clocks: reference to the xclk input clock.
+- clock-names: should be "xclk".
+- VDDE-supply: sensor digital IO supply. Must be 1.8 volts.
+- VDDIN-supply: sensor internal regulator supply. Must be 1.8 volts.
+
+Optional Properties:
+- reset-gpios: reference to the GPIO connected to the xsdn pin, if any.
+ This is an active low signal to the mipid02.
+
+Required subnodes:
+ - ports: A ports node with one port child node per device input and output
+ port, in accordance with the video interface bindings defined in
+ Documentation/devicetree/bindings/media/video-interfaces.txt. The
+ port nodes are numbered as follows:
+
+ Port Description
+ -----------------------------
+ 0 CSI-2 first input port
+ 1 CSI-2 second input port
+ 2 PARALLEL output
+
+Endpoint node required property for CSI-2 connection is:
+- data-lanes: Should be <1> for Port 1. for Port 0 dual-lane operation should be
+<1 2> or <2 1>. For Port 0 single-lane operation should be <1> or <2>.
+Endpoint node optional property for CSI-2 connection is:
+- lane-polarities: any lane can be inverted or not.
+
+Endpoint node required property for PARALLEL connection is:
+- bus-width: should be set to <6>, <7>, <8>, <10> or <12>.
+Endpoint node optional properties for PARALLEL connection are:
+- hsync-active: active state of the HSYNC signal, 0/1 for LOW/HIGH respectively.
+LOW being the default.
+- vsync-active: active state of the VSYNC signal, 0/1 for LOW/HIGH respectively.
+LOW being the default.
+
+Example:
+
+mipid02: csi2rx@14 {
+ compatible = "st,st-mipid02";
+ reg = <0x14>;
+ status = "okay";
+ clocks = <&clk_ext_camera_12>;
+ clock-names = "xclk";
+ VDDE-supply = <&vdd>;
+ VDDIN-supply = <&vdd>;
+ ports {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ port@0 {
+ reg = <0>;
+
+ ep0: endpoint {
+ data-lanes = <1 2>;
+ remote-endpoint = <&mipi_csi2_in>;
+ };
+ };
+ port@2 {
+ reg = <2>;
+
+ ep2: endpoint {
+ bus-width = <8>;
+ hsync-active = <0>;
+ vsync-active = <0>;
+ remote-endpoint = <&parallel_out>;
+ };
+ };
+ };
+};
diff --git a/MAINTAINERS b/MAINTAINERS
index e17ebf7..74da99d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14668,6 +14668,13 @@ S: Maintained
F: drivers/iio/imu/st_lsm6dsx/
F: Documentation/devicetree/bindings/iio/imu/st_lsm6dsx.txt

+ST MIPID02 CSI-2 TO PARALLEL BRIDGE DRIVER
+M: Mickael Guene <[email protected]>
+L: [email protected]
+T: git git://linuxtv.org/media_tree.git
+S: Maintained
+F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
+
ST STM32 I2C/SMBUS DRIVER
M: Pierre-Yves MORDRET <[email protected]>
L: [email protected]
--
2.7.4


2019-03-27 09:57:23

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v4 0/2] Add support for MIPID02 CSI-2 to PARALLEL bridge I2C device

Hi,

This is the v4 of my MIPID02 series which introduces support of
STMicroelectronics MIPID02 CSI-2 to PARALLEL I2C bridge. It allows using a CSI-2
sensor with a PARALLEL interface. Current driver implementation doesn't support
CSI-2 second input port usage. It doesn't support also YUV420, RGB565 and RGB444
input formats.

Thanks to Sakari for review.

Changes in v4:
- Fix and clarify endpoints properties documentation
- Add support of enum_mbus_code
- Only use V4L2_CID_PIXEL_RATE to compute link speed
- Use MEDIA_BUS_FMT_UYVY8_1X16 instead of MEDIA_BUS_FMT_UYVY8_2X8 for CSI-2 link
- Fix miscellaneous typos
- Fix wrong code behavior for set_fmt and get_fmt

Changes in v3:
- Fix potential wrong error code for mipid02_stream_disable and mipid02_stream_enable
- Remove useless memset for ep in mipid02_parse_rx_ep and mipid02_parse_tx_ep
- Add second CSI-2 input pad even if it's not yet supported
- Add support of get_fmt, set_fmt and link_validate and only access subdev connected to mipid02

Changes in v2:
- Add precision about first CSI-2 port data rate
- Document endpoints supported properties
- Rename 'mipid02@14' into generic 'csi2rx@14' in example
- Merge MAINTAINERS patch 3 into patch 1 and 2
- Fix line too long in Kconfig
- Add missing delay after reset release
- Various style fixes
- Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed

Mickael Guene (2):
dt-bindings: Document MIPID02 bindings
media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

.../bindings/media/i2c/st,st-mipid02.txt | 82 ++
MAINTAINERS | 8 +
drivers/media/i2c/Kconfig | 14 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/st-mipid02.c | 1043 ++++++++++++++++++++
5 files changed, 1148 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
create mode 100644 drivers/media/i2c/st-mipid02.c

--
2.7.4


2019-03-27 09:58:13

by Mickael GUENE

[permalink] [raw]
Subject: [PATCH v4 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

This V4L2 subdev driver enables STMicroelectronics MIPID02 device.

Signed-off-by: Mickael Guene <[email protected]>
---

Changes in v4:
- Add support of enum_mbus_code
- Only use V4L2_CID_PIXEL_RATE to compute link speed
- Use MEDIA_BUS_FMT_UYVY8_1X16 instead of MEDIA_BUS_FMT_UYVY8_2X8 for CSI-2 link
- Fix miscellaneous typos
- Fix wrong code behavior for set_fmt and get_fmt

Changes in v3:
- Fix potential wrong error code for mipid02_stream_disable and mipid02_stream_enable
- Remove useless memset for ep in mipid02_parse_rx_ep and mipid02_parse_tx_ep
- Add second CSI-2 input pad even if it's not yet supported
- Add support of get_fmt, set_fmt and link_validate and only access subdev connected to mipid02

Changes in v2:
- Merge MAINTAINERS patch 3 into patch 1 and 2
- Fix line too long in Kconfig
- Add missing delay after reset release
- Various style fixes
- Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed

MAINTAINERS | 1 +
drivers/media/i2c/Kconfig | 14 +
drivers/media/i2c/Makefile | 1 +
drivers/media/i2c/st-mipid02.c | 1043 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 1059 insertions(+)
create mode 100644 drivers/media/i2c/st-mipid02.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 74da99d..a14fe81 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14673,6 +14673,7 @@ M: Mickael Guene <[email protected]>
L: [email protected]
T: git git://linuxtv.org/media_tree.git
S: Maintained
+F: drivers/media/i2c/st-mipid02.c
F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt

ST STM32 I2C/SMBUS DRIVER
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 1a1746b..2dc9e15 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -1135,6 +1135,20 @@ config VIDEO_I2C
To compile this driver as a module, choose M here: the
module will be called video-i2c

+config VIDEO_ST_MIPID02
+ tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
+ depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
+ depends on MEDIA_CAMERA_SUPPORT
+ depends on MEDIA_CONTROLLER
+ select V4L2_FWNODE
+ help
+ Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
+ It is used to allow usage of CSI-2 sensor with PARALLEL port
+ controller.
+
+ To compile this driver as a module, choose M here: the
+ module will be called st-mipid02.
+
endmenu

endif
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index a64fca8..d8ad9da 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
obj-$(CONFIG_VIDEO_IMX274) += imx274.o
obj-$(CONFIG_VIDEO_IMX319) += imx319.o
obj-$(CONFIG_VIDEO_IMX355) += imx355.o
+obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o

obj-$(CONFIG_SDR_MAX2175) += max2175.o
diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
new file mode 100644
index 0000000..1d09bae
--- /dev/null
+++ b/drivers/media/i2c/st-mipid02.c
@@ -0,0 +1,1043 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
+ *
+ * Copyright (C) STMicroelectronics SA 2019
+ * Authors: Mickael Guene <[email protected]>
+ * for STMicroelectronics.
+ *
+ *
+ */
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/of_graph.h>
+#include <linux/regulator/consumer.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+
+#define MIPID02_CLK_LANE_WR_REG1 0x01
+#define MIPID02_CLK_LANE_REG1 0x02
+#define MIPID02_CLK_LANE_REG3 0x04
+#define MIPID02_DATA_LANE0_REG1 0x05
+#define MIPID02_DATA_LANE0_REG2 0x06
+#define MIPID02_DATA_LANE1_REG1 0x09
+#define MIPID02_DATA_LANE1_REG2 0x0a
+#define MIPID02_MODE_REG1 0x14
+#define MIPID02_MODE_REG2 0x15
+#define MIPID02_DATA_ID_RREG 0x17
+#define MIPID02_DATA_SELECTION_CTRL 0x19
+#define MIPID02_PIX_WIDTH_CTRL 0x1e
+#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
+
+/* Bits definition for MIPID02_CLK_LANE_REG1 */
+#define CLK_ENABLE BIT(0)
+/* Bits definition for MIPID02_CLK_LANE_REG3 */
+#define CLK_MIPI_CSI BIT(1)
+/* Bits definition for MIPID02_DATA_LANE0_REG1 */
+#define DATA_ENABLE BIT(0)
+/* Bits definition for MIPID02_DATA_LANEx_REG2 */
+#define DATA_MIPI_CSI BIT(0)
+/* Bits definition for MIPID02_MODE_REG1 */
+#define MODE_DATA_SWAP BIT(2)
+#define MODE_NO_BYPASS BIT(6)
+/* Bits definition for MIPID02_MODE_REG2 */
+#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
+#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
+/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
+#define SELECTION_MANUAL_DATA BIT(2)
+#define SELECTION_MANUAL_WIDTH BIT(3)
+
+static const u32 mipid02_supported_fmt_codes[] = {
+ MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
+ MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
+ MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
+ MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
+ MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SGBRG12_1X12,
+ MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SRGGB12_1X12,
+ MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_BGR888_1X24
+};
+
+/* regulator supplies */
+static const char * const mipid02_supply_name[] = {
+ "VDDE", /* 1.8V digital I/O supply */
+ "VDDIN", /* 1V8 voltage regulator supply */
+};
+
+#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
+
+#define MIPID02_SINK_0 0
+#define MIPID02_SINK_1 1
+#define MIPID02_SOURCE 2
+#define MIPID02_PAD_NB 3
+
+struct mipid02_dev {
+ struct i2c_client *i2c_client;
+ struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
+ struct v4l2_subdev sd;
+ struct media_pad pad[MIPID02_PAD_NB];
+ struct clk *xclk;
+ struct gpio_desc *reset_gpio;
+ /* endpoints info */
+ struct v4l2_fwnode_endpoint rx;
+ u64 link_frequency;
+ struct v4l2_fwnode_endpoint tx;
+ /* remote source */
+ struct v4l2_async_subdev asd;
+ struct v4l2_async_notifier notifier;
+ struct v4l2_subdev *s_subdev;
+ /* registers */
+ struct {
+ u8 clk_lane_reg1;
+ u8 data_lane0_reg1;
+ u8 data_lane1_reg1;
+ u8 mode_reg1;
+ u8 mode_reg2;
+ u8 data_id_rreg;
+ u8 pix_width_ctrl;
+ u8 pix_width_ctrl_emb;
+ } r;
+ /* lock to protect all members below */
+ struct mutex lock;
+ bool streaming;
+ struct v4l2_mbus_framefmt fmt;
+};
+
+static int bpp_from_code(__u32 code)
+{
+ switch (code) {
+ case MEDIA_BUS_FMT_SBGGR8_1X8:
+ case MEDIA_BUS_FMT_SGBRG8_1X8:
+ case MEDIA_BUS_FMT_SGRBG8_1X8:
+ case MEDIA_BUS_FMT_SRGGB8_1X8:
+ return 8;
+ case MEDIA_BUS_FMT_SBGGR10_1X10:
+ case MEDIA_BUS_FMT_SGBRG10_1X10:
+ case MEDIA_BUS_FMT_SGRBG10_1X10:
+ case MEDIA_BUS_FMT_SRGGB10_1X10:
+ return 10;
+ case MEDIA_BUS_FMT_SBGGR12_1X12:
+ case MEDIA_BUS_FMT_SGBRG12_1X12:
+ case MEDIA_BUS_FMT_SGRBG12_1X12:
+ case MEDIA_BUS_FMT_SRGGB12_1X12:
+ return 12;
+ case MEDIA_BUS_FMT_UYVY8_1X16:
+ return 16;
+ case MEDIA_BUS_FMT_BGR888_1X24:
+ return 24;
+ default:
+ return 0;
+ }
+}
+
+static u8 data_type_from_code(__u32 code)
+{
+ switch (code) {
+ case MEDIA_BUS_FMT_SBGGR8_1X8:
+ case MEDIA_BUS_FMT_SGBRG8_1X8:
+ case MEDIA_BUS_FMT_SGRBG8_1X8:
+ case MEDIA_BUS_FMT_SRGGB8_1X8:
+ return 0x2a;
+ case MEDIA_BUS_FMT_SBGGR10_1X10:
+ case MEDIA_BUS_FMT_SGBRG10_1X10:
+ case MEDIA_BUS_FMT_SGRBG10_1X10:
+ case MEDIA_BUS_FMT_SRGGB10_1X10:
+ return 0x2b;
+ case MEDIA_BUS_FMT_SBGGR12_1X12:
+ case MEDIA_BUS_FMT_SGBRG12_1X12:
+ case MEDIA_BUS_FMT_SGRBG12_1X12:
+ case MEDIA_BUS_FMT_SRGGB12_1X12:
+ return 0x2c;
+ case MEDIA_BUS_FMT_UYVY8_1X16:
+ return 0x1e;
+ case MEDIA_BUS_FMT_BGR888_1X24:
+ return 0x24;
+ default:
+ return 0;
+ }
+}
+
+static void init_format(struct v4l2_mbus_framefmt *fmt)
+{
+ fmt->code = MEDIA_BUS_FMT_SBGGR8_1X8;
+ fmt->field = V4L2_FIELD_NONE;
+ fmt->colorspace = V4L2_COLORSPACE_SRGB;
+ fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
+ fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
+ fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
+ fmt->width = 640;
+ fmt->height = 480;
+}
+
+static __u32 get_fmt_code(__u32 code)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(mipid02_supported_fmt_codes); i++) {
+ if (code == mipid02_supported_fmt_codes[i])
+ return code;
+ }
+
+ return mipid02_supported_fmt_codes[0];
+}
+
+static __u32 serial_to_parallel_code(__u32 serial)
+{
+ if (serial == MEDIA_BUS_FMT_UYVY8_1X16)
+ return MEDIA_BUS_FMT_UYVY8_2X8;
+
+ return serial;
+}
+
+static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
+{
+ return container_of(sd, struct mipid02_dev, sd);
+}
+
+static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct i2c_msg msg[2];
+ u8 buf[2];
+ int ret;
+
+ buf[0] = reg >> 8;
+ buf[1] = reg & 0xff;
+
+ msg[0].addr = client->addr;
+ msg[0].flags = client->flags;
+ msg[0].buf = buf;
+ msg[0].len = sizeof(buf);
+
+ msg[1].addr = client->addr;
+ msg[1].flags = client->flags | I2C_M_RD;
+ msg[1].buf = val;
+ msg[1].len = 1;
+
+ ret = i2c_transfer(client->adapter, msg, 2);
+ if (ret < 0) {
+ dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
+ __func__, client->addr, reg, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct i2c_msg msg;
+ u8 buf[3];
+ int ret;
+
+ buf[0] = reg >> 8;
+ buf[1] = reg & 0xff;
+ buf[2] = val;
+
+ msg.addr = client->addr;
+ msg.flags = client->flags;
+ msg.buf = buf;
+ msg.len = sizeof(buf);
+
+ ret = i2c_transfer(client->adapter, &msg, 1);
+ if (ret < 0) {
+ dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
+ __func__, reg, ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mipid02_get_regulators(struct mipid02_dev *bridge)
+{
+ unsigned int i;
+
+ for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
+ bridge->supplies[i].supply = mipid02_supply_name[i];
+
+ return devm_regulator_bulk_get(&bridge->i2c_client->dev,
+ MIPID02_NUM_SUPPLIES,
+ bridge->supplies);
+}
+
+static void mipid02_apply_reset(struct mipid02_dev *bridge)
+{
+ gpiod_set_value_cansleep(bridge->reset_gpio, 0);
+ usleep_range(5000, 10000);
+ gpiod_set_value_cansleep(bridge->reset_gpio, 1);
+ usleep_range(5000, 10000);
+ gpiod_set_value_cansleep(bridge->reset_gpio, 0);
+ usleep_range(5000, 10000);
+}
+
+static int mipid02_set_power_on(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret;
+
+ ret = clk_prepare_enable(bridge->xclk);
+ if (ret) {
+ dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
+ return ret;
+ }
+
+ ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
+ bridge->supplies);
+ if (ret) {
+ dev_err(&client->dev, "%s: failed to enable regulators\n",
+ __func__);
+ goto xclk_off;
+ }
+
+ if (bridge->reset_gpio) {
+ dev_dbg(&client->dev, "apply reset");
+ mipid02_apply_reset(bridge);
+ } else {
+ dev_dbg(&client->dev, "don't apply reset");
+ usleep_range(5000, 10000);
+ }
+
+ return 0;
+
+xclk_off:
+ clk_disable_unprepare(bridge->xclk);
+ return ret;
+}
+
+static void mipid02_set_power_off(struct mipid02_dev *bridge)
+{
+ regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
+ clk_disable_unprepare(bridge->xclk);
+}
+
+static int mipid02_detect(struct mipid02_dev *bridge)
+{
+ u8 reg;
+
+ /*
+ * There is no version registers. Just try to read register
+ * MIPID02_CLK_LANE_WR_REG1.
+ */
+ return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
+}
+
+static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
+ struct v4l2_subdev *subdev)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ struct v4l2_ctrl *ctrl;
+ u32 pixel_clock;
+ u32 bpp = bpp_from_code(bridge->fmt.code);
+
+ ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
+ if (!ctrl)
+ return 0;
+ pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
+
+ return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
+}
+
+/*
+ * We need to know link frequency to setup clk_lane_reg1 timings. Link frequency
+ * will be computed using connected device V4L2_CID_PIXEL_RATE, bit per pixel
+ * and number of lanes.
+ */
+static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_subdev *subdev = bridge->s_subdev;
+ u32 link_freq;
+
+ link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, subdev);
+ if (!link_freq) {
+ dev_err(&client->dev, "Failed to detect link frequency");
+ return -EINVAL;
+ }
+
+ dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
+ bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
+
+ return 0;
+}
+
+static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ bool *polarities = ep->bus.mipi_csi2.lane_polarities;
+
+ /* midid02 doesn't support clock lane remapping */
+ if (ep->bus.mipi_csi2.clock_lane != 0) {
+ dev_err(&client->dev, "clk lane must be map to lane 0\n");
+ return -EINVAL;
+ }
+ bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
+ bool are_lanes_swap, bool *polarities)
+{
+ bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
+
+ if (nb == 1 && are_lanes_swap)
+ return 0;
+
+ /*
+ * data lane 0 as pin swap polarity reversed compared to clock and
+ * data lane 1
+ */
+ if (!are_pin_swap)
+ bridge->r.data_lane0_reg1 = 1 << 1;
+ bridge->r.data_lane0_reg1 |= DATA_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
+ bool are_lanes_swap, bool *polarities)
+{
+ bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
+
+ if (nb == 1 && !are_lanes_swap)
+ return 0;
+
+ if (are_pin_swap)
+ bridge->r.data_lane1_reg1 = 1 << 1;
+ bridge->r.data_lane1_reg1 |= DATA_ENABLE;
+
+ return 0;
+}
+
+static int mipid02_configure_from_rx(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->rx;
+ bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
+ bool *polarities = ep->bus.mipi_csi2.lane_polarities;
+ int nb = ep->bus.mipi_csi2.num_data_lanes;
+ int ret;
+
+ ret = mipid02_configure_clk_lane(bridge);
+ if (ret)
+ return ret;
+
+ ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
+ polarities);
+ if (ret)
+ return ret;
+
+ ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
+ polarities);
+ if (ret)
+ return ret;
+
+ bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
+ bridge->r.mode_reg1 |= (nb - 1) << 1;
+
+ return mipid02_configure_from_rx_speed(bridge);
+}
+
+static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint *ep = &bridge->tx;
+
+ bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
+ bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
+ if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
+ bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
+ if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
+ bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
+
+ return 0;
+}
+
+static int mipid02_configure_from_code(struct mipid02_dev *bridge)
+{
+ u8 data_type;
+
+ bridge->r.data_id_rreg = 0;
+ data_type = data_type_from_code(bridge->fmt.code);
+ if (!data_type)
+ return -EINVAL;
+ bridge->r.data_id_rreg = data_type;
+
+ return 0;
+}
+
+static int mipid02_stream_disable(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret;
+
+ /* Disable all lanes */
+ ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
+ if (ret)
+ goto error;
+error:
+ if (ret)
+ dev_err(&client->dev, "failed to stream off %d", ret);
+
+ return ret;
+}
+
+static int mipid02_stream_enable(struct mipid02_dev *bridge)
+{
+ struct i2c_client *client = bridge->i2c_client;
+ int ret = -EINVAL;
+
+ if (!bridge->s_subdev)
+ goto error;
+
+ memset(&bridge->r, 0, sizeof(bridge->r));
+ /* build registers content */
+ ret = mipid02_configure_from_rx(bridge);
+ if (ret)
+ goto error;
+ ret = mipid02_configure_from_tx(bridge);
+ if (ret)
+ goto error;
+ ret = mipid02_configure_from_code(bridge);
+ if (ret)
+ goto error;
+
+ /* write mipi registers */
+ ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
+ bridge->r.clk_lane_reg1);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
+ bridge->r.data_lane0_reg1);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
+ DATA_MIPI_CSI);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
+ bridge->r.data_lane1_reg1);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
+ DATA_MIPI_CSI);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_MODE_REG1,
+ MODE_NO_BYPASS | bridge->r.mode_reg1);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_MODE_REG2,
+ bridge->r.mode_reg2);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
+ bridge->r.data_id_rreg);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
+ SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
+ bridge->r.pix_width_ctrl);
+ if (ret)
+ goto error;
+ ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
+ bridge->r.pix_width_ctrl_emb);
+ if (ret)
+ goto error;
+
+ return 0;
+
+error:
+ dev_err(&client->dev, "failed to stream on %d", ret);
+ mipid02_stream_disable(bridge);
+
+ return ret;
+}
+
+static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int ret = 0;
+
+ dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
+ enable, bridge->streaming);
+ mutex_lock(&bridge->lock);
+
+ if (bridge->streaming == enable)
+ goto out;
+
+ ret = enable ? mipid02_stream_enable(bridge) :
+ mipid02_stream_disable(bridge);
+ if (!ret)
+ bridge->streaming = enable;
+
+out:
+ dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
+ bridge->streaming, ret);
+ mutex_unlock(&bridge->lock);
+
+ return ret;
+}
+
+static int mipid02_enum_mbus_code(struct v4l2_subdev *sd,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_mbus_code_enum *code)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ int ret = 0;
+
+ switch (code->pad) {
+ case MIPID02_SINK_0:
+ if (code->index >= ARRAY_SIZE(mipid02_supported_fmt_codes))
+ ret = -EINVAL;
+ else
+ code->code = mipid02_supported_fmt_codes[code->index];
+ break;
+ case MIPID02_SOURCE:
+ if (code->index == 0)
+ code->code = serial_to_parallel_code(bridge->fmt.code);
+ else
+ ret = -EINVAL;
+ break;
+ default:
+ ret = -EINVAL;
+ }
+
+ return ret;
+}
+
+static int mipid02_get_fmt(struct v4l2_subdev *sd,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_format *format)
+{
+ struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct i2c_client *client = bridge->i2c_client;
+ struct v4l2_mbus_framefmt *fmt;
+
+ dev_dbg(&client->dev, "%s probe %d", __func__, format->pad);
+
+ if (format->pad >= MIPID02_PAD_NB)
+ return -EINVAL;
+ /* second CSI-2 pad not yet supported */
+ if (format->pad == MIPID02_SINK_1)
+ return -EINVAL;
+
+ if (format->which == V4L2_SUBDEV_FORMAT_TRY)
+ fmt = v4l2_subdev_get_try_format(&bridge->sd, cfg, format->pad);
+ else
+ fmt = &bridge->fmt;
+
+ mutex_lock(&bridge->lock);
+
+ *mbus_fmt = *fmt;
+ /* code may need to be converted for source */
+ if (format->pad == MIPID02_SOURCE)
+ mbus_fmt->code = serial_to_parallel_code(mbus_fmt->code);
+
+ mutex_unlock(&bridge->lock);
+
+ return 0;
+}
+
+static void mipid02_set_fmt_source(struct v4l2_subdev *sd,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_format *format)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+
+ /* source pad mirror active sink pad */
+ format->format = bridge->fmt;
+ /* but code may need to be converted */
+ format->format.code = serial_to_parallel_code(format->format.code);
+
+ /* only apply format for V4L2_SUBDEV_FORMAT_TRY case */
+ if (format->which != V4L2_SUBDEV_FORMAT_TRY)
+ return;
+
+ *v4l2_subdev_get_try_format(sd, cfg, format->pad) = format->format;
+}
+
+static void mipid02_set_fmt_sink(struct v4l2_subdev *sd,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_format *format)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct v4l2_mbus_framefmt *fmt;
+
+ format->format.code = get_fmt_code(format->format.code);
+
+ if (format->which == V4L2_SUBDEV_FORMAT_TRY)
+ fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
+ else
+ fmt = &bridge->fmt;
+
+ *fmt = format->format;
+}
+
+static int mipid02_set_fmt(struct v4l2_subdev *sd,
+ struct v4l2_subdev_pad_config *cfg,
+ struct v4l2_subdev_format *format)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int ret = 0;
+
+ dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
+
+ if (format->pad >= MIPID02_PAD_NB)
+ return -EINVAL;
+ /* second CSI-2 pad not yet supported */
+ if (format->pad == MIPID02_SINK_1)
+ return -EINVAL;
+
+ mutex_lock(&bridge->lock);
+
+ if (bridge->streaming) {
+ ret = -EBUSY;
+ goto error;
+ }
+
+ if (format->pad == MIPID02_SOURCE)
+ mipid02_set_fmt_source(sd, cfg, format);
+ else
+ mipid02_set_fmt_sink(sd, cfg, format);
+
+error:
+ mutex_unlock(&bridge->lock);
+
+ return ret;
+}
+
+static const struct v4l2_subdev_video_ops mipid02_video_ops = {
+ .s_stream = mipid02_s_stream,
+};
+
+static const struct v4l2_subdev_pad_ops mipid02_pad_ops = {
+ .enum_mbus_code = mipid02_enum_mbus_code,
+ .get_fmt = mipid02_get_fmt,
+ .set_fmt = mipid02_set_fmt,
+};
+
+static const struct v4l2_subdev_ops mipid02_subdev_ops = {
+ .video = &mipid02_video_ops,
+ .pad = &mipid02_pad_ops,
+};
+
+static const struct media_entity_operations mipid02_subdev_entity_ops = {
+ .link_validate = v4l2_subdev_link_validate,
+};
+
+static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
+ struct v4l2_subdev *s_subdev,
+ struct v4l2_async_subdev *asd)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
+ struct i2c_client *client = bridge->i2c_client;
+ int source_pad;
+ int ret;
+
+ dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
+
+ source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
+ s_subdev->fwnode,
+ MEDIA_PAD_FL_SOURCE);
+ if (source_pad < 0) {
+ dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
+ s_subdev->name);
+ return source_pad;
+ }
+
+ ret = media_create_pad_link(&s_subdev->entity, source_pad,
+ &bridge->sd.entity, 0,
+ MEDIA_LNK_FL_ENABLED |
+ MEDIA_LNK_FL_IMMUTABLE);
+ if (ret) {
+ dev_err(&client->dev, "Couldn't create media link %d", ret);
+ return ret;
+ }
+
+ bridge->s_subdev = s_subdev;
+
+ return 0;
+}
+
+static void mipid02_async_unbind(struct v4l2_async_notifier *notifier,
+ struct v4l2_subdev *s_subdev,
+ struct v4l2_async_subdev *asd)
+{
+ struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
+
+ bridge->s_subdev = NULL;
+}
+
+static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
+ .bound = mipid02_async_bound,
+ .unbind = mipid02_async_unbind,
+};
+
+static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_CSI2_DPHY };
+ struct i2c_client *client = bridge->i2c_client;
+ struct device_node *ep_node;
+ int ret;
+
+ /* parse rx (endpoint 0) */
+ ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
+ 0, 0);
+ if (!ep_node) {
+ dev_err(&client->dev, "unable to find port0 ep");
+ ret = -EINVAL;
+ goto error;
+ }
+
+ ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
+ if (ret) {
+ dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
+ ret);
+ goto error_of_node_put;
+ }
+
+ /* do some sanity checks */
+ if (ep.bus.mipi_csi2.num_data_lanes > 2) {
+ dev_err(&client->dev, "max supported data lanes is 2 / got %d",
+ ep.bus.mipi_csi2.num_data_lanes);
+ ret = -EINVAL;
+ goto error_v4l2_fwnode_endpoint_free;
+ }
+
+ /* register it for later use */
+ bridge->rx = ep;
+ v4l2_fwnode_endpoint_free(&ep);
+
+ /* register async notifier so we get noticed when sensor is connected */
+ bridge->asd.match.fwnode =
+ fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
+ bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
+ of_node_put(ep_node);
+
+ v4l2_async_notifier_init(&bridge->notifier);
+ ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
+ if (ret) {
+ dev_err(&client->dev, "fail to register asd to notifier %d",
+ ret);
+ goto error_fwnode_handle_put;
+ }
+ bridge->notifier.ops = &mipid02_notifier_ops;
+
+ ret = v4l2_async_subdev_notifier_register(&bridge->sd,
+ &bridge->notifier);
+ if (ret)
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+
+ return ret;
+
+error_v4l2_fwnode_endpoint_free:
+ v4l2_fwnode_endpoint_free(&ep);
+error_of_node_put:
+ of_node_put(ep_node);
+error:
+
+ return ret;
+
+error_fwnode_handle_put:
+ fwnode_handle_put(bridge->asd.match.fwnode);
+
+ return ret;
+}
+
+static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
+{
+ struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_PARALLEL };
+ struct i2c_client *client = bridge->i2c_client;
+ struct device_node *ep_node;
+ int ret;
+
+ /* parse tx (endpoint 2) */
+ ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
+ 2, 0);
+ if (!ep_node) {
+ dev_err(&client->dev, "unable to find port1 ep");
+ ret = -EINVAL;
+ goto error;
+ }
+
+ ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
+ if (ret) {
+ dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
+ goto error_of_node_put;
+ }
+
+ of_node_put(ep_node);
+ bridge->tx = ep;
+
+ return 0;
+
+error_of_node_put:
+ of_node_put(ep_node);
+error:
+
+ return -EINVAL;
+}
+
+static int mipid02_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ struct device *dev = &client->dev;
+ struct mipid02_dev *bridge;
+ u32 clk_freq;
+ int ret;
+
+ bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
+ if (!bridge)
+ return -ENOMEM;
+
+ init_format(&bridge->fmt);
+
+ bridge->i2c_client = client;
+ v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
+
+ /* got and check clock */
+ bridge->xclk = devm_clk_get(dev, "xclk");
+ if (IS_ERR(bridge->xclk)) {
+ dev_err(dev, "failed to get xclk\n");
+ return PTR_ERR(bridge->xclk);
+ }
+
+ clk_freq = clk_get_rate(bridge->xclk);
+ if (clk_freq < 6000000 || clk_freq > 27000000) {
+ dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
+ clk_freq);
+ return -EINVAL;
+ }
+
+ bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
+ GPIOD_OUT_HIGH);
+
+ ret = mipid02_get_regulators(bridge);
+ if (ret) {
+ dev_err(dev, "failed to get regulators %d", ret);
+ return ret;
+ }
+
+ mutex_init(&bridge->lock);
+ bridge->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+ bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
+ bridge->sd.entity.ops = &mipid02_subdev_entity_ops;
+ bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
+ bridge->pad[1].flags = MEDIA_PAD_FL_SINK;
+ bridge->pad[2].flags = MEDIA_PAD_FL_SOURCE;
+ ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
+ bridge->pad);
+ if (ret) {
+ dev_err(&client->dev, "pads init failed %d", ret);
+ return ret;
+ }
+
+ /* enable clock, power and reset device if available */
+ ret = mipid02_set_power_on(bridge);
+ if (ret)
+ goto entity_cleanup;
+
+ ret = mipid02_detect(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to detect mipid02 %d", ret);
+ goto power_off;
+ }
+
+ ret = mipid02_parse_tx_ep(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to parse tx %d", ret);
+ goto power_off;
+ }
+
+ ret = mipid02_parse_rx_ep(bridge);
+ if (ret) {
+ dev_err(&client->dev, "failed to parse rx %d", ret);
+ goto power_off;
+ }
+
+ ret = v4l2_async_register_subdev(&bridge->sd);
+ if (ret < 0) {
+ dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
+ ret);
+ goto unregister_notifier;
+ }
+
+ dev_info(&client->dev, "mipid02 device probe successfully");
+
+ return 0;
+
+unregister_notifier:
+ v4l2_async_notifier_unregister(&bridge->notifier);
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+power_off:
+ mipid02_set_power_off(bridge);
+entity_cleanup:
+ media_entity_cleanup(&bridge->sd.entity);
+
+ return ret;
+}
+
+static int mipid02_remove(struct i2c_client *client)
+{
+ struct v4l2_subdev *sd = i2c_get_clientdata(client);
+ struct mipid02_dev *bridge = to_mipid02_dev(sd);
+
+ v4l2_async_notifier_unregister(&bridge->notifier);
+ v4l2_async_notifier_cleanup(&bridge->notifier);
+ v4l2_async_unregister_subdev(&bridge->sd);
+ mipid02_set_power_off(bridge);
+ media_entity_cleanup(&bridge->sd.entity);
+
+ return 0;
+}
+
+static const struct i2c_device_id mipid02_id[] = {
+ { "st-mipid02", 0},
+ {},
+};
+MODULE_DEVICE_TABLE(i2c, mipid02_id);
+
+static const struct of_device_id mipid02_dt_ids[] = {
+ { .compatible = "st,st-mipid02" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
+
+static struct i2c_driver mipid02_i2c_driver = {
+ .driver = {
+ .name = "st-mipid02",
+ .of_match_table = mipid02_dt_ids,
+ },
+ .id_table = mipid02_id,
+ .probe = mipid02_probe,
+ .remove = mipid02_remove,
+};
+
+module_i2c_driver(mipid02_i2c_driver);
+
+MODULE_AUTHOR("Mickael Guene <[email protected]>");
+MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
+MODULE_LICENSE("GPL v2");
--
2.7.4


2019-03-28 17:44:45

by Rob Herring (Arm)

[permalink] [raw]
Subject: Re: [PATCH v4 1/2] dt-bindings: Document MIPID02 bindings

On Wed, 27 Mar 2019 10:55:43 +0100, Mickael Guene wrote:
> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
> bridge.
>
> Signed-off-by: Mickael Guene <[email protected]>
> ---
>
> Changes in v4:
> - Fix and clarify endpoints properties documentation
>
> Changes in v3: None
> Changes in v2:
> - Add precision about first CSI-2 port data rate
> - Document endpoints supported properties
> - Rename 'mipid02@14' into generic 'csi2rx@14' in example
>
> .../bindings/media/i2c/st,st-mipid02.txt | 82 ++++++++++++++++++++++
> MAINTAINERS | 7 ++
> 2 files changed, 89 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>

Reviewed-by: Rob Herring <[email protected]>

2019-04-06 10:39:56

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v4 1/2] dt-bindings: Document MIPID02 bindings

Hi Mickael,

On Wed, Mar 27, 2019 at 10:55:43AM +0100, Mickael Guene wrote:
> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
> bridge.
>
> Signed-off-by: Mickael Guene <[email protected]>
> ---
>
> Changes in v4:
> - Fix and clarify endpoints properties documentation
>
> Changes in v3: None
> Changes in v2:
> - Add precision about first CSI-2 port data rate
> - Document endpoints supported properties
> - Rename 'mipid02@14' into generic 'csi2rx@14' in example
>
> .../bindings/media/i2c/st,st-mipid02.txt | 82 ++++++++++++++++++++++
> MAINTAINERS | 7 ++
> 2 files changed, 89 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>
> diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> new file mode 100644
> index 0000000..754a175
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> @@ -0,0 +1,82 @@
> +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
> +
> +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
> +time. Active port input stream will be de-serialized and its content outputted
> +through PARALLEL output port.
> +CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
> +input port is a single lane 800Mbps. Both ports support clock and data lane
> +polarity swap. First port also supports data lane swap.
> +PARALLEL output port has a maximum width of 12 bits.
> +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
> +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
> +
> +Required Properties:
> +- compatible: should be "st,st-mipid02"

s/should/shall/ overall; other values aren't really valid in any of the
cases.

--
Sakari Ailus

2019-04-06 10:40:39

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v4 1/2] dt-bindings: Document MIPID02 bindings

On Sat, Apr 06, 2019 at 01:35:25PM +0300, Sakari Ailus wrote:
> Hi Mickael,
>
> On Wed, Mar 27, 2019 at 10:55:43AM +0100, Mickael Guene wrote:
> > This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
> > bridge.
> >
> > Signed-off-by: Mickael Guene <[email protected]>
> > ---
> >
> > Changes in v4:
> > - Fix and clarify endpoints properties documentation
> >
> > Changes in v3: None
> > Changes in v2:
> > - Add precision about first CSI-2 port data rate
> > - Document endpoints supported properties
> > - Rename 'mipid02@14' into generic 'csi2rx@14' in example
> >
> > .../bindings/media/i2c/st,st-mipid02.txt | 82 ++++++++++++++++++++++
> > MAINTAINERS | 7 ++
> > 2 files changed, 89 insertions(+)
> > create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> >
> > diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> > new file mode 100644
> > index 0000000..754a175
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
> > @@ -0,0 +1,82 @@
> > +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
> > +
> > +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
> > +time. Active port input stream will be de-serialized and its content outputted
> > +through PARALLEL output port.
> > +CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
> > +input port is a single lane 800Mbps. Both ports support clock and data lane
> > +polarity swap. First port also supports data lane swap.
> > +PARALLEL output port has a maximum width of 12 bits.
> > +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
> > +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
> > +
> > +Required Properties:
> > +- compatible: should be "st,st-mipid02"
>
> s/should/shall/ overall; other values aren't really valid in any of the
> cases.

As this is the only change and it's trivial, I can do that as well. Let me
know if that works for you.

--
Sakari Ailus

2019-04-06 11:04:00

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

On Tue, Mar 26, 2019 at 02:36:41PM +0000, Mickael GUENE wrote:
> Sakari,
>
> >> +static int bpp_from_code(__u32 code)
> >> +{
> >> + switch (code) {
> >> + case MEDIA_BUS_FMT_SBGGR8_1X8:
> >> + case MEDIA_BUS_FMT_SGBRG8_1X8:
> >> + case MEDIA_BUS_FMT_SGRBG8_1X8:
> >> + case MEDIA_BUS_FMT_SRGGB8_1X8:
> >> + return 8;
> >> + case MEDIA_BUS_FMT_SBGGR10_1X10:
> >> + case MEDIA_BUS_FMT_SGBRG10_1X10:
> >> + case MEDIA_BUS_FMT_SGRBG10_1X10:
> >> + case MEDIA_BUS_FMT_SRGGB10_1X10:
> >> + return 10;
> >> + case MEDIA_BUS_FMT_SBGGR12_1X12:
> >> + case MEDIA_BUS_FMT_SGBRG12_1X12:
> >> + case MEDIA_BUS_FMT_SGRBG12_1X12:
> >> + case MEDIA_BUS_FMT_SRGGB12_1X12:
> >> + return 12;
> >> + case MEDIA_BUS_FMT_UYVY8_2X8:
> >
> > This is good for the parallel bus, but on CSI-2 side you should have
> > MEDIA_BUS_FMT_UYVY8_1X16 instead. This isn't technically correct for a
> > serial bus, but the custom is to use the one sample / pixel formats on the
> > serial busses.
> >
> Should MEDIA_BUS_FMT_BGR888_1X24 be something like
> MEDIA_BUS_FMT_BGR888_3X8 for parallel output bus ?

Good point. Yes.

Could you add that format to
Documentation/media/uapi/v4l/subdev-formats.rst, please, in a separate
patch?

--
Regards,

Sakari Ailus
[email protected]

2019-04-08 06:19:40

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Sakari,

On 4/6/19 13:01, Sakari Ailus wrote:
> On Tue, Mar 26, 2019 at 02:36:41PM +0000, Mickael GUENE wrote:
>> Sakari,
>>
>>>> +static int bpp_from_code(__u32 code)
>>>> +{
>>>> + switch (code) {
>>>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>>>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>>>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>>>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>>>> + return 8;
>>>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>>>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>>>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>>>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>>>> + return 10;
>>>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>>>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>>>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>>>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>>>> + return 12;
>>>> + case MEDIA_BUS_FMT_UYVY8_2X8:
>>>
>>> This is good for the parallel bus, but on CSI-2 side you should have
>>> MEDIA_BUS_FMT_UYVY8_1X16 instead. This isn't technically correct for a
>>> serial bus, but the custom is to use the one sample / pixel formats on the
>>> serial busses.
>>>
>> Should MEDIA_BUS_FMT_BGR888_1X24 be something like
>> MEDIA_BUS_FMT_BGR888_3X8 for parallel output bus ?
>
> Good point. Yes.
>
> Could you add that format to
> Documentation/media/uapi/v4l/subdev-formats.rst, please, in a separate
> patch?
>
I also need to add it to include/uapi/linux/media-bus-format.h I
suppose ?
I was also wondering if I should add MEDIA_BUS_FMT_BGR888_3X8_BE and
MEDIA_BUS_FMT_BGR888_3X8_LE instead ?

So if you are ok with the above I will add MEDIA_BUS_FMT_BGR888_3X8 to
subdev-formats.rs and media-bus-format.h as a first parch of my serie.

Mickael

2019-04-08 06:21:46

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v4 1/2] dt-bindings: Document MIPID02 bindings

Hi Sakari,

On 4/6/19 12:38, Sakari Ailus wrote:
> On Sat, Apr 06, 2019 at 01:35:25PM +0300, Sakari Ailus wrote:
>> Hi Mickael,
>>
>> On Wed, Mar 27, 2019 at 10:55:43AM +0100, Mickael Guene wrote:
>>> This adds documentation of device tree for MIPID02 CSI-2 to PARALLEL
>>> bridge.
>>>
>>> Signed-off-by: Mickael Guene <[email protected]>
>>> ---
>>>
>>> Changes in v4:
>>> - Fix and clarify endpoints properties documentation
>>>
>>> Changes in v3: None
>>> Changes in v2:
>>> - Add precision about first CSI-2 port data rate
>>> - Document endpoints supported properties
>>> - Rename 'mipid02@14' into generic 'csi2rx@14' in example
>>>
>>> .../bindings/media/i2c/st,st-mipid02.txt | 82 ++++++++++++++++++++++
>>> MAINTAINERS | 7 ++
>>> 2 files changed, 89 insertions(+)
>>> create mode 100644 Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>>>
>>> diff --git a/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>>> new file mode 100644
>>> index 0000000..754a175
>>> --- /dev/null
>>> +++ b/Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>>> @@ -0,0 +1,82 @@
>>> +STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge
>>> +
>>> +MIPID02 has two CSI-2 input ports, only one of those ports can be active at a
>>> +time. Active port input stream will be de-serialized and its content outputted
>>> +through PARALLEL output port.
>>> +CSI-2 first input port is a dual lane 800Mbps per lane whereas CSI-2 second
>>> +input port is a single lane 800Mbps. Both ports support clock and data lane
>>> +polarity swap. First port also supports data lane swap.
>>> +PARALLEL output port has a maximum width of 12 bits.
>>> +Supported formats are RAW6, RAW7, RAW8, RAW10, RAW12, RGB565, RGB888, RGB444,
>>> +YUV420 8-bit, YUV422 8-bit and YUV420 10-bit.
>>> +
>>> +Required Properties:
>>> +- compatible: should be "st,st-mipid02"
>>
>> s/should/shall/ overall; other values aren't really valid in any of the
>> cases.
>
> As this is the only change and it's trivial, I can do that as well. Let me
> know if that works for you.
>
I prefer to keep it with my patch serie if you are ok ?

Mickael

2019-04-08 08:23:28

by Mickael GUENE

[permalink] [raw]
Subject: Re: [PATCH v4 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Sakari,

On 4/6/19 12:56, Sakari Ailus wrote:
> Hi Mickael,
>
> On Wed, Mar 27, 2019 at 10:55:44AM +0100, Mickael Guene wrote:
>> This V4L2 subdev driver enables STMicroelectronics MIPID02 device.
>>
>> Signed-off-by: Mickael Guene <[email protected]>
>> ---
>>
>> Changes in v4:
>> - Add support of enum_mbus_code
>> - Only use V4L2_CID_PIXEL_RATE to compute link speed
>> - Use MEDIA_BUS_FMT_UYVY8_1X16 instead of MEDIA_BUS_FMT_UYVY8_2X8 for CSI-2 link
>> - Fix miscellaneous typos
>> - Fix wrong code behavior for set_fmt and get_fmt
>>
>> Changes in v3:
>> - Fix potential wrong error code for mipid02_stream_disable and mipid02_stream_enable
>> - Remove useless memset for ep in mipid02_parse_rx_ep and mipid02_parse_tx_ep
>> - Add second CSI-2 input pad even if it's not yet supported
>> - Add support of get_fmt, set_fmt and link_validate and only access subdev connected to mipid02
>>
>> Changes in v2:
>> - Merge MAINTAINERS patch 3 into patch 1 and 2
>> - Fix line too long in Kconfig
>> - Add missing delay after reset release
>> - Various style fixes
>> - Fix mipid02_stream_enable returning no error when mipid02_find_sensor failed
>>
>> MAINTAINERS | 1 +
>> drivers/media/i2c/Kconfig | 14 +
>> drivers/media/i2c/Makefile | 1 +
>> drivers/media/i2c/st-mipid02.c | 1043 ++++++++++++++++++++++++++++++++++++++++
>> 4 files changed, 1059 insertions(+)
>> create mode 100644 drivers/media/i2c/st-mipid02.c
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 74da99d..a14fe81 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -14673,6 +14673,7 @@ M: Mickael Guene <[email protected]>
>> L: [email protected]
>> T: git git://linuxtv.org/media_tree.git
>> S: Maintained
>> +F: drivers/media/i2c/st-mipid02.c
>> F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.txt
>>
>> ST STM32 I2C/SMBUS DRIVER
>> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
>> index 1a1746b..2dc9e15 100644
>> --- a/drivers/media/i2c/Kconfig
>> +++ b/drivers/media/i2c/Kconfig
>> @@ -1135,6 +1135,20 @@ config VIDEO_I2C
>> To compile this driver as a module, choose M here: the
>> module will be called video-i2c
>>
>> +config VIDEO_ST_MIPID02
>> + tristate "STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge"
>> + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
>> + depends on MEDIA_CAMERA_SUPPORT
>> + depends on MEDIA_CONTROLLER
>
> VIDEO_V4L2_SUBDEV_API already requires MEDIA_CONTROLLER, so
> MEDIA_CONTROLLER here is redundant.
>
ok I will remove it.
>> + select V4L2_FWNODE
>> + help
>> + Support for STMicroelectronics MIPID02 CSI-2 to PARALLEL bridge.
>> + It is used to allow usage of CSI-2 sensor with PARALLEL port
>> + controller.
>> +
>> + To compile this driver as a module, choose M here: the
>> + module will be called st-mipid02.
>> +
>> endmenu
>>
>> endif
>> diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
>> index a64fca8..d8ad9da 100644
>> --- a/drivers/media/i2c/Makefile
>> +++ b/drivers/media/i2c/Makefile
>> @@ -113,5 +113,6 @@ obj-$(CONFIG_VIDEO_IMX258) += imx258.o
>> obj-$(CONFIG_VIDEO_IMX274) += imx274.o
>> obj-$(CONFIG_VIDEO_IMX319) += imx319.o
>> obj-$(CONFIG_VIDEO_IMX355) += imx355.o
>> +obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o
>>
>> obj-$(CONFIG_SDR_MAX2175) += max2175.o
>> diff --git a/drivers/media/i2c/st-mipid02.c b/drivers/media/i2c/st-mipid02.c
>> new file mode 100644
>> index 0000000..1d09bae
>> --- /dev/null
>> +++ b/drivers/media/i2c/st-mipid02.c
>> @@ -0,0 +1,1043 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
>> + *
>> + * Copyright (C) STMicroelectronics SA 2019
>> + * Authors: Mickael Guene <[email protected]>
>> + * for STMicroelectronics.
>> + *
>> + *
>> + */
>> +
>> +#include <linux/clk.h>
>> +#include <linux/delay.h>
>> +#include <linux/gpio/consumer.h>
>> +#include <linux/i2c.h>
>> +#include <linux/module.h>
>> +#include <linux/of_graph.h>
>> +#include <linux/regulator/consumer.h>
>> +#include <media/v4l2-async.h>
>> +#include <media/v4l2-ctrls.h>
>> +#include <media/v4l2-device.h>
>> +#include <media/v4l2-fwnode.h>
>> +#include <media/v4l2-subdev.h>
>> +
>> +#define MIPID02_CLK_LANE_WR_REG1 0x01
>> +#define MIPID02_CLK_LANE_REG1 0x02
>> +#define MIPID02_CLK_LANE_REG3 0x04
>> +#define MIPID02_DATA_LANE0_REG1 0x05
>> +#define MIPID02_DATA_LANE0_REG2 0x06
>> +#define MIPID02_DATA_LANE1_REG1 0x09
>> +#define MIPID02_DATA_LANE1_REG2 0x0a
>> +#define MIPID02_MODE_REG1 0x14
>> +#define MIPID02_MODE_REG2 0x15
>> +#define MIPID02_DATA_ID_RREG 0x17
>> +#define MIPID02_DATA_SELECTION_CTRL 0x19
>> +#define MIPID02_PIX_WIDTH_CTRL 0x1e
>> +#define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f
>> +
>> +/* Bits definition for MIPID02_CLK_LANE_REG1 */
>> +#define CLK_ENABLE BIT(0)
>> +/* Bits definition for MIPID02_CLK_LANE_REG3 */
>> +#define CLK_MIPI_CSI BIT(1)
>> +/* Bits definition for MIPID02_DATA_LANE0_REG1 */
>> +#define DATA_ENABLE BIT(0)
>> +/* Bits definition for MIPID02_DATA_LANEx_REG2 */
>> +#define DATA_MIPI_CSI BIT(0)
>> +/* Bits definition for MIPID02_MODE_REG1 */
>> +#define MODE_DATA_SWAP BIT(2)
>> +#define MODE_NO_BYPASS BIT(6)
>> +/* Bits definition for MIPID02_MODE_REG2 */
>> +#define MODE_HSYNC_ACTIVE_HIGH BIT(1)
>> +#define MODE_VSYNC_ACTIVE_HIGH BIT(2)
>> +/* Bits definition for MIPID02_DATA_SELECTION_CTRL */
>> +#define SELECTION_MANUAL_DATA BIT(2)
>> +#define SELECTION_MANUAL_WIDTH BIT(3)
>> +
>> +static const u32 mipid02_supported_fmt_codes[] = {
>> + MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
>> + MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
>> + MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
>> + MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
>> + MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SGBRG12_1X12,
>> + MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SRGGB12_1X12,
>> + MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_BGR888_1X24
>> +};
>> +
>> +/* regulator supplies */
>> +static const char * const mipid02_supply_name[] = {
>> + "VDDE", /* 1.8V digital I/O supply */
>> + "VDDIN", /* 1V8 voltage regulator supply */
>> +};
>> +
>> +#define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name)
>> +
>> +#define MIPID02_SINK_0 0
>> +#define MIPID02_SINK_1 1
>> +#define MIPID02_SOURCE 2
>> +#define MIPID02_PAD_NB 3
>> +
>> +struct mipid02_dev {
>> + struct i2c_client *i2c_client;
>> + struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES];
>> + struct v4l2_subdev sd;
>> + struct media_pad pad[MIPID02_PAD_NB];
>> + struct clk *xclk;
>> + struct gpio_desc *reset_gpio;
>> + /* endpoints info */
>> + struct v4l2_fwnode_endpoint rx;
>> + u64 link_frequency;
>> + struct v4l2_fwnode_endpoint tx;
>> + /* remote source */
>> + struct v4l2_async_subdev asd;
>> + struct v4l2_async_notifier notifier;
>> + struct v4l2_subdev *s_subdev;
>> + /* registers */
>> + struct {
>> + u8 clk_lane_reg1;
>> + u8 data_lane0_reg1;
>> + u8 data_lane1_reg1;
>> + u8 mode_reg1;
>> + u8 mode_reg2;
>> + u8 data_id_rreg;
>> + u8 pix_width_ctrl;
>> + u8 pix_width_ctrl_emb;
>> + } r;
>> + /* lock to protect all members below */
>> + struct mutex lock;
>> + bool streaming;
>> + struct v4l2_mbus_framefmt fmt;
>> +};
>> +
>> +static int bpp_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 8;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 10;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 12;
>> + case MEDIA_BUS_FMT_UYVY8_1X16:
>> + return 16;
>> + case MEDIA_BUS_FMT_BGR888_1X24:
>> + return 24;
>> + default:
>> + return 0;
>> + }
>> +}
>> +
>> +static u8 data_type_from_code(__u32 code)
>> +{
>> + switch (code) {
>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
>> + return 0x2a;
>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
>> + return 0x2b;
>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
>> + return 0x2c;
>> + case MEDIA_BUS_FMT_UYVY8_1X16:
>> + return 0x1e;
>> + case MEDIA_BUS_FMT_BGR888_1X24:
>> + return 0x24;
>> + default:
>> + return 0;
>> + }
>> +}
>> +
>> +static void init_format(struct v4l2_mbus_framefmt *fmt)
>> +{
>> + fmt->code = MEDIA_BUS_FMT_SBGGR8_1X8;
>> + fmt->field = V4L2_FIELD_NONE;
>> + fmt->colorspace = V4L2_COLORSPACE_SRGB;
>> + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
>> + fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
>> + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
>> + fmt->width = 640;
>> + fmt->height = 480;
>> +}
>> +
>> +static __u32 get_fmt_code(__u32 code)
>> +{
>> + unsigned int i;
>> +
>> + for (i = 0; i < ARRAY_SIZE(mipid02_supported_fmt_codes); i++) {
>> + if (code == mipid02_supported_fmt_codes[i])
>> + return code;
>> + }
>> +
>> + return mipid02_supported_fmt_codes[0];
>> +}
>> +
>> +static __u32 serial_to_parallel_code(__u32 serial)
>> +{
>> + if (serial == MEDIA_BUS_FMT_UYVY8_1X16)
>> + return MEDIA_BUS_FMT_UYVY8_2X8;
>> +
>> + return serial;
>> +}
>> +
>> +static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd)
>> +{
>> + return container_of(sd, struct mipid02_dev, sd);
>> +}
>> +
>> +static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct i2c_msg msg[2];
>> + u8 buf[2];
>> + int ret;
>> +
>> + buf[0] = reg >> 8;
>> + buf[1] = reg & 0xff;
>> +
>> + msg[0].addr = client->addr;
>> + msg[0].flags = client->flags;
>> + msg[0].buf = buf;
>> + msg[0].len = sizeof(buf);
>> +
>> + msg[1].addr = client->addr;
>> + msg[1].flags = client->flags | I2C_M_RD;
>> + msg[1].buf = val;
>> + msg[1].len = 1;
>> +
>> + ret = i2c_transfer(client->adapter, msg, 2);
>> + if (ret < 0) {
>> + dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n",
>> + __func__, client->addr, reg, ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct i2c_msg msg;
>> + u8 buf[3];
>> + int ret;
>> +
>> + buf[0] = reg >> 8;
>> + buf[1] = reg & 0xff;
>> + buf[2] = val;
>> +
>> + msg.addr = client->addr;
>> + msg.flags = client->flags;
>> + msg.buf = buf;
>> + msg.len = sizeof(buf);
>> +
>> + ret = i2c_transfer(client->adapter, &msg, 1);
>> + if (ret < 0) {
>> + dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n",
>> + __func__, reg, ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_get_regulators(struct mipid02_dev *bridge)
>> +{
>> + unsigned int i;
>> +
>> + for (i = 0; i < MIPID02_NUM_SUPPLIES; i++)
>> + bridge->supplies[i].supply = mipid02_supply_name[i];
>> +
>> + return devm_regulator_bulk_get(&bridge->i2c_client->dev,
>> + MIPID02_NUM_SUPPLIES,
>> + bridge->supplies);
>> +}
>> +
>> +static void mipid02_apply_reset(struct mipid02_dev *bridge)
>> +{
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
>> + usleep_range(5000, 10000);
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 1);
>> + usleep_range(5000, 10000);
>> + gpiod_set_value_cansleep(bridge->reset_gpio, 0);
>> + usleep_range(5000, 10000);
>> +}
>> +
>> +static int mipid02_set_power_on(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret;
>> +
>> + ret = clk_prepare_enable(bridge->xclk);
>> + if (ret) {
>> + dev_err(&client->dev, "%s: failed to enable clock\n", __func__);
>> + return ret;
>> + }
>> +
>> + ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES,
>> + bridge->supplies);
>> + if (ret) {
>> + dev_err(&client->dev, "%s: failed to enable regulators\n",
>> + __func__);
>> + goto xclk_off;
>> + }
>> +
>> + if (bridge->reset_gpio) {
>> + dev_dbg(&client->dev, "apply reset");
>> + mipid02_apply_reset(bridge);
>> + } else {
>> + dev_dbg(&client->dev, "don't apply reset");
>> + usleep_range(5000, 10000);
>> + }
>> +
>> + return 0;
>> +
>> +xclk_off:
>> + clk_disable_unprepare(bridge->xclk);
>> + return ret;
>> +}
>> +
>> +static void mipid02_set_power_off(struct mipid02_dev *bridge)
>> +{
>> + regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies);
>> + clk_disable_unprepare(bridge->xclk);
>> +}
>> +
>> +static int mipid02_detect(struct mipid02_dev *bridge)
>> +{
>> + u8 reg;
>> +
>> + /*
>> + * There is no version registers. Just try to read register
>> + * MIPID02_CLK_LANE_WR_REG1.
>> + */
>> + return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, &reg);
>> +}
>> +
>> +static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge,
>> + struct v4l2_subdev *subdev)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + struct v4l2_ctrl *ctrl;
>> + u32 pixel_clock;
>> + u32 bpp = bpp_from_code(bridge->fmt.code);
>> +
>> + ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
>> + if (!ctrl)
>> + return 0;
>> + pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
>> +
>> + return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes);
>> +}
>> +
>> +/*
>> + * We need to know link frequency to setup clk_lane_reg1 timings. Link frequency
>> + * will be computed using connected device V4L2_CID_PIXEL_RATE, bit per pixel
>> + * and number of lanes.
>> + */
>> +static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_subdev *subdev = bridge->s_subdev;
>> + u32 link_freq;
>> +
>> + link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, subdev);
>> + if (!link_freq) {
>> + dev_err(&client->dev, "Failed to detect link frequency");
>> + return -EINVAL;
>> + }
>> +
>> + dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq);
>> + bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_clk_lane(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
>> +
>> + /* midid02 doesn't support clock lane remapping */
>> + if (ep->bus.mipi_csi2.clock_lane != 0) {
>> + dev_err(&client->dev, "clk lane must be map to lane 0\n");
>> + return -EINVAL;
>> + }
>> + bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb,
>> + bool are_lanes_swap, bool *polarities)
>> +{
>> + bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1];
>> +
>> + if (nb == 1 && are_lanes_swap)
>> + return 0;
>> +
>> + /*
>> + * data lane 0 as pin swap polarity reversed compared to clock and
>> + * data lane 1
>> + */
>> + if (!are_pin_swap)
>> + bridge->r.data_lane0_reg1 = 1 << 1;
>> + bridge->r.data_lane0_reg1 |= DATA_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb,
>> + bool are_lanes_swap, bool *polarities)
>> +{
>> + bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2];
>> +
>> + if (nb == 1 && !are_lanes_swap)
>> + return 0;
>> +
>> + if (are_pin_swap)
>> + bridge->r.data_lane1_reg1 = 1 << 1;
>> + bridge->r.data_lane1_reg1 |= DATA_ENABLE;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_from_rx(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->rx;
>> + bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2;
>> + bool *polarities = ep->bus.mipi_csi2.lane_polarities;
>> + int nb = ep->bus.mipi_csi2.num_data_lanes;
>> + int ret;
>> +
>> + ret = mipid02_configure_clk_lane(bridge);
>> + if (ret)
>> + return ret;
>> +
>> + ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap,
>> + polarities);
>> + if (ret)
>> + return ret;
>> +
>> + ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap,
>> + polarities);
>> + if (ret)
>> + return ret;
>> +
>> + bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0;
>> + bridge->r.mode_reg1 |= (nb - 1) << 1;
>> +
>> + return mipid02_configure_from_rx_speed(bridge);
>> +}
>> +
>> +static int mipid02_configure_from_tx(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint *ep = &bridge->tx;
>> +
>> + bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width;
>> + bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width;
>> + if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
>> + bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH;
>> + if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
>> + bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_configure_from_code(struct mipid02_dev *bridge)
>> +{
>> + u8 data_type;
>> +
>> + bridge->r.data_id_rreg = 0;
>> + data_type = data_type_from_code(bridge->fmt.code);
>> + if (!data_type)
>> + return -EINVAL;
>> + bridge->r.data_id_rreg = data_type;
>> +
>> + return 0;
>> +}
>> +
>> +static int mipid02_stream_disable(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret;
>> +
>> + /* Disable all lanes */
>> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0);
>> + if (ret)
>> + goto error;
>> +error:
>> + if (ret)
>> + dev_err(&client->dev, "failed to stream off %d", ret);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_stream_enable(struct mipid02_dev *bridge)
>> +{
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret = -EINVAL;
>> +
>> + if (!bridge->s_subdev)
>> + goto error;
>> +
>> + memset(&bridge->r, 0, sizeof(bridge->r));
>> + /* build registers content */
>> + ret = mipid02_configure_from_rx(bridge);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_configure_from_tx(bridge);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_configure_from_code(bridge);
>> + if (ret)
>> + goto error;
>> +
>> + /* write mipi registers */
>> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1,
>> + bridge->r.clk_lane_reg1);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1,
>> + bridge->r.data_lane0_reg1);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2,
>> + DATA_MIPI_CSI);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1,
>> + bridge->r.data_lane1_reg1);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2,
>> + DATA_MIPI_CSI);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_MODE_REG1,
>> + MODE_NO_BYPASS | bridge->r.mode_reg1);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_MODE_REG2,
>> + bridge->r.mode_reg2);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG,
>> + bridge->r.data_id_rreg);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL,
>> + SELECTION_MANUAL_DATA | SELECTION_MANUAL_WIDTH);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL,
>> + bridge->r.pix_width_ctrl);
>> + if (ret)
>> + goto error;
>> + ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB,
>> + bridge->r.pix_width_ctrl_emb);
>> + if (ret)
>> + goto error;
>> +
>> + return 0;
>> +
>> +error:
>> + dev_err(&client->dev, "failed to stream on %d", ret);
>> + mipid02_stream_disable(bridge);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_s_stream(struct v4l2_subdev *sd, int enable)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret = 0;
>> +
>> + dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__,
>> + enable, bridge->streaming);
>> + mutex_lock(&bridge->lock);
>> +
>> + if (bridge->streaming == enable)
>> + goto out;
>> +
>> + ret = enable ? mipid02_stream_enable(bridge) :
>> + mipid02_stream_disable(bridge);
>> + if (!ret)
>> + bridge->streaming = enable;
>> +
>> +out:
>> + dev_dbg(&client->dev, "%s current now = %d / %d", __func__,
>> + bridge->streaming, ret);
>> + mutex_unlock(&bridge->lock);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_enum_mbus_code(struct v4l2_subdev *sd,
>> + struct v4l2_subdev_pad_config *cfg,
>> + struct v4l2_subdev_mbus_code_enum *code)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + int ret = 0;
>> +
>> + switch (code->pad) {
>> + case MIPID02_SINK_0:
>> + if (code->index >= ARRAY_SIZE(mipid02_supported_fmt_codes))
>> + ret = -EINVAL;
>> + else
>> + code->code = mipid02_supported_fmt_codes[code->index];
>> + break;
>> + case MIPID02_SOURCE:
>> + if (code->index == 0)
>> + code->code = serial_to_parallel_code(bridge->fmt.code);
>> + else
>> + ret = -EINVAL;
>> + break;
>> + default:
>> + ret = -EINVAL;
>> + }
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_get_fmt(struct v4l2_subdev *sd,
>> + struct v4l2_subdev_pad_config *cfg,
>> + struct v4l2_subdev_format *format)
>> +{
>> + struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct v4l2_mbus_framefmt *fmt;
>> +
>> + dev_dbg(&client->dev, "%s probe %d", __func__, format->pad);
>> +
>> + if (format->pad >= MIPID02_PAD_NB)
>> + return -EINVAL;
>> + /* second CSI-2 pad not yet supported */
>> + if (format->pad == MIPID02_SINK_1)
>> + return -EINVAL;
>> +
>> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
>> + fmt = v4l2_subdev_get_try_format(&bridge->sd, cfg, format->pad);
>> + else
>> + fmt = &bridge->fmt;
>> +
>> + mutex_lock(&bridge->lock);
>> +
>> + *mbus_fmt = *fmt;
>> + /* code may need to be converted for source */
>> + if (format->pad == MIPID02_SOURCE)
>> + mbus_fmt->code = serial_to_parallel_code(mbus_fmt->code);
>> +
>> + mutex_unlock(&bridge->lock);
>> +
>> + return 0;
>> +}
>> +
>> +static void mipid02_set_fmt_source(struct v4l2_subdev *sd,
>> + struct v4l2_subdev_pad_config *cfg,
>> + struct v4l2_subdev_format *format)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> +
>> + /* source pad mirror active sink pad */
>> + format->format = bridge->fmt;
>> + /* but code may need to be converted */
>> + format->format.code = serial_to_parallel_code(format->format.code);
>> +
>> + /* only apply format for V4L2_SUBDEV_FORMAT_TRY case */
>> + if (format->which != V4L2_SUBDEV_FORMAT_TRY)
>> + return;
>> +
>> + *v4l2_subdev_get_try_format(sd, cfg, format->pad) = format->format;
>> +}
>> +
>> +static void mipid02_set_fmt_sink(struct v4l2_subdev *sd,
>> + struct v4l2_subdev_pad_config *cfg,
>> + struct v4l2_subdev_format *format)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct v4l2_mbus_framefmt *fmt;
>> +
>> + format->format.code = get_fmt_code(format->format.code);
>> +
>> + if (format->which == V4L2_SUBDEV_FORMAT_TRY)
>> + fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
>> + else
>> + fmt = &bridge->fmt;
>> +
>> + *fmt = format->format;
>> +}
>> +
>> +static int mipid02_set_fmt(struct v4l2_subdev *sd,
>> + struct v4l2_subdev_pad_config *cfg,
>> + struct v4l2_subdev_format *format)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int ret = 0;
>> +
>> + dev_dbg(&client->dev, "%s for %d", __func__, format->pad);
>> +
>> + if (format->pad >= MIPID02_PAD_NB)
>> + return -EINVAL;
>> + /* second CSI-2 pad not yet supported */
>> + if (format->pad == MIPID02_SINK_1)
>> + return -EINVAL;
>> +
>> + mutex_lock(&bridge->lock);
>> +
>> + if (bridge->streaming) {
>> + ret = -EBUSY;
>> + goto error;
>> + }
>> +
>> + if (format->pad == MIPID02_SOURCE)
>> + mipid02_set_fmt_source(sd, cfg, format);
>> + else
>> + mipid02_set_fmt_sink(sd, cfg, format);
>> +
>> +error:
>> + mutex_unlock(&bridge->lock);
>> +
>> + return ret;
>> +}
>> +
>> +static const struct v4l2_subdev_video_ops mipid02_video_ops = {
>> + .s_stream = mipid02_s_stream,
>> +};
>> +
>> +static const struct v4l2_subdev_pad_ops mipid02_pad_ops = {
>> + .enum_mbus_code = mipid02_enum_mbus_code,
>> + .get_fmt = mipid02_get_fmt,
>> + .set_fmt = mipid02_set_fmt,
>> +};
>> +
>> +static const struct v4l2_subdev_ops mipid02_subdev_ops = {
>> + .video = &mipid02_video_ops,
>> + .pad = &mipid02_pad_ops,
>> +};
>> +
>> +static const struct media_entity_operations mipid02_subdev_entity_ops = {
>> + .link_validate = v4l2_subdev_link_validate,
>> +};
>> +
>> +static int mipid02_async_bound(struct v4l2_async_notifier *notifier,
>> + struct v4l2_subdev *s_subdev,
>> + struct v4l2_async_subdev *asd)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
>> + struct i2c_client *client = bridge->i2c_client;
>> + int source_pad;
>> + int ret;
>> +
>> + dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev);
>> +
>> + source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
>> + s_subdev->fwnode,
>> + MEDIA_PAD_FL_SOURCE);
>> + if (source_pad < 0) {
>> + dev_err(&client->dev, "Couldn't find output pad for subdev %s\n",
>> + s_subdev->name);
>> + return source_pad;
>> + }
>> +
>> + ret = media_create_pad_link(&s_subdev->entity, source_pad,
>> + &bridge->sd.entity, 0,
>> + MEDIA_LNK_FL_ENABLED |
>> + MEDIA_LNK_FL_IMMUTABLE);
>> + if (ret) {
>> + dev_err(&client->dev, "Couldn't create media link %d", ret);
>> + return ret;
>> + }
>> +
>> + bridge->s_subdev = s_subdev;
>> +
>> + return 0;
>> +}
>> +
>> +static void mipid02_async_unbind(struct v4l2_async_notifier *notifier,
>> + struct v4l2_subdev *s_subdev,
>> + struct v4l2_async_subdev *asd)
>> +{
>> + struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd);
>> +
>> + bridge->s_subdev = NULL;
>> +}
>> +
>> +static const struct v4l2_async_notifier_operations mipid02_notifier_ops = {
>> + .bound = mipid02_async_bound,
>> + .unbind = mipid02_async_unbind,
>> +};
>> +
>> +static int mipid02_parse_rx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_CSI2_DPHY };
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct device_node *ep_node;
>> + int ret;
>> +
>> + /* parse rx (endpoint 0) */
>> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
>> + 0, 0);
>> + if (!ep_node) {
>> + dev_err(&client->dev, "unable to find port0 ep");
>> + ret = -EINVAL;
>> + goto error;
>> + }
>> +
>> + ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep_node), &ep);
>> + if (ret) {
>> + dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n",
>> + ret);
>> + goto error_of_node_put;
>> + }
>> +
>> + /* do some sanity checks */
>> + if (ep.bus.mipi_csi2.num_data_lanes > 2) {
>> + dev_err(&client->dev, "max supported data lanes is 2 / got %d",
>> + ep.bus.mipi_csi2.num_data_lanes);
>> + ret = -EINVAL;
>> + goto error_v4l2_fwnode_endpoint_free;
>> + }
>> +
>> + /* register it for later use */
>> + bridge->rx = ep;
>> + v4l2_fwnode_endpoint_free(&ep);
>
> As you don't use variable size properties, you could use
> v4l2_fwnode_endpoint_parse() instead. The note in
> v4l2_fwnode_endpoint_parse() telling to use
> v4l2_fwnode_endpoint_alloc_parse() in new drivers isn't actully entirely
> correct; the old function is just fine if you don't need any variable size
> properties. I'll submit a patch to change that.
>
Ok. I forgot to change it when I remove link_frequencies usage.
>> +
>> + /* register async notifier so we get noticed when sensor is connected */
>> + bridge->asd.match.fwnode =
>> + fwnode_graph_get_remote_port_parent(of_fwnode_handle(ep_node));
>> + bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
>> + of_node_put(ep_node);
>> +
>> + v4l2_async_notifier_init(&bridge->notifier);
>> + ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd);
>> + if (ret) {
>> + dev_err(&client->dev, "fail to register asd to notifier %d",
>> + ret);
>> + goto error_fwnode_handle_put;
>> + }
>> + bridge->notifier.ops = &mipid02_notifier_ops;
>> +
>> + ret = v4l2_async_subdev_notifier_register(&bridge->sd,
>> + &bridge->notifier);
>> + if (ret)
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> +
>> + return ret;
>> +
>> +error_v4l2_fwnode_endpoint_free:
>> + v4l2_fwnode_endpoint_free(&ep);
>> +error_of_node_put:
>> + of_node_put(ep_node);
>> +error:
>> +
>> + return ret;
>> +
>> +error_fwnode_handle_put:
>> + fwnode_handle_put(bridge->asd.match.fwnode);
>
> You only go here once; please drop the label and goto and move the code
> where it's used.
>
Ok
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_parse_tx_ep(struct mipid02_dev *bridge)
>> +{
>> + struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_PARALLEL };
>> + struct i2c_client *client = bridge->i2c_client;
>> + struct device_node *ep_node;
>> + int ret;
>> +
>> + /* parse tx (endpoint 2) */
>> + ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node,
>> + 2, 0);
>> + if (!ep_node) {
>> + dev_err(&client->dev, "unable to find port1 ep");
>> + ret = -EINVAL;
>> + goto error;
>> + }
>> +
>> + ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
>> + if (ret) {
>> + dev_err(&client->dev, "Could not parse v4l2 endpoint\n");
>> + goto error_of_node_put;
>> + }
>> +
>> + of_node_put(ep_node);
>> + bridge->tx = ep;
>> +
>> + return 0;
>> +
>> +error_of_node_put:
>> + of_node_put(ep_node);
>> +error:
>> +
>> + return -EINVAL;
>> +}
>> +
>> +static int mipid02_probe(struct i2c_client *client,
>> + const struct i2c_device_id *id)
>> +{
>> + struct device *dev = &client->dev;
>> + struct mipid02_dev *bridge;
>> + u32 clk_freq;
>> + int ret;
>> +
>> + bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
>> + if (!bridge)
>> + return -ENOMEM;
>> +
>> + init_format(&bridge->fmt);
>> +
>> + bridge->i2c_client = client;
>> + v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops);
>> +
>> + /* got and check clock */
>> + bridge->xclk = devm_clk_get(dev, "xclk");
>> + if (IS_ERR(bridge->xclk)) {
>> + dev_err(dev, "failed to get xclk\n");
>> + return PTR_ERR(bridge->xclk);
>> + }
>> +
>> + clk_freq = clk_get_rate(bridge->xclk);
>> + if (clk_freq < 6000000 || clk_freq > 27000000) {
>> + dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n",
>> + clk_freq);
>> + return -EINVAL;
>> + }
>> +
>> + bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset",
>> + GPIOD_OUT_HIGH);
>> +
>> + ret = mipid02_get_regulators(bridge);
>> + if (ret) {
>> + dev_err(dev, "failed to get regulators %d", ret);
>> + return ret;
>> + }
>> +
>> + mutex_init(&bridge->lock);
>> + bridge->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
>> + bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
>> + bridge->sd.entity.ops = &mipid02_subdev_entity_ops;
>> + bridge->pad[0].flags = MEDIA_PAD_FL_SINK;
>> + bridge->pad[1].flags = MEDIA_PAD_FL_SINK;
>> + bridge->pad[2].flags = MEDIA_PAD_FL_SOURCE;
>> + ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB,
>> + bridge->pad);
>> + if (ret) {
>> + dev_err(&client->dev, "pads init failed %d", ret);
>
> mutex_destroy(&bridge->lock);
>
> Please add a label and change return to goto below.
>
Ok. I missed mutex_destroy ...
>> + return ret;
>> + }
>> +
>> + /* enable clock, power and reset device if available */
>> + ret = mipid02_set_power_on(bridge);
>> + if (ret)
>> + goto entity_cleanup;
>> +
>> + ret = mipid02_detect(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to detect mipid02 %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = mipid02_parse_tx_ep(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to parse tx %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = mipid02_parse_rx_ep(bridge);
>> + if (ret) {
>> + dev_err(&client->dev, "failed to parse rx %d", ret);
>> + goto power_off;
>> + }
>> +
>> + ret = v4l2_async_register_subdev(&bridge->sd);
>> + if (ret < 0) {
>> + dev_err(&client->dev, "v4l2_async_register_subdev failed %d",
>> + ret);
>> + goto unregister_notifier;
>> + }
>> +
>> + dev_info(&client->dev, "mipid02 device probe successfully");
>> +
>> + return 0;
>> +
>> +unregister_notifier:
>> + v4l2_async_notifier_unregister(&bridge->notifier);
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> +power_off:
>> + mipid02_set_power_off(bridge);
>> +entity_cleanup:
>> + media_entity_cleanup(&bridge->sd.entity);
>> +
>> + return ret;
>> +}
>> +
>> +static int mipid02_remove(struct i2c_client *client)
>> +{
>> + struct v4l2_subdev *sd = i2c_get_clientdata(client);
>> + struct mipid02_dev *bridge = to_mipid02_dev(sd);
>> +
>> + v4l2_async_notifier_unregister(&bridge->notifier);
>> + v4l2_async_notifier_cleanup(&bridge->notifier);
>> + v4l2_async_unregister_subdev(&bridge->sd);
>> + mipid02_set_power_off(bridge);
>> + media_entity_cleanup(&bridge->sd.entity);
>
> mutex_destroy(&bridge->lock);
>
Ok
>> +
>> + return 0;
>> +}
>> +
>> +static const struct i2c_device_id mipid02_id[] = {
>> + { "st-mipid02", 0},
>> + {},
>> +};
>> +MODULE_DEVICE_TABLE(i2c, mipid02_id);
>> +
>> +static const struct of_device_id mipid02_dt_ids[] = {
>> + { .compatible = "st,st-mipid02" },
>> + { /* sentinel */ }
>> +};
>> +MODULE_DEVICE_TABLE(of, mipid02_dt_ids);
>> +
>> +static struct i2c_driver mipid02_i2c_driver = {
>> + .driver = {
>> + .name = "st-mipid02",
>> + .of_match_table = mipid02_dt_ids,
>> + },
>> + .id_table = mipid02_id,
>> + .probe = mipid02_probe,
>
> Probe_new, and you can omit the I?C ID table. Unless it's needed, of
> course, but generally it isn't.
It isn't need. I will switch to probe_new.

> The above are just minor improvements and cleanups. If you're fine with the
> proposed change to the first patch, I can merge this version of the patches
> and you could submit changes to the above in a separate patch.
>
Thanks for the proposal, but I prefer to to a full v5 if you agree ?
I will also add MEDIA_BUS_FMT_BGR888_3X8 or MEDIA_BUS_FMT_BGR888_3X8_BE
support.
>> + .remove = mipid02_remove,
>> +};
>> +
>> +module_i2c_driver(mipid02_i2c_driver);
>> +
>> +MODULE_AUTHOR("Mickael Guene <[email protected]>");
>> +MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
>> +MODULE_LICENSE("GPL v2");
>

2019-04-08 09:18:24

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v4 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

Hi Mickael,

On Mon, Apr 08, 2019 at 08:20:09AM +0000, Mickael GUENE wrote:
...
> > The above are just minor improvements and cleanups. If you're fine with the
> > proposed change to the first patch, I can merge this version of the patches
> > and you could submit changes to the above in a separate patch.
> >
> Thanks for the proposal, but I prefer to to a full v5 if you agree ?

That is fine, too.

> I will also add MEDIA_BUS_FMT_BGR888_3X8 or MEDIA_BUS_FMT_BGR888_3X8_BE
> support.

Ack. Please add a separate patch for that.

I guess postfix isn't needed as changing the endianness just changes the
pixel order in this case.

> >> + .remove = mipid02_remove,
> >> +};
> >> +
> >> +module_i2c_driver(mipid02_i2c_driver);
> >> +
> >> +MODULE_AUTHOR("Mickael Guene <[email protected]>");
> >> +MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");
> >> +MODULE_LICENSE("GPL v2");

--
Sakari Ailus
[email protected]

2019-04-08 09:23:58

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] media:st-mipid02: MIPID02 CSI-2 to PARALLEL bridge driver

On Mon, Apr 08, 2019 at 06:17:18AM +0000, Mickael GUENE wrote:
> Hi Sakari,
>
> On 4/6/19 13:01, Sakari Ailus wrote:
> > On Tue, Mar 26, 2019 at 02:36:41PM +0000, Mickael GUENE wrote:
> >> Sakari,
> >>
> >>>> +static int bpp_from_code(__u32 code)
> >>>> +{
> >>>> + switch (code) {
> >>>> + case MEDIA_BUS_FMT_SBGGR8_1X8:
> >>>> + case MEDIA_BUS_FMT_SGBRG8_1X8:
> >>>> + case MEDIA_BUS_FMT_SGRBG8_1X8:
> >>>> + case MEDIA_BUS_FMT_SRGGB8_1X8:
> >>>> + return 8;
> >>>> + case MEDIA_BUS_FMT_SBGGR10_1X10:
> >>>> + case MEDIA_BUS_FMT_SGBRG10_1X10:
> >>>> + case MEDIA_BUS_FMT_SGRBG10_1X10:
> >>>> + case MEDIA_BUS_FMT_SRGGB10_1X10:
> >>>> + return 10;
> >>>> + case MEDIA_BUS_FMT_SBGGR12_1X12:
> >>>> + case MEDIA_BUS_FMT_SGBRG12_1X12:
> >>>> + case MEDIA_BUS_FMT_SGRBG12_1X12:
> >>>> + case MEDIA_BUS_FMT_SRGGB12_1X12:
> >>>> + return 12;
> >>>> + case MEDIA_BUS_FMT_UYVY8_2X8:
> >>>
> >>> This is good for the parallel bus, but on CSI-2 side you should have
> >>> MEDIA_BUS_FMT_UYVY8_1X16 instead. This isn't technically correct for a
> >>> serial bus, but the custom is to use the one sample / pixel formats on the
> >>> serial busses.
> >>>
> >> Should MEDIA_BUS_FMT_BGR888_1X24 be something like
> >> MEDIA_BUS_FMT_BGR888_3X8 for parallel output bus ?
> >
> > Good point. Yes.
> >
> > Could you add that format to
> > Documentation/media/uapi/v4l/subdev-formats.rst, please, in a separate
> > patch?
> >
> I also need to add it to include/uapi/linux/media-bus-format.h I
> suppose ?

Yes, that's correct.

> I was also wondering if I should add MEDIA_BUS_FMT_BGR888_3X8_BE and
> MEDIA_BUS_FMT_BGR888_3X8_LE instead ?

If it's either, then "BE" but in this case I think it's better be without
the explicit endianness as the endianness change results in pixel order
change --- the format itself remains valid.

I wonder what others think, too. Hans, any opinion?

>
> So if you are ok with the above I will add MEDIA_BUS_FMT_BGR888_3X8 to
> subdev-formats.rs and media-bus-format.h as a first parch of my serie.

--
Regards,

Sakari Ailus
[email protected]