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