2 * vsp1_drv.c -- R-Car VSP1 Driver
4 * Copyright (C) 2013 Renesas 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>
19 #include <linux/platform_device.h>
20 #include <linux/videodev2.h>
24 #include "vsp1_rwpf.h"
27 /* -----------------------------------------------------------------------------
31 static irqreturn_t vsp1_irq_handler(int irq, void *data)
33 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
34 struct vsp1_device *vsp1 = data;
35 irqreturn_t ret = IRQ_NONE;
38 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
39 struct vsp1_rwpf *wpf = vsp1->wpf[i];
40 struct vsp1_pipeline *pipe;
46 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
47 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
48 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
50 if (status & VI6_WFP_IRQ_STA_FRE) {
51 vsp1_pipeline_frame_end(pipe);
59 /* -----------------------------------------------------------------------------
64 * vsp1_create_links - Create links from all sources to the given sink
66 * This function creates media links from all valid sources to the given sink
67 * pad. Links that would be invalid according to the VSP1 hardware capabilities
68 * are skipped. Those include all links
70 * - from a UDS to a UDS (UDS entities can't be chained)
71 * - from an entity to itself (no loops are allowed)
73 static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
75 struct media_entity *entity = &sink->subdev.entity;
76 struct vsp1_entity *source;
80 list_for_each_entry(source, &vsp1->entities, list_dev) {
83 if (source->type == sink->type)
86 if (source->type == VSP1_ENTITY_LIF ||
87 source->type == VSP1_ENTITY_WPF)
90 flags = source->type == VSP1_ENTITY_RPF &&
91 sink->type == VSP1_ENTITY_WPF &&
92 source->index == sink->index
93 ? MEDIA_LNK_FL_ENABLED : 0;
95 for (pad = 0; pad < entity->num_pads; ++pad) {
96 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
99 ret = media_entity_create_link(&source->subdev.entity,
105 if (flags & MEDIA_LNK_FL_ENABLED)
106 source->sink = entity;
113 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
115 struct vsp1_entity *entity;
116 struct vsp1_entity *next;
118 list_for_each_entry_safe(entity, next, &vsp1->entities, list_dev) {
119 list_del(&entity->list_dev);
120 vsp1_entity_destroy(entity);
123 v4l2_device_unregister(&vsp1->v4l2_dev);
124 media_device_unregister(&vsp1->media_dev);
127 static int vsp1_create_entities(struct vsp1_device *vsp1)
129 struct media_device *mdev = &vsp1->media_dev;
130 struct v4l2_device *vdev = &vsp1->v4l2_dev;
131 struct vsp1_entity *entity;
135 mdev->dev = vsp1->dev;
136 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
137 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
138 dev_name(mdev->dev));
139 ret = media_device_register(mdev);
141 dev_err(vsp1->dev, "media device registration failed (%d)\n",
147 ret = v4l2_device_register(vsp1->dev, vdev);
149 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
154 /* Instantiate all the entities. */
155 if (vsp1->pdata->features & VSP1_HAS_LIF) {
156 vsp1->lif = vsp1_lif_create(vsp1);
157 if (IS_ERR(vsp1->lif)) {
158 ret = PTR_ERR(vsp1->lif);
162 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
165 for (i = 0; i < vsp1->pdata->rpf_count; ++i) {
166 struct vsp1_rwpf *rpf;
168 rpf = vsp1_rpf_create(vsp1, i);
175 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
178 for (i = 0; i < vsp1->pdata->uds_count; ++i) {
179 struct vsp1_uds *uds;
181 uds = vsp1_uds_create(vsp1, i);
188 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
191 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
192 struct vsp1_rwpf *wpf;
194 wpf = vsp1_wpf_create(vsp1, i);
201 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
205 list_for_each_entry(entity, &vsp1->entities, list_dev) {
206 if (entity->type == VSP1_ENTITY_LIF ||
207 entity->type == VSP1_ENTITY_RPF)
210 ret = vsp1_create_links(vsp1, entity);
215 if (vsp1->pdata->features & VSP1_HAS_LIF) {
216 ret = media_entity_create_link(
217 &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
218 &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
223 /* Register all subdevs. */
224 list_for_each_entry(entity, &vsp1->entities, list_dev) {
225 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
231 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
235 vsp1_destroy_entities(vsp1);
240 static int vsp1_device_init(struct vsp1_device *vsp1)
245 /* Reset any channel that might be running. */
246 status = vsp1_read(vsp1, VI6_STATUS);
248 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
249 unsigned int timeout;
251 if (!(status & VI6_STATUS_SYS_ACT(i)))
254 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
255 for (timeout = 10; timeout > 0; --timeout) {
256 status = vsp1_read(vsp1, VI6_STATUS);
257 if (!(status & VI6_STATUS_SYS_ACT(i)))
260 usleep_range(1000, 2000);
264 dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
269 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
270 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
272 for (i = 0; i < vsp1->pdata->rpf_count; ++i)
273 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
275 for (i = 0; i < vsp1->pdata->uds_count; ++i)
276 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
278 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
279 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
280 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
281 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
282 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
283 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
285 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
286 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
287 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
288 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
293 static int vsp1_clocks_enable(struct vsp1_device *vsp1)
297 ret = clk_prepare_enable(vsp1->clock);
301 if (IS_ERR(vsp1->rt_clock))
304 ret = clk_prepare_enable(vsp1->rt_clock);
306 clk_disable_unprepare(vsp1->clock);
313 static void vsp1_clocks_disable(struct vsp1_device *vsp1)
315 if (!IS_ERR(vsp1->rt_clock))
316 clk_disable_unprepare(vsp1->rt_clock);
317 clk_disable_unprepare(vsp1->clock);
321 * vsp1_device_get - Acquire the VSP1 device
323 * Increment the VSP1 reference count and initialize the device if the first
324 * reference is taken.
326 * Return a pointer to the VSP1 device or NULL if an error occured.
328 struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
330 struct vsp1_device *__vsp1 = vsp1;
333 mutex_lock(&vsp1->lock);
334 if (vsp1->ref_count > 0)
337 ret = vsp1_clocks_enable(vsp1);
343 ret = vsp1_device_init(vsp1);
345 vsp1_clocks_disable(vsp1);
354 mutex_unlock(&vsp1->lock);
359 * vsp1_device_put - Release the VSP1 device
361 * Decrement the VSP1 reference count and cleanup the device if the last
362 * reference is released.
364 void vsp1_device_put(struct vsp1_device *vsp1)
366 mutex_lock(&vsp1->lock);
368 if (--vsp1->ref_count == 0)
369 vsp1_clocks_disable(vsp1);
371 mutex_unlock(&vsp1->lock);
374 /* -----------------------------------------------------------------------------
378 #ifdef CONFIG_PM_SLEEP
379 static int vsp1_pm_suspend(struct device *dev)
381 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
383 WARN_ON(mutex_is_locked(&vsp1->lock));
385 if (vsp1->ref_count == 0)
388 vsp1_clocks_disable(vsp1);
392 static int vsp1_pm_resume(struct device *dev)
394 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
396 WARN_ON(mutex_is_locked(&vsp1->lock));
401 return vsp1_clocks_enable(vsp1);
405 static const struct dev_pm_ops vsp1_pm_ops = {
406 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
409 /* -----------------------------------------------------------------------------
413 static struct vsp1_platform_data *
414 vsp1_get_platform_data(struct platform_device *pdev)
416 struct vsp1_platform_data *pdata = pdev->dev.platform_data;
419 dev_err(&pdev->dev, "missing platform data\n");
423 if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
424 dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
429 if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
430 dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
435 if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
436 dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
444 static int vsp1_probe(struct platform_device *pdev)
446 struct vsp1_device *vsp1;
447 struct resource *irq;
451 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
455 vsp1->dev = &pdev->dev;
456 mutex_init(&vsp1->lock);
457 INIT_LIST_HEAD(&vsp1->entities);
459 vsp1->pdata = vsp1_get_platform_data(pdev);
460 if (vsp1->pdata == NULL)
463 /* I/O, IRQ and clock resources */
464 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
465 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
466 if (IS_ERR(vsp1->mmio))
467 return PTR_ERR(vsp1->mmio);
469 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
470 if (IS_ERR(vsp1->clock)) {
471 dev_err(&pdev->dev, "failed to get clock\n");
472 return PTR_ERR(vsp1->clock);
475 /* The RT clock is optional */
476 vsp1->rt_clock = devm_clk_get(&pdev->dev, "rt");
478 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
480 dev_err(&pdev->dev, "missing IRQ\n");
484 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
485 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
487 dev_err(&pdev->dev, "failed to request IRQ\n");
491 /* Instanciate entities */
492 ret = vsp1_create_entities(vsp1);
494 dev_err(&pdev->dev, "failed to create entities\n");
498 platform_set_drvdata(pdev, vsp1);
503 static int vsp1_remove(struct platform_device *pdev)
505 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
507 vsp1_destroy_entities(vsp1);
512 static struct platform_driver vsp1_platform_driver = {
514 .remove = vsp1_remove,
516 .owner = THIS_MODULE,
522 module_platform_driver(vsp1_platform_driver);
524 MODULE_ALIAS("vsp1");
525 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
526 MODULE_DESCRIPTION("Renesas VSP1 Driver");
527 MODULE_LICENSE("GPL");