xref: /linux/drivers/base/property.c (revision 40286d6379aacfcc053253ef78dc78b09addffda)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * property.c - Unified device property interface.
4  *
5  * Copyright (C) 2014, Intel Corporation
6  * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  *          Mika Westerberg <mika.westerberg@linux.intel.com>
8  */
9 
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/kconfig.h>
14 #include <linux/of.h>
15 #include <linux/property.h>
16 #include <linux/phy.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 
21 struct fwnode_handle *__dev_fwnode(struct device *dev)
22 {
23 	return IS_ENABLED(CONFIG_OF) && dev->of_node ?
24 		of_fwnode_handle(dev->of_node) : dev->fwnode;
25 }
26 EXPORT_SYMBOL_GPL(__dev_fwnode);
27 
28 const struct fwnode_handle *__dev_fwnode_const(const struct device *dev)
29 {
30 	return IS_ENABLED(CONFIG_OF) && dev->of_node ?
31 		of_fwnode_handle(dev->of_node) : dev->fwnode;
32 }
33 EXPORT_SYMBOL_GPL(__dev_fwnode_const);
34 
35 /**
36  * device_property_present - check if a property of a device is present
37  * @dev: Device whose property is being checked
38  * @propname: Name of the property
39  *
40  * Check if property @propname is present in the device firmware description.
41  * This function is the unambiguous way to check that given property is present
42  * in the device firmware description.
43  *
44  * Return: true if property @propname is present. Otherwise, returns false.
45  */
46 bool device_property_present(const struct device *dev, const char *propname)
47 {
48 	return fwnode_property_present(dev_fwnode(dev), propname);
49 }
50 EXPORT_SYMBOL_GPL(device_property_present);
51 
52 /**
53  * fwnode_property_present - check if a property of a firmware node is present
54  * @fwnode: Firmware node whose property to check
55  * @propname: Name of the property
56  *
57  * Check if property @propname is present in the firmware node description.
58  * This function is the unambiguous way to check that given property is present
59  * in the firmware node description.
60  *
61  * Return: true if property @propname is present. Otherwise, returns false.
62  */
63 bool fwnode_property_present(const struct fwnode_handle *fwnode,
64 			     const char *propname)
65 {
66 	bool ret;
67 
68 	if (IS_ERR_OR_NULL(fwnode))
69 		return false;
70 
71 	ret = fwnode_call_bool_op(fwnode, property_present, propname);
72 	if (ret)
73 		return ret;
74 
75 	return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
76 }
77 EXPORT_SYMBOL_GPL(fwnode_property_present);
78 
79 /**
80  * device_property_read_bool - Return the value for a boolean property of a device
81  * @dev: Device whose property is being checked
82  * @propname: Name of the property
83  *
84  * Use device_property_present() to check for the property presence.
85  *
86  * Return: if property @propname is true or false in the device firmware description.
87  */
88 bool device_property_read_bool(const struct device *dev, const char *propname)
89 {
90 	return fwnode_property_read_bool(dev_fwnode(dev), propname);
91 }
92 EXPORT_SYMBOL_GPL(device_property_read_bool);
93 
94 /**
95  * fwnode_property_read_bool - Return the value for a boolean property of a firmware node
96  * @fwnode: Firmware node whose property to check
97  * @propname: Name of the property
98  *
99  * Use fwnode_property_present() to check for the property presence.
100  *
101  * Return: if property @propname is true or false in the firmware node description.
102  */
103 bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
104 			     const char *propname)
105 {
106 	bool ret;
107 
108 	if (IS_ERR_OR_NULL(fwnode))
109 		return false;
110 
111 	ret = fwnode_call_bool_op(fwnode, property_read_bool, propname);
112 	if (ret)
113 		return ret;
114 
115 	return fwnode_call_bool_op(fwnode->secondary, property_read_bool, propname);
116 }
117 EXPORT_SYMBOL_GPL(fwnode_property_read_bool);
118 
119 /**
120  * device_property_read_u8_array - return a u8 array property of a device
121  * @dev: Device to get the property of
122  * @propname: Name of the property
123  * @val: The values are stored here or %NULL to return the number of values
124  * @nval: Size of the @val array
125  *
126  * Function reads an array of u8 properties with @propname from the device
127  * firmware description and stores them to @val if found.
128  *
129  * It's recommended to call device_property_count_u8() instead of calling
130  * this function with @val equals %NULL and @nval equals 0.
131  *
132  * Return: number of values if @val was %NULL,
133  *         %0 if the property was found (success),
134  *	   %-EINVAL if given arguments are not valid,
135  *	   %-ENODATA if the property does not have a value,
136  *	   %-EPROTO if the property is not an array of numbers,
137  *	   %-EOVERFLOW if the size of the property is not as expected.
138  *	   %-ENXIO if no suitable firmware interface is present.
139  */
140 int device_property_read_u8_array(const struct device *dev, const char *propname,
141 				  u8 *val, size_t nval)
142 {
143 	return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
144 }
145 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
146 
147 /**
148  * device_property_read_u16_array - return a u16 array property of a device
149  * @dev: Device to get the property of
150  * @propname: Name of the property
151  * @val: The values are stored here or %NULL to return the number of values
152  * @nval: Size of the @val array
153  *
154  * Function reads an array of u16 properties with @propname from the device
155  * firmware description and stores them to @val if found.
156  *
157  * It's recommended to call device_property_count_u16() instead of calling
158  * this function with @val equals %NULL and @nval equals 0.
159  *
160  * Return: number of values if @val was %NULL,
161  *         %0 if the property was found (success),
162  *	   %-EINVAL if given arguments are not valid,
163  *	   %-ENODATA if the property does not have a value,
164  *	   %-EPROTO if the property is not an array of numbers,
165  *	   %-EOVERFLOW if the size of the property is not as expected.
166  *	   %-ENXIO if no suitable firmware interface is present.
167  */
168 int device_property_read_u16_array(const struct device *dev, const char *propname,
169 				   u16 *val, size_t nval)
170 {
171 	return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
172 }
173 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
174 
175 /**
176  * device_property_read_u32_array - return a u32 array property of a device
177  * @dev: Device to get the property of
178  * @propname: Name of the property
179  * @val: The values are stored here or %NULL to return the number of values
180  * @nval: Size of the @val array
181  *
182  * Function reads an array of u32 properties with @propname from the device
183  * firmware description and stores them to @val if found.
184  *
185  * It's recommended to call device_property_count_u32() instead of calling
186  * this function with @val equals %NULL and @nval equals 0.
187  *
188  * Return: number of values if @val was %NULL,
189  *         %0 if the property was found (success),
190  *	   %-EINVAL if given arguments are not valid,
191  *	   %-ENODATA if the property does not have a value,
192  *	   %-EPROTO if the property is not an array of numbers,
193  *	   %-EOVERFLOW if the size of the property is not as expected.
194  *	   %-ENXIO if no suitable firmware interface is present.
195  */
196 int device_property_read_u32_array(const struct device *dev, const char *propname,
197 				   u32 *val, size_t nval)
198 {
199 	return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
200 }
201 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
202 
203 /**
204  * device_property_read_u64_array - return a u64 array property of a device
205  * @dev: Device to get the property of
206  * @propname: Name of the property
207  * @val: The values are stored here or %NULL to return the number of values
208  * @nval: Size of the @val array
209  *
210  * Function reads an array of u64 properties with @propname from the device
211  * firmware description and stores them to @val if found.
212  *
213  * It's recommended to call device_property_count_u64() instead of calling
214  * this function with @val equals %NULL and @nval equals 0.
215  *
216  * Return: number of values if @val was %NULL,
217  *         %0 if the property was found (success),
218  *	   %-EINVAL if given arguments are not valid,
219  *	   %-ENODATA if the property does not have a value,
220  *	   %-EPROTO if the property is not an array of numbers,
221  *	   %-EOVERFLOW if the size of the property is not as expected.
222  *	   %-ENXIO if no suitable firmware interface is present.
223  */
224 int device_property_read_u64_array(const struct device *dev, const char *propname,
225 				   u64 *val, size_t nval)
226 {
227 	return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
228 }
229 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
230 
231 /**
232  * device_property_read_string_array - return a string array property of device
233  * @dev: Device to get the property of
234  * @propname: Name of the property
235  * @val: The values are stored here or %NULL to return the number of values
236  * @nval: Size of the @val array
237  *
238  * Function reads an array of string properties with @propname from the device
239  * firmware description and stores them to @val if found.
240  *
241  * It's recommended to call device_property_string_array_count() instead of calling
242  * this function with @val equals %NULL and @nval equals 0.
243  *
244  * Return: number of values read on success if @val is non-NULL,
245  *	   number of values available on success if @val is NULL,
246  *	   %-EINVAL if given arguments are not valid,
247  *	   %-ENODATA if the property does not have a value,
248  *	   %-EPROTO or %-EILSEQ if the property is not an array of strings,
249  *	   %-EOVERFLOW if the size of the property is not as expected.
250  *	   %-ENXIO if no suitable firmware interface is present.
251  */
252 int device_property_read_string_array(const struct device *dev, const char *propname,
253 				      const char **val, size_t nval)
254 {
255 	return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
256 }
257 EXPORT_SYMBOL_GPL(device_property_read_string_array);
258 
259 /**
260  * device_property_read_string - return a string property of a device
261  * @dev: Device to get the property of
262  * @propname: Name of the property
263  * @val: The value is stored here
264  *
265  * Function reads property @propname from the device firmware description and
266  * stores the value into @val if found. The value is checked to be a string.
267  *
268  * Return: %0 if the property was found (success),
269  *	   %-EINVAL if given arguments are not valid,
270  *	   %-ENODATA if the property does not have a value,
271  *	   %-EPROTO or %-EILSEQ if the property type is not a string.
272  *	   %-ENXIO if no suitable firmware interface is present.
273  */
274 int device_property_read_string(const struct device *dev, const char *propname,
275 				const char **val)
276 {
277 	return fwnode_property_read_string(dev_fwnode(dev), propname, val);
278 }
279 EXPORT_SYMBOL_GPL(device_property_read_string);
280 
281 /**
282  * device_property_match_string - find a string in an array and return index
283  * @dev: Device to get the property of
284  * @propname: Name of the property holding the array
285  * @string: String to look for
286  *
287  * Find a given string in a string array and if it is found return the
288  * index back.
289  *
290  * Return: index, starting from %0, if the property was found (success),
291  *	   %-EINVAL if given arguments are not valid,
292  *	   %-ENODATA if the property does not have a value,
293  *	   %-EPROTO if the property is not an array of strings,
294  *	   %-ENXIO if no suitable firmware interface is present.
295  */
296 int device_property_match_string(const struct device *dev, const char *propname,
297 				 const char *string)
298 {
299 	return fwnode_property_match_string(dev_fwnode(dev), propname, string);
300 }
301 EXPORT_SYMBOL_GPL(device_property_match_string);
302 
303 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
304 					  const char *propname,
305 					  unsigned int elem_size, void *val,
306 					  size_t nval)
307 {
308 	int ret;
309 
310 	if (IS_ERR_OR_NULL(fwnode))
311 		return -EINVAL;
312 
313 	ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
314 				 elem_size, val, nval);
315 	if (ret != -EINVAL)
316 		return ret;
317 
318 	return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
319 				  elem_size, val, nval);
320 }
321 
322 /**
323  * fwnode_property_read_u8_array - return a u8 array property of firmware node
324  * @fwnode: Firmware node to get the property of
325  * @propname: Name of the property
326  * @val: The values are stored here or %NULL to return the number of values
327  * @nval: Size of the @val array
328  *
329  * Read an array of u8 properties with @propname from @fwnode and stores them to
330  * @val if found.
331  *
332  * It's recommended to call fwnode_property_count_u8() instead of calling
333  * this function with @val equals %NULL and @nval equals 0.
334  *
335  * Return: number of values if @val was %NULL,
336  *         %0 if the property was found (success),
337  *	   %-EINVAL if given arguments are not valid,
338  *	   %-ENODATA if the property does not have a value,
339  *	   %-EPROTO if the property is not an array of numbers,
340  *	   %-EOVERFLOW if the size of the property is not as expected,
341  *	   %-ENXIO if no suitable firmware interface is present.
342  */
343 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
344 				  const char *propname, u8 *val, size_t nval)
345 {
346 	return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
347 					      val, nval);
348 }
349 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
350 
351 /**
352  * fwnode_property_read_u16_array - return a u16 array property of firmware node
353  * @fwnode: Firmware node to get the property of
354  * @propname: Name of the property
355  * @val: The values are stored here or %NULL to return the number of values
356  * @nval: Size of the @val array
357  *
358  * Read an array of u16 properties with @propname from @fwnode and store them to
359  * @val if found.
360  *
361  * It's recommended to call fwnode_property_count_u16() instead of calling
362  * this function with @val equals %NULL and @nval equals 0.
363  *
364  * Return: number of values if @val was %NULL,
365  *         %0 if the property was found (success),
366  *	   %-EINVAL if given arguments are not valid,
367  *	   %-ENODATA if the property does not have a value,
368  *	   %-EPROTO if the property is not an array of numbers,
369  *	   %-EOVERFLOW if the size of the property is not as expected,
370  *	   %-ENXIO if no suitable firmware interface is present.
371  */
372 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
373 				   const char *propname, u16 *val, size_t nval)
374 {
375 	return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
376 					      val, nval);
377 }
378 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
379 
380 /**
381  * fwnode_property_read_u32_array - return a u32 array property of firmware node
382  * @fwnode: Firmware node to get the property of
383  * @propname: Name of the property
384  * @val: The values are stored here or %NULL to return the number of values
385  * @nval: Size of the @val array
386  *
387  * Read an array of u32 properties with @propname from @fwnode store them to
388  * @val if found.
389  *
390  * It's recommended to call fwnode_property_count_u32() instead of calling
391  * this function with @val equals %NULL and @nval equals 0.
392  *
393  * Return: number of values if @val was %NULL,
394  *         %0 if the property was found (success),
395  *	   %-EINVAL if given arguments are not valid,
396  *	   %-ENODATA if the property does not have a value,
397  *	   %-EPROTO if the property is not an array of numbers,
398  *	   %-EOVERFLOW if the size of the property is not as expected,
399  *	   %-ENXIO if no suitable firmware interface is present.
400  */
401 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
402 				   const char *propname, u32 *val, size_t nval)
403 {
404 	return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
405 					      val, nval);
406 }
407 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
408 
409 /**
410  * fwnode_property_read_u64_array - return a u64 array property firmware node
411  * @fwnode: Firmware node to get the property of
412  * @propname: Name of the property
413  * @val: The values are stored here or %NULL to return the number of values
414  * @nval: Size of the @val array
415  *
416  * Read an array of u64 properties with @propname from @fwnode and store them to
417  * @val if found.
418  *
419  * It's recommended to call fwnode_property_count_u64() instead of calling
420  * this function with @val equals %NULL and @nval equals 0.
421  *
422  * Return: number of values if @val was %NULL,
423  *         %0 if the property was found (success),
424  *	   %-EINVAL if given arguments are not valid,
425  *	   %-ENODATA if the property does not have a value,
426  *	   %-EPROTO if the property is not an array of numbers,
427  *	   %-EOVERFLOW if the size of the property is not as expected,
428  *	   %-ENXIO if no suitable firmware interface is present.
429  */
430 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
431 				   const char *propname, u64 *val, size_t nval)
432 {
433 	return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
434 					      val, nval);
435 }
436 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
437 
438 /**
439  * fwnode_property_read_string_array - return string array property of a node
440  * @fwnode: Firmware node to get the property of
441  * @propname: Name of the property
442  * @val: The values are stored here or %NULL to return the number of values
443  * @nval: Size of the @val array
444  *
445  * Read an string list property @propname from the given firmware node and store
446  * them to @val if found.
447  *
448  * It's recommended to call fwnode_property_string_array_count() instead of calling
449  * this function with @val equals %NULL and @nval equals 0.
450  *
451  * Return: number of values read on success if @val is non-NULL,
452  *	   number of values available on success if @val is NULL,
453  *	   %-EINVAL if given arguments are not valid,
454  *	   %-ENODATA if the property does not have a value,
455  *	   %-EPROTO or %-EILSEQ if the property is not an array of strings,
456  *	   %-EOVERFLOW if the size of the property is not as expected,
457  *	   %-ENXIO if no suitable firmware interface is present.
458  */
459 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
460 				      const char *propname, const char **val,
461 				      size_t nval)
462 {
463 	int ret;
464 
465 	if (IS_ERR_OR_NULL(fwnode))
466 		return -EINVAL;
467 
468 	ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
469 				 val, nval);
470 	if (ret != -EINVAL)
471 		return ret;
472 
473 	return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
474 				  val, nval);
475 }
476 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
477 
478 /**
479  * fwnode_property_read_string - return a string property of a firmware node
480  * @fwnode: Firmware node to get the property of
481  * @propname: Name of the property
482  * @val: The value is stored here
483  *
484  * Read property @propname from the given firmware node and store the value into
485  * @val if found.  The value is checked to be a string.
486  *
487  * Return: %0 if the property was found (success),
488  *	   %-EINVAL if given arguments are not valid,
489  *	   %-ENODATA if the property does not have a value,
490  *	   %-EPROTO or %-EILSEQ if the property is not a string,
491  *	   %-ENXIO if no suitable firmware interface is present.
492  */
493 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
494 				const char *propname, const char **val)
495 {
496 	int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
497 
498 	return ret < 0 ? ret : 0;
499 }
500 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
501 
502 /**
503  * fwnode_property_match_string - find a string in an array and return index
504  * @fwnode: Firmware node to get the property of
505  * @propname: Name of the property holding the array
506  * @string: String to look for
507  *
508  * Find a given string in a string array and if it is found return the
509  * index back.
510  *
511  * Return: index, starting from %0, if the property was found (success),
512  *	   %-EINVAL if given arguments are not valid,
513  *	   %-ENODATA if the property does not have a value,
514  *	   %-EPROTO if the property is not an array of strings,
515  *	   %-ENXIO if no suitable firmware interface is present.
516  */
517 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
518 	const char *propname, const char *string)
519 {
520 	const char **values;
521 	int nval, ret;
522 
523 	nval = fwnode_property_string_array_count(fwnode, propname);
524 	if (nval < 0)
525 		return nval;
526 
527 	if (nval == 0)
528 		return -ENODATA;
529 
530 	values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
531 	if (!values)
532 		return -ENOMEM;
533 
534 	ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
535 	if (ret < 0)
536 		goto out_free;
537 
538 	ret = match_string(values, nval, string);
539 	if (ret < 0)
540 		ret = -ENODATA;
541 
542 out_free:
543 	kfree(values);
544 	return ret;
545 }
546 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
547 
548 /**
549  * fwnode_property_match_property_string - find a property string value in an array and return index
550  * @fwnode: Firmware node to get the property of
551  * @propname: Name of the property holding the string value
552  * @array: String array to search in
553  * @n: Size of the @array
554  *
555  * Find a property string value in a given @array and if it is found return
556  * the index back.
557  *
558  * Return: index, starting from %0, if the string value was found in the @array (success),
559  *	   %-ENOENT when the string value was not found in the @array,
560  *	   %-EINVAL if given arguments are not valid,
561  *	   %-ENODATA if the property does not have a value,
562  *	   %-EPROTO or %-EILSEQ if the property is not a string,
563  *	   %-ENXIO if no suitable firmware interface is present.
564  */
565 int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
566 	const char *propname, const char * const *array, size_t n)
567 {
568 	const char *string;
569 	int ret;
570 
571 	ret = fwnode_property_read_string(fwnode, propname, &string);
572 	if (ret)
573 		return ret;
574 
575 	ret = match_string(array, n, string);
576 	if (ret < 0)
577 		ret = -ENOENT;
578 
579 	return ret;
580 }
581 EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);
582 
583 /**
584  * fwnode_property_get_reference_args() - Find a reference with arguments
585  * @fwnode:	Firmware node where to look for the reference
586  * @prop:	The name of the property
587  * @nargs_prop:	The name of the property telling the number of
588  *		arguments in the referred node. NULL if @nargs is known,
589  *		otherwise @nargs is ignored.
590  * @nargs:	Number of arguments. Ignored if @nargs_prop is non-NULL.
591  * @index:	Index of the reference, from zero onwards.
592  * @args:	Result structure with reference and integer arguments.
593  *		May be NULL.
594  *
595  * Obtain a reference based on a named property in an fwnode, with
596  * integer arguments.
597  *
598  * The caller is responsible for calling fwnode_handle_put() on the returned
599  * @args->fwnode pointer.
600  *
601  * Return: %0 on success
602  *	    %-ENOENT when the index is out of bounds, the index has an empty
603  *		     reference or the property was not found
604  *	    %-EINVAL on parse error
605  */
606 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
607 				       const char *prop, const char *nargs_prop,
608 				       unsigned int nargs, unsigned int index,
609 				       struct fwnode_reference_args *args)
610 {
611 	int ret;
612 
613 	if (IS_ERR_OR_NULL(fwnode))
614 		return -ENOENT;
615 
616 	ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
617 				 nargs, index, args);
618 	if (ret == 0)
619 		return ret;
620 
621 	if (IS_ERR_OR_NULL(fwnode->secondary))
622 		return ret;
623 
624 	return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
625 				  nargs, index, args);
626 }
627 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
628 
629 /**
630  * fwnode_find_reference - Find named reference to a fwnode_handle
631  * @fwnode: Firmware node where to look for the reference
632  * @name: The name of the reference
633  * @index: Index of the reference
634  *
635  * @index can be used when the named reference holds a table of references.
636  *
637  * The caller is responsible for calling fwnode_handle_put() on the returned
638  * fwnode pointer.
639  *
640  * Return: a pointer to the reference fwnode, when found. Otherwise,
641  * returns an error pointer.
642  */
643 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
644 					    const char *name,
645 					    unsigned int index)
646 {
647 	struct fwnode_reference_args args;
648 	int ret;
649 
650 	ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
651 						 &args);
652 	return ret ? ERR_PTR(ret) : args.fwnode;
653 }
654 EXPORT_SYMBOL_GPL(fwnode_find_reference);
655 
656 /**
657  * fwnode_get_name - Return the name of a node
658  * @fwnode: The firmware node
659  *
660  * Return: a pointer to the node name, or %NULL.
661  */
662 const char *fwnode_get_name(const struct fwnode_handle *fwnode)
663 {
664 	return fwnode_call_ptr_op(fwnode, get_name);
665 }
666 EXPORT_SYMBOL_GPL(fwnode_get_name);
667 
668 /**
669  * fwnode_get_name_prefix - Return the prefix of node for printing purposes
670  * @fwnode: The firmware node
671  *
672  * Return: the prefix of a node, intended to be printed right before the node.
673  * The prefix works also as a separator between the nodes.
674  */
675 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
676 {
677 	return fwnode_call_ptr_op(fwnode, get_name_prefix);
678 }
679 
680 /**
681  * fwnode_name_eq - Return true if node name is equal
682  * @fwnode: The firmware node
683  * @name: The name to which to compare the node name
684  *
685  * Compare the name provided as an argument to the name of the node, stopping
686  * the comparison at either NUL or '@' character, whichever comes first. This
687  * function is generally used for comparing node names while ignoring the
688  * possible unit address of the node.
689  *
690  * Return: true if the node name matches with the name provided in the @name
691  * argument, false otherwise.
692  */
693 bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)
694 {
695 	const char *node_name;
696 	ptrdiff_t len;
697 
698 	node_name = fwnode_get_name(fwnode);
699 	if (!node_name)
700 		return false;
701 
702 	len = strchrnul(node_name, '@') - node_name;
703 
704 	return str_has_prefix(node_name, name) == len;
705 }
706 EXPORT_SYMBOL_GPL(fwnode_name_eq);
707 
708 /**
709  * fwnode_get_parent - Return parent firwmare node
710  * @fwnode: Firmware whose parent is retrieved
711  *
712  * The caller is responsible for calling fwnode_handle_put() on the returned
713  * fwnode pointer.
714  *
715  * Return: parent firmware node of the given node if possible or %NULL if no
716  * parent was available.
717  */
718 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
719 {
720 	return fwnode_call_ptr_op(fwnode, get_parent);
721 }
722 EXPORT_SYMBOL_GPL(fwnode_get_parent);
723 
724 /**
725  * fwnode_get_next_parent - Iterate to the node's parent
726  * @fwnode: Firmware whose parent is retrieved
727  *
728  * This is like fwnode_get_parent() except that it drops the refcount
729  * on the passed node, making it suitable for iterating through a
730  * node's parents.
731  *
732  * The caller is responsible for calling fwnode_handle_put() on the returned
733  * fwnode pointer. Note that this function also puts a reference to @fwnode
734  * unconditionally.
735  *
736  * Return: parent firmware node of the given node if possible or %NULL if no
737  * parent was available.
738  */
739 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
740 {
741 	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
742 
743 	fwnode_handle_put(fwnode);
744 
745 	return parent;
746 }
747 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
748 
749 /**
750  * fwnode_count_parents - Return the number of parents a node has
751  * @fwnode: The node the parents of which are to be counted
752  *
753  * Return: the number of parents a node has.
754  */
755 unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
756 {
757 	struct fwnode_handle *parent;
758 	unsigned int count = 0;
759 
760 	fwnode_for_each_parent_node(fwnode, parent)
761 		count++;
762 
763 	return count;
764 }
765 EXPORT_SYMBOL_GPL(fwnode_count_parents);
766 
767 /**
768  * fwnode_get_nth_parent - Return an nth parent of a node
769  * @fwnode: The node the parent of which is requested
770  * @depth: Distance of the parent from the node
771  *
772  * The caller is responsible for calling fwnode_handle_put() on the returned
773  * fwnode pointer.
774  *
775  * Return: the nth parent of a node. If there is no parent at the requested
776  * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
777  * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
778  */
779 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
780 					    unsigned int depth)
781 {
782 	struct fwnode_handle *parent;
783 
784 	if (depth == 0)
785 		return fwnode_handle_get(fwnode);
786 
787 	fwnode_for_each_parent_node(fwnode, parent) {
788 		if (--depth == 0)
789 			return parent;
790 	}
791 	return NULL;
792 }
793 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
794 
795 /**
796  * fwnode_get_next_child_node - Return the next child node handle for a node
797  * @fwnode: Firmware node to find the next child node for.
798  * @child: Handle to one of the node's child nodes or a %NULL handle.
799  *
800  * The caller is responsible for calling fwnode_handle_put() on the returned
801  * fwnode pointer. Note that this function also puts a reference to @child
802  * unconditionally.
803  */
804 struct fwnode_handle *
805 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
806 			   struct fwnode_handle *child)
807 {
808 	struct fwnode_handle *next;
809 
810 	if (IS_ERR_OR_NULL(fwnode))
811 		return NULL;
812 
813 	/* Try to find a child in primary fwnode */
814 	next = fwnode_call_ptr_op(fwnode, get_next_child_node, child);
815 	if (next)
816 		return next;
817 
818 	/* When no more children in primary, continue with secondary */
819 	return fwnode_call_ptr_op(fwnode->secondary, get_next_child_node, child);
820 }
821 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
822 
823 /**
824  * fwnode_get_next_available_child_node - Return the next available child node handle for a node
825  * @fwnode: Firmware node to find the next child node for.
826  * @child: Handle to one of the node's child nodes or a %NULL handle.
827  *
828  * The caller is responsible for calling fwnode_handle_put() on the returned
829  * fwnode pointer. Note that this function also puts a reference to @child
830  * unconditionally.
831  */
832 struct fwnode_handle *
833 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
834 				     struct fwnode_handle *child)
835 {
836 	struct fwnode_handle *next_child = child;
837 
838 	if (IS_ERR_OR_NULL(fwnode))
839 		return NULL;
840 
841 	do {
842 		next_child = fwnode_get_next_child_node(fwnode, next_child);
843 		if (!next_child)
844 			return NULL;
845 	} while (!fwnode_device_is_available(next_child));
846 
847 	return next_child;
848 }
849 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
850 
851 /**
852  * device_get_next_child_node - Return the next child node handle for a device
853  * @dev: Device to find the next child node for.
854  * @child: Handle to one of the device's child nodes or a %NULL handle.
855  *
856  * The caller is responsible for calling fwnode_handle_put() on the returned
857  * fwnode pointer. Note that this function also puts a reference to @child
858  * unconditionally.
859  */
860 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
861 						 struct fwnode_handle *child)
862 {
863 	return fwnode_get_next_child_node(dev_fwnode(dev), child);
864 }
865 EXPORT_SYMBOL_GPL(device_get_next_child_node);
866 
867 /**
868  * fwnode_get_named_child_node - Return first matching named child node handle
869  * @fwnode: Firmware node to find the named child node for.
870  * @childname: String to match child node name against.
871  *
872  * The caller is responsible for calling fwnode_handle_put() on the returned
873  * fwnode pointer.
874  */
875 struct fwnode_handle *
876 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
877 			    const char *childname)
878 {
879 	return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
880 }
881 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
882 
883 /**
884  * device_get_named_child_node - Return first matching named child node handle
885  * @dev: Device to find the named child node for.
886  * @childname: String to match child node name against.
887  *
888  * The caller is responsible for calling fwnode_handle_put() on the returned
889  * fwnode pointer.
890  */
891 struct fwnode_handle *device_get_named_child_node(const struct device *dev,
892 						  const char *childname)
893 {
894 	return fwnode_get_named_child_node(dev_fwnode(dev), childname);
895 }
896 EXPORT_SYMBOL_GPL(device_get_named_child_node);
897 
898 /**
899  * fwnode_handle_get - Obtain a reference to a device node
900  * @fwnode: Pointer to the device node to obtain the reference to.
901  *
902  * The caller is responsible for calling fwnode_handle_put() on the returned
903  * fwnode pointer.
904  *
905  * Return: the fwnode handle.
906  */
907 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
908 {
909 	if (!fwnode_has_op(fwnode, get))
910 		return fwnode;
911 
912 	return fwnode_call_ptr_op(fwnode, get);
913 }
914 EXPORT_SYMBOL_GPL(fwnode_handle_get);
915 
916 /**
917  * fwnode_device_is_available - check if a device is available for use
918  * @fwnode: Pointer to the fwnode of the device.
919  *
920  * Return: true if device is available for use. Otherwise, returns false.
921  *
922  * For fwnode node types that don't implement the .device_is_available()
923  * operation, this function returns true.
924  */
925 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
926 {
927 	if (IS_ERR_OR_NULL(fwnode))
928 		return false;
929 
930 	if (!fwnode_has_op(fwnode, device_is_available))
931 		return true;
932 
933 	return fwnode_call_bool_op(fwnode, device_is_available);
934 }
935 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
936 
937 /**
938  * fwnode_get_child_node_count - return the number of child nodes for a given firmware node
939  * @fwnode: Pointer to the parent firmware node
940  *
941  * Return: the number of child nodes for a given firmware node.
942  */
943 unsigned int fwnode_get_child_node_count(const struct fwnode_handle *fwnode)
944 {
945 	struct fwnode_handle *child;
946 	unsigned int count = 0;
947 
948 	fwnode_for_each_child_node(fwnode, child)
949 		count++;
950 
951 	return count;
952 }
953 EXPORT_SYMBOL_GPL(fwnode_get_child_node_count);
954 
955 /**
956  * fwnode_get_named_child_node_count - number of child nodes with given name
957  * @fwnode: Node which child nodes are counted.
958  * @name: String to match child node name against.
959  *
960  * Scan child nodes and count all the nodes with a specific name. Potential
961  * 'number' -ending after the 'at sign' for scanned names is ignored.
962  * E.g.::
963  *   fwnode_get_named_child_node_count(fwnode, "channel");
964  * would match all the nodes::
965  *   channel { }, channel@0 {}, channel@0xabba {}...
966  *
967  * Return: the number of child nodes with a matching name for a given device.
968  */
969 unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,
970 					       const char *name)
971 {
972 	struct fwnode_handle *child;
973 	unsigned int count = 0;
974 
975 	fwnode_for_each_named_child_node(fwnode, child, name)
976 		count++;
977 
978 	return count;
979 }
980 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count);
981 
982 bool device_dma_supported(const struct device *dev)
983 {
984 	return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
985 }
986 EXPORT_SYMBOL_GPL(device_dma_supported);
987 
988 enum dev_dma_attr device_get_dma_attr(const struct device *dev)
989 {
990 	if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))
991 		return DEV_DMA_NOT_SUPPORTED;
992 
993 	return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);
994 }
995 EXPORT_SYMBOL_GPL(device_get_dma_attr);
996 
997 /**
998  * fwnode_get_phy_mode - Get phy mode for given firmware node
999  * @fwnode:	Pointer to the given node
1000  *
1001  * The function gets phy interface string from property 'phy-mode' or
1002  * 'phy-connection-type', and return its index in phy_modes table, or errno in
1003  * error case.
1004  */
1005 int fwnode_get_phy_mode(const struct fwnode_handle *fwnode)
1006 {
1007 	const char *pm;
1008 	int err, i;
1009 
1010 	err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
1011 	if (err < 0)
1012 		err = fwnode_property_read_string(fwnode,
1013 						  "phy-connection-type", &pm);
1014 	if (err < 0)
1015 		return err;
1016 
1017 	for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1018 		if (!strcasecmp(pm, phy_modes(i)))
1019 			return i;
1020 
1021 	return -ENODEV;
1022 }
1023 EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
1024 
1025 /**
1026  * device_get_phy_mode - Get phy mode for given device
1027  * @dev:	Pointer to the given device
1028  *
1029  * The function gets phy interface string from property 'phy-mode' or
1030  * 'phy-connection-type', and return its index in phy_modes table, or errno in
1031  * error case.
1032  */
1033 int device_get_phy_mode(struct device *dev)
1034 {
1035 	return fwnode_get_phy_mode(dev_fwnode(dev));
1036 }
1037 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1038 
1039 /**
1040  * fwnode_iomap - Maps the memory mapped IO for a given fwnode
1041  * @fwnode:	Pointer to the firmware node
1042  * @index:	Index of the IO range
1043  *
1044  * Return: a pointer to the mapped memory.
1045  */
1046 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)
1047 {
1048 	return fwnode_call_ptr_op(fwnode, iomap, index);
1049 }
1050 EXPORT_SYMBOL(fwnode_iomap);
1051 
1052 /**
1053  * fwnode_irq_get - Get IRQ directly from a fwnode
1054  * @fwnode:	Pointer to the firmware node
1055  * @index:	Zero-based index of the IRQ
1056  *
1057  * Return: Linux IRQ number on success. Negative errno on failure.
1058  */
1059 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
1060 {
1061 	int ret;
1062 
1063 	ret = fwnode_call_int_op(fwnode, irq_get, index);
1064 	/* We treat mapping errors as invalid case */
1065 	if (ret == 0)
1066 		return -EINVAL;
1067 
1068 	return ret;
1069 }
1070 EXPORT_SYMBOL(fwnode_irq_get);
1071 
1072 /**
1073  * fwnode_irq_get_byname - Get IRQ from a fwnode using its name
1074  * @fwnode:	Pointer to the firmware node
1075  * @name:	IRQ name
1076  *
1077  * Description:
1078  * Find a match to the string @name in the 'interrupt-names' string array
1079  * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ
1080  * number of the IRQ resource corresponding to the index of the matched
1081  * string.
1082  *
1083  * Return: Linux IRQ number on success, or negative errno otherwise.
1084  */
1085 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
1086 {
1087 	int index;
1088 
1089 	if (!name)
1090 		return -EINVAL;
1091 
1092 	index = fwnode_property_match_string(fwnode, "interrupt-names",  name);
1093 	if (index < 0)
1094 		return index;
1095 
1096 	return fwnode_irq_get(fwnode, index);
1097 }
1098 EXPORT_SYMBOL(fwnode_irq_get_byname);
1099 
1100 /**
1101  * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
1102  * @fwnode: Pointer to the parent firmware node
1103  * @prev: Previous endpoint node or %NULL to get the first
1104  *
1105  * The caller is responsible for calling fwnode_handle_put() on the returned
1106  * fwnode pointer. Note that this function also puts a reference to @prev
1107  * unconditionally.
1108  *
1109  * Return: an endpoint firmware node pointer or %NULL if no more endpoints
1110  * are available.
1111  */
1112 struct fwnode_handle *
1113 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1114 			       struct fwnode_handle *prev)
1115 {
1116 	struct fwnode_handle *ep, *port_parent = NULL;
1117 	const struct fwnode_handle *parent;
1118 
1119 	/*
1120 	 * If this function is in a loop and the previous iteration returned
1121 	 * an endpoint from fwnode->secondary, then we need to use the secondary
1122 	 * as parent rather than @fwnode.
1123 	 */
1124 	if (prev) {
1125 		port_parent = fwnode_graph_get_port_parent(prev);
1126 		parent = port_parent;
1127 	} else {
1128 		parent = fwnode;
1129 	}
1130 	if (IS_ERR_OR_NULL(parent))
1131 		return NULL;
1132 
1133 	ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
1134 	if (ep)
1135 		goto out_put_port_parent;
1136 
1137 	ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);
1138 
1139 out_put_port_parent:
1140 	fwnode_handle_put(port_parent);
1141 	return ep;
1142 }
1143 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1144 
1145 /**
1146  * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1147  * @endpoint: Endpoint firmware node of the port
1148  *
1149  * The caller is responsible for calling fwnode_handle_put() on the returned
1150  * fwnode pointer.
1151  *
1152  * Return: the firmware node of the device the @endpoint belongs to.
1153  */
1154 struct fwnode_handle *
1155 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1156 {
1157 	struct fwnode_handle *port, *parent;
1158 
1159 	port = fwnode_get_parent(endpoint);
1160 	parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1161 
1162 	fwnode_handle_put(port);
1163 
1164 	return parent;
1165 }
1166 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1167 
1168 /**
1169  * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1170  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1171  *
1172  * Extracts firmware node of a remote device the @fwnode points to.
1173  *
1174  * The caller is responsible for calling fwnode_handle_put() on the returned
1175  * fwnode pointer.
1176  */
1177 struct fwnode_handle *
1178 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1179 {
1180 	struct fwnode_handle *endpoint, *parent;
1181 
1182 	endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1183 	parent = fwnode_graph_get_port_parent(endpoint);
1184 
1185 	fwnode_handle_put(endpoint);
1186 
1187 	return parent;
1188 }
1189 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1190 
1191 /**
1192  * fwnode_graph_get_remote_port - Return fwnode of a remote port
1193  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1194  *
1195  * Extracts firmware node of a remote port the @fwnode points to.
1196  *
1197  * The caller is responsible for calling fwnode_handle_put() on the returned
1198  * fwnode pointer.
1199  */
1200 struct fwnode_handle *
1201 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1202 {
1203 	return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1204 }
1205 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1206 
1207 /**
1208  * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1209  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1210  *
1211  * Extracts firmware node of a remote endpoint the @fwnode points to.
1212  *
1213  * The caller is responsible for calling fwnode_handle_put() on the returned
1214  * fwnode pointer.
1215  */
1216 struct fwnode_handle *
1217 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1218 {
1219 	return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1220 }
1221 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1222 
1223 static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
1224 {
1225 	struct fwnode_handle *dev_node;
1226 	bool available;
1227 
1228 	dev_node = fwnode_graph_get_remote_port_parent(ep);
1229 	available = fwnode_device_is_available(dev_node);
1230 	fwnode_handle_put(dev_node);
1231 
1232 	return available;
1233 }
1234 
1235 /**
1236  * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1237  * @fwnode: parent fwnode_handle containing the graph
1238  * @port: identifier of the port node
1239  * @endpoint: identifier of the endpoint node under the port node
1240  * @flags: fwnode lookup flags
1241  *
1242  * The caller is responsible for calling fwnode_handle_put() on the returned
1243  * fwnode pointer.
1244  *
1245  * Return: the fwnode handle of the local endpoint corresponding the port and
1246  * endpoint IDs or %NULL if not found.
1247  *
1248  * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1249  * has not been found, look for the closest endpoint ID greater than the
1250  * specified one and return the endpoint that corresponds to it, if present.
1251  *
1252  * Does not return endpoints that belong to disabled devices or endpoints that
1253  * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1254  */
1255 struct fwnode_handle *
1256 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1257 				u32 port, u32 endpoint, unsigned long flags)
1258 {
1259 	struct fwnode_handle *ep, *best_ep = NULL;
1260 	unsigned int best_ep_id = 0;
1261 	bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1262 	bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1263 
1264 	fwnode_graph_for_each_endpoint(fwnode, ep) {
1265 		struct fwnode_endpoint fwnode_ep = { 0 };
1266 		int ret;
1267 
1268 		if (enabled_only && !fwnode_graph_remote_available(ep))
1269 			continue;
1270 
1271 		ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1272 		if (ret < 0)
1273 			continue;
1274 
1275 		if (fwnode_ep.port != port)
1276 			continue;
1277 
1278 		if (fwnode_ep.id == endpoint)
1279 			return ep;
1280 
1281 		if (!endpoint_next)
1282 			continue;
1283 
1284 		/*
1285 		 * If the endpoint that has just been found is not the first
1286 		 * matching one and the ID of the one found previously is closer
1287 		 * to the requested endpoint ID, skip it.
1288 		 */
1289 		if (fwnode_ep.id < endpoint ||
1290 		    (best_ep && best_ep_id < fwnode_ep.id))
1291 			continue;
1292 
1293 		fwnode_handle_put(best_ep);
1294 		best_ep = fwnode_handle_get(ep);
1295 		best_ep_id = fwnode_ep.id;
1296 	}
1297 
1298 	return best_ep;
1299 }
1300 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1301 
1302 /**
1303  * fwnode_graph_get_endpoint_count - Count endpoints on a device node
1304  * @fwnode: The node related to a device
1305  * @flags: fwnode lookup flags
1306  * Count endpoints in a device node.
1307  *
1308  * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
1309  * and endpoints connected to disabled devices are counted.
1310  */
1311 unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
1312 					     unsigned long flags)
1313 {
1314 	struct fwnode_handle *ep;
1315 	unsigned int count = 0;
1316 
1317 	fwnode_graph_for_each_endpoint(fwnode, ep) {
1318 		if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
1319 		    fwnode_graph_remote_available(ep))
1320 			count++;
1321 	}
1322 
1323 	return count;
1324 }
1325 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
1326 
1327 /**
1328  * fwnode_graph_parse_endpoint - parse common endpoint node properties
1329  * @fwnode: pointer to endpoint fwnode_handle
1330  * @endpoint: pointer to the fwnode endpoint data structure
1331  *
1332  * Parse @fwnode representing a graph endpoint node and store the
1333  * information in @endpoint. The caller must hold a reference to
1334  * @fwnode.
1335  */
1336 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1337 				struct fwnode_endpoint *endpoint)
1338 {
1339 	memset(endpoint, 0, sizeof(*endpoint));
1340 
1341 	return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1342 }
1343 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1344 
1345 const void *device_get_match_data(const struct device *dev)
1346 {
1347 	return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1348 }
1349 EXPORT_SYMBOL_GPL(device_get_match_data);
1350 
1351 static unsigned int fwnode_graph_devcon_matches(const struct fwnode_handle *fwnode,
1352 						const char *con_id, void *data,
1353 						devcon_match_fn_t match,
1354 						void **matches,
1355 						unsigned int matches_len)
1356 {
1357 	struct fwnode_handle *node;
1358 	struct fwnode_handle *ep;
1359 	unsigned int count = 0;
1360 	void *ret;
1361 
1362 	fwnode_graph_for_each_endpoint(fwnode, ep) {
1363 		if (matches && count >= matches_len) {
1364 			fwnode_handle_put(ep);
1365 			break;
1366 		}
1367 
1368 		node = fwnode_graph_get_remote_port_parent(ep);
1369 		if (!fwnode_device_is_available(node)) {
1370 			fwnode_handle_put(node);
1371 			continue;
1372 		}
1373 
1374 		ret = match(node, con_id, data);
1375 		fwnode_handle_put(node);
1376 		if (ret) {
1377 			if (matches)
1378 				matches[count] = ret;
1379 			count++;
1380 		}
1381 	}
1382 	return count;
1383 }
1384 
1385 static unsigned int fwnode_devcon_matches(const struct fwnode_handle *fwnode,
1386 					  const char *con_id, void *data,
1387 					  devcon_match_fn_t match,
1388 					  void **matches,
1389 					  unsigned int matches_len)
1390 {
1391 	struct fwnode_handle *node;
1392 	unsigned int count = 0;
1393 	unsigned int i;
1394 	void *ret;
1395 
1396 	for (i = 0; ; i++) {
1397 		if (matches && count >= matches_len)
1398 			break;
1399 
1400 		node = fwnode_find_reference(fwnode, con_id, i);
1401 		if (IS_ERR(node))
1402 			break;
1403 
1404 		ret = match(node, NULL, data);
1405 		fwnode_handle_put(node);
1406 		if (ret) {
1407 			if (matches)
1408 				matches[count] = ret;
1409 			count++;
1410 		}
1411 	}
1412 
1413 	return count;
1414 }
1415 
1416 /**
1417  * fwnode_connection_find_match - Find connection from a device node
1418  * @fwnode: Device node with the connection
1419  * @con_id: Identifier for the connection
1420  * @data: Data for the match function
1421  * @match: Function to check and convert the connection description
1422  *
1423  * Find a connection with unique identifier @con_id between @fwnode and another
1424  * device node. @match will be used to convert the connection description to
1425  * data the caller is expecting to be returned.
1426  */
1427 void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
1428 				   const char *con_id, void *data,
1429 				   devcon_match_fn_t match)
1430 {
1431 	unsigned int count;
1432 	void *ret;
1433 
1434 	if (!fwnode || !match)
1435 		return NULL;
1436 
1437 	count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1438 	if (count)
1439 		return ret;
1440 
1441 	count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1442 	return count ? ret : NULL;
1443 }
1444 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
1445 
1446 /**
1447  * fwnode_connection_find_matches - Find connections from a device node
1448  * @fwnode: Device node with the connection
1449  * @con_id: Identifier for the connection
1450  * @data: Data for the match function
1451  * @match: Function to check and convert the connection description
1452  * @matches: (Optional) array of pointers to fill with matches
1453  * @matches_len: Length of @matches
1454  *
1455  * Find up to @matches_len connections with unique identifier @con_id between
1456  * @fwnode and other device nodes. @match will be used to convert the
1457  * connection description to data the caller is expecting to be returned
1458  * through the @matches array.
1459  *
1460  * If @matches is %NULL @matches_len is ignored and the total number of resolved
1461  * matches is returned.
1462  *
1463  * Return: Number of matches resolved, or negative errno.
1464  */
1465 int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
1466 				   const char *con_id, void *data,
1467 				   devcon_match_fn_t match,
1468 				   void **matches, unsigned int matches_len)
1469 {
1470 	unsigned int count_graph;
1471 	unsigned int count_ref;
1472 
1473 	if (!fwnode || !match)
1474 		return -EINVAL;
1475 
1476 	count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,
1477 						  matches, matches_len);
1478 
1479 	if (matches) {
1480 		matches += count_graph;
1481 		matches_len -= count_graph;
1482 	}
1483 
1484 	count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,
1485 					  matches, matches_len);
1486 
1487 	return count_graph + count_ref;
1488 }
1489 EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);
1490