2 * Xilinx Video IP Composite Device
4 * Copyright (C) 2013-2015 Ideas on Board
5 * Copyright (C) 2013-2015 Xilinx, Inc.
7 * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
8 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
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.
15 #include <linux/list.h>
16 #include <linux/module.h>
18 #include <linux/of_graph.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-fwnode.h>
27 #include "xilinx-dma.h"
28 #include "xilinx-vipp.h"
30 #define XVIPP_DMA_S2MM 0
31 #define XVIPP_DMA_MM2S 1
34 * This is for backward compatibility for existing applications,
35 * and planned to be deprecated
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);
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
51 struct xvip_graph_entity {
52 struct list_head list;
53 struct device_node *node;
54 struct media_entity *entity;
56 struct v4l2_async_subdev asd;
57 struct v4l2_subdev *subdev;
61 /* -----------------------------------------------------------------------------
65 static struct xvip_graph_entity *
66 xvip_graph_find_entity(struct xvip_composite_device *xdev,
67 const struct device_node *node)
69 struct xvip_graph_entity *entity;
71 list_for_each_entry(entity, &xdev->entities, list) {
72 if (entity->node == node)
79 static int xvip_graph_build_one(struct xvip_composite_device *xdev,
80 struct xvip_graph_entity *entity)
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;
93 dev_dbg(xdev->dev, "creating links for entity %s\n", local->name);
96 /* Get the next endpoint and parse its link. */
97 next = of_graph_get_next_endpoint(entity->node, ep);
104 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
106 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
108 dev_err(xdev->dev, "failed to parse link for %pOF\n",
113 /* Skip sink ports, they will be processed from the other end of
116 if (link.local_port >= local->num_pads) {
117 dev_err(xdev->dev, "invalid port number %u for %pOF\n",
119 to_of_node(link.local_node));
120 v4l2_fwnode_put_link(&link);
125 local_pad = &local->pads[link.local_port];
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),
131 v4l2_fwnode_put_link(&link);
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),
140 v4l2_fwnode_put_link(&link);
144 /* Find the remote entity. */
145 ent = xvip_graph_find_entity(xdev,
146 to_of_node(link.remote_node));
148 dev_err(xdev->dev, "no entity found for %pOF\n",
149 to_of_node(link.remote_node));
150 v4l2_fwnode_put_link(&link);
155 remote = ent->entity;
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);
165 remote_pad = &remote->pads[link.remote_port];
167 v4l2_fwnode_put_link(&link);
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);
174 ret = media_create_pad_link(local, local_pad->index,
175 remote, remote_pad->index,
179 "failed to create %s:%u -> %s:%u link\n",
180 local->name, local_pad->index,
181 remote->name, remote_pad->index);
190 static struct xvip_dma *
191 xvip_graph_find_dma(struct xvip_composite_device *xdev, unsigned int port)
193 struct xvip_dma *dma;
195 list_for_each_entry(dma, &xdev->dmas, list) {
196 if (dma->port == port)
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
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
212 * Return: streaming status (true or false) if successful or warn_on if subdev
213 * is not present and return false
215 bool xvip_subdev_set_streaming(struct xvip_composite_device *xdev,
216 struct v4l2_subdev *subdev, bool enable)
218 struct xvip_graph_entity *entity;
220 list_for_each_entry(entity, &xdev->entities, list)
221 if (entity->node == subdev->dev->of_node) {
222 bool status = entity->streaming;
224 entity->streaming = enable;
228 WARN(1, "Should never get here\n");
232 static int xvip_graph_build_dma(struct xvip_composite_device *xdev)
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;
247 dev_dbg(xdev->dev, "creating links for DMA engines\n");
250 /* Get the next endpoint and parse its link. */
251 next = of_graph_get_next_endpoint(node, ep);
258 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
260 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
262 dev_err(xdev->dev, "failed to parse link for %pOF\n",
267 /* Find the DMA engine. */
268 dma = xvip_graph_find_dma(xdev, link.local_port);
270 dev_err(xdev->dev, "no DMA engine found for port %u\n",
272 v4l2_fwnode_put_link(&link);
277 dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
280 /* Find the remote entity. */
281 ent = xvip_graph_find_entity(xdev,
282 to_of_node(link.remote_node));
284 dev_err(xdev->dev, "no entity found for %pOF\n",
285 to_of_node(link.remote_node));
286 v4l2_fwnode_put_link(&link);
291 if (link.remote_port >= ent->entity->num_pads) {
292 dev_err(xdev->dev, "invalid port number %u on %pOF\n",
294 to_of_node(link.remote_node));
295 v4l2_fwnode_put_link(&link);
300 if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
301 source = &dma->video.entity;
302 source_pad = &dma->pad;
304 sink_pad = &sink->pads[link.remote_port];
306 source = ent->entity;
307 source_pad = &source->pads[link.remote_port];
308 sink = &dma->video.entity;
309 sink_pad = &dma->pad;
312 v4l2_fwnode_put_link(&link);
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);
319 ret = media_create_pad_link(source, source_pad->index,
320 sink, sink_pad->index,
324 "failed to create %s:%u -> %s:%u link\n",
325 source->name, source_pad->index,
326 sink->name, sink_pad->index);
335 static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
337 struct xvip_composite_device *xdev =
338 container_of(notifier, struct xvip_composite_device, notifier);
339 struct xvip_graph_entity *entity;
342 dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
344 /* Create links for every entity. */
345 list_for_each_entry(entity, &xdev->entities, list) {
346 ret = xvip_graph_build_one(xdev, entity);
351 /* Create links for DMA channels. */
352 ret = xvip_graph_build_dma(xdev);
356 ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
358 dev_err(xdev->dev, "failed to register subdev nodes\n");
360 return media_device_register(&xdev->media_dev);
363 static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
364 struct v4l2_subdev *subdev,
365 struct v4l2_async_subdev *asd)
367 struct xvip_composite_device *xdev =
368 container_of(notifier, struct xvip_composite_device, notifier);
369 struct xvip_graph_entity *entity;
371 /* Locate the entity corresponding to the bound subdev and store the
374 list_for_each_entry(entity, &xdev->entities, list) {
375 if (entity->node != subdev->dev->of_node)
378 if (entity->subdev) {
379 dev_err(xdev->dev, "duplicate subdev for node %pOF\n",
384 dev_dbg(xdev->dev, "subdev %s bound\n", subdev->name);
385 entity->entity = &subdev->entity;
386 entity->subdev = subdev;
390 dev_err(xdev->dev, "no entity for subdev %s\n", subdev->name);
394 static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
395 struct device_node *node)
397 struct xvip_graph_entity *entity;
398 struct device_node *remote;
399 struct device_node *ep = NULL;
402 dev_dbg(xdev->dev, "parsing node %pOF\n", node);
405 ep = of_graph_get_next_endpoint(node, ep);
409 dev_dbg(xdev->dev, "handling endpoint %pOF\n", ep);
411 remote = of_graph_get_remote_port_parent(ep);
412 if (remote == NULL) {
417 /* Skip entities that we have already processed. */
418 if (remote == xdev->dev->of_node ||
419 xvip_graph_find_entity(xdev, remote)) {
424 entity = devm_kzalloc(xdev->dev, sizeof(*entity), GFP_KERNEL);
425 if (entity == NULL) {
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);
442 static int xvip_graph_parse(struct xvip_composite_device *xdev)
444 struct xvip_graph_entity *entity;
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
453 ret = xvip_graph_parse_one(xdev, xdev->dev->of_node);
457 list_for_each_entry(entity, &xdev->entities, list) {
458 ret = xvip_graph_parse_one(xdev, entity->node);
466 static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
467 struct device_node *node)
469 struct xvip_dma *dma;
470 enum v4l2_buf_type type;
471 const char *direction;
475 ret = of_property_read_string(node, "direction", &direction);
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;
488 of_property_read_u32(node, "reg", &index);
490 dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
494 ret = xvip_dma_init(xdev, dma, type, index);
496 dev_err(xdev->dev, "%pOF initialization failed\n", node);
500 list_add_tail(&dma->list, &xdev->dmas);
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;
514 static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
516 struct device_node *ports;
517 struct device_node *port;
520 ports = of_get_child_by_name(xdev->dev->of_node, "ports");
522 dev_err(xdev->dev, "ports node not present\n");
526 for_each_child_of_node(ports, port) {
527 ret = xvip_graph_dma_init_one(xdev, port);
537 static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
539 struct xvip_graph_entity *entityp;
540 struct xvip_graph_entity *entity;
541 struct xvip_dma *dmap;
542 struct xvip_dma *dma;
544 v4l2_async_notifier_unregister(&xdev->notifier);
546 list_for_each_entry_safe(entity, entityp, &xdev->entities, list) {
547 of_node_put(entity->node);
548 list_del(&entity->list);
551 list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
552 xvip_dma_cleanup(dma);
553 list_del(&dma->list);
557 static int xvip_graph_init(struct xvip_composite_device *xdev)
559 struct xvip_graph_entity *entity;
560 struct v4l2_async_subdev **subdevs = NULL;
561 unsigned int num_subdevs;
565 /* Init the DMA channels. */
566 ret = xvip_graph_dma_init(xdev);
568 dev_err(xdev->dev, "DMA initialization failed\n");
572 /* Parse the graph to extract a list of subdevice DT nodes. */
573 ret = xvip_graph_parse(xdev);
575 dev_err(xdev->dev, "graph parsing failed\n");
579 if (!xdev->num_subdevs) {
580 dev_err(xdev->dev, "no subdev found in graph\n");
584 /* Register the subdevices notifier. */
585 num_subdevs = xdev->num_subdevs;
586 subdevs = devm_kzalloc(xdev->dev, sizeof(*subdevs) * num_subdevs,
588 if (subdevs == NULL) {
594 list_for_each_entry(entity, &xdev->entities, list)
595 subdevs[i++] = &entity->asd;
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;
602 ret = v4l2_async_notifier_register(&xdev->v4l2_dev, &xdev->notifier);
604 dev_err(xdev->dev, "notifier registration failed\n");
612 xvip_graph_cleanup(xdev);
617 /* -----------------------------------------------------------------------------
618 * Media Controller and V4L2
621 static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
623 v4l2_device_unregister(&xdev->v4l2_dev);
624 media_device_unregister(&xdev->media_dev);
625 media_device_cleanup(&xdev->media_dev);
628 static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
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;
637 media_device_init(&xdev->media_dev);
639 xdev->v4l2_dev.mdev = &xdev->media_dev;
640 ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
642 dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
644 media_device_cleanup(&xdev->media_dev);
651 /* -----------------------------------------------------------------------------
652 * Platform Device Driver
655 static int xvip_composite_probe(struct platform_device *pdev)
657 struct xvip_composite_device *xdev;
660 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
664 xdev->dev = &pdev->dev;
665 mutex_init(&xdev->lock);
666 INIT_LIST_HEAD(&xdev->entities);
667 INIT_LIST_HEAD(&xdev->dmas);
669 ret = xvip_composite_v4l2_init(xdev);
673 ret = xvip_graph_init(xdev);
677 platform_set_drvdata(pdev, xdev);
679 dev_info(xdev->dev, "device registered\n");
684 xvip_composite_v4l2_cleanup(xdev);
688 static int xvip_composite_remove(struct platform_device *pdev)
690 struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
692 mutex_destroy(&xdev->lock);
693 xvip_graph_cleanup(xdev);
694 xvip_composite_v4l2_cleanup(xdev);
699 static const struct of_device_id xvip_composite_of_id_table[] = {
700 { .compatible = "xlnx,video" },
703 MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
705 static struct platform_driver xvip_composite_driver = {
707 .name = "xilinx-video",
708 .of_match_table = xvip_composite_of_id_table,
710 .probe = xvip_composite_probe,
711 .remove = xvip_composite_remove,
714 module_platform_driver(xvip_composite_driver);
716 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
717 MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
718 MODULE_LICENSE("GPL v2");