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 | // SPDX-License-Identifier: GPL-2.0 /* * Driver for STM32 Digital Camera Memory Interface Pixel Processor * * Copyright (C) STMicroelectronics SA 2023 * Authors: Hugues Fruchet <hugues.fruchet@foss.st.com> * Alain Volmat <alain.volmat@foss.st.com> * for STMicroelectronics. */ #include <linux/clk.h> #include <linux/delay.h> #include <linux/init.h> #include <linux/module.h> #include <linux/pinctrl/consumer.h> #include <linux/platform_device.h> #include <linux/pm_runtime.h> #include <linux/property.h> #include <linux/reset.h> #include <media/media-device.h> #include <media/v4l2-device.h> #include <media/v4l2-fwnode.h> #include "dcmipp-common.h" #define DCMIPP_MDEV_MODEL_NAME "DCMIPP MDEV" #define DCMIPP_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) { \ .src_ent = src, \ .src_pad = srcpad, \ .sink_ent = sink, \ .sink_pad = sinkpad, \ .flags = link_flags, \ } struct dcmipp_device { /* The platform device */ struct platform_device pdev; struct device *dev; /* Hardware resources */ void __iomem *regs; struct clk *kclk; /* The pipeline configuration */ const struct dcmipp_pipeline_config *pipe_cfg; /* The Associated media_device parent */ struct media_device mdev; /* Internal v4l2 parent device*/ struct v4l2_device v4l2_dev; /* Entities */ struct dcmipp_ent_device **entity; struct v4l2_async_notifier notifier; }; static inline struct dcmipp_device * notifier_to_dcmipp(struct v4l2_async_notifier *n) { return container_of(n, struct dcmipp_device, notifier); } /* Structure which describes individual configuration for each entity */ struct dcmipp_ent_config { const char *name; struct dcmipp_ent_device *(*init) (struct device *dev, const char *entity_name, struct v4l2_device *v4l2_dev, void __iomem *regs); void (*release)(struct dcmipp_ent_device *ved); }; /* Structure which describes links between entities */ struct dcmipp_ent_link { unsigned int src_ent; u16 src_pad; unsigned int sink_ent; u16 sink_pad; u32 flags; }; /* Structure which describes the whole topology */ struct dcmipp_pipeline_config { const struct dcmipp_ent_config *ents; size_t num_ents; const struct dcmipp_ent_link *links; size_t num_links; }; /* -------------------------------------------------------------------------- * Topology Configuration */ static const struct dcmipp_ent_config stm32mp13_ent_config[] = { { .name = "dcmipp_parallel", .init = dcmipp_par_ent_init, .release = dcmipp_par_ent_release, }, { .name = "dcmipp_dump_postproc", .init = dcmipp_byteproc_ent_init, .release = dcmipp_byteproc_ent_release, }, { .name = "dcmipp_dump_capture", .init = dcmipp_bytecap_ent_init, .release = dcmipp_bytecap_ent_release, }, }; #define ID_PARALLEL 0 #define ID_DUMP_BYTEPROC 1 #define ID_DUMP_CAPTURE 2 static const struct dcmipp_ent_link stm32mp13_ent_links[] = { DCMIPP_ENT_LINK(ID_PARALLEL, 1, ID_DUMP_BYTEPROC, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE), DCMIPP_ENT_LINK(ID_DUMP_BYTEPROC, 1, ID_DUMP_CAPTURE, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE), }; static const struct dcmipp_pipeline_config stm32mp13_pipe_cfg = { .ents = stm32mp13_ent_config, .num_ents = ARRAY_SIZE(stm32mp13_ent_config), .links = stm32mp13_ent_links, .num_links = ARRAY_SIZE(stm32mp13_ent_links) }; #define LINK_FLAG_TO_STR(f) ((f) == 0 ? "" :\ (f) == MEDIA_LNK_FL_ENABLED ? "ENABLED" :\ (f) == MEDIA_LNK_FL_IMMUTABLE ? "IMMUTABLE" :\ (f) == (MEDIA_LNK_FL_ENABLED |\ MEDIA_LNK_FL_IMMUTABLE) ?\ "ENABLED, IMMUTABLE" :\ "UNKNOWN") static int dcmipp_create_links(struct dcmipp_device *dcmipp) { unsigned int i; int ret; /* Initialize the links between entities */ for (i = 0; i < dcmipp->pipe_cfg->num_links; i++) { const struct dcmipp_ent_link *link = &dcmipp->pipe_cfg->links[i]; struct dcmipp_ent_device *ved_src = dcmipp->entity[link->src_ent]; struct dcmipp_ent_device *ved_sink = dcmipp->entity[link->sink_ent]; dev_dbg(dcmipp->dev, "Create link \"%s\":%d -> %d:\"%s\" [%s]\n", dcmipp->pipe_cfg->ents[link->src_ent].name, link->src_pad, link->sink_pad, dcmipp->pipe_cfg->ents[link->sink_ent].name, LINK_FLAG_TO_STR(link->flags)); ret = media_create_pad_link(ved_src->ent, link->src_pad, ved_sink->ent, link->sink_pad, link->flags); if (ret) return ret; } return 0; } static int dcmipp_graph_init(struct dcmipp_device *dcmipp); static int dcmipp_create_subdevs(struct dcmipp_device *dcmipp) { int ret, i; /* Call all subdev inits */ for (i = 0; i < dcmipp->pipe_cfg->num_ents; i++) { const char *name = dcmipp->pipe_cfg->ents[i].name; dev_dbg(dcmipp->dev, "add subdev %s\n", name); dcmipp->entity[i] = dcmipp->pipe_cfg->ents[i].init(dcmipp->dev, name, &dcmipp->v4l2_dev, dcmipp->regs); if (IS_ERR(dcmipp->entity[i])) { dev_err(dcmipp->dev, "failed to init subdev %s\n", name); ret = PTR_ERR(dcmipp->entity[i]); goto err_init_entity; } } /* Initialize links */ ret = dcmipp_create_links(dcmipp); if (ret) goto err_init_entity; ret = dcmipp_graph_init(dcmipp); if (ret < 0) goto err_init_entity; return 0; err_init_entity: while (i > 0) dcmipp->pipe_cfg->ents[i - 1].release(dcmipp->entity[i - 1]); return ret; } static const struct of_device_id dcmipp_of_match[] = { { .compatible = "st,stm32mp13-dcmipp", .data = &stm32mp13_pipe_cfg }, { /* end node */ }, }; MODULE_DEVICE_TABLE(of, dcmipp_of_match); static irqreturn_t dcmipp_irq_thread(int irq, void *arg) { struct dcmipp_device *dcmipp = arg; struct dcmipp_ent_device *ved; unsigned int i; /* Call irq thread of each entities of pipeline */ for (i = 0; i < dcmipp->pipe_cfg->num_ents; i++) { ved = dcmipp->entity[i]; if (ved->thread_fn && ved->handler_ret == IRQ_WAKE_THREAD) ved->thread_fn(irq, ved); } return IRQ_HANDLED; } static irqreturn_t dcmipp_irq_callback(int irq, void *arg) { struct dcmipp_device *dcmipp = arg; struct dcmipp_ent_device *ved; irqreturn_t ret = IRQ_HANDLED; unsigned int i; /* Call irq handler of each entities of pipeline */ for (i = 0; i < dcmipp->pipe_cfg->num_ents; i++) { ved = dcmipp->entity[i]; if (ved->handler) ved->handler_ret = ved->handler(irq, ved); else if (ved->thread_fn) ved->handler_ret = IRQ_WAKE_THREAD; else ved->handler_ret = IRQ_HANDLED; if (ved->handler_ret != IRQ_HANDLED) ret = ved->handler_ret; } return ret; } static int dcmipp_graph_notify_bound(struct v4l2_async_notifier *notifier, struct v4l2_subdev *subdev, struct v4l2_async_connection *asd) { struct dcmipp_device *dcmipp = notifier_to_dcmipp(notifier); unsigned int ret; int src_pad; struct dcmipp_ent_device *sink; struct v4l2_fwnode_endpoint vep = { .bus_type = V4L2_MBUS_PARALLEL }; struct fwnode_handle *ep; dev_dbg(dcmipp->dev, "Subdev \"%s\" bound\n", subdev->name); /* * Link this sub-device to DCMIPP, it could be * a parallel camera sensor or a CSI-2 to parallel bridge */ src_pad = media_entity_get_fwnode_pad(&subdev->entity, subdev->fwnode, MEDIA_PAD_FL_SOURCE); /* Get bus characteristics from devicetree */ ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dcmipp->dev), 0, 0, FWNODE_GRAPH_ENDPOINT_NEXT); if (!ep) { dev_err(dcmipp->dev, "Could not find the endpoint\n"); return -ENODEV; } /* Check for parallel bus-type first, then bt656 */ ret = v4l2_fwnode_endpoint_parse(ep, &vep); if (ret) { vep.bus_type = V4L2_MBUS_BT656; ret = v4l2_fwnode_endpoint_parse(ep, &vep); if (ret) { dev_err(dcmipp->dev, "Could not parse the endpoint\n"); fwnode_handle_put(ep); return ret; } } fwnode_handle_put(ep); if (vep.bus.parallel.bus_width == 0) { dev_err(dcmipp->dev, "Invalid parallel interface bus-width\n"); return -ENODEV; } /* Only 8 bits bus width supported with BT656 bus */ if (vep.bus_type == V4L2_MBUS_BT656 && vep.bus.parallel.bus_width != 8) { dev_err(dcmipp->dev, "BT656 bus conflicts with %u bits bus width (8 bits required)\n", vep.bus.parallel.bus_width); return -ENODEV; } /* Parallel input device detected, connect it to parallel subdev */ sink = dcmipp->entity[ID_PARALLEL]; sink->bus.flags = vep.bus.parallel.flags; sink->bus.bus_width = vep.bus.parallel.bus_width; sink->bus.data_shift = vep.bus.parallel.data_shift; sink->bus_type = vep.bus_type; ret = media_create_pad_link(&subdev->entity, src_pad, sink->ent, 0, MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); if (ret) { dev_err(dcmipp->dev, "Failed to create media pad link with subdev \"%s\"\n", subdev->name); return ret; } dev_dbg(dcmipp->dev, "DCMIPP is now linked to \"%s\"\n", subdev->name); return 0; } static void dcmipp_graph_notify_unbind(struct v4l2_async_notifier *notifier, struct v4l2_subdev *sd, struct v4l2_async_connection *asd) { struct dcmipp_device *dcmipp = notifier_to_dcmipp(notifier); dev_dbg(dcmipp->dev, "Removing %s\n", sd->name); } static int dcmipp_graph_notify_complete(struct v4l2_async_notifier *notifier) { struct dcmipp_device *dcmipp = notifier_to_dcmipp(notifier); int ret; /* Register the media device */ ret = media_device_register(&dcmipp->mdev); if (ret) { dev_err(dcmipp->mdev.dev, "media device register failed (err=%d)\n", ret); return ret; } /* Expose all subdev's nodes*/ ret = v4l2_device_register_subdev_nodes(&dcmipp->v4l2_dev); if (ret) { dev_err(dcmipp->mdev.dev, "dcmipp subdev nodes registration failed (err=%d)\n", ret); media_device_unregister(&dcmipp->mdev); return ret; } dev_dbg(dcmipp->dev, "Notify complete !\n"); return 0; } static const struct v4l2_async_notifier_operations dcmipp_graph_notify_ops = { .bound = dcmipp_graph_notify_bound, .unbind = dcmipp_graph_notify_unbind, .complete = dcmipp_graph_notify_complete, }; static int dcmipp_graph_init(struct dcmipp_device *dcmipp) { struct v4l2_async_connection *asd; struct fwnode_handle *ep; int ret; ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dcmipp->dev), 0, 0, FWNODE_GRAPH_ENDPOINT_NEXT); if (!ep) { dev_err(dcmipp->dev, "Failed to get next endpoint\n"); return -EINVAL; } v4l2_async_nf_init(&dcmipp->notifier, &dcmipp->v4l2_dev); asd = v4l2_async_nf_add_fwnode_remote(&dcmipp->notifier, ep, struct v4l2_async_connection); fwnode_handle_put(ep); if (IS_ERR(asd)) { dev_err(dcmipp->dev, "Failed to add fwnode remote subdev\n"); return PTR_ERR(asd); } dcmipp->notifier.ops = &dcmipp_graph_notify_ops; ret = v4l2_async_nf_register(&dcmipp->notifier); if (ret < 0) { dev_err(dcmipp->dev, "Failed to register notifier\n"); v4l2_async_nf_cleanup(&dcmipp->notifier); return ret; } return 0; } static int dcmipp_probe(struct platform_device *pdev) { struct dcmipp_device *dcmipp; struct clk *kclk; const struct dcmipp_pipeline_config *pipe_cfg; struct reset_control *rstc; int irq; int ret; dcmipp = devm_kzalloc(&pdev->dev, sizeof(*dcmipp), GFP_KERNEL); if (!dcmipp) return -ENOMEM; dcmipp->dev = &pdev->dev; pipe_cfg = device_get_match_data(dcmipp->dev); if (!pipe_cfg) { dev_err(&pdev->dev, "Can't get device data\n"); return -ENODEV; } dcmipp->pipe_cfg = pipe_cfg; platform_set_drvdata(pdev, dcmipp); /* Get hardware resources from devicetree */ rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); if (IS_ERR(rstc)) return dev_err_probe(&pdev->dev, PTR_ERR(rstc), "Could not get reset control\n"); irq = platform_get_irq(pdev, 0); if (irq <= 0) { if (irq != -EPROBE_DEFER) dev_err(&pdev->dev, "Could not get irq\n"); return irq ? irq : -ENXIO; } dcmipp->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL); if (IS_ERR(dcmipp->regs)) { dev_err(&pdev->dev, "Could not map registers\n"); return PTR_ERR(dcmipp->regs); } ret = devm_request_threaded_irq(&pdev->dev, irq, dcmipp_irq_callback, dcmipp_irq_thread, IRQF_ONESHOT, dev_name(&pdev->dev), dcmipp); if (ret) { dev_err(&pdev->dev, "Unable to request irq %d\n", irq); return ret; } /* Reset device */ ret = reset_control_assert(rstc); if (ret) { dev_err(&pdev->dev, "Failed to assert the reset line\n"); return ret; } usleep_range(3000, 5000); ret = reset_control_deassert(rstc); if (ret) { dev_err(&pdev->dev, "Failed to deassert the reset line\n"); return ret; } kclk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(kclk)) return dev_err_probe(&pdev->dev, PTR_ERR(kclk), "Unable to get kclk\n"); dcmipp->kclk = kclk; dcmipp->entity = devm_kcalloc(&pdev->dev, dcmipp->pipe_cfg->num_ents, sizeof(*dcmipp->entity), GFP_KERNEL); if (!dcmipp->entity) return -ENOMEM; /* Register the v4l2 struct */ ret = v4l2_device_register(&pdev->dev, &dcmipp->v4l2_dev); if (ret) { dev_err(&pdev->dev, "v4l2 device register failed (err=%d)\n", ret); return ret; } /* Link the media device within the v4l2_device */ dcmipp->v4l2_dev.mdev = &dcmipp->mdev; /* Initialize media device */ strscpy(dcmipp->mdev.model, DCMIPP_MDEV_MODEL_NAME, sizeof(dcmipp->mdev.model)); dcmipp->mdev.dev = &pdev->dev; media_device_init(&dcmipp->mdev); /* Initialize subdevs */ ret = dcmipp_create_subdevs(dcmipp); if (ret) { media_device_cleanup(&dcmipp->mdev); v4l2_device_unregister(&dcmipp->v4l2_dev); return ret; } pm_runtime_enable(dcmipp->dev); dev_info(&pdev->dev, "Probe done"); return 0; } static void dcmipp_remove(struct platform_device *pdev) { struct dcmipp_device *dcmipp = platform_get_drvdata(pdev); unsigned int i; pm_runtime_disable(&pdev->dev); v4l2_async_nf_unregister(&dcmipp->notifier); v4l2_async_nf_cleanup(&dcmipp->notifier); for (i = 0; i < dcmipp->pipe_cfg->num_ents; i++) dcmipp->pipe_cfg->ents[i].release(dcmipp->entity[i]); media_device_unregister(&dcmipp->mdev); media_device_cleanup(&dcmipp->mdev); v4l2_device_unregister(&dcmipp->v4l2_dev); } static int dcmipp_runtime_suspend(struct device *dev) { struct dcmipp_device *dcmipp = dev_get_drvdata(dev); clk_disable_unprepare(dcmipp->kclk); return 0; } static int dcmipp_runtime_resume(struct device *dev) { struct dcmipp_device *dcmipp = dev_get_drvdata(dev); int ret; ret = clk_prepare_enable(dcmipp->kclk); if (ret) dev_err(dev, "%s: Failed to prepare_enable kclk\n", __func__); return ret; } static int dcmipp_suspend(struct device *dev) { /* disable clock */ pm_runtime_force_suspend(dev); /* change pinctrl state */ pinctrl_pm_select_sleep_state(dev); return 0; } static int dcmipp_resume(struct device *dev) { /* restore pinctl default state */ pinctrl_pm_select_default_state(dev); /* clock enable */ pm_runtime_force_resume(dev); return 0; } static const struct dev_pm_ops dcmipp_pm_ops = { SYSTEM_SLEEP_PM_OPS(dcmipp_suspend, dcmipp_resume) RUNTIME_PM_OPS(dcmipp_runtime_suspend, dcmipp_runtime_resume, NULL) }; static struct platform_driver dcmipp_pdrv = { .probe = dcmipp_probe, .remove_new = dcmipp_remove, .driver = { .name = DCMIPP_PDEV_NAME, .of_match_table = dcmipp_of_match, .pm = pm_ptr(&dcmipp_pm_ops), }, }; module_platform_driver(dcmipp_pdrv); MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@foss.st.com>"); MODULE_AUTHOR("Alain Volmat <alain.volmat@foss.st.com>"); MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface with Pixel Processor driver"); MODULE_LICENSE("GPL"); |