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_node(struct fwnode_handle *fwnode)
42 return fwnode && fwnode->type == FWNODE_PDATA;
45 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
47 return is_pset_node(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);
138 else if (is_pset_node(fwnode))
139 return !!pset_prop_get(to_pset_node(fwnode), propname);
142 EXPORT_SYMBOL_GPL(fwnode_property_present);
145 * device_property_read_u8_array - return a u8 array property of a device
146 * @dev: Device to get the property of
147 * @propname: Name of the property
148 * @val: The values are stored here or %NULL to return the number of values
149 * @nval: Size of the @val array
151 * Function reads an array of u8 properties with @propname from the device
152 * firmware description and stores them to @val if found.
154 * Return: number of values if @val was %NULL,
155 * %0 if the property was found (success),
156 * %-EINVAL if given arguments are not valid,
157 * %-ENODATA if the property does not have a value,
158 * %-EPROTO if the property is not an array of numbers,
159 * %-EOVERFLOW if the size of the property is not as expected.
160 * %-ENXIO if no suitable firmware interface is present.
162 int device_property_read_u8_array(struct device *dev, const char *propname,
163 u8 *val, size_t nval)
165 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
167 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
170 * device_property_read_u16_array - return a u16 array property of a device
171 * @dev: Device to get the property of
172 * @propname: Name of the property
173 * @val: The values are stored here or %NULL to return the number of values
174 * @nval: Size of the @val array
176 * Function reads an array of u16 properties with @propname from the device
177 * firmware description and stores them to @val if found.
179 * Return: number of values if @val was %NULL,
180 * %0 if the property was found (success),
181 * %-EINVAL if given arguments are not valid,
182 * %-ENODATA if the property does not have a value,
183 * %-EPROTO if the property is not an array of numbers,
184 * %-EOVERFLOW if the size of the property is not as expected.
185 * %-ENXIO if no suitable firmware interface is present.
187 int device_property_read_u16_array(struct device *dev, const char *propname,
188 u16 *val, size_t nval)
190 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
192 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
195 * device_property_read_u32_array - return a u32 array property of a device
196 * @dev: Device to get the property of
197 * @propname: Name of the property
198 * @val: The values are stored here or %NULL to return the number of values
199 * @nval: Size of the @val array
201 * Function reads an array of u32 properties with @propname from the device
202 * firmware description and stores them to @val if found.
204 * Return: number of values if @val was %NULL,
205 * %0 if the property was found (success),
206 * %-EINVAL if given arguments are not valid,
207 * %-ENODATA if the property does not have a value,
208 * %-EPROTO if the property is not an array of numbers,
209 * %-EOVERFLOW if the size of the property is not as expected.
210 * %-ENXIO if no suitable firmware interface is present.
212 int device_property_read_u32_array(struct device *dev, const char *propname,
213 u32 *val, size_t nval)
215 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
217 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
220 * device_property_read_u64_array - return a u64 array property of a device
221 * @dev: Device to get the property of
222 * @propname: Name of the property
223 * @val: The values are stored here or %NULL to return the number of values
224 * @nval: Size of the @val array
226 * Function reads an array of u64 properties with @propname from the device
227 * firmware description and stores them to @val if found.
229 * Return: number of values if @val was %NULL,
230 * %0 if the property was found (success),
231 * %-EINVAL if given arguments are not valid,
232 * %-ENODATA if the property does not have a value,
233 * %-EPROTO if the property is not an array of numbers,
234 * %-EOVERFLOW if the size of the property is not as expected.
235 * %-ENXIO if no suitable firmware interface is present.
237 int device_property_read_u64_array(struct device *dev, const char *propname,
238 u64 *val, size_t nval)
240 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
242 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
245 * device_property_read_string_array - return a string array property of device
246 * @dev: Device to get the property of
247 * @propname: Name of the property
248 * @val: The values are stored here or %NULL to return the number of values
249 * @nval: Size of the @val array
251 * Function reads an array of string properties with @propname from the device
252 * firmware description and stores them to @val if found.
254 * Return: number of values if @val was %NULL,
255 * %0 if the property was found (success),
256 * %-EINVAL if given arguments are not valid,
257 * %-ENODATA if the property does not have a value,
258 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
259 * %-EOVERFLOW if the size of the property is not as expected.
260 * %-ENXIO if no suitable firmware interface is present.
262 int device_property_read_string_array(struct device *dev, const char *propname,
263 const char **val, size_t nval)
265 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
267 EXPORT_SYMBOL_GPL(device_property_read_string_array);
270 * device_property_read_string - return a string property of a device
271 * @dev: Device to get the property of
272 * @propname: Name of the property
273 * @val: The value is stored here
275 * Function reads property @propname from the device firmware description and
276 * stores the value into @val if found. The value is checked to be a string.
278 * Return: %0 if the property was found (success),
279 * %-EINVAL if given arguments are not valid,
280 * %-ENODATA if the property does not have a value,
281 * %-EPROTO or %-EILSEQ if the property type is not a string.
282 * %-ENXIO if no suitable firmware interface is present.
284 int device_property_read_string(struct device *dev, const char *propname,
287 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
289 EXPORT_SYMBOL_GPL(device_property_read_string);
292 * device_property_match_string - find a string in an array and return index
293 * @dev: Device to get the property of
294 * @propname: Name of the property holding the array
295 * @string: String to look for
297 * Find a given string in a string array and if it is found return the
300 * Return: %0 if the property was found (success),
301 * %-EINVAL if given arguments are not valid,
302 * %-ENODATA if the property does not have a value,
303 * %-EPROTO if the property is not an array of strings,
304 * %-ENXIO if no suitable firmware interface is present.
306 int device_property_match_string(struct device *dev, const char *propname,
309 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
311 EXPORT_SYMBOL_GPL(device_property_match_string);
313 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
314 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
315 : of_property_count_elems_of_size((node), (propname), sizeof(type))
317 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
320 if (is_of_node(_fwnode_)) \
321 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
322 _type_, _val_, _nval_); \
323 else if (is_acpi_node(_fwnode_)) \
324 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
326 else if (is_pset_node(_fwnode_)) \
327 _ret_ = pset_prop_read_array(to_pset_node(_fwnode_), _propname_, \
328 _proptype_, _val_, _nval_); \
335 * fwnode_property_read_u8_array - return a u8 array property of firmware node
336 * @fwnode: Firmware node to get the property of
337 * @propname: Name of the property
338 * @val: The values are stored here or %NULL to return the number of values
339 * @nval: Size of the @val array
341 * Read an array of u8 properties with @propname from @fwnode and stores them to
344 * Return: number of values if @val was %NULL,
345 * %0 if the property was found (success),
346 * %-EINVAL if given arguments are not valid,
347 * %-ENODATA if the property does not have a value,
348 * %-EPROTO if the property is not an array of numbers,
349 * %-EOVERFLOW if the size of the property is not as expected,
350 * %-ENXIO if no suitable firmware interface is present.
352 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
353 const char *propname, u8 *val, size_t nval)
355 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
358 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
361 * fwnode_property_read_u16_array - return a u16 array property of firmware node
362 * @fwnode: Firmware node to get the property of
363 * @propname: Name of the property
364 * @val: The values are stored here or %NULL to return the number of values
365 * @nval: Size of the @val array
367 * Read an array of u16 properties with @propname from @fwnode and store them to
370 * Return: number of values if @val was %NULL,
371 * %0 if the property was found (success),
372 * %-EINVAL if given arguments are not valid,
373 * %-ENODATA if the property does not have a value,
374 * %-EPROTO if the property is not an array of numbers,
375 * %-EOVERFLOW if the size of the property is not as expected,
376 * %-ENXIO if no suitable firmware interface is present.
378 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
379 const char *propname, u16 *val, size_t nval)
381 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
384 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
387 * fwnode_property_read_u32_array - return a u32 array property of firmware node
388 * @fwnode: Firmware node to get the property of
389 * @propname: Name of the property
390 * @val: The values are stored here or %NULL to return the number of values
391 * @nval: Size of the @val array
393 * Read an array of u32 properties with @propname from @fwnode store them to
396 * Return: number of values if @val was %NULL,
397 * %0 if the property was found (success),
398 * %-EINVAL if given arguments are not valid,
399 * %-ENODATA if the property does not have a value,
400 * %-EPROTO if the property is not an array of numbers,
401 * %-EOVERFLOW if the size of the property is not as expected,
402 * %-ENXIO if no suitable firmware interface is present.
404 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
405 const char *propname, u32 *val, size_t nval)
407 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
410 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
413 * fwnode_property_read_u64_array - return a u64 array property firmware node
414 * @fwnode: Firmware node to get the property of
415 * @propname: Name of the property
416 * @val: The values are stored here or %NULL to return the number of values
417 * @nval: Size of the @val array
419 * Read an array of u64 properties with @propname from @fwnode and store them to
422 * Return: number of values if @val was %NULL,
423 * %0 if the property was found (success),
424 * %-EINVAL if given arguments are not valid,
425 * %-ENODATA if the property does not have a value,
426 * %-EPROTO if the property is not an array of numbers,
427 * %-EOVERFLOW if the size of the property is not as expected,
428 * %-ENXIO if no suitable firmware interface is present.
430 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
431 const char *propname, u64 *val, size_t nval)
433 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
436 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
439 * fwnode_property_read_string_array - return string array property of a 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 string list property @propname from the given firmware node and store
446 * them to @val if found.
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 strings,
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_string_array(struct fwnode_handle *fwnode,
457 const char *propname, const char **val,
460 if (is_of_node(fwnode))
462 of_property_read_string_array(to_of_node(fwnode),
463 propname, val, nval) :
464 of_property_count_strings(to_of_node(fwnode), propname);
465 else if (is_acpi_node(fwnode))
466 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
468 else if (is_pset_node(fwnode))
469 return pset_prop_read_array(to_pset_node(fwnode), propname,
470 DEV_PROP_STRING, val, nval);
473 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
476 * fwnode_property_read_string - return a string property of a firmware node
477 * @fwnode: Firmware node to get the property of
478 * @propname: Name of the property
479 * @val: The value is stored here
481 * Read property @propname from the given firmware node and store the value into
482 * @val if found. The value is checked to be a string.
484 * Return: %0 if the property was found (success),
485 * %-EINVAL if given arguments are not valid,
486 * %-ENODATA if the property does not have a value,
487 * %-EPROTO or %-EILSEQ if the property is not a string,
488 * %-ENXIO if no suitable firmware interface is present.
490 int fwnode_property_read_string(struct fwnode_handle *fwnode,
491 const char *propname, const char **val)
493 if (is_of_node(fwnode))
494 return of_property_read_string(to_of_node(fwnode), propname, val);
495 else if (is_acpi_node(fwnode))
496 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
498 else if (is_pset_node(fwnode))
499 return pset_prop_read_array(to_pset_node(fwnode), propname,
500 DEV_PROP_STRING, val, 1);
503 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
506 * fwnode_property_match_string - find a string in an array and return index
507 * @fwnode: Firmware node to get the property of
508 * @propname: Name of the property holding the array
509 * @string: String to look for
511 * Find a given string in a string array and if it is found return the
514 * Return: %0 if the property was found (success),
515 * %-EINVAL if given arguments are not valid,
516 * %-ENODATA if the property does not have a value,
517 * %-EPROTO if the property is not an array of strings,
518 * %-ENXIO if no suitable firmware interface is present.
520 int fwnode_property_match_string(struct fwnode_handle *fwnode,
521 const char *propname, const char *string)
526 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
530 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
534 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
539 for (i = 0; i < nval; i++) {
540 if (!strcmp(values[i], string)) {
549 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
552 * device_get_next_child_node - Return the next child node handle for a device
553 * @dev: Device to find the next child node for.
554 * @child: Handle to one of the device's child nodes or a null handle.
556 struct fwnode_handle *device_get_next_child_node(struct device *dev,
557 struct fwnode_handle *child)
559 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
560 struct device_node *node;
562 node = of_get_next_available_child(dev->of_node, to_of_node(child));
564 return &node->fwnode;
565 } else if (IS_ENABLED(CONFIG_ACPI)) {
566 return acpi_get_next_subnode(dev, child);
570 EXPORT_SYMBOL_GPL(device_get_next_child_node);
573 * fwnode_handle_put - Drop reference to a device node
574 * @fwnode: Pointer to the device node to drop the reference to.
576 * This has to be used when terminating device_for_each_child_node() iteration
577 * with break or return to prevent stale device node references from being left
580 void fwnode_handle_put(struct fwnode_handle *fwnode)
582 if (is_of_node(fwnode))
583 of_node_put(to_of_node(fwnode));
585 EXPORT_SYMBOL_GPL(fwnode_handle_put);
588 * device_get_child_node_count - return the number of child nodes for device
589 * @dev: Device to cound the child nodes for
591 unsigned int device_get_child_node_count(struct device *dev)
593 struct fwnode_handle *child;
594 unsigned int count = 0;
596 device_for_each_child_node(dev, child)
601 EXPORT_SYMBOL_GPL(device_get_child_node_count);
603 bool device_dma_supported(struct device *dev)
605 /* For DT, this is always supported.
606 * For ACPI, this depends on CCA, which
607 * is determined by the acpi_dma_supported().
609 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
612 return acpi_dma_supported(ACPI_COMPANION(dev));
614 EXPORT_SYMBOL_GPL(device_dma_supported);
616 enum dev_dma_attr device_get_dma_attr(struct device *dev)
618 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
620 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
621 if (of_dma_is_coherent(dev->of_node))
622 attr = DEV_DMA_COHERENT;
624 attr = DEV_DMA_NON_COHERENT;
626 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
630 EXPORT_SYMBOL_GPL(device_get_dma_attr);
633 * device_get_phy_mode - Get phy mode for given device
634 * @dev: Pointer to the given device
636 * The function gets phy interface string from property 'phy-mode' or
637 * 'phy-connection-type', and return its index in phy_modes table, or errno in
640 int device_get_phy_mode(struct device *dev)
645 err = device_property_read_string(dev, "phy-mode", &pm);
647 err = device_property_read_string(dev,
648 "phy-connection-type", &pm);
652 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
653 if (!strcasecmp(pm, phy_modes(i)))
658 EXPORT_SYMBOL_GPL(device_get_phy_mode);
660 static void *device_get_mac_addr(struct device *dev,
661 const char *name, char *addr,
664 int ret = device_property_read_u8_array(dev, name, addr, alen);
666 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
672 * device_get_mac_address - Get the MAC for a given device
673 * @dev: Pointer to the device
674 * @addr: Address of buffer to store the MAC in
675 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
677 * Search the firmware node for the best MAC address to use. 'mac-address' is
678 * checked first, because that is supposed to contain to "most recent" MAC
679 * address. If that isn't set, then 'local-mac-address' is checked next,
680 * because that is the default address. If that isn't set, then the obsolete
681 * 'address' is checked, just in case we're using an old device tree.
683 * Note that the 'address' property is supposed to contain a virtual address of
684 * the register set, but some DTS files have redefined that property to be the
687 * All-zero MAC addresses are rejected, because those could be properties that
688 * exist in the firmware tables, but were not updated by the firmware. For
689 * example, the DTS could define 'mac-address' and 'local-mac-address', with
690 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
691 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
692 * exists but is all zeros.
694 void *device_get_mac_address(struct device *dev, char *addr, int alen)
698 res = device_get_mac_addr(dev, "mac-address", addr, alen);
702 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
706 return device_get_mac_addr(dev, "address", addr, alen);
708 EXPORT_SYMBOL(device_get_mac_address);