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"
32 /* -----------------------------------------------------------------------------
36 static irqreturn_t vsp1_irq_handler(int irq, void *data)
38 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
39 struct vsp1_device *vsp1 = data;
40 irqreturn_t ret = IRQ_NONE;
43 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
44 struct vsp1_rwpf *wpf = vsp1->wpf[i];
45 struct vsp1_pipeline *pipe;
51 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
52 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
53 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
55 if (status & VI6_WFP_IRQ_STA_FRE) {
56 vsp1_pipeline_frame_end(pipe);
64 /* -----------------------------------------------------------------------------
69 * vsp1_create_links - Create links from all sources to the given sink
71 * This function creates media links from all valid sources to the given sink
72 * pad. Links that would be invalid according to the VSP1 hardware capabilities
73 * are skipped. Those include all links
75 * - from a UDS to a UDS (UDS entities can't be chained)
76 * - from an entity to itself (no loops are allowed)
78 static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
80 struct media_entity *entity = &sink->subdev.entity;
81 struct vsp1_entity *source;
85 list_for_each_entry(source, &vsp1->entities, list_dev) {
88 if (source->type == sink->type)
91 if (source->type == VSP1_ENTITY_LIF ||
92 source->type == VSP1_ENTITY_WPF)
95 flags = source->type == VSP1_ENTITY_RPF &&
96 sink->type == VSP1_ENTITY_WPF &&
97 source->index == sink->index
98 ? MEDIA_LNK_FL_ENABLED : 0;
100 for (pad = 0; pad < entity->num_pads; ++pad) {
101 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
104 ret = media_create_pad_link(&source->subdev.entity,
110 if (flags & MEDIA_LNK_FL_ENABLED)
111 source->sink = entity;
118 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
120 struct vsp1_entity *entity;
121 struct vsp1_entity *next;
123 list_for_each_entry_safe(entity, next, &vsp1->entities, list_dev) {
124 list_del(&entity->list_dev);
125 vsp1_entity_destroy(entity);
128 v4l2_device_unregister(&vsp1->v4l2_dev);
129 media_device_unregister(&vsp1->media_dev);
130 media_device_cleanup(&vsp1->media_dev);
133 static int vsp1_create_entities(struct vsp1_device *vsp1)
135 struct media_device *mdev = &vsp1->media_dev;
136 struct v4l2_device *vdev = &vsp1->v4l2_dev;
137 struct vsp1_entity *entity;
141 mdev->dev = vsp1->dev;
142 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
143 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
144 dev_name(mdev->dev));
145 media_device_init(mdev);
148 ret = v4l2_device_register(vsp1->dev, vdev);
150 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
155 /* Instantiate all the entities. */
156 vsp1->bru = vsp1_bru_create(vsp1);
157 if (IS_ERR(vsp1->bru)) {
158 ret = PTR_ERR(vsp1->bru);
162 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
164 vsp1->hsi = vsp1_hsit_create(vsp1, true);
165 if (IS_ERR(vsp1->hsi)) {
166 ret = PTR_ERR(vsp1->hsi);
170 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
172 vsp1->hst = vsp1_hsit_create(vsp1, false);
173 if (IS_ERR(vsp1->hst)) {
174 ret = PTR_ERR(vsp1->hst);
178 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
180 if (vsp1->pdata.features & VSP1_HAS_LIF) {
181 vsp1->lif = vsp1_lif_create(vsp1);
182 if (IS_ERR(vsp1->lif)) {
183 ret = PTR_ERR(vsp1->lif);
187 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
190 if (vsp1->pdata.features & VSP1_HAS_LUT) {
191 vsp1->lut = vsp1_lut_create(vsp1);
192 if (IS_ERR(vsp1->lut)) {
193 ret = PTR_ERR(vsp1->lut);
197 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
200 for (i = 0; i < vsp1->pdata.rpf_count; ++i) {
201 struct vsp1_rwpf *rpf;
203 rpf = vsp1_rpf_create(vsp1, i);
210 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
213 if (vsp1->pdata.features & VSP1_HAS_SRU) {
214 vsp1->sru = vsp1_sru_create(vsp1);
215 if (IS_ERR(vsp1->sru)) {
216 ret = PTR_ERR(vsp1->sru);
220 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
223 for (i = 0; i < vsp1->pdata.uds_count; ++i) {
224 struct vsp1_uds *uds;
226 uds = vsp1_uds_create(vsp1, i);
233 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
236 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
237 struct vsp1_rwpf *wpf;
239 wpf = vsp1_wpf_create(vsp1, i);
246 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
249 /* Register all subdevs. */
250 list_for_each_entry(entity, &vsp1->entities, list_dev) {
251 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
258 list_for_each_entry(entity, &vsp1->entities, list_dev) {
259 if (entity->type == VSP1_ENTITY_LIF) {
260 ret = vsp1_wpf_create_links(vsp1, entity);
263 } else if (entity->type == VSP1_ENTITY_RPF) {
264 ret = vsp1_rpf_create_links(vsp1, entity);
268 ret = vsp1_create_links(vsp1, entity);
274 if (vsp1->pdata.features & VSP1_HAS_LIF) {
275 ret = media_create_pad_link(
276 &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
277 &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
282 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
286 ret = media_device_register(mdev);
290 vsp1_destroy_entities(vsp1);
295 static int vsp1_device_init(struct vsp1_device *vsp1)
300 /* Reset any channel that might be running. */
301 status = vsp1_read(vsp1, VI6_STATUS);
303 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
304 unsigned int timeout;
306 if (!(status & VI6_STATUS_SYS_ACT(i)))
309 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
310 for (timeout = 10; timeout > 0; --timeout) {
311 status = vsp1_read(vsp1, VI6_STATUS);
312 if (!(status & VI6_STATUS_SYS_ACT(i)))
315 usleep_range(1000, 2000);
319 dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
324 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
325 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
327 for (i = 0; i < vsp1->pdata.rpf_count; ++i)
328 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
330 for (i = 0; i < vsp1->pdata.uds_count; ++i)
331 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
333 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
334 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
335 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
336 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
337 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
338 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
340 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
341 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
342 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
343 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
349 * vsp1_device_get - Acquire the VSP1 device
351 * Increment the VSP1 reference count and initialize the device if the first
352 * reference is taken.
354 * Return 0 on success or a negative error code otherwise.
356 int vsp1_device_get(struct vsp1_device *vsp1)
360 mutex_lock(&vsp1->lock);
361 if (vsp1->ref_count > 0)
364 ret = clk_prepare_enable(vsp1->clock);
368 ret = vsp1_device_init(vsp1);
370 clk_disable_unprepare(vsp1->clock);
378 mutex_unlock(&vsp1->lock);
383 * vsp1_device_put - Release the VSP1 device
385 * Decrement the VSP1 reference count and cleanup the device if the last
386 * reference is released.
388 void vsp1_device_put(struct vsp1_device *vsp1)
390 mutex_lock(&vsp1->lock);
392 if (--vsp1->ref_count == 0)
393 clk_disable_unprepare(vsp1->clock);
395 mutex_unlock(&vsp1->lock);
398 /* -----------------------------------------------------------------------------
402 #ifdef CONFIG_PM_SLEEP
403 static int vsp1_pm_suspend(struct device *dev)
405 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
407 WARN_ON(mutex_is_locked(&vsp1->lock));
409 if (vsp1->ref_count == 0)
412 vsp1_pipelines_suspend(vsp1);
414 clk_disable_unprepare(vsp1->clock);
419 static int vsp1_pm_resume(struct device *dev)
421 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
423 WARN_ON(mutex_is_locked(&vsp1->lock));
425 if (vsp1->ref_count == 0)
428 clk_prepare_enable(vsp1->clock);
430 vsp1_pipelines_resume(vsp1);
436 static const struct dev_pm_ops vsp1_pm_ops = {
437 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
440 /* -----------------------------------------------------------------------------
444 static int vsp1_parse_dt(struct vsp1_device *vsp1)
446 struct device_node *np = vsp1->dev->of_node;
447 struct vsp1_platform_data *pdata = &vsp1->pdata;
449 if (of_property_read_bool(np, "renesas,has-lif"))
450 pdata->features |= VSP1_HAS_LIF;
451 if (of_property_read_bool(np, "renesas,has-lut"))
452 pdata->features |= VSP1_HAS_LUT;
453 if (of_property_read_bool(np, "renesas,has-sru"))
454 pdata->features |= VSP1_HAS_SRU;
456 of_property_read_u32(np, "renesas,#rpf", &pdata->rpf_count);
457 of_property_read_u32(np, "renesas,#uds", &pdata->uds_count);
458 of_property_read_u32(np, "renesas,#wpf", &pdata->wpf_count);
460 if (pdata->rpf_count <= 0 || pdata->rpf_count > VSP1_MAX_RPF) {
461 dev_err(vsp1->dev, "invalid number of RPF (%u)\n",
466 if (pdata->uds_count <= 0 || pdata->uds_count > VSP1_MAX_UDS) {
467 dev_err(vsp1->dev, "invalid number of UDS (%u)\n",
472 if (pdata->wpf_count <= 0 || pdata->wpf_count > VSP1_MAX_WPF) {
473 dev_err(vsp1->dev, "invalid number of WPF (%u)\n",
481 static int vsp1_probe(struct platform_device *pdev)
483 struct vsp1_device *vsp1;
484 struct resource *irq;
488 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
492 vsp1->dev = &pdev->dev;
493 mutex_init(&vsp1->lock);
494 INIT_LIST_HEAD(&vsp1->entities);
496 ret = vsp1_parse_dt(vsp1);
500 /* I/O, IRQ and clock resources */
501 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
502 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
503 if (IS_ERR(vsp1->mmio))
504 return PTR_ERR(vsp1->mmio);
506 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
507 if (IS_ERR(vsp1->clock)) {
508 dev_err(&pdev->dev, "failed to get clock\n");
509 return PTR_ERR(vsp1->clock);
512 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
514 dev_err(&pdev->dev, "missing IRQ\n");
518 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
519 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
521 dev_err(&pdev->dev, "failed to request IRQ\n");
525 /* Instanciate entities */
526 ret = vsp1_create_entities(vsp1);
528 dev_err(&pdev->dev, "failed to create entities\n");
532 platform_set_drvdata(pdev, vsp1);
537 static int vsp1_remove(struct platform_device *pdev)
539 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
541 vsp1_destroy_entities(vsp1);
546 static const struct of_device_id vsp1_of_match[] = {
547 { .compatible = "renesas,vsp1" },
551 static struct platform_driver vsp1_platform_driver = {
553 .remove = vsp1_remove,
557 .of_match_table = vsp1_of_match,
561 module_platform_driver(vsp1_platform_driver);
563 MODULE_ALIAS("vsp1");
564 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
565 MODULE_DESCRIPTION("Renesas VSP1 Driver");
566 MODULE_LICENSE("GPL");