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