[media] media-device: split media initialization and registration
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_drv.c
CommitLineData
26e0ca22
LP
1/*
2 * vsp1_drv.c -- R-Car VSP1 Driver
3 *
139c9286 4 * Copyright (C) 2013-2015 Renesas Electronics Corporation
26e0ca22
LP
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>
0b82fb95 19#include <linux/of.h>
26e0ca22
LP
20#include <linux/platform_device.h>
21#include <linux/videodev2.h>
22
23#include "vsp1.h"
629bb6d4 24#include "vsp1_bru.h"
5cdf5741 25#include "vsp1_hsit.h"
26e0ca22 26#include "vsp1_lif.h"
989af883 27#include "vsp1_lut.h"
26e0ca22 28#include "vsp1_rwpf.h"
a626e64e 29#include "vsp1_sru.h"
26e0ca22
LP
30#include "vsp1_uds.h"
31
32/* -----------------------------------------------------------------------------
33 * Interrupt Handling
34 */
35
36static irqreturn_t vsp1_irq_handler(int irq, void *data)
37{
38 u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
39 struct vsp1_device *vsp1 = data;
40 irqreturn_t ret = IRQ_NONE;
41 unsigned int i;
42
32d17597 43 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
26e0ca22
LP
44 struct vsp1_rwpf *wpf = vsp1->wpf[i];
45 struct vsp1_pipeline *pipe;
46 u32 status;
47
48 if (wpf == NULL)
49 continue;
50
51 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
52 status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
53 vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
54
55 if (status & VI6_WFP_IRQ_STA_FRE) {
56 vsp1_pipeline_frame_end(pipe);
57 ret = IRQ_HANDLED;
58 }
59 }
60
61 return ret;
62}
63
64/* -----------------------------------------------------------------------------
65 * Entities
66 */
67
68/*
69 * vsp1_create_links - Create links from all sources to the given sink
70 *
71 * This function creates media links from all valid sources to the given sink
72 * pad. Links that would be invalid according to the VSP1 hardware capabilities
73 * are skipped. Those include all links
74 *
75 * - from a UDS to a UDS (UDS entities can't be chained)
76 * - from an entity to itself (no loops are allowed)
77 */
78static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
79{
80 struct media_entity *entity = &sink->subdev.entity;
81 struct vsp1_entity *source;
82 unsigned int pad;
83 int ret;
84
85 list_for_each_entry(source, &vsp1->entities, list_dev) {
86 u32 flags;
87
88 if (source->type == sink->type)
89 continue;
90
91 if (source->type == VSP1_ENTITY_LIF ||
92 source->type == VSP1_ENTITY_WPF)
93 continue;
94
95 flags = source->type == VSP1_ENTITY_RPF &&
96 sink->type == VSP1_ENTITY_WPF &&
97 source->index == sink->index
98 ? MEDIA_LNK_FL_ENABLED : 0;
99
100 for (pad = 0; pad < entity->num_pads; ++pad) {
101 if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
102 continue;
103
8df00a15 104 ret = media_create_pad_link(&source->subdev.entity,
26e0ca22
LP
105 source->source_pad,
106 entity, pad, flags);
107 if (ret < 0)
108 return ret;
db32eb6c
KM
109
110 if (flags & MEDIA_LNK_FL_ENABLED)
111 source->sink = entity;
26e0ca22
LP
112 }
113 }
114
115 return 0;
116}
117
118static void vsp1_destroy_entities(struct vsp1_device *vsp1)
119{
120 struct vsp1_entity *entity;
121 struct vsp1_entity *next;
122
123 list_for_each_entry_safe(entity, next, &vsp1->entities, list_dev) {
124 list_del(&entity->list_dev);
125 vsp1_entity_destroy(entity);
126 }
127
128 v4l2_device_unregister(&vsp1->v4l2_dev);
129 media_device_unregister(&vsp1->media_dev);
9832e155 130 media_device_cleanup(&vsp1->media_dev);
26e0ca22
LP
131}
132
133static int vsp1_create_entities(struct vsp1_device *vsp1)
134{
135 struct media_device *mdev = &vsp1->media_dev;
136 struct v4l2_device *vdev = &vsp1->v4l2_dev;
137 struct vsp1_entity *entity;
138 unsigned int i;
139 int ret;
140
141 mdev->dev = vsp1->dev;
142 strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
10d79b99
LP
143 snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
144 dev_name(mdev->dev));
9832e155 145 media_device_init(mdev);
26e0ca22
LP
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. */
629bb6d4
LP
156 vsp1->bru = vsp1_bru_create(vsp1);
157 if (IS_ERR(vsp1->bru)) {
158 ret = PTR_ERR(vsp1->bru);
159 goto done;
160 }
161
162 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
163
5cdf5741
LP
164 vsp1->hsi = vsp1_hsit_create(vsp1, true);
165 if (IS_ERR(vsp1->hsi)) {
166 ret = PTR_ERR(vsp1->hsi);
167 goto done;
168 }
169
170 list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
171
172 vsp1->hst = vsp1_hsit_create(vsp1, false);
173 if (IS_ERR(vsp1->hst)) {
174 ret = PTR_ERR(vsp1->hst);
175 goto done;
176 }
177
178 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
179
32d17597 180 if (vsp1->pdata.features & VSP1_HAS_LIF) {
26e0ca22
LP
181 vsp1->lif = vsp1_lif_create(vsp1);
182 if (IS_ERR(vsp1->lif)) {
183 ret = PTR_ERR(vsp1->lif);
184 goto done;
185 }
186
187 list_add_tail(&vsp1->lif->entity.list_dev, &vsp1->entities);
188 }
189
32d17597 190 if (vsp1->pdata.features & VSP1_HAS_LUT) {
989af883
LP
191 vsp1->lut = vsp1_lut_create(vsp1);
192 if (IS_ERR(vsp1->lut)) {
193 ret = PTR_ERR(vsp1->lut);
194 goto done;
195 }
196
197 list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
198 }
199
32d17597 200 for (i = 0; i < vsp1->pdata.rpf_count; ++i) {
26e0ca22
LP
201 struct vsp1_rwpf *rpf;
202
203 rpf = vsp1_rpf_create(vsp1, i);
204 if (IS_ERR(rpf)) {
205 ret = PTR_ERR(rpf);
206 goto done;
207 }
208
209 vsp1->rpf[i] = rpf;
210 list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
211 }
212
32d17597 213 if (vsp1->pdata.features & VSP1_HAS_SRU) {
a626e64e
LP
214 vsp1->sru = vsp1_sru_create(vsp1);
215 if (IS_ERR(vsp1->sru)) {
216 ret = PTR_ERR(vsp1->sru);
217 goto done;
218 }
219
220 list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
221 }
222
32d17597 223 for (i = 0; i < vsp1->pdata.uds_count; ++i) {
26e0ca22
LP
224 struct vsp1_uds *uds;
225
226 uds = vsp1_uds_create(vsp1, i);
227 if (IS_ERR(uds)) {
228 ret = PTR_ERR(uds);
229 goto done;
230 }
231
232 vsp1->uds[i] = uds;
233 list_add_tail(&uds->entity.list_dev, &vsp1->entities);
234 }
235
32d17597 236 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
26e0ca22
LP
237 struct vsp1_rwpf *wpf;
238
239 wpf = vsp1_wpf_create(vsp1, i);
240 if (IS_ERR(wpf)) {
241 ret = PTR_ERR(wpf);
242 goto done;
243 }
244
245 vsp1->wpf[i] = wpf;
246 list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
247 }
248
7213fe7e
JMC
249 /* Register all subdevs. */
250 list_for_each_entry(entity, &vsp1->entities, list_dev) {
251 ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
252 &entity->subdev);
253 if (ret < 0)
254 goto done;
255 }
256
26e0ca22
LP
257 /* Create links. */
258 list_for_each_entry(entity, &vsp1->entities, list_dev) {
c7621b30 259 if (entity->type == VSP1_ENTITY_LIF) {
d8a2cf41 260 ret = vsp1_wpf_create_links(vsp1, entity);
c7621b30
JMC
261 if (ret < 0)
262 goto done;
1488eee3 263 } else if (entity->type == VSP1_ENTITY_RPF) {
d8a2cf41 264 ret = vsp1_rpf_create_links(vsp1, entity);
c7621b30
JMC
265 if (ret < 0)
266 goto done;
1488eee3
JMC
267 } else {
268 ret = vsp1_create_links(vsp1, entity);
269 if (ret < 0)
270 goto done;
c7621b30 271 }
26e0ca22
LP
272 }
273
32d17597 274 if (vsp1->pdata.features & VSP1_HAS_LIF) {
8df00a15 275 ret = media_create_pad_link(
26e0ca22
LP
276 &vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
277 &vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
278 if (ret < 0)
279 return ret;
280 }
281
26e0ca22 282 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
9832e155
JMC
283 if (ret < 0)
284 goto done;
285
286 ret = media_device_register(mdev);
26e0ca22
LP
287
288done:
289 if (ret < 0)
290 vsp1_destroy_entities(vsp1);
291
292 return ret;
293}
294
295static int vsp1_device_init(struct vsp1_device *vsp1)
296{
297 unsigned int i;
298 u32 status;
299
300 /* Reset any channel that might be running. */
301 status = vsp1_read(vsp1, VI6_STATUS);
302
32d17597 303 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
26e0ca22
LP
304 unsigned int timeout;
305
306 if (!(status & VI6_STATUS_SYS_ACT(i)))
307 continue;
308
309 vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
310 for (timeout = 10; timeout > 0; --timeout) {
311 status = vsp1_read(vsp1, VI6_STATUS);
312 if (!(status & VI6_STATUS_SYS_ACT(i)))
313 break;
314
315 usleep_range(1000, 2000);
316 }
317
318 if (!timeout) {
319 dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
320 return -ETIMEDOUT;
321 }
322 }
323
324 vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
325 (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
326
32d17597 327 for (i = 0; i < vsp1->pdata.rpf_count; ++i)
26e0ca22
LP
328 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
329
32d17597 330 for (i = 0; i < vsp1->pdata.uds_count; ++i)
26e0ca22
LP
331 vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
332
333 vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
334 vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
335 vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
336 vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
337 vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
338 vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
339
340 vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
341 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
342 vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
343 (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
344
345 return 0;
346}
347
348/*
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 *
4c16d6a0 354 * Return 0 on success or a negative error code otherwise.
26e0ca22 355 */
4c16d6a0 356int vsp1_device_get(struct vsp1_device *vsp1)
26e0ca22 357{
4c16d6a0 358 int ret = 0;
26e0ca22
LP
359
360 mutex_lock(&vsp1->lock);
361 if (vsp1->ref_count > 0)
362 goto done;
363
4fc78784 364 ret = clk_prepare_enable(vsp1->clock);
4c16d6a0 365 if (ret < 0)
26e0ca22 366 goto done;
26e0ca22
LP
367
368 ret = vsp1_device_init(vsp1);
369 if (ret < 0) {
4fc78784 370 clk_disable_unprepare(vsp1->clock);
26e0ca22
LP
371 goto done;
372 }
373
374done:
4c16d6a0 375 if (!ret)
26e0ca22
LP
376 vsp1->ref_count++;
377
378 mutex_unlock(&vsp1->lock);
4c16d6a0 379 return ret;
26e0ca22
LP
380}
381
382/*
383 * vsp1_device_put - Release the VSP1 device
384 *
385 * Decrement the VSP1 reference count and cleanup the device if the last
386 * reference is released.
387 */
388void vsp1_device_put(struct vsp1_device *vsp1)
389{
390 mutex_lock(&vsp1->lock);
391
392 if (--vsp1->ref_count == 0)
4fc78784 393 clk_disable_unprepare(vsp1->clock);
26e0ca22
LP
394
395 mutex_unlock(&vsp1->lock);
396}
397
398/* -----------------------------------------------------------------------------
399 * Power Management
400 */
401
402#ifdef CONFIG_PM_SLEEP
403static int vsp1_pm_suspend(struct device *dev)
404{
405 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
406
407 WARN_ON(mutex_is_locked(&vsp1->lock));
408
409 if (vsp1->ref_count == 0)
410 return 0;
411
139c9286
SF
412 vsp1_pipelines_suspend(vsp1);
413
4fc78784 414 clk_disable_unprepare(vsp1->clock);
139c9286 415
26e0ca22
LP
416 return 0;
417}
418
419static int vsp1_pm_resume(struct device *dev)
420{
421 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
422
423 WARN_ON(mutex_is_locked(&vsp1->lock));
424
139c9286 425 if (vsp1->ref_count == 0)
26e0ca22
LP
426 return 0;
427
139c9286
SF
428 clk_prepare_enable(vsp1->clock);
429
430 vsp1_pipelines_resume(vsp1);
431
432 return 0;
26e0ca22
LP
433}
434#endif
435
436static const struct dev_pm_ops vsp1_pm_ops = {
437 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
438};
439
440/* -----------------------------------------------------------------------------
441 * Platform Driver
442 */
443
32d17597 444static int vsp1_parse_dt(struct vsp1_device *vsp1)
26e0ca22 445{
32d17597
LP
446 struct device_node *np = vsp1->dev->of_node;
447 struct vsp1_platform_data *pdata = &vsp1->pdata;
448
449 if (of_property_read_bool(np, "renesas,has-lif"))
450 pdata->features |= VSP1_HAS_LIF;
451 if (of_property_read_bool(np, "renesas,has-lut"))
452 pdata->features |= VSP1_HAS_LUT;
453 if (of_property_read_bool(np, "renesas,has-sru"))
454 pdata->features |= VSP1_HAS_SRU;
455
456 of_property_read_u32(np, "renesas,#rpf", &pdata->rpf_count);
457 of_property_read_u32(np, "renesas,#uds", &pdata->uds_count);
458 of_property_read_u32(np, "renesas,#wpf", &pdata->wpf_count);
26e0ca22 459
7005a817 460 if (pdata->rpf_count <= 0 || pdata->rpf_count > VSP1_MAX_RPF) {
32d17597 461 dev_err(vsp1->dev, "invalid number of RPF (%u)\n",
26e0ca22 462 pdata->rpf_count);
0b82fb95 463 return -EINVAL;
26e0ca22
LP
464 }
465
7005a817 466 if (pdata->uds_count <= 0 || pdata->uds_count > VSP1_MAX_UDS) {
32d17597 467 dev_err(vsp1->dev, "invalid number of UDS (%u)\n",
26e0ca22 468 pdata->uds_count);
0b82fb95 469 return -EINVAL;
26e0ca22
LP
470 }
471
7005a817 472 if (pdata->wpf_count <= 0 || pdata->wpf_count > VSP1_MAX_WPF) {
32d17597 473 dev_err(vsp1->dev, "invalid number of WPF (%u)\n",
26e0ca22 474 pdata->wpf_count);
0b82fb95 475 return -EINVAL;
26e0ca22
LP
476 }
477
0b82fb95
LP
478 return 0;
479}
480
26e0ca22
LP
481static int vsp1_probe(struct platform_device *pdev)
482{
483 struct vsp1_device *vsp1;
484 struct resource *irq;
485 struct resource *io;
486 int ret;
487
488 vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
489 if (vsp1 == NULL)
490 return -ENOMEM;
491
492 vsp1->dev = &pdev->dev;
493 mutex_init(&vsp1->lock);
494 INIT_LIST_HEAD(&vsp1->entities);
495
32d17597 496 ret = vsp1_parse_dt(vsp1);
0b82fb95
LP
497 if (ret < 0)
498 return ret;
499
26e0ca22
LP
500 /* I/O, IRQ and clock resources */
501 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
502 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
cbec6d3a
MCC
503 if (IS_ERR(vsp1->mmio))
504 return PTR_ERR(vsp1->mmio);
26e0ca22
LP
505
506 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
507 if (IS_ERR(vsp1->clock)) {
508 dev_err(&pdev->dev, "failed to get clock\n");
509 return PTR_ERR(vsp1->clock);
510 }
511
512 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
513 if (!irq) {
514 dev_err(&pdev->dev, "missing IRQ\n");
515 return -EINVAL;
516 }
517
518 ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
519 IRQF_SHARED, dev_name(&pdev->dev), vsp1);
520 if (ret < 0) {
521 dev_err(&pdev->dev, "failed to request IRQ\n");
522 return ret;
523 }
524
525 /* Instanciate entities */
526 ret = vsp1_create_entities(vsp1);
527 if (ret < 0) {
528 dev_err(&pdev->dev, "failed to create entities\n");
529 return ret;
530 }
531
532 platform_set_drvdata(pdev, vsp1);
533
534 return 0;
535}
536
537static int vsp1_remove(struct platform_device *pdev)
538{
539 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
540
541 vsp1_destroy_entities(vsp1);
542
543 return 0;
544}
545
0b82fb95
LP
546static const struct of_device_id vsp1_of_match[] = {
547 { .compatible = "renesas,vsp1" },
548 { },
549};
550
26e0ca22
LP
551static struct platform_driver vsp1_platform_driver = {
552 .probe = vsp1_probe,
553 .remove = vsp1_remove,
554 .driver = {
26e0ca22
LP
555 .name = "vsp1",
556 .pm = &vsp1_pm_ops,
0b82fb95 557 .of_match_table = vsp1_of_match,
26e0ca22
LP
558 },
559};
560
561module_platform_driver(vsp1_platform_driver);
562
563MODULE_ALIAS("vsp1");
564MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
565MODULE_DESCRIPTION("Renesas VSP1 Driver");
566MODULE_LICENSE("GPL");
This page took 0.149331 seconds and 5 git commands to generate.