Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.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_lif.h"
24 #include "vsp1_rwpf.h"
25 #include "vsp1_uds.h"
26
27 /* -----------------------------------------------------------------------------
28  * Interrupt Handling
29  */
30
31 static 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
38         for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
39                 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  */
73 static 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;
104
105                         if (flags & MEDIA_LNK_FL_ENABLED)
106                                 source->sink = entity;
107                 }
108         }
109
110         return 0;
111 }
112
113 static 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
127 static 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));
137         snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
138                  dev_name(mdev->dev));
139         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
233 done:
234         if (ret < 0)
235                 vsp1_destroy_entities(vsp1);
236
237         return ret;
238 }
239
240 static 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
248         for (i = 0; i < vsp1->pdata->wpf_count; ++i) {
249                 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
272         for (i = 0; i < vsp1->pdata->rpf_count; ++i)
273                 vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
274
275         for (i = 0; i < vsp1->pdata->uds_count; ++i)
276                 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 static int vsp1_clocks_enable(struct vsp1_device *vsp1)
294 {
295         int ret;
296
297         ret = clk_prepare_enable(vsp1->clock);
298         if (ret < 0)
299                 return ret;
300
301         if (IS_ERR(vsp1->rt_clock))
302                 return 0;
303
304         ret = clk_prepare_enable(vsp1->rt_clock);
305         if (ret < 0) {
306                 clk_disable_unprepare(vsp1->clock);
307                 return ret;
308         }
309
310         return 0;
311 }
312
313 static void vsp1_clocks_disable(struct vsp1_device *vsp1)
314 {
315         if (!IS_ERR(vsp1->rt_clock))
316                 clk_disable_unprepare(vsp1->rt_clock);
317         clk_disable_unprepare(vsp1->clock);
318 }
319
320 /*
321  * vsp1_device_get - Acquire the VSP1 device
322  *
323  * Increment the VSP1 reference count and initialize the device if the first
324  * reference is taken.
325  *
326  * Return a pointer to the VSP1 device or NULL if an error occurred.
327  */
328 struct vsp1_device *vsp1_device_get(struct vsp1_device *vsp1)
329 {
330         struct vsp1_device *__vsp1 = vsp1;
331         int ret;
332
333         mutex_lock(&vsp1->lock);
334         if (vsp1->ref_count > 0)
335                 goto done;
336
337         ret = vsp1_clocks_enable(vsp1);
338         if (ret < 0) {
339                 __vsp1 = NULL;
340                 goto done;
341         }
342
343         ret = vsp1_device_init(vsp1);
344         if (ret < 0) {
345                 vsp1_clocks_disable(vsp1);
346                 __vsp1 = NULL;
347                 goto done;
348         }
349
350 done:
351         if (__vsp1)
352                 vsp1->ref_count++;
353
354         mutex_unlock(&vsp1->lock);
355         return __vsp1;
356 }
357
358 /*
359  * vsp1_device_put - Release the VSP1 device
360  *
361  * Decrement the VSP1 reference count and cleanup the device if the last
362  * reference is released.
363  */
364 void vsp1_device_put(struct vsp1_device *vsp1)
365 {
366         mutex_lock(&vsp1->lock);
367
368         if (--vsp1->ref_count == 0)
369                 vsp1_clocks_disable(vsp1);
370
371         mutex_unlock(&vsp1->lock);
372 }
373
374 /* -----------------------------------------------------------------------------
375  * Power Management
376  */
377
378 #ifdef CONFIG_PM_SLEEP
379 static int vsp1_pm_suspend(struct device *dev)
380 {
381         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
382
383         WARN_ON(mutex_is_locked(&vsp1->lock));
384
385         if (vsp1->ref_count == 0)
386                 return 0;
387
388         vsp1_clocks_disable(vsp1);
389         return 0;
390 }
391
392 static int vsp1_pm_resume(struct device *dev)
393 {
394         struct vsp1_device *vsp1 = dev_get_drvdata(dev);
395
396         WARN_ON(mutex_is_locked(&vsp1->lock));
397
398         if (vsp1->ref_count)
399                 return 0;
400
401         return vsp1_clocks_enable(vsp1);
402 }
403 #endif
404
405 static const struct dev_pm_ops vsp1_pm_ops = {
406         SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
407 };
408
409 /* -----------------------------------------------------------------------------
410  * Platform Driver
411  */
412
413 static struct vsp1_platform_data *
414 vsp1_get_platform_data(struct platform_device *pdev)
415 {
416         struct vsp1_platform_data *pdata = pdev->dev.platform_data;
417
418         if (pdata == NULL) {
419                 dev_err(&pdev->dev, "missing platform data\n");
420                 return NULL;
421         }
422
423         if (pdata->rpf_count <= 0 || pdata->rpf_count > VPS1_MAX_RPF) {
424                 dev_err(&pdev->dev, "invalid number of RPF (%u)\n",
425                         pdata->rpf_count);
426                 return NULL;
427         }
428
429         if (pdata->uds_count <= 0 || pdata->uds_count > VPS1_MAX_UDS) {
430                 dev_err(&pdev->dev, "invalid number of UDS (%u)\n",
431                         pdata->uds_count);
432                 return NULL;
433         }
434
435         if (pdata->wpf_count <= 0 || pdata->wpf_count > VPS1_MAX_WPF) {
436                 dev_err(&pdev->dev, "invalid number of WPF (%u)\n",
437                         pdata->wpf_count);
438                 return NULL;
439         }
440
441         return pdata;
442 }
443
444 static int vsp1_probe(struct platform_device *pdev)
445 {
446         struct vsp1_device *vsp1;
447         struct resource *irq;
448         struct resource *io;
449         int ret;
450
451         vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
452         if (vsp1 == NULL)
453                 return -ENOMEM;
454
455         vsp1->dev = &pdev->dev;
456         mutex_init(&vsp1->lock);
457         INIT_LIST_HEAD(&vsp1->entities);
458
459         vsp1->pdata = vsp1_get_platform_data(pdev);
460         if (vsp1->pdata == NULL)
461                 return -ENODEV;
462
463         /* I/O, IRQ and clock resources */
464         io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
465         vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
466         if (IS_ERR(vsp1->mmio))
467                 return PTR_ERR(vsp1->mmio);
468
469         vsp1->clock = devm_clk_get(&pdev->dev, NULL);
470         if (IS_ERR(vsp1->clock)) {
471                 dev_err(&pdev->dev, "failed to get clock\n");
472                 return PTR_ERR(vsp1->clock);
473         }
474
475         /* The RT clock is optional */
476         vsp1->rt_clock = devm_clk_get(&pdev->dev, "rt");
477
478         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
479         if (!irq) {
480                 dev_err(&pdev->dev, "missing IRQ\n");
481                 return -EINVAL;
482         }
483
484         ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
485                               IRQF_SHARED, dev_name(&pdev->dev), vsp1);
486         if (ret < 0) {
487                 dev_err(&pdev->dev, "failed to request IRQ\n");
488                 return ret;
489         }
490
491         /* Instanciate entities */
492         ret = vsp1_create_entities(vsp1);
493         if (ret < 0) {
494                 dev_err(&pdev->dev, "failed to create entities\n");
495                 return ret;
496         }
497
498         platform_set_drvdata(pdev, vsp1);
499
500         return 0;
501 }
502
503 static int vsp1_remove(struct platform_device *pdev)
504 {
505         struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
506
507         vsp1_destroy_entities(vsp1);
508
509         return 0;
510 }
511
512 static struct platform_driver vsp1_platform_driver = {
513         .probe          = vsp1_probe,
514         .remove         = vsp1_remove,
515         .driver         = {
516                 .owner  = THIS_MODULE,
517                 .name   = "vsp1",
518                 .pm     = &vsp1_pm_ops,
519         },
520 };
521
522 module_platform_driver(vsp1_platform_driver);
523
524 MODULE_ALIAS("vsp1");
525 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
526 MODULE_DESCRIPTION("Renesas VSP1 Driver");
527 MODULE_LICENSE("GPL");