2 * vsp1_drv.c -- R-Car VSP1 Driver
4 * Copyright (C) 2013-2015 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
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>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/videodev2.h>
25 #include <media/rcar-fcp.h>
26 #include <media/v4l2-subdev.h>
32 #include "vsp1_hsit.h"
35 #include "vsp1_pipe.h"
36 #include "vsp1_rwpf.h"
39 #include "vsp1_video.h"
41 /* -----------------------------------------------------------------------------
45 static irqreturn_t
vsp1_irq_handler(int irq
, void *data
)
47 u32 mask
= VI6_WFP_IRQ_STA_DFE
| VI6_WFP_IRQ_STA_FRE
;
48 struct vsp1_device
*vsp1
= data
;
49 irqreturn_t ret
= IRQ_NONE
;
53 for (i
= 0; i
< vsp1
->info
->wpf_count
; ++i
) {
54 struct vsp1_rwpf
*wpf
= vsp1
->wpf
[i
];
59 status
= vsp1_read(vsp1
, VI6_WPF_IRQ_STA(i
));
60 vsp1_write(vsp1
, VI6_WPF_IRQ_STA(i
), ~status
& mask
);
62 if (status
& VI6_WFP_IRQ_STA_FRE
) {
63 vsp1_pipeline_frame_end(wpf
->pipe
);
68 status
= vsp1_read(vsp1
, VI6_DISP_IRQ_STA
);
69 vsp1_write(vsp1
, VI6_DISP_IRQ_STA
, ~status
& VI6_DISP_IRQ_STA_DST
);
71 if (status
& VI6_DISP_IRQ_STA_DST
) {
72 vsp1_drm_display_start(vsp1
);
79 /* -----------------------------------------------------------------------------
84 * vsp1_create_sink_links - Create links from all sources to the given sink
86 * This function creates media links from all valid sources to the given sink
87 * pad. Links that would be invalid according to the VSP1 hardware capabilities
88 * are skipped. Those include all links
90 * - from a UDS to a UDS (UDS entities can't be chained)
91 * - from an entity to itself (no loops are allowed)
93 static int vsp1_create_sink_links(struct vsp1_device
*vsp1
,
94 struct vsp1_entity
*sink
)
96 struct media_entity
*entity
= &sink
->subdev
.entity
;
97 struct vsp1_entity
*source
;
101 list_for_each_entry(source
, &vsp1
->entities
, list_dev
) {
104 if (source
->type
== sink
->type
)
107 if (source
->type
== VSP1_ENTITY_LIF
||
108 source
->type
== VSP1_ENTITY_WPF
)
111 flags
= source
->type
== VSP1_ENTITY_RPF
&&
112 sink
->type
== VSP1_ENTITY_WPF
&&
113 source
->index
== sink
->index
114 ? MEDIA_LNK_FL_ENABLED
: 0;
116 for (pad
= 0; pad
< entity
->num_pads
; ++pad
) {
117 if (!(entity
->pads
[pad
].flags
& MEDIA_PAD_FL_SINK
))
120 ret
= media_create_pad_link(&source
->subdev
.entity
,
126 if (flags
& MEDIA_LNK_FL_ENABLED
)
127 source
->sink
= entity
;
134 static int vsp1_uapi_create_links(struct vsp1_device
*vsp1
)
136 struct vsp1_entity
*entity
;
140 list_for_each_entry(entity
, &vsp1
->entities
, list_dev
) {
141 if (entity
->type
== VSP1_ENTITY_LIF
||
142 entity
->type
== VSP1_ENTITY_RPF
)
145 ret
= vsp1_create_sink_links(vsp1
, entity
);
151 ret
= media_create_pad_link(&vsp1
->wpf
[0]->entity
.subdev
.entity
,
153 &vsp1
->lif
->entity
.subdev
.entity
,
159 for (i
= 0; i
< vsp1
->info
->rpf_count
; ++i
) {
160 struct vsp1_rwpf
*rpf
= vsp1
->rpf
[i
];
162 ret
= media_create_pad_link(&rpf
->video
->video
.entity
, 0,
163 &rpf
->entity
.subdev
.entity
,
165 MEDIA_LNK_FL_ENABLED
|
166 MEDIA_LNK_FL_IMMUTABLE
);
171 for (i
= 0; i
< vsp1
->info
->wpf_count
; ++i
) {
172 /* Connect the video device to the WPF. All connections are
173 * immutable except for the WPF0 source link if a LIF is
176 struct vsp1_rwpf
*wpf
= vsp1
->wpf
[i
];
177 unsigned int flags
= MEDIA_LNK_FL_ENABLED
;
179 if (!(vsp1
->info
->features
& VSP1_HAS_LIF
) || i
!= 0)
180 flags
|= MEDIA_LNK_FL_IMMUTABLE
;
182 ret
= media_create_pad_link(&wpf
->entity
.subdev
.entity
,
184 &wpf
->video
->video
.entity
, 0,
193 static void vsp1_destroy_entities(struct vsp1_device
*vsp1
)
195 struct vsp1_entity
*entity
, *_entity
;
196 struct vsp1_video
*video
, *_video
;
198 list_for_each_entry_safe(entity
, _entity
, &vsp1
->entities
, list_dev
) {
199 list_del(&entity
->list_dev
);
200 vsp1_entity_destroy(entity
);
203 list_for_each_entry_safe(video
, _video
, &vsp1
->videos
, list
) {
204 list_del(&video
->list
);
205 vsp1_video_cleanup(video
);
208 v4l2_device_unregister(&vsp1
->v4l2_dev
);
209 media_device_unregister(&vsp1
->media_dev
);
210 media_device_cleanup(&vsp1
->media_dev
);
212 if (!vsp1
->info
->uapi
)
213 vsp1_drm_cleanup(vsp1
);
216 static int vsp1_create_entities(struct vsp1_device
*vsp1
)
218 struct media_device
*mdev
= &vsp1
->media_dev
;
219 struct v4l2_device
*vdev
= &vsp1
->v4l2_dev
;
220 struct vsp1_entity
*entity
;
224 mdev
->dev
= vsp1
->dev
;
225 strlcpy(mdev
->model
, "VSP1", sizeof(mdev
->model
));
226 snprintf(mdev
->bus_info
, sizeof(mdev
->bus_info
), "platform:%s",
227 dev_name(mdev
->dev
));
228 media_device_init(mdev
);
230 vsp1
->media_ops
.link_setup
= vsp1_entity_link_setup
;
231 /* Don't perform link validation when the userspace API is disabled as
232 * the pipeline is configured internally by the driver in that case, and
233 * its configuration can thus be trusted.
235 if (vsp1
->info
->uapi
)
236 vsp1
->media_ops
.link_validate
= v4l2_subdev_link_validate
;
239 ret
= v4l2_device_register(vsp1
->dev
, vdev
);
241 dev_err(vsp1
->dev
, "V4L2 device registration failed (%d)\n",
246 /* Instantiate all the entities. */
247 if (vsp1
->info
->features
& VSP1_HAS_BRU
) {
248 vsp1
->bru
= vsp1_bru_create(vsp1
);
249 if (IS_ERR(vsp1
->bru
)) {
250 ret
= PTR_ERR(vsp1
->bru
);
254 list_add_tail(&vsp1
->bru
->entity
.list_dev
, &vsp1
->entities
);
257 vsp1
->hsi
= vsp1_hsit_create(vsp1
, true);
258 if (IS_ERR(vsp1
->hsi
)) {
259 ret
= PTR_ERR(vsp1
->hsi
);
263 list_add_tail(&vsp1
->hsi
->entity
.list_dev
, &vsp1
->entities
);
265 vsp1
->hst
= vsp1_hsit_create(vsp1
, false);
266 if (IS_ERR(vsp1
->hst
)) {
267 ret
= PTR_ERR(vsp1
->hst
);
271 list_add_tail(&vsp1
->hst
->entity
.list_dev
, &vsp1
->entities
);
273 if (vsp1
->info
->features
& VSP1_HAS_LIF
) {
274 vsp1
->lif
= vsp1_lif_create(vsp1
);
275 if (IS_ERR(vsp1
->lif
)) {
276 ret
= PTR_ERR(vsp1
->lif
);
280 list_add_tail(&vsp1
->lif
->entity
.list_dev
, &vsp1
->entities
);
283 if (vsp1
->info
->features
& VSP1_HAS_LUT
) {
284 vsp1
->lut
= vsp1_lut_create(vsp1
);
285 if (IS_ERR(vsp1
->lut
)) {
286 ret
= PTR_ERR(vsp1
->lut
);
290 list_add_tail(&vsp1
->lut
->entity
.list_dev
, &vsp1
->entities
);
293 for (i
= 0; i
< vsp1
->info
->rpf_count
; ++i
) {
294 struct vsp1_rwpf
*rpf
;
296 rpf
= vsp1_rpf_create(vsp1
, i
);
303 list_add_tail(&rpf
->entity
.list_dev
, &vsp1
->entities
);
305 if (vsp1
->info
->uapi
) {
306 struct vsp1_video
*video
= vsp1_video_create(vsp1
, rpf
);
309 ret
= PTR_ERR(video
);
313 list_add_tail(&video
->list
, &vsp1
->videos
);
317 if (vsp1
->info
->features
& VSP1_HAS_SRU
) {
318 vsp1
->sru
= vsp1_sru_create(vsp1
);
319 if (IS_ERR(vsp1
->sru
)) {
320 ret
= PTR_ERR(vsp1
->sru
);
324 list_add_tail(&vsp1
->sru
->entity
.list_dev
, &vsp1
->entities
);
327 for (i
= 0; i
< vsp1
->info
->uds_count
; ++i
) {
328 struct vsp1_uds
*uds
;
330 uds
= vsp1_uds_create(vsp1
, i
);
337 list_add_tail(&uds
->entity
.list_dev
, &vsp1
->entities
);
340 for (i
= 0; i
< vsp1
->info
->wpf_count
; ++i
) {
341 struct vsp1_rwpf
*wpf
;
343 wpf
= vsp1_wpf_create(vsp1
, i
);
350 list_add_tail(&wpf
->entity
.list_dev
, &vsp1
->entities
);
352 if (vsp1
->info
->uapi
) {
353 struct vsp1_video
*video
= vsp1_video_create(vsp1
, wpf
);
356 ret
= PTR_ERR(video
);
360 list_add_tail(&video
->list
, &vsp1
->videos
);
361 wpf
->entity
.sink
= &video
->video
.entity
;
365 /* Register all subdevs. */
366 list_for_each_entry(entity
, &vsp1
->entities
, list_dev
) {
367 ret
= v4l2_device_register_subdev(&vsp1
->v4l2_dev
,
374 if (vsp1
->info
->uapi
)
375 ret
= vsp1_uapi_create_links(vsp1
);
377 ret
= vsp1_drm_create_links(vsp1
);
381 /* Register subdev nodes if the userspace API is enabled or initialize
382 * the DRM pipeline otherwise.
384 if (vsp1
->info
->uapi
)
385 ret
= v4l2_device_register_subdev_nodes(&vsp1
->v4l2_dev
);
387 ret
= vsp1_drm_init(vsp1
);
391 ret
= media_device_register(mdev
);
395 vsp1_destroy_entities(vsp1
);
400 int vsp1_reset_wpf(struct vsp1_device
*vsp1
, unsigned int index
)
402 unsigned int timeout
;
405 status
= vsp1_read(vsp1
, VI6_STATUS
);
406 if (!(status
& VI6_STATUS_SYS_ACT(index
)))
409 vsp1_write(vsp1
, VI6_SRESET
, VI6_SRESET_SRTS(index
));
410 for (timeout
= 10; timeout
> 0; --timeout
) {
411 status
= vsp1_read(vsp1
, VI6_STATUS
);
412 if (!(status
& VI6_STATUS_SYS_ACT(index
)))
415 usleep_range(1000, 2000);
419 dev_err(vsp1
->dev
, "failed to reset wpf.%u\n", index
);
426 static int vsp1_device_init(struct vsp1_device
*vsp1
)
431 /* Reset any channel that might be running. */
432 for (i
= 0; i
< vsp1
->info
->wpf_count
; ++i
) {
433 ret
= vsp1_reset_wpf(vsp1
, i
);
438 vsp1_write(vsp1
, VI6_CLK_DCSWT
, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT
) |
439 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT
));
441 for (i
= 0; i
< vsp1
->info
->rpf_count
; ++i
)
442 vsp1_write(vsp1
, VI6_DPR_RPF_ROUTE(i
), VI6_DPR_NODE_UNUSED
);
444 for (i
= 0; i
< vsp1
->info
->uds_count
; ++i
)
445 vsp1_write(vsp1
, VI6_DPR_UDS_ROUTE(i
), VI6_DPR_NODE_UNUSED
);
447 vsp1_write(vsp1
, VI6_DPR_SRU_ROUTE
, VI6_DPR_NODE_UNUSED
);
448 vsp1_write(vsp1
, VI6_DPR_LUT_ROUTE
, VI6_DPR_NODE_UNUSED
);
449 vsp1_write(vsp1
, VI6_DPR_CLU_ROUTE
, VI6_DPR_NODE_UNUSED
);
450 vsp1_write(vsp1
, VI6_DPR_HST_ROUTE
, VI6_DPR_NODE_UNUSED
);
451 vsp1_write(vsp1
, VI6_DPR_HSI_ROUTE
, VI6_DPR_NODE_UNUSED
);
452 vsp1_write(vsp1
, VI6_DPR_BRU_ROUTE
, VI6_DPR_NODE_UNUSED
);
454 vsp1_write(vsp1
, VI6_DPR_HGO_SMPPT
, (7 << VI6_DPR_SMPPT_TGW_SHIFT
) |
455 (VI6_DPR_NODE_UNUSED
<< VI6_DPR_SMPPT_PT_SHIFT
));
456 vsp1_write(vsp1
, VI6_DPR_HGT_SMPPT
, (7 << VI6_DPR_SMPPT_TGW_SHIFT
) |
457 (VI6_DPR_NODE_UNUSED
<< VI6_DPR_SMPPT_PT_SHIFT
));
459 vsp1_dlm_setup(vsp1
);
465 * vsp1_device_get - Acquire the VSP1 device
467 * Make sure the device is not suspended and initialize it if needed.
469 * Return 0 on success or a negative error code otherwise.
471 int vsp1_device_get(struct vsp1_device
*vsp1
)
475 ret
= pm_runtime_get_sync(vsp1
->dev
);
476 return ret
< 0 ? ret
: 0;
480 * vsp1_device_put - Release the VSP1 device
482 * Decrement the VSP1 reference count and cleanup the device if the last
483 * reference is released.
485 void vsp1_device_put(struct vsp1_device
*vsp1
)
487 pm_runtime_put_sync(vsp1
->dev
);
490 /* -----------------------------------------------------------------------------
494 #ifdef CONFIG_PM_SLEEP
495 static int vsp1_pm_suspend(struct device
*dev
)
497 struct vsp1_device
*vsp1
= dev_get_drvdata(dev
);
499 vsp1_pipelines_suspend(vsp1
);
500 pm_runtime_force_suspend(vsp1
->dev
);
505 static int vsp1_pm_resume(struct device
*dev
)
507 struct vsp1_device
*vsp1
= dev_get_drvdata(dev
);
509 pm_runtime_force_resume(vsp1
->dev
);
510 vsp1_pipelines_resume(vsp1
);
516 static int vsp1_pm_runtime_suspend(struct device
*dev
)
518 struct vsp1_device
*vsp1
= dev_get_drvdata(dev
);
520 rcar_fcp_disable(vsp1
->fcp
);
525 static int vsp1_pm_runtime_resume(struct device
*dev
)
527 struct vsp1_device
*vsp1
= dev_get_drvdata(dev
);
531 ret
= vsp1_device_init(vsp1
);
536 return rcar_fcp_enable(vsp1
->fcp
);
539 static const struct dev_pm_ops vsp1_pm_ops
= {
540 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend
, vsp1_pm_resume
)
541 SET_RUNTIME_PM_OPS(vsp1_pm_runtime_suspend
, vsp1_pm_runtime_resume
, NULL
)
544 /* -----------------------------------------------------------------------------
548 static const struct vsp1_device_info vsp1_device_infos
[] = {
550 .version
= VI6_IP_VERSION_MODEL_VSPS_H2
,
552 .features
= VSP1_HAS_BRU
| VSP1_HAS_LUT
| VSP1_HAS_SRU
,
559 .version
= VI6_IP_VERSION_MODEL_VSPR_H2
,
561 .features
= VSP1_HAS_BRU
| VSP1_HAS_SRU
,
568 .version
= VI6_IP_VERSION_MODEL_VSPD_GEN2
,
570 .features
= VSP1_HAS_BRU
| VSP1_HAS_LIF
| VSP1_HAS_LUT
,
577 .version
= VI6_IP_VERSION_MODEL_VSPS_M2
,
579 .features
= VSP1_HAS_BRU
| VSP1_HAS_LUT
| VSP1_HAS_SRU
,
586 .version
= VI6_IP_VERSION_MODEL_VSPI_GEN3
,
588 .features
= VSP1_HAS_LUT
| VSP1_HAS_SRU
,
594 .version
= VI6_IP_VERSION_MODEL_VSPBD_GEN3
,
596 .features
= VSP1_HAS_BRU
,
602 .version
= VI6_IP_VERSION_MODEL_VSPBC_GEN3
,
604 .features
= VSP1_HAS_BRU
| VSP1_HAS_LUT
,
610 .version
= VI6_IP_VERSION_MODEL_VSPD_GEN3
,
612 .features
= VSP1_HAS_BRU
| VSP1_HAS_LIF
,
619 static int vsp1_probe(struct platform_device
*pdev
)
621 struct vsp1_device
*vsp1
;
622 struct device_node
*fcp_node
;
623 struct resource
*irq
;
629 vsp1
= devm_kzalloc(&pdev
->dev
, sizeof(*vsp1
), GFP_KERNEL
);
633 vsp1
->dev
= &pdev
->dev
;
634 INIT_LIST_HEAD(&vsp1
->entities
);
635 INIT_LIST_HEAD(&vsp1
->videos
);
637 platform_set_drvdata(pdev
, vsp1
);
639 /* I/O and IRQ resources (clock managed by the clock PM domain) */
640 io
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
641 vsp1
->mmio
= devm_ioremap_resource(&pdev
->dev
, io
);
642 if (IS_ERR(vsp1
->mmio
))
643 return PTR_ERR(vsp1
->mmio
);
645 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
647 dev_err(&pdev
->dev
, "missing IRQ\n");
651 ret
= devm_request_irq(&pdev
->dev
, irq
->start
, vsp1_irq_handler
,
652 IRQF_SHARED
, dev_name(&pdev
->dev
), vsp1
);
654 dev_err(&pdev
->dev
, "failed to request IRQ\n");
659 fcp_node
= of_parse_phandle(pdev
->dev
.of_node
, "renesas,fcp", 0);
661 vsp1
->fcp
= rcar_fcp_get(fcp_node
);
662 of_node_put(fcp_node
);
663 if (IS_ERR(vsp1
->fcp
)) {
664 dev_dbg(&pdev
->dev
, "FCP not found (%ld)\n",
666 return PTR_ERR(vsp1
->fcp
);
670 /* Configure device parameters based on the version register. */
671 pm_runtime_enable(&pdev
->dev
);
673 ret
= pm_runtime_get_sync(&pdev
->dev
);
677 version
= vsp1_read(vsp1
, VI6_IP_VERSION
);
678 pm_runtime_put_sync(&pdev
->dev
);
680 for (i
= 0; i
< ARRAY_SIZE(vsp1_device_infos
); ++i
) {
681 if ((version
& VI6_IP_VERSION_MODEL_MASK
) ==
682 vsp1_device_infos
[i
].version
) {
683 vsp1
->info
= &vsp1_device_infos
[i
];
689 dev_err(&pdev
->dev
, "unsupported IP version 0x%08x\n", version
);
694 dev_dbg(&pdev
->dev
, "IP version 0x%08x\n", version
);
696 /* Instanciate entities */
697 ret
= vsp1_create_entities(vsp1
);
699 dev_err(&pdev
->dev
, "failed to create entities\n");
705 pm_runtime_disable(&pdev
->dev
);
710 static int vsp1_remove(struct platform_device
*pdev
)
712 struct vsp1_device
*vsp1
= platform_get_drvdata(pdev
);
714 vsp1_destroy_entities(vsp1
);
715 rcar_fcp_put(vsp1
->fcp
);
717 pm_runtime_disable(&pdev
->dev
);
722 static const struct of_device_id vsp1_of_match
[] = {
723 { .compatible
= "renesas,vsp1" },
724 { .compatible
= "renesas,vsp2" },
728 static struct platform_driver vsp1_platform_driver
= {
730 .remove
= vsp1_remove
,
734 .of_match_table
= vsp1_of_match
,
738 module_platform_driver(vsp1_platform_driver
);
740 MODULE_ALIAS("vsp1");
741 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
742 MODULE_DESCRIPTION("Renesas VSP1 Driver");
743 MODULE_LICENSE("GPL");