1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Rockchip ISP1 Driver - Base driver
5 * Copyright (C) 2019 Collabora, Ltd.
7 * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd.
8 * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
11 #include <linux/clk.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
15 #include <linux/of_graph.h>
16 #include <linux/of_platform.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/pm_runtime.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-mc.h>
22 #include "rkisp1-common.h"
23 #include "rkisp1-csi.h"
30 * MIPI serial camera interface
31 * Image Signal Processing
32 * Many Image Enhancement Blocks
35 * RBG display ready image
40 * rkisp1-resizer.c rkisp1-capture.c
41 * |====================| |=======================|
42 * rkisp1-isp.c Main Picture Path
43 * |==========================| |===============================================|
44 * +-----------+ +--+--+--+--+ +--------+ +--------+ +-----------+
45 * | | | | | | | | | | | | |
46 * +--------+ |\ | | | | | | | -->| Crop |->| RSZ |------------->| |
47 * | MIPI |--->| \ | | | | | | | | | | | | | |
48 * +--------+ | | | | |IE|IE|IE|IE| | +--------+ +--------+ | Memory |
49 * |MUX|--->| ISP |->|0 |1 |2 |3 |---+ | Interface |
50 * +--------+ | | | | | | | | | | +--------+ +--------+ +--------+ | |
51 * |Parallel|--->| / | | | | | | | | | | | | | | | |
52 * +--------+ |/ | | | | | | | -->| Crop |->| RSZ |->| RGB |->| |
53 * | | | | | | | | | | | | Rotate | | |
54 * +-----------+ +--+--+--+--+ +--------+ +--------+ +--------+ +-----------+
56 * +--------+ | |===============================================|
57 * | DMA |------------------------------------+ Self Picture Path
60 * rkisp1-stats.c rkisp1-params.c
61 * |===============| |===============|
62 * +---------------+ +---------------+
66 * +---------------+ +---------------+
72 * +----------+ +----------+
73 * | Sensor 1 | | Sensor X |
74 * ------------ ... ------------
76 * +----------+ +----------+
85 * --------------- +-----------+
87 * +-------------+ | (output) |
91 * | 0 | 1 |<---------+
95 * +-------------| 2 | 3 |----------+
99 * +- ---------+ +-----------+ +-----------+
100 * | 0 | | 0 | | stats |
101 * ------------- ------------- | (capture) |
102 * | Resizer | | Resizer | +-----------+
103 * ------------| ------------|
105 * +-----------+ +-----------+
108 * +-----------+ +-----------+
109 * | selfpath | | mainpath |
110 * | (capture) | | (capture) |
111 * +-----------+ +-----------+
114 struct rkisp1_isr_data {
116 irqreturn_t (*isr)(int irq, void *ctx);
119 /* ----------------------------------------------------------------------------
123 static int rkisp1_subdev_notifier_bound(struct v4l2_async_notifier *notifier,
124 struct v4l2_subdev *sd,
125 struct v4l2_async_subdev *asd)
127 struct rkisp1_device *rkisp1 =
128 container_of(notifier, struct rkisp1_device, notifier);
129 struct rkisp1_sensor_async *s_asd =
130 container_of(asd, struct rkisp1_sensor_async, asd);
136 source_pad = media_entity_get_fwnode_pad(&sd->entity, s_asd->source_ep,
137 MEDIA_PAD_FL_SOURCE);
138 if (source_pad < 0) {
139 dev_err(rkisp1->dev, "failed to find source pad for %s\n",
144 if (s_asd->port == 0)
145 return rkisp1_csi_link_sensor(rkisp1, sd, s_asd, source_pad);
147 ret = media_create_pad_link(&sd->entity, source_pad,
148 &rkisp1->isp.sd.entity,
149 RKISP1_ISP_PAD_SINK_VIDEO,
150 !s_asd->index ? MEDIA_LNK_FL_ENABLED : 0);
152 dev_err(rkisp1->dev, "failed to link source pad of %s\n",
160 static int rkisp1_subdev_notifier_complete(struct v4l2_async_notifier *notifier)
162 struct rkisp1_device *rkisp1 =
163 container_of(notifier, struct rkisp1_device, notifier);
165 return v4l2_device_register_subdev_nodes(&rkisp1->v4l2_dev);
168 static void rkisp1_subdev_notifier_destroy(struct v4l2_async_subdev *asd)
170 struct rkisp1_sensor_async *rk_asd =
171 container_of(asd, struct rkisp1_sensor_async, asd);
173 fwnode_handle_put(rk_asd->source_ep);
176 static const struct v4l2_async_notifier_operations rkisp1_subdev_notifier_ops = {
177 .bound = rkisp1_subdev_notifier_bound,
178 .complete = rkisp1_subdev_notifier_complete,
179 .destroy = rkisp1_subdev_notifier_destroy,
182 static int rkisp1_subdev_notifier_register(struct rkisp1_device *rkisp1)
184 struct v4l2_async_notifier *ntf = &rkisp1->notifier;
185 struct fwnode_handle *fwnode = dev_fwnode(rkisp1->dev);
186 struct fwnode_handle *ep;
187 unsigned int index = 0;
190 v4l2_async_nf_init(ntf);
192 ntf->ops = &rkisp1_subdev_notifier_ops;
194 fwnode_graph_for_each_endpoint(fwnode, ep) {
195 struct fwnode_handle *port;
196 struct v4l2_fwnode_endpoint vep = { };
197 struct rkisp1_sensor_async *rk_asd;
198 struct fwnode_handle *source;
201 /* Select the bus type based on the port. */
202 port = fwnode_get_parent(ep);
203 fwnode_property_read_u32(port, "reg", ®);
204 fwnode_handle_put(port);
208 /* MIPI CSI-2 port */
209 if (!(rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2)) {
211 "internal CSI must be available for port 0\n");
216 vep.bus_type = V4L2_MBUS_CSI2_DPHY;
221 * Parallel port. The bus-type property in DT is
222 * mandatory for port 1, it will be used to determine if
223 * it's PARALLEL or BT656.
225 vep.bus_type = V4L2_MBUS_UNKNOWN;
229 /* Parse the endpoint and validate the bus type. */
230 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
232 dev_err(rkisp1->dev, "failed to parse endpoint %pfw\n",
237 if (vep.base.port == 1) {
238 if (vep.bus_type != V4L2_MBUS_PARALLEL &&
239 vep.bus_type != V4L2_MBUS_BT656) {
241 "port 1 must be parallel or BT656\n");
247 /* Add the async subdev to the notifier. */
248 source = fwnode_graph_get_remote_endpoint(ep);
251 "endpoint %pfw has no remote endpoint\n",
257 rk_asd = v4l2_async_nf_add_fwnode(ntf, source,
258 struct rkisp1_sensor_async);
259 if (IS_ERR(rk_asd)) {
260 fwnode_handle_put(source);
261 ret = PTR_ERR(rk_asd);
265 rk_asd->index = index++;
266 rk_asd->source_ep = source;
267 rk_asd->mbus_type = vep.bus_type;
268 rk_asd->port = vep.base.port;
270 if (vep.bus_type == V4L2_MBUS_CSI2_DPHY) {
271 rk_asd->mbus_flags = vep.bus.mipi_csi2.flags;
272 rk_asd->lanes = vep.bus.mipi_csi2.num_data_lanes;
274 rk_asd->mbus_flags = vep.bus.parallel.flags;
277 dev_dbg(rkisp1->dev, "registered ep id %d, bus type %u, %u lanes\n",
278 vep.base.id, rk_asd->mbus_type, rk_asd->lanes);
282 fwnode_handle_put(ep);
283 v4l2_async_nf_cleanup(ntf);
288 dev_dbg(rkisp1->dev, "no remote subdevice found\n");
290 ret = v4l2_async_nf_register(&rkisp1->v4l2_dev, ntf);
292 v4l2_async_nf_cleanup(ntf);
299 /* ----------------------------------------------------------------------------
303 static int __maybe_unused rkisp1_runtime_suspend(struct device *dev)
305 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
307 clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks);
308 return pinctrl_pm_select_sleep_state(dev);
311 static int __maybe_unused rkisp1_runtime_resume(struct device *dev)
313 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
316 ret = pinctrl_pm_select_default_state(dev);
319 ret = clk_bulk_prepare_enable(rkisp1->clk_size, rkisp1->clks);
326 static const struct dev_pm_ops rkisp1_pm_ops = {
327 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
328 pm_runtime_force_resume)
329 SET_RUNTIME_PM_OPS(rkisp1_runtime_suspend, rkisp1_runtime_resume, NULL)
332 /* ----------------------------------------------------------------------------
336 static int rkisp1_create_links(struct rkisp1_device *rkisp1)
341 if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) {
342 /* Link the CSI receiver to the ISP. */
343 ret = media_create_pad_link(&rkisp1->csi.sd.entity,
345 &rkisp1->isp.sd.entity,
346 RKISP1_ISP_PAD_SINK_VIDEO,
347 MEDIA_LNK_FL_ENABLED);
352 /* create ISP->RSZ->CAP links */
353 for (i = 0; i < 2; i++) {
354 struct media_entity *resizer =
355 &rkisp1->resizer_devs[i].sd.entity;
356 struct media_entity *capture =
357 &rkisp1->capture_devs[i].vnode.vdev.entity;
359 ret = media_create_pad_link(&rkisp1->isp.sd.entity,
360 RKISP1_ISP_PAD_SOURCE_VIDEO,
361 resizer, RKISP1_RSZ_PAD_SINK,
362 MEDIA_LNK_FL_ENABLED);
366 ret = media_create_pad_link(resizer, RKISP1_RSZ_PAD_SRC,
368 MEDIA_LNK_FL_ENABLED |
369 MEDIA_LNK_FL_IMMUTABLE);
375 ret = media_create_pad_link(&rkisp1->params.vnode.vdev.entity, 0,
376 &rkisp1->isp.sd.entity,
377 RKISP1_ISP_PAD_SINK_PARAMS,
378 MEDIA_LNK_FL_ENABLED |
379 MEDIA_LNK_FL_IMMUTABLE);
384 return media_create_pad_link(&rkisp1->isp.sd.entity,
385 RKISP1_ISP_PAD_SOURCE_STATS,
386 &rkisp1->stats.vnode.vdev.entity, 0,
387 MEDIA_LNK_FL_ENABLED |
388 MEDIA_LNK_FL_IMMUTABLE);
391 static void rkisp1_entities_unregister(struct rkisp1_device *rkisp1)
393 if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2)
394 rkisp1_csi_unregister(rkisp1);
395 rkisp1_params_unregister(rkisp1);
396 rkisp1_stats_unregister(rkisp1);
397 rkisp1_capture_devs_unregister(rkisp1);
398 rkisp1_resizer_devs_unregister(rkisp1);
399 rkisp1_isp_unregister(rkisp1);
402 static int rkisp1_entities_register(struct rkisp1_device *rkisp1)
406 ret = rkisp1_isp_register(rkisp1);
410 ret = rkisp1_resizer_devs_register(rkisp1);
414 ret = rkisp1_capture_devs_register(rkisp1);
418 ret = rkisp1_stats_register(rkisp1);
422 ret = rkisp1_params_register(rkisp1);
426 if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) {
427 ret = rkisp1_csi_register(rkisp1);
432 ret = rkisp1_create_links(rkisp1);
439 rkisp1_entities_unregister(rkisp1);
443 static irqreturn_t rkisp1_isr(int irq, void *ctx)
446 * Call rkisp1_capture_isr() first to handle the frame that
447 * potentially completed using the current frame_sequence number before
448 * it is potentially incremented by rkisp1_isp_isr() in the vertical
451 rkisp1_capture_isr(irq, ctx);
452 rkisp1_isp_isr(irq, ctx);
453 rkisp1_csi_isr(irq, ctx);
458 static const char * const px30_isp_clks[] = {
465 static const struct rkisp1_isr_data px30_isp_isrs[] = {
466 { "isp", rkisp1_isp_isr },
467 { "mi", rkisp1_capture_isr },
468 { "mipi", rkisp1_csi_isr },
471 static const struct rkisp1_info px30_isp_info = {
472 .clks = px30_isp_clks,
473 .clk_size = ARRAY_SIZE(px30_isp_clks),
474 .isrs = px30_isp_isrs,
475 .isr_size = ARRAY_SIZE(px30_isp_isrs),
476 .isp_ver = RKISP1_V12,
477 .features = RKISP1_FEATURE_MIPI_CSI2,
480 static const char * const rk3399_isp_clks[] = {
486 static const struct rkisp1_isr_data rk3399_isp_isrs[] = {
487 { NULL, rkisp1_isr },
490 static const struct rkisp1_info rk3399_isp_info = {
491 .clks = rk3399_isp_clks,
492 .clk_size = ARRAY_SIZE(rk3399_isp_clks),
493 .isrs = rk3399_isp_isrs,
494 .isr_size = ARRAY_SIZE(rk3399_isp_isrs),
495 .isp_ver = RKISP1_V10,
496 .features = RKISP1_FEATURE_MIPI_CSI2,
499 static const struct of_device_id rkisp1_of_match[] = {
501 .compatible = "rockchip,px30-cif-isp",
502 .data = &px30_isp_info,
505 .compatible = "rockchip,rk3399-cif-isp",
506 .data = &rk3399_isp_info,
510 MODULE_DEVICE_TABLE(of, rkisp1_of_match);
512 static int rkisp1_probe(struct platform_device *pdev)
514 const struct rkisp1_info *info;
515 struct device *dev = &pdev->dev;
516 struct rkisp1_device *rkisp1;
517 struct v4l2_device *v4l2_dev;
522 rkisp1 = devm_kzalloc(dev, sizeof(*rkisp1), GFP_KERNEL);
526 info = of_device_get_match_data(dev);
529 dev_set_drvdata(dev, rkisp1);
532 mutex_init(&rkisp1->stream_lock);
534 rkisp1->base_addr = devm_platform_ioremap_resource(pdev, 0);
535 if (IS_ERR(rkisp1->base_addr))
536 return PTR_ERR(rkisp1->base_addr);
538 for (i = 0; i < info->isr_size; i++) {
539 irq = info->isrs[i].name
540 ? platform_get_irq_byname(pdev, info->isrs[i].name)
541 : platform_get_irq(pdev, i);
545 ret = devm_request_irq(dev, irq, info->isrs[i].isr, IRQF_SHARED,
546 dev_driver_string(dev), dev);
548 dev_err(dev, "request irq failed: %d\n", ret);
553 for (i = 0; i < info->clk_size; i++)
554 rkisp1->clks[i].id = info->clks[i];
555 ret = devm_clk_bulk_get(dev, info->clk_size, rkisp1->clks);
558 rkisp1->clk_size = info->clk_size;
560 pm_runtime_enable(&pdev->dev);
562 ret = pm_runtime_resume_and_get(&pdev->dev);
564 goto err_pm_runtime_disable;
566 cif_id = rkisp1_read(rkisp1, RKISP1_CIF_VI_ID);
567 dev_dbg(rkisp1->dev, "CIF_ID 0x%08x\n", cif_id);
569 pm_runtime_put(&pdev->dev);
571 rkisp1->media_dev.hw_revision = info->isp_ver;
572 strscpy(rkisp1->media_dev.model, RKISP1_DRIVER_NAME,
573 sizeof(rkisp1->media_dev.model));
574 rkisp1->media_dev.dev = &pdev->dev;
575 strscpy(rkisp1->media_dev.bus_info, RKISP1_BUS_INFO,
576 sizeof(rkisp1->media_dev.bus_info));
577 media_device_init(&rkisp1->media_dev);
579 v4l2_dev = &rkisp1->v4l2_dev;
580 v4l2_dev->mdev = &rkisp1->media_dev;
581 strscpy(v4l2_dev->name, RKISP1_DRIVER_NAME, sizeof(v4l2_dev->name));
583 ret = v4l2_device_register(rkisp1->dev, &rkisp1->v4l2_dev);
585 goto err_pm_runtime_disable;
587 ret = media_device_register(&rkisp1->media_dev);
589 dev_err(dev, "Failed to register media device: %d\n", ret);
590 goto err_unreg_v4l2_dev;
593 if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) {
594 ret = rkisp1_csi_init(rkisp1);
596 goto err_unreg_media_dev;
599 ret = rkisp1_entities_register(rkisp1);
601 goto err_cleanup_csi;
603 ret = rkisp1_subdev_notifier_register(rkisp1);
605 goto err_unreg_entities;
607 rkisp1_debug_init(rkisp1);
612 rkisp1_entities_unregister(rkisp1);
614 if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2)
615 rkisp1_csi_cleanup(rkisp1);
617 media_device_unregister(&rkisp1->media_dev);
619 v4l2_device_unregister(&rkisp1->v4l2_dev);
620 err_pm_runtime_disable:
621 pm_runtime_disable(&pdev->dev);
625 static void rkisp1_remove(struct platform_device *pdev)
627 struct rkisp1_device *rkisp1 = platform_get_drvdata(pdev);
629 v4l2_async_nf_unregister(&rkisp1->notifier);
630 v4l2_async_nf_cleanup(&rkisp1->notifier);
632 rkisp1_entities_unregister(rkisp1);
633 if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2)
634 rkisp1_csi_cleanup(rkisp1);
635 rkisp1_debug_cleanup(rkisp1);
637 media_device_unregister(&rkisp1->media_dev);
638 v4l2_device_unregister(&rkisp1->v4l2_dev);
640 pm_runtime_disable(&pdev->dev);
643 static struct platform_driver rkisp1_drv = {
645 .name = RKISP1_DRIVER_NAME,
646 .of_match_table = of_match_ptr(rkisp1_of_match),
647 .pm = &rkisp1_pm_ops,
649 .probe = rkisp1_probe,
650 .remove_new = rkisp1_remove,
653 module_platform_driver(rkisp1_drv);
654 MODULE_DESCRIPTION("Rockchip ISP1 platform driver");
655 MODULE_LICENSE("Dual MIT/GPL");