1 // SPDX-License-Identifier: GPL-2.0
3 * property.c - Unified device property interface.
5 * Copyright (C) 2014, Intel Corporation
10 #include <linux/acpi.h>
11 #include <linux/export.h>
12 #include <linux/kernel.h>
14 #include <linux/of_address.h>
15 #include <linux/of_graph.h>
16 #include <linux/of_irq.h>
17 #include <linux/property.h>
18 #include <linux/phy.h>
20 struct fwnode_handle *dev_fwnode(struct device *dev)
22 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
23 of_fwnode_handle(dev->of_node) : dev->fwnode;
25 EXPORT_SYMBOL_GPL(dev_fwnode);
28 * device_property_present - check if a property of a device is present
29 * @dev: Device whose property is being checked
30 * @propname: Name of the property
32 * Check if property @propname is present in the device firmware description.
34 bool device_property_present(struct device *dev, const char *propname)
36 return fwnode_property_present(dev_fwnode(dev), propname);
38 EXPORT_SYMBOL_GPL(device_property_present);
41 * fwnode_property_present - check if a property of a firmware node is present
42 * @fwnode: Firmware node whose property to check
43 * @propname: Name of the property
45 bool fwnode_property_present(const struct fwnode_handle *fwnode,
50 if (IS_ERR_OR_NULL(fwnode))
53 ret = fwnode_call_bool_op(fwnode, property_present, propname);
57 return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
59 EXPORT_SYMBOL_GPL(fwnode_property_present);
62 * device_property_read_u8_array - return a u8 array property of a device
63 * @dev: Device to get the property of
64 * @propname: Name of the property
65 * @val: The values are stored here or %NULL to return the number of values
66 * @nval: Size of the @val array
68 * Function reads an array of u8 properties with @propname from the device
69 * firmware description and stores them to @val if found.
71 * It's recommended to call device_property_count_u8() instead of calling
72 * this function with @val equals %NULL and @nval equals 0.
74 * Return: number of values if @val was %NULL,
75 * %0 if the property was found (success),
76 * %-EINVAL if given arguments are not valid,
77 * %-ENODATA if the property does not have a value,
78 * %-EPROTO if the property is not an array of numbers,
79 * %-EOVERFLOW if the size of the property is not as expected.
80 * %-ENXIO if no suitable firmware interface is present.
82 int device_property_read_u8_array(struct device *dev, const char *propname,
85 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
87 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
90 * device_property_read_u16_array - return a u16 array property of a device
91 * @dev: Device to get the property of
92 * @propname: Name of the property
93 * @val: The values are stored here or %NULL to return the number of values
94 * @nval: Size of the @val array
96 * Function reads an array of u16 properties with @propname from the device
97 * firmware description and stores them to @val if found.
99 * It's recommended to call device_property_count_u16() instead of calling
100 * this function with @val equals %NULL and @nval equals 0.
102 * Return: number of values if @val was %NULL,
103 * %0 if the property was found (success),
104 * %-EINVAL if given arguments are not valid,
105 * %-ENODATA if the property does not have a value,
106 * %-EPROTO if the property is not an array of numbers,
107 * %-EOVERFLOW if the size of the property is not as expected.
108 * %-ENXIO if no suitable firmware interface is present.
110 int device_property_read_u16_array(struct device *dev, const char *propname,
111 u16 *val, size_t nval)
113 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
115 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
118 * device_property_read_u32_array - return a u32 array property of a device
119 * @dev: Device to get the property of
120 * @propname: Name of the property
121 * @val: The values are stored here or %NULL to return the number of values
122 * @nval: Size of the @val array
124 * Function reads an array of u32 properties with @propname from the device
125 * firmware description and stores them to @val if found.
127 * It's recommended to call device_property_count_u32() instead of calling
128 * this function with @val equals %NULL and @nval equals 0.
130 * Return: number of values if @val was %NULL,
131 * %0 if the property was found (success),
132 * %-EINVAL if given arguments are not valid,
133 * %-ENODATA if the property does not have a value,
134 * %-EPROTO if the property is not an array of numbers,
135 * %-EOVERFLOW if the size of the property is not as expected.
136 * %-ENXIO if no suitable firmware interface is present.
138 int device_property_read_u32_array(struct device *dev, const char *propname,
139 u32 *val, size_t nval)
141 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
143 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
146 * device_property_read_u64_array - return a u64 array property of a device
147 * @dev: Device to get the property of
148 * @propname: Name of the property
149 * @val: The values are stored here or %NULL to return the number of values
150 * @nval: Size of the @val array
152 * Function reads an array of u64 properties with @propname from the device
153 * firmware description and stores them to @val if found.
155 * It's recommended to call device_property_count_u64() instead of calling
156 * this function with @val equals %NULL and @nval equals 0.
158 * Return: number of values if @val was %NULL,
159 * %0 if the property was found (success),
160 * %-EINVAL if given arguments are not valid,
161 * %-ENODATA if the property does not have a value,
162 * %-EPROTO if the property is not an array of numbers,
163 * %-EOVERFLOW if the size of the property is not as expected.
164 * %-ENXIO if no suitable firmware interface is present.
166 int device_property_read_u64_array(struct device *dev, const char *propname,
167 u64 *val, size_t nval)
169 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
171 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
174 * device_property_read_string_array - return a string array property of device
175 * @dev: Device to get the property of
176 * @propname: Name of the property
177 * @val: The values are stored here or %NULL to return the number of values
178 * @nval: Size of the @val array
180 * Function reads an array of string properties with @propname from the device
181 * firmware description and stores them to @val if found.
183 * It's recommended to call device_property_string_array_count() instead of calling
184 * this function with @val equals %NULL and @nval equals 0.
186 * Return: number of values read on success if @val is non-NULL,
187 * number of values available on success if @val is NULL,
188 * %-EINVAL if given arguments are not valid,
189 * %-ENODATA if the property does not have a value,
190 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
191 * %-EOVERFLOW if the size of the property is not as expected.
192 * %-ENXIO if no suitable firmware interface is present.
194 int device_property_read_string_array(struct device *dev, const char *propname,
195 const char **val, size_t nval)
197 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
199 EXPORT_SYMBOL_GPL(device_property_read_string_array);
202 * device_property_read_string - return a string property of a device
203 * @dev: Device to get the property of
204 * @propname: Name of the property
205 * @val: The value is stored here
207 * Function reads property @propname from the device firmware description and
208 * stores the value into @val if found. The value is checked to be a string.
210 * Return: %0 if the property was found (success),
211 * %-EINVAL if given arguments are not valid,
212 * %-ENODATA if the property does not have a value,
213 * %-EPROTO or %-EILSEQ if the property type is not a string.
214 * %-ENXIO if no suitable firmware interface is present.
216 int device_property_read_string(struct device *dev, const char *propname,
219 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
221 EXPORT_SYMBOL_GPL(device_property_read_string);
224 * device_property_match_string - find a string in an array and return index
225 * @dev: Device to get the property of
226 * @propname: Name of the property holding the array
227 * @string: String to look for
229 * Find a given string in a string array and if it is found return the
232 * Return: %0 if the property was found (success),
233 * %-EINVAL if given arguments are not valid,
234 * %-ENODATA if the property does not have a value,
235 * %-EPROTO if the property is not an array of strings,
236 * %-ENXIO if no suitable firmware interface is present.
238 int device_property_match_string(struct device *dev, const char *propname,
241 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
243 EXPORT_SYMBOL_GPL(device_property_match_string);
245 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
246 const char *propname,
247 unsigned int elem_size, void *val,
252 if (IS_ERR_OR_NULL(fwnode))
255 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
256 elem_size, val, nval);
260 return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
261 elem_size, val, nval);
265 * fwnode_property_read_u8_array - return a u8 array property of firmware node
266 * @fwnode: Firmware node to get the property of
267 * @propname: Name of the property
268 * @val: The values are stored here or %NULL to return the number of values
269 * @nval: Size of the @val array
271 * Read an array of u8 properties with @propname from @fwnode and stores them to
274 * It's recommended to call fwnode_property_count_u8() instead of calling
275 * this function with @val equals %NULL and @nval equals 0.
277 * Return: number of values if @val was %NULL,
278 * %0 if the property was found (success),
279 * %-EINVAL if given arguments are not valid,
280 * %-ENODATA if the property does not have a value,
281 * %-EPROTO if the property is not an array of numbers,
282 * %-EOVERFLOW if the size of the property is not as expected,
283 * %-ENXIO if no suitable firmware interface is present.
285 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
286 const char *propname, u8 *val, size_t nval)
288 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
291 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
294 * fwnode_property_read_u16_array - return a u16 array property of firmware node
295 * @fwnode: Firmware node to get the property of
296 * @propname: Name of the property
297 * @val: The values are stored here or %NULL to return the number of values
298 * @nval: Size of the @val array
300 * Read an array of u16 properties with @propname from @fwnode and store them to
303 * It's recommended to call fwnode_property_count_u16() instead of calling
304 * this function with @val equals %NULL and @nval equals 0.
306 * Return: number of values if @val was %NULL,
307 * %0 if the property was found (success),
308 * %-EINVAL if given arguments are not valid,
309 * %-ENODATA if the property does not have a value,
310 * %-EPROTO if the property is not an array of numbers,
311 * %-EOVERFLOW if the size of the property is not as expected,
312 * %-ENXIO if no suitable firmware interface is present.
314 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
315 const char *propname, u16 *val, size_t nval)
317 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
320 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
323 * fwnode_property_read_u32_array - return a u32 array property of firmware node
324 * @fwnode: Firmware node to get the property of
325 * @propname: Name of the property
326 * @val: The values are stored here or %NULL to return the number of values
327 * @nval: Size of the @val array
329 * Read an array of u32 properties with @propname from @fwnode store them to
332 * It's recommended to call fwnode_property_count_u32() instead of calling
333 * this function with @val equals %NULL and @nval equals 0.
335 * Return: number of values if @val was %NULL,
336 * %0 if the property was found (success),
337 * %-EINVAL if given arguments are not valid,
338 * %-ENODATA if the property does not have a value,
339 * %-EPROTO if the property is not an array of numbers,
340 * %-EOVERFLOW if the size of the property is not as expected,
341 * %-ENXIO if no suitable firmware interface is present.
343 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
344 const char *propname, u32 *val, size_t nval)
346 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
349 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
352 * fwnode_property_read_u64_array - return a u64 array property firmware node
353 * @fwnode: Firmware node to get the property of
354 * @propname: Name of the property
355 * @val: The values are stored here or %NULL to return the number of values
356 * @nval: Size of the @val array
358 * Read an array of u64 properties with @propname from @fwnode and store them to
361 * It's recommended to call fwnode_property_count_u64() instead of calling
362 * this function with @val equals %NULL and @nval equals 0.
364 * Return: number of values if @val was %NULL,
365 * %0 if the property was found (success),
366 * %-EINVAL if given arguments are not valid,
367 * %-ENODATA if the property does not have a value,
368 * %-EPROTO if the property is not an array of numbers,
369 * %-EOVERFLOW if the size of the property is not as expected,
370 * %-ENXIO if no suitable firmware interface is present.
372 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
373 const char *propname, u64 *val, size_t nval)
375 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
378 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
381 * fwnode_property_read_string_array - return string array property of a node
382 * @fwnode: Firmware node to get the property of
383 * @propname: Name of the property
384 * @val: The values are stored here or %NULL to return the number of values
385 * @nval: Size of the @val array
387 * Read an string list property @propname from the given firmware node and store
388 * them to @val if found.
390 * It's recommended to call fwnode_property_string_array_count() instead of calling
391 * this function with @val equals %NULL and @nval equals 0.
393 * Return: number of values read on success if @val is non-NULL,
394 * number of values available on success if @val is NULL,
395 * %-EINVAL if given arguments are not valid,
396 * %-ENODATA if the property does not have a value,
397 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
398 * %-EOVERFLOW if the size of the property is not as expected,
399 * %-ENXIO if no suitable firmware interface is present.
401 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
402 const char *propname, const char **val,
407 if (IS_ERR_OR_NULL(fwnode))
410 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
415 return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
418 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
421 * fwnode_property_read_string - return a string property of a firmware node
422 * @fwnode: Firmware node to get the property of
423 * @propname: Name of the property
424 * @val: The value is stored here
426 * Read property @propname from the given firmware node and store the value into
427 * @val if found. The value is checked to be a string.
429 * Return: %0 if the property was found (success),
430 * %-EINVAL if given arguments are not valid,
431 * %-ENODATA if the property does not have a value,
432 * %-EPROTO or %-EILSEQ if the property is not a string,
433 * %-ENXIO if no suitable firmware interface is present.
435 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
436 const char *propname, const char **val)
438 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
440 return ret < 0 ? ret : 0;
442 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
445 * fwnode_property_match_string - find a string in an array and return index
446 * @fwnode: Firmware node to get the property of
447 * @propname: Name of the property holding the array
448 * @string: String to look for
450 * Find a given string in a string array and if it is found return the
453 * Return: %0 if the property was found (success),
454 * %-EINVAL if given arguments are not valid,
455 * %-ENODATA if the property does not have a value,
456 * %-EPROTO if the property is not an array of strings,
457 * %-ENXIO if no suitable firmware interface is present.
459 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
460 const char *propname, const char *string)
465 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
472 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
476 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
480 ret = match_string(values, nval, string);
487 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
490 * fwnode_property_get_reference_args() - Find a reference with arguments
491 * @fwnode: Firmware node where to look for the reference
492 * @prop: The name of the property
493 * @nargs_prop: The name of the property telling the number of
494 * arguments in the referred node. NULL if @nargs is known,
495 * otherwise @nargs is ignored. Only relevant on OF.
496 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
497 * @index: Index of the reference, from zero onwards.
498 * @args: Result structure with reference and integer arguments.
500 * Obtain a reference based on a named property in an fwnode, with
503 * Caller is responsible to call fwnode_handle_put() on the returned
504 * args->fwnode pointer.
506 * Returns: %0 on success
507 * %-ENOENT when the index is out of bounds, the index has an empty
508 * reference or the property was not found
509 * %-EINVAL on parse error
511 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
512 const char *prop, const char *nargs_prop,
513 unsigned int nargs, unsigned int index,
514 struct fwnode_reference_args *args)
518 if (IS_ERR_OR_NULL(fwnode))
521 ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
526 if (IS_ERR_OR_NULL(fwnode->secondary))
529 return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
532 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
535 * fwnode_find_reference - Find named reference to a fwnode_handle
536 * @fwnode: Firmware node where to look for the reference
537 * @name: The name of the reference
538 * @index: Index of the reference
540 * @index can be used when the named reference holds a table of references.
542 * Returns pointer to the reference fwnode, or ERR_PTR. Caller is responsible to
543 * call fwnode_handle_put() on the returned fwnode pointer.
545 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
549 struct fwnode_reference_args args;
552 ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
554 return ret ? ERR_PTR(ret) : args.fwnode;
556 EXPORT_SYMBOL_GPL(fwnode_find_reference);
559 * fwnode_get_name - Return the name of a node
560 * @fwnode: The firmware node
562 * Returns a pointer to the node name.
564 const char *fwnode_get_name(const struct fwnode_handle *fwnode)
566 return fwnode_call_ptr_op(fwnode, get_name);
568 EXPORT_SYMBOL_GPL(fwnode_get_name);
571 * fwnode_get_name_prefix - Return the prefix of node for printing purposes
572 * @fwnode: The firmware node
574 * Returns the prefix of a node, intended to be printed right before the node.
575 * The prefix works also as a separator between the nodes.
577 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
579 return fwnode_call_ptr_op(fwnode, get_name_prefix);
583 * fwnode_get_parent - Return parent firwmare node
584 * @fwnode: Firmware whose parent is retrieved
586 * Return parent firmware node of the given node if possible or %NULL if no
587 * parent was available.
589 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
591 return fwnode_call_ptr_op(fwnode, get_parent);
593 EXPORT_SYMBOL_GPL(fwnode_get_parent);
596 * fwnode_get_next_parent - Iterate to the node's parent
597 * @fwnode: Firmware whose parent is retrieved
599 * This is like fwnode_get_parent() except that it drops the refcount
600 * on the passed node, making it suitable for iterating through a
603 * Returns a node pointer with refcount incremented, use
604 * fwnode_handle_node() on it when done.
606 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
608 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
610 fwnode_handle_put(fwnode);
614 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
617 * fwnode_get_next_parent_dev - Find device of closest ancestor fwnode
618 * @fwnode: firmware node
620 * Given a firmware node (@fwnode), this function finds its closest ancestor
621 * firmware node that has a corresponding struct device and returns that struct
624 * The caller of this function is expected to call put_device() on the returned
625 * device when they are done.
627 struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
629 struct fwnode_handle *parent;
632 fwnode_for_each_parent_node(fwnode, parent) {
633 dev = get_dev_from_fwnode(parent);
635 fwnode_handle_put(parent);
643 * fwnode_count_parents - Return the number of parents a node has
644 * @fwnode: The node the parents of which are to be counted
646 * Returns the number of parents a node has.
648 unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
650 struct fwnode_handle *parent;
651 unsigned int count = 0;
653 fwnode_for_each_parent_node(fwnode, parent)
658 EXPORT_SYMBOL_GPL(fwnode_count_parents);
661 * fwnode_get_nth_parent - Return an nth parent of a node
662 * @fwnode: The node the parent of which is requested
663 * @depth: Distance of the parent from the node
665 * Returns the nth parent of a node. If there is no parent at the requested
666 * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
667 * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
669 * The caller is responsible for calling fwnode_handle_put() for the returned
672 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
675 struct fwnode_handle *parent;
678 return fwnode_handle_get(fwnode);
680 fwnode_for_each_parent_node(fwnode, parent) {
686 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
689 * fwnode_is_ancestor_of - Test if @ancestor is ancestor of @child
690 * @ancestor: Firmware which is tested for being an ancestor
691 * @child: Firmware which is tested for being the child
693 * A node is considered an ancestor of itself too.
695 * Returns true if @ancestor is an ancestor of @child. Otherwise, returns false.
697 bool fwnode_is_ancestor_of(struct fwnode_handle *ancestor, struct fwnode_handle *child)
699 struct fwnode_handle *parent;
701 if (IS_ERR_OR_NULL(ancestor))
704 if (child == ancestor)
707 fwnode_for_each_parent_node(child, parent) {
708 if (parent == ancestor) {
709 fwnode_handle_put(parent);
717 * fwnode_get_next_child_node - Return the next child node handle for a node
718 * @fwnode: Firmware node to find the next child node for.
719 * @child: Handle to one of the node's child nodes or a %NULL handle.
721 struct fwnode_handle *
722 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
723 struct fwnode_handle *child)
725 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
727 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
730 * fwnode_get_next_available_child_node - Return the next
731 * available child node handle for a node
732 * @fwnode: Firmware node to find the next child node for.
733 * @child: Handle to one of the node's child nodes or a %NULL handle.
735 struct fwnode_handle *
736 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
737 struct fwnode_handle *child)
739 struct fwnode_handle *next_child = child;
741 if (IS_ERR_OR_NULL(fwnode))
745 next_child = fwnode_get_next_child_node(fwnode, next_child);
748 } while (!fwnode_device_is_available(next_child));
752 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
755 * device_get_next_child_node - Return the next child node handle for a device
756 * @dev: Device to find the next child node for.
757 * @child: Handle to one of the device's child nodes or a null handle.
759 struct fwnode_handle *device_get_next_child_node(struct device *dev,
760 struct fwnode_handle *child)
762 const struct fwnode_handle *fwnode = dev_fwnode(dev);
763 struct fwnode_handle *next;
765 if (IS_ERR_OR_NULL(fwnode))
768 /* Try to find a child in primary fwnode */
769 next = fwnode_get_next_child_node(fwnode, child);
773 /* When no more children in primary, continue with secondary */
774 return fwnode_get_next_child_node(fwnode->secondary, child);
776 EXPORT_SYMBOL_GPL(device_get_next_child_node);
779 * fwnode_get_named_child_node - Return first matching named child node handle
780 * @fwnode: Firmware node to find the named child node for.
781 * @childname: String to match child node name against.
783 struct fwnode_handle *
784 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
785 const char *childname)
787 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
789 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
792 * device_get_named_child_node - Return first matching named child node handle
793 * @dev: Device to find the named child node for.
794 * @childname: String to match child node name against.
796 struct fwnode_handle *device_get_named_child_node(struct device *dev,
797 const char *childname)
799 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
801 EXPORT_SYMBOL_GPL(device_get_named_child_node);
804 * fwnode_handle_get - Obtain a reference to a device node
805 * @fwnode: Pointer to the device node to obtain the reference to.
807 * Returns the fwnode handle.
809 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
811 if (!fwnode_has_op(fwnode, get))
814 return fwnode_call_ptr_op(fwnode, get);
816 EXPORT_SYMBOL_GPL(fwnode_handle_get);
819 * fwnode_handle_put - Drop reference to a device node
820 * @fwnode: Pointer to the device node to drop the reference to.
822 * This has to be used when terminating device_for_each_child_node() iteration
823 * with break or return to prevent stale device node references from being left
826 void fwnode_handle_put(struct fwnode_handle *fwnode)
828 fwnode_call_void_op(fwnode, put);
830 EXPORT_SYMBOL_GPL(fwnode_handle_put);
833 * fwnode_device_is_available - check if a device is available for use
834 * @fwnode: Pointer to the fwnode of the device.
836 * For fwnode node types that don't implement the .device_is_available()
837 * operation, this function returns true.
839 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
841 if (IS_ERR_OR_NULL(fwnode))
844 if (!fwnode_has_op(fwnode, device_is_available))
847 return fwnode_call_bool_op(fwnode, device_is_available);
849 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
852 * device_get_child_node_count - return the number of child nodes for device
853 * @dev: Device to cound the child nodes for
855 unsigned int device_get_child_node_count(struct device *dev)
857 struct fwnode_handle *child;
858 unsigned int count = 0;
860 device_for_each_child_node(dev, child)
865 EXPORT_SYMBOL_GPL(device_get_child_node_count);
867 bool device_dma_supported(struct device *dev)
869 return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
871 EXPORT_SYMBOL_GPL(device_dma_supported);
873 enum dev_dma_attr device_get_dma_attr(struct device *dev)
875 if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))
876 return DEV_DMA_NOT_SUPPORTED;
878 return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);
880 EXPORT_SYMBOL_GPL(device_get_dma_attr);
883 * fwnode_get_phy_mode - Get phy mode for given firmware node
884 * @fwnode: Pointer to the given node
886 * The function gets phy interface string from property 'phy-mode' or
887 * 'phy-connection-type', and return its index in phy_modes table, or errno in
890 int fwnode_get_phy_mode(struct fwnode_handle *fwnode)
895 err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
897 err = fwnode_property_read_string(fwnode,
898 "phy-connection-type", &pm);
902 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
903 if (!strcasecmp(pm, phy_modes(i)))
908 EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
911 * device_get_phy_mode - Get phy mode for given device
912 * @dev: Pointer to the given device
914 * The function gets phy interface string from property 'phy-mode' or
915 * 'phy-connection-type', and return its index in phy_modes table, or errno in
918 int device_get_phy_mode(struct device *dev)
920 return fwnode_get_phy_mode(dev_fwnode(dev));
922 EXPORT_SYMBOL_GPL(device_get_phy_mode);
925 * fwnode_iomap - Maps the memory mapped IO for a given fwnode
926 * @fwnode: Pointer to the firmware node
927 * @index: Index of the IO range
929 * Returns a pointer to the mapped memory.
931 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)
933 return fwnode_call_ptr_op(fwnode, iomap, index);
935 EXPORT_SYMBOL(fwnode_iomap);
938 * fwnode_irq_get - Get IRQ directly from a fwnode
939 * @fwnode: Pointer to the firmware node
940 * @index: Zero-based index of the IRQ
942 * Returns Linux IRQ number on success. Other values are determined
943 * accordingly to acpi_/of_ irq_get() operation.
945 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
947 return fwnode_call_int_op(fwnode, irq_get, index);
949 EXPORT_SYMBOL(fwnode_irq_get);
952 * fwnode_irq_get_byname - Get IRQ from a fwnode using its name
953 * @fwnode: Pointer to the firmware node
957 * Find a match to the string @name in the 'interrupt-names' string array
958 * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ
959 * number of the IRQ resource corresponding to the index of the matched
963 * Linux IRQ number on success, or negative errno otherwise.
965 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
972 index = fwnode_property_match_string(fwnode, "interrupt-names", name);
976 return fwnode_irq_get(fwnode, index);
978 EXPORT_SYMBOL(fwnode_irq_get_byname);
981 * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
982 * @fwnode: Pointer to the parent firmware node
983 * @prev: Previous endpoint node or %NULL to get the first
985 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
988 struct fwnode_handle *
989 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
990 struct fwnode_handle *prev)
992 const struct fwnode_handle *parent;
993 struct fwnode_handle *ep;
996 * If this function is in a loop and the previous iteration returned
997 * an endpoint from fwnode->secondary, then we need to use the secondary
998 * as parent rather than @fwnode.
1001 parent = fwnode_graph_get_port_parent(prev);
1004 if (IS_ERR_OR_NULL(parent))
1007 ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
1011 return fwnode_graph_get_next_endpoint(parent->secondary, NULL);
1013 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1016 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1017 * @endpoint: Endpoint firmware node of the port
1019 * Return: the firmware node of the device the @endpoint belongs to.
1021 struct fwnode_handle *
1022 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1024 struct fwnode_handle *port, *parent;
1026 port = fwnode_get_parent(endpoint);
1027 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1029 fwnode_handle_put(port);
1033 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1036 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1037 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1039 * Extracts firmware node of a remote device the @fwnode points to.
1041 struct fwnode_handle *
1042 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1044 struct fwnode_handle *endpoint, *parent;
1046 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1047 parent = fwnode_graph_get_port_parent(endpoint);
1049 fwnode_handle_put(endpoint);
1053 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1056 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1057 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1059 * Extracts firmware node of a remote port the @fwnode points to.
1061 struct fwnode_handle *
1062 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1064 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1066 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1069 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1070 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1072 * Extracts firmware node of a remote endpoint the @fwnode points to.
1074 struct fwnode_handle *
1075 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1077 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1079 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1081 static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
1083 struct fwnode_handle *dev_node;
1086 dev_node = fwnode_graph_get_remote_port_parent(ep);
1087 available = fwnode_device_is_available(dev_node);
1088 fwnode_handle_put(dev_node);
1094 * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1095 * @fwnode: parent fwnode_handle containing the graph
1096 * @port: identifier of the port node
1097 * @endpoint: identifier of the endpoint node under the port node
1098 * @flags: fwnode lookup flags
1100 * Return the fwnode handle of the local endpoint corresponding the port and
1101 * endpoint IDs or NULL if not found.
1103 * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1104 * has not been found, look for the closest endpoint ID greater than the
1105 * specified one and return the endpoint that corresponds to it, if present.
1107 * Does not return endpoints that belong to disabled devices or endpoints that
1108 * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1110 * The returned endpoint needs to be released by calling fwnode_handle_put() on
1111 * it when it is not needed any more.
1113 struct fwnode_handle *
1114 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1115 u32 port, u32 endpoint, unsigned long flags)
1117 struct fwnode_handle *ep, *best_ep = NULL;
1118 unsigned int best_ep_id = 0;
1119 bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1120 bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1122 fwnode_graph_for_each_endpoint(fwnode, ep) {
1123 struct fwnode_endpoint fwnode_ep = { 0 };
1126 if (enabled_only && !fwnode_graph_remote_available(ep))
1129 ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1133 if (fwnode_ep.port != port)
1136 if (fwnode_ep.id == endpoint)
1143 * If the endpoint that has just been found is not the first
1144 * matching one and the ID of the one found previously is closer
1145 * to the requested endpoint ID, skip it.
1147 if (fwnode_ep.id < endpoint ||
1148 (best_ep && best_ep_id < fwnode_ep.id))
1151 fwnode_handle_put(best_ep);
1152 best_ep = fwnode_handle_get(ep);
1153 best_ep_id = fwnode_ep.id;
1158 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1161 * fwnode_graph_get_endpoint_count - Count endpoints on a device node
1162 * @fwnode: The node related to a device
1163 * @flags: fwnode lookup flags
1164 * Count endpoints in a device node.
1166 * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
1167 * and endpoints connected to disabled devices are counted.
1169 unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
1170 unsigned long flags)
1172 struct fwnode_handle *ep;
1173 unsigned int count = 0;
1175 fwnode_graph_for_each_endpoint(fwnode, ep) {
1176 if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
1177 fwnode_graph_remote_available(ep))
1183 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
1186 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1187 * @fwnode: pointer to endpoint fwnode_handle
1188 * @endpoint: pointer to the fwnode endpoint data structure
1190 * Parse @fwnode representing a graph endpoint node and store the
1191 * information in @endpoint. The caller must hold a reference to
1194 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1195 struct fwnode_endpoint *endpoint)
1197 memset(endpoint, 0, sizeof(*endpoint));
1199 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1201 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1203 const void *device_get_match_data(struct device *dev)
1205 return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1207 EXPORT_SYMBOL_GPL(device_get_match_data);
1209 static unsigned int fwnode_graph_devcon_matches(struct fwnode_handle *fwnode,
1210 const char *con_id, void *data,
1211 devcon_match_fn_t match,
1213 unsigned int matches_len)
1215 struct fwnode_handle *node;
1216 struct fwnode_handle *ep;
1217 unsigned int count = 0;
1220 fwnode_graph_for_each_endpoint(fwnode, ep) {
1221 if (matches && count >= matches_len) {
1222 fwnode_handle_put(ep);
1226 node = fwnode_graph_get_remote_port_parent(ep);
1227 if (!fwnode_device_is_available(node)) {
1228 fwnode_handle_put(node);
1232 ret = match(node, con_id, data);
1233 fwnode_handle_put(node);
1236 matches[count] = ret;
1243 static unsigned int fwnode_devcon_matches(struct fwnode_handle *fwnode,
1244 const char *con_id, void *data,
1245 devcon_match_fn_t match,
1247 unsigned int matches_len)
1249 struct fwnode_handle *node;
1250 unsigned int count = 0;
1254 for (i = 0; ; i++) {
1255 if (matches && count >= matches_len)
1258 node = fwnode_find_reference(fwnode, con_id, i);
1262 ret = match(node, NULL, data);
1263 fwnode_handle_put(node);
1266 matches[count] = ret;
1275 * fwnode_connection_find_match - Find connection from a device node
1276 * @fwnode: Device node with the connection
1277 * @con_id: Identifier for the connection
1278 * @data: Data for the match function
1279 * @match: Function to check and convert the connection description
1281 * Find a connection with unique identifier @con_id between @fwnode and another
1282 * device node. @match will be used to convert the connection description to
1283 * data the caller is expecting to be returned.
1285 void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
1286 const char *con_id, void *data,
1287 devcon_match_fn_t match)
1292 if (!fwnode || !match)
1295 count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1299 count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1300 return count ? ret : NULL;
1302 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
1305 * fwnode_connection_find_matches - Find connections from a device node
1306 * @fwnode: Device node with the connection
1307 * @con_id: Identifier for the connection
1308 * @data: Data for the match function
1309 * @match: Function to check and convert the connection description
1310 * @matches: (Optional) array of pointers to fill with matches
1311 * @matches_len: Length of @matches
1313 * Find up to @matches_len connections with unique identifier @con_id between
1314 * @fwnode and other device nodes. @match will be used to convert the
1315 * connection description to data the caller is expecting to be returned
1316 * through the @matches array.
1317 * If @matches is NULL @matches_len is ignored and the total number of resolved
1318 * matches is returned.
1320 * Return: Number of matches resolved, or negative errno.
1322 int fwnode_connection_find_matches(struct fwnode_handle *fwnode,
1323 const char *con_id, void *data,
1324 devcon_match_fn_t match,
1325 void **matches, unsigned int matches_len)
1327 unsigned int count_graph;
1328 unsigned int count_ref;
1330 if (!fwnode || !match)
1333 count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,
1334 matches, matches_len);
1337 matches += count_graph;
1338 matches_len -= count_graph;
1341 count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,
1342 matches, matches_len);
1344 return count_graph + count_ref;
1346 EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);