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