Lines Matching +full:imx7 +full:- +full:reset
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.
28 #include <linux/reset.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-fwnode.h>
34 #include <media/v4l2-mc.h>
35 #include <media/v4l2-subdev.h>
37 #define CSIS_DRIVER_NAME "imx-mipi-csis"
111 /* D-PHY status control */
118 /* D-PHY common control */
130 /* D-PHY Master and Slave Control register Low */
166 /* D-PHY Master and Slave Control register High */
168 /* D-PHY Slave Control register Low */
170 /* D-PHY Slave Control register High */
224 /* Non-image packet data buffers */
231 /* MIPI CSI-2 Data Types */
273 /* Non-image data receive events */
274 { false, MIPI_CSIS_INT_SRC_EVEN_BEFORE, "Non-image data before even frame" },
275 { false, MIPI_CSIS_INT_SRC_EVEN_AFTER, "Non-image data after even frame" },
276 { false, MIPI_CSIS_INT_SRC_ODD_BEFORE, "Non-image data before odd frame" },
277 { false, MIPI_CSIS_INT_SRC_ODD_AFTER, "Non-image data after odd frame" },
339 /* -----------------------------------------------------------------------------
474 * The CSI-2 specification suggests in Annex A "JPEG8 Data
507 /* -----------------------------------------------------------------------------
513 return readl(csis->regs + reg); in mipi_csis_read()
519 writel(val, csis->regs + reg); in mipi_csis_write()
551 mask = (1 << (csis->bus.num_data_lanes + 1)) - 1; in mipi_csis_system_enable()
571 * respectively, although the 8-bit mode transfers half a pixel per in __mipi_csis_set_format()
572 * clock sample and the 16-bit mode one pixel). While both mode work in __mipi_csis_set_format()
575 * that integrate the CSIS can operate in 16-bit bit mode, and some do in __mipi_csis_set_format()
576 * not support 8-bit mode (this is the case of the i.MX8MP), use dual in __mipi_csis_set_format()
581 if (csis_fmt->data_type == MIPI_CSI2_DATA_TYPE_YUV422_8) in __mipi_csis_set_format()
584 val |= MIPI_CSIS_ISPCFG_FMT(csis_fmt->data_type); in __mipi_csis_set_format()
588 val = format->width | (format->height << 16); in __mipi_csis_set_format()
599 link_freq = v4l2_get_link_freq(csis->src_sd->ctrl_handler, in mipi_csis_calculate_params()
600 csis_fmt->width, in mipi_csis_calculate_params()
601 csis->bus.num_data_lanes * 2); in mipi_csis_calculate_params()
603 dev_err(csis->dev, "Unable to obtain link frequency: %d\n", in mipi_csis_calculate_params()
611 dev_dbg(csis->dev, "Out-of-bound lane rate %u\n", lane_rate); in mipi_csis_calculate_params()
612 return -EINVAL; in mipi_csis_calculate_params()
618 * (which is documented as corresponding to CSI-2 v0.87 to v1.00) until in mipi_csis_calculate_params()
621 csis->hs_settle = (lane_rate - 5000000) / 45000000; in mipi_csis_calculate_params()
622 csis->clk_settle = 0; in mipi_csis_calculate_params()
624 dev_dbg(csis->dev, "lane rate %u, Tclk_settle %u, Ths_settle %u\n", in mipi_csis_calculate_params()
625 lane_rate, csis->clk_settle, csis->hs_settle); in mipi_csis_calculate_params()
627 if (csis->debug.hs_settle < 0xff) { in mipi_csis_calculate_params()
628 dev_dbg(csis->dev, "overriding Ths_settle with %u\n", in mipi_csis_calculate_params()
629 csis->debug.hs_settle); in mipi_csis_calculate_params()
630 csis->hs_settle = csis->debug.hs_settle; in mipi_csis_calculate_params()
633 if (csis->debug.clk_settle < 4) { in mipi_csis_calculate_params()
634 dev_dbg(csis->dev, "overriding Tclk_settle with %u\n", in mipi_csis_calculate_params()
635 csis->debug.clk_settle); in mipi_csis_calculate_params()
636 csis->clk_settle = csis->debug.clk_settle; in mipi_csis_calculate_params()
646 int lanes = csis->bus.num_data_lanes; in mipi_csis_set_params()
651 val |= (lanes - 1) << MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET; in mipi_csis_set_params()
652 if (csis->info->version == MIPI_CSIS_V3_3) in mipi_csis_set_params()
659 MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(csis->hs_settle) | in mipi_csis_set_params()
660 MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(csis->clk_settle)); in mipi_csis_set_params()
692 return clk_bulk_prepare_enable(csis->info->num_clocks, csis->clks); in mipi_csis_clk_enable()
697 clk_bulk_disable_unprepare(csis->info->num_clocks, csis->clks); in mipi_csis_clk_disable()
705 csis->clks = devm_kcalloc(csis->dev, csis->info->num_clocks, in mipi_csis_clk_get()
706 sizeof(*csis->clks), GFP_KERNEL); in mipi_csis_clk_get()
708 if (!csis->clks) in mipi_csis_clk_get()
709 return -ENOMEM; in mipi_csis_clk_get()
711 for (i = 0; i < csis->info->num_clocks; i++) in mipi_csis_clk_get()
712 csis->clks[i].id = mipi_csis_clk_id[i]; in mipi_csis_clk_get()
714 ret = devm_clk_bulk_get(csis->dev, csis->info->num_clocks, in mipi_csis_clk_get()
715 csis->clks); in mipi_csis_clk_get()
720 ret = clk_set_rate(csis->clks[MIPI_CSIS_CLK_WRAP].clk, in mipi_csis_clk_get()
721 csis->clk_frequency); in mipi_csis_clk_get()
723 dev_err(csis->dev, "set rate=%d failed: %d\n", in mipi_csis_clk_get()
724 csis->clk_frequency, ret); in mipi_csis_clk_get()
756 spin_lock_irqsave(&csis->slock, flags); in mipi_csis_irq_handler()
759 if ((status & MIPI_CSIS_INT_SRC_ERRORS) || csis->debug.enable) { in mipi_csis_irq_handler()
761 struct mipi_csis_event *event = &csis->events[i]; in mipi_csis_irq_handler()
763 if ((!event->debug && (status & event->mask)) || in mipi_csis_irq_handler()
764 (event->debug && (dbg_status & event->mask))) in mipi_csis_irq_handler()
765 event->counter++; in mipi_csis_irq_handler()
768 spin_unlock_irqrestore(&csis->slock, flags); in mipi_csis_irq_handler()
776 /* -----------------------------------------------------------------------------
777 * PHY regulator and reset
782 if (csis->info->version != MIPI_CSIS_V3_3) in mipi_csis_phy_enable()
785 return regulator_enable(csis->mipi_phy_regulator); in mipi_csis_phy_enable()
790 if (csis->info->version != MIPI_CSIS_V3_3) in mipi_csis_phy_disable()
793 return regulator_disable(csis->mipi_phy_regulator); in mipi_csis_phy_disable()
798 if (csis->info->version != MIPI_CSIS_V3_3) in mipi_csis_phy_reset()
801 reset_control_assert(csis->mrst); in mipi_csis_phy_reset()
803 reset_control_deassert(csis->mrst); in mipi_csis_phy_reset()
808 if (csis->info->version != MIPI_CSIS_V3_3) in mipi_csis_phy_init()
811 /* Get MIPI PHY reset and regulator. */ in mipi_csis_phy_init()
812 csis->mrst = devm_reset_control_get_exclusive(csis->dev, NULL); in mipi_csis_phy_init()
813 if (IS_ERR(csis->mrst)) in mipi_csis_phy_init()
814 return PTR_ERR(csis->mrst); in mipi_csis_phy_init()
816 csis->mipi_phy_regulator = devm_regulator_get(csis->dev, "phy"); in mipi_csis_phy_init()
817 if (IS_ERR(csis->mipi_phy_regulator)) in mipi_csis_phy_init()
818 return PTR_ERR(csis->mipi_phy_regulator); in mipi_csis_phy_init()
820 return regulator_set_voltage(csis->mipi_phy_regulator, 1000000, in mipi_csis_phy_init()
824 /* -----------------------------------------------------------------------------
833 spin_lock_irqsave(&csis->slock, flags); in mipi_csis_clear_counters()
835 csis->events[i].counter = 0; in mipi_csis_clear_counters()
836 spin_unlock_irqrestore(&csis->slock, flags); in mipi_csis_clear_counters()
842 : MIPI_CSIS_NUM_EVENTS - 8; in mipi_csis_log_counters()
846 spin_lock_irqsave(&csis->slock, flags); in mipi_csis_log_counters()
849 if (csis->events[i].counter > 0 || csis->debug.enable) in mipi_csis_log_counters()
850 dev_info(csis->dev, "%s events: %d\n", in mipi_csis_log_counters()
851 csis->events[i].name, in mipi_csis_log_counters()
852 csis->events[i].counter); in mipi_csis_log_counters()
854 spin_unlock_irqrestore(&csis->slock, flags); in mipi_csis_log_counters()
881 if (!pm_runtime_get_if_in_use(csis->dev)) in mipi_csis_dump_regs()
884 dev_info(csis->dev, "--- REGISTERS ---\n"); in mipi_csis_dump_regs()
888 dev_info(csis->dev, "%14s: 0x%08x\n", registers[i].name, cfg); in mipi_csis_dump_regs()
891 pm_runtime_put(csis->dev); in mipi_csis_dump_regs()
898 struct mipi_csis_device *csis = m->private; in mipi_csis_dump_regs_show()
906 csis->debug.hs_settle = UINT_MAX; in mipi_csis_debugfs_init()
907 csis->debug.clk_settle = UINT_MAX; in mipi_csis_debugfs_init()
909 csis->debugfs_root = debugfs_create_dir(dev_name(csis->dev), NULL); in mipi_csis_debugfs_init()
911 debugfs_create_bool("debug_enable", 0600, csis->debugfs_root, in mipi_csis_debugfs_init()
912 &csis->debug.enable); in mipi_csis_debugfs_init()
913 debugfs_create_file("dump_regs", 0600, csis->debugfs_root, csis, in mipi_csis_debugfs_init()
915 debugfs_create_u32("tclk_settle", 0600, csis->debugfs_root, in mipi_csis_debugfs_init()
916 &csis->debug.clk_settle); in mipi_csis_debugfs_init()
917 debugfs_create_u32("ths_settle", 0600, csis->debugfs_root, in mipi_csis_debugfs_init()
918 &csis->debug.hs_settle); in mipi_csis_debugfs_init()
923 debugfs_remove_recursive(csis->debugfs_root); in mipi_csis_debugfs_exit()
926 /* -----------------------------------------------------------------------------
944 v4l2_subdev_call(csis->src_sd, video, s_stream, 0); in mipi_csis_s_stream()
947 if (csis->debug.enable) in mipi_csis_s_stream()
950 pm_runtime_put(csis->dev); in mipi_csis_s_stream()
958 csis_fmt = find_csis_format(format->code); in mipi_csis_s_stream()
966 ret = pm_runtime_resume_and_get(csis->dev); in mipi_csis_s_stream()
972 ret = v4l2_subdev_call(csis->src_sd, video, s_stream, 1); in mipi_csis_s_stream()
984 pm_runtime_put(csis->dev); in mipi_csis_s_stream()
999 if (code->pad == CSIS_PAD_SOURCE) { in mipi_csis_enum_mbus_code()
1002 if (code->index > 0) in mipi_csis_enum_mbus_code()
1003 return -EINVAL; in mipi_csis_enum_mbus_code()
1005 fmt = v4l2_subdev_get_pad_format(sd, sd_state, code->pad); in mipi_csis_enum_mbus_code()
1006 code->code = fmt->code; in mipi_csis_enum_mbus_code()
1010 if (code->pad != CSIS_PAD_SINK) in mipi_csis_enum_mbus_code()
1011 return -EINVAL; in mipi_csis_enum_mbus_code()
1013 if (code->index >= ARRAY_SIZE(mipi_csis_formats)) in mipi_csis_enum_mbus_code()
1014 return -EINVAL; in mipi_csis_enum_mbus_code()
1016 code->code = mipi_csis_formats[code->index].code; in mipi_csis_enum_mbus_code()
1033 if (sdformat->pad == CSIS_PAD_SOURCE) in mipi_csis_set_fmt()
1036 if (sdformat->pad != CSIS_PAD_SINK) in mipi_csis_set_fmt()
1037 return -EINVAL; in mipi_csis_set_fmt()
1046 csis_fmt = find_csis_format(sdformat->format.code); in mipi_csis_set_fmt()
1050 switch (csis_fmt->width % 8) { in mipi_csis_set_fmt()
1067 v4l_bound_align_image(&sdformat->format.width, 1, in mipi_csis_set_fmt()
1069 &sdformat->format.height, 1, in mipi_csis_set_fmt()
1072 fmt = v4l2_subdev_get_pad_format(sd, sd_state, sdformat->pad); in mipi_csis_set_fmt()
1074 fmt->code = csis_fmt->code; in mipi_csis_set_fmt()
1075 fmt->width = sdformat->format.width; in mipi_csis_set_fmt()
1076 fmt->height = sdformat->format.height; in mipi_csis_set_fmt()
1077 fmt->field = V4L2_FIELD_NONE; in mipi_csis_set_fmt()
1078 fmt->colorspace = sdformat->format.colorspace; in mipi_csis_set_fmt()
1079 fmt->quantization = sdformat->format.quantization; in mipi_csis_set_fmt()
1080 fmt->xfer_func = sdformat->format.xfer_func; in mipi_csis_set_fmt()
1081 fmt->ycbcr_enc = sdformat->format.ycbcr_enc; in mipi_csis_set_fmt()
1083 sdformat->format = *fmt; in mipi_csis_set_fmt()
1087 *fmt = sdformat->format; in mipi_csis_set_fmt()
1090 fmt->code = csis_fmt->output; in mipi_csis_set_fmt()
1098 struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[0]; in mipi_csis_get_frame_desc()
1104 return -EINVAL; in mipi_csis_get_frame_desc()
1108 csis_fmt = find_csis_format(fmt->code); in mipi_csis_get_frame_desc()
1112 return -EPIPE; in mipi_csis_get_frame_desc()
1114 fd->type = V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL; in mipi_csis_get_frame_desc()
1115 fd->num_entries = 1; in mipi_csis_get_frame_desc()
1119 entry->flags = 0; in mipi_csis_get_frame_desc()
1120 entry->pixelcode = csis_fmt->code; in mipi_csis_get_frame_desc()
1121 entry->bus.csi2.vc = 0; in mipi_csis_get_frame_desc()
1122 entry->bus.csi2.dt = csis_fmt->data_type; in mipi_csis_get_frame_desc()
1153 if (csis->debug.enable) in mipi_csis_log_status()
1181 /* -----------------------------------------------------------------------------
1193 dev_dbg(csis->dev, "link setup %s -> %s", remote_pad->entity->name, in mipi_csis_link_setup()
1194 local_pad->entity->name); in mipi_csis_link_setup()
1197 if (!(local_pad->flags & MEDIA_PAD_FL_SINK)) in mipi_csis_link_setup()
1200 remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity); in mipi_csis_link_setup()
1203 if (csis->src_sd) in mipi_csis_link_setup()
1204 return -EBUSY; in mipi_csis_link_setup()
1206 csis->src_sd = remote_sd; in mipi_csis_link_setup()
1208 csis->src_sd = NULL; in mipi_csis_link_setup()
1220 /* -----------------------------------------------------------------------------
1235 struct media_pad *sink = &csis->sd.entity.pads[CSIS_PAD_SINK]; in mipi_csis_notify_bound()
1254 v4l2_async_subdev_nf_init(&csis->notifier, &csis->sd); in mipi_csis_async_register()
1256 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csis->dev), 0, 0, in mipi_csis_async_register()
1259 return -ENOTCONN; in mipi_csis_async_register()
1267 dev_err(csis->dev, in mipi_csis_async_register()
1269 ret = -EINVAL; in mipi_csis_async_register()
1274 csis->bus = vep.bus.mipi_csi2; in mipi_csis_async_register()
1276 dev_dbg(csis->dev, "data lanes: %d\n", csis->bus.num_data_lanes); in mipi_csis_async_register()
1277 dev_dbg(csis->dev, "flags: 0x%08x\n", csis->bus.flags); in mipi_csis_async_register()
1279 asd = v4l2_async_nf_add_fwnode_remote(&csis->notifier, ep, in mipi_csis_async_register()
1288 csis->notifier.ops = &mipi_csis_notify_ops; in mipi_csis_async_register()
1290 ret = v4l2_async_nf_register(&csis->notifier); in mipi_csis_async_register()
1294 return v4l2_async_register_subdev(&csis->sd); in mipi_csis_async_register()
1302 /* -----------------------------------------------------------------------------
1314 return -EAGAIN; in mipi_csis_runtime_suspend()
1329 return -EAGAIN; in mipi_csis_runtime_resume()
1345 /* -----------------------------------------------------------------------------
1351 struct v4l2_subdev *sd = &csis->sd; in mipi_csis_subdev_init()
1355 sd->owner = THIS_MODULE; in mipi_csis_subdev_init()
1356 snprintf(sd->name, sizeof(sd->name), "csis-%s", in mipi_csis_subdev_init()
1357 dev_name(csis->dev)); in mipi_csis_subdev_init()
1359 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in mipi_csis_subdev_init()
1360 sd->ctrl_handler = NULL; in mipi_csis_subdev_init()
1362 sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; in mipi_csis_subdev_init()
1363 sd->entity.ops = &mipi_csis_entity_ops; in mipi_csis_subdev_init()
1365 sd->dev = csis->dev; in mipi_csis_subdev_init()
1367 csis->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK in mipi_csis_subdev_init()
1369 csis->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE in mipi_csis_subdev_init()
1371 ret = media_entity_pads_init(&sd->entity, CSIS_PADS_NUM, csis->pads); in mipi_csis_subdev_init()
1377 media_entity_cleanup(&sd->entity); in mipi_csis_subdev_init()
1386 struct device_node *node = csis->dev->of_node; in mipi_csis_parse_dt()
1388 if (of_property_read_u32(node, "clock-frequency", in mipi_csis_parse_dt()
1389 &csis->clk_frequency)) in mipi_csis_parse_dt()
1390 csis->clk_frequency = DEFAULT_SCLK_CSIS_FREQ; in mipi_csis_parse_dt()
1397 struct device *dev = &pdev->dev; in mipi_csis_probe()
1404 return -ENOMEM; in mipi_csis_probe()
1406 spin_lock_init(&csis->slock); in mipi_csis_probe()
1408 csis->dev = dev; in mipi_csis_probe()
1409 csis->info = of_device_get_match_data(dev); in mipi_csis_probe()
1411 memcpy(csis->events, mipi_csis_events, sizeof(csis->events)); in mipi_csis_probe()
1421 csis->regs = devm_platform_ioremap_resource(pdev, 0); in mipi_csis_probe()
1422 if (IS_ERR(csis->regs)) in mipi_csis_probe()
1423 return PTR_ERR(csis->regs); in mipi_csis_probe()
1437 /* Reset PHY and enable the clocks. */ in mipi_csis_probe()
1453 platform_set_drvdata(pdev, &csis->sd); in mipi_csis_probe()
1473 csis->bus.num_data_lanes, csis->clk_frequency); in mipi_csis_probe()
1480 v4l2_subdev_cleanup(&csis->sd); in mipi_csis_probe()
1481 media_entity_cleanup(&csis->sd.entity); in mipi_csis_probe()
1482 v4l2_async_nf_unregister(&csis->notifier); in mipi_csis_probe()
1483 v4l2_async_nf_cleanup(&csis->notifier); in mipi_csis_probe()
1484 v4l2_async_unregister_subdev(&csis->sd); in mipi_csis_probe()
1495 v4l2_async_nf_unregister(&csis->notifier); in mipi_csis_remove()
1496 v4l2_async_nf_cleanup(&csis->notifier); in mipi_csis_remove()
1497 v4l2_async_unregister_subdev(&csis->sd); in mipi_csis_remove()
1499 if (!pm_runtime_enabled(&pdev->dev)) in mipi_csis_remove()
1500 mipi_csis_runtime_suspend(&pdev->dev); in mipi_csis_remove()
1502 pm_runtime_disable(&pdev->dev); in mipi_csis_remove()
1503 v4l2_subdev_cleanup(&csis->sd); in mipi_csis_remove()
1504 media_entity_cleanup(&csis->sd.entity); in mipi_csis_remove()
1505 pm_runtime_set_suspended(&pdev->dev); in mipi_csis_remove()
1510 .compatible = "fsl,imx7-mipi-csi2",
1516 .compatible = "fsl,imx8mm-mipi-csi2",
1538 MODULE_DESCRIPTION("i.MX7 & i.MX8 MIPI CSI-2 receiver driver");
1540 MODULE_ALIAS("platform:imx-mipi-csi2");