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>
33 #include "vsp1_hsit.h"
36 #include "vsp1_pipe.h"
37 #include "vsp1_rwpf.h"
40 #include "vsp1_video.h"
42 /* -----------------------------------------------------------------------------
46 static irqreturn_t vsp1_irq_handler(int irq, void *data)
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;
54 for (i = 0; i < vsp1->info->wpf_count; ++i) {
55 struct vsp1_rwpf *wpf = vsp1->wpf[i];
60 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
61 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
63 if (status & VI6_WFP_IRQ_STA_DFE) {
64 vsp1_pipeline_frame_end(wpf->pipe);
69 status = vsp1_read(vsp1, VI6_DISP_IRQ_STA);
70 vsp1_write(vsp1, VI6_DISP_IRQ_STA, ~status & VI6_DISP_IRQ_STA_DST);
72 if (status & VI6_DISP_IRQ_STA_DST) {
73 vsp1_drm_display_start(vsp1);
80 /* -----------------------------------------------------------------------------
85 * vsp1_create_sink_links - Create links from all sources to the given sink
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
91 * - from a UDS to a UDS (UDS entities can't be chained)
92 * - from an entity to itself (no loops are allowed)
94 static int vsp1_create_sink_links(struct vsp1_device *vsp1,
95 struct vsp1_entity *sink)
97 struct media_entity *entity = &sink->subdev.entity;
98 struct vsp1_entity *source;
102 list_for_each_entry(source, &vsp1->entities, list_dev) {
105 if (source->type == sink->type)
108 if (source->type == VSP1_ENTITY_LIF ||
109 source->type == VSP1_ENTITY_WPF)
112 flags = source->type == VSP1_ENTITY_RPF &&
113 sink->type == VSP1_ENTITY_WPF &&
114 source->index == sink->index
115 ? MEDIA_LNK_FL_ENABLED : 0;
117 for (pad = 0; pad < entity->num_pads; ++pad) {
118 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
121 ret = media_create_pad_link(&source->subdev.entity,
127 if (flags & MEDIA_LNK_FL_ENABLED)
128 source->sink = entity;
135 static int vsp1_uapi_create_links(struct vsp1_device *vsp1)
137 struct vsp1_entity *entity;
141 list_for_each_entry(entity, &vsp1->entities, list_dev) {
142 if (entity->type == VSP1_ENTITY_LIF ||
143 entity->type == VSP1_ENTITY_RPF)
146 ret = vsp1_create_sink_links(vsp1, entity);
152 ret = media_create_pad_link(&vsp1->wpf[0]->entity.subdev.entity,
154 &vsp1->lif->entity.subdev.entity,
160 for (i = 0; i < vsp1->info->rpf_count; ++i) {
161 struct vsp1_rwpf *rpf = vsp1->rpf[i];
163 ret = media_create_pad_link(&rpf->video->video.entity, 0,
164 &rpf->entity.subdev.entity,
166 MEDIA_LNK_FL_ENABLED |
167 MEDIA_LNK_FL_IMMUTABLE);
172 for (i = 0; i < vsp1->info->wpf_count; ++i) {
173 /* Connect the video device to the WPF. All connections are
176 struct vsp1_rwpf *wpf = vsp1->wpf[i];
178 ret = media_create_pad_link(&wpf->entity.subdev.entity,
180 &wpf->video->video.entity, 0,
181 MEDIA_LNK_FL_IMMUTABLE |
182 MEDIA_LNK_FL_ENABLED);
190 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
192 struct vsp1_entity *entity, *_entity;
193 struct vsp1_video *video, *_video;
195 list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
196 list_del(&entity->list_dev);
197 vsp1_entity_destroy(entity);
200 list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
201 list_del(&video->list);
202 vsp1_video_cleanup(video);
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);
210 if (!vsp1->info->uapi)
211 vsp1_drm_cleanup(vsp1);
214 static int vsp1_create_entities(struct vsp1_device *vsp1)
216 struct media_device *mdev = &vsp1->media_dev;
217 struct v4l2_device *vdev = &vsp1->v4l2_dev;
218 struct vsp1_entity *entity;
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);
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.
234 if (vsp1->info->uapi)
235 vsp1->media_ops.link_validate = v4l2_subdev_link_validate;
238 ret = v4l2_device_register(vsp1->dev, vdev);
240 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
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);
253 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
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);
263 list_add_tail(&vsp1->clu->entity.list_dev, &vsp1->entities);
266 vsp1->hsi = vsp1_hsit_create(vsp1, true);
267 if (IS_ERR(vsp1->hsi)) {
268 ret = PTR_ERR(vsp1->hsi);
272 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
274 vsp1->hst = vsp1_hsit_create(vsp1, false);
275 if (IS_ERR(vsp1->hst)) {
276 ret = PTR_ERR(vsp1->hst);
280 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
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.
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);
293 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
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);
303 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
306 for (i = 0; i < vsp1->info->rpf_count; ++i) {
307 struct vsp1_rwpf *rpf;
309 rpf = vsp1_rpf_create(vsp1, i);
316 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
318 if (vsp1->info->uapi) {
319 struct vsp1_video *video = vsp1_video_create(vsp1, rpf);
322 ret = PTR_ERR(video);
326 list_add_tail(&video->list, &vsp1->videos);
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);
337 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
340 for (i = 0; i < vsp1->info->uds_count; ++i) {
341 struct vsp1_uds *uds;
343 uds = vsp1_uds_create(vsp1, i);
350 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
353 for (i = 0; i < vsp1->info->wpf_count; ++i) {
354 struct vsp1_rwpf *wpf;
356 wpf = vsp1_wpf_create(vsp1, i);
363 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
365 if (vsp1->info->uapi) {
366 struct vsp1_video *video = vsp1_video_create(vsp1, wpf);
369 ret = PTR_ERR(video);
373 list_add_tail(&video->list, &vsp1->videos);
374 wpf->entity.sink = &video->video.entity;
378 /* Register all subdevs. */
379 list_for_each_entry(entity, &vsp1->entities, list_dev) {
380 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
387 if (vsp1->info->uapi)
388 ret = vsp1_uapi_create_links(vsp1);
390 ret = vsp1_drm_create_links(vsp1);
394 /* Register subdev nodes if the userspace API is enabled or initialize
395 * the DRM pipeline otherwise.
397 if (vsp1->info->uapi) {
398 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
402 ret = media_device_register(mdev);
404 ret = vsp1_drm_init(vsp1);
409 vsp1_destroy_entities(vsp1);
414 int vsp1_reset_wpf(struct vsp1_device *vsp1, unsigned int index)
416 unsigned int timeout;
419 status = vsp1_read(vsp1, VI6_STATUS);
420 if (!(status & VI6_STATUS_SYS_ACT(index)))
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)))
429 usleep_range(1000, 2000);
433 dev_err(vsp1->dev, "failed to reset wpf.%u\n", index);
440 static int vsp1_device_init(struct vsp1_device *vsp1)
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);
452 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
453 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
455 for (i = 0; i < vsp1->info->rpf_count; ++i)
456 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
458 for (i = 0; i < vsp1->info->uds_count; ++i)
459 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
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);
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));
473 vsp1_dlm_setup(vsp1);
479 * vsp1_device_get - Acquire the VSP1 device
481 * Make sure the device is not suspended and initialize it if needed.
483 * Return 0 on success or a negative error code otherwise.
485 int vsp1_device_get(struct vsp1_device *vsp1)
489 ret = pm_runtime_get_sync(vsp1->dev);
490 return ret < 0 ? ret : 0;
494 * vsp1_device_put - Release the VSP1 device
496 * Decrement the VSP1 reference count and cleanup the device if the last
497 * reference is released.
499 void vsp1_device_put(struct vsp1_device *vsp1)
501 pm_runtime_put_sync(vsp1->dev);
504 /* -----------------------------------------------------------------------------
508 static int __maybe_unused vsp1_pm_suspend(struct device *dev)
510 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
512 vsp1_pipelines_suspend(vsp1);
513 pm_runtime_force_suspend(vsp1->dev);
518 static int __maybe_unused vsp1_pm_resume(struct device *dev)
520 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
522 pm_runtime_force_resume(vsp1->dev);
523 vsp1_pipelines_resume(vsp1);
528 static int __maybe_unused vsp1_pm_runtime_suspend(struct device *dev)
530 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
532 rcar_fcp_disable(vsp1->fcp);
537 static int __maybe_unused vsp1_pm_runtime_resume(struct device *dev)
539 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
543 ret = vsp1_device_init(vsp1);
548 return rcar_fcp_enable(vsp1->fcp);
551 static const struct dev_pm_ops vsp1_pm_ops = {
552 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
553 SET_RUNTIME_PM_OPS(vsp1_pm_runtime_suspend, vsp1_pm_runtime_resume, NULL)
556 /* -----------------------------------------------------------------------------
560 static const struct vsp1_device_info vsp1_device_infos[] = {
562 .version = VI6_IP_VERSION_MODEL_VSPS_H2,
565 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
566 | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
573 .version = VI6_IP_VERSION_MODEL_VSPR_H2,
576 .features = VSP1_HAS_BRU | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
583 .version = VI6_IP_VERSION_MODEL_VSPD_GEN2,
586 .features = VSP1_HAS_BRU | VSP1_HAS_LIF | VSP1_HAS_LUT,
593 .version = VI6_IP_VERSION_MODEL_VSPS_M2,
596 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
597 | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
604 .version = VI6_IP_VERSION_MODEL_VSPS_V2H,
607 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
608 | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
615 .version = VI6_IP_VERSION_MODEL_VSPD_V2H,
618 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
626 .version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
629 .features = VSP1_HAS_CLU | VSP1_HAS_LUT | VSP1_HAS_SRU
630 | VSP1_HAS_WPF_HFLIP | VSP1_HAS_WPF_VFLIP,
636 .version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
639 .features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
645 .version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
648 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
649 | VSP1_HAS_WPF_VFLIP,
655 .version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
658 .features = VSP1_HAS_BRU | VSP1_HAS_LIF | VSP1_HAS_WPF_VFLIP,
665 static int vsp1_probe(struct platform_device *pdev)
667 struct vsp1_device *vsp1;
668 struct device_node *fcp_node;
669 struct resource *irq;
674 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
678 vsp1->dev = &pdev->dev;
679 INIT_LIST_HEAD(&vsp1->entities);
680 INIT_LIST_HEAD(&vsp1->videos);
682 platform_set_drvdata(pdev, vsp1);
684 /* I/O and IRQ resources (clock managed by the clock PM domain) */
685 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
686 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
687 if (IS_ERR(vsp1->mmio))
688 return PTR_ERR(vsp1->mmio);
690 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
692 dev_err(&pdev->dev, "missing IRQ\n");
696 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
697 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
699 dev_err(&pdev->dev, "failed to request IRQ\n");
704 fcp_node = of_parse_phandle(pdev->dev.of_node, "renesas,fcp", 0);
706 vsp1->fcp = rcar_fcp_get(fcp_node);
707 of_node_put(fcp_node);
708 if (IS_ERR(vsp1->fcp)) {
709 dev_dbg(&pdev->dev, "FCP not found (%ld)\n",
711 return PTR_ERR(vsp1->fcp);
715 /* Configure device parameters based on the version register. */
716 pm_runtime_enable(&pdev->dev);
718 ret = pm_runtime_get_sync(&pdev->dev);
722 vsp1->version = vsp1_read(vsp1, VI6_IP_VERSION);
723 pm_runtime_put_sync(&pdev->dev);
725 for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) {
726 if ((vsp1->version & VI6_IP_VERSION_MODEL_MASK) ==
727 vsp1_device_infos[i].version) {
728 vsp1->info = &vsp1_device_infos[i];
734 dev_err(&pdev->dev, "unsupported IP version 0x%08x\n",
740 dev_dbg(&pdev->dev, "IP version 0x%08x\n", vsp1->version);
742 /* Instanciate entities */
743 ret = vsp1_create_entities(vsp1);
745 dev_err(&pdev->dev, "failed to create entities\n");
751 pm_runtime_disable(&pdev->dev);
756 static int vsp1_remove(struct platform_device *pdev)
758 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
760 vsp1_destroy_entities(vsp1);
761 rcar_fcp_put(vsp1->fcp);
763 pm_runtime_disable(&pdev->dev);
768 static const struct of_device_id vsp1_of_match[] = {
769 { .compatible = "renesas,vsp1" },
770 { .compatible = "renesas,vsp2" },
774 static struct platform_driver vsp1_platform_driver = {
776 .remove = vsp1_remove,
780 .of_match_table = vsp1_of_match,
784 module_platform_driver(vsp1_platform_driver);
786 MODULE_ALIAS("vsp1");
787 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
788 MODULE_DESCRIPTION("Renesas VSP1 Driver");
789 MODULE_LICENSE("GPL");