xref: /linux/drivers/media/platform/cadence/cdns-csi2rx.c (revision 3c8c153914812a98eaa0b5a6cf09c511a06aafbe)
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>
163c46ab9dSArnd Bergmann #include <linux/slab.h>
171fc3b37fSMaxime Ripard 
181fc3b37fSMaxime Ripard #include <media/v4l2-ctrls.h>
191fc3b37fSMaxime Ripard #include <media/v4l2-device.h>
201fc3b37fSMaxime Ripard #include <media/v4l2-fwnode.h>
211fc3b37fSMaxime Ripard #include <media/v4l2-subdev.h>
221fc3b37fSMaxime Ripard 
231fc3b37fSMaxime Ripard #define CSI2RX_DEVICE_CFG_REG			0x000
241fc3b37fSMaxime Ripard 
251fc3b37fSMaxime Ripard #define CSI2RX_SOFT_RESET_REG			0x004
261fc3b37fSMaxime Ripard #define CSI2RX_SOFT_RESET_PROTOCOL			BIT(1)
271fc3b37fSMaxime Ripard #define CSI2RX_SOFT_RESET_FRONT				BIT(0)
281fc3b37fSMaxime Ripard 
291fc3b37fSMaxime Ripard #define CSI2RX_STATIC_CFG_REG			0x008
301fc3b37fSMaxime Ripard #define CSI2RX_STATIC_CFG_DLANE_MAP(llane, plane)	((plane) << (16 + (llane) * 4))
311fc3b37fSMaxime Ripard #define CSI2RX_STATIC_CFG_LANES_MASK			GENMASK(11, 8)
321fc3b37fSMaxime Ripard 
331fc3b37fSMaxime Ripard #define CSI2RX_STREAM_BASE(n)		(((n) + 1) * 0x100)
341fc3b37fSMaxime Ripard 
351fc3b37fSMaxime Ripard #define CSI2RX_STREAM_CTRL_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x000)
361fc3b37fSMaxime Ripard #define CSI2RX_STREAM_CTRL_START			BIT(0)
371fc3b37fSMaxime Ripard 
381fc3b37fSMaxime Ripard #define CSI2RX_STREAM_DATA_CFG_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x008)
391fc3b37fSMaxime Ripard #define CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT		BIT(31)
401fc3b37fSMaxime Ripard #define CSI2RX_STREAM_DATA_CFG_VC_SELECT(n)		BIT((n) + 16)
411fc3b37fSMaxime Ripard 
421fc3b37fSMaxime Ripard #define CSI2RX_STREAM_CFG_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x00c)
431fc3b37fSMaxime Ripard #define CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF		(1 << 8)
441fc3b37fSMaxime Ripard 
451fc3b37fSMaxime Ripard #define CSI2RX_LANES_MAX	4
461fc3b37fSMaxime Ripard #define CSI2RX_STREAMS_MAX	4
471fc3b37fSMaxime Ripard 
481fc3b37fSMaxime Ripard enum csi2rx_pads {
491fc3b37fSMaxime Ripard 	CSI2RX_PAD_SINK,
501fc3b37fSMaxime Ripard 	CSI2RX_PAD_SOURCE_STREAM0,
511fc3b37fSMaxime Ripard 	CSI2RX_PAD_SOURCE_STREAM1,
521fc3b37fSMaxime Ripard 	CSI2RX_PAD_SOURCE_STREAM2,
531fc3b37fSMaxime Ripard 	CSI2RX_PAD_SOURCE_STREAM3,
541fc3b37fSMaxime Ripard 	CSI2RX_PAD_MAX,
551fc3b37fSMaxime Ripard };
561fc3b37fSMaxime Ripard 
571fc3b37fSMaxime Ripard struct csi2rx_priv {
581fc3b37fSMaxime Ripard 	struct device			*dev;
591fc3b37fSMaxime Ripard 	unsigned int			count;
601fc3b37fSMaxime Ripard 
611fc3b37fSMaxime Ripard 	/*
621fc3b37fSMaxime Ripard 	 * Used to prevent race conditions between multiple,
631fc3b37fSMaxime Ripard 	 * concurrent calls to start and stop.
641fc3b37fSMaxime Ripard 	 */
651fc3b37fSMaxime Ripard 	struct mutex			lock;
661fc3b37fSMaxime Ripard 
671fc3b37fSMaxime Ripard 	void __iomem			*base;
681fc3b37fSMaxime Ripard 	struct clk			*sys_clk;
691fc3b37fSMaxime Ripard 	struct clk			*p_clk;
701fc3b37fSMaxime Ripard 	struct clk			*pixel_clk[CSI2RX_STREAMS_MAX];
711fc3b37fSMaxime Ripard 	struct phy			*dphy;
721fc3b37fSMaxime Ripard 
731fc3b37fSMaxime Ripard 	u8				lanes[CSI2RX_LANES_MAX];
741fc3b37fSMaxime Ripard 	u8				num_lanes;
751fc3b37fSMaxime Ripard 	u8				max_lanes;
761fc3b37fSMaxime Ripard 	u8				max_streams;
771fc3b37fSMaxime Ripard 	bool				has_internal_dphy;
781fc3b37fSMaxime Ripard 
791fc3b37fSMaxime Ripard 	struct v4l2_subdev		subdev;
801fc3b37fSMaxime Ripard 	struct v4l2_async_notifier	notifier;
811fc3b37fSMaxime Ripard 	struct media_pad		pads[CSI2RX_PAD_MAX];
821fc3b37fSMaxime Ripard 
831fc3b37fSMaxime Ripard 	/* Remote source */
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,
1312eca8e4cSChristophe JAILLET 						       csi2rx->max_lanes);
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:
17728d42d2fSSakari Ailus 	for (; i > 0; i--)
17828d42d2fSSakari 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;
285cca65f64SEvgeny Novikov 	int ret;
2861fc3b37fSMaxime Ripard 
2871fc3b37fSMaxime Ripard 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2881fc3b37fSMaxime Ripard 	csi2rx->base = devm_ioremap_resource(&pdev->dev, res);
2891fc3b37fSMaxime Ripard 	if (IS_ERR(csi2rx->base))
2901fc3b37fSMaxime Ripard 		return PTR_ERR(csi2rx->base);
2911fc3b37fSMaxime Ripard 
2921fc3b37fSMaxime Ripard 	csi2rx->sys_clk = devm_clk_get(&pdev->dev, "sys_clk");
2931fc3b37fSMaxime Ripard 	if (IS_ERR(csi2rx->sys_clk)) {
2941fc3b37fSMaxime Ripard 		dev_err(&pdev->dev, "Couldn't get sys clock\n");
2951fc3b37fSMaxime Ripard 		return PTR_ERR(csi2rx->sys_clk);
2961fc3b37fSMaxime Ripard 	}
2971fc3b37fSMaxime Ripard 
2981fc3b37fSMaxime Ripard 	csi2rx->p_clk = devm_clk_get(&pdev->dev, "p_clk");
2991fc3b37fSMaxime Ripard 	if (IS_ERR(csi2rx->p_clk)) {
3001fc3b37fSMaxime Ripard 		dev_err(&pdev->dev, "Couldn't get P clock\n");
3011fc3b37fSMaxime Ripard 		return PTR_ERR(csi2rx->p_clk);
3021fc3b37fSMaxime Ripard 	}
3031fc3b37fSMaxime Ripard 
3041fc3b37fSMaxime Ripard 	csi2rx->dphy = devm_phy_optional_get(&pdev->dev, "dphy");
3051fc3b37fSMaxime Ripard 	if (IS_ERR(csi2rx->dphy)) {
3061fc3b37fSMaxime Ripard 		dev_err(&pdev->dev, "Couldn't get external D-PHY\n");
3071fc3b37fSMaxime Ripard 		return PTR_ERR(csi2rx->dphy);
3081fc3b37fSMaxime Ripard 	}
3091fc3b37fSMaxime Ripard 
3101fc3b37fSMaxime Ripard 	/*
3111fc3b37fSMaxime Ripard 	 * FIXME: Once we'll have external D-PHY support, the check
3121fc3b37fSMaxime Ripard 	 * will need to be removed.
3131fc3b37fSMaxime Ripard 	 */
3141fc3b37fSMaxime Ripard 	if (csi2rx->dphy) {
3151fc3b37fSMaxime Ripard 		dev_err(&pdev->dev, "External D-PHY not supported yet\n");
3161fc3b37fSMaxime Ripard 		return -EINVAL;
3171fc3b37fSMaxime Ripard 	}
3181fc3b37fSMaxime Ripard 
319cca65f64SEvgeny Novikov 	ret = clk_prepare_enable(csi2rx->p_clk);
320cca65f64SEvgeny Novikov 	if (ret) {
321cca65f64SEvgeny Novikov 		dev_err(&pdev->dev, "Couldn't prepare and enable P clock\n");
322cca65f64SEvgeny Novikov 		return ret;
323cca65f64SEvgeny Novikov 	}
324cca65f64SEvgeny Novikov 
3251fc3b37fSMaxime Ripard 	dev_cfg = readl(csi2rx->base + CSI2RX_DEVICE_CFG_REG);
3261fc3b37fSMaxime Ripard 	clk_disable_unprepare(csi2rx->p_clk);
3271fc3b37fSMaxime Ripard 
3281fc3b37fSMaxime Ripard 	csi2rx->max_lanes = dev_cfg & 7;
3291fc3b37fSMaxime Ripard 	if (csi2rx->max_lanes > CSI2RX_LANES_MAX) {
3301fc3b37fSMaxime Ripard 		dev_err(&pdev->dev, "Invalid number of lanes: %u\n",
3311fc3b37fSMaxime Ripard 			csi2rx->max_lanes);
3321fc3b37fSMaxime Ripard 		return -EINVAL;
3331fc3b37fSMaxime Ripard 	}
3341fc3b37fSMaxime Ripard 
3351fc3b37fSMaxime Ripard 	csi2rx->max_streams = (dev_cfg >> 4) & 7;
3361fc3b37fSMaxime Ripard 	if (csi2rx->max_streams > CSI2RX_STREAMS_MAX) {
3371fc3b37fSMaxime Ripard 		dev_err(&pdev->dev, "Invalid number of streams: %u\n",
3381fc3b37fSMaxime Ripard 			csi2rx->max_streams);
3391fc3b37fSMaxime Ripard 		return -EINVAL;
3401fc3b37fSMaxime Ripard 	}
3411fc3b37fSMaxime Ripard 
3421fc3b37fSMaxime Ripard 	csi2rx->has_internal_dphy = dev_cfg & BIT(3) ? true : false;
3431fc3b37fSMaxime Ripard 
3441fc3b37fSMaxime Ripard 	/*
3451fc3b37fSMaxime Ripard 	 * FIXME: Once we'll have internal D-PHY support, the check
3461fc3b37fSMaxime Ripard 	 * will need to be removed.
3471fc3b37fSMaxime Ripard 	 */
3481fc3b37fSMaxime Ripard 	if (csi2rx->has_internal_dphy) {
3491fc3b37fSMaxime Ripard 		dev_err(&pdev->dev, "Internal D-PHY not supported yet\n");
3501fc3b37fSMaxime Ripard 		return -EINVAL;
3511fc3b37fSMaxime Ripard 	}
3521fc3b37fSMaxime Ripard 
3531fc3b37fSMaxime Ripard 	for (i = 0; i < csi2rx->max_streams; i++) {
3541fc3b37fSMaxime Ripard 		char clk_name[16];
3551fc3b37fSMaxime Ripard 
3561fc3b37fSMaxime Ripard 		snprintf(clk_name, sizeof(clk_name), "pixel_if%u_clk", i);
3571fc3b37fSMaxime Ripard 		csi2rx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name);
3581fc3b37fSMaxime Ripard 		if (IS_ERR(csi2rx->pixel_clk[i])) {
3591fc3b37fSMaxime Ripard 			dev_err(&pdev->dev, "Couldn't get clock %s\n", clk_name);
3601fc3b37fSMaxime Ripard 			return PTR_ERR(csi2rx->pixel_clk[i]);
3611fc3b37fSMaxime Ripard 		}
3621fc3b37fSMaxime Ripard 	}
3631fc3b37fSMaxime Ripard 
3641fc3b37fSMaxime Ripard 	return 0;
3651fc3b37fSMaxime Ripard }
3661fc3b37fSMaxime Ripard 
3671fc3b37fSMaxime Ripard static int csi2rx_parse_dt(struct csi2rx_priv *csi2rx)
3681fc3b37fSMaxime Ripard {
36960359a28SSakari Ailus 	struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
37088367b15SEzequiel Garcia 	struct v4l2_async_subdev *asd;
3711fc3b37fSMaxime Ripard 	struct fwnode_handle *fwh;
3721fc3b37fSMaxime Ripard 	struct device_node *ep;
3731fc3b37fSMaxime Ripard 	int ret;
3741fc3b37fSMaxime Ripard 
3751fc3b37fSMaxime Ripard 	ep = of_graph_get_endpoint_by_regs(csi2rx->dev->of_node, 0, 0);
3761fc3b37fSMaxime Ripard 	if (!ep)
3771fc3b37fSMaxime Ripard 		return -EINVAL;
3781fc3b37fSMaxime Ripard 
3791fc3b37fSMaxime Ripard 	fwh = of_fwnode_handle(ep);
3801fc3b37fSMaxime Ripard 	ret = v4l2_fwnode_endpoint_parse(fwh, &v4l2_ep);
3811fc3b37fSMaxime Ripard 	if (ret) {
3821fc3b37fSMaxime Ripard 		dev_err(csi2rx->dev, "Could not parse v4l2 endpoint\n");
3831fc3b37fSMaxime Ripard 		of_node_put(ep);
3841fc3b37fSMaxime Ripard 		return ret;
3851fc3b37fSMaxime Ripard 	}
3861fc3b37fSMaxime Ripard 
3872d95e7edSSakari Ailus 	if (v4l2_ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
3881fc3b37fSMaxime Ripard 		dev_err(csi2rx->dev, "Unsupported media bus type: 0x%x\n",
3891fc3b37fSMaxime Ripard 			v4l2_ep.bus_type);
3901fc3b37fSMaxime Ripard 		of_node_put(ep);
3911fc3b37fSMaxime Ripard 		return -EINVAL;
3921fc3b37fSMaxime Ripard 	}
3931fc3b37fSMaxime Ripard 
3941fc3b37fSMaxime Ripard 	memcpy(csi2rx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes,
3951fc3b37fSMaxime Ripard 	       sizeof(csi2rx->lanes));
3961fc3b37fSMaxime Ripard 	csi2rx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
3971fc3b37fSMaxime Ripard 	if (csi2rx->num_lanes > csi2rx->max_lanes) {
3981fc3b37fSMaxime Ripard 		dev_err(csi2rx->dev, "Unsupported number of data-lanes: %d\n",
3991fc3b37fSMaxime Ripard 			csi2rx->num_lanes);
4001fc3b37fSMaxime Ripard 		of_node_put(ep);
4011fc3b37fSMaxime Ripard 		return -EINVAL;
4021fc3b37fSMaxime Ripard 	}
4031fc3b37fSMaxime Ripard 
404*3c8c1539SSakari Ailus 	v4l2_async_nf_init(&csi2rx->notifier);
4051fc3b37fSMaxime Ripard 
406*3c8c1539SSakari Ailus 	asd = v4l2_async_nf_add_fwnode_remote(&csi2rx->notifier, fwh,
407b01edcbdSLaurent Pinchart 					      struct v4l2_async_subdev);
40888367b15SEzequiel Garcia 	of_node_put(ep);
40988367b15SEzequiel Garcia 	if (IS_ERR(asd))
41088367b15SEzequiel Garcia 		return PTR_ERR(asd);
411d079f94cSSteve Longerbeam 
4121fc3b37fSMaxime Ripard 	csi2rx->notifier.ops = &csi2rx_notifier_ops;
4131fc3b37fSMaxime Ripard 
414*3c8c1539SSakari Ailus 	ret = v4l2_async_subdev_nf_register(&csi2rx->subdev, &csi2rx->notifier);
415d079f94cSSteve Longerbeam 	if (ret)
416*3c8c1539SSakari Ailus 		v4l2_async_nf_cleanup(&csi2rx->notifier);
417d079f94cSSteve Longerbeam 
418d079f94cSSteve Longerbeam 	return ret;
4191fc3b37fSMaxime Ripard }
4201fc3b37fSMaxime Ripard 
4211fc3b37fSMaxime Ripard static int csi2rx_probe(struct platform_device *pdev)
4221fc3b37fSMaxime Ripard {
4231fc3b37fSMaxime Ripard 	struct csi2rx_priv *csi2rx;
4241fc3b37fSMaxime Ripard 	unsigned int i;
4251fc3b37fSMaxime Ripard 	int ret;
4261fc3b37fSMaxime Ripard 
4271fc3b37fSMaxime Ripard 	csi2rx = kzalloc(sizeof(*csi2rx), GFP_KERNEL);
4281fc3b37fSMaxime Ripard 	if (!csi2rx)
4291fc3b37fSMaxime Ripard 		return -ENOMEM;
4301fc3b37fSMaxime Ripard 	platform_set_drvdata(pdev, csi2rx);
4311fc3b37fSMaxime Ripard 	csi2rx->dev = &pdev->dev;
4321fc3b37fSMaxime Ripard 	mutex_init(&csi2rx->lock);
4331fc3b37fSMaxime Ripard 
4341fc3b37fSMaxime Ripard 	ret = csi2rx_get_resources(csi2rx, pdev);
4351fc3b37fSMaxime Ripard 	if (ret)
4361fc3b37fSMaxime Ripard 		goto err_free_priv;
4371fc3b37fSMaxime Ripard 
4381fc3b37fSMaxime Ripard 	ret = csi2rx_parse_dt(csi2rx);
4391fc3b37fSMaxime Ripard 	if (ret)
4401fc3b37fSMaxime Ripard 		goto err_free_priv;
4411fc3b37fSMaxime Ripard 
4421fc3b37fSMaxime Ripard 	csi2rx->subdev.owner = THIS_MODULE;
4431fc3b37fSMaxime Ripard 	csi2rx->subdev.dev = &pdev->dev;
4441fc3b37fSMaxime Ripard 	v4l2_subdev_init(&csi2rx->subdev, &csi2rx_subdev_ops);
4451fc3b37fSMaxime Ripard 	v4l2_set_subdevdata(&csi2rx->subdev, &pdev->dev);
4461fc3b37fSMaxime Ripard 	snprintf(csi2rx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s",
4471fc3b37fSMaxime Ripard 		 KBUILD_MODNAME, dev_name(&pdev->dev));
4481fc3b37fSMaxime Ripard 
4491fc3b37fSMaxime Ripard 	/* Create our media pads */
4501fc3b37fSMaxime Ripard 	csi2rx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
4511fc3b37fSMaxime Ripard 	csi2rx->pads[CSI2RX_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
4521fc3b37fSMaxime Ripard 	for (i = CSI2RX_PAD_SOURCE_STREAM0; i < CSI2RX_PAD_MAX; i++)
4531fc3b37fSMaxime Ripard 		csi2rx->pads[i].flags = MEDIA_PAD_FL_SOURCE;
4541fc3b37fSMaxime Ripard 
4551fc3b37fSMaxime Ripard 	ret = media_entity_pads_init(&csi2rx->subdev.entity, CSI2RX_PAD_MAX,
4561fc3b37fSMaxime Ripard 				     csi2rx->pads);
4571fc3b37fSMaxime Ripard 	if (ret)
458d079f94cSSteve Longerbeam 		goto err_cleanup;
4591fc3b37fSMaxime Ripard 
4601fc3b37fSMaxime Ripard 	ret = v4l2_async_register_subdev(&csi2rx->subdev);
4611fc3b37fSMaxime Ripard 	if (ret < 0)
462d079f94cSSteve Longerbeam 		goto err_cleanup;
4631fc3b37fSMaxime Ripard 
4641fc3b37fSMaxime Ripard 	dev_info(&pdev->dev,
4651fc3b37fSMaxime Ripard 		 "Probed CSI2RX with %u/%u lanes, %u streams, %s D-PHY\n",
4661fc3b37fSMaxime Ripard 		 csi2rx->num_lanes, csi2rx->max_lanes, csi2rx->max_streams,
4671fc3b37fSMaxime Ripard 		 csi2rx->has_internal_dphy ? "internal" : "no");
4681fc3b37fSMaxime Ripard 
4691fc3b37fSMaxime Ripard 	return 0;
4701fc3b37fSMaxime Ripard 
471d079f94cSSteve Longerbeam err_cleanup:
472*3c8c1539SSakari Ailus 	v4l2_async_nf_cleanup(&csi2rx->notifier);
4731fc3b37fSMaxime Ripard err_free_priv:
4741fc3b37fSMaxime Ripard 	kfree(csi2rx);
4751fc3b37fSMaxime Ripard 	return ret;
4761fc3b37fSMaxime Ripard }
4771fc3b37fSMaxime Ripard 
4781fc3b37fSMaxime Ripard static int csi2rx_remove(struct platform_device *pdev)
4791fc3b37fSMaxime Ripard {
4801fc3b37fSMaxime Ripard 	struct csi2rx_priv *csi2rx = platform_get_drvdata(pdev);
4811fc3b37fSMaxime Ripard 
4821fc3b37fSMaxime Ripard 	v4l2_async_unregister_subdev(&csi2rx->subdev);
4831fc3b37fSMaxime Ripard 	kfree(csi2rx);
4841fc3b37fSMaxime Ripard 
4851fc3b37fSMaxime Ripard 	return 0;
4861fc3b37fSMaxime Ripard }
4871fc3b37fSMaxime Ripard 
4881fc3b37fSMaxime Ripard static const struct of_device_id csi2rx_of_table[] = {
4891fc3b37fSMaxime Ripard 	{ .compatible = "cdns,csi2rx" },
4901fc3b37fSMaxime Ripard 	{ },
4911fc3b37fSMaxime Ripard };
4921fc3b37fSMaxime Ripard MODULE_DEVICE_TABLE(of, csi2rx_of_table);
4931fc3b37fSMaxime Ripard 
4941fc3b37fSMaxime Ripard static struct platform_driver csi2rx_driver = {
4951fc3b37fSMaxime Ripard 	.probe	= csi2rx_probe,
4961fc3b37fSMaxime Ripard 	.remove	= csi2rx_remove,
4971fc3b37fSMaxime Ripard 
4981fc3b37fSMaxime Ripard 	.driver	= {
4991fc3b37fSMaxime Ripard 		.name		= "cdns-csi2rx",
5001fc3b37fSMaxime Ripard 		.of_match_table	= csi2rx_of_table,
5011fc3b37fSMaxime Ripard 	},
5021fc3b37fSMaxime Ripard };
5031fc3b37fSMaxime Ripard module_platform_driver(csi2rx_driver);
5041fc3b37fSMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin.com>");
5051fc3b37fSMaxime Ripard MODULE_DESCRIPTION("Cadence CSI2-RX controller");
5061fc3b37fSMaxime Ripard MODULE_LICENSE("GPL");
507