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.
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;
203 struct xvip_dma *dma;
206 dev_dbg(xdev->dev, "creating links for DMA engines\n");
208 for_each_endpoint_of_node(node, ep) {
209 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
211 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
213 dev_err(xdev->dev, "failed to parse link for %pOF\n",
218 /* Find the DMA engine. */
219 dma = xvip_graph_find_dma(xdev, link.local_port);
221 dev_err(xdev->dev, "no DMA engine found for port %u\n",
223 v4l2_fwnode_put_link(&link);
228 dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
231 /* Find the remote entity. */
232 ent = xvip_graph_find_entity(xdev, link.remote_node);
234 dev_err(xdev->dev, "no entity found for %pOF\n",
235 to_of_node(link.remote_node));
236 v4l2_fwnode_put_link(&link);
241 if (link.remote_port >= ent->entity->num_pads) {
242 dev_err(xdev->dev, "invalid port number %u on %pOF\n",
244 to_of_node(link.remote_node));
245 v4l2_fwnode_put_link(&link);
250 if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
251 source = &dma->video.entity;
252 source_pad = &dma->pad;
254 sink_pad = &sink->pads[link.remote_port];
256 source = ent->entity;
257 source_pad = &source->pads[link.remote_port];
258 sink = &dma->video.entity;
259 sink_pad = &dma->pad;
262 v4l2_fwnode_put_link(&link);
264 /* Create the media link. */
265 dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
266 source->name, source_pad->index,
267 sink->name, sink_pad->index);
269 ret = media_create_pad_link(source, source_pad->index,
270 sink, sink_pad->index,
274 "failed to create %s:%u -> %s:%u link\n",
275 source->name, source_pad->index,
276 sink->name, sink_pad->index);
285 static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
287 struct xvip_composite_device *xdev =
288 container_of(notifier, struct xvip_composite_device, notifier);
289 struct xvip_graph_entity *entity;
290 struct v4l2_async_connection *asd;
293 dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
295 /* Create links for every entity. */
296 list_for_each_entry(asd, &xdev->notifier.done_list, asc_entry) {
297 entity = to_xvip_entity(asd);
298 ret = xvip_graph_build_one(xdev, entity);
303 /* Create links for DMA channels. */
304 ret = xvip_graph_build_dma(xdev);
308 ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
310 dev_err(xdev->dev, "failed to register subdev nodes\n");
312 return media_device_register(&xdev->media_dev);
315 static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
316 struct v4l2_subdev *subdev,
317 struct v4l2_async_connection *asc)
319 struct xvip_graph_entity *entity = to_xvip_entity(asc);
321 entity->entity = &subdev->entity;
322 entity->subdev = subdev;
327 static const struct v4l2_async_notifier_operations xvip_graph_notify_ops = {
328 .bound = xvip_graph_notify_bound,
329 .complete = xvip_graph_notify_complete,
332 static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
333 struct fwnode_handle *fwnode)
335 struct fwnode_handle *remote;
336 struct fwnode_handle *ep = NULL;
339 dev_dbg(xdev->dev, "parsing node %p\n", fwnode);
342 struct xvip_graph_entity *xge;
344 ep = fwnode_graph_get_next_endpoint(fwnode, ep);
348 dev_dbg(xdev->dev, "handling endpoint %p\n", ep);
350 remote = fwnode_graph_get_remote_port_parent(ep);
351 if (remote == NULL) {
353 goto err_notifier_cleanup;
356 fwnode_handle_put(ep);
358 /* Skip entities that we have already processed. */
359 if (remote == of_fwnode_handle(xdev->dev->of_node) ||
360 xvip_graph_find_entity(xdev, remote)) {
361 fwnode_handle_put(remote);
365 xge = v4l2_async_nf_add_fwnode(&xdev->notifier, remote,
366 struct xvip_graph_entity);
367 fwnode_handle_put(remote);
370 goto err_notifier_cleanup;
376 err_notifier_cleanup:
377 v4l2_async_nf_cleanup(&xdev->notifier);
378 fwnode_handle_put(ep);
382 static int xvip_graph_parse(struct xvip_composite_device *xdev)
384 struct xvip_graph_entity *entity;
385 struct v4l2_async_connection *asd;
389 * Walk the links to parse the full graph. Start by parsing the
390 * composite node and then parse entities in turn. The list_for_each
391 * loop will handle entities added at the end of the list while walking
394 ret = xvip_graph_parse_one(xdev, of_fwnode_handle(xdev->dev->of_node));
398 list_for_each_entry(asd, &xdev->notifier.waiting_list, asc_entry) {
399 entity = to_xvip_entity(asd);
400 ret = xvip_graph_parse_one(xdev, entity->asd.match.fwnode);
402 v4l2_async_nf_cleanup(&xdev->notifier);
410 static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
411 struct device_node *node)
413 struct xvip_dma *dma;
414 enum v4l2_buf_type type;
415 const char *direction;
419 ret = of_property_read_string(node, "direction", &direction);
423 if (strcmp(direction, "input") == 0)
424 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
425 else if (strcmp(direction, "output") == 0)
426 type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
430 of_property_read_u32(node, "reg", &index);
432 dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
436 ret = xvip_dma_init(xdev, dma, type, index);
438 dev_err(xdev->dev, "%pOF initialization failed\n", node);
442 list_add_tail(&dma->list, &xdev->dmas);
444 xdev->v4l2_caps |= type == V4L2_BUF_TYPE_VIDEO_CAPTURE
445 ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_VIDEO_OUTPUT;
450 static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
452 struct device_node *ports;
453 struct device_node *port;
456 ports = of_get_child_by_name(xdev->dev->of_node, "ports");
458 dev_err(xdev->dev, "ports node not present\n");
462 for_each_child_of_node(ports, port) {
463 ret = xvip_graph_dma_init_one(xdev, port);
474 static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
476 struct xvip_dma *dmap;
477 struct xvip_dma *dma;
479 v4l2_async_nf_unregister(&xdev->notifier);
480 v4l2_async_nf_cleanup(&xdev->notifier);
482 list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
483 xvip_dma_cleanup(dma);
484 list_del(&dma->list);
488 static int xvip_graph_init(struct xvip_composite_device *xdev)
492 /* Init the DMA channels. */
493 ret = xvip_graph_dma_init(xdev);
495 dev_err(xdev->dev, "DMA initialization failed\n");
499 v4l2_async_nf_init(&xdev->notifier, &xdev->v4l2_dev);
501 /* Parse the graph to extract a list of subdevice DT nodes. */
502 ret = xvip_graph_parse(xdev);
504 dev_err(xdev->dev, "graph parsing failed\n");
508 if (list_empty(&xdev->notifier.waiting_list)) {
509 dev_err(xdev->dev, "no subdev found in graph\n");
514 /* Register the subdevices notifier. */
515 xdev->notifier.ops = &xvip_graph_notify_ops;
517 ret = v4l2_async_nf_register(&xdev->notifier);
519 dev_err(xdev->dev, "notifier registration failed\n");
527 xvip_graph_cleanup(xdev);
532 /* -----------------------------------------------------------------------------
533 * Media Controller and V4L2
536 static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
538 v4l2_device_unregister(&xdev->v4l2_dev);
539 media_device_unregister(&xdev->media_dev);
540 media_device_cleanup(&xdev->media_dev);
543 static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
547 xdev->media_dev.dev = xdev->dev;
548 strscpy(xdev->media_dev.model, "Xilinx Video Composite Device",
549 sizeof(xdev->media_dev.model));
550 xdev->media_dev.hw_revision = 0;
552 media_device_init(&xdev->media_dev);
554 xdev->v4l2_dev.mdev = &xdev->media_dev;
555 ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
557 dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
559 media_device_cleanup(&xdev->media_dev);
566 /* -----------------------------------------------------------------------------
567 * Platform Device Driver
570 static int xvip_composite_probe(struct platform_device *pdev)
572 struct xvip_composite_device *xdev;
575 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
579 xdev->dev = &pdev->dev;
580 INIT_LIST_HEAD(&xdev->dmas);
582 ret = xvip_composite_v4l2_init(xdev);
586 ret = xvip_graph_init(xdev);
590 platform_set_drvdata(pdev, xdev);
592 dev_info(xdev->dev, "device registered\n");
597 xvip_composite_v4l2_cleanup(xdev);
601 static void xvip_composite_remove(struct platform_device *pdev)
603 struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
605 xvip_graph_cleanup(xdev);
606 xvip_composite_v4l2_cleanup(xdev);
609 static const struct of_device_id xvip_composite_of_id_table[] = {
610 { .compatible = "xlnx,video" },
613 MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
615 static struct platform_driver xvip_composite_driver = {
617 .name = "xilinx-video",
618 .of_match_table = xvip_composite_of_id_table,
620 .probe = xvip_composite_probe,
621 .remove = xvip_composite_remove,
624 module_platform_driver(xvip_composite_driver);
627 MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
628 MODULE_LICENSE("GPL v2");