v4l: vsp1: Remove unexisting rt clocks
[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-2014 Renesas Electronics 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 /*
333  * vsp1_device_get - Acquire the VSP1 device
334  *
335  * Increment the VSP1 reference count and initialize the device if the first
336  * reference is taken.
337  *
338  * Return a pointer to the VSP1 device or NULL if an error occurred.
339  */
340 struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
341 {
342         struct vsp1_device *__vsp1 = vsp1;
343         int ret;
344
345         mutex_lock(&vsp1->lock);
346         if (vsp1->ref_count > 0)
347                 goto done;
348
349         ret = clk_prepare_enable(vsp1->clock);
350         if (ret < 0) {
351                 __vsp1 = NULL;
352                 goto done;
353         }
354
355         ret = vsp1_device_init(vsp1);
356         if (ret < 0) {
357                 clk_disable_unprepare(vsp1->clock);
358                 __vsp1 = NULL;
359                 goto done;
360         }
361
362 done:
363         if (__vsp1)
364                 vsp1->ref_count++;
365
366         mutex_unlock(&vsp1->lock);
367         return __vsp1;
368 }
369
370 /*
371  * vsp1_device_put - Release the VSP1 device
372  *
373  * Decrement the VSP1 reference count and cleanup the device if the last
374  * reference is released.
375  */
376 void vsp1_device_put(struct vsp1_device *vsp1)
377 {
378         mutex_lock(&vsp1->lock);
379
380         if (--vsp1->ref_count == 0)
381                 clk_disable_unprepare(vsp1->clock);
382
383         mutex_unlock(&vsp1->lock);
384 }
385
386 /* -----------------------------------------------------------------------------
387  * Power Management
388  */
389
390 #ifdef CONFIG_PM_SLEEP
391 static int vsp1_pm_suspend(struct device *dev)
392 {
393         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
394
395         WARN_ON(mutex_is_locked(&vsp1->lock));
396
397         if (vsp1->ref_count == 0)
398                 return 0;
399
400         clk_disable_unprepare(vsp1->clock);
401         return 0;
402 }
403
404 static int vsp1_pm_resume(struct device *dev)
405 {
406         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
407
408         WARN_ON(mutex_is_locked(&vsp1->lock));
409
410         if (vsp1->ref_count)
411                 return 0;
412
413         return clk_prepare_enable(vsp1->clock);
414 }
415 #endif
416
417 static const struct dev_pm_ops vsp1_pm_ops = {
418         SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
419 };
420
421 /* -----------------------------------------------------------------------------
422  * Platform Driver
423  */
424
425 static struct vsp1_platform_data *
426 vsp1_get_platform_data(struct platform_device *pdev)
427 {
428         struct vsp1_platform_data *pdata = pdev->dev.platform_data;
429
430         if (pdata == NULL) {
431                 dev_err(&pdev->dev, "missing platform data\n");
432                 return NULL;
433         }
434
435         if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
436                 dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
437                         pdata->rpf_count);
438                 return NULL;
439         }
440
441         if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
442                 dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
443                         pdata->uds_count);
444                 return NULL;
445         }
446
447         if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
448                 dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
449                         pdata->wpf_count);
450                 return NULL;
451         }
452
453         return pdata;
454 }
455
456 static int vsp1_probe(struct platform_device *pdev)
457 {
458         struct vsp1_device *vsp1;
459         struct resource *irq;
460         struct resource *io;
461         int ret;
462
463         vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
464         if (vsp1 == NULL)
465                 return -ENOMEM;
466
467         vsp1->dev = &pdev->dev;
468         mutex_init(&vsp1->lock);
469         INIT_LIST_HEAD(&vsp1->entities);
470
471         vsp1->pdata = vsp1_get_platform_data(pdev);
472         if (vsp1->pdata == NULL)
473                 return -ENODEV;
474
475         /* I/O, IRQ and clock resources */
476         io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
477         vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
478         if (IS_ERR(vsp1->mmio))
479                 return PTR_ERR(vsp1->mmio);
480
481         vsp1->clock = devm_clk_get(&pdev->dev, NULL);
482         if (IS_ERR(vsp1->clock)) {
483                 dev_err(&pdev->dev, "failed to get clock\n");
484                 return PTR_ERR(vsp1->clock);
485         }
486
487         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
488         if (!irq) {
489                 dev_err(&pdev->dev, "missing IRQ\n");
490                 return -EINVAL;
491         }
492
493         ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
494                               IRQF_SHARED, dev_name(&pdev->dev), vsp1);
495         if (ret < 0) {
496                 dev_err(&pdev->dev, "failed to request IRQ\n");
497                 return ret;
498         }
499
500         /* Instanciate entities */
501         ret = vsp1_create_entities(vsp1);
502         if (ret < 0) {
503                 dev_err(&pdev->dev, "failed to create entities\n");
504                 return ret;
505         }
506
507         platform_set_drvdata(pdev, vsp1);
508
509         return 0;
510 }
511
512 static int vsp1_remove(struct platform_device *pdev)
513 {
514         struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
515
516         vsp1_destroy_entities(vsp1);
517
518         return 0;
519 }
520
521 static struct platform_driver vsp1_platform_driver = {
522         .probe          = vsp1_probe,
523         .remove         = vsp1_remove,
524         .driver         = {
525                 .owner  = THIS_MODULE,
526                 .name   = "vsp1",
527                 .pm     = &vsp1_pm_ops,
528         },
529 };
530
531 module_platform_driver(vsp1_platform_driver);
532
533 MODULE_ALIAS("vsp1");
534 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
535 MODULE_DESCRIPTION("Renesas VSP1 Driver");
536 MODULE_LICENSE("GPL");