]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/vsp1/vsp1_drv.c
Merge tag 'sunxi-drm-for-4.14' of https://git.kernel.org/pub/scm/linux/kernel/git...
[linux.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         return ret < 0 ? ret : 0;
553 }
554
555 /*
556  * vsp1_device_put - Release the VSP1 device
557  *
558  * Decrement the VSP1 reference count and cleanup the device if the last
559  * reference is released.
560  */
561 void vsp1_device_put(struct vsp1_device *vsp1)
562 {
563         pm_runtime_put_sync(vsp1->dev);
564 }
565
566 /* -----------------------------------------------------------------------------
567  * Power Management
568  */
569
570 static int __maybe_unused vsp1_pm_suspend(struct device *dev)
571 {
572         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
573
574         vsp1_pipelines_suspend(vsp1);
575         pm_runtime_force_suspend(vsp1->dev);
576
577         return 0;
578 }
579
580 static int __maybe_unused vsp1_pm_resume(struct device *dev)
581 {
582         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
583
584         pm_runtime_force_resume(vsp1->dev);
585         vsp1_pipelines_resume(vsp1);
586
587         return 0;
588 }
589
590 static int __maybe_unused vsp1_pm_runtime_suspend(struct device *dev)
591 {
592         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
593
594         rcar_fcp_disable(vsp1->fcp);
595
596         return 0;
597 }
598
599 static int __maybe_unused vsp1_pm_runtime_resume(struct device *dev)
600 {
601         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
602         int ret;
603
604         if (vsp1->info) {
605                 ret = vsp1_device_init(vsp1);
606                 if (ret < 0)
607                         return ret;
608         }
609
610         return rcar_fcp_enable(vsp1->fcp);
611 }
612
613 static const struct dev_pm_ops vsp1_pm_ops = {
614         SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
615         SET_RUNTIME_PM_OPS(vsp1_pm_runtime_suspend, vsp1_pm_runtime_resume, NULL)
616 };
617
618 /* -----------------------------------------------------------------------------
619  * Platform Driver
620  */
621
622 static const struct vsp1_device_info vsp1_device_infos[] = {
623         {
624                 .version = VI6_IP_VERSION_MODEL_VSPS_H2,
625                 .model = "VSP1-S",
626                 .gen = 2,
627                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
628                           | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
629                           | VSP1_HAS_WPF_VFLIP,
630                 .rpf_count = 5,
631                 .uds_count = 3,
632                 .wpf_count = 4,
633                 .num_bru_inputs = 4,
634                 .uapi = true,
635         }, {
636                 .version = VI6_IP_VERSION_MODEL_VSPR_H2,
637                 .model = "VSP1-R",
638                 .gen = 2,
639                 .features = VSP1_HAS_BRU | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
640                 .rpf_count = 5,
641                 .uds_count = 3,
642                 .wpf_count = 4,
643                 .num_bru_inputs = 4,
644                 .uapi = true,
645         }, {
646                 .version = VI6_IP_VERSION_MODEL_VSPD_GEN2,
647                 .model = "VSP1-D",
648                 .gen = 2,
649                 .features = VSP1_HAS_BRU | VSP1_HAS_HGO | VSP1_HAS_LUT,
650                 .lif_count = 1,
651                 .rpf_count = 4,
652                 .uds_count = 1,
653                 .wpf_count = 1,
654                 .num_bru_inputs = 4,
655                 .uapi = true,
656         }, {
657                 .version = VI6_IP_VERSION_MODEL_VSPS_M2,
658                 .model = "VSP1-S",
659                 .gen = 2,
660                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
661                           | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
662                           | VSP1_HAS_WPF_VFLIP,
663                 .rpf_count = 5,
664                 .uds_count = 1,
665                 .wpf_count = 4,
666                 .num_bru_inputs = 4,
667                 .uapi = true,
668         }, {
669                 .version = VI6_IP_VERSION_MODEL_VSPS_V2H,
670                 .model = "VSP1V-S",
671                 .gen = 2,
672                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
673                           | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
674                 .rpf_count = 4,
675                 .uds_count = 1,
676                 .wpf_count = 4,
677                 .num_bru_inputs = 4,
678                 .uapi = true,
679         }, {
680                 .version = VI6_IP_VERSION_MODEL_VSPD_V2H,
681                 .model = "VSP1V-D",
682                 .gen = 2,
683                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT,
684                 .lif_count = 1,
685                 .rpf_count = 4,
686                 .uds_count = 1,
687                 .wpf_count = 1,
688                 .num_bru_inputs = 4,
689                 .uapi = true,
690         }, {
691                 .version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
692                 .model = "VSP2-I",
693                 .gen = 3,
694                 .features = VSP1_HAS_CLU | VSP1_HAS_HGO | VSP1_HAS_HGT
695                           | VSP1_HAS_LUT | VSP1_HAS_SRU | VSP1_HAS_WPF_HFLIP
696                           | VSP1_HAS_WPF_VFLIP,
697                 .rpf_count = 1,
698                 .uds_count = 1,
699                 .wpf_count = 1,
700                 .uapi = true,
701         }, {
702                 .version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
703                 .model = "VSP2-BD",
704                 .gen = 3,
705                 .features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
706                 .rpf_count = 5,
707                 .wpf_count = 1,
708                 .num_bru_inputs = 5,
709                 .uapi = true,
710         }, {
711                 .version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
712                 .model = "VSP2-BC",
713                 .gen = 3,
714                 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
715                           | VSP1_HAS_LUT | VSP1_HAS_WPF_VFLIP,
716                 .rpf_count = 5,
717                 .wpf_count = 1,
718                 .num_bru_inputs = 5,
719                 .uapi = true,
720         }, {
721                 .version = VI6_IP_VERSION_MODEL_VSPBS_GEN3,
722                 .model = "VSP2-BS",
723                 .gen = 3,
724                 .features = VSP1_HAS_BRS | VSP1_HAS_WPF_VFLIP,
725                 .rpf_count = 2,
726                 .wpf_count = 1,
727                 .uapi = true,
728         }, {
729                 .version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
730                 .model = "VSP2-D",
731                 .gen = 3,
732                 .features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
733                 .lif_count = 1,
734                 .rpf_count = 5,
735                 .wpf_count = 2,
736                 .num_bru_inputs = 5,
737         }, {
738                 .version = VI6_IP_VERSION_MODEL_VSPD_V3,
739                 .model = "VSP2-D",
740                 .gen = 3,
741                 .features = VSP1_HAS_BRS | VSP1_HAS_BRU,
742                 .lif_count = 1,
743                 .rpf_count = 5,
744                 .wpf_count = 1,
745                 .num_bru_inputs = 5,
746         }, {
747                 .version = VI6_IP_VERSION_MODEL_VSPDL_GEN3,
748                 .model = "VSP2-DL",
749                 .gen = 3,
750                 .features = VSP1_HAS_BRS | VSP1_HAS_BRU,
751                 .lif_count = 2,
752                 .rpf_count = 5,
753                 .wpf_count = 2,
754                 .num_bru_inputs = 5,
755         },
756 };
757
758 static int vsp1_probe(struct platform_device *pdev)
759 {
760         struct vsp1_device *vsp1;
761         struct device_node *fcp_node;
762         struct resource *irq;
763         struct resource *io;
764         unsigned int i;
765         int ret;
766
767         vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
768         if (vsp1 == NULL)
769                 return -ENOMEM;
770
771         vsp1->dev = &pdev->dev;
772         INIT_LIST_HEAD(&vsp1->entities);
773         INIT_LIST_HEAD(&vsp1->videos);
774
775         platform_set_drvdata(pdev, vsp1);
776
777         /* I/O and IRQ resources (clock managed by the clock PM domain) */
778         io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
779         vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
780         if (IS_ERR(vsp1->mmio))
781                 return PTR_ERR(vsp1->mmio);
782
783         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
784         if (!irq) {
785                 dev_err(&pdev->dev, "missing IRQ\n");
786                 return -EINVAL;
787         }
788
789         ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
790                               IRQF_SHARED, dev_name(&pdev->dev), vsp1);
791         if (ret < 0) {
792                 dev_err(&pdev->dev, "failed to request IRQ\n");
793                 return ret;
794         }
795
796         /* FCP (optional) */
797         fcp_node = of_parse_phandle(pdev->dev.of_node, "renesas,fcp", 0);
798         if (fcp_node) {
799                 vsp1->fcp = rcar_fcp_get(fcp_node);
800                 of_node_put(fcp_node);
801                 if (IS_ERR(vsp1->fcp)) {
802                         dev_dbg(&pdev->dev, "FCP not found (%ld)\n",
803                                 PTR_ERR(vsp1->fcp));
804                         return PTR_ERR(vsp1->fcp);
805                 }
806
807                 /*
808                  * When the FCP is present, it handles all bus master accesses
809                  * for the VSP and must thus be used in place of the VSP device
810                  * to map DMA buffers.
811                  */
812                 vsp1->bus_master = rcar_fcp_get_device(vsp1->fcp);
813         } else {
814                 vsp1->bus_master = vsp1->dev;
815         }
816
817         /* Configure device parameters based on the version register. */
818         pm_runtime_enable(&pdev->dev);
819
820         ret = pm_runtime_get_sync(&pdev->dev);
821         if (ret < 0)
822                 goto done;
823
824         vsp1->version = vsp1_read(vsp1, VI6_IP_VERSION);
825         pm_runtime_put_sync(&pdev->dev);
826
827         for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) {
828                 if ((vsp1->version & VI6_IP_VERSION_MODEL_MASK) ==
829                     vsp1_device_infos[i].version) {
830                         vsp1->info = &vsp1_device_infos[i];
831                         break;
832                 }
833         }
834
835         if (!vsp1->info) {
836                 dev_err(&pdev->dev, "unsupported IP version 0x%08x\n",
837                         vsp1->version);
838                 ret = -ENXIO;
839                 goto done;
840         }
841
842         dev_dbg(&pdev->dev, "IP version 0x%08x\n", vsp1->version);
843
844         /* Instanciate entities */
845         ret = vsp1_create_entities(vsp1);
846         if (ret < 0) {
847                 dev_err(&pdev->dev, "failed to create entities\n");
848                 goto done;
849         }
850
851 done:
852         if (ret)
853                 pm_runtime_disable(&pdev->dev);
854
855         return ret;
856 }
857
858 static int vsp1_remove(struct platform_device *pdev)
859 {
860         struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
861
862         vsp1_destroy_entities(vsp1);
863         rcar_fcp_put(vsp1->fcp);
864
865         pm_runtime_disable(&pdev->dev);
866
867         return 0;
868 }
869
870 static const struct of_device_id vsp1_of_match[] = {
871         { .compatible = "renesas,vsp1" },
872         { .compatible = "renesas,vsp2" },
873         { },
874 };
875 MODULE_DEVICE_TABLE(of, vsp1_of_match);
876
877 static struct platform_driver vsp1_platform_driver = {
878         .probe          = vsp1_probe,
879         .remove         = vsp1_remove,
880         .driver         = {
881                 .name   = "vsp1",
882                 .pm     = &vsp1_pm_ops,
883                 .of_match_table = vsp1_of_match,
884         },
885 };
886
887 module_platform_driver(vsp1_platform_driver);
888
889 MODULE_ALIAS("vsp1");
890 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
891 MODULE_DESCRIPTION("Renesas VSP1 Driver");
892 MODULE_LICENSE("GPL");