Lines Matching +full:io +full:- +full:multiplex

1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Cadence MIPI-CSI2 TX Controller
5 * Copyright (C) 2017-2019 Cadence Design Systems Inc.
10 #include <linux/io.h>
18 #include <media/mipi-csi2.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-fwnode.h>
22 #include <media/v4l2-subdev.h>
163 if (code->pad || code->index >= ARRAY_SIZE(csi2tx_formats)) in csi2tx_enum_mbus_code()
164 return -EINVAL; in csi2tx_enum_mbus_code()
166 code->code = csi2tx_formats[code->index].mbus; in csi2tx_enum_mbus_code()
178 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) in __csi2tx_get_pad_format()
180 fmt->pad); in __csi2tx_get_pad_format()
182 return &csi2tx->pad_fmts[fmt->pad]; in __csi2tx_get_pad_format()
192 if (fmt->pad == CSI2TX_PAD_SOURCE) in csi2tx_get_pad_format()
193 return -EINVAL; in csi2tx_get_pad_format()
197 return -EINVAL; in csi2tx_get_pad_format()
199 fmt->format = *format; in csi2tx_get_pad_format()
208 const struct v4l2_mbus_framefmt *src_format = &fmt->format; in csi2tx_set_pad_format()
212 if (fmt->pad == CSI2TX_PAD_SOURCE) in csi2tx_set_pad_format()
213 return -EINVAL; in csi2tx_set_pad_format()
215 if (!csi2tx_get_fmt_from_mbus(fmt->format.code)) in csi2tx_set_pad_format()
220 return -EINVAL; in csi2tx_set_pad_format()
233 /* Set Wake Up value in the D-PHY */
237 csi2tx->base + CSI2TX_DPHY_CLK_WAKEUP_REG); in csi2tx_dphy_set_wakeup()
241 * Finishes the D-PHY initialization
252 for (i = 0; i < csi2tx->num_lanes; i++) in csi2tx_dphy_init_finish()
253 reg |= CSI2TX_DPHY_CFG_LANE_ENABLE(csi2tx->lanes[i] - 1); in csi2tx_dphy_init_finish()
254 writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG); in csi2tx_dphy_init_finish()
261 csi2tx->base + CSI2TX_DPHY_CFG_REG); in csi2tx_dphy_init_finish()
264 /* Configures D-PHY in CSIv1.3 */
274 for (i = 0; i < csi2tx->num_lanes; i++) in csi2tx_dphy_setup()
275 reg |= CSI2TX_DPHY_CFG_LANE_RESET(csi2tx->lanes[i] - 1); in csi2tx_dphy_setup()
276 writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG); in csi2tx_dphy_setup()
281 /* Configures D-PHY in CSIv2 */
290 writel(reg, csi2tx->base + CSI2TX_V2_DPHY_CFG_REG); in csi2tx_v2_dphy_setup()
297 writel(CSI2TX_CONFIG_SRST_REQ, csi2tx->base + CSI2TX_CONFIG_REG); in csi2tx_reset()
304 struct media_entity *entity = &csi2tx->subdev.entity; in csi2tx_start()
310 writel(CSI2TX_CONFIG_CFG_REQ, csi2tx->base + CSI2TX_CONFIG_REG); in csi2tx_start()
314 if (csi2tx->vops && csi2tx->vops->dphy_setup) { in csi2tx_start()
315 csi2tx->vops->dphy_setup(csi2tx); in csi2tx_start()
330 list_for_each_entry(link, &entity->links, list) { in csi2tx_start()
334 int pad_idx = -1; in csi2tx_start()
338 struct media_pad *pad = &csi2tx->pads[i]; in csi2tx_start()
340 if ((pad == link->sink) && in csi2tx_start()
341 (link->flags & MEDIA_LNK_FL_ENABLED)) { in csi2tx_start()
350 mfmt = &csi2tx->pad_fmts[pad_idx]; in csi2tx_start()
351 fmt = csi2tx_get_fmt_from_mbus(mfmt->code); in csi2tx_start()
355 stream = pad_idx - CSI2TX_PAD_SINK_STREAM0; in csi2tx_start()
364 writel(CSI2TX_DT_CFG_DT(fmt->dt), in csi2tx_start()
365 csi2tx->base + CSI2TX_DT_CFG_REG(stream)); in csi2tx_start()
367 writel(CSI2TX_DT_FORMAT_BYTES_PER_LINE(mfmt->width * fmt->bpp) | in csi2tx_start()
368 CSI2TX_DT_FORMAT_MAX_LINE_NUM(mfmt->height + 1), in csi2tx_start()
369 csi2tx->base + CSI2TX_DT_FORMAT_REG(stream)); in csi2tx_start()
376 csi2tx->base + CSI2TX_STREAM_IF_CFG_REG(stream)); in csi2tx_start()
380 writel(0, csi2tx->base + CSI2TX_CONFIG_REG); in csi2tx_start()
388 csi2tx->base + CSI2TX_CONFIG_REG); in csi2tx_stop()
396 mutex_lock(&csi2tx->lock); in csi2tx_s_stream()
403 if (!csi2tx->count) { in csi2tx_s_stream()
409 csi2tx->count++; in csi2tx_s_stream()
411 csi2tx->count--; in csi2tx_s_stream()
416 if (!csi2tx->count) in csi2tx_s_stream()
421 mutex_unlock(&csi2tx->lock); in csi2tx_s_stream()
441 csi2tx->base = devm_platform_ioremap_resource(pdev, 0); in csi2tx_get_resources()
442 if (IS_ERR(csi2tx->base)) in csi2tx_get_resources()
443 return PTR_ERR(csi2tx->base); in csi2tx_get_resources()
445 csi2tx->p_clk = devm_clk_get(&pdev->dev, "p_clk"); in csi2tx_get_resources()
446 if (IS_ERR(csi2tx->p_clk)) { in csi2tx_get_resources()
447 dev_err(&pdev->dev, "Couldn't get p_clk\n"); in csi2tx_get_resources()
448 return PTR_ERR(csi2tx->p_clk); in csi2tx_get_resources()
451 csi2tx->esc_clk = devm_clk_get(&pdev->dev, "esc_clk"); in csi2tx_get_resources()
452 if (IS_ERR(csi2tx->esc_clk)) { in csi2tx_get_resources()
453 dev_err(&pdev->dev, "Couldn't get the esc_clk\n"); in csi2tx_get_resources()
454 return PTR_ERR(csi2tx->esc_clk); in csi2tx_get_resources()
457 ret = clk_prepare_enable(csi2tx->p_clk); in csi2tx_get_resources()
459 dev_err(&pdev->dev, "Couldn't prepare and enable p_clk\n"); in csi2tx_get_resources()
463 dev_cfg = readl(csi2tx->base + CSI2TX_DEVICE_CONFIG_REG); in csi2tx_get_resources()
464 clk_disable_unprepare(csi2tx->p_clk); in csi2tx_get_resources()
466 csi2tx->max_lanes = dev_cfg & CSI2TX_DEVICE_CONFIG_LANES_MASK; in csi2tx_get_resources()
467 if (csi2tx->max_lanes > CSI2TX_LANES_MAX) { in csi2tx_get_resources()
468 dev_err(&pdev->dev, "Invalid number of lanes: %u\n", in csi2tx_get_resources()
469 csi2tx->max_lanes); in csi2tx_get_resources()
470 return -EINVAL; in csi2tx_get_resources()
473 csi2tx->max_streams = (dev_cfg & CSI2TX_DEVICE_CONFIG_STREAMS_MASK) >> 4; in csi2tx_get_resources()
474 if (csi2tx->max_streams > CSI2TX_STREAMS_MAX) { in csi2tx_get_resources()
475 dev_err(&pdev->dev, "Invalid number of streams: %u\n", in csi2tx_get_resources()
476 csi2tx->max_streams); in csi2tx_get_resources()
477 return -EINVAL; in csi2tx_get_resources()
480 csi2tx->has_internal_dphy = !!(dev_cfg & CSI2TX_DEVICE_CONFIG_HAS_DPHY); in csi2tx_get_resources()
482 for (i = 0; i < csi2tx->max_streams; i++) { in csi2tx_get_resources()
486 csi2tx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name); in csi2tx_get_resources()
487 if (IS_ERR(csi2tx->pixel_clk[i])) { in csi2tx_get_resources()
488 dev_err(&pdev->dev, "Couldn't get clock %s\n", in csi2tx_get_resources()
490 return PTR_ERR(csi2tx->pixel_clk[i]); in csi2tx_get_resources()
503 ep = of_graph_get_endpoint_by_regs(csi2tx->dev->of_node, 0, 0); in csi2tx_check_lanes()
505 return -EINVAL; in csi2tx_check_lanes()
509 dev_err(csi2tx->dev, "Could not parse v4l2 endpoint\n"); in csi2tx_check_lanes()
514 dev_err(csi2tx->dev, "Unsupported media bus type: 0x%x\n", in csi2tx_check_lanes()
516 ret = -EINVAL; in csi2tx_check_lanes()
520 csi2tx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes; in csi2tx_check_lanes()
521 if (csi2tx->num_lanes > csi2tx->max_lanes) { in csi2tx_check_lanes()
522 dev_err(csi2tx->dev, in csi2tx_check_lanes()
524 ret = -EINVAL; in csi2tx_check_lanes()
528 for (i = 0; i < csi2tx->num_lanes; i++) { in csi2tx_check_lanes()
530 dev_err(csi2tx->dev, "Invalid lane[%d] number: %u\n", in csi2tx_check_lanes()
532 ret = -EINVAL; in csi2tx_check_lanes()
537 memcpy(csi2tx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes, in csi2tx_check_lanes()
538 sizeof(csi2tx->lanes)); in csi2tx_check_lanes()
559 .compatible = "cdns,csi2tx-1.3",
563 .compatible = "cdns,csi2tx-2.1",
579 return -ENOMEM; in csi2tx_probe()
581 mutex_init(&csi2tx->lock); in csi2tx_probe()
582 csi2tx->dev = &pdev->dev; in csi2tx_probe()
588 of_id = of_match_node(csi2tx_of_table, pdev->dev.of_node); in csi2tx_probe()
589 csi2tx->vops = (struct csi2tx_vops *)of_id->data; in csi2tx_probe()
591 v4l2_subdev_init(&csi2tx->subdev, &csi2tx_subdev_ops); in csi2tx_probe()
592 csi2tx->subdev.owner = THIS_MODULE; in csi2tx_probe()
593 csi2tx->subdev.dev = &pdev->dev; in csi2tx_probe()
594 csi2tx->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in csi2tx_probe()
595 snprintf(csi2tx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s", in csi2tx_probe()
596 KBUILD_MODNAME, dev_name(&pdev->dev)); in csi2tx_probe()
603 csi2tx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; in csi2tx_probe()
604 csi2tx->pads[CSI2TX_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE; in csi2tx_probe()
606 csi2tx->pads[i].flags = MEDIA_PAD_FL_SINK; in csi2tx_probe()
610 * moment. The CSI link can multiplex various streams with in csi2tx_probe()
615 csi2tx->pad_fmts[i] = fmt_default; in csi2tx_probe()
617 ret = media_entity_pads_init(&csi2tx->subdev.entity, CSI2TX_PAD_MAX, in csi2tx_probe()
618 csi2tx->pads); in csi2tx_probe()
622 ret = v4l2_async_register_subdev(&csi2tx->subdev); in csi2tx_probe()
626 dev_info(&pdev->dev, in csi2tx_probe()
627 "Probed CSI2TX with %u/%u lanes, %u streams, %s D-PHY\n", in csi2tx_probe()
628 csi2tx->num_lanes, csi2tx->max_lanes, csi2tx->max_streams, in csi2tx_probe()
629 csi2tx->has_internal_dphy ? "internal" : "no"); in csi2tx_probe()
642 v4l2_async_unregister_subdev(&csi2tx->subdev); in csi2tx_remove()
653 .name = "cdns-csi2tx",
659 MODULE_DESCRIPTION("Cadence CSI2-TX controller");