2 * NVIDIA Media controller graph management
4 * Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Bryan Wu <pengw@nvidia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/clk.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
17 #include <linux/of_graph.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/reset.h>
22 #include <linux/slab.h>
23 #include <linux/tegra-soc.h>
24 #include <linux/tegra_pm_domains.h>
26 #include <media/media-device.h>
27 #include <media/v4l2-async.h>
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-of.h>
31 #include <media/tegra_v4l2_camera.h>
33 #include "camera/mc_common.h"
37 /* -----------------------------------------------------------------------------
41 static struct tegra_vi_graph_entity *
42 tegra_vi_graph_find_entity(struct tegra_mc_vi *vi,
43 const struct device_node *node)
45 struct tegra_vi_graph_entity *entity;
47 list_for_each_entry(entity, &vi->entities, list) {
48 if (entity->node == node)
55 static int tegra_vi_graph_build_one(struct tegra_mc_vi *vi,
56 struct tegra_vi_graph_entity *entity)
58 u32 link_flags = MEDIA_LNK_FL_ENABLED;
59 struct media_entity *local = entity->entity;
60 struct media_entity *remote;
61 struct media_pad *local_pad;
62 struct media_pad *remote_pad;
63 struct tegra_vi_graph_entity *ent;
64 struct v4l2_of_link link;
65 struct device_node *ep = NULL;
66 struct device_node *next;
69 dev_info(vi->dev, "creating links for entity %s\n", local->name);
72 /* Get the next endpoint and parse its link. */
73 next = of_graph_get_next_endpoint(entity->node, ep);
80 dev_info(vi->dev, "processing endpoint %s\n", ep->full_name);
82 ret = v4l2_of_parse_link(ep, &link);
84 dev_err(vi->dev, "failed to parse link for %s\n",
89 /* Skip sink ports, they will be processed from the other end of
92 if (link.local_port >= local->num_pads) {
93 dev_err(vi->dev, "invalid port number %u on %s\n",
94 link.local_port, link.local_node->full_name);
95 v4l2_of_put_link(&link);
100 local_pad = &local->pads[link.local_port];
102 if (local_pad->flags & MEDIA_PAD_FL_SINK) {
103 dev_info(vi->dev, "skipping sink port %s:%u\n",
104 link.local_node->full_name, link.local_port);
105 v4l2_of_put_link(&link);
109 /* Skip channel entity , they will be processed separately. */
110 if (link.remote_node == vi->dev->of_node) {
111 dev_info(vi->dev, "skipping channel port %s:%u\n",
112 link.local_node->full_name, link.local_port);
113 v4l2_of_put_link(&link);
117 /* Find the remote entity. */
118 ent = tegra_vi_graph_find_entity(vi, link.remote_node);
120 dev_err(vi->dev, "no entity found for %s\n",
121 link.remote_node->full_name);
122 v4l2_of_put_link(&link);
127 remote = ent->entity;
129 if (link.remote_port >= remote->num_pads) {
130 dev_err(vi->dev, "invalid port number %u on %s\n",
131 link.remote_port, link.remote_node->full_name);
132 v4l2_of_put_link(&link);
137 remote_pad = &remote->pads[link.remote_port];
139 v4l2_of_put_link(&link);
141 /* Create the media link. */
142 dev_info(vi->dev, "creating %s:%u -> %s:%u link\n",
143 local->name, local_pad->index,
144 remote->name, remote_pad->index);
146 ret = media_entity_create_link(local, local_pad->index,
147 remote, remote_pad->index,
151 "failed to create %s:%u -> %s:%u link\n",
152 local->name, local_pad->index,
153 remote->name, remote_pad->index);
162 static int tegra_vi_graph_build_links(struct tegra_mc_vi *vi)
164 u32 link_flags = MEDIA_LNK_FL_ENABLED;
165 struct device_node *node = vi->dev->of_node;
166 struct media_entity *source;
167 struct media_entity *sink;
168 struct media_pad *source_pad;
169 struct media_pad *sink_pad;
170 struct tegra_vi_graph_entity *ent;
171 struct v4l2_of_link link;
172 struct device_node *ep = NULL;
173 struct device_node *next;
174 struct tegra_channel *chan;
177 dev_info(vi->dev, "creating links for channels\n");
180 /* Get the next endpoint and parse its link. */
181 next = of_graph_get_next_endpoint(node, ep);
182 if (next == NULL || !of_device_is_available(next))
188 dev_info(vi->dev, "processing endpoint %s\n", ep->full_name);
190 ret = v4l2_of_parse_link(ep, &link);
192 dev_err(vi->dev, "failed to parse link for %s\n",
197 if (link.local_port >= vi->num_channels) {
198 dev_err(vi->dev, "wrong channel number for port %u\n",
200 v4l2_of_put_link(&link);
205 chan = &vi->chans[link.local_port];
207 dev_info(vi->dev, "creating link for channel %s\n",
210 /* Find the remote entity. */
211 ent = tegra_vi_graph_find_entity(vi, link.remote_node);
213 dev_err(vi->dev, "no entity found for %s\n",
214 link.remote_node->full_name);
215 v4l2_of_put_link(&link);
220 if (NULL == ent->entity) {
221 dev_dbg(vi->dev, "entity not bounded %s\n",
222 link.remote_node->full_name);
226 source = ent->entity;
227 source_pad = &source->pads[link.remote_port];
228 sink = &chan->video.entity;
229 sink_pad = &chan->pad;
231 v4l2_of_put_link(&link);
233 /* Create the media link. */
234 dev_info(vi->dev, "creating %s:%u -> %s:%u link\n",
235 source->name, source_pad->index,
236 sink->name, sink_pad->index);
238 ret = media_entity_create_link(source, source_pad->index,
239 sink, sink_pad->index,
243 "failed to create %s:%u -> %s:%u link\n",
244 source->name, source_pad->index,
245 sink->name, sink_pad->index);
249 tegra_channel_init_subdevices(chan);
250 } while (next != NULL);
256 static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
258 struct tegra_mc_vi *vi =
259 container_of(notifier, struct tegra_mc_vi, notifier);
260 struct tegra_vi_graph_entity *entity;
263 dev_info(vi->dev, "notify complete, all subdevs registered\n");
265 /* Create links for every entity. */
266 list_for_each_entry(entity, &vi->entities, list) {
267 if (entity->entity != NULL) {
268 ret = tegra_vi_graph_build_one(vi, entity);
274 /* Create links for channels */
275 ret = tegra_vi_graph_build_links(vi);
279 ret = v4l2_device_register_subdev_nodes(&vi->v4l2_dev);
281 dev_err(vi->dev, "failed to register subdev nodes\n");
288 static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
289 struct v4l2_subdev *subdev,
290 struct v4l2_async_subdev *asd)
292 struct tegra_mc_vi *vi =
293 container_of(notifier, struct tegra_mc_vi, notifier);
294 struct tegra_vi_graph_entity *entity;
296 /* Locate the entity corresponding to the bound subdev and store the
299 list_for_each_entry(entity, &vi->entities, list) {
300 if (entity->node != subdev->dev->of_node)
303 if (entity->subdev) {
304 dev_err(vi->dev, "duplicate subdev for node %s\n",
305 entity->node->full_name);
309 dev_info(vi->dev, "subdev %s bound\n", subdev->name);
310 entity->entity = &subdev->entity;
311 entity->subdev = subdev;
316 dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
320 void tegra_vi_graph_cleanup(struct tegra_mc_vi *vi)
322 struct tegra_vi_graph_entity *entityp;
323 struct tegra_vi_graph_entity *entity;
325 v4l2_async_notifier_unregister(&vi->notifier);
327 list_for_each_entry_safe(entity, entityp, &vi->entities, list) {
328 of_node_put(entity->node);
329 list_del(&entity->list);
333 int tegra_vi_get_port_info(struct tegra_channel *chan,
334 struct device_node *node, unsigned int index)
336 struct device_node *ep = NULL;
337 struct device_node *ports;
338 struct device_node *port;
342 ports = of_get_child_by_name(node, "ports");
346 for_each_child_of_node(ports, port) {
347 if (!port->name || of_node_cmp(port->name, "port"))
350 ret = of_property_read_u32(port, "reg", &value);
357 for_each_child_of_node(port, ep) {
358 if (!ep->name || of_node_cmp(ep->name, "endpoint"))
362 ret = of_property_read_u32(ep, "csi-port", &value);
364 dev_err(&chan->video.dev, "csi port error\n");
365 chan->port[0] = value;
367 /* Get number of data lanes for the endpoint */
368 ret = of_property_read_u32(ep, "bus-width", &value);
370 dev_err(&chan->video.dev, "num lanes error\n");
371 chan->numlanes = value;
374 dev_err(&chan->video.dev, "num lanes >12!\n");
378 * for numlanes greater than 4 multiple CSI bricks
379 * are needed to capture the image, the logic below
380 * checks for numlanes > 4 and add a new CSI brick
381 * as a valid port. Loops around the three CSI
382 * bricks to add as many ports necessary.
385 for (i = 1; value > 0; i++, value -= 4) {
386 int next_port = chan->port[i-1] + 2;
387 next_port = (next_port % (PORT_F + 1));
388 chan->port[i] = next_port;
396 static int tegra_vi_graph_parse_one(struct tegra_mc_vi *vi,
397 struct device_node *node)
399 struct device_node *ep = NULL;
400 struct device_node *next;
401 struct device_node *remote = NULL;
402 struct tegra_vi_graph_entity *entity;
405 dev_info(vi->dev, "parsing node %s\n", node->full_name);
408 /* Parse all the remote entities and put them into the list */
409 next = of_graph_get_next_endpoint(node, ep);
416 dev_info(vi->dev, "handling endpoint %s\n", ep->full_name);
418 remote = of_graph_get_remote_port_parent(ep);
424 /* Skip entities that we have already processed. */
425 if (remote == vi->dev->of_node ||
426 tegra_vi_graph_find_entity(vi, remote) ||
427 !of_device_is_available(remote)) {
432 entity = devm_kzalloc(vi->dev, sizeof(*entity),
434 if (entity == NULL) {
440 entity->node = remote;
441 entity->asd.match_type = V4L2_ASYNC_MATCH_OF;
442 entity->asd.match.of.node = remote;
443 list_add_tail(&entity->list, &vi->entities);
451 int tegra_vi_tpg_graph_init(struct tegra_mc_vi *mc_vi)
454 u32 link_flags = MEDIA_LNK_FL_ENABLED;
455 struct tegra_csi_device *csi = mc_vi->csi;
456 struct media_entity *source = &csi->subdev.entity;
458 mc_vi->num_subdevs = mc_vi->num_channels;
459 for (i = 0; i < mc_vi->num_channels; i++) {
460 struct tegra_channel *chan = &mc_vi->chans[i];
461 struct media_entity *sink = &chan->video.entity;
462 struct media_pad *source_pad = &csi->pads[i];
463 struct media_pad *sink_pad = &chan->pad;
465 /* Use non-bypass mode by default */
468 /* Create the media link. */
469 dev_info(mc_vi->dev, "creating %s:%u -> %s:%u link\n",
470 source->name, source_pad->index,
471 sink->name, sink_pad->index);
473 err = media_entity_create_link(source, source_pad->index,
474 sink, sink_pad->index,
478 "failed to create %s:%u -> %s:%u link\n",
479 source->name, source_pad->index,
480 sink->name, sink_pad->index);
483 tegra_channel_init_subdevices(chan);
489 int tegra_vi_graph_init(struct tegra_mc_vi *vi)
491 struct tegra_vi_graph_entity *entity;
492 struct v4l2_async_subdev **subdevs = NULL;
493 unsigned int num_subdevs = 0;
497 * Walk the links to parse the full graph. Start by parsing the
498 * composite node and then parse entities in turn. The list_for_each
499 * loop will handle entities added at the end of the list while walking
502 ret = tegra_vi_graph_parse_one(vi, vi->dev->of_node);
506 list_for_each_entry(entity, &vi->entities, list) {
507 ret = tegra_vi_graph_parse_one(vi, entity->node);
512 if (!vi->num_subdevs) {
513 dev_info(vi->dev, "warning: no subdev found in graph\n");
514 tegra_clean_unlinked_channels(vi);
518 /* Register the subdevices notifier. */
519 num_subdevs = vi->num_subdevs;
520 subdevs = devm_kzalloc(vi->dev, sizeof(*subdevs) * num_subdevs,
522 if (subdevs == NULL) {
528 * Add code to check for sensors and
529 * set TPG mode for VI if no sensors found
530 * logic varies for different platforms
533 list_for_each_entry(entity, &vi->entities, list)
534 subdevs[i++] = &entity->asd;
536 vi->notifier.subdevs = subdevs;
537 vi->notifier.num_subdevs = num_subdevs;
538 vi->notifier.bound = tegra_vi_graph_notify_bound;
539 vi->notifier.complete = tegra_vi_graph_notify_complete;
541 vi->subdevs_bound = 0;
543 ret = v4l2_async_notifier_register(&vi->v4l2_dev, &vi->notifier);
545 dev_err(vi->dev, "notifier registration failed\n");
549 if (!vi->link_status) {
550 if (vi->subdevs_bound) {
551 ret = tegra_vi_graph_notify_complete(&vi->notifier);
555 tegra_clean_unlinked_channels(vi);
562 tegra_vi_graph_cleanup(vi);