1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for Renesas R-Car VIN 4 * 5 * Copyright (C) 2016 Renesas Electronics Corp. 6 * Copyright (C) 2011-2013 Renesas Solutions Corp. 7 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com> 8 * Copyright (C) 2008 Magnus Damm 9 * 10 * Based on the soc-camera rcar_vin driver 11 */ 12 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/of_device.h> 16 #include <linux/of_graph.h> 17 #include <linux/platform_device.h> 18 #include <linux/pm_runtime.h> 19 #include <linux/slab.h> 20 21 #include <media/v4l2-async.h> 22 #include <media/v4l2-fwnode.h> 23 #include <media/v4l2-mc.h> 24 25 #include "rcar-vin.h" 26 27 /* 28 * The companion CSI-2 receiver driver (rcar-csi2) is known 29 * and we know it has one source pad (pad 0) and four sink 30 * pads (pad 1-4). So to translate a pad on the remote 31 * CSI-2 receiver to/from the VIN internal channel number simply 32 * subtract/add one from the pad/channel number. 33 */ 34 #define rvin_group_csi_pad_to_channel(pad) ((pad) - 1) 35 #define rvin_group_csi_channel_to_pad(channel) ((channel) + 1) 36 37 /* 38 * Not all VINs are created equal, master VINs control the 39 * routing for other VIN's. We can figure out which VIN is 40 * master by looking at a VINs id. 41 */ 42 #define rvin_group_id_to_master(vin) ((vin) < 4 ? 0 : 4) 43 44 #define v4l2_dev_to_vin(d) container_of(d, struct rvin_dev, v4l2_dev) 45 46 /* ----------------------------------------------------------------------------- 47 * Gen3 Group Allocator 48 */ 49 50 /* FIXME: This should if we find a system that supports more 51 * than one group for the whole system be replaced with a linked 52 * list of groups. And eventually all of this should be replaced 53 * with a global device allocator API. 54 * 55 * But for now this works as on all supported systems there will 56 * be only one group for all instances. 57 */ 58 59 static DEFINE_MUTEX(rvin_group_lock); 60 static struct rvin_group *rvin_group_data; 61 62 static void rvin_group_cleanup(struct rvin_group *group) 63 { 64 media_device_cleanup(&group->mdev); 65 mutex_destroy(&group->lock); 66 } 67 68 static int rvin_group_init(struct rvin_group *group, struct rvin_dev *vin, 69 int (*link_setup)(struct rvin_dev *), 70 const struct media_device_ops *ops) 71 { 72 struct media_device *mdev = &group->mdev; 73 const struct of_device_id *match; 74 struct device_node *np; 75 76 mutex_init(&group->lock); 77 78 /* Count number of VINs in the system */ 79 group->count = 0; 80 for_each_matching_node(np, vin->dev->driver->of_match_table) 81 if (of_device_is_available(np)) 82 group->count++; 83 84 vin_dbg(vin, "found %u enabled VIN's in DT", group->count); 85 86 group->link_setup = link_setup; 87 88 mdev->dev = vin->dev; 89 mdev->ops = ops; 90 91 match = of_match_node(vin->dev->driver->of_match_table, 92 vin->dev->of_node); 93 94 strscpy(mdev->driver_name, KBUILD_MODNAME, sizeof(mdev->driver_name)); 95 strscpy(mdev->model, match->compatible, sizeof(mdev->model)); 96 97 media_device_init(mdev); 98 99 return 0; 100 } 101 102 static void rvin_group_release(struct kref *kref) 103 { 104 struct rvin_group *group = 105 container_of(kref, struct rvin_group, refcount); 106 107 mutex_lock(&rvin_group_lock); 108 109 rvin_group_data = NULL; 110 111 rvin_group_cleanup(group); 112 113 kfree(group); 114 115 mutex_unlock(&rvin_group_lock); 116 } 117 118 static int rvin_group_get(struct rvin_dev *vin, 119 int (*link_setup)(struct rvin_dev *), 120 const struct media_device_ops *ops) 121 { 122 struct rvin_group *group; 123 u32 id; 124 int ret; 125 126 /* Make sure VIN id is present and sane */ 127 ret = of_property_read_u32(vin->dev->of_node, "renesas,id", &id); 128 if (ret) { 129 vin_err(vin, "%pOF: No renesas,id property found\n", 130 vin->dev->of_node); 131 return -EINVAL; 132 } 133 134 if (id >= RCAR_VIN_NUM) { 135 vin_err(vin, "%pOF: Invalid renesas,id '%u'\n", 136 vin->dev->of_node, id); 137 return -EINVAL; 138 } 139 140 /* Join or create a VIN group */ 141 mutex_lock(&rvin_group_lock); 142 if (rvin_group_data) { 143 group = rvin_group_data; 144 kref_get(&group->refcount); 145 } else { 146 group = kzalloc(sizeof(*group), GFP_KERNEL); 147 if (!group) { 148 ret = -ENOMEM; 149 goto err_group; 150 } 151 152 ret = rvin_group_init(group, vin, link_setup, ops); 153 if (ret) { 154 kfree(group); 155 vin_err(vin, "Failed to initialize group\n"); 156 goto err_group; 157 } 158 159 kref_init(&group->refcount); 160 161 rvin_group_data = group; 162 } 163 mutex_unlock(&rvin_group_lock); 164 165 /* Add VIN to group */ 166 mutex_lock(&group->lock); 167 168 if (group->vin[id]) { 169 vin_err(vin, "Duplicate renesas,id property value %u\n", id); 170 mutex_unlock(&group->lock); 171 kref_put(&group->refcount, rvin_group_release); 172 return -EINVAL; 173 } 174 175 group->vin[id] = vin; 176 177 vin->id = id; 178 vin->group = group; 179 vin->v4l2_dev.mdev = &group->mdev; 180 181 mutex_unlock(&group->lock); 182 183 return 0; 184 err_group: 185 mutex_unlock(&rvin_group_lock); 186 return ret; 187 } 188 189 static void rvin_group_put(struct rvin_dev *vin) 190 { 191 struct rvin_group *group = vin->group; 192 193 mutex_lock(&group->lock); 194 195 vin->group = NULL; 196 vin->v4l2_dev.mdev = NULL; 197 198 if (WARN_ON(group->vin[vin->id] != vin)) 199 goto out; 200 201 group->vin[vin->id] = NULL; 202 out: 203 mutex_unlock(&group->lock); 204 205 kref_put(&group->refcount, rvin_group_release); 206 } 207 208 /* group lock should be held when calling this function. */ 209 static int rvin_group_entity_to_remote_id(struct rvin_group *group, 210 struct media_entity *entity) 211 { 212 struct v4l2_subdev *sd; 213 unsigned int i; 214 215 sd = media_entity_to_v4l2_subdev(entity); 216 217 for (i = 0; i < RVIN_REMOTES_MAX; i++) 218 if (group->remotes[i].subdev == sd) 219 return i; 220 221 return -ENODEV; 222 } 223 224 static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier) 225 { 226 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev); 227 unsigned int i; 228 int ret; 229 230 ret = media_device_register(&vin->group->mdev); 231 if (ret) 232 return ret; 233 234 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev); 235 if (ret) { 236 vin_err(vin, "Failed to register subdev nodes\n"); 237 return ret; 238 } 239 240 /* Register all video nodes for the group. */ 241 for (i = 0; i < RCAR_VIN_NUM; i++) { 242 if (vin->group->vin[i] && 243 !video_is_registered(&vin->group->vin[i]->vdev)) { 244 ret = rvin_v4l2_register(vin->group->vin[i]); 245 if (ret) 246 return ret; 247 } 248 } 249 250 return vin->group->link_setup(vin); 251 } 252 253 static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier, 254 struct v4l2_subdev *subdev, 255 struct v4l2_async_subdev *asd) 256 { 257 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev); 258 unsigned int i; 259 260 for (i = 0; i < RCAR_VIN_NUM; i++) 261 if (vin->group->vin[i]) 262 rvin_v4l2_unregister(vin->group->vin[i]); 263 264 mutex_lock(&vin->group->lock); 265 266 for (i = 0; i < RVIN_CSI_MAX; i++) { 267 if (vin->group->remotes[i].asd != asd) 268 continue; 269 vin->group->remotes[i].subdev = NULL; 270 vin_dbg(vin, "Unbind %s from slot %u\n", subdev->name, i); 271 break; 272 } 273 274 mutex_unlock(&vin->group->lock); 275 276 media_device_unregister(&vin->group->mdev); 277 } 278 279 static int rvin_group_notify_bound(struct v4l2_async_notifier *notifier, 280 struct v4l2_subdev *subdev, 281 struct v4l2_async_subdev *asd) 282 { 283 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev); 284 unsigned int i; 285 286 mutex_lock(&vin->group->lock); 287 288 for (i = 0; i < RVIN_CSI_MAX; i++) { 289 if (vin->group->remotes[i].asd != asd) 290 continue; 291 vin->group->remotes[i].subdev = subdev; 292 vin_dbg(vin, "Bound %s to slot %u\n", subdev->name, i); 293 break; 294 } 295 296 mutex_unlock(&vin->group->lock); 297 298 return 0; 299 } 300 301 static const struct v4l2_async_notifier_operations rvin_group_notify_ops = { 302 .bound = rvin_group_notify_bound, 303 .unbind = rvin_group_notify_unbind, 304 .complete = rvin_group_notify_complete, 305 }; 306 307 static int rvin_group_parse_of(struct rvin_dev *vin, unsigned int port, 308 unsigned int id) 309 { 310 struct fwnode_handle *ep, *fwnode; 311 struct v4l2_fwnode_endpoint vep = { 312 .bus_type = V4L2_MBUS_CSI2_DPHY, 313 }; 314 struct v4l2_async_subdev *asd; 315 int ret; 316 317 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(vin->dev), port, id, 0); 318 if (!ep) 319 return 0; 320 321 fwnode = fwnode_graph_get_remote_endpoint(ep); 322 ret = v4l2_fwnode_endpoint_parse(ep, &vep); 323 fwnode_handle_put(ep); 324 if (ret) { 325 vin_err(vin, "Failed to parse %pOF\n", to_of_node(fwnode)); 326 ret = -EINVAL; 327 goto out; 328 } 329 330 asd = v4l2_async_nf_add_fwnode(&vin->group->notifier, fwnode, 331 struct v4l2_async_subdev); 332 if (IS_ERR(asd)) { 333 ret = PTR_ERR(asd); 334 goto out; 335 } 336 337 vin->group->remotes[vep.base.id].asd = asd; 338 339 vin_dbg(vin, "Add group OF device %pOF to slot %u\n", 340 to_of_node(fwnode), vep.base.id); 341 out: 342 fwnode_handle_put(fwnode); 343 344 return ret; 345 } 346 347 static void rvin_group_notifier_cleanup(struct rvin_dev *vin) 348 { 349 if (&vin->v4l2_dev == vin->group->notifier.v4l2_dev) { 350 v4l2_async_nf_unregister(&vin->group->notifier); 351 v4l2_async_nf_cleanup(&vin->group->notifier); 352 } 353 } 354 355 static int rvin_group_notifier_init(struct rvin_dev *vin, unsigned int port, 356 unsigned int max_id) 357 { 358 unsigned int count = 0, vin_mask = 0; 359 unsigned int i, id; 360 int ret; 361 362 mutex_lock(&vin->group->lock); 363 364 /* If not all VIN's are registered don't register the notifier. */ 365 for (i = 0; i < RCAR_VIN_NUM; i++) { 366 if (vin->group->vin[i]) { 367 count++; 368 vin_mask |= BIT(i); 369 } 370 } 371 372 if (vin->group->count != count) { 373 mutex_unlock(&vin->group->lock); 374 return 0; 375 } 376 377 mutex_unlock(&vin->group->lock); 378 379 v4l2_async_nf_init(&vin->group->notifier); 380 381 /* 382 * Some subdevices may overlap but the parser function can handle it and 383 * each subdevice will only be registered once with the group notifier. 384 */ 385 for (i = 0; i < RCAR_VIN_NUM; i++) { 386 if (!(vin_mask & BIT(i))) 387 continue; 388 389 for (id = 0; id < max_id; id++) { 390 if (vin->group->remotes[id].asd) 391 continue; 392 393 ret = rvin_group_parse_of(vin->group->vin[i], port, id); 394 if (ret) 395 return ret; 396 } 397 } 398 399 if (list_empty(&vin->group->notifier.asd_list)) 400 return 0; 401 402 vin->group->notifier.ops = &rvin_group_notify_ops; 403 ret = v4l2_async_nf_register(&vin->v4l2_dev, &vin->group->notifier); 404 if (ret < 0) { 405 vin_err(vin, "Notifier registration failed\n"); 406 v4l2_async_nf_cleanup(&vin->group->notifier); 407 return ret; 408 } 409 410 return 0; 411 } 412 413 /* ----------------------------------------------------------------------------- 414 * Controls 415 */ 416 417 static int rvin_s_ctrl(struct v4l2_ctrl *ctrl) 418 { 419 struct rvin_dev *vin = 420 container_of(ctrl->handler, struct rvin_dev, ctrl_handler); 421 422 switch (ctrl->id) { 423 case V4L2_CID_ALPHA_COMPONENT: 424 rvin_set_alpha(vin, ctrl->val); 425 break; 426 } 427 428 return 0; 429 } 430 431 static const struct v4l2_ctrl_ops rvin_ctrl_ops = { 432 .s_ctrl = rvin_s_ctrl, 433 }; 434 435 static void rvin_free_controls(struct rvin_dev *vin) 436 { 437 v4l2_ctrl_handler_free(&vin->ctrl_handler); 438 vin->vdev.ctrl_handler = NULL; 439 } 440 441 static int rvin_create_controls(struct rvin_dev *vin, struct v4l2_subdev *subdev) 442 { 443 int ret; 444 445 ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 16); 446 if (ret < 0) 447 return ret; 448 449 /* The VIN directly deals with alpha component. */ 450 v4l2_ctrl_new_std(&vin->ctrl_handler, &rvin_ctrl_ops, 451 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255); 452 453 if (vin->ctrl_handler.error) { 454 ret = vin->ctrl_handler.error; 455 rvin_free_controls(vin); 456 return ret; 457 } 458 459 /* For the non-MC mode add controls from the subdevice. */ 460 if (subdev) { 461 ret = v4l2_ctrl_add_handler(&vin->ctrl_handler, 462 subdev->ctrl_handler, NULL, true); 463 if (ret < 0) { 464 rvin_free_controls(vin); 465 return ret; 466 } 467 } 468 469 vin->vdev.ctrl_handler = &vin->ctrl_handler; 470 471 return 0; 472 } 473 474 /* ----------------------------------------------------------------------------- 475 * Async notifier 476 */ 477 478 static int rvin_find_pad(struct v4l2_subdev *sd, int direction) 479 { 480 unsigned int pad; 481 482 if (sd->entity.num_pads <= 1) 483 return 0; 484 485 for (pad = 0; pad < sd->entity.num_pads; pad++) 486 if (sd->entity.pads[pad].flags & direction) 487 return pad; 488 489 return -EINVAL; 490 } 491 492 /* ----------------------------------------------------------------------------- 493 * Parallel async notifier 494 */ 495 496 /* The vin lock should be held when calling the subdevice attach and detach */ 497 static int rvin_parallel_subdevice_attach(struct rvin_dev *vin, 498 struct v4l2_subdev *subdev) 499 { 500 struct v4l2_subdev_mbus_code_enum code = { 501 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 502 }; 503 int ret; 504 505 /* Find source and sink pad of remote subdevice */ 506 ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE); 507 if (ret < 0) 508 return ret; 509 vin->parallel.source_pad = ret; 510 511 ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK); 512 vin->parallel.sink_pad = ret < 0 ? 0 : ret; 513 514 if (vin->info->use_mc) { 515 vin->parallel.subdev = subdev; 516 return 0; 517 } 518 519 /* Find compatible subdevices mbus format */ 520 vin->mbus_code = 0; 521 code.index = 0; 522 code.pad = vin->parallel.source_pad; 523 while (!vin->mbus_code && 524 !v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) { 525 code.index++; 526 switch (code.code) { 527 case MEDIA_BUS_FMT_YUYV8_1X16: 528 case MEDIA_BUS_FMT_UYVY8_1X16: 529 case MEDIA_BUS_FMT_UYVY8_2X8: 530 case MEDIA_BUS_FMT_UYVY10_2X10: 531 case MEDIA_BUS_FMT_RGB888_1X24: 532 vin->mbus_code = code.code; 533 vin_dbg(vin, "Found media bus format for %s: %d\n", 534 subdev->name, vin->mbus_code); 535 break; 536 default: 537 break; 538 } 539 } 540 541 if (!vin->mbus_code) { 542 vin_err(vin, "Unsupported media bus format for %s\n", 543 subdev->name); 544 return -EINVAL; 545 } 546 547 /* Read tvnorms */ 548 ret = v4l2_subdev_call(subdev, video, g_tvnorms, &vin->vdev.tvnorms); 549 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) 550 return ret; 551 552 /* Read standard */ 553 vin->std = V4L2_STD_UNKNOWN; 554 ret = v4l2_subdev_call(subdev, video, g_std, &vin->std); 555 if (ret < 0 && ret != -ENOIOCTLCMD) 556 return ret; 557 558 /* Add the controls */ 559 ret = rvin_create_controls(vin, subdev); 560 if (ret < 0) 561 return ret; 562 563 vin->parallel.subdev = subdev; 564 565 return 0; 566 } 567 568 static void rvin_parallel_subdevice_detach(struct rvin_dev *vin) 569 { 570 rvin_v4l2_unregister(vin); 571 vin->parallel.subdev = NULL; 572 573 if (!vin->info->use_mc) 574 rvin_free_controls(vin); 575 } 576 577 static int rvin_parallel_notify_complete(struct v4l2_async_notifier *notifier) 578 { 579 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev); 580 struct media_entity *source; 581 struct media_entity *sink; 582 int ret; 583 584 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev); 585 if (ret < 0) { 586 vin_err(vin, "Failed to register subdev nodes\n"); 587 return ret; 588 } 589 590 if (!video_is_registered(&vin->vdev)) { 591 ret = rvin_v4l2_register(vin); 592 if (ret < 0) 593 return ret; 594 } 595 596 if (!vin->info->use_mc) 597 return 0; 598 599 /* If we're running with media-controller, link the subdevs. */ 600 source = &vin->parallel.subdev->entity; 601 sink = &vin->vdev.entity; 602 603 ret = media_create_pad_link(source, vin->parallel.source_pad, 604 sink, vin->parallel.sink_pad, 0); 605 if (ret) 606 vin_err(vin, "Error adding link from %s to %s: %d\n", 607 source->name, sink->name, ret); 608 609 return ret; 610 } 611 612 static void rvin_parallel_notify_unbind(struct v4l2_async_notifier *notifier, 613 struct v4l2_subdev *subdev, 614 struct v4l2_async_subdev *asd) 615 { 616 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev); 617 618 vin_dbg(vin, "unbind parallel subdev %s\n", subdev->name); 619 620 mutex_lock(&vin->lock); 621 rvin_parallel_subdevice_detach(vin); 622 mutex_unlock(&vin->lock); 623 } 624 625 static int rvin_parallel_notify_bound(struct v4l2_async_notifier *notifier, 626 struct v4l2_subdev *subdev, 627 struct v4l2_async_subdev *asd) 628 { 629 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev); 630 int ret; 631 632 mutex_lock(&vin->lock); 633 ret = rvin_parallel_subdevice_attach(vin, subdev); 634 mutex_unlock(&vin->lock); 635 if (ret) 636 return ret; 637 638 v4l2_set_subdev_hostdata(subdev, vin); 639 640 vin_dbg(vin, "bound subdev %s source pad: %u sink pad: %u\n", 641 subdev->name, vin->parallel.source_pad, 642 vin->parallel.sink_pad); 643 644 return 0; 645 } 646 647 static const struct v4l2_async_notifier_operations rvin_parallel_notify_ops = { 648 .bound = rvin_parallel_notify_bound, 649 .unbind = rvin_parallel_notify_unbind, 650 .complete = rvin_parallel_notify_complete, 651 }; 652 653 static int rvin_parallel_parse_of(struct rvin_dev *vin) 654 { 655 struct fwnode_handle *ep, *fwnode; 656 struct v4l2_fwnode_endpoint vep = { 657 .bus_type = V4L2_MBUS_UNKNOWN, 658 }; 659 struct v4l2_async_subdev *asd; 660 int ret; 661 662 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(vin->dev), 0, 0, 0); 663 if (!ep) 664 return 0; 665 666 fwnode = fwnode_graph_get_remote_endpoint(ep); 667 ret = v4l2_fwnode_endpoint_parse(ep, &vep); 668 fwnode_handle_put(ep); 669 if (ret) { 670 vin_err(vin, "Failed to parse %pOF\n", to_of_node(fwnode)); 671 ret = -EINVAL; 672 goto out; 673 } 674 675 switch (vep.bus_type) { 676 case V4L2_MBUS_PARALLEL: 677 case V4L2_MBUS_BT656: 678 vin_dbg(vin, "Found %s media bus\n", 679 vep.bus_type == V4L2_MBUS_PARALLEL ? 680 "PARALLEL" : "BT656"); 681 vin->parallel.mbus_type = vep.bus_type; 682 vin->parallel.bus = vep.bus.parallel; 683 break; 684 default: 685 vin_err(vin, "Unknown media bus type\n"); 686 ret = -EINVAL; 687 goto out; 688 } 689 690 asd = v4l2_async_nf_add_fwnode(&vin->notifier, fwnode, 691 struct v4l2_async_subdev); 692 if (IS_ERR(asd)) { 693 ret = PTR_ERR(asd); 694 goto out; 695 } 696 697 vin->parallel.asd = asd; 698 699 vin_dbg(vin, "Add parallel OF device %pOF\n", to_of_node(fwnode)); 700 out: 701 fwnode_handle_put(fwnode); 702 703 return ret; 704 } 705 706 static void rvin_parallel_cleanup(struct rvin_dev *vin) 707 { 708 v4l2_async_nf_unregister(&vin->notifier); 709 v4l2_async_nf_cleanup(&vin->notifier); 710 } 711 712 static int rvin_parallel_init(struct rvin_dev *vin) 713 { 714 int ret; 715 716 v4l2_async_nf_init(&vin->notifier); 717 718 ret = rvin_parallel_parse_of(vin); 719 if (ret) 720 return ret; 721 722 if (!vin->parallel.asd) 723 return -ENODEV; 724 725 vin_dbg(vin, "Found parallel subdevice %pOF\n", 726 to_of_node(vin->parallel.asd->match.fwnode)); 727 728 vin->notifier.ops = &rvin_parallel_notify_ops; 729 ret = v4l2_async_nf_register(&vin->v4l2_dev, &vin->notifier); 730 if (ret < 0) { 731 vin_err(vin, "Notifier registration failed\n"); 732 v4l2_async_nf_cleanup(&vin->notifier); 733 return ret; 734 } 735 736 return 0; 737 } 738 739 /* ----------------------------------------------------------------------------- 740 * CSI-2 741 */ 742 743 /* 744 * Link setup for the links between a VIN and a CSI-2 receiver is a bit 745 * complex. The reason for this is that the register controlling routing 746 * is not present in each VIN instance. There are special VINs which 747 * control routing for themselves and other VINs. There are not many 748 * different possible links combinations that can be enabled at the same 749 * time, therefor all already enabled links which are controlled by a 750 * master VIN need to be taken into account when making the decision 751 * if a new link can be enabled or not. 752 * 753 * 1. Find out which VIN the link the user tries to enable is connected to. 754 * 2. Lookup which master VIN controls the links for this VIN. 755 * 3. Start with a bitmask with all bits set. 756 * 4. For each previously enabled link from the master VIN bitwise AND its 757 * route mask (see documentation for mask in struct rvin_group_route) 758 * with the bitmask. 759 * 5. Bitwise AND the mask for the link the user tries to enable to the bitmask. 760 * 6. If the bitmask is not empty at this point the new link can be enabled 761 * while keeping all previous links enabled. Update the CHSEL value of the 762 * master VIN and inform the user that the link could be enabled. 763 * 764 * Please note that no link can be enabled if any VIN in the group is 765 * currently open. 766 */ 767 static int rvin_csi2_link_notify(struct media_link *link, u32 flags, 768 unsigned int notification) 769 { 770 struct rvin_group *group = container_of(link->graph_obj.mdev, 771 struct rvin_group, mdev); 772 struct media_entity *entity; 773 struct video_device *vdev; 774 struct rvin_dev *vin; 775 unsigned int i; 776 int csi_id, ret; 777 778 ret = v4l2_pipeline_link_notify(link, flags, notification); 779 if (ret) 780 return ret; 781 782 /* Only care about link enablement for VIN nodes. */ 783 if (!(flags & MEDIA_LNK_FL_ENABLED) || 784 !is_media_entity_v4l2_video_device(link->sink->entity)) 785 return 0; 786 787 /* 788 * Don't allow link changes if any stream in the graph is active as 789 * modifying the CHSEL register fields can disrupt running streams. 790 */ 791 media_device_for_each_entity(entity, &group->mdev) 792 if (media_entity_is_streaming(entity)) 793 return -EBUSY; 794 795 /* Find the master VIN that controls the routes. */ 796 vdev = media_entity_to_video_device(link->sink->entity); 797 vin = container_of(vdev, struct rvin_dev, vdev); 798 799 mutex_lock(&group->lock); 800 801 csi_id = rvin_group_entity_to_remote_id(group, link->source->entity); 802 if (csi_id == -ENODEV) { 803 struct v4l2_subdev *sd; 804 805 /* 806 * Make sure the source entity subdevice is registered as 807 * a parallel input of one of the enabled VINs if it is not 808 * one of the CSI-2 subdevices. 809 * 810 * No hardware configuration required for parallel inputs, 811 * we can return here. 812 */ 813 sd = media_entity_to_v4l2_subdev(link->source->entity); 814 for (i = 0; i < RCAR_VIN_NUM; i++) { 815 if (group->vin[i] && 816 group->vin[i]->parallel.subdev == sd) { 817 group->vin[i]->is_csi = false; 818 ret = 0; 819 goto out; 820 } 821 } 822 823 vin_err(vin, "Subdevice %s not registered to any VIN\n", 824 link->source->entity->name); 825 ret = -ENODEV; 826 } else { 827 const struct rvin_group_route *route; 828 unsigned int chsel = UINT_MAX; 829 unsigned int master_id; 830 831 master_id = rvin_group_id_to_master(vin->id); 832 833 if (WARN_ON(!group->vin[master_id])) { 834 ret = -ENODEV; 835 goto out; 836 } 837 838 /* Make sure group is connected to same CSI-2 */ 839 for (i = master_id; i < master_id + 4; i++) { 840 struct media_pad *csi_pad; 841 842 if (!group->vin[i]) 843 continue; 844 845 /* Get remote CSI-2, if any. */ 846 csi_pad = media_pad_remote_pad_first( 847 &group->vin[i]->vdev.entity.pads[0]); 848 if (!csi_pad) 849 continue; 850 851 if (csi_pad->entity != link->source->entity) { 852 vin_dbg(vin, "Already attached to %s\n", 853 csi_pad->entity->name); 854 ret = -EBUSY; 855 goto out; 856 } 857 } 858 859 for (route = vin->info->routes; route->chsel; route++) { 860 if (route->master == master_id && route->csi == csi_id) { 861 chsel = route->chsel; 862 break; 863 } 864 } 865 866 if (chsel == UINT_MAX) { 867 vin_err(vin, "No CHSEL value found\n"); 868 ret = -EINVAL; 869 goto out; 870 } 871 872 ret = rvin_set_channel_routing(group->vin[master_id], chsel); 873 if (ret) 874 goto out; 875 876 vin->is_csi = true; 877 } 878 out: 879 mutex_unlock(&group->lock); 880 881 return ret; 882 } 883 884 static const struct media_device_ops rvin_csi2_media_ops = { 885 .link_notify = rvin_csi2_link_notify, 886 }; 887 888 static int rvin_csi2_create_link(struct rvin_group *group, unsigned int id, 889 const struct rvin_group_route *route) 890 { 891 struct media_entity *source = &group->remotes[route->csi].subdev->entity; 892 struct media_entity *sink = &group->vin[id]->vdev.entity; 893 struct media_pad *sink_pad = &sink->pads[0]; 894 unsigned int channel; 895 int ret; 896 897 for (channel = 0; channel < 4; channel++) { 898 unsigned int source_idx = rvin_group_csi_channel_to_pad(channel); 899 struct media_pad *source_pad = &source->pads[source_idx]; 900 901 /* Skip if link already exists. */ 902 if (media_entity_find_link(source_pad, sink_pad)) 903 continue; 904 905 ret = media_create_pad_link(source, source_idx, sink, 0, 0); 906 if (ret) 907 return ret; 908 } 909 910 return 0; 911 } 912 913 static int rvin_csi2_setup_links(struct rvin_dev *vin) 914 { 915 const struct rvin_group_route *route; 916 unsigned int id; 917 int ret = -EINVAL; 918 919 /* Create all media device links between VINs and CSI-2's. */ 920 mutex_lock(&vin->group->lock); 921 for (route = vin->info->routes; route->chsel; route++) { 922 /* Check that VIN' master is part of the group. */ 923 if (!vin->group->vin[route->master]) 924 continue; 925 926 /* Check that CSI-2 is part of the group. */ 927 if (!vin->group->remotes[route->csi].subdev) 928 continue; 929 930 for (id = route->master; id < route->master + 4; id++) { 931 /* Check that VIN is part of the group. */ 932 if (!vin->group->vin[id]) 933 continue; 934 935 ret = rvin_csi2_create_link(vin->group, id, route); 936 if (ret) 937 goto out; 938 } 939 } 940 out: 941 mutex_unlock(&vin->group->lock); 942 943 return ret; 944 } 945 946 static void rvin_csi2_cleanup(struct rvin_dev *vin) 947 { 948 rvin_parallel_cleanup(vin); 949 rvin_group_notifier_cleanup(vin); 950 rvin_group_put(vin); 951 rvin_free_controls(vin); 952 } 953 954 static int rvin_csi2_init(struct rvin_dev *vin) 955 { 956 int ret; 957 958 vin->pad.flags = MEDIA_PAD_FL_SINK; 959 ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad); 960 if (ret) 961 return ret; 962 963 ret = rvin_create_controls(vin, NULL); 964 if (ret < 0) 965 return ret; 966 967 ret = rvin_group_get(vin, rvin_csi2_setup_links, &rvin_csi2_media_ops); 968 if (ret) 969 goto err_controls; 970 971 /* It's OK to not have a parallel subdevice. */ 972 ret = rvin_parallel_init(vin); 973 if (ret && ret != -ENODEV) 974 goto err_group; 975 976 ret = rvin_group_notifier_init(vin, 1, RVIN_CSI_MAX); 977 if (ret) 978 goto err_parallel; 979 980 return 0; 981 err_parallel: 982 rvin_parallel_cleanup(vin); 983 err_group: 984 rvin_group_put(vin); 985 err_controls: 986 rvin_free_controls(vin); 987 988 return ret; 989 } 990 991 /* ----------------------------------------------------------------------------- 992 * ISP 993 */ 994 995 static int rvin_isp_setup_links(struct rvin_dev *vin) 996 { 997 unsigned int i; 998 int ret = -EINVAL; 999 1000 /* Create all media device links between VINs and ISP's. */ 1001 mutex_lock(&vin->group->lock); 1002 for (i = 0; i < RCAR_VIN_NUM; i++) { 1003 struct media_pad *source_pad, *sink_pad; 1004 struct media_entity *source, *sink; 1005 unsigned int source_slot = i / 8; 1006 unsigned int source_idx = i % 8 + 1; 1007 1008 if (!vin->group->vin[i]) 1009 continue; 1010 1011 /* Check that ISP is part of the group. */ 1012 if (!vin->group->remotes[source_slot].subdev) 1013 continue; 1014 1015 source = &vin->group->remotes[source_slot].subdev->entity; 1016 source_pad = &source->pads[source_idx]; 1017 1018 sink = &vin->group->vin[i]->vdev.entity; 1019 sink_pad = &sink->pads[0]; 1020 1021 /* Skip if link already exists. */ 1022 if (media_entity_find_link(source_pad, sink_pad)) 1023 continue; 1024 1025 ret = media_create_pad_link(source, source_idx, sink, 0, 1026 MEDIA_LNK_FL_ENABLED | 1027 MEDIA_LNK_FL_IMMUTABLE); 1028 if (ret) { 1029 vin_err(vin, "Error adding link from %s to %s\n", 1030 source->name, sink->name); 1031 break; 1032 } 1033 } 1034 mutex_unlock(&vin->group->lock); 1035 1036 return ret; 1037 } 1038 1039 static void rvin_isp_cleanup(struct rvin_dev *vin) 1040 { 1041 rvin_group_notifier_cleanup(vin); 1042 rvin_group_put(vin); 1043 rvin_free_controls(vin); 1044 } 1045 1046 static int rvin_isp_init(struct rvin_dev *vin) 1047 { 1048 int ret; 1049 1050 vin->pad.flags = MEDIA_PAD_FL_SINK; 1051 ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad); 1052 if (ret) 1053 return ret; 1054 1055 ret = rvin_create_controls(vin, NULL); 1056 if (ret < 0) 1057 return ret; 1058 1059 ret = rvin_group_get(vin, rvin_isp_setup_links, NULL); 1060 if (ret) 1061 goto err_controls; 1062 1063 ret = rvin_group_notifier_init(vin, 2, RVIN_ISP_MAX); 1064 if (ret) 1065 goto err_group; 1066 1067 return 0; 1068 err_group: 1069 rvin_group_put(vin); 1070 err_controls: 1071 rvin_free_controls(vin); 1072 1073 return ret; 1074 } 1075 1076 /* ----------------------------------------------------------------------------- 1077 * Suspend / Resume 1078 */ 1079 1080 static int __maybe_unused rvin_suspend(struct device *dev) 1081 { 1082 struct rvin_dev *vin = dev_get_drvdata(dev); 1083 1084 if (vin->state != RUNNING) 1085 return 0; 1086 1087 rvin_stop_streaming(vin); 1088 1089 vin->state = SUSPENDED; 1090 1091 return 0; 1092 } 1093 1094 static int __maybe_unused rvin_resume(struct device *dev) 1095 { 1096 struct rvin_dev *vin = dev_get_drvdata(dev); 1097 1098 if (vin->state != SUSPENDED) 1099 return 0; 1100 1101 /* 1102 * Restore group master CHSEL setting. 1103 * 1104 * This needs to be done by every VIN resuming not only the master 1105 * as we don't know if and in which order the master VINs will 1106 * be resumed. 1107 */ 1108 if (vin->info->use_mc) { 1109 unsigned int master_id = rvin_group_id_to_master(vin->id); 1110 struct rvin_dev *master = vin->group->vin[master_id]; 1111 int ret; 1112 1113 if (WARN_ON(!master)) 1114 return -ENODEV; 1115 1116 ret = rvin_set_channel_routing(master, master->chsel); 1117 if (ret) 1118 return ret; 1119 } 1120 1121 return rvin_start_streaming(vin); 1122 } 1123 1124 /* ----------------------------------------------------------------------------- 1125 * Platform Device Driver 1126 */ 1127 1128 static const struct rvin_info rcar_info_h1 = { 1129 .model = RCAR_H1, 1130 .use_mc = false, 1131 .max_width = 2048, 1132 .max_height = 2048, 1133 .scaler = rvin_scaler_gen2, 1134 }; 1135 1136 static const struct rvin_info rcar_info_m1 = { 1137 .model = RCAR_M1, 1138 .use_mc = false, 1139 .max_width = 2048, 1140 .max_height = 2048, 1141 .scaler = rvin_scaler_gen2, 1142 }; 1143 1144 static const struct rvin_info rcar_info_gen2 = { 1145 .model = RCAR_GEN2, 1146 .use_mc = false, 1147 .max_width = 2048, 1148 .max_height = 2048, 1149 .scaler = rvin_scaler_gen2, 1150 }; 1151 1152 static const struct rvin_group_route rcar_info_r8a774e1_routes[] = { 1153 { .master = 0, .csi = RVIN_CSI20, .chsel = 0x04 }, 1154 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 }, 1155 { .master = 4, .csi = RVIN_CSI20, .chsel = 0x04 }, 1156 { /* Sentinel */ } 1157 }; 1158 1159 static const struct rvin_info rcar_info_r8a774e1 = { 1160 .model = RCAR_GEN3, 1161 .use_mc = true, 1162 .max_width = 4096, 1163 .max_height = 4096, 1164 .routes = rcar_info_r8a774e1_routes, 1165 }; 1166 1167 static const struct rvin_group_route rcar_info_r8a7795_routes[] = { 1168 { .master = 0, .csi = RVIN_CSI20, .chsel = 0x04 }, 1169 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 }, 1170 { .master = 4, .csi = RVIN_CSI20, .chsel = 0x04 }, 1171 { .master = 4, .csi = RVIN_CSI41, .chsel = 0x03 }, 1172 { /* Sentinel */ } 1173 }; 1174 1175 static const struct rvin_info rcar_info_r8a7795 = { 1176 .model = RCAR_GEN3, 1177 .use_mc = true, 1178 .nv12 = true, 1179 .max_width = 4096, 1180 .max_height = 4096, 1181 .routes = rcar_info_r8a7795_routes, 1182 .scaler = rvin_scaler_gen3, 1183 }; 1184 1185 static const struct rvin_group_route rcar_info_r8a7796_routes[] = { 1186 { .master = 0, .csi = RVIN_CSI20, .chsel = 0x04 }, 1187 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 }, 1188 { .master = 4, .csi = RVIN_CSI20, .chsel = 0x04 }, 1189 { .master = 4, .csi = RVIN_CSI40, .chsel = 0x03 }, 1190 { /* Sentinel */ } 1191 }; 1192 1193 static const struct rvin_info rcar_info_r8a7796 = { 1194 .model = RCAR_GEN3, 1195 .use_mc = true, 1196 .nv12 = true, 1197 .max_width = 4096, 1198 .max_height = 4096, 1199 .routes = rcar_info_r8a7796_routes, 1200 .scaler = rvin_scaler_gen3, 1201 }; 1202 1203 static const struct rvin_group_route rcar_info_r8a77965_routes[] = { 1204 { .master = 0, .csi = RVIN_CSI20, .chsel = 0x04 }, 1205 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 }, 1206 { .master = 4, .csi = RVIN_CSI20, .chsel = 0x04 }, 1207 { .master = 4, .csi = RVIN_CSI40, .chsel = 0x03 }, 1208 { /* Sentinel */ } 1209 }; 1210 1211 static const struct rvin_info rcar_info_r8a77965 = { 1212 .model = RCAR_GEN3, 1213 .use_mc = true, 1214 .nv12 = true, 1215 .max_width = 4096, 1216 .max_height = 4096, 1217 .routes = rcar_info_r8a77965_routes, 1218 .scaler = rvin_scaler_gen3, 1219 }; 1220 1221 static const struct rvin_group_route rcar_info_r8a77970_routes[] = { 1222 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 }, 1223 { /* Sentinel */ } 1224 }; 1225 1226 static const struct rvin_info rcar_info_r8a77970 = { 1227 .model = RCAR_GEN3, 1228 .use_mc = true, 1229 .max_width = 4096, 1230 .max_height = 4096, 1231 .routes = rcar_info_r8a77970_routes, 1232 }; 1233 1234 static const struct rvin_group_route rcar_info_r8a77980_routes[] = { 1235 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 }, 1236 { .master = 4, .csi = RVIN_CSI41, .chsel = 0x03 }, 1237 { /* Sentinel */ } 1238 }; 1239 1240 static const struct rvin_info rcar_info_r8a77980 = { 1241 .model = RCAR_GEN3, 1242 .use_mc = true, 1243 .nv12 = true, 1244 .max_width = 4096, 1245 .max_height = 4096, 1246 .routes = rcar_info_r8a77980_routes, 1247 }; 1248 1249 static const struct rvin_group_route rcar_info_r8a77990_routes[] = { 1250 { .master = 4, .csi = RVIN_CSI40, .chsel = 0x03 }, 1251 { /* Sentinel */ } 1252 }; 1253 1254 static const struct rvin_info rcar_info_r8a77990 = { 1255 .model = RCAR_GEN3, 1256 .use_mc = true, 1257 .nv12 = true, 1258 .max_width = 4096, 1259 .max_height = 4096, 1260 .routes = rcar_info_r8a77990_routes, 1261 .scaler = rvin_scaler_gen3, 1262 }; 1263 1264 static const struct rvin_group_route rcar_info_r8a77995_routes[] = { 1265 { /* Sentinel */ } 1266 }; 1267 1268 static const struct rvin_info rcar_info_r8a77995 = { 1269 .model = RCAR_GEN3, 1270 .use_mc = true, 1271 .nv12 = true, 1272 .max_width = 4096, 1273 .max_height = 4096, 1274 .routes = rcar_info_r8a77995_routes, 1275 .scaler = rvin_scaler_gen3, 1276 }; 1277 1278 static const struct rvin_info rcar_info_r8a779a0 = { 1279 .model = RCAR_GEN3, 1280 .use_mc = true, 1281 .use_isp = true, 1282 .nv12 = true, 1283 .max_width = 4096, 1284 .max_height = 4096, 1285 }; 1286 1287 static const struct rvin_info rcar_info_r8a779g0 = { 1288 .model = RCAR_GEN3, 1289 .use_mc = true, 1290 .use_isp = true, 1291 .nv12 = true, 1292 .max_width = 4096, 1293 .max_height = 4096, 1294 }; 1295 1296 static const struct of_device_id rvin_of_id_table[] = { 1297 { 1298 .compatible = "renesas,vin-r8a774a1", 1299 .data = &rcar_info_r8a7796, 1300 }, 1301 { 1302 .compatible = "renesas,vin-r8a774b1", 1303 .data = &rcar_info_r8a77965, 1304 }, 1305 { 1306 .compatible = "renesas,vin-r8a774c0", 1307 .data = &rcar_info_r8a77990, 1308 }, 1309 { 1310 .compatible = "renesas,vin-r8a774e1", 1311 .data = &rcar_info_r8a774e1, 1312 }, 1313 { 1314 .compatible = "renesas,vin-r8a7778", 1315 .data = &rcar_info_m1, 1316 }, 1317 { 1318 .compatible = "renesas,vin-r8a7779", 1319 .data = &rcar_info_h1, 1320 }, 1321 { 1322 .compatible = "renesas,rcar-gen2-vin", 1323 .data = &rcar_info_gen2, 1324 }, 1325 { 1326 .compatible = "renesas,vin-r8a7795", 1327 .data = &rcar_info_r8a7795, 1328 }, 1329 { 1330 .compatible = "renesas,vin-r8a7796", 1331 .data = &rcar_info_r8a7796, 1332 }, 1333 { 1334 .compatible = "renesas,vin-r8a77961", 1335 .data = &rcar_info_r8a7796, 1336 }, 1337 { 1338 .compatible = "renesas,vin-r8a77965", 1339 .data = &rcar_info_r8a77965, 1340 }, 1341 { 1342 .compatible = "renesas,vin-r8a77970", 1343 .data = &rcar_info_r8a77970, 1344 }, 1345 { 1346 .compatible = "renesas,vin-r8a77980", 1347 .data = &rcar_info_r8a77980, 1348 }, 1349 { 1350 .compatible = "renesas,vin-r8a77990", 1351 .data = &rcar_info_r8a77990, 1352 }, 1353 { 1354 .compatible = "renesas,vin-r8a77995", 1355 .data = &rcar_info_r8a77995, 1356 }, 1357 { 1358 .compatible = "renesas,vin-r8a779a0", 1359 .data = &rcar_info_r8a779a0, 1360 }, 1361 { 1362 .compatible = "renesas,vin-r8a779g0", 1363 .data = &rcar_info_r8a779g0, 1364 }, 1365 { /* Sentinel */ }, 1366 }; 1367 MODULE_DEVICE_TABLE(of, rvin_of_id_table); 1368 1369 static int rcar_vin_probe(struct platform_device *pdev) 1370 { 1371 struct rvin_dev *vin; 1372 int irq, ret; 1373 1374 vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL); 1375 if (!vin) 1376 return -ENOMEM; 1377 1378 vin->dev = &pdev->dev; 1379 vin->info = of_device_get_match_data(&pdev->dev); 1380 vin->alpha = 0xff; 1381 1382 vin->base = devm_platform_ioremap_resource(pdev, 0); 1383 if (IS_ERR(vin->base)) 1384 return PTR_ERR(vin->base); 1385 1386 irq = platform_get_irq(pdev, 0); 1387 if (irq < 0) 1388 return irq; 1389 1390 ret = rvin_dma_register(vin, irq); 1391 if (ret) 1392 return ret; 1393 1394 platform_set_drvdata(pdev, vin); 1395 1396 if (vin->info->use_isp) { 1397 ret = rvin_isp_init(vin); 1398 } else if (vin->info->use_mc) { 1399 ret = rvin_csi2_init(vin); 1400 1401 if (vin->info->scaler && 1402 rvin_group_id_to_master(vin->id) == vin->id) 1403 vin->scaler = vin->info->scaler; 1404 } else { 1405 ret = rvin_parallel_init(vin); 1406 1407 if (vin->info->scaler) 1408 vin->scaler = vin->info->scaler; 1409 } 1410 1411 if (ret) { 1412 rvin_dma_unregister(vin); 1413 return ret; 1414 } 1415 1416 pm_suspend_ignore_children(&pdev->dev, true); 1417 pm_runtime_enable(&pdev->dev); 1418 1419 return 0; 1420 } 1421 1422 static void rcar_vin_remove(struct platform_device *pdev) 1423 { 1424 struct rvin_dev *vin = platform_get_drvdata(pdev); 1425 1426 pm_runtime_disable(&pdev->dev); 1427 1428 rvin_v4l2_unregister(vin); 1429 1430 if (vin->info->use_isp) 1431 rvin_isp_cleanup(vin); 1432 else if (vin->info->use_mc) 1433 rvin_csi2_cleanup(vin); 1434 else 1435 rvin_parallel_cleanup(vin); 1436 1437 rvin_dma_unregister(vin); 1438 } 1439 1440 static SIMPLE_DEV_PM_OPS(rvin_pm_ops, rvin_suspend, rvin_resume); 1441 1442 static struct platform_driver rcar_vin_driver = { 1443 .driver = { 1444 .name = "rcar-vin", 1445 .suppress_bind_attrs = true, 1446 .pm = &rvin_pm_ops, 1447 .of_match_table = rvin_of_id_table, 1448 }, 1449 .probe = rcar_vin_probe, 1450 .remove_new = rcar_vin_remove, 1451 }; 1452 1453 module_platform_driver(rcar_vin_driver); 1454 1455 MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>"); 1456 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver"); 1457 MODULE_LICENSE("GPL"); 1458