1 /*
2  * Xilinx Video IP Composite Device
3  *
4  * Copyright (C) 2013-2015 Ideas on Board
5  * Copyright (C) 2013-2015 Xilinx, Inc.
6  *
7  * Contacts: Hyun Kwon <hyun.kwon@xilinx.com>
8  *           Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9  *
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.
13  */
14 
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/of_graph.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-fwnode.h>
26 
27 #include "xilinx-dma.h"
28 #include "xilinx-vipp.h"
29 
30 #define XVIPP_DMA_S2MM				0
31 #define XVIPP_DMA_MM2S				1
32 
33 /**
34  * struct xvip_graph_entity - Entity in the video graph
35  * @list: list entry in a graph entities list
36  * @node: the entity's DT node
37  * @entity: media entity, from the corresponding V4L2 subdev
38  * @asd: subdev asynchronous registration information
39  * @subdev: V4L2 subdev
40  */
41 struct xvip_graph_entity {
42 	struct list_head list;
43 	struct device_node *node;
44 	struct media_entity *entity;
45 
46 	struct v4l2_async_subdev asd;
47 	struct v4l2_subdev *subdev;
48 };
49 
50 /* -----------------------------------------------------------------------------
51  * Graph Management
52  */
53 
54 static struct xvip_graph_entity *
55 xvip_graph_find_entity(struct xvip_composite_device *xdev,
56 		       const struct device_node *node)
57 {
58 	struct xvip_graph_entity *entity;
59 
60 	list_for_each_entry(entity, &xdev->entities, list) {
61 		if (entity->node == node)
62 			return entity;
63 	}
64 
65 	return NULL;
66 }
67 
68 static int xvip_graph_build_one(struct xvip_composite_device *xdev,
69 				struct xvip_graph_entity *entity)
70 {
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 device_node *ep = NULL;
79 	struct device_node *next;
80 	int ret = 0;
81 
82 	dev_dbg(xdev->dev, "creating links for entity %s\n", local->name);
83 
84 	while (1) {
85 		/* Get the next endpoint and parse its link. */
86 		next = of_graph_get_next_endpoint(entity->node, ep);
87 		if (next == NULL)
88 			break;
89 
90 		of_node_put(ep);
91 		ep = next;
92 
93 		dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
94 
95 		ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
96 		if (ret < 0) {
97 			dev_err(xdev->dev, "failed to parse link for %pOF\n",
98 				ep);
99 			continue;
100 		}
101 
102 		/* Skip sink ports, they will be processed from the other end of
103 		 * the link.
104 		 */
105 		if (link.local_port >= local->num_pads) {
106 			dev_err(xdev->dev, "invalid port number %u for %pOF\n",
107 				link.local_port,
108 				to_of_node(link.local_node));
109 			v4l2_fwnode_put_link(&link);
110 			ret = -EINVAL;
111 			break;
112 		}
113 
114 		local_pad = &local->pads[link.local_port];
115 
116 		if (local_pad->flags & MEDIA_PAD_FL_SINK) {
117 			dev_dbg(xdev->dev, "skipping sink port %pOF:%u\n",
118 				to_of_node(link.local_node),
119 				link.local_port);
120 			v4l2_fwnode_put_link(&link);
121 			continue;
122 		}
123 
124 		/* Skip DMA engines, they will be processed separately. */
125 		if (link.remote_node == of_fwnode_handle(xdev->dev->of_node)) {
126 			dev_dbg(xdev->dev, "skipping DMA port %pOF:%u\n",
127 				to_of_node(link.local_node),
128 				link.local_port);
129 			v4l2_fwnode_put_link(&link);
130 			continue;
131 		}
132 
133 		/* Find the remote entity. */
134 		ent = xvip_graph_find_entity(xdev,
135 					     to_of_node(link.remote_node));
136 		if (ent == NULL) {
137 			dev_err(xdev->dev, "no entity found for %pOF\n",
138 				to_of_node(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 %pOF\n",
148 				link.remote_port, to_of_node(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 	of_node_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 device_node *next;
204 	struct xvip_dma *dma;
205 	int ret = 0;
206 
207 	dev_dbg(xdev->dev, "creating links for DMA engines\n");
208 
209 	while (1) {
210 		/* Get the next endpoint and parse its link. */
211 		next = of_graph_get_next_endpoint(node, ep);
212 		if (next == NULL)
213 			break;
214 
215 		of_node_put(ep);
216 		ep = next;
217 
218 		dev_dbg(xdev->dev, "processing endpoint %pOF\n", ep);
219 
220 		ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
221 		if (ret < 0) {
222 			dev_err(xdev->dev, "failed to parse link for %pOF\n",
223 				ep);
224 			continue;
225 		}
226 
227 		/* Find the DMA engine. */
228 		dma = xvip_graph_find_dma(xdev, link.local_port);
229 		if (dma == NULL) {
230 			dev_err(xdev->dev, "no DMA engine found for port %u\n",
231 				link.local_port);
232 			v4l2_fwnode_put_link(&link);
233 			ret = -EINVAL;
234 			break;
235 		}
236 
237 		dev_dbg(xdev->dev, "creating link for DMA engine %s\n",
238 			dma->video.name);
239 
240 		/* Find the remote entity. */
241 		ent = xvip_graph_find_entity(xdev,
242 					     to_of_node(link.remote_node));
243 		if (ent == NULL) {
244 			dev_err(xdev->dev, "no entity found for %pOF\n",
245 				to_of_node(link.remote_node));
246 			v4l2_fwnode_put_link(&link);
247 			ret = -ENODEV;
248 			break;
249 		}
250 
251 		if (link.remote_port >= ent->entity->num_pads) {
252 			dev_err(xdev->dev, "invalid port number %u on %pOF\n",
253 				link.remote_port,
254 				to_of_node(link.remote_node));
255 			v4l2_fwnode_put_link(&link);
256 			ret = -EINVAL;
257 			break;
258 		}
259 
260 		if (dma->pad.flags & MEDIA_PAD_FL_SOURCE) {
261 			source = &dma->video.entity;
262 			source_pad = &dma->pad;
263 			sink = ent->entity;
264 			sink_pad = &sink->pads[link.remote_port];
265 		} else {
266 			source = ent->entity;
267 			source_pad = &source->pads[link.remote_port];
268 			sink = &dma->video.entity;
269 			sink_pad = &dma->pad;
270 		}
271 
272 		v4l2_fwnode_put_link(&link);
273 
274 		/* Create the media link. */
275 		dev_dbg(xdev->dev, "creating %s:%u -> %s:%u link\n",
276 			source->name, source_pad->index,
277 			sink->name, sink_pad->index);
278 
279 		ret = media_create_pad_link(source, source_pad->index,
280 					       sink, sink_pad->index,
281 					       link_flags);
282 		if (ret < 0) {
283 			dev_err(xdev->dev,
284 				"failed to create %s:%u -> %s:%u link\n",
285 				source->name, source_pad->index,
286 				sink->name, sink_pad->index);
287 			break;
288 		}
289 	}
290 
291 	of_node_put(ep);
292 	return ret;
293 }
294 
295 static int xvip_graph_notify_complete(struct v4l2_async_notifier *notifier)
296 {
297 	struct xvip_composite_device *xdev =
298 		container_of(notifier, struct xvip_composite_device, notifier);
299 	struct xvip_graph_entity *entity;
300 	int ret;
301 
302 	dev_dbg(xdev->dev, "notify complete, all subdevs registered\n");
303 
304 	/* Create links for every entity. */
305 	list_for_each_entry(entity, &xdev->entities, list) {
306 		ret = xvip_graph_build_one(xdev, entity);
307 		if (ret < 0)
308 			return ret;
309 	}
310 
311 	/* Create links for DMA channels. */
312 	ret = xvip_graph_build_dma(xdev);
313 	if (ret < 0)
314 		return ret;
315 
316 	ret = v4l2_device_register_subdev_nodes(&xdev->v4l2_dev);
317 	if (ret < 0)
318 		dev_err(xdev->dev, "failed to register subdev nodes\n");
319 
320 	return media_device_register(&xdev->media_dev);
321 }
322 
323 static int xvip_graph_notify_bound(struct v4l2_async_notifier *notifier,
324 				   struct v4l2_subdev *subdev,
325 				   struct v4l2_async_subdev *asd)
326 {
327 	struct xvip_composite_device *xdev =
328 		container_of(notifier, struct xvip_composite_device, notifier);
329 	struct xvip_graph_entity *entity;
330 
331 	/* Locate the entity corresponding to the bound subdev and store the
332 	 * subdev pointer.
333 	 */
334 	list_for_each_entry(entity, &xdev->entities, list) {
335 		if (entity->node != subdev->dev->of_node)
336 			continue;
337 
338 		if (entity->subdev) {
339 			dev_err(xdev->dev, "duplicate subdev for node %pOF\n",
340 				entity->node);
341 			return -EINVAL;
342 		}
343 
344 		dev_dbg(xdev->dev, "subdev %s bound\n", subdev->name);
345 		entity->entity = &subdev->entity;
346 		entity->subdev = subdev;
347 		return 0;
348 	}
349 
350 	dev_err(xdev->dev, "no entity for subdev %s\n", subdev->name);
351 	return -EINVAL;
352 }
353 
354 static const struct v4l2_async_notifier_operations xvip_graph_notify_ops = {
355 	.bound = xvip_graph_notify_bound,
356 	.complete = xvip_graph_notify_complete,
357 };
358 
359 static int xvip_graph_parse_one(struct xvip_composite_device *xdev,
360 				struct device_node *node)
361 {
362 	struct xvip_graph_entity *entity;
363 	struct device_node *remote;
364 	struct device_node *ep = NULL;
365 	int ret = 0;
366 
367 	dev_dbg(xdev->dev, "parsing node %pOF\n", node);
368 
369 	while (1) {
370 		ep = of_graph_get_next_endpoint(node, ep);
371 		if (ep == NULL)
372 			break;
373 
374 		dev_dbg(xdev->dev, "handling endpoint %pOF\n", ep);
375 
376 		remote = of_graph_get_remote_port_parent(ep);
377 		if (remote == NULL) {
378 			ret = -EINVAL;
379 			break;
380 		}
381 
382 		/* Skip entities that we have already processed. */
383 		if (remote == xdev->dev->of_node ||
384 		    xvip_graph_find_entity(xdev, remote)) {
385 			of_node_put(remote);
386 			continue;
387 		}
388 
389 		entity = devm_kzalloc(xdev->dev, sizeof(*entity), GFP_KERNEL);
390 		if (entity == NULL) {
391 			of_node_put(remote);
392 			ret = -ENOMEM;
393 			break;
394 		}
395 
396 		entity->node = remote;
397 		entity->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
398 		entity->asd.match.fwnode.fwnode = of_fwnode_handle(remote);
399 		list_add_tail(&entity->list, &xdev->entities);
400 		xdev->num_subdevs++;
401 	}
402 
403 	of_node_put(ep);
404 	return ret;
405 }
406 
407 static int xvip_graph_parse(struct xvip_composite_device *xdev)
408 {
409 	struct xvip_graph_entity *entity;
410 	int ret;
411 
412 	/*
413 	 * Walk the links to parse the full graph. Start by parsing the
414 	 * composite node and then parse entities in turn. The list_for_each
415 	 * loop will handle entities added at the end of the list while walking
416 	 * the links.
417 	 */
418 	ret = xvip_graph_parse_one(xdev, xdev->dev->of_node);
419 	if (ret < 0)
420 		return 0;
421 
422 	list_for_each_entry(entity, &xdev->entities, list) {
423 		ret = xvip_graph_parse_one(xdev, entity->node);
424 		if (ret < 0)
425 			break;
426 	}
427 
428 	return ret;
429 }
430 
431 static int xvip_graph_dma_init_one(struct xvip_composite_device *xdev,
432 				   struct device_node *node)
433 {
434 	struct xvip_dma *dma;
435 	enum v4l2_buf_type type;
436 	const char *direction;
437 	unsigned int index;
438 	int ret;
439 
440 	ret = of_property_read_string(node, "direction", &direction);
441 	if (ret < 0)
442 		return ret;
443 
444 	if (strcmp(direction, "input") == 0)
445 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
446 	else if (strcmp(direction, "output") == 0)
447 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
448 	else
449 		return -EINVAL;
450 
451 	of_property_read_u32(node, "reg", &index);
452 
453 	dma = devm_kzalloc(xdev->dev, sizeof(*dma), GFP_KERNEL);
454 	if (dma == NULL)
455 		return -ENOMEM;
456 
457 	ret = xvip_dma_init(xdev, dma, type, index);
458 	if (ret < 0) {
459 		dev_err(xdev->dev, "%pOF initialization failed\n", node);
460 		return ret;
461 	}
462 
463 	list_add_tail(&dma->list, &xdev->dmas);
464 
465 	xdev->v4l2_caps |= type == V4L2_BUF_TYPE_VIDEO_CAPTURE
466 			 ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_VIDEO_OUTPUT;
467 
468 	return 0;
469 }
470 
471 static int xvip_graph_dma_init(struct xvip_composite_device *xdev)
472 {
473 	struct device_node *ports;
474 	struct device_node *port;
475 	int ret;
476 
477 	ports = of_get_child_by_name(xdev->dev->of_node, "ports");
478 	if (ports == NULL) {
479 		dev_err(xdev->dev, "ports node not present\n");
480 		return -EINVAL;
481 	}
482 
483 	for_each_child_of_node(ports, port) {
484 		ret = xvip_graph_dma_init_one(xdev, port);
485 		if (ret < 0) {
486 			of_node_put(port);
487 			return ret;
488 		}
489 	}
490 
491 	return 0;
492 }
493 
494 static void xvip_graph_cleanup(struct xvip_composite_device *xdev)
495 {
496 	struct xvip_graph_entity *entityp;
497 	struct xvip_graph_entity *entity;
498 	struct xvip_dma *dmap;
499 	struct xvip_dma *dma;
500 
501 	v4l2_async_notifier_unregister(&xdev->notifier);
502 
503 	list_for_each_entry_safe(entity, entityp, &xdev->entities, list) {
504 		of_node_put(entity->node);
505 		list_del(&entity->list);
506 	}
507 
508 	list_for_each_entry_safe(dma, dmap, &xdev->dmas, list) {
509 		xvip_dma_cleanup(dma);
510 		list_del(&dma->list);
511 	}
512 }
513 
514 static int xvip_graph_init(struct xvip_composite_device *xdev)
515 {
516 	struct xvip_graph_entity *entity;
517 	struct v4l2_async_subdev **subdevs = NULL;
518 	unsigned int num_subdevs;
519 	unsigned int i;
520 	int ret;
521 
522 	/* Init the DMA channels. */
523 	ret = xvip_graph_dma_init(xdev);
524 	if (ret < 0) {
525 		dev_err(xdev->dev, "DMA initialization failed\n");
526 		goto done;
527 	}
528 
529 	/* Parse the graph to extract a list of subdevice DT nodes. */
530 	ret = xvip_graph_parse(xdev);
531 	if (ret < 0) {
532 		dev_err(xdev->dev, "graph parsing failed\n");
533 		goto done;
534 	}
535 
536 	if (!xdev->num_subdevs) {
537 		dev_err(xdev->dev, "no subdev found in graph\n");
538 		goto done;
539 	}
540 
541 	/* Register the subdevices notifier. */
542 	num_subdevs = xdev->num_subdevs;
543 	subdevs = devm_kzalloc(xdev->dev, sizeof(*subdevs) * num_subdevs,
544 			       GFP_KERNEL);
545 	if (subdevs == NULL) {
546 		ret = -ENOMEM;
547 		goto done;
548 	}
549 
550 	i = 0;
551 	list_for_each_entry(entity, &xdev->entities, list)
552 		subdevs[i++] = &entity->asd;
553 
554 	xdev->notifier.subdevs = subdevs;
555 	xdev->notifier.num_subdevs = num_subdevs;
556 	xdev->notifier.ops = &xvip_graph_notify_ops;
557 
558 	ret = v4l2_async_notifier_register(&xdev->v4l2_dev, &xdev->notifier);
559 	if (ret < 0) {
560 		dev_err(xdev->dev, "notifier registration failed\n");
561 		goto done;
562 	}
563 
564 	ret = 0;
565 
566 done:
567 	if (ret < 0)
568 		xvip_graph_cleanup(xdev);
569 
570 	return ret;
571 }
572 
573 /* -----------------------------------------------------------------------------
574  * Media Controller and V4L2
575  */
576 
577 static void xvip_composite_v4l2_cleanup(struct xvip_composite_device *xdev)
578 {
579 	v4l2_device_unregister(&xdev->v4l2_dev);
580 	media_device_unregister(&xdev->media_dev);
581 	media_device_cleanup(&xdev->media_dev);
582 }
583 
584 static int xvip_composite_v4l2_init(struct xvip_composite_device *xdev)
585 {
586 	int ret;
587 
588 	xdev->media_dev.dev = xdev->dev;
589 	strlcpy(xdev->media_dev.model, "Xilinx Video Composite Device",
590 		sizeof(xdev->media_dev.model));
591 	xdev->media_dev.hw_revision = 0;
592 
593 	media_device_init(&xdev->media_dev);
594 
595 	xdev->v4l2_dev.mdev = &xdev->media_dev;
596 	ret = v4l2_device_register(xdev->dev, &xdev->v4l2_dev);
597 	if (ret < 0) {
598 		dev_err(xdev->dev, "V4L2 device registration failed (%d)\n",
599 			ret);
600 		media_device_cleanup(&xdev->media_dev);
601 		return ret;
602 	}
603 
604 	return 0;
605 }
606 
607 /* -----------------------------------------------------------------------------
608  * Platform Device Driver
609  */
610 
611 static int xvip_composite_probe(struct platform_device *pdev)
612 {
613 	struct xvip_composite_device *xdev;
614 	int ret;
615 
616 	xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
617 	if (!xdev)
618 		return -ENOMEM;
619 
620 	xdev->dev = &pdev->dev;
621 	INIT_LIST_HEAD(&xdev->entities);
622 	INIT_LIST_HEAD(&xdev->dmas);
623 
624 	ret = xvip_composite_v4l2_init(xdev);
625 	if (ret < 0)
626 		return ret;
627 
628 	ret = xvip_graph_init(xdev);
629 	if (ret < 0)
630 		goto error;
631 
632 	platform_set_drvdata(pdev, xdev);
633 
634 	dev_info(xdev->dev, "device registered\n");
635 
636 	return 0;
637 
638 error:
639 	xvip_composite_v4l2_cleanup(xdev);
640 	return ret;
641 }
642 
643 static int xvip_composite_remove(struct platform_device *pdev)
644 {
645 	struct xvip_composite_device *xdev = platform_get_drvdata(pdev);
646 
647 	xvip_graph_cleanup(xdev);
648 	xvip_composite_v4l2_cleanup(xdev);
649 
650 	return 0;
651 }
652 
653 static const struct of_device_id xvip_composite_of_id_table[] = {
654 	{ .compatible = "xlnx,video" },
655 	{ }
656 };
657 MODULE_DEVICE_TABLE(of, xvip_composite_of_id_table);
658 
659 static struct platform_driver xvip_composite_driver = {
660 	.driver = {
661 		.name = "xilinx-video",
662 		.of_match_table = xvip_composite_of_id_table,
663 	},
664 	.probe = xvip_composite_probe,
665 	.remove = xvip_composite_remove,
666 };
667 
668 module_platform_driver(xvip_composite_driver);
669 
670 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
671 MODULE_DESCRIPTION("Xilinx Video IP Composite Driver");
672 MODULE_LICENSE("GPL v2");
673