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/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/etherdevice.h>
19 #include <linux/phy.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);
29 * device_property_present - check if a property of a device is present
30 * @dev: Device whose property is being checked
31 * @propname: Name of the property
33 * Check if property @propname is present in the device firmware description.
35 bool device_property_present(struct device *dev, const char *propname)
37 return fwnode_property_present(dev_fwnode(dev), propname);
39 EXPORT_SYMBOL_GPL(device_property_present);
42 * fwnode_property_present - check if a property of a firmware node is present
43 * @fwnode: Firmware node whose property to check
44 * @propname: Name of the property
46 bool fwnode_property_present(const struct fwnode_handle *fwnode,
51 if (IS_ERR_OR_NULL(fwnode))
54 ret = fwnode_call_bool_op(fwnode, property_present, propname);
58 return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
60 EXPORT_SYMBOL_GPL(fwnode_property_present);
63 * device_property_read_u8_array - return a u8 array property of a device
64 * @dev: Device to get the property of
65 * @propname: Name of the property
66 * @val: The values are stored here or %NULL to return the number of values
67 * @nval: Size of the @val array
69 * Function reads an array of u8 properties with @propname from the device
70 * firmware description and stores them to @val if found.
72 * Return: number of values if @val was %NULL,
73 * %0 if the property was found (success),
74 * %-EINVAL if given arguments are not valid,
75 * %-ENODATA if the property does not have a value,
76 * %-EPROTO if the property is not an array of numbers,
77 * %-EOVERFLOW if the size of the property is not as expected.
78 * %-ENXIO if no suitable firmware interface is present.
80 int device_property_read_u8_array(struct device *dev, const char *propname,
83 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
85 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
88 * device_property_read_u16_array - return a u16 array property of a device
89 * @dev: Device to get the property of
90 * @propname: Name of the property
91 * @val: The values are stored here or %NULL to return the number of values
92 * @nval: Size of the @val array
94 * Function reads an array of u16 properties with @propname from the device
95 * firmware description and stores them to @val if found.
97 * Return: number of values if @val was %NULL,
98 * %0 if the property was found (success),
99 * %-EINVAL if given arguments are not valid,
100 * %-ENODATA if the property does not have a value,
101 * %-EPROTO if the property is not an array of numbers,
102 * %-EOVERFLOW if the size of the property is not as expected.
103 * %-ENXIO if no suitable firmware interface is present.
105 int device_property_read_u16_array(struct device *dev, const char *propname,
106 u16 *val, size_t nval)
108 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
110 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
113 * device_property_read_u32_array - return a u32 array property of a device
114 * @dev: Device to get the property of
115 * @propname: Name of the property
116 * @val: The values are stored here or %NULL to return the number of values
117 * @nval: Size of the @val array
119 * Function reads an array of u32 properties with @propname from the device
120 * firmware description and stores them to @val if found.
122 * Return: number of values if @val was %NULL,
123 * %0 if the property was found (success),
124 * %-EINVAL if given arguments are not valid,
125 * %-ENODATA if the property does not have a value,
126 * %-EPROTO if the property is not an array of numbers,
127 * %-EOVERFLOW if the size of the property is not as expected.
128 * %-ENXIO if no suitable firmware interface is present.
130 int device_property_read_u32_array(struct device *dev, const char *propname,
131 u32 *val, size_t nval)
133 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
135 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
138 * device_property_read_u64_array - return a u64 array property of a device
139 * @dev: Device to get the property of
140 * @propname: Name of the property
141 * @val: The values are stored here or %NULL to return the number of values
142 * @nval: Size of the @val array
144 * Function reads an array of u64 properties with @propname from the device
145 * firmware description and stores them to @val if found.
147 * Return: number of values if @val was %NULL,
148 * %0 if the property was found (success),
149 * %-EINVAL if given arguments are not valid,
150 * %-ENODATA if the property does not have a value,
151 * %-EPROTO if the property is not an array of numbers,
152 * %-EOVERFLOW if the size of the property is not as expected.
153 * %-ENXIO if no suitable firmware interface is present.
155 int device_property_read_u64_array(struct device *dev, const char *propname,
156 u64 *val, size_t nval)
158 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
160 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
163 * device_property_read_string_array - return a string array property of device
164 * @dev: Device to get the property of
165 * @propname: Name of the property
166 * @val: The values are stored here or %NULL to return the number of values
167 * @nval: Size of the @val array
169 * Function reads an array of string properties with @propname from the device
170 * firmware description and stores them to @val if found.
172 * Return: number of values read on success if @val is non-NULL,
173 * number of values available on success if @val is NULL,
174 * %-EINVAL if given arguments are not valid,
175 * %-ENODATA if the property does not have a value,
176 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
177 * %-EOVERFLOW if the size of the property is not as expected.
178 * %-ENXIO if no suitable firmware interface is present.
180 int device_property_read_string_array(struct device *dev, const char *propname,
181 const char **val, size_t nval)
183 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
185 EXPORT_SYMBOL_GPL(device_property_read_string_array);
188 * device_property_read_string - return a string property of a device
189 * @dev: Device to get the property of
190 * @propname: Name of the property
191 * @val: The value is stored here
193 * Function reads property @propname from the device firmware description and
194 * stores the value into @val if found. The value is checked to be a string.
196 * Return: %0 if the property was found (success),
197 * %-EINVAL if given arguments are not valid,
198 * %-ENODATA if the property does not have a value,
199 * %-EPROTO or %-EILSEQ if the property type is not a string.
200 * %-ENXIO if no suitable firmware interface is present.
202 int device_property_read_string(struct device *dev, const char *propname,
205 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
207 EXPORT_SYMBOL_GPL(device_property_read_string);
210 * device_property_match_string - find a string in an array and return index
211 * @dev: Device to get the property of
212 * @propname: Name of the property holding the array
213 * @string: String to look for
215 * Find a given string in a string array and if it is found return the
218 * Return: %0 if the property was found (success),
219 * %-EINVAL if given arguments are not valid,
220 * %-ENODATA if the property does not have a value,
221 * %-EPROTO if the property is not an array of strings,
222 * %-ENXIO if no suitable firmware interface is present.
224 int device_property_match_string(struct device *dev, const char *propname,
227 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
229 EXPORT_SYMBOL_GPL(device_property_match_string);
231 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
232 const char *propname,
233 unsigned int elem_size, void *val,
238 if (IS_ERR_OR_NULL(fwnode))
241 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
242 elem_size, val, nval);
246 return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
247 elem_size, val, nval);
251 * fwnode_property_read_u8_array - return a u8 array property of firmware node
252 * @fwnode: Firmware node to get the property of
253 * @propname: Name of the property
254 * @val: The values are stored here or %NULL to return the number of values
255 * @nval: Size of the @val array
257 * Read an array of u8 properties with @propname from @fwnode and stores them to
260 * Return: number of values if @val was %NULL,
261 * %0 if the property was found (success),
262 * %-EINVAL if given arguments are not valid,
263 * %-ENODATA if the property does not have a value,
264 * %-EPROTO if the property is not an array of numbers,
265 * %-EOVERFLOW if the size of the property is not as expected,
266 * %-ENXIO if no suitable firmware interface is present.
268 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
269 const char *propname, u8 *val, size_t nval)
271 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
274 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
277 * fwnode_property_read_u16_array - return a u16 array property of firmware node
278 * @fwnode: Firmware node to get the property of
279 * @propname: Name of the property
280 * @val: The values are stored here or %NULL to return the number of values
281 * @nval: Size of the @val array
283 * Read an array of u16 properties with @propname from @fwnode and store them to
286 * Return: number of values if @val was %NULL,
287 * %0 if the property was found (success),
288 * %-EINVAL if given arguments are not valid,
289 * %-ENODATA if the property does not have a value,
290 * %-EPROTO if the property is not an array of numbers,
291 * %-EOVERFLOW if the size of the property is not as expected,
292 * %-ENXIO if no suitable firmware interface is present.
294 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
295 const char *propname, u16 *val, size_t nval)
297 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
300 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
303 * fwnode_property_read_u32_array - return a u32 array property of firmware node
304 * @fwnode: Firmware node to get the property of
305 * @propname: Name of the property
306 * @val: The values are stored here or %NULL to return the number of values
307 * @nval: Size of the @val array
309 * Read an array of u32 properties with @propname from @fwnode store them to
312 * Return: number of values if @val was %NULL,
313 * %0 if the property was found (success),
314 * %-EINVAL if given arguments are not valid,
315 * %-ENODATA if the property does not have a value,
316 * %-EPROTO if the property is not an array of numbers,
317 * %-EOVERFLOW if the size of the property is not as expected,
318 * %-ENXIO if no suitable firmware interface is present.
320 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
321 const char *propname, u32 *val, size_t nval)
323 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
326 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
329 * fwnode_property_read_u64_array - return a u64 array property firmware node
330 * @fwnode: Firmware node to get the property of
331 * @propname: Name of the property
332 * @val: The values are stored here or %NULL to return the number of values
333 * @nval: Size of the @val array
335 * Read an array of u64 properties with @propname from @fwnode and store them to
338 * Return: number of values if @val was %NULL,
339 * %0 if the property was found (success),
340 * %-EINVAL if given arguments are not valid,
341 * %-ENODATA if the property does not have a value,
342 * %-EPROTO if the property is not an array of numbers,
343 * %-EOVERFLOW if the size of the property is not as expected,
344 * %-ENXIO if no suitable firmware interface is present.
346 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
347 const char *propname, u64 *val, size_t nval)
349 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
352 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
355 * fwnode_property_read_string_array - return string array property of a node
356 * @fwnode: Firmware node to get the property of
357 * @propname: Name of the property
358 * @val: The values are stored here or %NULL to return the number of values
359 * @nval: Size of the @val array
361 * Read an string list property @propname from the given firmware node and store
362 * them to @val if found.
364 * Return: number of values read on success if @val is non-NULL,
365 * number of values available on success if @val is NULL,
366 * %-EINVAL if given arguments are not valid,
367 * %-ENODATA if the property does not have a value,
368 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
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_string_array(const struct fwnode_handle *fwnode,
373 const char *propname, const char **val,
378 if (IS_ERR_OR_NULL(fwnode))
381 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
386 return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
389 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
392 * fwnode_property_read_string - return a string property of a firmware node
393 * @fwnode: Firmware node to get the property of
394 * @propname: Name of the property
395 * @val: The value is stored here
397 * Read property @propname from the given firmware node and store the value into
398 * @val if found. The value is checked to be a string.
400 * Return: %0 if the property was found (success),
401 * %-EINVAL if given arguments are not valid,
402 * %-ENODATA if the property does not have a value,
403 * %-EPROTO or %-EILSEQ if the property is not a string,
404 * %-ENXIO if no suitable firmware interface is present.
406 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
407 const char *propname, const char **val)
409 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
411 return ret < 0 ? ret : 0;
413 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
416 * fwnode_property_match_string - find a string in an array and return index
417 * @fwnode: Firmware node to get the property of
418 * @propname: Name of the property holding the array
419 * @string: String to look for
421 * Find a given string in a string array and if it is found return the
424 * Return: %0 if the property was found (success),
425 * %-EINVAL if given arguments are not valid,
426 * %-ENODATA if the property does not have a value,
427 * %-EPROTO if the property is not an array of strings,
428 * %-ENXIO if no suitable firmware interface is present.
430 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
431 const char *propname, const char *string)
436 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
443 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
447 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
451 ret = match_string(values, nval, string);
458 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
461 * fwnode_property_get_reference_args() - Find a reference with arguments
462 * @fwnode: Firmware node where to look for the reference
463 * @prop: The name of the property
464 * @nargs_prop: The name of the property telling the number of
465 * arguments in the referred node. NULL if @nargs is known,
466 * otherwise @nargs is ignored. Only relevant on OF.
467 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
468 * @index: Index of the reference, from zero onwards.
469 * @args: Result structure with reference and integer arguments.
471 * Obtain a reference based on a named property in an fwnode, with
474 * Caller is responsible to call fwnode_handle_put() on the returned
475 * args->fwnode pointer.
477 * Returns: %0 on success
478 * %-ENOENT when the index is out of bounds, the index has an empty
479 * reference or the property was not found
480 * %-EINVAL on parse error
482 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
483 const char *prop, const char *nargs_prop,
484 unsigned int nargs, unsigned int index,
485 struct fwnode_reference_args *args)
489 if (IS_ERR_OR_NULL(fwnode))
492 ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
497 if (IS_ERR_OR_NULL(fwnode->secondary))
500 return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
503 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
506 * fwnode_find_reference - Find named reference to a fwnode_handle
507 * @fwnode: Firmware node where to look for the reference
508 * @name: The name of the reference
509 * @index: Index of the reference
511 * @index can be used when the named reference holds a table of references.
513 * Returns pointer to the reference fwnode, or ERR_PTR. Caller is responsible to
514 * call fwnode_handle_put() on the returned fwnode pointer.
516 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
520 struct fwnode_reference_args args;
523 ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
525 return ret ? ERR_PTR(ret) : args.fwnode;
527 EXPORT_SYMBOL_GPL(fwnode_find_reference);
530 * device_remove_properties - Remove properties from a device object.
531 * @dev: Device whose properties to remove.
533 * The function removes properties previously associated to the device
534 * firmware node with device_add_properties(). Memory allocated to the
535 * properties will also be released.
537 void device_remove_properties(struct device *dev)
539 struct fwnode_handle *fwnode = dev_fwnode(dev);
544 if (is_software_node(fwnode->secondary)) {
545 fwnode_remove_software_node(fwnode->secondary);
546 set_secondary_fwnode(dev, NULL);
549 EXPORT_SYMBOL_GPL(device_remove_properties);
552 * device_add_properties - Add a collection of properties to a device object.
553 * @dev: Device to add properties to.
554 * @properties: Collection of properties to add.
556 * Associate a collection of device properties represented by @properties with
557 * @dev. The function takes a copy of @properties.
559 * WARNING: The callers should not use this function if it is known that there
560 * is no real firmware node associated with @dev! In that case the callers
561 * should create a software node and assign it to @dev directly.
563 int device_add_properties(struct device *dev,
564 const struct property_entry *properties)
566 struct fwnode_handle *fwnode;
568 fwnode = fwnode_create_software_node(properties, NULL);
570 return PTR_ERR(fwnode);
572 set_secondary_fwnode(dev, fwnode);
575 EXPORT_SYMBOL_GPL(device_add_properties);
578 * fwnode_get_name - Return the name of a node
579 * @fwnode: The firmware node
581 * Returns a pointer to the node name.
583 const char *fwnode_get_name(const struct fwnode_handle *fwnode)
585 return fwnode_call_ptr_op(fwnode, get_name);
587 EXPORT_SYMBOL_GPL(fwnode_get_name);
590 * fwnode_get_name_prefix - Return the prefix of node for printing purposes
591 * @fwnode: The firmware node
593 * Returns the prefix of a node, intended to be printed right before the node.
594 * The prefix works also as a separator between the nodes.
596 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
598 return fwnode_call_ptr_op(fwnode, get_name_prefix);
602 * fwnode_get_parent - Return parent firwmare node
603 * @fwnode: Firmware whose parent is retrieved
605 * Return parent firmware node of the given node if possible or %NULL if no
606 * parent was available.
608 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
610 return fwnode_call_ptr_op(fwnode, get_parent);
612 EXPORT_SYMBOL_GPL(fwnode_get_parent);
615 * fwnode_get_next_parent - Iterate to the node's parent
616 * @fwnode: Firmware whose parent is retrieved
618 * This is like fwnode_get_parent() except that it drops the refcount
619 * on the passed node, making it suitable for iterating through a
622 * Returns a node pointer with refcount incremented, use
623 * fwnode_handle_node() on it when done.
625 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
627 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
629 fwnode_handle_put(fwnode);
633 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
636 * fwnode_get_next_parent_dev - Find device of closest ancestor fwnode
637 * @fwnode: firmware node
639 * Given a firmware node (@fwnode), this function finds its closest ancestor
640 * firmware node that has a corresponding struct device and returns that struct
643 * The caller of this function is expected to call put_device() on the returned
644 * device when they are done.
646 struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
650 fwnode_handle_get(fwnode);
652 fwnode = fwnode_get_next_parent(fwnode);
655 dev = get_dev_from_fwnode(fwnode);
657 fwnode_handle_put(fwnode);
662 * fwnode_count_parents - Return the number of parents a node has
663 * @fwnode: The node the parents of which are to be counted
665 * Returns the number of parents a node has.
667 unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
669 struct fwnode_handle *__fwnode;
672 __fwnode = fwnode_get_parent(fwnode);
674 for (count = 0; __fwnode; count++)
675 __fwnode = fwnode_get_next_parent(__fwnode);
679 EXPORT_SYMBOL_GPL(fwnode_count_parents);
682 * fwnode_get_nth_parent - Return an nth parent of a node
683 * @fwnode: The node the parent of which is requested
684 * @depth: Distance of the parent from the node
686 * Returns the nth parent of a node. If there is no parent at the requested
687 * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
688 * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
690 * The caller is responsible for calling fwnode_handle_put() for the returned
693 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
696 fwnode_handle_get(fwnode);
701 fwnode = fwnode_get_next_parent(fwnode);
706 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
709 * fwnode_is_ancestor_of - Test if @test_ancestor is ancestor of @test_child
710 * @test_ancestor: Firmware which is tested for being an ancestor
711 * @test_child: Firmware which is tested for being the child
713 * A node is considered an ancestor of itself too.
715 * Returns true if @test_ancestor is an ancestor of @test_child.
716 * Otherwise, returns false.
718 bool fwnode_is_ancestor_of(struct fwnode_handle *test_ancestor,
719 struct fwnode_handle *test_child)
721 if (IS_ERR_OR_NULL(test_ancestor))
724 fwnode_handle_get(test_child);
726 if (test_child == test_ancestor) {
727 fwnode_handle_put(test_child);
730 test_child = fwnode_get_next_parent(test_child);
731 } while (test_child);
736 * fwnode_get_next_child_node - Return the next child node handle for a node
737 * @fwnode: Firmware node to find the next child node for.
738 * @child: Handle to one of the node's child nodes or a %NULL handle.
740 struct fwnode_handle *
741 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
742 struct fwnode_handle *child)
744 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
746 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
749 * fwnode_get_next_available_child_node - Return the next
750 * available child node handle for a node
751 * @fwnode: Firmware node to find the next child node for.
752 * @child: Handle to one of the node's child nodes or a %NULL handle.
754 struct fwnode_handle *
755 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
756 struct fwnode_handle *child)
758 struct fwnode_handle *next_child = child;
760 if (IS_ERR_OR_NULL(fwnode))
764 next_child = fwnode_get_next_child_node(fwnode, next_child);
767 } while (!fwnode_device_is_available(next_child));
771 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
774 * device_get_next_child_node - Return the next child node handle for a device
775 * @dev: Device to find the next child node for.
776 * @child: Handle to one of the device's child nodes or a null handle.
778 struct fwnode_handle *device_get_next_child_node(struct device *dev,
779 struct fwnode_handle *child)
781 const struct fwnode_handle *fwnode = dev_fwnode(dev);
782 struct fwnode_handle *next;
784 if (IS_ERR_OR_NULL(fwnode))
787 /* Try to find a child in primary fwnode */
788 next = fwnode_get_next_child_node(fwnode, child);
792 /* When no more children in primary, continue with secondary */
793 return fwnode_get_next_child_node(fwnode->secondary, child);
795 EXPORT_SYMBOL_GPL(device_get_next_child_node);
798 * fwnode_get_named_child_node - Return first matching named child node handle
799 * @fwnode: Firmware node to find the named child node for.
800 * @childname: String to match child node name against.
802 struct fwnode_handle *
803 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
804 const char *childname)
806 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
808 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
811 * device_get_named_child_node - Return first matching named child node handle
812 * @dev: Device to find the named child node for.
813 * @childname: String to match child node name against.
815 struct fwnode_handle *device_get_named_child_node(struct device *dev,
816 const char *childname)
818 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
820 EXPORT_SYMBOL_GPL(device_get_named_child_node);
823 * fwnode_handle_get - Obtain a reference to a device node
824 * @fwnode: Pointer to the device node to obtain the reference to.
826 * Returns the fwnode handle.
828 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
830 if (!fwnode_has_op(fwnode, get))
833 return fwnode_call_ptr_op(fwnode, get);
835 EXPORT_SYMBOL_GPL(fwnode_handle_get);
838 * fwnode_handle_put - Drop reference to a device node
839 * @fwnode: Pointer to the device node to drop the reference to.
841 * This has to be used when terminating device_for_each_child_node() iteration
842 * with break or return to prevent stale device node references from being left
845 void fwnode_handle_put(struct fwnode_handle *fwnode)
847 fwnode_call_void_op(fwnode, put);
849 EXPORT_SYMBOL_GPL(fwnode_handle_put);
852 * fwnode_device_is_available - check if a device is available for use
853 * @fwnode: Pointer to the fwnode of the device.
855 * For fwnode node types that don't implement the .device_is_available()
856 * operation, this function returns true.
858 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
860 if (IS_ERR_OR_NULL(fwnode))
863 if (!fwnode_has_op(fwnode, device_is_available))
866 return fwnode_call_bool_op(fwnode, device_is_available);
868 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
871 * device_get_child_node_count - return the number of child nodes for device
872 * @dev: Device to cound the child nodes for
874 unsigned int device_get_child_node_count(struct device *dev)
876 struct fwnode_handle *child;
877 unsigned int count = 0;
879 device_for_each_child_node(dev, child)
884 EXPORT_SYMBOL_GPL(device_get_child_node_count);
886 bool device_dma_supported(struct device *dev)
888 const struct fwnode_handle *fwnode = dev_fwnode(dev);
890 /* For DT, this is always supported.
891 * For ACPI, this depends on CCA, which
892 * is determined by the acpi_dma_supported().
894 if (is_of_node(fwnode))
897 return acpi_dma_supported(to_acpi_device_node(fwnode));
899 EXPORT_SYMBOL_GPL(device_dma_supported);
901 enum dev_dma_attr device_get_dma_attr(struct device *dev)
903 const struct fwnode_handle *fwnode = dev_fwnode(dev);
904 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
906 if (is_of_node(fwnode)) {
907 if (of_dma_is_coherent(to_of_node(fwnode)))
908 attr = DEV_DMA_COHERENT;
910 attr = DEV_DMA_NON_COHERENT;
912 attr = acpi_get_dma_attr(to_acpi_device_node(fwnode));
916 EXPORT_SYMBOL_GPL(device_get_dma_attr);
919 * fwnode_get_phy_mode - Get phy mode for given firmware node
920 * @fwnode: Pointer to the given node
922 * The function gets phy interface string from property 'phy-mode' or
923 * 'phy-connection-type', and return its index in phy_modes table, or errno in
926 int fwnode_get_phy_mode(struct fwnode_handle *fwnode)
931 err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
933 err = fwnode_property_read_string(fwnode,
934 "phy-connection-type", &pm);
938 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
939 if (!strcasecmp(pm, phy_modes(i)))
944 EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
947 * device_get_phy_mode - Get phy mode for given device
948 * @dev: Pointer to the given device
950 * The function gets phy interface string from property 'phy-mode' or
951 * 'phy-connection-type', and return its index in phy_modes table, or errno in
954 int device_get_phy_mode(struct device *dev)
956 return fwnode_get_phy_mode(dev_fwnode(dev));
958 EXPORT_SYMBOL_GPL(device_get_phy_mode);
960 static void *fwnode_get_mac_addr(struct fwnode_handle *fwnode,
961 const char *name, char *addr,
964 int ret = fwnode_property_read_u8_array(fwnode, name, addr, alen);
966 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
972 * fwnode_get_mac_address - Get the MAC from the firmware node
973 * @fwnode: Pointer to the firmware node
974 * @addr: Address of buffer to store the MAC in
975 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
977 * Search the firmware node for the best MAC address to use. 'mac-address' is
978 * checked first, because that is supposed to contain to "most recent" MAC
979 * address. If that isn't set, then 'local-mac-address' is checked next,
980 * because that is the default address. If that isn't set, then the obsolete
981 * 'address' is checked, just in case we're using an old device tree.
983 * Note that the 'address' property is supposed to contain a virtual address of
984 * the register set, but some DTS files have redefined that property to be the
987 * All-zero MAC addresses are rejected, because those could be properties that
988 * exist in the firmware tables, but were not updated by the firmware. For
989 * example, the DTS could define 'mac-address' and 'local-mac-address', with
990 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
991 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
992 * exists but is all zeros.
994 void *fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr, int alen)
998 res = fwnode_get_mac_addr(fwnode, "mac-address", addr, alen);
1002 res = fwnode_get_mac_addr(fwnode, "local-mac-address", addr, alen);
1006 return fwnode_get_mac_addr(fwnode, "address", addr, alen);
1008 EXPORT_SYMBOL(fwnode_get_mac_address);
1011 * device_get_mac_address - Get the MAC for a given device
1012 * @dev: Pointer to the device
1013 * @addr: Address of buffer to store the MAC in
1014 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1016 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1018 return fwnode_get_mac_address(dev_fwnode(dev), addr, alen);
1020 EXPORT_SYMBOL(device_get_mac_address);
1023 * fwnode_irq_get - Get IRQ directly from a fwnode
1024 * @fwnode: Pointer to the firmware node
1025 * @index: Zero-based index of the IRQ
1027 * Returns Linux IRQ number on success. Other values are determined
1028 * accordingly to acpi_/of_ irq_get() operation.
1030 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
1032 struct resource res;
1035 if (is_of_node(fwnode))
1036 return of_irq_get(to_of_node(fwnode), index);
1038 ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
1044 EXPORT_SYMBOL(fwnode_irq_get);
1047 * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
1048 * @fwnode: Pointer to the parent firmware node
1049 * @prev: Previous endpoint node or %NULL to get the first
1051 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1054 struct fwnode_handle *
1055 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1056 struct fwnode_handle *prev)
1058 struct fwnode_handle *ep, *port_parent = NULL;
1059 const struct fwnode_handle *parent;
1062 * If this function is in a loop and the previous iteration returned
1063 * an endpoint from fwnode->secondary, then we need to use the secondary
1064 * as parent rather than @fwnode.
1067 port_parent = fwnode_graph_get_port_parent(prev);
1068 parent = port_parent;
1072 if (IS_ERR_OR_NULL(parent))
1075 ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
1077 goto out_put_port_parent;
1079 ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);
1081 out_put_port_parent:
1082 fwnode_handle_put(port_parent);
1085 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1088 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1089 * @endpoint: Endpoint firmware node of the port
1091 * Return: the firmware node of the device the @endpoint belongs to.
1093 struct fwnode_handle *
1094 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1096 struct fwnode_handle *port, *parent;
1098 port = fwnode_get_parent(endpoint);
1099 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1101 fwnode_handle_put(port);
1105 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1108 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1109 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1111 * Extracts firmware node of a remote device the @fwnode points to.
1113 struct fwnode_handle *
1114 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1116 struct fwnode_handle *endpoint, *parent;
1118 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1119 parent = fwnode_graph_get_port_parent(endpoint);
1121 fwnode_handle_put(endpoint);
1125 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1128 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1129 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1131 * Extracts firmware node of a remote port the @fwnode points to.
1133 struct fwnode_handle *
1134 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1136 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1138 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1141 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1142 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1144 * Extracts firmware node of a remote endpoint the @fwnode points to.
1146 struct fwnode_handle *
1147 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1149 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1151 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1154 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1155 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1156 * @port_id: identifier of the parent port node
1157 * @endpoint_id: identifier of the endpoint node
1159 * Return: Remote fwnode handle associated with remote endpoint node linked
1160 * to @node. Use fwnode_node_put() on it when done.
1162 struct fwnode_handle *
1163 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1166 struct fwnode_handle *endpoint = NULL;
1168 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1169 struct fwnode_endpoint fwnode_ep;
1170 struct fwnode_handle *remote;
1173 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1177 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1180 remote = fwnode_graph_get_remote_port_parent(endpoint);
1184 return fwnode_device_is_available(remote) ? remote : NULL;
1189 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1192 * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1193 * @fwnode: parent fwnode_handle containing the graph
1194 * @port: identifier of the port node
1195 * @endpoint: identifier of the endpoint node under the port node
1196 * @flags: fwnode lookup flags
1198 * Return the fwnode handle of the local endpoint corresponding the port and
1199 * endpoint IDs or NULL if not found.
1201 * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1202 * has not been found, look for the closest endpoint ID greater than the
1203 * specified one and return the endpoint that corresponds to it, if present.
1205 * Do not return endpoints that belong to disabled devices, unless
1206 * FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1208 * The returned endpoint needs to be released by calling fwnode_handle_put() on
1209 * it when it is not needed any more.
1211 struct fwnode_handle *
1212 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1213 u32 port, u32 endpoint, unsigned long flags)
1215 struct fwnode_handle *ep = NULL, *best_ep = NULL;
1216 unsigned int best_ep_id = 0;
1217 bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1218 bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1220 while ((ep = fwnode_graph_get_next_endpoint(fwnode, ep))) {
1221 struct fwnode_endpoint fwnode_ep = { 0 };
1225 struct fwnode_handle *dev_node;
1228 dev_node = fwnode_graph_get_remote_port_parent(ep);
1229 available = fwnode_device_is_available(dev_node);
1230 fwnode_handle_put(dev_node);
1235 ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1239 if (fwnode_ep.port != port)
1242 if (fwnode_ep.id == endpoint)
1249 * If the endpoint that has just been found is not the first
1250 * matching one and the ID of the one found previously is closer
1251 * to the requested endpoint ID, skip it.
1253 if (fwnode_ep.id < endpoint ||
1254 (best_ep && best_ep_id < fwnode_ep.id))
1257 fwnode_handle_put(best_ep);
1258 best_ep = fwnode_handle_get(ep);
1259 best_ep_id = fwnode_ep.id;
1264 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1267 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1268 * @fwnode: pointer to endpoint fwnode_handle
1269 * @endpoint: pointer to the fwnode endpoint data structure
1271 * Parse @fwnode representing a graph endpoint node and store the
1272 * information in @endpoint. The caller must hold a reference to
1275 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1276 struct fwnode_endpoint *endpoint)
1278 memset(endpoint, 0, sizeof(*endpoint));
1280 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1282 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1284 const void *device_get_match_data(struct device *dev)
1286 return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1288 EXPORT_SYMBOL_GPL(device_get_match_data);
1291 fwnode_graph_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
1292 void *data, devcon_match_fn_t match)
1294 struct fwnode_handle *node;
1295 struct fwnode_handle *ep;
1298 fwnode_graph_for_each_endpoint(fwnode, ep) {
1299 node = fwnode_graph_get_remote_port_parent(ep);
1300 if (!fwnode_device_is_available(node)) {
1301 fwnode_handle_put(node);
1305 ret = match(node, con_id, data);
1306 fwnode_handle_put(node);
1308 fwnode_handle_put(ep);
1316 fwnode_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
1317 void *data, devcon_match_fn_t match)
1319 struct fwnode_handle *node;
1323 for (i = 0; ; i++) {
1324 node = fwnode_find_reference(fwnode, con_id, i);
1328 ret = match(node, NULL, data);
1329 fwnode_handle_put(node);
1338 * fwnode_connection_find_match - Find connection from a device node
1339 * @fwnode: Device node with the connection
1340 * @con_id: Identifier for the connection
1341 * @data: Data for the match function
1342 * @match: Function to check and convert the connection description
1344 * Find a connection with unique identifier @con_id between @fwnode and another
1345 * device node. @match will be used to convert the connection description to
1346 * data the caller is expecting to be returned.
1348 void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
1349 const char *con_id, void *data,
1350 devcon_match_fn_t match)
1354 if (!fwnode || !match)
1357 ret = fwnode_graph_devcon_match(fwnode, con_id, data, match);
1361 return fwnode_devcon_match(fwnode, con_id, data, match);
1363 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);