1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Cadence MIPI-CSI2 RX Controller v1.3
5 * Copyright (C) 2017 Cadence Design Systems Inc.
9 #include <linux/delay.h>
11 #include <linux/module.h>
13 #include <linux/of_graph.h>
14 #include <linux/phy/phy.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 CSI2RX_DEVICE_CFG_REG 0x000
25 #define CSI2RX_SOFT_RESET_REG 0x004
26 #define CSI2RX_SOFT_RESET_PROTOCOL BIT(1)
27 #define CSI2RX_SOFT_RESET_FRONT BIT(0)
29 #define CSI2RX_STATIC_CFG_REG 0x008
30 #define CSI2RX_STATIC_CFG_DLANE_MAP(llane, plane) ((plane) << (16 + (llane) * 4))
31 #define CSI2RX_STATIC_CFG_LANES_MASK GENMASK(11, 8)
33 #define CSI2RX_STREAM_BASE(n) (((n) + 1) * 0x100)
35 #define CSI2RX_STREAM_CTRL_REG(n) (CSI2RX_STREAM_BASE(n) + 0x000)
36 #define CSI2RX_STREAM_CTRL_START BIT(0)
38 #define CSI2RX_STREAM_DATA_CFG_REG(n) (CSI2RX_STREAM_BASE(n) + 0x008)
39 #define CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT BIT(31)
40 #define CSI2RX_STREAM_DATA_CFG_VC_SELECT(n) BIT((n) + 16)
42 #define CSI2RX_STREAM_CFG_REG(n) (CSI2RX_STREAM_BASE(n) + 0x00c)
43 #define CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF (1 << 8)
45 #define CSI2RX_LANES_MAX 4
46 #define CSI2RX_STREAMS_MAX 4
50 CSI2RX_PAD_SOURCE_STREAM0,
51 CSI2RX_PAD_SOURCE_STREAM1,
52 CSI2RX_PAD_SOURCE_STREAM2,
53 CSI2RX_PAD_SOURCE_STREAM3,
62 * Used to prevent race conditions between multiple,
63 * concurrent calls to start and stop.
70 struct clk *pixel_clk[CSI2RX_STREAMS_MAX];
73 u8 lanes[CSI2RX_LANES_MAX];
77 bool has_internal_dphy;
79 struct v4l2_subdev subdev;
80 struct v4l2_async_notifier notifier;
81 struct media_pad pads[CSI2RX_PAD_MAX];
84 struct v4l2_subdev *source_subdev;
89 struct csi2rx_priv *v4l2_subdev_to_csi2rx(struct v4l2_subdev *subdev)
91 return container_of(subdev, struct csi2rx_priv, subdev);
94 static void csi2rx_reset(struct csi2rx_priv *csi2rx)
96 writel(CSI2RX_SOFT_RESET_PROTOCOL | CSI2RX_SOFT_RESET_FRONT,
97 csi2rx->base + CSI2RX_SOFT_RESET_REG);
101 writel(0, csi2rx->base + CSI2RX_SOFT_RESET_REG);
104 static int csi2rx_start(struct csi2rx_priv *csi2rx)
107 unsigned long lanes_used = 0;
111 ret = clk_prepare_enable(csi2rx->p_clk);
115 csi2rx_reset(csi2rx);
117 reg = csi2rx->num_lanes << 8;
118 for (i = 0; i < csi2rx->num_lanes; i++) {
119 reg |= CSI2RX_STATIC_CFG_DLANE_MAP(i, csi2rx->lanes[i]);
120 set_bit(csi2rx->lanes[i], &lanes_used);
124 * Even the unused lanes need to be mapped. In order to avoid
125 * to map twice to the same physical lane, keep the lanes used
126 * in the previous loop, and only map unused physical lanes to
127 * the rest of our logical lanes.
129 for (i = csi2rx->num_lanes; i < csi2rx->max_lanes; i++) {
130 unsigned int idx = find_first_zero_bit(&lanes_used,
132 set_bit(idx, &lanes_used);
133 reg |= CSI2RX_STATIC_CFG_DLANE_MAP(i, i + 1);
136 writel(reg, csi2rx->base + CSI2RX_STATIC_CFG_REG);
138 ret = v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, true);
140 goto err_disable_pclk;
143 * Create a static mapping between the CSI virtual channels
144 * and the output stream.
146 * This should be enhanced, but v4l2 lacks the support for
147 * changing that mapping dynamically.
149 * We also cannot enable and disable independent streams here,
150 * hence the reference counting.
152 for (i = 0; i < csi2rx->max_streams; i++) {
153 ret = clk_prepare_enable(csi2rx->pixel_clk[i]);
155 goto err_disable_pixclk;
157 writel(CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF,
158 csi2rx->base + CSI2RX_STREAM_CFG_REG(i));
160 writel(CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT |
161 CSI2RX_STREAM_DATA_CFG_VC_SELECT(i),
162 csi2rx->base + CSI2RX_STREAM_DATA_CFG_REG(i));
164 writel(CSI2RX_STREAM_CTRL_START,
165 csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
168 ret = clk_prepare_enable(csi2rx->sys_clk);
170 goto err_disable_pixclk;
172 clk_disable_unprepare(csi2rx->p_clk);
178 clk_disable_unprepare(csi2rx->pixel_clk[i - 1]);
181 clk_disable_unprepare(csi2rx->p_clk);
186 static void csi2rx_stop(struct csi2rx_priv *csi2rx)
190 clk_prepare_enable(csi2rx->p_clk);
191 clk_disable_unprepare(csi2rx->sys_clk);
193 for (i = 0; i < csi2rx->max_streams; i++) {
194 writel(0, csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
196 clk_disable_unprepare(csi2rx->pixel_clk[i]);
199 clk_disable_unprepare(csi2rx->p_clk);
201 if (v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, false))
202 dev_warn(csi2rx->dev, "Couldn't disable our subdev\n");
205 static int csi2rx_s_stream(struct v4l2_subdev *subdev, int enable)
207 struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev);
210 mutex_lock(&csi2rx->lock);
214 * If we're not the first users, there's no need to
215 * enable the whole controller.
217 if (!csi2rx->count) {
218 ret = csi2rx_start(csi2rx);
228 * Let the last user turn off the lights.
235 mutex_unlock(&csi2rx->lock);
239 static const struct v4l2_subdev_video_ops csi2rx_video_ops = {
240 .s_stream = csi2rx_s_stream,
243 static const struct v4l2_subdev_ops csi2rx_subdev_ops = {
244 .video = &csi2rx_video_ops,
247 static int csi2rx_async_bound(struct v4l2_async_notifier *notifier,
248 struct v4l2_subdev *s_subdev,
249 struct v4l2_async_subdev *asd)
251 struct v4l2_subdev *subdev = notifier->sd;
252 struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev);
254 csi2rx->source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
256 MEDIA_PAD_FL_SOURCE);
257 if (csi2rx->source_pad < 0) {
258 dev_err(csi2rx->dev, "Couldn't find output pad for subdev %s\n",
260 return csi2rx->source_pad;
263 csi2rx->source_subdev = s_subdev;
265 dev_dbg(csi2rx->dev, "Bound %s pad: %d\n", s_subdev->name,
268 return media_create_pad_link(&csi2rx->source_subdev->entity,
270 &csi2rx->subdev.entity, 0,
271 MEDIA_LNK_FL_ENABLED |
272 MEDIA_LNK_FL_IMMUTABLE);
275 static const struct v4l2_async_notifier_operations csi2rx_notifier_ops = {
276 .bound = csi2rx_async_bound,
279 static int csi2rx_get_resources(struct csi2rx_priv *csi2rx,
280 struct platform_device *pdev)
286 csi2rx->base = devm_platform_ioremap_resource(pdev, 0);
287 if (IS_ERR(csi2rx->base))
288 return PTR_ERR(csi2rx->base);
290 csi2rx->sys_clk = devm_clk_get(&pdev->dev, "sys_clk");
291 if (IS_ERR(csi2rx->sys_clk)) {
292 dev_err(&pdev->dev, "Couldn't get sys clock\n");
293 return PTR_ERR(csi2rx->sys_clk);
296 csi2rx->p_clk = devm_clk_get(&pdev->dev, "p_clk");
297 if (IS_ERR(csi2rx->p_clk)) {
298 dev_err(&pdev->dev, "Couldn't get P clock\n");
299 return PTR_ERR(csi2rx->p_clk);
302 csi2rx->dphy = devm_phy_optional_get(&pdev->dev, "dphy");
303 if (IS_ERR(csi2rx->dphy)) {
304 dev_err(&pdev->dev, "Couldn't get external D-PHY\n");
305 return PTR_ERR(csi2rx->dphy);
309 * FIXME: Once we'll have external D-PHY support, the check
310 * will need to be removed.
313 dev_err(&pdev->dev, "External D-PHY not supported yet\n");
317 ret = clk_prepare_enable(csi2rx->p_clk);
319 dev_err(&pdev->dev, "Couldn't prepare and enable P clock\n");
323 dev_cfg = readl(csi2rx->base + CSI2RX_DEVICE_CFG_REG);
324 clk_disable_unprepare(csi2rx->p_clk);
326 csi2rx->max_lanes = dev_cfg & 7;
327 if (csi2rx->max_lanes > CSI2RX_LANES_MAX) {
328 dev_err(&pdev->dev, "Invalid number of lanes: %u\n",
333 csi2rx->max_streams = (dev_cfg >> 4) & 7;
334 if (csi2rx->max_streams > CSI2RX_STREAMS_MAX) {
335 dev_err(&pdev->dev, "Invalid number of streams: %u\n",
336 csi2rx->max_streams);
340 csi2rx->has_internal_dphy = dev_cfg & BIT(3) ? true : false;
343 * FIXME: Once we'll have internal D-PHY support, the check
344 * will need to be removed.
346 if (csi2rx->has_internal_dphy) {
347 dev_err(&pdev->dev, "Internal D-PHY not supported yet\n");
351 for (i = 0; i < csi2rx->max_streams; i++) {
354 snprintf(clk_name, sizeof(clk_name), "pixel_if%u_clk", i);
355 csi2rx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name);
356 if (IS_ERR(csi2rx->pixel_clk[i])) {
357 dev_err(&pdev->dev, "Couldn't get clock %s\n", clk_name);
358 return PTR_ERR(csi2rx->pixel_clk[i]);
365 static int csi2rx_parse_dt(struct csi2rx_priv *csi2rx)
367 struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
368 struct v4l2_async_subdev *asd;
369 struct fwnode_handle *fwh;
370 struct device_node *ep;
373 ep = of_graph_get_endpoint_by_regs(csi2rx->dev->of_node, 0, 0);
377 fwh = of_fwnode_handle(ep);
378 ret = v4l2_fwnode_endpoint_parse(fwh, &v4l2_ep);
380 dev_err(csi2rx->dev, "Could not parse v4l2 endpoint\n");
385 if (v4l2_ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
386 dev_err(csi2rx->dev, "Unsupported media bus type: 0x%x\n",
392 memcpy(csi2rx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes,
393 sizeof(csi2rx->lanes));
394 csi2rx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
395 if (csi2rx->num_lanes > csi2rx->max_lanes) {
396 dev_err(csi2rx->dev, "Unsupported number of data-lanes: %d\n",
402 v4l2_async_nf_init(&csi2rx->notifier);
404 asd = v4l2_async_nf_add_fwnode_remote(&csi2rx->notifier, fwh,
405 struct v4l2_async_subdev);
410 csi2rx->notifier.ops = &csi2rx_notifier_ops;
412 ret = v4l2_async_subdev_nf_register(&csi2rx->subdev, &csi2rx->notifier);
414 v4l2_async_nf_cleanup(&csi2rx->notifier);
419 static int csi2rx_probe(struct platform_device *pdev)
421 struct csi2rx_priv *csi2rx;
425 csi2rx = kzalloc(sizeof(*csi2rx), GFP_KERNEL);
428 platform_set_drvdata(pdev, csi2rx);
429 csi2rx->dev = &pdev->dev;
430 mutex_init(&csi2rx->lock);
432 ret = csi2rx_get_resources(csi2rx, pdev);
436 ret = csi2rx_parse_dt(csi2rx);
440 csi2rx->subdev.owner = THIS_MODULE;
441 csi2rx->subdev.dev = &pdev->dev;
442 v4l2_subdev_init(&csi2rx->subdev, &csi2rx_subdev_ops);
443 v4l2_set_subdevdata(&csi2rx->subdev, &pdev->dev);
444 snprintf(csi2rx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s",
445 KBUILD_MODNAME, dev_name(&pdev->dev));
447 /* Create our media pads */
448 csi2rx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
449 csi2rx->pads[CSI2RX_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
450 for (i = CSI2RX_PAD_SOURCE_STREAM0; i < CSI2RX_PAD_MAX; i++)
451 csi2rx->pads[i].flags = MEDIA_PAD_FL_SOURCE;
453 ret = media_entity_pads_init(&csi2rx->subdev.entity, CSI2RX_PAD_MAX,
458 ret = v4l2_async_register_subdev(&csi2rx->subdev);
463 "Probed CSI2RX with %u/%u lanes, %u streams, %s D-PHY\n",
464 csi2rx->num_lanes, csi2rx->max_lanes, csi2rx->max_streams,
465 csi2rx->has_internal_dphy ? "internal" : "no");
470 v4l2_async_nf_cleanup(&csi2rx->notifier);
476 static void csi2rx_remove(struct platform_device *pdev)
478 struct csi2rx_priv *csi2rx = platform_get_drvdata(pdev);
480 v4l2_async_unregister_subdev(&csi2rx->subdev);
484 static const struct of_device_id csi2rx_of_table[] = {
485 { .compatible = "cdns,csi2rx" },
488 MODULE_DEVICE_TABLE(of, csi2rx_of_table);
490 static struct platform_driver csi2rx_driver = {
491 .probe = csi2rx_probe,
492 .remove_new = csi2rx_remove,
495 .name = "cdns-csi2rx",
496 .of_match_table = csi2rx_of_table,
499 module_platform_driver(csi2rx_driver);
501 MODULE_DESCRIPTION("Cadence CSI2-RX controller");
502 MODULE_LICENSE("GPL");