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/platform_device.h>
21 #include <linux/videodev2.h>
25 #include "vsp1_hsit.h"
28 #include "vsp1_rwpf.h"
31 #include "vsp1_video.h"
33 /* -----------------------------------------------------------------------------
37 static irqreturn_t vsp1_irq_handler(int irq, void *data)
39 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
40 struct vsp1_device *vsp1 = data;
41 irqreturn_t ret = IRQ_NONE;
44 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
45 struct vsp1_rwpf *wpf = vsp1->wpf[i];
46 struct vsp1_pipeline *pipe;
52 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
53 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
54 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
56 if (status & VI6_WFP_IRQ_STA_FRE) {
57 vsp1_pipeline_frame_end(pipe);
65 /* -----------------------------------------------------------------------------
70 * vsp1_create_links - Create links from all sources to the given sink
72 * This function creates media links from all valid sources to the given sink
73 * pad. Links that would be invalid according to the VSP1 hardware capabilities
74 * are skipped. Those include all links
76 * - from a UDS to a UDS (UDS entities can't be chained)
77 * - from an entity to itself (no loops are allowed)
79 static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
81 struct media_entity *entity = &sink->subdev.entity;
82 struct vsp1_entity *source;
86 if (sink->type == VSP1_ENTITY_RPF) {
87 struct vsp1_rwpf *rpf = to_rwpf(&sink->subdev);
89 /* RPFs have no source entities, just connect their source pad
90 * to their video device.
92 return media_create_pad_link(&rpf->entity.video->video.entity,
93 0, &rpf->entity.subdev.entity,
95 MEDIA_LNK_FL_ENABLED |
96 MEDIA_LNK_FL_IMMUTABLE);
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;
129 if (sink->type == VSP1_ENTITY_WPF) {
130 struct vsp1_rwpf *wpf = to_rwpf(&sink->subdev);
131 unsigned int flags = MEDIA_LNK_FL_ENABLED;
133 /* Connect the video device to the WPF. All connections are
134 * immutable except for the WPF0 source link if a LIF is
137 if (!(vsp1->pdata.features & VSP1_HAS_LIF) || sink->index != 0)
138 flags |= MEDIA_LNK_FL_IMMUTABLE;
140 return media_create_pad_link(&wpf->entity.subdev.entity,
142 &wpf->entity.video->video.entity,
149 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
151 struct vsp1_entity *entity, *_entity;
152 struct vsp1_video *video, *_video;
154 list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
155 list_del(&entity->list_dev);
156 vsp1_entity_destroy(entity);
159 list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
160 list_del(&video->list);
161 vsp1_video_cleanup(video);
164 v4l2_device_unregister(&vsp1->v4l2_dev);
165 media_device_unregister(&vsp1->media_dev);
166 media_device_cleanup(&vsp1->media_dev);
169 static int vsp1_create_entities(struct vsp1_device *vsp1)
171 struct media_device *mdev = &vsp1->media_dev;
172 struct v4l2_device *vdev = &vsp1->v4l2_dev;
173 struct vsp1_entity *entity;
177 mdev->dev = vsp1->dev;
178 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
179 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
180 dev_name(mdev->dev));
181 media_device_init(mdev);
184 ret = v4l2_device_register(vsp1->dev, vdev);
186 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
191 /* Instantiate all the entities. */
192 vsp1->bru = vsp1_bru_create(vsp1);
193 if (IS_ERR(vsp1->bru)) {
194 ret = PTR_ERR(vsp1->bru);
198 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
200 vsp1->hsi = vsp1_hsit_create(vsp1, true);
201 if (IS_ERR(vsp1->hsi)) {
202 ret = PTR_ERR(vsp1->hsi);
206 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
208 vsp1->hst = vsp1_hsit_create(vsp1, false);
209 if (IS_ERR(vsp1->hst)) {
210 ret = PTR_ERR(vsp1->hst);
214 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
216 if (vsp1->pdata.features & VSP1_HAS_LIF) {
217 vsp1->lif = vsp1_lif_create(vsp1);
218 if (IS_ERR(vsp1->lif)) {
219 ret = PTR_ERR(vsp1->lif);
223 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
226 if (vsp1->pdata.features & VSP1_HAS_LUT) {
227 vsp1->lut = vsp1_lut_create(vsp1);
228 if (IS_ERR(vsp1->lut)) {
229 ret = PTR_ERR(vsp1->lut);
233 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
236 for (i = 0; i < vsp1->pdata.rpf_count; ++i) {
237 struct vsp1_video *video;
238 struct vsp1_rwpf *rpf;
240 rpf = vsp1_rpf_create(vsp1, i);
247 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
249 video = vsp1_video_create(vsp1, rpf);
251 ret = PTR_ERR(video);
255 list_add_tail(&video->list, &vsp1->videos);
258 if (vsp1->pdata.features & VSP1_HAS_SRU) {
259 vsp1->sru = vsp1_sru_create(vsp1);
260 if (IS_ERR(vsp1->sru)) {
261 ret = PTR_ERR(vsp1->sru);
265 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
268 for (i = 0; i < vsp1->pdata.uds_count; ++i) {
269 struct vsp1_uds *uds;
271 uds = vsp1_uds_create(vsp1, i);
278 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
281 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
282 struct vsp1_video *video;
283 struct vsp1_rwpf *wpf;
285 wpf = vsp1_wpf_create(vsp1, i);
292 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
294 video = vsp1_video_create(vsp1, wpf);
296 ret = PTR_ERR(video);
300 list_add_tail(&video->list, &vsp1->videos);
301 wpf->entity.sink = &video->video.entity;
304 /* Register all subdevs. */
305 list_for_each_entry(entity, &vsp1->entities, list_dev) {
306 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
313 list_for_each_entry(entity, &vsp1->entities, list_dev) {
314 if (entity->type == VSP1_ENTITY_LIF)
317 ret = vsp1_create_links(vsp1, entity);
322 if (vsp1->pdata.features & VSP1_HAS_LIF) {
323 ret = media_create_pad_link(
324 &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
325 &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
330 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
334 ret = media_device_register(mdev);
338 vsp1_destroy_entities(vsp1);
343 static int vsp1_device_init(struct vsp1_device *vsp1)
348 /* Reset any channel that might be running. */
349 status = vsp1_read(vsp1, VI6_STATUS);
351 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
352 unsigned int timeout;
354 if (!(status & VI6_STATUS_SYS_ACT(i)))
357 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
358 for (timeout = 10; timeout > 0; --timeout) {
359 status = vsp1_read(vsp1, VI6_STATUS);
360 if (!(status & VI6_STATUS_SYS_ACT(i)))
363 usleep_range(1000, 2000);
367 dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
372 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
373 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
375 for (i = 0; i < vsp1->pdata.rpf_count; ++i)
376 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
378 for (i = 0; i < vsp1->pdata.uds_count; ++i)
379 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
381 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
382 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
383 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
384 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
385 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
386 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
388 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
389 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
390 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
391 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
397 * vsp1_device_get - Acquire the VSP1 device
399 * Increment the VSP1 reference count and initialize the device if the first
400 * reference is taken.
402 * Return 0 on success or a negative error code otherwise.
404 int vsp1_device_get(struct vsp1_device *vsp1)
408 mutex_lock(&vsp1->lock);
409 if (vsp1->ref_count > 0)
412 ret = clk_prepare_enable(vsp1->clock);
416 ret = vsp1_device_init(vsp1);
418 clk_disable_unprepare(vsp1->clock);
426 mutex_unlock(&vsp1->lock);
431 * vsp1_device_put - Release the VSP1 device
433 * Decrement the VSP1 reference count and cleanup the device if the last
434 * reference is released.
436 void vsp1_device_put(struct vsp1_device *vsp1)
438 mutex_lock(&vsp1->lock);
440 if (--vsp1->ref_count == 0)
441 clk_disable_unprepare(vsp1->clock);
443 mutex_unlock(&vsp1->lock);
446 /* -----------------------------------------------------------------------------
450 #ifdef CONFIG_PM_SLEEP
451 static int vsp1_pm_suspend(struct device *dev)
453 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
455 WARN_ON(mutex_is_locked(&vsp1->lock));
457 if (vsp1->ref_count == 0)
460 vsp1_pipelines_suspend(vsp1);
462 clk_disable_unprepare(vsp1->clock);
467 static int vsp1_pm_resume(struct device *dev)
469 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
471 WARN_ON(mutex_is_locked(&vsp1->lock));
473 if (vsp1->ref_count == 0)
476 clk_prepare_enable(vsp1->clock);
478 vsp1_pipelines_resume(vsp1);
484 static const struct dev_pm_ops vsp1_pm_ops = {
485 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
488 /* -----------------------------------------------------------------------------
492 static int vsp1_parse_dt(struct vsp1_device *vsp1)
494 struct device_node *np = vsp1->dev->of_node;
495 struct vsp1_platform_data *pdata = &vsp1->pdata;
497 if (of_property_read_bool(np, "renesas,has-lif"))
498 pdata->features |= VSP1_HAS_LIF;
499 if (of_property_read_bool(np, "renesas,has-lut"))
500 pdata->features |= VSP1_HAS_LUT;
501 if (of_property_read_bool(np, "renesas,has-sru"))
502 pdata->features |= VSP1_HAS_SRU;
504 of_property_read_u32(np, "renesas,#rpf", &pdata->rpf_count);
505 of_property_read_u32(np, "renesas,#uds", &pdata->uds_count);
506 of_property_read_u32(np, "renesas,#wpf", &pdata->wpf_count);
508 if (pdata->rpf_count <= 0 || pdata->rpf_count > VSP1_MAX_RPF) {
509 dev_err(vsp1->dev, "invalid number of RPF (%u)\n",
514 if (pdata->uds_count > VSP1_MAX_UDS) {
515 dev_err(vsp1->dev, "invalid number of UDS (%u)\n",
520 if (pdata->wpf_count <= 0 || pdata->wpf_count > VSP1_MAX_WPF) {
521 dev_err(vsp1->dev, "invalid number of WPF (%u)\n",
529 static int vsp1_probe(struct platform_device *pdev)
531 struct vsp1_device *vsp1;
532 struct resource *irq;
536 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
540 vsp1->dev = &pdev->dev;
541 mutex_init(&vsp1->lock);
542 INIT_LIST_HEAD(&vsp1->entities);
543 INIT_LIST_HEAD(&vsp1->videos);
545 ret = vsp1_parse_dt(vsp1);
549 /* I/O, IRQ and clock resources */
550 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
551 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
552 if (IS_ERR(vsp1->mmio))
553 return PTR_ERR(vsp1->mmio);
555 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
556 if (IS_ERR(vsp1->clock)) {
557 dev_err(&pdev->dev, "failed to get clock\n");
558 return PTR_ERR(vsp1->clock);
561 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
563 dev_err(&pdev->dev, "missing IRQ\n");
567 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
568 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
570 dev_err(&pdev->dev, "failed to request IRQ\n");
574 /* Instanciate entities */
575 ret = vsp1_create_entities(vsp1);
577 dev_err(&pdev->dev, "failed to create entities\n");
581 platform_set_drvdata(pdev, vsp1);
586 static int vsp1_remove(struct platform_device *pdev)
588 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
590 vsp1_destroy_entities(vsp1);
595 static const struct of_device_id vsp1_of_match[] = {
596 { .compatible = "renesas,vsp1" },
600 static struct platform_driver vsp1_platform_driver = {
602 .remove = vsp1_remove,
606 .of_match_table = vsp1_of_match,
610 module_platform_driver(vsp1_platform_driver);
612 MODULE_ALIAS("vsp1");
613 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
614 MODULE_DESCRIPTION("Renesas VSP1 Driver");
615 MODULE_LICENSE("GPL");