11fc3b37fSMaxime Ripard // SPDX-License-Identifier: GPL-2.0+ 21fc3b37fSMaxime Ripard /* 31fc3b37fSMaxime Ripard * Driver for Cadence MIPI-CSI2 RX Controller v1.3 41fc3b37fSMaxime Ripard * 51fc3b37fSMaxime Ripard * Copyright (C) 2017 Cadence Design Systems Inc. 61fc3b37fSMaxime Ripard */ 71fc3b37fSMaxime Ripard 81fc3b37fSMaxime Ripard #include <linux/clk.h> 91fc3b37fSMaxime Ripard #include <linux/delay.h> 101fc3b37fSMaxime Ripard #include <linux/io.h> 111fc3b37fSMaxime Ripard #include <linux/module.h> 121fc3b37fSMaxime Ripard #include <linux/of.h> 131fc3b37fSMaxime Ripard #include <linux/of_graph.h> 141fc3b37fSMaxime Ripard #include <linux/phy/phy.h> 151fc3b37fSMaxime Ripard #include <linux/platform_device.h> 161fc3b37fSMaxime Ripard 171fc3b37fSMaxime Ripard #include <media/v4l2-ctrls.h> 181fc3b37fSMaxime Ripard #include <media/v4l2-device.h> 191fc3b37fSMaxime Ripard #include <media/v4l2-fwnode.h> 201fc3b37fSMaxime Ripard #include <media/v4l2-subdev.h> 211fc3b37fSMaxime Ripard 221fc3b37fSMaxime Ripard #define CSI2RX_DEVICE_CFG_REG 0x000 231fc3b37fSMaxime Ripard 241fc3b37fSMaxime Ripard #define CSI2RX_SOFT_RESET_REG 0x004 251fc3b37fSMaxime Ripard #define CSI2RX_SOFT_RESET_PROTOCOL BIT(1) 261fc3b37fSMaxime Ripard #define CSI2RX_SOFT_RESET_FRONT BIT(0) 271fc3b37fSMaxime Ripard 281fc3b37fSMaxime Ripard #define CSI2RX_STATIC_CFG_REG 0x008 291fc3b37fSMaxime Ripard #define CSI2RX_STATIC_CFG_DLANE_MAP(llane, plane) ((plane) << (16 + (llane) * 4)) 301fc3b37fSMaxime Ripard #define CSI2RX_STATIC_CFG_LANES_MASK GENMASK(11, 8) 311fc3b37fSMaxime Ripard 321fc3b37fSMaxime Ripard #define CSI2RX_STREAM_BASE(n) (((n) + 1) * 0x100) 331fc3b37fSMaxime Ripard 341fc3b37fSMaxime Ripard #define CSI2RX_STREAM_CTRL_REG(n) (CSI2RX_STREAM_BASE(n) + 0x000) 351fc3b37fSMaxime Ripard #define CSI2RX_STREAM_CTRL_START BIT(0) 361fc3b37fSMaxime Ripard 371fc3b37fSMaxime Ripard #define CSI2RX_STREAM_DATA_CFG_REG(n) (CSI2RX_STREAM_BASE(n) + 0x008) 381fc3b37fSMaxime Ripard #define CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT BIT(31) 391fc3b37fSMaxime Ripard #define CSI2RX_STREAM_DATA_CFG_VC_SELECT(n) BIT((n) + 16) 401fc3b37fSMaxime Ripard 411fc3b37fSMaxime Ripard #define CSI2RX_STREAM_CFG_REG(n) (CSI2RX_STREAM_BASE(n) + 0x00c) 421fc3b37fSMaxime Ripard #define CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF (1 << 8) 431fc3b37fSMaxime Ripard 441fc3b37fSMaxime Ripard #define CSI2RX_LANES_MAX 4 451fc3b37fSMaxime Ripard #define CSI2RX_STREAMS_MAX 4 461fc3b37fSMaxime Ripard 471fc3b37fSMaxime Ripard enum csi2rx_pads { 481fc3b37fSMaxime Ripard CSI2RX_PAD_SINK, 491fc3b37fSMaxime Ripard CSI2RX_PAD_SOURCE_STREAM0, 501fc3b37fSMaxime Ripard CSI2RX_PAD_SOURCE_STREAM1, 511fc3b37fSMaxime Ripard CSI2RX_PAD_SOURCE_STREAM2, 521fc3b37fSMaxime Ripard CSI2RX_PAD_SOURCE_STREAM3, 531fc3b37fSMaxime Ripard CSI2RX_PAD_MAX, 541fc3b37fSMaxime Ripard }; 551fc3b37fSMaxime Ripard 561fc3b37fSMaxime Ripard struct csi2rx_priv { 571fc3b37fSMaxime Ripard struct device *dev; 581fc3b37fSMaxime Ripard unsigned int count; 591fc3b37fSMaxime Ripard 601fc3b37fSMaxime Ripard /* 611fc3b37fSMaxime Ripard * Used to prevent race conditions between multiple, 621fc3b37fSMaxime Ripard * concurrent calls to start and stop. 631fc3b37fSMaxime Ripard */ 641fc3b37fSMaxime Ripard struct mutex lock; 651fc3b37fSMaxime Ripard 661fc3b37fSMaxime Ripard void __iomem *base; 671fc3b37fSMaxime Ripard struct clk *sys_clk; 681fc3b37fSMaxime Ripard struct clk *p_clk; 691fc3b37fSMaxime Ripard struct clk *pixel_clk[CSI2RX_STREAMS_MAX]; 701fc3b37fSMaxime Ripard struct phy *dphy; 711fc3b37fSMaxime Ripard 721fc3b37fSMaxime Ripard u8 lanes[CSI2RX_LANES_MAX]; 731fc3b37fSMaxime Ripard u8 num_lanes; 741fc3b37fSMaxime Ripard u8 max_lanes; 751fc3b37fSMaxime Ripard u8 max_streams; 761fc3b37fSMaxime Ripard bool has_internal_dphy; 771fc3b37fSMaxime Ripard 781fc3b37fSMaxime Ripard struct v4l2_subdev subdev; 791fc3b37fSMaxime Ripard struct v4l2_async_notifier notifier; 801fc3b37fSMaxime Ripard struct media_pad pads[CSI2RX_PAD_MAX]; 811fc3b37fSMaxime Ripard 821fc3b37fSMaxime Ripard /* Remote source */ 831fc3b37fSMaxime Ripard struct v4l2_async_subdev asd; 841fc3b37fSMaxime Ripard struct v4l2_subdev *source_subdev; 851fc3b37fSMaxime Ripard int source_pad; 861fc3b37fSMaxime Ripard }; 871fc3b37fSMaxime Ripard 881fc3b37fSMaxime Ripard static inline 891fc3b37fSMaxime Ripard struct csi2rx_priv *v4l2_subdev_to_csi2rx(struct v4l2_subdev *subdev) 901fc3b37fSMaxime Ripard { 911fc3b37fSMaxime Ripard return container_of(subdev, struct csi2rx_priv, subdev); 921fc3b37fSMaxime Ripard } 931fc3b37fSMaxime Ripard 941fc3b37fSMaxime Ripard static void csi2rx_reset(struct csi2rx_priv *csi2rx) 951fc3b37fSMaxime Ripard { 961fc3b37fSMaxime Ripard writel(CSI2RX_SOFT_RESET_PROTOCOL | CSI2RX_SOFT_RESET_FRONT, 971fc3b37fSMaxime Ripard csi2rx->base + CSI2RX_SOFT_RESET_REG); 981fc3b37fSMaxime Ripard 991fc3b37fSMaxime Ripard udelay(10); 1001fc3b37fSMaxime Ripard 1011fc3b37fSMaxime Ripard writel(0, csi2rx->base + CSI2RX_SOFT_RESET_REG); 1021fc3b37fSMaxime Ripard } 1031fc3b37fSMaxime Ripard 1041fc3b37fSMaxime Ripard static int csi2rx_start(struct csi2rx_priv *csi2rx) 1051fc3b37fSMaxime Ripard { 1061fc3b37fSMaxime Ripard unsigned int i; 1071fc3b37fSMaxime Ripard unsigned long lanes_used = 0; 1081fc3b37fSMaxime Ripard u32 reg; 1091fc3b37fSMaxime Ripard int ret; 1101fc3b37fSMaxime Ripard 1111fc3b37fSMaxime Ripard ret = clk_prepare_enable(csi2rx->p_clk); 1121fc3b37fSMaxime Ripard if (ret) 1131fc3b37fSMaxime Ripard return ret; 1141fc3b37fSMaxime Ripard 1151fc3b37fSMaxime Ripard csi2rx_reset(csi2rx); 1161fc3b37fSMaxime Ripard 1171fc3b37fSMaxime Ripard reg = csi2rx->num_lanes << 8; 1181fc3b37fSMaxime Ripard for (i = 0; i < csi2rx->num_lanes; i++) { 1191fc3b37fSMaxime Ripard reg |= CSI2RX_STATIC_CFG_DLANE_MAP(i, csi2rx->lanes[i]); 1201fc3b37fSMaxime Ripard set_bit(csi2rx->lanes[i], &lanes_used); 1211fc3b37fSMaxime Ripard } 1221fc3b37fSMaxime Ripard 1231fc3b37fSMaxime Ripard /* 1241fc3b37fSMaxime Ripard * Even the unused lanes need to be mapped. In order to avoid 1251fc3b37fSMaxime Ripard * to map twice to the same physical lane, keep the lanes used 1261fc3b37fSMaxime Ripard * in the previous loop, and only map unused physical lanes to 1271fc3b37fSMaxime Ripard * the rest of our logical lanes. 1281fc3b37fSMaxime Ripard */ 1291fc3b37fSMaxime Ripard for (i = csi2rx->num_lanes; i < csi2rx->max_lanes; i++) { 1301fc3b37fSMaxime Ripard unsigned int idx = find_first_zero_bit(&lanes_used, 1311fc3b37fSMaxime Ripard sizeof(lanes_used)); 1321fc3b37fSMaxime Ripard set_bit(idx, &lanes_used); 1331fc3b37fSMaxime Ripard reg |= CSI2RX_STATIC_CFG_DLANE_MAP(i, i + 1); 1341fc3b37fSMaxime Ripard } 1351fc3b37fSMaxime Ripard 1361fc3b37fSMaxime Ripard writel(reg, csi2rx->base + CSI2RX_STATIC_CFG_REG); 1371fc3b37fSMaxime Ripard 1381fc3b37fSMaxime Ripard ret = v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, true); 1391fc3b37fSMaxime Ripard if (ret) 1401fc3b37fSMaxime Ripard goto err_disable_pclk; 1411fc3b37fSMaxime Ripard 1421fc3b37fSMaxime Ripard /* 1431fc3b37fSMaxime Ripard * Create a static mapping between the CSI virtual channels 1441fc3b37fSMaxime Ripard * and the output stream. 1451fc3b37fSMaxime Ripard * 1461fc3b37fSMaxime Ripard * This should be enhanced, but v4l2 lacks the support for 1471fc3b37fSMaxime Ripard * changing that mapping dynamically. 1481fc3b37fSMaxime Ripard * 1491fc3b37fSMaxime Ripard * We also cannot enable and disable independent streams here, 1501fc3b37fSMaxime Ripard * hence the reference counting. 1511fc3b37fSMaxime Ripard */ 1521fc3b37fSMaxime Ripard for (i = 0; i < csi2rx->max_streams; i++) { 1531fc3b37fSMaxime Ripard ret = clk_prepare_enable(csi2rx->pixel_clk[i]); 1541fc3b37fSMaxime Ripard if (ret) 1551fc3b37fSMaxime Ripard goto err_disable_pixclk; 1561fc3b37fSMaxime Ripard 1571fc3b37fSMaxime Ripard writel(CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF, 1581fc3b37fSMaxime Ripard csi2rx->base + CSI2RX_STREAM_CFG_REG(i)); 1591fc3b37fSMaxime Ripard 1601fc3b37fSMaxime Ripard writel(CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT | 1611fc3b37fSMaxime Ripard CSI2RX_STREAM_DATA_CFG_VC_SELECT(i), 1621fc3b37fSMaxime Ripard csi2rx->base + CSI2RX_STREAM_DATA_CFG_REG(i)); 1631fc3b37fSMaxime Ripard 1641fc3b37fSMaxime Ripard writel(CSI2RX_STREAM_CTRL_START, 1651fc3b37fSMaxime Ripard csi2rx->base + CSI2RX_STREAM_CTRL_REG(i)); 1661fc3b37fSMaxime Ripard } 1671fc3b37fSMaxime Ripard 1681fc3b37fSMaxime Ripard ret = clk_prepare_enable(csi2rx->sys_clk); 1691fc3b37fSMaxime Ripard if (ret) 1701fc3b37fSMaxime Ripard goto err_disable_pixclk; 1711fc3b37fSMaxime Ripard 1721fc3b37fSMaxime Ripard clk_disable_unprepare(csi2rx->p_clk); 1731fc3b37fSMaxime Ripard 1741fc3b37fSMaxime Ripard return 0; 1751fc3b37fSMaxime Ripard 1761fc3b37fSMaxime Ripard err_disable_pixclk: 177*28d42d2fSSakari Ailus for (; i > 0; i--) 178*28d42d2fSSakari Ailus clk_disable_unprepare(csi2rx->pixel_clk[i - 1]); 1791fc3b37fSMaxime Ripard 1801fc3b37fSMaxime Ripard err_disable_pclk: 1811fc3b37fSMaxime Ripard clk_disable_unprepare(csi2rx->p_clk); 1821fc3b37fSMaxime Ripard 1831fc3b37fSMaxime Ripard return ret; 1841fc3b37fSMaxime Ripard } 1851fc3b37fSMaxime Ripard 1861fc3b37fSMaxime Ripard static void csi2rx_stop(struct csi2rx_priv *csi2rx) 1871fc3b37fSMaxime Ripard { 1881fc3b37fSMaxime Ripard unsigned int i; 1891fc3b37fSMaxime Ripard 1901fc3b37fSMaxime Ripard clk_prepare_enable(csi2rx->p_clk); 1911fc3b37fSMaxime Ripard clk_disable_unprepare(csi2rx->sys_clk); 1921fc3b37fSMaxime Ripard 1931fc3b37fSMaxime Ripard for (i = 0; i < csi2rx->max_streams; i++) { 1941fc3b37fSMaxime Ripard writel(0, csi2rx->base + CSI2RX_STREAM_CTRL_REG(i)); 1951fc3b37fSMaxime Ripard 1961fc3b37fSMaxime Ripard clk_disable_unprepare(csi2rx->pixel_clk[i]); 1971fc3b37fSMaxime Ripard } 1981fc3b37fSMaxime Ripard 1991fc3b37fSMaxime Ripard clk_disable_unprepare(csi2rx->p_clk); 2001fc3b37fSMaxime Ripard 2011fc3b37fSMaxime Ripard if (v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, false)) 2021fc3b37fSMaxime Ripard dev_warn(csi2rx->dev, "Couldn't disable our subdev\n"); 2031fc3b37fSMaxime Ripard } 2041fc3b37fSMaxime Ripard 2051fc3b37fSMaxime Ripard static int csi2rx_s_stream(struct v4l2_subdev *subdev, int enable) 2061fc3b37fSMaxime Ripard { 2071fc3b37fSMaxime Ripard struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev); 2081fc3b37fSMaxime Ripard int ret = 0; 2091fc3b37fSMaxime Ripard 2101fc3b37fSMaxime Ripard mutex_lock(&csi2rx->lock); 2111fc3b37fSMaxime Ripard 2121fc3b37fSMaxime Ripard if (enable) { 2131fc3b37fSMaxime Ripard /* 2141fc3b37fSMaxime Ripard * If we're not the first users, there's no need to 2151fc3b37fSMaxime Ripard * enable the whole controller. 2161fc3b37fSMaxime Ripard */ 2171fc3b37fSMaxime Ripard if (!csi2rx->count) { 2181fc3b37fSMaxime Ripard ret = csi2rx_start(csi2rx); 2191fc3b37fSMaxime Ripard if (ret) 2201fc3b37fSMaxime Ripard goto out; 2211fc3b37fSMaxime Ripard } 2221fc3b37fSMaxime Ripard 2231fc3b37fSMaxime Ripard csi2rx->count++; 2241fc3b37fSMaxime Ripard } else { 2251fc3b37fSMaxime Ripard csi2rx->count--; 2261fc3b37fSMaxime Ripard 2271fc3b37fSMaxime Ripard /* 2281fc3b37fSMaxime Ripard * Let the last user turn off the lights. 2291fc3b37fSMaxime Ripard */ 2301fc3b37fSMaxime Ripard if (!csi2rx->count) 2311fc3b37fSMaxime Ripard csi2rx_stop(csi2rx); 2321fc3b37fSMaxime Ripard } 2331fc3b37fSMaxime Ripard 2341fc3b37fSMaxime Ripard out: 2351fc3b37fSMaxime Ripard mutex_unlock(&csi2rx->lock); 2361fc3b37fSMaxime Ripard return ret; 2371fc3b37fSMaxime Ripard } 2381fc3b37fSMaxime Ripard 2391fc3b37fSMaxime Ripard static const struct v4l2_subdev_video_ops csi2rx_video_ops = { 2401fc3b37fSMaxime Ripard .s_stream = csi2rx_s_stream, 2411fc3b37fSMaxime Ripard }; 2421fc3b37fSMaxime Ripard 2431fc3b37fSMaxime Ripard static const struct v4l2_subdev_ops csi2rx_subdev_ops = { 2441fc3b37fSMaxime Ripard .video = &csi2rx_video_ops, 2451fc3b37fSMaxime Ripard }; 2461fc3b37fSMaxime Ripard 2471fc3b37fSMaxime Ripard static int csi2rx_async_bound(struct v4l2_async_notifier *notifier, 2481fc3b37fSMaxime Ripard struct v4l2_subdev *s_subdev, 2491fc3b37fSMaxime Ripard struct v4l2_async_subdev *asd) 2501fc3b37fSMaxime Ripard { 2511fc3b37fSMaxime Ripard struct v4l2_subdev *subdev = notifier->sd; 2521fc3b37fSMaxime Ripard struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev); 2531fc3b37fSMaxime Ripard 2541fc3b37fSMaxime Ripard csi2rx->source_pad = media_entity_get_fwnode_pad(&s_subdev->entity, 2551fc3b37fSMaxime Ripard s_subdev->fwnode, 2561fc3b37fSMaxime Ripard MEDIA_PAD_FL_SOURCE); 2571fc3b37fSMaxime Ripard if (csi2rx->source_pad < 0) { 2581fc3b37fSMaxime Ripard dev_err(csi2rx->dev, "Couldn't find output pad for subdev %s\n", 2591fc3b37fSMaxime Ripard s_subdev->name); 2601fc3b37fSMaxime Ripard return csi2rx->source_pad; 2611fc3b37fSMaxime Ripard } 2621fc3b37fSMaxime Ripard 2631fc3b37fSMaxime Ripard csi2rx->source_subdev = s_subdev; 2641fc3b37fSMaxime Ripard 2651fc3b37fSMaxime Ripard dev_dbg(csi2rx->dev, "Bound %s pad: %d\n", s_subdev->name, 2661fc3b37fSMaxime Ripard csi2rx->source_pad); 2671fc3b37fSMaxime Ripard 2681fc3b37fSMaxime Ripard return media_create_pad_link(&csi2rx->source_subdev->entity, 2691fc3b37fSMaxime Ripard csi2rx->source_pad, 2701fc3b37fSMaxime Ripard &csi2rx->subdev.entity, 0, 2711fc3b37fSMaxime Ripard MEDIA_LNK_FL_ENABLED | 2721fc3b37fSMaxime Ripard MEDIA_LNK_FL_IMMUTABLE); 2731fc3b37fSMaxime Ripard } 2741fc3b37fSMaxime Ripard 2751fc3b37fSMaxime Ripard static const struct v4l2_async_notifier_operations csi2rx_notifier_ops = { 2761fc3b37fSMaxime Ripard .bound = csi2rx_async_bound, 2771fc3b37fSMaxime Ripard }; 2781fc3b37fSMaxime Ripard 2791fc3b37fSMaxime Ripard static int csi2rx_get_resources(struct csi2rx_priv *csi2rx, 2801fc3b37fSMaxime Ripard struct platform_device *pdev) 2811fc3b37fSMaxime Ripard { 2821fc3b37fSMaxime Ripard struct resource *res; 2831fc3b37fSMaxime Ripard unsigned char i; 2841fc3b37fSMaxime Ripard u32 dev_cfg; 2851fc3b37fSMaxime Ripard 2861fc3b37fSMaxime Ripard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2871fc3b37fSMaxime Ripard csi2rx->base = devm_ioremap_resource(&pdev->dev, res); 2881fc3b37fSMaxime Ripard if (IS_ERR(csi2rx->base)) 2891fc3b37fSMaxime Ripard return PTR_ERR(csi2rx->base); 2901fc3b37fSMaxime Ripard 2911fc3b37fSMaxime Ripard csi2rx->sys_clk = devm_clk_get(&pdev->dev, "sys_clk"); 2921fc3b37fSMaxime Ripard if (IS_ERR(csi2rx->sys_clk)) { 2931fc3b37fSMaxime Ripard dev_err(&pdev->dev, "Couldn't get sys clock\n"); 2941fc3b37fSMaxime Ripard return PTR_ERR(csi2rx->sys_clk); 2951fc3b37fSMaxime Ripard } 2961fc3b37fSMaxime Ripard 2971fc3b37fSMaxime Ripard csi2rx->p_clk = devm_clk_get(&pdev->dev, "p_clk"); 2981fc3b37fSMaxime Ripard if (IS_ERR(csi2rx->p_clk)) { 2991fc3b37fSMaxime Ripard dev_err(&pdev->dev, "Couldn't get P clock\n"); 3001fc3b37fSMaxime Ripard return PTR_ERR(csi2rx->p_clk); 3011fc3b37fSMaxime Ripard } 3021fc3b37fSMaxime Ripard 3031fc3b37fSMaxime Ripard csi2rx->dphy = devm_phy_optional_get(&pdev->dev, "dphy"); 3041fc3b37fSMaxime Ripard if (IS_ERR(csi2rx->dphy)) { 3051fc3b37fSMaxime Ripard dev_err(&pdev->dev, "Couldn't get external D-PHY\n"); 3061fc3b37fSMaxime Ripard return PTR_ERR(csi2rx->dphy); 3071fc3b37fSMaxime Ripard } 3081fc3b37fSMaxime Ripard 3091fc3b37fSMaxime Ripard /* 3101fc3b37fSMaxime Ripard * FIXME: Once we'll have external D-PHY support, the check 3111fc3b37fSMaxime Ripard * will need to be removed. 3121fc3b37fSMaxime Ripard */ 3131fc3b37fSMaxime Ripard if (csi2rx->dphy) { 3141fc3b37fSMaxime Ripard dev_err(&pdev->dev, "External D-PHY not supported yet\n"); 3151fc3b37fSMaxime Ripard return -EINVAL; 3161fc3b37fSMaxime Ripard } 3171fc3b37fSMaxime Ripard 3181fc3b37fSMaxime Ripard clk_prepare_enable(csi2rx->p_clk); 3191fc3b37fSMaxime Ripard dev_cfg = readl(csi2rx->base + CSI2RX_DEVICE_CFG_REG); 3201fc3b37fSMaxime Ripard clk_disable_unprepare(csi2rx->p_clk); 3211fc3b37fSMaxime Ripard 3221fc3b37fSMaxime Ripard csi2rx->max_lanes = dev_cfg & 7; 3231fc3b37fSMaxime Ripard if (csi2rx->max_lanes > CSI2RX_LANES_MAX) { 3241fc3b37fSMaxime Ripard dev_err(&pdev->dev, "Invalid number of lanes: %u\n", 3251fc3b37fSMaxime Ripard csi2rx->max_lanes); 3261fc3b37fSMaxime Ripard return -EINVAL; 3271fc3b37fSMaxime Ripard } 3281fc3b37fSMaxime Ripard 3291fc3b37fSMaxime Ripard csi2rx->max_streams = (dev_cfg >> 4) & 7; 3301fc3b37fSMaxime Ripard if (csi2rx->max_streams > CSI2RX_STREAMS_MAX) { 3311fc3b37fSMaxime Ripard dev_err(&pdev->dev, "Invalid number of streams: %u\n", 3321fc3b37fSMaxime Ripard csi2rx->max_streams); 3331fc3b37fSMaxime Ripard return -EINVAL; 3341fc3b37fSMaxime Ripard } 3351fc3b37fSMaxime Ripard 3361fc3b37fSMaxime Ripard csi2rx->has_internal_dphy = dev_cfg & BIT(3) ? true : false; 3371fc3b37fSMaxime Ripard 3381fc3b37fSMaxime Ripard /* 3391fc3b37fSMaxime Ripard * FIXME: Once we'll have internal D-PHY support, the check 3401fc3b37fSMaxime Ripard * will need to be removed. 3411fc3b37fSMaxime Ripard */ 3421fc3b37fSMaxime Ripard if (csi2rx->has_internal_dphy) { 3431fc3b37fSMaxime Ripard dev_err(&pdev->dev, "Internal D-PHY not supported yet\n"); 3441fc3b37fSMaxime Ripard return -EINVAL; 3451fc3b37fSMaxime Ripard } 3461fc3b37fSMaxime Ripard 3471fc3b37fSMaxime Ripard for (i = 0; i < csi2rx->max_streams; i++) { 3481fc3b37fSMaxime Ripard char clk_name[16]; 3491fc3b37fSMaxime Ripard 3501fc3b37fSMaxime Ripard snprintf(clk_name, sizeof(clk_name), "pixel_if%u_clk", i); 3511fc3b37fSMaxime Ripard csi2rx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name); 3521fc3b37fSMaxime Ripard if (IS_ERR(csi2rx->pixel_clk[i])) { 3531fc3b37fSMaxime Ripard dev_err(&pdev->dev, "Couldn't get clock %s\n", clk_name); 3541fc3b37fSMaxime Ripard return PTR_ERR(csi2rx->pixel_clk[i]); 3551fc3b37fSMaxime Ripard } 3561fc3b37fSMaxime Ripard } 3571fc3b37fSMaxime Ripard 3581fc3b37fSMaxime Ripard return 0; 3591fc3b37fSMaxime Ripard } 3601fc3b37fSMaxime Ripard 3611fc3b37fSMaxime Ripard static int csi2rx_parse_dt(struct csi2rx_priv *csi2rx) 3621fc3b37fSMaxime Ripard { 3631fc3b37fSMaxime Ripard struct v4l2_fwnode_endpoint v4l2_ep; 3641fc3b37fSMaxime Ripard struct fwnode_handle *fwh; 3651fc3b37fSMaxime Ripard struct device_node *ep; 3661fc3b37fSMaxime Ripard int ret; 3671fc3b37fSMaxime Ripard 3681fc3b37fSMaxime Ripard ep = of_graph_get_endpoint_by_regs(csi2rx->dev->of_node, 0, 0); 3691fc3b37fSMaxime Ripard if (!ep) 3701fc3b37fSMaxime Ripard return -EINVAL; 3711fc3b37fSMaxime Ripard 3721fc3b37fSMaxime Ripard fwh = of_fwnode_handle(ep); 3731fc3b37fSMaxime Ripard ret = v4l2_fwnode_endpoint_parse(fwh, &v4l2_ep); 3741fc3b37fSMaxime Ripard if (ret) { 3751fc3b37fSMaxime Ripard dev_err(csi2rx->dev, "Could not parse v4l2 endpoint\n"); 3761fc3b37fSMaxime Ripard of_node_put(ep); 3771fc3b37fSMaxime Ripard return ret; 3781fc3b37fSMaxime Ripard } 3791fc3b37fSMaxime Ripard 3801fc3b37fSMaxime Ripard if (v4l2_ep.bus_type != V4L2_MBUS_CSI2) { 3811fc3b37fSMaxime Ripard dev_err(csi2rx->dev, "Unsupported media bus type: 0x%x\n", 3821fc3b37fSMaxime Ripard v4l2_ep.bus_type); 3831fc3b37fSMaxime Ripard of_node_put(ep); 3841fc3b37fSMaxime Ripard return -EINVAL; 3851fc3b37fSMaxime Ripard } 3861fc3b37fSMaxime Ripard 3871fc3b37fSMaxime Ripard memcpy(csi2rx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes, 3881fc3b37fSMaxime Ripard sizeof(csi2rx->lanes)); 3891fc3b37fSMaxime Ripard csi2rx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes; 3901fc3b37fSMaxime Ripard if (csi2rx->num_lanes > csi2rx->max_lanes) { 3911fc3b37fSMaxime Ripard dev_err(csi2rx->dev, "Unsupported number of data-lanes: %d\n", 3921fc3b37fSMaxime Ripard csi2rx->num_lanes); 3931fc3b37fSMaxime Ripard of_node_put(ep); 3941fc3b37fSMaxime Ripard return -EINVAL; 3951fc3b37fSMaxime Ripard } 3961fc3b37fSMaxime Ripard 3971fc3b37fSMaxime Ripard csi2rx->asd.match.fwnode = fwnode_graph_get_remote_port_parent(fwh); 3981fc3b37fSMaxime Ripard csi2rx->asd.match_type = V4L2_ASYNC_MATCH_FWNODE; 3991fc3b37fSMaxime Ripard of_node_put(ep); 4001fc3b37fSMaxime Ripard 4011fc3b37fSMaxime Ripard csi2rx->notifier.subdevs = devm_kzalloc(csi2rx->dev, 4021fc3b37fSMaxime Ripard sizeof(*csi2rx->notifier.subdevs), 4031fc3b37fSMaxime Ripard GFP_KERNEL); 4041fc3b37fSMaxime Ripard if (!csi2rx->notifier.subdevs) 4051fc3b37fSMaxime Ripard return -ENOMEM; 4061fc3b37fSMaxime Ripard 4071fc3b37fSMaxime Ripard csi2rx->notifier.subdevs[0] = &csi2rx->asd; 4081fc3b37fSMaxime Ripard csi2rx->notifier.num_subdevs = 1; 4091fc3b37fSMaxime Ripard csi2rx->notifier.ops = &csi2rx_notifier_ops; 4101fc3b37fSMaxime Ripard 4111fc3b37fSMaxime Ripard return v4l2_async_subdev_notifier_register(&csi2rx->subdev, 4121fc3b37fSMaxime Ripard &csi2rx->notifier); 4131fc3b37fSMaxime Ripard } 4141fc3b37fSMaxime Ripard 4151fc3b37fSMaxime Ripard static int csi2rx_probe(struct platform_device *pdev) 4161fc3b37fSMaxime Ripard { 4171fc3b37fSMaxime Ripard struct csi2rx_priv *csi2rx; 4181fc3b37fSMaxime Ripard unsigned int i; 4191fc3b37fSMaxime Ripard int ret; 4201fc3b37fSMaxime Ripard 4211fc3b37fSMaxime Ripard csi2rx = kzalloc(sizeof(*csi2rx), GFP_KERNEL); 4221fc3b37fSMaxime Ripard if (!csi2rx) 4231fc3b37fSMaxime Ripard return -ENOMEM; 4241fc3b37fSMaxime Ripard platform_set_drvdata(pdev, csi2rx); 4251fc3b37fSMaxime Ripard csi2rx->dev = &pdev->dev; 4261fc3b37fSMaxime Ripard mutex_init(&csi2rx->lock); 4271fc3b37fSMaxime Ripard 4281fc3b37fSMaxime Ripard ret = csi2rx_get_resources(csi2rx, pdev); 4291fc3b37fSMaxime Ripard if (ret) 4301fc3b37fSMaxime Ripard goto err_free_priv; 4311fc3b37fSMaxime Ripard 4321fc3b37fSMaxime Ripard ret = csi2rx_parse_dt(csi2rx); 4331fc3b37fSMaxime Ripard if (ret) 4341fc3b37fSMaxime Ripard goto err_free_priv; 4351fc3b37fSMaxime Ripard 4361fc3b37fSMaxime Ripard csi2rx->subdev.owner = THIS_MODULE; 4371fc3b37fSMaxime Ripard csi2rx->subdev.dev = &pdev->dev; 4381fc3b37fSMaxime Ripard v4l2_subdev_init(&csi2rx->subdev, &csi2rx_subdev_ops); 4391fc3b37fSMaxime Ripard v4l2_set_subdevdata(&csi2rx->subdev, &pdev->dev); 4401fc3b37fSMaxime Ripard snprintf(csi2rx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s", 4411fc3b37fSMaxime Ripard KBUILD_MODNAME, dev_name(&pdev->dev)); 4421fc3b37fSMaxime Ripard 4431fc3b37fSMaxime Ripard /* Create our media pads */ 4441fc3b37fSMaxime Ripard csi2rx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; 4451fc3b37fSMaxime Ripard csi2rx->pads[CSI2RX_PAD_SINK].flags = MEDIA_PAD_FL_SINK; 4461fc3b37fSMaxime Ripard for (i = CSI2RX_PAD_SOURCE_STREAM0; i < CSI2RX_PAD_MAX; i++) 4471fc3b37fSMaxime Ripard csi2rx->pads[i].flags = MEDIA_PAD_FL_SOURCE; 4481fc3b37fSMaxime Ripard 4491fc3b37fSMaxime Ripard ret = media_entity_pads_init(&csi2rx->subdev.entity, CSI2RX_PAD_MAX, 4501fc3b37fSMaxime Ripard csi2rx->pads); 4511fc3b37fSMaxime Ripard if (ret) 4521fc3b37fSMaxime Ripard goto err_free_priv; 4531fc3b37fSMaxime Ripard 4541fc3b37fSMaxime Ripard ret = v4l2_async_register_subdev(&csi2rx->subdev); 4551fc3b37fSMaxime Ripard if (ret < 0) 4561fc3b37fSMaxime Ripard goto err_free_priv; 4571fc3b37fSMaxime Ripard 4581fc3b37fSMaxime Ripard dev_info(&pdev->dev, 4591fc3b37fSMaxime Ripard "Probed CSI2RX with %u/%u lanes, %u streams, %s D-PHY\n", 4601fc3b37fSMaxime Ripard csi2rx->num_lanes, csi2rx->max_lanes, csi2rx->max_streams, 4611fc3b37fSMaxime Ripard csi2rx->has_internal_dphy ? "internal" : "no"); 4621fc3b37fSMaxime Ripard 4631fc3b37fSMaxime Ripard return 0; 4641fc3b37fSMaxime Ripard 4651fc3b37fSMaxime Ripard err_free_priv: 4661fc3b37fSMaxime Ripard kfree(csi2rx); 4671fc3b37fSMaxime Ripard return ret; 4681fc3b37fSMaxime Ripard } 4691fc3b37fSMaxime Ripard 4701fc3b37fSMaxime Ripard static int csi2rx_remove(struct platform_device *pdev) 4711fc3b37fSMaxime Ripard { 4721fc3b37fSMaxime Ripard struct csi2rx_priv *csi2rx = platform_get_drvdata(pdev); 4731fc3b37fSMaxime Ripard 4741fc3b37fSMaxime Ripard v4l2_async_unregister_subdev(&csi2rx->subdev); 4751fc3b37fSMaxime Ripard kfree(csi2rx); 4761fc3b37fSMaxime Ripard 4771fc3b37fSMaxime Ripard return 0; 4781fc3b37fSMaxime Ripard } 4791fc3b37fSMaxime Ripard 4801fc3b37fSMaxime Ripard static const struct of_device_id csi2rx_of_table[] = { 4811fc3b37fSMaxime Ripard { .compatible = "cdns,csi2rx" }, 4821fc3b37fSMaxime Ripard { }, 4831fc3b37fSMaxime Ripard }; 4841fc3b37fSMaxime Ripard MODULE_DEVICE_TABLE(of, csi2rx_of_table); 4851fc3b37fSMaxime Ripard 4861fc3b37fSMaxime Ripard static struct platform_driver csi2rx_driver = { 4871fc3b37fSMaxime Ripard .probe = csi2rx_probe, 4881fc3b37fSMaxime Ripard .remove = csi2rx_remove, 4891fc3b37fSMaxime Ripard 4901fc3b37fSMaxime Ripard .driver = { 4911fc3b37fSMaxime Ripard .name = "cdns-csi2rx", 4921fc3b37fSMaxime Ripard .of_match_table = csi2rx_of_table, 4931fc3b37fSMaxime Ripard }, 4941fc3b37fSMaxime Ripard }; 4951fc3b37fSMaxime Ripard module_platform_driver(csi2rx_driver); 4961fc3b37fSMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin.com>"); 4971fc3b37fSMaxime Ripard MODULE_DESCRIPTION("Cadence CSI2-RX controller"); 4981fc3b37fSMaxime Ripard MODULE_LICENSE("GPL"); 499