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