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