]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/media/platform/tegra/camera/graph.c
d43f1de010788782d9d7455a78f094b25c26d853
[sojka/nv-tegra/linux-3.10.git] / drivers / media / platform / tegra / camera / graph.c
1 /*
2  * NVIDIA Media controller graph management
3  *
4  * Copyright (c) 2015-2016, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * Author: Bryan Wu <pengw@nvidia.com>
7  *
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.
11  */
12
13 #include <linux/clk.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/of.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>
25
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>
32
33 #include "camera/mc_common.h"
34 #include "csi/csi.h"
35
36
37 /* -----------------------------------------------------------------------------
38  * Graph Management
39  */
40
41 static struct tegra_vi_graph_entity *
42 tegra_vi_graph_find_entity(struct tegra_mc_vi *vi,
43                        const struct device_node *node)
44 {
45         struct tegra_vi_graph_entity *entity;
46
47         list_for_each_entry(entity, &vi->entities, list) {
48                 if (entity->node == node)
49                         return entity;
50         }
51
52         return NULL;
53 }
54
55 static int tegra_vi_graph_build_one(struct tegra_mc_vi *vi,
56                                     struct tegra_vi_graph_entity *entity)
57 {
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;
67         int ret = 0;
68
69         dev_info(vi->dev, "creating links for entity %s\n", local->name);
70
71         do {
72                 /* Get the next endpoint and parse its link. */
73                 next = of_graph_get_next_endpoint(entity->node, ep);
74                 if (next == NULL)
75                         break;
76
77                 of_node_put(ep);
78                 ep = next;
79
80                 dev_info(vi->dev, "processing endpoint %s\n", ep->full_name);
81
82                 ret = v4l2_of_parse_link(ep, &link);
83                 if (ret < 0) {
84                         dev_err(vi->dev, "failed to parse link for %s\n",
85                                 ep->full_name);
86                         continue;
87                 }
88
89                 /* Skip sink ports, they will be processed from the other end of
90                  * the link.
91                  */
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);
96                         ret = -EINVAL;
97                         break;
98                 }
99
100                 local_pad = &local->pads[link.local_port];
101
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);
106                         continue;
107                 }
108
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);
114                         continue;
115                 }
116
117                 /* Find the remote entity. */
118                 ent = tegra_vi_graph_find_entity(vi, link.remote_node);
119                 if (ent == NULL) {
120                         dev_err(vi->dev, "no entity found for %s\n",
121                                 link.remote_node->full_name);
122                         v4l2_of_put_link(&link);
123                         ret = -EINVAL;
124                         break;
125                 }
126
127                 remote = ent->entity;
128
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);
133                         ret = -EINVAL;
134                         break;
135                 }
136
137                 remote_pad = &remote->pads[link.remote_port];
138
139                 v4l2_of_put_link(&link);
140
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);
145
146                 ret = media_entity_create_link(local, local_pad->index,
147                                                remote, remote_pad->index,
148                                                link_flags);
149                 if (ret < 0) {
150                         dev_err(vi->dev,
151                                 "failed to create %s:%u -> %s:%u link\n",
152                                 local->name, local_pad->index,
153                                 remote->name, remote_pad->index);
154                         break;
155                 }
156         } while (next);
157
158         of_node_put(ep);
159         return ret;
160 }
161
162 static int tegra_vi_graph_build_links(struct tegra_mc_vi *vi)
163 {
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;
175         int ret = 0;
176
177         dev_info(vi->dev, "creating links for channels\n");
178
179         do {
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))
183                         break;
184
185                 of_node_put(ep);
186                 ep = next;
187
188                 dev_info(vi->dev, "processing endpoint %s\n", ep->full_name);
189
190                 ret = v4l2_of_parse_link(ep, &link);
191                 if (ret < 0) {
192                         dev_err(vi->dev, "failed to parse link for %s\n",
193                                 ep->full_name);
194                         continue;
195                 }
196
197                 if (link.local_port >= vi->num_channels) {
198                         dev_err(vi->dev, "wrong channel number for port %u\n",
199                                 link.local_port);
200                         v4l2_of_put_link(&link);
201                         ret = -EINVAL;
202                         break;
203                 }
204
205                 chan = &vi->chans[link.local_port];
206
207                 dev_info(vi->dev, "creating link for channel %s\n",
208                         chan->video.name);
209
210                 /* Find the remote entity. */
211                 ent = tegra_vi_graph_find_entity(vi, link.remote_node);
212                 if (ent == NULL) {
213                         dev_err(vi->dev, "no entity found for %s\n",
214                                 link.remote_node->full_name);
215                         v4l2_of_put_link(&link);
216                         ret = -EINVAL;
217                         break;
218                 }
219
220                 if (NULL == ent->entity) {
221                         dev_dbg(vi->dev, "entity not bounded %s\n",
222                                 link.remote_node->full_name);
223                         continue;
224                 }
225
226                 source = ent->entity;
227                 source_pad = &source->pads[link.remote_port];
228                 sink = &chan->video.entity;
229                 sink_pad = &chan->pad;
230
231                 v4l2_of_put_link(&link);
232
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);
237
238                 ret = media_entity_create_link(source, source_pad->index,
239                                                sink, sink_pad->index,
240                                                link_flags);
241                 if (ret < 0) {
242                         dev_err(vi->dev,
243                                 "failed to create %s:%u -> %s:%u link\n",
244                                 source->name, source_pad->index,
245                                 sink->name, sink_pad->index);
246                         break;
247                 }
248
249                 tegra_channel_init_subdevices(chan);
250         } while (next != NULL);
251
252         of_node_put(ep);
253         return ret;
254 }
255
256 static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
257 {
258         struct tegra_mc_vi *vi =
259                 container_of(notifier, struct tegra_mc_vi, notifier);
260         struct tegra_vi_graph_entity *entity;
261         int ret;
262
263         dev_info(vi->dev, "notify complete, all subdevs registered\n");
264
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);
269                         if (ret < 0)
270                                 return ret;
271                 }
272         }
273
274         /* Create links for channels */
275         ret = tegra_vi_graph_build_links(vi);
276         if (ret < 0)
277                 return ret;
278
279         ret = v4l2_device_register_subdev_nodes(&vi->v4l2_dev);
280         if (ret < 0)
281                 dev_err(vi->dev, "failed to register subdev nodes\n");
282
283         vi->link_status++;
284
285         return ret;
286 }
287
288 static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
289                                    struct v4l2_subdev *subdev,
290                                    struct v4l2_async_subdev *asd)
291 {
292         struct tegra_mc_vi *vi =
293                 container_of(notifier, struct tegra_mc_vi, notifier);
294         struct tegra_vi_graph_entity *entity;
295
296         /* Locate the entity corresponding to the bound subdev and store the
297          * subdev pointer.
298          */
299         list_for_each_entry(entity, &vi->entities, list) {
300                 if (entity->node != subdev->dev->of_node)
301                         continue;
302
303                 if (entity->subdev) {
304                         dev_err(vi->dev, "duplicate subdev for node %s\n",
305                                 entity->node->full_name);
306                         return -EINVAL;
307                 }
308
309                 dev_info(vi->dev, "subdev %s bound\n", subdev->name);
310                 entity->entity = &subdev->entity;
311                 entity->subdev = subdev;
312                 vi->subdevs_bound++;
313                 return 0;
314         }
315
316         dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
317         return -EINVAL;
318 }
319
320 void tegra_vi_graph_cleanup(struct tegra_mc_vi *vi)
321 {
322         struct tegra_vi_graph_entity *entityp;
323         struct tegra_vi_graph_entity *entity;
324
325         v4l2_async_notifier_unregister(&vi->notifier);
326
327         list_for_each_entry_safe(entity, entityp, &vi->entities, list) {
328                 of_node_put(entity->node);
329                 list_del(&entity->list);
330         }
331 }
332
333 int tegra_vi_get_port_info(struct tegra_channel *chan,
334                         struct device_node *node, unsigned int index)
335 {
336         struct device_node *ep = NULL;
337         struct device_node *ports;
338         struct device_node *port;
339         int value = 0xFFFF;
340         int ret = 0, i;
341
342         ports = of_get_child_by_name(node, "ports");
343         if (ports == NULL)
344                 ports = node;
345
346         for_each_child_of_node(ports, port) {
347                 if (!port->name || of_node_cmp(port->name, "port"))
348                         continue;
349
350                 ret = of_property_read_u32(port, "reg", &value);
351                 if (ret < 0)
352                         continue;
353
354                 if (value != index)
355                         continue;
356
357                 for_each_child_of_node(port, ep) {
358                         if (!ep->name || of_node_cmp(ep->name, "endpoint"))
359                                 continue;
360
361                         /* Get CSI port */
362                         ret = of_property_read_u32(ep, "csi-port", &value);
363                         if (ret < 0)
364                                 dev_err(&chan->video.dev, "csi port error\n");
365                         chan->port[0] = value;
366
367                         /* Get number of data lanes for the endpoint */
368                         ret = of_property_read_u32(ep, "bus-width", &value);
369                         if (ret < 0)
370                                 dev_err(&chan->video.dev, "num lanes error\n");
371                         chan->numlanes = value;
372
373                         if (value > 12) {
374                                 dev_err(&chan->video.dev, "num lanes >12!\n");
375                                 return -EINVAL;
376                         }
377                         /*
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.
383                          */
384                         value -= 4;
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;
389                         }
390                 }
391         }
392
393         return ret;
394 }
395
396 static int tegra_vi_graph_parse_one(struct tegra_mc_vi *vi,
397                                 struct device_node *node)
398 {
399         struct device_node *ep = NULL;
400         struct device_node *next;
401         struct device_node *remote = NULL;
402         struct tegra_vi_graph_entity *entity;
403         int ret = 0;
404
405         dev_info(vi->dev, "parsing node %s\n", node->full_name);
406
407         do {
408                 /* Parse all the remote entities and put them into the list */
409                 next = of_graph_get_next_endpoint(node, ep);
410                 if (next == NULL)
411                         break;
412
413                 of_node_put(ep);
414                 ep = next;
415
416                 dev_info(vi->dev, "handling endpoint %s\n", ep->full_name);
417
418                 remote = of_graph_get_remote_port_parent(ep);
419                 if (!remote) {
420                         ret = -EINVAL;
421                         break;
422                 }
423
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)) {
428                         of_node_put(remote);
429                         continue;
430                 }
431
432                 entity = devm_kzalloc(vi->dev, sizeof(*entity),
433                                 GFP_KERNEL);
434                 if (entity == NULL) {
435                         of_node_put(remote);
436                         ret = -ENOMEM;
437                         break;
438                 }
439
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);
444                 vi->num_subdevs++;
445         } while (next);
446
447         of_node_put(ep);
448         return ret;
449 }
450
451 int tegra_vi_tpg_graph_init(struct tegra_mc_vi *mc_vi)
452 {
453         int err = 0, i;
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;
457
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;
464
465                 /* Use non-bypass mode by default */
466                 chan->bypass = 0;
467
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);
472
473                 err = media_entity_create_link(source, source_pad->index,
474                                                sink, sink_pad->index,
475                                                link_flags);
476                 if (err < 0) {
477                         dev_err(mc_vi->dev,
478                                 "failed to create %s:%u -> %s:%u link\n",
479                                 source->name, source_pad->index,
480                                 sink->name, sink_pad->index);
481                         return err;
482                 }
483                 tegra_channel_init_subdevices(chan);
484         }
485
486         return 0;
487 }
488
489 int tegra_vi_graph_init(struct tegra_mc_vi *vi)
490 {
491         struct tegra_vi_graph_entity *entity;
492         struct v4l2_async_subdev **subdevs = NULL;
493         unsigned int num_subdevs = 0;
494         int ret = 0, i;
495
496         /*
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
500          * the links.
501          */
502         ret = tegra_vi_graph_parse_one(vi, vi->dev->of_node);
503         if (ret < 0)
504                 return 0;
505
506         list_for_each_entry(entity, &vi->entities, list) {
507                 ret = tegra_vi_graph_parse_one(vi, entity->node);
508                 if (ret < 0)
509                         break;
510         }
511
512         if (!vi->num_subdevs) {
513                 dev_info(vi->dev, "warning: no subdev found in graph\n");
514                 tegra_clean_unlinked_channels(vi);
515                 goto done;
516         }
517
518         /* Register the subdevices notifier. */
519         num_subdevs = vi->num_subdevs;
520         subdevs = devm_kzalloc(vi->dev, sizeof(*subdevs) * num_subdevs,
521                                GFP_KERNEL);
522         if (subdevs == NULL) {
523                 ret = -ENOMEM;
524                 goto done;
525         }
526
527         /*
528          * Add code to check for sensors and
529          * set TPG mode for VI if no sensors found
530          * logic varies for different platforms
531          */
532         i = 0;
533         list_for_each_entry(entity, &vi->entities, list)
534                 subdevs[i++] = &entity->asd;
535
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;
540         vi->link_status = 0;
541         vi->subdevs_bound = 0;
542
543         ret = v4l2_async_notifier_register(&vi->v4l2_dev, &vi->notifier);
544         if (ret < 0) {
545                 dev_err(vi->dev, "notifier registration failed\n");
546                 goto done;
547         }
548
549         if (!vi->link_status) {
550                 if (vi->subdevs_bound) {
551                         ret = tegra_vi_graph_notify_complete(&vi->notifier);
552                         if (ret < 0)
553                                 goto done;
554                 }
555                 tegra_clean_unlinked_channels(vi);
556         }
557
558         return 0;
559
560 done:
561         if (ret < 0)
562                 tegra_vi_graph_cleanup(vi);
563
564         return ret;
565 }