Lines Matching +full:memcpy +full:- +full:bus +full:- +full:width
1 // SPDX-License-Identifier: GPL-2.0
3 * Samsung CSIS MIPI CSI-2 receiver driver.
5 * The Samsung CSIS IP is a MIPI CSI-2 receiver found in various NXP i.MX7 and
10 * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. All Rights Reserved.
11 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-event.h>
34 #include <media/v4l2-fwnode.h>
35 #include <media/v4l2-mc.h>
36 #include <media/v4l2-subdev.h>
38 #define CSIS_DRIVER_NAME "imx-mipi-csis"
112 /* D-PHY status control */
119 /* D-PHY common control */
131 /* D-PHY Master and Slave Control register Low */
167 /* D-PHY Master and Slave Control register High */
169 /* D-PHY Slave Control register Low */
171 /* D-PHY Slave Control register High */
225 /* Non-image packet data buffers */
232 /* MIPI CSI-2 Data Types */
274 /* Non-image data receive events */
275 { false, MIPI_CSIS_INT_SRC_EVEN_BEFORE, "Non-image data before even frame" },
276 { false, MIPI_CSIS_INT_SRC_EVEN_AFTER, "Non-image data after even frame" },
277 { false, MIPI_CSIS_INT_SRC_ODD_BEFORE, "Non-image data before odd frame" },
278 { false, MIPI_CSIS_INT_SRC_ODD_AFTER, "Non-image data after odd frame" },
329 struct v4l2_mbus_config_mipi_csi2 bus; member
344 /* -----------------------------------------------------------------------------
352 u8 width; member
361 .width = 16,
368 .width = 16,
373 .width = 24,
380 .width = 8,
385 .width = 8,
390 .width = 8,
395 .width = 8,
400 .width = 8,
405 .width = 10,
410 .width = 10,
415 .width = 10,
420 .width = 10,
425 .width = 10,
430 .width = 12,
435 .width = 12,
440 .width = 12,
445 .width = 12,
450 .width = 12,
455 .width = 14,
460 .width = 14,
465 .width = 14,
470 .width = 14,
479 * The CSI-2 specification suggests in Annex A "JPEG8 Data
498 .width = 8,
512 /* -----------------------------------------------------------------------------
518 return readl(csis->regs + reg); in mipi_csis_read()
524 writel(val, csis->regs + reg); in mipi_csis_write()
556 mask = (1 << (csis->bus.num_data_lanes + 1)) - 1; in mipi_csis_system_enable()
576 * respectively, although the 8-bit mode transfers half a pixel per in __mipi_csis_set_format()
577 * clock sample and the 16-bit mode one pixel). While both mode work in __mipi_csis_set_format()
580 * that integrate the CSIS can operate in 16-bit bit mode, and some do in __mipi_csis_set_format()
581 * not support 8-bit mode (this is the case of the i.MX8MP), use dual in __mipi_csis_set_format()
586 if (csis_fmt->data_type == MIPI_CSI2_DATA_TYPE_YUV422_8) in __mipi_csis_set_format()
589 val |= MIPI_CSIS_ISPCFG_FMT(csis_fmt->data_type); in __mipi_csis_set_format()
593 val = format->width | (format->height << 16); in __mipi_csis_set_format()
604 link_freq = v4l2_get_link_freq(csis->source.sd->ctrl_handler, in mipi_csis_calculate_params()
605 csis_fmt->width, in mipi_csis_calculate_params()
606 csis->bus.num_data_lanes * 2); in mipi_csis_calculate_params()
608 dev_err(csis->dev, "Unable to obtain link frequency: %d\n", in mipi_csis_calculate_params()
616 dev_dbg(csis->dev, "Out-of-bound lane rate %u\n", lane_rate); in mipi_csis_calculate_params()
617 return -EINVAL; in mipi_csis_calculate_params()
623 * (which is documented as corresponding to CSI-2 v0.87 to v1.00) until in mipi_csis_calculate_params()
626 csis->hs_settle = (lane_rate - 5000000) / 45000000; in mipi_csis_calculate_params()
627 csis->clk_settle = 0; in mipi_csis_calculate_params()
629 dev_dbg(csis->dev, "lane rate %u, Tclk_settle %u, Ths_settle %u\n", in mipi_csis_calculate_params()
630 lane_rate, csis->clk_settle, csis->hs_settle); in mipi_csis_calculate_params()
632 if (csis->debug.hs_settle < 0xff) { in mipi_csis_calculate_params()
633 dev_dbg(csis->dev, "overriding Ths_settle with %u\n", in mipi_csis_calculate_params()
634 csis->debug.hs_settle); in mipi_csis_calculate_params()
635 csis->hs_settle = csis->debug.hs_settle; in mipi_csis_calculate_params()
638 if (csis->debug.clk_settle < 4) { in mipi_csis_calculate_params()
639 dev_dbg(csis->dev, "overriding Tclk_settle with %u\n", in mipi_csis_calculate_params()
640 csis->debug.clk_settle); in mipi_csis_calculate_params()
641 csis->clk_settle = csis->debug.clk_settle; in mipi_csis_calculate_params()
651 int lanes = csis->bus.num_data_lanes; in mipi_csis_set_params()
656 val |= (lanes - 1) << MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET; in mipi_csis_set_params()
657 if (csis->info->version == MIPI_CSIS_V3_3) in mipi_csis_set_params()
664 MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(csis->hs_settle) | in mipi_csis_set_params()
665 MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(csis->clk_settle)); in mipi_csis_set_params()
697 return clk_bulk_prepare_enable(csis->info->num_clocks, csis->clks); in mipi_csis_clk_enable()
702 clk_bulk_disable_unprepare(csis->info->num_clocks, csis->clks); in mipi_csis_clk_disable()
710 csis->clks = devm_kcalloc(csis->dev, csis->info->num_clocks, in mipi_csis_clk_get()
711 sizeof(*csis->clks), GFP_KERNEL); in mipi_csis_clk_get()
713 if (!csis->clks) in mipi_csis_clk_get()
714 return -ENOMEM; in mipi_csis_clk_get()
716 for (i = 0; i < csis->info->num_clocks; i++) in mipi_csis_clk_get()
717 csis->clks[i].id = mipi_csis_clk_id[i]; in mipi_csis_clk_get()
719 ret = devm_clk_bulk_get(csis->dev, csis->info->num_clocks, in mipi_csis_clk_get()
720 csis->clks); in mipi_csis_clk_get()
725 ret = clk_set_rate(csis->clks[MIPI_CSIS_CLK_WRAP].clk, in mipi_csis_clk_get()
726 csis->clk_frequency); in mipi_csis_clk_get()
728 dev_err(csis->dev, "set rate=%d failed: %d\n", in mipi_csis_clk_get()
729 csis->clk_frequency, ret); in mipi_csis_clk_get()
759 v4l2_event_queue(csis->sd.devnode, &event); in mipi_csis_queue_event_sof()
773 spin_lock_irqsave(&csis->slock, flags); in mipi_csis_irq_handler()
776 if ((status & MIPI_CSIS_INT_SRC_ERRORS) || csis->debug.enable) { in mipi_csis_irq_handler()
778 struct mipi_csis_event *event = &csis->events[i]; in mipi_csis_irq_handler()
780 if ((!event->debug && (status & event->mask)) || in mipi_csis_irq_handler()
781 (event->debug && (dbg_status & event->mask))) in mipi_csis_irq_handler()
782 event->counter++; in mipi_csis_irq_handler()
789 spin_unlock_irqrestore(&csis->slock, flags); in mipi_csis_irq_handler()
797 /* -----------------------------------------------------------------------------
803 if (csis->info->version != MIPI_CSIS_V3_3) in mipi_csis_phy_enable()
806 return regulator_enable(csis->mipi_phy_regulator); in mipi_csis_phy_enable()
811 if (csis->info->version != MIPI_CSIS_V3_3) in mipi_csis_phy_disable()
814 return regulator_disable(csis->mipi_phy_regulator); in mipi_csis_phy_disable()
819 if (csis->info->version != MIPI_CSIS_V3_3) in mipi_csis_phy_reset()
822 reset_control_assert(csis->mrst); in mipi_csis_phy_reset()
824 reset_control_deassert(csis->mrst); in mipi_csis_phy_reset()
829 if (csis->info->version != MIPI_CSIS_V3_3) in mipi_csis_phy_init()
833 csis->mrst = devm_reset_control_get_exclusive(csis->dev, NULL); in mipi_csis_phy_init()
834 if (IS_ERR(csis->mrst)) in mipi_csis_phy_init()
835 return PTR_ERR(csis->mrst); in mipi_csis_phy_init()
837 csis->mipi_phy_regulator = devm_regulator_get(csis->dev, "phy"); in mipi_csis_phy_init()
838 if (IS_ERR(csis->mipi_phy_regulator)) in mipi_csis_phy_init()
839 return PTR_ERR(csis->mipi_phy_regulator); in mipi_csis_phy_init()
841 return regulator_set_voltage(csis->mipi_phy_regulator, 1000000, in mipi_csis_phy_init()
845 /* -----------------------------------------------------------------------------
854 spin_lock_irqsave(&csis->slock, flags); in mipi_csis_clear_counters()
856 csis->events[i].counter = 0; in mipi_csis_clear_counters()
857 spin_unlock_irqrestore(&csis->slock, flags); in mipi_csis_clear_counters()
863 : MIPI_CSIS_NUM_EVENTS - 8; in mipi_csis_log_counters()
868 spin_lock_irqsave(&csis->slock, flags); in mipi_csis_log_counters()
870 counters[i] = csis->events[i].counter; in mipi_csis_log_counters()
871 spin_unlock_irqrestore(&csis->slock, flags); in mipi_csis_log_counters()
874 if (counters[i] > 0 || csis->debug.enable) in mipi_csis_log_counters()
875 dev_info(csis->dev, "%s events: %d\n", in mipi_csis_log_counters()
876 csis->events[i].name, in mipi_csis_log_counters()
905 if (!pm_runtime_get_if_in_use(csis->dev)) in mipi_csis_dump_regs()
908 dev_info(csis->dev, "--- REGISTERS ---\n"); in mipi_csis_dump_regs()
912 dev_info(csis->dev, "%14s: 0x%08x\n", registers[i].name, cfg); in mipi_csis_dump_regs()
915 pm_runtime_put(csis->dev); in mipi_csis_dump_regs()
922 struct mipi_csis_device *csis = m->private; in mipi_csis_dump_regs_show()
930 csis->debug.hs_settle = UINT_MAX; in mipi_csis_debugfs_init()
931 csis->debug.clk_settle = UINT_MAX; in mipi_csis_debugfs_init()
933 csis->debugfs_root = debugfs_create_dir(dev_name(csis->dev), NULL); in mipi_csis_debugfs_init()
935 debugfs_create_bool("debug_enable", 0600, csis->debugfs_root, in mipi_csis_debugfs_init()
936 &csis->debug.enable); in mipi_csis_debugfs_init()
937 debugfs_create_file("dump_regs", 0600, csis->debugfs_root, csis, in mipi_csis_debugfs_init()
939 debugfs_create_u32("tclk_settle", 0600, csis->debugfs_root, in mipi_csis_debugfs_init()
940 &csis->debug.clk_settle); in mipi_csis_debugfs_init()
941 debugfs_create_u32("ths_settle", 0600, csis->debugfs_root, in mipi_csis_debugfs_init()
942 &csis->debug.hs_settle); in mipi_csis_debugfs_init()
947 debugfs_remove_recursive(csis->debugfs_root); in mipi_csis_debugfs_exit()
950 /* -----------------------------------------------------------------------------
968 v4l2_subdev_disable_streams(csis->source.sd, in mipi_csis_s_stream()
969 csis->source.pad->index, BIT(0)); in mipi_csis_s_stream()
972 if (csis->debug.enable) in mipi_csis_s_stream()
975 pm_runtime_put(csis->dev); in mipi_csis_s_stream()
983 csis_fmt = find_csis_format(format->code); in mipi_csis_s_stream()
991 ret = pm_runtime_resume_and_get(csis->dev); in mipi_csis_s_stream()
997 ret = v4l2_subdev_enable_streams(csis->source.sd, in mipi_csis_s_stream()
998 csis->source.pad->index, BIT(0)); in mipi_csis_s_stream()
1010 pm_runtime_put(csis->dev); in mipi_csis_s_stream()
1025 if (code->pad == CSIS_PAD_SOURCE) { in mipi_csis_enum_mbus_code()
1028 if (code->index > 0) in mipi_csis_enum_mbus_code()
1029 return -EINVAL; in mipi_csis_enum_mbus_code()
1031 fmt = v4l2_subdev_state_get_format(sd_state, code->pad); in mipi_csis_enum_mbus_code()
1032 code->code = fmt->code; in mipi_csis_enum_mbus_code()
1036 if (code->pad != CSIS_PAD_SINK) in mipi_csis_enum_mbus_code()
1037 return -EINVAL; in mipi_csis_enum_mbus_code()
1039 if (code->index >= ARRAY_SIZE(mipi_csis_formats)) in mipi_csis_enum_mbus_code()
1040 return -EINVAL; in mipi_csis_enum_mbus_code()
1042 code->code = mipi_csis_formats[code->index].code; in mipi_csis_enum_mbus_code()
1059 if (sdformat->pad == CSIS_PAD_SOURCE) in mipi_csis_set_fmt()
1062 if (sdformat->pad != CSIS_PAD_SINK) in mipi_csis_set_fmt()
1063 return -EINVAL; in mipi_csis_set_fmt()
1066 * Validate the media bus code and clamp and align the size. in mipi_csis_set_fmt()
1069 * need to align the width for formats that are not multiples of 8 in mipi_csis_set_fmt()
1072 csis_fmt = find_csis_format(sdformat->format.code); in mipi_csis_set_fmt()
1076 switch (csis_fmt->width % 8) { in mipi_csis_set_fmt()
1093 v4l_bound_align_image(&sdformat->format.width, 1, in mipi_csis_set_fmt()
1095 &sdformat->format.height, 1, in mipi_csis_set_fmt()
1098 fmt = v4l2_subdev_state_get_format(sd_state, sdformat->pad); in mipi_csis_set_fmt()
1100 fmt->code = csis_fmt->code; in mipi_csis_set_fmt()
1101 fmt->width = sdformat->format.width; in mipi_csis_set_fmt()
1102 fmt->height = sdformat->format.height; in mipi_csis_set_fmt()
1103 fmt->field = V4L2_FIELD_NONE; in mipi_csis_set_fmt()
1104 fmt->colorspace = sdformat->format.colorspace; in mipi_csis_set_fmt()
1105 fmt->quantization = sdformat->format.quantization; in mipi_csis_set_fmt()
1106 fmt->xfer_func = sdformat->format.xfer_func; in mipi_csis_set_fmt()
1107 fmt->ycbcr_enc = sdformat->format.ycbcr_enc; in mipi_csis_set_fmt()
1109 sdformat->format = *fmt; in mipi_csis_set_fmt()
1113 *fmt = sdformat->format; in mipi_csis_set_fmt()
1116 fmt->code = csis_fmt->output; in mipi_csis_set_fmt()
1124 struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[0]; in mipi_csis_get_frame_desc()
1130 return -EINVAL; in mipi_csis_get_frame_desc()
1134 csis_fmt = find_csis_format(fmt->code); in mipi_csis_get_frame_desc()
1138 return -EPIPE; in mipi_csis_get_frame_desc()
1140 fd->type = V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL; in mipi_csis_get_frame_desc()
1141 fd->num_entries = 1; in mipi_csis_get_frame_desc()
1143 entry->flags = 0; in mipi_csis_get_frame_desc()
1144 entry->pixelcode = csis_fmt->code; in mipi_csis_get_frame_desc()
1145 entry->bus.csi2.vc = 0; in mipi_csis_get_frame_desc()
1146 entry->bus.csi2.dt = csis_fmt->data_type; in mipi_csis_get_frame_desc()
1159 fmt.format.width = MIPI_CSIS_DEF_PIX_WIDTH; in mipi_csis_init_state()
1177 if (csis->debug.enable) in mipi_csis_log_status()
1186 if (sub->type != V4L2_EVENT_FRAME_SYNC) in mipi_csis_subscribe_event()
1187 return -EINVAL; in mipi_csis_subscribe_event()
1190 if (sub->id != 0) in mipi_csis_subscribe_event()
1191 return -EINVAL; in mipi_csis_subscribe_event()
1223 /* -----------------------------------------------------------------------------
1235 dev_dbg(csis->dev, "link setup %s -> %s", remote_pad->entity->name, in mipi_csis_link_setup()
1236 local_pad->entity->name); in mipi_csis_link_setup()
1239 if (!(local_pad->flags & MEDIA_PAD_FL_SINK)) in mipi_csis_link_setup()
1242 remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity); in mipi_csis_link_setup()
1245 if (csis->source.sd) in mipi_csis_link_setup()
1246 return -EBUSY; in mipi_csis_link_setup()
1248 csis->source.sd = remote_sd; in mipi_csis_link_setup()
1249 csis->source.pad = remote_pad; in mipi_csis_link_setup()
1251 csis->source.sd = NULL; in mipi_csis_link_setup()
1252 csis->source.pad = NULL; in mipi_csis_link_setup()
1264 /* -----------------------------------------------------------------------------
1279 struct media_pad *sink = &csis->sd.entity.pads[CSIS_PAD_SINK]; in mipi_csis_notify_bound()
1298 v4l2_async_subdev_nf_init(&csis->notifier, &csis->sd); in mipi_csis_async_register()
1300 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csis->dev), 0, 0, in mipi_csis_async_register()
1303 return -ENOTCONN; in mipi_csis_async_register()
1309 for (i = 0; i < vep.bus.mipi_csi2.num_data_lanes; ++i) { in mipi_csis_async_register()
1310 if (vep.bus.mipi_csi2.data_lanes[i] != i + 1) { in mipi_csis_async_register()
1311 dev_err(csis->dev, in mipi_csis_async_register()
1313 ret = -EINVAL; in mipi_csis_async_register()
1318 csis->bus = vep.bus.mipi_csi2; in mipi_csis_async_register()
1320 dev_dbg(csis->dev, "data lanes: %d\n", csis->bus.num_data_lanes); in mipi_csis_async_register()
1321 dev_dbg(csis->dev, "flags: 0x%08x\n", csis->bus.flags); in mipi_csis_async_register()
1323 asd = v4l2_async_nf_add_fwnode_remote(&csis->notifier, ep, in mipi_csis_async_register()
1332 csis->notifier.ops = &mipi_csis_notify_ops; in mipi_csis_async_register()
1334 ret = v4l2_async_nf_register(&csis->notifier); in mipi_csis_async_register()
1338 return v4l2_async_register_subdev(&csis->sd); in mipi_csis_async_register()
1346 /* -----------------------------------------------------------------------------
1358 return -EAGAIN; in mipi_csis_runtime_suspend()
1373 return -EAGAIN; in mipi_csis_runtime_resume()
1389 /* -----------------------------------------------------------------------------
1395 struct v4l2_subdev *sd = &csis->sd; in mipi_csis_subdev_init()
1399 sd->internal_ops = &mipi_csis_internal_ops; in mipi_csis_subdev_init()
1400 sd->owner = THIS_MODULE; in mipi_csis_subdev_init()
1401 snprintf(sd->name, sizeof(sd->name), "csis-%s", in mipi_csis_subdev_init()
1402 dev_name(csis->dev)); in mipi_csis_subdev_init()
1404 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; in mipi_csis_subdev_init()
1405 sd->ctrl_handler = NULL; in mipi_csis_subdev_init()
1407 sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; in mipi_csis_subdev_init()
1408 sd->entity.ops = &mipi_csis_entity_ops; in mipi_csis_subdev_init()
1410 sd->dev = csis->dev; in mipi_csis_subdev_init()
1412 csis->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK in mipi_csis_subdev_init()
1414 csis->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE in mipi_csis_subdev_init()
1416 ret = media_entity_pads_init(&sd->entity, CSIS_PADS_NUM, csis->pads); in mipi_csis_subdev_init()
1422 media_entity_cleanup(&sd->entity); in mipi_csis_subdev_init()
1431 struct device_node *node = csis->dev->of_node; in mipi_csis_parse_dt()
1433 if (of_property_read_u32(node, "clock-frequency", in mipi_csis_parse_dt()
1434 &csis->clk_frequency)) in mipi_csis_parse_dt()
1435 csis->clk_frequency = DEFAULT_SCLK_CSIS_FREQ; in mipi_csis_parse_dt()
1442 struct device *dev = &pdev->dev; in mipi_csis_probe()
1449 return -ENOMEM; in mipi_csis_probe()
1451 spin_lock_init(&csis->slock); in mipi_csis_probe()
1453 csis->dev = dev; in mipi_csis_probe()
1454 csis->info = of_device_get_match_data(dev); in mipi_csis_probe()
1456 memcpy(csis->events, mipi_csis_events, sizeof(csis->events)); in mipi_csis_probe()
1466 csis->regs = devm_platform_ioremap_resource(pdev, 0); in mipi_csis_probe()
1467 if (IS_ERR(csis->regs)) in mipi_csis_probe()
1468 return PTR_ERR(csis->regs); in mipi_csis_probe()
1498 platform_set_drvdata(pdev, &csis->sd); in mipi_csis_probe()
1518 csis->bus.num_data_lanes, csis->clk_frequency); in mipi_csis_probe()
1525 v4l2_subdev_cleanup(&csis->sd); in mipi_csis_probe()
1526 media_entity_cleanup(&csis->sd.entity); in mipi_csis_probe()
1527 v4l2_async_nf_unregister(&csis->notifier); in mipi_csis_probe()
1528 v4l2_async_nf_cleanup(&csis->notifier); in mipi_csis_probe()
1529 v4l2_async_unregister_subdev(&csis->sd); in mipi_csis_probe()
1540 v4l2_async_nf_unregister(&csis->notifier); in mipi_csis_remove()
1541 v4l2_async_nf_cleanup(&csis->notifier); in mipi_csis_remove()
1542 v4l2_async_unregister_subdev(&csis->sd); in mipi_csis_remove()
1544 if (!pm_runtime_enabled(&pdev->dev)) in mipi_csis_remove()
1545 mipi_csis_runtime_suspend(&pdev->dev); in mipi_csis_remove()
1547 pm_runtime_disable(&pdev->dev); in mipi_csis_remove()
1548 v4l2_subdev_cleanup(&csis->sd); in mipi_csis_remove()
1549 media_entity_cleanup(&csis->sd.entity); in mipi_csis_remove()
1550 pm_runtime_set_suspended(&pdev->dev); in mipi_csis_remove()
1555 .compatible = "fsl,imx7-mipi-csi2",
1561 .compatible = "fsl,imx8mm-mipi-csi2",
1583 MODULE_DESCRIPTION("i.MX7 & i.MX8 MIPI CSI-2 receiver driver");
1585 MODULE_ALIAS("platform:imx-mipi-csi2");