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 const struct fwnode_operations pset_fwnode_ops;
30 static inline bool is_pset_node(const struct fwnode_handle *fwnode)
32 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
35 #define to_pset_node(__fwnode) \
37 typeof(__fwnode) __to_pset_node_fwnode = __fwnode; \
39 is_pset_node(__to_pset_node_fwnode) ? \
40 container_of(__to_pset_node_fwnode, \
41 struct property_set, fwnode) : \
45 static const struct property_entry *
46 pset_prop_get(const struct property_set *pset, const char *name)
48 const struct property_entry *prop;
50 if (!pset || !pset->properties)
53 for (prop = pset->properties; prop->name; prop++)
54 if (!strcmp(name, prop->name))
60 static const void *pset_prop_find(const struct property_set *pset,
61 const char *propname, size_t length)
63 const struct property_entry *prop;
66 prop = pset_prop_get(pset, propname);
68 return ERR_PTR(-EINVAL);
70 pointer = prop->pointer.raw_data;
72 pointer = &prop->value.raw_data;
74 return ERR_PTR(-ENODATA);
75 if (length > prop->length)
76 return ERR_PTR(-EOVERFLOW);
80 static int pset_prop_read_u8_array(const struct property_set *pset,
82 u8 *values, size_t nval)
85 size_t length = nval * sizeof(*values);
87 pointer = pset_prop_find(pset, propname, length);
89 return PTR_ERR(pointer);
91 memcpy(values, pointer, length);
95 static int pset_prop_read_u16_array(const struct property_set *pset,
97 u16 *values, size_t nval)
100 size_t length = nval * sizeof(*values);
102 pointer = pset_prop_find(pset, propname, length);
104 return PTR_ERR(pointer);
106 memcpy(values, pointer, length);
110 static int pset_prop_read_u32_array(const struct property_set *pset,
111 const char *propname,
112 u32 *values, size_t nval)
115 size_t length = nval * sizeof(*values);
117 pointer = pset_prop_find(pset, propname, length);
119 return PTR_ERR(pointer);
121 memcpy(values, pointer, length);
125 static int pset_prop_read_u64_array(const struct property_set *pset,
126 const char *propname,
127 u64 *values, size_t nval)
130 size_t length = nval * sizeof(*values);
132 pointer = pset_prop_find(pset, propname, length);
134 return PTR_ERR(pointer);
136 memcpy(values, pointer, length);
140 static int pset_prop_count_elems_of_size(const struct property_set *pset,
141 const char *propname, size_t length)
143 const struct property_entry *prop;
145 prop = pset_prop_get(pset, propname);
149 return prop->length / length;
152 static int pset_prop_read_string_array(const struct property_set *pset,
153 const char *propname,
154 const char **strings, size_t nval)
156 const struct property_entry *prop;
158 size_t array_len, length;
160 /* Find out the array length. */
161 prop = pset_prop_get(pset, propname);
166 /* The array length for a non-array string property is 1. */
169 /* Find the length of an array. */
170 array_len = pset_prop_count_elems_of_size(pset, propname,
171 sizeof(const char *));
173 /* Return how many there are if strings is NULL. */
177 array_len = min(nval, array_len);
178 length = array_len * sizeof(*strings);
180 pointer = pset_prop_find(pset, propname, length);
182 return PTR_ERR(pointer);
184 memcpy(strings, pointer, length);
189 struct fwnode_handle *dev_fwnode(struct device *dev)
191 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
192 &dev->of_node->fwnode : dev->fwnode;
194 EXPORT_SYMBOL_GPL(dev_fwnode);
196 static bool pset_fwnode_property_present(const struct fwnode_handle *fwnode,
197 const char *propname)
199 return !!pset_prop_get(to_pset_node(fwnode), propname);
202 static int pset_fwnode_read_int_array(const struct fwnode_handle *fwnode,
203 const char *propname,
204 unsigned int elem_size, void *val,
207 const struct property_set *node = to_pset_node(fwnode);
210 return pset_prop_count_elems_of_size(node, propname, elem_size);
214 return pset_prop_read_u8_array(node, propname, val, nval);
216 return pset_prop_read_u16_array(node, propname, val, nval);
218 return pset_prop_read_u32_array(node, propname, val, nval);
220 return pset_prop_read_u64_array(node, propname, val, nval);
227 pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
228 const char *propname,
229 const char **val, size_t nval)
231 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
235 static const struct fwnode_operations pset_fwnode_ops = {
236 .property_present = pset_fwnode_property_present,
237 .property_read_int_array = pset_fwnode_read_int_array,
238 .property_read_string_array = pset_fwnode_property_read_string_array,
242 * device_property_present - check if a property of a device is present
243 * @dev: Device whose property is being checked
244 * @propname: Name of the property
246 * Check if property @propname is present in the device firmware description.
248 bool device_property_present(struct device *dev, const char *propname)
250 return fwnode_property_present(dev_fwnode(dev), propname);
252 EXPORT_SYMBOL_GPL(device_property_present);
255 * fwnode_property_present - check if a property of a firmware node is present
256 * @fwnode: Firmware node whose property to check
257 * @propname: Name of the property
259 bool fwnode_property_present(const struct fwnode_handle *fwnode,
260 const char *propname)
264 ret = fwnode_call_bool_op(fwnode, property_present, propname);
265 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
266 !IS_ERR_OR_NULL(fwnode->secondary))
267 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
271 EXPORT_SYMBOL_GPL(fwnode_property_present);
274 * device_property_read_u8_array - return a u8 array property of a device
275 * @dev: Device to get the property of
276 * @propname: Name of the property
277 * @val: The values are stored here or %NULL to return the number of values
278 * @nval: Size of the @val array
280 * Function reads an array of u8 properties with @propname from the device
281 * firmware description and stores them to @val if found.
283 * Return: number of values if @val was %NULL,
284 * %0 if the property was found (success),
285 * %-EINVAL if given arguments are not valid,
286 * %-ENODATA if the property does not have a value,
287 * %-EPROTO if the property is not an array of numbers,
288 * %-EOVERFLOW if the size of the property is not as expected.
289 * %-ENXIO if no suitable firmware interface is present.
291 int device_property_read_u8_array(struct device *dev, const char *propname,
292 u8 *val, size_t nval)
294 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
296 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
299 * device_property_read_u16_array - return a u16 array property of a device
300 * @dev: Device 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 * Function reads an array of u16 properties with @propname from the device
306 * firmware description and stores them to @val if found.
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 device_property_read_u16_array(struct device *dev, const char *propname,
317 u16 *val, size_t nval)
319 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
321 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
324 * device_property_read_u32_array - return a u32 array property of a device
325 * @dev: Device to get the property of
326 * @propname: Name of the property
327 * @val: The values are stored here or %NULL to return the number of values
328 * @nval: Size of the @val array
330 * Function reads an array of u32 properties with @propname from the device
331 * firmware description and stores them to @val if found.
333 * Return: number of values if @val was %NULL,
334 * %0 if the property was found (success),
335 * %-EINVAL if given arguments are not valid,
336 * %-ENODATA if the property does not have a value,
337 * %-EPROTO if the property is not an array of numbers,
338 * %-EOVERFLOW if the size of the property is not as expected.
339 * %-ENXIO if no suitable firmware interface is present.
341 int device_property_read_u32_array(struct device *dev, const char *propname,
342 u32 *val, size_t nval)
344 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
346 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
349 * device_property_read_u64_array - return a u64 array property of a device
350 * @dev: Device to get the property of
351 * @propname: Name of the property
352 * @val: The values are stored here or %NULL to return the number of values
353 * @nval: Size of the @val array
355 * Function reads an array of u64 properties with @propname from the device
356 * firmware description and stores them to @val if found.
358 * Return: number of values if @val was %NULL,
359 * %0 if the property was found (success),
360 * %-EINVAL if given arguments are not valid,
361 * %-ENODATA if the property does not have a value,
362 * %-EPROTO if the property is not an array of numbers,
363 * %-EOVERFLOW if the size of the property is not as expected.
364 * %-ENXIO if no suitable firmware interface is present.
366 int device_property_read_u64_array(struct device *dev, const char *propname,
367 u64 *val, size_t nval)
369 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
371 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
374 * device_property_read_string_array - return a string array property of device
375 * @dev: Device to get the property of
376 * @propname: Name of the property
377 * @val: The values are stored here or %NULL to return the number of values
378 * @nval: Size of the @val array
380 * Function reads an array of string properties with @propname from the device
381 * firmware description and stores them to @val if found.
383 * Return: number of values read on success if @val is non-NULL,
384 * number of values available on success if @val is NULL,
385 * %-EINVAL if given arguments are not valid,
386 * %-ENODATA if the property does not have a value,
387 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
388 * %-EOVERFLOW if the size of the property is not as expected.
389 * %-ENXIO if no suitable firmware interface is present.
391 int device_property_read_string_array(struct device *dev, const char *propname,
392 const char **val, size_t nval)
394 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
396 EXPORT_SYMBOL_GPL(device_property_read_string_array);
399 * device_property_read_string - return a string property of a device
400 * @dev: Device to get the property of
401 * @propname: Name of the property
402 * @val: The value is stored here
404 * Function reads property @propname from the device firmware description and
405 * stores the value into @val if found. The value is checked to be a string.
407 * Return: %0 if the property was found (success),
408 * %-EINVAL if given arguments are not valid,
409 * %-ENODATA if the property does not have a value,
410 * %-EPROTO or %-EILSEQ if the property type is not a string.
411 * %-ENXIO if no suitable firmware interface is present.
413 int device_property_read_string(struct device *dev, const char *propname,
416 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
418 EXPORT_SYMBOL_GPL(device_property_read_string);
421 * device_property_match_string - find a string in an array and return index
422 * @dev: Device to get the property of
423 * @propname: Name of the property holding the array
424 * @string: String to look for
426 * Find a given string in a string array and if it is found return the
429 * Return: %0 if the property was found (success),
430 * %-EINVAL if given arguments are not valid,
431 * %-ENODATA if the property does not have a value,
432 * %-EPROTO if the property is not an array of strings,
433 * %-ENXIO if no suitable firmware interface is present.
435 int device_property_match_string(struct device *dev, const char *propname,
438 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
440 EXPORT_SYMBOL_GPL(device_property_match_string);
442 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
443 const char *propname,
444 unsigned int elem_size, void *val,
449 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
450 elem_size, val, nval);
451 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
452 !IS_ERR_OR_NULL(fwnode->secondary))
453 ret = fwnode_call_int_op(
454 fwnode->secondary, property_read_int_array, propname,
455 elem_size, val, nval);
461 * fwnode_property_read_u8_array - return a u8 array property of firmware node
462 * @fwnode: Firmware node to get the property of
463 * @propname: Name of the property
464 * @val: The values are stored here or %NULL to return the number of values
465 * @nval: Size of the @val array
467 * Read an array of u8 properties with @propname from @fwnode and stores them to
470 * Return: number of values if @val was %NULL,
471 * %0 if the property was found (success),
472 * %-EINVAL if given arguments are not valid,
473 * %-ENODATA if the property does not have a value,
474 * %-EPROTO if the property is not an array of numbers,
475 * %-EOVERFLOW if the size of the property is not as expected,
476 * %-ENXIO if no suitable firmware interface is present.
478 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
479 const char *propname, u8 *val, size_t nval)
481 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
484 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
487 * fwnode_property_read_u16_array - return a u16 array property of firmware node
488 * @fwnode: Firmware node to get the property of
489 * @propname: Name of the property
490 * @val: The values are stored here or %NULL to return the number of values
491 * @nval: Size of the @val array
493 * Read an array of u16 properties with @propname from @fwnode and store them to
496 * Return: number of values if @val was %NULL,
497 * %0 if the property was found (success),
498 * %-EINVAL if given arguments are not valid,
499 * %-ENODATA if the property does not have a value,
500 * %-EPROTO if the property is not an array of numbers,
501 * %-EOVERFLOW if the size of the property is not as expected,
502 * %-ENXIO if no suitable firmware interface is present.
504 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
505 const char *propname, u16 *val, size_t nval)
507 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
510 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
513 * fwnode_property_read_u32_array - return a u32 array property of firmware node
514 * @fwnode: Firmware node to get the property of
515 * @propname: Name of the property
516 * @val: The values are stored here or %NULL to return the number of values
517 * @nval: Size of the @val array
519 * Read an array of u32 properties with @propname from @fwnode store them to
522 * Return: number of values if @val was %NULL,
523 * %0 if the property was found (success),
524 * %-EINVAL if given arguments are not valid,
525 * %-ENODATA if the property does not have a value,
526 * %-EPROTO if the property is not an array of numbers,
527 * %-EOVERFLOW if the size of the property is not as expected,
528 * %-ENXIO if no suitable firmware interface is present.
530 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
531 const char *propname, u32 *val, size_t nval)
533 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
536 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
539 * fwnode_property_read_u64_array - return a u64 array property firmware node
540 * @fwnode: Firmware node to get the property of
541 * @propname: Name of the property
542 * @val: The values are stored here or %NULL to return the number of values
543 * @nval: Size of the @val array
545 * Read an array of u64 properties with @propname from @fwnode and store them to
548 * Return: number of values if @val was %NULL,
549 * %0 if the property was found (success),
550 * %-EINVAL if given arguments are not valid,
551 * %-ENODATA if the property does not have a value,
552 * %-EPROTO if the property is not an array of numbers,
553 * %-EOVERFLOW if the size of the property is not as expected,
554 * %-ENXIO if no suitable firmware interface is present.
556 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
557 const char *propname, u64 *val, size_t nval)
559 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
562 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
565 * fwnode_property_read_string_array - return string array property of a node
566 * @fwnode: Firmware node to get the property of
567 * @propname: Name of the property
568 * @val: The values are stored here or %NULL to return the number of values
569 * @nval: Size of the @val array
571 * Read an string list property @propname from the given firmware node and store
572 * them to @val if found.
574 * Return: number of values read on success if @val is non-NULL,
575 * number of values available on success if @val is NULL,
576 * %-EINVAL if given arguments are not valid,
577 * %-ENODATA if the property does not have a value,
578 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
579 * %-EOVERFLOW if the size of the property is not as expected,
580 * %-ENXIO if no suitable firmware interface is present.
582 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
583 const char *propname, const char **val,
588 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
590 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
591 !IS_ERR_OR_NULL(fwnode->secondary))
592 ret = fwnode_call_int_op(fwnode->secondary,
593 property_read_string_array, propname,
597 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
600 * fwnode_property_read_string - return a string property of a firmware node
601 * @fwnode: Firmware node to get the property of
602 * @propname: Name of the property
603 * @val: The value is stored here
605 * Read property @propname from the given firmware node and store the value into
606 * @val if found. The value is checked to be a string.
608 * Return: %0 if the property was found (success),
609 * %-EINVAL if given arguments are not valid,
610 * %-ENODATA if the property does not have a value,
611 * %-EPROTO or %-EILSEQ if the property is not a string,
612 * %-ENXIO if no suitable firmware interface is present.
614 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
615 const char *propname, const char **val)
617 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
619 return ret < 0 ? ret : 0;
621 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
624 * fwnode_property_match_string - find a string in an array and return index
625 * @fwnode: Firmware node to get the property of
626 * @propname: Name of the property holding the array
627 * @string: String to look for
629 * Find a given string in a string array and if it is found return the
632 * Return: %0 if the property was found (success),
633 * %-EINVAL if given arguments are not valid,
634 * %-ENODATA if the property does not have a value,
635 * %-EPROTO if the property is not an array of strings,
636 * %-ENXIO if no suitable firmware interface is present.
638 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
639 const char *propname, const char *string)
644 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
651 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
655 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
659 ret = match_string(values, nval, string);
666 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
669 * fwnode_property_get_reference_args() - Find a reference with arguments
670 * @fwnode: Firmware node where to look for the reference
671 * @prop: The name of the property
672 * @nargs_prop: The name of the property telling the number of
673 * arguments in the referred node. NULL if @nargs is known,
674 * otherwise @nargs is ignored. Only relevant on OF.
675 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
676 * @index: Index of the reference, from zero onwards.
677 * @args: Result structure with reference and integer arguments.
679 * Obtain a reference based on a named property in an fwnode, with
682 * Caller is responsible to call fwnode_handle_put() on the returned
683 * args->fwnode pointer.
686 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
687 const char *prop, const char *nargs_prop,
688 unsigned int nargs, unsigned int index,
689 struct fwnode_reference_args *args)
691 return fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
694 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
696 static int property_copy_string_array(struct property_entry *dst,
697 const struct property_entry *src)
700 size_t nval = src->length / sizeof(*d);
703 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
707 for (i = 0; i < nval; i++) {
708 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
709 if (!d[i] && src->pointer.str[i]) {
717 dst->pointer.raw_data = d;
721 static int property_entry_copy_data(struct property_entry *dst,
722 const struct property_entry *src)
726 dst->name = kstrdup(src->name, GFP_KERNEL);
736 if (src->is_string) {
737 error = property_copy_string_array(dst, src);
741 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
742 src->length, GFP_KERNEL);
743 if (!dst->pointer.raw_data) {
748 } else if (src->is_string) {
749 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
750 if (!dst->value.str && src->value.str) {
755 dst->value.raw_data = src->value.raw_data;
758 dst->length = src->length;
759 dst->is_array = src->is_array;
760 dst->is_string = src->is_string;
769 static void property_entry_free_data(const struct property_entry *p)
774 if (p->is_string && p->pointer.str) {
775 nval = p->length / sizeof(const char *);
776 for (i = 0; i < nval; i++)
777 kfree(p->pointer.str[i]);
779 kfree(p->pointer.raw_data);
780 } else if (p->is_string) {
787 * property_entries_dup - duplicate array of properties
788 * @properties: array of properties to copy
790 * This function creates a deep copy of the given NULL-terminated array
791 * of property entries.
793 struct property_entry *
794 property_entries_dup(const struct property_entry *properties)
796 struct property_entry *p;
799 while (properties[n].name)
802 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
804 return ERR_PTR(-ENOMEM);
806 for (i = 0; i < n; i++) {
807 int ret = property_entry_copy_data(&p[i], &properties[i]);
810 property_entry_free_data(&p[i]);
818 EXPORT_SYMBOL_GPL(property_entries_dup);
821 * property_entries_free - free previously allocated array of properties
822 * @properties: array of properties to destroy
824 * This function frees given NULL-terminated array of property entries,
825 * along with their data.
827 void property_entries_free(const struct property_entry *properties)
829 const struct property_entry *p;
831 for (p = properties; p->name; p++)
832 property_entry_free_data(p);
836 EXPORT_SYMBOL_GPL(property_entries_free);
839 * pset_free_set - releases memory allocated for copied property set
840 * @pset: Property set to release
842 * Function takes previously copied property set and releases all the
843 * memory allocated to it.
845 static void pset_free_set(struct property_set *pset)
850 property_entries_free(pset->properties);
855 * pset_copy_set - copies property set
856 * @pset: Property set to copy
858 * This function takes a deep copy of the given property set and returns
859 * pointer to the copy. Call device_free_property_set() to free resources
860 * allocated in this function.
862 * Return: Pointer to the new property set or error pointer.
864 static struct property_set *pset_copy_set(const struct property_set *pset)
866 struct property_entry *properties;
867 struct property_set *p;
869 p = kzalloc(sizeof(*p), GFP_KERNEL);
871 return ERR_PTR(-ENOMEM);
873 properties = property_entries_dup(pset->properties);
874 if (IS_ERR(properties)) {
876 return ERR_CAST(properties);
879 p->properties = properties;
884 * device_remove_properties - Remove properties from a device object.
885 * @dev: Device whose properties to remove.
887 * The function removes properties previously associated to the device
888 * secondary firmware node with device_add_properties(). Memory allocated
889 * to the properties will also be released.
891 void device_remove_properties(struct device *dev)
893 struct fwnode_handle *fwnode;
895 fwnode = dev_fwnode(dev);
899 * Pick either primary or secondary node depending which one holds
900 * the pset. If there is no real firmware node (ACPI/DT) primary
901 * will hold the pset.
903 if (is_pset_node(fwnode)) {
904 set_primary_fwnode(dev, NULL);
905 pset_free_set(to_pset_node(fwnode));
907 fwnode = fwnode->secondary;
908 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
909 set_secondary_fwnode(dev, NULL);
910 pset_free_set(to_pset_node(fwnode));
914 EXPORT_SYMBOL_GPL(device_remove_properties);
917 * device_add_properties - Add a collection of properties to a device object.
918 * @dev: Device to add properties to.
919 * @properties: Collection of properties to add.
921 * Associate a collection of device properties represented by @properties with
922 * @dev as its secondary firmware node. The function takes a copy of
925 int device_add_properties(struct device *dev,
926 const struct property_entry *properties)
928 struct property_set *p, pset;
933 pset.properties = properties;
935 p = pset_copy_set(&pset);
939 p->fwnode.ops = &pset_fwnode_ops;
940 set_secondary_fwnode(dev, &p->fwnode);
943 EXPORT_SYMBOL_GPL(device_add_properties);
946 * fwnode_get_next_parent - Iterate to the node's parent
947 * @fwnode: Firmware whose parent is retrieved
949 * This is like fwnode_get_parent() except that it drops the refcount
950 * on the passed node, making it suitable for iterating through a
953 * Returns a node pointer with refcount incremented, use
954 * fwnode_handle_node() on it when done.
956 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
958 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
960 fwnode_handle_put(fwnode);
964 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
967 * fwnode_get_parent - Return parent firwmare node
968 * @fwnode: Firmware whose parent is retrieved
970 * Return parent firmware node of the given node if possible or %NULL if no
971 * parent was available.
973 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
975 return fwnode_call_ptr_op(fwnode, get_parent);
977 EXPORT_SYMBOL_GPL(fwnode_get_parent);
980 * fwnode_get_next_child_node - Return the next child node handle for a node
981 * @fwnode: Firmware node to find the next child node for.
982 * @child: Handle to one of the node's child nodes or a %NULL handle.
984 struct fwnode_handle *
985 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
986 struct fwnode_handle *child)
988 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
990 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
993 * device_get_next_child_node - Return the next child node handle for a device
994 * @dev: Device to find the next child node for.
995 * @child: Handle to one of the device's child nodes or a null handle.
997 struct fwnode_handle *device_get_next_child_node(struct device *dev,
998 struct fwnode_handle *child)
1000 struct acpi_device *adev = ACPI_COMPANION(dev);
1001 struct fwnode_handle *fwnode = NULL;
1004 fwnode = &dev->of_node->fwnode;
1006 fwnode = acpi_fwnode_handle(adev);
1008 return fwnode_get_next_child_node(fwnode, child);
1010 EXPORT_SYMBOL_GPL(device_get_next_child_node);
1013 * fwnode_get_named_child_node - Return first matching named child node handle
1014 * @fwnode: Firmware node to find the named child node for.
1015 * @childname: String to match child node name against.
1017 struct fwnode_handle *
1018 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
1019 const char *childname)
1021 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
1023 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1026 * device_get_named_child_node - Return first matching named child node handle
1027 * @dev: Device to find the named child node for.
1028 * @childname: String to match child node name against.
1030 struct fwnode_handle *device_get_named_child_node(struct device *dev,
1031 const char *childname)
1033 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1035 EXPORT_SYMBOL_GPL(device_get_named_child_node);
1038 * fwnode_handle_get - Obtain a reference to a device node
1039 * @fwnode: Pointer to the device node to obtain the reference to.
1041 void fwnode_handle_get(struct fwnode_handle *fwnode)
1043 fwnode_call_void_op(fwnode, get);
1045 EXPORT_SYMBOL_GPL(fwnode_handle_get);
1048 * fwnode_handle_put - Drop reference to a device node
1049 * @fwnode: Pointer to the device node to drop the reference to.
1051 * This has to be used when terminating device_for_each_child_node() iteration
1052 * with break or return to prevent stale device node references from being left
1055 void fwnode_handle_put(struct fwnode_handle *fwnode)
1057 fwnode_call_void_op(fwnode, put);
1059 EXPORT_SYMBOL_GPL(fwnode_handle_put);
1062 * fwnode_device_is_available - check if a device is available for use
1063 * @fwnode: Pointer to the fwnode of the device.
1065 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
1067 return fwnode_call_bool_op(fwnode, device_is_available);
1069 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1072 * device_get_child_node_count - return the number of child nodes for device
1073 * @dev: Device to cound the child nodes for
1075 unsigned int device_get_child_node_count(struct device *dev)
1077 struct fwnode_handle *child;
1078 unsigned int count = 0;
1080 device_for_each_child_node(dev, child)
1085 EXPORT_SYMBOL_GPL(device_get_child_node_count);
1087 bool device_dma_supported(struct device *dev)
1089 /* For DT, this is always supported.
1090 * For ACPI, this depends on CCA, which
1091 * is determined by the acpi_dma_supported().
1093 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1096 return acpi_dma_supported(ACPI_COMPANION(dev));
1098 EXPORT_SYMBOL_GPL(device_dma_supported);
1100 enum dev_dma_attr device_get_dma_attr(struct device *dev)
1102 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1104 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1105 if (of_dma_is_coherent(dev->of_node))
1106 attr = DEV_DMA_COHERENT;
1108 attr = DEV_DMA_NON_COHERENT;
1110 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1114 EXPORT_SYMBOL_GPL(device_get_dma_attr);
1117 * device_get_phy_mode - Get phy mode for given device
1118 * @dev: Pointer to the given device
1120 * The function gets phy interface string from property 'phy-mode' or
1121 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1124 int device_get_phy_mode(struct device *dev)
1129 err = device_property_read_string(dev, "phy-mode", &pm);
1131 err = device_property_read_string(dev,
1132 "phy-connection-type", &pm);
1136 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1137 if (!strcasecmp(pm, phy_modes(i)))
1142 EXPORT_SYMBOL_GPL(device_get_phy_mode);
1144 static void *device_get_mac_addr(struct device *dev,
1145 const char *name, char *addr,
1148 int ret = device_property_read_u8_array(dev, name, addr, alen);
1150 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
1156 * device_get_mac_address - Get the MAC for a given device
1157 * @dev: Pointer to the device
1158 * @addr: Address of buffer to store the MAC in
1159 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1161 * Search the firmware node for the best MAC address to use. 'mac-address' is
1162 * checked first, because that is supposed to contain to "most recent" MAC
1163 * address. If that isn't set, then 'local-mac-address' is checked next,
1164 * because that is the default address. If that isn't set, then the obsolete
1165 * 'address' is checked, just in case we're using an old device tree.
1167 * Note that the 'address' property is supposed to contain a virtual address of
1168 * the register set, but some DTS files have redefined that property to be the
1171 * All-zero MAC addresses are rejected, because those could be properties that
1172 * exist in the firmware tables, but were not updated by the firmware. For
1173 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1174 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1175 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1176 * exists but is all zeros.
1178 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1182 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1186 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1190 return device_get_mac_addr(dev, "address", addr, alen);
1192 EXPORT_SYMBOL(device_get_mac_address);
1195 * device_graph_get_next_endpoint - Get next endpoint firmware node
1196 * @fwnode: Pointer to the parent firmware node
1197 * @prev: Previous endpoint node or %NULL to get the first
1199 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1202 struct fwnode_handle *
1203 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1204 struct fwnode_handle *prev)
1206 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
1208 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1211 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1212 * @endpoint: Endpoint firmware node of the port
1214 * Return: the firmware node of the device the @endpoint belongs to.
1216 struct fwnode_handle *
1217 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1219 struct fwnode_handle *port, *parent;
1221 port = fwnode_get_parent(endpoint);
1222 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1224 fwnode_handle_put(port);
1228 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1231 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1232 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1234 * Extracts firmware node of a remote device the @fwnode points to.
1236 struct fwnode_handle *
1237 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1239 struct fwnode_handle *endpoint, *parent;
1241 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1242 parent = fwnode_graph_get_port_parent(endpoint);
1244 fwnode_handle_put(endpoint);
1248 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1251 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1252 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1254 * Extracts firmware node of a remote port the @fwnode points to.
1256 struct fwnode_handle *
1257 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1259 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1261 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1264 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1265 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1267 * Extracts firmware node of a remote endpoint the @fwnode points to.
1269 struct fwnode_handle *
1270 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1272 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1274 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1277 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1278 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1279 * @port_id: identifier of the parent port node
1280 * @endpoint_id: identifier of the endpoint node
1282 * Return: Remote fwnode handle associated with remote endpoint node linked
1283 * to @node. Use fwnode_node_put() on it when done.
1285 struct fwnode_handle *
1286 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1289 struct fwnode_handle *endpoint = NULL;
1291 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1292 struct fwnode_endpoint fwnode_ep;
1293 struct fwnode_handle *remote;
1296 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1300 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1303 remote = fwnode_graph_get_remote_port_parent(endpoint);
1307 return fwnode_device_is_available(remote) ? remote : NULL;
1312 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1315 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1316 * @fwnode: pointer to endpoint fwnode_handle
1317 * @endpoint: pointer to the fwnode endpoint data structure
1319 * Parse @fwnode representing a graph endpoint node and store the
1320 * information in @endpoint. The caller must hold a reference to
1323 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1324 struct fwnode_endpoint *endpoint)
1326 memset(endpoint, 0, sizeof(*endpoint));
1328 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1330 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);