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>
26 #include "vsp1_hsit.h"
29 #include "vsp1_rwpf.h"
32 #include "vsp1_video.h"
34 /* -----------------------------------------------------------------------------
38 static irqreturn_t
vsp1_irq_handler(int irq
, void *data
)
40 u32 mask
= VI6_WFP_IRQ_STA_DFE
| VI6_WFP_IRQ_STA_FRE
;
41 struct vsp1_device
*vsp1
= data
;
42 irqreturn_t ret
= IRQ_NONE
;
45 for (i
= 0; i
< vsp1
->pdata
.wpf_count
; ++i
) {
46 struct vsp1_rwpf
*wpf
= vsp1
->wpf
[i
];
47 struct vsp1_pipeline
*pipe
;
53 pipe
= to_vsp1_pipeline(&wpf
->entity
.subdev
.entity
);
54 status
= vsp1_read(vsp1
, VI6_WPF_IRQ_STA(i
));
55 vsp1_write(vsp1
, VI6_WPF_IRQ_STA(i
), ~status
& mask
);
57 if (status
& VI6_WFP_IRQ_STA_FRE
) {
58 vsp1_pipeline_frame_end(pipe
);
66 /* -----------------------------------------------------------------------------
71 * vsp1_create_sink_links - Create links from all sources to the given sink
73 * This function creates media links from all valid sources to the given sink
74 * pad. Links that would be invalid according to the VSP1 hardware capabilities
75 * are skipped. Those include all links
77 * - from a UDS to a UDS (UDS entities can't be chained)
78 * - from an entity to itself (no loops are allowed)
80 static int vsp1_create_sink_links(struct vsp1_device
*vsp1
,
81 struct vsp1_entity
*sink
)
83 struct media_entity
*entity
= &sink
->subdev
.entity
;
84 struct vsp1_entity
*source
;
88 list_for_each_entry(source
, &vsp1
->entities
, list_dev
) {
91 if (source
->type
== sink
->type
)
94 if (source
->type
== VSP1_ENTITY_LIF
||
95 source
->type
== VSP1_ENTITY_WPF
)
98 flags
= source
->type
== VSP1_ENTITY_RPF
&&
99 sink
->type
== VSP1_ENTITY_WPF
&&
100 source
->index
== sink
->index
101 ? MEDIA_LNK_FL_ENABLED
: 0;
103 for (pad
= 0; pad
< entity
->num_pads
; ++pad
) {
104 if (!(entity
->pads
[pad
].flags
& MEDIA_PAD_FL_SINK
))
107 ret
= media_create_pad_link(&source
->subdev
.entity
,
113 if (flags
& MEDIA_LNK_FL_ENABLED
)
114 source
->sink
= entity
;
121 static int vsp1_create_links(struct vsp1_device
*vsp1
)
123 struct vsp1_entity
*entity
;
127 list_for_each_entry(entity
, &vsp1
->entities
, list_dev
) {
128 if (entity
->type
== VSP1_ENTITY_LIF
||
129 entity
->type
== VSP1_ENTITY_RPF
)
132 ret
= vsp1_create_sink_links(vsp1
, entity
);
137 if (vsp1
->pdata
.features
& VSP1_HAS_LIF
) {
138 ret
= media_create_pad_link(&vsp1
->wpf
[0]->entity
.subdev
.entity
,
140 &vsp1
->lif
->entity
.subdev
.entity
,
146 for (i
= 0; i
< vsp1
->pdata
.rpf_count
; ++i
) {
147 struct vsp1_rwpf
*rpf
= vsp1
->rpf
[i
];
149 ret
= media_create_pad_link(&rpf
->video
->video
.entity
, 0,
150 &rpf
->entity
.subdev
.entity
,
152 MEDIA_LNK_FL_ENABLED
|
153 MEDIA_LNK_FL_IMMUTABLE
);
158 for (i
= 0; i
< vsp1
->pdata
.wpf_count
; ++i
) {
159 /* Connect the video device to the WPF. All connections are
160 * immutable except for the WPF0 source link if a LIF is
163 struct vsp1_rwpf
*wpf
= vsp1
->wpf
[i
];
164 unsigned int flags
= MEDIA_LNK_FL_ENABLED
;
166 if (!(vsp1
->pdata
.features
& VSP1_HAS_LIF
) || i
!= 0)
167 flags
|= MEDIA_LNK_FL_IMMUTABLE
;
169 ret
= media_create_pad_link(&wpf
->entity
.subdev
.entity
,
171 &wpf
->video
->video
.entity
, 0,
180 static void vsp1_destroy_entities(struct vsp1_device
*vsp1
)
182 struct vsp1_entity
*entity
, *_entity
;
183 struct vsp1_video
*video
, *_video
;
185 list_for_each_entry_safe(entity
, _entity
, &vsp1
->entities
, list_dev
) {
186 list_del(&entity
->list_dev
);
187 vsp1_entity_destroy(entity
);
190 list_for_each_entry_safe(video
, _video
, &vsp1
->videos
, list
) {
191 list_del(&video
->list
);
192 vsp1_video_cleanup(video
);
195 v4l2_device_unregister(&vsp1
->v4l2_dev
);
196 media_device_unregister(&vsp1
->media_dev
);
197 media_device_cleanup(&vsp1
->media_dev
);
200 static int vsp1_create_entities(struct vsp1_device
*vsp1
)
202 struct media_device
*mdev
= &vsp1
->media_dev
;
203 struct v4l2_device
*vdev
= &vsp1
->v4l2_dev
;
204 struct vsp1_entity
*entity
;
208 mdev
->dev
= vsp1
->dev
;
209 strlcpy(mdev
->model
, "VSP1", sizeof(mdev
->model
));
210 snprintf(mdev
->bus_info
, sizeof(mdev
->bus_info
), "platform:%s",
211 dev_name(mdev
->dev
));
212 media_device_init(mdev
);
215 ret
= v4l2_device_register(vsp1
->dev
, vdev
);
217 dev_err(vsp1
->dev
, "V4L2 device registration failed (%d)\n",
222 /* Instantiate all the entities. */
223 if (vsp1
->pdata
.features
& VSP1_HAS_BRU
) {
224 vsp1
->bru
= vsp1_bru_create(vsp1
);
225 if (IS_ERR(vsp1
->bru
)) {
226 ret
= PTR_ERR(vsp1
->bru
);
230 list_add_tail(&vsp1
->bru
->entity
.list_dev
, &vsp1
->entities
);
233 vsp1
->hsi
= vsp1_hsit_create(vsp1
, true);
234 if (IS_ERR(vsp1
->hsi
)) {
235 ret
= PTR_ERR(vsp1
->hsi
);
239 list_add_tail(&vsp1
->hsi
->entity
.list_dev
, &vsp1
->entities
);
241 vsp1
->hst
= vsp1_hsit_create(vsp1
, false);
242 if (IS_ERR(vsp1
->hst
)) {
243 ret
= PTR_ERR(vsp1
->hst
);
247 list_add_tail(&vsp1
->hst
->entity
.list_dev
, &vsp1
->entities
);
249 if (vsp1
->pdata
.features
& VSP1_HAS_LIF
) {
250 vsp1
->lif
= vsp1_lif_create(vsp1
);
251 if (IS_ERR(vsp1
->lif
)) {
252 ret
= PTR_ERR(vsp1
->lif
);
256 list_add_tail(&vsp1
->lif
->entity
.list_dev
, &vsp1
->entities
);
259 if (vsp1
->pdata
.features
& VSP1_HAS_LUT
) {
260 vsp1
->lut
= vsp1_lut_create(vsp1
);
261 if (IS_ERR(vsp1
->lut
)) {
262 ret
= PTR_ERR(vsp1
->lut
);
266 list_add_tail(&vsp1
->lut
->entity
.list_dev
, &vsp1
->entities
);
269 for (i
= 0; i
< vsp1
->pdata
.rpf_count
; ++i
) {
270 struct vsp1_video
*video
;
271 struct vsp1_rwpf
*rpf
;
273 rpf
= vsp1_rpf_create(vsp1
, i
);
280 list_add_tail(&rpf
->entity
.list_dev
, &vsp1
->entities
);
282 video
= vsp1_video_create(vsp1
, rpf
);
284 ret
= PTR_ERR(video
);
288 list_add_tail(&video
->list
, &vsp1
->videos
);
291 if (vsp1
->pdata
.features
& VSP1_HAS_SRU
) {
292 vsp1
->sru
= vsp1_sru_create(vsp1
);
293 if (IS_ERR(vsp1
->sru
)) {
294 ret
= PTR_ERR(vsp1
->sru
);
298 list_add_tail(&vsp1
->sru
->entity
.list_dev
, &vsp1
->entities
);
301 for (i
= 0; i
< vsp1
->pdata
.uds_count
; ++i
) {
302 struct vsp1_uds
*uds
;
304 uds
= vsp1_uds_create(vsp1
, i
);
311 list_add_tail(&uds
->entity
.list_dev
, &vsp1
->entities
);
314 for (i
= 0; i
< vsp1
->pdata
.wpf_count
; ++i
) {
315 struct vsp1_video
*video
;
316 struct vsp1_rwpf
*wpf
;
318 wpf
= vsp1_wpf_create(vsp1
, i
);
325 list_add_tail(&wpf
->entity
.list_dev
, &vsp1
->entities
);
327 video
= vsp1_video_create(vsp1
, wpf
);
329 ret
= PTR_ERR(video
);
333 list_add_tail(&video
->list
, &vsp1
->videos
);
334 wpf
->entity
.sink
= &video
->video
.entity
;
337 /* Register all subdevs. */
338 list_for_each_entry(entity
, &vsp1
->entities
, list_dev
) {
339 ret
= v4l2_device_register_subdev(&vsp1
->v4l2_dev
,
346 ret
= vsp1_create_links(vsp1
);
350 ret
= v4l2_device_register_subdev_nodes(&vsp1
->v4l2_dev
);
354 ret
= media_device_register(mdev
);
358 vsp1_destroy_entities(vsp1
);
363 static int vsp1_device_init(struct vsp1_device
*vsp1
)
368 /* Reset any channel that might be running. */
369 status
= vsp1_read(vsp1
, VI6_STATUS
);
371 for (i
= 0; i
< vsp1
->pdata
.wpf_count
; ++i
) {
372 unsigned int timeout
;
374 if (!(status
& VI6_STATUS_SYS_ACT(i
)))
377 vsp1_write(vsp1
, VI6_SRESET
, VI6_SRESET_SRTS(i
));
378 for (timeout
= 10; timeout
> 0; --timeout
) {
379 status
= vsp1_read(vsp1
, VI6_STATUS
);
380 if (!(status
& VI6_STATUS_SYS_ACT(i
)))
383 usleep_range(1000, 2000);
387 dev_err(vsp1
->dev
, "failed to reset wpf.%u\n", i
);
392 vsp1_write(vsp1
, VI6_CLK_DCSWT
, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT
) |
393 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT
));
395 for (i
= 0; i
< vsp1
->pdata
.rpf_count
; ++i
)
396 vsp1_write(vsp1
, VI6_DPR_RPF_ROUTE(i
), VI6_DPR_NODE_UNUSED
);
398 for (i
= 0; i
< vsp1
->pdata
.uds_count
; ++i
)
399 vsp1_write(vsp1
, VI6_DPR_UDS_ROUTE(i
), VI6_DPR_NODE_UNUSED
);
401 vsp1_write(vsp1
, VI6_DPR_SRU_ROUTE
, VI6_DPR_NODE_UNUSED
);
402 vsp1_write(vsp1
, VI6_DPR_LUT_ROUTE
, VI6_DPR_NODE_UNUSED
);
403 vsp1_write(vsp1
, VI6_DPR_CLU_ROUTE
, VI6_DPR_NODE_UNUSED
);
404 vsp1_write(vsp1
, VI6_DPR_HST_ROUTE
, VI6_DPR_NODE_UNUSED
);
405 vsp1_write(vsp1
, VI6_DPR_HSI_ROUTE
, VI6_DPR_NODE_UNUSED
);
406 vsp1_write(vsp1
, VI6_DPR_BRU_ROUTE
, VI6_DPR_NODE_UNUSED
);
408 vsp1_write(vsp1
, VI6_DPR_HGO_SMPPT
, (7 << VI6_DPR_SMPPT_TGW_SHIFT
) |
409 (VI6_DPR_NODE_UNUSED
<< VI6_DPR_SMPPT_PT_SHIFT
));
410 vsp1_write(vsp1
, VI6_DPR_HGT_SMPPT
, (7 << VI6_DPR_SMPPT_TGW_SHIFT
) |
411 (VI6_DPR_NODE_UNUSED
<< VI6_DPR_SMPPT_PT_SHIFT
));
417 * vsp1_device_get - Acquire the VSP1 device
419 * Increment the VSP1 reference count and initialize the device if the first
420 * reference is taken.
422 * Return 0 on success or a negative error code otherwise.
424 int vsp1_device_get(struct vsp1_device
*vsp1
)
428 mutex_lock(&vsp1
->lock
);
429 if (vsp1
->ref_count
> 0)
432 ret
= clk_prepare_enable(vsp1
->clock
);
436 ret
= vsp1_device_init(vsp1
);
438 clk_disable_unprepare(vsp1
->clock
);
446 mutex_unlock(&vsp1
->lock
);
451 * vsp1_device_put - Release the VSP1 device
453 * Decrement the VSP1 reference count and cleanup the device if the last
454 * reference is released.
456 void vsp1_device_put(struct vsp1_device
*vsp1
)
458 mutex_lock(&vsp1
->lock
);
460 if (--vsp1
->ref_count
== 0)
461 clk_disable_unprepare(vsp1
->clock
);
463 mutex_unlock(&vsp1
->lock
);
466 /* -----------------------------------------------------------------------------
470 #ifdef CONFIG_PM_SLEEP
471 static int vsp1_pm_suspend(struct device
*dev
)
473 struct vsp1_device
*vsp1
= dev_get_drvdata(dev
);
475 WARN_ON(mutex_is_locked(&vsp1
->lock
));
477 if (vsp1
->ref_count
== 0)
480 vsp1_pipelines_suspend(vsp1
);
482 clk_disable_unprepare(vsp1
->clock
);
487 static int vsp1_pm_resume(struct device
*dev
)
489 struct vsp1_device
*vsp1
= dev_get_drvdata(dev
);
491 WARN_ON(mutex_is_locked(&vsp1
->lock
));
493 if (vsp1
->ref_count
== 0)
496 clk_prepare_enable(vsp1
->clock
);
498 vsp1_pipelines_resume(vsp1
);
504 static const struct dev_pm_ops vsp1_pm_ops
= {
505 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend
, vsp1_pm_resume
)
508 /* -----------------------------------------------------------------------------
512 static int vsp1_parse_dt(struct vsp1_device
*vsp1
)
514 struct device_node
*np
= vsp1
->dev
->of_node
;
515 struct vsp1_platform_data
*pdata
= &vsp1
->pdata
;
517 if (of_property_read_bool(np
, "renesas,has-lif"))
518 pdata
->features
|= VSP1_HAS_LIF
;
519 if (of_property_read_bool(np
, "renesas,has-lut"))
520 pdata
->features
|= VSP1_HAS_LUT
;
521 if (of_property_read_bool(np
, "renesas,has-sru"))
522 pdata
->features
|= VSP1_HAS_SRU
;
524 of_property_read_u32(np
, "renesas,#rpf", &pdata
->rpf_count
);
525 of_property_read_u32(np
, "renesas,#uds", &pdata
->uds_count
);
526 of_property_read_u32(np
, "renesas,#wpf", &pdata
->wpf_count
);
528 if (pdata
->rpf_count
<= 0 || pdata
->rpf_count
> VSP1_MAX_RPF
) {
529 dev_err(vsp1
->dev
, "invalid number of RPF (%u)\n",
534 if (pdata
->uds_count
> VSP1_MAX_UDS
) {
535 dev_err(vsp1
->dev
, "invalid number of UDS (%u)\n",
540 if (pdata
->wpf_count
<= 0 || pdata
->wpf_count
> VSP1_MAX_WPF
) {
541 dev_err(vsp1
->dev
, "invalid number of WPF (%u)\n",
546 pdata
->features
|= VSP1_HAS_BRU
;
547 pdata
->num_bru_inputs
= 4;
552 static int vsp1_probe(struct platform_device
*pdev
)
554 struct vsp1_device
*vsp1
;
555 struct resource
*irq
;
559 vsp1
= devm_kzalloc(&pdev
->dev
, sizeof(*vsp1
), GFP_KERNEL
);
563 vsp1
->dev
= &pdev
->dev
;
564 mutex_init(&vsp1
->lock
);
565 INIT_LIST_HEAD(&vsp1
->entities
);
566 INIT_LIST_HEAD(&vsp1
->videos
);
568 ret
= vsp1_parse_dt(vsp1
);
572 /* I/O, IRQ and clock resources */
573 io
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
574 vsp1
->mmio
= devm_ioremap_resource(&pdev
->dev
, io
);
575 if (IS_ERR(vsp1
->mmio
))
576 return PTR_ERR(vsp1
->mmio
);
578 vsp1
->clock
= devm_clk_get(&pdev
->dev
, NULL
);
579 if (IS_ERR(vsp1
->clock
)) {
580 dev_err(&pdev
->dev
, "failed to get clock\n");
581 return PTR_ERR(vsp1
->clock
);
584 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
586 dev_err(&pdev
->dev
, "missing IRQ\n");
590 ret
= devm_request_irq(&pdev
->dev
, irq
->start
, vsp1_irq_handler
,
591 IRQF_SHARED
, dev_name(&pdev
->dev
), vsp1
);
593 dev_err(&pdev
->dev
, "failed to request IRQ\n");
597 /* Instanciate entities */
598 ret
= vsp1_create_entities(vsp1
);
600 dev_err(&pdev
->dev
, "failed to create entities\n");
604 platform_set_drvdata(pdev
, vsp1
);
609 static int vsp1_remove(struct platform_device
*pdev
)
611 struct vsp1_device
*vsp1
= platform_get_drvdata(pdev
);
613 vsp1_destroy_entities(vsp1
);
618 static const struct of_device_id vsp1_of_match
[] = {
619 { .compatible
= "renesas,vsp1" },
623 static struct platform_driver vsp1_platform_driver
= {
625 .remove
= vsp1_remove
,
629 .of_match_table
= vsp1_of_match
,
633 module_platform_driver(vsp1_platform_driver
);
635 MODULE_ALIAS("vsp1");
636 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
637 MODULE_DESCRIPTION("Renesas VSP1 Driver");
638 MODULE_LICENSE("GPL");