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_pipe.h"
34 #include "vsp1_rwpf.h"
37 #include "vsp1_video.h"
39 /* -----------------------------------------------------------------------------
43 static irqreturn_t vsp1_irq_handler(int irq, void *data)
45 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
46 struct vsp1_device *vsp1 = data;
47 irqreturn_t ret = IRQ_NONE;
51 for (i = 0; i < vsp1->info->wpf_count; ++i) {
52 struct vsp1_rwpf *wpf = vsp1->wpf[i];
57 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
58 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
60 if (status & VI6_WFP_IRQ_STA_FRE) {
61 vsp1_pipeline_frame_end(wpf->pipe);
66 status = vsp1_read(vsp1, VI6_DISP_IRQ_STA);
67 vsp1_write(vsp1, VI6_DISP_IRQ_STA, ~status & VI6_DISP_IRQ_STA_DST);
69 if (status & VI6_DISP_IRQ_STA_DST) {
70 vsp1_drm_display_start(vsp1);
77 /* -----------------------------------------------------------------------------
82 * vsp1_create_sink_links - Create links from all sources to the given sink
84 * This function creates media links from all valid sources to the given sink
85 * pad. Links that would be invalid according to the VSP1 hardware capabilities
86 * are skipped. Those include all links
88 * - from a UDS to a UDS (UDS entities can't be chained)
89 * - from an entity to itself (no loops are allowed)
91 static int vsp1_create_sink_links(struct vsp1_device *vsp1,
92 struct vsp1_entity *sink)
94 struct media_entity *entity = &sink->subdev.entity;
95 struct vsp1_entity *source;
99 list_for_each_entry(source, &vsp1->entities, list_dev) {
102 if (source->type == sink->type)
105 if (source->type == VSP1_ENTITY_LIF ||
106 source->type == VSP1_ENTITY_WPF)
109 flags = source->type == VSP1_ENTITY_RPF &&
110 sink->type == VSP1_ENTITY_WPF &&
111 source->index == sink->index
112 ? MEDIA_LNK_FL_ENABLED : 0;
114 for (pad = 0; pad < entity->num_pads; ++pad) {
115 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
118 ret = media_create_pad_link(&source->subdev.entity,
124 if (flags & MEDIA_LNK_FL_ENABLED)
125 source->sink = entity;
132 static int vsp1_uapi_create_links(struct vsp1_device *vsp1)
134 struct vsp1_entity *entity;
138 list_for_each_entry(entity, &vsp1->entities, list_dev) {
139 if (entity->type == VSP1_ENTITY_LIF ||
140 entity->type == VSP1_ENTITY_RPF)
143 ret = vsp1_create_sink_links(vsp1, entity);
148 if (vsp1->info->features & VSP1_HAS_LIF) {
149 ret = media_create_pad_link(&vsp1->wpf[0]->entity.subdev.entity,
151 &vsp1->lif->entity.subdev.entity,
157 for (i = 0; i < vsp1->info->rpf_count; ++i) {
158 struct vsp1_rwpf *rpf = vsp1->rpf[i];
160 ret = media_create_pad_link(&rpf->video->video.entity, 0,
161 &rpf->entity.subdev.entity,
163 MEDIA_LNK_FL_ENABLED |
164 MEDIA_LNK_FL_IMMUTABLE);
169 for (i = 0; i < vsp1->info->wpf_count; ++i) {
170 /* Connect the video device to the WPF. All connections are
171 * immutable except for the WPF0 source link if a LIF is
174 struct vsp1_rwpf *wpf = vsp1->wpf[i];
175 unsigned int flags = MEDIA_LNK_FL_ENABLED;
177 if (!(vsp1->info->features & VSP1_HAS_LIF) || i != 0)
178 flags |= MEDIA_LNK_FL_IMMUTABLE;
180 ret = media_create_pad_link(&wpf->entity.subdev.entity,
182 &wpf->video->video.entity, 0,
191 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
193 struct vsp1_entity *entity, *_entity;
194 struct vsp1_video *video, *_video;
196 list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
197 list_del(&entity->list_dev);
198 vsp1_entity_destroy(entity);
201 list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
202 list_del(&video->list);
203 vsp1_video_cleanup(video);
206 v4l2_device_unregister(&vsp1->v4l2_dev);
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 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
224 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
225 dev_name(mdev->dev));
226 media_device_init(mdev);
228 vsp1->media_ops.link_setup = vsp1_entity_link_setup;
229 /* Don't perform link validation when the userspace API is disabled as
230 * the pipeline is configured internally by the driver in that case, and
231 * its configuration can thus be trusted.
233 if (vsp1->info->uapi)
234 vsp1->media_ops.link_validate = v4l2_subdev_link_validate;
237 ret = v4l2_device_register(vsp1->dev, vdev);
239 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
244 /* Instantiate all the entities. */
245 if (vsp1->info->features & VSP1_HAS_BRU) {
246 vsp1->bru = vsp1_bru_create(vsp1);
247 if (IS_ERR(vsp1->bru)) {
248 ret = PTR_ERR(vsp1->bru);
252 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
255 vsp1->hsi = vsp1_hsit_create(vsp1, true);
256 if (IS_ERR(vsp1->hsi)) {
257 ret = PTR_ERR(vsp1->hsi);
261 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
263 vsp1->hst = vsp1_hsit_create(vsp1, false);
264 if (IS_ERR(vsp1->hst)) {
265 ret = PTR_ERR(vsp1->hst);
269 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
271 if (vsp1->info->features & VSP1_HAS_LIF) {
272 vsp1->lif = vsp1_lif_create(vsp1);
273 if (IS_ERR(vsp1->lif)) {
274 ret = PTR_ERR(vsp1->lif);
278 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
281 if (vsp1->info->features & VSP1_HAS_LUT) {
282 vsp1->lut = vsp1_lut_create(vsp1);
283 if (IS_ERR(vsp1->lut)) {
284 ret = PTR_ERR(vsp1->lut);
288 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
291 for (i = 0; i < vsp1->info->rpf_count; ++i) {
292 struct vsp1_rwpf *rpf;
294 rpf = vsp1_rpf_create(vsp1, i);
301 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
303 if (vsp1->info->uapi) {
304 struct vsp1_video *video = vsp1_video_create(vsp1, rpf);
307 ret = PTR_ERR(video);
311 list_add_tail(&video->list, &vsp1->videos);
315 if (vsp1->info->features & VSP1_HAS_SRU) {
316 vsp1->sru = vsp1_sru_create(vsp1);
317 if (IS_ERR(vsp1->sru)) {
318 ret = PTR_ERR(vsp1->sru);
322 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
325 for (i = 0; i < vsp1->info->uds_count; ++i) {
326 struct vsp1_uds *uds;
328 uds = vsp1_uds_create(vsp1, i);
335 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
338 for (i = 0; i < vsp1->info->wpf_count; ++i) {
339 struct vsp1_rwpf *wpf;
341 wpf = vsp1_wpf_create(vsp1, i);
348 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
350 if (vsp1->info->uapi) {
351 struct vsp1_video *video = vsp1_video_create(vsp1, wpf);
354 ret = PTR_ERR(video);
358 list_add_tail(&video->list, &vsp1->videos);
359 wpf->entity.sink = &video->video.entity;
363 /* Register all subdevs. */
364 list_for_each_entry(entity, &vsp1->entities, list_dev) {
365 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
372 if (vsp1->info->uapi)
373 ret = vsp1_uapi_create_links(vsp1);
375 ret = vsp1_drm_create_links(vsp1);
379 /* Register subdev nodes if the userspace API is enabled or initialize
380 * the DRM pipeline otherwise.
382 if (vsp1->info->uapi)
383 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
385 ret = vsp1_drm_init(vsp1);
389 ret = media_device_register(mdev);
393 vsp1_destroy_entities(vsp1);
398 int vsp1_reset_wpf(struct vsp1_device *vsp1, unsigned int index)
400 unsigned int timeout;
403 status = vsp1_read(vsp1, VI6_STATUS);
404 if (!(status & VI6_STATUS_SYS_ACT(index)))
407 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(index));
408 for (timeout = 10; timeout > 0; --timeout) {
409 status = vsp1_read(vsp1, VI6_STATUS);
410 if (!(status & VI6_STATUS_SYS_ACT(index)))
413 usleep_range(1000, 2000);
417 dev_err(vsp1->dev, "failed to reset wpf.%u\n", index);
424 static int vsp1_device_init(struct vsp1_device *vsp1)
429 /* Reset any channel that might be running. */
430 for (i = 0; i < vsp1->info->wpf_count; ++i) {
431 ret = vsp1_reset_wpf(vsp1, i);
436 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
437 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
439 for (i = 0; i < vsp1->info->rpf_count; ++i)
440 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
442 for (i = 0; i < vsp1->info->uds_count; ++i)
443 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
445 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
446 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
447 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
448 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
449 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
450 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
452 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
453 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
454 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
455 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
457 vsp1_dlm_setup(vsp1);
463 * vsp1_device_get - Acquire the VSP1 device
465 * Increment the VSP1 reference count and initialize the device if the first
466 * reference is taken.
468 * Return 0 on success or a negative error code otherwise.
470 int vsp1_device_get(struct vsp1_device *vsp1)
474 mutex_lock(&vsp1->lock);
475 if (vsp1->ref_count > 0)
478 ret = clk_prepare_enable(vsp1->clock);
482 ret = vsp1_device_init(vsp1);
484 clk_disable_unprepare(vsp1->clock);
492 mutex_unlock(&vsp1->lock);
497 * vsp1_device_put - Release the VSP1 device
499 * Decrement the VSP1 reference count and cleanup the device if the last
500 * reference is released.
502 void vsp1_device_put(struct vsp1_device *vsp1)
504 mutex_lock(&vsp1->lock);
506 if (--vsp1->ref_count == 0)
507 clk_disable_unprepare(vsp1->clock);
509 mutex_unlock(&vsp1->lock);
512 /* -----------------------------------------------------------------------------
516 #ifdef CONFIG_PM_SLEEP
517 static int vsp1_pm_suspend(struct device *dev)
519 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
521 WARN_ON(mutex_is_locked(&vsp1->lock));
523 if (vsp1->ref_count == 0)
526 vsp1_pipelines_suspend(vsp1);
528 clk_disable_unprepare(vsp1->clock);
533 static int vsp1_pm_resume(struct device *dev)
535 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
537 WARN_ON(mutex_is_locked(&vsp1->lock));
539 if (vsp1->ref_count == 0)
542 clk_prepare_enable(vsp1->clock);
544 vsp1_pipelines_resume(vsp1);
550 static const struct dev_pm_ops vsp1_pm_ops = {
551 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
554 /* -----------------------------------------------------------------------------
558 static const struct vsp1_device_info vsp1_device_infos[] = {
560 .version = VI6_IP_VERSION_MODEL_VSPS_H2,
562 .features = VSP1_HAS_BRU | VSP1_HAS_LUT | VSP1_HAS_SRU,
569 .version = VI6_IP_VERSION_MODEL_VSPR_H2,
571 .features = VSP1_HAS_BRU | VSP1_HAS_SRU,
578 .version = VI6_IP_VERSION_MODEL_VSPD_GEN2,
580 .features = VSP1_HAS_BRU | VSP1_HAS_LIF | VSP1_HAS_LUT,
587 .version = VI6_IP_VERSION_MODEL_VSPS_M2,
589 .features = VSP1_HAS_BRU | VSP1_HAS_LUT | VSP1_HAS_SRU,
596 .version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
598 .features = VSP1_HAS_LUT | VSP1_HAS_SRU,
604 .version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
606 .features = VSP1_HAS_BRU,
612 .version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
614 .features = VSP1_HAS_BRU | VSP1_HAS_LUT,
620 .version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
622 .features = VSP1_HAS_BRU | VSP1_HAS_LIF,
629 static int vsp1_probe(struct platform_device *pdev)
631 struct vsp1_device *vsp1;
632 struct resource *irq;
638 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
642 vsp1->dev = &pdev->dev;
643 mutex_init(&vsp1->lock);
644 INIT_LIST_HEAD(&vsp1->entities);
645 INIT_LIST_HEAD(&vsp1->videos);
647 /* I/O, IRQ and clock resources */
648 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
649 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
650 if (IS_ERR(vsp1->mmio))
651 return PTR_ERR(vsp1->mmio);
653 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
654 if (IS_ERR(vsp1->clock)) {
655 dev_err(&pdev->dev, "failed to get clock\n");
656 return PTR_ERR(vsp1->clock);
659 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
661 dev_err(&pdev->dev, "missing IRQ\n");
665 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
666 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
668 dev_err(&pdev->dev, "failed to request IRQ\n");
672 /* Configure device parameters based on the version register. */
673 ret = clk_prepare_enable(vsp1->clock);
677 version = vsp1_read(vsp1, VI6_IP_VERSION);
678 clk_disable_unprepare(vsp1->clock);
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);
693 dev_dbg(&pdev->dev, "IP version 0x%08x\n", version);
695 /* Instanciate entities */
696 ret = vsp1_create_entities(vsp1);
698 dev_err(&pdev->dev, "failed to create entities\n");
702 platform_set_drvdata(pdev, vsp1);
707 static int vsp1_remove(struct platform_device *pdev)
709 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
711 vsp1_destroy_entities(vsp1);
716 static const struct of_device_id vsp1_of_match[] = {
717 { .compatible = "renesas,vsp1" },
718 { .compatible = "renesas,vsp2" },
722 static struct platform_driver vsp1_platform_driver = {
724 .remove = vsp1_remove,
728 .of_match_table = vsp1_of_match,
732 module_platform_driver(vsp1_platform_driver);
734 MODULE_ALIAS("vsp1");
735 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
736 MODULE_DESCRIPTION("Renesas VSP1 Driver");
737 MODULE_LICENSE("GPL");