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_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 #include <linux/sys_soc.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-fwnode.h>
24 #include <media/v4l2-mc.h>
29 * The companion CSI-2 receiver driver (rcar-csi2) is known
30 * and we know it has one source pad (pad 0) and four sink
31 * pads (pad 1-4). So to translate a pad on the remote
32 * CSI-2 receiver to/from the VIN internal channel number simply
33 * subtract/add one from the pad/channel number.
35 #define rvin_group_csi_pad_to_channel(pad) ((pad) - 1)
36 #define rvin_group_csi_channel_to_pad(channel) ((channel) + 1)
39 * Not all VINs are created equal, master VINs control the
40 * routing for other VIN's. We can figure out which VIN is
41 * master by looking at a VINs id.
43 #define rvin_group_id_to_master(vin) ((vin) < 4 ? 0 : 4)
45 #define v4l2_dev_to_vin(d) container_of(d, struct rvin_dev, v4l2_dev)
47 /* -----------------------------------------------------------------------------
48 * Gen3 Group Allocator
51 /* FIXME: This should if we find a system that supports more
52 * than one group for the whole system be replaced with a linked
53 * list of groups. And eventually all of this should be replaced
54 * with a global device allocator API.
56 * But for now this works as on all supported systems there will
57 * be only one group for all instances.
60 static DEFINE_MUTEX(rvin_group_lock);
61 static struct rvin_group *rvin_group_data;
63 static void rvin_group_cleanup(struct rvin_group *group)
65 media_device_cleanup(&group->mdev);
66 mutex_destroy(&group->lock);
69 static int rvin_group_init(struct rvin_group *group, struct rvin_dev *vin,
70 int (*link_setup)(struct rvin_dev *),
71 const struct media_device_ops *ops)
73 struct media_device *mdev = &group->mdev;
74 const struct of_device_id *match;
75 struct device_node *np;
77 mutex_init(&group->lock);
79 /* Count number of VINs in the system */
81 for_each_matching_node(np, vin->dev->driver->of_match_table)
82 if (of_device_is_available(np))
85 vin_dbg(vin, "found %u enabled VIN's in DT", group->count);
87 group->link_setup = link_setup;
92 match = of_match_node(vin->dev->driver->of_match_table,
95 strscpy(mdev->driver_name, KBUILD_MODNAME, sizeof(mdev->driver_name));
96 strscpy(mdev->model, match->compatible, sizeof(mdev->model));
97 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
100 media_device_init(mdev);
105 static void rvin_group_release(struct kref *kref)
107 struct rvin_group *group =
108 container_of(kref, struct rvin_group, refcount);
110 mutex_lock(&rvin_group_lock);
112 rvin_group_data = NULL;
114 rvin_group_cleanup(group);
118 mutex_unlock(&rvin_group_lock);
121 static int rvin_group_get(struct rvin_dev *vin,
122 int (*link_setup)(struct rvin_dev *),
123 const struct media_device_ops *ops)
125 struct rvin_group *group;
129 /* Make sure VIN id is present and sane */
130 ret = of_property_read_u32(vin->dev->of_node, "renesas,id", &id);
132 vin_err(vin, "%pOF: No renesas,id property found\n",
137 if (id >= RCAR_VIN_NUM) {
138 vin_err(vin, "%pOF: Invalid renesas,id '%u'\n",
139 vin->dev->of_node, id);
143 /* Join or create a VIN group */
144 mutex_lock(&rvin_group_lock);
145 if (rvin_group_data) {
146 group = rvin_group_data;
147 kref_get(&group->refcount);
149 group = kzalloc(sizeof(*group), GFP_KERNEL);
155 ret = rvin_group_init(group, vin, link_setup, ops);
158 vin_err(vin, "Failed to initialize group\n");
162 kref_init(&group->refcount);
164 rvin_group_data = group;
166 mutex_unlock(&rvin_group_lock);
168 /* Add VIN to group */
169 mutex_lock(&group->lock);
171 if (group->vin[id]) {
172 vin_err(vin, "Duplicate renesas,id property value %u\n", id);
173 mutex_unlock(&group->lock);
174 kref_put(&group->refcount, rvin_group_release);
178 group->vin[id] = vin;
182 vin->v4l2_dev.mdev = &group->mdev;
184 mutex_unlock(&group->lock);
188 mutex_unlock(&rvin_group_lock);
192 static void rvin_group_put(struct rvin_dev *vin)
194 struct rvin_group *group = vin->group;
196 mutex_lock(&group->lock);
199 vin->v4l2_dev.mdev = NULL;
201 if (WARN_ON(group->vin[vin->id] != vin))
204 group->vin[vin->id] = NULL;
206 mutex_unlock(&group->lock);
208 kref_put(&group->refcount, rvin_group_release);
211 /* group lock should be held when calling this function. */
212 static int rvin_group_entity_to_remote_id(struct rvin_group *group,
213 struct media_entity *entity)
215 struct v4l2_subdev *sd;
218 sd = media_entity_to_v4l2_subdev(entity);
220 for (i = 0; i < RVIN_REMOTES_MAX; i++)
221 if (group->remotes[i].subdev == sd)
227 static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier)
229 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
233 ret = media_device_register(&vin->group->mdev);
237 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
239 vin_err(vin, "Failed to register subdev nodes\n");
243 /* Register all video nodes for the group. */
244 for (i = 0; i < RCAR_VIN_NUM; i++) {
245 if (vin->group->vin[i] &&
246 !video_is_registered(&vin->group->vin[i]->vdev)) {
247 ret = rvin_v4l2_register(vin->group->vin[i]);
253 return vin->group->link_setup(vin);
256 static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier,
257 struct v4l2_subdev *subdev,
258 struct v4l2_async_subdev *asd)
260 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
263 for (i = 0; i < RCAR_VIN_NUM; i++)
264 if (vin->group->vin[i])
265 rvin_v4l2_unregister(vin->group->vin[i]);
267 mutex_lock(&vin->group->lock);
269 for (i = 0; i < RVIN_CSI_MAX; i++) {
270 if (vin->group->remotes[i].asd != asd)
272 vin->group->remotes[i].subdev = NULL;
273 vin_dbg(vin, "Unbind %s from slot %u\n", subdev->name, i);
277 mutex_unlock(&vin->group->lock);
279 media_device_unregister(&vin->group->mdev);
282 static int rvin_group_notify_bound(struct v4l2_async_notifier *notifier,
283 struct v4l2_subdev *subdev,
284 struct v4l2_async_subdev *asd)
286 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
289 mutex_lock(&vin->group->lock);
291 for (i = 0; i < RVIN_CSI_MAX; i++) {
292 if (vin->group->remotes[i].asd != asd)
294 vin->group->remotes[i].subdev = subdev;
295 vin_dbg(vin, "Bound %s to slot %u\n", subdev->name, i);
299 mutex_unlock(&vin->group->lock);
304 static const struct v4l2_async_notifier_operations rvin_group_notify_ops = {
305 .bound = rvin_group_notify_bound,
306 .unbind = rvin_group_notify_unbind,
307 .complete = rvin_group_notify_complete,
310 static int rvin_group_parse_of(struct rvin_dev *vin, unsigned int port,
313 struct fwnode_handle *ep, *fwnode;
314 struct v4l2_fwnode_endpoint vep = {
315 .bus_type = V4L2_MBUS_CSI2_DPHY,
317 struct v4l2_async_subdev *asd;
320 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(vin->dev), port, id, 0);
324 fwnode = fwnode_graph_get_remote_endpoint(ep);
325 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
326 fwnode_handle_put(ep);
328 vin_err(vin, "Failed to parse %pOF\n", to_of_node(fwnode));
333 asd = v4l2_async_nf_add_fwnode(&vin->group->notifier, fwnode,
334 struct v4l2_async_subdev);
340 vin->group->remotes[vep.base.id].asd = asd;
342 vin_dbg(vin, "Add group OF device %pOF to slot %u\n",
343 to_of_node(fwnode), vep.base.id);
345 fwnode_handle_put(fwnode);
350 static void rvin_group_notifier_cleanup(struct rvin_dev *vin)
352 if (&vin->v4l2_dev == vin->group->notifier.v4l2_dev) {
353 v4l2_async_nf_unregister(&vin->group->notifier);
354 v4l2_async_nf_cleanup(&vin->group->notifier);
358 static int rvin_group_notifier_init(struct rvin_dev *vin, unsigned int port,
361 unsigned int count = 0, vin_mask = 0;
365 mutex_lock(&vin->group->lock);
367 /* If not all VIN's are registered don't register the notifier. */
368 for (i = 0; i < RCAR_VIN_NUM; i++) {
369 if (vin->group->vin[i]) {
375 if (vin->group->count != count) {
376 mutex_unlock(&vin->group->lock);
380 mutex_unlock(&vin->group->lock);
382 v4l2_async_nf_init(&vin->group->notifier);
385 * Some subdevices may overlap but the parser function can handle it and
386 * each subdevice will only be registered once with the group notifier.
388 for (i = 0; i < RCAR_VIN_NUM; i++) {
389 if (!(vin_mask & BIT(i)))
392 for (id = 0; id < max_id; id++) {
393 if (vin->group->remotes[id].asd)
396 ret = rvin_group_parse_of(vin->group->vin[i], port, id);
402 if (list_empty(&vin->group->notifier.asd_list))
405 vin->group->notifier.ops = &rvin_group_notify_ops;
406 ret = v4l2_async_nf_register(&vin->v4l2_dev, &vin->group->notifier);
408 vin_err(vin, "Notifier registration failed\n");
409 v4l2_async_nf_cleanup(&vin->group->notifier);
416 /* -----------------------------------------------------------------------------
420 static int rvin_s_ctrl(struct v4l2_ctrl *ctrl)
422 struct rvin_dev *vin =
423 container_of(ctrl->handler, struct rvin_dev, ctrl_handler);
426 case V4L2_CID_ALPHA_COMPONENT:
427 rvin_set_alpha(vin, ctrl->val);
434 static const struct v4l2_ctrl_ops rvin_ctrl_ops = {
435 .s_ctrl = rvin_s_ctrl,
438 static void rvin_free_controls(struct rvin_dev *vin)
440 v4l2_ctrl_handler_free(&vin->ctrl_handler);
441 vin->vdev.ctrl_handler = NULL;
444 static int rvin_create_controls(struct rvin_dev *vin, struct v4l2_subdev *subdev)
448 ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 16);
452 /* The VIN directly deals with alpha component. */
453 v4l2_ctrl_new_std(&vin->ctrl_handler, &rvin_ctrl_ops,
454 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
456 if (vin->ctrl_handler.error) {
457 ret = vin->ctrl_handler.error;
458 rvin_free_controls(vin);
462 /* For the non-MC mode add controls from the subdevice. */
464 ret = v4l2_ctrl_add_handler(&vin->ctrl_handler,
465 subdev->ctrl_handler, NULL, true);
467 rvin_free_controls(vin);
472 vin->vdev.ctrl_handler = &vin->ctrl_handler;
477 /* -----------------------------------------------------------------------------
481 static int rvin_find_pad(struct v4l2_subdev *sd, int direction)
485 if (sd->entity.num_pads <= 1)
488 for (pad = 0; pad < sd->entity.num_pads; pad++)
489 if (sd->entity.pads[pad].flags & direction)
495 /* -----------------------------------------------------------------------------
496 * Parallel async notifier
499 /* The vin lock should be held when calling the subdevice attach and detach */
500 static int rvin_parallel_subdevice_attach(struct rvin_dev *vin,
501 struct v4l2_subdev *subdev)
503 struct v4l2_subdev_mbus_code_enum code = {
504 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
508 /* Find source and sink pad of remote subdevice */
509 ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
512 vin->parallel.source_pad = ret;
514 ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
515 vin->parallel.sink_pad = ret < 0 ? 0 : ret;
517 if (vin->info->use_mc) {
518 vin->parallel.subdev = subdev;
522 /* Find compatible subdevices mbus format */
525 code.pad = vin->parallel.source_pad;
526 while (!vin->mbus_code &&
527 !v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
530 case MEDIA_BUS_FMT_YUYV8_1X16:
531 case MEDIA_BUS_FMT_UYVY8_1X16:
532 case MEDIA_BUS_FMT_UYVY8_2X8:
533 case MEDIA_BUS_FMT_UYVY10_2X10:
534 case MEDIA_BUS_FMT_RGB888_1X24:
535 vin->mbus_code = code.code;
536 vin_dbg(vin, "Found media bus format for %s: %d\n",
537 subdev->name, vin->mbus_code);
544 if (!vin->mbus_code) {
545 vin_err(vin, "Unsupported media bus format for %s\n",
551 ret = v4l2_subdev_call(subdev, video, g_tvnorms, &vin->vdev.tvnorms);
552 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
556 vin->std = V4L2_STD_UNKNOWN;
557 ret = v4l2_subdev_call(subdev, video, g_std, &vin->std);
558 if (ret < 0 && ret != -ENOIOCTLCMD)
561 /* Add the controls */
562 ret = rvin_create_controls(vin, subdev);
566 vin->parallel.subdev = subdev;
571 static void rvin_parallel_subdevice_detach(struct rvin_dev *vin)
573 rvin_v4l2_unregister(vin);
574 vin->parallel.subdev = NULL;
576 if (!vin->info->use_mc)
577 rvin_free_controls(vin);
580 static int rvin_parallel_notify_complete(struct v4l2_async_notifier *notifier)
582 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
583 struct media_entity *source;
584 struct media_entity *sink;
587 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
589 vin_err(vin, "Failed to register subdev nodes\n");
593 if (!video_is_registered(&vin->vdev)) {
594 ret = rvin_v4l2_register(vin);
599 if (!vin->info->use_mc)
602 /* If we're running with media-controller, link the subdevs. */
603 source = &vin->parallel.subdev->entity;
604 sink = &vin->vdev.entity;
606 ret = media_create_pad_link(source, vin->parallel.source_pad,
607 sink, vin->parallel.sink_pad, 0);
609 vin_err(vin, "Error adding link from %s to %s: %d\n",
610 source->name, sink->name, ret);
615 static void rvin_parallel_notify_unbind(struct v4l2_async_notifier *notifier,
616 struct v4l2_subdev *subdev,
617 struct v4l2_async_subdev *asd)
619 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
621 vin_dbg(vin, "unbind parallel subdev %s\n", subdev->name);
623 mutex_lock(&vin->lock);
624 rvin_parallel_subdevice_detach(vin);
625 mutex_unlock(&vin->lock);
628 static int rvin_parallel_notify_bound(struct v4l2_async_notifier *notifier,
629 struct v4l2_subdev *subdev,
630 struct v4l2_async_subdev *asd)
632 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
635 mutex_lock(&vin->lock);
636 ret = rvin_parallel_subdevice_attach(vin, subdev);
637 mutex_unlock(&vin->lock);
641 v4l2_set_subdev_hostdata(subdev, vin);
643 vin_dbg(vin, "bound subdev %s source pad: %u sink pad: %u\n",
644 subdev->name, vin->parallel.source_pad,
645 vin->parallel.sink_pad);
650 static const struct v4l2_async_notifier_operations rvin_parallel_notify_ops = {
651 .bound = rvin_parallel_notify_bound,
652 .unbind = rvin_parallel_notify_unbind,
653 .complete = rvin_parallel_notify_complete,
656 static int rvin_parallel_parse_of(struct rvin_dev *vin)
658 struct fwnode_handle *ep, *fwnode;
659 struct v4l2_fwnode_endpoint vep = {
660 .bus_type = V4L2_MBUS_UNKNOWN,
662 struct v4l2_async_subdev *asd;
665 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(vin->dev), 0, 0, 0);
669 fwnode = fwnode_graph_get_remote_endpoint(ep);
670 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
671 fwnode_handle_put(ep);
673 vin_err(vin, "Failed to parse %pOF\n", to_of_node(fwnode));
678 switch (vep.bus_type) {
679 case V4L2_MBUS_PARALLEL:
680 case V4L2_MBUS_BT656:
681 vin_dbg(vin, "Found %s media bus\n",
682 vep.bus_type == V4L2_MBUS_PARALLEL ?
683 "PARALLEL" : "BT656");
684 vin->parallel.mbus_type = vep.bus_type;
685 vin->parallel.bus = vep.bus.parallel;
688 vin_err(vin, "Unknown media bus type\n");
693 asd = v4l2_async_nf_add_fwnode(&vin->notifier, fwnode,
694 struct v4l2_async_subdev);
700 vin->parallel.asd = asd;
702 vin_dbg(vin, "Add parallel OF device %pOF\n", to_of_node(fwnode));
704 fwnode_handle_put(fwnode);
709 static void rvin_parallel_cleanup(struct rvin_dev *vin)
711 v4l2_async_nf_unregister(&vin->notifier);
712 v4l2_async_nf_cleanup(&vin->notifier);
715 static int rvin_parallel_init(struct rvin_dev *vin)
719 v4l2_async_nf_init(&vin->notifier);
721 ret = rvin_parallel_parse_of(vin);
725 if (!vin->parallel.asd)
728 vin_dbg(vin, "Found parallel subdevice %pOF\n",
729 to_of_node(vin->parallel.asd->match.fwnode));
731 vin->notifier.ops = &rvin_parallel_notify_ops;
732 ret = v4l2_async_nf_register(&vin->v4l2_dev, &vin->notifier);
734 vin_err(vin, "Notifier registration failed\n");
735 v4l2_async_nf_cleanup(&vin->notifier);
742 /* -----------------------------------------------------------------------------
746 static unsigned int rvin_csi2_get_mask(struct rvin_dev *vin,
747 enum rvin_csi_id csi_id,
748 unsigned char channel)
750 const struct rvin_group_route *route;
751 unsigned int mask = 0;
753 for (route = vin->info->routes; route->mask; route++) {
754 if (route->vin == vin->id &&
755 route->csi == csi_id &&
756 route->channel == channel) {
758 "Adding route: vin: %d csi: %d channel: %d\n",
759 route->vin, route->csi, route->channel);
768 * Link setup for the links between a VIN and a CSI-2 receiver is a bit
769 * complex. The reason for this is that the register controlling routing
770 * is not present in each VIN instance. There are special VINs which
771 * control routing for themselves and other VINs. There are not many
772 * different possible links combinations that can be enabled at the same
773 * time, therefor all already enabled links which are controlled by a
774 * master VIN need to be taken into account when making the decision
775 * if a new link can be enabled or not.
777 * 1. Find out which VIN the link the user tries to enable is connected to.
778 * 2. Lookup which master VIN controls the links for this VIN.
779 * 3. Start with a bitmask with all bits set.
780 * 4. For each previously enabled link from the master VIN bitwise AND its
781 * route mask (see documentation for mask in struct rvin_group_route)
783 * 5. Bitwise AND the mask for the link the user tries to enable to the bitmask.
784 * 6. If the bitmask is not empty at this point the new link can be enabled
785 * while keeping all previous links enabled. Update the CHSEL value of the
786 * master VIN and inform the user that the link could be enabled.
788 * Please note that no link can be enabled if any VIN in the group is
791 static int rvin_csi2_link_notify(struct media_link *link, u32 flags,
792 unsigned int notification)
794 struct rvin_group *group = container_of(link->graph_obj.mdev,
795 struct rvin_group, mdev);
796 struct media_entity *entity;
797 struct video_device *vdev;
798 struct rvin_dev *vin;
802 ret = v4l2_pipeline_link_notify(link, flags, notification);
806 /* Only care about link enablement for VIN nodes. */
807 if (!(flags & MEDIA_LNK_FL_ENABLED) ||
808 !is_media_entity_v4l2_video_device(link->sink->entity))
812 * Don't allow link changes if any entity in the graph is
813 * streaming, modifying the CHSEL register fields can disrupt
816 media_device_for_each_entity(entity, &group->mdev)
817 if (entity->stream_count)
820 /* Find the master VIN that controls the routes. */
821 vdev = media_entity_to_video_device(link->sink->entity);
822 vin = container_of(vdev, struct rvin_dev, vdev);
824 mutex_lock(&group->lock);
826 csi_id = rvin_group_entity_to_remote_id(group, link->source->entity);
827 if (csi_id == -ENODEV) {
828 struct v4l2_subdev *sd;
831 * Make sure the source entity subdevice is registered as
832 * a parallel input of one of the enabled VINs if it is not
833 * one of the CSI-2 subdevices.
835 * No hardware configuration required for parallel inputs,
836 * we can return here.
838 sd = media_entity_to_v4l2_subdev(link->source->entity);
839 for (i = 0; i < RCAR_VIN_NUM; i++) {
841 group->vin[i]->parallel.subdev == sd) {
842 group->vin[i]->is_csi = false;
848 vin_err(vin, "Subdevice %s not registered to any VIN\n",
849 link->source->entity->name);
852 unsigned int master_id, channel, mask_new;
853 unsigned int mask = ~0;
854 struct media_pad *csi_pad;
856 master_id = rvin_group_id_to_master(vin->id);
858 if (WARN_ON(!group->vin[master_id])) {
863 /* Build a mask for already enabled links. */
864 for (i = master_id; i < master_id + 4; i++) {
868 /* Get remote CSI-2, if any. */
869 csi_pad = media_entity_remote_pad(
870 &group->vin[i]->vdev.entity.pads[0]);
874 csi_id = rvin_group_entity_to_remote_id(group,
876 channel = rvin_group_csi_pad_to_channel(csi_pad->index);
878 mask &= rvin_csi2_get_mask(group->vin[i], csi_id, channel);
881 channel = rvin_group_csi_pad_to_channel(link->source->index);
882 mask_new = mask & rvin_csi2_get_mask(vin, csi_id, channel);
883 vin_dbg(vin, "Try link change mask: 0x%x new: 0x%x\n", mask,
891 /* New valid CHSEL found, set the new value. */
892 ret = rvin_set_channel_routing(group->vin[master_id],
900 mutex_unlock(&group->lock);
905 static const struct media_device_ops rvin_csi2_media_ops = {
906 .link_notify = rvin_csi2_link_notify,
909 static int rvin_csi2_create_link(struct rvin_group *group,
910 const struct rvin_group_route *route)
913 struct media_entity *source = &group->remotes[route->csi].subdev->entity;
914 unsigned int source_idx = rvin_group_csi_channel_to_pad(route->channel);
915 struct media_entity *sink = &group->vin[route->vin]->vdev.entity;
916 struct media_pad *source_pad = &source->pads[source_idx];
917 struct media_pad *sink_pad = &sink->pads[0];
919 /* Skip if link already exists. */
920 if (media_entity_find_link(source_pad, sink_pad))
923 return media_create_pad_link(source, source_idx, sink, 0, 0);
926 static int rvin_csi2_setup_links(struct rvin_dev *vin)
928 const struct rvin_group_route *route;
931 /* Create all media device links between VINs and CSI-2's. */
932 mutex_lock(&vin->group->lock);
933 for (route = vin->info->routes; route->mask; route++) {
934 /* Check that VIN is part of the group. */
935 if (!vin->group->vin[route->vin])
938 /* Check that VIN' master is part of the group. */
939 if (!vin->group->vin[rvin_group_id_to_master(route->vin)])
942 /* Check that CSI-2 is part of the group. */
943 if (!vin->group->remotes[route->csi].subdev)
946 ret = rvin_csi2_create_link(vin->group, route);
950 mutex_unlock(&vin->group->lock);
955 static void rvin_csi2_cleanup(struct rvin_dev *vin)
957 rvin_parallel_cleanup(vin);
958 rvin_group_notifier_cleanup(vin);
960 rvin_free_controls(vin);
963 static int rvin_csi2_init(struct rvin_dev *vin)
967 vin->pad.flags = MEDIA_PAD_FL_SINK;
968 ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
972 ret = rvin_create_controls(vin, NULL);
976 ret = rvin_group_get(vin, rvin_csi2_setup_links, &rvin_csi2_media_ops);
980 /* It's OK to not have a parallel subdevice. */
981 ret = rvin_parallel_init(vin);
982 if (ret && ret != -ENODEV)
985 ret = rvin_group_notifier_init(vin, 1, RVIN_CSI_MAX);
991 rvin_parallel_cleanup(vin);
995 rvin_free_controls(vin);
1000 /* -----------------------------------------------------------------------------
1004 static int rvin_isp_setup_links(struct rvin_dev *vin)
1009 /* Create all media device links between VINs and ISP's. */
1010 mutex_lock(&vin->group->lock);
1011 for (i = 0; i < RCAR_VIN_NUM; i++) {
1012 struct media_pad *source_pad, *sink_pad;
1013 struct media_entity *source, *sink;
1014 unsigned int source_slot = i / 8;
1015 unsigned int source_idx = i % 8 + 1;
1017 if (!vin->group->vin[i])
1020 /* Check that ISP is part of the group. */
1021 if (!vin->group->remotes[source_slot].subdev)
1024 source = &vin->group->remotes[source_slot].subdev->entity;
1025 source_pad = &source->pads[source_idx];
1027 sink = &vin->group->vin[i]->vdev.entity;
1028 sink_pad = &sink->pads[0];
1030 /* Skip if link already exists. */
1031 if (media_entity_find_link(source_pad, sink_pad))
1034 ret = media_create_pad_link(source, source_idx, sink, 0,
1035 MEDIA_LNK_FL_ENABLED |
1036 MEDIA_LNK_FL_IMMUTABLE);
1038 vin_err(vin, "Error adding link from %s to %s\n",
1039 source->name, sink->name);
1043 mutex_unlock(&vin->group->lock);
1048 static void rvin_isp_cleanup(struct rvin_dev *vin)
1050 rvin_group_notifier_cleanup(vin);
1051 rvin_group_put(vin);
1052 rvin_free_controls(vin);
1055 static int rvin_isp_init(struct rvin_dev *vin)
1059 vin->pad.flags = MEDIA_PAD_FL_SINK;
1060 ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
1064 ret = rvin_create_controls(vin, NULL);
1068 ret = rvin_group_get(vin, rvin_isp_setup_links, NULL);
1072 ret = rvin_group_notifier_init(vin, 2, RVIN_ISP_MAX);
1078 rvin_group_put(vin);
1080 rvin_free_controls(vin);
1085 /* -----------------------------------------------------------------------------
1089 static int __maybe_unused rvin_suspend(struct device *dev)
1091 struct rvin_dev *vin = dev_get_drvdata(dev);
1093 if (vin->state != RUNNING)
1096 rvin_stop_streaming(vin);
1098 vin->state = SUSPENDED;
1103 static int __maybe_unused rvin_resume(struct device *dev)
1105 struct rvin_dev *vin = dev_get_drvdata(dev);
1107 if (vin->state != SUSPENDED)
1111 * Restore group master CHSEL setting.
1113 * This needs to be done by every VIN resuming not only the master
1114 * as we don't know if and in which order the master VINs will
1117 if (vin->info->use_mc) {
1118 unsigned int master_id = rvin_group_id_to_master(vin->id);
1119 struct rvin_dev *master = vin->group->vin[master_id];
1122 if (WARN_ON(!master))
1125 ret = rvin_set_channel_routing(master, master->chsel);
1130 return rvin_start_streaming(vin);
1133 /* -----------------------------------------------------------------------------
1134 * Platform Device Driver
1137 static const struct rvin_info rcar_info_h1 = {
1144 static const struct rvin_info rcar_info_m1 = {
1151 static const struct rvin_info rcar_info_gen2 = {
1158 static const struct rvin_group_route rcar_info_r8a774e1_routes[] = {
1159 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1160 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
1161 { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
1162 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
1163 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
1164 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1165 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
1166 { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
1167 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1168 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
1169 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1170 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
1171 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1172 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
1173 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1174 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
1175 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
1176 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
1177 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
1178 { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
1179 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
1180 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
1181 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
1182 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
1186 static const struct rvin_info rcar_info_r8a774e1 = {
1191 .routes = rcar_info_r8a774e1_routes,
1194 static const struct rvin_group_route rcar_info_r8a7795_routes[] = {
1195 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1196 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
1197 { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
1198 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
1199 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
1200 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1201 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
1202 { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
1203 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1204 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
1205 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1206 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
1207 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1208 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
1209 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1210 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
1211 { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1212 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
1213 { .csi = RVIN_CSI41, .channel = 1, .vin = 4, .mask = BIT(2) },
1214 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
1215 { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
1216 { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
1217 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
1218 { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
1219 { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
1220 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
1221 { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
1222 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
1223 { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
1224 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
1225 { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
1226 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
1230 static const struct rvin_info rcar_info_r8a7795 = {
1236 .routes = rcar_info_r8a7795_routes,
1239 static const struct rvin_group_route rcar_info_r8a7795es1_routes[] = {
1240 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1241 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
1242 { .csi = RVIN_CSI21, .channel = 0, .vin = 0, .mask = BIT(2) | BIT(5) },
1243 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
1244 { .csi = RVIN_CSI21, .channel = 0, .vin = 1, .mask = BIT(1) },
1245 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1246 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
1247 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
1248 { .csi = RVIN_CSI21, .channel = 1, .vin = 1, .mask = BIT(5) },
1249 { .csi = RVIN_CSI21, .channel = 0, .vin = 2, .mask = BIT(0) },
1250 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1251 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
1252 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1253 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
1254 { .csi = RVIN_CSI21, .channel = 2, .vin = 2, .mask = BIT(5) },
1255 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1256 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) },
1257 { .csi = RVIN_CSI21, .channel = 1, .vin = 3, .mask = BIT(2) },
1258 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1259 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
1260 { .csi = RVIN_CSI21, .channel = 3, .vin = 3, .mask = BIT(5) },
1261 { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1262 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
1263 { .csi = RVIN_CSI21, .channel = 0, .vin = 4, .mask = BIT(2) | BIT(5) },
1264 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
1265 { .csi = RVIN_CSI21, .channel = 0, .vin = 5, .mask = BIT(1) },
1266 { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
1267 { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(3) },
1268 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
1269 { .csi = RVIN_CSI21, .channel = 1, .vin = 5, .mask = BIT(5) },
1270 { .csi = RVIN_CSI21, .channel = 0, .vin = 6, .mask = BIT(0) },
1271 { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
1272 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
1273 { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
1274 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
1275 { .csi = RVIN_CSI21, .channel = 2, .vin = 6, .mask = BIT(5) },
1276 { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
1277 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) },
1278 { .csi = RVIN_CSI21, .channel = 1, .vin = 7, .mask = BIT(2) },
1279 { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
1280 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
1281 { .csi = RVIN_CSI21, .channel = 3, .vin = 7, .mask = BIT(5) },
1285 static const struct rvin_info rcar_info_r8a7795es1 = {
1290 .routes = rcar_info_r8a7795es1_routes,
1293 static const struct rvin_group_route rcar_info_r8a7796_routes[] = {
1294 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1295 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
1296 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
1297 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1298 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
1299 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
1300 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1301 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
1302 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1303 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
1304 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1305 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) },
1306 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1307 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
1308 { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1309 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
1310 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
1311 { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
1312 { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(3) },
1313 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
1314 { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
1315 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
1316 { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
1317 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
1318 { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
1319 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) },
1320 { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
1321 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
1325 static const struct rvin_info rcar_info_r8a7796 = {
1331 .routes = rcar_info_r8a7796_routes,
1334 static const struct rvin_group_route rcar_info_r8a77965_routes[] = {
1335 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1336 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
1337 { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
1338 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
1339 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
1340 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1341 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
1342 { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
1343 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1344 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
1345 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1346 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
1347 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1348 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
1349 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1350 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
1351 { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1352 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
1353 { .csi = RVIN_CSI40, .channel = 1, .vin = 4, .mask = BIT(2) },
1354 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
1355 { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
1356 { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
1357 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
1358 { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
1359 { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
1360 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
1361 { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
1362 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
1363 { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
1364 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
1365 { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
1366 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
1370 static const struct rvin_info rcar_info_r8a77965 = {
1376 .routes = rcar_info_r8a77965_routes,
1379 static const struct rvin_group_route rcar_info_r8a77970_routes[] = {
1380 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1381 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1382 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
1383 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1384 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1385 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1386 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1390 static const struct rvin_info rcar_info_r8a77970 = {
1395 .routes = rcar_info_r8a77970_routes,
1398 static const struct rvin_group_route rcar_info_r8a77980_routes[] = {
1399 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1400 { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
1401 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1402 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
1403 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1404 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1405 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1406 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1407 { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1408 { .csi = RVIN_CSI41, .channel = 1, .vin = 4, .mask = BIT(2) },
1409 { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
1410 { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
1411 { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
1412 { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
1413 { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
1414 { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
1418 static const struct rvin_info rcar_info_r8a77980 = {
1424 .routes = rcar_info_r8a77980_routes,
1427 static const struct rvin_group_route rcar_info_r8a77990_routes[] = {
1428 { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1429 { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
1430 { .csi = RVIN_CSI40, .channel = 1, .vin = 4, .mask = BIT(2) },
1431 { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
1435 static const struct rvin_info rcar_info_r8a77990 = {
1441 .routes = rcar_info_r8a77990_routes,
1444 static const struct rvin_group_route rcar_info_r8a77995_routes[] = {
1448 static const struct rvin_info rcar_info_r8a77995 = {
1454 .routes = rcar_info_r8a77995_routes,
1457 static const struct rvin_info rcar_info_r8a779a0 = {
1466 static const struct of_device_id rvin_of_id_table[] = {
1468 .compatible = "renesas,vin-r8a774a1",
1469 .data = &rcar_info_r8a7796,
1472 .compatible = "renesas,vin-r8a774b1",
1473 .data = &rcar_info_r8a77965,
1476 .compatible = "renesas,vin-r8a774c0",
1477 .data = &rcar_info_r8a77990,
1480 .compatible = "renesas,vin-r8a774e1",
1481 .data = &rcar_info_r8a774e1,
1484 .compatible = "renesas,vin-r8a7778",
1485 .data = &rcar_info_m1,
1488 .compatible = "renesas,vin-r8a7779",
1489 .data = &rcar_info_h1,
1492 .compatible = "renesas,rcar-gen2-vin",
1493 .data = &rcar_info_gen2,
1496 .compatible = "renesas,vin-r8a7795",
1497 .data = &rcar_info_r8a7795,
1500 .compatible = "renesas,vin-r8a7796",
1501 .data = &rcar_info_r8a7796,
1504 .compatible = "renesas,vin-r8a77961",
1505 .data = &rcar_info_r8a7796,
1508 .compatible = "renesas,vin-r8a77965",
1509 .data = &rcar_info_r8a77965,
1512 .compatible = "renesas,vin-r8a77970",
1513 .data = &rcar_info_r8a77970,
1516 .compatible = "renesas,vin-r8a77980",
1517 .data = &rcar_info_r8a77980,
1520 .compatible = "renesas,vin-r8a77990",
1521 .data = &rcar_info_r8a77990,
1524 .compatible = "renesas,vin-r8a77995",
1525 .data = &rcar_info_r8a77995,
1528 .compatible = "renesas,vin-r8a779a0",
1529 .data = &rcar_info_r8a779a0,
1533 MODULE_DEVICE_TABLE(of, rvin_of_id_table);
1535 static const struct soc_device_attribute r8a7795es1[] = {
1537 .soc_id = "r8a7795", .revision = "ES1.*",
1538 .data = &rcar_info_r8a7795es1,
1543 static int rcar_vin_probe(struct platform_device *pdev)
1545 const struct soc_device_attribute *attr;
1546 struct rvin_dev *vin;
1549 vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
1553 vin->dev = &pdev->dev;
1554 vin->info = of_device_get_match_data(&pdev->dev);
1558 * Special care is needed on r8a7795 ES1.x since it
1559 * uses different routing than r8a7795 ES2.0.
1561 attr = soc_device_match(r8a7795es1);
1563 vin->info = attr->data;
1565 vin->base = devm_platform_ioremap_resource(pdev, 0);
1566 if (IS_ERR(vin->base))
1567 return PTR_ERR(vin->base);
1569 irq = platform_get_irq(pdev, 0);
1573 ret = rvin_dma_register(vin, irq);
1577 platform_set_drvdata(pdev, vin);
1579 if (vin->info->use_isp)
1580 ret = rvin_isp_init(vin);
1581 else if (vin->info->use_mc)
1582 ret = rvin_csi2_init(vin);
1584 ret = rvin_parallel_init(vin);
1587 rvin_dma_unregister(vin);
1591 pm_suspend_ignore_children(&pdev->dev, true);
1592 pm_runtime_enable(&pdev->dev);
1597 static int rcar_vin_remove(struct platform_device *pdev)
1599 struct rvin_dev *vin = platform_get_drvdata(pdev);
1601 pm_runtime_disable(&pdev->dev);
1603 rvin_v4l2_unregister(vin);
1605 if (vin->info->use_isp)
1606 rvin_isp_cleanup(vin);
1607 else if (vin->info->use_mc)
1608 rvin_csi2_cleanup(vin);
1610 rvin_parallel_cleanup(vin);
1612 rvin_dma_unregister(vin);
1617 static SIMPLE_DEV_PM_OPS(rvin_pm_ops, rvin_suspend, rvin_resume);
1619 static struct platform_driver rcar_vin_driver = {
1622 .suppress_bind_attrs = true,
1624 .of_match_table = rvin_of_id_table,
1626 .probe = rcar_vin_probe,
1627 .remove = rcar_vin_remove,
1630 module_platform_driver(rcar_vin_driver);
1632 MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
1633 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");
1634 MODULE_LICENSE("GPL");