Lines Matching +full:lane +full:- +full:polarities
1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for ST MIPID02 CSI-2 to PARALLEL bridge
19 #include <media/v4l2-async.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-subdev.h>
181 fmt->code = MEDIA_BUS_FMT_SBGGR8_1X8; in init_format()
182 fmt->field = V4L2_FIELD_NONE; in init_format()
183 fmt->colorspace = V4L2_COLORSPACE_SRGB; in init_format()
184 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB); in init_format()
185 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; in init_format()
186 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB); in init_format()
187 fmt->width = 640; in init_format()
188 fmt->height = 480; in init_format()
220 struct i2c_client *client = bridge->i2c_client; in mipid02_read_reg()
228 msg[0].addr = client->addr; in mipid02_read_reg()
229 msg[0].flags = client->flags; in mipid02_read_reg()
233 msg[1].addr = client->addr; in mipid02_read_reg()
234 msg[1].flags = client->flags | I2C_M_RD; in mipid02_read_reg()
238 ret = i2c_transfer(client->adapter, msg, 2); in mipid02_read_reg()
240 dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n", in mipid02_read_reg()
241 __func__, client->addr, reg, ret); in mipid02_read_reg()
250 struct i2c_client *client = bridge->i2c_client; in mipid02_write_reg()
259 msg.addr = client->addr; in mipid02_write_reg()
260 msg.flags = client->flags; in mipid02_write_reg()
264 ret = i2c_transfer(client->adapter, &msg, 1); in mipid02_write_reg()
266 dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n", in mipid02_write_reg()
279 bridge->supplies[i].supply = mipid02_supply_name[i]; in mipid02_get_regulators()
281 return devm_regulator_bulk_get(&bridge->i2c_client->dev, in mipid02_get_regulators()
283 bridge->supplies); in mipid02_get_regulators()
288 gpiod_set_value_cansleep(bridge->reset_gpio, 0); in mipid02_apply_reset()
290 gpiod_set_value_cansleep(bridge->reset_gpio, 1); in mipid02_apply_reset()
292 gpiod_set_value_cansleep(bridge->reset_gpio, 0); in mipid02_apply_reset()
298 struct i2c_client *client = bridge->i2c_client; in mipid02_set_power_on()
301 ret = clk_prepare_enable(bridge->xclk); in mipid02_set_power_on()
303 dev_err(&client->dev, "%s: failed to enable clock\n", __func__); in mipid02_set_power_on()
308 bridge->supplies); in mipid02_set_power_on()
310 dev_err(&client->dev, "%s: failed to enable regulators\n", in mipid02_set_power_on()
315 if (bridge->reset_gpio) { in mipid02_set_power_on()
316 dev_dbg(&client->dev, "apply reset"); in mipid02_set_power_on()
319 dev_dbg(&client->dev, "don't apply reset"); in mipid02_set_power_on()
326 clk_disable_unprepare(bridge->xclk); in mipid02_set_power_on()
332 regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies); in mipid02_set_power_off()
333 clk_disable_unprepare(bridge->xclk); in mipid02_set_power_off()
354 ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_LINK_FREQ); in mipid02_get_link_freq_from_cid_link_freq()
359 ret = v4l2_querymenu(subdev->ctrl_handler, &qm); in mipid02_get_link_freq_from_cid_link_freq()
369 struct v4l2_fwnode_endpoint *ep = &bridge->rx; in mipid02_get_link_freq_from_cid_pixel_rate()
372 u32 bpp = bpp_from_code(bridge->fmt.code); in mipid02_get_link_freq_from_cid_pixel_rate()
374 ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE); in mipid02_get_link_freq_from_cid_pixel_rate()
379 return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes); in mipid02_get_link_freq_from_cid_pixel_rate()
389 struct i2c_client *client = bridge->i2c_client; in mipid02_configure_from_rx_speed()
390 struct v4l2_subdev *subdev = bridge->s_subdev; in mipid02_configure_from_rx_speed()
398 dev_err(&client->dev, "Failed to get link frequency"); in mipid02_configure_from_rx_speed()
399 return -EINVAL; in mipid02_configure_from_rx_speed()
403 dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq); in mipid02_configure_from_rx_speed()
404 bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2; in mipid02_configure_from_rx_speed()
411 struct i2c_client *client = bridge->i2c_client; in mipid02_configure_clk_lane()
412 struct v4l2_fwnode_endpoint *ep = &bridge->rx; in mipid02_configure_clk_lane()
413 bool *polarities = ep->bus.mipi_csi2.lane_polarities; in mipid02_configure_clk_lane() local
415 /* midid02 doesn't support clock lane remapping */ in mipid02_configure_clk_lane()
416 if (ep->bus.mipi_csi2.clock_lane != 0) { in mipid02_configure_clk_lane()
417 dev_err(&client->dev, "clk lane must be map to lane 0\n"); in mipid02_configure_clk_lane()
418 return -EINVAL; in mipid02_configure_clk_lane()
420 bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE; in mipid02_configure_clk_lane()
426 bool are_lanes_swap, bool *polarities) in mipid02_configure_data0_lane() argument
428 bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1]; in mipid02_configure_data0_lane()
434 * data lane 0 as pin swap polarity reversed compared to clock and in mipid02_configure_data0_lane()
435 * data lane 1 in mipid02_configure_data0_lane()
438 bridge->r.data_lane0_reg1 = 1 << 1; in mipid02_configure_data0_lane()
439 bridge->r.data_lane0_reg1 |= DATA_ENABLE; in mipid02_configure_data0_lane()
445 bool are_lanes_swap, bool *polarities) in mipid02_configure_data1_lane() argument
447 bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2]; in mipid02_configure_data1_lane()
453 bridge->r.data_lane1_reg1 = 1 << 1; in mipid02_configure_data1_lane()
454 bridge->r.data_lane1_reg1 |= DATA_ENABLE; in mipid02_configure_data1_lane()
461 struct v4l2_fwnode_endpoint *ep = &bridge->rx; in mipid02_configure_from_rx()
462 bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2; in mipid02_configure_from_rx()
463 bool *polarities = ep->bus.mipi_csi2.lane_polarities; in mipid02_configure_from_rx() local
464 int nb = ep->bus.mipi_csi2.num_data_lanes; in mipid02_configure_from_rx()
472 polarities); in mipid02_configure_from_rx()
477 polarities); in mipid02_configure_from_rx()
481 bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0; in mipid02_configure_from_rx()
482 bridge->r.mode_reg1 |= (nb - 1) << 1; in mipid02_configure_from_rx()
489 struct v4l2_fwnode_endpoint *ep = &bridge->tx; in mipid02_configure_from_tx()
491 bridge->r.data_selection_ctrl = SELECTION_MANUAL_WIDTH; in mipid02_configure_from_tx()
492 bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width; in mipid02_configure_from_tx()
493 bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width; in mipid02_configure_from_tx()
494 if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) in mipid02_configure_from_tx()
495 bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH; in mipid02_configure_from_tx()
496 if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) in mipid02_configure_from_tx()
497 bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH; in mipid02_configure_from_tx()
506 bridge->r.data_id_rreg = 0; in mipid02_configure_from_code()
508 if (bridge->fmt.code != MEDIA_BUS_FMT_JPEG_1X8) { in mipid02_configure_from_code()
509 bridge->r.data_selection_ctrl |= SELECTION_MANUAL_DATA; in mipid02_configure_from_code()
511 data_type = data_type_from_code(bridge->fmt.code); in mipid02_configure_from_code()
513 return -EINVAL; in mipid02_configure_from_code()
514 bridge->r.data_id_rreg = data_type; in mipid02_configure_from_code()
522 struct i2c_client *client = bridge->i2c_client; in mipid02_stream_disable()
537 dev_err(&client->dev, "failed to stream off %d", ret); in mipid02_stream_disable()
544 struct i2c_client *client = bridge->i2c_client; in mipid02_stream_enable()
545 int ret = -EINVAL; in mipid02_stream_enable()
547 if (!bridge->s_subdev) in mipid02_stream_enable()
550 memset(&bridge->r, 0, sizeof(bridge->r)); in mipid02_stream_enable()
564 bridge->r.clk_lane_reg1); in mipid02_stream_enable()
571 bridge->r.data_lane0_reg1); in mipid02_stream_enable()
579 bridge->r.data_lane1_reg1); in mipid02_stream_enable()
587 MODE_NO_BYPASS | bridge->r.mode_reg1); in mipid02_stream_enable()
591 bridge->r.mode_reg2); in mipid02_stream_enable()
595 bridge->r.data_id_rreg); in mipid02_stream_enable()
599 bridge->r.data_selection_ctrl); in mipid02_stream_enable()
603 bridge->r.pix_width_ctrl); in mipid02_stream_enable()
607 bridge->r.pix_width_ctrl_emb); in mipid02_stream_enable()
614 dev_err(&client->dev, "failed to stream on %d", ret); in mipid02_stream_enable()
623 struct i2c_client *client = bridge->i2c_client; in mipid02_s_stream()
626 dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__, in mipid02_s_stream()
627 enable, bridge->streaming); in mipid02_s_stream()
628 mutex_lock(&bridge->lock); in mipid02_s_stream()
630 if (bridge->streaming == enable) in mipid02_s_stream()
636 bridge->streaming = enable; in mipid02_s_stream()
639 dev_dbg(&client->dev, "%s current now = %d / %d", __func__, in mipid02_s_stream()
640 bridge->streaming, ret); in mipid02_s_stream()
641 mutex_unlock(&bridge->lock); in mipid02_s_stream()
653 switch (code->pad) { in mipid02_enum_mbus_code()
655 if (code->index >= ARRAY_SIZE(mipid02_supported_fmt_codes)) in mipid02_enum_mbus_code()
656 ret = -EINVAL; in mipid02_enum_mbus_code()
658 code->code = mipid02_supported_fmt_codes[code->index]; in mipid02_enum_mbus_code()
661 if (code->index == 0) in mipid02_enum_mbus_code()
662 code->code = serial_to_parallel_code(bridge->fmt.code); in mipid02_enum_mbus_code()
664 ret = -EINVAL; in mipid02_enum_mbus_code()
667 ret = -EINVAL; in mipid02_enum_mbus_code()
677 struct v4l2_mbus_framefmt *mbus_fmt = &format->format; in mipid02_get_fmt()
679 struct i2c_client *client = bridge->i2c_client; in mipid02_get_fmt()
682 dev_dbg(&client->dev, "%s probe %d", __func__, format->pad); in mipid02_get_fmt()
684 if (format->pad >= MIPID02_PAD_NB) in mipid02_get_fmt()
685 return -EINVAL; in mipid02_get_fmt()
686 /* second CSI-2 pad not yet supported */ in mipid02_get_fmt()
687 if (format->pad == MIPID02_SINK_1) in mipid02_get_fmt()
688 return -EINVAL; in mipid02_get_fmt()
690 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in mipid02_get_fmt()
691 fmt = v4l2_subdev_get_try_format(&bridge->sd, cfg, format->pad); in mipid02_get_fmt()
693 fmt = &bridge->fmt; in mipid02_get_fmt()
695 mutex_lock(&bridge->lock); in mipid02_get_fmt()
699 if (format->pad == MIPID02_SOURCE) in mipid02_get_fmt()
700 mbus_fmt->code = serial_to_parallel_code(mbus_fmt->code); in mipid02_get_fmt()
702 mutex_unlock(&bridge->lock); in mipid02_get_fmt()
714 format->format = bridge->fmt; in mipid02_set_fmt_source()
716 format->format.code = serial_to_parallel_code(format->format.code); in mipid02_set_fmt_source()
719 if (format->which != V4L2_SUBDEV_FORMAT_TRY) in mipid02_set_fmt_source()
722 *v4l2_subdev_get_try_format(sd, cfg, format->pad) = format->format; in mipid02_set_fmt_source()
732 format->format.code = get_fmt_code(format->format.code); in mipid02_set_fmt_sink()
734 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in mipid02_set_fmt_sink()
735 fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad); in mipid02_set_fmt_sink()
737 fmt = &bridge->fmt; in mipid02_set_fmt_sink()
739 *fmt = format->format; in mipid02_set_fmt_sink()
747 struct i2c_client *client = bridge->i2c_client; in mipid02_set_fmt()
750 dev_dbg(&client->dev, "%s for %d", __func__, format->pad); in mipid02_set_fmt()
752 if (format->pad >= MIPID02_PAD_NB) in mipid02_set_fmt()
753 return -EINVAL; in mipid02_set_fmt()
754 /* second CSI-2 pad not yet supported */ in mipid02_set_fmt()
755 if (format->pad == MIPID02_SINK_1) in mipid02_set_fmt()
756 return -EINVAL; in mipid02_set_fmt()
758 mutex_lock(&bridge->lock); in mipid02_set_fmt()
760 if (bridge->streaming) { in mipid02_set_fmt()
761 ret = -EBUSY; in mipid02_set_fmt()
765 if (format->pad == MIPID02_SOURCE) in mipid02_set_fmt()
771 mutex_unlock(&bridge->lock); in mipid02_set_fmt()
799 struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd); in mipid02_async_bound()
800 struct i2c_client *client = bridge->i2c_client; in mipid02_async_bound()
804 dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev); in mipid02_async_bound()
806 source_pad = media_entity_get_fwnode_pad(&s_subdev->entity, in mipid02_async_bound()
807 s_subdev->fwnode, in mipid02_async_bound()
810 dev_err(&client->dev, "Couldn't find output pad for subdev %s\n", in mipid02_async_bound()
811 s_subdev->name); in mipid02_async_bound()
815 ret = media_create_pad_link(&s_subdev->entity, source_pad, in mipid02_async_bound()
816 &bridge->sd.entity, 0, in mipid02_async_bound()
820 dev_err(&client->dev, "Couldn't create media link %d", ret); in mipid02_async_bound()
824 bridge->s_subdev = s_subdev; in mipid02_async_bound()
833 struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd); in mipid02_async_unbind()
835 bridge->s_subdev = NULL; in mipid02_async_unbind()
846 struct i2c_client *client = bridge->i2c_client; in mipid02_parse_rx_ep()
851 ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node, in mipid02_parse_rx_ep()
854 dev_err(&client->dev, "unable to find port0 ep"); in mipid02_parse_rx_ep()
855 ret = -EINVAL; in mipid02_parse_rx_ep()
861 dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n", in mipid02_parse_rx_ep()
868 dev_err(&client->dev, "max supported data lanes is 2 / got %d", in mipid02_parse_rx_ep()
870 ret = -EINVAL; in mipid02_parse_rx_ep()
875 bridge->rx = ep; in mipid02_parse_rx_ep()
878 bridge->asd.match.fwnode = in mipid02_parse_rx_ep()
880 bridge->asd.match_type = V4L2_ASYNC_MATCH_FWNODE; in mipid02_parse_rx_ep()
883 v4l2_async_notifier_init(&bridge->notifier); in mipid02_parse_rx_ep()
884 ret = v4l2_async_notifier_add_subdev(&bridge->notifier, &bridge->asd); in mipid02_parse_rx_ep()
886 dev_err(&client->dev, "fail to register asd to notifier %d", in mipid02_parse_rx_ep()
888 fwnode_handle_put(bridge->asd.match.fwnode); in mipid02_parse_rx_ep()
891 bridge->notifier.ops = &mipid02_notifier_ops; in mipid02_parse_rx_ep()
893 ret = v4l2_async_subdev_notifier_register(&bridge->sd, in mipid02_parse_rx_ep()
894 &bridge->notifier); in mipid02_parse_rx_ep()
896 v4l2_async_notifier_cleanup(&bridge->notifier); in mipid02_parse_rx_ep()
910 struct i2c_client *client = bridge->i2c_client; in mipid02_parse_tx_ep()
915 ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node, in mipid02_parse_tx_ep()
918 dev_err(&client->dev, "unable to find port1 ep"); in mipid02_parse_tx_ep()
919 ret = -EINVAL; in mipid02_parse_tx_ep()
925 dev_err(&client->dev, "Could not parse v4l2 endpoint\n"); in mipid02_parse_tx_ep()
930 bridge->tx = ep; in mipid02_parse_tx_ep()
938 return -EINVAL; in mipid02_parse_tx_ep()
943 struct device *dev = &client->dev; in mipid02_probe()
950 return -ENOMEM; in mipid02_probe()
952 init_format(&bridge->fmt); in mipid02_probe()
954 bridge->i2c_client = client; in mipid02_probe()
955 v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops); in mipid02_probe()
958 bridge->xclk = devm_clk_get(dev, "xclk"); in mipid02_probe()
959 if (IS_ERR(bridge->xclk)) { in mipid02_probe()
961 return PTR_ERR(bridge->xclk); in mipid02_probe()
964 clk_freq = clk_get_rate(bridge->xclk); in mipid02_probe()
966 dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n", in mipid02_probe()
968 return -EINVAL; in mipid02_probe()
971 bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset", in mipid02_probe()
974 if (IS_ERR(bridge->reset_gpio)) { in mipid02_probe()
976 return PTR_ERR(bridge->reset_gpio); in mipid02_probe()
985 mutex_init(&bridge->lock); in mipid02_probe()
986 bridge->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in mipid02_probe()
987 bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; in mipid02_probe()
988 bridge->sd.entity.ops = &mipid02_subdev_entity_ops; in mipid02_probe()
989 bridge->pad[0].flags = MEDIA_PAD_FL_SINK; in mipid02_probe()
990 bridge->pad[1].flags = MEDIA_PAD_FL_SINK; in mipid02_probe()
991 bridge->pad[2].flags = MEDIA_PAD_FL_SOURCE; in mipid02_probe()
992 ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB, in mipid02_probe()
993 bridge->pad); in mipid02_probe()
995 dev_err(&client->dev, "pads init failed %d", ret); in mipid02_probe()
1006 dev_err(&client->dev, "failed to detect mipid02 %d", ret); in mipid02_probe()
1012 dev_err(&client->dev, "failed to parse tx %d", ret); in mipid02_probe()
1018 dev_err(&client->dev, "failed to parse rx %d", ret); in mipid02_probe()
1022 ret = v4l2_async_register_subdev(&bridge->sd); in mipid02_probe()
1024 dev_err(&client->dev, "v4l2_async_register_subdev failed %d", in mipid02_probe()
1029 dev_info(&client->dev, "mipid02 device probe successfully"); in mipid02_probe()
1034 v4l2_async_notifier_unregister(&bridge->notifier); in mipid02_probe()
1035 v4l2_async_notifier_cleanup(&bridge->notifier); in mipid02_probe()
1039 media_entity_cleanup(&bridge->sd.entity); in mipid02_probe()
1041 mutex_destroy(&bridge->lock); in mipid02_probe()
1051 v4l2_async_notifier_unregister(&bridge->notifier); in mipid02_remove()
1052 v4l2_async_notifier_cleanup(&bridge->notifier); in mipid02_remove()
1053 v4l2_async_unregister_subdev(&bridge->sd); in mipid02_remove()
1055 media_entity_cleanup(&bridge->sd.entity); in mipid02_remove()
1056 mutex_destroy(&bridge->lock); in mipid02_remove()
1062 { .compatible = "st,st-mipid02" },
1069 .name = "st-mipid02",
1079 MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver");