media: rcar-vin: Breakout media link creation
[platform/kernel/linux-starfive.git] / drivers / media / platform / rcar-vin / rcar-core.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Renesas R-Car VIN
4  *
5  * Copyright (C) 2016 Renesas Electronics Corp.
6  * Copyright (C) 2011-2013 Renesas Solutions Corp.
7  * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
8  * Copyright (C) 2008 Magnus Damm
9  *
10  * Based on the soc-camera rcar_vin driver
11  */
12
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/of_graph.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/sys_soc.h>
21
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-fwnode.h>
24 #include <media/v4l2-mc.h>
25
26 #include "rcar-vin.h"
27
28 /*
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.
34  */
35 #define rvin_group_csi_pad_to_channel(pad) ((pad) - 1)
36 #define rvin_group_csi_channel_to_pad(channel) ((channel) + 1)
37
38 /*
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.
42  */
43 #define rvin_group_id_to_master(vin) ((vin) < 4 ? 0 : 4)
44
45 #define v4l2_dev_to_vin(d)      container_of(d, struct rvin_dev, v4l2_dev)
46
47 /* -----------------------------------------------------------------------------
48  * Gen3 Group Allocator
49  */
50
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.
55  *
56  * But for now this works as on all supported systems there will
57  * be only one group for all instances.
58  */
59
60 static DEFINE_MUTEX(rvin_group_lock);
61 static struct rvin_group *rvin_group_data;
62
63 static void rvin_group_cleanup(struct rvin_group *group)
64 {
65         media_device_cleanup(&group->mdev);
66         mutex_destroy(&group->lock);
67 }
68
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)
72 {
73         struct media_device *mdev = &group->mdev;
74         const struct of_device_id *match;
75         struct device_node *np;
76
77         mutex_init(&group->lock);
78
79         /* Count number of VINs in the system */
80         group->count = 0;
81         for_each_matching_node(np, vin->dev->driver->of_match_table)
82                 if (of_device_is_available(np))
83                         group->count++;
84
85         vin_dbg(vin, "found %u enabled VIN's in DT", group->count);
86
87         group->link_setup = link_setup;
88
89         mdev->dev = vin->dev;
90         mdev->ops = ops;
91
92         match = of_match_node(vin->dev->driver->of_match_table,
93                               vin->dev->of_node);
94
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",
98                  dev_name(mdev->dev));
99
100         media_device_init(mdev);
101
102         return 0;
103 }
104
105 static void rvin_group_release(struct kref *kref)
106 {
107         struct rvin_group *group =
108                 container_of(kref, struct rvin_group, refcount);
109
110         mutex_lock(&rvin_group_lock);
111
112         rvin_group_data = NULL;
113
114         rvin_group_cleanup(group);
115
116         kfree(group);
117
118         mutex_unlock(&rvin_group_lock);
119 }
120
121 static int rvin_group_get(struct rvin_dev *vin,
122                           int (*link_setup)(struct rvin_dev *),
123                           const struct media_device_ops *ops)
124 {
125         struct rvin_group *group;
126         u32 id;
127         int ret;
128
129         /* Make sure VIN id is present and sane */
130         ret = of_property_read_u32(vin->dev->of_node, "renesas,id", &id);
131         if (ret) {
132                 vin_err(vin, "%pOF: No renesas,id property found\n",
133                         vin->dev->of_node);
134                 return -EINVAL;
135         }
136
137         if (id >= RCAR_VIN_NUM) {
138                 vin_err(vin, "%pOF: Invalid renesas,id '%u'\n",
139                         vin->dev->of_node, id);
140                 return -EINVAL;
141         }
142
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);
148         } else {
149                 group = kzalloc(sizeof(*group), GFP_KERNEL);
150                 if (!group) {
151                         ret = -ENOMEM;
152                         goto err_group;
153                 }
154
155                 ret = rvin_group_init(group, vin, link_setup, ops);
156                 if (ret) {
157                         kfree(group);
158                         vin_err(vin, "Failed to initialize group\n");
159                         goto err_group;
160                 }
161
162                 kref_init(&group->refcount);
163
164                 rvin_group_data = group;
165         }
166         mutex_unlock(&rvin_group_lock);
167
168         /* Add VIN to group */
169         mutex_lock(&group->lock);
170
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);
175                 return -EINVAL;
176         }
177
178         group->vin[id] = vin;
179
180         vin->id = id;
181         vin->group = group;
182         vin->v4l2_dev.mdev = &group->mdev;
183
184         mutex_unlock(&group->lock);
185
186         return 0;
187 err_group:
188         mutex_unlock(&rvin_group_lock);
189         return ret;
190 }
191
192 static void rvin_group_put(struct rvin_dev *vin)
193 {
194         struct rvin_group *group = vin->group;
195
196         mutex_lock(&group->lock);
197
198         vin->group = NULL;
199         vin->v4l2_dev.mdev = NULL;
200
201         if (WARN_ON(group->vin[vin->id] != vin))
202                 goto out;
203
204         group->vin[vin->id] = NULL;
205 out:
206         mutex_unlock(&group->lock);
207
208         kref_put(&group->refcount, rvin_group_release);
209 }
210
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)
214 {
215         struct v4l2_subdev *sd;
216         unsigned int i;
217
218         sd = media_entity_to_v4l2_subdev(entity);
219
220         for (i = 0; i < RVIN_REMOTES_MAX; i++)
221                 if (group->remotes[i].subdev == sd)
222                         return i;
223
224         return -ENODEV;
225 }
226
227 static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier)
228 {
229         struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
230         unsigned int i;
231         int ret;
232
233         ret = media_device_register(&vin->group->mdev);
234         if (ret)
235                 return ret;
236
237         ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
238         if (ret) {
239                 vin_err(vin, "Failed to register subdev nodes\n");
240                 return ret;
241         }
242
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]);
248                         if (ret)
249                                 return ret;
250                 }
251         }
252
253         return vin->group->link_setup(vin);
254 }
255
256 static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier,
257                                      struct v4l2_subdev *subdev,
258                                      struct v4l2_async_subdev *asd)
259 {
260         struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
261         unsigned int i;
262
263         for (i = 0; i < RCAR_VIN_NUM; i++)
264                 if (vin->group->vin[i])
265                         rvin_v4l2_unregister(vin->group->vin[i]);
266
267         mutex_lock(&vin->group->lock);
268
269         for (i = 0; i < RVIN_CSI_MAX; i++) {
270                 if (vin->group->remotes[i].asd != asd)
271                         continue;
272                 vin->group->remotes[i].subdev = NULL;
273                 vin_dbg(vin, "Unbind %s from slot %u\n", subdev->name, i);
274                 break;
275         }
276
277         mutex_unlock(&vin->group->lock);
278
279         media_device_unregister(&vin->group->mdev);
280 }
281
282 static int rvin_group_notify_bound(struct v4l2_async_notifier *notifier,
283                                    struct v4l2_subdev *subdev,
284                                    struct v4l2_async_subdev *asd)
285 {
286         struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
287         unsigned int i;
288
289         mutex_lock(&vin->group->lock);
290
291         for (i = 0; i < RVIN_CSI_MAX; i++) {
292                 if (vin->group->remotes[i].asd != asd)
293                         continue;
294                 vin->group->remotes[i].subdev = subdev;
295                 vin_dbg(vin, "Bound %s to slot %u\n", subdev->name, i);
296                 break;
297         }
298
299         mutex_unlock(&vin->group->lock);
300
301         return 0;
302 }
303
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,
308 };
309
310 static int rvin_group_parse_of(struct rvin_dev *vin, unsigned int port,
311                                unsigned int id)
312 {
313         struct fwnode_handle *ep, *fwnode;
314         struct v4l2_fwnode_endpoint vep = {
315                 .bus_type = V4L2_MBUS_CSI2_DPHY,
316         };
317         struct v4l2_async_subdev *asd;
318         int ret;
319
320         ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(vin->dev), port, id, 0);
321         if (!ep)
322                 return 0;
323
324         fwnode = fwnode_graph_get_remote_endpoint(ep);
325         ret = v4l2_fwnode_endpoint_parse(ep, &vep);
326         fwnode_handle_put(ep);
327         if (ret) {
328                 vin_err(vin, "Failed to parse %pOF\n", to_of_node(fwnode));
329                 ret = -EINVAL;
330                 goto out;
331         }
332
333         asd = v4l2_async_nf_add_fwnode(&vin->group->notifier, fwnode,
334                                        struct v4l2_async_subdev);
335         if (IS_ERR(asd)) {
336                 ret = PTR_ERR(asd);
337                 goto out;
338         }
339
340         vin->group->remotes[vep.base.id].asd = asd;
341
342         vin_dbg(vin, "Add group OF device %pOF to slot %u\n",
343                 to_of_node(fwnode), vep.base.id);
344 out:
345         fwnode_handle_put(fwnode);
346
347         return ret;
348 }
349
350 static void rvin_group_notifier_cleanup(struct rvin_dev *vin)
351 {
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);
355         }
356 }
357
358 static int rvin_group_notifier_init(struct rvin_dev *vin, unsigned int port,
359                                     unsigned int max_id)
360 {
361         unsigned int count = 0, vin_mask = 0;
362         unsigned int i, id;
363         int ret;
364
365         mutex_lock(&vin->group->lock);
366
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]) {
370                         count++;
371                         vin_mask |= BIT(i);
372                 }
373         }
374
375         if (vin->group->count != count) {
376                 mutex_unlock(&vin->group->lock);
377                 return 0;
378         }
379
380         mutex_unlock(&vin->group->lock);
381
382         v4l2_async_nf_init(&vin->group->notifier);
383
384         /*
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.
387          */
388         for (i = 0; i < RCAR_VIN_NUM; i++) {
389                 if (!(vin_mask & BIT(i)))
390                         continue;
391
392                 for (id = 0; id < max_id; id++) {
393                         if (vin->group->remotes[id].asd)
394                                 continue;
395
396                         ret = rvin_group_parse_of(vin->group->vin[i], port, id);
397                         if (ret)
398                                 return ret;
399                 }
400         }
401
402         if (list_empty(&vin->group->notifier.asd_list))
403                 return 0;
404
405         vin->group->notifier.ops = &rvin_group_notify_ops;
406         ret = v4l2_async_nf_register(&vin->v4l2_dev, &vin->group->notifier);
407         if (ret < 0) {
408                 vin_err(vin, "Notifier registration failed\n");
409                 v4l2_async_nf_cleanup(&vin->group->notifier);
410                 return ret;
411         }
412
413         return 0;
414 }
415
416 /* -----------------------------------------------------------------------------
417  * Controls
418  */
419
420 static int rvin_s_ctrl(struct v4l2_ctrl *ctrl)
421 {
422         struct rvin_dev *vin =
423                 container_of(ctrl->handler, struct rvin_dev, ctrl_handler);
424
425         switch (ctrl->id) {
426         case V4L2_CID_ALPHA_COMPONENT:
427                 rvin_set_alpha(vin, ctrl->val);
428                 break;
429         }
430
431         return 0;
432 }
433
434 static const struct v4l2_ctrl_ops rvin_ctrl_ops = {
435         .s_ctrl = rvin_s_ctrl,
436 };
437
438 static void rvin_free_controls(struct rvin_dev *vin)
439 {
440         v4l2_ctrl_handler_free(&vin->ctrl_handler);
441         vin->vdev.ctrl_handler = NULL;
442 }
443
444 static int rvin_create_controls(struct rvin_dev *vin, struct v4l2_subdev *subdev)
445 {
446         int ret;
447
448         ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 16);
449         if (ret < 0)
450                 return ret;
451
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);
455
456         if (vin->ctrl_handler.error) {
457                 ret = vin->ctrl_handler.error;
458                 rvin_free_controls(vin);
459                 return ret;
460         }
461
462         /* For the non-MC mode add controls from the subdevice. */
463         if (subdev) {
464                 ret = v4l2_ctrl_add_handler(&vin->ctrl_handler,
465                                             subdev->ctrl_handler, NULL, true);
466                 if (ret < 0) {
467                         rvin_free_controls(vin);
468                         return ret;
469                 }
470         }
471
472         vin->vdev.ctrl_handler = &vin->ctrl_handler;
473
474         return 0;
475 }
476
477 /* -----------------------------------------------------------------------------
478  * Async notifier
479  */
480
481 static int rvin_find_pad(struct v4l2_subdev *sd, int direction)
482 {
483         unsigned int pad;
484
485         if (sd->entity.num_pads <= 1)
486                 return 0;
487
488         for (pad = 0; pad < sd->entity.num_pads; pad++)
489                 if (sd->entity.pads[pad].flags & direction)
490                         return pad;
491
492         return -EINVAL;
493 }
494
495 /* -----------------------------------------------------------------------------
496  * Parallel async notifier
497  */
498
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)
502 {
503         struct v4l2_subdev_mbus_code_enum code = {
504                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
505         };
506         int ret;
507
508         /* Find source and sink pad of remote subdevice */
509         ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
510         if (ret < 0)
511                 return ret;
512         vin->parallel.source_pad = ret;
513
514         ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
515         vin->parallel.sink_pad = ret < 0 ? 0 : ret;
516
517         if (vin->info->use_mc) {
518                 vin->parallel.subdev = subdev;
519                 return 0;
520         }
521
522         /* Find compatible subdevices mbus format */
523         vin->mbus_code = 0;
524         code.index = 0;
525         code.pad = vin->parallel.source_pad;
526         while (!vin->mbus_code &&
527                !v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
528                 code.index++;
529                 switch (code.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);
538                         break;
539                 default:
540                         break;
541                 }
542         }
543
544         if (!vin->mbus_code) {
545                 vin_err(vin, "Unsupported media bus format for %s\n",
546                         subdev->name);
547                 return -EINVAL;
548         }
549
550         /* Read tvnorms */
551         ret = v4l2_subdev_call(subdev, video, g_tvnorms, &vin->vdev.tvnorms);
552         if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
553                 return ret;
554
555         /* Read standard */
556         vin->std = V4L2_STD_UNKNOWN;
557         ret = v4l2_subdev_call(subdev, video, g_std, &vin->std);
558         if (ret < 0 && ret != -ENOIOCTLCMD)
559                 return ret;
560
561         /* Add the controls */
562         ret = rvin_create_controls(vin, subdev);
563         if (ret < 0)
564                 return ret;
565
566         vin->parallel.subdev = subdev;
567
568         return 0;
569 }
570
571 static void rvin_parallel_subdevice_detach(struct rvin_dev *vin)
572 {
573         rvin_v4l2_unregister(vin);
574         vin->parallel.subdev = NULL;
575
576         if (!vin->info->use_mc)
577                 rvin_free_controls(vin);
578 }
579
580 static int rvin_parallel_notify_complete(struct v4l2_async_notifier *notifier)
581 {
582         struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
583         struct media_entity *source;
584         struct media_entity *sink;
585         int ret;
586
587         ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
588         if (ret < 0) {
589                 vin_err(vin, "Failed to register subdev nodes\n");
590                 return ret;
591         }
592
593         if (!video_is_registered(&vin->vdev)) {
594                 ret = rvin_v4l2_register(vin);
595                 if (ret < 0)
596                         return ret;
597         }
598
599         if (!vin->info->use_mc)
600                 return 0;
601
602         /* If we're running with media-controller, link the subdevs. */
603         source = &vin->parallel.subdev->entity;
604         sink = &vin->vdev.entity;
605
606         ret = media_create_pad_link(source, vin->parallel.source_pad,
607                                     sink, vin->parallel.sink_pad, 0);
608         if (ret)
609                 vin_err(vin, "Error adding link from %s to %s: %d\n",
610                         source->name, sink->name, ret);
611
612         return ret;
613 }
614
615 static void rvin_parallel_notify_unbind(struct v4l2_async_notifier *notifier,
616                                         struct v4l2_subdev *subdev,
617                                         struct v4l2_async_subdev *asd)
618 {
619         struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
620
621         vin_dbg(vin, "unbind parallel subdev %s\n", subdev->name);
622
623         mutex_lock(&vin->lock);
624         rvin_parallel_subdevice_detach(vin);
625         mutex_unlock(&vin->lock);
626 }
627
628 static int rvin_parallel_notify_bound(struct v4l2_async_notifier *notifier,
629                                       struct v4l2_subdev *subdev,
630                                       struct v4l2_async_subdev *asd)
631 {
632         struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
633         int ret;
634
635         mutex_lock(&vin->lock);
636         ret = rvin_parallel_subdevice_attach(vin, subdev);
637         mutex_unlock(&vin->lock);
638         if (ret)
639                 return ret;
640
641         v4l2_set_subdev_hostdata(subdev, vin);
642
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);
646
647         return 0;
648 }
649
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,
654 };
655
656 static int rvin_parallel_parse_of(struct rvin_dev *vin)
657 {
658         struct fwnode_handle *ep, *fwnode;
659         struct v4l2_fwnode_endpoint vep = {
660                 .bus_type = V4L2_MBUS_UNKNOWN,
661         };
662         struct v4l2_async_subdev *asd;
663         int ret;
664
665         ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(vin->dev), 0, 0, 0);
666         if (!ep)
667                 return 0;
668
669         fwnode = fwnode_graph_get_remote_endpoint(ep);
670         ret = v4l2_fwnode_endpoint_parse(ep, &vep);
671         fwnode_handle_put(ep);
672         if (ret) {
673                 vin_err(vin, "Failed to parse %pOF\n", to_of_node(fwnode));
674                 ret = -EINVAL;
675                 goto out;
676         }
677
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;
686                 break;
687         default:
688                 vin_err(vin, "Unknown media bus type\n");
689                 ret = -EINVAL;
690                 goto out;
691         }
692
693         asd = v4l2_async_nf_add_fwnode(&vin->notifier, fwnode,
694                                        struct v4l2_async_subdev);
695         if (IS_ERR(asd)) {
696                 ret = PTR_ERR(asd);
697                 goto out;
698         }
699
700         vin->parallel.asd = asd;
701
702         vin_dbg(vin, "Add parallel OF device %pOF\n", to_of_node(fwnode));
703 out:
704         fwnode_handle_put(fwnode);
705
706         return ret;
707 }
708
709 static void rvin_parallel_cleanup(struct rvin_dev *vin)
710 {
711         v4l2_async_nf_unregister(&vin->notifier);
712         v4l2_async_nf_cleanup(&vin->notifier);
713 }
714
715 static int rvin_parallel_init(struct rvin_dev *vin)
716 {
717         int ret;
718
719         v4l2_async_nf_init(&vin->notifier);
720
721         ret = rvin_parallel_parse_of(vin);
722         if (ret)
723                 return ret;
724
725         if (!vin->parallel.asd)
726                 return -ENODEV;
727
728         vin_dbg(vin, "Found parallel subdevice %pOF\n",
729                 to_of_node(vin->parallel.asd->match.fwnode));
730
731         vin->notifier.ops = &rvin_parallel_notify_ops;
732         ret = v4l2_async_nf_register(&vin->v4l2_dev, &vin->notifier);
733         if (ret < 0) {
734                 vin_err(vin, "Notifier registration failed\n");
735                 v4l2_async_nf_cleanup(&vin->notifier);
736                 return ret;
737         }
738
739         return 0;
740 }
741
742 /* -----------------------------------------------------------------------------
743  * CSI-2
744  */
745
746 static unsigned int rvin_csi2_get_mask(struct rvin_dev *vin,
747                                        enum rvin_csi_id csi_id,
748                                        unsigned char channel)
749 {
750         const struct rvin_group_route *route;
751         unsigned int mask = 0;
752
753         for (route = vin->info->routes; route->mask; route++) {
754                 if (route->vin == vin->id &&
755                     route->csi == csi_id &&
756                     route->channel == channel) {
757                         vin_dbg(vin,
758                                 "Adding route: vin: %d csi: %d channel: %d\n",
759                                 route->vin, route->csi, route->channel);
760                         mask |= route->mask;
761                 }
762         }
763
764         return mask;
765 }
766
767 /*
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.
776  *
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)
782  *    with the bitmask.
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.
787  *
788  * Please note that no link can be enabled if any VIN in the group is
789  * currently open.
790  */
791 static int rvin_csi2_link_notify(struct media_link *link, u32 flags,
792                                  unsigned int notification)
793 {
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;
799         unsigned int i;
800         int csi_id, ret;
801
802         ret = v4l2_pipeline_link_notify(link, flags, notification);
803         if (ret)
804                 return ret;
805
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))
809                 return 0;
810
811         /*
812          * Don't allow link changes if any entity in the graph is
813          * streaming, modifying the CHSEL register fields can disrupt
814          * running streams.
815          */
816         media_device_for_each_entity(entity, &group->mdev)
817                 if (entity->stream_count)
818                         return -EBUSY;
819
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);
823
824         mutex_lock(&group->lock);
825
826         csi_id = rvin_group_entity_to_remote_id(group, link->source->entity);
827         if (csi_id == -ENODEV) {
828                 struct v4l2_subdev *sd;
829
830                 /*
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.
834                  *
835                  * No hardware configuration required for parallel inputs,
836                  * we can return here.
837                  */
838                 sd = media_entity_to_v4l2_subdev(link->source->entity);
839                 for (i = 0; i < RCAR_VIN_NUM; i++) {
840                         if (group->vin[i] &&
841                             group->vin[i]->parallel.subdev == sd) {
842                                 group->vin[i]->is_csi = false;
843                                 ret = 0;
844                                 goto out;
845                         }
846                 }
847
848                 vin_err(vin, "Subdevice %s not registered to any VIN\n",
849                         link->source->entity->name);
850                 ret = -ENODEV;
851         } else {
852                 unsigned int master_id, channel, mask_new;
853                 unsigned int mask = ~0;
854                 struct media_pad *csi_pad;
855
856                 master_id = rvin_group_id_to_master(vin->id);
857
858                 if (WARN_ON(!group->vin[master_id])) {
859                         ret = -ENODEV;
860                         goto out;
861                 }
862
863                 /* Build a mask for already enabled links. */
864                 for (i = master_id; i < master_id + 4; i++) {
865                         if (!group->vin[i])
866                                 continue;
867
868                         /* Get remote CSI-2, if any. */
869                         csi_pad = media_entity_remote_pad(
870                                         &group->vin[i]->vdev.entity.pads[0]);
871                         if (!csi_pad)
872                                 continue;
873
874                         csi_id = rvin_group_entity_to_remote_id(group,
875                                                                 csi_pad->entity);
876                         channel = rvin_group_csi_pad_to_channel(csi_pad->index);
877
878                         mask &= rvin_csi2_get_mask(group->vin[i], csi_id, channel);
879                 }
880
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,
884                         mask_new);
885
886                 if (!mask_new) {
887                         ret = -EMLINK;
888                         goto out;
889                 }
890
891                 /* New valid CHSEL found, set the new value. */
892                 ret = rvin_set_channel_routing(group->vin[master_id],
893                                                __ffs(mask_new));
894                 if (ret)
895                         goto out;
896
897                 vin->is_csi = true;
898         }
899 out:
900         mutex_unlock(&group->lock);
901
902         return ret;
903 }
904
905 static const struct media_device_ops rvin_csi2_media_ops = {
906         .link_notify = rvin_csi2_link_notify,
907 };
908
909 static int rvin_csi2_create_link(struct rvin_group *group,
910                                  const struct rvin_group_route *route)
911
912 {
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];
918
919         /* Skip if link already exists. */
920         if (media_entity_find_link(source_pad, sink_pad))
921                 return 0;
922
923         return media_create_pad_link(source, source_idx, sink, 0, 0);
924 }
925
926 static int rvin_csi2_setup_links(struct rvin_dev *vin)
927 {
928         const struct rvin_group_route *route;
929         int ret = -EINVAL;
930
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])
936                         continue;
937
938                 /* Check that VIN' master is part of the group. */
939                 if (!vin->group->vin[rvin_group_id_to_master(route->vin)])
940                         continue;
941
942                 /* Check that CSI-2 is part of the group. */
943                 if (!vin->group->remotes[route->csi].subdev)
944                         continue;
945
946                 ret = rvin_csi2_create_link(vin->group, route);
947                 if (ret)
948                         break;
949         }
950         mutex_unlock(&vin->group->lock);
951
952         return ret;
953 }
954
955 static void rvin_csi2_cleanup(struct rvin_dev *vin)
956 {
957         rvin_parallel_cleanup(vin);
958         rvin_group_notifier_cleanup(vin);
959         rvin_group_put(vin);
960         rvin_free_controls(vin);
961 }
962
963 static int rvin_csi2_init(struct rvin_dev *vin)
964 {
965         int ret;
966
967         vin->pad.flags = MEDIA_PAD_FL_SINK;
968         ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
969         if (ret)
970                 return ret;
971
972         ret = rvin_create_controls(vin, NULL);
973         if (ret < 0)
974                 return ret;
975
976         ret = rvin_group_get(vin, rvin_csi2_setup_links, &rvin_csi2_media_ops);
977         if (ret)
978                 goto err_controls;
979
980         /* It's OK to not have a parallel subdevice. */
981         ret = rvin_parallel_init(vin);
982         if (ret && ret != -ENODEV)
983                 goto err_group;
984
985         ret = rvin_group_notifier_init(vin, 1, RVIN_CSI_MAX);
986         if (ret)
987                 goto err_parallel;
988
989         return 0;
990 err_parallel:
991         rvin_parallel_cleanup(vin);
992 err_group:
993         rvin_group_put(vin);
994 err_controls:
995         rvin_free_controls(vin);
996
997         return ret;
998 }
999
1000 /* -----------------------------------------------------------------------------
1001  * ISP
1002  */
1003
1004 static int rvin_isp_setup_links(struct rvin_dev *vin)
1005 {
1006         unsigned int i;
1007         int ret = -EINVAL;
1008
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;
1016
1017                 if (!vin->group->vin[i])
1018                         continue;
1019
1020                 /* Check that ISP is part of the group. */
1021                 if (!vin->group->remotes[source_slot].subdev)
1022                         continue;
1023
1024                 source = &vin->group->remotes[source_slot].subdev->entity;
1025                 source_pad = &source->pads[source_idx];
1026
1027                 sink = &vin->group->vin[i]->vdev.entity;
1028                 sink_pad = &sink->pads[0];
1029
1030                 /* Skip if link already exists. */
1031                 if (media_entity_find_link(source_pad, sink_pad))
1032                         continue;
1033
1034                 ret = media_create_pad_link(source, source_idx, sink, 0,
1035                                             MEDIA_LNK_FL_ENABLED |
1036                                             MEDIA_LNK_FL_IMMUTABLE);
1037                 if (ret) {
1038                         vin_err(vin, "Error adding link from %s to %s\n",
1039                                 source->name, sink->name);
1040                         break;
1041                 }
1042         }
1043         mutex_unlock(&vin->group->lock);
1044
1045         return ret;
1046 }
1047
1048 static void rvin_isp_cleanup(struct rvin_dev *vin)
1049 {
1050         rvin_group_notifier_cleanup(vin);
1051         rvin_group_put(vin);
1052         rvin_free_controls(vin);
1053 }
1054
1055 static int rvin_isp_init(struct rvin_dev *vin)
1056 {
1057         int ret;
1058
1059         vin->pad.flags = MEDIA_PAD_FL_SINK;
1060         ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
1061         if (ret)
1062                 return ret;
1063
1064         ret = rvin_create_controls(vin, NULL);
1065         if (ret < 0)
1066                 return ret;
1067
1068         ret = rvin_group_get(vin, rvin_isp_setup_links, NULL);
1069         if (ret)
1070                 goto err_controls;
1071
1072         ret = rvin_group_notifier_init(vin, 2, RVIN_ISP_MAX);
1073         if (ret)
1074                 goto err_group;
1075
1076         return 0;
1077 err_group:
1078         rvin_group_put(vin);
1079 err_controls:
1080         rvin_free_controls(vin);
1081
1082         return ret;
1083 }
1084
1085 /* -----------------------------------------------------------------------------
1086  * Suspend / Resume
1087  */
1088
1089 static int __maybe_unused rvin_suspend(struct device *dev)
1090 {
1091         struct rvin_dev *vin = dev_get_drvdata(dev);
1092
1093         if (vin->state != RUNNING)
1094                 return 0;
1095
1096         rvin_stop_streaming(vin);
1097
1098         vin->state = SUSPENDED;
1099
1100         return 0;
1101 }
1102
1103 static int __maybe_unused rvin_resume(struct device *dev)
1104 {
1105         struct rvin_dev *vin = dev_get_drvdata(dev);
1106
1107         if (vin->state != SUSPENDED)
1108                 return 0;
1109
1110         /*
1111          * Restore group master CHSEL setting.
1112          *
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
1115          * be resumed.
1116          */
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];
1120                 int ret;
1121
1122                 if (WARN_ON(!master))
1123                         return -ENODEV;
1124
1125                 ret = rvin_set_channel_routing(master, master->chsel);
1126                 if (ret)
1127                         return ret;
1128         }
1129
1130         return rvin_start_streaming(vin);
1131 }
1132
1133 /* -----------------------------------------------------------------------------
1134  * Platform Device Driver
1135  */
1136
1137 static const struct rvin_info rcar_info_h1 = {
1138         .model = RCAR_H1,
1139         .use_mc = false,
1140         .max_width = 2048,
1141         .max_height = 2048,
1142 };
1143
1144 static const struct rvin_info rcar_info_m1 = {
1145         .model = RCAR_M1,
1146         .use_mc = false,
1147         .max_width = 2048,
1148         .max_height = 2048,
1149 };
1150
1151 static const struct rvin_info rcar_info_gen2 = {
1152         .model = RCAR_GEN2,
1153         .use_mc = false,
1154         .max_width = 2048,
1155         .max_height = 2048,
1156 };
1157
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) },
1183         { /* Sentinel */ }
1184 };
1185
1186 static const struct rvin_info rcar_info_r8a774e1 = {
1187         .model = RCAR_GEN3,
1188         .use_mc = true,
1189         .max_width = 4096,
1190         .max_height = 4096,
1191         .routes = rcar_info_r8a774e1_routes,
1192 };
1193
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) },
1227         { /* Sentinel */ }
1228 };
1229
1230 static const struct rvin_info rcar_info_r8a7795 = {
1231         .model = RCAR_GEN3,
1232         .use_mc = true,
1233         .nv12 = true,
1234         .max_width = 4096,
1235         .max_height = 4096,
1236         .routes = rcar_info_r8a7795_routes,
1237 };
1238
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) },
1282         { /* Sentinel */ }
1283 };
1284
1285 static const struct rvin_info rcar_info_r8a7795es1 = {
1286         .model = RCAR_GEN3,
1287         .use_mc = true,
1288         .max_width = 4096,
1289         .max_height = 4096,
1290         .routes = rcar_info_r8a7795es1_routes,
1291 };
1292
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) },
1322         { /* Sentinel */ }
1323 };
1324
1325 static const struct rvin_info rcar_info_r8a7796 = {
1326         .model = RCAR_GEN3,
1327         .use_mc = true,
1328         .nv12 = true,
1329         .max_width = 4096,
1330         .max_height = 4096,
1331         .routes = rcar_info_r8a7796_routes,
1332 };
1333
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) },
1367         { /* Sentinel */ }
1368 };
1369
1370 static const struct rvin_info rcar_info_r8a77965 = {
1371         .model = RCAR_GEN3,
1372         .use_mc = true,
1373         .nv12 = true,
1374         .max_width = 4096,
1375         .max_height = 4096,
1376         .routes = rcar_info_r8a77965_routes,
1377 };
1378
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) },
1387         { /* Sentinel */ }
1388 };
1389
1390 static const struct rvin_info rcar_info_r8a77970 = {
1391         .model = RCAR_GEN3,
1392         .use_mc = true,
1393         .max_width = 4096,
1394         .max_height = 4096,
1395         .routes = rcar_info_r8a77970_routes,
1396 };
1397
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) },
1415         { /* Sentinel */ }
1416 };
1417
1418 static const struct rvin_info rcar_info_r8a77980 = {
1419         .model = RCAR_GEN3,
1420         .use_mc = true,
1421         .nv12 = true,
1422         .max_width = 4096,
1423         .max_height = 4096,
1424         .routes = rcar_info_r8a77980_routes,
1425 };
1426
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) },
1432         { /* Sentinel */ }
1433 };
1434
1435 static const struct rvin_info rcar_info_r8a77990 = {
1436         .model = RCAR_GEN3,
1437         .use_mc = true,
1438         .nv12 = true,
1439         .max_width = 4096,
1440         .max_height = 4096,
1441         .routes = rcar_info_r8a77990_routes,
1442 };
1443
1444 static const struct rvin_group_route rcar_info_r8a77995_routes[] = {
1445         { /* Sentinel */ }
1446 };
1447
1448 static const struct rvin_info rcar_info_r8a77995 = {
1449         .model = RCAR_GEN3,
1450         .use_mc = true,
1451         .nv12 = true,
1452         .max_width = 4096,
1453         .max_height = 4096,
1454         .routes = rcar_info_r8a77995_routes,
1455 };
1456
1457 static const struct rvin_info rcar_info_r8a779a0 = {
1458         .model = RCAR_GEN3,
1459         .use_mc = true,
1460         .use_isp = true,
1461         .nv12 = true,
1462         .max_width = 4096,
1463         .max_height = 4096,
1464 };
1465
1466 static const struct of_device_id rvin_of_id_table[] = {
1467         {
1468                 .compatible = "renesas,vin-r8a774a1",
1469                 .data = &rcar_info_r8a7796,
1470         },
1471         {
1472                 .compatible = "renesas,vin-r8a774b1",
1473                 .data = &rcar_info_r8a77965,
1474         },
1475         {
1476                 .compatible = "renesas,vin-r8a774c0",
1477                 .data = &rcar_info_r8a77990,
1478         },
1479         {
1480                 .compatible = "renesas,vin-r8a774e1",
1481                 .data = &rcar_info_r8a774e1,
1482         },
1483         {
1484                 .compatible = "renesas,vin-r8a7778",
1485                 .data = &rcar_info_m1,
1486         },
1487         {
1488                 .compatible = "renesas,vin-r8a7779",
1489                 .data = &rcar_info_h1,
1490         },
1491         {
1492                 .compatible = "renesas,rcar-gen2-vin",
1493                 .data = &rcar_info_gen2,
1494         },
1495         {
1496                 .compatible = "renesas,vin-r8a7795",
1497                 .data = &rcar_info_r8a7795,
1498         },
1499         {
1500                 .compatible = "renesas,vin-r8a7796",
1501                 .data = &rcar_info_r8a7796,
1502         },
1503         {
1504                 .compatible = "renesas,vin-r8a77961",
1505                 .data = &rcar_info_r8a7796,
1506         },
1507         {
1508                 .compatible = "renesas,vin-r8a77965",
1509                 .data = &rcar_info_r8a77965,
1510         },
1511         {
1512                 .compatible = "renesas,vin-r8a77970",
1513                 .data = &rcar_info_r8a77970,
1514         },
1515         {
1516                 .compatible = "renesas,vin-r8a77980",
1517                 .data = &rcar_info_r8a77980,
1518         },
1519         {
1520                 .compatible = "renesas,vin-r8a77990",
1521                 .data = &rcar_info_r8a77990,
1522         },
1523         {
1524                 .compatible = "renesas,vin-r8a77995",
1525                 .data = &rcar_info_r8a77995,
1526         },
1527         {
1528                 .compatible = "renesas,vin-r8a779a0",
1529                 .data = &rcar_info_r8a779a0,
1530         },
1531         { /* Sentinel */ },
1532 };
1533 MODULE_DEVICE_TABLE(of, rvin_of_id_table);
1534
1535 static const struct soc_device_attribute r8a7795es1[] = {
1536         {
1537                 .soc_id = "r8a7795", .revision = "ES1.*",
1538                 .data = &rcar_info_r8a7795es1,
1539         },
1540         { /* Sentinel */ }
1541 };
1542
1543 static int rcar_vin_probe(struct platform_device *pdev)
1544 {
1545         const struct soc_device_attribute *attr;
1546         struct rvin_dev *vin;
1547         int irq, ret;
1548
1549         vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
1550         if (!vin)
1551                 return -ENOMEM;
1552
1553         vin->dev = &pdev->dev;
1554         vin->info = of_device_get_match_data(&pdev->dev);
1555         vin->alpha = 0xff;
1556
1557         /*
1558          * Special care is needed on r8a7795 ES1.x since it
1559          * uses different routing than r8a7795 ES2.0.
1560          */
1561         attr = soc_device_match(r8a7795es1);
1562         if (attr)
1563                 vin->info = attr->data;
1564
1565         vin->base = devm_platform_ioremap_resource(pdev, 0);
1566         if (IS_ERR(vin->base))
1567                 return PTR_ERR(vin->base);
1568
1569         irq = platform_get_irq(pdev, 0);
1570         if (irq < 0)
1571                 return irq;
1572
1573         ret = rvin_dma_register(vin, irq);
1574         if (ret)
1575                 return ret;
1576
1577         platform_set_drvdata(pdev, vin);
1578
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);
1583         else
1584                 ret = rvin_parallel_init(vin);
1585
1586         if (ret) {
1587                 rvin_dma_unregister(vin);
1588                 return ret;
1589         }
1590
1591         pm_suspend_ignore_children(&pdev->dev, true);
1592         pm_runtime_enable(&pdev->dev);
1593
1594         return 0;
1595 }
1596
1597 static int rcar_vin_remove(struct platform_device *pdev)
1598 {
1599         struct rvin_dev *vin = platform_get_drvdata(pdev);
1600
1601         pm_runtime_disable(&pdev->dev);
1602
1603         rvin_v4l2_unregister(vin);
1604
1605         if (vin->info->use_isp)
1606                 rvin_isp_cleanup(vin);
1607         else if (vin->info->use_mc)
1608                 rvin_csi2_cleanup(vin);
1609         else
1610                 rvin_parallel_cleanup(vin);
1611
1612         rvin_dma_unregister(vin);
1613
1614         return 0;
1615 }
1616
1617 static SIMPLE_DEV_PM_OPS(rvin_pm_ops, rvin_suspend, rvin_resume);
1618
1619 static struct platform_driver rcar_vin_driver = {
1620         .driver = {
1621                 .name = "rcar-vin",
1622                 .suppress_bind_attrs = true,
1623                 .pm = &rvin_pm_ops,
1624                 .of_match_table = rvin_of_id_table,
1625         },
1626         .probe = rcar_vin_probe,
1627         .remove = rcar_vin_remove,
1628 };
1629
1630 module_platform_driver(rcar_vin_driver);
1631
1632 MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
1633 MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");
1634 MODULE_LICENSE("GPL");