1 // SPDX-License-Identifier: GPL-2.0
3 * property.c - Unified device property interface.
5 * Copyright (C) 2014, Intel Corporation
6 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/kconfig.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>
21 struct fwnode_handle *__dev_fwnode(struct device *dev)
23 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
24 of_fwnode_handle(dev->of_node) : dev->fwnode;
26 EXPORT_SYMBOL_GPL(__dev_fwnode);
28 const struct fwnode_handle *__dev_fwnode_const(const struct device *dev)
30 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
31 of_fwnode_handle(dev->of_node) : dev->fwnode;
33 EXPORT_SYMBOL_GPL(__dev_fwnode_const);
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
40 * Check if property @propname is present in the device firmware description.
42 * Return: true if property @propname is present. Otherwise, returns false.
44 bool device_property_present(const struct device *dev, const char *propname)
46 return fwnode_property_present(dev_fwnode(dev), propname);
48 EXPORT_SYMBOL_GPL(device_property_present);
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
55 * Return: true if property @propname is present. Otherwise, returns false.
57 bool fwnode_property_present(const struct fwnode_handle *fwnode,
62 if (IS_ERR_OR_NULL(fwnode))
65 ret = fwnode_call_bool_op(fwnode, property_present, propname);
69 return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
71 EXPORT_SYMBOL_GPL(fwnode_property_present);
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
78 * Return if property @propname is true or false in the device firmware description.
80 * Return: true if property @propname is present. Otherwise, returns false.
82 bool device_property_read_bool(const struct device *dev, const char *propname)
84 return fwnode_property_read_bool(dev_fwnode(dev), propname);
86 EXPORT_SYMBOL_GPL(device_property_read_bool);
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
93 * Return if property @propname is true or false in the firmware description.
95 bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
100 if (IS_ERR_OR_NULL(fwnode))
103 ret = fwnode_call_bool_op(fwnode, property_read_bool, propname);
107 return fwnode_call_bool_op(fwnode->secondary, property_read_bool, propname);
109 EXPORT_SYMBOL_GPL(fwnode_property_read_bool);
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
118 * Function reads an array of u8 properties with @propname from the device
119 * firmware description and stores them to @val if found.
121 * It's recommended to call device_property_count_u8() instead of calling
122 * this function with @val equals %NULL and @nval equals 0.
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.
132 int device_property_read_u8_array(const struct device *dev, const char *propname,
133 u8 *val, size_t nval)
135 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
137 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
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
146 * Function reads an array of u16 properties with @propname from the device
147 * firmware description and stores them to @val if found.
149 * It's recommended to call device_property_count_u16() instead of calling
150 * this function with @val equals %NULL and @nval equals 0.
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.
160 int device_property_read_u16_array(const struct device *dev, const char *propname,
161 u16 *val, size_t nval)
163 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
165 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
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
174 * Function reads an array of u32 properties with @propname from the device
175 * firmware description and stores them to @val if found.
177 * It's recommended to call device_property_count_u32() instead of calling
178 * this function with @val equals %NULL and @nval equals 0.
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.
188 int device_property_read_u32_array(const struct device *dev, const char *propname,
189 u32 *val, size_t nval)
191 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
193 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
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
202 * Function reads an array of u64 properties with @propname from the device
203 * firmware description and stores them to @val if found.
205 * It's recommended to call device_property_count_u64() instead of calling
206 * this function with @val equals %NULL and @nval equals 0.
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.
216 int device_property_read_u64_array(const struct device *dev, const char *propname,
217 u64 *val, size_t nval)
219 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
221 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
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
230 * Function reads an array of string properties with @propname from the device
231 * firmware description and stores them to @val if found.
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.
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.
244 int device_property_read_string_array(const struct device *dev, const char *propname,
245 const char **val, size_t nval)
247 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
249 EXPORT_SYMBOL_GPL(device_property_read_string_array);
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
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.
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.
266 int device_property_read_string(const struct device *dev, const char *propname,
269 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
271 EXPORT_SYMBOL_GPL(device_property_read_string);
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
279 * Find a given string in a string array and if it is found return the
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.
288 int device_property_match_string(const struct device *dev, const char *propname,
291 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
293 EXPORT_SYMBOL_GPL(device_property_match_string);
295 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
296 const char *propname,
297 unsigned int elem_size, void *val,
302 if (IS_ERR_OR_NULL(fwnode))
305 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
306 elem_size, val, nval);
310 return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
311 elem_size, val, nval);
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
321 * Read an array of u8 properties with @propname from @fwnode and stores them to
324 * It's recommended to call fwnode_property_count_u8() instead of calling
325 * this function with @val equals %NULL and @nval equals 0.
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.
335 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
336 const char *propname, u8 *val, size_t nval)
338 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
341 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
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
350 * Read an array of u16 properties with @propname from @fwnode and store them to
353 * It's recommended to call fwnode_property_count_u16() instead of calling
354 * this function with @val equals %NULL and @nval equals 0.
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.
364 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
365 const char *propname, u16 *val, size_t nval)
367 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
370 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
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
379 * Read an array of u32 properties with @propname from @fwnode store them to
382 * It's recommended to call fwnode_property_count_u32() instead of calling
383 * this function with @val equals %NULL and @nval equals 0.
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.
393 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
394 const char *propname, u32 *val, size_t nval)
396 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
399 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
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
408 * Read an array of u64 properties with @propname from @fwnode and store them to
411 * It's recommended to call fwnode_property_count_u64() instead of calling
412 * this function with @val equals %NULL and @nval equals 0.
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.
422 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
423 const char *propname, u64 *val, size_t nval)
425 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
428 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
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
437 * Read an string list property @propname from the given firmware node and store
438 * them to @val if found.
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.
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.
451 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
452 const char *propname, const char **val,
457 if (IS_ERR_OR_NULL(fwnode))
460 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
465 return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
468 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
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
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.
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.
485 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
486 const char *propname, const char **val)
488 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
490 return ret < 0 ? ret : 0;
492 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
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
500 * Find a given string in a string array and if it is found return the
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.
509 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
510 const char *propname, const char *string)
515 nval = fwnode_property_string_array_count(fwnode, propname);
522 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
526 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
530 ret = match_string(values, nval, string);
538 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
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
547 * Find a property string value in a given @array and if it is found return
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.
557 int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
558 const char *propname, const char * const *array, size_t n)
563 ret = fwnode_property_read_string(fwnode, propname, &string);
567 ret = match_string(array, n, string);
573 EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);
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.
587 * Obtain a reference based on a named property in an fwnode, with
590 * The caller is responsible for calling fwnode_handle_put() on the returned
591 * @args->fwnode pointer.
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
598 int 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)
605 if (IS_ERR_OR_NULL(fwnode))
608 ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
613 if (IS_ERR_OR_NULL(fwnode->secondary))
616 return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
619 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
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
627 * @index can be used when the named reference holds a table of references.
629 * The caller is responsible for calling fwnode_handle_put() on the returned
632 * Return: a pointer to the reference fwnode, when found. Otherwise,
633 * returns an error pointer.
635 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
639 struct fwnode_reference_args args;
642 ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
644 return ret ? ERR_PTR(ret) : args.fwnode;
646 EXPORT_SYMBOL_GPL(fwnode_find_reference);
649 * fwnode_get_name - Return the name of a node
650 * @fwnode: The firmware node
652 * Return: a pointer to the node name, or %NULL.
654 const char *fwnode_get_name(const struct fwnode_handle *fwnode)
656 return fwnode_call_ptr_op(fwnode, get_name);
658 EXPORT_SYMBOL_GPL(fwnode_get_name);
661 * fwnode_get_name_prefix - Return the prefix of node for printing purposes
662 * @fwnode: The firmware node
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.
667 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
669 return fwnode_call_ptr_op(fwnode, get_name_prefix);
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
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.
682 * Return: true if the node name matches with the name provided in the @name
683 * argument, false otherwise.
685 bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)
687 const char *node_name;
690 node_name = fwnode_get_name(fwnode);
694 len = strchrnul(node_name, '@') - node_name;
696 return str_has_prefix(node_name, name) == len;
698 EXPORT_SYMBOL_GPL(fwnode_name_eq);
701 * fwnode_get_parent - Return parent firwmare node
702 * @fwnode: Firmware whose parent is retrieved
704 * The caller is responsible for calling fwnode_handle_put() on the returned
707 * Return: parent firmware node of the given node if possible or %NULL if no
708 * parent was available.
710 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
712 return fwnode_call_ptr_op(fwnode, get_parent);
714 EXPORT_SYMBOL_GPL(fwnode_get_parent);
717 * fwnode_get_next_parent - Iterate to the node's parent
718 * @fwnode: Firmware whose parent is retrieved
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
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
728 * Return: parent firmware node of the given node if possible or %NULL if no
729 * parent was available.
731 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
733 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
735 fwnode_handle_put(fwnode);
739 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
742 * fwnode_count_parents - Return the number of parents a node has
743 * @fwnode: The node the parents of which are to be counted
745 * Return: the number of parents a node has.
747 unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
749 struct fwnode_handle *parent;
750 unsigned int count = 0;
752 fwnode_for_each_parent_node(fwnode, parent)
757 EXPORT_SYMBOL_GPL(fwnode_count_parents);
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
764 * The caller is responsible for calling fwnode_handle_put() on the returned
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.
771 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
774 struct fwnode_handle *parent;
777 return fwnode_handle_get(fwnode);
779 fwnode_for_each_parent_node(fwnode, parent) {
785 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
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.
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
796 struct fwnode_handle *
797 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
798 struct fwnode_handle *child)
800 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
802 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
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.
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
813 struct fwnode_handle *
814 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
815 struct fwnode_handle *child)
817 struct fwnode_handle *next_child = child;
819 if (IS_ERR_OR_NULL(fwnode))
823 next_child = fwnode_get_next_child_node(fwnode, next_child);
826 } while (!fwnode_device_is_available(next_child));
830 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
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.
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
841 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
842 struct fwnode_handle *child)
844 const struct fwnode_handle *fwnode = dev_fwnode(dev);
845 struct fwnode_handle *next;
847 if (IS_ERR_OR_NULL(fwnode))
850 /* Try to find a child in primary fwnode */
851 next = fwnode_get_next_child_node(fwnode, child);
855 /* When no more children in primary, continue with secondary */
856 return fwnode_get_next_child_node(fwnode->secondary, child);
858 EXPORT_SYMBOL_GPL(device_get_next_child_node);
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.
865 * The caller is responsible for calling fwnode_handle_put() on the returned
868 struct fwnode_handle *
869 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
870 const char *childname)
872 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
874 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
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.
881 * The caller is responsible for calling fwnode_handle_put() on the returned
884 struct fwnode_handle *device_get_named_child_node(const struct device *dev,
885 const char *childname)
887 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
889 EXPORT_SYMBOL_GPL(device_get_named_child_node);
892 * fwnode_handle_get - Obtain a reference to a device node
893 * @fwnode: Pointer to the device node to obtain the reference to.
895 * The caller is responsible for calling fwnode_handle_put() on the returned
898 * Return: the fwnode handle.
900 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
902 if (!fwnode_has_op(fwnode, get))
905 return fwnode_call_ptr_op(fwnode, get);
907 EXPORT_SYMBOL_GPL(fwnode_handle_get);
910 * fwnode_device_is_available - check if a device is available for use
911 * @fwnode: Pointer to the fwnode of the device.
913 * Return: true if device is available for use. Otherwise, returns false.
915 * For fwnode node types that don't implement the .device_is_available()
916 * operation, this function returns true.
918 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
920 if (IS_ERR_OR_NULL(fwnode))
923 if (!fwnode_has_op(fwnode, device_is_available))
926 return fwnode_call_bool_op(fwnode, device_is_available);
928 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
931 * fwnode_get_child_node_count - return the number of child nodes for a given firmware node
932 * @fwnode: Pointer to the parent firmware node
934 * Return: the number of child nodes for a given firmware node.
936 unsigned int fwnode_get_child_node_count(const struct fwnode_handle *fwnode)
938 struct fwnode_handle *child;
939 unsigned int count = 0;
941 fwnode_for_each_child_node(fwnode, child)
946 EXPORT_SYMBOL_GPL(fwnode_get_child_node_count);
949 * fwnode_get_named_child_node_count - number of child nodes with given name
950 * @fwnode: Node which child nodes are counted.
951 * @name: String to match child node name against.
953 * Scan child nodes and count all the nodes with a specific name. Potential
954 * 'number' -ending after the 'at sign' for scanned names is ignored.
956 * fwnode_get_named_child_node_count(fwnode, "channel");
957 * would match all the nodes::
958 * channel { }, channel@0 {}, channel@0xabba {}...
960 * Return: the number of child nodes with a matching name for a given device.
962 unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,
965 struct fwnode_handle *child;
966 unsigned int count = 0;
968 fwnode_for_each_named_child_node(fwnode, child, name)
973 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count);
975 bool device_dma_supported(const struct device *dev)
977 return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
979 EXPORT_SYMBOL_GPL(device_dma_supported);
981 enum dev_dma_attr device_get_dma_attr(const struct device *dev)
983 if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))
984 return DEV_DMA_NOT_SUPPORTED;
986 return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);
988 EXPORT_SYMBOL_GPL(device_get_dma_attr);
991 * fwnode_get_phy_mode - Get phy mode for given firmware node
992 * @fwnode: Pointer to the given node
994 * The function gets phy interface string from property 'phy-mode' or
995 * 'phy-connection-type', and return its index in phy_modes table, or errno in
998 int fwnode_get_phy_mode(const struct fwnode_handle *fwnode)
1003 err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
1005 err = fwnode_property_read_string(fwnode,
1006 "phy-connection-type", &pm);
1010 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1011 if (!strcasecmp(pm, phy_modes(i)))
1016 EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
1019 * device_get_phy_mode - Get phy mode for given device
1020 * @dev: Pointer to the given device
1022 * The function gets phy interface string from property 'phy-mode' or
1023 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1026 int device_get_phy_mode(struct device *dev)
1028 return fwnode_get_phy_mode(dev_fwnode(dev));
1030 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1033 * fwnode_iomap - Maps the memory mapped IO for a given fwnode
1034 * @fwnode: Pointer to the firmware node
1035 * @index: Index of the IO range
1037 * Return: a pointer to the mapped memory.
1039 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)
1041 return fwnode_call_ptr_op(fwnode, iomap, index);
1043 EXPORT_SYMBOL(fwnode_iomap);
1046 * fwnode_irq_get - Get IRQ directly from a fwnode
1047 * @fwnode: Pointer to the firmware node
1048 * @index: Zero-based index of the IRQ
1050 * Return: Linux IRQ number on success. Negative errno on failure.
1052 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
1056 ret = fwnode_call_int_op(fwnode, irq_get, index);
1057 /* We treat mapping errors as invalid case */
1063 EXPORT_SYMBOL(fwnode_irq_get);
1066 * fwnode_irq_get_byname - Get IRQ from a fwnode using its name
1067 * @fwnode: Pointer to the firmware node
1071 * Find a match to the string @name in the 'interrupt-names' string array
1072 * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ
1073 * number of the IRQ resource corresponding to the index of the matched
1076 * Return: Linux IRQ number on success, or negative errno otherwise.
1078 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
1085 index = fwnode_property_match_string(fwnode, "interrupt-names", name);
1089 return fwnode_irq_get(fwnode, index);
1091 EXPORT_SYMBOL(fwnode_irq_get_byname);
1094 * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
1095 * @fwnode: Pointer to the parent firmware node
1096 * @prev: Previous endpoint node or %NULL to get the first
1098 * The caller is responsible for calling fwnode_handle_put() on the returned
1099 * fwnode pointer. Note that this function also puts a reference to @prev
1102 * Return: an endpoint firmware node pointer or %NULL if no more endpoints
1105 struct fwnode_handle *
1106 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1107 struct fwnode_handle *prev)
1109 struct fwnode_handle *ep, *port_parent = NULL;
1110 const struct fwnode_handle *parent;
1113 * If this function is in a loop and the previous iteration returned
1114 * an endpoint from fwnode->secondary, then we need to use the secondary
1115 * as parent rather than @fwnode.
1118 port_parent = fwnode_graph_get_port_parent(prev);
1119 parent = port_parent;
1123 if (IS_ERR_OR_NULL(parent))
1126 ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
1128 goto out_put_port_parent;
1130 ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);
1132 out_put_port_parent:
1133 fwnode_handle_put(port_parent);
1136 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1139 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1140 * @endpoint: Endpoint firmware node of the port
1142 * The caller is responsible for calling fwnode_handle_put() on the returned
1145 * Return: the firmware node of the device the @endpoint belongs to.
1147 struct fwnode_handle *
1148 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1150 struct fwnode_handle *port, *parent;
1152 port = fwnode_get_parent(endpoint);
1153 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1155 fwnode_handle_put(port);
1159 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1162 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1163 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1165 * Extracts firmware node of a remote device the @fwnode points to.
1167 * The caller is responsible for calling fwnode_handle_put() on the returned
1170 struct fwnode_handle *
1171 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1173 struct fwnode_handle *endpoint, *parent;
1175 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1176 parent = fwnode_graph_get_port_parent(endpoint);
1178 fwnode_handle_put(endpoint);
1182 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1185 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1186 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1188 * Extracts firmware node of a remote port the @fwnode points to.
1190 * The caller is responsible for calling fwnode_handle_put() on the returned
1193 struct fwnode_handle *
1194 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1196 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1198 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1201 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1202 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1204 * Extracts firmware node of a remote endpoint the @fwnode points to.
1206 * The caller is responsible for calling fwnode_handle_put() on the returned
1209 struct fwnode_handle *
1210 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1212 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1214 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1216 static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
1218 struct fwnode_handle *dev_node;
1221 dev_node = fwnode_graph_get_remote_port_parent(ep);
1222 available = fwnode_device_is_available(dev_node);
1223 fwnode_handle_put(dev_node);
1229 * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1230 * @fwnode: parent fwnode_handle containing the graph
1231 * @port: identifier of the port node
1232 * @endpoint: identifier of the endpoint node under the port node
1233 * @flags: fwnode lookup flags
1235 * The caller is responsible for calling fwnode_handle_put() on the returned
1238 * Return: the fwnode handle of the local endpoint corresponding the port and
1239 * endpoint IDs or %NULL if not found.
1241 * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1242 * has not been found, look for the closest endpoint ID greater than the
1243 * specified one and return the endpoint that corresponds to it, if present.
1245 * Does not return endpoints that belong to disabled devices or endpoints that
1246 * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1248 struct fwnode_handle *
1249 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1250 u32 port, u32 endpoint, unsigned long flags)
1252 struct fwnode_handle *ep, *best_ep = NULL;
1253 unsigned int best_ep_id = 0;
1254 bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1255 bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1257 fwnode_graph_for_each_endpoint(fwnode, ep) {
1258 struct fwnode_endpoint fwnode_ep = { 0 };
1261 if (enabled_only && !fwnode_graph_remote_available(ep))
1264 ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1268 if (fwnode_ep.port != port)
1271 if (fwnode_ep.id == endpoint)
1278 * If the endpoint that has just been found is not the first
1279 * matching one and the ID of the one found previously is closer
1280 * to the requested endpoint ID, skip it.
1282 if (fwnode_ep.id < endpoint ||
1283 (best_ep && best_ep_id < fwnode_ep.id))
1286 fwnode_handle_put(best_ep);
1287 best_ep = fwnode_handle_get(ep);
1288 best_ep_id = fwnode_ep.id;
1293 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1296 * fwnode_graph_get_endpoint_count - Count endpoints on a device node
1297 * @fwnode: The node related to a device
1298 * @flags: fwnode lookup flags
1299 * Count endpoints in a device node.
1301 * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
1302 * and endpoints connected to disabled devices are counted.
1304 unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
1305 unsigned long flags)
1307 struct fwnode_handle *ep;
1308 unsigned int count = 0;
1310 fwnode_graph_for_each_endpoint(fwnode, ep) {
1311 if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
1312 fwnode_graph_remote_available(ep))
1318 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
1321 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1322 * @fwnode: pointer to endpoint fwnode_handle
1323 * @endpoint: pointer to the fwnode endpoint data structure
1325 * Parse @fwnode representing a graph endpoint node and store the
1326 * information in @endpoint. The caller must hold a reference to
1329 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1330 struct fwnode_endpoint *endpoint)
1332 memset(endpoint, 0, sizeof(*endpoint));
1334 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1336 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1338 const void *device_get_match_data(const struct device *dev)
1340 return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1342 EXPORT_SYMBOL_GPL(device_get_match_data);
1344 static unsigned int fwnode_graph_devcon_matches(const struct fwnode_handle *fwnode,
1345 const char *con_id, void *data,
1346 devcon_match_fn_t match,
1348 unsigned int matches_len)
1350 struct fwnode_handle *node;
1351 struct fwnode_handle *ep;
1352 unsigned int count = 0;
1355 fwnode_graph_for_each_endpoint(fwnode, ep) {
1356 if (matches && count >= matches_len) {
1357 fwnode_handle_put(ep);
1361 node = fwnode_graph_get_remote_port_parent(ep);
1362 if (!fwnode_device_is_available(node)) {
1363 fwnode_handle_put(node);
1367 ret = match(node, con_id, data);
1368 fwnode_handle_put(node);
1371 matches[count] = ret;
1378 static unsigned int fwnode_devcon_matches(const struct fwnode_handle *fwnode,
1379 const char *con_id, void *data,
1380 devcon_match_fn_t match,
1382 unsigned int matches_len)
1384 struct fwnode_handle *node;
1385 unsigned int count = 0;
1389 for (i = 0; ; i++) {
1390 if (matches && count >= matches_len)
1393 node = fwnode_find_reference(fwnode, con_id, i);
1397 ret = match(node, NULL, data);
1398 fwnode_handle_put(node);
1401 matches[count] = ret;
1410 * fwnode_connection_find_match - Find connection from a device node
1411 * @fwnode: Device node with the connection
1412 * @con_id: Identifier for the connection
1413 * @data: Data for the match function
1414 * @match: Function to check and convert the connection description
1416 * Find a connection with unique identifier @con_id between @fwnode and another
1417 * device node. @match will be used to convert the connection description to
1418 * data the caller is expecting to be returned.
1420 void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
1421 const char *con_id, void *data,
1422 devcon_match_fn_t match)
1427 if (!fwnode || !match)
1430 count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1434 count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1435 return count ? ret : NULL;
1437 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
1440 * fwnode_connection_find_matches - Find connections from a device node
1441 * @fwnode: Device node with the connection
1442 * @con_id: Identifier for the connection
1443 * @data: Data for the match function
1444 * @match: Function to check and convert the connection description
1445 * @matches: (Optional) array of pointers to fill with matches
1446 * @matches_len: Length of @matches
1448 * Find up to @matches_len connections with unique identifier @con_id between
1449 * @fwnode and other device nodes. @match will be used to convert the
1450 * connection description to data the caller is expecting to be returned
1451 * through the @matches array.
1453 * If @matches is %NULL @matches_len is ignored and the total number of resolved
1454 * matches is returned.
1456 * Return: Number of matches resolved, or negative errno.
1458 int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
1459 const char *con_id, void *data,
1460 devcon_match_fn_t match,
1461 void **matches, unsigned int matches_len)
1463 unsigned int count_graph;
1464 unsigned int count_ref;
1466 if (!fwnode || !match)
1469 count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,
1470 matches, matches_len);
1473 matches += count_graph;
1474 matches_len -= count_graph;
1477 count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,
1478 matches, matches_len);
1480 return count_graph + count_ref;
1482 EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);