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/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 ret = fwnode_call_bool_op(fwnode, property_present, propname);
51 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
52 !IS_ERR_OR_NULL(fwnode->secondary))
53 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
57 EXPORT_SYMBOL_GPL(fwnode_property_present);
60 * device_property_read_u8_array - return a u8 array property of a device
61 * @dev: Device to get the property of
62 * @propname: Name of the property
63 * @val: The values are stored here or %NULL to return the number of values
64 * @nval: Size of the @val array
66 * Function reads an array of u8 properties with @propname from the device
67 * firmware description and stores them to @val if found.
69 * Return: number of values if @val was %NULL,
70 * %0 if the property was found (success),
71 * %-EINVAL if given arguments are not valid,
72 * %-ENODATA if the property does not have a value,
73 * %-EPROTO if the property is not an array of numbers,
74 * %-EOVERFLOW if the size of the property is not as expected.
75 * %-ENXIO if no suitable firmware interface is present.
77 int device_property_read_u8_array(struct device *dev, const char *propname,
80 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
82 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
85 * device_property_read_u16_array - return a u16 array property of a device
86 * @dev: Device to get the property of
87 * @propname: Name of the property
88 * @val: The values are stored here or %NULL to return the number of values
89 * @nval: Size of the @val array
91 * Function reads an array of u16 properties with @propname from the device
92 * firmware description and stores them to @val if found.
94 * Return: number of values if @val was %NULL,
95 * %0 if the property was found (success),
96 * %-EINVAL if given arguments are not valid,
97 * %-ENODATA if the property does not have a value,
98 * %-EPROTO if the property is not an array of numbers,
99 * %-EOVERFLOW if the size of the property is not as expected.
100 * %-ENXIO if no suitable firmware interface is present.
102 int device_property_read_u16_array(struct device *dev, const char *propname,
103 u16 *val, size_t nval)
105 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
107 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
110 * device_property_read_u32_array - return a u32 array property of a device
111 * @dev: Device to get the property of
112 * @propname: Name of the property
113 * @val: The values are stored here or %NULL to return the number of values
114 * @nval: Size of the @val array
116 * Function reads an array of u32 properties with @propname from the device
117 * firmware description and stores them to @val if found.
119 * Return: number of values if @val was %NULL,
120 * %0 if the property was found (success),
121 * %-EINVAL if given arguments are not valid,
122 * %-ENODATA if the property does not have a value,
123 * %-EPROTO if the property is not an array of numbers,
124 * %-EOVERFLOW if the size of the property is not as expected.
125 * %-ENXIO if no suitable firmware interface is present.
127 int device_property_read_u32_array(struct device *dev, const char *propname,
128 u32 *val, size_t nval)
130 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
132 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
135 * device_property_read_u64_array - return a u64 array property of a device
136 * @dev: Device to get the property of
137 * @propname: Name of the property
138 * @val: The values are stored here or %NULL to return the number of values
139 * @nval: Size of the @val array
141 * Function reads an array of u64 properties with @propname from the device
142 * firmware description and stores them to @val if found.
144 * Return: number of values if @val was %NULL,
145 * %0 if the property was found (success),
146 * %-EINVAL if given arguments are not valid,
147 * %-ENODATA if the property does not have a value,
148 * %-EPROTO if the property is not an array of numbers,
149 * %-EOVERFLOW if the size of the property is not as expected.
150 * %-ENXIO if no suitable firmware interface is present.
152 int device_property_read_u64_array(struct device *dev, const char *propname,
153 u64 *val, size_t nval)
155 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
157 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
160 * device_property_read_string_array - return a string array property of device
161 * @dev: Device to get the property of
162 * @propname: Name of the property
163 * @val: The values are stored here or %NULL to return the number of values
164 * @nval: Size of the @val array
166 * Function reads an array of string properties with @propname from the device
167 * firmware description and stores them to @val if found.
169 * Return: number of values read on success if @val is non-NULL,
170 * number of values available on success if @val is NULL,
171 * %-EINVAL if given arguments are not valid,
172 * %-ENODATA if the property does not have a value,
173 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
174 * %-EOVERFLOW if the size of the property is not as expected.
175 * %-ENXIO if no suitable firmware interface is present.
177 int device_property_read_string_array(struct device *dev, const char *propname,
178 const char **val, size_t nval)
180 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
182 EXPORT_SYMBOL_GPL(device_property_read_string_array);
185 * device_property_read_string - return a string property of a device
186 * @dev: Device to get the property of
187 * @propname: Name of the property
188 * @val: The value is stored here
190 * Function reads property @propname from the device firmware description and
191 * stores the value into @val if found. The value is checked to be a string.
193 * Return: %0 if the property was found (success),
194 * %-EINVAL if given arguments are not valid,
195 * %-ENODATA if the property does not have a value,
196 * %-EPROTO or %-EILSEQ if the property type is not a string.
197 * %-ENXIO if no suitable firmware interface is present.
199 int device_property_read_string(struct device *dev, const char *propname,
202 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
204 EXPORT_SYMBOL_GPL(device_property_read_string);
207 * device_property_match_string - find a string in an array and return index
208 * @dev: Device to get the property of
209 * @propname: Name of the property holding the array
210 * @string: String to look for
212 * Find a given string in a string array and if it is found return the
215 * Return: %0 if the property was found (success),
216 * %-EINVAL if given arguments are not valid,
217 * %-ENODATA if the property does not have a value,
218 * %-EPROTO if the property is not an array of strings,
219 * %-ENXIO if no suitable firmware interface is present.
221 int device_property_match_string(struct device *dev, const char *propname,
224 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
226 EXPORT_SYMBOL_GPL(device_property_match_string);
228 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
229 const char *propname,
230 unsigned int elem_size, void *val,
235 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
236 elem_size, val, nval);
237 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
238 !IS_ERR_OR_NULL(fwnode->secondary))
239 ret = fwnode_call_int_op(
240 fwnode->secondary, property_read_int_array, propname,
241 elem_size, val, nval);
247 * fwnode_property_read_u8_array - return a u8 array property of firmware node
248 * @fwnode: Firmware node to get the property of
249 * @propname: Name of the property
250 * @val: The values are stored here or %NULL to return the number of values
251 * @nval: Size of the @val array
253 * Read an array of u8 properties with @propname from @fwnode and stores them to
256 * Return: number of values if @val was %NULL,
257 * %0 if the property was found (success),
258 * %-EINVAL if given arguments are not valid,
259 * %-ENODATA if the property does not have a value,
260 * %-EPROTO if the property is not an array of numbers,
261 * %-EOVERFLOW if the size of the property is not as expected,
262 * %-ENXIO if no suitable firmware interface is present.
264 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
265 const char *propname, u8 *val, size_t nval)
267 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
270 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
273 * fwnode_property_read_u16_array - return a u16 array property of firmware node
274 * @fwnode: Firmware node to get the property of
275 * @propname: Name of the property
276 * @val: The values are stored here or %NULL to return the number of values
277 * @nval: Size of the @val array
279 * Read an array of u16 properties with @propname from @fwnode and store them to
282 * Return: number of values if @val was %NULL,
283 * %0 if the property was found (success),
284 * %-EINVAL if given arguments are not valid,
285 * %-ENODATA if the property does not have a value,
286 * %-EPROTO if the property is not an array of numbers,
287 * %-EOVERFLOW if the size of the property is not as expected,
288 * %-ENXIO if no suitable firmware interface is present.
290 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
291 const char *propname, u16 *val, size_t nval)
293 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
296 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
299 * fwnode_property_read_u32_array - return a u32 array property of firmware node
300 * @fwnode: Firmware node to get the property of
301 * @propname: Name of the property
302 * @val: The values are stored here or %NULL to return the number of values
303 * @nval: Size of the @val array
305 * Read an array of u32 properties with @propname from @fwnode store them to
308 * Return: number of values if @val was %NULL,
309 * %0 if the property was found (success),
310 * %-EINVAL if given arguments are not valid,
311 * %-ENODATA if the property does not have a value,
312 * %-EPROTO if the property is not an array of numbers,
313 * %-EOVERFLOW if the size of the property is not as expected,
314 * %-ENXIO if no suitable firmware interface is present.
316 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
317 const char *propname, u32 *val, size_t nval)
319 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
322 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
325 * fwnode_property_read_u64_array - return a u64 array property firmware node
326 * @fwnode: Firmware node to get the property of
327 * @propname: Name of the property
328 * @val: The values are stored here or %NULL to return the number of values
329 * @nval: Size of the @val array
331 * Read an array of u64 properties with @propname from @fwnode and store them to
334 * Return: number of values if @val was %NULL,
335 * %0 if the property was found (success),
336 * %-EINVAL if given arguments are not valid,
337 * %-ENODATA if the property does not have a value,
338 * %-EPROTO if the property is not an array of numbers,
339 * %-EOVERFLOW if the size of the property is not as expected,
340 * %-ENXIO if no suitable firmware interface is present.
342 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
343 const char *propname, u64 *val, size_t nval)
345 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
348 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
351 * fwnode_property_read_string_array - return string array property of a node
352 * @fwnode: Firmware node to get the property of
353 * @propname: Name of the property
354 * @val: The values are stored here or %NULL to return the number of values
355 * @nval: Size of the @val array
357 * Read an string list property @propname from the given firmware node and store
358 * them to @val if found.
360 * Return: number of values read on success if @val is non-NULL,
361 * number of values available on success if @val is NULL,
362 * %-EINVAL if given arguments are not valid,
363 * %-ENODATA if the property does not have a value,
364 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
365 * %-EOVERFLOW if the size of the property is not as expected,
366 * %-ENXIO if no suitable firmware interface is present.
368 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
369 const char *propname, const char **val,
374 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
376 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
377 !IS_ERR_OR_NULL(fwnode->secondary))
378 ret = fwnode_call_int_op(fwnode->secondary,
379 property_read_string_array, propname,
383 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
386 * fwnode_property_read_string - return a string property of a firmware node
387 * @fwnode: Firmware node to get the property of
388 * @propname: Name of the property
389 * @val: The value is stored here
391 * Read property @propname from the given firmware node and store the value into
392 * @val if found. The value is checked to be a string.
394 * Return: %0 if the property was found (success),
395 * %-EINVAL if given arguments are not valid,
396 * %-ENODATA if the property does not have a value,
397 * %-EPROTO or %-EILSEQ if the property is not a string,
398 * %-ENXIO if no suitable firmware interface is present.
400 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
401 const char *propname, const char **val)
403 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
405 return ret < 0 ? ret : 0;
407 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
410 * fwnode_property_match_string - find a string in an array and return index
411 * @fwnode: Firmware node to get the property of
412 * @propname: Name of the property holding the array
413 * @string: String to look for
415 * Find a given string in a string array and if it is found return the
418 * Return: %0 if the property was found (success),
419 * %-EINVAL if given arguments are not valid,
420 * %-ENODATA if the property does not have a value,
421 * %-EPROTO if the property is not an array of strings,
422 * %-ENXIO if no suitable firmware interface is present.
424 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
425 const char *propname, const char *string)
430 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
437 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
441 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
445 ret = match_string(values, nval, string);
452 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
455 * fwnode_property_get_reference_args() - Find a reference with arguments
456 * @fwnode: Firmware node where to look for the reference
457 * @prop: The name of the property
458 * @nargs_prop: The name of the property telling the number of
459 * arguments in the referred node. NULL if @nargs is known,
460 * otherwise @nargs is ignored. Only relevant on OF.
461 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
462 * @index: Index of the reference, from zero onwards.
463 * @args: Result structure with reference and integer arguments.
465 * Obtain a reference based on a named property in an fwnode, with
468 * Caller is responsible to call fwnode_handle_put() on the returned
469 * args->fwnode pointer.
471 * Returns: %0 on success
472 * %-ENOENT when the index is out of bounds, the index has an empty
473 * reference or the property was not found
474 * %-EINVAL on parse error
476 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
477 const char *prop, const char *nargs_prop,
478 unsigned int nargs, unsigned int index,
479 struct fwnode_reference_args *args)
483 ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
486 if (ret < 0 && !IS_ERR_OR_NULL(fwnode) &&
487 !IS_ERR_OR_NULL(fwnode->secondary))
488 ret = fwnode_call_int_op(fwnode->secondary, get_reference_args,
489 prop, nargs_prop, nargs, index, args);
493 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
496 * fwnode_find_reference - Find named reference to a fwnode_handle
497 * @fwnode: Firmware node where to look for the reference
498 * @name: The name of the reference
499 * @index: Index of the reference
501 * @index can be used when the named reference holds a table of references.
503 * Returns pointer to the reference fwnode, or ERR_PTR. Caller is responsible to
504 * call fwnode_handle_put() on the returned fwnode pointer.
506 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
510 struct fwnode_reference_args args;
513 ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
515 return ret ? ERR_PTR(ret) : args.fwnode;
517 EXPORT_SYMBOL_GPL(fwnode_find_reference);
520 * fwnode_get_name - Return the name of a node
521 * @fwnode: The firmware node
523 * Returns a pointer to the node name.
525 const char *fwnode_get_name(const struct fwnode_handle *fwnode)
527 return fwnode_call_ptr_op(fwnode, get_name);
529 EXPORT_SYMBOL_GPL(fwnode_get_name);
532 * fwnode_get_name_prefix - Return the prefix of node for printing purposes
533 * @fwnode: The firmware node
535 * Returns the prefix of a node, intended to be printed right before the node.
536 * The prefix works also as a separator between the nodes.
538 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
540 return fwnode_call_ptr_op(fwnode, get_name_prefix);
544 * fwnode_get_parent - Return parent firwmare node
545 * @fwnode: Firmware whose parent is retrieved
547 * Return parent firmware node of the given node if possible or %NULL if no
548 * parent was available.
550 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
552 return fwnode_call_ptr_op(fwnode, get_parent);
554 EXPORT_SYMBOL_GPL(fwnode_get_parent);
557 * fwnode_get_next_parent - Iterate to the node's parent
558 * @fwnode: Firmware whose parent is retrieved
560 * This is like fwnode_get_parent() except that it drops the refcount
561 * on the passed node, making it suitable for iterating through a
564 * Returns a node pointer with refcount incremented, use
565 * fwnode_handle_node() on it when done.
567 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
569 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
571 fwnode_handle_put(fwnode);
575 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
578 * fwnode_get_next_parent_dev - Find device of closest ancestor fwnode
579 * @fwnode: firmware node
581 * Given a firmware node (@fwnode), this function finds its closest ancestor
582 * firmware node that has a corresponding struct device and returns that struct
585 * The caller of this function is expected to call put_device() on the returned
586 * device when they are done.
588 struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
592 fwnode_handle_get(fwnode);
594 fwnode = fwnode_get_next_parent(fwnode);
597 dev = get_dev_from_fwnode(fwnode);
599 fwnode_handle_put(fwnode);
604 * fwnode_count_parents - Return the number of parents a node has
605 * @fwnode: The node the parents of which are to be counted
607 * Returns the number of parents a node has.
609 unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
611 struct fwnode_handle *__fwnode;
614 __fwnode = fwnode_get_parent(fwnode);
616 for (count = 0; __fwnode; count++)
617 __fwnode = fwnode_get_next_parent(__fwnode);
621 EXPORT_SYMBOL_GPL(fwnode_count_parents);
624 * fwnode_get_nth_parent - Return an nth parent of a node
625 * @fwnode: The node the parent of which is requested
626 * @depth: Distance of the parent from the node
628 * Returns the nth parent of a node. If there is no parent at the requested
629 * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
630 * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
632 * The caller is responsible for calling fwnode_handle_put() for the returned
635 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
640 fwnode_handle_get(fwnode);
642 for (i = 0; i < depth && fwnode; i++)
643 fwnode = fwnode_get_next_parent(fwnode);
647 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
650 * fwnode_is_ancestor_of - Test if @test_ancestor is ancestor of @test_child
651 * @test_ancestor: Firmware which is tested for being an ancestor
652 * @test_child: Firmware which is tested for being the child
654 * A node is considered an ancestor of itself too.
656 * Returns true if @test_ancestor is an ancestor of @test_child.
657 * Otherwise, returns false.
659 bool fwnode_is_ancestor_of(struct fwnode_handle *test_ancestor,
660 struct fwnode_handle *test_child)
665 fwnode_handle_get(test_child);
667 if (test_child == test_ancestor) {
668 fwnode_handle_put(test_child);
671 test_child = fwnode_get_next_parent(test_child);
677 * fwnode_get_next_child_node - Return the next child node handle for a node
678 * @fwnode: Firmware node to find the next child node for.
679 * @child: Handle to one of the node's child nodes or a %NULL handle.
681 struct fwnode_handle *
682 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
683 struct fwnode_handle *child)
685 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
687 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
690 * fwnode_get_next_available_child_node - Return the next
691 * available child node handle for a node
692 * @fwnode: Firmware node to find the next child node for.
693 * @child: Handle to one of the node's child nodes or a %NULL handle.
695 struct fwnode_handle *
696 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
697 struct fwnode_handle *child)
699 struct fwnode_handle *next_child = child;
705 next_child = fwnode_get_next_child_node(fwnode, next_child);
708 } while (!fwnode_device_is_available(next_child));
712 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
715 * device_get_next_child_node - Return the next child node handle for a device
716 * @dev: Device to find the next child node for.
717 * @child: Handle to one of the device's child nodes or a null handle.
719 struct fwnode_handle *device_get_next_child_node(struct device *dev,
720 struct fwnode_handle *child)
722 const struct fwnode_handle *fwnode = dev_fwnode(dev);
723 struct fwnode_handle *next;
725 /* Try to find a child in primary fwnode */
726 next = fwnode_get_next_child_node(fwnode, child);
730 /* When no more children in primary, continue with secondary */
731 if (fwnode && !IS_ERR_OR_NULL(fwnode->secondary))
732 next = fwnode_get_next_child_node(fwnode->secondary, child);
736 EXPORT_SYMBOL_GPL(device_get_next_child_node);
739 * fwnode_get_named_child_node - Return first matching named child node handle
740 * @fwnode: Firmware node to find the named child node for.
741 * @childname: String to match child node name against.
743 struct fwnode_handle *
744 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
745 const char *childname)
747 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
749 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
752 * device_get_named_child_node - Return first matching named child node handle
753 * @dev: Device to find the named child node for.
754 * @childname: String to match child node name against.
756 struct fwnode_handle *device_get_named_child_node(struct device *dev,
757 const char *childname)
759 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
761 EXPORT_SYMBOL_GPL(device_get_named_child_node);
764 * fwnode_handle_get - Obtain a reference to a device node
765 * @fwnode: Pointer to the device node to obtain the reference to.
767 * Returns the fwnode handle.
769 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
771 if (!fwnode_has_op(fwnode, get))
774 return fwnode_call_ptr_op(fwnode, get);
776 EXPORT_SYMBOL_GPL(fwnode_handle_get);
779 * fwnode_handle_put - Drop reference to a device node
780 * @fwnode: Pointer to the device node to drop the reference to.
782 * This has to be used when terminating device_for_each_child_node() iteration
783 * with break or return to prevent stale device node references from being left
786 void fwnode_handle_put(struct fwnode_handle *fwnode)
788 fwnode_call_void_op(fwnode, put);
790 EXPORT_SYMBOL_GPL(fwnode_handle_put);
793 * fwnode_device_is_available - check if a device is available for use
794 * @fwnode: Pointer to the fwnode of the device.
796 * For fwnode node types that don't implement the .device_is_available()
797 * operation, this function returns true.
799 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
801 if (!fwnode_has_op(fwnode, device_is_available))
804 return fwnode_call_bool_op(fwnode, device_is_available);
806 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
809 * device_get_child_node_count - return the number of child nodes for device
810 * @dev: Device to cound the child nodes for
812 unsigned int device_get_child_node_count(struct device *dev)
814 struct fwnode_handle *child;
815 unsigned int count = 0;
817 device_for_each_child_node(dev, child)
822 EXPORT_SYMBOL_GPL(device_get_child_node_count);
824 bool device_dma_supported(struct device *dev)
826 const struct fwnode_handle *fwnode = dev_fwnode(dev);
828 /* For DT, this is always supported.
829 * For ACPI, this depends on CCA, which
830 * is determined by the acpi_dma_supported().
832 if (is_of_node(fwnode))
835 return acpi_dma_supported(to_acpi_device_node(fwnode));
837 EXPORT_SYMBOL_GPL(device_dma_supported);
839 enum dev_dma_attr device_get_dma_attr(struct device *dev)
841 const struct fwnode_handle *fwnode = dev_fwnode(dev);
842 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
844 if (is_of_node(fwnode)) {
845 if (of_dma_is_coherent(to_of_node(fwnode)))
846 attr = DEV_DMA_COHERENT;
848 attr = DEV_DMA_NON_COHERENT;
850 attr = acpi_get_dma_attr(to_acpi_device_node(fwnode));
854 EXPORT_SYMBOL_GPL(device_get_dma_attr);
857 * fwnode_get_phy_mode - Get phy mode for given firmware node
858 * @fwnode: Pointer to the given node
860 * The function gets phy interface string from property 'phy-mode' or
861 * 'phy-connection-type', and return its index in phy_modes table, or errno in
864 int fwnode_get_phy_mode(struct fwnode_handle *fwnode)
869 err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
871 err = fwnode_property_read_string(fwnode,
872 "phy-connection-type", &pm);
876 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
877 if (!strcasecmp(pm, phy_modes(i)))
882 EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
885 * device_get_phy_mode - Get phy mode for given device
886 * @dev: Pointer to the given device
888 * The function gets phy interface string from property 'phy-mode' or
889 * 'phy-connection-type', and return its index in phy_modes table, or errno in
892 int device_get_phy_mode(struct device *dev)
894 return fwnode_get_phy_mode(dev_fwnode(dev));
896 EXPORT_SYMBOL_GPL(device_get_phy_mode);
899 * fwnode_irq_get - Get IRQ directly from a fwnode
900 * @fwnode: Pointer to the firmware node
901 * @index: Zero-based index of the IRQ
903 * Returns Linux IRQ number on success. Other values are determined
904 * accordingly to acpi_/of_ irq_get() operation.
906 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
911 if (is_of_node(fwnode))
912 return of_irq_get(to_of_node(fwnode), index);
914 ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
920 EXPORT_SYMBOL(fwnode_irq_get);
923 * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
924 * @fwnode: Pointer to the parent firmware node
925 * @prev: Previous endpoint node or %NULL to get the first
927 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
930 struct fwnode_handle *
931 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
932 struct fwnode_handle *prev)
934 const struct fwnode_handle *parent;
935 struct fwnode_handle *ep;
938 * If this function is in a loop and the previous iteration returned
939 * an endpoint from fwnode->secondary, then we need to use the secondary
940 * as parent rather than @fwnode.
943 parent = fwnode_graph_get_port_parent(prev);
947 ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
949 if (IS_ERR_OR_NULL(ep) &&
950 !IS_ERR_OR_NULL(parent) && !IS_ERR_OR_NULL(parent->secondary))
951 ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);
955 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
958 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
959 * @endpoint: Endpoint firmware node of the port
961 * Return: the firmware node of the device the @endpoint belongs to.
963 struct fwnode_handle *
964 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
966 struct fwnode_handle *port, *parent;
968 port = fwnode_get_parent(endpoint);
969 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
971 fwnode_handle_put(port);
975 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
978 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
979 * @fwnode: Endpoint firmware node pointing to the remote endpoint
981 * Extracts firmware node of a remote device the @fwnode points to.
983 struct fwnode_handle *
984 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
986 struct fwnode_handle *endpoint, *parent;
988 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
989 parent = fwnode_graph_get_port_parent(endpoint);
991 fwnode_handle_put(endpoint);
995 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
998 * fwnode_graph_get_remote_port - Return fwnode of a remote port
999 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1001 * Extracts firmware node of a remote port the @fwnode points to.
1003 struct fwnode_handle *
1004 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1006 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1008 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1011 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1012 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1014 * Extracts firmware node of a remote endpoint the @fwnode points to.
1016 struct fwnode_handle *
1017 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1019 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1021 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1023 static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
1025 struct fwnode_handle *dev_node;
1028 dev_node = fwnode_graph_get_remote_port_parent(ep);
1029 available = fwnode_device_is_available(dev_node);
1030 fwnode_handle_put(dev_node);
1036 * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1037 * @fwnode: parent fwnode_handle containing the graph
1038 * @port: identifier of the port node
1039 * @endpoint: identifier of the endpoint node under the port node
1040 * @flags: fwnode lookup flags
1042 * Return the fwnode handle of the local endpoint corresponding the port and
1043 * endpoint IDs or NULL if not found.
1045 * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1046 * has not been found, look for the closest endpoint ID greater than the
1047 * specified one and return the endpoint that corresponds to it, if present.
1049 * Does not return endpoints that belong to disabled devices or endpoints that
1050 * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1052 * The returned endpoint needs to be released by calling fwnode_handle_put() on
1053 * it when it is not needed any more.
1055 struct fwnode_handle *
1056 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1057 u32 port, u32 endpoint, unsigned long flags)
1059 struct fwnode_handle *ep, *best_ep = NULL;
1060 unsigned int best_ep_id = 0;
1061 bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1062 bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1064 fwnode_graph_for_each_endpoint(fwnode, ep) {
1065 struct fwnode_endpoint fwnode_ep = { 0 };
1068 if (enabled_only && !fwnode_graph_remote_available(ep))
1071 ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1075 if (fwnode_ep.port != port)
1078 if (fwnode_ep.id == endpoint)
1085 * If the endpoint that has just been found is not the first
1086 * matching one and the ID of the one found previously is closer
1087 * to the requested endpoint ID, skip it.
1089 if (fwnode_ep.id < endpoint ||
1090 (best_ep && best_ep_id < fwnode_ep.id))
1093 fwnode_handle_put(best_ep);
1094 best_ep = fwnode_handle_get(ep);
1095 best_ep_id = fwnode_ep.id;
1100 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1103 * fwnode_graph_get_endpoint_count - Count endpoints on a device node
1104 * @fwnode: The node related to a device
1105 * @flags: fwnode lookup flags
1106 * Count endpoints in a device node.
1108 * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
1109 * and endpoints connected to disabled devices are counted.
1111 unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
1112 unsigned long flags)
1114 struct fwnode_handle *ep;
1115 unsigned int count = 0;
1117 fwnode_graph_for_each_endpoint(fwnode, ep) {
1118 if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
1119 fwnode_graph_remote_available(ep))
1125 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
1128 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1129 * @fwnode: pointer to endpoint fwnode_handle
1130 * @endpoint: pointer to the fwnode endpoint data structure
1132 * Parse @fwnode representing a graph endpoint node and store the
1133 * information in @endpoint. The caller must hold a reference to
1136 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1137 struct fwnode_endpoint *endpoint)
1139 memset(endpoint, 0, sizeof(*endpoint));
1141 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1143 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1145 const void *device_get_match_data(struct device *dev)
1147 return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1149 EXPORT_SYMBOL_GPL(device_get_match_data);
1152 fwnode_graph_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
1153 void *data, devcon_match_fn_t match)
1155 struct fwnode_handle *node;
1156 struct fwnode_handle *ep;
1159 fwnode_graph_for_each_endpoint(fwnode, ep) {
1160 node = fwnode_graph_get_remote_port_parent(ep);
1161 if (!fwnode_device_is_available(node)) {
1162 fwnode_handle_put(node);
1166 ret = match(node, con_id, data);
1167 fwnode_handle_put(node);
1169 fwnode_handle_put(ep);
1177 fwnode_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
1178 void *data, devcon_match_fn_t match)
1180 struct fwnode_handle *node;
1184 for (i = 0; ; i++) {
1185 node = fwnode_find_reference(fwnode, con_id, i);
1189 ret = match(node, NULL, data);
1190 fwnode_handle_put(node);
1199 * fwnode_connection_find_match - Find connection from a device node
1200 * @fwnode: Device node with the connection
1201 * @con_id: Identifier for the connection
1202 * @data: Data for the match function
1203 * @match: Function to check and convert the connection description
1205 * Find a connection with unique identifier @con_id between @fwnode and another
1206 * device node. @match will be used to convert the connection description to
1207 * data the caller is expecting to be returned.
1209 void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
1210 const char *con_id, void *data,
1211 devcon_match_fn_t match)
1215 if (!fwnode || !match)
1218 ret = fwnode_graph_devcon_match(fwnode, con_id, data, match);
1222 return fwnode_devcon_match(fwnode, con_id, data, match);
1224 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);