Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | // SPDX-License-Identifier: (GPL-2.0+ OR MIT) /* * Rockchip ISP1 Driver - Base driver * * Copyright (C) 2019 Collabora, Ltd. * * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd. * Copyright (C) 2017 Rockchip Electronics Co., Ltd. */ #include <linux/clk.h> #include <linux/interrupt.h> #include <linux/module.h> #include <linux/of.h> #include <linux/of_graph.h> #include <linux/platform_device.h> #include <linux/pinctrl/consumer.h> #include <linux/pm_runtime.h> #include <media/v4l2-fwnode.h> #include <media/v4l2-mc.h> #include "rkisp1-common.h" #include "rkisp1-csi.h" /* * ISP Details * ----------- * * ISP Comprises with: * MIPI serial camera interface * Image Signal Processing * Many Image Enhancement Blocks * Crop * Resizer * RBG display ready image * Image Rotation * * ISP Block Diagram * ----------------- * rkisp1-resizer.c rkisp1-capture.c * |====================| |=======================| * rkisp1-isp.c Main Picture Path * |==========================| |===============================================| * +-----------+ +--+--+--+--+ +--------+ +--------+ +-----------+ * | | | | | | | | | | | | | * +--------+ |\ | | | | | | | -->| Crop |->| RSZ |------------->| | * | MIPI |--->| \ | | | | | | | | | | | | | | * +--------+ | | | | |IE|IE|IE|IE| | +--------+ +--------+ | Memory | * |MUX|--->| ISP |->|0 |1 |2 |3 |---+ | Interface | * +--------+ | | | | | | | | | | +--------+ +--------+ +--------+ | | * |Parallel|--->| / | | | | | | | | | | | | | | | | * +--------+ |/ | | | | | | | -->| Crop |->| RSZ |->| RGB |->| | * | | | | | | | | | | | | Rotate | | | * +-----------+ +--+--+--+--+ +--------+ +--------+ +--------+ +-----------+ * ^ * +--------+ | |===============================================| * | DMA |------------------------------------+ Self Picture Path * +--------+ * * rkisp1-stats.c rkisp1-params.c * |===============| |===============| * +---------------+ +---------------+ * | | | | * | ISP | | ISP | * | | | | * +---------------+ +---------------+ * * * Media Topology * -------------- * * +----------+ +----------+ * | Sensor 1 | | Sensor X | * ------------ ... ------------ * | 0 | | 0 | * +----------+ +----------+ * | | * \----\ /----/ * | | * v v * +-------------+ * | 0 | * --------------- * | CSI-2 RX | * --------------- +-----------+ * | 1 | | params | * +-------------+ | (output) | * | +-----------+ * v | * +------+------+ | * | 0 | 1 |<---------+ * |------+------| * | ISP | * |------+------| * +-------------| 2 | 3 |----------+ * | +------+------+ | * | | | * v v v * +- ---------+ +-----------+ +-----------+ * | 0 | | 0 | | stats | * ------------- ------------- | (capture) | * | Resizer | | Resizer | +-----------+ * ------------| ------------| * | 1 | | 1 | * +-----------+ +-----------+ * | | * v v * +-----------+ +-----------+ * | selfpath | | mainpath | * | (capture) | | (capture) | * +-----------+ +-----------+ */ struct rkisp1_isr_data { const char *name; irqreturn_t (*isr)(int irq, void *ctx); }; /* ---------------------------------------------------------------------------- * Sensor DT bindings */ static int rkisp1_subdev_notifier_bound(struct v4l2_async_notifier *notifier, struct v4l2_subdev *sd, struct v4l2_async_connection *asc) { struct rkisp1_device *rkisp1 = container_of(notifier, struct rkisp1_device, notifier); struct rkisp1_sensor_async *s_asd = container_of(asc, struct rkisp1_sensor_async, asd); int source_pad; int ret; s_asd->sd = sd; source_pad = media_entity_get_fwnode_pad(&sd->entity, s_asd->source_ep, MEDIA_PAD_FL_SOURCE); if (source_pad < 0) { dev_err(rkisp1->dev, "failed to find source pad for %s\n", sd->name); return source_pad; } if (s_asd->port == 0) return rkisp1_csi_link_sensor(rkisp1, sd, s_asd, source_pad); ret = media_create_pad_link(&sd->entity, source_pad, &rkisp1->isp.sd.entity, RKISP1_ISP_PAD_SINK_VIDEO, !s_asd->index ? MEDIA_LNK_FL_ENABLED : 0); if (ret) { dev_err(rkisp1->dev, "failed to link source pad of %s\n", sd->name); return ret; } return 0; } static int rkisp1_subdev_notifier_complete(struct v4l2_async_notifier *notifier) { struct rkisp1_device *rkisp1 = container_of(notifier, struct rkisp1_device, notifier); return v4l2_device_register_subdev_nodes(&rkisp1->v4l2_dev); } static void rkisp1_subdev_notifier_destroy(struct v4l2_async_connection *asc) { struct rkisp1_sensor_async *rk_asd = container_of(asc, struct rkisp1_sensor_async, asd); fwnode_handle_put(rk_asd->source_ep); } static const struct v4l2_async_notifier_operations rkisp1_subdev_notifier_ops = { .bound = rkisp1_subdev_notifier_bound, .complete = rkisp1_subdev_notifier_complete, .destroy = rkisp1_subdev_notifier_destroy, }; static int rkisp1_subdev_notifier_register(struct rkisp1_device *rkisp1) { struct v4l2_async_notifier *ntf = &rkisp1->notifier; struct fwnode_handle *fwnode = dev_fwnode(rkisp1->dev); struct fwnode_handle *ep; unsigned int index = 0; int ret = 0; v4l2_async_nf_init(ntf, &rkisp1->v4l2_dev); ntf->ops = &rkisp1_subdev_notifier_ops; fwnode_graph_for_each_endpoint(fwnode, ep) { struct fwnode_handle *port; struct v4l2_fwnode_endpoint vep = { }; struct rkisp1_sensor_async *rk_asd; struct fwnode_handle *source; u32 reg = 0; /* Select the bus type based on the port. */ port = fwnode_get_parent(ep); fwnode_property_read_u32(port, "reg", ®); fwnode_handle_put(port); switch (reg) { case 0: /* MIPI CSI-2 port */ if (!(rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2)) { dev_err(rkisp1->dev, "internal CSI must be available for port 0\n"); ret = -EINVAL; break; } vep.bus_type = V4L2_MBUS_CSI2_DPHY; break; case 1: /* * Parallel port. The bus-type property in DT is * mandatory for port 1, it will be used to determine if * it's PARALLEL or BT656. */ vep.bus_type = V4L2_MBUS_UNKNOWN; break; } /* Parse the endpoint and validate the bus type. */ ret = v4l2_fwnode_endpoint_parse(ep, &vep); if (ret) { dev_err(rkisp1->dev, "failed to parse endpoint %pfw\n", ep); break; } if (vep.base.port == 1) { if (vep.bus_type != V4L2_MBUS_PARALLEL && vep.bus_type != V4L2_MBUS_BT656) { dev_err(rkisp1->dev, "port 1 must be parallel or BT656\n"); ret = -EINVAL; break; } } /* Add the async subdev to the notifier. */ source = fwnode_graph_get_remote_endpoint(ep); if (!source) { dev_err(rkisp1->dev, "endpoint %pfw has no remote endpoint\n", ep); ret = -ENODEV; break; } rk_asd = v4l2_async_nf_add_fwnode(ntf, source, struct rkisp1_sensor_async); if (IS_ERR(rk_asd)) { fwnode_handle_put(source); ret = PTR_ERR(rk_asd); break; } rk_asd->index = index++; rk_asd->source_ep = source; rk_asd->mbus_type = vep.bus_type; rk_asd->port = vep.base.port; if (vep.bus_type == V4L2_MBUS_CSI2_DPHY) { rk_asd->mbus_flags = vep.bus.mipi_csi2.flags; rk_asd->lanes = vep.bus.mipi_csi2.num_data_lanes; } else { rk_asd->mbus_flags = vep.bus.parallel.flags; } dev_dbg(rkisp1->dev, "registered ep id %d, bus type %u, %u lanes\n", vep.base.id, rk_asd->mbus_type, rk_asd->lanes); } if (ret) { fwnode_handle_put(ep); v4l2_async_nf_cleanup(ntf); return ret; } if (!index) dev_dbg(rkisp1->dev, "no remote subdevice found\n"); ret = v4l2_async_nf_register(ntf); if (ret) { v4l2_async_nf_cleanup(ntf); return ret; } return 0; } /* ---------------------------------------------------------------------------- * Power */ static int __maybe_unused rkisp1_runtime_suspend(struct device *dev) { struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks); return pinctrl_pm_select_sleep_state(dev); } static int __maybe_unused rkisp1_runtime_resume(struct device *dev) { struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); int ret; ret = pinctrl_pm_select_default_state(dev); if (ret) return ret; ret = clk_bulk_prepare_enable(rkisp1->clk_size, rkisp1->clks); if (ret) return ret; return 0; } static const struct dev_pm_ops rkisp1_pm_ops = { SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) SET_RUNTIME_PM_OPS(rkisp1_runtime_suspend, rkisp1_runtime_resume, NULL) }; /* ---------------------------------------------------------------------------- * Core */ static int rkisp1_create_links(struct rkisp1_device *rkisp1) { unsigned int i; int ret; if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) { /* Link the CSI receiver to the ISP. */ ret = media_create_pad_link(&rkisp1->csi.sd.entity, RKISP1_CSI_PAD_SRC, &rkisp1->isp.sd.entity, RKISP1_ISP_PAD_SINK_VIDEO, MEDIA_LNK_FL_ENABLED); if (ret) return ret; } /* create ISP->RSZ->CAP links */ for (i = 0; i < 2; i++) { struct media_entity *resizer = &rkisp1->resizer_devs[i].sd.entity; struct media_entity *capture = &rkisp1->capture_devs[i].vnode.vdev.entity; ret = media_create_pad_link(&rkisp1->isp.sd.entity, RKISP1_ISP_PAD_SOURCE_VIDEO, resizer, RKISP1_RSZ_PAD_SINK, MEDIA_LNK_FL_ENABLED); if (ret) return ret; ret = media_create_pad_link(resizer, RKISP1_RSZ_PAD_SRC, capture, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE); if (ret) return ret; } /* params links */ ret = media_create_pad_link(&rkisp1->params.vnode.vdev.entity, 0, &rkisp1->isp.sd.entity, RKISP1_ISP_PAD_SINK_PARAMS, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE); if (ret) return ret; /* 3A stats links */ return media_create_pad_link(&rkisp1->isp.sd.entity, RKISP1_ISP_PAD_SOURCE_STATS, &rkisp1->stats.vnode.vdev.entity, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE); } static void rkisp1_entities_unregister(struct rkisp1_device *rkisp1) { if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) rkisp1_csi_unregister(rkisp1); rkisp1_params_unregister(rkisp1); rkisp1_stats_unregister(rkisp1); rkisp1_capture_devs_unregister(rkisp1); rkisp1_resizer_devs_unregister(rkisp1); rkisp1_isp_unregister(rkisp1); } static int rkisp1_entities_register(struct rkisp1_device *rkisp1) { int ret; ret = rkisp1_isp_register(rkisp1); if (ret) goto error; ret = rkisp1_resizer_devs_register(rkisp1); if (ret) goto error; ret = rkisp1_capture_devs_register(rkisp1); if (ret) goto error; ret = rkisp1_stats_register(rkisp1); if (ret) goto error; ret = rkisp1_params_register(rkisp1); if (ret) goto error; if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) { ret = rkisp1_csi_register(rkisp1); if (ret) goto error; } ret = rkisp1_create_links(rkisp1); if (ret) goto error; return 0; error: rkisp1_entities_unregister(rkisp1); return ret; } static irqreturn_t rkisp1_isr(int irq, void *ctx) { /* * Call rkisp1_capture_isr() first to handle the frame that * potentially completed using the current frame_sequence number before * it is potentially incremented by rkisp1_isp_isr() in the vertical * sync. */ rkisp1_capture_isr(irq, ctx); rkisp1_isp_isr(irq, ctx); rkisp1_csi_isr(irq, ctx); return IRQ_HANDLED; } static const char * const px30_isp_clks[] = { "isp", "aclk", "hclk", "pclk", }; static const struct rkisp1_isr_data px30_isp_isrs[] = { { "isp", rkisp1_isp_isr }, { "mi", rkisp1_capture_isr }, { "mipi", rkisp1_csi_isr }, }; static const struct rkisp1_info px30_isp_info = { .clks = px30_isp_clks, .clk_size = ARRAY_SIZE(px30_isp_clks), .isrs = px30_isp_isrs, .isr_size = ARRAY_SIZE(px30_isp_isrs), .isp_ver = RKISP1_V12, .features = RKISP1_FEATURE_MIPI_CSI2, }; static const char * const rk3399_isp_clks[] = { "isp", "aclk", "hclk", }; static const struct rkisp1_isr_data rk3399_isp_isrs[] = { { NULL, rkisp1_isr }, }; static const struct rkisp1_info rk3399_isp_info = { .clks = rk3399_isp_clks, .clk_size = ARRAY_SIZE(rk3399_isp_clks), .isrs = rk3399_isp_isrs, .isr_size = ARRAY_SIZE(rk3399_isp_isrs), .isp_ver = RKISP1_V10, .features = RKISP1_FEATURE_MIPI_CSI2, }; static const struct of_device_id rkisp1_of_match[] = { { .compatible = "rockchip,px30-cif-isp", .data = &px30_isp_info, }, { .compatible = "rockchip,rk3399-cif-isp", .data = &rk3399_isp_info, }, {}, }; MODULE_DEVICE_TABLE(of, rkisp1_of_match); static int rkisp1_probe(struct platform_device *pdev) { const struct rkisp1_info *info; struct device *dev = &pdev->dev; struct rkisp1_device *rkisp1; struct v4l2_device *v4l2_dev; unsigned int i; int ret, irq; u32 cif_id; rkisp1 = devm_kzalloc(dev, sizeof(*rkisp1), GFP_KERNEL); if (!rkisp1) return -ENOMEM; info = of_device_get_match_data(dev); rkisp1->info = info; dev_set_drvdata(dev, rkisp1); rkisp1->dev = dev; mutex_init(&rkisp1->stream_lock); rkisp1->base_addr = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(rkisp1->base_addr)) return PTR_ERR(rkisp1->base_addr); for (i = 0; i < info->isr_size; i++) { irq = info->isrs[i].name ? platform_get_irq_byname(pdev, info->isrs[i].name) : platform_get_irq(pdev, i); if (irq < 0) return irq; ret = devm_request_irq(dev, irq, info->isrs[i].isr, IRQF_SHARED, dev_driver_string(dev), dev); if (ret) { dev_err(dev, "request irq failed: %d\n", ret); return ret; } } for (i = 0; i < info->clk_size; i++) rkisp1->clks[i].id = info->clks[i]; ret = devm_clk_bulk_get(dev, info->clk_size, rkisp1->clks); if (ret) return ret; rkisp1->clk_size = info->clk_size; pm_runtime_enable(&pdev->dev); ret = pm_runtime_resume_and_get(&pdev->dev); if (ret) goto err_pm_runtime_disable; cif_id = rkisp1_read(rkisp1, RKISP1_CIF_VI_ID); dev_dbg(rkisp1->dev, "CIF_ID 0x%08x\n", cif_id); pm_runtime_put(&pdev->dev); rkisp1->media_dev.hw_revision = info->isp_ver; strscpy(rkisp1->media_dev.model, RKISP1_DRIVER_NAME, sizeof(rkisp1->media_dev.model)); rkisp1->media_dev.dev = &pdev->dev; strscpy(rkisp1->media_dev.bus_info, RKISP1_BUS_INFO, sizeof(rkisp1->media_dev.bus_info)); media_device_init(&rkisp1->media_dev); v4l2_dev = &rkisp1->v4l2_dev; v4l2_dev->mdev = &rkisp1->media_dev; strscpy(v4l2_dev->name, RKISP1_DRIVER_NAME, sizeof(v4l2_dev->name)); ret = v4l2_device_register(rkisp1->dev, &rkisp1->v4l2_dev); if (ret) goto err_pm_runtime_disable; ret = media_device_register(&rkisp1->media_dev); if (ret) { dev_err(dev, "Failed to register media device: %d\n", ret); goto err_unreg_v4l2_dev; } if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) { ret = rkisp1_csi_init(rkisp1); if (ret) goto err_unreg_media_dev; } ret = rkisp1_entities_register(rkisp1); if (ret) goto err_cleanup_csi; ret = rkisp1_subdev_notifier_register(rkisp1); if (ret) goto err_unreg_entities; rkisp1_debug_init(rkisp1); return 0; err_unreg_entities: rkisp1_entities_unregister(rkisp1); err_cleanup_csi: if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) rkisp1_csi_cleanup(rkisp1); err_unreg_media_dev: media_device_unregister(&rkisp1->media_dev); err_unreg_v4l2_dev: v4l2_device_unregister(&rkisp1->v4l2_dev); err_pm_runtime_disable: pm_runtime_disable(&pdev->dev); return ret; } static void rkisp1_remove(struct platform_device *pdev) { struct rkisp1_device *rkisp1 = platform_get_drvdata(pdev); v4l2_async_nf_unregister(&rkisp1->notifier); v4l2_async_nf_cleanup(&rkisp1->notifier); rkisp1_entities_unregister(rkisp1); if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) rkisp1_csi_cleanup(rkisp1); rkisp1_debug_cleanup(rkisp1); media_device_unregister(&rkisp1->media_dev); v4l2_device_unregister(&rkisp1->v4l2_dev); pm_runtime_disable(&pdev->dev); } static struct platform_driver rkisp1_drv = { .driver = { .name = RKISP1_DRIVER_NAME, .of_match_table = of_match_ptr(rkisp1_of_match), .pm = &rkisp1_pm_ops, }, .probe = rkisp1_probe, .remove_new = rkisp1_remove, }; module_platform_driver(rkisp1_drv); MODULE_DESCRIPTION("Rockchip ISP1 platform driver"); MODULE_LICENSE("Dual MIT/GPL"); |