xref: /linux/drivers/base/property.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
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/cleanup.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/export.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 	int nval, ret;
521 
522 	nval = fwnode_property_string_array_count(fwnode, propname);
523 	if (nval < 0)
524 		return nval;
525 
526 	if (nval == 0)
527 		return -ENODATA;
528 
529 	const char **values __free(kfree) = kcalloc(nval, sizeof(*values), GFP_KERNEL);
530 	if (!values)
531 		return -ENOMEM;
532 
533 	ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
534 	if (ret < 0)
535 		return ret;
536 
537 	ret = match_string(values, nval, string);
538 	if (ret < 0)
539 		return -ENODATA;
540 
541 	return ret;
542 }
543 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
544 
545 /**
546  * fwnode_property_match_property_string - find a property string value in an array and return index
547  * @fwnode: Firmware node to get the property of
548  * @propname: Name of the property holding the string value
549  * @array: String array to search in
550  * @n: Size of the @array
551  *
552  * Find a property string value in a given @array and if it is found return
553  * the index back.
554  *
555  * Return: index, starting from %0, if the string value was found in the @array (success),
556  *	   %-ENOENT when the string value was not found in the @array,
557  *	   %-EINVAL if given arguments are not valid,
558  *	   %-ENODATA if the property does not have a value,
559  *	   %-EPROTO or %-EILSEQ if the property is not a string,
560  *	   %-ENXIO if no suitable firmware interface is present.
561  */
562 int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
563 	const char *propname, const char * const *array, size_t n)
564 {
565 	const char *string;
566 	int ret;
567 
568 	ret = fwnode_property_read_string(fwnode, propname, &string);
569 	if (ret)
570 		return ret;
571 
572 	ret = match_string(array, n, string);
573 	if (ret < 0)
574 		ret = -ENOENT;
575 
576 	return ret;
577 }
578 EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);
579 
580 /**
581  * fwnode_property_get_reference_args() - Find a reference with arguments
582  * @fwnode:	Firmware node where to look for the reference
583  * @prop:	The name of the property
584  * @nargs_prop:	The name of the property telling the number of
585  *		arguments in the referred node. NULL if @nargs is known,
586  *		otherwise @nargs is ignored.
587  * @nargs:	Number of arguments. Ignored if @nargs_prop is non-NULL.
588  * @index:	Index of the reference, from zero onwards.
589  * @args:	Result structure with reference and integer arguments.
590  *		May be NULL.
591  *
592  * Obtain a reference based on a named property in an fwnode, with
593  * integer arguments.
594  *
595  * The caller is responsible for calling fwnode_handle_put() on the returned
596  * @args->fwnode pointer.
597  *
598  * Return: %0 on success
599  *	    %-ENOENT when the index is out of bounds, the index has an empty
600  *		     reference or the property was not found
601  *	    %-EINVAL on parse error
602  *	    %-ENOTCONN when the remote firmware node exists but has not been
603  *		       registered yet
604  */
605 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
606 				       const char *prop, const char *nargs_prop,
607 				       unsigned int nargs, unsigned int index,
608 				       struct fwnode_reference_args *args)
609 {
610 	int ret;
611 
612 	if (IS_ERR_OR_NULL(fwnode))
613 		return -ENOENT;
614 
615 	ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
616 				 nargs, index, args);
617 	if (ret == 0)
618 		return ret;
619 
620 	if (IS_ERR_OR_NULL(fwnode->secondary))
621 		return ret;
622 
623 	return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
624 				  nargs, index, args);
625 }
626 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
627 
628 /**
629  * fwnode_find_reference - Find named reference to a fwnode_handle
630  * @fwnode: Firmware node where to look for the reference
631  * @name: The name of the reference
632  * @index: Index of the reference
633  *
634  * @index can be used when the named reference holds a table of references.
635  *
636  * The caller is responsible for calling fwnode_handle_put() on the returned
637  * fwnode pointer.
638  *
639  * Return: a pointer to the reference fwnode, when found. Otherwise,
640  * returns an error pointer.
641  */
642 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
643 					    const char *name,
644 					    unsigned int index)
645 {
646 	struct fwnode_reference_args args;
647 	int ret;
648 
649 	ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
650 						 &args);
651 	return ret ? ERR_PTR(ret) : args.fwnode;
652 }
653 EXPORT_SYMBOL_GPL(fwnode_find_reference);
654 
655 /**
656  * fwnode_get_name - Return the name of a node
657  * @fwnode: The firmware node
658  *
659  * Return: a pointer to the node name, or %NULL.
660  */
661 const char *fwnode_get_name(const struct fwnode_handle *fwnode)
662 {
663 	return fwnode_call_ptr_op(fwnode, get_name);
664 }
665 EXPORT_SYMBOL_GPL(fwnode_get_name);
666 
667 /**
668  * fwnode_get_name_prefix - Return the prefix of node for printing purposes
669  * @fwnode: The firmware node
670  *
671  * Return: the prefix of a node, intended to be printed right before the node.
672  * The prefix works also as a separator between the nodes.
673  */
674 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
675 {
676 	return fwnode_call_ptr_op(fwnode, get_name_prefix);
677 }
678 
679 /**
680  * fwnode_name_eq - Return true if node name is equal
681  * @fwnode: The firmware node
682  * @name: The name to which to compare the node name
683  *
684  * Compare the name provided as an argument to the name of the node, stopping
685  * the comparison at either NUL or '@' character, whichever comes first. This
686  * function is generally used for comparing node names while ignoring the
687  * possible unit address of the node.
688  *
689  * Return: true if the node name matches with the name provided in the @name
690  * argument, false otherwise.
691  */
692 bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)
693 {
694 	const char *node_name;
695 	ptrdiff_t len;
696 
697 	node_name = fwnode_get_name(fwnode);
698 	if (!node_name)
699 		return false;
700 
701 	len = strchrnul(node_name, '@') - node_name;
702 
703 	return str_has_prefix(node_name, name) == len;
704 }
705 EXPORT_SYMBOL_GPL(fwnode_name_eq);
706 
707 /**
708  * fwnode_get_parent - Return parent firwmare node
709  * @fwnode: Firmware whose parent is retrieved
710  *
711  * The caller is responsible for calling fwnode_handle_put() on the returned
712  * fwnode pointer.
713  *
714  * Return: parent firmware node of the given node if possible or %NULL if no
715  * parent was available.
716  */
717 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
718 {
719 	return fwnode_call_ptr_op(fwnode, get_parent);
720 }
721 EXPORT_SYMBOL_GPL(fwnode_get_parent);
722 
723 /**
724  * fwnode_get_next_parent - Iterate to the node's parent
725  * @fwnode: Firmware whose parent is retrieved
726  *
727  * This is like fwnode_get_parent() except that it drops the refcount
728  * on the passed node, making it suitable for iterating through a
729  * node's parents.
730  *
731  * The caller is responsible for calling fwnode_handle_put() on the returned
732  * fwnode pointer. Note that this function also puts a reference to @fwnode
733  * unconditionally.
734  *
735  * Return: parent firmware node of the given node if possible or %NULL if no
736  * parent was available.
737  */
738 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
739 {
740 	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
741 
742 	fwnode_handle_put(fwnode);
743 
744 	return parent;
745 }
746 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
747 
748 /**
749  * fwnode_count_parents - Return the number of parents a node has
750  * @fwnode: The node the parents of which are to be counted
751  *
752  * Return: the number of parents a node has.
753  */
754 unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
755 {
756 	struct fwnode_handle *parent;
757 	unsigned int count = 0;
758 
759 	fwnode_for_each_parent_node(fwnode, parent)
760 		count++;
761 
762 	return count;
763 }
764 EXPORT_SYMBOL_GPL(fwnode_count_parents);
765 
766 /**
767  * fwnode_get_nth_parent - Return an nth parent of a node
768  * @fwnode: The node the parent of which is requested
769  * @depth: Distance of the parent from the node
770  *
771  * The caller is responsible for calling fwnode_handle_put() on the returned
772  * fwnode pointer.
773  *
774  * Return: the nth parent of a node. If there is no parent at the requested
775  * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
776  * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
777  */
778 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
779 					    unsigned int depth)
780 {
781 	struct fwnode_handle *parent;
782 
783 	if (depth == 0)
784 		return fwnode_handle_get(fwnode);
785 
786 	fwnode_for_each_parent_node(fwnode, parent) {
787 		if (--depth == 0)
788 			return parent;
789 	}
790 	return NULL;
791 }
792 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
793 
794 /**
795  * fwnode_get_next_child_node - Return the next child node handle for a node
796  * @fwnode: Firmware node to find the next child node for.
797  * @child: Handle to one of the node's child nodes or a %NULL handle.
798  *
799  * The caller is responsible for calling fwnode_handle_put() on the returned
800  * fwnode pointer. Note that this function also puts a reference to @child
801  * unconditionally.
802  */
803 struct fwnode_handle *
804 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
805 			   struct fwnode_handle *child)
806 {
807 	const struct fwnode_handle *parent;
808 	struct fwnode_handle *child_parent __free(fwnode_handle) = NULL;
809 	struct fwnode_handle *next;
810 
811 	/*
812 	 * If this function is in a loop and the previous iteration returned
813 	 * an child from fwnode->secondary, then we need to use the secondary
814 	 * as parent rather than @fwnode.
815 	 */
816 	if (child) {
817 		child_parent = fwnode_get_parent(child);
818 		parent = child_parent;
819 	} else {
820 		parent = fwnode;
821 	}
822 	if (IS_ERR_OR_NULL(parent))
823 		return NULL;
824 
825 	/* Try to find a child in primary fwnode */
826 	next = fwnode_call_ptr_op(parent, get_next_child_node, child);
827 	if (next)
828 		return next;
829 
830 	/* When no more children in primary, continue with secondary */
831 	return fwnode_get_next_child_node(parent->secondary, NULL);
832 }
833 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
834 
835 /**
836  * fwnode_get_next_available_child_node - Return the next available child node handle for a node
837  * @fwnode: Firmware node to find the next child node for.
838  * @child: Handle to one of the node's child nodes or a %NULL handle.
839  *
840  * The caller is responsible for calling fwnode_handle_put() on the returned
841  * fwnode pointer. Note that this function also puts a reference to @child
842  * unconditionally.
843  */
844 struct fwnode_handle *
845 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
846 				     struct fwnode_handle *child)
847 {
848 	struct fwnode_handle *next_child = child;
849 
850 	if (IS_ERR_OR_NULL(fwnode))
851 		return NULL;
852 
853 	do {
854 		next_child = fwnode_get_next_child_node(fwnode, next_child);
855 		if (!next_child)
856 			return NULL;
857 	} while (!fwnode_device_is_available(next_child));
858 
859 	return next_child;
860 }
861 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
862 
863 /**
864  * device_get_next_child_node - Return the next child node handle for a device
865  * @dev: Device to find the next child node for.
866  * @child: Handle to one of the device's child nodes or a %NULL handle.
867  *
868  * The caller is responsible for calling fwnode_handle_put() on the returned
869  * fwnode pointer. Note that this function also puts a reference to @child
870  * unconditionally.
871  */
872 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
873 						 struct fwnode_handle *child)
874 {
875 	return fwnode_get_next_child_node(dev_fwnode(dev), child);
876 }
877 EXPORT_SYMBOL_GPL(device_get_next_child_node);
878 
879 /**
880  * fwnode_get_named_child_node - Return first matching named child node handle
881  * @fwnode: Firmware node to find the named child node for.
882  * @childname: String to match child node name against.
883  *
884  * The caller is responsible for calling fwnode_handle_put() on the returned
885  * fwnode pointer.
886  */
887 struct fwnode_handle *
888 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
889 			    const char *childname)
890 {
891 	return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
892 }
893 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
894 
895 /**
896  * device_get_named_child_node - Return first matching named child node handle
897  * @dev: Device to find the named child node for.
898  * @childname: String to match child node name against.
899  *
900  * The caller is responsible for calling fwnode_handle_put() on the returned
901  * fwnode pointer.
902  */
903 struct fwnode_handle *device_get_named_child_node(const struct device *dev,
904 						  const char *childname)
905 {
906 	return fwnode_get_named_child_node(dev_fwnode(dev), childname);
907 }
908 EXPORT_SYMBOL_GPL(device_get_named_child_node);
909 
910 /**
911  * fwnode_handle_get - Obtain a reference to a device node
912  * @fwnode: Pointer to the device node to obtain the reference to.
913  *
914  * The caller is responsible for calling fwnode_handle_put() on the returned
915  * fwnode pointer.
916  *
917  * Return: the fwnode handle.
918  */
919 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
920 {
921 	if (!fwnode_has_op(fwnode, get))
922 		return fwnode;
923 
924 	return fwnode_call_ptr_op(fwnode, get);
925 }
926 EXPORT_SYMBOL_GPL(fwnode_handle_get);
927 
928 /**
929  * fwnode_device_is_available - check if a device is available for use
930  * @fwnode: Pointer to the fwnode of the device.
931  *
932  * Return: true if device is available for use. Otherwise, returns false.
933  *
934  * For fwnode node types that don't implement the .device_is_available()
935  * operation, this function returns true.
936  */
937 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
938 {
939 	if (IS_ERR_OR_NULL(fwnode))
940 		return false;
941 
942 	if (!fwnode_has_op(fwnode, device_is_available))
943 		return true;
944 
945 	return fwnode_call_bool_op(fwnode, device_is_available);
946 }
947 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
948 
949 /**
950  * fwnode_get_child_node_count - return the number of child nodes for a given firmware node
951  * @fwnode: Pointer to the parent firmware node
952  *
953  * Return: the number of child nodes for a given firmware node.
954  */
955 unsigned int fwnode_get_child_node_count(const struct fwnode_handle *fwnode)
956 {
957 	struct fwnode_handle *child;
958 	unsigned int count = 0;
959 
960 	fwnode_for_each_child_node(fwnode, child)
961 		count++;
962 
963 	return count;
964 }
965 EXPORT_SYMBOL_GPL(fwnode_get_child_node_count);
966 
967 /**
968  * fwnode_get_named_child_node_count - number of child nodes with given name
969  * @fwnode: Node which child nodes are counted.
970  * @name: String to match child node name against.
971  *
972  * Scan child nodes and count all the nodes with a specific name. Potential
973  * 'number' -ending after the 'at sign' for scanned names is ignored.
974  * E.g.::
975  *   fwnode_get_named_child_node_count(fwnode, "channel");
976  * would match all the nodes::
977  *   channel { }, channel@0 {}, channel@0xabba {}...
978  *
979  * Return: the number of child nodes with a matching name for a given device.
980  */
981 unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,
982 					       const char *name)
983 {
984 	struct fwnode_handle *child;
985 	unsigned int count = 0;
986 
987 	fwnode_for_each_named_child_node(fwnode, child, name)
988 		count++;
989 
990 	return count;
991 }
992 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count);
993 
994 bool device_dma_supported(const struct device *dev)
995 {
996 	return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
997 }
998 EXPORT_SYMBOL_GPL(device_dma_supported);
999 
1000 enum dev_dma_attr device_get_dma_attr(const struct device *dev)
1001 {
1002 	if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))
1003 		return DEV_DMA_NOT_SUPPORTED;
1004 
1005 	return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);
1006 }
1007 EXPORT_SYMBOL_GPL(device_get_dma_attr);
1008 
1009 /**
1010  * fwnode_get_phy_mode - Get phy mode for given firmware node
1011  * @fwnode:	Pointer to the given node
1012  *
1013  * The function gets phy interface string from property 'phy-mode' or
1014  * 'phy-connection-type', and return its index in phy_modes table, or errno in
1015  * error case.
1016  */
1017 int fwnode_get_phy_mode(const struct fwnode_handle *fwnode)
1018 {
1019 	const char *pm;
1020 	int err, i;
1021 
1022 	err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
1023 	if (err < 0)
1024 		err = fwnode_property_read_string(fwnode,
1025 						  "phy-connection-type", &pm);
1026 	if (err < 0)
1027 		return err;
1028 
1029 	for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1030 		if (!strcasecmp(pm, phy_modes(i)))
1031 			return i;
1032 
1033 	return -ENODEV;
1034 }
1035 EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
1036 
1037 /**
1038  * device_get_phy_mode - Get phy mode for given device
1039  * @dev:	Pointer to the given device
1040  *
1041  * The function gets phy interface string from property 'phy-mode' or
1042  * 'phy-connection-type', and return its index in phy_modes table, or errno in
1043  * error case.
1044  */
1045 int device_get_phy_mode(struct device *dev)
1046 {
1047 	return fwnode_get_phy_mode(dev_fwnode(dev));
1048 }
1049 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1050 
1051 /**
1052  * fwnode_iomap - Maps the memory mapped IO for a given fwnode
1053  * @fwnode:	Pointer to the firmware node
1054  * @index:	Index of the IO range
1055  *
1056  * Return: a pointer to the mapped memory.
1057  */
1058 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)
1059 {
1060 	return fwnode_call_ptr_op(fwnode, iomap, index);
1061 }
1062 EXPORT_SYMBOL(fwnode_iomap);
1063 
1064 /**
1065  * fwnode_irq_get - Get IRQ directly from a fwnode
1066  * @fwnode:	Pointer to the firmware node
1067  * @index:	Zero-based index of the IRQ
1068  *
1069  * Return: Linux IRQ number on success. Negative errno on failure.
1070  */
1071 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
1072 {
1073 	int ret;
1074 
1075 	ret = fwnode_call_int_op(fwnode, irq_get, index);
1076 	/* We treat mapping errors as invalid case */
1077 	if (ret == 0)
1078 		return -EINVAL;
1079 
1080 	return ret;
1081 }
1082 EXPORT_SYMBOL(fwnode_irq_get);
1083 
1084 /**
1085  * fwnode_irq_get_byname - Get IRQ from a fwnode using its name
1086  * @fwnode:	Pointer to the firmware node
1087  * @name:	IRQ name
1088  *
1089  * Description:
1090  * Find a match to the string @name in the 'interrupt-names' string array
1091  * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ
1092  * number of the IRQ resource corresponding to the index of the matched
1093  * string.
1094  *
1095  * Return: Linux IRQ number on success, or negative errno otherwise.
1096  */
1097 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
1098 {
1099 	int index;
1100 
1101 	if (!name)
1102 		return -EINVAL;
1103 
1104 	index = fwnode_property_match_string(fwnode, "interrupt-names",  name);
1105 	if (index < 0)
1106 		return index;
1107 
1108 	return fwnode_irq_get(fwnode, index);
1109 }
1110 EXPORT_SYMBOL(fwnode_irq_get_byname);
1111 
1112 /**
1113  * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
1114  * @fwnode: Pointer to the parent firmware node
1115  * @prev: Previous endpoint node or %NULL to get the first
1116  *
1117  * The caller is responsible for calling fwnode_handle_put() on the returned
1118  * fwnode pointer. Note that this function also puts a reference to @prev
1119  * unconditionally.
1120  *
1121  * Return: an endpoint firmware node pointer or %NULL if no more endpoints
1122  * are available.
1123  */
1124 struct fwnode_handle *
1125 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1126 			       struct fwnode_handle *prev)
1127 {
1128 	const struct fwnode_handle *parent;
1129 	struct fwnode_handle *port_parent __free(fwnode_handle) = NULL;
1130 	struct fwnode_handle *ep;
1131 
1132 	/*
1133 	 * If this function is in a loop and the previous iteration returned
1134 	 * an endpoint from fwnode->secondary, then we need to use the secondary
1135 	 * as parent rather than @fwnode.
1136 	 */
1137 	if (prev) {
1138 		port_parent = fwnode_graph_get_port_parent(prev);
1139 		parent = port_parent;
1140 	} else {
1141 		parent = fwnode;
1142 	}
1143 	if (IS_ERR_OR_NULL(parent))
1144 		return NULL;
1145 
1146 	ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
1147 	if (ep)
1148 		return ep;
1149 
1150 	return fwnode_graph_get_next_endpoint(parent->secondary, NULL);
1151 }
1152 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1153 
1154 /**
1155  * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1156  * @endpoint: Endpoint firmware node of the port
1157  *
1158  * The caller is responsible for calling fwnode_handle_put() on the returned
1159  * fwnode pointer.
1160  *
1161  * Return: the firmware node of the device the @endpoint belongs to.
1162  */
1163 struct fwnode_handle *
1164 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1165 {
1166 	struct fwnode_handle *port, *parent;
1167 
1168 	port = fwnode_get_parent(endpoint);
1169 	parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1170 
1171 	fwnode_handle_put(port);
1172 
1173 	return parent;
1174 }
1175 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1176 
1177 /**
1178  * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1179  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1180  *
1181  * Extracts firmware node of a remote device the @fwnode points to.
1182  *
1183  * The caller is responsible for calling fwnode_handle_put() on the returned
1184  * fwnode pointer.
1185  */
1186 struct fwnode_handle *
1187 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1188 {
1189 	struct fwnode_handle *endpoint, *parent;
1190 
1191 	endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1192 	parent = fwnode_graph_get_port_parent(endpoint);
1193 
1194 	fwnode_handle_put(endpoint);
1195 
1196 	return parent;
1197 }
1198 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1199 
1200 /**
1201  * fwnode_graph_get_remote_port - Return fwnode of a remote port
1202  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1203  *
1204  * Extracts firmware node of a remote port the @fwnode points to.
1205  *
1206  * The caller is responsible for calling fwnode_handle_put() on the returned
1207  * fwnode pointer.
1208  */
1209 struct fwnode_handle *
1210 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1211 {
1212 	return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1213 }
1214 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1215 
1216 /**
1217  * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1218  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1219  *
1220  * Extracts firmware node of a remote endpoint the @fwnode points to.
1221  *
1222  * The caller is responsible for calling fwnode_handle_put() on the returned
1223  * fwnode pointer.
1224  */
1225 struct fwnode_handle *
1226 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1227 {
1228 	return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1229 }
1230 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1231 
1232 static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
1233 {
1234 	struct fwnode_handle *dev_node;
1235 	bool available;
1236 
1237 	dev_node = fwnode_graph_get_remote_port_parent(ep);
1238 	available = fwnode_device_is_available(dev_node);
1239 	fwnode_handle_put(dev_node);
1240 
1241 	return available;
1242 }
1243 
1244 /**
1245  * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1246  * @fwnode: parent fwnode_handle containing the graph
1247  * @port: identifier of the port node
1248  * @endpoint: identifier of the endpoint node under the port node
1249  * @flags: fwnode lookup flags
1250  *
1251  * The caller is responsible for calling fwnode_handle_put() on the returned
1252  * fwnode pointer.
1253  *
1254  * Return: the fwnode handle of the local endpoint corresponding the port and
1255  * endpoint IDs or %NULL if not found.
1256  *
1257  * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1258  * has not been found, look for the closest endpoint ID greater than the
1259  * specified one and return the endpoint that corresponds to it, if present.
1260  *
1261  * Does not return endpoints that belong to disabled devices or endpoints that
1262  * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1263  */
1264 struct fwnode_handle *
1265 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1266 				u32 port, u32 endpoint, unsigned long flags)
1267 {
1268 	struct fwnode_handle *ep, *best_ep = NULL;
1269 	unsigned int best_ep_id = 0;
1270 	bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1271 	bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1272 
1273 	fwnode_graph_for_each_endpoint(fwnode, ep) {
1274 		struct fwnode_endpoint fwnode_ep = { 0 };
1275 		int ret;
1276 
1277 		if (enabled_only && !fwnode_graph_remote_available(ep))
1278 			continue;
1279 
1280 		ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1281 		if (ret < 0)
1282 			continue;
1283 
1284 		if (fwnode_ep.port != port)
1285 			continue;
1286 
1287 		if (fwnode_ep.id == endpoint) {
1288 			fwnode_handle_put(best_ep);
1289 			return ep;
1290 		}
1291 
1292 		if (!endpoint_next)
1293 			continue;
1294 
1295 		/*
1296 		 * If the endpoint that has just been found is not the first
1297 		 * matching one and the ID of the one found previously is closer
1298 		 * to the requested endpoint ID, skip it.
1299 		 */
1300 		if (fwnode_ep.id < endpoint ||
1301 		    (best_ep && best_ep_id < fwnode_ep.id))
1302 			continue;
1303 
1304 		fwnode_handle_put(best_ep);
1305 		best_ep = fwnode_handle_get(ep);
1306 		best_ep_id = fwnode_ep.id;
1307 	}
1308 
1309 	return best_ep;
1310 }
1311 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1312 
1313 /**
1314  * fwnode_graph_get_endpoint_count - Count endpoints on a device node
1315  * @fwnode: The node related to a device
1316  * @flags: fwnode lookup flags
1317  * Count endpoints in a device node.
1318  *
1319  * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
1320  * and endpoints connected to disabled devices are counted.
1321  */
1322 unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
1323 					     unsigned long flags)
1324 {
1325 	struct fwnode_handle *ep;
1326 	unsigned int count = 0;
1327 
1328 	fwnode_graph_for_each_endpoint(fwnode, ep) {
1329 		if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
1330 		    fwnode_graph_remote_available(ep))
1331 			count++;
1332 	}
1333 
1334 	return count;
1335 }
1336 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
1337 
1338 /**
1339  * fwnode_graph_parse_endpoint - parse common endpoint node properties
1340  * @fwnode: pointer to endpoint fwnode_handle
1341  * @endpoint: pointer to the fwnode endpoint data structure
1342  *
1343  * Parse @fwnode representing a graph endpoint node and store the
1344  * information in @endpoint. The caller must hold a reference to
1345  * @fwnode.
1346  */
1347 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1348 				struct fwnode_endpoint *endpoint)
1349 {
1350 	memset(endpoint, 0, sizeof(*endpoint));
1351 
1352 	return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1353 }
1354 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1355 
1356 const void *device_get_match_data(const struct device *dev)
1357 {
1358 	return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1359 }
1360 EXPORT_SYMBOL_GPL(device_get_match_data);
1361 
1362 static unsigned int fwnode_graph_devcon_matches(const struct fwnode_handle *fwnode,
1363 						const char *con_id, void *data,
1364 						devcon_match_fn_t match,
1365 						void **matches,
1366 						unsigned int matches_len)
1367 {
1368 	struct fwnode_handle *node;
1369 	struct fwnode_handle *ep;
1370 	unsigned int count = 0;
1371 	void *ret;
1372 
1373 	fwnode_graph_for_each_endpoint(fwnode, ep) {
1374 		if (matches && count >= matches_len) {
1375 			fwnode_handle_put(ep);
1376 			break;
1377 		}
1378 
1379 		node = fwnode_graph_get_remote_port_parent(ep);
1380 		if (!fwnode_device_is_available(node)) {
1381 			fwnode_handle_put(node);
1382 			continue;
1383 		}
1384 
1385 		ret = match(node, con_id, data);
1386 		fwnode_handle_put(node);
1387 		if (ret) {
1388 			if (matches)
1389 				matches[count] = ret;
1390 			count++;
1391 		}
1392 	}
1393 	return count;
1394 }
1395 
1396 static unsigned int fwnode_devcon_matches(const struct fwnode_handle *fwnode,
1397 					  const char *con_id, void *data,
1398 					  devcon_match_fn_t match,
1399 					  void **matches,
1400 					  unsigned int matches_len)
1401 {
1402 	struct fwnode_handle *node;
1403 	unsigned int count = 0;
1404 	unsigned int i;
1405 	void *ret;
1406 
1407 	for (i = 0; ; i++) {
1408 		if (matches && count >= matches_len)
1409 			break;
1410 
1411 		node = fwnode_find_reference(fwnode, con_id, i);
1412 		if (IS_ERR(node))
1413 			break;
1414 
1415 		ret = match(node, NULL, data);
1416 		fwnode_handle_put(node);
1417 		if (ret) {
1418 			if (matches)
1419 				matches[count] = ret;
1420 			count++;
1421 		}
1422 	}
1423 
1424 	return count;
1425 }
1426 
1427 /**
1428  * fwnode_connection_find_match - Find connection from a device node
1429  * @fwnode: Device node with the connection
1430  * @con_id: Identifier for the connection
1431  * @data: Data for the match function
1432  * @match: Function to check and convert the connection description
1433  *
1434  * Find a connection with unique identifier @con_id between @fwnode and another
1435  * device node. @match will be used to convert the connection description to
1436  * data the caller is expecting to be returned.
1437  */
1438 void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
1439 				   const char *con_id, void *data,
1440 				   devcon_match_fn_t match)
1441 {
1442 	unsigned int count;
1443 	void *ret;
1444 
1445 	if (!fwnode || !match)
1446 		return NULL;
1447 
1448 	count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1449 	if (count)
1450 		return ret;
1451 
1452 	count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1453 	return count ? ret : NULL;
1454 }
1455 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
1456 
1457 /**
1458  * fwnode_connection_find_matches - Find connections from a device node
1459  * @fwnode: Device node with the connection
1460  * @con_id: Identifier for the connection
1461  * @data: Data for the match function
1462  * @match: Function to check and convert the connection description
1463  * @matches: (Optional) array of pointers to fill with matches
1464  * @matches_len: Length of @matches
1465  *
1466  * Find up to @matches_len connections with unique identifier @con_id between
1467  * @fwnode and other device nodes. @match will be used to convert the
1468  * connection description to data the caller is expecting to be returned
1469  * through the @matches array.
1470  *
1471  * If @matches is %NULL @matches_len is ignored and the total number of resolved
1472  * matches is returned.
1473  *
1474  * Return: Number of matches resolved, or negative errno.
1475  */
1476 int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
1477 				   const char *con_id, void *data,
1478 				   devcon_match_fn_t match,
1479 				   void **matches, unsigned int matches_len)
1480 {
1481 	unsigned int count_graph;
1482 	unsigned int count_ref;
1483 
1484 	if (!fwnode || !match)
1485 		return -EINVAL;
1486 
1487 	count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,
1488 						  matches, matches_len);
1489 
1490 	if (matches) {
1491 		matches += count_graph;
1492 		matches_len -= count_graph;
1493 	}
1494 
1495 	count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,
1496 					  matches, matches_len);
1497 
1498 	return count_graph + count_ref;
1499 }
1500 EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);
1501