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/property.h>
19 #include <linux/etherdevice.h>
20 #include <linux/phy.h>
23 * device_add_property_set - Add a collection of properties to a device object.
24 * @dev: Device to add properties to.
25 * @pset: Collection of properties to add.
27 * Associate a collection of device properties represented by @pset with @dev
28 * as its secondary firmware node.
30 void device_add_property_set(struct device
*dev
, struct property_set
*pset
)
35 pset
->fwnode
.type
= FWNODE_PDATA
;
36 set_secondary_fwnode(dev
, &pset
->fwnode
);
38 EXPORT_SYMBOL_GPL(device_add_property_set
);
40 static inline bool is_pset(struct fwnode_handle
*fwnode
)
42 return fwnode
&& fwnode
->type
== FWNODE_PDATA
;
45 static inline struct property_set
*to_pset(struct fwnode_handle
*fwnode
)
47 return is_pset(fwnode
) ?
48 container_of(fwnode
, struct property_set
, fwnode
) : NULL
;
51 static struct property_entry
*pset_prop_get(struct property_set
*pset
,
54 struct property_entry
*prop
;
56 if (!pset
|| !pset
->properties
)
59 for (prop
= pset
->properties
; prop
->name
; prop
++)
60 if (!strcmp(name
, prop
->name
))
66 static int pset_prop_read_array(struct property_set
*pset
, const char *name
,
67 enum dev_prop_type type
, void *val
, size_t nval
)
69 struct property_entry
*prop
;
70 unsigned int item_size
;
72 prop
= pset_prop_get(pset
, name
);
76 if (prop
->type
!= type
)
82 if (prop
->nval
< nval
)
87 item_size
= sizeof(u8
);
90 item_size
= sizeof(u16
);
93 item_size
= sizeof(u32
);
96 item_size
= sizeof(u64
);
99 item_size
= sizeof(const char *);
104 memcpy(val
, prop
->value
.raw_data
, nval
* item_size
);
108 static inline struct fwnode_handle
*dev_fwnode(struct device
*dev
)
110 return IS_ENABLED(CONFIG_OF
) && dev
->of_node
?
111 &dev
->of_node
->fwnode
: dev
->fwnode
;
115 * device_property_present - check if a property of a device is present
116 * @dev: Device whose property is being checked
117 * @propname: Name of the property
119 * Check if property @propname is present in the device firmware description.
121 bool device_property_present(struct device
*dev
, const char *propname
)
123 return fwnode_property_present(dev_fwnode(dev
), propname
);
125 EXPORT_SYMBOL_GPL(device_property_present
);
128 * fwnode_property_present - check if a property of a firmware node is present
129 * @fwnode: Firmware node whose property to check
130 * @propname: Name of the property
132 bool fwnode_property_present(struct fwnode_handle
*fwnode
, const char *propname
)
134 if (is_of_node(fwnode
))
135 return of_property_read_bool(to_of_node(fwnode
), propname
);
136 else if (is_acpi_node(fwnode
))
137 return !acpi_node_prop_get(fwnode
, propname
, NULL
);
139 return !!pset_prop_get(to_pset(fwnode
), propname
);
141 EXPORT_SYMBOL_GPL(fwnode_property_present
);
144 * device_property_read_u8_array - return a u8 array property of a device
145 * @dev: Device to get the property of
146 * @propname: Name of the property
147 * @val: The values are stored here or %NULL to return the number of values
148 * @nval: Size of the @val array
150 * Function reads an array of u8 properties with @propname from the device
151 * firmware description and stores them to @val if found.
153 * Return: number of values if @val was %NULL,
154 * %0 if the property was found (success),
155 * %-EINVAL if given arguments are not valid,
156 * %-ENODATA if the property does not have a value,
157 * %-EPROTO if the property is not an array of numbers,
158 * %-EOVERFLOW if the size of the property is not as expected.
159 * %-ENXIO if no suitable firmware interface is present.
161 int device_property_read_u8_array(struct device
*dev
, const char *propname
,
162 u8
*val
, size_t nval
)
164 return fwnode_property_read_u8_array(dev_fwnode(dev
), propname
, val
, nval
);
166 EXPORT_SYMBOL_GPL(device_property_read_u8_array
);
169 * device_property_read_u16_array - return a u16 array property of a device
170 * @dev: Device to get the property of
171 * @propname: Name of the property
172 * @val: The values are stored here or %NULL to return the number of values
173 * @nval: Size of the @val array
175 * Function reads an array of u16 properties with @propname from the device
176 * firmware description and stores them to @val if found.
178 * Return: number of values if @val was %NULL,
179 * %0 if the property was found (success),
180 * %-EINVAL if given arguments are not valid,
181 * %-ENODATA if the property does not have a value,
182 * %-EPROTO if the property is not an array of numbers,
183 * %-EOVERFLOW if the size of the property is not as expected.
184 * %-ENXIO if no suitable firmware interface is present.
186 int device_property_read_u16_array(struct device
*dev
, const char *propname
,
187 u16
*val
, size_t nval
)
189 return fwnode_property_read_u16_array(dev_fwnode(dev
), propname
, val
, nval
);
191 EXPORT_SYMBOL_GPL(device_property_read_u16_array
);
194 * device_property_read_u32_array - return a u32 array property of a device
195 * @dev: Device to get the property of
196 * @propname: Name of the property
197 * @val: The values are stored here or %NULL to return the number of values
198 * @nval: Size of the @val array
200 * Function reads an array of u32 properties with @propname from the device
201 * firmware description and stores them to @val if found.
203 * Return: number of values if @val was %NULL,
204 * %0 if the property was found (success),
205 * %-EINVAL if given arguments are not valid,
206 * %-ENODATA if the property does not have a value,
207 * %-EPROTO if the property is not an array of numbers,
208 * %-EOVERFLOW if the size of the property is not as expected.
209 * %-ENXIO if no suitable firmware interface is present.
211 int device_property_read_u32_array(struct device
*dev
, const char *propname
,
212 u32
*val
, size_t nval
)
214 return fwnode_property_read_u32_array(dev_fwnode(dev
), propname
, val
, nval
);
216 EXPORT_SYMBOL_GPL(device_property_read_u32_array
);
219 * device_property_read_u64_array - return a u64 array property of a device
220 * @dev: Device to get the property of
221 * @propname: Name of the property
222 * @val: The values are stored here or %NULL to return the number of values
223 * @nval: Size of the @val array
225 * Function reads an array of u64 properties with @propname from the device
226 * firmware description and stores them to @val if found.
228 * Return: number of values if @val was %NULL,
229 * %0 if the property was found (success),
230 * %-EINVAL if given arguments are not valid,
231 * %-ENODATA if the property does not have a value,
232 * %-EPROTO if the property is not an array of numbers,
233 * %-EOVERFLOW if the size of the property is not as expected.
234 * %-ENXIO if no suitable firmware interface is present.
236 int device_property_read_u64_array(struct device
*dev
, const char *propname
,
237 u64
*val
, size_t nval
)
239 return fwnode_property_read_u64_array(dev_fwnode(dev
), propname
, val
, nval
);
241 EXPORT_SYMBOL_GPL(device_property_read_u64_array
);
244 * device_property_read_string_array - return a string array property of device
245 * @dev: Device to get the property of
246 * @propname: Name of the property
247 * @val: The values are stored here or %NULL to return the number of values
248 * @nval: Size of the @val array
250 * Function reads an array of string properties with @propname from the device
251 * firmware description and stores them to @val if found.
253 * Return: number of values if @val was %NULL,
254 * %0 if the property was found (success),
255 * %-EINVAL if given arguments are not valid,
256 * %-ENODATA if the property does not have a value,
257 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
258 * %-EOVERFLOW if the size of the property is not as expected.
259 * %-ENXIO if no suitable firmware interface is present.
261 int device_property_read_string_array(struct device
*dev
, const char *propname
,
262 const char **val
, size_t nval
)
264 return fwnode_property_read_string_array(dev_fwnode(dev
), propname
, val
, nval
);
266 EXPORT_SYMBOL_GPL(device_property_read_string_array
);
269 * device_property_read_string - return a string property of a device
270 * @dev: Device to get the property of
271 * @propname: Name of the property
272 * @val: The value is stored here
274 * Function reads property @propname from the device firmware description and
275 * stores the value into @val if found. The value is checked to be a string.
277 * Return: %0 if the property was found (success),
278 * %-EINVAL if given arguments are not valid,
279 * %-ENODATA if the property does not have a value,
280 * %-EPROTO or %-EILSEQ if the property type is not a string.
281 * %-ENXIO if no suitable firmware interface is present.
283 int device_property_read_string(struct device
*dev
, const char *propname
,
286 return fwnode_property_read_string(dev_fwnode(dev
), propname
, val
);
288 EXPORT_SYMBOL_GPL(device_property_read_string
);
291 * device_property_match_string - find a string in an array and return index
292 * @dev: Device to get the property of
293 * @propname: Name of the property holding the array
294 * @string: String to look for
296 * Find a given string in a string array and if it is found return the
299 * Return: %0 if the property was found (success),
300 * %-EINVAL if given arguments are not valid,
301 * %-ENODATA if the property does not have a value,
302 * %-EPROTO if the property is not an array of strings,
303 * %-ENXIO if no suitable firmware interface is present.
305 int device_property_match_string(struct device
*dev
, const char *propname
,
308 return fwnode_property_match_string(dev_fwnode(dev
), propname
, string
);
310 EXPORT_SYMBOL_GPL(device_property_match_string
);
312 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
313 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
314 : of_property_count_elems_of_size((node), (propname), sizeof(type))
316 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
319 if (is_of_node(_fwnode_)) \
320 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
321 _type_, _val_, _nval_); \
322 else if (is_acpi_node(_fwnode_)) \
323 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
325 else if (is_pset(_fwnode_)) \
326 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
327 _proptype_, _val_, _nval_); \
334 * fwnode_property_read_u8_array - return a u8 array property of firmware node
335 * @fwnode: Firmware node to get the property of
336 * @propname: Name of the property
337 * @val: The values are stored here or %NULL to return the number of values
338 * @nval: Size of the @val array
340 * Read an array of u8 properties with @propname from @fwnode and stores them to
343 * Return: number of values if @val was %NULL,
344 * %0 if the property was found (success),
345 * %-EINVAL if given arguments are not valid,
346 * %-ENODATA if the property does not have a value,
347 * %-EPROTO if the property is not an array of numbers,
348 * %-EOVERFLOW if the size of the property is not as expected,
349 * %-ENXIO if no suitable firmware interface is present.
351 int fwnode_property_read_u8_array(struct fwnode_handle
*fwnode
,
352 const char *propname
, u8
*val
, size_t nval
)
354 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u8
, DEV_PROP_U8
,
357 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array
);
360 * fwnode_property_read_u16_array - return a u16 array property of firmware node
361 * @fwnode: Firmware node to get the property of
362 * @propname: Name of the property
363 * @val: The values are stored here or %NULL to return the number of values
364 * @nval: Size of the @val array
366 * Read an array of u16 properties with @propname from @fwnode and store them to
369 * Return: number of values if @val was %NULL,
370 * %0 if the property was found (success),
371 * %-EINVAL if given arguments are not valid,
372 * %-ENODATA if the property does not have a value,
373 * %-EPROTO if the property is not an array of numbers,
374 * %-EOVERFLOW if the size of the property is not as expected,
375 * %-ENXIO if no suitable firmware interface is present.
377 int fwnode_property_read_u16_array(struct fwnode_handle
*fwnode
,
378 const char *propname
, u16
*val
, size_t nval
)
380 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u16
, DEV_PROP_U16
,
383 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array
);
386 * fwnode_property_read_u32_array - return a u32 array property of firmware node
387 * @fwnode: Firmware node to get the property of
388 * @propname: Name of the property
389 * @val: The values are stored here or %NULL to return the number of values
390 * @nval: Size of the @val array
392 * Read an array of u32 properties with @propname from @fwnode store them to
395 * Return: number of values if @val was %NULL,
396 * %0 if the property was found (success),
397 * %-EINVAL if given arguments are not valid,
398 * %-ENODATA if the property does not have a value,
399 * %-EPROTO if the property is not an array of numbers,
400 * %-EOVERFLOW if the size of the property is not as expected,
401 * %-ENXIO if no suitable firmware interface is present.
403 int fwnode_property_read_u32_array(struct fwnode_handle
*fwnode
,
404 const char *propname
, u32
*val
, size_t nval
)
406 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u32
, DEV_PROP_U32
,
409 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array
);
412 * fwnode_property_read_u64_array - return a u64 array property firmware node
413 * @fwnode: Firmware node to get the property of
414 * @propname: Name of the property
415 * @val: The values are stored here or %NULL to return the number of values
416 * @nval: Size of the @val array
418 * Read an array of u64 properties with @propname from @fwnode and store them to
421 * Return: number of values if @val was %NULL,
422 * %0 if the property was found (success),
423 * %-EINVAL if given arguments are not valid,
424 * %-ENODATA if the property does not have a value,
425 * %-EPROTO if the property is not an array of numbers,
426 * %-EOVERFLOW if the size of the property is not as expected,
427 * %-ENXIO if no suitable firmware interface is present.
429 int fwnode_property_read_u64_array(struct fwnode_handle
*fwnode
,
430 const char *propname
, u64
*val
, size_t nval
)
432 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u64
, DEV_PROP_U64
,
435 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array
);
438 * fwnode_property_read_string_array - return string array property of a node
439 * @fwnode: Firmware node to get the property of
440 * @propname: Name of the property
441 * @val: The values are stored here or %NULL to return the number of values
442 * @nval: Size of the @val array
444 * Read an string list property @propname from the given firmware node and store
445 * them to @val if found.
447 * Return: number of values if @val was %NULL,
448 * %0 if the property was found (success),
449 * %-EINVAL if given arguments are not valid,
450 * %-ENODATA if the property does not have a value,
451 * %-EPROTO if the property is not an array of strings,
452 * %-EOVERFLOW if the size of the property is not as expected,
453 * %-ENXIO if no suitable firmware interface is present.
455 int fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
456 const char *propname
, const char **val
,
459 if (is_of_node(fwnode
))
461 of_property_read_string_array(to_of_node(fwnode
),
462 propname
, val
, nval
) :
463 of_property_count_strings(to_of_node(fwnode
), propname
);
464 else if (is_acpi_node(fwnode
))
465 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
467 else if (is_pset(fwnode
))
468 return pset_prop_read_array(to_pset(fwnode
), propname
,
469 DEV_PROP_STRING
, val
, nval
);
472 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array
);
475 * fwnode_property_read_string - return a string property of a firmware node
476 * @fwnode: Firmware node to get the property of
477 * @propname: Name of the property
478 * @val: The value is stored here
480 * Read property @propname from the given firmware node and store the value into
481 * @val if found. The value is checked to be a string.
483 * Return: %0 if the property was found (success),
484 * %-EINVAL if given arguments are not valid,
485 * %-ENODATA if the property does not have a value,
486 * %-EPROTO or %-EILSEQ if the property is not a string,
487 * %-ENXIO if no suitable firmware interface is present.
489 int fwnode_property_read_string(struct fwnode_handle
*fwnode
,
490 const char *propname
, const char **val
)
492 if (is_of_node(fwnode
))
493 return of_property_read_string(to_of_node(fwnode
), propname
, val
);
494 else if (is_acpi_node(fwnode
))
495 return acpi_node_prop_read(fwnode
, propname
, DEV_PROP_STRING
,
498 return pset_prop_read_array(to_pset(fwnode
), propname
,
499 DEV_PROP_STRING
, val
, 1);
501 EXPORT_SYMBOL_GPL(fwnode_property_read_string
);
504 * fwnode_property_match_string - find a string in an array and return index
505 * @fwnode: Firmware node to get the property of
506 * @propname: Name of the property holding the array
507 * @string: String to look for
509 * Find a given string in a string array and if it is found return the
512 * Return: %0 if the property was found (success),
513 * %-EINVAL if given arguments are not valid,
514 * %-ENODATA if the property does not have a value,
515 * %-EPROTO if the property is not an array of strings,
516 * %-ENXIO if no suitable firmware interface is present.
518 int fwnode_property_match_string(struct fwnode_handle
*fwnode
,
519 const char *propname
, const char *string
)
524 nval
= fwnode_property_read_string_array(fwnode
, propname
, NULL
, 0);
528 values
= kcalloc(nval
, sizeof(*values
), GFP_KERNEL
);
532 ret
= fwnode_property_read_string_array(fwnode
, propname
, values
, nval
);
537 for (i
= 0; i
< nval
; i
++) {
538 if (!strcmp(values
[i
], string
)) {
547 EXPORT_SYMBOL_GPL(fwnode_property_match_string
);
550 * device_get_next_child_node - Return the next child node handle for a device
551 * @dev: Device to find the next child node for.
552 * @child: Handle to one of the device's child nodes or a null handle.
554 struct fwnode_handle
*device_get_next_child_node(struct device
*dev
,
555 struct fwnode_handle
*child
)
557 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
558 struct device_node
*node
;
560 node
= of_get_next_available_child(dev
->of_node
, to_of_node(child
));
562 return &node
->fwnode
;
563 } else if (IS_ENABLED(CONFIG_ACPI
)) {
564 return acpi_get_next_subnode(dev
, child
);
568 EXPORT_SYMBOL_GPL(device_get_next_child_node
);
571 * fwnode_handle_put - Drop reference to a device node
572 * @fwnode: Pointer to the device node to drop the reference to.
574 * This has to be used when terminating device_for_each_child_node() iteration
575 * with break or return to prevent stale device node references from being left
578 void fwnode_handle_put(struct fwnode_handle
*fwnode
)
580 if (is_of_node(fwnode
))
581 of_node_put(to_of_node(fwnode
));
583 EXPORT_SYMBOL_GPL(fwnode_handle_put
);
586 * device_get_child_node_count - return the number of child nodes for device
587 * @dev: Device to cound the child nodes for
589 unsigned int device_get_child_node_count(struct device
*dev
)
591 struct fwnode_handle
*child
;
592 unsigned int count
= 0;
594 device_for_each_child_node(dev
, child
)
599 EXPORT_SYMBOL_GPL(device_get_child_node_count
);
601 bool device_dma_supported(struct device
*dev
)
603 /* For DT, this is always supported.
604 * For ACPI, this depends on CCA, which
605 * is determined by the acpi_dma_supported().
607 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
)
610 return acpi_dma_supported(ACPI_COMPANION(dev
));
612 EXPORT_SYMBOL_GPL(device_dma_supported
);
614 enum dev_dma_attr
device_get_dma_attr(struct device
*dev
)
616 enum dev_dma_attr attr
= DEV_DMA_NOT_SUPPORTED
;
618 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
619 if (of_dma_is_coherent(dev
->of_node
))
620 attr
= DEV_DMA_COHERENT
;
622 attr
= DEV_DMA_NON_COHERENT
;
624 attr
= acpi_get_dma_attr(ACPI_COMPANION(dev
));
628 EXPORT_SYMBOL_GPL(device_get_dma_attr
);
631 * device_get_phy_mode - Get phy mode for given device
632 * @dev: Pointer to the given device
634 * The function gets phy interface string from property 'phy-mode' or
635 * 'phy-connection-type', and return its index in phy_modes table, or errno in
638 int device_get_phy_mode(struct device
*dev
)
643 err
= device_property_read_string(dev
, "phy-mode", &pm
);
645 err
= device_property_read_string(dev
,
646 "phy-connection-type", &pm
);
650 for (i
= 0; i
< PHY_INTERFACE_MODE_MAX
; i
++)
651 if (!strcasecmp(pm
, phy_modes(i
)))
656 EXPORT_SYMBOL_GPL(device_get_phy_mode
);
658 static void *device_get_mac_addr(struct device
*dev
,
659 const char *name
, char *addr
,
662 int ret
= device_property_read_u8_array(dev
, name
, addr
, alen
);
664 if (ret
== 0 && alen
== ETH_ALEN
&& is_valid_ether_addr(addr
))
670 * device_get_mac_address - Get the MAC for a given device
671 * @dev: Pointer to the device
672 * @addr: Address of buffer to store the MAC in
673 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
675 * Search the firmware node for the best MAC address to use. 'mac-address' is
676 * checked first, because that is supposed to contain to "most recent" MAC
677 * address. If that isn't set, then 'local-mac-address' is checked next,
678 * because that is the default address. If that isn't set, then the obsolete
679 * 'address' is checked, just in case we're using an old device tree.
681 * Note that the 'address' property is supposed to contain a virtual address of
682 * the register set, but some DTS files have redefined that property to be the
685 * All-zero MAC addresses are rejected, because those could be properties that
686 * exist in the firmware tables, but were not updated by the firmware. For
687 * example, the DTS could define 'mac-address' and 'local-mac-address', with
688 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
689 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
690 * exists but is all zeros.
692 void *device_get_mac_address(struct device
*dev
, char *addr
, int alen
)
696 res
= device_get_mac_addr(dev
, "mac-address", addr
, alen
);
700 res
= device_get_mac_addr(dev
, "local-mac-address", addr
, alen
);
704 return device_get_mac_addr(dev
, "address", addr
, alen
);
706 EXPORT_SYMBOL(device_get_mac_address
);