1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Driver for Cadence MIPI-CSI2 TX Controller
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2017-2019 Cadence Design Systems Inc.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/clk.h>
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <linux/io.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/mutex.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun #include <linux/of_graph.h>
15*4882a593Smuzhiyun #include <linux/platform_device.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
19*4882a593Smuzhiyun #include <media/v4l2-device.h>
20*4882a593Smuzhiyun #include <media/v4l2-fwnode.h>
21*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define CSI2TX_DEVICE_CONFIG_REG 0x00
24*4882a593Smuzhiyun #define CSI2TX_DEVICE_CONFIG_STREAMS_MASK GENMASK(6, 4)
25*4882a593Smuzhiyun #define CSI2TX_DEVICE_CONFIG_HAS_DPHY BIT(3)
26*4882a593Smuzhiyun #define CSI2TX_DEVICE_CONFIG_LANES_MASK GENMASK(2, 0)
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define CSI2TX_CONFIG_REG 0x20
29*4882a593Smuzhiyun #define CSI2TX_CONFIG_CFG_REQ BIT(2)
30*4882a593Smuzhiyun #define CSI2TX_CONFIG_SRST_REQ BIT(1)
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_REG 0x28
33*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_CLK_RESET BIT(16)
34*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_LANE_RESET(n) BIT((n) + 12)
35*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_MODE_MASK GENMASK(9, 8)
36*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_MODE_LPDT (2 << 8)
37*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_MODE_HS (1 << 8)
38*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_MODE_ULPS (0 << 8)
39*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_CLK_ENABLE BIT(4)
40*4882a593Smuzhiyun #define CSI2TX_DPHY_CFG_LANE_ENABLE(n) BIT(n)
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define CSI2TX_DPHY_CLK_WAKEUP_REG 0x2c
43*4882a593Smuzhiyun #define CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(n) ((n) & 0xffff)
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #define CSI2TX_DT_CFG_REG(n) (0x80 + (n) * 8)
46*4882a593Smuzhiyun #define CSI2TX_DT_CFG_DT(n) (((n) & 0x3f) << 2)
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #define CSI2TX_DT_FORMAT_REG(n) (0x84 + (n) * 8)
49*4882a593Smuzhiyun #define CSI2TX_DT_FORMAT_BYTES_PER_LINE(n) (((n) & 0xffff) << 16)
50*4882a593Smuzhiyun #define CSI2TX_DT_FORMAT_MAX_LINE_NUM(n) ((n) & 0xffff)
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #define CSI2TX_STREAM_IF_CFG_REG(n) (0x100 + (n) * 4)
53*4882a593Smuzhiyun #define CSI2TX_STREAM_IF_CFG_FILL_LEVEL(n) ((n) & 0x1f)
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* CSI2TX V2 Registers */
56*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_REG 0x28
57*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_RESET BIT(16)
58*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_CLOCK_MODE BIT(10)
59*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_MODE_MASK GENMASK(9, 8)
60*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_MODE_LPDT (2 << 8)
61*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_MODE_HS (1 << 8)
62*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_MODE_ULPS (0 << 8)
63*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_CLK_ENABLE BIT(4)
64*4882a593Smuzhiyun #define CSI2TX_V2_DPHY_CFG_LANE_ENABLE(n) BIT(n)
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #define CSI2TX_LANES_MAX 4
67*4882a593Smuzhiyun #define CSI2TX_STREAMS_MAX 4
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun enum csi2tx_pads {
70*4882a593Smuzhiyun CSI2TX_PAD_SOURCE,
71*4882a593Smuzhiyun CSI2TX_PAD_SINK_STREAM0,
72*4882a593Smuzhiyun CSI2TX_PAD_SINK_STREAM1,
73*4882a593Smuzhiyun CSI2TX_PAD_SINK_STREAM2,
74*4882a593Smuzhiyun CSI2TX_PAD_SINK_STREAM3,
75*4882a593Smuzhiyun CSI2TX_PAD_MAX,
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun struct csi2tx_fmt {
79*4882a593Smuzhiyun u32 mbus;
80*4882a593Smuzhiyun u32 dt;
81*4882a593Smuzhiyun u32 bpp;
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun struct csi2tx_priv;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* CSI2TX Variant Operations */
87*4882a593Smuzhiyun struct csi2tx_vops {
88*4882a593Smuzhiyun void (*dphy_setup)(struct csi2tx_priv *csi2tx);
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun struct csi2tx_priv {
92*4882a593Smuzhiyun struct device *dev;
93*4882a593Smuzhiyun unsigned int count;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun * Used to prevent race conditions between multiple,
97*4882a593Smuzhiyun * concurrent calls to start and stop.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun struct mutex lock;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun void __iomem *base;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun struct csi2tx_vops *vops;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun struct clk *esc_clk;
106*4882a593Smuzhiyun struct clk *p_clk;
107*4882a593Smuzhiyun struct clk *pixel_clk[CSI2TX_STREAMS_MAX];
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun struct v4l2_subdev subdev;
110*4882a593Smuzhiyun struct media_pad pads[CSI2TX_PAD_MAX];
111*4882a593Smuzhiyun struct v4l2_mbus_framefmt pad_fmts[CSI2TX_PAD_MAX];
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun bool has_internal_dphy;
114*4882a593Smuzhiyun u8 lanes[CSI2TX_LANES_MAX];
115*4882a593Smuzhiyun unsigned int num_lanes;
116*4882a593Smuzhiyun unsigned int max_lanes;
117*4882a593Smuzhiyun unsigned int max_streams;
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun static const struct csi2tx_fmt csi2tx_formats[] = {
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun .mbus = MEDIA_BUS_FMT_UYVY8_1X16,
123*4882a593Smuzhiyun .bpp = 2,
124*4882a593Smuzhiyun .dt = 0x1e,
125*4882a593Smuzhiyun },
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun .mbus = MEDIA_BUS_FMT_RGB888_1X24,
128*4882a593Smuzhiyun .bpp = 3,
129*4882a593Smuzhiyun .dt = 0x24,
130*4882a593Smuzhiyun },
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun static const struct v4l2_mbus_framefmt fmt_default = {
134*4882a593Smuzhiyun .width = 1280,
135*4882a593Smuzhiyun .height = 720,
136*4882a593Smuzhiyun .code = MEDIA_BUS_FMT_RGB888_1X24,
137*4882a593Smuzhiyun .field = V4L2_FIELD_NONE,
138*4882a593Smuzhiyun .colorspace = V4L2_COLORSPACE_DEFAULT,
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun static inline
v4l2_subdev_to_csi2tx(struct v4l2_subdev * subdev)142*4882a593Smuzhiyun struct csi2tx_priv *v4l2_subdev_to_csi2tx(struct v4l2_subdev *subdev)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun return container_of(subdev, struct csi2tx_priv, subdev);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
csi2tx_get_fmt_from_mbus(u32 mbus)147*4882a593Smuzhiyun static const struct csi2tx_fmt *csi2tx_get_fmt_from_mbus(u32 mbus)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun unsigned int i;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(csi2tx_formats); i++)
152*4882a593Smuzhiyun if (csi2tx_formats[i].mbus == mbus)
153*4882a593Smuzhiyun return &csi2tx_formats[i];
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun return NULL;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
csi2tx_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)158*4882a593Smuzhiyun static int csi2tx_enum_mbus_code(struct v4l2_subdev *subdev,
159*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
160*4882a593Smuzhiyun struct v4l2_subdev_mbus_code_enum *code)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun if (code->pad || code->index >= ARRAY_SIZE(csi2tx_formats))
163*4882a593Smuzhiyun return -EINVAL;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun code->code = csi2tx_formats[code->index].mbus;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun static struct v4l2_mbus_framefmt *
__csi2tx_get_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)171*4882a593Smuzhiyun __csi2tx_get_pad_format(struct v4l2_subdev *subdev,
172*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
173*4882a593Smuzhiyun struct v4l2_subdev_format *fmt)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun struct csi2tx_priv *csi2tx = v4l2_subdev_to_csi2tx(subdev);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
178*4882a593Smuzhiyun return v4l2_subdev_get_try_format(subdev, cfg,
179*4882a593Smuzhiyun fmt->pad);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun return &csi2tx->pad_fmts[fmt->pad];
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
csi2tx_get_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)184*4882a593Smuzhiyun static int csi2tx_get_pad_format(struct v4l2_subdev *subdev,
185*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
186*4882a593Smuzhiyun struct v4l2_subdev_format *fmt)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun const struct v4l2_mbus_framefmt *format;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /* Multiplexed pad? */
191*4882a593Smuzhiyun if (fmt->pad == CSI2TX_PAD_SOURCE)
192*4882a593Smuzhiyun return -EINVAL;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun format = __csi2tx_get_pad_format(subdev, cfg, fmt);
195*4882a593Smuzhiyun if (!format)
196*4882a593Smuzhiyun return -EINVAL;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun fmt->format = *format;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun return 0;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
csi2tx_set_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)203*4882a593Smuzhiyun static int csi2tx_set_pad_format(struct v4l2_subdev *subdev,
204*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
205*4882a593Smuzhiyun struct v4l2_subdev_format *fmt)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun const struct v4l2_mbus_framefmt *src_format = &fmt->format;
208*4882a593Smuzhiyun struct v4l2_mbus_framefmt *dst_format;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /* Multiplexed pad? */
211*4882a593Smuzhiyun if (fmt->pad == CSI2TX_PAD_SOURCE)
212*4882a593Smuzhiyun return -EINVAL;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun if (!csi2tx_get_fmt_from_mbus(fmt->format.code))
215*4882a593Smuzhiyun src_format = &fmt_default;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun dst_format = __csi2tx_get_pad_format(subdev, cfg, fmt);
218*4882a593Smuzhiyun if (!dst_format)
219*4882a593Smuzhiyun return -EINVAL;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun *dst_format = *src_format;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun return 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun static const struct v4l2_subdev_pad_ops csi2tx_pad_ops = {
227*4882a593Smuzhiyun .enum_mbus_code = csi2tx_enum_mbus_code,
228*4882a593Smuzhiyun .get_fmt = csi2tx_get_pad_format,
229*4882a593Smuzhiyun .set_fmt = csi2tx_set_pad_format,
230*4882a593Smuzhiyun };
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* Set Wake Up value in the D-PHY */
csi2tx_dphy_set_wakeup(struct csi2tx_priv * csi2tx)233*4882a593Smuzhiyun static void csi2tx_dphy_set_wakeup(struct csi2tx_priv *csi2tx)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun writel(CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(32),
236*4882a593Smuzhiyun csi2tx->base + CSI2TX_DPHY_CLK_WAKEUP_REG);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /*
240*4882a593Smuzhiyun * Finishes the D-PHY initialization
241*4882a593Smuzhiyun * reg dphy cfg value to be used
242*4882a593Smuzhiyun */
csi2tx_dphy_init_finish(struct csi2tx_priv * csi2tx,u32 reg)243*4882a593Smuzhiyun static void csi2tx_dphy_init_finish(struct csi2tx_priv *csi2tx, u32 reg)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun unsigned int i;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun udelay(10);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /* Enable our (clock and data) lanes */
250*4882a593Smuzhiyun reg |= CSI2TX_DPHY_CFG_CLK_ENABLE;
251*4882a593Smuzhiyun for (i = 0; i < csi2tx->num_lanes; i++)
252*4882a593Smuzhiyun reg |= CSI2TX_DPHY_CFG_LANE_ENABLE(csi2tx->lanes[i] - 1);
253*4882a593Smuzhiyun writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG);
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun udelay(10);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /* Switch to HS mode */
258*4882a593Smuzhiyun reg &= ~CSI2TX_DPHY_CFG_MODE_MASK;
259*4882a593Smuzhiyun writel(reg | CSI2TX_DPHY_CFG_MODE_HS,
260*4882a593Smuzhiyun csi2tx->base + CSI2TX_DPHY_CFG_REG);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* Configures D-PHY in CSIv1.3 */
csi2tx_dphy_setup(struct csi2tx_priv * csi2tx)264*4882a593Smuzhiyun static void csi2tx_dphy_setup(struct csi2tx_priv *csi2tx)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun u32 reg;
267*4882a593Smuzhiyun unsigned int i;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun csi2tx_dphy_set_wakeup(csi2tx);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* Put our lanes (clock and data) out of reset */
272*4882a593Smuzhiyun reg = CSI2TX_DPHY_CFG_CLK_RESET | CSI2TX_DPHY_CFG_MODE_LPDT;
273*4882a593Smuzhiyun for (i = 0; i < csi2tx->num_lanes; i++)
274*4882a593Smuzhiyun reg |= CSI2TX_DPHY_CFG_LANE_RESET(csi2tx->lanes[i] - 1);
275*4882a593Smuzhiyun writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun csi2tx_dphy_init_finish(csi2tx, reg);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun /* Configures D-PHY in CSIv2 */
csi2tx_v2_dphy_setup(struct csi2tx_priv * csi2tx)281*4882a593Smuzhiyun static void csi2tx_v2_dphy_setup(struct csi2tx_priv *csi2tx)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun u32 reg;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun csi2tx_dphy_set_wakeup(csi2tx);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* Put our lanes (clock and data) out of reset */
288*4882a593Smuzhiyun reg = CSI2TX_V2_DPHY_CFG_RESET | CSI2TX_V2_DPHY_CFG_MODE_LPDT;
289*4882a593Smuzhiyun writel(reg, csi2tx->base + CSI2TX_V2_DPHY_CFG_REG);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun csi2tx_dphy_init_finish(csi2tx, reg);
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
csi2tx_reset(struct csi2tx_priv * csi2tx)294*4882a593Smuzhiyun static void csi2tx_reset(struct csi2tx_priv *csi2tx)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun writel(CSI2TX_CONFIG_SRST_REQ, csi2tx->base + CSI2TX_CONFIG_REG);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun udelay(10);
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
csi2tx_start(struct csi2tx_priv * csi2tx)301*4882a593Smuzhiyun static int csi2tx_start(struct csi2tx_priv *csi2tx)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun struct media_entity *entity = &csi2tx->subdev.entity;
304*4882a593Smuzhiyun struct media_link *link;
305*4882a593Smuzhiyun unsigned int i;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun csi2tx_reset(csi2tx);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun writel(CSI2TX_CONFIG_CFG_REQ, csi2tx->base + CSI2TX_CONFIG_REG);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun udelay(10);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun if (csi2tx->vops && csi2tx->vops->dphy_setup) {
314*4882a593Smuzhiyun csi2tx->vops->dphy_setup(csi2tx);
315*4882a593Smuzhiyun udelay(10);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun /*
319*4882a593Smuzhiyun * Create a static mapping between the CSI virtual channels
320*4882a593Smuzhiyun * and the input streams.
321*4882a593Smuzhiyun *
322*4882a593Smuzhiyun * This should be enhanced, but v4l2 lacks the support for
323*4882a593Smuzhiyun * changing that mapping dynamically at the moment.
324*4882a593Smuzhiyun *
325*4882a593Smuzhiyun * We're protected from the userspace setting up links at the
326*4882a593Smuzhiyun * same time by the upper layer having called
327*4882a593Smuzhiyun * media_pipeline_start().
328*4882a593Smuzhiyun */
329*4882a593Smuzhiyun list_for_each_entry(link, &entity->links, list) {
330*4882a593Smuzhiyun struct v4l2_mbus_framefmt *mfmt;
331*4882a593Smuzhiyun const struct csi2tx_fmt *fmt;
332*4882a593Smuzhiyun unsigned int stream;
333*4882a593Smuzhiyun int pad_idx = -1;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /* Only consider our enabled input pads */
336*4882a593Smuzhiyun for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++) {
337*4882a593Smuzhiyun struct media_pad *pad = &csi2tx->pads[i];
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun if ((pad == link->sink) &&
340*4882a593Smuzhiyun (link->flags & MEDIA_LNK_FL_ENABLED)) {
341*4882a593Smuzhiyun pad_idx = i;
342*4882a593Smuzhiyun break;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun if (pad_idx < 0)
347*4882a593Smuzhiyun continue;
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun mfmt = &csi2tx->pad_fmts[pad_idx];
350*4882a593Smuzhiyun fmt = csi2tx_get_fmt_from_mbus(mfmt->code);
351*4882a593Smuzhiyun if (!fmt)
352*4882a593Smuzhiyun continue;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun stream = pad_idx - CSI2TX_PAD_SINK_STREAM0;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun /*
357*4882a593Smuzhiyun * We use the stream ID there, but it's wrong.
358*4882a593Smuzhiyun *
359*4882a593Smuzhiyun * A stream could very well send a data type that is
360*4882a593Smuzhiyun * not equal to its stream ID. We need to find a
361*4882a593Smuzhiyun * proper way to address it.
362*4882a593Smuzhiyun */
363*4882a593Smuzhiyun writel(CSI2TX_DT_CFG_DT(fmt->dt),
364*4882a593Smuzhiyun csi2tx->base + CSI2TX_DT_CFG_REG(stream));
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun writel(CSI2TX_DT_FORMAT_BYTES_PER_LINE(mfmt->width * fmt->bpp) |
367*4882a593Smuzhiyun CSI2TX_DT_FORMAT_MAX_LINE_NUM(mfmt->height + 1),
368*4882a593Smuzhiyun csi2tx->base + CSI2TX_DT_FORMAT_REG(stream));
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun * TODO: This needs to be calculated based on the
372*4882a593Smuzhiyun * output CSI2 clock rate.
373*4882a593Smuzhiyun */
374*4882a593Smuzhiyun writel(CSI2TX_STREAM_IF_CFG_FILL_LEVEL(4),
375*4882a593Smuzhiyun csi2tx->base + CSI2TX_STREAM_IF_CFG_REG(stream));
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun /* Disable the configuration mode */
379*4882a593Smuzhiyun writel(0, csi2tx->base + CSI2TX_CONFIG_REG);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun return 0;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
csi2tx_stop(struct csi2tx_priv * csi2tx)384*4882a593Smuzhiyun static void csi2tx_stop(struct csi2tx_priv *csi2tx)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun writel(CSI2TX_CONFIG_CFG_REQ | CSI2TX_CONFIG_SRST_REQ,
387*4882a593Smuzhiyun csi2tx->base + CSI2TX_CONFIG_REG);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
csi2tx_s_stream(struct v4l2_subdev * subdev,int enable)390*4882a593Smuzhiyun static int csi2tx_s_stream(struct v4l2_subdev *subdev, int enable)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun struct csi2tx_priv *csi2tx = v4l2_subdev_to_csi2tx(subdev);
393*4882a593Smuzhiyun int ret = 0;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun mutex_lock(&csi2tx->lock);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun if (enable) {
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun * If we're not the first users, there's no need to
400*4882a593Smuzhiyun * enable the whole controller.
401*4882a593Smuzhiyun */
402*4882a593Smuzhiyun if (!csi2tx->count) {
403*4882a593Smuzhiyun ret = csi2tx_start(csi2tx);
404*4882a593Smuzhiyun if (ret)
405*4882a593Smuzhiyun goto out;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun csi2tx->count++;
409*4882a593Smuzhiyun } else {
410*4882a593Smuzhiyun csi2tx->count--;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun /*
413*4882a593Smuzhiyun * Let the last user turn off the lights.
414*4882a593Smuzhiyun */
415*4882a593Smuzhiyun if (!csi2tx->count)
416*4882a593Smuzhiyun csi2tx_stop(csi2tx);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun out:
420*4882a593Smuzhiyun mutex_unlock(&csi2tx->lock);
421*4882a593Smuzhiyun return ret;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun static const struct v4l2_subdev_video_ops csi2tx_video_ops = {
425*4882a593Smuzhiyun .s_stream = csi2tx_s_stream,
426*4882a593Smuzhiyun };
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun static const struct v4l2_subdev_ops csi2tx_subdev_ops = {
429*4882a593Smuzhiyun .pad = &csi2tx_pad_ops,
430*4882a593Smuzhiyun .video = &csi2tx_video_ops,
431*4882a593Smuzhiyun };
432*4882a593Smuzhiyun
csi2tx_get_resources(struct csi2tx_priv * csi2tx,struct platform_device * pdev)433*4882a593Smuzhiyun static int csi2tx_get_resources(struct csi2tx_priv *csi2tx,
434*4882a593Smuzhiyun struct platform_device *pdev)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun struct resource *res;
437*4882a593Smuzhiyun unsigned int i;
438*4882a593Smuzhiyun u32 dev_cfg;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
441*4882a593Smuzhiyun csi2tx->base = devm_ioremap_resource(&pdev->dev, res);
442*4882a593Smuzhiyun if (IS_ERR(csi2tx->base))
443*4882a593Smuzhiyun return PTR_ERR(csi2tx->base);
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun csi2tx->p_clk = devm_clk_get(&pdev->dev, "p_clk");
446*4882a593Smuzhiyun if (IS_ERR(csi2tx->p_clk)) {
447*4882a593Smuzhiyun dev_err(&pdev->dev, "Couldn't get p_clk\n");
448*4882a593Smuzhiyun return PTR_ERR(csi2tx->p_clk);
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun csi2tx->esc_clk = devm_clk_get(&pdev->dev, "esc_clk");
452*4882a593Smuzhiyun if (IS_ERR(csi2tx->esc_clk)) {
453*4882a593Smuzhiyun dev_err(&pdev->dev, "Couldn't get the esc_clk\n");
454*4882a593Smuzhiyun return PTR_ERR(csi2tx->esc_clk);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun clk_prepare_enable(csi2tx->p_clk);
458*4882a593Smuzhiyun dev_cfg = readl(csi2tx->base + CSI2TX_DEVICE_CONFIG_REG);
459*4882a593Smuzhiyun clk_disable_unprepare(csi2tx->p_clk);
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun csi2tx->max_lanes = dev_cfg & CSI2TX_DEVICE_CONFIG_LANES_MASK;
462*4882a593Smuzhiyun if (csi2tx->max_lanes > CSI2TX_LANES_MAX) {
463*4882a593Smuzhiyun dev_err(&pdev->dev, "Invalid number of lanes: %u\n",
464*4882a593Smuzhiyun csi2tx->max_lanes);
465*4882a593Smuzhiyun return -EINVAL;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun csi2tx->max_streams = (dev_cfg & CSI2TX_DEVICE_CONFIG_STREAMS_MASK) >> 4;
469*4882a593Smuzhiyun if (csi2tx->max_streams > CSI2TX_STREAMS_MAX) {
470*4882a593Smuzhiyun dev_err(&pdev->dev, "Invalid number of streams: %u\n",
471*4882a593Smuzhiyun csi2tx->max_streams);
472*4882a593Smuzhiyun return -EINVAL;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun csi2tx->has_internal_dphy = !!(dev_cfg & CSI2TX_DEVICE_CONFIG_HAS_DPHY);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun for (i = 0; i < csi2tx->max_streams; i++) {
478*4882a593Smuzhiyun char clk_name[16];
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun snprintf(clk_name, sizeof(clk_name), "pixel_if%u_clk", i);
481*4882a593Smuzhiyun csi2tx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name);
482*4882a593Smuzhiyun if (IS_ERR(csi2tx->pixel_clk[i])) {
483*4882a593Smuzhiyun dev_err(&pdev->dev, "Couldn't get clock %s\n",
484*4882a593Smuzhiyun clk_name);
485*4882a593Smuzhiyun return PTR_ERR(csi2tx->pixel_clk[i]);
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun return 0;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
csi2tx_check_lanes(struct csi2tx_priv * csi2tx)492*4882a593Smuzhiyun static int csi2tx_check_lanes(struct csi2tx_priv *csi2tx)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
495*4882a593Smuzhiyun struct device_node *ep;
496*4882a593Smuzhiyun int ret, i;
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun ep = of_graph_get_endpoint_by_regs(csi2tx->dev->of_node, 0, 0);
499*4882a593Smuzhiyun if (!ep)
500*4882a593Smuzhiyun return -EINVAL;
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &v4l2_ep);
503*4882a593Smuzhiyun if (ret) {
504*4882a593Smuzhiyun dev_err(csi2tx->dev, "Could not parse v4l2 endpoint\n");
505*4882a593Smuzhiyun goto out;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun if (v4l2_ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
509*4882a593Smuzhiyun dev_err(csi2tx->dev, "Unsupported media bus type: 0x%x\n",
510*4882a593Smuzhiyun v4l2_ep.bus_type);
511*4882a593Smuzhiyun ret = -EINVAL;
512*4882a593Smuzhiyun goto out;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun csi2tx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
516*4882a593Smuzhiyun if (csi2tx->num_lanes > csi2tx->max_lanes) {
517*4882a593Smuzhiyun dev_err(csi2tx->dev,
518*4882a593Smuzhiyun "Current configuration uses more lanes than supported\n");
519*4882a593Smuzhiyun ret = -EINVAL;
520*4882a593Smuzhiyun goto out;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun for (i = 0; i < csi2tx->num_lanes; i++) {
524*4882a593Smuzhiyun if (v4l2_ep.bus.mipi_csi2.data_lanes[i] < 1) {
525*4882a593Smuzhiyun dev_err(csi2tx->dev, "Invalid lane[%d] number: %u\n",
526*4882a593Smuzhiyun i, v4l2_ep.bus.mipi_csi2.data_lanes[i]);
527*4882a593Smuzhiyun ret = -EINVAL;
528*4882a593Smuzhiyun goto out;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun memcpy(csi2tx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes,
533*4882a593Smuzhiyun sizeof(csi2tx->lanes));
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun out:
536*4882a593Smuzhiyun of_node_put(ep);
537*4882a593Smuzhiyun return ret;
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun static const struct csi2tx_vops csi2tx_vops = {
541*4882a593Smuzhiyun .dphy_setup = csi2tx_dphy_setup,
542*4882a593Smuzhiyun };
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun static const struct csi2tx_vops csi2tx_v2_vops = {
545*4882a593Smuzhiyun .dphy_setup = csi2tx_v2_dphy_setup,
546*4882a593Smuzhiyun };
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun static const struct of_device_id csi2tx_of_table[] = {
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun .compatible = "cdns,csi2tx",
551*4882a593Smuzhiyun .data = &csi2tx_vops
552*4882a593Smuzhiyun },
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun .compatible = "cdns,csi2tx-1.3",
555*4882a593Smuzhiyun .data = &csi2tx_vops
556*4882a593Smuzhiyun },
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun .compatible = "cdns,csi2tx-2.1",
559*4882a593Smuzhiyun .data = &csi2tx_v2_vops
560*4882a593Smuzhiyun },
561*4882a593Smuzhiyun { }
562*4882a593Smuzhiyun };
563*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, csi2tx_of_table);
564*4882a593Smuzhiyun
csi2tx_probe(struct platform_device * pdev)565*4882a593Smuzhiyun static int csi2tx_probe(struct platform_device *pdev)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun struct csi2tx_priv *csi2tx;
568*4882a593Smuzhiyun const struct of_device_id *of_id;
569*4882a593Smuzhiyun unsigned int i;
570*4882a593Smuzhiyun int ret;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun csi2tx = kzalloc(sizeof(*csi2tx), GFP_KERNEL);
573*4882a593Smuzhiyun if (!csi2tx)
574*4882a593Smuzhiyun return -ENOMEM;
575*4882a593Smuzhiyun platform_set_drvdata(pdev, csi2tx);
576*4882a593Smuzhiyun mutex_init(&csi2tx->lock);
577*4882a593Smuzhiyun csi2tx->dev = &pdev->dev;
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun ret = csi2tx_get_resources(csi2tx, pdev);
580*4882a593Smuzhiyun if (ret)
581*4882a593Smuzhiyun goto err_free_priv;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun of_id = of_match_node(csi2tx_of_table, pdev->dev.of_node);
584*4882a593Smuzhiyun csi2tx->vops = (struct csi2tx_vops *)of_id->data;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun v4l2_subdev_init(&csi2tx->subdev, &csi2tx_subdev_ops);
587*4882a593Smuzhiyun csi2tx->subdev.owner = THIS_MODULE;
588*4882a593Smuzhiyun csi2tx->subdev.dev = &pdev->dev;
589*4882a593Smuzhiyun csi2tx->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
590*4882a593Smuzhiyun snprintf(csi2tx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s",
591*4882a593Smuzhiyun KBUILD_MODNAME, dev_name(&pdev->dev));
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun ret = csi2tx_check_lanes(csi2tx);
594*4882a593Smuzhiyun if (ret)
595*4882a593Smuzhiyun goto err_free_priv;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun /* Create our media pads */
598*4882a593Smuzhiyun csi2tx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
599*4882a593Smuzhiyun csi2tx->pads[CSI2TX_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
600*4882a593Smuzhiyun for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++)
601*4882a593Smuzhiyun csi2tx->pads[i].flags = MEDIA_PAD_FL_SINK;
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun /*
604*4882a593Smuzhiyun * Only the input pads are considered to have a format at the
605*4882a593Smuzhiyun * moment. The CSI link can multiplex various streams with
606*4882a593Smuzhiyun * different formats, and we can't expose this in v4l2 right
607*4882a593Smuzhiyun * now.
608*4882a593Smuzhiyun */
609*4882a593Smuzhiyun for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++)
610*4882a593Smuzhiyun csi2tx->pad_fmts[i] = fmt_default;
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun ret = media_entity_pads_init(&csi2tx->subdev.entity, CSI2TX_PAD_MAX,
613*4882a593Smuzhiyun csi2tx->pads);
614*4882a593Smuzhiyun if (ret)
615*4882a593Smuzhiyun goto err_free_priv;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun ret = v4l2_async_register_subdev(&csi2tx->subdev);
618*4882a593Smuzhiyun if (ret < 0)
619*4882a593Smuzhiyun goto err_free_priv;
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun dev_info(&pdev->dev,
622*4882a593Smuzhiyun "Probed CSI2TX with %u/%u lanes, %u streams, %s D-PHY\n",
623*4882a593Smuzhiyun csi2tx->num_lanes, csi2tx->max_lanes, csi2tx->max_streams,
624*4882a593Smuzhiyun csi2tx->has_internal_dphy ? "internal" : "no");
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun return 0;
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun err_free_priv:
629*4882a593Smuzhiyun kfree(csi2tx);
630*4882a593Smuzhiyun return ret;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
csi2tx_remove(struct platform_device * pdev)633*4882a593Smuzhiyun static int csi2tx_remove(struct platform_device *pdev)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun struct csi2tx_priv *csi2tx = platform_get_drvdata(pdev);
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun v4l2_async_unregister_subdev(&csi2tx->subdev);
638*4882a593Smuzhiyun kfree(csi2tx);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun return 0;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun static struct platform_driver csi2tx_driver = {
644*4882a593Smuzhiyun .probe = csi2tx_probe,
645*4882a593Smuzhiyun .remove = csi2tx_remove,
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun .driver = {
648*4882a593Smuzhiyun .name = "cdns-csi2tx",
649*4882a593Smuzhiyun .of_match_table = csi2tx_of_table,
650*4882a593Smuzhiyun },
651*4882a593Smuzhiyun };
652*4882a593Smuzhiyun module_platform_driver(csi2tx_driver);
653*4882a593Smuzhiyun MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin.com>");
654*4882a593Smuzhiyun MODULE_DESCRIPTION("Cadence CSI2-TX controller");
655*4882a593Smuzhiyun MODULE_LICENSE("GPL");
656