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