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>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/acpi.h>
15 #include <linux/export.h>
16 #include <linux/kernel.h>
18 #include <linux/of_address.h>
19 #include <linux/of_graph.h>
20 #include <linux/property.h>
21 #include <linux/etherdevice.h>
22 #include <linux/phy.h>
26 struct fwnode_handle fwnode;
27 const struct property_entry *properties;
30 static const struct fwnode_operations pset_fwnode_ops;
32 static inline bool is_pset_node(const struct fwnode_handle *fwnode)
34 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
37 #define to_pset_node(__fwnode) \
39 typeof(__fwnode) __to_pset_node_fwnode = __fwnode; \
41 is_pset_node(__to_pset_node_fwnode) ? \
42 container_of(__to_pset_node_fwnode, \
43 struct property_set, fwnode) : \
47 static const struct property_entry *
48 pset_prop_get(const struct property_set *pset, const char *name)
50 const struct property_entry *prop;
52 if (!pset || !pset->properties)
55 for (prop = pset->properties; prop->name; prop++)
56 if (!strcmp(name, prop->name))
62 static const void *pset_prop_find(const struct property_set *pset,
63 const char *propname, size_t length)
65 const struct property_entry *prop;
68 prop = pset_prop_get(pset, propname);
70 return ERR_PTR(-EINVAL);
72 pointer = prop->pointer.raw_data;
74 pointer = &prop->value.raw_data;
76 return ERR_PTR(-ENODATA);
77 if (length > prop->length)
78 return ERR_PTR(-EOVERFLOW);
82 static int pset_prop_read_u8_array(const struct property_set *pset,
84 u8 *values, size_t nval)
87 size_t length = nval * sizeof(*values);
89 pointer = pset_prop_find(pset, propname, length);
91 return PTR_ERR(pointer);
93 memcpy(values, pointer, length);
97 static int pset_prop_read_u16_array(const struct property_set *pset,
99 u16 *values, size_t nval)
102 size_t length = nval * sizeof(*values);
104 pointer = pset_prop_find(pset, propname, length);
106 return PTR_ERR(pointer);
108 memcpy(values, pointer, length);
112 static int pset_prop_read_u32_array(const struct property_set *pset,
113 const char *propname,
114 u32 *values, size_t nval)
117 size_t length = nval * sizeof(*values);
119 pointer = pset_prop_find(pset, propname, length);
121 return PTR_ERR(pointer);
123 memcpy(values, pointer, length);
127 static int pset_prop_read_u64_array(const struct property_set *pset,
128 const char *propname,
129 u64 *values, size_t nval)
132 size_t length = nval * sizeof(*values);
134 pointer = pset_prop_find(pset, propname, length);
136 return PTR_ERR(pointer);
138 memcpy(values, pointer, length);
142 static int pset_prop_count_elems_of_size(const struct property_set *pset,
143 const char *propname, size_t length)
145 const struct property_entry *prop;
147 prop = pset_prop_get(pset, propname);
151 return prop->length / length;
154 static int pset_prop_read_string_array(const struct property_set *pset,
155 const char *propname,
156 const char **strings, size_t nval)
158 const struct property_entry *prop;
160 size_t array_len, length;
162 /* Find out the array length. */
163 prop = pset_prop_get(pset, propname);
168 /* The array length for a non-array string property is 1. */
171 /* Find the length of an array. */
172 array_len = pset_prop_count_elems_of_size(pset, propname,
173 sizeof(const char *));
175 /* Return how many there are if strings is NULL. */
179 array_len = min(nval, array_len);
180 length = array_len * sizeof(*strings);
182 pointer = pset_prop_find(pset, propname, length);
184 return PTR_ERR(pointer);
186 memcpy(strings, pointer, length);
191 struct fwnode_handle *dev_fwnode(struct device *dev)
193 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
194 &dev->of_node->fwnode : dev->fwnode;
196 EXPORT_SYMBOL_GPL(dev_fwnode);
198 static bool pset_fwnode_property_present(const struct fwnode_handle *fwnode,
199 const char *propname)
201 return !!pset_prop_get(to_pset_node(fwnode), propname);
204 static int pset_fwnode_read_int_array(const struct fwnode_handle *fwnode,
205 const char *propname,
206 unsigned int elem_size, void *val,
209 const struct property_set *node = to_pset_node(fwnode);
212 return pset_prop_count_elems_of_size(node, propname, elem_size);
216 return pset_prop_read_u8_array(node, propname, val, nval);
218 return pset_prop_read_u16_array(node, propname, val, nval);
220 return pset_prop_read_u32_array(node, propname, val, nval);
222 return pset_prop_read_u64_array(node, propname, val, nval);
229 pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
230 const char *propname,
231 const char **val, size_t nval)
233 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
237 static const struct fwnode_operations pset_fwnode_ops = {
238 .property_present = pset_fwnode_property_present,
239 .property_read_int_array = pset_fwnode_read_int_array,
240 .property_read_string_array = pset_fwnode_property_read_string_array,
244 * device_property_present - check if a property of a device is present
245 * @dev: Device whose property is being checked
246 * @propname: Name of the property
248 * Check if property @propname is present in the device firmware description.
250 bool device_property_present(struct device *dev, const char *propname)
252 return fwnode_property_present(dev_fwnode(dev), propname);
254 EXPORT_SYMBOL_GPL(device_property_present);
257 * fwnode_property_present - check if a property of a firmware node is present
258 * @fwnode: Firmware node whose property to check
259 * @propname: Name of the property
261 bool fwnode_property_present(const struct fwnode_handle *fwnode,
262 const char *propname)
266 ret = fwnode_call_bool_op(fwnode, property_present, propname);
267 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
268 !IS_ERR_OR_NULL(fwnode->secondary))
269 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
273 EXPORT_SYMBOL_GPL(fwnode_property_present);
276 * device_property_read_u8_array - return a u8 array property of a device
277 * @dev: Device to get the property of
278 * @propname: Name of the property
279 * @val: The values are stored here or %NULL to return the number of values
280 * @nval: Size of the @val array
282 * Function reads an array of u8 properties with @propname from the device
283 * firmware description and stores them to @val if found.
285 * Return: number of values if @val was %NULL,
286 * %0 if the property was found (success),
287 * %-EINVAL if given arguments are not valid,
288 * %-ENODATA if the property does not have a value,
289 * %-EPROTO if the property is not an array of numbers,
290 * %-EOVERFLOW if the size of the property is not as expected.
291 * %-ENXIO if no suitable firmware interface is present.
293 int device_property_read_u8_array(struct device *dev, const char *propname,
294 u8 *val, size_t nval)
296 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
298 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
301 * device_property_read_u16_array - return a u16 array property of a device
302 * @dev: Device to get the property of
303 * @propname: Name of the property
304 * @val: The values are stored here or %NULL to return the number of values
305 * @nval: Size of the @val array
307 * Function reads an array of u16 properties with @propname from the device
308 * firmware description and stores them to @val if found.
310 * Return: number of values if @val was %NULL,
311 * %0 if the property was found (success),
312 * %-EINVAL if given arguments are not valid,
313 * %-ENODATA if the property does not have a value,
314 * %-EPROTO if the property is not an array of numbers,
315 * %-EOVERFLOW if the size of the property is not as expected.
316 * %-ENXIO if no suitable firmware interface is present.
318 int device_property_read_u16_array(struct device *dev, const char *propname,
319 u16 *val, size_t nval)
321 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
323 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
326 * device_property_read_u32_array - return a u32 array property of a device
327 * @dev: Device to get the property of
328 * @propname: Name of the property
329 * @val: The values are stored here or %NULL to return the number of values
330 * @nval: Size of the @val array
332 * Function reads an array of u32 properties with @propname from the device
333 * firmware description and stores them to @val if found.
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 device_property_read_u32_array(struct device *dev, const char *propname,
344 u32 *val, size_t nval)
346 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
348 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
351 * device_property_read_u64_array - return a u64 array property of a device
352 * @dev: Device 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 * Function reads an array of u64 properties with @propname from the device
358 * firmware description and stores them to @val if found.
360 * Return: number of values if @val was %NULL,
361 * %0 if the property was found (success),
362 * %-EINVAL if given arguments are not valid,
363 * %-ENODATA if the property does not have a value,
364 * %-EPROTO if the property is not an array of numbers,
365 * %-EOVERFLOW if the size of the property is not as expected.
366 * %-ENXIO if no suitable firmware interface is present.
368 int device_property_read_u64_array(struct device *dev, const char *propname,
369 u64 *val, size_t nval)
371 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
373 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
376 * device_property_read_string_array - return a string array property of device
377 * @dev: Device to get the property of
378 * @propname: Name of the property
379 * @val: The values are stored here or %NULL to return the number of values
380 * @nval: Size of the @val array
382 * Function reads an array of string properties with @propname from the device
383 * firmware description and stores them to @val if found.
385 * Return: number of values read on success if @val is non-NULL,
386 * number of values available on success if @val is NULL,
387 * %-EINVAL if given arguments are not valid,
388 * %-ENODATA if the property does not have a value,
389 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
390 * %-EOVERFLOW if the size of the property is not as expected.
391 * %-ENXIO if no suitable firmware interface is present.
393 int device_property_read_string_array(struct device *dev, const char *propname,
394 const char **val, size_t nval)
396 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
398 EXPORT_SYMBOL_GPL(device_property_read_string_array);
401 * device_property_read_string - return a string property of a device
402 * @dev: Device to get the property of
403 * @propname: Name of the property
404 * @val: The value is stored here
406 * Function reads property @propname from the device firmware description and
407 * stores the value into @val if found. The value is checked to be a string.
409 * Return: %0 if the property was found (success),
410 * %-EINVAL if given arguments are not valid,
411 * %-ENODATA if the property does not have a value,
412 * %-EPROTO or %-EILSEQ if the property type is not a string.
413 * %-ENXIO if no suitable firmware interface is present.
415 int device_property_read_string(struct device *dev, const char *propname,
418 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
420 EXPORT_SYMBOL_GPL(device_property_read_string);
423 * device_property_match_string - find a string in an array and return index
424 * @dev: Device to get the property of
425 * @propname: Name of the property holding the array
426 * @string: String to look for
428 * Find a given string in a string array and if it is found return the
431 * Return: %0 if the property was found (success),
432 * %-EINVAL if given arguments are not valid,
433 * %-ENODATA if the property does not have a value,
434 * %-EPROTO if the property is not an array of strings,
435 * %-ENXIO if no suitable firmware interface is present.
437 int device_property_match_string(struct device *dev, const char *propname,
440 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
442 EXPORT_SYMBOL_GPL(device_property_match_string);
444 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
445 const char *propname,
446 unsigned int elem_size, void *val,
451 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
452 elem_size, val, nval);
453 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
454 !IS_ERR_OR_NULL(fwnode->secondary))
455 ret = fwnode_call_int_op(
456 fwnode->secondary, property_read_int_array, propname,
457 elem_size, val, nval);
463 * fwnode_property_read_u8_array - return a u8 array property of firmware node
464 * @fwnode: Firmware node to get the property of
465 * @propname: Name of the property
466 * @val: The values are stored here or %NULL to return the number of values
467 * @nval: Size of the @val array
469 * Read an array of u8 properties with @propname from @fwnode and stores them to
472 * Return: number of values if @val was %NULL,
473 * %0 if the property was found (success),
474 * %-EINVAL if given arguments are not valid,
475 * %-ENODATA if the property does not have a value,
476 * %-EPROTO if the property is not an array of numbers,
477 * %-EOVERFLOW if the size of the property is not as expected,
478 * %-ENXIO if no suitable firmware interface is present.
480 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
481 const char *propname, u8 *val, size_t nval)
483 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
486 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
489 * fwnode_property_read_u16_array - return a u16 array property of firmware node
490 * @fwnode: Firmware node to get the property of
491 * @propname: Name of the property
492 * @val: The values are stored here or %NULL to return the number of values
493 * @nval: Size of the @val array
495 * Read an array of u16 properties with @propname from @fwnode and store them to
498 * Return: number of values if @val was %NULL,
499 * %0 if the property was found (success),
500 * %-EINVAL if given arguments are not valid,
501 * %-ENODATA if the property does not have a value,
502 * %-EPROTO if the property is not an array of numbers,
503 * %-EOVERFLOW if the size of the property is not as expected,
504 * %-ENXIO if no suitable firmware interface is present.
506 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
507 const char *propname, u16 *val, size_t nval)
509 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
512 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
515 * fwnode_property_read_u32_array - return a u32 array property of firmware node
516 * @fwnode: Firmware node to get the property of
517 * @propname: Name of the property
518 * @val: The values are stored here or %NULL to return the number of values
519 * @nval: Size of the @val array
521 * Read an array of u32 properties with @propname from @fwnode store them to
524 * Return: number of values if @val was %NULL,
525 * %0 if the property was found (success),
526 * %-EINVAL if given arguments are not valid,
527 * %-ENODATA if the property does not have a value,
528 * %-EPROTO if the property is not an array of numbers,
529 * %-EOVERFLOW if the size of the property is not as expected,
530 * %-ENXIO if no suitable firmware interface is present.
532 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
533 const char *propname, u32 *val, size_t nval)
535 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
538 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
541 * fwnode_property_read_u64_array - return a u64 array property firmware node
542 * @fwnode: Firmware node to get the property of
543 * @propname: Name of the property
544 * @val: The values are stored here or %NULL to return the number of values
545 * @nval: Size of the @val array
547 * Read an array of u64 properties with @propname from @fwnode and store them to
550 * Return: number of values if @val was %NULL,
551 * %0 if the property was found (success),
552 * %-EINVAL if given arguments are not valid,
553 * %-ENODATA if the property does not have a value,
554 * %-EPROTO if the property is not an array of numbers,
555 * %-EOVERFLOW if the size of the property is not as expected,
556 * %-ENXIO if no suitable firmware interface is present.
558 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
559 const char *propname, u64 *val, size_t nval)
561 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
564 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
567 * fwnode_property_read_string_array - return string array property of a node
568 * @fwnode: Firmware node to get the property of
569 * @propname: Name of the property
570 * @val: The values are stored here or %NULL to return the number of values
571 * @nval: Size of the @val array
573 * Read an string list property @propname from the given firmware node and store
574 * them to @val if found.
576 * Return: number of values read on success if @val is non-NULL,
577 * number of values available on success if @val is NULL,
578 * %-EINVAL if given arguments are not valid,
579 * %-ENODATA if the property does not have a value,
580 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
581 * %-EOVERFLOW if the size of the property is not as expected,
582 * %-ENXIO if no suitable firmware interface is present.
584 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
585 const char *propname, const char **val,
590 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
592 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
593 !IS_ERR_OR_NULL(fwnode->secondary))
594 ret = fwnode_call_int_op(fwnode->secondary,
595 property_read_string_array, propname,
599 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
602 * fwnode_property_read_string - return a string property of a firmware node
603 * @fwnode: Firmware node to get the property of
604 * @propname: Name of the property
605 * @val: The value is stored here
607 * Read property @propname from the given firmware node and store the value into
608 * @val if found. The value is checked to be a string.
610 * Return: %0 if the property was found (success),
611 * %-EINVAL if given arguments are not valid,
612 * %-ENODATA if the property does not have a value,
613 * %-EPROTO or %-EILSEQ if the property is not a string,
614 * %-ENXIO if no suitable firmware interface is present.
616 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
617 const char *propname, const char **val)
619 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
621 return ret < 0 ? ret : 0;
623 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
626 * fwnode_property_match_string - find a string in an array and return index
627 * @fwnode: Firmware node to get the property of
628 * @propname: Name of the property holding the array
629 * @string: String to look for
631 * Find a given string in a string array and if it is found return the
634 * Return: %0 if the property was found (success),
635 * %-EINVAL if given arguments are not valid,
636 * %-ENODATA if the property does not have a value,
637 * %-EPROTO if the property is not an array of strings,
638 * %-ENXIO if no suitable firmware interface is present.
640 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
641 const char *propname, const char *string)
646 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
653 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
657 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
661 ret = match_string(values, nval, string);
668 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
671 * fwnode_property_get_reference_args() - Find a reference with arguments
672 * @fwnode: Firmware node where to look for the reference
673 * @prop: The name of the property
674 * @nargs_prop: The name of the property telling the number of
675 * arguments in the referred node. NULL if @nargs is known,
676 * otherwise @nargs is ignored. Only relevant on OF.
677 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
678 * @index: Index of the reference, from zero onwards.
679 * @args: Result structure with reference and integer arguments.
681 * Obtain a reference based on a named property in an fwnode, with
684 * Caller is responsible to call fwnode_handle_put() on the returned
685 * args->fwnode pointer.
687 * Returns: %0 on success
688 * %-ENOENT when the index is out of bounds, the index has an empty
689 * reference or the property was not found
690 * %-EINVAL on parse error
692 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
693 const char *prop, const char *nargs_prop,
694 unsigned int nargs, unsigned int index,
695 struct fwnode_reference_args *args)
697 return fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
700 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
702 static int property_copy_string_array(struct property_entry *dst,
703 const struct property_entry *src)
706 size_t nval = src->length / sizeof(*d);
709 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
713 for (i = 0; i < nval; i++) {
714 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
715 if (!d[i] && src->pointer.str[i]) {
723 dst->pointer.raw_data = d;
727 static int property_entry_copy_data(struct property_entry *dst,
728 const struct property_entry *src)
732 dst->name = kstrdup(src->name, GFP_KERNEL);
742 if (src->is_string) {
743 error = property_copy_string_array(dst, src);
747 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
748 src->length, GFP_KERNEL);
749 if (!dst->pointer.raw_data) {
754 } else if (src->is_string) {
755 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
756 if (!dst->value.str && src->value.str) {
761 dst->value.raw_data = src->value.raw_data;
764 dst->length = src->length;
765 dst->is_array = src->is_array;
766 dst->is_string = src->is_string;
775 static void property_entry_free_data(const struct property_entry *p)
780 if (p->is_string && p->pointer.str) {
781 nval = p->length / sizeof(const char *);
782 for (i = 0; i < nval; i++)
783 kfree(p->pointer.str[i]);
785 kfree(p->pointer.raw_data);
786 } else if (p->is_string) {
793 * property_entries_dup - duplicate array of properties
794 * @properties: array of properties to copy
796 * This function creates a deep copy of the given NULL-terminated array
797 * of property entries.
799 struct property_entry *
800 property_entries_dup(const struct property_entry *properties)
802 struct property_entry *p;
805 while (properties[n].name)
808 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
810 return ERR_PTR(-ENOMEM);
812 for (i = 0; i < n; i++) {
813 int ret = property_entry_copy_data(&p[i], &properties[i]);
816 property_entry_free_data(&p[i]);
824 EXPORT_SYMBOL_GPL(property_entries_dup);
827 * property_entries_free - free previously allocated array of properties
828 * @properties: array of properties to destroy
830 * This function frees given NULL-terminated array of property entries,
831 * along with their data.
833 void property_entries_free(const struct property_entry *properties)
835 const struct property_entry *p;
837 for (p = properties; p->name; p++)
838 property_entry_free_data(p);
842 EXPORT_SYMBOL_GPL(property_entries_free);
845 * pset_free_set - releases memory allocated for copied property set
846 * @pset: Property set to release
848 * Function takes previously copied property set and releases all the
849 * memory allocated to it.
851 static void pset_free_set(struct property_set *pset)
856 property_entries_free(pset->properties);
861 * pset_copy_set - copies property set
862 * @pset: Property set to copy
864 * This function takes a deep copy of the given property set and returns
865 * pointer to the copy. Call device_free_property_set() to free resources
866 * allocated in this function.
868 * Return: Pointer to the new property set or error pointer.
870 static struct property_set *pset_copy_set(const struct property_set *pset)
872 struct property_entry *properties;
873 struct property_set *p;
875 p = kzalloc(sizeof(*p), GFP_KERNEL);
877 return ERR_PTR(-ENOMEM);
879 properties = property_entries_dup(pset->properties);
880 if (IS_ERR(properties)) {
882 return ERR_CAST(properties);
885 p->properties = properties;
890 * device_remove_properties - Remove properties from a device object.
891 * @dev: Device whose properties to remove.
893 * The function removes properties previously associated to the device
894 * secondary firmware node with device_add_properties(). Memory allocated
895 * to the properties will also be released.
897 void device_remove_properties(struct device *dev)
899 struct fwnode_handle *fwnode;
900 struct property_set *pset;
902 fwnode = dev_fwnode(dev);
906 * Pick either primary or secondary node depending which one holds
907 * the pset. If there is no real firmware node (ACPI/DT) primary
908 * will hold the pset.
910 pset = to_pset_node(fwnode);
912 set_primary_fwnode(dev, NULL);
914 pset = to_pset_node(fwnode->secondary);
915 if (pset && dev == pset->dev)
916 set_secondary_fwnode(dev, NULL);
918 if (pset && dev == pset->dev)
921 EXPORT_SYMBOL_GPL(device_remove_properties);
924 * device_add_properties - Add a collection of properties to a device object.
925 * @dev: Device to add properties to.
926 * @properties: Collection of properties to add.
928 * Associate a collection of device properties represented by @properties with
929 * @dev as its secondary firmware node. The function takes a copy of
932 int device_add_properties(struct device *dev,
933 const struct property_entry *properties)
935 struct property_set *p, pset;
940 pset.properties = properties;
942 p = pset_copy_set(&pset);
946 p->fwnode.ops = &pset_fwnode_ops;
947 set_secondary_fwnode(dev, &p->fwnode);
951 EXPORT_SYMBOL_GPL(device_add_properties);
954 * fwnode_get_next_parent - Iterate to the node's parent
955 * @fwnode: Firmware whose parent is retrieved
957 * This is like fwnode_get_parent() except that it drops the refcount
958 * on the passed node, making it suitable for iterating through a
961 * Returns a node pointer with refcount incremented, use
962 * fwnode_handle_node() on it when done.
964 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
966 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
968 fwnode_handle_put(fwnode);
972 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
975 * fwnode_get_parent - Return parent firwmare node
976 * @fwnode: Firmware whose parent is retrieved
978 * Return parent firmware node of the given node if possible or %NULL if no
979 * parent was available.
981 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
983 return fwnode_call_ptr_op(fwnode, get_parent);
985 EXPORT_SYMBOL_GPL(fwnode_get_parent);
988 * fwnode_get_next_child_node - Return the next child node handle for a node
989 * @fwnode: Firmware node to find the next child node for.
990 * @child: Handle to one of the node's child nodes or a %NULL handle.
992 struct fwnode_handle *
993 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
994 struct fwnode_handle *child)
996 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
998 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
1001 * device_get_next_child_node - Return the next child node handle for a device
1002 * @dev: Device to find the next child node for.
1003 * @child: Handle to one of the device's child nodes or a null handle.
1005 struct fwnode_handle *device_get_next_child_node(struct device *dev,
1006 struct fwnode_handle *child)
1008 struct acpi_device *adev = ACPI_COMPANION(dev);
1009 struct fwnode_handle *fwnode = NULL;
1012 fwnode = &dev->of_node->fwnode;
1014 fwnode = acpi_fwnode_handle(adev);
1016 return fwnode_get_next_child_node(fwnode, child);
1018 EXPORT_SYMBOL_GPL(device_get_next_child_node);
1021 * fwnode_get_named_child_node - Return first matching named child node handle
1022 * @fwnode: Firmware node to find the named child node for.
1023 * @childname: String to match child node name against.
1025 struct fwnode_handle *
1026 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
1027 const char *childname)
1029 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
1031 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1034 * device_get_named_child_node - Return first matching named child node handle
1035 * @dev: Device to find the named child node for.
1036 * @childname: String to match child node name against.
1038 struct fwnode_handle *device_get_named_child_node(struct device *dev,
1039 const char *childname)
1041 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1043 EXPORT_SYMBOL_GPL(device_get_named_child_node);
1046 * fwnode_handle_get - Obtain a reference to a device node
1047 * @fwnode: Pointer to the device node to obtain the reference to.
1049 * Returns the fwnode handle.
1051 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
1053 if (!fwnode_has_op(fwnode, get))
1056 return fwnode_call_ptr_op(fwnode, get);
1058 EXPORT_SYMBOL_GPL(fwnode_handle_get);
1061 * fwnode_handle_put - Drop reference to a device node
1062 * @fwnode: Pointer to the device node to drop the reference to.
1064 * This has to be used when terminating device_for_each_child_node() iteration
1065 * with break or return to prevent stale device node references from being left
1068 void fwnode_handle_put(struct fwnode_handle *fwnode)
1070 fwnode_call_void_op(fwnode, put);
1072 EXPORT_SYMBOL_GPL(fwnode_handle_put);
1075 * fwnode_device_is_available - check if a device is available for use
1076 * @fwnode: Pointer to the fwnode of the device.
1078 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
1080 return fwnode_call_bool_op(fwnode, device_is_available);
1082 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1085 * device_get_child_node_count - return the number of child nodes for device
1086 * @dev: Device to cound the child nodes for
1088 unsigned int device_get_child_node_count(struct device *dev)
1090 struct fwnode_handle *child;
1091 unsigned int count = 0;
1093 device_for_each_child_node(dev, child)
1098 EXPORT_SYMBOL_GPL(device_get_child_node_count);
1100 bool device_dma_supported(struct device *dev)
1102 /* For DT, this is always supported.
1103 * For ACPI, this depends on CCA, which
1104 * is determined by the acpi_dma_supported().
1106 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1109 return acpi_dma_supported(ACPI_COMPANION(dev));
1111 EXPORT_SYMBOL_GPL(device_dma_supported);
1113 enum dev_dma_attr device_get_dma_attr(struct device *dev)
1115 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1117 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1118 if (of_dma_is_coherent(dev->of_node))
1119 attr = DEV_DMA_COHERENT;
1121 attr = DEV_DMA_NON_COHERENT;
1123 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1127 EXPORT_SYMBOL_GPL(device_get_dma_attr);
1130 * device_get_phy_mode - Get phy mode for given device
1131 * @dev: Pointer to the given device
1133 * The function gets phy interface string from property 'phy-mode' or
1134 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1137 int device_get_phy_mode(struct device *dev)
1142 err = device_property_read_string(dev, "phy-mode", &pm);
1144 err = device_property_read_string(dev,
1145 "phy-connection-type", &pm);
1149 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1150 if (!strcasecmp(pm, phy_modes(i)))
1155 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1157 static void *device_get_mac_addr(struct device *dev,
1158 const char *name, char *addr,
1161 int ret = device_property_read_u8_array(dev, name, addr, alen);
1163 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
1169 * device_get_mac_address - Get the MAC for a given device
1170 * @dev: Pointer to the device
1171 * @addr: Address of buffer to store the MAC in
1172 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1174 * Search the firmware node for the best MAC address to use. 'mac-address' is
1175 * checked first, because that is supposed to contain to "most recent" MAC
1176 * address. If that isn't set, then 'local-mac-address' is checked next,
1177 * because that is the default address. If that isn't set, then the obsolete
1178 * 'address' is checked, just in case we're using an old device tree.
1180 * Note that the 'address' property is supposed to contain a virtual address of
1181 * the register set, but some DTS files have redefined that property to be the
1184 * All-zero MAC addresses are rejected, because those could be properties that
1185 * exist in the firmware tables, but were not updated by the firmware. For
1186 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1187 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1188 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1189 * exists but is all zeros.
1191 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1195 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1199 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1203 return device_get_mac_addr(dev, "address", addr, alen);
1205 EXPORT_SYMBOL(device_get_mac_address);
1208 * device_graph_get_next_endpoint - Get next endpoint firmware node
1209 * @fwnode: Pointer to the parent firmware node
1210 * @prev: Previous endpoint node or %NULL to get the first
1212 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1215 struct fwnode_handle *
1216 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1217 struct fwnode_handle *prev)
1219 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
1221 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1224 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1225 * @endpoint: Endpoint firmware node of the port
1227 * Return: the firmware node of the device the @endpoint belongs to.
1229 struct fwnode_handle *
1230 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1232 struct fwnode_handle *port, *parent;
1234 port = fwnode_get_parent(endpoint);
1235 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1237 fwnode_handle_put(port);
1241 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1244 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1245 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1247 * Extracts firmware node of a remote device the @fwnode points to.
1249 struct fwnode_handle *
1250 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1252 struct fwnode_handle *endpoint, *parent;
1254 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1255 parent = fwnode_graph_get_port_parent(endpoint);
1257 fwnode_handle_put(endpoint);
1261 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1264 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1265 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1267 * Extracts firmware node of a remote port the @fwnode points to.
1269 struct fwnode_handle *
1270 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1272 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1274 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1277 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1278 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1280 * Extracts firmware node of a remote endpoint the @fwnode points to.
1282 struct fwnode_handle *
1283 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1285 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1287 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1290 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1291 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1292 * @port_id: identifier of the parent port node
1293 * @endpoint_id: identifier of the endpoint node
1295 * Return: Remote fwnode handle associated with remote endpoint node linked
1296 * to @node. Use fwnode_node_put() on it when done.
1298 struct fwnode_handle *
1299 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1302 struct fwnode_handle *endpoint = NULL;
1304 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1305 struct fwnode_endpoint fwnode_ep;
1306 struct fwnode_handle *remote;
1309 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1313 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1316 remote = fwnode_graph_get_remote_port_parent(endpoint);
1320 return fwnode_device_is_available(remote) ? remote : NULL;
1325 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1328 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1329 * @fwnode: pointer to endpoint fwnode_handle
1330 * @endpoint: pointer to the fwnode endpoint data structure
1332 * Parse @fwnode representing a graph endpoint node and store the
1333 * information in @endpoint. The caller must hold a reference to
1336 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1337 struct fwnode_endpoint *endpoint)
1339 memset(endpoint, 0, sizeof(*endpoint));
1341 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1343 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);