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);
103 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
105 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
107 dev_err(xdev->dev, "failed to parse link for %pOF\n",
112 /* Skip sink ports, they will be processed from the other end of
115 if (link.local_port >= local->num_pads) {
116 dev_err(xdev->dev, "invalid port number %u for %pOF\n",
118 to_of_node(link.local_node));
119 v4l2_fwnode_put_link(&link);
124 local_pad = &local->pads[link.local_port];
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),
130 v4l2_fwnode_put_link(&link);
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),
139 v4l2_fwnode_put_link(&link);
143 /* Find the remote entity. */
144 ent = xvip_graph_find_entity(xdev,
145 to_of_node(link.remote_node));
147 dev_err(xdev->dev, "no entity found for %pOF\n",
148 to_of_node(link.remote_node));
149 v4l2_fwnode_put_link(&link);
154 remote = ent->entity;
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);
164 remote_pad = &remote->pads[link.remote_port];
166 v4l2_fwnode_put_link(&link);
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);
173 ret = media_create_pad_link(local, local_pad->index,
174 remote, remote_pad->index,
178 "failed to create %s:%u -> %s:%u link\n",
179 local->name, local_pad->index,
180 remote->name, remote_pad->index);
188 static struct xvip_dma *
189 xvip_graph_find_dma(struct xvip_composite_device *xdev, unsigned int port)
191 struct xvip_dma *dma;
193 list_for_each_entry(dma, &xdev->dmas, list) {
194 if (dma->port == port)
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
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
210 * Return: streaming status (true or false) if successful or warn_on if subdev
211 * is not present and return false
213 bool xvip_subdev_set_streaming(struct xvip_composite_device *xdev,
214 struct v4l2_subdev *subdev, bool enable)
216 struct xvip_graph_entity *entity;
218 list_for_each_entry(entity, &xdev->entities, list)
219 if (entity->node == subdev->dev->of_node) {
220 bool status = entity->streaming;
222 entity->streaming = enable;
226 WARN(1, "Should never get here\n");
230 static int xvip_graph_build_dma(struct xvip_composite_device *xdev)
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;
245 dev_dbg(xdev->dev, "creating links for DMA engines\n");
248 /* Get the next endpoint and parse its link. */
249 next = of_graph_get_next_endpoint(node, ep);
255 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
257 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
259 dev_err(xdev->dev, "failed to parse link for %pOF\n",
264 /* Find the DMA engine. */
265 dma = xvip_graph_find_dma(xdev, link.local_port);
267 dev_err(xdev->dev, "no DMA engine found for port %u\n",
269 v4l2_fwnode_put_link(&link);
274 dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
277 /* Find the remote entity. */
278 ent = xvip_graph_find_entity(xdev,
279 to_of_node(link.remote_node));
281 dev_err(xdev->dev, "no entity found for %pOF\n",
282 to_of_node(link.remote_node));
283 v4l2_fwnode_put_link(&link);
288 if (link.remote_port >= ent->entity->num_pads) {
289 dev_err(xdev->dev, "invalid port number %u on %pOF\n",
291 to_of_node(link.remote_node));
292 v4l2_fwnode_put_link(&link);
297 if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
298 source = &dma->video.entity;
299 source_pad = &dma->pad;
301 sink_pad = &sink->pads[link.remote_port];
303 source = ent->entity;
304 source_pad = &source->pads[link.remote_port];
305 sink = &dma->video.entity;
306 sink_pad = &dma->pad;
309 v4l2_fwnode_put_link(&link);
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);
316 ret = media_create_pad_link(source, source_pad->index,
317 sink, sink_pad->index,
321 "failed to create %s:%u -> %s:%u link\n",
322 source->name, source_pad->index,
323 sink->name, sink_pad->index);
331 static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
333 struct xvip_composite_device *xdev =
334 container_of(notifier, struct xvip_composite_device, notifier);
335 struct xvip_graph_entity *entity;
338 dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
340 /* Create links for every entity. */
341 list_for_each_entry(entity, &xdev->entities, list) {
342 ret = xvip_graph_build_one(xdev, entity);
347 /* Create links for DMA channels. */
348 ret = xvip_graph_build_dma(xdev);
352 ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
354 dev_err(xdev->dev, "failed to register subdev nodes\n");
356 return media_device_register(&xdev->media_dev);
359 static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
360 struct v4l2_subdev *subdev,
361 struct v4l2_async_subdev *asd)
363 struct xvip_composite_device *xdev =
364 container_of(notifier, struct xvip_composite_device, notifier);
365 struct xvip_graph_entity *entity;
367 /* Locate the entity corresponding to the bound subdev and store the
370 list_for_each_entry(entity, &xdev->entities, list) {
371 if (entity->node != subdev->dev->of_node)
374 if (entity->subdev) {
375 dev_err(xdev->dev, "duplicate subdev for node %pOF\n",
380 dev_dbg(xdev->dev, "subdev %s bound\n", subdev->name);
381 entity->entity = &subdev->entity;
382 entity->subdev = subdev;
386 dev_err(xdev->dev, "no entity for subdev %s\n", subdev->name);
390 static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
391 struct device_node *node)
393 struct xvip_graph_entity *entity;
394 struct device_node *remote;
395 struct device_node *ep = NULL;
398 dev_dbg(xdev->dev, "parsing node %pOF\n", node);
401 ep = of_graph_get_next_endpoint(node, ep);
405 dev_dbg(xdev->dev, "handling endpoint %pOF\n", ep);
407 remote = of_graph_get_remote_port_parent(ep);
408 if (remote == NULL) {
413 /* Skip entities that we have already processed. */
414 if (remote == xdev->dev->of_node ||
415 xvip_graph_find_entity(xdev, remote)) {
420 entity = devm_kzalloc(xdev->dev, sizeof(*entity), GFP_KERNEL);
421 if (entity == NULL) {
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);
438 static int xvip_graph_parse(struct xvip_composite_device *xdev)
440 struct xvip_graph_entity *entity;
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
449 ret = xvip_graph_parse_one(xdev, xdev->dev->of_node);
453 list_for_each_entry(entity, &xdev->entities, list) {
454 ret = xvip_graph_parse_one(xdev, entity->node);
462 static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
463 struct device_node *node)
465 struct xvip_dma *dma;
466 enum v4l2_buf_type type;
467 const char *direction;
471 ret = of_property_read_string(node, "direction", &direction);
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;
484 of_property_read_u32(node, "reg", &index);
486 dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
490 ret = xvip_dma_init(xdev, dma, type, index);
492 dev_err(xdev->dev, "%pOF initialization failed\n", node);
496 list_add_tail(&dma->list, &xdev->dmas);
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;
510 static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
512 struct device_node *ports;
513 struct device_node *port;
516 ports = of_get_child_by_name(xdev->dev->of_node, "ports");
518 dev_err(xdev->dev, "ports node not present\n");
522 for_each_child_of_node(ports, port) {
523 ret = xvip_graph_dma_init_one(xdev, port);
533 static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
535 struct xvip_graph_entity *entityp;
536 struct xvip_graph_entity *entity;
537 struct xvip_dma *dmap;
538 struct xvip_dma *dma;
540 v4l2_async_notifier_unregister(&xdev->notifier);
542 list_for_each_entry_safe(entity, entityp, &xdev->entities, list) {
543 of_node_put(entity->node);
544 list_del(&entity->list);
547 list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
548 xvip_dma_cleanup(dma);
549 list_del(&dma->list);
553 static int xvip_graph_init(struct xvip_composite_device *xdev)
555 struct xvip_graph_entity *entity;
556 struct v4l2_async_subdev **subdevs = NULL;
557 unsigned int num_subdevs;
561 /* Init the DMA channels. */
562 ret = xvip_graph_dma_init(xdev);
564 dev_err(xdev->dev, "DMA initialization failed\n");
568 /* Parse the graph to extract a list of subdevice DT nodes. */
569 ret = xvip_graph_parse(xdev);
571 dev_err(xdev->dev, "graph parsing failed\n");
575 if (!xdev->num_subdevs) {
576 dev_err(xdev->dev, "no subdev found in graph\n");
580 /* Register the subdevices notifier. */
581 num_subdevs = xdev->num_subdevs;
582 subdevs = devm_kzalloc(xdev->dev, sizeof(*subdevs) * num_subdevs,
584 if (subdevs == NULL) {
590 list_for_each_entry(entity, &xdev->entities, list)
591 subdevs[i++] = &entity->asd;
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;
598 ret = v4l2_async_notifier_register(&xdev->v4l2_dev, &xdev->notifier);
600 dev_err(xdev->dev, "notifier registration failed\n");
608 xvip_graph_cleanup(xdev);
613 /* -----------------------------------------------------------------------------
614 * Media Controller and V4L2
617 static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
619 v4l2_device_unregister(&xdev->v4l2_dev);
620 media_device_unregister(&xdev->media_dev);
621 media_device_cleanup(&xdev->media_dev);
624 static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
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;
633 media_device_init(&xdev->media_dev);
635 xdev->v4l2_dev.mdev = &xdev->media_dev;
636 ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
638 dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
640 media_device_cleanup(&xdev->media_dev);
647 /* -----------------------------------------------------------------------------
648 * Platform Device Driver
651 static int xvip_composite_probe(struct platform_device *pdev)
653 struct xvip_composite_device *xdev;
656 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
660 xdev->dev = &pdev->dev;
661 mutex_init(&xdev->lock);
662 INIT_LIST_HEAD(&xdev->entities);
663 INIT_LIST_HEAD(&xdev->dmas);
665 ret = xvip_composite_v4l2_init(xdev);
669 ret = xvip_graph_init(xdev);
673 platform_set_drvdata(pdev, xdev);
675 dev_info(xdev->dev, "device registered\n");
680 xvip_composite_v4l2_cleanup(xdev);
684 static int xvip_composite_remove(struct platform_device *pdev)
686 struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
688 mutex_destroy(&xdev->lock);
689 xvip_graph_cleanup(xdev);
690 xvip_composite_v4l2_cleanup(xdev);
695 static const struct of_device_id xvip_composite_of_id_table[] = {
696 { .compatible = "xlnx,video" },
699 MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
701 static struct platform_driver xvip_composite_driver = {
703 .name = "xilinx-video",
704 .of_match_table = xvip_composite_of_id_table,
706 .probe = xvip_composite_probe,
707 .remove = xvip_composite_remove,
710 module_platform_driver(xvip_composite_driver);
712 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
713 MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
714 MODULE_LICENSE("GPL v2");