blob: e58e49c88415c3a00c521309cf47435c06553123 [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_drv.c -- R-Car VSP1 Driver
3 *
4 * Copyright (C) 2013 Renesas Corporation
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
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.
12 */
13
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>
21
22#include "vsp1.h"
23#include "vsp1_lif.h"
24#include "vsp1_rwpf.h"
25#include "vsp1_uds.h"
26
27/* -----------------------------------------------------------------------------
28 * Interrupt Handling
29 */
30
31static irqreturn_t vsp1_irq_handler(int irq, void *data)
32{
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;
36 unsigned int i;
37
38 for (i = 0; i < VPS1_MAX_WPF; ++i) {
39 struct vsp1_rwpf *wpf = vsp1->wpf[i];
40 struct vsp1_pipeline *pipe;
41 u32 status;
42
43 if (wpf == NULL)
44 continue;
45
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);
49
50 if (status & VI6_WFP_IRQ_STA_FRE) {
51 vsp1_pipeline_frame_end(pipe);
52 ret = IRQ_HANDLED;
53 }
54 }
55
56 return ret;
57}
58
59/* -----------------------------------------------------------------------------
60 * Entities
61 */
62
63/*
64 * vsp1_create_links - Create links from all sources to the given sink
65 *
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
69 *
70 * - from a UDS to a UDS (UDS entities can't be chained)
71 * - from an entity to itself (no loops are allowed)
72 */
73static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
74{
75 struct media_entity *entity = &sink->subdev.entity;
76 struct vsp1_entity *source;
77 unsigned int pad;
78 int ret;
79
80 list_for_each_entry(source, &vsp1->entities, list_dev) {
81 u32 flags;
82
83 if (source->type == sink->type)
84 continue;
85
86 if (source->type == VSP1_ENTITY_LIF ||
87 source->type == VSP1_ENTITY_WPF)
88 continue;
89
90 flags = source->type == VSP1_ENTITY_RPF &&
91 sink->type == VSP1_ENTITY_WPF &&
92 source->index == sink->index
93 ? MEDIA_LNK_FL_ENABLED : 0;
94
95 for (pad = 0; pad < entity->num_pads; ++pad) {
96 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
97 continue;
98
99 ret = media_entity_create_link(&source->subdev.entity,
100 source->source_pad,
101 entity, pad, flags);
102 if (ret < 0)
103 return ret;
104 }
105 }
106
107 return 0;
108}
109
110static void vsp1_destroy_entities(struct vsp1_device *vsp1)
111{
112 struct vsp1_entity *entity;
113 struct vsp1_entity *next;
114
115 list_for_each_entry_safe(entity, next, &vsp1->entities, list_dev) {
116 list_del(&entity->list_dev);
117 vsp1_entity_destroy(entity);
118 }
119
120 v4l2_device_unregister(&vsp1->v4l2_dev);
121 media_device_unregister(&vsp1->media_dev);
122}
123
124static int vsp1_create_entities(struct vsp1_device *vsp1)
125{
126 struct media_device *mdev = &vsp1->media_dev;
127 struct v4l2_device *vdev = &vsp1->v4l2_dev;
128 struct vsp1_entity *entity;
129 unsigned int i;
130 int ret;
131
132 mdev->dev = vsp1->dev;
133 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
134 ret = media_device_register(mdev);
135 if (ret < 0) {
136 dev_err(vsp1->dev, "media device registration failed (%d)\n",
137 ret);
138 return ret;
139 }
140
141 vdev->mdev = mdev;
142 ret = v4l2_device_register(vsp1->dev, vdev);
143 if (ret < 0) {
144 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
145 ret);
146 goto done;
147 }
148
149 /* Instantiate all the entities. */
150 if (vsp1->pdata->features & VSP1_HAS_LIF) {
151 vsp1->lif = vsp1_lif_create(vsp1);
152 if (IS_ERR(vsp1->lif)) {
153 ret = PTR_ERR(vsp1->lif);
154 goto done;
155 }
156
157 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
158 }
159
160 for (i = 0; i < vsp1->pdata->rpf_count; ++i) {
161 struct vsp1_rwpf *rpf;
162
163 rpf = vsp1_rpf_create(vsp1, i);
164 if (IS_ERR(rpf)) {
165 ret = PTR_ERR(rpf);
166 goto done;
167 }
168
169 vsp1->rpf[i] = rpf;
170 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
171 }
172
173 for (i = 0; i < vsp1->pdata->uds_count; ++i) {
174 struct vsp1_uds *uds;
175
176 uds = vsp1_uds_create(vsp1, i);
177 if (IS_ERR(uds)) {
178 ret = PTR_ERR(uds);
179 goto done;
180 }
181
182 vsp1->uds[i] = uds;
183 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
184 }
185
186 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
187 struct vsp1_rwpf *wpf;
188
189 wpf = vsp1_wpf_create(vsp1, i);
190 if (IS_ERR(wpf)) {
191 ret = PTR_ERR(wpf);
192 goto done;
193 }
194
195 vsp1->wpf[i] = wpf;
196 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
197 }
198
199 /* Create links. */
200 list_for_each_entry(entity, &vsp1->entities, list_dev) {
201 if (entity->type == VSP1_ENTITY_LIF ||
202 entity->type == VSP1_ENTITY_RPF)
203 continue;
204
205 ret = vsp1_create_links(vsp1, entity);
206 if (ret < 0)
207 goto done;
208 }
209
210 if (vsp1->pdata->features & VSP1_HAS_LIF) {
211 ret = media_entity_create_link(
212 &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
213 &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
214 if (ret < 0)
215 return ret;
216 }
217
218 /* Register all subdevs. */
219 list_for_each_entry(entity, &vsp1->entities, list_dev) {
220 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
221 &entity->subdev);
222 if (ret < 0)
223 goto done;
224 }
225
226 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
227
228done:
229 if (ret < 0)
230 vsp1_destroy_entities(vsp1);
231
232 return ret;
233}
234
235static int vsp1_device_init(struct vsp1_device *vsp1)
236{
237 unsigned int i;
238 u32 status;
239
240 /* Reset any channel that might be running. */
241 status = vsp1_read(vsp1, VI6_STATUS);
242
243 for (i = 0; i < VPS1_MAX_WPF; ++i) {
244 unsigned int timeout;
245
246 if (!(status & VI6_STATUS_SYS_ACT(i)))
247 continue;
248
249 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
250 for (timeout = 10; timeout > 0; --timeout) {
251 status = vsp1_read(vsp1, VI6_STATUS);
252 if (!(status & VI6_STATUS_SYS_ACT(i)))
253 break;
254
255 usleep_range(1000, 2000);
256 }
257
258 if (!timeout) {
259 dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
260 return -ETIMEDOUT;
261 }
262 }
263
264 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
265 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
266
267 for (i = 0; i < VPS1_MAX_RPF; ++i)
268 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
269
270 for (i = 0; i < VPS1_MAX_UDS; ++i)
271 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
272
273 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
274 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
275 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
276 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
277 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
278 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
279
280 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
281 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
282 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
283 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
284
285 return 0;
286}
287
288/*
289 * vsp1_device_get - Acquire the VSP1 device
290 *
291 * Increment the VSP1 reference count and initialize the device if the first
292 * reference is taken.
293 *
294 * Return a pointer to the VSP1 device or NULL if an error occured.
295 */
296struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
297{
298 struct vsp1_device *__vsp1 = vsp1;
299 int ret;
300
301 mutex_lock(&vsp1->lock);
302 if (vsp1->ref_count > 0)
303 goto done;
304
305 ret = clk_prepare_enable(vsp1->clock);
306 if (ret < 0) {
307 __vsp1 = NULL;
308 goto done;
309 }
310
311 ret = vsp1_device_init(vsp1);
312 if (ret < 0) {
313 clk_disable_unprepare(vsp1->clock);
314 __vsp1 = NULL;
315 goto done;
316 }
317
318done:
319 if (__vsp1)
320 vsp1->ref_count++;
321
322 mutex_unlock(&vsp1->lock);
323 return __vsp1;
324}
325
326/*
327 * vsp1_device_put - Release the VSP1 device
328 *
329 * Decrement the VSP1 reference count and cleanup the device if the last
330 * reference is released.
331 */
332void vsp1_device_put(struct vsp1_device *vsp1)
333{
334 mutex_lock(&vsp1->lock);
335
336 if (--vsp1->ref_count == 0)
337 clk_disable_unprepare(vsp1->clock);
338
339 mutex_unlock(&vsp1->lock);
340}
341
342/* -----------------------------------------------------------------------------
343 * Power Management
344 */
345
346#ifdef CONFIG_PM_SLEEP
347static int vsp1_pm_suspend(struct device *dev)
348{
349 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
350
351 WARN_ON(mutex_is_locked(&vsp1->lock));
352
353 if (vsp1->ref_count == 0)
354 return 0;
355
356 clk_disable_unprepare(vsp1->clock);
357 return 0;
358}
359
360static int vsp1_pm_resume(struct device *dev)
361{
362 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
363
364 WARN_ON(mutex_is_locked(&vsp1->lock));
365
366 if (vsp1->ref_count)
367 return 0;
368
369 return clk_prepare_enable(vsp1->clock);
370}
371#endif
372
373static const struct dev_pm_ops vsp1_pm_ops = {
374 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
375};
376
377/* -----------------------------------------------------------------------------
378 * Platform Driver
379 */
380
381static struct vsp1_platform_data *
382vsp1_get_platform_data(struct platform_device *pdev)
383{
384 struct vsp1_platform_data *pdata = pdev->dev.platform_data;
385
386 if (pdata == NULL) {
387 dev_err(&pdev->dev, "missing platform data\n");
388 return NULL;
389 }
390
391 if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
392 dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
393 pdata->rpf_count);
394 return NULL;
395 }
396
397 if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
398 dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
399 pdata->uds_count);
400 return NULL;
401 }
402
403 if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
404 dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
405 pdata->wpf_count);
406 return NULL;
407 }
408
409 return pdata;
410}
411
412static int vsp1_probe(struct platform_device *pdev)
413{
414 struct vsp1_device *vsp1;
415 struct resource *irq;
416 struct resource *io;
417 int ret;
418
419 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
420 if (vsp1 == NULL)
421 return -ENOMEM;
422
423 vsp1->dev = &pdev->dev;
424 mutex_init(&vsp1->lock);
425 INIT_LIST_HEAD(&vsp1->entities);
426
427 vsp1->pdata = vsp1_get_platform_data(pdev);
428 if (vsp1->pdata == NULL)
429 return -ENODEV;
430
431 /* I/O, IRQ and clock resources */
432 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
433 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
434 if (IS_ERR((void *)vsp1->mmio))
435 return PTR_ERR((void *)vsp1->mmio);
436
437 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
438 if (IS_ERR(vsp1->clock)) {
439 dev_err(&pdev->dev, "failed to get clock\n");
440 return PTR_ERR(vsp1->clock);
441 }
442
443 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
444 if (!irq) {
445 dev_err(&pdev->dev, "missing IRQ\n");
446 return -EINVAL;
447 }
448
449 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
450 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
451 if (ret < 0) {
452 dev_err(&pdev->dev, "failed to request IRQ\n");
453 return ret;
454 }
455
456 /* Instanciate entities */
457 ret = vsp1_create_entities(vsp1);
458 if (ret < 0) {
459 dev_err(&pdev->dev, "failed to create entities\n");
460 return ret;
461 }
462
463 platform_set_drvdata(pdev, vsp1);
464
465 return 0;
466}
467
468static int vsp1_remove(struct platform_device *pdev)
469{
470 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
471
472 vsp1_destroy_entities(vsp1);
473
474 return 0;
475}
476
477static struct platform_driver vsp1_platform_driver = {
478 .probe = vsp1_probe,
479 .remove = vsp1_remove,
480 .driver = {
481 .owner = THIS_MODULE,
482 .name = "vsp1",
483 .pm = &vsp1_pm_ops,
484 },
485};
486
487module_platform_driver(vsp1_platform_driver);
488
489MODULE_ALIAS("vsp1");
490MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
491MODULE_DESCRIPTION("Renesas VSP1 Driver");
492MODULE_LICENSE("GPL");