1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT) 2 /* 3 * Rockchip ISP1 Driver - Base driver 4 * 5 * Copyright (C) 2019 Collabora, Ltd. 6 * 7 * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd. 8 * Copyright (C) 2017 Rockchip Electronics Co., Ltd. 9 */ 10 11 #include <linux/clk.h> 12 #include <linux/debugfs.h> 13 #include <linux/interrupt.h> 14 #include <linux/module.h> 15 #include <linux/of.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> 22 23 #include "rkisp1-common.h" 24 25 /* 26 * ISP Details 27 * ----------- 28 * 29 * ISP Comprises with: 30 * MIPI serial camera interface 31 * Image Signal Processing 32 * Many Image Enhancement Blocks 33 * Crop 34 * Resizer 35 * RBG display ready image 36 * Image Rotation 37 * 38 * ISP Block Diagram 39 * ----------------- 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 * +-----------+ +--+--+--+--+ +--------+ +--------+ +--------+ +-----------+ 55 * ^ 56 * +--------+ | |===============================================| 57 * | DMA |------------------------------------+ Self Picture Path 58 * +--------+ 59 * 60 * rkisp1-stats.c rkisp1-params.c 61 * |===============| |===============| 62 * +---------------+ +---------------+ 63 * | | | | 64 * | ISP | | ISP | 65 * | | | | 66 * +---------------+ +---------------+ 67 * 68 * 69 * Media Topology 70 * -------------- 71 * +----------+ +----------+ 72 * | Sensor 2 | | Sensor X | 73 * ------------ ... ------------ 74 * | 0 | | 0 | 75 * +----------+ +----------+ +-----------+ 76 * \ | | params | 77 * \ | | (output) | 78 * +----------+ \ | +-----------+ 79 * | Sensor 1 | v v | 80 * ------------ +------+------+ | 81 * | 0 |----->| 0 | 1 |<---------+ 82 * +----------+ |------+------| 83 * | ISP | 84 * |------+------| 85 * +-------------| 2 | 3 |----------+ 86 * | +------+------+ | 87 * | | | 88 * v v v 89 * +- ---------+ +-----------+ +-----------+ 90 * | 0 | | 0 | | stats | 91 * ------------- ------------- | (capture) | 92 * | Resizer | | Resizer | +-----------+ 93 * ------------| ------------| 94 * | 1 | | 1 | 95 * +-----------+ +-----------+ 96 * | | 97 * v v 98 * +-----------+ +-----------+ 99 * | selfpath | | mainpath | 100 * | (capture) | | (capture) | 101 * +-----------+ +-----------+ 102 */ 103 104 struct rkisp1_match_data { 105 const char * const *clks; 106 unsigned int size; 107 enum rkisp1_cif_isp_version isp_ver; 108 }; 109 110 /* ---------------------------------------------------------------------------- 111 * Sensor DT bindings 112 */ 113 114 static int rkisp1_create_links(struct rkisp1_device *rkisp1) 115 { 116 struct media_entity *source, *sink; 117 unsigned int flags, source_pad; 118 struct v4l2_subdev *sd; 119 unsigned int i; 120 int ret; 121 122 /* sensor links */ 123 flags = MEDIA_LNK_FL_ENABLED; 124 list_for_each_entry(sd, &rkisp1->v4l2_dev.subdevs, list) { 125 if (sd == &rkisp1->isp.sd || 126 sd == &rkisp1->resizer_devs[RKISP1_MAINPATH].sd || 127 sd == &rkisp1->resizer_devs[RKISP1_SELFPATH].sd) 128 continue; 129 130 ret = media_entity_get_fwnode_pad(&sd->entity, sd->fwnode, 131 MEDIA_PAD_FL_SOURCE); 132 if (ret < 0) { 133 dev_err(rkisp1->dev, "failed to find src pad for %s\n", 134 sd->name); 135 return ret; 136 } 137 source_pad = ret; 138 139 ret = media_create_pad_link(&sd->entity, source_pad, 140 &rkisp1->isp.sd.entity, 141 RKISP1_ISP_PAD_SINK_VIDEO, 142 flags); 143 if (ret) 144 return ret; 145 146 flags = 0; 147 } 148 149 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE; 150 151 /* create ISP->RSZ->CAP links */ 152 for (i = 0; i < 2; i++) { 153 source = &rkisp1->isp.sd.entity; 154 sink = &rkisp1->resizer_devs[i].sd.entity; 155 ret = media_create_pad_link(source, RKISP1_ISP_PAD_SOURCE_VIDEO, 156 sink, RKISP1_RSZ_PAD_SINK, 157 MEDIA_LNK_FL_ENABLED); 158 if (ret) 159 return ret; 160 161 source = sink; 162 sink = &rkisp1->capture_devs[i].vnode.vdev.entity; 163 ret = media_create_pad_link(source, RKISP1_RSZ_PAD_SRC, 164 sink, 0, flags); 165 if (ret) 166 return ret; 167 } 168 169 /* params links */ 170 source = &rkisp1->params.vnode.vdev.entity; 171 sink = &rkisp1->isp.sd.entity; 172 ret = media_create_pad_link(source, 0, sink, 173 RKISP1_ISP_PAD_SINK_PARAMS, flags); 174 if (ret) 175 return ret; 176 177 /* 3A stats links */ 178 source = &rkisp1->isp.sd.entity; 179 sink = &rkisp1->stats.vnode.vdev.entity; 180 return media_create_pad_link(source, RKISP1_ISP_PAD_SOURCE_STATS, 181 sink, 0, flags); 182 } 183 184 static int rkisp1_subdev_notifier_bound(struct v4l2_async_notifier *notifier, 185 struct v4l2_subdev *sd, 186 struct v4l2_async_subdev *asd) 187 { 188 struct rkisp1_device *rkisp1 = 189 container_of(notifier, struct rkisp1_device, notifier); 190 struct rkisp1_sensor_async *s_asd = 191 container_of(asd, struct rkisp1_sensor_async, asd); 192 193 s_asd->pixel_rate_ctrl = v4l2_ctrl_find(sd->ctrl_handler, 194 V4L2_CID_PIXEL_RATE); 195 s_asd->sd = sd; 196 s_asd->dphy = devm_phy_get(rkisp1->dev, "dphy"); 197 if (IS_ERR(s_asd->dphy)) { 198 if (PTR_ERR(s_asd->dphy) != -EPROBE_DEFER) 199 dev_err(rkisp1->dev, "Couldn't get the MIPI D-PHY\n"); 200 return PTR_ERR(s_asd->dphy); 201 } 202 203 phy_init(s_asd->dphy); 204 205 return 0; 206 } 207 208 static void rkisp1_subdev_notifier_unbind(struct v4l2_async_notifier *notifier, 209 struct v4l2_subdev *sd, 210 struct v4l2_async_subdev *asd) 211 { 212 struct rkisp1_sensor_async *s_asd = 213 container_of(asd, struct rkisp1_sensor_async, asd); 214 215 phy_exit(s_asd->dphy); 216 } 217 218 static int rkisp1_subdev_notifier_complete(struct v4l2_async_notifier *notifier) 219 { 220 struct rkisp1_device *rkisp1 = 221 container_of(notifier, struct rkisp1_device, notifier); 222 int ret; 223 224 ret = rkisp1_create_links(rkisp1); 225 if (ret) 226 return ret; 227 228 ret = v4l2_device_register_subdev_nodes(&rkisp1->v4l2_dev); 229 if (ret) 230 return ret; 231 232 dev_dbg(rkisp1->dev, "Async subdev notifier completed\n"); 233 234 return 0; 235 } 236 237 static const struct v4l2_async_notifier_operations rkisp1_subdev_notifier_ops = { 238 .bound = rkisp1_subdev_notifier_bound, 239 .unbind = rkisp1_subdev_notifier_unbind, 240 .complete = rkisp1_subdev_notifier_complete, 241 }; 242 243 static int rkisp1_subdev_notifier(struct rkisp1_device *rkisp1) 244 { 245 struct v4l2_async_notifier *ntf = &rkisp1->notifier; 246 unsigned int next_id = 0; 247 int ret; 248 249 v4l2_async_nf_init(ntf); 250 251 while (1) { 252 struct v4l2_fwnode_endpoint vep = { 253 .bus_type = V4L2_MBUS_CSI2_DPHY 254 }; 255 struct rkisp1_sensor_async *rk_asd; 256 struct fwnode_handle *ep; 257 258 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(rkisp1->dev), 259 0, next_id, 260 FWNODE_GRAPH_ENDPOINT_NEXT); 261 if (!ep) 262 break; 263 264 ret = v4l2_fwnode_endpoint_parse(ep, &vep); 265 if (ret) 266 goto err_parse; 267 268 rk_asd = v4l2_async_nf_add_fwnode_remote(ntf, ep, 269 struct 270 rkisp1_sensor_async); 271 if (IS_ERR(rk_asd)) { 272 ret = PTR_ERR(rk_asd); 273 goto err_parse; 274 } 275 276 rk_asd->mbus_type = vep.bus_type; 277 rk_asd->mbus_flags = vep.bus.mipi_csi2.flags; 278 rk_asd->lanes = vep.bus.mipi_csi2.num_data_lanes; 279 280 dev_dbg(rkisp1->dev, "registered ep id %d with %d lanes\n", 281 vep.base.id, rk_asd->lanes); 282 283 next_id = vep.base.id + 1; 284 285 fwnode_handle_put(ep); 286 287 continue; 288 err_parse: 289 fwnode_handle_put(ep); 290 v4l2_async_nf_cleanup(ntf); 291 return ret; 292 } 293 294 if (next_id == 0) 295 dev_dbg(rkisp1->dev, "no remote subdevice found\n"); 296 ntf->ops = &rkisp1_subdev_notifier_ops; 297 ret = v4l2_async_nf_register(&rkisp1->v4l2_dev, ntf); 298 if (ret) { 299 v4l2_async_nf_cleanup(ntf); 300 return ret; 301 } 302 return 0; 303 } 304 305 /* ---------------------------------------------------------------------------- 306 * Power 307 */ 308 309 static int __maybe_unused rkisp1_runtime_suspend(struct device *dev) 310 { 311 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); 312 313 clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks); 314 return pinctrl_pm_select_sleep_state(dev); 315 } 316 317 static int __maybe_unused rkisp1_runtime_resume(struct device *dev) 318 { 319 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); 320 int ret; 321 322 ret = pinctrl_pm_select_default_state(dev); 323 if (ret) 324 return ret; 325 ret = clk_bulk_prepare_enable(rkisp1->clk_size, rkisp1->clks); 326 if (ret) 327 return ret; 328 329 return 0; 330 } 331 332 static const struct dev_pm_ops rkisp1_pm_ops = { 333 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 334 pm_runtime_force_resume) 335 SET_RUNTIME_PM_OPS(rkisp1_runtime_suspend, rkisp1_runtime_resume, NULL) 336 }; 337 338 /* ---------------------------------------------------------------------------- 339 * Core 340 */ 341 342 static int rkisp1_entities_register(struct rkisp1_device *rkisp1) 343 { 344 int ret; 345 346 ret = rkisp1_isp_register(rkisp1); 347 if (ret) 348 return ret; 349 350 ret = rkisp1_resizer_devs_register(rkisp1); 351 if (ret) 352 goto err_unreg_isp_subdev; 353 354 ret = rkisp1_capture_devs_register(rkisp1); 355 if (ret) 356 goto err_unreg_resizer_devs; 357 358 ret = rkisp1_stats_register(rkisp1); 359 if (ret) 360 goto err_unreg_capture_devs; 361 362 ret = rkisp1_params_register(rkisp1); 363 if (ret) 364 goto err_unreg_stats; 365 366 ret = rkisp1_subdev_notifier(rkisp1); 367 if (ret) { 368 dev_err(rkisp1->dev, 369 "Failed to register subdev notifier(%d)\n", ret); 370 goto err_unreg_params; 371 } 372 373 return 0; 374 err_unreg_params: 375 rkisp1_params_unregister(rkisp1); 376 err_unreg_stats: 377 rkisp1_stats_unregister(rkisp1); 378 err_unreg_capture_devs: 379 rkisp1_capture_devs_unregister(rkisp1); 380 err_unreg_resizer_devs: 381 rkisp1_resizer_devs_unregister(rkisp1); 382 err_unreg_isp_subdev: 383 rkisp1_isp_unregister(rkisp1); 384 return ret; 385 } 386 387 static irqreturn_t rkisp1_isr(int irq, void *ctx) 388 { 389 struct device *dev = ctx; 390 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); 391 392 /* 393 * Call rkisp1_capture_isr() first to handle the frame that 394 * potentially completed using the current frame_sequence number before 395 * it is potentially incremented by rkisp1_isp_isr() in the vertical 396 * sync. 397 */ 398 rkisp1_capture_isr(rkisp1); 399 rkisp1_isp_isr(rkisp1); 400 rkisp1_mipi_isr(rkisp1); 401 402 return IRQ_HANDLED; 403 } 404 405 static const char * const rk3399_isp_clks[] = { 406 "isp", 407 "aclk", 408 "hclk", 409 }; 410 411 static const struct rkisp1_match_data rk3399_isp_match_data = { 412 .clks = rk3399_isp_clks, 413 .size = ARRAY_SIZE(rk3399_isp_clks), 414 .isp_ver = RKISP1_V10, 415 }; 416 417 static const struct of_device_id rkisp1_of_match[] = { 418 { 419 .compatible = "rockchip,rk3399-cif-isp", 420 .data = &rk3399_isp_match_data, 421 }, 422 {}, 423 }; 424 MODULE_DEVICE_TABLE(of, rkisp1_of_match); 425 426 static void rkisp1_debug_init(struct rkisp1_device *rkisp1) 427 { 428 struct rkisp1_debug *debug = &rkisp1->debug; 429 430 debug->debugfs_dir = debugfs_create_dir(RKISP1_DRIVER_NAME, NULL); 431 debugfs_create_ulong("data_loss", 0444, debug->debugfs_dir, 432 &debug->data_loss); 433 debugfs_create_ulong("outform_size_err", 0444, debug->debugfs_dir, 434 &debug->outform_size_error); 435 debugfs_create_ulong("img_stabilization_size_error", 0444, 436 debug->debugfs_dir, 437 &debug->img_stabilization_size_error); 438 debugfs_create_ulong("inform_size_error", 0444, debug->debugfs_dir, 439 &debug->inform_size_error); 440 debugfs_create_ulong("irq_delay", 0444, debug->debugfs_dir, 441 &debug->irq_delay); 442 debugfs_create_ulong("mipi_error", 0444, debug->debugfs_dir, 443 &debug->mipi_error); 444 debugfs_create_ulong("stats_error", 0444, debug->debugfs_dir, 445 &debug->stats_error); 446 debugfs_create_ulong("mp_stop_timeout", 0444, debug->debugfs_dir, 447 &debug->stop_timeout[RKISP1_MAINPATH]); 448 debugfs_create_ulong("sp_stop_timeout", 0444, debug->debugfs_dir, 449 &debug->stop_timeout[RKISP1_SELFPATH]); 450 debugfs_create_ulong("mp_frame_drop", 0444, debug->debugfs_dir, 451 &debug->frame_drop[RKISP1_MAINPATH]); 452 debugfs_create_ulong("sp_frame_drop", 0444, debug->debugfs_dir, 453 &debug->frame_drop[RKISP1_SELFPATH]); 454 } 455 456 static int rkisp1_probe(struct platform_device *pdev) 457 { 458 const struct rkisp1_match_data *match_data; 459 struct device *dev = &pdev->dev; 460 struct rkisp1_device *rkisp1; 461 struct v4l2_device *v4l2_dev; 462 unsigned int i; 463 int ret, irq; 464 465 match_data = of_device_get_match_data(&pdev->dev); 466 if (!match_data) 467 return -ENODEV; 468 469 rkisp1 = devm_kzalloc(dev, sizeof(*rkisp1), GFP_KERNEL); 470 if (!rkisp1) 471 return -ENOMEM; 472 473 dev_set_drvdata(dev, rkisp1); 474 rkisp1->dev = dev; 475 476 mutex_init(&rkisp1->stream_lock); 477 478 rkisp1->base_addr = devm_platform_ioremap_resource(pdev, 0); 479 if (IS_ERR(rkisp1->base_addr)) 480 return PTR_ERR(rkisp1->base_addr); 481 482 irq = platform_get_irq(pdev, 0); 483 if (irq < 0) 484 return irq; 485 486 ret = devm_request_irq(dev, irq, rkisp1_isr, IRQF_SHARED, 487 dev_driver_string(dev), dev); 488 if (ret) { 489 dev_err(dev, "request irq failed: %d\n", ret); 490 return ret; 491 } 492 493 rkisp1->irq = irq; 494 495 for (i = 0; i < match_data->size; i++) 496 rkisp1->clks[i].id = match_data->clks[i]; 497 ret = devm_clk_bulk_get(dev, match_data->size, rkisp1->clks); 498 if (ret) 499 return ret; 500 rkisp1->clk_size = match_data->size; 501 502 pm_runtime_enable(&pdev->dev); 503 504 rkisp1->media_dev.hw_revision = match_data->isp_ver; 505 strscpy(rkisp1->media_dev.model, RKISP1_DRIVER_NAME, 506 sizeof(rkisp1->media_dev.model)); 507 rkisp1->media_dev.dev = &pdev->dev; 508 strscpy(rkisp1->media_dev.bus_info, RKISP1_BUS_INFO, 509 sizeof(rkisp1->media_dev.bus_info)); 510 media_device_init(&rkisp1->media_dev); 511 512 v4l2_dev = &rkisp1->v4l2_dev; 513 v4l2_dev->mdev = &rkisp1->media_dev; 514 strscpy(v4l2_dev->name, RKISP1_DRIVER_NAME, sizeof(v4l2_dev->name)); 515 516 ret = v4l2_device_register(rkisp1->dev, &rkisp1->v4l2_dev); 517 if (ret) 518 return ret; 519 520 ret = media_device_register(&rkisp1->media_dev); 521 if (ret) { 522 dev_err(dev, "Failed to register media device: %d\n", ret); 523 goto err_unreg_v4l2_dev; 524 } 525 526 ret = rkisp1_entities_register(rkisp1); 527 if (ret) 528 goto err_unreg_media_dev; 529 530 rkisp1_debug_init(rkisp1); 531 532 return 0; 533 534 err_unreg_media_dev: 535 media_device_unregister(&rkisp1->media_dev); 536 err_unreg_v4l2_dev: 537 v4l2_device_unregister(&rkisp1->v4l2_dev); 538 pm_runtime_disable(&pdev->dev); 539 return ret; 540 } 541 542 static int rkisp1_remove(struct platform_device *pdev) 543 { 544 struct rkisp1_device *rkisp1 = platform_get_drvdata(pdev); 545 546 v4l2_async_nf_unregister(&rkisp1->notifier); 547 v4l2_async_nf_cleanup(&rkisp1->notifier); 548 549 rkisp1_params_unregister(rkisp1); 550 rkisp1_stats_unregister(rkisp1); 551 rkisp1_capture_devs_unregister(rkisp1); 552 rkisp1_resizer_devs_unregister(rkisp1); 553 rkisp1_isp_unregister(rkisp1); 554 555 media_device_unregister(&rkisp1->media_dev); 556 v4l2_device_unregister(&rkisp1->v4l2_dev); 557 558 pm_runtime_disable(&pdev->dev); 559 560 debugfs_remove_recursive(rkisp1->debug.debugfs_dir); 561 return 0; 562 } 563 564 static struct platform_driver rkisp1_drv = { 565 .driver = { 566 .name = RKISP1_DRIVER_NAME, 567 .of_match_table = of_match_ptr(rkisp1_of_match), 568 .pm = &rkisp1_pm_ops, 569 }, 570 .probe = rkisp1_probe, 571 .remove = rkisp1_remove, 572 }; 573 574 module_platform_driver(rkisp1_drv); 575 MODULE_DESCRIPTION("Rockchip ISP1 platform driver"); 576 MODULE_LICENSE("Dual MIT/GPL"); 577