2 * property.c - Unified device property interface.
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/acpi.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
17 #include <linux/of_address.h>
18 #include <linux/of_graph.h>
19 #include <linux/property.h>
20 #include <linux/etherdevice.h>
21 #include <linux/phy.h>
24 struct fwnode_handle fwnode;
25 const struct property_entry *properties;
28 static inline bool is_pset_node(struct fwnode_handle *fwnode)
30 return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA;
33 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
35 return is_pset_node(fwnode) ?
36 container_of(fwnode, struct property_set, fwnode) : NULL;
39 static const struct property_entry *pset_prop_get(struct property_set *pset,
42 const struct property_entry *prop;
44 if (!pset || !pset->properties)
47 for (prop = pset->properties; prop->name; prop++)
48 if (!strcmp(name, prop->name))
54 static const void *pset_prop_find(struct property_set *pset,
55 const char *propname, size_t length)
57 const struct property_entry *prop;
60 prop = pset_prop_get(pset, propname);
62 return ERR_PTR(-EINVAL);
64 pointer = prop->pointer.raw_data;
66 pointer = &prop->value.raw_data;
68 return ERR_PTR(-ENODATA);
69 if (length > prop->length)
70 return ERR_PTR(-EOVERFLOW);
74 static int pset_prop_read_u8_array(struct property_set *pset,
76 u8 *values, size_t nval)
79 size_t length = nval * sizeof(*values);
81 pointer = pset_prop_find(pset, propname, length);
83 return PTR_ERR(pointer);
85 memcpy(values, pointer, length);
89 static int pset_prop_read_u16_array(struct property_set *pset,
91 u16 *values, size_t nval)
94 size_t length = nval * sizeof(*values);
96 pointer = pset_prop_find(pset, propname, length);
98 return PTR_ERR(pointer);
100 memcpy(values, pointer, length);
104 static int pset_prop_read_u32_array(struct property_set *pset,
105 const char *propname,
106 u32 *values, size_t nval)
109 size_t length = nval * sizeof(*values);
111 pointer = pset_prop_find(pset, propname, length);
113 return PTR_ERR(pointer);
115 memcpy(values, pointer, length);
119 static int pset_prop_read_u64_array(struct property_set *pset,
120 const char *propname,
121 u64 *values, size_t nval)
124 size_t length = nval * sizeof(*values);
126 pointer = pset_prop_find(pset, propname, length);
128 return PTR_ERR(pointer);
130 memcpy(values, pointer, length);
134 static int pset_prop_count_elems_of_size(struct property_set *pset,
135 const char *propname, size_t length)
137 const struct property_entry *prop;
139 prop = pset_prop_get(pset, propname);
143 return prop->length / length;
146 static int pset_prop_read_string_array(struct property_set *pset,
147 const char *propname,
148 const char **strings, size_t nval)
150 const struct property_entry *prop;
152 size_t array_len, length;
154 /* Find out the array length. */
155 prop = pset_prop_get(pset, propname);
160 /* The array length for a non-array string property is 1. */
163 /* Find the length of an array. */
164 array_len = pset_prop_count_elems_of_size(pset, propname,
165 sizeof(const char *));
167 /* Return how many there are if strings is NULL. */
171 array_len = min(nval, array_len);
172 length = array_len * sizeof(*strings);
174 pointer = pset_prop_find(pset, propname, length);
176 return PTR_ERR(pointer);
178 memcpy(strings, pointer, length);
183 struct fwnode_handle *dev_fwnode(struct device *dev)
185 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
186 &dev->of_node->fwnode : dev->fwnode;
188 EXPORT_SYMBOL_GPL(dev_fwnode);
191 * device_property_present - check if a property of a device is present
192 * @dev: Device whose property is being checked
193 * @propname: Name of the property
195 * Check if property @propname is present in the device firmware description.
197 bool device_property_present(struct device *dev, const char *propname)
199 return fwnode_property_present(dev_fwnode(dev), propname);
201 EXPORT_SYMBOL_GPL(device_property_present);
203 static bool __fwnode_property_present(struct fwnode_handle *fwnode,
204 const char *propname)
206 if (is_of_node(fwnode))
207 return of_property_read_bool(to_of_node(fwnode), propname);
208 else if (is_acpi_node(fwnode))
209 return !acpi_node_prop_get(fwnode, propname, NULL);
210 else if (is_pset_node(fwnode))
211 return !!pset_prop_get(to_pset_node(fwnode), propname);
216 * fwnode_property_present - check if a property of a firmware node is present
217 * @fwnode: Firmware node whose property to check
218 * @propname: Name of the property
220 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
224 ret = __fwnode_property_present(fwnode, propname);
225 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
226 !IS_ERR_OR_NULL(fwnode->secondary))
227 ret = __fwnode_property_present(fwnode->secondary, propname);
230 EXPORT_SYMBOL_GPL(fwnode_property_present);
233 * device_property_read_u8_array - return a u8 array property of a device
234 * @dev: Device to get the property of
235 * @propname: Name of the property
236 * @val: The values are stored here or %NULL to return the number of values
237 * @nval: Size of the @val array
239 * Function reads an array of u8 properties with @propname from the device
240 * firmware description and stores them to @val if found.
242 * Return: number of values if @val was %NULL,
243 * %0 if the property was found (success),
244 * %-EINVAL if given arguments are not valid,
245 * %-ENODATA if the property does not have a value,
246 * %-EPROTO if the property is not an array of numbers,
247 * %-EOVERFLOW if the size of the property is not as expected.
248 * %-ENXIO if no suitable firmware interface is present.
250 int device_property_read_u8_array(struct device *dev, const char *propname,
251 u8 *val, size_t nval)
253 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
255 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
258 * device_property_read_u16_array - return a u16 array property of a device
259 * @dev: Device to get the property of
260 * @propname: Name of the property
261 * @val: The values are stored here or %NULL to return the number of values
262 * @nval: Size of the @val array
264 * Function reads an array of u16 properties with @propname from the device
265 * firmware description and stores them to @val if found.
267 * Return: number of values if @val was %NULL,
268 * %0 if the property was found (success),
269 * %-EINVAL if given arguments are not valid,
270 * %-ENODATA if the property does not have a value,
271 * %-EPROTO if the property is not an array of numbers,
272 * %-EOVERFLOW if the size of the property is not as expected.
273 * %-ENXIO if no suitable firmware interface is present.
275 int device_property_read_u16_array(struct device *dev, const char *propname,
276 u16 *val, size_t nval)
278 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
280 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
283 * device_property_read_u32_array - return a u32 array property of a device
284 * @dev: Device to get the property of
285 * @propname: Name of the property
286 * @val: The values are stored here or %NULL to return the number of values
287 * @nval: Size of the @val array
289 * Function reads an array of u32 properties with @propname from the device
290 * firmware description and stores them to @val if found.
292 * Return: number of values if @val was %NULL,
293 * %0 if the property was found (success),
294 * %-EINVAL if given arguments are not valid,
295 * %-ENODATA if the property does not have a value,
296 * %-EPROTO if the property is not an array of numbers,
297 * %-EOVERFLOW if the size of the property is not as expected.
298 * %-ENXIO if no suitable firmware interface is present.
300 int device_property_read_u32_array(struct device *dev, const char *propname,
301 u32 *val, size_t nval)
303 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
305 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
308 * device_property_read_u64_array - return a u64 array property of a device
309 * @dev: Device to get the property of
310 * @propname: Name of the property
311 * @val: The values are stored here or %NULL to return the number of values
312 * @nval: Size of the @val array
314 * Function reads an array of u64 properties with @propname from the device
315 * firmware description and stores them to @val if found.
317 * Return: number of values if @val was %NULL,
318 * %0 if the property was found (success),
319 * %-EINVAL if given arguments are not valid,
320 * %-ENODATA if the property does not have a value,
321 * %-EPROTO if the property is not an array of numbers,
322 * %-EOVERFLOW if the size of the property is not as expected.
323 * %-ENXIO if no suitable firmware interface is present.
325 int device_property_read_u64_array(struct device *dev, const char *propname,
326 u64 *val, size_t nval)
328 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
330 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
333 * device_property_read_string_array - return a string array property of device
334 * @dev: Device to get the property of
335 * @propname: Name of the property
336 * @val: The values are stored here or %NULL to return the number of values
337 * @nval: Size of the @val array
339 * Function reads an array of string properties with @propname from the device
340 * firmware description and stores them to @val if found.
342 * Return: number of values read on success if @val is non-NULL,
343 * number of values available on success if @val is NULL,
344 * %-EINVAL if given arguments are not valid,
345 * %-ENODATA if the property does not have a value,
346 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
347 * %-EOVERFLOW if the size of the property is not as expected.
348 * %-ENXIO if no suitable firmware interface is present.
350 int device_property_read_string_array(struct device *dev, const char *propname,
351 const char **val, size_t nval)
353 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
355 EXPORT_SYMBOL_GPL(device_property_read_string_array);
358 * device_property_read_string - return a string property of a device
359 * @dev: Device to get the property of
360 * @propname: Name of the property
361 * @val: The value is stored here
363 * Function reads property @propname from the device firmware description and
364 * stores the value into @val if found. The value is checked to be a string.
366 * Return: %0 if the property was found (success),
367 * %-EINVAL if given arguments are not valid,
368 * %-ENODATA if the property does not have a value,
369 * %-EPROTO or %-EILSEQ if the property type is not a string.
370 * %-ENXIO if no suitable firmware interface is present.
372 int device_property_read_string(struct device *dev, const char *propname,
375 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
377 EXPORT_SYMBOL_GPL(device_property_read_string);
380 * device_property_match_string - find a string in an array and return index
381 * @dev: Device to get the property of
382 * @propname: Name of the property holding the array
383 * @string: String to look for
385 * Find a given string in a string array and if it is found return the
388 * Return: %0 if the property was found (success),
389 * %-EINVAL if given arguments are not valid,
390 * %-ENODATA if the property does not have a value,
391 * %-EPROTO if the property is not an array of strings,
392 * %-ENXIO if no suitable firmware interface is present.
394 int device_property_match_string(struct device *dev, const char *propname,
397 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
399 EXPORT_SYMBOL_GPL(device_property_match_string);
401 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
402 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
403 : of_property_count_elems_of_size((node), (propname), sizeof(type))
405 #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
406 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
407 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
409 #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
412 if (is_of_node(_fwnode_)) \
413 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
414 _type_, _val_, _nval_); \
415 else if (is_acpi_node(_fwnode_)) \
416 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
418 else if (is_pset_node(_fwnode_)) \
419 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
420 _type_, _val_, _nval_); \
426 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
429 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
431 if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) && \
432 !IS_ERR_OR_NULL(_fwnode_->secondary)) \
433 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \
434 _proptype_, _val_, _nval_); \
439 * fwnode_property_read_u8_array - return a u8 array property of firmware node
440 * @fwnode: Firmware node to get the property of
441 * @propname: Name of the property
442 * @val: The values are stored here or %NULL to return the number of values
443 * @nval: Size of the @val array
445 * Read an array of u8 properties with @propname from @fwnode and stores them to
448 * Return: number of values if @val was %NULL,
449 * %0 if the property was found (success),
450 * %-EINVAL if given arguments are not valid,
451 * %-ENODATA if the property does not have a value,
452 * %-EPROTO if the property is not an array of numbers,
453 * %-EOVERFLOW if the size of the property is not as expected,
454 * %-ENXIO if no suitable firmware interface is present.
456 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
457 const char *propname, u8 *val, size_t nval)
459 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
462 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
465 * fwnode_property_read_u16_array - return a u16 array property of firmware node
466 * @fwnode: Firmware node to get the property of
467 * @propname: Name of the property
468 * @val: The values are stored here or %NULL to return the number of values
469 * @nval: Size of the @val array
471 * Read an array of u16 properties with @propname from @fwnode and store them to
474 * Return: number of values if @val was %NULL,
475 * %0 if the property was found (success),
476 * %-EINVAL if given arguments are not valid,
477 * %-ENODATA if the property does not have a value,
478 * %-EPROTO if the property is not an array of numbers,
479 * %-EOVERFLOW if the size of the property is not as expected,
480 * %-ENXIO if no suitable firmware interface is present.
482 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
483 const char *propname, u16 *val, size_t nval)
485 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
488 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
491 * fwnode_property_read_u32_array - return a u32 array property of firmware node
492 * @fwnode: Firmware node to get the property of
493 * @propname: Name of the property
494 * @val: The values are stored here or %NULL to return the number of values
495 * @nval: Size of the @val array
497 * Read an array of u32 properties with @propname from @fwnode store them to
500 * Return: number of values if @val was %NULL,
501 * %0 if the property was found (success),
502 * %-EINVAL if given arguments are not valid,
503 * %-ENODATA if the property does not have a value,
504 * %-EPROTO if the property is not an array of numbers,
505 * %-EOVERFLOW if the size of the property is not as expected,
506 * %-ENXIO if no suitable firmware interface is present.
508 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
509 const char *propname, u32 *val, size_t nval)
511 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
514 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
517 * fwnode_property_read_u64_array - return a u64 array property firmware node
518 * @fwnode: Firmware node to get the property of
519 * @propname: Name of the property
520 * @val: The values are stored here or %NULL to return the number of values
521 * @nval: Size of the @val array
523 * Read an array of u64 properties with @propname from @fwnode and store them to
526 * Return: number of values if @val was %NULL,
527 * %0 if the property was found (success),
528 * %-EINVAL if given arguments are not valid,
529 * %-ENODATA if the property does not have a value,
530 * %-EPROTO if the property is not an array of numbers,
531 * %-EOVERFLOW if the size of the property is not as expected,
532 * %-ENXIO if no suitable firmware interface is present.
534 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
535 const char *propname, u64 *val, size_t nval)
537 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
540 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
542 static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
543 const char *propname,
544 const char **val, size_t nval)
546 if (is_of_node(fwnode))
548 of_property_read_string_array(to_of_node(fwnode),
549 propname, val, nval) :
550 of_property_count_strings(to_of_node(fwnode), propname);
551 else if (is_acpi_node(fwnode))
552 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
554 else if (is_pset_node(fwnode))
555 return pset_prop_read_string_array(to_pset_node(fwnode),
556 propname, val, nval);
561 * fwnode_property_read_string_array - return string array property of a node
562 * @fwnode: Firmware node to get the property of
563 * @propname: Name of the property
564 * @val: The values are stored here or %NULL to return the number of values
565 * @nval: Size of the @val array
567 * Read an string list property @propname from the given firmware node and store
568 * them to @val if found.
570 * Return: number of values read on success if @val is non-NULL,
571 * number of values available on success if @val is NULL,
572 * %-EINVAL if given arguments are not valid,
573 * %-ENODATA if the property does not have a value,
574 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
575 * %-EOVERFLOW if the size of the property is not as expected,
576 * %-ENXIO if no suitable firmware interface is present.
578 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
579 const char *propname, const char **val,
584 ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
585 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
586 !IS_ERR_OR_NULL(fwnode->secondary))
587 ret = __fwnode_property_read_string_array(fwnode->secondary,
588 propname, val, nval);
591 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
594 * fwnode_property_read_string - return a string property of a firmware node
595 * @fwnode: Firmware node to get the property of
596 * @propname: Name of the property
597 * @val: The value is stored here
599 * Read property @propname from the given firmware node and store the value into
600 * @val if found. The value is checked to be a string.
602 * Return: %0 if the property was found (success),
603 * %-EINVAL if given arguments are not valid,
604 * %-ENODATA if the property does not have a value,
605 * %-EPROTO or %-EILSEQ if the property is not a string,
606 * %-ENXIO if no suitable firmware interface is present.
608 int fwnode_property_read_string(struct fwnode_handle *fwnode,
609 const char *propname, const char **val)
611 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
613 return ret < 0 ? ret : 0;
615 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
618 * fwnode_property_match_string - find a string in an array and return index
619 * @fwnode: Firmware node to get the property of
620 * @propname: Name of the property holding the array
621 * @string: String to look for
623 * Find a given string in a string array and if it is found return the
626 * Return: %0 if the property was found (success),
627 * %-EINVAL if given arguments are not valid,
628 * %-ENODATA if the property does not have a value,
629 * %-EPROTO if the property is not an array of strings,
630 * %-ENXIO if no suitable firmware interface is present.
632 int fwnode_property_match_string(struct fwnode_handle *fwnode,
633 const char *propname, const char *string)
638 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
645 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
649 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
653 ret = match_string(values, nval, string);
660 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
662 static int property_copy_string_array(struct property_entry *dst,
663 const struct property_entry *src)
666 size_t nval = src->length / sizeof(*d);
669 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
673 for (i = 0; i < nval; i++) {
674 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
675 if (!d[i] && src->pointer.str[i]) {
683 dst->pointer.raw_data = d;
687 static int property_entry_copy_data(struct property_entry *dst,
688 const struct property_entry *src)
692 dst->name = kstrdup(src->name, GFP_KERNEL);
702 if (src->is_string) {
703 error = property_copy_string_array(dst, src);
707 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
708 src->length, GFP_KERNEL);
709 if (!dst->pointer.raw_data) {
714 } else if (src->is_string) {
715 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
716 if (!dst->value.str && src->value.str) {
721 dst->value.raw_data = src->value.raw_data;
724 dst->length = src->length;
725 dst->is_array = src->is_array;
726 dst->is_string = src->is_string;
735 static void property_entry_free_data(const struct property_entry *p)
740 if (p->is_string && p->pointer.str) {
741 nval = p->length / sizeof(const char *);
742 for (i = 0; i < nval; i++)
743 kfree(p->pointer.str[i]);
745 kfree(p->pointer.raw_data);
746 } else if (p->is_string) {
753 * property_entries_dup - duplicate array of properties
754 * @properties: array of properties to copy
756 * This function creates a deep copy of the given NULL-terminated array
757 * of property entries.
759 struct property_entry *
760 property_entries_dup(const struct property_entry *properties)
762 struct property_entry *p;
765 while (properties[n].name)
768 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
770 return ERR_PTR(-ENOMEM);
772 for (i = 0; i < n; i++) {
773 int ret = property_entry_copy_data(&p[i], &properties[i]);
776 property_entry_free_data(&p[i]);
784 EXPORT_SYMBOL_GPL(property_entries_dup);
787 * property_entries_free - free previously allocated array of properties
788 * @properties: array of properties to destroy
790 * This function frees given NULL-terminated array of property entries,
791 * along with their data.
793 void property_entries_free(const struct property_entry *properties)
795 const struct property_entry *p;
797 for (p = properties; p->name; p++)
798 property_entry_free_data(p);
802 EXPORT_SYMBOL_GPL(property_entries_free);
805 * pset_free_set - releases memory allocated for copied property set
806 * @pset: Property set to release
808 * Function takes previously copied property set and releases all the
809 * memory allocated to it.
811 static void pset_free_set(struct property_set *pset)
816 property_entries_free(pset->properties);
821 * pset_copy_set - copies property set
822 * @pset: Property set to copy
824 * This function takes a deep copy of the given property set and returns
825 * pointer to the copy. Call device_free_property_set() to free resources
826 * allocated in this function.
828 * Return: Pointer to the new property set or error pointer.
830 static struct property_set *pset_copy_set(const struct property_set *pset)
832 struct property_entry *properties;
833 struct property_set *p;
835 p = kzalloc(sizeof(*p), GFP_KERNEL);
837 return ERR_PTR(-ENOMEM);
839 properties = property_entries_dup(pset->properties);
840 if (IS_ERR(properties)) {
842 return ERR_CAST(properties);
845 p->properties = properties;
850 * device_remove_properties - Remove properties from a device object.
851 * @dev: Device whose properties to remove.
853 * The function removes properties previously associated to the device
854 * secondary firmware node with device_add_properties(). Memory allocated
855 * to the properties will also be released.
857 void device_remove_properties(struct device *dev)
859 struct fwnode_handle *fwnode;
861 fwnode = dev_fwnode(dev);
865 * Pick either primary or secondary node depending which one holds
866 * the pset. If there is no real firmware node (ACPI/DT) primary
867 * will hold the pset.
869 if (is_pset_node(fwnode)) {
870 set_primary_fwnode(dev, NULL);
871 pset_free_set(to_pset_node(fwnode));
873 fwnode = fwnode->secondary;
874 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
875 set_secondary_fwnode(dev, NULL);
876 pset_free_set(to_pset_node(fwnode));
880 EXPORT_SYMBOL_GPL(device_remove_properties);
883 * device_add_properties - Add a collection of properties to a device object.
884 * @dev: Device to add properties to.
885 * @properties: Collection of properties to add.
887 * Associate a collection of device properties represented by @properties with
888 * @dev as its secondary firmware node. The function takes a copy of
891 int device_add_properties(struct device *dev,
892 const struct property_entry *properties)
894 struct property_set *p, pset;
899 pset.properties = properties;
901 p = pset_copy_set(&pset);
905 p->fwnode.type = FWNODE_PDATA;
906 set_secondary_fwnode(dev, &p->fwnode);
909 EXPORT_SYMBOL_GPL(device_add_properties);
912 * fwnode_get_next_parent - Iterate to the node's parent
913 * @fwnode: Firmware whose parent is retrieved
915 * This is like fwnode_get_parent() except that it drops the refcount
916 * on the passed node, making it suitable for iterating through a
919 * Returns a node pointer with refcount incremented, use
920 * fwnode_handle_node() on it when done.
922 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
924 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
926 fwnode_handle_put(fwnode);
930 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
933 * fwnode_get_parent - Return parent firwmare node
934 * @fwnode: Firmware whose parent is retrieved
936 * Return parent firmware node of the given node if possible or %NULL if no
937 * parent was available.
939 struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode)
941 struct fwnode_handle *parent = NULL;
943 if (is_of_node(fwnode)) {
944 struct device_node *node;
946 node = of_get_parent(to_of_node(fwnode));
948 parent = &node->fwnode;
949 } else if (is_acpi_node(fwnode)) {
950 parent = acpi_node_get_parent(fwnode);
955 EXPORT_SYMBOL_GPL(fwnode_get_parent);
958 * fwnode_get_next_child_node - Return the next child node handle for a node
959 * @fwnode: Firmware node to find the next child node for.
960 * @child: Handle to one of the node's child nodes or a %NULL handle.
962 struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode,
963 struct fwnode_handle *child)
965 if (is_of_node(fwnode)) {
966 struct device_node *node;
968 node = of_get_next_available_child(to_of_node(fwnode),
971 return &node->fwnode;
972 } else if (is_acpi_node(fwnode)) {
973 return acpi_get_next_subnode(fwnode, child);
978 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
981 * device_get_next_child_node - Return the next child node handle for a device
982 * @dev: Device to find the next child node for.
983 * @child: Handle to one of the device's child nodes or a null handle.
985 struct fwnode_handle *device_get_next_child_node(struct device *dev,
986 struct fwnode_handle *child)
988 struct acpi_device *adev = ACPI_COMPANION(dev);
989 struct fwnode_handle *fwnode = NULL;
992 fwnode = &dev->of_node->fwnode;
994 fwnode = acpi_fwnode_handle(adev);
996 return fwnode_get_next_child_node(fwnode, child);
998 EXPORT_SYMBOL_GPL(device_get_next_child_node);
1001 * fwnode_get_named_child_node - Return first matching named child node handle
1002 * @fwnode: Firmware node to find the named child node for.
1003 * @childname: String to match child node name against.
1005 struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode,
1006 const char *childname)
1008 struct fwnode_handle *child;
1011 * Find first matching named child node of this fwnode.
1012 * For ACPI this will be a data only sub-node.
1014 fwnode_for_each_child_node(fwnode, child) {
1015 if (is_of_node(child)) {
1016 if (!of_node_cmp(to_of_node(child)->name, childname))
1018 } else if (is_acpi_data_node(child)) {
1019 if (acpi_data_node_match(child, childname))
1026 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1029 * device_get_named_child_node - Return first matching named child node handle
1030 * @dev: Device to find the named child node for.
1031 * @childname: String to match child node name against.
1033 struct fwnode_handle *device_get_named_child_node(struct device *dev,
1034 const char *childname)
1036 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1038 EXPORT_SYMBOL_GPL(device_get_named_child_node);
1041 * fwnode_handle_get - Obtain a reference to a device node
1042 * @fwnode: Pointer to the device node to obtain the reference to.
1044 void fwnode_handle_get(struct fwnode_handle *fwnode)
1046 if (is_of_node(fwnode))
1047 of_node_get(to_of_node(fwnode));
1049 EXPORT_SYMBOL_GPL(fwnode_handle_get);
1052 * fwnode_handle_put - Drop reference to a device node
1053 * @fwnode: Pointer to the device node to drop the reference to.
1055 * This has to be used when terminating device_for_each_child_node() iteration
1056 * with break or return to prevent stale device node references from being left
1059 void fwnode_handle_put(struct fwnode_handle *fwnode)
1061 if (is_of_node(fwnode))
1062 of_node_put(to_of_node(fwnode));
1064 EXPORT_SYMBOL_GPL(fwnode_handle_put);
1067 * device_get_child_node_count - return the number of child nodes for device
1068 * @dev: Device to cound the child nodes for
1070 unsigned int device_get_child_node_count(struct device *dev)
1072 struct fwnode_handle *child;
1073 unsigned int count = 0;
1075 device_for_each_child_node(dev, child)
1080 EXPORT_SYMBOL_GPL(device_get_child_node_count);
1082 bool device_dma_supported(struct device *dev)
1084 /* For DT, this is always supported.
1085 * For ACPI, this depends on CCA, which
1086 * is determined by the acpi_dma_supported().
1088 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1091 return acpi_dma_supported(ACPI_COMPANION(dev));
1093 EXPORT_SYMBOL_GPL(device_dma_supported);
1095 enum dev_dma_attr device_get_dma_attr(struct device *dev)
1097 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1099 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1100 if (of_dma_is_coherent(dev->of_node))
1101 attr = DEV_DMA_COHERENT;
1103 attr = DEV_DMA_NON_COHERENT;
1105 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1109 EXPORT_SYMBOL_GPL(device_get_dma_attr);
1112 * device_get_phy_mode - Get phy mode for given device
1113 * @dev: Pointer to the given device
1115 * The function gets phy interface string from property 'phy-mode' or
1116 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1119 int device_get_phy_mode(struct device *dev)
1124 err = device_property_read_string(dev, "phy-mode", &pm);
1126 err = device_property_read_string(dev,
1127 "phy-connection-type", &pm);
1131 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1132 if (!strcasecmp(pm, phy_modes(i)))
1137 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1139 static void *device_get_mac_addr(struct device *dev,
1140 const char *name, char *addr,
1143 int ret = device_property_read_u8_array(dev, name, addr, alen);
1145 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
1151 * device_get_mac_address - Get the MAC for a given device
1152 * @dev: Pointer to the device
1153 * @addr: Address of buffer to store the MAC in
1154 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1156 * Search the firmware node for the best MAC address to use. 'mac-address' is
1157 * checked first, because that is supposed to contain to "most recent" MAC
1158 * address. If that isn't set, then 'local-mac-address' is checked next,
1159 * because that is the default address. If that isn't set, then the obsolete
1160 * 'address' is checked, just in case we're using an old device tree.
1162 * Note that the 'address' property is supposed to contain a virtual address of
1163 * the register set, but some DTS files have redefined that property to be the
1166 * All-zero MAC addresses are rejected, because those could be properties that
1167 * exist in the firmware tables, but were not updated by the firmware. For
1168 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1169 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1170 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1171 * exists but is all zeros.
1173 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1177 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1181 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1185 return device_get_mac_addr(dev, "address", addr, alen);
1187 EXPORT_SYMBOL(device_get_mac_address);
1190 * device_graph_get_next_endpoint - Get next endpoint firmware node
1191 * @fwnode: Pointer to the parent firmware node
1192 * @prev: Previous endpoint node or %NULL to get the first
1194 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1197 struct fwnode_handle *
1198 fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode,
1199 struct fwnode_handle *prev)
1201 struct fwnode_handle *endpoint = NULL;
1203 if (is_of_node(fwnode)) {
1204 struct device_node *node;
1206 node = of_graph_get_next_endpoint(to_of_node(fwnode),
1210 endpoint = &node->fwnode;
1211 } else if (is_acpi_node(fwnode)) {
1212 endpoint = acpi_graph_get_next_endpoint(fwnode, prev);
1213 if (IS_ERR(endpoint))
1220 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1223 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1224 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1226 * Extracts firmware node of a remote device the @fwnode points to.
1228 struct fwnode_handle *
1229 fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode)
1231 struct fwnode_handle *parent = NULL;
1233 if (is_of_node(fwnode)) {
1234 struct device_node *node;
1236 node = of_graph_get_remote_port_parent(to_of_node(fwnode));
1238 parent = &node->fwnode;
1239 } else if (is_acpi_node(fwnode)) {
1242 ret = acpi_graph_get_remote_endpoint(fwnode, &parent, NULL,
1250 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1253 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1254 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1256 * Extracts firmware node of a remote port the @fwnode points to.
1258 struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode)
1260 struct fwnode_handle *port = NULL;
1262 if (is_of_node(fwnode)) {
1263 struct device_node *node;
1265 node = of_graph_get_remote_port(to_of_node(fwnode));
1267 port = &node->fwnode;
1268 } else if (is_acpi_node(fwnode)) {
1271 ret = acpi_graph_get_remote_endpoint(fwnode, NULL, &port, NULL);
1278 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1281 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1282 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1284 * Extracts firmware node of a remote endpoint the @fwnode points to.
1286 struct fwnode_handle *
1287 fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode)
1289 struct fwnode_handle *endpoint = NULL;
1291 if (is_of_node(fwnode)) {
1292 struct device_node *node;
1294 node = of_parse_phandle(to_of_node(fwnode), "remote-endpoint",
1297 endpoint = &node->fwnode;
1298 } else if (is_acpi_node(fwnode)) {
1301 ret = acpi_graph_get_remote_endpoint(fwnode, NULL, NULL,
1309 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1312 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1313 * @fwnode: pointer to endpoint fwnode_handle
1314 * @endpoint: pointer to the fwnode endpoint data structure
1316 * Parse @fwnode representing a graph endpoint node and store the
1317 * information in @endpoint. The caller must hold a reference to
1320 int fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode,
1321 struct fwnode_endpoint *endpoint)
1323 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1325 memset(endpoint, 0, sizeof(*endpoint));
1327 endpoint->local_fwnode = fwnode;
1329 if (is_acpi_node(port_fwnode)) {
1330 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1331 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1333 fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port);
1334 fwnode_property_read_u32(fwnode, "reg", &endpoint->id);
1337 fwnode_handle_put(port_fwnode);
1341 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);