Lines Matching +full:data +full:- +full:lanes
1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Renesas R-Car MIPI CSI-2 Receiver
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-mc.h>
24 #include <media/v4l2-subdev.h>
54 * Channel Data Type Select
55 * VCDT[0-15]: Channel 0 VCDT[16-31]: Channel 1
56 * VCDT2[0-15]: Channel 2 VCDT2[16-31]: Channel 3
65 /* Frame Data Type Select */
80 /* Long Data Type Setting 0 */
83 /* Long Data Type Setting 1 */
103 /* Short Packet Data */
130 u16 data; member
304 /* PHY Data Lane Monitor */
376 unsigned short lanes; member
392 return ioread32(priv->base + reg); in rcsi2_read()
395 static void rcsi2_write(struct rcar_csi2 *priv, unsigned int reg, u32 data) in rcsi2_write() argument
397 iowrite32(data, priv->base + reg); in rcsi2_write()
404 reset_control_assert(priv->rstc); in rcsi2_enter_standby()
406 pm_runtime_put(priv->dev); in rcsi2_enter_standby()
411 pm_runtime_get_sync(priv->dev); in rcsi2_exit_standby()
412 reset_control_deassert(priv->rstc); in rcsi2_exit_standby()
416 unsigned int lanes) in rcsi2_wait_phy_start() argument
420 /* Wait for the clock and data lanes to enter LP-11 state. */ in rcsi2_wait_phy_start()
422 const u32 lane_mask = (1 << lanes) - 1; in rcsi2_wait_phy_start()
431 dev_err(priv->dev, "Timeout waiting for LP-11 state\n"); in rcsi2_wait_phy_start()
433 return -ETIMEDOUT; in rcsi2_wait_phy_start()
440 for (hsfreq = priv->info->hsfreqrange; hsfreq->mbps != 0; hsfreq++) in rcsi2_set_phypll()
441 if (hsfreq->mbps >= mbps) in rcsi2_set_phypll()
444 if (!hsfreq->mbps) { in rcsi2_set_phypll()
445 dev_err(priv->dev, "Unsupported PHY speed (%u Mbps)", mbps); in rcsi2_set_phypll()
446 return -ERANGE; in rcsi2_set_phypll()
449 rcsi2_write(priv, PHYPLL_REG, PHYPLL_HSFREQRANGE(hsfreq->reg)); in rcsi2_set_phypll()
455 unsigned int lanes) in rcsi2_calc_mbps() argument
461 if (!priv->remote) in rcsi2_calc_mbps()
462 return -ENODEV; in rcsi2_calc_mbps()
464 source = priv->remote; in rcsi2_calc_mbps()
467 ctrl = v4l2_ctrl_find(source->ctrl_handler, V4L2_CID_PIXEL_RATE); in rcsi2_calc_mbps()
469 dev_err(priv->dev, "no pixel rate control in subdev %s\n", in rcsi2_calc_mbps()
470 source->name); in rcsi2_calc_mbps()
471 return -EINVAL; in rcsi2_calc_mbps()
480 do_div(mbps, lanes * 1000000); in rcsi2_calc_mbps()
486 unsigned int *lanes) in rcsi2_get_active_lanes() argument
492 *lanes = priv->lanes; in rcsi2_get_active_lanes()
494 ret = v4l2_subdev_call(priv->remote, pad, get_mbus_config, in rcsi2_get_active_lanes()
495 priv->remote_pad, &mbus_config); in rcsi2_get_active_lanes()
496 if (ret == -ENOIOCTLCMD) { in rcsi2_get_active_lanes()
497 dev_dbg(priv->dev, "No remote mbus configuration available\n"); in rcsi2_get_active_lanes()
502 dev_err(priv->dev, "Failed to get remote mbus configuration\n"); in rcsi2_get_active_lanes()
507 dev_err(priv->dev, "Unsupported media bus type %u\n", in rcsi2_get_active_lanes()
509 return -EINVAL; in rcsi2_get_active_lanes()
521 if (num_lanes > priv->lanes) { in rcsi2_get_active_lanes()
522 dev_err(priv->dev, in rcsi2_get_active_lanes()
523 "Unsupported mbus config: too many data lanes %u\n", in rcsi2_get_active_lanes()
525 return -EINVAL; in rcsi2_get_active_lanes()
528 *lanes = num_lanes; in rcsi2_get_active_lanes()
537 unsigned int lanes; in rcsi2_start_receiver() local
541 dev_dbg(priv->dev, "Input size (%ux%u%c)\n", in rcsi2_start_receiver()
542 priv->mf.width, priv->mf.height, in rcsi2_start_receiver()
543 priv->mf.field == V4L2_FIELD_NONE ? 'p' : 'i'); in rcsi2_start_receiver()
546 format = rcsi2_code_to_fmt(priv->mf.code); in rcsi2_start_receiver()
549 * Enable all supported CSI-2 channels with virtual channel and in rcsi2_start_receiver()
550 * data type matching. in rcsi2_start_receiver()
556 for (i = 0; i < priv->info->num_channels; i++) { in rcsi2_start_receiver()
560 VCDT_SEL_DT(format->datatype); in rcsi2_start_receiver()
569 if (priv->mf.field == V4L2_FIELD_ALTERNATE) { in rcsi2_start_receiver()
573 if (priv->mf.height == 240) in rcsi2_start_receiver()
580 * Get the number of active data lanes inspecting the remote mbus in rcsi2_start_receiver()
583 ret = rcsi2_get_active_lanes(priv, &lanes); in rcsi2_start_receiver()
588 phycnt |= (1 << lanes) - 1; in rcsi2_start_receiver()
590 mbps = rcsi2_calc_mbps(priv, format->bpp, lanes); in rcsi2_start_receiver()
606 /* Lanes are zero indexed. */ in rcsi2_start_receiver()
608 LSWAP_L0SEL(priv->lane_swap[0] - 1) | in rcsi2_start_receiver()
609 LSWAP_L1SEL(priv->lane_swap[1] - 1) | in rcsi2_start_receiver()
610 LSWAP_L2SEL(priv->lane_swap[2] - 1) | in rcsi2_start_receiver()
611 LSWAP_L3SEL(priv->lane_swap[3] - 1)); in rcsi2_start_receiver()
614 if (priv->info->init_phtw) { in rcsi2_start_receiver()
615 ret = priv->info->init_phtw(priv, mbps); in rcsi2_start_receiver()
620 if (priv->info->hsfreqrange) { in rcsi2_start_receiver()
626 if (priv->info->csi0clkfreqrange) in rcsi2_start_receiver()
628 CSI0CLKFREQRANGE(priv->info->csi0clkfreqrange)); in rcsi2_start_receiver()
637 ret = rcsi2_wait_phy_start(priv, lanes); in rcsi2_start_receiver()
642 if (priv->info->phy_post_init) { in rcsi2_start_receiver()
643 ret = priv->info->phy_post_init(priv); in rcsi2_start_receiver()
649 if (priv->info->clear_ulps) in rcsi2_start_receiver()
668 ret = v4l2_subdev_call(priv->remote, video, s_stream, 1); in rcsi2_start()
680 v4l2_subdev_call(priv->remote, video, s_stream, 0); in rcsi2_stop()
688 mutex_lock(&priv->lock); in rcsi2_s_stream()
690 if (!priv->remote) { in rcsi2_s_stream()
691 ret = -ENODEV; in rcsi2_s_stream()
695 if (enable && priv->stream_count == 0) { in rcsi2_s_stream()
699 } else if (!enable && priv->stream_count == 1) { in rcsi2_s_stream()
703 priv->stream_count += enable ? 1 : -1; in rcsi2_s_stream()
705 mutex_unlock(&priv->lock); in rcsi2_s_stream()
717 if (!rcsi2_code_to_fmt(format->format.code)) in rcsi2_set_pad_format()
718 format->format.code = rcar_csi2_formats[0].code; in rcsi2_set_pad_format()
720 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) { in rcsi2_set_pad_format()
721 priv->mf = format->format; in rcsi2_set_pad_format()
724 *framefmt = format->format; in rcsi2_set_pad_format()
736 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) in rcsi2_get_pad_format()
737 format->format = priv->mf; in rcsi2_get_pad_format()
739 format->format = *v4l2_subdev_get_try_format(sd, cfg, 0); in rcsi2_get_pad_format()
758 static irqreturn_t rcsi2_irq(int irq, void *data) in rcsi2_irq() argument
760 struct rcar_csi2 *priv = data; in rcsi2_irq()
776 dev_info(priv->dev, "Transfer error, restarting CSI-2 receiver\n"); in rcsi2_irq()
781 static irqreturn_t rcsi2_irq_thread(int irq, void *data) in rcsi2_irq_thread() argument
783 struct rcar_csi2 *priv = data; in rcsi2_irq_thread()
785 mutex_lock(&priv->lock); in rcsi2_irq_thread()
789 dev_warn(priv->dev, "Failed to restart CSI-2 receiver\n"); in rcsi2_irq_thread()
790 mutex_unlock(&priv->lock); in rcsi2_irq_thread()
795 /* -----------------------------------------------------------------------------
806 pad = media_entity_get_fwnode_pad(&subdev->entity, asd->match.fwnode, in rcsi2_notify_bound()
809 dev_err(priv->dev, "Failed to find pad for %s\n", subdev->name); in rcsi2_notify_bound()
813 priv->remote = subdev; in rcsi2_notify_bound()
814 priv->remote_pad = pad; in rcsi2_notify_bound()
816 dev_dbg(priv->dev, "Bound %s pad: %d\n", subdev->name, pad); in rcsi2_notify_bound()
818 return media_create_pad_link(&subdev->entity, pad, in rcsi2_notify_bound()
819 &priv->subdev.entity, 0, in rcsi2_notify_bound()
830 priv->remote = NULL; in rcsi2_notify_unbind()
832 dev_dbg(priv->dev, "Unbind %s\n", subdev->name); in rcsi2_notify_unbind()
846 if (vep->base.port || vep->base.id) in rcsi2_parse_v4l2()
847 return -ENOTCONN; in rcsi2_parse_v4l2()
849 if (vep->bus_type != V4L2_MBUS_CSI2_DPHY) { in rcsi2_parse_v4l2()
850 dev_err(priv->dev, "Unsupported bus: %u\n", vep->bus_type); in rcsi2_parse_v4l2()
851 return -EINVAL; in rcsi2_parse_v4l2()
854 priv->lanes = vep->bus.mipi_csi2.num_data_lanes; in rcsi2_parse_v4l2()
855 if (priv->lanes != 1 && priv->lanes != 2 && priv->lanes != 4) { in rcsi2_parse_v4l2()
856 dev_err(priv->dev, "Unsupported number of data-lanes: %u\n", in rcsi2_parse_v4l2()
857 priv->lanes); in rcsi2_parse_v4l2()
858 return -EINVAL; in rcsi2_parse_v4l2()
861 for (i = 0; i < ARRAY_SIZE(priv->lane_swap); i++) { in rcsi2_parse_v4l2()
862 priv->lane_swap[i] = i < priv->lanes ? in rcsi2_parse_v4l2()
863 vep->bus.mipi_csi2.data_lanes[i] : i; in rcsi2_parse_v4l2()
866 if (priv->lane_swap[i] < 1 || priv->lane_swap[i] > 4) { in rcsi2_parse_v4l2()
867 dev_err(priv->dev, "data-lanes must be in 1-4 range\n"); in rcsi2_parse_v4l2()
868 return -EINVAL; in rcsi2_parse_v4l2()
883 ep = of_graph_get_endpoint_by_regs(priv->dev->of_node, 0, 0); in rcsi2_parse_dt()
885 dev_err(priv->dev, "Not connected to subdevice\n"); in rcsi2_parse_dt()
886 return -EINVAL; in rcsi2_parse_dt()
891 dev_err(priv->dev, "Could not parse v4l2 endpoint\n"); in rcsi2_parse_dt()
893 return -EINVAL; in rcsi2_parse_dt()
905 dev_dbg(priv->dev, "Found '%pOF'\n", to_of_node(fwnode)); in rcsi2_parse_dt()
907 v4l2_async_notifier_init(&priv->notifier); in rcsi2_parse_dt()
908 priv->notifier.ops = &rcar_csi2_notify_ops; in rcsi2_parse_dt()
910 asd = v4l2_async_notifier_add_fwnode_subdev(&priv->notifier, fwnode, in rcsi2_parse_dt()
916 ret = v4l2_async_subdev_notifier_register(&priv->subdev, in rcsi2_parse_dt()
917 &priv->notifier); in rcsi2_parse_dt()
919 v4l2_async_notifier_cleanup(&priv->notifier); in rcsi2_parse_dt()
924 /* -----------------------------------------------------------------------------
930 static int rcsi2_phtw_write(struct rcar_csi2 *priv, u16 data, u16 code) in rcsi2_phtw_write() argument
935 PHTW_DWEN | PHTW_TESTDIN_DATA(data) | in rcsi2_phtw_write()
946 dev_err(priv->dev, "Timeout waiting for PHTW_DWEN and/or PHTW_CWEN\n"); in rcsi2_phtw_write()
948 return -ETIMEDOUT; in rcsi2_phtw_write()
957 for (value = values; value->data || value->code; value++) { in rcsi2_phtw_write_array()
958 ret = rcsi2_phtw_write(priv, value->data, value->code); in rcsi2_phtw_write_array()
971 for (value = values; value->mbps; value++) in rcsi2_phtw_write_mbps()
972 if (value->mbps >= mbps) in rcsi2_phtw_write_mbps()
975 if (!value->mbps) { in rcsi2_phtw_write_mbps()
976 dev_err(priv->dev, "Unsupported PHY speed (%u Mbps)", mbps); in rcsi2_phtw_write_mbps()
977 return -ERANGE; in rcsi2_phtw_write_mbps()
980 return rcsi2_phtw_write(priv, value->reg, code); in rcsi2_phtw_write_mbps()
987 { .data = 0xcc, .code = 0xe2 }, in __rcsi2_init_phtw_h3_v3h_m3n()
988 { .data = 0x01, .code = 0xe3 }, in __rcsi2_init_phtw_h3_v3h_m3n()
989 { .data = 0x11, .code = 0xe4 }, in __rcsi2_init_phtw_h3_v3h_m3n()
990 { .data = 0x01, .code = 0xe5 }, in __rcsi2_init_phtw_h3_v3h_m3n()
991 { .data = 0x10, .code = 0x04 }, in __rcsi2_init_phtw_h3_v3h_m3n()
996 { .data = 0x38, .code = 0x08 }, in __rcsi2_init_phtw_h3_v3h_m3n()
997 { .data = 0x01, .code = 0x00 }, in __rcsi2_init_phtw_h3_v3h_m3n()
998 { .data = 0x4b, .code = 0xac }, in __rcsi2_init_phtw_h3_v3h_m3n()
999 { .data = 0x03, .code = 0x00 }, in __rcsi2_init_phtw_h3_v3h_m3n()
1000 { .data = 0x80, .code = 0x07 }, in __rcsi2_init_phtw_h3_v3h_m3n()
1042 { .data = 0xee, .code = 0x34 }, in rcsi2_phy_post_init_v3m_e3()
1043 { .data = 0xee, .code = 0x44 }, in rcsi2_phy_post_init_v3m_e3()
1044 { .data = 0xee, .code = 0x54 }, in rcsi2_phy_post_init_v3m_e3()
1045 { .data = 0xee, .code = 0x84 }, in rcsi2_phy_post_init_v3m_e3()
1046 { .data = 0xee, .code = 0x94 }, in rcsi2_phy_post_init_v3m_e3()
1053 /* -----------------------------------------------------------------------------
1068 priv->base = devm_ioremap_resource(&pdev->dev, res); in rcsi2_probe_resources()
1069 if (IS_ERR(priv->base)) in rcsi2_probe_resources()
1070 return PTR_ERR(priv->base); in rcsi2_probe_resources()
1076 ret = devm_request_threaded_irq(&pdev->dev, irq, rcsi2_irq, in rcsi2_probe_resources()
1082 priv->rstc = devm_reset_control_get(&pdev->dev, NULL); in rcsi2_probe_resources()
1084 return PTR_ERR_OR_ZERO(priv->rstc); in rcsi2_probe_resources()
1142 .compatible = "renesas,r8a774a1-csi2",
1143 .data = &rcar_csi2_info_r8a7796,
1146 .compatible = "renesas,r8a774b1-csi2",
1147 .data = &rcar_csi2_info_r8a77965,
1150 .compatible = "renesas,r8a774c0-csi2",
1151 .data = &rcar_csi2_info_r8a77990,
1154 .compatible = "renesas,r8a774e1-csi2",
1155 .data = &rcar_csi2_info_r8a7795,
1158 .compatible = "renesas,r8a7795-csi2",
1159 .data = &rcar_csi2_info_r8a7795,
1162 .compatible = "renesas,r8a7796-csi2",
1163 .data = &rcar_csi2_info_r8a7796,
1166 .compatible = "renesas,r8a77965-csi2",
1167 .data = &rcar_csi2_info_r8a77965,
1170 .compatible = "renesas,r8a77970-csi2",
1171 .data = &rcar_csi2_info_r8a77970,
1174 .compatible = "renesas,r8a77980-csi2",
1175 .data = &rcar_csi2_info_r8a77980,
1178 .compatible = "renesas,r8a77990-csi2",
1179 .data = &rcar_csi2_info_r8a77990,
1188 .data = &rcar_csi2_info_r8a7795es1,
1192 .data = &rcar_csi2_info_r8a7795es2,
1204 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); in rcsi2_probe()
1206 return -ENOMEM; in rcsi2_probe()
1208 priv->info = of_device_get_match_data(&pdev->dev); in rcsi2_probe()
1216 priv->info = attr->data; in rcsi2_probe()
1218 priv->dev = &pdev->dev; in rcsi2_probe()
1220 mutex_init(&priv->lock); in rcsi2_probe()
1221 priv->stream_count = 0; in rcsi2_probe()
1225 dev_err(priv->dev, "Failed to get resources\n"); in rcsi2_probe()
1235 priv->subdev.owner = THIS_MODULE; in rcsi2_probe()
1236 priv->subdev.dev = &pdev->dev; in rcsi2_probe()
1237 v4l2_subdev_init(&priv->subdev, &rcar_csi2_subdev_ops); in rcsi2_probe()
1238 v4l2_set_subdevdata(&priv->subdev, &pdev->dev); in rcsi2_probe()
1239 snprintf(priv->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s %s", in rcsi2_probe()
1240 KBUILD_MODNAME, dev_name(&pdev->dev)); in rcsi2_probe()
1241 priv->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; in rcsi2_probe()
1243 priv->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; in rcsi2_probe()
1244 priv->subdev.entity.ops = &rcar_csi2_entity_ops; in rcsi2_probe()
1246 priv->pads[RCAR_CSI2_SINK].flags = MEDIA_PAD_FL_SINK; in rcsi2_probe()
1248 priv->pads[i].flags = MEDIA_PAD_FL_SOURCE; in rcsi2_probe()
1250 ret = media_entity_pads_init(&priv->subdev.entity, NR_OF_RCAR_CSI2_PAD, in rcsi2_probe()
1251 priv->pads); in rcsi2_probe()
1255 pm_runtime_enable(&pdev->dev); in rcsi2_probe()
1257 ret = v4l2_async_register_subdev(&priv->subdev); in rcsi2_probe()
1261 dev_info(priv->dev, "%d lanes found\n", priv->lanes); in rcsi2_probe()
1266 v4l2_async_notifier_unregister(&priv->notifier); in rcsi2_probe()
1267 v4l2_async_notifier_cleanup(&priv->notifier); in rcsi2_probe()
1276 v4l2_async_notifier_unregister(&priv->notifier); in rcsi2_remove()
1277 v4l2_async_notifier_cleanup(&priv->notifier); in rcsi2_remove()
1278 v4l2_async_unregister_subdev(&priv->subdev); in rcsi2_remove()
1280 pm_runtime_disable(&pdev->dev); in rcsi2_remove()
1289 .name = "rcar-csi2",
1297 MODULE_DESCRIPTION("Renesas R-Car MIPI CSI-2 receiver driver");