2 * vsp1_drv.c -- R-Car VSP1 Driver
4 * Copyright (C) 2013-2014 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>
19 #include <linux/platform_device.h>
20 #include <linux/videodev2.h>
23 #include "vsp1_hsit.h"
26 #include "vsp1_rwpf.h"
30 /* -----------------------------------------------------------------------------
34 static irqreturn_t vsp1_irq_handler(int irq, void *data)
36 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
37 struct vsp1_device *vsp1 = data;
38 irqreturn_t ret = IRQ_NONE;
41 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
42 struct vsp1_rwpf *wpf = vsp1->wpf[i];
43 struct vsp1_pipeline *pipe;
49 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
50 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
51 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
53 if (status & VI6_WFP_IRQ_STA_FRE) {
54 vsp1_pipeline_frame_end(pipe);
62 /* -----------------------------------------------------------------------------
67 * vsp1_create_links - Create links from all sources to the given sink
69 * This function creates media links from all valid sources to the given sink
70 * pad. Links that would be invalid according to the VSP1 hardware capabilities
71 * are skipped. Those include all links
73 * - from a UDS to a UDS (UDS entities can't be chained)
74 * - from an entity to itself (no loops are allowed)
76 static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
78 struct media_entity *entity = &sink->subdev.entity;
79 struct vsp1_entity *source;
83 list_for_each_entry(source, &vsp1->entities, list_dev) {
86 if (source->type == sink->type)
89 if (source->type == VSP1_ENTITY_LIF ||
90 source->type == VSP1_ENTITY_WPF)
93 flags = source->type == VSP1_ENTITY_RPF &&
94 sink->type == VSP1_ENTITY_WPF &&
95 source->index == sink->index
96 ? MEDIA_LNK_FL_ENABLED : 0;
98 for (pad = 0; pad < entity->num_pads; ++pad) {
99 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
102 ret = media_entity_create_link(&source->subdev.entity,
108 if (flags & MEDIA_LNK_FL_ENABLED)
109 source->sink = entity;
116 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
118 struct vsp1_entity *entity;
119 struct vsp1_entity *next;
121 list_for_each_entry_safe(entity, next, &vsp1->entities, list_dev) {
122 list_del(&entity->list_dev);
123 vsp1_entity_destroy(entity);
126 v4l2_device_unregister(&vsp1->v4l2_dev);
127 media_device_unregister(&vsp1->media_dev);
130 static int vsp1_create_entities(struct vsp1_device *vsp1)
132 struct media_device *mdev = &vsp1->media_dev;
133 struct v4l2_device *vdev = &vsp1->v4l2_dev;
134 struct vsp1_entity *entity;
138 mdev->dev = vsp1->dev;
139 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
140 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
141 dev_name(mdev->dev));
142 ret = media_device_register(mdev);
144 dev_err(vsp1->dev, "media device registration failed (%d)\n",
150 ret = v4l2_device_register(vsp1->dev, vdev);
152 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
157 /* Instantiate all the entities. */
158 vsp1->hsi = vsp1_hsit_create(vsp1, true);
159 if (IS_ERR(vsp1->hsi)) {
160 ret = PTR_ERR(vsp1->hsi);
164 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
166 vsp1->hst = vsp1_hsit_create(vsp1, false);
167 if (IS_ERR(vsp1->hst)) {
168 ret = PTR_ERR(vsp1->hst);
172 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
174 if (vsp1->pdata->features & VSP1_HAS_LIF) {
175 vsp1->lif = vsp1_lif_create(vsp1);
176 if (IS_ERR(vsp1->lif)) {
177 ret = PTR_ERR(vsp1->lif);
181 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
184 if (vsp1->pdata->features & VSP1_HAS_LUT) {
185 vsp1->lut = vsp1_lut_create(vsp1);
186 if (IS_ERR(vsp1->lut)) {
187 ret = PTR_ERR(vsp1->lut);
191 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
194 for (i = 0; i < vsp1->pdata->rpf_count; ++i) {
195 struct vsp1_rwpf *rpf;
197 rpf = vsp1_rpf_create(vsp1, i);
204 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
207 if (vsp1->pdata->features & VSP1_HAS_SRU) {
208 vsp1->sru = vsp1_sru_create(vsp1);
209 if (IS_ERR(vsp1->sru)) {
210 ret = PTR_ERR(vsp1->sru);
214 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
217 for (i = 0; i < vsp1->pdata->uds_count; ++i) {
218 struct vsp1_uds *uds;
220 uds = vsp1_uds_create(vsp1, i);
227 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
230 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
231 struct vsp1_rwpf *wpf;
233 wpf = vsp1_wpf_create(vsp1, i);
240 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
244 list_for_each_entry(entity, &vsp1->entities, list_dev) {
245 if (entity->type == VSP1_ENTITY_LIF ||
246 entity->type == VSP1_ENTITY_RPF)
249 ret = vsp1_create_links(vsp1, entity);
254 if (vsp1->pdata->features & VSP1_HAS_LIF) {
255 ret = media_entity_create_link(
256 &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
257 &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
262 /* Register all subdevs. */
263 list_for_each_entry(entity, &vsp1->entities, list_dev) {
264 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
270 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
274 vsp1_destroy_entities(vsp1);
279 static int vsp1_device_init(struct vsp1_device *vsp1)
284 /* Reset any channel that might be running. */
285 status = vsp1_read(vsp1, VI6_STATUS);
287 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
288 unsigned int timeout;
290 if (!(status & VI6_STATUS_SYS_ACT(i)))
293 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
294 for (timeout = 10; timeout > 0; --timeout) {
295 status = vsp1_read(vsp1, VI6_STATUS);
296 if (!(status & VI6_STATUS_SYS_ACT(i)))
299 usleep_range(1000, 2000);
303 dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
308 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
309 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
311 for (i = 0; i < vsp1->pdata->rpf_count; ++i)
312 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
314 for (i = 0; i < vsp1->pdata->uds_count; ++i)
315 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
317 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
318 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
319 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
320 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
321 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
322 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
324 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
325 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
326 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
327 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
333 * vsp1_device_get - Acquire the VSP1 device
335 * Increment the VSP1 reference count and initialize the device if the first
336 * reference is taken.
338 * Return a pointer to the VSP1 device or NULL if an error occurred.
340 struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
342 struct vsp1_device *__vsp1 = vsp1;
345 mutex_lock(&vsp1->lock);
346 if (vsp1->ref_count > 0)
349 ret = clk_prepare_enable(vsp1->clock);
355 ret = vsp1_device_init(vsp1);
357 clk_disable_unprepare(vsp1->clock);
366 mutex_unlock(&vsp1->lock);
371 * vsp1_device_put - Release the VSP1 device
373 * Decrement the VSP1 reference count and cleanup the device if the last
374 * reference is released.
376 void vsp1_device_put(struct vsp1_device *vsp1)
378 mutex_lock(&vsp1->lock);
380 if (--vsp1->ref_count == 0)
381 clk_disable_unprepare(vsp1->clock);
383 mutex_unlock(&vsp1->lock);
386 /* -----------------------------------------------------------------------------
390 #ifdef CONFIG_PM_SLEEP
391 static int vsp1_pm_suspend(struct device *dev)
393 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
395 WARN_ON(mutex_is_locked(&vsp1->lock));
397 if (vsp1->ref_count == 0)
400 clk_disable_unprepare(vsp1->clock);
404 static int vsp1_pm_resume(struct device *dev)
406 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
408 WARN_ON(mutex_is_locked(&vsp1->lock));
413 return clk_prepare_enable(vsp1->clock);
417 static const struct dev_pm_ops vsp1_pm_ops = {
418 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
421 /* -----------------------------------------------------------------------------
425 static struct vsp1_platform_data *
426 vsp1_get_platform_data(struct platform_device *pdev)
428 struct vsp1_platform_data *pdata = pdev->dev.platform_data;
431 dev_err(&pdev->dev, "missing platform data\n");
435 if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
436 dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
441 if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
442 dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
447 if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
448 dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
456 static int vsp1_probe(struct platform_device *pdev)
458 struct vsp1_device *vsp1;
459 struct resource *irq;
463 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
467 vsp1->dev = &pdev->dev;
468 mutex_init(&vsp1->lock);
469 INIT_LIST_HEAD(&vsp1->entities);
471 vsp1->pdata = vsp1_get_platform_data(pdev);
472 if (vsp1->pdata == NULL)
475 /* I/O, IRQ and clock resources */
476 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
477 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
478 if (IS_ERR(vsp1->mmio))
479 return PTR_ERR(vsp1->mmio);
481 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
482 if (IS_ERR(vsp1->clock)) {
483 dev_err(&pdev->dev, "failed to get clock\n");
484 return PTR_ERR(vsp1->clock);
487 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
489 dev_err(&pdev->dev, "missing IRQ\n");
493 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
494 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
496 dev_err(&pdev->dev, "failed to request IRQ\n");
500 /* Instanciate entities */
501 ret = vsp1_create_entities(vsp1);
503 dev_err(&pdev->dev, "failed to create entities\n");
507 platform_set_drvdata(pdev, vsp1);
512 static int vsp1_remove(struct platform_device *pdev)
514 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
516 vsp1_destroy_entities(vsp1);
521 static struct platform_driver vsp1_platform_driver = {
523 .remove = vsp1_remove,
525 .owner = THIS_MODULE,
531 module_platform_driver(vsp1_platform_driver);
533 MODULE_ALIAS("vsp1");
534 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
535 MODULE_DESCRIPTION("Renesas VSP1 Driver");
536 MODULE_LICENSE("GPL");