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/videodev2.h>
24 #include <media/v4l2-subdev.h>
30 #include "vsp1_hsit.h"
33 #include "vsp1_rwpf.h"
36 #include "vsp1_video.h"
38 /* -----------------------------------------------------------------------------
42 static irqreturn_t vsp1_irq_handler(int irq, void *data)
44 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
45 struct vsp1_device *vsp1 = data;
46 irqreturn_t ret = IRQ_NONE;
50 for (i = 0; i < vsp1->info->wpf_count; ++i) {
51 struct vsp1_rwpf *wpf = vsp1->wpf[i];
52 struct vsp1_pipeline *pipe;
57 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
58 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
59 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
61 if (status & VI6_WFP_IRQ_STA_FRE) {
62 vsp1_pipeline_frame_end(pipe);
67 status = vsp1_read(vsp1, VI6_DISP_IRQ_STA);
68 vsp1_write(vsp1, VI6_DISP_IRQ_STA, ~status & VI6_DISP_IRQ_STA_DST);
70 if (status & VI6_DISP_IRQ_STA_DST) {
71 struct vsp1_rwpf *wpf = vsp1->wpf[0];
72 struct vsp1_pipeline *pipe;
75 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
76 vsp1_pipeline_display_start(pipe);
85 /* -----------------------------------------------------------------------------
90 * vsp1_create_sink_links - Create links from all sources to the given sink
92 * This function creates media links from all valid sources to the given sink
93 * pad. Links that would be invalid according to the VSP1 hardware capabilities
94 * are skipped. Those include all links
96 * - from a UDS to a UDS (UDS entities can't be chained)
97 * - from an entity to itself (no loops are allowed)
99 static int vsp1_create_sink_links(struct vsp1_device *vsp1,
100 struct vsp1_entity *sink)
102 struct media_entity *entity = &sink->subdev.entity;
103 struct vsp1_entity *source;
107 list_for_each_entry(source, &vsp1->entities, list_dev) {
110 if (source->type == sink->type)
113 if (source->type == VSP1_ENTITY_LIF ||
114 source->type == VSP1_ENTITY_WPF)
117 flags = source->type == VSP1_ENTITY_RPF &&
118 sink->type == VSP1_ENTITY_WPF &&
119 source->index == sink->index
120 ? MEDIA_LNK_FL_ENABLED : 0;
122 for (pad = 0; pad < entity->num_pads; ++pad) {
123 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
126 ret = media_create_pad_link(&source->subdev.entity,
132 if (flags & MEDIA_LNK_FL_ENABLED)
133 source->sink = entity;
140 static int vsp1_uapi_create_links(struct vsp1_device *vsp1)
142 struct vsp1_entity *entity;
146 list_for_each_entry(entity, &vsp1->entities, list_dev) {
147 if (entity->type == VSP1_ENTITY_LIF ||
148 entity->type == VSP1_ENTITY_RPF)
151 ret = vsp1_create_sink_links(vsp1, entity);
156 if (vsp1->info->features & VSP1_HAS_LIF) {
157 ret = media_create_pad_link(&vsp1->wpf[0]->entity.subdev.entity,
159 &vsp1->lif->entity.subdev.entity,
165 for (i = 0; i < vsp1->info->rpf_count; ++i) {
166 struct vsp1_rwpf *rpf = vsp1->rpf[i];
168 ret = media_create_pad_link(&rpf->video->video.entity, 0,
169 &rpf->entity.subdev.entity,
171 MEDIA_LNK_FL_ENABLED |
172 MEDIA_LNK_FL_IMMUTABLE);
177 for (i = 0; i < vsp1->info->wpf_count; ++i) {
178 /* Connect the video device to the WPF. All connections are
179 * immutable except for the WPF0 source link if a LIF is
182 struct vsp1_rwpf *wpf = vsp1->wpf[i];
183 unsigned int flags = MEDIA_LNK_FL_ENABLED;
185 if (!(vsp1->info->features & VSP1_HAS_LIF) || i != 0)
186 flags |= MEDIA_LNK_FL_IMMUTABLE;
188 ret = media_create_pad_link(&wpf->entity.subdev.entity,
190 &wpf->video->video.entity, 0,
199 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
201 struct vsp1_entity *entity, *_entity;
202 struct vsp1_video *video, *_video;
204 list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
205 list_del(&entity->list_dev);
206 vsp1_entity_destroy(entity);
209 list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
210 list_del(&video->list);
211 vsp1_video_cleanup(video);
214 v4l2_device_unregister(&vsp1->v4l2_dev);
215 media_device_unregister(&vsp1->media_dev);
216 media_device_cleanup(&vsp1->media_dev);
218 if (!vsp1->info->uapi)
219 vsp1_drm_cleanup(vsp1);
222 static int vsp1_create_entities(struct vsp1_device *vsp1)
224 struct media_device *mdev = &vsp1->media_dev;
225 struct v4l2_device *vdev = &vsp1->v4l2_dev;
226 struct vsp1_entity *entity;
230 mdev->dev = vsp1->dev;
231 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
232 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
233 dev_name(mdev->dev));
234 media_device_init(mdev);
236 vsp1->media_ops.link_setup = vsp1_entity_link_setup;
237 /* Don't perform link validation when the userspace API is disabled as
238 * the pipeline is configured internally by the driver in that case, and
239 * its configuration can thus be trusted.
241 if (vsp1->info->uapi)
242 vsp1->media_ops.link_validate = v4l2_subdev_link_validate;
245 ret = v4l2_device_register(vsp1->dev, vdev);
247 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
252 /* Instantiate all the entities. */
253 if (vsp1->info->features & VSP1_HAS_BRU) {
254 vsp1->bru = vsp1_bru_create(vsp1);
255 if (IS_ERR(vsp1->bru)) {
256 ret = PTR_ERR(vsp1->bru);
260 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
263 vsp1->hsi = vsp1_hsit_create(vsp1, true);
264 if (IS_ERR(vsp1->hsi)) {
265 ret = PTR_ERR(vsp1->hsi);
269 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
271 vsp1->hst = vsp1_hsit_create(vsp1, false);
272 if (IS_ERR(vsp1->hst)) {
273 ret = PTR_ERR(vsp1->hst);
277 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
279 if (vsp1->info->features & VSP1_HAS_LIF) {
280 vsp1->lif = vsp1_lif_create(vsp1);
281 if (IS_ERR(vsp1->lif)) {
282 ret = PTR_ERR(vsp1->lif);
286 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
289 if (vsp1->info->features & VSP1_HAS_LUT) {
290 vsp1->lut = vsp1_lut_create(vsp1);
291 if (IS_ERR(vsp1->lut)) {
292 ret = PTR_ERR(vsp1->lut);
296 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
299 for (i = 0; i < vsp1->info->rpf_count; ++i) {
300 struct vsp1_rwpf *rpf;
302 rpf = vsp1_rpf_create(vsp1, i);
309 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
311 if (vsp1->info->uapi) {
312 struct vsp1_video *video = vsp1_video_create(vsp1, rpf);
315 ret = PTR_ERR(video);
319 list_add_tail(&video->list, &vsp1->videos);
323 if (vsp1->info->features & VSP1_HAS_SRU) {
324 vsp1->sru = vsp1_sru_create(vsp1);
325 if (IS_ERR(vsp1->sru)) {
326 ret = PTR_ERR(vsp1->sru);
330 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
333 for (i = 0; i < vsp1->info->uds_count; ++i) {
334 struct vsp1_uds *uds;
336 uds = vsp1_uds_create(vsp1, i);
343 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
346 for (i = 0; i < vsp1->info->wpf_count; ++i) {
347 struct vsp1_rwpf *wpf;
349 wpf = vsp1_wpf_create(vsp1, i);
356 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
358 if (vsp1->info->uapi) {
359 struct vsp1_video *video = vsp1_video_create(vsp1, wpf);
362 ret = PTR_ERR(video);
366 list_add_tail(&video->list, &vsp1->videos);
367 wpf->entity.sink = &video->video.entity;
371 /* Register all subdevs. */
372 list_for_each_entry(entity, &vsp1->entities, list_dev) {
373 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
380 if (vsp1->info->uapi)
381 ret = vsp1_uapi_create_links(vsp1);
383 ret = vsp1_drm_create_links(vsp1);
387 /* Register subdev nodes if the userspace API is enabled or initialize
388 * the DRM pipeline otherwise.
390 if (vsp1->info->uapi)
391 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
393 ret = vsp1_drm_init(vsp1);
397 ret = media_device_register(mdev);
401 vsp1_destroy_entities(vsp1);
406 int vsp1_reset_wpf(struct vsp1_device *vsp1, unsigned int index)
408 unsigned int timeout;
411 status = vsp1_read(vsp1, VI6_STATUS);
412 if (!(status & VI6_STATUS_SYS_ACT(index)))
415 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(index));
416 for (timeout = 10; timeout > 0; --timeout) {
417 status = vsp1_read(vsp1, VI6_STATUS);
418 if (!(status & VI6_STATUS_SYS_ACT(index)))
421 usleep_range(1000, 2000);
425 dev_err(vsp1->dev, "failed to reset wpf.%u\n", index);
432 static int vsp1_device_init(struct vsp1_device *vsp1)
437 /* Reset any channel that might be running. */
438 for (i = 0; i < vsp1->info->wpf_count; ++i) {
439 ret = vsp1_reset_wpf(vsp1, i);
444 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
445 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
447 for (i = 0; i < vsp1->info->rpf_count; ++i)
448 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
450 for (i = 0; i < vsp1->info->uds_count; ++i)
451 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
453 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
454 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
455 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
456 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
457 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
458 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
460 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
461 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
462 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
463 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
465 if (!vsp1->info->uapi)
472 * vsp1_device_get - Acquire the VSP1 device
474 * Increment the VSP1 reference count and initialize the device if the first
475 * reference is taken.
477 * Return 0 on success or a negative error code otherwise.
479 int vsp1_device_get(struct vsp1_device *vsp1)
483 mutex_lock(&vsp1->lock);
484 if (vsp1->ref_count > 0)
487 ret = clk_prepare_enable(vsp1->clock);
491 ret = vsp1_device_init(vsp1);
493 clk_disable_unprepare(vsp1->clock);
501 mutex_unlock(&vsp1->lock);
506 * vsp1_device_put - Release the VSP1 device
508 * Decrement the VSP1 reference count and cleanup the device if the last
509 * reference is released.
511 void vsp1_device_put(struct vsp1_device *vsp1)
513 mutex_lock(&vsp1->lock);
515 if (--vsp1->ref_count == 0)
516 clk_disable_unprepare(vsp1->clock);
518 mutex_unlock(&vsp1->lock);
521 /* -----------------------------------------------------------------------------
525 #ifdef CONFIG_PM_SLEEP
526 static int vsp1_pm_suspend(struct device *dev)
528 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
530 WARN_ON(mutex_is_locked(&vsp1->lock));
532 if (vsp1->ref_count == 0)
535 vsp1_pipelines_suspend(vsp1);
537 clk_disable_unprepare(vsp1->clock);
542 static int vsp1_pm_resume(struct device *dev)
544 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
546 WARN_ON(mutex_is_locked(&vsp1->lock));
548 if (vsp1->ref_count == 0)
551 clk_prepare_enable(vsp1->clock);
553 vsp1_pipelines_resume(vsp1);
559 static const struct dev_pm_ops vsp1_pm_ops = {
560 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
563 /* -----------------------------------------------------------------------------
567 static const struct vsp1_device_info vsp1_device_infos[] = {
569 .version = VI6_IP_VERSION_MODEL_VSPS_H2,
570 .features = VSP1_HAS_BRU | VSP1_HAS_LUT | VSP1_HAS_SRU,
577 .version = VI6_IP_VERSION_MODEL_VSPR_H2,
578 .features = VSP1_HAS_BRU | VSP1_HAS_SRU,
585 .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,
594 .features = VSP1_HAS_BRU | VSP1_HAS_LUT | VSP1_HAS_SRU,
601 .version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
602 .features = VSP1_HAS_LUT | VSP1_HAS_SRU,
608 .version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
609 .features = VSP1_HAS_BRU,
615 .version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
616 .features = VSP1_HAS_BRU | VSP1_HAS_LUT,
622 .version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
623 .features = VSP1_HAS_BRU | VSP1_HAS_LIF,
630 static int vsp1_probe(struct platform_device *pdev)
632 struct vsp1_device *vsp1;
633 struct resource *irq;
639 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
643 vsp1->dev = &pdev->dev;
644 mutex_init(&vsp1->lock);
645 INIT_LIST_HEAD(&vsp1->entities);
646 INIT_LIST_HEAD(&vsp1->videos);
648 /* I/O, IRQ and clock resources */
649 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
650 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
651 if (IS_ERR(vsp1->mmio))
652 return PTR_ERR(vsp1->mmio);
654 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
655 if (IS_ERR(vsp1->clock)) {
656 dev_err(&pdev->dev, "failed to get clock\n");
657 return PTR_ERR(vsp1->clock);
660 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
662 dev_err(&pdev->dev, "missing IRQ\n");
666 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
667 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
669 dev_err(&pdev->dev, "failed to request IRQ\n");
673 /* Configure device parameters based on the version register. */
674 ret = clk_prepare_enable(vsp1->clock);
678 version = vsp1_read(vsp1, VI6_IP_VERSION);
679 clk_disable_unprepare(vsp1->clock);
681 for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) {
682 if ((version & VI6_IP_VERSION_MODEL_MASK) ==
683 vsp1_device_infos[i].version) {
684 vsp1->info = &vsp1_device_infos[i];
690 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");
703 platform_set_drvdata(pdev, vsp1);
708 static int vsp1_remove(struct platform_device *pdev)
710 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
712 vsp1_destroy_entities(vsp1);
717 static const struct of_device_id vsp1_of_match[] = {
718 { .compatible = "renesas,vsp1" },
719 { .compatible = "renesas,vsp2" },
723 static struct platform_driver vsp1_platform_driver = {
725 .remove = vsp1_remove,
729 .of_match_table = vsp1_of_match,
733 module_platform_driver(vsp1_platform_driver);
735 MODULE_ALIAS("vsp1");
736 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
737 MODULE_DESCRIPTION("Renesas VSP1 Driver");
738 MODULE_LICENSE("GPL");