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