[media] v4l: vsp1: Support VSP1 instances without any UDS
[cascardo/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/platform_device.h>
21 #include <linux/videodev2.h>
22
23 #include "vsp1.h"
24 #include "vsp1_bru.h"
25 #include "vsp1_hsit.h"
26 #include "vsp1_lif.h"
27 #include "vsp1_lut.h"
28 #include "vsp1_rwpf.h"
29 #include "vsp1_sru.h"
30 #include "vsp1_uds.h"
31 #include "vsp1_video.h"
32
33 /* -----------------------------------------------------------------------------
34  * Interrupt Handling
35  */
36
37 static irqreturn_t vsp1_irq_handler(int irq, void *data)
38 {
39         u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
40         struct vsp1_device *vsp1 = data;
41         irqreturn_t ret = IRQ_NONE;
42         unsigned int i;
43
44         for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
45                 struct vsp1_rwpf *wpf = vsp1->wpf[i];
46                 struct vsp1_pipeline *pipe;
47                 u32 status;
48
49                 if (wpf == NULL)
50                         continue;
51
52                 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
53                 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
54                 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
55
56                 if (status & VI6_WFP_IRQ_STA_FRE) {
57                         vsp1_pipeline_frame_end(pipe);
58                         ret = IRQ_HANDLED;
59                 }
60         }
61
62         return ret;
63 }
64
65 /* -----------------------------------------------------------------------------
66  * Entities
67  */
68
69 /*
70  * vsp1_create_links - Create links from all sources to the given sink
71  *
72  * This function creates media links from all valid sources to the given sink
73  * pad. Links that would be invalid according to the VSP1 hardware capabilities
74  * are skipped. Those include all links
75  *
76  * - from a UDS to a UDS (UDS entities can't be chained)
77  * - from an entity to itself (no loops are allowed)
78  */
79 static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
80 {
81         struct media_entity *entity = &sink->subdev.entity;
82         struct vsp1_entity *source;
83         unsigned int pad;
84         int ret;
85
86         if (sink->type == VSP1_ENTITY_RPF) {
87                 struct vsp1_rwpf *rpf = to_rwpf(&sink->subdev);
88
89                 /* RPFs have no source entities, just connect their source pad
90                  * to their video device.
91                  */
92                 return media_create_pad_link(&rpf->entity.video->video.entity,
93                                              0, &rpf->entity.subdev.entity,
94                                              RWPF_PAD_SINK,
95                                              MEDIA_LNK_FL_ENABLED |
96                                              MEDIA_LNK_FL_IMMUTABLE);
97         }
98
99         list_for_each_entry(source, &vsp1->entities, list_dev) {
100                 u32 flags;
101
102                 if (source->type == sink->type)
103                         continue;
104
105                 if (source->type == VSP1_ENTITY_LIF ||
106                     source->type == VSP1_ENTITY_WPF)
107                         continue;
108
109                 flags = source->type == VSP1_ENTITY_RPF &&
110                         sink->type == VSP1_ENTITY_WPF &&
111                         source->index == sink->index
112                       ? MEDIA_LNK_FL_ENABLED : 0;
113
114                 for (pad = 0; pad < entity->num_pads; ++pad) {
115                         if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
116                                 continue;
117
118                         ret = media_create_pad_link(&source->subdev.entity,
119                                                        source->source_pad,
120                                                        entity, pad, flags);
121                         if (ret < 0)
122                                 return ret;
123
124                         if (flags & MEDIA_LNK_FL_ENABLED)
125                                 source->sink = entity;
126                 }
127         }
128
129         if (sink->type == VSP1_ENTITY_WPF) {
130                 struct vsp1_rwpf *wpf = to_rwpf(&sink->subdev);
131                 unsigned int flags = MEDIA_LNK_FL_ENABLED;
132
133                 /* Connect the video device to the WPF. All connections are
134                  * immutable except for the WPF0 source link if a LIF is
135                  * present.
136                  */
137                 if (!(vsp1->pdata.features & VSP1_HAS_LIF) || sink->index != 0)
138                         flags |= MEDIA_LNK_FL_IMMUTABLE;
139
140                 return media_create_pad_link(&wpf->entity.subdev.entity,
141                                              RWPF_PAD_SOURCE,
142                                              &wpf->entity.video->video.entity,
143                                              0, flags);
144         }
145
146         return 0;
147 }
148
149 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
150 {
151         struct vsp1_entity *entity, *_entity;
152         struct vsp1_video *video, *_video;
153
154         list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
155                 list_del(&entity->list_dev);
156                 vsp1_entity_destroy(entity);
157         }
158
159         list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
160                 list_del(&video->list);
161                 vsp1_video_cleanup(video);
162         }
163
164         v4l2_device_unregister(&vsp1->v4l2_dev);
165         media_device_unregister(&vsp1->media_dev);
166         media_device_cleanup(&vsp1->media_dev);
167 }
168
169 static int vsp1_create_entities(struct vsp1_device *vsp1)
170 {
171         struct media_device *mdev = &vsp1->media_dev;
172         struct v4l2_device *vdev = &vsp1->v4l2_dev;
173         struct vsp1_entity *entity;
174         unsigned int i;
175         int ret;
176
177         mdev->dev = vsp1->dev;
178         strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
179         snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
180                  dev_name(mdev->dev));
181         media_device_init(mdev);
182
183         vdev->mdev = mdev;
184         ret = v4l2_device_register(vsp1->dev, vdev);
185         if (ret < 0) {
186                 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
187                         ret);
188                 goto done;
189         }
190
191         /* Instantiate all the entities. */
192         vsp1->bru = vsp1_bru_create(vsp1);
193         if (IS_ERR(vsp1->bru)) {
194                 ret = PTR_ERR(vsp1->bru);
195                 goto done;
196         }
197
198         list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
199
200         vsp1->hsi = vsp1_hsit_create(vsp1, true);
201         if (IS_ERR(vsp1->hsi)) {
202                 ret = PTR_ERR(vsp1->hsi);
203                 goto done;
204         }
205
206         list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
207
208         vsp1->hst = vsp1_hsit_create(vsp1, false);
209         if (IS_ERR(vsp1->hst)) {
210                 ret = PTR_ERR(vsp1->hst);
211                 goto done;
212         }
213
214         list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
215
216         if (vsp1->pdata.features & VSP1_HAS_LIF) {
217                 vsp1->lif = vsp1_lif_create(vsp1);
218                 if (IS_ERR(vsp1->lif)) {
219                         ret = PTR_ERR(vsp1->lif);
220                         goto done;
221                 }
222
223                 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
224         }
225
226         if (vsp1->pdata.features & VSP1_HAS_LUT) {
227                 vsp1->lut = vsp1_lut_create(vsp1);
228                 if (IS_ERR(vsp1->lut)) {
229                         ret = PTR_ERR(vsp1->lut);
230                         goto done;
231                 }
232
233                 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
234         }
235
236         for (i = 0; i < vsp1->pdata.rpf_count; ++i) {
237                 struct vsp1_video *video;
238                 struct vsp1_rwpf *rpf;
239
240                 rpf = vsp1_rpf_create(vsp1, i);
241                 if (IS_ERR(rpf)) {
242                         ret = PTR_ERR(rpf);
243                         goto done;
244                 }
245
246                 vsp1->rpf[i] = rpf;
247                 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
248
249                 video = vsp1_video_create(vsp1, rpf);
250                 if (IS_ERR(video)) {
251                         ret = PTR_ERR(video);
252                         goto done;
253                 }
254
255                 list_add_tail(&video->list, &vsp1->videos);
256         }
257
258         if (vsp1->pdata.features & VSP1_HAS_SRU) {
259                 vsp1->sru = vsp1_sru_create(vsp1);
260                 if (IS_ERR(vsp1->sru)) {
261                         ret = PTR_ERR(vsp1->sru);
262                         goto done;
263                 }
264
265                 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
266         }
267
268         for (i = 0; i < vsp1->pdata.uds_count; ++i) {
269                 struct vsp1_uds *uds;
270
271                 uds = vsp1_uds_create(vsp1, i);
272                 if (IS_ERR(uds)) {
273                         ret = PTR_ERR(uds);
274                         goto done;
275                 }
276
277                 vsp1->uds[i] = uds;
278                 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
279         }
280
281         for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
282                 struct vsp1_video *video;
283                 struct vsp1_rwpf *wpf;
284
285                 wpf = vsp1_wpf_create(vsp1, i);
286                 if (IS_ERR(wpf)) {
287                         ret = PTR_ERR(wpf);
288                         goto done;
289                 }
290
291                 vsp1->wpf[i] = wpf;
292                 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
293
294                 video = vsp1_video_create(vsp1, wpf);
295                 if (IS_ERR(video)) {
296                         ret = PTR_ERR(video);
297                         goto done;
298                 }
299
300                 list_add_tail(&video->list, &vsp1->videos);
301                 wpf->entity.sink = &video->video.entity;
302         }
303
304         /* Register all subdevs. */
305         list_for_each_entry(entity, &vsp1->entities, list_dev) {
306                 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
307                                                   &entity->subdev);
308                 if (ret < 0)
309                         goto done;
310         }
311
312         /* Create links. */
313         list_for_each_entry(entity, &vsp1->entities, list_dev) {
314                 if (entity->type == VSP1_ENTITY_LIF)
315                         continue;
316
317                 ret = vsp1_create_links(vsp1, entity);
318                 if (ret < 0)
319                         goto done;
320         }
321
322         if (vsp1->pdata.features & VSP1_HAS_LIF) {
323                 ret = media_create_pad_link(
324                         &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
325                         &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
326                 if (ret < 0)
327                         return ret;
328         }
329
330         ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
331         if (ret < 0)
332                 goto done;
333
334         ret = media_device_register(mdev);
335
336 done:
337         if (ret < 0)
338                 vsp1_destroy_entities(vsp1);
339
340         return ret;
341 }
342
343 static int vsp1_device_init(struct vsp1_device *vsp1)
344 {
345         unsigned int i;
346         u32 status;
347
348         /* Reset any channel that might be running. */
349         status = vsp1_read(vsp1, VI6_STATUS);
350
351         for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
352                 unsigned int timeout;
353
354                 if (!(status & VI6_STATUS_SYS_ACT(i)))
355                         continue;
356
357                 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
358                 for (timeout = 10; timeout > 0; --timeout) {
359                         status = vsp1_read(vsp1, VI6_STATUS);
360                         if (!(status & VI6_STATUS_SYS_ACT(i)))
361                                 break;
362
363                         usleep_range(1000, 2000);
364                 }
365
366                 if (!timeout) {
367                         dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
368                         return -ETIMEDOUT;
369                 }
370         }
371
372         vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
373                    (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
374
375         for (i = 0; i < vsp1->pdata.rpf_count; ++i)
376                 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
377
378         for (i = 0; i < vsp1->pdata.uds_count; ++i)
379                 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
380
381         vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
382         vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
383         vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
384         vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
385         vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
386         vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
387
388         vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
389                    (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
390         vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
391                    (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
392
393         return 0;
394 }
395
396 /*
397  * vsp1_device_get - Acquire the VSP1 device
398  *
399  * Increment the VSP1 reference count and initialize the device if the first
400  * reference is taken.
401  *
402  * Return 0 on success or a negative error code otherwise.
403  */
404 int vsp1_device_get(struct vsp1_device *vsp1)
405 {
406         int ret = 0;
407
408         mutex_lock(&vsp1->lock);
409         if (vsp1->ref_count > 0)
410                 goto done;
411
412         ret = clk_prepare_enable(vsp1->clock);
413         if (ret < 0)
414                 goto done;
415
416         ret = vsp1_device_init(vsp1);
417         if (ret < 0) {
418                 clk_disable_unprepare(vsp1->clock);
419                 goto done;
420         }
421
422 done:
423         if (!ret)
424                 vsp1->ref_count++;
425
426         mutex_unlock(&vsp1->lock);
427         return ret;
428 }
429
430 /*
431  * vsp1_device_put - Release the VSP1 device
432  *
433  * Decrement the VSP1 reference count and cleanup the device if the last
434  * reference is released.
435  */
436 void vsp1_device_put(struct vsp1_device *vsp1)
437 {
438         mutex_lock(&vsp1->lock);
439
440         if (--vsp1->ref_count == 0)
441                 clk_disable_unprepare(vsp1->clock);
442
443         mutex_unlock(&vsp1->lock);
444 }
445
446 /* -----------------------------------------------------------------------------
447  * Power Management
448  */
449
450 #ifdef CONFIG_PM_SLEEP
451 static int vsp1_pm_suspend(struct device *dev)
452 {
453         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
454
455         WARN_ON(mutex_is_locked(&vsp1->lock));
456
457         if (vsp1->ref_count == 0)
458                 return 0;
459
460         vsp1_pipelines_suspend(vsp1);
461
462         clk_disable_unprepare(vsp1->clock);
463
464         return 0;
465 }
466
467 static int vsp1_pm_resume(struct device *dev)
468 {
469         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
470
471         WARN_ON(mutex_is_locked(&vsp1->lock));
472
473         if (vsp1->ref_count == 0)
474                 return 0;
475
476         clk_prepare_enable(vsp1->clock);
477
478         vsp1_pipelines_resume(vsp1);
479
480         return 0;
481 }
482 #endif
483
484 static const struct dev_pm_ops vsp1_pm_ops = {
485         SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
486 };
487
488 /* -----------------------------------------------------------------------------
489  * Platform Driver
490  */
491
492 static int vsp1_parse_dt(struct vsp1_device *vsp1)
493 {
494         struct device_node *np = vsp1->dev->of_node;
495         struct vsp1_platform_data *pdata = &vsp1->pdata;
496
497         if (of_property_read_bool(np, "renesas,has-lif"))
498                 pdata->features |= VSP1_HAS_LIF;
499         if (of_property_read_bool(np, "renesas,has-lut"))
500                 pdata->features |= VSP1_HAS_LUT;
501         if (of_property_read_bool(np, "renesas,has-sru"))
502                 pdata->features |= VSP1_HAS_SRU;
503
504         of_property_read_u32(np, "renesas,#rpf", &pdata->rpf_count);
505         of_property_read_u32(np, "renesas,#uds", &pdata->uds_count);
506         of_property_read_u32(np, "renesas,#wpf", &pdata->wpf_count);
507
508         if (pdata->rpf_count <= 0 || pdata->rpf_count > VSP1_MAX_RPF) {
509                 dev_err(vsp1->dev, "invalid number of RPF (%u)\n",
510                         pdata->rpf_count);
511                 return -EINVAL;
512         }
513
514         if (pdata->uds_count > VSP1_MAX_UDS) {
515                 dev_err(vsp1->dev, "invalid number of UDS (%u)\n",
516                         pdata->uds_count);
517                 return -EINVAL;
518         }
519
520         if (pdata->wpf_count <= 0 || pdata->wpf_count > VSP1_MAX_WPF) {
521                 dev_err(vsp1->dev, "invalid number of WPF (%u)\n",
522                         pdata->wpf_count);
523                 return -EINVAL;
524         }
525
526         return 0;
527 }
528
529 static int vsp1_probe(struct platform_device *pdev)
530 {
531         struct vsp1_device *vsp1;
532         struct resource *irq;
533         struct resource *io;
534         int ret;
535
536         vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
537         if (vsp1 == NULL)
538                 return -ENOMEM;
539
540         vsp1->dev = &pdev->dev;
541         mutex_init(&vsp1->lock);
542         INIT_LIST_HEAD(&vsp1->entities);
543         INIT_LIST_HEAD(&vsp1->videos);
544
545         ret = vsp1_parse_dt(vsp1);
546         if (ret < 0)
547                 return ret;
548
549         /* I/O, IRQ and clock resources */
550         io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
551         vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
552         if (IS_ERR(vsp1->mmio))
553                 return PTR_ERR(vsp1->mmio);
554
555         vsp1->clock = devm_clk_get(&pdev->dev, NULL);
556         if (IS_ERR(vsp1->clock)) {
557                 dev_err(&pdev->dev, "failed to get clock\n");
558                 return PTR_ERR(vsp1->clock);
559         }
560
561         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
562         if (!irq) {
563                 dev_err(&pdev->dev, "missing IRQ\n");
564                 return -EINVAL;
565         }
566
567         ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
568                               IRQF_SHARED, dev_name(&pdev->dev), vsp1);
569         if (ret < 0) {
570                 dev_err(&pdev->dev, "failed to request IRQ\n");
571                 return ret;
572         }
573
574         /* Instanciate entities */
575         ret = vsp1_create_entities(vsp1);
576         if (ret < 0) {
577                 dev_err(&pdev->dev, "failed to create entities\n");
578                 return ret;
579         }
580
581         platform_set_drvdata(pdev, vsp1);
582
583         return 0;
584 }
585
586 static int vsp1_remove(struct platform_device *pdev)
587 {
588         struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
589
590         vsp1_destroy_entities(vsp1);
591
592         return 0;
593 }
594
595 static const struct of_device_id vsp1_of_match[] = {
596         { .compatible = "renesas,vsp1" },
597         { },
598 };
599
600 static struct platform_driver vsp1_platform_driver = {
601         .probe          = vsp1_probe,
602         .remove         = vsp1_remove,
603         .driver         = {
604                 .name   = "vsp1",
605                 .pm     = &vsp1_pm_ops,
606                 .of_match_table = vsp1_of_match,
607         },
608 };
609
610 module_platform_driver(vsp1_platform_driver);
611
612 MODULE_ALIAS("vsp1");
613 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
614 MODULE_DESCRIPTION("Renesas VSP1 Driver");
615 MODULE_LICENSE("GPL");