]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/v4l2-core/v4l2-fwnode.c
media: v4l: fwnode: Read lane inversion information despite lane numbering
[linux.git] / drivers / media / v4l2-core / v4l2-fwnode.c
1 /*
2  * V4L2 fwnode binding parsing library
3  *
4  * The origins of the V4L2 fwnode library are in V4L2 OF library that
5  * formerly was located in v4l2-of.c.
6  *
7  * Copyright (c) 2016 Intel Corporation.
8  * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
9  *
10  * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
11  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
12  *
13  * Copyright (C) 2012 Renesas Electronics Corp.
14  * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of version 2 of the GNU General Public License as
18  * published by the Free Software Foundation.
19  */
20 #include <linux/acpi.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/property.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29
30 #include <media/v4l2-async.h>
31 #include <media/v4l2-fwnode.h>
32 #include <media/v4l2-subdev.h>
33
34 enum v4l2_fwnode_bus_type {
35         V4L2_FWNODE_BUS_TYPE_GUESS = 0,
36         V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
37         V4L2_FWNODE_BUS_TYPE_CSI1,
38         V4L2_FWNODE_BUS_TYPE_CCP2,
39         V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
40         V4L2_FWNODE_BUS_TYPE_PARALLEL,
41         V4L2_FWNODE_BUS_TYPE_BT656,
42         NR_OF_V4L2_FWNODE_BUS_TYPE,
43 };
44
45 static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
46                                                struct v4l2_fwnode_endpoint *vep,
47                                                enum v4l2_fwnode_bus_type bus_type)
48 {
49         struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2;
50         bool have_clk_lane = false;
51         unsigned int flags = 0, lanes_used = 0;
52         u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
53         unsigned int num_data_lanes = 0;
54         unsigned int i;
55         u32 v;
56         int rval;
57
58         if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY)
59                 num_data_lanes = min_t(u32, bus->num_data_lanes,
60                                        V4L2_FWNODE_CSI2_MAX_DATA_LANES);
61
62         rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
63         if (rval > 0) {
64                 num_data_lanes =
65                         min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
66
67                 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
68                                                num_data_lanes);
69
70                 for (i = 0; i < num_data_lanes; i++) {
71                         if (lanes_used & BIT(array[i]))
72                                 pr_warn("duplicated lane %u in data-lanes\n",
73                                         array[i]);
74                         lanes_used |= BIT(array[i]);
75
76                         bus->data_lanes[i] = array[i];
77                         pr_debug("lane %u position %u\n", i, array[i]);
78                 }
79         }
80
81         rval = fwnode_property_read_u32_array(fwnode, "lane-polarities", NULL,
82                                               0);
83         if (rval > 0) {
84                 if (rval != 1 + num_data_lanes /* clock+data */) {
85                         pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
86                                 1 + num_data_lanes, rval);
87                         return -EINVAL;
88                 }
89
90                 fwnode_property_read_u32_array(fwnode, "lane-polarities", array,
91                                                1 + num_data_lanes);
92
93                 for (i = 0; i < 1 + num_data_lanes; i++) {
94                         bus->lane_polarities[i] = array[i];
95                         pr_debug("lane %u polarity %sinverted",
96                                  i, array[i] ? "" : "not ");
97                 }
98         } else {
99                 pr_debug("no lane polarities defined, assuming not inverted\n");
100         }
101
102         if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
103                 if (lanes_used & BIT(v))
104                         pr_warn("duplicated lane %u in clock-lanes\n", v);
105                 lanes_used |= BIT(v);
106
107                 bus->clock_lane = v;
108                 have_clk_lane = true;
109                 pr_debug("clock lane position %u\n", v);
110         }
111
112         if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
113                 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
114                 pr_debug("non-continuous clock\n");
115         } else {
116                 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
117         }
118
119         if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY || lanes_used ||
120             have_clk_lane || (flags & ~V4L2_MBUS_CSI2_CONTINUOUS_CLOCK)) {
121                 bus->flags = flags;
122                 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
123                 bus->num_data_lanes = num_data_lanes;
124         }
125
126         return 0;
127 }
128
129 #define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH |      \
130                              V4L2_MBUS_HSYNC_ACTIVE_LOW |       \
131                              V4L2_MBUS_VSYNC_ACTIVE_HIGH |      \
132                              V4L2_MBUS_VSYNC_ACTIVE_LOW |       \
133                              V4L2_MBUS_FIELD_EVEN_HIGH |        \
134                              V4L2_MBUS_FIELD_EVEN_LOW)
135
136 static void v4l2_fwnode_endpoint_parse_parallel_bus(
137         struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep,
138         enum v4l2_fwnode_bus_type bus_type)
139 {
140         struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
141         unsigned int flags = 0;
142         u32 v;
143
144         if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
145                 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
146                         V4L2_MBUS_HSYNC_ACTIVE_LOW;
147                 pr_debug("hsync-active %s\n", v ? "high" : "low");
148         }
149
150         if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
151                 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
152                         V4L2_MBUS_VSYNC_ACTIVE_LOW;
153                 pr_debug("vsync-active %s\n", v ? "high" : "low");
154         }
155
156         if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
157                 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
158                         V4L2_MBUS_FIELD_EVEN_LOW;
159                 pr_debug("field-even-active %s\n", v ? "high" : "low");
160         }
161
162         if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
163                 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
164                         V4L2_MBUS_PCLK_SAMPLE_FALLING;
165                 pr_debug("pclk-sample %s\n", v ? "high" : "low");
166         }
167
168         if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
169                 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
170                         V4L2_MBUS_DATA_ACTIVE_LOW;
171                 pr_debug("data-active %s\n", v ? "high" : "low");
172         }
173
174         if (fwnode_property_present(fwnode, "slave-mode")) {
175                 pr_debug("slave mode\n");
176                 flags |= V4L2_MBUS_SLAVE;
177         } else {
178                 flags |= V4L2_MBUS_MASTER;
179         }
180
181         if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
182                 bus->bus_width = v;
183                 pr_debug("bus-width %u\n", v);
184         }
185
186         if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
187                 bus->data_shift = v;
188                 pr_debug("data-shift %u\n", v);
189         }
190
191         if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
192                 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
193                         V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
194                 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
195         }
196
197         if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
198                 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
199                         V4L2_MBUS_DATA_ENABLE_LOW;
200                 pr_debug("data-enable-active %s\n", v ? "high" : "low");
201         }
202
203         switch (bus_type) {
204         default:
205                 bus->flags = flags;
206                 if (flags & PARALLEL_MBUS_FLAGS)
207                         vep->bus_type = V4L2_MBUS_PARALLEL;
208                 else
209                         vep->bus_type = V4L2_MBUS_BT656;
210                 break;
211         case V4L2_FWNODE_BUS_TYPE_PARALLEL:
212                 vep->bus_type = V4L2_MBUS_PARALLEL;
213                 bus->flags = flags;
214                 break;
215         case V4L2_FWNODE_BUS_TYPE_BT656:
216                 vep->bus_type = V4L2_MBUS_BT656;
217                 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
218                 break;
219         }
220 }
221
222 static void
223 v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
224                                     struct v4l2_fwnode_endpoint *vep,
225                                     enum v4l2_fwnode_bus_type bus_type)
226 {
227         struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
228         u32 v;
229
230         if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
231                 bus->clock_inv = v;
232                 pr_debug("clock-inv %u\n", v);
233         }
234
235         if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
236                 bus->strobe = v;
237                 pr_debug("strobe %u\n", v);
238         }
239
240         if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
241                 bus->data_lane = v;
242                 pr_debug("data-lanes %u\n", v);
243         }
244
245         if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
246                 bus->clock_lane = v;
247                 pr_debug("clock-lanes %u\n", v);
248         }
249
250         if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
251                 vep->bus_type = V4L2_MBUS_CCP2;
252         else
253                 vep->bus_type = V4L2_MBUS_CSI1;
254 }
255
256 static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
257                                         struct v4l2_fwnode_endpoint *vep)
258 {
259         u32 bus_type = 0;
260         int rval;
261
262         pr_debug("===== begin V4L2 endpoint properties\n");
263
264         fwnode_graph_parse_endpoint(fwnode, &vep->base);
265
266         /* Zero fields from bus_type to until the end */
267         memset(&vep->bus_type, 0, sizeof(*vep) -
268                offsetof(typeof(*vep), bus_type));
269
270         fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
271
272         switch (bus_type) {
273         case V4L2_FWNODE_BUS_TYPE_GUESS:
274                 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
275                                                            bus_type);
276                 if (rval)
277                         return rval;
278
279                 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
280                         v4l2_fwnode_endpoint_parse_parallel_bus(
281                                 fwnode, vep, V4L2_MBUS_UNKNOWN);
282
283                 break;
284         case V4L2_FWNODE_BUS_TYPE_CCP2:
285         case V4L2_FWNODE_BUS_TYPE_CSI1:
286                 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
287
288                 break;
289         case V4L2_FWNODE_BUS_TYPE_CSI2_DPHY:
290                 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
291                 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
292                                                            bus_type);
293                 if (rval)
294                         return rval;
295
296                 break;
297         case V4L2_FWNODE_BUS_TYPE_PARALLEL:
298         case V4L2_FWNODE_BUS_TYPE_BT656:
299                 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep, bus_type);
300
301                 break;
302         default:
303                 pr_warn("unsupported bus type %u\n", bus_type);
304                 return -EINVAL;
305         }
306
307         return 0;
308 }
309
310 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
311                                struct v4l2_fwnode_endpoint *vep)
312 {
313         int ret;
314
315         ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
316
317         pr_debug("===== end V4L2 endpoint properties\n");
318
319         return ret;
320 }
321 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
322
323 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
324 {
325         if (IS_ERR_OR_NULL(vep))
326                 return;
327
328         kfree(vep->link_frequencies);
329 }
330 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
331
332 int v4l2_fwnode_endpoint_alloc_parse(
333         struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
334 {
335         int rval;
336
337         rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
338         if (rval < 0)
339                 return rval;
340
341         rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
342                                               NULL, 0);
343         if (rval > 0) {
344                 unsigned int i;
345
346                 vep->link_frequencies =
347                         kmalloc_array(rval, sizeof(*vep->link_frequencies),
348                                       GFP_KERNEL);
349                 if (!vep->link_frequencies)
350                         return -ENOMEM;
351
352                 vep->nr_of_link_frequencies = rval;
353
354                 rval = fwnode_property_read_u64_array(
355                         fwnode, "link-frequencies", vep->link_frequencies,
356                         vep->nr_of_link_frequencies);
357                 if (rval < 0) {
358                         v4l2_fwnode_endpoint_free(vep);
359                         return rval;
360                 }
361
362                 for (i = 0; i < vep->nr_of_link_frequencies; i++)
363                         pr_info("link-frequencies %u value %llu\n", i,
364                                 vep->link_frequencies[i]);
365         }
366
367         pr_debug("===== end V4L2 endpoint properties\n");
368
369         return 0;
370 }
371 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
372
373 int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
374                            struct v4l2_fwnode_link *link)
375 {
376         const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
377         struct fwnode_handle *fwnode;
378
379         memset(link, 0, sizeof(*link));
380
381         fwnode = fwnode_get_parent(__fwnode);
382         fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
383         fwnode = fwnode_get_next_parent(fwnode);
384         if (is_of_node(fwnode) &&
385             of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
386                 fwnode = fwnode_get_next_parent(fwnode);
387         link->local_node = fwnode;
388
389         fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
390         if (!fwnode) {
391                 fwnode_handle_put(fwnode);
392                 return -ENOLINK;
393         }
394
395         fwnode = fwnode_get_parent(fwnode);
396         fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
397         fwnode = fwnode_get_next_parent(fwnode);
398         if (is_of_node(fwnode) &&
399             of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
400                 fwnode = fwnode_get_next_parent(fwnode);
401         link->remote_node = fwnode;
402
403         return 0;
404 }
405 EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
406
407 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
408 {
409         fwnode_handle_put(link->local_node);
410         fwnode_handle_put(link->remote_node);
411 }
412 EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
413
414 static int v4l2_async_notifier_fwnode_parse_endpoint(
415         struct device *dev, struct v4l2_async_notifier *notifier,
416         struct fwnode_handle *endpoint, unsigned int asd_struct_size,
417         int (*parse_endpoint)(struct device *dev,
418                             struct v4l2_fwnode_endpoint *vep,
419                             struct v4l2_async_subdev *asd))
420 {
421         struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
422         struct v4l2_async_subdev *asd;
423         int ret;
424
425         asd = kzalloc(asd_struct_size, GFP_KERNEL);
426         if (!asd)
427                 return -ENOMEM;
428
429         asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
430         asd->match.fwnode =
431                 fwnode_graph_get_remote_port_parent(endpoint);
432         if (!asd->match.fwnode) {
433                 dev_warn(dev, "bad remote port parent\n");
434                 ret = -ENOTCONN;
435                 goto out_err;
436         }
437
438         ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
439         if (ret) {
440                 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
441                          ret);
442                 goto out_err;
443         }
444
445         ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
446         if (ret == -ENOTCONN)
447                 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
448                         vep.base.id);
449         else if (ret < 0)
450                 dev_warn(dev,
451                          "driver could not parse port@%u/endpoint@%u (%d)\n",
452                          vep.base.port, vep.base.id, ret);
453         v4l2_fwnode_endpoint_free(&vep);
454         if (ret < 0)
455                 goto out_err;
456
457         ret = v4l2_async_notifier_add_subdev(notifier, asd);
458         if (ret < 0) {
459                 /* not an error if asd already exists */
460                 if (ret == -EEXIST)
461                         ret = 0;
462                 goto out_err;
463         }
464
465         return 0;
466
467 out_err:
468         fwnode_handle_put(asd->match.fwnode);
469         kfree(asd);
470
471         return ret == -ENOTCONN ? 0 : ret;
472 }
473
474 static int __v4l2_async_notifier_parse_fwnode_endpoints(
475         struct device *dev, struct v4l2_async_notifier *notifier,
476         size_t asd_struct_size, unsigned int port, bool has_port,
477         int (*parse_endpoint)(struct device *dev,
478                             struct v4l2_fwnode_endpoint *vep,
479                             struct v4l2_async_subdev *asd))
480 {
481         struct fwnode_handle *fwnode;
482         int ret = 0;
483
484         if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
485                 return -EINVAL;
486
487         fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
488                 struct fwnode_handle *dev_fwnode;
489                 bool is_available;
490
491                 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
492                 is_available = fwnode_device_is_available(dev_fwnode);
493                 fwnode_handle_put(dev_fwnode);
494                 if (!is_available)
495                         continue;
496
497                 if (has_port) {
498                         struct fwnode_endpoint ep;
499
500                         ret = fwnode_graph_parse_endpoint(fwnode, &ep);
501                         if (ret)
502                                 break;
503
504                         if (ep.port != port)
505                                 continue;
506                 }
507
508                 ret = v4l2_async_notifier_fwnode_parse_endpoint(
509                         dev, notifier, fwnode, asd_struct_size, parse_endpoint);
510                 if (ret < 0)
511                         break;
512         }
513
514         fwnode_handle_put(fwnode);
515
516         return ret;
517 }
518
519 int v4l2_async_notifier_parse_fwnode_endpoints(
520         struct device *dev, struct v4l2_async_notifier *notifier,
521         size_t asd_struct_size,
522         int (*parse_endpoint)(struct device *dev,
523                             struct v4l2_fwnode_endpoint *vep,
524                             struct v4l2_async_subdev *asd))
525 {
526         return __v4l2_async_notifier_parse_fwnode_endpoints(
527                 dev, notifier, asd_struct_size, 0, false, parse_endpoint);
528 }
529 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
530
531 int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
532         struct device *dev, struct v4l2_async_notifier *notifier,
533         size_t asd_struct_size, unsigned int port,
534         int (*parse_endpoint)(struct device *dev,
535                             struct v4l2_fwnode_endpoint *vep,
536                             struct v4l2_async_subdev *asd))
537 {
538         return __v4l2_async_notifier_parse_fwnode_endpoints(
539                 dev, notifier, asd_struct_size, port, true, parse_endpoint);
540 }
541 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
542
543 /*
544  * v4l2_fwnode_reference_parse - parse references for async sub-devices
545  * @dev: the device node the properties of which are parsed for references
546  * @notifier: the async notifier where the async subdevs will be added
547  * @prop: the name of the property
548  *
549  * Return: 0 on success
550  *         -ENOENT if no entries were found
551  *         -ENOMEM if memory allocation failed
552  *         -EINVAL if property parsing failed
553  */
554 static int v4l2_fwnode_reference_parse(
555         struct device *dev, struct v4l2_async_notifier *notifier,
556         const char *prop)
557 {
558         struct fwnode_reference_args args;
559         unsigned int index;
560         int ret;
561
562         for (index = 0;
563              !(ret = fwnode_property_get_reference_args(
564                        dev_fwnode(dev), prop, NULL, 0, index, &args));
565              index++)
566                 fwnode_handle_put(args.fwnode);
567
568         if (!index)
569                 return -ENOENT;
570
571         /*
572          * Note that right now both -ENODATA and -ENOENT may signal
573          * out-of-bounds access. Return the error in cases other than that.
574          */
575         if (ret != -ENOENT && ret != -ENODATA)
576                 return ret;
577
578         for (index = 0; !fwnode_property_get_reference_args(
579                      dev_fwnode(dev), prop, NULL, 0, index, &args);
580              index++) {
581                 struct v4l2_async_subdev *asd;
582
583                 asd = v4l2_async_notifier_add_fwnode_subdev(
584                         notifier, args.fwnode, sizeof(*asd));
585                 if (IS_ERR(asd)) {
586                         ret = PTR_ERR(asd);
587                         /* not an error if asd already exists */
588                         if (ret == -EEXIST) {
589                                 fwnode_handle_put(args.fwnode);
590                                 continue;
591                         }
592
593                         goto error;
594                 }
595         }
596
597         return 0;
598
599 error:
600         fwnode_handle_put(args.fwnode);
601         return ret;
602 }
603
604 /*
605  * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
606  *                                      arguments
607  * @fwnode: fwnode to read @prop from
608  * @notifier: notifier for @dev
609  * @prop: the name of the property
610  * @index: the index of the reference to get
611  * @props: the array of integer property names
612  * @nprops: the number of integer property names in @nprops
613  *
614  * First find an fwnode referred to by the reference at @index in @prop.
615  *
616  * Then under that fwnode, @nprops times, for each property in @props,
617  * iteratively follow child nodes starting from fwnode such that they have the
618  * property in @props array at the index of the child node distance from the
619  * root node and the value of that property matching with the integer argument
620  * of the reference, at the same index.
621  *
622  * The child fwnode reched at the end of the iteration is then returned to the
623  * caller.
624  *
625  * The core reason for this is that you cannot refer to just any node in ACPI.
626  * So to refer to an endpoint (easy in DT) you need to refer to a device, then
627  * provide a list of (property name, property value) tuples where each tuple
628  * uniquely identifies a child node. The first tuple identifies a child directly
629  * underneath the device fwnode, the next tuple identifies a child node
630  * underneath the fwnode identified by the previous tuple, etc. until you
631  * reached the fwnode you need.
632  *
633  * An example with a graph, as defined in Documentation/acpi/dsd/graph.txt:
634  *
635  *      Scope (\_SB.PCI0.I2C2)
636  *      {
637  *              Device (CAM0)
638  *              {
639  *                      Name (_DSD, Package () {
640  *                              ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
641  *                              Package () {
642  *                                      Package () {
643  *                                              "compatible",
644  *                                              Package () { "nokia,smia" }
645  *                                      },
646  *                              },
647  *                              ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
648  *                              Package () {
649  *                                      Package () { "port0", "PRT0" },
650  *                              }
651  *                      })
652  *                      Name (PRT0, Package() {
653  *                              ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
654  *                              Package () {
655  *                                      Package () { "port", 0 },
656  *                              },
657  *                              ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
658  *                              Package () {
659  *                                      Package () { "endpoint0", "EP00" },
660  *                              }
661  *                      })
662  *                      Name (EP00, Package() {
663  *                              ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
664  *                              Package () {
665  *                                      Package () { "endpoint", 0 },
666  *                                      Package () {
667  *                                              "remote-endpoint",
668  *                                              Package() {
669  *                                                      \_SB.PCI0.ISP, 4, 0
670  *                                              }
671  *                                      },
672  *                              }
673  *                      })
674  *              }
675  *      }
676  *
677  *      Scope (\_SB.PCI0)
678  *      {
679  *              Device (ISP)
680  *              {
681  *                      Name (_DSD, Package () {
682  *                              ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
683  *                              Package () {
684  *                                      Package () { "port4", "PRT4" },
685  *                              }
686  *                      })
687  *
688  *                      Name (PRT4, Package() {
689  *                              ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
690  *                              Package () {
691  *                                      Package () { "port", 4 },
692  *                              },
693  *                              ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
694  *                              Package () {
695  *                                      Package () { "endpoint0", "EP40" },
696  *                              }
697  *                      })
698  *
699  *                      Name (EP40, Package() {
700  *                              ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
701  *                              Package () {
702  *                                      Package () { "endpoint", 0 },
703  *                                      Package () {
704  *                                              "remote-endpoint",
705  *                                              Package () {
706  *                                                      \_SB.PCI0.I2C2.CAM0,
707  *                                                      0, 0
708  *                                              }
709  *                                      },
710  *                              }
711  *                      })
712  *              }
713  *      }
714  *
715  * From the EP40 node under ISP device, you could parse the graph remote
716  * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
717  *
718  *  @fwnode: fwnode referring to EP40 under ISP.
719  *  @prop: "remote-endpoint"
720  *  @index: 0
721  *  @props: "port", "endpoint"
722  *  @nprops: 2
723  *
724  * And you'd get back fwnode referring to EP00 under CAM0.
725  *
726  * The same works the other way around: if you use EP00 under CAM0 as the
727  * fwnode, you'll get fwnode referring to EP40 under ISP.
728  *
729  * The same example in DT syntax would look like this:
730  *
731  * cam: cam0 {
732  *      compatible = "nokia,smia";
733  *
734  *      port {
735  *              port = <0>;
736  *              endpoint {
737  *                      endpoint = <0>;
738  *                      remote-endpoint = <&isp 4 0>;
739  *              };
740  *      };
741  * };
742  *
743  * isp: isp {
744  *      ports {
745  *              port@4 {
746  *                      port = <4>;
747  *                      endpoint {
748  *                              endpoint = <0>;
749  *                              remote-endpoint = <&cam 0 0>;
750  *                      };
751  *              };
752  *      };
753  * };
754  *
755  * Return: 0 on success
756  *         -ENOENT if no entries (or the property itself) were found
757  *         -EINVAL if property parsing otherwise failed
758  *         -ENOMEM if memory allocation failed
759  */
760 static struct fwnode_handle *v4l2_fwnode_reference_get_int_prop(
761         struct fwnode_handle *fwnode, const char *prop, unsigned int index,
762         const char * const *props, unsigned int nprops)
763 {
764         struct fwnode_reference_args fwnode_args;
765         u64 *args = fwnode_args.args;
766         struct fwnode_handle *child;
767         int ret;
768
769         /*
770          * Obtain remote fwnode as well as the integer arguments.
771          *
772          * Note that right now both -ENODATA and -ENOENT may signal
773          * out-of-bounds access. Return -ENOENT in that case.
774          */
775         ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
776                                                  index, &fwnode_args);
777         if (ret)
778                 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
779
780         /*
781          * Find a node in the tree under the referred fwnode corresponding to
782          * the integer arguments.
783          */
784         fwnode = fwnode_args.fwnode;
785         while (nprops--) {
786                 u32 val;
787
788                 /* Loop over all child nodes under fwnode. */
789                 fwnode_for_each_child_node(fwnode, child) {
790                         if (fwnode_property_read_u32(child, *props, &val))
791                                 continue;
792
793                         /* Found property, see if its value matches. */
794                         if (val == *args)
795                                 break;
796                 }
797
798                 fwnode_handle_put(fwnode);
799
800                 /* No property found; return an error here. */
801                 if (!child) {
802                         fwnode = ERR_PTR(-ENOENT);
803                         break;
804                 }
805
806                 props++;
807                 args++;
808                 fwnode = child;
809         }
810
811         return fwnode;
812 }
813
814 /*
815  * v4l2_fwnode_reference_parse_int_props - parse references for async
816  *                                         sub-devices
817  * @dev: struct device pointer
818  * @notifier: notifier for @dev
819  * @prop: the name of the property
820  * @props: the array of integer property names
821  * @nprops: the number of integer properties
822  *
823  * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
824  * property @prop with integer arguments with child nodes matching in properties
825  * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
826  * accordingly.
827  *
828  * While it is technically possible to use this function on DT, it is only
829  * meaningful on ACPI. On Device tree you can refer to any node in the tree but
830  * on ACPI the references are limited to devices.
831  *
832  * Return: 0 on success
833  *         -ENOENT if no entries (or the property itself) were found
834  *         -EINVAL if property parsing otherwisefailed
835  *         -ENOMEM if memory allocation failed
836  */
837 static int v4l2_fwnode_reference_parse_int_props(
838         struct device *dev, struct v4l2_async_notifier *notifier,
839         const char *prop, const char * const *props, unsigned int nprops)
840 {
841         struct fwnode_handle *fwnode;
842         unsigned int index;
843         int ret;
844
845         index = 0;
846         do {
847                 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
848                                                             prop, index,
849                                                             props, nprops);
850                 if (IS_ERR(fwnode)) {
851                         /*
852                          * Note that right now both -ENODATA and -ENOENT may
853                          * signal out-of-bounds access. Return the error in
854                          * cases other than that.
855                          */
856                         if (PTR_ERR(fwnode) != -ENOENT &&
857                             PTR_ERR(fwnode) != -ENODATA)
858                                 return PTR_ERR(fwnode);
859                         break;
860                 }
861                 fwnode_handle_put(fwnode);
862                 index++;
863         } while (1);
864
865         for (index = 0; !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(
866                                          dev_fwnode(dev), prop, index, props,
867                                          nprops))); index++) {
868                 struct v4l2_async_subdev *asd;
869
870                 asd = v4l2_async_notifier_add_fwnode_subdev(notifier, fwnode,
871                                                             sizeof(*asd));
872                 if (IS_ERR(asd)) {
873                         ret = PTR_ERR(asd);
874                         /* not an error if asd already exists */
875                         if (ret == -EEXIST) {
876                                 fwnode_handle_put(fwnode);
877                                 continue;
878                         }
879
880                         goto error;
881                 }
882         }
883
884         return PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
885
886 error:
887         fwnode_handle_put(fwnode);
888         return ret;
889 }
890
891 int v4l2_async_notifier_parse_fwnode_sensor_common(
892         struct device *dev, struct v4l2_async_notifier *notifier)
893 {
894         static const char * const led_props[] = { "led" };
895         static const struct {
896                 const char *name;
897                 const char * const *props;
898                 unsigned int nprops;
899         } props[] = {
900                 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
901                 { "lens-focus", NULL, 0 },
902         };
903         unsigned int i;
904
905         for (i = 0; i < ARRAY_SIZE(props); i++) {
906                 int ret;
907
908                 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
909                         ret = v4l2_fwnode_reference_parse_int_props(
910                                 dev, notifier, props[i].name,
911                                 props[i].props, props[i].nprops);
912                 else
913                         ret = v4l2_fwnode_reference_parse(
914                                 dev, notifier, props[i].name);
915                 if (ret && ret != -ENOENT) {
916                         dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
917                                  props[i].name, ret);
918                         return ret;
919                 }
920         }
921
922         return 0;
923 }
924 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_sensor_common);
925
926 int v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd)
927 {
928         struct v4l2_async_notifier *notifier;
929         int ret;
930
931         if (WARN_ON(!sd->dev))
932                 return -ENODEV;
933
934         notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
935         if (!notifier)
936                 return -ENOMEM;
937
938         v4l2_async_notifier_init(notifier);
939
940         ret = v4l2_async_notifier_parse_fwnode_sensor_common(sd->dev,
941                                                              notifier);
942         if (ret < 0)
943                 goto out_cleanup;
944
945         ret = v4l2_async_subdev_notifier_register(sd, notifier);
946         if (ret < 0)
947                 goto out_cleanup;
948
949         ret = v4l2_async_register_subdev(sd);
950         if (ret < 0)
951                 goto out_unregister;
952
953         sd->subdev_notifier = notifier;
954
955         return 0;
956
957 out_unregister:
958         v4l2_async_notifier_unregister(notifier);
959
960 out_cleanup:
961         v4l2_async_notifier_cleanup(notifier);
962         kfree(notifier);
963
964         return ret;
965 }
966 EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor_common);
967
968 int v4l2_async_register_fwnode_subdev(
969         struct v4l2_subdev *sd, size_t asd_struct_size,
970         unsigned int *ports, unsigned int num_ports,
971         int (*parse_endpoint)(struct device *dev,
972                               struct v4l2_fwnode_endpoint *vep,
973                               struct v4l2_async_subdev *asd))
974 {
975         struct v4l2_async_notifier *notifier;
976         struct device *dev = sd->dev;
977         struct fwnode_handle *fwnode;
978         int ret;
979
980         if (WARN_ON(!dev))
981                 return -ENODEV;
982
983         fwnode = dev_fwnode(dev);
984         if (!fwnode_device_is_available(fwnode))
985                 return -ENODEV;
986
987         notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
988         if (!notifier)
989                 return -ENOMEM;
990
991         v4l2_async_notifier_init(notifier);
992
993         if (!ports) {
994                 ret = v4l2_async_notifier_parse_fwnode_endpoints(
995                         dev, notifier, asd_struct_size, parse_endpoint);
996                 if (ret < 0)
997                         goto out_cleanup;
998         } else {
999                 unsigned int i;
1000
1001                 for (i = 0; i < num_ports; i++) {
1002                         ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
1003                                 dev, notifier, asd_struct_size,
1004                                 ports[i], parse_endpoint);
1005                         if (ret < 0)
1006                                 goto out_cleanup;
1007                 }
1008         }
1009
1010         ret = v4l2_async_subdev_notifier_register(sd, notifier);
1011         if (ret < 0)
1012                 goto out_cleanup;
1013
1014         ret = v4l2_async_register_subdev(sd);
1015         if (ret < 0)
1016                 goto out_unregister;
1017
1018         sd->subdev_notifier = notifier;
1019
1020         return 0;
1021
1022 out_unregister:
1023         v4l2_async_notifier_unregister(notifier);
1024 out_cleanup:
1025         v4l2_async_notifier_cleanup(notifier);
1026         kfree(notifier);
1027
1028         return ret;
1029 }
1030 EXPORT_SYMBOL_GPL(v4l2_async_register_fwnode_subdev);
1031
1032 MODULE_LICENSE("GPL");
1033 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1034 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1035 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");