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>
35 #include "vsp1_hsit.h"
38 #include "vsp1_pipe.h"
39 #include "vsp1_rwpf.h"
42 #include "vsp1_video.h"
44 /* -----------------------------------------------------------------------------
48 static irqreturn_t vsp1_irq_handler(int irq, void *data)
50 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
51 struct vsp1_device *vsp1 = data;
52 irqreturn_t ret = IRQ_NONE;
56 for (i = 0; i < vsp1->info->wpf_count; ++i) {
57 struct vsp1_rwpf *wpf = vsp1->wpf[i];
62 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
63 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
65 if (status & VI6_WFP_IRQ_STA_DFE) {
66 vsp1_pipeline_frame_end(wpf->pipe);
74 /* -----------------------------------------------------------------------------
79 * vsp1_create_sink_links - Create links from all sources to the given sink
81 * This function creates media links from all valid sources to the given sink
82 * pad. Links that would be invalid according to the VSP1 hardware capabilities
83 * are skipped. Those include all links
85 * - from a UDS to a UDS (UDS entities can't be chained)
86 * - from an entity to itself (no loops are allowed)
88 * Furthermore, the BRS can't be connected to histogram generators, but no
89 * special check is currently needed as all VSP instances that include a BRS
90 * have no histogram generator.
92 static int vsp1_create_sink_links(struct vsp1_device *vsp1,
93 struct vsp1_entity *sink)
95 struct media_entity *entity = &sink->subdev.entity;
96 struct vsp1_entity *source;
100 list_for_each_entry(source, &vsp1->entities, list_dev) {
103 if (source->type == sink->type)
106 if (source->type == VSP1_ENTITY_HGO ||
107 source->type == VSP1_ENTITY_HGT ||
108 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)
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->hgo->histo.entity.subdev.entity,
154 &vsp1->hgo->histo.video.entity, 0,
155 MEDIA_LNK_FL_ENABLED |
156 MEDIA_LNK_FL_IMMUTABLE);
162 ret = media_create_pad_link(&vsp1->hgt->histo.entity.subdev.entity,
164 &vsp1->hgt->histo.video.entity, 0,
165 MEDIA_LNK_FL_ENABLED |
166 MEDIA_LNK_FL_IMMUTABLE);
171 for (i = 0; i < vsp1->info->lif_count; ++i) {
175 ret = media_create_pad_link(&vsp1->wpf[i]->entity.subdev.entity,
177 &vsp1->lif[i]->entity.subdev.entity,
183 for (i = 0; i < vsp1->info->rpf_count; ++i) {
184 struct vsp1_rwpf *rpf = vsp1->rpf[i];
186 ret = media_create_pad_link(&rpf->video->video.entity, 0,
187 &rpf->entity.subdev.entity,
189 MEDIA_LNK_FL_ENABLED |
190 MEDIA_LNK_FL_IMMUTABLE);
195 for (i = 0; i < vsp1->info->wpf_count; ++i) {
197 * Connect the video device to the WPF. All connections are
200 struct vsp1_rwpf *wpf = vsp1->wpf[i];
202 ret = media_create_pad_link(&wpf->entity.subdev.entity,
204 &wpf->video->video.entity, 0,
205 MEDIA_LNK_FL_IMMUTABLE |
206 MEDIA_LNK_FL_ENABLED);
214 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
216 struct vsp1_entity *entity, *_entity;
217 struct vsp1_video *video, *_video;
219 list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
220 list_del(&entity->list_dev);
221 vsp1_entity_destroy(entity);
224 list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
225 list_del(&video->list);
226 vsp1_video_cleanup(video);
229 v4l2_device_unregister(&vsp1->v4l2_dev);
230 if (vsp1->info->uapi)
231 media_device_unregister(&vsp1->media_dev);
232 media_device_cleanup(&vsp1->media_dev);
234 if (!vsp1->info->uapi)
235 vsp1_drm_cleanup(vsp1);
238 static int vsp1_create_entities(struct vsp1_device *vsp1)
240 struct media_device *mdev = &vsp1->media_dev;
241 struct v4l2_device *vdev = &vsp1->v4l2_dev;
242 struct vsp1_entity *entity;
246 mdev->dev = vsp1->dev;
247 mdev->hw_revision = vsp1->version;
248 strlcpy(mdev->model, vsp1->info->model, sizeof(mdev->model));
249 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
250 dev_name(mdev->dev));
251 media_device_init(mdev);
253 vsp1->media_ops.link_setup = vsp1_entity_link_setup;
255 * Don't perform link validation when the userspace API is disabled as
256 * the pipeline is configured internally by the driver in that case, and
257 * its configuration can thus be trusted.
259 if (vsp1->info->uapi)
260 vsp1->media_ops.link_validate = v4l2_subdev_link_validate;
263 ret = v4l2_device_register(vsp1->dev, vdev);
265 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
270 /* Instantiate all the entities. */
271 if (vsp1->info->features & VSP1_HAS_BRS) {
272 vsp1->brs = vsp1_bru_create(vsp1, VSP1_ENTITY_BRS);
273 if (IS_ERR(vsp1->brs)) {
274 ret = PTR_ERR(vsp1->brs);
278 list_add_tail(&vsp1->brs->entity.list_dev, &vsp1->entities);
281 if (vsp1->info->features & VSP1_HAS_BRU) {
282 vsp1->bru = vsp1_bru_create(vsp1, VSP1_ENTITY_BRU);
283 if (IS_ERR(vsp1->bru)) {
284 ret = PTR_ERR(vsp1->bru);
288 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
291 if (vsp1->info->features & VSP1_HAS_CLU) {
292 vsp1->clu = vsp1_clu_create(vsp1);
293 if (IS_ERR(vsp1->clu)) {
294 ret = PTR_ERR(vsp1->clu);
298 list_add_tail(&vsp1->clu->entity.list_dev, &vsp1->entities);
301 vsp1->hsi = vsp1_hsit_create(vsp1, true);
302 if (IS_ERR(vsp1->hsi)) {
303 ret = PTR_ERR(vsp1->hsi);
307 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
309 vsp1->hst = vsp1_hsit_create(vsp1, false);
310 if (IS_ERR(vsp1->hst)) {
311 ret = PTR_ERR(vsp1->hst);
315 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
317 if (vsp1->info->features & VSP1_HAS_HGO && vsp1->info->uapi) {
318 vsp1->hgo = vsp1_hgo_create(vsp1);
319 if (IS_ERR(vsp1->hgo)) {
320 ret = PTR_ERR(vsp1->hgo);
324 list_add_tail(&vsp1->hgo->histo.entity.list_dev,
328 if (vsp1->info->features & VSP1_HAS_HGT && vsp1->info->uapi) {
329 vsp1->hgt = vsp1_hgt_create(vsp1);
330 if (IS_ERR(vsp1->hgt)) {
331 ret = PTR_ERR(vsp1->hgt);
335 list_add_tail(&vsp1->hgt->histo.entity.list_dev,
340 * The LIFs are only supported when used in conjunction with the DU, in
341 * which case the userspace API is disabled. If the userspace API is
342 * enabled skip the LIFs, even when present.
344 if (!vsp1->info->uapi) {
345 for (i = 0; i < vsp1->info->lif_count; ++i) {
346 struct vsp1_lif *lif;
348 lif = vsp1_lif_create(vsp1, i);
355 list_add_tail(&lif->entity.list_dev, &vsp1->entities);
359 if (vsp1->info->features & VSP1_HAS_LUT) {
360 vsp1->lut = vsp1_lut_create(vsp1);
361 if (IS_ERR(vsp1->lut)) {
362 ret = PTR_ERR(vsp1->lut);
366 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
369 for (i = 0; i < vsp1->info->rpf_count; ++i) {
370 struct vsp1_rwpf *rpf;
372 rpf = vsp1_rpf_create(vsp1, i);
379 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
381 if (vsp1->info->uapi) {
382 struct vsp1_video *video = vsp1_video_create(vsp1, rpf);
385 ret = PTR_ERR(video);
389 list_add_tail(&video->list, &vsp1->videos);
393 if (vsp1->info->features & VSP1_HAS_SRU) {
394 vsp1->sru = vsp1_sru_create(vsp1);
395 if (IS_ERR(vsp1->sru)) {
396 ret = PTR_ERR(vsp1->sru);
400 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
403 for (i = 0; i < vsp1->info->uds_count; ++i) {
404 struct vsp1_uds *uds;
406 uds = vsp1_uds_create(vsp1, i);
413 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
416 for (i = 0; i < vsp1->info->wpf_count; ++i) {
417 struct vsp1_rwpf *wpf;
419 wpf = vsp1_wpf_create(vsp1, i);
426 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
428 if (vsp1->info->uapi) {
429 struct vsp1_video *video = vsp1_video_create(vsp1, wpf);
432 ret = PTR_ERR(video);
436 list_add_tail(&video->list, &vsp1->videos);
440 /* Register all subdevs. */
441 list_for_each_entry(entity, &vsp1->entities, list_dev) {
442 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
449 * Create links and register subdev nodes if the userspace API is
450 * enabled or initialize the DRM pipeline otherwise.
452 if (vsp1->info->uapi) {
453 ret = vsp1_uapi_create_links(vsp1);
457 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
461 ret = media_device_register(mdev);
463 ret = vsp1_drm_init(vsp1);
468 vsp1_destroy_entities(vsp1);
473 int vsp1_reset_wpf(struct vsp1_device *vsp1, unsigned int index)
475 unsigned int timeout;
478 status = vsp1_read(vsp1, VI6_STATUS);
479 if (!(status & VI6_STATUS_SYS_ACT(index)))
482 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(index));
483 for (timeout = 10; timeout > 0; --timeout) {
484 status = vsp1_read(vsp1, VI6_STATUS);
485 if (!(status & VI6_STATUS_SYS_ACT(index)))
488 usleep_range(1000, 2000);
492 dev_err(vsp1->dev, "failed to reset wpf.%u\n", index);
499 static int vsp1_device_init(struct vsp1_device *vsp1)
504 /* Reset any channel that might be running. */
505 for (i = 0; i < vsp1->info->wpf_count; ++i) {
506 ret = vsp1_reset_wpf(vsp1, i);
511 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
512 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
514 for (i = 0; i < vsp1->info->rpf_count; ++i)
515 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
517 for (i = 0; i < vsp1->info->uds_count; ++i)
518 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
520 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
521 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
522 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
523 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
524 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
525 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
527 if (vsp1->info->features & VSP1_HAS_BRS)
528 vsp1_write(vsp1, VI6_DPR_ILV_BRS_ROUTE, VI6_DPR_NODE_UNUSED);
530 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
531 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
532 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
533 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
535 vsp1_dlm_setup(vsp1);
541 * vsp1_device_get - Acquire the VSP1 device
543 * Make sure the device is not suspended and initialize it if needed.
545 * Return 0 on success or a negative error code otherwise.
547 int vsp1_device_get(struct vsp1_device *vsp1)
551 ret = pm_runtime_get_sync(vsp1->dev);
552 return ret < 0 ? ret : 0;
556 * vsp1_device_put - Release the VSP1 device
558 * Decrement the VSP1 reference count and cleanup the device if the last
559 * reference is released.
561 void vsp1_device_put(struct vsp1_device *vsp1)
563 pm_runtime_put_sync(vsp1->dev);
566 /* -----------------------------------------------------------------------------
570 static int __maybe_unused vsp1_pm_suspend(struct device *dev)
572 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
574 vsp1_pipelines_suspend(vsp1);
575 pm_runtime_force_suspend(vsp1->dev);
580 static int __maybe_unused vsp1_pm_resume(struct device *dev)
582 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
584 pm_runtime_force_resume(vsp1->dev);
585 vsp1_pipelines_resume(vsp1);
590 static int __maybe_unused vsp1_pm_runtime_suspend(struct device *dev)
592 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
594 rcar_fcp_disable(vsp1->fcp);
599 static int __maybe_unused vsp1_pm_runtime_resume(struct device *dev)
601 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
605 ret = vsp1_device_init(vsp1);
610 return rcar_fcp_enable(vsp1->fcp);
613 static const struct dev_pm_ops vsp1_pm_ops = {
614 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
615 SET_RUNTIME_PM_OPS(vsp1_pm_runtime_suspend, vsp1_pm_runtime_resume, NULL)
618 /* -----------------------------------------------------------------------------
622 static const struct vsp1_device_info vsp1_device_infos[] = {
624 .version = VI6_IP_VERSION_MODEL_VSPS_H2,
627 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
628 | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
629 | VSP1_HAS_WPF_VFLIP,
636 .version = VI6_IP_VERSION_MODEL_VSPR_H2,
639 .features = VSP1_HAS_BRU | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
646 .version = VI6_IP_VERSION_MODEL_VSPD_GEN2,
649 .features = VSP1_HAS_BRU | VSP1_HAS_HGO | VSP1_HAS_LUT,
657 .version = VI6_IP_VERSION_MODEL_VSPS_M2,
660 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
661 | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
662 | VSP1_HAS_WPF_VFLIP,
669 .version = VI6_IP_VERSION_MODEL_VSPS_V2H,
672 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
673 | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
680 .version = VI6_IP_VERSION_MODEL_VSPD_V2H,
683 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT,
691 .version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
694 .features = VSP1_HAS_CLU | VSP1_HAS_HGO | VSP1_HAS_HGT
695 | VSP1_HAS_LUT | VSP1_HAS_SRU | VSP1_HAS_WPF_HFLIP
696 | VSP1_HAS_WPF_VFLIP,
702 .version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
705 .features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
711 .version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
714 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
715 | VSP1_HAS_LUT | VSP1_HAS_WPF_VFLIP,
721 .version = VI6_IP_VERSION_MODEL_VSPBS_GEN3,
724 .features = VSP1_HAS_BRS | VSP1_HAS_WPF_VFLIP,
729 .version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
732 .features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
738 .version = VI6_IP_VERSION_MODEL_VSPD_V3,
741 .features = VSP1_HAS_BRS | VSP1_HAS_BRU,
747 .version = VI6_IP_VERSION_MODEL_VSPDL_GEN3,
750 .features = VSP1_HAS_BRS | VSP1_HAS_BRU,
758 static int vsp1_probe(struct platform_device *pdev)
760 struct vsp1_device *vsp1;
761 struct device_node *fcp_node;
762 struct resource *irq;
767 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
771 vsp1->dev = &pdev->dev;
772 INIT_LIST_HEAD(&vsp1->entities);
773 INIT_LIST_HEAD(&vsp1->videos);
775 platform_set_drvdata(pdev, vsp1);
777 /* I/O and IRQ resources (clock managed by the clock PM domain) */
778 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
779 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
780 if (IS_ERR(vsp1->mmio))
781 return PTR_ERR(vsp1->mmio);
783 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
785 dev_err(&pdev->dev, "missing IRQ\n");
789 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
790 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
792 dev_err(&pdev->dev, "failed to request IRQ\n");
797 fcp_node = of_parse_phandle(pdev->dev.of_node, "renesas,fcp", 0);
799 vsp1->fcp = rcar_fcp_get(fcp_node);
800 of_node_put(fcp_node);
801 if (IS_ERR(vsp1->fcp)) {
802 dev_dbg(&pdev->dev, "FCP not found (%ld)\n",
804 return PTR_ERR(vsp1->fcp);
808 * When the FCP is present, it handles all bus master accesses
809 * for the VSP and must thus be used in place of the VSP device
810 * to map DMA buffers.
812 vsp1->bus_master = rcar_fcp_get_device(vsp1->fcp);
814 vsp1->bus_master = vsp1->dev;
817 /* Configure device parameters based on the version register. */
818 pm_runtime_enable(&pdev->dev);
820 ret = pm_runtime_get_sync(&pdev->dev);
824 vsp1->version = vsp1_read(vsp1, VI6_IP_VERSION);
825 pm_runtime_put_sync(&pdev->dev);
827 for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) {
828 if ((vsp1->version & VI6_IP_VERSION_MODEL_MASK) ==
829 vsp1_device_infos[i].version) {
830 vsp1->info = &vsp1_device_infos[i];
836 dev_err(&pdev->dev, "unsupported IP version 0x%08x\n",
842 dev_dbg(&pdev->dev, "IP version 0x%08x\n", vsp1->version);
844 /* Instanciate entities */
845 ret = vsp1_create_entities(vsp1);
847 dev_err(&pdev->dev, "failed to create entities\n");
853 pm_runtime_disable(&pdev->dev);
858 static int vsp1_remove(struct platform_device *pdev)
860 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
862 vsp1_destroy_entities(vsp1);
863 rcar_fcp_put(vsp1->fcp);
865 pm_runtime_disable(&pdev->dev);
870 static const struct of_device_id vsp1_of_match[] = {
871 { .compatible = "renesas,vsp1" },
872 { .compatible = "renesas,vsp2" },
875 MODULE_DEVICE_TABLE(of, vsp1_of_match);
877 static struct platform_driver vsp1_platform_driver = {
879 .remove = vsp1_remove,
883 .of_match_table = vsp1_of_match,
887 module_platform_driver(vsp1_platform_driver);
889 MODULE_ALIAS("vsp1");
890 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
891 MODULE_DESCRIPTION("Renesas VSP1 Driver");
892 MODULE_LICENSE("GPL");