]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/media/platform/xilinx/xilinx-vipp.c
b9a1acabbdae543c2bd28c7004ce6c5ee611d576
[zynq/linux.git] / drivers / media / platform / xilinx / xilinx-vipp.c
1 /*
2  * Xilinx Video IP Composite Device
3  *
4  * Copyright (C) 2013-2015 Ideas on Board
5  * Copyright (C) 2013-2015 Xilinx, Inc.
6  *
7  * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
8  *           Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/of_graph.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-fwnode.h>
26
27 #include "xilinx-dma.h"
28 #include "xilinx-vipp.h"
29
30 #define XVIPP_DMA_S2MM                          0
31 #define XVIPP_DMA_MM2S                          1
32
33 /*
34  * This is for backward compatibility for existing applications,
35  * and planned to be deprecated
36  */
37 static bool xvip_is_mplane = true;
38 MODULE_PARM_DESC(is_mplane,
39                  "v4l2 device capability to handle multi planar formats");
40 module_param_named(is_mplane, xvip_is_mplane, bool, 0444);
41
42 /**
43  * struct xvip_graph_entity - Entity in the video graph
44  * @list: list entry in a graph entities list
45  * @node: the entity's DT node
46  * @entity: media entity, from the corresponding V4L2 subdev
47  * @asd: subdev asynchronous registration information
48  * @subdev: V4L2 subdev
49  * @streaming: status of the V4L2 subdev if streaming or not
50  */
51 struct xvip_graph_entity {
52         struct list_head list;
53         struct device_node *node;
54         struct media_entity *entity;
55
56         struct v4l2_async_subdev asd;
57         struct v4l2_subdev *subdev;
58         bool streaming;
59 };
60
61 /* -----------------------------------------------------------------------------
62  * Graph Management
63  */
64
65 static struct xvip_graph_entity *
66 xvip_graph_find_entity(struct xvip_composite_device *xdev,
67                        const struct device_node *node)
68 {
69         struct xvip_graph_entity *entity;
70
71         list_for_each_entry(entity, &xdev->entities, list) {
72                 if (entity->node == node)
73                         return entity;
74         }
75
76         return NULL;
77 }
78
79 static int xvip_graph_build_one(struct xvip_composite_device *xdev,
80                                 struct xvip_graph_entity *entity)
81 {
82         u32 link_flags = MEDIA_LNK_FL_ENABLED;
83         struct media_entity *local = entity->entity;
84         struct media_entity *remote;
85         struct media_pad *local_pad;
86         struct media_pad *remote_pad;
87         struct xvip_graph_entity *ent;
88         struct v4l2_fwnode_link link;
89         struct device_node *ep = NULL;
90         struct device_node *next;
91         int ret = 0;
92
93         dev_dbg(xdev->dev, "creating links for entity %s\n", local->name);
94
95         while (1) {
96                 /* Get the next endpoint and parse its link. */
97                 next = of_graph_get_next_endpoint(entity->node, ep);
98                 if (next == NULL)
99                         break;
100
101                 of_node_put(ep);
102                 ep = next;
103
104                 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
105
106                 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
107                 if (ret < 0) {
108                         dev_err(xdev->dev, "failed to parse link for %pOF\n",
109                                 ep);
110                         continue;
111                 }
112
113                 /* Skip sink ports, they will be processed from the other end of
114                  * the link.
115                  */
116                 if (link.local_port >= local->num_pads) {
117                         dev_err(xdev->dev, "invalid port number %u for %pOF\n",
118                                 link.local_port,
119                                 to_of_node(link.local_node));
120                         v4l2_fwnode_put_link(&link);
121                         ret = -EINVAL;
122                         break;
123                 }
124
125                 local_pad = &local->pads[link.local_port];
126
127                 if (local_pad->flags & MEDIA_PAD_FL_SINK) {
128                         dev_dbg(xdev->dev, "skipping sink port %pOF:%u\n",
129                                 to_of_node(link.local_node),
130                                 link.local_port);
131                         v4l2_fwnode_put_link(&link);
132                         continue;
133                 }
134
135                 /* Skip DMA engines, they will be processed separately. */
136                 if (link.remote_node == of_fwnode_handle(xdev->dev->of_node)) {
137                         dev_dbg(xdev->dev, "skipping DMA port %pOF:%u\n",
138                                 to_of_node(link.local_node),
139                                 link.local_port);
140                         v4l2_fwnode_put_link(&link);
141                         continue;
142                 }
143
144                 /* Find the remote entity. */
145                 ent = xvip_graph_find_entity(xdev,
146                                              to_of_node(link.remote_node));
147                 if (ent == NULL) {
148                         dev_err(xdev->dev, "no entity found for %pOF\n",
149                                 to_of_node(link.remote_node));
150                         v4l2_fwnode_put_link(&link);
151                         ret = -ENODEV;
152                         break;
153                 }
154
155                 remote = ent->entity;
156
157                 if (link.remote_port >= remote->num_pads) {
158                         dev_err(xdev->dev, "invalid port number %u on %pOF\n",
159                                 link.remote_port, to_of_node(link.remote_node));
160                         v4l2_fwnode_put_link(&link);
161                         ret = -EINVAL;
162                         break;
163                 }
164
165                 remote_pad = &remote->pads[link.remote_port];
166
167                 v4l2_fwnode_put_link(&link);
168
169                 /* Create the media link. */
170                 dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
171                         local->name, local_pad->index,
172                         remote->name, remote_pad->index);
173
174                 ret = media_create_pad_link(local, local_pad->index,
175                                                remote, remote_pad->index,
176                                                link_flags);
177                 if (ret < 0) {
178                         dev_err(xdev->dev,
179                                 "failed to create %s:%u -> %s:%u link\n",
180                                 local->name, local_pad->index,
181                                 remote->name, remote_pad->index);
182                         break;
183                 }
184         }
185
186         of_node_put(ep);
187         return ret;
188 }
189
190 static struct xvip_dma *
191 xvip_graph_find_dma(struct xvip_composite_device *xdev, unsigned int port)
192 {
193         struct xvip_dma *dma;
194
195         list_for_each_entry(dma, &xdev->dmas, list) {
196                 if (dma->port == port)
197                         return dma;
198         }
199
200         return NULL;
201 }
202
203 /**
204  * xvip_subdev_set_streaming - Find and update streaming status of subdev
205  * @xdev: Composite video device
206  * @subdev: V4L2 sub-device
207  * @enable: enable/disable streaming status
208  *
209  * Walk the xvip graph entities list and find if subdev is present. Returns
210  * streaming status of subdev and update the status as requested
211  *
212  * Return: streaming status (true or false) if successful or warn_on if subdev
213  * is not present and return false
214  */
215 bool xvip_subdev_set_streaming(struct xvip_composite_device *xdev,
216                                struct v4l2_subdev *subdev, bool enable)
217 {
218         struct xvip_graph_entity *entity;
219
220         list_for_each_entry(entity, &xdev->entities, list)
221                 if (entity->node == subdev->dev->of_node) {
222                         bool status = entity->streaming;
223
224                         entity->streaming = enable;
225                         return status;
226                 }
227
228         WARN(1, "Should never get here\n");
229         return false;
230 }
231
232 static int xvip_graph_build_dma(struct xvip_composite_device *xdev)
233 {
234         u32 link_flags = MEDIA_LNK_FL_ENABLED;
235         struct device_node *node = xdev->dev->of_node;
236         struct media_entity *source;
237         struct media_entity *sink;
238         struct media_pad *source_pad;
239         struct media_pad *sink_pad;
240         struct xvip_graph_entity *ent;
241         struct v4l2_fwnode_link link;
242         struct device_node *ep = NULL;
243         struct device_node *next;
244         struct xvip_dma *dma;
245         int ret = 0;
246
247         dev_dbg(xdev->dev, "creating links for DMA engines\n");
248
249         while (1) {
250                 /* Get the next endpoint and parse its link. */
251                 next = of_graph_get_next_endpoint(node, ep);
252                 if (next == NULL)
253                         break;
254
255                 of_node_put(ep);
256                 ep = next;
257
258                 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
259
260                 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
261                 if (ret < 0) {
262                         dev_err(xdev->dev, "failed to parse link for %pOF\n",
263                                 ep);
264                         continue;
265                 }
266
267                 /* Find the DMA engine. */
268                 dma = xvip_graph_find_dma(xdev, link.local_port);
269                 if (dma == NULL) {
270                         dev_err(xdev->dev, "no DMA engine found for port %u\n",
271                                 link.local_port);
272                         v4l2_fwnode_put_link(&link);
273                         ret = -EINVAL;
274                         break;
275                 }
276
277                 dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
278                         dma->video.name);
279
280                 /* Find the remote entity. */
281                 ent = xvip_graph_find_entity(xdev,
282                                              to_of_node(link.remote_node));
283                 if (ent == NULL) {
284                         dev_err(xdev->dev, "no entity found for %pOF\n",
285                                 to_of_node(link.remote_node));
286                         v4l2_fwnode_put_link(&link);
287                         ret = -ENODEV;
288                         break;
289                 }
290
291                 if (link.remote_port >= ent->entity->num_pads) {
292                         dev_err(xdev->dev, "invalid port number %u on %pOF\n",
293                                 link.remote_port,
294                                 to_of_node(link.remote_node));
295                         v4l2_fwnode_put_link(&link);
296                         ret = -EINVAL;
297                         break;
298                 }
299
300                 if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
301                         source = &dma->video.entity;
302                         source_pad = &dma->pad;
303                         sink = ent->entity;
304                         sink_pad = &sink->pads[link.remote_port];
305                 } else {
306                         source = ent->entity;
307                         source_pad = &source->pads[link.remote_port];
308                         sink = &dma->video.entity;
309                         sink_pad = &dma->pad;
310                 }
311
312                 v4l2_fwnode_put_link(&link);
313
314                 /* Create the media link. */
315                 dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
316                         source->name, source_pad->index,
317                         sink->name, sink_pad->index);
318
319                 ret = media_create_pad_link(source, source_pad->index,
320                                                sink, sink_pad->index,
321                                                link_flags);
322                 if (ret < 0) {
323                         dev_err(xdev->dev,
324                                 "failed to create %s:%u -> %s:%u link\n",
325                                 source->name, source_pad->index,
326                                 sink->name, sink_pad->index);
327                         break;
328                 }
329         }
330
331         of_node_put(ep);
332         return ret;
333 }
334
335 static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
336 {
337         struct xvip_composite_device *xdev =
338                 container_of(notifier, struct xvip_composite_device, notifier);
339         struct xvip_graph_entity *entity;
340         int ret;
341
342         dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
343
344         /* Create links for every entity. */
345         list_for_each_entry(entity, &xdev->entities, list) {
346                 ret = xvip_graph_build_one(xdev, entity);
347                 if (ret < 0)
348                         return ret;
349         }
350
351         /* Create links for DMA channels. */
352         ret = xvip_graph_build_dma(xdev);
353         if (ret < 0)
354                 return ret;
355
356         ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
357         if (ret < 0)
358                 dev_err(xdev->dev, "failed to register subdev nodes\n");
359
360         return media_device_register(&xdev->media_dev);
361 }
362
363 static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
364                                    struct v4l2_subdev *subdev,
365                                    struct v4l2_async_subdev *asd)
366 {
367         struct xvip_composite_device *xdev =
368                 container_of(notifier, struct xvip_composite_device, notifier);
369         struct xvip_graph_entity *entity;
370
371         /* Locate the entity corresponding to the bound subdev and store the
372          * subdev pointer.
373          */
374         list_for_each_entry(entity, &xdev->entities, list) {
375                 if (entity->node != subdev->dev->of_node)
376                         continue;
377
378                 if (entity->subdev) {
379                         dev_err(xdev->dev, "duplicate subdev for node %pOF\n",
380                                 entity->node);
381                         return -EINVAL;
382                 }
383
384                 dev_dbg(xdev->dev, "subdev %s bound\n", subdev->name);
385                 entity->entity = &subdev->entity;
386                 entity->subdev = subdev;
387                 return 0;
388         }
389
390         dev_err(xdev->dev, "no entity for subdev %s\n", subdev->name);
391         return -EINVAL;
392 }
393
394 static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
395                                 struct device_node *node)
396 {
397         struct xvip_graph_entity *entity;
398         struct device_node *remote;
399         struct device_node *ep = NULL;
400         int ret = 0;
401
402         dev_dbg(xdev->dev, "parsing node %pOF\n", node);
403
404         while (1) {
405                 ep = of_graph_get_next_endpoint(node, ep);
406                 if (ep == NULL)
407                         break;
408
409                 dev_dbg(xdev->dev, "handling endpoint %pOF\n", ep);
410
411                 remote = of_graph_get_remote_port_parent(ep);
412                 if (remote == NULL) {
413                         ret = -EINVAL;
414                         break;
415                 }
416
417                 /* Skip entities that we have already processed. */
418                 if (remote == xdev->dev->of_node ||
419                     xvip_graph_find_entity(xdev, remote)) {
420                         of_node_put(remote);
421                         continue;
422                 }
423
424                 entity = devm_kzalloc(xdev->dev, sizeof(*entity), GFP_KERNEL);
425                 if (entity == NULL) {
426                         of_node_put(remote);
427                         ret = -ENOMEM;
428                         break;
429                 }
430
431                 entity->node = remote;
432                 entity->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
433                 entity->asd.match.fwnode.fwnode = of_fwnode_handle(remote);
434                 list_add_tail(&entity->list, &xdev->entities);
435                 xdev->num_subdevs++;
436         }
437
438         of_node_put(ep);
439         return ret;
440 }
441
442 static int xvip_graph_parse(struct xvip_composite_device *xdev)
443 {
444         struct xvip_graph_entity *entity;
445         int ret;
446
447         /*
448          * Walk the links to parse the full graph. Start by parsing the
449          * composite node and then parse entities in turn. The list_for_each
450          * loop will handle entities added at the end of the list while walking
451          * the links.
452          */
453         ret = xvip_graph_parse_one(xdev, xdev->dev->of_node);
454         if (ret < 0)
455                 return 0;
456
457         list_for_each_entry(entity, &xdev->entities, list) {
458                 ret = xvip_graph_parse_one(xdev, entity->node);
459                 if (ret < 0)
460                         break;
461         }
462
463         return ret;
464 }
465
466 static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
467                                    struct device_node *node)
468 {
469         struct xvip_dma *dma;
470         enum v4l2_buf_type type;
471         const char *direction;
472         unsigned int index;
473         int ret;
474
475         ret = of_property_read_string(node, "direction", &direction);
476         if (ret < 0)
477                 return ret;
478
479         if (strcmp(direction, "input") == 0)
480                 type = xvip_is_mplane ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
481                                                 V4L2_BUF_TYPE_VIDEO_CAPTURE;
482         else if (strcmp(direction, "output") == 0)
483                 type = xvip_is_mplane ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
484                                         V4L2_BUF_TYPE_VIDEO_OUTPUT;
485         else
486                 return -EINVAL;
487
488         of_property_read_u32(node, "reg", &index);
489
490         dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
491         if (dma == NULL)
492                 return -ENOMEM;
493
494         ret = xvip_dma_init(xdev, dma, type, index);
495         if (ret < 0) {
496                 dev_err(xdev->dev, "%pOF initialization failed\n", node);
497                 return ret;
498         }
499
500         list_add_tail(&dma->list, &xdev->dmas);
501
502         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
503                 xdev->v4l2_caps |= V4L2_CAP_VIDEO_CAPTURE_MPLANE;
504         else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
505                 xdev->v4l2_caps |= V4L2_CAP_VIDEO_CAPTURE;
506         else if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
507                 xdev->v4l2_caps |= V4L2_CAP_VIDEO_OUTPUT;
508         else if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
509                 xdev->v4l2_caps |= V4L2_CAP_VIDEO_OUTPUT_MPLANE;
510
511         return 0;
512 }
513
514 static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
515 {
516         struct device_node *ports;
517         struct device_node *port;
518         int ret;
519
520         ports = of_get_child_by_name(xdev->dev->of_node, "ports");
521         if (ports == NULL) {
522                 dev_err(xdev->dev, "ports node not present\n");
523                 return -EINVAL;
524         }
525
526         for_each_child_of_node(ports, port) {
527                 ret = xvip_graph_dma_init_one(xdev, port);
528                 if (ret < 0) {
529                         of_node_put(port);
530                         return ret;
531                 }
532         }
533
534         return 0;
535 }
536
537 static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
538 {
539         struct xvip_graph_entity *entityp;
540         struct xvip_graph_entity *entity;
541         struct xvip_dma *dmap;
542         struct xvip_dma *dma;
543
544         v4l2_async_notifier_unregister(&xdev->notifier);
545
546         list_for_each_entry_safe(entity, entityp, &xdev->entities, list) {
547                 of_node_put(entity->node);
548                 list_del(&entity->list);
549         }
550
551         list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
552                 xvip_dma_cleanup(dma);
553                 list_del(&dma->list);
554         }
555 }
556
557 static int xvip_graph_init(struct xvip_composite_device *xdev)
558 {
559         struct xvip_graph_entity *entity;
560         struct v4l2_async_subdev **subdevs = NULL;
561         unsigned int num_subdevs;
562         unsigned int i;
563         int ret;
564
565         /* Init the DMA channels. */
566         ret = xvip_graph_dma_init(xdev);
567         if (ret < 0) {
568                 dev_err(xdev->dev, "DMA initialization failed\n");
569                 goto done;
570         }
571
572         /* Parse the graph to extract a list of subdevice DT nodes. */
573         ret = xvip_graph_parse(xdev);
574         if (ret < 0) {
575                 dev_err(xdev->dev, "graph parsing failed\n");
576                 goto done;
577         }
578
579         if (!xdev->num_subdevs) {
580                 dev_err(xdev->dev, "no subdev found in graph\n");
581                 goto done;
582         }
583
584         /* Register the subdevices notifier. */
585         num_subdevs = xdev->num_subdevs;
586         subdevs = devm_kzalloc(xdev->dev, sizeof(*subdevs) * num_subdevs,
587                                GFP_KERNEL);
588         if (subdevs == NULL) {
589                 ret = -ENOMEM;
590                 goto done;
591         }
592
593         i = 0;
594         list_for_each_entry(entity, &xdev->entities, list)
595                 subdevs[i++] = &entity->asd;
596
597         xdev->notifier.subdevs = subdevs;
598         xdev->notifier.num_subdevs = num_subdevs;
599         xdev->notifier.bound = xvip_graph_notify_bound;
600         xdev->notifier.complete = xvip_graph_notify_complete;
601
602         ret = v4l2_async_notifier_register(&xdev->v4l2_dev, &xdev->notifier);
603         if (ret < 0) {
604                 dev_err(xdev->dev, "notifier registration failed\n");
605                 goto done;
606         }
607
608         ret = 0;
609
610 done:
611         if (ret < 0)
612                 xvip_graph_cleanup(xdev);
613
614         return ret;
615 }
616
617 /* -----------------------------------------------------------------------------
618  * Media Controller and V4L2
619  */
620
621 static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
622 {
623         v4l2_device_unregister(&xdev->v4l2_dev);
624         media_device_unregister(&xdev->media_dev);
625         media_device_cleanup(&xdev->media_dev);
626 }
627
628 static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
629 {
630         int ret;
631
632         xdev->media_dev.dev = xdev->dev;
633         strlcpy(xdev->media_dev.model, "Xilinx Video Composite Device",
634                 sizeof(xdev->media_dev.model));
635         xdev->media_dev.hw_revision = 0;
636
637         media_device_init(&xdev->media_dev);
638
639         xdev->v4l2_dev.mdev = &xdev->media_dev;
640         ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
641         if (ret < 0) {
642                 dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
643                         ret);
644                 media_device_cleanup(&xdev->media_dev);
645                 return ret;
646         }
647
648         return 0;
649 }
650
651 /* -----------------------------------------------------------------------------
652  * Platform Device Driver
653  */
654
655 static int xvip_composite_probe(struct platform_device *pdev)
656 {
657         struct xvip_composite_device *xdev;
658         int ret;
659
660         xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
661         if (!xdev)
662                 return -ENOMEM;
663
664         xdev->dev = &pdev->dev;
665         mutex_init(&xdev->lock);
666         INIT_LIST_HEAD(&xdev->entities);
667         INIT_LIST_HEAD(&xdev->dmas);
668
669         ret = xvip_composite_v4l2_init(xdev);
670         if (ret < 0)
671                 return ret;
672
673         ret = xvip_graph_init(xdev);
674         if (ret < 0)
675                 goto error;
676
677         platform_set_drvdata(pdev, xdev);
678
679         dev_info(xdev->dev, "device registered\n");
680
681         return 0;
682
683 error:
684         xvip_composite_v4l2_cleanup(xdev);
685         return ret;
686 }
687
688 static int xvip_composite_remove(struct platform_device *pdev)
689 {
690         struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
691
692         mutex_destroy(&xdev->lock);
693         xvip_graph_cleanup(xdev);
694         xvip_composite_v4l2_cleanup(xdev);
695
696         return 0;
697 }
698
699 static const struct of_device_id xvip_composite_of_id_table[] = {
700         { .compatible = "xlnx,video" },
701         { }
702 };
703 MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
704
705 static struct platform_driver xvip_composite_driver = {
706         .driver = {
707                 .name = "xilinx-video",
708                 .of_match_table = xvip_composite_of_id_table,
709         },
710         .probe = xvip_composite_probe,
711         .remove = xvip_composite_remove,
712 };
713
714 module_platform_driver(xvip_composite_driver);
715
716 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
717 MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
718 MODULE_LICENSE("GPL v2");