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