1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Video IP Composite Device
4  *
5  * Copyright (C) 2013-2015 Ideas on Board
6  * Copyright (C) 2013-2015 Xilinx, Inc.
7  *
8  * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
9  *           Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10  */
11 
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 
19 #include <media/v4l2-async.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-fwnode.h>
23 
24 #include "xilinx-dma.h"
25 #include "xilinx-vipp.h"
26 
27 #define XVIPP_DMA_S2MM				0
28 #define XVIPP_DMA_MM2S				1
29 
30 /**
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
35  */
36 struct xvip_graph_entity {
37 	struct v4l2_async_connection asd; /* must be first */
38 	struct media_entity *entity;
39 	struct v4l2_subdev *subdev;
40 };
41 
42 static inline struct xvip_graph_entity *
43 to_xvip_entity(struct v4l2_async_connection *asd)
44 {
45 	return container_of(asd, struct xvip_graph_entity, asd);
46 }
47 
48 /* -----------------------------------------------------------------------------
49  * Graph Management
50  */
51 
52 static struct xvip_graph_entity *
53 xvip_graph_find_entity(struct xvip_composite_device *xdev,
54 		       const struct fwnode_handle *fwnode)
55 {
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
61 	};
62 	unsigned int i;
63 
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)
68 				return entity;
69 		}
70 	}
71 
72 	return NULL;
73 }
74 
75 static int xvip_graph_build_one(struct xvip_composite_device *xdev,
76 				struct xvip_graph_entity *entity)
77 {
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;
86 	int ret = 0;
87 
88 	dev_dbg(xdev->dev, "creating links for entity %s\n", local->name);
89 
90 	while (1) {
91 		/* Get the next endpoint and parse its link. */
92 		ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
93 						    ep);
94 		if (ep == NULL)
95 			break;
96 
97 		dev_dbg(xdev->dev, "processing endpoint %p\n", ep);
98 
99 		ret = v4l2_fwnode_parse_link(ep, &link);
100 		if (ret < 0) {
101 			dev_err(xdev->dev, "failed to parse link for %p\n",
102 				ep);
103 			continue;
104 		}
105 
106 		/* Skip sink ports, they will be processed from the other end of
107 		 * the link.
108 		 */
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);
113 			ret = -EINVAL;
114 			break;
115 		}
116 
117 		local_pad = &local->pads[link.local_port];
118 
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);
123 			continue;
124 		}
125 
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);
131 			continue;
132 		}
133 
134 		/* Find the remote entity. */
135 		ent = xvip_graph_find_entity(xdev, link.remote_node);
136 		if (ent == NULL) {
137 			dev_err(xdev->dev, "no entity found for %p\n",
138 				link.remote_node);
139 			v4l2_fwnode_put_link(&link);
140 			ret = -ENODEV;
141 			break;
142 		}
143 
144 		remote = ent->entity;
145 
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);
150 			ret = -EINVAL;
151 			break;
152 		}
153 
154 		remote_pad = &remote->pads[link.remote_port];
155 
156 		v4l2_fwnode_put_link(&link);
157 
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);
162 
163 		ret = media_create_pad_link(local, local_pad->index,
164 					       remote, remote_pad->index,
165 					       link_flags);
166 		if (ret < 0) {
167 			dev_err(xdev->dev,
168 				"failed to create %s:%u -> %s:%u link\n",
169 				local->name, local_pad->index,
170 				remote->name, remote_pad->index);
171 			break;
172 		}
173 	}
174 
175 	fwnode_handle_put(ep);
176 	return ret;
177 }
178 
179 static struct xvip_dma *
180 xvip_graph_find_dma(struct xvip_composite_device *xdev, unsigned int port)
181 {
182 	struct xvip_dma *dma;
183 
184 	list_for_each_entry(dma, &xdev->dmas, list) {
185 		if (dma->port == port)
186 			return dma;
187 	}
188 
189 	return NULL;
190 }
191 
192 static int xvip_graph_build_dma(struct xvip_composite_device *xdev)
193 {
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;
204 	int ret = 0;
205 
206 	dev_dbg(xdev->dev, "creating links for DMA engines\n");
207 
208 	while (1) {
209 		/* Get the next endpoint and parse its link. */
210 		ep = of_graph_get_next_endpoint(node, ep);
211 		if (ep == NULL)
212 			break;
213 
214 		dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
215 
216 		ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
217 		if (ret < 0) {
218 			dev_err(xdev->dev, "failed to parse link for %pOF\n",
219 				ep);
220 			continue;
221 		}
222 
223 		/* Find the DMA engine. */
224 		dma = xvip_graph_find_dma(xdev, link.local_port);
225 		if (dma == NULL) {
226 			dev_err(xdev->dev, "no DMA engine found for port %u\n",
227 				link.local_port);
228 			v4l2_fwnode_put_link(&link);
229 			ret = -EINVAL;
230 			break;
231 		}
232 
233 		dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
234 			dma->video.name);
235 
236 		/* Find the remote entity. */
237 		ent = xvip_graph_find_entity(xdev, link.remote_node);
238 		if (ent == NULL) {
239 			dev_err(xdev->dev, "no entity found for %pOF\n",
240 				to_of_node(link.remote_node));
241 			v4l2_fwnode_put_link(&link);
242 			ret = -ENODEV;
243 			break;
244 		}
245 
246 		if (link.remote_port >= ent->entity->num_pads) {
247 			dev_err(xdev->dev, "invalid port number %u on %pOF\n",
248 				link.remote_port,
249 				to_of_node(link.remote_node));
250 			v4l2_fwnode_put_link(&link);
251 			ret = -EINVAL;
252 			break;
253 		}
254 
255 		if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
256 			source = &dma->video.entity;
257 			source_pad = &dma->pad;
258 			sink = ent->entity;
259 			sink_pad = &sink->pads[link.remote_port];
260 		} else {
261 			source = ent->entity;
262 			source_pad = &source->pads[link.remote_port];
263 			sink = &dma->video.entity;
264 			sink_pad = &dma->pad;
265 		}
266 
267 		v4l2_fwnode_put_link(&link);
268 
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);
273 
274 		ret = media_create_pad_link(source, source_pad->index,
275 					       sink, sink_pad->index,
276 					       link_flags);
277 		if (ret < 0) {
278 			dev_err(xdev->dev,
279 				"failed to create %s:%u -> %s:%u link\n",
280 				source->name, source_pad->index,
281 				sink->name, sink_pad->index);
282 			break;
283 		}
284 	}
285 
286 	of_node_put(ep);
287 	return ret;
288 }
289 
290 static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
291 {
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;
296 	int ret;
297 
298 	dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
299 
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);
304 		if (ret < 0)
305 			return ret;
306 	}
307 
308 	/* Create links for DMA channels. */
309 	ret = xvip_graph_build_dma(xdev);
310 	if (ret < 0)
311 		return ret;
312 
313 	ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
314 	if (ret < 0)
315 		dev_err(xdev->dev, "failed to register subdev nodes\n");
316 
317 	return media_device_register(&xdev->media_dev);
318 }
319 
320 static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
321 				   struct v4l2_subdev *subdev,
322 				   struct v4l2_async_connection *asc)
323 {
324 	struct xvip_graph_entity *entity = to_xvip_entity(asc);
325 
326 	entity->entity = &subdev->entity;
327 	entity->subdev = subdev;
328 
329 	return 0;
330 }
331 
332 static const struct v4l2_async_notifier_operations xvip_graph_notify_ops = {
333 	.bound = xvip_graph_notify_bound,
334 	.complete = xvip_graph_notify_complete,
335 };
336 
337 static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
338 				struct fwnode_handle *fwnode)
339 {
340 	struct fwnode_handle *remote;
341 	struct fwnode_handle *ep = NULL;
342 	int ret = 0;
343 
344 	dev_dbg(xdev->dev, "parsing node %p\n", fwnode);
345 
346 	while (1) {
347 		struct xvip_graph_entity *xge;
348 
349 		ep = fwnode_graph_get_next_endpoint(fwnode, ep);
350 		if (ep == NULL)
351 			break;
352 
353 		dev_dbg(xdev->dev, "handling endpoint %p\n", ep);
354 
355 		remote = fwnode_graph_get_remote_port_parent(ep);
356 		if (remote == NULL) {
357 			ret = -EINVAL;
358 			goto err_notifier_cleanup;
359 		}
360 
361 		fwnode_handle_put(ep);
362 
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);
367 			continue;
368 		}
369 
370 		xge = v4l2_async_nf_add_fwnode(&xdev->notifier, remote,
371 					       struct xvip_graph_entity);
372 		fwnode_handle_put(remote);
373 		if (IS_ERR(xge)) {
374 			ret = PTR_ERR(xge);
375 			goto err_notifier_cleanup;
376 		}
377 	}
378 
379 	return 0;
380 
381 err_notifier_cleanup:
382 	v4l2_async_nf_cleanup(&xdev->notifier);
383 	fwnode_handle_put(ep);
384 	return ret;
385 }
386 
387 static int xvip_graph_parse(struct xvip_composite_device *xdev)
388 {
389 	struct xvip_graph_entity *entity;
390 	struct v4l2_async_connection *asd;
391 	int ret;
392 
393 	/*
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
397 	 * the links.
398 	 */
399 	ret = xvip_graph_parse_one(xdev, of_fwnode_handle(xdev->dev->of_node));
400 	if (ret < 0)
401 		return 0;
402 
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);
406 		if (ret < 0) {
407 			v4l2_async_nf_cleanup(&xdev->notifier);
408 			break;
409 		}
410 	}
411 
412 	return ret;
413 }
414 
415 static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
416 				   struct device_node *node)
417 {
418 	struct xvip_dma *dma;
419 	enum v4l2_buf_type type;
420 	const char *direction;
421 	unsigned int index;
422 	int ret;
423 
424 	ret = of_property_read_string(node, "direction", &direction);
425 	if (ret < 0)
426 		return ret;
427 
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;
432 	else
433 		return -EINVAL;
434 
435 	of_property_read_u32(node, "reg", &index);
436 
437 	dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
438 	if (dma == NULL)
439 		return -ENOMEM;
440 
441 	ret = xvip_dma_init(xdev, dma, type, index);
442 	if (ret < 0) {
443 		dev_err(xdev->dev, "%pOF initialization failed\n", node);
444 		return ret;
445 	}
446 
447 	list_add_tail(&dma->list, &xdev->dmas);
448 
449 	xdev->v4l2_caps |= type == V4L2_BUF_TYPE_VIDEO_CAPTURE
450 			 ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_VIDEO_OUTPUT;
451 
452 	return 0;
453 }
454 
455 static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
456 {
457 	struct device_node *ports;
458 	struct device_node *port;
459 	int ret = 0;
460 
461 	ports = of_get_child_by_name(xdev->dev->of_node, "ports");
462 	if (ports == NULL) {
463 		dev_err(xdev->dev, "ports node not present\n");
464 		return -EINVAL;
465 	}
466 
467 	for_each_child_of_node(ports, port) {
468 		ret = xvip_graph_dma_init_one(xdev, port);
469 		if (ret) {
470 			of_node_put(port);
471 			break;
472 		}
473 	}
474 
475 	of_node_put(ports);
476 	return ret;
477 }
478 
479 static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
480 {
481 	struct xvip_dma *dmap;
482 	struct xvip_dma *dma;
483 
484 	v4l2_async_nf_unregister(&xdev->notifier);
485 	v4l2_async_nf_cleanup(&xdev->notifier);
486 
487 	list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
488 		xvip_dma_cleanup(dma);
489 		list_del(&dma->list);
490 	}
491 }
492 
493 static int xvip_graph_init(struct xvip_composite_device *xdev)
494 {
495 	int ret;
496 
497 	/* Init the DMA channels. */
498 	ret = xvip_graph_dma_init(xdev);
499 	if (ret < 0) {
500 		dev_err(xdev->dev, "DMA initialization failed\n");
501 		goto done;
502 	}
503 
504 	v4l2_async_nf_init(&xdev->notifier, &xdev->v4l2_dev);
505 
506 	/* Parse the graph to extract a list of subdevice DT nodes. */
507 	ret = xvip_graph_parse(xdev);
508 	if (ret < 0) {
509 		dev_err(xdev->dev, "graph parsing failed\n");
510 		goto done;
511 	}
512 
513 	if (list_empty(&xdev->notifier.waiting_list)) {
514 		dev_err(xdev->dev, "no subdev found in graph\n");
515 		ret = -ENOENT;
516 		goto done;
517 	}
518 
519 	/* Register the subdevices notifier. */
520 	xdev->notifier.ops = &xvip_graph_notify_ops;
521 
522 	ret = v4l2_async_nf_register(&xdev->notifier);
523 	if (ret < 0) {
524 		dev_err(xdev->dev, "notifier registration failed\n");
525 		goto done;
526 	}
527 
528 	ret = 0;
529 
530 done:
531 	if (ret < 0)
532 		xvip_graph_cleanup(xdev);
533 
534 	return ret;
535 }
536 
537 /* -----------------------------------------------------------------------------
538  * Media Controller and V4L2
539  */
540 
541 static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
542 {
543 	v4l2_device_unregister(&xdev->v4l2_dev);
544 	media_device_unregister(&xdev->media_dev);
545 	media_device_cleanup(&xdev->media_dev);
546 }
547 
548 static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
549 {
550 	int ret;
551 
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;
556 
557 	media_device_init(&xdev->media_dev);
558 
559 	xdev->v4l2_dev.mdev = &xdev->media_dev;
560 	ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
561 	if (ret < 0) {
562 		dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
563 			ret);
564 		media_device_cleanup(&xdev->media_dev);
565 		return ret;
566 	}
567 
568 	return 0;
569 }
570 
571 /* -----------------------------------------------------------------------------
572  * Platform Device Driver
573  */
574 
575 static int xvip_composite_probe(struct platform_device *pdev)
576 {
577 	struct xvip_composite_device *xdev;
578 	int ret;
579 
580 	xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
581 	if (!xdev)
582 		return -ENOMEM;
583 
584 	xdev->dev = &pdev->dev;
585 	INIT_LIST_HEAD(&xdev->dmas);
586 
587 	ret = xvip_composite_v4l2_init(xdev);
588 	if (ret < 0)
589 		return ret;
590 
591 	ret = xvip_graph_init(xdev);
592 	if (ret < 0)
593 		goto error;
594 
595 	platform_set_drvdata(pdev, xdev);
596 
597 	dev_info(xdev->dev, "device registered\n");
598 
599 	return 0;
600 
601 error:
602 	xvip_composite_v4l2_cleanup(xdev);
603 	return ret;
604 }
605 
606 static void xvip_composite_remove(struct platform_device *pdev)
607 {
608 	struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
609 
610 	xvip_graph_cleanup(xdev);
611 	xvip_composite_v4l2_cleanup(xdev);
612 }
613 
614 static const struct of_device_id xvip_composite_of_id_table[] = {
615 	{ .compatible = "xlnx,video" },
616 	{ }
617 };
618 MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
619 
620 static struct platform_driver xvip_composite_driver = {
621 	.driver = {
622 		.name = "xilinx-video",
623 		.of_match_table = xvip_composite_of_id_table,
624 	},
625 	.probe = xvip_composite_probe,
626 	.remove_new = xvip_composite_remove,
627 };
628 
629 module_platform_driver(xvip_composite_driver);
630 
631 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
632 MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
633 MODULE_LICENSE("GPL v2");
634