GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / media / platform / vsp1 / vsp1_drv.c
1 /*
2  * vsp1_drv.c  --  R-Car VSP1 Driver
3  *
4  * Copyright (C) 2013-2015 Renesas Electronics Corporation
5  *
6  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/videodev2.h>
24
25 #include <media/rcar-fcp.h>
26 #include <media/v4l2-subdev.h>
27
28 #include "vsp1.h"
29 #include "vsp1_bru.h"
30 #include "vsp1_clu.h"
31 #include "vsp1_dl.h"
32 #include "vsp1_drm.h"
33 #include "vsp1_hgo.h"
34 #include "vsp1_hgt.h"
35 #include "vsp1_hsit.h"
36 #include "vsp1_lif.h"
37 #include "vsp1_lut.h"
38 #include "vsp1_pipe.h"
39 #include "vsp1_rwpf.h"
40 #include "vsp1_sru.h"
41 #include "vsp1_uds.h"
42 #include "vsp1_video.h"
43
44 /* -----------------------------------------------------------------------------
45  * Interrupt Handling
46  */
47
48 static irqreturn_t vsp1_irq_handler(int irq, void *data)
49 {
50         u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
51         struct vsp1_device *vsp1 = data;
52         irqreturn_t ret = IRQ_NONE;
53         unsigned int i;
54         u32 status;
55
56         for (i = 0; i < vsp1->info->wpf_count; ++i) {
57                 struct vsp1_rwpf *wpf = vsp1->wpf[i];
58
59                 if (wpf == NULL)
60                         continue;
61
62                 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
63                 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
64
65                 if (status & VI6_WFP_IRQ_STA_DFE) {
66                         vsp1_pipeline_frame_end(wpf->pipe);
67                         ret = IRQ_HANDLED;
68                 }
69         }
70
71         return ret;
72 }
73
74 /* -----------------------------------------------------------------------------
75  * Entities
76  */
77
78 /*
79  * vsp1_create_sink_links - Create links from all sources to the given sink
80  *
81  * This function creates media links from all valid sources to the given sink
82  * pad. Links that would be invalid according to the VSP1 hardware capabilities
83  * are skipped. Those include all links
84  *
85  * - from a UDS to a UDS (UDS entities can't be chained)
86  * - from an entity to itself (no loops are allowed)
87  *
88  * Furthermore, the BRS can't be connected to histogram generators, but no
89  * special check is currently needed as all VSP instances that include a BRS
90  * have no histogram generator.
91  */
92 static int vsp1_create_sink_links(struct vsp1_device *vsp1,
93                                   struct vsp1_entity *sink)
94 {
95         struct media_entity *entity = &sink->subdev.entity;
96         struct vsp1_entity *source;
97         unsigned int pad;
98         int ret;
99
100         list_for_each_entry(source, &vsp1->entities, list_dev) {
101                 u32 flags;
102
103                 if (source->type == sink->type)
104                         continue;
105
106                 if (source->type == VSP1_ENTITY_HGO ||
107                     source->type == VSP1_ENTITY_HGT ||
108                     source->type == VSP1_ENTITY_LIF ||
109                     source->type == VSP1_ENTITY_WPF)
110                         continue;
111
112                 flags = source->type == VSP1_ENTITY_RPF &&
113                         sink->type == VSP1_ENTITY_WPF &&
114                         source->index == sink->index
115                       ? MEDIA_LNK_FL_ENABLED : 0;
116
117                 for (pad = 0; pad < entity->num_pads; ++pad) {
118                         if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
119                                 continue;
120
121                         ret = media_create_pad_link(&source->subdev.entity,
122                                                        source->source_pad,
123                                                        entity, pad, flags);
124                         if (ret < 0)
125                                 return ret;
126
127                         if (flags & MEDIA_LNK_FL_ENABLED)
128                                 source->sink = sink;
129                 }
130         }
131
132         return 0;
133 }
134
135 static int vsp1_uapi_create_links(struct vsp1_device *vsp1)
136 {
137         struct vsp1_entity *entity;
138         unsigned int i;
139         int ret;
140
141         list_for_each_entry(entity, &vsp1->entities, list_dev) {
142                 if (entity->type == VSP1_ENTITY_LIF ||
143                     entity->type == VSP1_ENTITY_RPF)
144                         continue;
145
146                 ret = vsp1_create_sink_links(vsp1, entity);
147                 if (ret < 0)
148                         return ret;
149         }
150
151         if (vsp1->hgo) {
152                 ret = media_create_pad_link(&vsp1->hgo->histo.entity.subdev.entity,
153                                             HISTO_PAD_SOURCE,
154                                             &vsp1->hgo->histo.video.entity, 0,
155                                             MEDIA_LNK_FL_ENABLED |
156                                             MEDIA_LNK_FL_IMMUTABLE);
157                 if (ret < 0)
158                         return ret;
159         }
160
161         if (vsp1->hgt) {
162                 ret = media_create_pad_link(&vsp1->hgt->histo.entity.subdev.entity,
163                                             HISTO_PAD_SOURCE,
164                                             &vsp1->hgt->histo.video.entity, 0,
165                                             MEDIA_LNK_FL_ENABLED |
166                                             MEDIA_LNK_FL_IMMUTABLE);
167                 if (ret < 0)
168                         return ret;
169         }
170
171         for (i = 0; i < vsp1->info->lif_count; ++i) {
172                 if (!vsp1->lif[i])
173                         continue;
174
175                 ret = media_create_pad_link(&vsp1->wpf[i]->entity.subdev.entity,
176                                             RWPF_PAD_SOURCE,
177                                             &vsp1->lif[i]->entity.subdev.entity,
178                                             LIF_PAD_SINK, 0);
179                 if (ret < 0)
180                         return ret;
181         }
182
183         for (i = 0; i < vsp1->info->rpf_count; ++i) {
184                 struct vsp1_rwpf *rpf = vsp1->rpf[i];
185
186                 ret = media_create_pad_link(&rpf->video->video.entity, 0,
187                                             &rpf->entity.subdev.entity,
188                                             RWPF_PAD_SINK,
189                                             MEDIA_LNK_FL_ENABLED |
190                                             MEDIA_LNK_FL_IMMUTABLE);
191                 if (ret < 0)
192                         return ret;
193         }
194
195         for (i = 0; i < vsp1->info->wpf_count; ++i) {
196                 /*
197                  * Connect the video device to the WPF. All connections are
198                  * immutable.
199                  */
200                 struct vsp1_rwpf *wpf = vsp1->wpf[i];
201
202                 ret = media_create_pad_link(&wpf->entity.subdev.entity,
203                                             RWPF_PAD_SOURCE,
204                                             &wpf->video->video.entity, 0,
205                                             MEDIA_LNK_FL_IMMUTABLE |
206                                             MEDIA_LNK_FL_ENABLED);
207                 if (ret < 0)
208                         return ret;
209         }
210
211         return 0;
212 }
213
214 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
215 {
216         struct vsp1_entity *entity, *_entity;
217         struct vsp1_video *video, *_video;
218
219         list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
220                 list_del(&entity->list_dev);
221                 vsp1_entity_destroy(entity);
222         }
223
224         list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
225                 list_del(&video->list);
226                 vsp1_video_cleanup(video);
227         }
228
229         v4l2_device_unregister(&vsp1->v4l2_dev);
230         if (vsp1->info->uapi)
231                 media_device_unregister(&vsp1->media_dev);
232         media_device_cleanup(&vsp1->media_dev);
233
234         if (!vsp1->info->uapi)
235                 vsp1_drm_cleanup(vsp1);
236 }
237
238 static int vsp1_create_entities(struct vsp1_device *vsp1)
239 {
240         struct media_device *mdev = &vsp1->media_dev;
241         struct v4l2_device *vdev = &vsp1->v4l2_dev;
242         struct vsp1_entity *entity;
243         unsigned int i;
244         int ret;
245
246         mdev->dev = vsp1->dev;
247         mdev->hw_revision = vsp1->version;
248         strlcpy(mdev->model, vsp1->info->model, sizeof(mdev->model));
249         snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
250                  dev_name(mdev->dev));
251         media_device_init(mdev);
252
253         vsp1->media_ops.link_setup = vsp1_entity_link_setup;
254         /*
255          * Don't perform link validation when the userspace API is disabled as
256          * the pipeline is configured internally by the driver in that case, and
257          * its configuration can thus be trusted.
258          */
259         if (vsp1->info->uapi)
260                 vsp1->media_ops.link_validate = v4l2_subdev_link_validate;
261
262         vdev->mdev = mdev;
263         ret = v4l2_device_register(vsp1->dev, vdev);
264         if (ret < 0) {
265                 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
266                         ret);
267                 goto done;
268         }
269
270         /* Instantiate all the entities. */
271         if (vsp1->info->features & VSP1_HAS_BRS) {
272                 vsp1->brs = vsp1_bru_create(vsp1, VSP1_ENTITY_BRS);
273                 if (IS_ERR(vsp1->brs)) {
274                         ret = PTR_ERR(vsp1->brs);
275                         goto done;
276                 }
277
278                 list_add_tail(&vsp1->brs->entity.list_dev, &vsp1->entities);
279         }
280
281         if (vsp1->info->features & VSP1_HAS_BRU) {
282                 vsp1->bru = vsp1_bru_create(vsp1, VSP1_ENTITY_BRU);
283                 if (IS_ERR(vsp1->bru)) {
284                         ret = PTR_ERR(vsp1->bru);
285                         goto done;
286                 }
287
288                 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
289         }
290
291         if (vsp1->info->features & VSP1_HAS_CLU) {
292                 vsp1->clu = vsp1_clu_create(vsp1);
293                 if (IS_ERR(vsp1->clu)) {
294                         ret = PTR_ERR(vsp1->clu);
295                         goto done;
296                 }
297
298                 list_add_tail(&vsp1->clu->entity.list_dev, &vsp1->entities);
299         }
300
301         vsp1->hsi = vsp1_hsit_create(vsp1, true);
302         if (IS_ERR(vsp1->hsi)) {
303                 ret = PTR_ERR(vsp1->hsi);
304                 goto done;
305         }
306
307         list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
308
309         vsp1->hst = vsp1_hsit_create(vsp1, false);
310         if (IS_ERR(vsp1->hst)) {
311                 ret = PTR_ERR(vsp1->hst);
312                 goto done;
313         }
314
315         list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
316
317         if (vsp1->info->features & VSP1_HAS_HGO && vsp1->info->uapi) {
318                 vsp1->hgo = vsp1_hgo_create(vsp1);
319                 if (IS_ERR(vsp1->hgo)) {
320                         ret = PTR_ERR(vsp1->hgo);
321                         goto done;
322                 }
323
324                 list_add_tail(&vsp1->hgo->histo.entity.list_dev,
325                               &vsp1->entities);
326         }
327
328         if (vsp1->info->features & VSP1_HAS_HGT && vsp1->info->uapi) {
329                 vsp1->hgt = vsp1_hgt_create(vsp1);
330                 if (IS_ERR(vsp1->hgt)) {
331                         ret = PTR_ERR(vsp1->hgt);
332                         goto done;
333                 }
334
335                 list_add_tail(&vsp1->hgt->histo.entity.list_dev,
336                               &vsp1->entities);
337         }
338
339         /*
340          * The LIFs are only supported when used in conjunction with the DU, in
341          * which case the userspace API is disabled. If the userspace API is
342          * enabled skip the LIFs, even when present.
343          */
344         if (!vsp1->info->uapi) {
345                 for (i = 0; i < vsp1->info->lif_count; ++i) {
346                         struct vsp1_lif *lif;
347
348                         lif = vsp1_lif_create(vsp1, i);
349                         if (IS_ERR(lif)) {
350                                 ret = PTR_ERR(lif);
351                                 goto done;
352                         }
353
354                         vsp1->lif[i] = lif;
355                         list_add_tail(&lif->entity.list_dev, &vsp1->entities);
356                 }
357         }
358
359         if (vsp1->info->features & VSP1_HAS_LUT) {
360                 vsp1->lut = vsp1_lut_create(vsp1);
361                 if (IS_ERR(vsp1->lut)) {
362                         ret = PTR_ERR(vsp1->lut);
363                         goto done;
364                 }
365
366                 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
367         }
368
369         for (i = 0; i < vsp1->info->rpf_count; ++i) {
370                 struct vsp1_rwpf *rpf;
371
372                 rpf = vsp1_rpf_create(vsp1, i);
373                 if (IS_ERR(rpf)) {
374                         ret = PTR_ERR(rpf);
375                         goto done;
376                 }
377
378                 vsp1->rpf[i] = rpf;
379                 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
380
381                 if (vsp1->info->uapi) {
382                         struct vsp1_video *video = vsp1_video_create(vsp1, rpf);
383
384                         if (IS_ERR(video)) {
385                                 ret = PTR_ERR(video);
386                                 goto done;
387                         }
388
389                         list_add_tail(&video->list, &vsp1->videos);
390                 }
391         }
392
393         if (vsp1->info->features & VSP1_HAS_SRU) {
394                 vsp1->sru = vsp1_sru_create(vsp1);
395                 if (IS_ERR(vsp1->sru)) {
396                         ret = PTR_ERR(vsp1->sru);
397                         goto done;
398                 }
399
400                 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
401         }
402
403         for (i = 0; i < vsp1->info->uds_count; ++i) {
404                 struct vsp1_uds *uds;
405
406                 uds = vsp1_uds_create(vsp1, i);
407                 if (IS_ERR(uds)) {
408                         ret = PTR_ERR(uds);
409                         goto done;
410                 }
411
412                 vsp1->uds[i] = uds;
413                 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
414         }
415
416         for (i = 0; i < vsp1->info->wpf_count; ++i) {
417                 struct vsp1_rwpf *wpf;
418
419                 wpf = vsp1_wpf_create(vsp1, i);
420                 if (IS_ERR(wpf)) {
421                         ret = PTR_ERR(wpf);
422                         goto done;
423                 }
424
425                 vsp1->wpf[i] = wpf;
426                 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
427
428                 if (vsp1->info->uapi) {
429                         struct vsp1_video *video = vsp1_video_create(vsp1, wpf);
430
431                         if (IS_ERR(video)) {
432                                 ret = PTR_ERR(video);
433                                 goto done;
434                         }
435
436                         list_add_tail(&video->list, &vsp1->videos);
437                 }
438         }
439
440         /* Register all subdevs. */
441         list_for_each_entry(entity, &vsp1->entities, list_dev) {
442                 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
443                                                   &entity->subdev);
444                 if (ret < 0)
445                         goto done;
446         }
447
448         /*
449          * Create links and register subdev nodes if the userspace API is
450          * enabled or initialize the DRM pipeline otherwise.
451          */
452         if (vsp1->info->uapi) {
453                 ret = vsp1_uapi_create_links(vsp1);
454                 if (ret < 0)
455                         goto done;
456
457                 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
458                 if (ret < 0)
459                         goto done;
460
461                 ret = media_device_register(mdev);
462         } else {
463                 ret = vsp1_drm_init(vsp1);
464         }
465
466 done:
467         if (ret < 0)
468                 vsp1_destroy_entities(vsp1);
469
470         return ret;
471 }
472
473 int vsp1_reset_wpf(struct vsp1_device *vsp1, unsigned int index)
474 {
475         unsigned int timeout;
476         u32 status;
477
478         status = vsp1_read(vsp1, VI6_STATUS);
479         if (!(status & VI6_STATUS_SYS_ACT(index)))
480                 return 0;
481
482         vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(index));
483         for (timeout = 10; timeout > 0; --timeout) {
484                 status = vsp1_read(vsp1, VI6_STATUS);
485                 if (!(status & VI6_STATUS_SYS_ACT(index)))
486                         break;
487
488                 usleep_range(1000, 2000);
489         }
490
491         if (!timeout) {
492                 dev_err(vsp1->dev, "failed to reset wpf.%u\n", index);
493                 return -ETIMEDOUT;
494         }
495
496         return 0;
497 }
498
499 static int vsp1_device_init(struct vsp1_device *vsp1)
500 {
501         unsigned int i;
502         int ret;
503
504         /* Reset any channel that might be running. */
505         for (i = 0; i < vsp1->info->wpf_count; ++i) {
506                 ret = vsp1_reset_wpf(vsp1, i);
507                 if (ret < 0)
508                         return ret;
509         }
510
511         vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
512                    (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
513
514         for (i = 0; i < vsp1->info->rpf_count; ++i)
515                 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
516
517         for (i = 0; i < vsp1->info->uds_count; ++i)
518                 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
519
520         vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
521         vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
522         vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
523         vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
524         vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
525         vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
526
527         if (vsp1->info->features & VSP1_HAS_BRS)
528                 vsp1_write(vsp1, VI6_DPR_ILV_BRS_ROUTE, VI6_DPR_NODE_UNUSED);
529
530         vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
531                    (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
532         vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
533                    (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
534
535         vsp1_dlm_setup(vsp1);
536
537         return 0;
538 }
539
540 /*
541  * vsp1_device_get - Acquire the VSP1 device
542  *
543  * Make sure the device is not suspended and initialize it if needed.
544  *
545  * Return 0 on success or a negative error code otherwise.
546  */
547 int vsp1_device_get(struct vsp1_device *vsp1)
548 {
549         int ret;
550
551         ret = pm_runtime_get_sync(vsp1->dev);
552         if (ret < 0) {
553                 pm_runtime_put_noidle(vsp1->dev);
554                 return ret;
555         }
556
557         return 0;
558 }
559
560 /*
561  * vsp1_device_put - Release the VSP1 device
562  *
563  * Decrement the VSP1 reference count and cleanup the device if the last
564  * reference is released.
565  */
566 void vsp1_device_put(struct vsp1_device *vsp1)
567 {
568         pm_runtime_put_sync(vsp1->dev);
569 }
570
571 /* -----------------------------------------------------------------------------
572  * Power Management
573  */
574
575 static int __maybe_unused vsp1_pm_suspend(struct device *dev)
576 {
577         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
578
579         /*
580          * When used as part of a display pipeline, the VSP is stopped and
581          * restarted explicitly by the DU.
582          */
583         if (!vsp1->drm)
584                 vsp1_pipelines_suspend(vsp1);
585
586         pm_runtime_force_suspend(vsp1->dev);
587
588         return 0;
589 }
590
591 static int __maybe_unused vsp1_pm_resume(struct device *dev)
592 {
593         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
594
595         pm_runtime_force_resume(vsp1->dev);
596
597         /*
598          * When used as part of a display pipeline, the VSP is stopped and
599          * restarted explicitly by the DU.
600          */
601         if (!vsp1->drm)
602                 vsp1_pipelines_resume(vsp1);
603
604         return 0;
605 }
606
607 static int __maybe_unused vsp1_pm_runtime_suspend(struct device *dev)
608 {
609         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
610
611         rcar_fcp_disable(vsp1->fcp);
612
613         return 0;
614 }
615
616 static int __maybe_unused vsp1_pm_runtime_resume(struct device *dev)
617 {
618         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
619         int ret;
620
621         if (vsp1->info) {
622                 ret = vsp1_device_init(vsp1);
623                 if (ret < 0)
624                         return ret;
625         }
626
627         return rcar_fcp_enable(vsp1->fcp);
628 }
629
630 static const struct dev_pm_ops vsp1_pm_ops = {
631         SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
632         SET_RUNTIME_PM_OPS(vsp1_pm_runtime_suspend, vsp1_pm_runtime_resume, NULL)
633 };
634
635 /* -----------------------------------------------------------------------------
636  * Platform Driver
637  */
638
639 static const struct vsp1_device_info vsp1_device_infos[] = {
640         {
641                 .version = VI6_IP_VERSION_MODEL_VSPS_H2,
642                 .model = "VSP1-S",
643                 .gen = 2,
644                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
645                           | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
646                           | VSP1_HAS_WPF_VFLIP,
647                 .rpf_count = 5,
648                 .uds_count = 3,
649                 .wpf_count = 4,
650                 .num_bru_inputs = 4,
651                 .uapi = true,
652         }, {
653                 .version = VI6_IP_VERSION_MODEL_VSPR_H2,
654                 .model = "VSP1-R",
655                 .gen = 2,
656                 .features = VSP1_HAS_BRU | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
657                 .rpf_count = 5,
658                 .uds_count = 3,
659                 .wpf_count = 4,
660                 .num_bru_inputs = 4,
661                 .uapi = true,
662         }, {
663                 .version = VI6_IP_VERSION_MODEL_VSPD_GEN2,
664                 .model = "VSP1-D",
665                 .gen = 2,
666                 .features = VSP1_HAS_BRU | VSP1_HAS_HGO | VSP1_HAS_LUT,
667                 .lif_count = 1,
668                 .rpf_count = 4,
669                 .uds_count = 1,
670                 .wpf_count = 1,
671                 .num_bru_inputs = 4,
672                 .uapi = true,
673         }, {
674                 .version = VI6_IP_VERSION_MODEL_VSPS_M2,
675                 .model = "VSP1-S",
676                 .gen = 2,
677                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
678                           | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
679                           | VSP1_HAS_WPF_VFLIP,
680                 .rpf_count = 5,
681                 .uds_count = 1,
682                 .wpf_count = 4,
683                 .num_bru_inputs = 4,
684                 .uapi = true,
685         }, {
686                 .version = VI6_IP_VERSION_MODEL_VSPS_V2H,
687                 .model = "VSP1V-S",
688                 .gen = 2,
689                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
690                           | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
691                 .rpf_count = 4,
692                 .uds_count = 1,
693                 .wpf_count = 4,
694                 .num_bru_inputs = 4,
695                 .uapi = true,
696         }, {
697                 .version = VI6_IP_VERSION_MODEL_VSPD_V2H,
698                 .model = "VSP1V-D",
699                 .gen = 2,
700                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT,
701                 .lif_count = 1,
702                 .rpf_count = 4,
703                 .uds_count = 1,
704                 .wpf_count = 1,
705                 .num_bru_inputs = 4,
706                 .uapi = true,
707         }, {
708                 .version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
709                 .model = "VSP2-I",
710                 .gen = 3,
711                 .features = VSP1_HAS_CLU | VSP1_HAS_HGO | VSP1_HAS_HGT
712                           | VSP1_HAS_LUT | VSP1_HAS_SRU | VSP1_HAS_WPF_HFLIP
713                           | VSP1_HAS_WPF_VFLIP,
714                 .rpf_count = 1,
715                 .uds_count = 1,
716                 .wpf_count = 1,
717                 .uapi = true,
718         }, {
719                 .version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
720                 .model = "VSP2-BD",
721                 .gen = 3,
722                 .features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
723                 .rpf_count = 5,
724                 .wpf_count = 1,
725                 .num_bru_inputs = 5,
726                 .uapi = true,
727         }, {
728                 .version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
729                 .model = "VSP2-BC",
730                 .gen = 3,
731                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
732                           | VSP1_HAS_LUT | VSP1_HAS_WPF_VFLIP,
733                 .rpf_count = 5,
734                 .wpf_count = 1,
735                 .num_bru_inputs = 5,
736                 .uapi = true,
737         }, {
738                 .version = VI6_IP_VERSION_MODEL_VSPBS_GEN3,
739                 .model = "VSP2-BS",
740                 .gen = 3,
741                 .features = VSP1_HAS_BRS | VSP1_HAS_WPF_VFLIP,
742                 .rpf_count = 2,
743                 .wpf_count = 1,
744                 .uapi = true,
745         }, {
746                 .version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
747                 .model = "VSP2-D",
748                 .gen = 3,
749                 .features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
750                 .lif_count = 1,
751                 .rpf_count = 5,
752                 .wpf_count = 2,
753                 .num_bru_inputs = 5,
754         }, {
755                 .version = VI6_IP_VERSION_MODEL_VSPD_V3,
756                 .model = "VSP2-D",
757                 .gen = 3,
758                 .features = VSP1_HAS_BRS | VSP1_HAS_BRU,
759                 .lif_count = 1,
760                 .rpf_count = 5,
761                 .wpf_count = 1,
762                 .num_bru_inputs = 5,
763         }, {
764                 .version = VI6_IP_VERSION_MODEL_VSPDL_GEN3,
765                 .model = "VSP2-DL",
766                 .gen = 3,
767                 .features = VSP1_HAS_BRS | VSP1_HAS_BRU,
768                 .lif_count = 2,
769                 .rpf_count = 5,
770                 .wpf_count = 2,
771                 .num_bru_inputs = 5,
772         },
773 };
774
775 static int vsp1_probe(struct platform_device *pdev)
776 {
777         struct vsp1_device *vsp1;
778         struct device_node *fcp_node;
779         struct resource *irq;
780         struct resource *io;
781         unsigned int i;
782         int ret;
783
784         vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
785         if (vsp1 == NULL)
786                 return -ENOMEM;
787
788         vsp1->dev = &pdev->dev;
789         INIT_LIST_HEAD(&vsp1->entities);
790         INIT_LIST_HEAD(&vsp1->videos);
791
792         platform_set_drvdata(pdev, vsp1);
793
794         /* I/O and IRQ resources (clock managed by the clock PM domain) */
795         io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
796         vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
797         if (IS_ERR(vsp1->mmio))
798                 return PTR_ERR(vsp1->mmio);
799
800         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
801         if (!irq) {
802                 dev_err(&pdev->dev, "missing IRQ\n");
803                 return -EINVAL;
804         }
805
806         ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
807                               IRQF_SHARED, dev_name(&pdev->dev), vsp1);
808         if (ret < 0) {
809                 dev_err(&pdev->dev, "failed to request IRQ\n");
810                 return ret;
811         }
812
813         /* FCP (optional) */
814         fcp_node = of_parse_phandle(pdev->dev.of_node, "renesas,fcp", 0);
815         if (fcp_node) {
816                 vsp1->fcp = rcar_fcp_get(fcp_node);
817                 of_node_put(fcp_node);
818                 if (IS_ERR(vsp1->fcp)) {
819                         dev_dbg(&pdev->dev, "FCP not found (%ld)\n",
820                                 PTR_ERR(vsp1->fcp));
821                         return PTR_ERR(vsp1->fcp);
822                 }
823
824                 /*
825                  * When the FCP is present, it handles all bus master accesses
826                  * for the VSP and must thus be used in place of the VSP device
827                  * to map DMA buffers.
828                  */
829                 vsp1->bus_master = rcar_fcp_get_device(vsp1->fcp);
830         } else {
831                 vsp1->bus_master = vsp1->dev;
832         }
833
834         /* Configure device parameters based on the version register. */
835         pm_runtime_enable(&pdev->dev);
836
837         ret = vsp1_device_get(vsp1);
838         if (ret < 0)
839                 goto done;
840
841         vsp1->version = vsp1_read(vsp1, VI6_IP_VERSION);
842         vsp1_device_put(vsp1);
843
844         for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) {
845                 if ((vsp1->version & VI6_IP_VERSION_MODEL_MASK) ==
846                     vsp1_device_infos[i].version) {
847                         vsp1->info = &vsp1_device_infos[i];
848                         break;
849                 }
850         }
851
852         if (!vsp1->info) {
853                 dev_err(&pdev->dev, "unsupported IP version 0x%08x\n",
854                         vsp1->version);
855                 ret = -ENXIO;
856                 goto done;
857         }
858
859         dev_dbg(&pdev->dev, "IP version 0x%08x\n", vsp1->version);
860
861         /* Instanciate entities */
862         ret = vsp1_create_entities(vsp1);
863         if (ret < 0) {
864                 dev_err(&pdev->dev, "failed to create entities\n");
865                 goto done;
866         }
867
868 done:
869         if (ret) {
870                 pm_runtime_disable(&pdev->dev);
871                 rcar_fcp_put(vsp1->fcp);
872         }
873
874         return ret;
875 }
876
877 static int vsp1_remove(struct platform_device *pdev)
878 {
879         struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
880
881         vsp1_destroy_entities(vsp1);
882         rcar_fcp_put(vsp1->fcp);
883
884         pm_runtime_disable(&pdev->dev);
885
886         return 0;
887 }
888
889 static const struct of_device_id vsp1_of_match[] = {
890         { .compatible = "renesas,vsp1" },
891         { .compatible = "renesas,vsp2" },
892         { },
893 };
894 MODULE_DEVICE_TABLE(of, vsp1_of_match);
895
896 static struct platform_driver vsp1_platform_driver = {
897         .probe          = vsp1_probe,
898         .remove         = vsp1_remove,
899         .driver         = {
900                 .name   = "vsp1",
901                 .pm     = &vsp1_pm_ops,
902                 .of_match_table = vsp1_of_match,
903         },
904 };
905
906 module_platform_driver(vsp1_platform_driver);
907
908 MODULE_ALIAS("vsp1");
909 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
910 MODULE_DESCRIPTION("Renesas VSP1 Driver");
911 MODULE_LICENSE("GPL");