1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Renesas R-Car VIN
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
10 * Based on the soc-camera rcar_vin driver
13 #include <linux/module.h>
15 #include <linux/of_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
20 #include <media/v4l2-async.h>
21 #include <media/v4l2-fwnode.h>
22 #include <media/v4l2-mc.h>
27 * The companion CSI-2 receiver driver (rcar-csi2) is known
28 * and we know it has one source pad (pad 0) and four sink
29 * pads (pad 1-4). So to translate a pad on the remote
30 * CSI-2 receiver to/from the VIN internal channel number simply
31 * subtract/add one from the pad/channel number.
33 #define rvin_group_csi_pad_to_channel(pad) ((pad) - 1)
34 #define rvin_group_csi_channel_to_pad(channel) ((channel) + 1)
37 * Not all VINs are created equal, master VINs control the
38 * routing for other VIN's. We can figure out which VIN is
39 * master by looking at a VINs id.
41 #define rvin_group_id_to_master(vin) ((vin) < 4 ? 0 : 4)
43 #define v4l2_dev_to_vin(d) container_of(d, struct rvin_dev, v4l2_dev)
45 /* -----------------------------------------------------------------------------
46 * Gen3 Group Allocator
49 /* FIXME: This should if we find a system that supports more
50 * than one group for the whole system be replaced with a linked
51 * list of groups. And eventually all of this should be replaced
52 * with a global device allocator API.
54 * But for now this works as on all supported systems there will
55 * be only one group for all instances.
58 static DEFINE_MUTEX(rvin_group_lock);
59 static struct rvin_group *rvin_group_data;
61 static void rvin_group_cleanup(struct rvin_group *group)
63 media_device_cleanup(&group->mdev);
64 mutex_destroy(&group->lock);
67 static int rvin_group_init(struct rvin_group *group, struct rvin_dev *vin,
68 int (*link_setup)(struct rvin_dev *),
69 const struct media_device_ops *ops)
71 struct media_device *mdev = &group->mdev;
72 const struct of_device_id *match;
73 struct device_node *np;
75 mutex_init(&group->lock);
77 /* Count number of VINs in the system */
79 for_each_matching_node(np, vin->dev->driver->of_match_table)
80 if (of_device_is_available(np))
83 vin_dbg(vin, "found %u enabled VIN's in DT", group->count);
85 group->link_setup = link_setup;
90 match = of_match_node(vin->dev->driver->of_match_table,
93 strscpy(mdev->driver_name, KBUILD_MODNAME, sizeof(mdev->driver_name));
94 strscpy(mdev->model, match->compatible, sizeof(mdev->model));
96 media_device_init(mdev);
101 static void rvin_group_release(struct kref *kref)
103 struct rvin_group *group =
104 container_of(kref, struct rvin_group, refcount);
106 mutex_lock(&rvin_group_lock);
108 rvin_group_data = NULL;
110 rvin_group_cleanup(group);
114 mutex_unlock(&rvin_group_lock);
117 static int rvin_group_get(struct rvin_dev *vin,
118 int (*link_setup)(struct rvin_dev *),
119 const struct media_device_ops *ops)
121 struct rvin_group *group;
125 /* Make sure VIN id is present and sane */
126 ret = of_property_read_u32(vin->dev->of_node, "renesas,id", &id);
128 vin_err(vin, "%pOF: No renesas,id property found\n",
133 if (id >= RCAR_VIN_NUM) {
134 vin_err(vin, "%pOF: Invalid renesas,id '%u'\n",
135 vin->dev->of_node, id);
139 /* Join or create a VIN group */
140 mutex_lock(&rvin_group_lock);
141 if (rvin_group_data) {
142 group = rvin_group_data;
143 kref_get(&group->refcount);
145 group = kzalloc(sizeof(*group), GFP_KERNEL);
151 ret = rvin_group_init(group, vin, link_setup, ops);
154 vin_err(vin, "Failed to initialize group\n");
158 kref_init(&group->refcount);
160 rvin_group_data = group;
162 mutex_unlock(&rvin_group_lock);
164 /* Add VIN to group */
165 mutex_lock(&group->lock);
167 if (group->vin[id]) {
168 vin_err(vin, "Duplicate renesas,id property value %u\n", id);
169 mutex_unlock(&group->lock);
170 kref_put(&group->refcount, rvin_group_release);
174 group->vin[id] = vin;
178 vin->v4l2_dev.mdev = &group->mdev;
180 mutex_unlock(&group->lock);
184 mutex_unlock(&rvin_group_lock);
188 static void rvin_group_put(struct rvin_dev *vin)
190 struct rvin_group *group = vin->group;
192 mutex_lock(&group->lock);
195 vin->v4l2_dev.mdev = NULL;
197 if (WARN_ON(group->vin[vin->id] != vin))
200 group->vin[vin->id] = NULL;
202 mutex_unlock(&group->lock);
204 kref_put(&group->refcount, rvin_group_release);
207 /* group lock should be held when calling this function. */
208 static int rvin_group_entity_to_remote_id(struct rvin_group *group,
209 struct media_entity *entity)
211 struct v4l2_subdev *sd;
214 sd = media_entity_to_v4l2_subdev(entity);
216 for (i = 0; i < RVIN_REMOTES_MAX; i++)
217 if (group->remotes[i].subdev == sd)
223 static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier)
225 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
229 ret = media_device_register(&vin->group->mdev);
233 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
235 vin_err(vin, "Failed to register subdev nodes\n");
239 /* Register all video nodes for the group. */
240 for (i = 0; i < RCAR_VIN_NUM; i++) {
241 if (vin->group->vin[i] &&
242 !video_is_registered(&vin->group->vin[i]->vdev)) {
243 ret = rvin_v4l2_register(vin->group->vin[i]);
249 return vin->group->link_setup(vin);
252 static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier,
253 struct v4l2_subdev *subdev,
254 struct v4l2_async_connection *asc)
256 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
259 for (i = 0; i < RCAR_VIN_NUM; i++)
260 if (vin->group->vin[i])
261 rvin_v4l2_unregister(vin->group->vin[i]);
263 mutex_lock(&vin->group->lock);
265 for (i = 0; i < RVIN_CSI_MAX; i++) {
266 if (vin->group->remotes[i].asc != asc)
268 vin->group->remotes[i].subdev = NULL;
269 vin_dbg(vin, "Unbind %s from slot %u\n", subdev->name, i);
273 mutex_unlock(&vin->group->lock);
275 media_device_unregister(&vin->group->mdev);
278 static int rvin_group_notify_bound(struct v4l2_async_notifier *notifier,
279 struct v4l2_subdev *subdev,
280 struct v4l2_async_connection *asc)
282 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
285 mutex_lock(&vin->group->lock);
287 for (i = 0; i < RVIN_CSI_MAX; i++) {
288 if (vin->group->remotes[i].asc != asc)
290 vin->group->remotes[i].subdev = subdev;
291 vin_dbg(vin, "Bound %s to slot %u\n", subdev->name, i);
295 mutex_unlock(&vin->group->lock);
300 static const struct v4l2_async_notifier_operations rvin_group_notify_ops = {
301 .bound = rvin_group_notify_bound,
302 .unbind = rvin_group_notify_unbind,
303 .complete = rvin_group_notify_complete,
306 static int rvin_group_parse_of(struct rvin_dev *vin, unsigned int port,
309 struct fwnode_handle *ep, *fwnode;
310 struct v4l2_fwnode_endpoint vep = {
311 .bus_type = V4L2_MBUS_CSI2_DPHY,
313 struct v4l2_async_connection *asc;
316 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(vin->dev), port, id, 0);
320 fwnode = fwnode_graph_get_remote_endpoint(ep);
321 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
322 fwnode_handle_put(ep);
324 vin_err(vin, "Failed to parse %pOF\n", to_of_node(fwnode));
329 asc = v4l2_async_nf_add_fwnode(&vin->group->notifier, fwnode,
330 struct v4l2_async_connection);
336 vin->group->remotes[vep.base.id].asc = asc;
338 vin_dbg(vin, "Add group OF device %pOF to slot %u\n",
339 to_of_node(fwnode), vep.base.id);
341 fwnode_handle_put(fwnode);
346 static void rvin_group_notifier_cleanup(struct rvin_dev *vin)
348 if (&vin->v4l2_dev == vin->group->notifier.v4l2_dev) {
349 v4l2_async_nf_unregister(&vin->group->notifier);
350 v4l2_async_nf_cleanup(&vin->group->notifier);
354 static int rvin_group_notifier_init(struct rvin_dev *vin, unsigned int port,
357 unsigned int count = 0, vin_mask = 0;
361 mutex_lock(&vin->group->lock);
363 /* If not all VIN's are registered don't register the notifier. */
364 for (i = 0; i < RCAR_VIN_NUM; i++) {
365 if (vin->group->vin[i]) {
371 if (vin->group->count != count) {
372 mutex_unlock(&vin->group->lock);
376 mutex_unlock(&vin->group->lock);
378 v4l2_async_nf_init(&vin->group->notifier, &vin->v4l2_dev);
381 * Some subdevices may overlap but the parser function can handle it and
382 * each subdevice will only be registered once with the group notifier.
384 for (i = 0; i < RCAR_VIN_NUM; i++) {
385 if (!(vin_mask & BIT(i)))
388 for (id = 0; id < max_id; id++) {
389 if (vin->group->remotes[id].asc)
392 ret = rvin_group_parse_of(vin->group->vin[i], port, id);
398 if (list_empty(&vin->group->notifier.waiting_list))
401 vin->group->notifier.ops = &rvin_group_notify_ops;
402 ret = v4l2_async_nf_register(&vin->group->notifier);
404 vin_err(vin, "Notifier registration failed\n");
405 v4l2_async_nf_cleanup(&vin->group->notifier);
412 /* -----------------------------------------------------------------------------
416 static int rvin_s_ctrl(struct v4l2_ctrl *ctrl)
418 struct rvin_dev *vin =
419 container_of(ctrl->handler, struct rvin_dev, ctrl_handler);
422 case V4L2_CID_ALPHA_COMPONENT:
423 rvin_set_alpha(vin, ctrl->val);
430 static const struct v4l2_ctrl_ops rvin_ctrl_ops = {
431 .s_ctrl = rvin_s_ctrl,
434 static void rvin_free_controls(struct rvin_dev *vin)
436 v4l2_ctrl_handler_free(&vin->ctrl_handler);
437 vin->vdev.ctrl_handler = NULL;
440 static int rvin_create_controls(struct rvin_dev *vin, struct v4l2_subdev *subdev)
444 ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 16);
448 /* The VIN directly deals with alpha component. */
449 v4l2_ctrl_new_std(&vin->ctrl_handler, &rvin_ctrl_ops,
450 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
452 if (vin->ctrl_handler.error) {
453 ret = vin->ctrl_handler.error;
454 rvin_free_controls(vin);
458 /* For the non-MC mode add controls from the subdevice. */
460 ret = v4l2_ctrl_add_handler(&vin->ctrl_handler,
461 subdev->ctrl_handler, NULL, true);
463 rvin_free_controls(vin);
468 vin->vdev.ctrl_handler = &vin->ctrl_handler;
473 /* -----------------------------------------------------------------------------
477 static int rvin_find_pad(struct v4l2_subdev *sd, int direction)
481 if (sd->entity.num_pads <= 1)
484 for (pad = 0; pad < sd->entity.num_pads; pad++)
485 if (sd->entity.pads[pad].flags & direction)
491 /* -----------------------------------------------------------------------------
492 * Parallel async notifier
495 /* The vin lock should be held when calling the subdevice attach and detach */
496 static int rvin_parallel_subdevice_attach(struct rvin_dev *vin,
497 struct v4l2_subdev *subdev)
499 struct v4l2_subdev_mbus_code_enum code = {
500 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
504 /* Find source and sink pad of remote subdevice */
505 ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
508 vin->parallel.source_pad = ret;
510 ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
511 vin->parallel.sink_pad = ret < 0 ? 0 : ret;
513 if (vin->info->use_mc) {
514 vin->parallel.subdev = subdev;
518 /* Find compatible subdevices mbus format */
521 code.pad = vin->parallel.source_pad;
522 while (!vin->mbus_code &&
523 !v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
526 case MEDIA_BUS_FMT_YUYV8_1X16:
527 case MEDIA_BUS_FMT_UYVY8_1X16:
528 case MEDIA_BUS_FMT_UYVY8_2X8:
529 case MEDIA_BUS_FMT_UYVY10_2X10:
530 case MEDIA_BUS_FMT_RGB888_1X24:
531 vin->mbus_code = code.code;
532 vin_dbg(vin, "Found media bus format for %s: %d\n",
533 subdev->name, vin->mbus_code);
540 if (!vin->mbus_code) {
541 vin_err(vin, "Unsupported media bus format for %s\n",
547 ret = v4l2_subdev_call(subdev, video, g_tvnorms, &vin->vdev.tvnorms);
548 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
552 vin->std = V4L2_STD_UNKNOWN;
553 ret = v4l2_subdev_call(subdev, video, g_std, &vin->std);
554 if (ret < 0 && ret != -ENOIOCTLCMD)
557 /* Add the controls */
558 ret = rvin_create_controls(vin, subdev);
562 vin->parallel.subdev = subdev;
567 static void rvin_parallel_subdevice_detach(struct rvin_dev *vin)
569 rvin_v4l2_unregister(vin);
570 vin->parallel.subdev = NULL;
572 if (!vin->info->use_mc)
573 rvin_free_controls(vin);
576 static int rvin_parallel_notify_complete(struct v4l2_async_notifier *notifier)
578 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
579 struct media_entity *source;
580 struct media_entity *sink;
583 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
585 vin_err(vin, "Failed to register subdev nodes\n");
589 if (!video_is_registered(&vin->vdev)) {
590 ret = rvin_v4l2_register(vin);
595 if (!vin->info->use_mc)
598 /* If we're running with media-controller, link the subdevs. */
599 source = &vin->parallel.subdev->entity;
600 sink = &vin->vdev.entity;
602 ret = media_create_pad_link(source, vin->parallel.source_pad,
603 sink, vin->parallel.sink_pad, 0);
605 vin_err(vin, "Error adding link from %s to %s: %d\n",
606 source->name, sink->name, ret);
611 static void rvin_parallel_notify_unbind(struct v4l2_async_notifier *notifier,
612 struct v4l2_subdev *subdev,
613 struct v4l2_async_connection *asc)
615 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
617 vin_dbg(vin, "unbind parallel subdev %s\n", subdev->name);
619 mutex_lock(&vin->lock);
620 rvin_parallel_subdevice_detach(vin);
621 mutex_unlock(&vin->lock);
624 static int rvin_parallel_notify_bound(struct v4l2_async_notifier *notifier,
625 struct v4l2_subdev *subdev,
626 struct v4l2_async_connection *asc)
628 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
631 mutex_lock(&vin->lock);
632 ret = rvin_parallel_subdevice_attach(vin, subdev);
633 mutex_unlock(&vin->lock);
637 v4l2_set_subdev_hostdata(subdev, vin);
639 vin_dbg(vin, "bound subdev %s source pad: %u sink pad: %u\n",
640 subdev->name, vin->parallel.source_pad,
641 vin->parallel.sink_pad);
646 static const struct v4l2_async_notifier_operations rvin_parallel_notify_ops = {
647 .bound = rvin_parallel_notify_bound,
648 .unbind = rvin_parallel_notify_unbind,
649 .complete = rvin_parallel_notify_complete,
652 static int rvin_parallel_parse_of(struct rvin_dev *vin)
654 struct fwnode_handle *ep, *fwnode;
655 struct v4l2_fwnode_endpoint vep = {
656 .bus_type = V4L2_MBUS_UNKNOWN,
658 struct v4l2_async_connection *asc;
661 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(vin->dev), 0, 0, 0);
665 fwnode = fwnode_graph_get_remote_endpoint(ep);
666 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
667 fwnode_handle_put(ep);
669 vin_err(vin, "Failed to parse %pOF\n", to_of_node(fwnode));
674 switch (vep.bus_type) {
675 case V4L2_MBUS_PARALLEL:
676 case V4L2_MBUS_BT656:
677 vin_dbg(vin, "Found %s media bus\n",
678 vep.bus_type == V4L2_MBUS_PARALLEL ?
679 "PARALLEL" : "BT656");
680 vin->parallel.mbus_type = vep.bus_type;
681 vin->parallel.bus = vep.bus.parallel;
684 vin_err(vin, "Unknown media bus type\n");
689 asc = v4l2_async_nf_add_fwnode(&vin->notifier, fwnode,
690 struct v4l2_async_connection);
696 vin->parallel.asc = asc;
698 vin_dbg(vin, "Add parallel OF device %pOF\n", to_of_node(fwnode));
700 fwnode_handle_put(fwnode);
705 static void rvin_parallel_cleanup(struct rvin_dev *vin)
707 v4l2_async_nf_unregister(&vin->notifier);
708 v4l2_async_nf_cleanup(&vin->notifier);
711 static int rvin_parallel_init(struct rvin_dev *vin)
715 v4l2_async_nf_init(&vin->notifier, &vin->v4l2_dev);
717 ret = rvin_parallel_parse_of(vin);
721 if (!vin->parallel.asc)
724 vin_dbg(vin, "Found parallel subdevice %pOF\n",
725 to_of_node(vin->parallel.asc->match.fwnode));
727 vin->notifier.ops = &rvin_parallel_notify_ops;
728 ret = v4l2_async_nf_register(&vin->notifier);
730 vin_err(vin, "Notifier registration failed\n");
731 v4l2_async_nf_cleanup(&vin->notifier);
738 /* -----------------------------------------------------------------------------
743 * Link setup for the links between a VIN and a CSI-2 receiver is a bit
744 * complex. The reason for this is that the register controlling routing
745 * is not present in each VIN instance. There are special VINs which
746 * control routing for themselves and other VINs. There are not many
747 * different possible links combinations that can be enabled at the same
748 * time, therefor all already enabled links which are controlled by a
749 * master VIN need to be taken into account when making the decision
750 * if a new link can be enabled or not.
752 * 1. Find out which VIN the link the user tries to enable is connected to.
753 * 2. Lookup which master VIN controls the links for this VIN.
754 * 3. Start with a bitmask with all bits set.
755 * 4. For each previously enabled link from the master VIN bitwise AND its
756 * route mask (see documentation for mask in struct rvin_group_route)
758 * 5. Bitwise AND the mask for the link the user tries to enable to the bitmask.
759 * 6. If the bitmask is not empty at this point the new link can be enabled
760 * while keeping all previous links enabled. Update the CHSEL value of the
761 * master VIN and inform the user that the link could be enabled.
763 * Please note that no link can be enabled if any VIN in the group is
766 static int rvin_csi2_link_notify(struct media_link *link, u32 flags,
767 unsigned int notification)
769 struct rvin_group *group = container_of(link->graph_obj.mdev,
770 struct rvin_group, mdev);
771 struct media_entity *entity;
772 struct video_device *vdev;
773 struct rvin_dev *vin;
777 ret = v4l2_pipeline_link_notify(link, flags, notification);
781 /* Only care about link enablement for VIN nodes. */
782 if (!(flags & MEDIA_LNK_FL_ENABLED) ||
783 !is_media_entity_v4l2_video_device(link->sink->entity))
787 * Don't allow link changes if any stream in the graph is active as
788 * modifying the CHSEL register fields can disrupt running streams.
790 media_device_for_each_entity(entity, &group->mdev)
791 if (media_entity_is_streaming(entity))
794 /* Find the master VIN that controls the routes. */
795 vdev = media_entity_to_video_device(link->sink->entity);
796 vin = container_of(vdev, struct rvin_dev, vdev);
798 mutex_lock(&group->lock);
800 csi_id = rvin_group_entity_to_remote_id(group, link->source->entity);
801 if (csi_id == -ENODEV) {
802 struct v4l2_subdev *sd;
805 * Make sure the source entity subdevice is registered as
806 * a parallel input of one of the enabled VINs if it is not
807 * one of the CSI-2 subdevices.
809 * No hardware configuration required for parallel inputs,
810 * we can return here.
812 sd = media_entity_to_v4l2_subdev(link->source->entity);
813 for (i = 0; i < RCAR_VIN_NUM; i++) {
815 group->vin[i]->parallel.subdev == sd) {
816 group->vin[i]->is_csi = false;
822 vin_err(vin, "Subdevice %s not registered to any VIN\n",
823 link->source->entity->name);
826 const struct rvin_group_route *route;
827 unsigned int chsel = UINT_MAX;
828 unsigned int master_id;
830 master_id = rvin_group_id_to_master(vin->id);
832 if (WARN_ON(!group->vin[master_id])) {
837 /* Make sure group is connected to same CSI-2 */
838 for (i = master_id; i < master_id + 4; i++) {
839 struct media_pad *csi_pad;
844 /* Get remote CSI-2, if any. */
845 csi_pad = media_pad_remote_pad_first(
846 &group->vin[i]->vdev.entity.pads[0]);
850 if (csi_pad->entity != link->source->entity) {
851 vin_dbg(vin, "Already attached to %s\n",
852 csi_pad->entity->name);
858 for (route = vin->info->routes; route->chsel; route++) {
859 if (route->master == master_id && route->csi == csi_id) {
860 chsel = route->chsel;
865 if (chsel == UINT_MAX) {
866 vin_err(vin, "No CHSEL value found\n");
871 ret = rvin_set_channel_routing(group->vin[master_id], chsel);
878 mutex_unlock(&group->lock);
883 static const struct media_device_ops rvin_csi2_media_ops = {
884 .link_notify = rvin_csi2_link_notify,
887 static int rvin_csi2_create_link(struct rvin_group *group, unsigned int id,
888 const struct rvin_group_route *route)
890 struct media_entity *source = &group->remotes[route->csi].subdev->entity;
891 struct media_entity *sink = &group->vin[id]->vdev.entity;
892 struct media_pad *sink_pad = &sink->pads[0];
893 unsigned int channel;
896 for (channel = 0; channel < 4; channel++) {
897 unsigned int source_idx = rvin_group_csi_channel_to_pad(channel);
898 struct media_pad *source_pad = &source->pads[source_idx];
900 /* Skip if link already exists. */
901 if (media_entity_find_link(source_pad, sink_pad))
904 ret = media_create_pad_link(source, source_idx, sink, 0, 0);
912 static int rvin_csi2_setup_links(struct rvin_dev *vin)
914 const struct rvin_group_route *route;
918 /* Create all media device links between VINs and CSI-2's. */
919 mutex_lock(&vin->group->lock);
920 for (route = vin->info->routes; route->chsel; route++) {
921 /* Check that VIN' master is part of the group. */
922 if (!vin->group->vin[route->master])
925 /* Check that CSI-2 is part of the group. */
926 if (!vin->group->remotes[route->csi].subdev)
929 for (id = route->master; id < route->master + 4; id++) {
930 /* Check that VIN is part of the group. */
931 if (!vin->group->vin[id])
934 ret = rvin_csi2_create_link(vin->group, id, route);
940 mutex_unlock(&vin->group->lock);
945 static void rvin_csi2_cleanup(struct rvin_dev *vin)
947 rvin_parallel_cleanup(vin);
948 rvin_group_notifier_cleanup(vin);
950 rvin_free_controls(vin);
953 static int rvin_csi2_init(struct rvin_dev *vin)
957 vin->pad.flags = MEDIA_PAD_FL_SINK;
958 ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
962 ret = rvin_create_controls(vin, NULL);
966 ret = rvin_group_get(vin, rvin_csi2_setup_links, &rvin_csi2_media_ops);
970 /* It's OK to not have a parallel subdevice. */
971 ret = rvin_parallel_init(vin);
972 if (ret && ret != -ENODEV)
975 ret = rvin_group_notifier_init(vin, 1, RVIN_CSI_MAX);
981 rvin_parallel_cleanup(vin);
985 rvin_free_controls(vin);
990 /* -----------------------------------------------------------------------------
994 static int rvin_isp_setup_links(struct rvin_dev *vin)
999 /* Create all media device links between VINs and ISP's. */
1000 mutex_lock(&vin->group->lock);
1001 for (i = 0; i < RCAR_VIN_NUM; i++) {
1002 struct media_pad *source_pad, *sink_pad;
1003 struct media_entity *source, *sink;
1004 unsigned int source_slot = i / 8;
1005 unsigned int source_idx = i % 8 + 1;
1007 if (!vin->group->vin[i])
1010 /* Check that ISP is part of the group. */
1011 if (!vin->group->remotes[source_slot].subdev)
1014 source = &vin->group->remotes[source_slot].subdev->entity;
1015 source_pad = &source->pads[source_idx];
1017 sink = &vin->group->vin[i]->vdev.entity;
1018 sink_pad = &sink->pads[0];
1020 /* Skip if link already exists. */
1021 if (media_entity_find_link(source_pad, sink_pad))
1024 ret = media_create_pad_link(source, source_idx, sink, 0,
1025 MEDIA_LNK_FL_ENABLED |
1026 MEDIA_LNK_FL_IMMUTABLE);
1028 vin_err(vin, "Error adding link from %s to %s\n",
1029 source->name, sink->name);
1033 mutex_unlock(&vin->group->lock);
1038 static void rvin_isp_cleanup(struct rvin_dev *vin)
1040 rvin_group_notifier_cleanup(vin);
1041 rvin_group_put(vin);
1042 rvin_free_controls(vin);
1045 static int rvin_isp_init(struct rvin_dev *vin)
1049 vin->pad.flags = MEDIA_PAD_FL_SINK;
1050 ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
1054 ret = rvin_create_controls(vin, NULL);
1058 ret = rvin_group_get(vin, rvin_isp_setup_links, NULL);
1062 ret = rvin_group_notifier_init(vin, 2, RVIN_ISP_MAX);
1068 rvin_group_put(vin);
1070 rvin_free_controls(vin);
1075 /* -----------------------------------------------------------------------------
1079 static int __maybe_unused rvin_suspend(struct device *dev)
1081 struct rvin_dev *vin = dev_get_drvdata(dev);
1083 if (vin->state != RUNNING)
1086 rvin_stop_streaming(vin);
1088 vin->state = SUSPENDED;
1093 static int __maybe_unused rvin_resume(struct device *dev)
1095 struct rvin_dev *vin = dev_get_drvdata(dev);
1097 if (vin->state != SUSPENDED)
1101 * Restore group master CHSEL setting.
1103 * This needs to be done by every VIN resuming not only the master
1104 * as we don't know if and in which order the master VINs will
1107 if (vin->info->use_mc) {
1108 unsigned int master_id = rvin_group_id_to_master(vin->id);
1109 struct rvin_dev *master = vin->group->vin[master_id];
1112 if (WARN_ON(!master))
1115 ret = rvin_set_channel_routing(master, master->chsel);
1120 return rvin_start_streaming(vin);
1123 /* -----------------------------------------------------------------------------
1124 * Platform Device Driver
1127 static const struct rvin_info rcar_info_h1 = {
1132 .scaler = rvin_scaler_gen2,
1135 static const struct rvin_info rcar_info_m1 = {
1140 .scaler = rvin_scaler_gen2,
1143 static const struct rvin_info rcar_info_gen2 = {
1148 .scaler = rvin_scaler_gen2,
1151 static const struct rvin_group_route rcar_info_r8a774e1_routes[] = {
1152 { .master = 0, .csi = RVIN_CSI20, .chsel = 0x04 },
1153 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 },
1154 { .master = 4, .csi = RVIN_CSI20, .chsel = 0x04 },
1158 static const struct rvin_info rcar_info_r8a774e1 = {
1163 .routes = rcar_info_r8a774e1_routes,
1166 static const struct rvin_group_route rcar_info_r8a7795_routes[] = {
1167 { .master = 0, .csi = RVIN_CSI20, .chsel = 0x04 },
1168 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 },
1169 { .master = 4, .csi = RVIN_CSI20, .chsel = 0x04 },
1170 { .master = 4, .csi = RVIN_CSI41, .chsel = 0x03 },
1174 static const struct rvin_info rcar_info_r8a7795 = {
1180 .routes = rcar_info_r8a7795_routes,
1181 .scaler = rvin_scaler_gen3,
1184 static const struct rvin_group_route rcar_info_r8a7796_routes[] = {
1185 { .master = 0, .csi = RVIN_CSI20, .chsel = 0x04 },
1186 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 },
1187 { .master = 4, .csi = RVIN_CSI20, .chsel = 0x04 },
1188 { .master = 4, .csi = RVIN_CSI40, .chsel = 0x03 },
1192 static const struct rvin_info rcar_info_r8a7796 = {
1198 .routes = rcar_info_r8a7796_routes,
1199 .scaler = rvin_scaler_gen3,
1202 static const struct rvin_group_route rcar_info_r8a77965_routes[] = {
1203 { .master = 0, .csi = RVIN_CSI20, .chsel = 0x04 },
1204 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 },
1205 { .master = 4, .csi = RVIN_CSI20, .chsel = 0x04 },
1206 { .master = 4, .csi = RVIN_CSI40, .chsel = 0x03 },
1210 static const struct rvin_info rcar_info_r8a77965 = {
1216 .routes = rcar_info_r8a77965_routes,
1217 .scaler = rvin_scaler_gen3,
1220 static const struct rvin_group_route rcar_info_r8a77970_routes[] = {
1221 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 },
1225 static const struct rvin_info rcar_info_r8a77970 = {
1230 .routes = rcar_info_r8a77970_routes,
1233 static const struct rvin_group_route rcar_info_r8a77980_routes[] = {
1234 { .master = 0, .csi = RVIN_CSI40, .chsel = 0x03 },
1235 { .master = 4, .csi = RVIN_CSI41, .chsel = 0x03 },
1239 static const struct rvin_info rcar_info_r8a77980 = {
1245 .routes = rcar_info_r8a77980_routes,
1248 static const struct rvin_group_route rcar_info_r8a77990_routes[] = {
1249 { .master = 4, .csi = RVIN_CSI40, .chsel = 0x03 },
1253 static const struct rvin_info rcar_info_r8a77990 = {
1259 .routes = rcar_info_r8a77990_routes,
1260 .scaler = rvin_scaler_gen3,
1263 static const struct rvin_group_route rcar_info_r8a77995_routes[] = {
1267 static const struct rvin_info rcar_info_r8a77995 = {
1273 .routes = rcar_info_r8a77995_routes,
1274 .scaler = rvin_scaler_gen3,
1277 static const struct rvin_info rcar_info_r8a779a0 = {
1286 static const struct rvin_info rcar_info_r8a779g0 = {
1295 static const struct of_device_id rvin_of_id_table[] = {
1297 .compatible = "renesas,vin-r8a774a1",
1298 .data = &rcar_info_r8a7796,
1301 .compatible = "renesas,vin-r8a774b1",
1302 .data = &rcar_info_r8a77965,
1305 .compatible = "renesas,vin-r8a774c0",
1306 .data = &rcar_info_r8a77990,
1309 .compatible = "renesas,vin-r8a774e1",
1310 .data = &rcar_info_r8a774e1,
1313 .compatible = "renesas,vin-r8a7778",
1314 .data = &rcar_info_m1,
1317 .compatible = "renesas,vin-r8a7779",
1318 .data = &rcar_info_h1,
1321 .compatible = "renesas,rcar-gen2-vin",
1322 .data = &rcar_info_gen2,
1325 .compatible = "renesas,vin-r8a7795",
1326 .data = &rcar_info_r8a7795,
1329 .compatible = "renesas,vin-r8a7796",
1330 .data = &rcar_info_r8a7796,
1333 .compatible = "renesas,vin-r8a77961",
1334 .data = &rcar_info_r8a7796,
1337 .compatible = "renesas,vin-r8a77965",
1338 .data = &rcar_info_r8a77965,
1341 .compatible = "renesas,vin-r8a77970",
1342 .data = &rcar_info_r8a77970,
1345 .compatible = "renesas,vin-r8a77980",
1346 .data = &rcar_info_r8a77980,
1349 .compatible = "renesas,vin-r8a77990",
1350 .data = &rcar_info_r8a77990,
1353 .compatible = "renesas,vin-r8a77995",
1354 .data = &rcar_info_r8a77995,
1357 .compatible = "renesas,vin-r8a779a0",
1358 .data = &rcar_info_r8a779a0,
1361 .compatible = "renesas,vin-r8a779g0",
1362 .data = &rcar_info_r8a779g0,
1366 MODULE_DEVICE_TABLE(of, rvin_of_id_table);
1368 static int rcar_vin_probe(struct platform_device *pdev)
1370 struct rvin_dev *vin;
1373 vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
1377 vin->dev = &pdev->dev;
1378 vin->info = of_device_get_match_data(&pdev->dev);
1381 vin->base = devm_platform_ioremap_resource(pdev, 0);
1382 if (IS_ERR(vin->base))
1383 return PTR_ERR(vin->base);
1385 irq = platform_get_irq(pdev, 0);
1389 ret = rvin_dma_register(vin, irq);
1393 platform_set_drvdata(pdev, vin);
1395 if (vin->info->use_isp) {
1396 ret = rvin_isp_init(vin);
1397 } else if (vin->info->use_mc) {
1398 ret = rvin_csi2_init(vin);
1400 if (vin->info->scaler &&
1401 rvin_group_id_to_master(vin->id) == vin->id)
1402 vin->scaler = vin->info->scaler;
1404 ret = rvin_parallel_init(vin);
1406 if (vin->info->scaler)
1407 vin->scaler = vin->info->scaler;
1411 rvin_dma_unregister(vin);
1415 pm_suspend_ignore_children(&pdev->dev, true);
1416 pm_runtime_enable(&pdev->dev);
1421 static void rcar_vin_remove(struct platform_device *pdev)
1423 struct rvin_dev *vin = platform_get_drvdata(pdev);
1425 pm_runtime_disable(&pdev->dev);
1427 rvin_v4l2_unregister(vin);
1429 if (vin->info->use_isp)
1430 rvin_isp_cleanup(vin);
1431 else if (vin->info->use_mc)
1432 rvin_csi2_cleanup(vin);
1434 rvin_parallel_cleanup(vin);
1436 rvin_dma_unregister(vin);
1439 static SIMPLE_DEV_PM_OPS(rvin_pm_ops, rvin_suspend, rvin_resume);
1441 static struct platform_driver rcar_vin_driver = {
1444 .suppress_bind_attrs = true,
1446 .of_match_table = rvin_of_id_table,
1448 .probe = rcar_vin_probe,
1449 .remove_new = rcar_vin_remove,
1452 module_platform_driver(rcar_vin_driver);
1454 MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
1455 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");
1456 MODULE_LICENSE("GPL");