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/debugfs.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
16 #include <linux/of_graph.h>
17 #include <linux/of_platform.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/phy/phy.h>
20 #include <linux/phy/phy-mipi-dphy.h>
21 #include <media/v4l2-fwnode.h>
23 #include "rkisp1-common.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 * +---------------+ +---------------+
71 * +----------+ +----------+
72 * | Sensor 2 | | Sensor X |
73 * ------------ ... ------------
75 * +----------+ +----------+ +-----------+
78 * +----------+ \ | +-----------+
80 * ------------ +------+------+ |
81 * | 0 |----->| 0 | 1 |<---------+
82 * +----------+ |------+------|
85 * +-------------| 2 | 3 |----------+
89 * +- ---------+ +-----------+ +-----------+
90 * | 0 | | 0 | | stats |
91 * ------------- ------------- | (capture) |
92 * | Resizer | | Resizer | +-----------+
93 * ------------| ------------|
95 * +-----------+ +-----------+
98 * +-----------+ +-----------+
99 * | selfpath | | mainpath |
100 * | (capture) | | (capture) |
101 * +-----------+ +-----------+
104 struct rkisp1_match_data {
105 const char * const *clks;
109 /* ----------------------------------------------------------------------------
113 static int rkisp1_create_links(struct rkisp1_device *rkisp1)
115 struct media_entity *source, *sink;
116 unsigned int flags, source_pad;
117 struct v4l2_subdev *sd;
122 flags = MEDIA_LNK_FL_ENABLED;
123 list_for_each_entry(sd, &rkisp1->v4l2_dev.subdevs, list) {
124 if (sd == &rkisp1->isp.sd ||
125 sd == &rkisp1->resizer_devs[RKISP1_MAINPATH].sd ||
126 sd == &rkisp1->resizer_devs[RKISP1_SELFPATH].sd)
129 ret = media_entity_get_fwnode_pad(&sd->entity, sd->fwnode,
130 MEDIA_PAD_FL_SOURCE);
132 dev_err(rkisp1->dev, "failed to find src pad for %s\n",
138 ret = media_create_pad_link(&sd->entity, source_pad,
139 &rkisp1->isp.sd.entity,
140 RKISP1_ISP_PAD_SINK_VIDEO,
148 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
150 /* create ISP->RSZ->CAP links */
151 for (i = 0; i < 2; i++) {
152 source = &rkisp1->isp.sd.entity;
153 sink = &rkisp1->resizer_devs[i].sd.entity;
154 ret = media_create_pad_link(source, RKISP1_ISP_PAD_SOURCE_VIDEO,
155 sink, RKISP1_RSZ_PAD_SINK,
156 MEDIA_LNK_FL_ENABLED);
161 sink = &rkisp1->capture_devs[i].vnode.vdev.entity;
162 ret = media_create_pad_link(source, RKISP1_RSZ_PAD_SRC,
169 source = &rkisp1->params.vnode.vdev.entity;
170 sink = &rkisp1->isp.sd.entity;
171 ret = media_create_pad_link(source, 0, sink,
172 RKISP1_ISP_PAD_SINK_PARAMS, flags);
177 source = &rkisp1->isp.sd.entity;
178 sink = &rkisp1->stats.vnode.vdev.entity;
179 return media_create_pad_link(source, RKISP1_ISP_PAD_SOURCE_STATS,
183 static int rkisp1_subdev_notifier_bound(struct v4l2_async_notifier *notifier,
184 struct v4l2_subdev *sd,
185 struct v4l2_async_subdev *asd)
187 struct rkisp1_device *rkisp1 =
188 container_of(notifier, struct rkisp1_device, notifier);
189 struct rkisp1_sensor_async *s_asd =
190 container_of(asd, struct rkisp1_sensor_async, asd);
192 s_asd->pixel_rate_ctrl = v4l2_ctrl_find(sd->ctrl_handler,
193 V4L2_CID_PIXEL_RATE);
195 s_asd->dphy = devm_phy_get(rkisp1->dev, "dphy");
196 if (IS_ERR(s_asd->dphy)) {
197 if (PTR_ERR(s_asd->dphy) != -EPROBE_DEFER)
198 dev_err(rkisp1->dev, "Couldn't get the MIPI D-PHY\n");
199 return PTR_ERR(s_asd->dphy);
202 phy_init(s_asd->dphy);
207 static void rkisp1_subdev_notifier_unbind(struct v4l2_async_notifier *notifier,
208 struct v4l2_subdev *sd,
209 struct v4l2_async_subdev *asd)
211 struct rkisp1_sensor_async *s_asd =
212 container_of(asd, struct rkisp1_sensor_async, asd);
214 phy_exit(s_asd->dphy);
217 static int rkisp1_subdev_notifier_complete(struct v4l2_async_notifier *notifier)
219 struct rkisp1_device *rkisp1 =
220 container_of(notifier, struct rkisp1_device, notifier);
223 ret = rkisp1_create_links(rkisp1);
227 ret = v4l2_device_register_subdev_nodes(&rkisp1->v4l2_dev);
231 dev_dbg(rkisp1->dev, "Async subdev notifier completed\n");
236 static const struct v4l2_async_notifier_operations rkisp1_subdev_notifier_ops = {
237 .bound = rkisp1_subdev_notifier_bound,
238 .unbind = rkisp1_subdev_notifier_unbind,
239 .complete = rkisp1_subdev_notifier_complete,
242 static int rkisp1_subdev_notifier(struct rkisp1_device *rkisp1)
244 struct v4l2_async_notifier *ntf = &rkisp1->notifier;
245 unsigned int next_id = 0;
248 v4l2_async_notifier_init(ntf);
251 struct v4l2_fwnode_endpoint vep = {
252 .bus_type = V4L2_MBUS_CSI2_DPHY
254 struct rkisp1_sensor_async *rk_asd = NULL;
255 struct v4l2_async_subdev *asd;
256 struct fwnode_handle *ep;
258 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(rkisp1->dev),
259 0, next_id, FWNODE_GRAPH_ENDPOINT_NEXT);
263 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
267 asd = v4l2_async_notifier_add_fwnode_remote_subdev(ntf, ep,
274 rk_asd = container_of(asd, struct rkisp1_sensor_async, asd);
275 rk_asd->mbus_type = vep.bus_type;
276 rk_asd->mbus_flags = vep.bus.mipi_csi2.flags;
277 rk_asd->lanes = vep.bus.mipi_csi2.num_data_lanes;
279 dev_dbg(rkisp1->dev, "registered ep id %d with %d lanes\n",
280 vep.base.id, rk_asd->lanes);
282 next_id = vep.base.id + 1;
284 fwnode_handle_put(ep);
288 fwnode_handle_put(ep);
289 v4l2_async_notifier_cleanup(ntf);
294 dev_dbg(rkisp1->dev, "no remote subdevice found\n");
295 ntf->ops = &rkisp1_subdev_notifier_ops;
296 ret = v4l2_async_notifier_register(&rkisp1->v4l2_dev, ntf);
298 v4l2_async_notifier_cleanup(ntf);
304 /* ----------------------------------------------------------------------------
308 static int __maybe_unused rkisp1_runtime_suspend(struct device *dev)
310 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
312 clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks);
313 return pinctrl_pm_select_sleep_state(dev);
316 static int __maybe_unused rkisp1_runtime_resume(struct device *dev)
318 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
321 ret = pinctrl_pm_select_default_state(dev);
324 ret = clk_bulk_prepare_enable(rkisp1->clk_size, rkisp1->clks);
331 static const struct dev_pm_ops rkisp1_pm_ops = {
332 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
333 pm_runtime_force_resume)
334 SET_RUNTIME_PM_OPS(rkisp1_runtime_suspend, rkisp1_runtime_resume, NULL)
337 /* ----------------------------------------------------------------------------
341 static int rkisp1_entities_register(struct rkisp1_device *rkisp1)
345 ret = rkisp1_isp_register(rkisp1);
349 ret = rkisp1_resizer_devs_register(rkisp1);
351 goto err_unreg_isp_subdev;
353 ret = rkisp1_capture_devs_register(rkisp1);
355 goto err_unreg_resizer_devs;
357 ret = rkisp1_stats_register(rkisp1);
359 goto err_unreg_capture_devs;
361 ret = rkisp1_params_register(rkisp1);
363 goto err_unreg_stats;
365 ret = rkisp1_subdev_notifier(rkisp1);
368 "Failed to register subdev notifier(%d)\n", ret);
369 goto err_unreg_params;
374 rkisp1_params_unregister(rkisp1);
376 rkisp1_stats_unregister(rkisp1);
377 err_unreg_capture_devs:
378 rkisp1_capture_devs_unregister(rkisp1);
379 err_unreg_resizer_devs:
380 rkisp1_resizer_devs_unregister(rkisp1);
381 err_unreg_isp_subdev:
382 rkisp1_isp_unregister(rkisp1);
386 static irqreturn_t rkisp1_isr(int irq, void *ctx)
388 struct device *dev = ctx;
389 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
392 * Call rkisp1_capture_isr() first to handle the frame that
393 * potentially completed using the current frame_sequence number before
394 * it is potentially incremented by rkisp1_isp_isr() in the vertical
397 rkisp1_capture_isr(rkisp1);
398 rkisp1_isp_isr(rkisp1);
399 rkisp1_mipi_isr(rkisp1);
404 static const char * const rk3399_isp_clks[] = {
412 static const struct rkisp1_match_data rk3399_isp_clk_data = {
413 .clks = rk3399_isp_clks,
414 .size = ARRAY_SIZE(rk3399_isp_clks),
417 static const struct of_device_id rkisp1_of_match[] = {
419 .compatible = "rockchip,rk3399-cif-isp",
420 .data = &rk3399_isp_clk_data,
424 MODULE_DEVICE_TABLE(of, rkisp1_of_match);
426 static void rkisp1_debug_init(struct rkisp1_device *rkisp1)
428 struct rkisp1_debug *debug = &rkisp1->debug;
430 debug->debugfs_dir = debugfs_create_dir(RKISP1_DRIVER_NAME, NULL);
431 if (!debug->debugfs_dir) {
432 dev_dbg(rkisp1->dev, "failed to create debugfs directory\n");
435 debugfs_create_ulong("data_loss", 0444, debug->debugfs_dir,
437 debugfs_create_ulong("outform_size_err", 0444, debug->debugfs_dir,
438 &debug->outform_size_error);
439 debugfs_create_ulong("img_stabilization_size_error", 0444,
441 &debug->img_stabilization_size_error);
442 debugfs_create_ulong("inform_size_error", 0444, debug->debugfs_dir,
443 &debug->inform_size_error);
444 debugfs_create_ulong("irq_delay", 0444, debug->debugfs_dir,
446 debugfs_create_ulong("mipi_error", 0444, debug->debugfs_dir,
448 debugfs_create_ulong("stats_error", 0444, debug->debugfs_dir,
449 &debug->stats_error);
450 debugfs_create_ulong("mp_stop_timeout", 0444, debug->debugfs_dir,
451 &debug->stop_timeout[RKISP1_MAINPATH]);
452 debugfs_create_ulong("sp_stop_timeout", 0444, debug->debugfs_dir,
453 &debug->stop_timeout[RKISP1_SELFPATH]);
454 debugfs_create_ulong("mp_frame_drop", 0444, debug->debugfs_dir,
455 &debug->frame_drop[RKISP1_MAINPATH]);
456 debugfs_create_ulong("sp_frame_drop", 0444, debug->debugfs_dir,
457 &debug->frame_drop[RKISP1_SELFPATH]);
460 static int rkisp1_probe(struct platform_device *pdev)
462 const struct rkisp1_match_data *clk_data;
463 struct device *dev = &pdev->dev;
464 struct rkisp1_device *rkisp1;
465 struct v4l2_device *v4l2_dev;
469 clk_data = of_device_get_match_data(&pdev->dev);
473 rkisp1 = devm_kzalloc(dev, sizeof(*rkisp1), GFP_KERNEL);
477 dev_set_drvdata(dev, rkisp1);
480 mutex_init(&rkisp1->stream_lock);
482 rkisp1->base_addr = devm_platform_ioremap_resource(pdev, 0);
483 if (IS_ERR(rkisp1->base_addr))
484 return PTR_ERR(rkisp1->base_addr);
486 irq = platform_get_irq(pdev, 0);
490 ret = devm_request_irq(dev, irq, rkisp1_isr, IRQF_SHARED,
491 dev_driver_string(dev), dev);
493 dev_err(dev, "request irq failed: %d\n", ret);
499 for (i = 0; i < clk_data->size; i++)
500 rkisp1->clks[i].id = clk_data->clks[i];
501 ret = devm_clk_bulk_get(dev, clk_data->size, rkisp1->clks);
504 rkisp1->clk_size = clk_data->size;
506 pm_runtime_enable(&pdev->dev);
508 strscpy(rkisp1->media_dev.model, RKISP1_DRIVER_NAME,
509 sizeof(rkisp1->media_dev.model));
510 rkisp1->media_dev.dev = &pdev->dev;
511 strscpy(rkisp1->media_dev.bus_info, RKISP1_BUS_INFO,
512 sizeof(rkisp1->media_dev.bus_info));
513 media_device_init(&rkisp1->media_dev);
515 v4l2_dev = &rkisp1->v4l2_dev;
516 v4l2_dev->mdev = &rkisp1->media_dev;
517 strscpy(v4l2_dev->name, RKISP1_DRIVER_NAME, sizeof(v4l2_dev->name));
519 ret = v4l2_device_register(rkisp1->dev, &rkisp1->v4l2_dev);
521 goto err_pm_runtime_disable;
523 ret = media_device_register(&rkisp1->media_dev);
525 dev_err(dev, "Failed to register media device: %d\n", ret);
526 goto err_unreg_v4l2_dev;
529 ret = rkisp1_entities_register(rkisp1);
531 goto err_unreg_media_dev;
533 rkisp1_debug_init(rkisp1);
538 media_device_unregister(&rkisp1->media_dev);
540 v4l2_device_unregister(&rkisp1->v4l2_dev);
541 err_pm_runtime_disable:
542 pm_runtime_disable(&pdev->dev);
546 static int rkisp1_remove(struct platform_device *pdev)
548 struct rkisp1_device *rkisp1 = platform_get_drvdata(pdev);
550 v4l2_async_notifier_unregister(&rkisp1->notifier);
551 v4l2_async_notifier_cleanup(&rkisp1->notifier);
553 rkisp1_params_unregister(rkisp1);
554 rkisp1_stats_unregister(rkisp1);
555 rkisp1_capture_devs_unregister(rkisp1);
556 rkisp1_resizer_devs_unregister(rkisp1);
557 rkisp1_isp_unregister(rkisp1);
559 media_device_unregister(&rkisp1->media_dev);
560 v4l2_device_unregister(&rkisp1->v4l2_dev);
562 pm_runtime_disable(&pdev->dev);
564 debugfs_remove_recursive(rkisp1->debug.debugfs_dir);
568 static struct platform_driver rkisp1_drv = {
570 .name = RKISP1_DRIVER_NAME,
571 .of_match_table = of_match_ptr(rkisp1_of_match),
572 .pm = &rkisp1_pm_ops,
574 .probe = rkisp1_probe,
575 .remove = rkisp1_remove,
578 module_platform_driver(rkisp1_drv);
579 MODULE_DESCRIPTION("Rockchip ISP1 platform driver");
580 MODULE_LICENSE("Dual MIT/GPL");