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