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