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 * Media Controller link notification
51 /* group lock should be held when calling this function. */
52 static int rvin_group_entity_to_csi_id(struct rvin_group *group,
53 struct media_entity *entity)
55 struct v4l2_subdev *sd;
58 sd = media_entity_to_v4l2_subdev(entity);
60 for (i = 0; i < RVIN_CSI_MAX; i++)
61 if (group->csi[i].subdev == sd)
67 static unsigned int rvin_group_get_mask(struct rvin_dev *vin,
68 enum rvin_csi_id csi_id,
69 unsigned char channel)
71 const struct rvin_group_route *route;
72 unsigned int mask = 0;
74 for (route = vin->info->routes; route->mask; route++) {
75 if (route->vin == vin->id &&
76 route->csi == csi_id &&
77 route->channel == channel) {
79 "Adding route: vin: %d csi: %d channel: %d\n",
80 route->vin, route->csi, route->channel);
89 * Link setup for the links between a VIN and a CSI-2 receiver is a bit
90 * complex. The reason for this is that the register controlling routing
91 * is not present in each VIN instance. There are special VINs which
92 * control routing for themselves and other VINs. There are not many
93 * different possible links combinations that can be enabled at the same
94 * time, therefor all already enabled links which are controlled by a
95 * master VIN need to be taken into account when making the decision
96 * if a new link can be enabled or not.
98 * 1. Find out which VIN the link the user tries to enable is connected to.
99 * 2. Lookup which master VIN controls the links for this VIN.
100 * 3. Start with a bitmask with all bits set.
101 * 4. For each previously enabled link from the master VIN bitwise AND its
102 * route mask (see documentation for mask in struct rvin_group_route)
104 * 5. Bitwise AND the mask for the link the user tries to enable to the bitmask.
105 * 6. If the bitmask is not empty at this point the new link can be enabled
106 * while keeping all previous links enabled. Update the CHSEL value of the
107 * master VIN and inform the user that the link could be enabled.
109 * Please note that no link can be enabled if any VIN in the group is
112 static int rvin_group_link_notify(struct media_link *link, u32 flags,
113 unsigned int notification)
115 struct rvin_group *group = container_of(link->graph_obj.mdev,
116 struct rvin_group, mdev);
117 unsigned int master_id, channel, mask_new, i;
118 unsigned int mask = ~0;
119 struct media_entity *entity;
120 struct video_device *vdev;
121 struct media_pad *csi_pad;
122 struct rvin_dev *vin = NULL;
125 ret = v4l2_pipeline_link_notify(link, flags, notification);
129 /* Only care about link enablement for VIN nodes. */
130 if (!(flags & MEDIA_LNK_FL_ENABLED) ||
131 !is_media_entity_v4l2_video_device(link->sink->entity))
135 * Don't allow link changes if any entity in the graph is
136 * streaming, modifying the CHSEL register fields can disrupt
139 media_device_for_each_entity(entity, &group->mdev)
140 if (entity->stream_count)
143 mutex_lock(&group->lock);
145 /* Find the master VIN that controls the routes. */
146 vdev = media_entity_to_video_device(link->sink->entity);
147 vin = container_of(vdev, struct rvin_dev, vdev);
148 master_id = rvin_group_id_to_master(vin->id);
150 if (WARN_ON(!group->vin[master_id])) {
155 /* Build a mask for already enabled links. */
156 for (i = master_id; i < master_id + 4; i++) {
160 /* Get remote CSI-2, if any. */
161 csi_pad = media_entity_remote_pad(
162 &group->vin[i]->vdev.entity.pads[0]);
166 csi_id = rvin_group_entity_to_csi_id(group, csi_pad->entity);
167 channel = rvin_group_csi_pad_to_channel(csi_pad->index);
169 mask &= rvin_group_get_mask(group->vin[i], csi_id, channel);
172 /* Add the new link to the existing mask and check if it works. */
173 csi_id = rvin_group_entity_to_csi_id(group, link->source->entity);
175 if (csi_id == -ENODEV) {
176 struct v4l2_subdev *sd;
179 * Make sure the source entity subdevice is registered as
180 * a parallel input of one of the enabled VINs if it is not
181 * one of the CSI-2 subdevices.
183 * No hardware configuration required for parallel inputs,
184 * we can return here.
186 sd = media_entity_to_v4l2_subdev(link->source->entity);
187 for (i = 0; i < RCAR_VIN_NUM; i++) {
188 if (group->vin[i] && group->vin[i]->parallel &&
189 group->vin[i]->parallel->subdev == sd) {
190 group->vin[i]->is_csi = false;
196 vin_err(vin, "Subdevice %s not registered to any VIN\n",
197 link->source->entity->name);
202 channel = rvin_group_csi_pad_to_channel(link->source->index);
203 mask_new = mask & rvin_group_get_mask(vin, csi_id, channel);
204 vin_dbg(vin, "Try link change mask: 0x%x new: 0x%x\n", mask, mask_new);
211 /* New valid CHSEL found, set the new value. */
212 ret = rvin_set_channel_routing(group->vin[master_id], __ffs(mask_new));
219 mutex_unlock(&group->lock);
224 static const struct media_device_ops rvin_media_ops = {
225 .link_notify = rvin_group_link_notify,
228 /* -----------------------------------------------------------------------------
229 * Gen3 CSI2 Group Allocator
232 /* FIXME: This should if we find a system that supports more
233 * than one group for the whole system be replaced with a linked
234 * list of groups. And eventually all of this should be replaced
235 * with a global device allocator API.
237 * But for now this works as on all supported systems there will
238 * be only one group for all instances.
241 static DEFINE_MUTEX(rvin_group_lock);
242 static struct rvin_group *rvin_group_data;
244 static void rvin_group_cleanup(struct rvin_group *group)
246 media_device_unregister(&group->mdev);
247 media_device_cleanup(&group->mdev);
248 mutex_destroy(&group->lock);
251 static int rvin_group_init(struct rvin_group *group, struct rvin_dev *vin)
253 struct media_device *mdev = &group->mdev;
254 const struct of_device_id *match;
255 struct device_node *np;
258 mutex_init(&group->lock);
260 /* Count number of VINs in the system */
262 for_each_matching_node(np, vin->dev->driver->of_match_table)
263 if (of_device_is_available(np))
266 vin_dbg(vin, "found %u enabled VIN's in DT", group->count);
268 mdev->dev = vin->dev;
269 mdev->ops = &rvin_media_ops;
271 match = of_match_node(vin->dev->driver->of_match_table,
274 strscpy(mdev->driver_name, KBUILD_MODNAME, sizeof(mdev->driver_name));
275 strscpy(mdev->model, match->compatible, sizeof(mdev->model));
276 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
277 dev_name(mdev->dev));
279 media_device_init(mdev);
281 ret = media_device_register(&group->mdev);
283 rvin_group_cleanup(group);
288 static void rvin_group_release(struct kref *kref)
290 struct rvin_group *group =
291 container_of(kref, struct rvin_group, refcount);
293 mutex_lock(&rvin_group_lock);
295 rvin_group_data = NULL;
297 rvin_group_cleanup(group);
301 mutex_unlock(&rvin_group_lock);
304 static int rvin_group_get(struct rvin_dev *vin)
306 struct rvin_group *group;
310 /* Make sure VIN id is present and sane */
311 ret = of_property_read_u32(vin->dev->of_node, "renesas,id", &id);
313 vin_err(vin, "%pOF: No renesas,id property found\n",
318 if (id >= RCAR_VIN_NUM) {
319 vin_err(vin, "%pOF: Invalid renesas,id '%u'\n",
320 vin->dev->of_node, id);
324 /* Join or create a VIN group */
325 mutex_lock(&rvin_group_lock);
326 if (rvin_group_data) {
327 group = rvin_group_data;
328 kref_get(&group->refcount);
330 group = kzalloc(sizeof(*group), GFP_KERNEL);
336 ret = rvin_group_init(group, vin);
339 vin_err(vin, "Failed to initialize group\n");
343 kref_init(&group->refcount);
345 rvin_group_data = group;
347 mutex_unlock(&rvin_group_lock);
349 /* Add VIN to group */
350 mutex_lock(&group->lock);
352 if (group->vin[id]) {
353 vin_err(vin, "Duplicate renesas,id property value %u\n", id);
354 mutex_unlock(&group->lock);
355 kref_put(&group->refcount, rvin_group_release);
359 group->vin[id] = vin;
363 vin->v4l2_dev.mdev = &group->mdev;
365 mutex_unlock(&group->lock);
369 mutex_unlock(&rvin_group_lock);
373 static void rvin_group_put(struct rvin_dev *vin)
375 struct rvin_group *group = vin->group;
377 mutex_lock(&group->lock);
380 vin->v4l2_dev.mdev = NULL;
382 if (WARN_ON(group->vin[vin->id] != vin))
385 group->vin[vin->id] = NULL;
387 mutex_unlock(&group->lock);
389 kref_put(&group->refcount, rvin_group_release);
392 /* -----------------------------------------------------------------------------
396 static int rvin_find_pad(struct v4l2_subdev *sd, int direction)
400 if (sd->entity.num_pads <= 1)
403 for (pad = 0; pad < sd->entity.num_pads; pad++)
404 if (sd->entity.pads[pad].flags & direction)
410 /* -----------------------------------------------------------------------------
411 * Parallel async notifier
414 /* The vin lock should be held when calling the subdevice attach and detach */
415 static int rvin_parallel_subdevice_attach(struct rvin_dev *vin,
416 struct v4l2_subdev *subdev)
418 struct v4l2_subdev_mbus_code_enum code = {
419 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
423 /* Find source and sink pad of remote subdevice */
424 ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
427 vin->parallel->source_pad = ret;
429 ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
430 vin->parallel->sink_pad = ret < 0 ? 0 : ret;
432 if (vin->info->use_mc) {
433 vin->parallel->subdev = subdev;
437 /* Find compatible subdevices mbus format */
440 code.pad = vin->parallel->source_pad;
441 while (!vin->mbus_code &&
442 !v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
445 case MEDIA_BUS_FMT_YUYV8_1X16:
446 case MEDIA_BUS_FMT_UYVY8_1X16:
447 case MEDIA_BUS_FMT_UYVY8_2X8:
448 case MEDIA_BUS_FMT_UYVY10_2X10:
449 case MEDIA_BUS_FMT_RGB888_1X24:
450 vin->mbus_code = code.code;
451 vin_dbg(vin, "Found media bus format for %s: %d\n",
452 subdev->name, vin->mbus_code);
459 if (!vin->mbus_code) {
460 vin_err(vin, "Unsupported media bus format for %s\n",
466 ret = v4l2_subdev_call(subdev, video, g_tvnorms, &vin->vdev.tvnorms);
467 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
471 vin->std = V4L2_STD_UNKNOWN;
472 ret = v4l2_subdev_call(subdev, video, g_std, &vin->std);
473 if (ret < 0 && ret != -ENOIOCTLCMD)
476 /* Add the controls */
477 ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 16);
481 ret = v4l2_ctrl_add_handler(&vin->ctrl_handler, subdev->ctrl_handler,
484 v4l2_ctrl_handler_free(&vin->ctrl_handler);
488 vin->vdev.ctrl_handler = &vin->ctrl_handler;
490 vin->parallel->subdev = subdev;
495 static void rvin_parallel_subdevice_detach(struct rvin_dev *vin)
497 rvin_v4l2_unregister(vin);
498 vin->parallel->subdev = NULL;
500 if (!vin->info->use_mc) {
501 v4l2_ctrl_handler_free(&vin->ctrl_handler);
502 vin->vdev.ctrl_handler = NULL;
506 static int rvin_parallel_notify_complete(struct v4l2_async_notifier *notifier)
508 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
509 struct media_entity *source;
510 struct media_entity *sink;
513 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
515 vin_err(vin, "Failed to register subdev nodes\n");
519 if (!video_is_registered(&vin->vdev)) {
520 ret = rvin_v4l2_register(vin);
525 if (!vin->info->use_mc)
528 /* If we're running with media-controller, link the subdevs. */
529 source = &vin->parallel->subdev->entity;
530 sink = &vin->vdev.entity;
532 ret = media_create_pad_link(source, vin->parallel->source_pad,
533 sink, vin->parallel->sink_pad, 0);
535 vin_err(vin, "Error adding link from %s to %s: %d\n",
536 source->name, sink->name, ret);
541 static void rvin_parallel_notify_unbind(struct v4l2_async_notifier *notifier,
542 struct v4l2_subdev *subdev,
543 struct v4l2_async_subdev *asd)
545 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
547 vin_dbg(vin, "unbind parallel subdev %s\n", subdev->name);
549 mutex_lock(&vin->lock);
550 rvin_parallel_subdevice_detach(vin);
551 mutex_unlock(&vin->lock);
554 static int rvin_parallel_notify_bound(struct v4l2_async_notifier *notifier,
555 struct v4l2_subdev *subdev,
556 struct v4l2_async_subdev *asd)
558 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
561 mutex_lock(&vin->lock);
562 ret = rvin_parallel_subdevice_attach(vin, subdev);
563 mutex_unlock(&vin->lock);
567 v4l2_set_subdev_hostdata(subdev, vin);
569 vin_dbg(vin, "bound subdev %s source pad: %u sink pad: %u\n",
570 subdev->name, vin->parallel->source_pad,
571 vin->parallel->sink_pad);
576 static const struct v4l2_async_notifier_operations rvin_parallel_notify_ops = {
577 .bound = rvin_parallel_notify_bound,
578 .unbind = rvin_parallel_notify_unbind,
579 .complete = rvin_parallel_notify_complete,
582 static int rvin_parallel_parse_v4l2(struct device *dev,
583 struct v4l2_fwnode_endpoint *vep,
584 struct v4l2_async_subdev *asd)
586 struct rvin_dev *vin = dev_get_drvdata(dev);
587 struct rvin_parallel_entity *rvpe =
588 container_of(asd, struct rvin_parallel_entity, asd);
590 if (vep->base.port || vep->base.id)
593 vin->parallel = rvpe;
594 vin->parallel->mbus_type = vep->bus_type;
596 switch (vin->parallel->mbus_type) {
597 case V4L2_MBUS_PARALLEL:
598 vin_dbg(vin, "Found PARALLEL media bus\n");
599 vin->parallel->mbus_flags = vep->bus.parallel.flags;
601 case V4L2_MBUS_BT656:
602 vin_dbg(vin, "Found BT656 media bus\n");
603 vin->parallel->mbus_flags = 0;
606 vin_err(vin, "Unknown media bus type\n");
613 static int rvin_parallel_init(struct rvin_dev *vin)
617 v4l2_async_notifier_init(&vin->notifier);
619 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
620 vin->dev, &vin->notifier, sizeof(struct rvin_parallel_entity),
621 0, rvin_parallel_parse_v4l2);
625 /* If using mc, it's fine not to have any input registered. */
627 return vin->info->use_mc ? 0 : -ENODEV;
629 vin_dbg(vin, "Found parallel subdevice %pOF\n",
630 to_of_node(vin->parallel->asd.match.fwnode));
632 vin->notifier.ops = &rvin_parallel_notify_ops;
633 ret = v4l2_async_notifier_register(&vin->v4l2_dev, &vin->notifier);
635 vin_err(vin, "Notifier registration failed\n");
636 v4l2_async_notifier_cleanup(&vin->group->notifier);
643 /* -----------------------------------------------------------------------------
644 * Group async notifier
647 static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier)
649 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
650 const struct rvin_group_route *route;
654 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
656 vin_err(vin, "Failed to register subdev nodes\n");
660 /* Register all video nodes for the group. */
661 for (i = 0; i < RCAR_VIN_NUM; i++) {
662 if (vin->group->vin[i] &&
663 !video_is_registered(&vin->group->vin[i]->vdev)) {
664 ret = rvin_v4l2_register(vin->group->vin[i]);
670 /* Create all media device links between VINs and CSI-2's. */
671 mutex_lock(&vin->group->lock);
672 for (route = vin->info->routes; route->mask; route++) {
673 struct media_pad *source_pad, *sink_pad;
674 struct media_entity *source, *sink;
675 unsigned int source_idx;
677 /* Check that VIN is part of the group. */
678 if (!vin->group->vin[route->vin])
681 /* Check that VIN' master is part of the group. */
682 if (!vin->group->vin[rvin_group_id_to_master(route->vin)])
685 /* Check that CSI-2 is part of the group. */
686 if (!vin->group->csi[route->csi].subdev)
689 source = &vin->group->csi[route->csi].subdev->entity;
690 source_idx = rvin_group_csi_channel_to_pad(route->channel);
691 source_pad = &source->pads[source_idx];
693 sink = &vin->group->vin[route->vin]->vdev.entity;
694 sink_pad = &sink->pads[0];
696 /* Skip if link already exists. */
697 if (media_entity_find_link(source_pad, sink_pad))
700 ret = media_create_pad_link(source, source_idx, sink, 0, 0);
702 vin_err(vin, "Error adding link from %s to %s\n",
703 source->name, sink->name);
707 mutex_unlock(&vin->group->lock);
712 static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier,
713 struct v4l2_subdev *subdev,
714 struct v4l2_async_subdev *asd)
716 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
719 for (i = 0; i < RCAR_VIN_NUM; i++)
720 if (vin->group->vin[i])
721 rvin_v4l2_unregister(vin->group->vin[i]);
723 mutex_lock(&vin->group->lock);
725 for (i = 0; i < RVIN_CSI_MAX; i++) {
726 if (vin->group->csi[i].fwnode != asd->match.fwnode)
728 vin->group->csi[i].subdev = NULL;
729 vin_dbg(vin, "Unbind CSI-2 %s from slot %u\n", subdev->name, i);
733 mutex_unlock(&vin->group->lock);
736 static int rvin_group_notify_bound(struct v4l2_async_notifier *notifier,
737 struct v4l2_subdev *subdev,
738 struct v4l2_async_subdev *asd)
740 struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
743 mutex_lock(&vin->group->lock);
745 for (i = 0; i < RVIN_CSI_MAX; i++) {
746 if (vin->group->csi[i].fwnode != asd->match.fwnode)
748 vin->group->csi[i].subdev = subdev;
749 vin_dbg(vin, "Bound CSI-2 %s to slot %u\n", subdev->name, i);
753 mutex_unlock(&vin->group->lock);
758 static const struct v4l2_async_notifier_operations rvin_group_notify_ops = {
759 .bound = rvin_group_notify_bound,
760 .unbind = rvin_group_notify_unbind,
761 .complete = rvin_group_notify_complete,
764 static int rvin_mc_parse_of_endpoint(struct device *dev,
765 struct v4l2_fwnode_endpoint *vep,
766 struct v4l2_async_subdev *asd)
768 struct rvin_dev *vin = dev_get_drvdata(dev);
771 if (vep->base.port != 1 || vep->base.id >= RVIN_CSI_MAX)
774 if (!of_device_is_available(to_of_node(asd->match.fwnode))) {
775 vin_dbg(vin, "OF device %pOF disabled, ignoring\n",
776 to_of_node(asd->match.fwnode));
780 mutex_lock(&vin->group->lock);
782 if (vin->group->csi[vep->base.id].fwnode) {
783 vin_dbg(vin, "OF device %pOF already handled\n",
784 to_of_node(asd->match.fwnode));
789 vin->group->csi[vep->base.id].fwnode = asd->match.fwnode;
791 vin_dbg(vin, "Add group OF device %pOF to slot %u\n",
792 to_of_node(asd->match.fwnode), vep->base.id);
794 mutex_unlock(&vin->group->lock);
799 static int rvin_mc_parse_of_graph(struct rvin_dev *vin)
801 unsigned int count = 0, vin_mask = 0;
805 mutex_lock(&vin->group->lock);
807 /* If not all VIN's are registered don't register the notifier. */
808 for (i = 0; i < RCAR_VIN_NUM; i++) {
809 if (vin->group->vin[i]) {
815 if (vin->group->count != count) {
816 mutex_unlock(&vin->group->lock);
820 mutex_unlock(&vin->group->lock);
822 v4l2_async_notifier_init(&vin->group->notifier);
825 * Have all VIN's look for CSI-2 subdevices. Some subdevices will
826 * overlap but the parser function can handle it, so each subdevice
827 * will only be registered once with the group notifier.
829 for (i = 0; i < RCAR_VIN_NUM; i++) {
830 if (!(vin_mask & BIT(i)))
833 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
834 vin->group->vin[i]->dev, &vin->group->notifier,
835 sizeof(struct v4l2_async_subdev), 1,
836 rvin_mc_parse_of_endpoint);
841 if (list_empty(&vin->group->notifier.asd_list))
844 vin->group->notifier.ops = &rvin_group_notify_ops;
845 ret = v4l2_async_notifier_register(&vin->v4l2_dev,
846 &vin->group->notifier);
848 vin_err(vin, "Notifier registration failed\n");
849 v4l2_async_notifier_cleanup(&vin->group->notifier);
856 static int rvin_mc_init(struct rvin_dev *vin)
860 vin->pad.flags = MEDIA_PAD_FL_SINK;
861 ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
865 ret = rvin_group_get(vin);
869 ret = rvin_mc_parse_of_graph(vin);
876 /* -----------------------------------------------------------------------------
877 * Platform Device Driver
880 static const struct rvin_info rcar_info_h1 = {
887 static const struct rvin_info rcar_info_m1 = {
894 static const struct rvin_info rcar_info_gen2 = {
901 static const struct rvin_group_route rcar_info_r8a7795_routes[] = {
902 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
903 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
904 { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
905 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
906 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
907 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
908 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
909 { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
910 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
911 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
912 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
913 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
914 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
915 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
916 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
917 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
918 { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
919 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
920 { .csi = RVIN_CSI41, .channel = 1, .vin = 4, .mask = BIT(2) },
921 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
922 { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
923 { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
924 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
925 { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
926 { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
927 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
928 { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
929 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
930 { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
931 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
932 { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
933 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
937 static const struct rvin_info rcar_info_r8a7795 = {
942 .routes = rcar_info_r8a7795_routes,
945 static const struct rvin_group_route rcar_info_r8a7795es1_routes[] = {
946 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
947 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
948 { .csi = RVIN_CSI21, .channel = 0, .vin = 0, .mask = BIT(2) | BIT(5) },
949 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
950 { .csi = RVIN_CSI21, .channel = 0, .vin = 1, .mask = BIT(1) },
951 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
952 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
953 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
954 { .csi = RVIN_CSI21, .channel = 1, .vin = 1, .mask = BIT(5) },
955 { .csi = RVIN_CSI21, .channel = 0, .vin = 2, .mask = BIT(0) },
956 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
957 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
958 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
959 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
960 { .csi = RVIN_CSI21, .channel = 2, .vin = 2, .mask = BIT(5) },
961 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
962 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) },
963 { .csi = RVIN_CSI21, .channel = 1, .vin = 3, .mask = BIT(2) },
964 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
965 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
966 { .csi = RVIN_CSI21, .channel = 3, .vin = 3, .mask = BIT(5) },
967 { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
968 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
969 { .csi = RVIN_CSI21, .channel = 0, .vin = 4, .mask = BIT(2) | BIT(5) },
970 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
971 { .csi = RVIN_CSI21, .channel = 0, .vin = 5, .mask = BIT(1) },
972 { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
973 { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(3) },
974 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
975 { .csi = RVIN_CSI21, .channel = 1, .vin = 5, .mask = BIT(5) },
976 { .csi = RVIN_CSI21, .channel = 0, .vin = 6, .mask = BIT(0) },
977 { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
978 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
979 { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
980 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
981 { .csi = RVIN_CSI21, .channel = 2, .vin = 6, .mask = BIT(5) },
982 { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
983 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) },
984 { .csi = RVIN_CSI21, .channel = 1, .vin = 7, .mask = BIT(2) },
985 { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
986 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
987 { .csi = RVIN_CSI21, .channel = 3, .vin = 7, .mask = BIT(5) },
991 static const struct rvin_info rcar_info_r8a7795es1 = {
996 .routes = rcar_info_r8a7795es1_routes,
999 static const struct rvin_group_route rcar_info_r8a7796_routes[] = {
1000 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1001 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
1002 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
1003 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1004 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
1005 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
1006 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1007 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
1008 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1009 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
1010 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1011 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) },
1012 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1013 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
1014 { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1015 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
1016 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
1017 { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
1018 { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(3) },
1019 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
1020 { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
1021 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
1022 { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
1023 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
1024 { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
1025 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) },
1026 { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
1027 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
1031 static const struct rvin_info rcar_info_r8a7796 = {
1036 .routes = rcar_info_r8a7796_routes,
1039 static const struct rvin_group_route rcar_info_r8a77965_routes[] = {
1040 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1041 { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
1042 { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
1043 { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
1044 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
1045 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1046 { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
1047 { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
1048 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1049 { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
1050 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1051 { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
1052 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1053 { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
1054 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1055 { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
1056 { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1057 { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
1058 { .csi = RVIN_CSI40, .channel = 1, .vin = 4, .mask = BIT(2) },
1059 { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
1060 { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
1061 { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
1062 { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
1063 { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
1064 { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
1065 { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
1066 { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
1067 { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
1068 { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
1069 { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
1070 { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
1071 { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
1075 static const struct rvin_info rcar_info_r8a77965 = {
1080 .routes = rcar_info_r8a77965_routes,
1083 static const struct rvin_group_route rcar_info_r8a77970_routes[] = {
1084 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1085 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1086 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
1087 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1088 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1089 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1090 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1094 static const struct rvin_info rcar_info_r8a77970 = {
1099 .routes = rcar_info_r8a77970_routes,
1102 static const struct rvin_group_route rcar_info_r8a77980_routes[] = {
1103 { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
1104 { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
1105 { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
1106 { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
1107 { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
1108 { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
1109 { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
1110 { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
1111 { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1112 { .csi = RVIN_CSI41, .channel = 1, .vin = 4, .mask = BIT(2) },
1113 { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
1114 { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
1115 { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
1116 { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
1117 { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
1118 { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
1122 static const struct rvin_info rcar_info_r8a77980 = {
1127 .routes = rcar_info_r8a77980_routes,
1130 static const struct rvin_group_route rcar_info_r8a77990_routes[] = {
1131 { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
1132 { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
1133 { .csi = RVIN_CSI40, .channel = 1, .vin = 4, .mask = BIT(2) },
1134 { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
1138 static const struct rvin_info rcar_info_r8a77990 = {
1143 .routes = rcar_info_r8a77990_routes,
1146 static const struct rvin_group_route rcar_info_r8a77995_routes[] = {
1150 static const struct rvin_info rcar_info_r8a77995 = {
1155 .routes = rcar_info_r8a77995_routes,
1158 static const struct of_device_id rvin_of_id_table[] = {
1160 .compatible = "renesas,vin-r8a774a1",
1161 .data = &rcar_info_r8a7796,
1164 .compatible = "renesas,vin-r8a774c0",
1165 .data = &rcar_info_r8a77990,
1168 .compatible = "renesas,vin-r8a7778",
1169 .data = &rcar_info_m1,
1172 .compatible = "renesas,vin-r8a7779",
1173 .data = &rcar_info_h1,
1176 .compatible = "renesas,vin-r8a7790",
1177 .data = &rcar_info_gen2,
1180 .compatible = "renesas,vin-r8a7791",
1181 .data = &rcar_info_gen2,
1184 .compatible = "renesas,vin-r8a7793",
1185 .data = &rcar_info_gen2,
1188 .compatible = "renesas,vin-r8a7794",
1189 .data = &rcar_info_gen2,
1192 .compatible = "renesas,rcar-gen2-vin",
1193 .data = &rcar_info_gen2,
1196 .compatible = "renesas,vin-r8a7795",
1197 .data = &rcar_info_r8a7795,
1200 .compatible = "renesas,vin-r8a7796",
1201 .data = &rcar_info_r8a7796,
1204 .compatible = "renesas,vin-r8a77965",
1205 .data = &rcar_info_r8a77965,
1208 .compatible = "renesas,vin-r8a77970",
1209 .data = &rcar_info_r8a77970,
1212 .compatible = "renesas,vin-r8a77980",
1213 .data = &rcar_info_r8a77980,
1216 .compatible = "renesas,vin-r8a77990",
1217 .data = &rcar_info_r8a77990,
1220 .compatible = "renesas,vin-r8a77995",
1221 .data = &rcar_info_r8a77995,
1225 MODULE_DEVICE_TABLE(of, rvin_of_id_table);
1227 static const struct soc_device_attribute r8a7795es1[] = {
1229 .soc_id = "r8a7795", .revision = "ES1.*",
1230 .data = &rcar_info_r8a7795es1,
1235 static int rcar_vin_probe(struct platform_device *pdev)
1237 const struct soc_device_attribute *attr;
1238 struct rvin_dev *vin;
1239 struct resource *mem;
1242 vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
1246 vin->dev = &pdev->dev;
1247 vin->info = of_device_get_match_data(&pdev->dev);
1250 * Special care is needed on r8a7795 ES1.x since it
1251 * uses different routing than r8a7795 ES2.0.
1253 attr = soc_device_match(r8a7795es1);
1255 vin->info = attr->data;
1257 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1261 vin->base = devm_ioremap_resource(vin->dev, mem);
1262 if (IS_ERR(vin->base))
1263 return PTR_ERR(vin->base);
1265 irq = platform_get_irq(pdev, 0);
1269 ret = rvin_dma_register(vin, irq);
1273 platform_set_drvdata(pdev, vin);
1275 if (vin->info->use_mc) {
1276 ret = rvin_mc_init(vin);
1278 goto error_dma_unregister;
1281 ret = rvin_parallel_init(vin);
1283 goto error_group_unregister;
1285 pm_suspend_ignore_children(&pdev->dev, true);
1286 pm_runtime_enable(&pdev->dev);
1290 error_group_unregister:
1291 if (vin->info->use_mc) {
1292 mutex_lock(&vin->group->lock);
1293 if (&vin->v4l2_dev == vin->group->notifier.v4l2_dev) {
1294 v4l2_async_notifier_unregister(&vin->group->notifier);
1295 v4l2_async_notifier_cleanup(&vin->group->notifier);
1297 mutex_unlock(&vin->group->lock);
1298 rvin_group_put(vin);
1301 error_dma_unregister:
1302 rvin_dma_unregister(vin);
1307 static int rcar_vin_remove(struct platform_device *pdev)
1309 struct rvin_dev *vin = platform_get_drvdata(pdev);
1311 pm_runtime_disable(&pdev->dev);
1313 rvin_v4l2_unregister(vin);
1315 v4l2_async_notifier_unregister(&vin->notifier);
1316 v4l2_async_notifier_cleanup(&vin->notifier);
1318 if (vin->info->use_mc) {
1319 mutex_lock(&vin->group->lock);
1320 if (&vin->v4l2_dev == vin->group->notifier.v4l2_dev) {
1321 v4l2_async_notifier_unregister(&vin->group->notifier);
1322 v4l2_async_notifier_cleanup(&vin->group->notifier);
1324 mutex_unlock(&vin->group->lock);
1325 rvin_group_put(vin);
1327 v4l2_ctrl_handler_free(&vin->ctrl_handler);
1330 rvin_dma_unregister(vin);
1335 static struct platform_driver rcar_vin_driver = {
1338 .of_match_table = rvin_of_id_table,
1340 .probe = rcar_vin_probe,
1341 .remove = rcar_vin_remove,
1344 module_platform_driver(rcar_vin_driver);
1346 MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
1347 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");
1348 MODULE_LICENSE("GPL");