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