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