blob: bcede46c4a952e1b9dc8811f2076c9a42f066474 [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"
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030023#include "vsp1_hsit.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030024#include "vsp1_lif.h"
25#include "vsp1_rwpf.h"
26#include "vsp1_uds.h"
27
28/* -----------------------------------------------------------------------------
29 * Interrupt Handling
30 */
31
32static irqreturn_t vsp1_irq_handler(int irq, void *data)
33{
34 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
35 struct vsp1_device *vsp1 = data;
36 irqreturn_t ret = IRQ_NONE;
37 unsigned int i;
38
Katsuya Matsubara83453a72013-07-26 06:32:12 -030039 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030040 struct vsp1_rwpf *wpf = vsp1->wpf[i];
41 struct vsp1_pipeline *pipe;
42 u32 status;
43
44 if (wpf == NULL)
45 continue;
46
47 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
48 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
49 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
50
51 if (status & VI6_WFP_IRQ_STA_FRE) {
52 vsp1_pipeline_frame_end(pipe);
53 ret = IRQ_HANDLED;
54 }
55 }
56
57 return ret;
58}
59
60/* -----------------------------------------------------------------------------
61 * Entities
62 */
63
64/*
65 * vsp1_create_links - Create links from all sources to the given sink
66 *
67 * This function creates media links from all valid sources to the given sink
68 * pad. Links that would be invalid according to the VSP1 hardware capabilities
69 * are skipped. Those include all links
70 *
71 * - from a UDS to a UDS (UDS entities can't be chained)
72 * - from an entity to itself (no loops are allowed)
73 */
74static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
75{
76 struct media_entity *entity = &sink->subdev.entity;
77 struct vsp1_entity *source;
78 unsigned int pad;
79 int ret;
80
81 list_for_each_entry(source, &vsp1->entities, list_dev) {
82 u32 flags;
83
84 if (source->type == sink->type)
85 continue;
86
87 if (source->type == VSP1_ENTITY_LIF ||
88 source->type == VSP1_ENTITY_WPF)
89 continue;
90
91 flags = source->type == VSP1_ENTITY_RPF &&
92 sink->type == VSP1_ENTITY_WPF &&
93 source->index == sink->index
94 ? MEDIA_LNK_FL_ENABLED : 0;
95
96 for (pad = 0; pad < entity->num_pads; ++pad) {
97 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
98 continue;
99
100 ret = media_entity_create_link(&source->subdev.entity,
101 source->source_pad,
102 entity, pad, flags);
103 if (ret < 0)
104 return ret;
Katsuya Matsubaradb32eb62013-07-26 06:32:11 -0300105
106 if (flags & MEDIA_LNK_FL_ENABLED)
107 source->sink = entity;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300108 }
109 }
110
111 return 0;
112}
113
114static void vsp1_destroy_entities(struct vsp1_device *vsp1)
115{
116 struct vsp1_entity *entity;
117 struct vsp1_entity *next;
118
119 list_for_each_entry_safe(entity, next, &vsp1->entities, list_dev) {
120 list_del(&entity->list_dev);
121 vsp1_entity_destroy(entity);
122 }
123
124 v4l2_device_unregister(&vsp1->v4l2_dev);
125 media_device_unregister(&vsp1->media_dev);
126}
127
128static int vsp1_create_entities(struct vsp1_device *vsp1)
129{
130 struct media_device *mdev = &vsp1->media_dev;
131 struct v4l2_device *vdev = &vsp1->v4l2_dev;
132 struct vsp1_entity *entity;
133 unsigned int i;
134 int ret;
135
136 mdev->dev = vsp1->dev;
137 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
Laurent Pinchart10d79b92013-08-22 14:11:47 -0300138 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
139 dev_name(mdev->dev));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300140 ret = media_device_register(mdev);
141 if (ret < 0) {
142 dev_err(vsp1->dev, "media device registration failed (%d)\n",
143 ret);
144 return ret;
145 }
146
147 vdev->mdev = mdev;
148 ret = v4l2_device_register(vsp1->dev, vdev);
149 if (ret < 0) {
150 dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
151 ret);
152 goto done;
153 }
154
155 /* Instantiate all the entities. */
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300156 vsp1->hsi = vsp1_hsit_create(vsp1, true);
157 if (IS_ERR(vsp1->hsi)) {
158 ret = PTR_ERR(vsp1->hsi);
159 goto done;
160 }
161
162 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
163
164 vsp1->hst = vsp1_hsit_create(vsp1, false);
165 if (IS_ERR(vsp1->hst)) {
166 ret = PTR_ERR(vsp1->hst);
167 goto done;
168 }
169
170 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
171
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300172 if (vsp1->pdata->features & VSP1_HAS_LIF) {
173 vsp1->lif = vsp1_lif_create(vsp1);
174 if (IS_ERR(vsp1->lif)) {
175 ret = PTR_ERR(vsp1->lif);
176 goto done;
177 }
178
179 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
180 }
181
182 for (i = 0; i < vsp1->pdata->rpf_count; ++i) {
183 struct vsp1_rwpf *rpf;
184
185 rpf = vsp1_rpf_create(vsp1, i);
186 if (IS_ERR(rpf)) {
187 ret = PTR_ERR(rpf);
188 goto done;
189 }
190
191 vsp1->rpf[i] = rpf;
192 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
193 }
194
195 for (i = 0; i < vsp1->pdata->uds_count; ++i) {
196 struct vsp1_uds *uds;
197
198 uds = vsp1_uds_create(vsp1, i);
199 if (IS_ERR(uds)) {
200 ret = PTR_ERR(uds);
201 goto done;
202 }
203
204 vsp1->uds[i] = uds;
205 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
206 }
207
208 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
209 struct vsp1_rwpf *wpf;
210
211 wpf = vsp1_wpf_create(vsp1, i);
212 if (IS_ERR(wpf)) {
213 ret = PTR_ERR(wpf);
214 goto done;
215 }
216
217 vsp1->wpf[i] = wpf;
218 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
219 }
220
221 /* Create links. */
222 list_for_each_entry(entity, &vsp1->entities, list_dev) {
223 if (entity->type == VSP1_ENTITY_LIF ||
224 entity->type == VSP1_ENTITY_RPF)
225 continue;
226
227 ret = vsp1_create_links(vsp1, entity);
228 if (ret < 0)
229 goto done;
230 }
231
232 if (vsp1->pdata->features & VSP1_HAS_LIF) {
233 ret = media_entity_create_link(
234 &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
235 &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
236 if (ret < 0)
237 return ret;
238 }
239
240 /* Register all subdevs. */
241 list_for_each_entry(entity, &vsp1->entities, list_dev) {
242 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
243 &entity->subdev);
244 if (ret < 0)
245 goto done;
246 }
247
248 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
249
250done:
251 if (ret < 0)
252 vsp1_destroy_entities(vsp1);
253
254 return ret;
255}
256
257static int vsp1_device_init(struct vsp1_device *vsp1)
258{
259 unsigned int i;
260 u32 status;
261
262 /* Reset any channel that might be running. */
263 status = vsp1_read(vsp1, VI6_STATUS);
264
Katsuya Matsubara83453a72013-07-26 06:32:12 -0300265 for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300266 unsigned int timeout;
267
268 if (!(status & VI6_STATUS_SYS_ACT(i)))
269 continue;
270
271 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
272 for (timeout = 10; timeout > 0; --timeout) {
273 status = vsp1_read(vsp1, VI6_STATUS);
274 if (!(status & VI6_STATUS_SYS_ACT(i)))
275 break;
276
277 usleep_range(1000, 2000);
278 }
279
280 if (!timeout) {
281 dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
282 return -ETIMEDOUT;
283 }
284 }
285
286 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
287 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
288
Katsuya Matsubara83453a72013-07-26 06:32:12 -0300289 for (i = 0; i < vsp1->pdata->rpf_count; ++i)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300290 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
291
Katsuya Matsubara83453a72013-07-26 06:32:12 -0300292 for (i = 0; i < vsp1->pdata->uds_count; ++i)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300293 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
294
295 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
296 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
297 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
298 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
299 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
300 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
301
302 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
303 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
304 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
305 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
306
307 return 0;
308}
309
Laurent Pinchartf2226a32013-08-22 14:29:46 -0300310static int vsp1_clocks_enable(struct vsp1_device *vsp1)
311{
312 int ret;
313
314 ret = clk_prepare_enable(vsp1->clock);
315 if (ret < 0)
316 return ret;
317
318 if (IS_ERR(vsp1->rt_clock))
319 return 0;
320
321 ret = clk_prepare_enable(vsp1->rt_clock);
322 if (ret < 0) {
323 clk_disable_unprepare(vsp1->clock);
324 return ret;
325 }
326
327 return 0;
328}
329
330static void vsp1_clocks_disable(struct vsp1_device *vsp1)
331{
332 if (!IS_ERR(vsp1->rt_clock))
333 clk_disable_unprepare(vsp1->rt_clock);
334 clk_disable_unprepare(vsp1->clock);
335}
336
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300337/*
338 * vsp1_device_get - Acquire the VSP1 device
339 *
340 * Increment the VSP1 reference count and initialize the device if the first
341 * reference is taken.
342 *
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300343 * Return a pointer to the VSP1 device or NULL if an error occurred.
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300344 */
345struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
346{
347 struct vsp1_device *__vsp1 = vsp1;
348 int ret;
349
350 mutex_lock(&vsp1->lock);
351 if (vsp1->ref_count > 0)
352 goto done;
353
Laurent Pinchartf2226a32013-08-22 14:29:46 -0300354 ret = vsp1_clocks_enable(vsp1);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300355 if (ret < 0) {
356 __vsp1 = NULL;
357 goto done;
358 }
359
360 ret = vsp1_device_init(vsp1);
361 if (ret < 0) {
Laurent Pinchartf2226a32013-08-22 14:29:46 -0300362 vsp1_clocks_disable(vsp1);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300363 __vsp1 = NULL;
364 goto done;
365 }
366
367done:
368 if (__vsp1)
369 vsp1->ref_count++;
370
371 mutex_unlock(&vsp1->lock);
372 return __vsp1;
373}
374
375/*
376 * vsp1_device_put - Release the VSP1 device
377 *
378 * Decrement the VSP1 reference count and cleanup the device if the last
379 * reference is released.
380 */
381void vsp1_device_put(struct vsp1_device *vsp1)
382{
383 mutex_lock(&vsp1->lock);
384
385 if (--vsp1->ref_count == 0)
Laurent Pinchartf2226a32013-08-22 14:29:46 -0300386 vsp1_clocks_disable(vsp1);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300387
388 mutex_unlock(&vsp1->lock);
389}
390
391/* -----------------------------------------------------------------------------
392 * Power Management
393 */
394
395#ifdef CONFIG_PM_SLEEP
396static int vsp1_pm_suspend(struct device *dev)
397{
398 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
399
400 WARN_ON(mutex_is_locked(&vsp1->lock));
401
402 if (vsp1->ref_count == 0)
403 return 0;
404
Laurent Pinchartf2226a32013-08-22 14:29:46 -0300405 vsp1_clocks_disable(vsp1);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300406 return 0;
407}
408
409static int vsp1_pm_resume(struct device *dev)
410{
411 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
412
413 WARN_ON(mutex_is_locked(&vsp1->lock));
414
415 if (vsp1->ref_count)
416 return 0;
417
Laurent Pinchartf2226a32013-08-22 14:29:46 -0300418 return vsp1_clocks_enable(vsp1);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300419}
420#endif
421
422static const struct dev_pm_ops vsp1_pm_ops = {
423 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
424};
425
426/* -----------------------------------------------------------------------------
427 * Platform Driver
428 */
429
430static struct vsp1_platform_data *
431vsp1_get_platform_data(struct platform_device *pdev)
432{
433 struct vsp1_platform_data *pdata = pdev->dev.platform_data;
434
435 if (pdata == NULL) {
436 dev_err(&pdev->dev, "missing platform data\n");
437 return NULL;
438 }
439
440 if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
441 dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
442 pdata->rpf_count);
443 return NULL;
444 }
445
446 if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
447 dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
448 pdata->uds_count);
449 return NULL;
450 }
451
452 if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
453 dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
454 pdata->wpf_count);
455 return NULL;
456 }
457
458 return pdata;
459}
460
461static int vsp1_probe(struct platform_device *pdev)
462{
463 struct vsp1_device *vsp1;
464 struct resource *irq;
465 struct resource *io;
466 int ret;
467
468 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
469 if (vsp1 == NULL)
470 return -ENOMEM;
471
472 vsp1->dev = &pdev->dev;
473 mutex_init(&vsp1->lock);
474 INIT_LIST_HEAD(&vsp1->entities);
475
476 vsp1->pdata = vsp1_get_platform_data(pdev);
477 if (vsp1->pdata == NULL)
478 return -ENODEV;
479
480 /* I/O, IRQ and clock resources */
481 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
482 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
Mauro Carvalho Chehabcbec6d32013-08-24 08:47:51 -0300483 if (IS_ERR(vsp1->mmio))
484 return PTR_ERR(vsp1->mmio);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300485
486 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
487 if (IS_ERR(vsp1->clock)) {
488 dev_err(&pdev->dev, "failed to get clock\n");
489 return PTR_ERR(vsp1->clock);
490 }
491
Laurent Pinchartf2226a32013-08-22 14:29:46 -0300492 /* The RT clock is optional */
493 vsp1->rt_clock = devm_clk_get(&pdev->dev, "rt");
494
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300495 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
496 if (!irq) {
497 dev_err(&pdev->dev, "missing IRQ\n");
498 return -EINVAL;
499 }
500
501 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
502 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
503 if (ret < 0) {
504 dev_err(&pdev->dev, "failed to request IRQ\n");
505 return ret;
506 }
507
508 /* Instanciate entities */
509 ret = vsp1_create_entities(vsp1);
510 if (ret < 0) {
511 dev_err(&pdev->dev, "failed to create entities\n");
512 return ret;
513 }
514
515 platform_set_drvdata(pdev, vsp1);
516
517 return 0;
518}
519
520static int vsp1_remove(struct platform_device *pdev)
521{
522 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
523
524 vsp1_destroy_entities(vsp1);
525
526 return 0;
527}
528
529static struct platform_driver vsp1_platform_driver = {
530 .probe = vsp1_probe,
531 .remove = vsp1_remove,
532 .driver = {
533 .owner = THIS_MODULE,
534 .name = "vsp1",
535 .pm = &vsp1_pm_ops,
536 },
537};
538
539module_platform_driver(vsp1_platform_driver);
540
541MODULE_ALIAS("vsp1");
542MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
543MODULE_DESCRIPTION("Renesas VSP1 Driver");
544MODULE_LICENSE("GPL");