16f684d4fSMaxime Ripard // SPDX-License-Identifier: GPL-2.0+
26f684d4fSMaxime Ripard /*
36f684d4fSMaxime Ripard * Driver for Cadence MIPI-CSI2 TX Controller
46f684d4fSMaxime Ripard *
5bf9df90bSJan Kotas * Copyright (C) 2017-2019 Cadence Design Systems Inc.
66f684d4fSMaxime Ripard */
76f684d4fSMaxime Ripard
86f684d4fSMaxime Ripard #include <linux/clk.h>
96f684d4fSMaxime Ripard #include <linux/delay.h>
106f684d4fSMaxime Ripard #include <linux/io.h>
116f684d4fSMaxime Ripard #include <linux/module.h>
126f684d4fSMaxime Ripard #include <linux/mutex.h>
136f684d4fSMaxime Ripard #include <linux/of.h>
146f684d4fSMaxime Ripard #include <linux/of_graph.h>
156f684d4fSMaxime Ripard #include <linux/platform_device.h>
163c46ab9dSArnd Bergmann #include <linux/slab.h>
176f684d4fSMaxime Ripard
18f87c445cSLaurent Pinchart #include <media/mipi-csi2.h>
196f684d4fSMaxime Ripard #include <media/v4l2-ctrls.h>
206f684d4fSMaxime Ripard #include <media/v4l2-device.h>
216f684d4fSMaxime Ripard #include <media/v4l2-fwnode.h>
226f684d4fSMaxime Ripard #include <media/v4l2-subdev.h>
236f684d4fSMaxime Ripard
246f684d4fSMaxime Ripard #define CSI2TX_DEVICE_CONFIG_REG 0x00
256f684d4fSMaxime Ripard #define CSI2TX_DEVICE_CONFIG_STREAMS_MASK GENMASK(6, 4)
266f684d4fSMaxime Ripard #define CSI2TX_DEVICE_CONFIG_HAS_DPHY BIT(3)
276f684d4fSMaxime Ripard #define CSI2TX_DEVICE_CONFIG_LANES_MASK GENMASK(2, 0)
286f684d4fSMaxime Ripard
296f684d4fSMaxime Ripard #define CSI2TX_CONFIG_REG 0x20
306f684d4fSMaxime Ripard #define CSI2TX_CONFIG_CFG_REQ BIT(2)
316f684d4fSMaxime Ripard #define CSI2TX_CONFIG_SRST_REQ BIT(1)
326f684d4fSMaxime Ripard
336f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_REG 0x28
346f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_CLK_RESET BIT(16)
356f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_LANE_RESET(n) BIT((n) + 12)
366f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_MODE_MASK GENMASK(9, 8)
376f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_MODE_LPDT (2 << 8)
386f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_MODE_HS (1 << 8)
396f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_MODE_ULPS (0 << 8)
406f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_CLK_ENABLE BIT(4)
416f684d4fSMaxime Ripard #define CSI2TX_DPHY_CFG_LANE_ENABLE(n) BIT(n)
426f684d4fSMaxime Ripard
436f684d4fSMaxime Ripard #define CSI2TX_DPHY_CLK_WAKEUP_REG 0x2c
446f684d4fSMaxime Ripard #define CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(n) ((n) & 0xffff)
456f684d4fSMaxime Ripard
466f684d4fSMaxime Ripard #define CSI2TX_DT_CFG_REG(n) (0x80 + (n) * 8)
476f684d4fSMaxime Ripard #define CSI2TX_DT_CFG_DT(n) (((n) & 0x3f) << 2)
486f684d4fSMaxime Ripard
496f684d4fSMaxime Ripard #define CSI2TX_DT_FORMAT_REG(n) (0x84 + (n) * 8)
506f684d4fSMaxime Ripard #define CSI2TX_DT_FORMAT_BYTES_PER_LINE(n) (((n) & 0xffff) << 16)
516f684d4fSMaxime Ripard #define CSI2TX_DT_FORMAT_MAX_LINE_NUM(n) ((n) & 0xffff)
526f684d4fSMaxime Ripard
536f684d4fSMaxime Ripard #define CSI2TX_STREAM_IF_CFG_REG(n) (0x100 + (n) * 4)
546f684d4fSMaxime Ripard #define CSI2TX_STREAM_IF_CFG_FILL_LEVEL(n) ((n) & 0x1f)
556f684d4fSMaxime Ripard
56050ff2adSJan Kotas /* CSI2TX V2 Registers */
57050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_REG 0x28
58050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_RESET BIT(16)
59050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_CLOCK_MODE BIT(10)
60050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_MODE_MASK GENMASK(9, 8)
61050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_MODE_LPDT (2 << 8)
62050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_MODE_HS (1 << 8)
63050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_MODE_ULPS (0 << 8)
64050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_CLK_ENABLE BIT(4)
65050ff2adSJan Kotas #define CSI2TX_V2_DPHY_CFG_LANE_ENABLE(n) BIT(n)
66050ff2adSJan Kotas
676f684d4fSMaxime Ripard #define CSI2TX_LANES_MAX 4
686f684d4fSMaxime Ripard #define CSI2TX_STREAMS_MAX 4
696f684d4fSMaxime Ripard
706f684d4fSMaxime Ripard enum csi2tx_pads {
716f684d4fSMaxime Ripard CSI2TX_PAD_SOURCE,
726f684d4fSMaxime Ripard CSI2TX_PAD_SINK_STREAM0,
736f684d4fSMaxime Ripard CSI2TX_PAD_SINK_STREAM1,
746f684d4fSMaxime Ripard CSI2TX_PAD_SINK_STREAM2,
756f684d4fSMaxime Ripard CSI2TX_PAD_SINK_STREAM3,
766f684d4fSMaxime Ripard CSI2TX_PAD_MAX,
776f684d4fSMaxime Ripard };
786f684d4fSMaxime Ripard
796f684d4fSMaxime Ripard struct csi2tx_fmt {
806f684d4fSMaxime Ripard u32 mbus;
816f684d4fSMaxime Ripard u32 dt;
826f684d4fSMaxime Ripard u32 bpp;
836f684d4fSMaxime Ripard };
846f684d4fSMaxime Ripard
85050ff2adSJan Kotas struct csi2tx_priv;
86050ff2adSJan Kotas
87050ff2adSJan Kotas /* CSI2TX Variant Operations */
88050ff2adSJan Kotas struct csi2tx_vops {
89050ff2adSJan Kotas void (*dphy_setup)(struct csi2tx_priv *csi2tx);
90050ff2adSJan Kotas };
91050ff2adSJan Kotas
926f684d4fSMaxime Ripard struct csi2tx_priv {
936f684d4fSMaxime Ripard struct device *dev;
946f684d4fSMaxime Ripard unsigned int count;
956f684d4fSMaxime Ripard
966f684d4fSMaxime Ripard /*
976f684d4fSMaxime Ripard * Used to prevent race conditions between multiple,
986f684d4fSMaxime Ripard * concurrent calls to start and stop.
996f684d4fSMaxime Ripard */
1006f684d4fSMaxime Ripard struct mutex lock;
1016f684d4fSMaxime Ripard
1026f684d4fSMaxime Ripard void __iomem *base;
1036f684d4fSMaxime Ripard
104050ff2adSJan Kotas struct csi2tx_vops *vops;
105050ff2adSJan Kotas
1066f684d4fSMaxime Ripard struct clk *esc_clk;
1076f684d4fSMaxime Ripard struct clk *p_clk;
1086f684d4fSMaxime Ripard struct clk *pixel_clk[CSI2TX_STREAMS_MAX];
1096f684d4fSMaxime Ripard
1106f684d4fSMaxime Ripard struct v4l2_subdev subdev;
1116f684d4fSMaxime Ripard struct media_pad pads[CSI2TX_PAD_MAX];
1126f684d4fSMaxime Ripard struct v4l2_mbus_framefmt pad_fmts[CSI2TX_PAD_MAX];
1136f684d4fSMaxime Ripard
1146f684d4fSMaxime Ripard bool has_internal_dphy;
1156f684d4fSMaxime Ripard u8 lanes[CSI2TX_LANES_MAX];
1166f684d4fSMaxime Ripard unsigned int num_lanes;
1176f684d4fSMaxime Ripard unsigned int max_lanes;
1186f684d4fSMaxime Ripard unsigned int max_streams;
1196f684d4fSMaxime Ripard };
1206f684d4fSMaxime Ripard
1216f684d4fSMaxime Ripard static const struct csi2tx_fmt csi2tx_formats[] = {
1226f684d4fSMaxime Ripard {
1236f684d4fSMaxime Ripard .mbus = MEDIA_BUS_FMT_UYVY8_1X16,
1246f684d4fSMaxime Ripard .bpp = 2,
125f87c445cSLaurent Pinchart .dt = MIPI_CSI2_DT_YUV422_8B,
1266f684d4fSMaxime Ripard },
1276f684d4fSMaxime Ripard {
1286f684d4fSMaxime Ripard .mbus = MEDIA_BUS_FMT_RGB888_1X24,
1296f684d4fSMaxime Ripard .bpp = 3,
130f87c445cSLaurent Pinchart .dt = MIPI_CSI2_DT_RGB888,
1316f684d4fSMaxime Ripard },
1326f684d4fSMaxime Ripard };
1336f684d4fSMaxime Ripard
1346f684d4fSMaxime Ripard static const struct v4l2_mbus_framefmt fmt_default = {
1356f684d4fSMaxime Ripard .width = 1280,
1366f684d4fSMaxime Ripard .height = 720,
1376f684d4fSMaxime Ripard .code = MEDIA_BUS_FMT_RGB888_1X24,
1386f684d4fSMaxime Ripard .field = V4L2_FIELD_NONE,
1396f684d4fSMaxime Ripard .colorspace = V4L2_COLORSPACE_DEFAULT,
1406f684d4fSMaxime Ripard };
1416f684d4fSMaxime Ripard
1426f684d4fSMaxime Ripard static inline
v4l2_subdev_to_csi2tx(struct v4l2_subdev * subdev)1436f684d4fSMaxime Ripard struct csi2tx_priv *v4l2_subdev_to_csi2tx(struct v4l2_subdev *subdev)
1446f684d4fSMaxime Ripard {
1456f684d4fSMaxime Ripard return container_of(subdev, struct csi2tx_priv, subdev);
1466f684d4fSMaxime Ripard }
1476f684d4fSMaxime Ripard
csi2tx_get_fmt_from_mbus(u32 mbus)1486f684d4fSMaxime Ripard static const struct csi2tx_fmt *csi2tx_get_fmt_from_mbus(u32 mbus)
1496f684d4fSMaxime Ripard {
1506f684d4fSMaxime Ripard unsigned int i;
1516f684d4fSMaxime Ripard
1526f684d4fSMaxime Ripard for (i = 0; i < ARRAY_SIZE(csi2tx_formats); i++)
1536f684d4fSMaxime Ripard if (csi2tx_formats[i].mbus == mbus)
1546f684d4fSMaxime Ripard return &csi2tx_formats[i];
1556f684d4fSMaxime Ripard
1566f684d4fSMaxime Ripard return NULL;
1576f684d4fSMaxime Ripard }
1586f684d4fSMaxime Ripard
csi2tx_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)1596f684d4fSMaxime Ripard static int csi2tx_enum_mbus_code(struct v4l2_subdev *subdev,
1600d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1616f684d4fSMaxime Ripard struct v4l2_subdev_mbus_code_enum *code)
1626f684d4fSMaxime Ripard {
1636f684d4fSMaxime Ripard if (code->pad || code->index >= ARRAY_SIZE(csi2tx_formats))
1646f684d4fSMaxime Ripard return -EINVAL;
1656f684d4fSMaxime Ripard
1666f684d4fSMaxime Ripard code->code = csi2tx_formats[code->index].mbus;
1676f684d4fSMaxime Ripard
1686f684d4fSMaxime Ripard return 0;
1696f684d4fSMaxime Ripard }
1706f684d4fSMaxime Ripard
1716f684d4fSMaxime Ripard static struct v4l2_mbus_framefmt *
__csi2tx_get_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1726f684d4fSMaxime Ripard __csi2tx_get_pad_format(struct v4l2_subdev *subdev,
1730d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1746f684d4fSMaxime Ripard struct v4l2_subdev_format *fmt)
1756f684d4fSMaxime Ripard {
1766f684d4fSMaxime Ripard struct csi2tx_priv *csi2tx = v4l2_subdev_to_csi2tx(subdev);
1776f684d4fSMaxime Ripard
1786f684d4fSMaxime Ripard if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1790d346d2aSTomi Valkeinen return v4l2_subdev_get_try_format(subdev, sd_state,
1806f684d4fSMaxime Ripard fmt->pad);
1816f684d4fSMaxime Ripard
1826f684d4fSMaxime Ripard return &csi2tx->pad_fmts[fmt->pad];
1836f684d4fSMaxime Ripard }
1846f684d4fSMaxime Ripard
csi2tx_get_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1856f684d4fSMaxime Ripard static int csi2tx_get_pad_format(struct v4l2_subdev *subdev,
1860d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1876f684d4fSMaxime Ripard struct v4l2_subdev_format *fmt)
1886f684d4fSMaxime Ripard {
1896f684d4fSMaxime Ripard const struct v4l2_mbus_framefmt *format;
1906f684d4fSMaxime Ripard
1916f684d4fSMaxime Ripard /* Multiplexed pad? */
1926f684d4fSMaxime Ripard if (fmt->pad == CSI2TX_PAD_SOURCE)
1936f684d4fSMaxime Ripard return -EINVAL;
1946f684d4fSMaxime Ripard
1950d346d2aSTomi Valkeinen format = __csi2tx_get_pad_format(subdev, sd_state, fmt);
1966f684d4fSMaxime Ripard if (!format)
1976f684d4fSMaxime Ripard return -EINVAL;
1986f684d4fSMaxime Ripard
1996f684d4fSMaxime Ripard fmt->format = *format;
2006f684d4fSMaxime Ripard
2016f684d4fSMaxime Ripard return 0;
2026f684d4fSMaxime Ripard }
2036f684d4fSMaxime Ripard
csi2tx_set_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)2046f684d4fSMaxime Ripard static int csi2tx_set_pad_format(struct v4l2_subdev *subdev,
2050d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
2066f684d4fSMaxime Ripard struct v4l2_subdev_format *fmt)
2076f684d4fSMaxime Ripard {
2086f684d4fSMaxime Ripard const struct v4l2_mbus_framefmt *src_format = &fmt->format;
2096f684d4fSMaxime Ripard struct v4l2_mbus_framefmt *dst_format;
2106f684d4fSMaxime Ripard
2116f684d4fSMaxime Ripard /* Multiplexed pad? */
2126f684d4fSMaxime Ripard if (fmt->pad == CSI2TX_PAD_SOURCE)
2136f684d4fSMaxime Ripard return -EINVAL;
2146f684d4fSMaxime Ripard
2156f684d4fSMaxime Ripard if (!csi2tx_get_fmt_from_mbus(fmt->format.code))
2166f684d4fSMaxime Ripard src_format = &fmt_default;
2176f684d4fSMaxime Ripard
2180d346d2aSTomi Valkeinen dst_format = __csi2tx_get_pad_format(subdev, sd_state, fmt);
2196f684d4fSMaxime Ripard if (!dst_format)
2206f684d4fSMaxime Ripard return -EINVAL;
2216f684d4fSMaxime Ripard
2226f684d4fSMaxime Ripard *dst_format = *src_format;
2236f684d4fSMaxime Ripard
2246f684d4fSMaxime Ripard return 0;
2256f684d4fSMaxime Ripard }
2266f684d4fSMaxime Ripard
2276f684d4fSMaxime Ripard static const struct v4l2_subdev_pad_ops csi2tx_pad_ops = {
2286f684d4fSMaxime Ripard .enum_mbus_code = csi2tx_enum_mbus_code,
2296f684d4fSMaxime Ripard .get_fmt = csi2tx_get_pad_format,
2306f684d4fSMaxime Ripard .set_fmt = csi2tx_set_pad_format,
2316f684d4fSMaxime Ripard };
2326f684d4fSMaxime Ripard
233050ff2adSJan Kotas /* Set Wake Up value in the D-PHY */
csi2tx_dphy_set_wakeup(struct csi2tx_priv * csi2tx)234050ff2adSJan Kotas static void csi2tx_dphy_set_wakeup(struct csi2tx_priv *csi2tx)
2356f684d4fSMaxime Ripard {
2366f684d4fSMaxime Ripard writel(CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(32),
2376f684d4fSMaxime Ripard csi2tx->base + CSI2TX_DPHY_CLK_WAKEUP_REG);
238050ff2adSJan Kotas }
2396f684d4fSMaxime Ripard
240050ff2adSJan Kotas /*
241050ff2adSJan Kotas * Finishes the D-PHY initialization
242050ff2adSJan Kotas * reg dphy cfg value to be used
243050ff2adSJan Kotas */
csi2tx_dphy_init_finish(struct csi2tx_priv * csi2tx,u32 reg)244050ff2adSJan Kotas static void csi2tx_dphy_init_finish(struct csi2tx_priv *csi2tx, u32 reg)
245050ff2adSJan Kotas {
246050ff2adSJan Kotas unsigned int i;
2476f684d4fSMaxime Ripard
2486f684d4fSMaxime Ripard udelay(10);
2496f684d4fSMaxime Ripard
2506f684d4fSMaxime Ripard /* Enable our (clock and data) lanes */
2516f684d4fSMaxime Ripard reg |= CSI2TX_DPHY_CFG_CLK_ENABLE;
2526f684d4fSMaxime Ripard for (i = 0; i < csi2tx->num_lanes; i++)
2536ded416dSJan Kotas reg |= CSI2TX_DPHY_CFG_LANE_ENABLE(csi2tx->lanes[i] - 1);
2546f684d4fSMaxime Ripard writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG);
2556f684d4fSMaxime Ripard
2566f684d4fSMaxime Ripard udelay(10);
2576f684d4fSMaxime Ripard
2586f684d4fSMaxime Ripard /* Switch to HS mode */
2596f684d4fSMaxime Ripard reg &= ~CSI2TX_DPHY_CFG_MODE_MASK;
2606f684d4fSMaxime Ripard writel(reg | CSI2TX_DPHY_CFG_MODE_HS,
2616f684d4fSMaxime Ripard csi2tx->base + CSI2TX_DPHY_CFG_REG);
262050ff2adSJan Kotas }
263050ff2adSJan Kotas
264050ff2adSJan Kotas /* Configures D-PHY in CSIv1.3 */
csi2tx_dphy_setup(struct csi2tx_priv * csi2tx)265050ff2adSJan Kotas static void csi2tx_dphy_setup(struct csi2tx_priv *csi2tx)
266050ff2adSJan Kotas {
267050ff2adSJan Kotas u32 reg;
268050ff2adSJan Kotas unsigned int i;
269050ff2adSJan Kotas
270050ff2adSJan Kotas csi2tx_dphy_set_wakeup(csi2tx);
271050ff2adSJan Kotas
272050ff2adSJan Kotas /* Put our lanes (clock and data) out of reset */
273050ff2adSJan Kotas reg = CSI2TX_DPHY_CFG_CLK_RESET | CSI2TX_DPHY_CFG_MODE_LPDT;
274050ff2adSJan Kotas for (i = 0; i < csi2tx->num_lanes; i++)
275050ff2adSJan Kotas reg |= CSI2TX_DPHY_CFG_LANE_RESET(csi2tx->lanes[i] - 1);
276050ff2adSJan Kotas writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG);
277050ff2adSJan Kotas
278050ff2adSJan Kotas csi2tx_dphy_init_finish(csi2tx, reg);
279050ff2adSJan Kotas }
280050ff2adSJan Kotas
281050ff2adSJan Kotas /* Configures D-PHY in CSIv2 */
csi2tx_v2_dphy_setup(struct csi2tx_priv * csi2tx)282050ff2adSJan Kotas static void csi2tx_v2_dphy_setup(struct csi2tx_priv *csi2tx)
283050ff2adSJan Kotas {
284050ff2adSJan Kotas u32 reg;
285050ff2adSJan Kotas
286050ff2adSJan Kotas csi2tx_dphy_set_wakeup(csi2tx);
287050ff2adSJan Kotas
288050ff2adSJan Kotas /* Put our lanes (clock and data) out of reset */
289050ff2adSJan Kotas reg = CSI2TX_V2_DPHY_CFG_RESET | CSI2TX_V2_DPHY_CFG_MODE_LPDT;
290050ff2adSJan Kotas writel(reg, csi2tx->base + CSI2TX_V2_DPHY_CFG_REG);
291050ff2adSJan Kotas
292050ff2adSJan Kotas csi2tx_dphy_init_finish(csi2tx, reg);
293050ff2adSJan Kotas }
294050ff2adSJan Kotas
csi2tx_reset(struct csi2tx_priv * csi2tx)295050ff2adSJan Kotas static void csi2tx_reset(struct csi2tx_priv *csi2tx)
296050ff2adSJan Kotas {
297050ff2adSJan Kotas writel(CSI2TX_CONFIG_SRST_REQ, csi2tx->base + CSI2TX_CONFIG_REG);
2986f684d4fSMaxime Ripard
2996f684d4fSMaxime Ripard udelay(10);
300050ff2adSJan Kotas }
301050ff2adSJan Kotas
csi2tx_start(struct csi2tx_priv * csi2tx)302050ff2adSJan Kotas static int csi2tx_start(struct csi2tx_priv *csi2tx)
303050ff2adSJan Kotas {
304050ff2adSJan Kotas struct media_entity *entity = &csi2tx->subdev.entity;
305050ff2adSJan Kotas struct media_link *link;
306050ff2adSJan Kotas unsigned int i;
307050ff2adSJan Kotas
308050ff2adSJan Kotas csi2tx_reset(csi2tx);
309050ff2adSJan Kotas
310050ff2adSJan Kotas writel(CSI2TX_CONFIG_CFG_REQ, csi2tx->base + CSI2TX_CONFIG_REG);
311050ff2adSJan Kotas
312050ff2adSJan Kotas udelay(10);
313050ff2adSJan Kotas
314050ff2adSJan Kotas if (csi2tx->vops && csi2tx->vops->dphy_setup) {
315050ff2adSJan Kotas csi2tx->vops->dphy_setup(csi2tx);
316050ff2adSJan Kotas udelay(10);
317050ff2adSJan Kotas }
3186f684d4fSMaxime Ripard
3196f684d4fSMaxime Ripard /*
3206f684d4fSMaxime Ripard * Create a static mapping between the CSI virtual channels
3216f684d4fSMaxime Ripard * and the input streams.
3226f684d4fSMaxime Ripard *
3236f684d4fSMaxime Ripard * This should be enhanced, but v4l2 lacks the support for
3246f684d4fSMaxime Ripard * changing that mapping dynamically at the moment.
3256f684d4fSMaxime Ripard *
3266f684d4fSMaxime Ripard * We're protected from the userspace setting up links at the
3276f684d4fSMaxime Ripard * same time by the upper layer having called
3286f684d4fSMaxime Ripard * media_pipeline_start().
3296f684d4fSMaxime Ripard */
3306f684d4fSMaxime Ripard list_for_each_entry(link, &entity->links, list) {
3316f684d4fSMaxime Ripard struct v4l2_mbus_framefmt *mfmt;
3326f684d4fSMaxime Ripard const struct csi2tx_fmt *fmt;
3336f684d4fSMaxime Ripard unsigned int stream;
3346f684d4fSMaxime Ripard int pad_idx = -1;
3356f684d4fSMaxime Ripard
3366f684d4fSMaxime Ripard /* Only consider our enabled input pads */
3376f684d4fSMaxime Ripard for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++) {
3386f684d4fSMaxime Ripard struct media_pad *pad = &csi2tx->pads[i];
3396f684d4fSMaxime Ripard
3406f684d4fSMaxime Ripard if ((pad == link->sink) &&
3416f684d4fSMaxime Ripard (link->flags & MEDIA_LNK_FL_ENABLED)) {
3426f684d4fSMaxime Ripard pad_idx = i;
3436f684d4fSMaxime Ripard break;
3446f684d4fSMaxime Ripard }
3456f684d4fSMaxime Ripard }
3466f684d4fSMaxime Ripard
3476f684d4fSMaxime Ripard if (pad_idx < 0)
3486f684d4fSMaxime Ripard continue;
3496f684d4fSMaxime Ripard
3506f684d4fSMaxime Ripard mfmt = &csi2tx->pad_fmts[pad_idx];
3516f684d4fSMaxime Ripard fmt = csi2tx_get_fmt_from_mbus(mfmt->code);
3526f684d4fSMaxime Ripard if (!fmt)
3536f684d4fSMaxime Ripard continue;
3546f684d4fSMaxime Ripard
3556f684d4fSMaxime Ripard stream = pad_idx - CSI2TX_PAD_SINK_STREAM0;
3566f684d4fSMaxime Ripard
3576f684d4fSMaxime Ripard /*
3586f684d4fSMaxime Ripard * We use the stream ID there, but it's wrong.
3596f684d4fSMaxime Ripard *
3606f684d4fSMaxime Ripard * A stream could very well send a data type that is
3616f684d4fSMaxime Ripard * not equal to its stream ID. We need to find a
3626f684d4fSMaxime Ripard * proper way to address it.
3636f684d4fSMaxime Ripard */
3646f684d4fSMaxime Ripard writel(CSI2TX_DT_CFG_DT(fmt->dt),
3656f684d4fSMaxime Ripard csi2tx->base + CSI2TX_DT_CFG_REG(stream));
3666f684d4fSMaxime Ripard
3676f684d4fSMaxime Ripard writel(CSI2TX_DT_FORMAT_BYTES_PER_LINE(mfmt->width * fmt->bpp) |
3686f684d4fSMaxime Ripard CSI2TX_DT_FORMAT_MAX_LINE_NUM(mfmt->height + 1),
3696f684d4fSMaxime Ripard csi2tx->base + CSI2TX_DT_FORMAT_REG(stream));
3706f684d4fSMaxime Ripard
3716f684d4fSMaxime Ripard /*
3726f684d4fSMaxime Ripard * TODO: This needs to be calculated based on the
3736f684d4fSMaxime Ripard * output CSI2 clock rate.
3746f684d4fSMaxime Ripard */
3756f684d4fSMaxime Ripard writel(CSI2TX_STREAM_IF_CFG_FILL_LEVEL(4),
3766f684d4fSMaxime Ripard csi2tx->base + CSI2TX_STREAM_IF_CFG_REG(stream));
3776f684d4fSMaxime Ripard }
3786f684d4fSMaxime Ripard
3796f684d4fSMaxime Ripard /* Disable the configuration mode */
3806f684d4fSMaxime Ripard writel(0, csi2tx->base + CSI2TX_CONFIG_REG);
3816f684d4fSMaxime Ripard
3826f684d4fSMaxime Ripard return 0;
3836f684d4fSMaxime Ripard }
3846f684d4fSMaxime Ripard
csi2tx_stop(struct csi2tx_priv * csi2tx)3856f684d4fSMaxime Ripard static void csi2tx_stop(struct csi2tx_priv *csi2tx)
3866f684d4fSMaxime Ripard {
3876f684d4fSMaxime Ripard writel(CSI2TX_CONFIG_CFG_REQ | CSI2TX_CONFIG_SRST_REQ,
3886f684d4fSMaxime Ripard csi2tx->base + CSI2TX_CONFIG_REG);
3896f684d4fSMaxime Ripard }
3906f684d4fSMaxime Ripard
csi2tx_s_stream(struct v4l2_subdev * subdev,int enable)3916f684d4fSMaxime Ripard static int csi2tx_s_stream(struct v4l2_subdev *subdev, int enable)
3926f684d4fSMaxime Ripard {
3936f684d4fSMaxime Ripard struct csi2tx_priv *csi2tx = v4l2_subdev_to_csi2tx(subdev);
3946f684d4fSMaxime Ripard int ret = 0;
3956f684d4fSMaxime Ripard
3966f684d4fSMaxime Ripard mutex_lock(&csi2tx->lock);
3976f684d4fSMaxime Ripard
3986f684d4fSMaxime Ripard if (enable) {
3996f684d4fSMaxime Ripard /*
4006f684d4fSMaxime Ripard * If we're not the first users, there's no need to
4016f684d4fSMaxime Ripard * enable the whole controller.
4026f684d4fSMaxime Ripard */
4036f684d4fSMaxime Ripard if (!csi2tx->count) {
4046f684d4fSMaxime Ripard ret = csi2tx_start(csi2tx);
4056f684d4fSMaxime Ripard if (ret)
4066f684d4fSMaxime Ripard goto out;
4076f684d4fSMaxime Ripard }
4086f684d4fSMaxime Ripard
4096f684d4fSMaxime Ripard csi2tx->count++;
4106f684d4fSMaxime Ripard } else {
4116f684d4fSMaxime Ripard csi2tx->count--;
4126f684d4fSMaxime Ripard
4136f684d4fSMaxime Ripard /*
4146f684d4fSMaxime Ripard * Let the last user turn off the lights.
4156f684d4fSMaxime Ripard */
4166f684d4fSMaxime Ripard if (!csi2tx->count)
4176f684d4fSMaxime Ripard csi2tx_stop(csi2tx);
4186f684d4fSMaxime Ripard }
4196f684d4fSMaxime Ripard
4206f684d4fSMaxime Ripard out:
4216f684d4fSMaxime Ripard mutex_unlock(&csi2tx->lock);
4226f684d4fSMaxime Ripard return ret;
4236f684d4fSMaxime Ripard }
4246f684d4fSMaxime Ripard
4256f684d4fSMaxime Ripard static const struct v4l2_subdev_video_ops csi2tx_video_ops = {
4266f684d4fSMaxime Ripard .s_stream = csi2tx_s_stream,
4276f684d4fSMaxime Ripard };
4286f684d4fSMaxime Ripard
4296f684d4fSMaxime Ripard static const struct v4l2_subdev_ops csi2tx_subdev_ops = {
4306f684d4fSMaxime Ripard .pad = &csi2tx_pad_ops,
4316f684d4fSMaxime Ripard .video = &csi2tx_video_ops,
4326f684d4fSMaxime Ripard };
4336f684d4fSMaxime Ripard
csi2tx_get_resources(struct csi2tx_priv * csi2tx,struct platform_device * pdev)4346f684d4fSMaxime Ripard static int csi2tx_get_resources(struct csi2tx_priv *csi2tx,
4356f684d4fSMaxime Ripard struct platform_device *pdev)
4366f684d4fSMaxime Ripard {
4376f684d4fSMaxime Ripard unsigned int i;
4386f684d4fSMaxime Ripard u32 dev_cfg;
439e6001f69SEvgeny Novikov int ret;
4406f684d4fSMaxime Ripard
441f5aae241SCai Huoqing csi2tx->base = devm_platform_ioremap_resource(pdev, 0);
4426f684d4fSMaxime Ripard if (IS_ERR(csi2tx->base))
4436f684d4fSMaxime Ripard return PTR_ERR(csi2tx->base);
4446f684d4fSMaxime Ripard
4456f684d4fSMaxime Ripard csi2tx->p_clk = devm_clk_get(&pdev->dev, "p_clk");
4466f684d4fSMaxime Ripard if (IS_ERR(csi2tx->p_clk)) {
4476f684d4fSMaxime Ripard dev_err(&pdev->dev, "Couldn't get p_clk\n");
4486f684d4fSMaxime Ripard return PTR_ERR(csi2tx->p_clk);
4496f684d4fSMaxime Ripard }
4506f684d4fSMaxime Ripard
4516f684d4fSMaxime Ripard csi2tx->esc_clk = devm_clk_get(&pdev->dev, "esc_clk");
4526f684d4fSMaxime Ripard if (IS_ERR(csi2tx->esc_clk)) {
4536f684d4fSMaxime Ripard dev_err(&pdev->dev, "Couldn't get the esc_clk\n");
4546f684d4fSMaxime Ripard return PTR_ERR(csi2tx->esc_clk);
4556f684d4fSMaxime Ripard }
4566f684d4fSMaxime Ripard
457e6001f69SEvgeny Novikov ret = clk_prepare_enable(csi2tx->p_clk);
458e6001f69SEvgeny Novikov if (ret) {
459e6001f69SEvgeny Novikov dev_err(&pdev->dev, "Couldn't prepare and enable p_clk\n");
460e6001f69SEvgeny Novikov return ret;
461e6001f69SEvgeny Novikov }
462e6001f69SEvgeny Novikov
4636f684d4fSMaxime Ripard dev_cfg = readl(csi2tx->base + CSI2TX_DEVICE_CONFIG_REG);
4646f684d4fSMaxime Ripard clk_disable_unprepare(csi2tx->p_clk);
4656f684d4fSMaxime Ripard
4666f684d4fSMaxime Ripard csi2tx->max_lanes = dev_cfg & CSI2TX_DEVICE_CONFIG_LANES_MASK;
4676f684d4fSMaxime Ripard if (csi2tx->max_lanes > CSI2TX_LANES_MAX) {
4686f684d4fSMaxime Ripard dev_err(&pdev->dev, "Invalid number of lanes: %u\n",
4696f684d4fSMaxime Ripard csi2tx->max_lanes);
4706f684d4fSMaxime Ripard return -EINVAL;
4716f684d4fSMaxime Ripard }
4726f684d4fSMaxime Ripard
4736f684d4fSMaxime Ripard csi2tx->max_streams = (dev_cfg & CSI2TX_DEVICE_CONFIG_STREAMS_MASK) >> 4;
4746f684d4fSMaxime Ripard if (csi2tx->max_streams > CSI2TX_STREAMS_MAX) {
4756f684d4fSMaxime Ripard dev_err(&pdev->dev, "Invalid number of streams: %u\n",
4766f684d4fSMaxime Ripard csi2tx->max_streams);
4776f684d4fSMaxime Ripard return -EINVAL;
4786f684d4fSMaxime Ripard }
4796f684d4fSMaxime Ripard
4806f684d4fSMaxime Ripard csi2tx->has_internal_dphy = !!(dev_cfg & CSI2TX_DEVICE_CONFIG_HAS_DPHY);
4816f684d4fSMaxime Ripard
4826f684d4fSMaxime Ripard for (i = 0; i < csi2tx->max_streams; i++) {
4836f684d4fSMaxime Ripard char clk_name[16];
4846f684d4fSMaxime Ripard
4856f684d4fSMaxime Ripard snprintf(clk_name, sizeof(clk_name), "pixel_if%u_clk", i);
4866f684d4fSMaxime Ripard csi2tx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name);
4876f684d4fSMaxime Ripard if (IS_ERR(csi2tx->pixel_clk[i])) {
4886f684d4fSMaxime Ripard dev_err(&pdev->dev, "Couldn't get clock %s\n",
4896f684d4fSMaxime Ripard clk_name);
4906f684d4fSMaxime Ripard return PTR_ERR(csi2tx->pixel_clk[i]);
4916f684d4fSMaxime Ripard }
4926f684d4fSMaxime Ripard }
4936f684d4fSMaxime Ripard
4946f684d4fSMaxime Ripard return 0;
4956f684d4fSMaxime Ripard }
4966f684d4fSMaxime Ripard
csi2tx_check_lanes(struct csi2tx_priv * csi2tx)4976f684d4fSMaxime Ripard static int csi2tx_check_lanes(struct csi2tx_priv *csi2tx)
4986f684d4fSMaxime Ripard {
49960359a28SSakari Ailus struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
5006f684d4fSMaxime Ripard struct device_node *ep;
501bf9df90bSJan Kotas int ret, i;
5026f684d4fSMaxime Ripard
5036f684d4fSMaxime Ripard ep = of_graph_get_endpoint_by_regs(csi2tx->dev->of_node, 0, 0);
5046f684d4fSMaxime Ripard if (!ep)
5056f684d4fSMaxime Ripard return -EINVAL;
5066f684d4fSMaxime Ripard
5076f684d4fSMaxime Ripard ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &v4l2_ep);
5086f684d4fSMaxime Ripard if (ret) {
5096f684d4fSMaxime Ripard dev_err(csi2tx->dev, "Could not parse v4l2 endpoint\n");
5106f684d4fSMaxime Ripard goto out;
5116f684d4fSMaxime Ripard }
5126f684d4fSMaxime Ripard
5132d95e7edSSakari Ailus if (v4l2_ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
5146f684d4fSMaxime Ripard dev_err(csi2tx->dev, "Unsupported media bus type: 0x%x\n",
5156f684d4fSMaxime Ripard v4l2_ep.bus_type);
5166f684d4fSMaxime Ripard ret = -EINVAL;
5176f684d4fSMaxime Ripard goto out;
5186f684d4fSMaxime Ripard }
5196f684d4fSMaxime Ripard
5206f684d4fSMaxime Ripard csi2tx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
5216f684d4fSMaxime Ripard if (csi2tx->num_lanes > csi2tx->max_lanes) {
5226f684d4fSMaxime Ripard dev_err(csi2tx->dev,
5236f684d4fSMaxime Ripard "Current configuration uses more lanes than supported\n");
5246f684d4fSMaxime Ripard ret = -EINVAL;
5256f684d4fSMaxime Ripard goto out;
5266f684d4fSMaxime Ripard }
5276f684d4fSMaxime Ripard
528bf9df90bSJan Kotas for (i = 0; i < csi2tx->num_lanes; i++) {
529bf9df90bSJan Kotas if (v4l2_ep.bus.mipi_csi2.data_lanes[i] < 1) {
530bf9df90bSJan Kotas dev_err(csi2tx->dev, "Invalid lane[%d] number: %u\n",
531bf9df90bSJan Kotas i, v4l2_ep.bus.mipi_csi2.data_lanes[i]);
532bf9df90bSJan Kotas ret = -EINVAL;
533bf9df90bSJan Kotas goto out;
534bf9df90bSJan Kotas }
535bf9df90bSJan Kotas }
536bf9df90bSJan Kotas
5376f684d4fSMaxime Ripard memcpy(csi2tx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes,
5386f684d4fSMaxime Ripard sizeof(csi2tx->lanes));
5396f684d4fSMaxime Ripard
5406f684d4fSMaxime Ripard out:
5416f684d4fSMaxime Ripard of_node_put(ep);
5426f684d4fSMaxime Ripard return ret;
5436f684d4fSMaxime Ripard }
5446f684d4fSMaxime Ripard
545050ff2adSJan Kotas static const struct csi2tx_vops csi2tx_vops = {
546050ff2adSJan Kotas .dphy_setup = csi2tx_dphy_setup,
547050ff2adSJan Kotas };
548050ff2adSJan Kotas
549050ff2adSJan Kotas static const struct csi2tx_vops csi2tx_v2_vops = {
550050ff2adSJan Kotas .dphy_setup = csi2tx_v2_dphy_setup,
551050ff2adSJan Kotas };
552050ff2adSJan Kotas
553050ff2adSJan Kotas static const struct of_device_id csi2tx_of_table[] = {
554050ff2adSJan Kotas {
555050ff2adSJan Kotas .compatible = "cdns,csi2tx",
556050ff2adSJan Kotas .data = &csi2tx_vops
557050ff2adSJan Kotas },
558050ff2adSJan Kotas {
559050ff2adSJan Kotas .compatible = "cdns,csi2tx-1.3",
560050ff2adSJan Kotas .data = &csi2tx_vops
561050ff2adSJan Kotas },
562050ff2adSJan Kotas {
563050ff2adSJan Kotas .compatible = "cdns,csi2tx-2.1",
564050ff2adSJan Kotas .data = &csi2tx_v2_vops
565050ff2adSJan Kotas },
566050ff2adSJan Kotas { }
567050ff2adSJan Kotas };
568050ff2adSJan Kotas MODULE_DEVICE_TABLE(of, csi2tx_of_table);
569050ff2adSJan Kotas
csi2tx_probe(struct platform_device * pdev)5706f684d4fSMaxime Ripard static int csi2tx_probe(struct platform_device *pdev)
5716f684d4fSMaxime Ripard {
5726f684d4fSMaxime Ripard struct csi2tx_priv *csi2tx;
573050ff2adSJan Kotas const struct of_device_id *of_id;
5746f684d4fSMaxime Ripard unsigned int i;
5756f684d4fSMaxime Ripard int ret;
5766f684d4fSMaxime Ripard
5776f684d4fSMaxime Ripard csi2tx = kzalloc(sizeof(*csi2tx), GFP_KERNEL);
5786f684d4fSMaxime Ripard if (!csi2tx)
5796f684d4fSMaxime Ripard return -ENOMEM;
5806f684d4fSMaxime Ripard platform_set_drvdata(pdev, csi2tx);
5816f684d4fSMaxime Ripard mutex_init(&csi2tx->lock);
5826f684d4fSMaxime Ripard csi2tx->dev = &pdev->dev;
5836f684d4fSMaxime Ripard
5846f684d4fSMaxime Ripard ret = csi2tx_get_resources(csi2tx, pdev);
5856f684d4fSMaxime Ripard if (ret)
5866f684d4fSMaxime Ripard goto err_free_priv;
5876f684d4fSMaxime Ripard
588050ff2adSJan Kotas of_id = of_match_node(csi2tx_of_table, pdev->dev.of_node);
589050ff2adSJan Kotas csi2tx->vops = (struct csi2tx_vops *)of_id->data;
590050ff2adSJan Kotas
5916f684d4fSMaxime Ripard v4l2_subdev_init(&csi2tx->subdev, &csi2tx_subdev_ops);
5926f684d4fSMaxime Ripard csi2tx->subdev.owner = THIS_MODULE;
5936f684d4fSMaxime Ripard csi2tx->subdev.dev = &pdev->dev;
5946f684d4fSMaxime Ripard csi2tx->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
5956f684d4fSMaxime Ripard snprintf(csi2tx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s",
5966f684d4fSMaxime Ripard KBUILD_MODNAME, dev_name(&pdev->dev));
5976f684d4fSMaxime Ripard
5986f684d4fSMaxime Ripard ret = csi2tx_check_lanes(csi2tx);
5996f684d4fSMaxime Ripard if (ret)
6006f684d4fSMaxime Ripard goto err_free_priv;
6016f684d4fSMaxime Ripard
6026f684d4fSMaxime Ripard /* Create our media pads */
6036f684d4fSMaxime Ripard csi2tx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
6046f684d4fSMaxime Ripard csi2tx->pads[CSI2TX_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
6056f684d4fSMaxime Ripard for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++)
6066f684d4fSMaxime Ripard csi2tx->pads[i].flags = MEDIA_PAD_FL_SINK;
6076f684d4fSMaxime Ripard
6086f684d4fSMaxime Ripard /*
6096f684d4fSMaxime Ripard * Only the input pads are considered to have a format at the
6106f684d4fSMaxime Ripard * moment. The CSI link can multiplex various streams with
6116f684d4fSMaxime Ripard * different formats, and we can't expose this in v4l2 right
6126f684d4fSMaxime Ripard * now.
6136f684d4fSMaxime Ripard */
6146f684d4fSMaxime Ripard for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++)
6156f684d4fSMaxime Ripard csi2tx->pad_fmts[i] = fmt_default;
6166f684d4fSMaxime Ripard
6176f684d4fSMaxime Ripard ret = media_entity_pads_init(&csi2tx->subdev.entity, CSI2TX_PAD_MAX,
6186f684d4fSMaxime Ripard csi2tx->pads);
6196f684d4fSMaxime Ripard if (ret)
6206f684d4fSMaxime Ripard goto err_free_priv;
6216f684d4fSMaxime Ripard
6226f684d4fSMaxime Ripard ret = v4l2_async_register_subdev(&csi2tx->subdev);
6236f684d4fSMaxime Ripard if (ret < 0)
6246f684d4fSMaxime Ripard goto err_free_priv;
6256f684d4fSMaxime Ripard
6266f684d4fSMaxime Ripard dev_info(&pdev->dev,
6276f684d4fSMaxime Ripard "Probed CSI2TX with %u/%u lanes, %u streams, %s D-PHY\n",
6286f684d4fSMaxime Ripard csi2tx->num_lanes, csi2tx->max_lanes, csi2tx->max_streams,
6296f684d4fSMaxime Ripard csi2tx->has_internal_dphy ? "internal" : "no");
6306f684d4fSMaxime Ripard
6316f684d4fSMaxime Ripard return 0;
6326f684d4fSMaxime Ripard
6336f684d4fSMaxime Ripard err_free_priv:
6346f684d4fSMaxime Ripard kfree(csi2tx);
6356f684d4fSMaxime Ripard return ret;
6366f684d4fSMaxime Ripard }
6376f684d4fSMaxime Ripard
csi2tx_remove(struct platform_device * pdev)638*60fa2efbSUwe Kleine-König static void csi2tx_remove(struct platform_device *pdev)
6396f684d4fSMaxime Ripard {
6406f684d4fSMaxime Ripard struct csi2tx_priv *csi2tx = platform_get_drvdata(pdev);
6416f684d4fSMaxime Ripard
6426f684d4fSMaxime Ripard v4l2_async_unregister_subdev(&csi2tx->subdev);
6436f684d4fSMaxime Ripard kfree(csi2tx);
6446f684d4fSMaxime Ripard }
6456f684d4fSMaxime Ripard
6466f684d4fSMaxime Ripard static struct platform_driver csi2tx_driver = {
6476f684d4fSMaxime Ripard .probe = csi2tx_probe,
648*60fa2efbSUwe Kleine-König .remove_new = csi2tx_remove,
6496f684d4fSMaxime Ripard
6506f684d4fSMaxime Ripard .driver = {
6516f684d4fSMaxime Ripard .name = "cdns-csi2tx",
6526f684d4fSMaxime Ripard .of_match_table = csi2tx_of_table,
6536f684d4fSMaxime Ripard },
6546f684d4fSMaxime Ripard };
6556f684d4fSMaxime Ripard module_platform_driver(csi2tx_driver);
6566f684d4fSMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin.com>");
6576f684d4fSMaxime Ripard MODULE_DESCRIPTION("Cadence CSI2-TX controller");
6586f684d4fSMaxime Ripard MODULE_LICENSE("GPL");
659