1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Video IP Composite Device
5 * Copyright (C) 2013-2015 Ideas on Board
6 * Copyright (C) 2013-2015 Xilinx, Inc.
8 * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
9 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12 #include <linux/list.h>
13 #include <linux/module.h>
15 #include <linux/of_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
19 #include <media/v4l2-async.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-fwnode.h>
24 #include "xilinx-dma.h"
25 #include "xilinx-vipp.h"
27 #define XVIPP_DMA_S2MM 0
28 #define XVIPP_DMA_MM2S 1
31 * struct xvip_graph_entity - Entity in the video graph
32 * @asd: subdev asynchronous registration information
33 * @entity: media entity, from the corresponding V4L2 subdev
34 * @subdev: V4L2 subdev
36 struct xvip_graph_entity {
37 struct v4l2_async_connection asd; /* must be first */
38 struct media_entity *entity;
39 struct v4l2_subdev *subdev;
42 static inline struct xvip_graph_entity *
43 to_xvip_entity(struct v4l2_async_connection *asd)
45 return container_of(asd, struct xvip_graph_entity, asd);
48 /* -----------------------------------------------------------------------------
52 static struct xvip_graph_entity *
53 xvip_graph_find_entity(struct xvip_composite_device *xdev,
54 const struct fwnode_handle *fwnode)
56 struct xvip_graph_entity *entity;
57 struct v4l2_async_connection *asd;
58 struct list_head *lists[] = {
59 &xdev->notifier.done_list,
60 &xdev->notifier.waiting_list
64 for (i = 0; i < ARRAY_SIZE(lists); i++) {
65 list_for_each_entry(asd, lists[i], asc_entry) {
66 entity = to_xvip_entity(asd);
67 if (entity->asd.match.fwnode == fwnode)
75 static int xvip_graph_build_one(struct xvip_composite_device *xdev,
76 struct xvip_graph_entity *entity)
78 u32 link_flags = MEDIA_LNK_FL_ENABLED;
79 struct media_entity *local = entity->entity;
80 struct media_entity *remote;
81 struct media_pad *local_pad;
82 struct media_pad *remote_pad;
83 struct xvip_graph_entity *ent;
84 struct v4l2_fwnode_link link;
85 struct fwnode_handle *ep = NULL;
88 dev_dbg(xdev->dev, "creating links for entity %s\n", local->name);
91 /* Get the next endpoint and parse its link. */
92 ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
97 dev_dbg(xdev->dev, "processing endpoint %p\n", ep);
99 ret = v4l2_fwnode_parse_link(ep, &link);
101 dev_err(xdev->dev, "failed to parse link for %p\n",
106 /* Skip sink ports, they will be processed from the other end of
109 if (link.local_port >= local->num_pads) {
110 dev_err(xdev->dev, "invalid port number %u for %p\n",
111 link.local_port, link.local_node);
112 v4l2_fwnode_put_link(&link);
117 local_pad = &local->pads[link.local_port];
119 if (local_pad->flags & MEDIA_PAD_FL_SINK) {
120 dev_dbg(xdev->dev, "skipping sink port %p:%u\n",
121 link.local_node, link.local_port);
122 v4l2_fwnode_put_link(&link);
126 /* Skip DMA engines, they will be processed separately. */
127 if (link.remote_node == of_fwnode_handle(xdev->dev->of_node)) {
128 dev_dbg(xdev->dev, "skipping DMA port %p:%u\n",
129 link.local_node, link.local_port);
130 v4l2_fwnode_put_link(&link);
134 /* Find the remote entity. */
135 ent = xvip_graph_find_entity(xdev, link.remote_node);
137 dev_err(xdev->dev, "no entity found for %p\n",
139 v4l2_fwnode_put_link(&link);
144 remote = ent->entity;
146 if (link.remote_port >= remote->num_pads) {
147 dev_err(xdev->dev, "invalid port number %u on %p\n",
148 link.remote_port, link.remote_node);
149 v4l2_fwnode_put_link(&link);
154 remote_pad = &remote->pads[link.remote_port];
156 v4l2_fwnode_put_link(&link);
158 /* Create the media link. */
159 dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
160 local->name, local_pad->index,
161 remote->name, remote_pad->index);
163 ret = media_create_pad_link(local, local_pad->index,
164 remote, remote_pad->index,
168 "failed to create %s:%u -> %s:%u link\n",
169 local->name, local_pad->index,
170 remote->name, remote_pad->index);
175 fwnode_handle_put(ep);
179 static struct xvip_dma *
180 xvip_graph_find_dma(struct xvip_composite_device *xdev, unsigned int port)
182 struct xvip_dma *dma;
184 list_for_each_entry(dma, &xdev->dmas, list) {
185 if (dma->port == port)
192 static int xvip_graph_build_dma(struct xvip_composite_device *xdev)
194 u32 link_flags = MEDIA_LNK_FL_ENABLED;
195 struct device_node *node = xdev->dev->of_node;
196 struct media_entity *source;
197 struct media_entity *sink;
198 struct media_pad *source_pad;
199 struct media_pad *sink_pad;
200 struct xvip_graph_entity *ent;
201 struct v4l2_fwnode_link link;
202 struct device_node *ep = NULL;
203 struct xvip_dma *dma;
206 dev_dbg(xdev->dev, "creating links for DMA engines\n");
209 /* Get the next endpoint and parse its link. */
210 ep = of_graph_get_next_endpoint(node, ep);
214 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
216 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
218 dev_err(xdev->dev, "failed to parse link for %pOF\n",
223 /* Find the DMA engine. */
224 dma = xvip_graph_find_dma(xdev, link.local_port);
226 dev_err(xdev->dev, "no DMA engine found for port %u\n",
228 v4l2_fwnode_put_link(&link);
233 dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
236 /* Find the remote entity. */
237 ent = xvip_graph_find_entity(xdev, link.remote_node);
239 dev_err(xdev->dev, "no entity found for %pOF\n",
240 to_of_node(link.remote_node));
241 v4l2_fwnode_put_link(&link);
246 if (link.remote_port >= ent->entity->num_pads) {
247 dev_err(xdev->dev, "invalid port number %u on %pOF\n",
249 to_of_node(link.remote_node));
250 v4l2_fwnode_put_link(&link);
255 if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
256 source = &dma->video.entity;
257 source_pad = &dma->pad;
259 sink_pad = &sink->pads[link.remote_port];
261 source = ent->entity;
262 source_pad = &source->pads[link.remote_port];
263 sink = &dma->video.entity;
264 sink_pad = &dma->pad;
267 v4l2_fwnode_put_link(&link);
269 /* Create the media link. */
270 dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
271 source->name, source_pad->index,
272 sink->name, sink_pad->index);
274 ret = media_create_pad_link(source, source_pad->index,
275 sink, sink_pad->index,
279 "failed to create %s:%u -> %s:%u link\n",
280 source->name, source_pad->index,
281 sink->name, sink_pad->index);
290 static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
292 struct xvip_composite_device *xdev =
293 container_of(notifier, struct xvip_composite_device, notifier);
294 struct xvip_graph_entity *entity;
295 struct v4l2_async_connection *asd;
298 dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
300 /* Create links for every entity. */
301 list_for_each_entry(asd, &xdev->notifier.done_list, asc_entry) {
302 entity = to_xvip_entity(asd);
303 ret = xvip_graph_build_one(xdev, entity);
308 /* Create links for DMA channels. */
309 ret = xvip_graph_build_dma(xdev);
313 ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
315 dev_err(xdev->dev, "failed to register subdev nodes\n");
317 return media_device_register(&xdev->media_dev);
320 static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
321 struct v4l2_subdev *subdev,
322 struct v4l2_async_connection *asc)
324 struct xvip_graph_entity *entity = to_xvip_entity(asc);
326 entity->entity = &subdev->entity;
327 entity->subdev = subdev;
332 static const struct v4l2_async_notifier_operations xvip_graph_notify_ops = {
333 .bound = xvip_graph_notify_bound,
334 .complete = xvip_graph_notify_complete,
337 static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
338 struct fwnode_handle *fwnode)
340 struct fwnode_handle *remote;
341 struct fwnode_handle *ep = NULL;
344 dev_dbg(xdev->dev, "parsing node %p\n", fwnode);
347 struct xvip_graph_entity *xge;
349 ep = fwnode_graph_get_next_endpoint(fwnode, ep);
353 dev_dbg(xdev->dev, "handling endpoint %p\n", ep);
355 remote = fwnode_graph_get_remote_port_parent(ep);
356 if (remote == NULL) {
358 goto err_notifier_cleanup;
361 fwnode_handle_put(ep);
363 /* Skip entities that we have already processed. */
364 if (remote == of_fwnode_handle(xdev->dev->of_node) ||
365 xvip_graph_find_entity(xdev, remote)) {
366 fwnode_handle_put(remote);
370 xge = v4l2_async_nf_add_fwnode(&xdev->notifier, remote,
371 struct xvip_graph_entity);
372 fwnode_handle_put(remote);
375 goto err_notifier_cleanup;
381 err_notifier_cleanup:
382 v4l2_async_nf_cleanup(&xdev->notifier);
383 fwnode_handle_put(ep);
387 static int xvip_graph_parse(struct xvip_composite_device *xdev)
389 struct xvip_graph_entity *entity;
390 struct v4l2_async_connection *asd;
394 * Walk the links to parse the full graph. Start by parsing the
395 * composite node and then parse entities in turn. The list_for_each
396 * loop will handle entities added at the end of the list while walking
399 ret = xvip_graph_parse_one(xdev, of_fwnode_handle(xdev->dev->of_node));
403 list_for_each_entry(asd, &xdev->notifier.waiting_list, asc_entry) {
404 entity = to_xvip_entity(asd);
405 ret = xvip_graph_parse_one(xdev, entity->asd.match.fwnode);
407 v4l2_async_nf_cleanup(&xdev->notifier);
415 static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
416 struct device_node *node)
418 struct xvip_dma *dma;
419 enum v4l2_buf_type type;
420 const char *direction;
424 ret = of_property_read_string(node, "direction", &direction);
428 if (strcmp(direction, "input") == 0)
429 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
430 else if (strcmp(direction, "output") == 0)
431 type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
435 of_property_read_u32(node, "reg", &index);
437 dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
441 ret = xvip_dma_init(xdev, dma, type, index);
443 dev_err(xdev->dev, "%pOF initialization failed\n", node);
447 list_add_tail(&dma->list, &xdev->dmas);
449 xdev->v4l2_caps |= type == V4L2_BUF_TYPE_VIDEO_CAPTURE
450 ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_VIDEO_OUTPUT;
455 static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
457 struct device_node *ports;
458 struct device_node *port;
461 ports = of_get_child_by_name(xdev->dev->of_node, "ports");
463 dev_err(xdev->dev, "ports node not present\n");
467 for_each_child_of_node(ports, port) {
468 ret = xvip_graph_dma_init_one(xdev, port);
479 static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
481 struct xvip_dma *dmap;
482 struct xvip_dma *dma;
484 v4l2_async_nf_unregister(&xdev->notifier);
485 v4l2_async_nf_cleanup(&xdev->notifier);
487 list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
488 xvip_dma_cleanup(dma);
489 list_del(&dma->list);
493 static int xvip_graph_init(struct xvip_composite_device *xdev)
497 /* Init the DMA channels. */
498 ret = xvip_graph_dma_init(xdev);
500 dev_err(xdev->dev, "DMA initialization failed\n");
504 v4l2_async_nf_init(&xdev->notifier, &xdev->v4l2_dev);
506 /* Parse the graph to extract a list of subdevice DT nodes. */
507 ret = xvip_graph_parse(xdev);
509 dev_err(xdev->dev, "graph parsing failed\n");
513 if (list_empty(&xdev->notifier.waiting_list)) {
514 dev_err(xdev->dev, "no subdev found in graph\n");
519 /* Register the subdevices notifier. */
520 xdev->notifier.ops = &xvip_graph_notify_ops;
522 ret = v4l2_async_nf_register(&xdev->notifier);
524 dev_err(xdev->dev, "notifier registration failed\n");
532 xvip_graph_cleanup(xdev);
537 /* -----------------------------------------------------------------------------
538 * Media Controller and V4L2
541 static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
543 v4l2_device_unregister(&xdev->v4l2_dev);
544 media_device_unregister(&xdev->media_dev);
545 media_device_cleanup(&xdev->media_dev);
548 static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
552 xdev->media_dev.dev = xdev->dev;
553 strscpy(xdev->media_dev.model, "Xilinx Video Composite Device",
554 sizeof(xdev->media_dev.model));
555 xdev->media_dev.hw_revision = 0;
557 media_device_init(&xdev->media_dev);
559 xdev->v4l2_dev.mdev = &xdev->media_dev;
560 ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
562 dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
564 media_device_cleanup(&xdev->media_dev);
571 /* -----------------------------------------------------------------------------
572 * Platform Device Driver
575 static int xvip_composite_probe(struct platform_device *pdev)
577 struct xvip_composite_device *xdev;
580 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
584 xdev->dev = &pdev->dev;
585 INIT_LIST_HEAD(&xdev->dmas);
587 ret = xvip_composite_v4l2_init(xdev);
591 ret = xvip_graph_init(xdev);
595 platform_set_drvdata(pdev, xdev);
597 dev_info(xdev->dev, "device registered\n");
602 xvip_composite_v4l2_cleanup(xdev);
606 static void xvip_composite_remove(struct platform_device *pdev)
608 struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
610 xvip_graph_cleanup(xdev);
611 xvip_composite_v4l2_cleanup(xdev);
614 static const struct of_device_id xvip_composite_of_id_table[] = {
615 { .compatible = "xlnx,video" },
618 MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
620 static struct platform_driver xvip_composite_driver = {
622 .name = "xilinx-video",
623 .of_match_table = xvip_composite_of_id_table,
625 .probe = xvip_composite_probe,
626 .remove_new = xvip_composite_remove,
629 module_platform_driver(xvip_composite_driver);
631 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
632 MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
633 MODULE_LICENSE("GPL v2");