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;
59 list_for_each_entry(asd, &xdev->notifier.done_list, asc_entry) {
60 entity = to_xvip_entity(asd);
61 if (entity->asd.match.fwnode == fwnode)
68 static int xvip_graph_build_one(struct xvip_composite_device *xdev,
69 struct xvip_graph_entity *entity)
71 u32 link_flags = MEDIA_LNK_FL_ENABLED;
72 struct media_entity *local = entity->entity;
73 struct media_entity *remote;
74 struct media_pad *local_pad;
75 struct media_pad *remote_pad;
76 struct xvip_graph_entity *ent;
77 struct v4l2_fwnode_link link;
78 struct fwnode_handle *ep = NULL;
81 dev_dbg(xdev->dev, "creating links for entity %s\n", local->name);
84 /* Get the next endpoint and parse its link. */
85 ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
90 dev_dbg(xdev->dev, "processing endpoint %p\n", ep);
92 ret = v4l2_fwnode_parse_link(ep, &link);
94 dev_err(xdev->dev, "failed to parse link for %p\n",
99 /* Skip sink ports, they will be processed from the other end of
102 if (link.local_port >= local->num_pads) {
103 dev_err(xdev->dev, "invalid port number %u for %p\n",
104 link.local_port, link.local_node);
105 v4l2_fwnode_put_link(&link);
110 local_pad = &local->pads[link.local_port];
112 if (local_pad->flags & MEDIA_PAD_FL_SINK) {
113 dev_dbg(xdev->dev, "skipping sink port %p:%u\n",
114 link.local_node, link.local_port);
115 v4l2_fwnode_put_link(&link);
119 /* Skip DMA engines, they will be processed separately. */
120 if (link.remote_node == of_fwnode_handle(xdev->dev->of_node)) {
121 dev_dbg(xdev->dev, "skipping DMA port %p:%u\n",
122 link.local_node, link.local_port);
123 v4l2_fwnode_put_link(&link);
127 /* Find the remote entity. */
128 ent = xvip_graph_find_entity(xdev, link.remote_node);
130 dev_err(xdev->dev, "no entity found for %p\n",
132 v4l2_fwnode_put_link(&link);
137 remote = ent->entity;
139 if (link.remote_port >= remote->num_pads) {
140 dev_err(xdev->dev, "invalid port number %u on %p\n",
141 link.remote_port, link.remote_node);
142 v4l2_fwnode_put_link(&link);
147 remote_pad = &remote->pads[link.remote_port];
149 v4l2_fwnode_put_link(&link);
151 /* Create the media link. */
152 dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
153 local->name, local_pad->index,
154 remote->name, remote_pad->index);
156 ret = media_create_pad_link(local, local_pad->index,
157 remote, remote_pad->index,
161 "failed to create %s:%u -> %s:%u link\n",
162 local->name, local_pad->index,
163 remote->name, remote_pad->index);
168 fwnode_handle_put(ep);
172 static struct xvip_dma *
173 xvip_graph_find_dma(struct xvip_composite_device *xdev, unsigned int port)
175 struct xvip_dma *dma;
177 list_for_each_entry(dma, &xdev->dmas, list) {
178 if (dma->port == port)
185 static int xvip_graph_build_dma(struct xvip_composite_device *xdev)
187 u32 link_flags = MEDIA_LNK_FL_ENABLED;
188 struct device_node *node = xdev->dev->of_node;
189 struct media_entity *source;
190 struct media_entity *sink;
191 struct media_pad *source_pad;
192 struct media_pad *sink_pad;
193 struct xvip_graph_entity *ent;
194 struct v4l2_fwnode_link link;
195 struct device_node *ep = NULL;
196 struct xvip_dma *dma;
199 dev_dbg(xdev->dev, "creating links for DMA engines\n");
202 /* Get the next endpoint and parse its link. */
203 ep = of_graph_get_next_endpoint(node, ep);
207 dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
209 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
211 dev_err(xdev->dev, "failed to parse link for %pOF\n",
216 /* Find the DMA engine. */
217 dma = xvip_graph_find_dma(xdev, link.local_port);
219 dev_err(xdev->dev, "no DMA engine found for port %u\n",
221 v4l2_fwnode_put_link(&link);
226 dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
229 /* Find the remote entity. */
230 ent = xvip_graph_find_entity(xdev, link.remote_node);
232 dev_err(xdev->dev, "no entity found for %pOF\n",
233 to_of_node(link.remote_node));
234 v4l2_fwnode_put_link(&link);
239 if (link.remote_port >= ent->entity->num_pads) {
240 dev_err(xdev->dev, "invalid port number %u on %pOF\n",
242 to_of_node(link.remote_node));
243 v4l2_fwnode_put_link(&link);
248 if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
249 source = &dma->video.entity;
250 source_pad = &dma->pad;
252 sink_pad = &sink->pads[link.remote_port];
254 source = ent->entity;
255 source_pad = &source->pads[link.remote_port];
256 sink = &dma->video.entity;
257 sink_pad = &dma->pad;
260 v4l2_fwnode_put_link(&link);
262 /* Create the media link. */
263 dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
264 source->name, source_pad->index,
265 sink->name, sink_pad->index);
267 ret = media_create_pad_link(source, source_pad->index,
268 sink, sink_pad->index,
272 "failed to create %s:%u -> %s:%u link\n",
273 source->name, source_pad->index,
274 sink->name, sink_pad->index);
283 static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
285 struct xvip_composite_device *xdev =
286 container_of(notifier, struct xvip_composite_device, notifier);
287 struct xvip_graph_entity *entity;
288 struct v4l2_async_connection *asd;
291 dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
293 /* Create links for every entity. */
294 list_for_each_entry(asd, &xdev->notifier.done_list, asc_entry) {
295 entity = to_xvip_entity(asd);
296 ret = xvip_graph_build_one(xdev, entity);
301 /* Create links for DMA channels. */
302 ret = xvip_graph_build_dma(xdev);
306 ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
308 dev_err(xdev->dev, "failed to register subdev nodes\n");
310 return media_device_register(&xdev->media_dev);
313 static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
314 struct v4l2_subdev *subdev,
315 struct v4l2_async_connection *asc)
317 struct xvip_graph_entity *entity = to_xvip_entity(asc);
319 entity->entity = &subdev->entity;
320 entity->subdev = subdev;
325 static const struct v4l2_async_notifier_operations xvip_graph_notify_ops = {
326 .bound = xvip_graph_notify_bound,
327 .complete = xvip_graph_notify_complete,
330 static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
331 struct fwnode_handle *fwnode)
333 struct fwnode_handle *remote;
334 struct fwnode_handle *ep = NULL;
337 dev_dbg(xdev->dev, "parsing node %p\n", fwnode);
340 struct xvip_graph_entity *xge;
342 ep = fwnode_graph_get_next_endpoint(fwnode, ep);
346 dev_dbg(xdev->dev, "handling endpoint %p\n", ep);
348 remote = fwnode_graph_get_remote_port_parent(ep);
349 if (remote == NULL) {
351 goto err_notifier_cleanup;
354 fwnode_handle_put(ep);
356 /* Skip entities that we have already processed. */
357 if (remote == of_fwnode_handle(xdev->dev->of_node) ||
358 xvip_graph_find_entity(xdev, remote)) {
359 fwnode_handle_put(remote);
363 xge = v4l2_async_nf_add_fwnode(&xdev->notifier, remote,
364 struct xvip_graph_entity);
365 fwnode_handle_put(remote);
368 goto err_notifier_cleanup;
374 err_notifier_cleanup:
375 v4l2_async_nf_cleanup(&xdev->notifier);
376 fwnode_handle_put(ep);
380 static int xvip_graph_parse(struct xvip_composite_device *xdev)
382 struct xvip_graph_entity *entity;
383 struct v4l2_async_connection *asd;
387 * Walk the links to parse the full graph. Start by parsing the
388 * composite node and then parse entities in turn. The list_for_each
389 * loop will handle entities added at the end of the list while walking
392 ret = xvip_graph_parse_one(xdev, of_fwnode_handle(xdev->dev->of_node));
396 list_for_each_entry(asd, &xdev->notifier.waiting_list, asc_entry) {
397 entity = to_xvip_entity(asd);
398 ret = xvip_graph_parse_one(xdev, entity->asd.match.fwnode);
400 v4l2_async_nf_cleanup(&xdev->notifier);
408 static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
409 struct device_node *node)
411 struct xvip_dma *dma;
412 enum v4l2_buf_type type;
413 const char *direction;
417 ret = of_property_read_string(node, "direction", &direction);
421 if (strcmp(direction, "input") == 0)
422 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
423 else if (strcmp(direction, "output") == 0)
424 type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
428 of_property_read_u32(node, "reg", &index);
430 dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
434 ret = xvip_dma_init(xdev, dma, type, index);
436 dev_err(xdev->dev, "%pOF initialization failed\n", node);
440 list_add_tail(&dma->list, &xdev->dmas);
442 xdev->v4l2_caps |= type == V4L2_BUF_TYPE_VIDEO_CAPTURE
443 ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_VIDEO_OUTPUT;
448 static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
450 struct device_node *ports;
451 struct device_node *port;
454 ports = of_get_child_by_name(xdev->dev->of_node, "ports");
456 dev_err(xdev->dev, "ports node not present\n");
460 for_each_child_of_node(ports, port) {
461 ret = xvip_graph_dma_init_one(xdev, port);
472 static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
474 struct xvip_dma *dmap;
475 struct xvip_dma *dma;
477 v4l2_async_nf_unregister(&xdev->notifier);
478 v4l2_async_nf_cleanup(&xdev->notifier);
480 list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
481 xvip_dma_cleanup(dma);
482 list_del(&dma->list);
486 static int xvip_graph_init(struct xvip_composite_device *xdev)
490 /* Init the DMA channels. */
491 ret = xvip_graph_dma_init(xdev);
493 dev_err(xdev->dev, "DMA initialization failed\n");
497 v4l2_async_nf_init(&xdev->notifier);
499 /* Parse the graph to extract a list of subdevice DT nodes. */
500 ret = xvip_graph_parse(xdev);
502 dev_err(xdev->dev, "graph parsing failed\n");
506 if (list_empty(&xdev->notifier.waiting_list)) {
507 dev_err(xdev->dev, "no subdev found in graph\n");
512 /* Register the subdevices notifier. */
513 xdev->notifier.ops = &xvip_graph_notify_ops;
515 ret = v4l2_async_nf_register(&xdev->v4l2_dev, &xdev->notifier);
517 dev_err(xdev->dev, "notifier registration failed\n");
525 xvip_graph_cleanup(xdev);
530 /* -----------------------------------------------------------------------------
531 * Media Controller and V4L2
534 static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
536 v4l2_device_unregister(&xdev->v4l2_dev);
537 media_device_unregister(&xdev->media_dev);
538 media_device_cleanup(&xdev->media_dev);
541 static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
545 xdev->media_dev.dev = xdev->dev;
546 strscpy(xdev->media_dev.model, "Xilinx Video Composite Device",
547 sizeof(xdev->media_dev.model));
548 xdev->media_dev.hw_revision = 0;
550 media_device_init(&xdev->media_dev);
552 xdev->v4l2_dev.mdev = &xdev->media_dev;
553 ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
555 dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
557 media_device_cleanup(&xdev->media_dev);
564 /* -----------------------------------------------------------------------------
565 * Platform Device Driver
568 static int xvip_composite_probe(struct platform_device *pdev)
570 struct xvip_composite_device *xdev;
573 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
577 xdev->dev = &pdev->dev;
578 INIT_LIST_HEAD(&xdev->dmas);
580 ret = xvip_composite_v4l2_init(xdev);
584 ret = xvip_graph_init(xdev);
588 platform_set_drvdata(pdev, xdev);
590 dev_info(xdev->dev, "device registered\n");
595 xvip_composite_v4l2_cleanup(xdev);
599 static void xvip_composite_remove(struct platform_device *pdev)
601 struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
603 xvip_graph_cleanup(xdev);
604 xvip_composite_v4l2_cleanup(xdev);
607 static const struct of_device_id xvip_composite_of_id_table[] = {
608 { .compatible = "xlnx,video" },
611 MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
613 static struct platform_driver xvip_composite_driver = {
615 .name = "xilinx-video",
616 .of_match_table = xvip_composite_of_id_table,
618 .probe = xvip_composite_probe,
619 .remove_new = xvip_composite_remove,
622 module_platform_driver(xvip_composite_driver);
624 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
625 MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
626 MODULE_LICENSE("GPL v2");