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