1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Cadence MIPI-CSI2 TX Controller
5 * Copyright (C) 2017-2019 Cadence Design Systems Inc.
9 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
14 #include <linux/of_graph.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-subdev.h>
23 #define CSI2TX_DEVICE_CONFIG_REG 0x00
24 #define CSI2TX_DEVICE_CONFIG_STREAMS_MASK GENMASK(6, 4)
25 #define CSI2TX_DEVICE_CONFIG_HAS_DPHY BIT(3)
26 #define CSI2TX_DEVICE_CONFIG_LANES_MASK GENMASK(2, 0)
28 #define CSI2TX_CONFIG_REG 0x20
29 #define CSI2TX_CONFIG_CFG_REQ BIT(2)
30 #define CSI2TX_CONFIG_SRST_REQ BIT(1)
32 #define CSI2TX_DPHY_CFG_REG 0x28
33 #define CSI2TX_DPHY_CFG_CLK_RESET BIT(16)
34 #define CSI2TX_DPHY_CFG_LANE_RESET(n) BIT((n) + 12)
35 #define CSI2TX_DPHY_CFG_MODE_MASK GENMASK(9, 8)
36 #define CSI2TX_DPHY_CFG_MODE_LPDT (2 << 8)
37 #define CSI2TX_DPHY_CFG_MODE_HS (1 << 8)
38 #define CSI2TX_DPHY_CFG_MODE_ULPS (0 << 8)
39 #define CSI2TX_DPHY_CFG_CLK_ENABLE BIT(4)
40 #define CSI2TX_DPHY_CFG_LANE_ENABLE(n) BIT(n)
42 #define CSI2TX_DPHY_CLK_WAKEUP_REG 0x2c
43 #define CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(n) ((n) & 0xffff)
45 #define CSI2TX_DT_CFG_REG(n) (0x80 + (n) * 8)
46 #define CSI2TX_DT_CFG_DT(n) (((n) & 0x3f) << 2)
48 #define CSI2TX_DT_FORMAT_REG(n) (0x84 + (n) * 8)
49 #define CSI2TX_DT_FORMAT_BYTES_PER_LINE(n) (((n) & 0xffff) << 16)
50 #define CSI2TX_DT_FORMAT_MAX_LINE_NUM(n) ((n) & 0xffff)
52 #define CSI2TX_STREAM_IF_CFG_REG(n) (0x100 + (n) * 4)
53 #define CSI2TX_STREAM_IF_CFG_FILL_LEVEL(n) ((n) & 0x1f)
55 /* CSI2TX V2 Registers */
56 #define CSI2TX_V2_DPHY_CFG_REG 0x28
57 #define CSI2TX_V2_DPHY_CFG_RESET BIT(16)
58 #define CSI2TX_V2_DPHY_CFG_CLOCK_MODE BIT(10)
59 #define CSI2TX_V2_DPHY_CFG_MODE_MASK GENMASK(9, 8)
60 #define CSI2TX_V2_DPHY_CFG_MODE_LPDT (2 << 8)
61 #define CSI2TX_V2_DPHY_CFG_MODE_HS (1 << 8)
62 #define CSI2TX_V2_DPHY_CFG_MODE_ULPS (0 << 8)
63 #define CSI2TX_V2_DPHY_CFG_CLK_ENABLE BIT(4)
64 #define CSI2TX_V2_DPHY_CFG_LANE_ENABLE(n) BIT(n)
66 #define CSI2TX_LANES_MAX 4
67 #define CSI2TX_STREAMS_MAX 4
71 CSI2TX_PAD_SINK_STREAM0
,
72 CSI2TX_PAD_SINK_STREAM1
,
73 CSI2TX_PAD_SINK_STREAM2
,
74 CSI2TX_PAD_SINK_STREAM3
,
86 /* CSI2TX Variant Operations */
88 void (*dphy_setup
)(struct csi2tx_priv
*csi2tx
);
96 * Used to prevent race conditions between multiple,
97 * concurrent calls to start and stop.
103 struct csi2tx_vops
*vops
;
107 struct clk
*pixel_clk
[CSI2TX_STREAMS_MAX
];
109 struct v4l2_subdev subdev
;
110 struct media_pad pads
[CSI2TX_PAD_MAX
];
111 struct v4l2_mbus_framefmt pad_fmts
[CSI2TX_PAD_MAX
];
113 bool has_internal_dphy
;
114 u8 lanes
[CSI2TX_LANES_MAX
];
115 unsigned int num_lanes
;
116 unsigned int max_lanes
;
117 unsigned int max_streams
;
120 static const struct csi2tx_fmt csi2tx_formats
[] = {
122 .mbus
= MEDIA_BUS_FMT_UYVY8_1X16
,
127 .mbus
= MEDIA_BUS_FMT_RGB888_1X24
,
133 static const struct v4l2_mbus_framefmt fmt_default
= {
136 .code
= MEDIA_BUS_FMT_RGB888_1X24
,
137 .field
= V4L2_FIELD_NONE
,
138 .colorspace
= V4L2_COLORSPACE_DEFAULT
,
142 struct csi2tx_priv
*v4l2_subdev_to_csi2tx(struct v4l2_subdev
*subdev
)
144 return container_of(subdev
, struct csi2tx_priv
, subdev
);
147 static const struct csi2tx_fmt
*csi2tx_get_fmt_from_mbus(u32 mbus
)
151 for (i
= 0; i
< ARRAY_SIZE(csi2tx_formats
); i
++)
152 if (csi2tx_formats
[i
].mbus
== mbus
)
153 return &csi2tx_formats
[i
];
158 static int csi2tx_enum_mbus_code(struct v4l2_subdev
*subdev
,
159 struct v4l2_subdev_pad_config
*cfg
,
160 struct v4l2_subdev_mbus_code_enum
*code
)
162 if (code
->pad
|| code
->index
>= ARRAY_SIZE(csi2tx_formats
))
165 code
->code
= csi2tx_formats
[code
->index
].mbus
;
170 static struct v4l2_mbus_framefmt
*
171 __csi2tx_get_pad_format(struct v4l2_subdev
*subdev
,
172 struct v4l2_subdev_pad_config
*cfg
,
173 struct v4l2_subdev_format
*fmt
)
175 struct csi2tx_priv
*csi2tx
= v4l2_subdev_to_csi2tx(subdev
);
177 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
178 return v4l2_subdev_get_try_format(subdev
, cfg
,
181 return &csi2tx
->pad_fmts
[fmt
->pad
];
184 static int csi2tx_get_pad_format(struct v4l2_subdev
*subdev
,
185 struct v4l2_subdev_pad_config
*cfg
,
186 struct v4l2_subdev_format
*fmt
)
188 const struct v4l2_mbus_framefmt
*format
;
190 /* Multiplexed pad? */
191 if (fmt
->pad
== CSI2TX_PAD_SOURCE
)
194 format
= __csi2tx_get_pad_format(subdev
, cfg
, fmt
);
198 fmt
->format
= *format
;
203 static int csi2tx_set_pad_format(struct v4l2_subdev
*subdev
,
204 struct v4l2_subdev_pad_config
*cfg
,
205 struct v4l2_subdev_format
*fmt
)
207 const struct v4l2_mbus_framefmt
*src_format
= &fmt
->format
;
208 struct v4l2_mbus_framefmt
*dst_format
;
210 /* Multiplexed pad? */
211 if (fmt
->pad
== CSI2TX_PAD_SOURCE
)
214 if (!csi2tx_get_fmt_from_mbus(fmt
->format
.code
))
215 src_format
= &fmt_default
;
217 dst_format
= __csi2tx_get_pad_format(subdev
, cfg
, fmt
);
221 *dst_format
= *src_format
;
226 static const struct v4l2_subdev_pad_ops csi2tx_pad_ops
= {
227 .enum_mbus_code
= csi2tx_enum_mbus_code
,
228 .get_fmt
= csi2tx_get_pad_format
,
229 .set_fmt
= csi2tx_set_pad_format
,
232 /* Set Wake Up value in the D-PHY */
233 static void csi2tx_dphy_set_wakeup(struct csi2tx_priv
*csi2tx
)
235 writel(CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(32),
236 csi2tx
->base
+ CSI2TX_DPHY_CLK_WAKEUP_REG
);
240 * Finishes the D-PHY initialization
241 * reg dphy cfg value to be used
243 static void csi2tx_dphy_init_finish(struct csi2tx_priv
*csi2tx
, u32 reg
)
249 /* Enable our (clock and data) lanes */
250 reg
|= CSI2TX_DPHY_CFG_CLK_ENABLE
;
251 for (i
= 0; i
< csi2tx
->num_lanes
; i
++)
252 reg
|= CSI2TX_DPHY_CFG_LANE_ENABLE(csi2tx
->lanes
[i
] - 1);
253 writel(reg
, csi2tx
->base
+ CSI2TX_DPHY_CFG_REG
);
257 /* Switch to HS mode */
258 reg
&= ~CSI2TX_DPHY_CFG_MODE_MASK
;
259 writel(reg
| CSI2TX_DPHY_CFG_MODE_HS
,
260 csi2tx
->base
+ CSI2TX_DPHY_CFG_REG
);
263 /* Configures D-PHY in CSIv1.3 */
264 static void csi2tx_dphy_setup(struct csi2tx_priv
*csi2tx
)
269 csi2tx_dphy_set_wakeup(csi2tx
);
271 /* Put our lanes (clock and data) out of reset */
272 reg
= CSI2TX_DPHY_CFG_CLK_RESET
| CSI2TX_DPHY_CFG_MODE_LPDT
;
273 for (i
= 0; i
< csi2tx
->num_lanes
; i
++)
274 reg
|= CSI2TX_DPHY_CFG_LANE_RESET(csi2tx
->lanes
[i
] - 1);
275 writel(reg
, csi2tx
->base
+ CSI2TX_DPHY_CFG_REG
);
277 csi2tx_dphy_init_finish(csi2tx
, reg
);
280 /* Configures D-PHY in CSIv2 */
281 static void csi2tx_v2_dphy_setup(struct csi2tx_priv
*csi2tx
)
285 csi2tx_dphy_set_wakeup(csi2tx
);
287 /* Put our lanes (clock and data) out of reset */
288 reg
= CSI2TX_V2_DPHY_CFG_RESET
| CSI2TX_V2_DPHY_CFG_MODE_LPDT
;
289 writel(reg
, csi2tx
->base
+ CSI2TX_V2_DPHY_CFG_REG
);
291 csi2tx_dphy_init_finish(csi2tx
, reg
);
294 static void csi2tx_reset(struct csi2tx_priv
*csi2tx
)
296 writel(CSI2TX_CONFIG_SRST_REQ
, csi2tx
->base
+ CSI2TX_CONFIG_REG
);
301 static int csi2tx_start(struct csi2tx_priv
*csi2tx
)
303 struct media_entity
*entity
= &csi2tx
->subdev
.entity
;
304 struct media_link
*link
;
307 csi2tx_reset(csi2tx
);
309 writel(CSI2TX_CONFIG_CFG_REQ
, csi2tx
->base
+ CSI2TX_CONFIG_REG
);
313 if (csi2tx
->vops
&& csi2tx
->vops
->dphy_setup
) {
314 csi2tx
->vops
->dphy_setup(csi2tx
);
319 * Create a static mapping between the CSI virtual channels
320 * and the input streams.
322 * This should be enhanced, but v4l2 lacks the support for
323 * changing that mapping dynamically at the moment.
325 * We're protected from the userspace setting up links at the
326 * same time by the upper layer having called
327 * media_pipeline_start().
329 list_for_each_entry(link
, &entity
->links
, list
) {
330 struct v4l2_mbus_framefmt
*mfmt
;
331 const struct csi2tx_fmt
*fmt
;
335 /* Only consider our enabled input pads */
336 for (i
= CSI2TX_PAD_SINK_STREAM0
; i
< CSI2TX_PAD_MAX
; i
++) {
337 struct media_pad
*pad
= &csi2tx
->pads
[i
];
339 if ((pad
== link
->sink
) &&
340 (link
->flags
& MEDIA_LNK_FL_ENABLED
)) {
349 mfmt
= &csi2tx
->pad_fmts
[pad_idx
];
350 fmt
= csi2tx_get_fmt_from_mbus(mfmt
->code
);
354 stream
= pad_idx
- CSI2TX_PAD_SINK_STREAM0
;
357 * We use the stream ID there, but it's wrong.
359 * A stream could very well send a data type that is
360 * not equal to its stream ID. We need to find a
361 * proper way to address it.
363 writel(CSI2TX_DT_CFG_DT(fmt
->dt
),
364 csi2tx
->base
+ CSI2TX_DT_CFG_REG(stream
));
366 writel(CSI2TX_DT_FORMAT_BYTES_PER_LINE(mfmt
->width
* fmt
->bpp
) |
367 CSI2TX_DT_FORMAT_MAX_LINE_NUM(mfmt
->height
+ 1),
368 csi2tx
->base
+ CSI2TX_DT_FORMAT_REG(stream
));
371 * TODO: This needs to be calculated based on the
372 * output CSI2 clock rate.
374 writel(CSI2TX_STREAM_IF_CFG_FILL_LEVEL(4),
375 csi2tx
->base
+ CSI2TX_STREAM_IF_CFG_REG(stream
));
378 /* Disable the configuration mode */
379 writel(0, csi2tx
->base
+ CSI2TX_CONFIG_REG
);
384 static void csi2tx_stop(struct csi2tx_priv
*csi2tx
)
386 writel(CSI2TX_CONFIG_CFG_REQ
| CSI2TX_CONFIG_SRST_REQ
,
387 csi2tx
->base
+ CSI2TX_CONFIG_REG
);
390 static int csi2tx_s_stream(struct v4l2_subdev
*subdev
, int enable
)
392 struct csi2tx_priv
*csi2tx
= v4l2_subdev_to_csi2tx(subdev
);
395 mutex_lock(&csi2tx
->lock
);
399 * If we're not the first users, there's no need to
400 * enable the whole controller.
402 if (!csi2tx
->count
) {
403 ret
= csi2tx_start(csi2tx
);
413 * Let the last user turn off the lights.
420 mutex_unlock(&csi2tx
->lock
);
424 static const struct v4l2_subdev_video_ops csi2tx_video_ops
= {
425 .s_stream
= csi2tx_s_stream
,
428 static const struct v4l2_subdev_ops csi2tx_subdev_ops
= {
429 .pad
= &csi2tx_pad_ops
,
430 .video
= &csi2tx_video_ops
,
433 static int csi2tx_get_resources(struct csi2tx_priv
*csi2tx
,
434 struct platform_device
*pdev
)
436 struct resource
*res
;
440 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
441 csi2tx
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
442 if (IS_ERR(csi2tx
->base
))
443 return PTR_ERR(csi2tx
->base
);
445 csi2tx
->p_clk
= devm_clk_get(&pdev
->dev
, "p_clk");
446 if (IS_ERR(csi2tx
->p_clk
)) {
447 dev_err(&pdev
->dev
, "Couldn't get p_clk\n");
448 return PTR_ERR(csi2tx
->p_clk
);
451 csi2tx
->esc_clk
= devm_clk_get(&pdev
->dev
, "esc_clk");
452 if (IS_ERR(csi2tx
->esc_clk
)) {
453 dev_err(&pdev
->dev
, "Couldn't get the esc_clk\n");
454 return PTR_ERR(csi2tx
->esc_clk
);
457 clk_prepare_enable(csi2tx
->p_clk
);
458 dev_cfg
= readl(csi2tx
->base
+ CSI2TX_DEVICE_CONFIG_REG
);
459 clk_disable_unprepare(csi2tx
->p_clk
);
461 csi2tx
->max_lanes
= dev_cfg
& CSI2TX_DEVICE_CONFIG_LANES_MASK
;
462 if (csi2tx
->max_lanes
> CSI2TX_LANES_MAX
) {
463 dev_err(&pdev
->dev
, "Invalid number of lanes: %u\n",
468 csi2tx
->max_streams
= (dev_cfg
& CSI2TX_DEVICE_CONFIG_STREAMS_MASK
) >> 4;
469 if (csi2tx
->max_streams
> CSI2TX_STREAMS_MAX
) {
470 dev_err(&pdev
->dev
, "Invalid number of streams: %u\n",
471 csi2tx
->max_streams
);
475 csi2tx
->has_internal_dphy
= !!(dev_cfg
& CSI2TX_DEVICE_CONFIG_HAS_DPHY
);
477 for (i
= 0; i
< csi2tx
->max_streams
; i
++) {
480 snprintf(clk_name
, sizeof(clk_name
), "pixel_if%u_clk", i
);
481 csi2tx
->pixel_clk
[i
] = devm_clk_get(&pdev
->dev
, clk_name
);
482 if (IS_ERR(csi2tx
->pixel_clk
[i
])) {
483 dev_err(&pdev
->dev
, "Couldn't get clock %s\n",
485 return PTR_ERR(csi2tx
->pixel_clk
[i
]);
492 static int csi2tx_check_lanes(struct csi2tx_priv
*csi2tx
)
494 struct v4l2_fwnode_endpoint v4l2_ep
= { .bus_type
= 0 };
495 struct device_node
*ep
;
498 ep
= of_graph_get_endpoint_by_regs(csi2tx
->dev
->of_node
, 0, 0);
502 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep
), &v4l2_ep
);
504 dev_err(csi2tx
->dev
, "Could not parse v4l2 endpoint\n");
508 if (v4l2_ep
.bus_type
!= V4L2_MBUS_CSI2_DPHY
) {
509 dev_err(csi2tx
->dev
, "Unsupported media bus type: 0x%x\n",
515 csi2tx
->num_lanes
= v4l2_ep
.bus
.mipi_csi2
.num_data_lanes
;
516 if (csi2tx
->num_lanes
> csi2tx
->max_lanes
) {
518 "Current configuration uses more lanes than supported\n");
523 for (i
= 0; i
< csi2tx
->num_lanes
; i
++) {
524 if (v4l2_ep
.bus
.mipi_csi2
.data_lanes
[i
] < 1) {
525 dev_err(csi2tx
->dev
, "Invalid lane[%d] number: %u\n",
526 i
, v4l2_ep
.bus
.mipi_csi2
.data_lanes
[i
]);
532 memcpy(csi2tx
->lanes
, v4l2_ep
.bus
.mipi_csi2
.data_lanes
,
533 sizeof(csi2tx
->lanes
));
540 static const struct csi2tx_vops csi2tx_vops
= {
541 .dphy_setup
= csi2tx_dphy_setup
,
544 static const struct csi2tx_vops csi2tx_v2_vops
= {
545 .dphy_setup
= csi2tx_v2_dphy_setup
,
548 static const struct of_device_id csi2tx_of_table
[] = {
550 .compatible
= "cdns,csi2tx",
554 .compatible
= "cdns,csi2tx-1.3",
558 .compatible
= "cdns,csi2tx-2.1",
559 .data
= &csi2tx_v2_vops
563 MODULE_DEVICE_TABLE(of
, csi2tx_of_table
);
565 static int csi2tx_probe(struct platform_device
*pdev
)
567 struct csi2tx_priv
*csi2tx
;
568 const struct of_device_id
*of_id
;
572 csi2tx
= kzalloc(sizeof(*csi2tx
), GFP_KERNEL
);
575 platform_set_drvdata(pdev
, csi2tx
);
576 mutex_init(&csi2tx
->lock
);
577 csi2tx
->dev
= &pdev
->dev
;
579 ret
= csi2tx_get_resources(csi2tx
, pdev
);
583 of_id
= of_match_node(csi2tx_of_table
, pdev
->dev
.of_node
);
584 csi2tx
->vops
= (struct csi2tx_vops
*)of_id
->data
;
586 v4l2_subdev_init(&csi2tx
->subdev
, &csi2tx_subdev_ops
);
587 csi2tx
->subdev
.owner
= THIS_MODULE
;
588 csi2tx
->subdev
.dev
= &pdev
->dev
;
589 csi2tx
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
590 snprintf(csi2tx
->subdev
.name
, V4L2_SUBDEV_NAME_SIZE
, "%s.%s",
591 KBUILD_MODNAME
, dev_name(&pdev
->dev
));
593 ret
= csi2tx_check_lanes(csi2tx
);
597 /* Create our media pads */
598 csi2tx
->subdev
.entity
.function
= MEDIA_ENT_F_VID_IF_BRIDGE
;
599 csi2tx
->pads
[CSI2TX_PAD_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
600 for (i
= CSI2TX_PAD_SINK_STREAM0
; i
< CSI2TX_PAD_MAX
; i
++)
601 csi2tx
->pads
[i
].flags
= MEDIA_PAD_FL_SINK
;
604 * Only the input pads are considered to have a format at the
605 * moment. The CSI link can multiplex various streams with
606 * different formats, and we can't expose this in v4l2 right
609 for (i
= CSI2TX_PAD_SINK_STREAM0
; i
< CSI2TX_PAD_MAX
; i
++)
610 csi2tx
->pad_fmts
[i
] = fmt_default
;
612 ret
= media_entity_pads_init(&csi2tx
->subdev
.entity
, CSI2TX_PAD_MAX
,
617 ret
= v4l2_async_register_subdev(&csi2tx
->subdev
);
622 "Probed CSI2TX with %u/%u lanes, %u streams, %s D-PHY\n",
623 csi2tx
->num_lanes
, csi2tx
->max_lanes
, csi2tx
->max_streams
,
624 csi2tx
->has_internal_dphy
? "internal" : "no");
633 static int csi2tx_remove(struct platform_device
*pdev
)
635 struct csi2tx_priv
*csi2tx
= platform_get_drvdata(pdev
);
637 v4l2_async_unregister_subdev(&csi2tx
->subdev
);
643 static struct platform_driver csi2tx_driver
= {
644 .probe
= csi2tx_probe
,
645 .remove
= csi2tx_remove
,
648 .name
= "cdns-csi2tx",
649 .of_match_table
= csi2tx_of_table
,
652 module_platform_driver(csi2tx_driver
);
653 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin.com>");
654 MODULE_DESCRIPTION("Cadence CSI2-TX controller");
655 MODULE_LICENSE("GPL");