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)
33 pset->fwnode.type = FWNODE_PDATA;
35 set_secondary_fwnode(dev, &pset->fwnode);
37 EXPORT_SYMBOL_GPL(device_add_property_set);
39 static inline bool is_pset(struct fwnode_handle *fwnode)
41 return fwnode && fwnode->type == FWNODE_PDATA;
44 static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
46 return is_pset(fwnode) ?
47 container_of(fwnode, struct property_set, fwnode) : NULL;
50 static struct property_entry *pset_prop_get(struct property_set *pset,
53 struct property_entry *prop;
55 if (!pset || !pset->properties)
58 for (prop = pset->properties; prop->name; prop++)
59 if (!strcmp(name, prop->name))
65 static int pset_prop_read_array(struct property_set *pset, const char *name,
66 enum dev_prop_type type, void *val, size_t nval)
68 struct property_entry *prop;
69 unsigned int item_size;
71 prop = pset_prop_get(pset, name);
75 if (prop->type != type)
81 if (prop->nval < nval)
86 item_size = sizeof(u8);
89 item_size = sizeof(u16);
92 item_size = sizeof(u32);
95 item_size = sizeof(u64);
98 item_size = sizeof(const char *);
103 memcpy(val, prop->value.raw_data, nval * item_size);
107 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
109 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
110 &dev->of_node->fwnode : dev->fwnode;
114 * device_property_present - check if a property of a device is present
115 * @dev: Device whose property is being checked
116 * @propname: Name of the property
118 * Check if property @propname is present in the device firmware description.
120 bool device_property_present(struct device *dev, const char *propname)
122 return fwnode_property_present(dev_fwnode(dev), propname);
124 EXPORT_SYMBOL_GPL(device_property_present);
127 * fwnode_property_present - check if a property of a firmware node is present
128 * @fwnode: Firmware node whose property to check
129 * @propname: Name of the property
131 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
133 if (is_of_node(fwnode))
134 return of_property_read_bool(to_of_node(fwnode), propname);
135 else if (is_acpi_node(fwnode))
136 return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL);
138 return !!pset_prop_get(to_pset(fwnode), propname);
140 EXPORT_SYMBOL_GPL(fwnode_property_present);
143 * device_property_read_u8_array - return a u8 array property of a device
144 * @dev: Device to get the property of
145 * @propname: Name of the property
146 * @val: The values are stored here or %NULL to return the number of values
147 * @nval: Size of the @val array
149 * Function reads an array of u8 properties with @propname from the device
150 * firmware description and stores them to @val if found.
152 * Return: number of values if @val was %NULL,
153 * %0 if the property was found (success),
154 * %-EINVAL if given arguments are not valid,
155 * %-ENODATA if the property does not have a value,
156 * %-EPROTO if the property is not an array of numbers,
157 * %-EOVERFLOW if the size of the property is not as expected.
158 * %-ENXIO if no suitable firmware interface is present.
160 int device_property_read_u8_array(struct device *dev, const char *propname,
161 u8 *val, size_t nval)
163 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
165 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
168 * device_property_read_u16_array - return a u16 array property of a device
169 * @dev: Device to get the property of
170 * @propname: Name of the property
171 * @val: The values are stored here or %NULL to return the number of values
172 * @nval: Size of the @val array
174 * Function reads an array of u16 properties with @propname from the device
175 * firmware description and stores them to @val if found.
177 * Return: number of values if @val was %NULL,
178 * %0 if the property was found (success),
179 * %-EINVAL if given arguments are not valid,
180 * %-ENODATA if the property does not have a value,
181 * %-EPROTO if the property is not an array of numbers,
182 * %-EOVERFLOW if the size of the property is not as expected.
183 * %-ENXIO if no suitable firmware interface is present.
185 int device_property_read_u16_array(struct device *dev, const char *propname,
186 u16 *val, size_t nval)
188 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
190 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
193 * device_property_read_u32_array - return a u32 array property of a device
194 * @dev: Device to get the property of
195 * @propname: Name of the property
196 * @val: The values are stored here or %NULL to return the number of values
197 * @nval: Size of the @val array
199 * Function reads an array of u32 properties with @propname from the device
200 * firmware description and stores them to @val if found.
202 * Return: number of values if @val was %NULL,
203 * %0 if the property was found (success),
204 * %-EINVAL if given arguments are not valid,
205 * %-ENODATA if the property does not have a value,
206 * %-EPROTO if the property is not an array of numbers,
207 * %-EOVERFLOW if the size of the property is not as expected.
208 * %-ENXIO if no suitable firmware interface is present.
210 int device_property_read_u32_array(struct device *dev, const char *propname,
211 u32 *val, size_t nval)
213 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
215 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
218 * device_property_read_u64_array - return a u64 array property of a device
219 * @dev: Device to get the property of
220 * @propname: Name of the property
221 * @val: The values are stored here or %NULL to return the number of values
222 * @nval: Size of the @val array
224 * Function reads an array of u64 properties with @propname from the device
225 * firmware description and stores them to @val if found.
227 * Return: number of values if @val was %NULL,
228 * %0 if the property was found (success),
229 * %-EINVAL if given arguments are not valid,
230 * %-ENODATA if the property does not have a value,
231 * %-EPROTO if the property is not an array of numbers,
232 * %-EOVERFLOW if the size of the property is not as expected.
233 * %-ENXIO if no suitable firmware interface is present.
235 int device_property_read_u64_array(struct device *dev, const char *propname,
236 u64 *val, size_t nval)
238 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
240 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
243 * device_property_read_string_array - return a string array property of device
244 * @dev: Device to get the property of
245 * @propname: Name of the property
246 * @val: The values are stored here or %NULL to return the number of values
247 * @nval: Size of the @val array
249 * Function reads an array of string properties with @propname from the device
250 * firmware description and stores them to @val if found.
252 * Return: number of values if @val was %NULL,
253 * %0 if the property was found (success),
254 * %-EINVAL if given arguments are not valid,
255 * %-ENODATA if the property does not have a value,
256 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
257 * %-EOVERFLOW if the size of the property is not as expected.
258 * %-ENXIO if no suitable firmware interface is present.
260 int device_property_read_string_array(struct device *dev, const char *propname,
261 const char **val, size_t nval)
263 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
265 EXPORT_SYMBOL_GPL(device_property_read_string_array);
268 * device_property_read_string - return a string property of a device
269 * @dev: Device to get the property of
270 * @propname: Name of the property
271 * @val: The value is stored here
273 * Function reads property @propname from the device firmware description and
274 * stores the value into @val if found. The value is checked to be a string.
276 * Return: %0 if the property was found (success),
277 * %-EINVAL if given arguments are not valid,
278 * %-ENODATA if the property does not have a value,
279 * %-EPROTO or %-EILSEQ if the property type is not a string.
280 * %-ENXIO if no suitable firmware interface is present.
282 int device_property_read_string(struct device *dev, const char *propname,
285 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
287 EXPORT_SYMBOL_GPL(device_property_read_string);
289 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
290 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
291 : of_property_count_elems_of_size((node), (propname), sizeof(type))
293 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
296 if (is_of_node(_fwnode_)) \
297 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
298 _type_, _val_, _nval_); \
299 else if (is_acpi_node(_fwnode_)) \
300 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
301 _proptype_, _val_, _nval_); \
302 else if (is_pset(_fwnode_)) \
303 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
304 _proptype_, _val_, _nval_); \
311 * fwnode_property_read_u8_array - return a u8 array property of firmware node
312 * @fwnode: Firmware node to get the property of
313 * @propname: Name of the property
314 * @val: The values are stored here or %NULL to return the number of values
315 * @nval: Size of the @val array
317 * Read an array of u8 properties with @propname from @fwnode and stores them to
320 * Return: number of values if @val was %NULL,
321 * %0 if the property was found (success),
322 * %-EINVAL if given arguments are not valid,
323 * %-ENODATA if the property does not have a value,
324 * %-EPROTO if the property is not an array of numbers,
325 * %-EOVERFLOW if the size of the property is not as expected,
326 * %-ENXIO if no suitable firmware interface is present.
328 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
329 const char *propname, u8 *val, size_t nval)
331 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
334 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
337 * fwnode_property_read_u16_array - return a u16 array property of firmware node
338 * @fwnode: Firmware node to get the property of
339 * @propname: Name of the property
340 * @val: The values are stored here or %NULL to return the number of values
341 * @nval: Size of the @val array
343 * Read an array of u16 properties with @propname from @fwnode and store them to
346 * Return: number of values if @val was %NULL,
347 * %0 if the property was found (success),
348 * %-EINVAL if given arguments are not valid,
349 * %-ENODATA if the property does not have a value,
350 * %-EPROTO if the property is not an array of numbers,
351 * %-EOVERFLOW if the size of the property is not as expected,
352 * %-ENXIO if no suitable firmware interface is present.
354 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
355 const char *propname, u16 *val, size_t nval)
357 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
360 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
363 * fwnode_property_read_u32_array - return a u32 array property of firmware node
364 * @fwnode: Firmware node to get the property of
365 * @propname: Name of the property
366 * @val: The values are stored here or %NULL to return the number of values
367 * @nval: Size of the @val array
369 * Read an array of u32 properties with @propname from @fwnode store them to
372 * Return: number of values if @val was %NULL,
373 * %0 if the property was found (success),
374 * %-EINVAL if given arguments are not valid,
375 * %-ENODATA if the property does not have a value,
376 * %-EPROTO if the property is not an array of numbers,
377 * %-EOVERFLOW if the size of the property is not as expected,
378 * %-ENXIO if no suitable firmware interface is present.
380 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
381 const char *propname, u32 *val, size_t nval)
383 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
386 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
389 * fwnode_property_read_u64_array - return a u64 array property firmware node
390 * @fwnode: Firmware node to get the property of
391 * @propname: Name of the property
392 * @val: The values are stored here or %NULL to return the number of values
393 * @nval: Size of the @val array
395 * Read an array of u64 properties with @propname from @fwnode and store them to
398 * Return: number of values if @val was %NULL,
399 * %0 if the property was found (success),
400 * %-EINVAL if given arguments are not valid,
401 * %-ENODATA if the property does not have a value,
402 * %-EPROTO if the property is not an array of numbers,
403 * %-EOVERFLOW if the size of the property is not as expected,
404 * %-ENXIO if no suitable firmware interface is present.
406 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
407 const char *propname, u64 *val, size_t nval)
409 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
412 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
415 * fwnode_property_read_string_array - return string array property of a node
416 * @fwnode: Firmware node to get the property of
417 * @propname: Name of the property
418 * @val: The values are stored here or %NULL to return the number of values
419 * @nval: Size of the @val array
421 * Read an string list property @propname from the given firmware node and store
422 * them to @val if found.
424 * Return: number of values if @val was %NULL,
425 * %0 if the property was found (success),
426 * %-EINVAL if given arguments are not valid,
427 * %-ENODATA if the property does not have a value,
428 * %-EPROTO if the property is not an array of strings,
429 * %-EOVERFLOW if the size of the property is not as expected,
430 * %-ENXIO if no suitable firmware interface is present.
432 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
433 const char *propname, const char **val,
436 if (is_of_node(fwnode))
438 of_property_read_string_array(to_of_node(fwnode),
439 propname, val, nval) :
440 of_property_count_strings(to_of_node(fwnode), propname);
441 else if (is_acpi_node(fwnode))
442 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
443 DEV_PROP_STRING, val, nval);
444 else if (is_pset(fwnode))
445 return pset_prop_read_array(to_pset(fwnode), propname,
446 DEV_PROP_STRING, val, nval);
449 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
452 * fwnode_property_read_string - return a string property of a firmware node
453 * @fwnode: Firmware node to get the property of
454 * @propname: Name of the property
455 * @val: The value is stored here
457 * Read property @propname from the given firmware node and store the value into
458 * @val if found. The value is checked to be a string.
460 * Return: %0 if the property was found (success),
461 * %-EINVAL if given arguments are not valid,
462 * %-ENODATA if the property does not have a value,
463 * %-EPROTO or %-EILSEQ if the property is not a string,
464 * %-ENXIO if no suitable firmware interface is present.
466 int fwnode_property_read_string(struct fwnode_handle *fwnode,
467 const char *propname, const char **val)
469 if (is_of_node(fwnode))
470 return of_property_read_string(to_of_node(fwnode), propname, val);
471 else if (is_acpi_node(fwnode))
472 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
473 DEV_PROP_STRING, val, 1);
477 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
480 * device_get_next_child_node - Return the next child node handle for a device
481 * @dev: Device to find the next child node for.
482 * @child: Handle to one of the device's child nodes or a null handle.
484 struct fwnode_handle *device_get_next_child_node(struct device *dev,
485 struct fwnode_handle *child)
487 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
488 struct device_node *node;
490 node = of_get_next_available_child(dev->of_node, to_of_node(child));
492 return &node->fwnode;
493 } else if (IS_ENABLED(CONFIG_ACPI)) {
494 struct acpi_device *node;
496 node = acpi_get_next_child(dev, to_acpi_node(child));
498 return acpi_fwnode_handle(node);
502 EXPORT_SYMBOL_GPL(device_get_next_child_node);
505 * fwnode_handle_put - Drop reference to a device node
506 * @fwnode: Pointer to the device node to drop the reference to.
508 * This has to be used when terminating device_for_each_child_node() iteration
509 * with break or return to prevent stale device node references from being left
512 void fwnode_handle_put(struct fwnode_handle *fwnode)
514 if (is_of_node(fwnode))
515 of_node_put(to_of_node(fwnode));
517 EXPORT_SYMBOL_GPL(fwnode_handle_put);
520 * device_get_child_node_count - return the number of child nodes for device
521 * @dev: Device to cound the child nodes for
523 unsigned int device_get_child_node_count(struct device *dev)
525 struct fwnode_handle *child;
526 unsigned int count = 0;
528 device_for_each_child_node(dev, child)
533 EXPORT_SYMBOL_GPL(device_get_child_node_count);
535 bool device_dma_is_coherent(struct device *dev)
537 bool coherent = false;
539 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
540 coherent = of_dma_is_coherent(dev->of_node);
542 acpi_check_dma(ACPI_COMPANION(dev), &coherent);
546 EXPORT_SYMBOL_GPL(device_dma_is_coherent);
549 * device_get_phy_mode - Get phy mode for given device
550 * @dev: Pointer to the given device
552 * The function gets phy interface string from property 'phy-mode' or
553 * 'phy-connection-type', and return its index in phy_modes table, or errno in
556 int device_get_phy_mode(struct device *dev)
561 err = device_property_read_string(dev, "phy-mode", &pm);
563 err = device_property_read_string(dev,
564 "phy-connection-type", &pm);
568 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
569 if (!strcasecmp(pm, phy_modes(i)))
574 EXPORT_SYMBOL_GPL(device_get_phy_mode);
576 static void *device_get_mac_addr(struct device *dev,
577 const char *name, char *addr,
580 int ret = device_property_read_u8_array(dev, name, addr, alen);
582 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
588 * device_get_mac_address - Get the MAC for a given device
589 * @dev: Pointer to the device
590 * @addr: Address of buffer to store the MAC in
591 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
593 * Search the firmware node for the best MAC address to use. 'mac-address' is
594 * checked first, because that is supposed to contain to "most recent" MAC
595 * address. If that isn't set, then 'local-mac-address' is checked next,
596 * because that is the default address. If that isn't set, then the obsolete
597 * 'address' is checked, just in case we're using an old device tree.
599 * Note that the 'address' property is supposed to contain a virtual address of
600 * the register set, but some DTS files have redefined that property to be the
603 * All-zero MAC addresses are rejected, because those could be properties that
604 * exist in the firmware tables, but were not updated by the firmware. For
605 * example, the DTS could define 'mac-address' and 'local-mac-address', with
606 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
607 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
608 * exists but is all zeros.
610 void *device_get_mac_address(struct device *dev, char *addr, int alen)
612 addr = device_get_mac_addr(dev, "mac-address", addr, alen);
616 addr = device_get_mac_addr(dev, "local-mac-address", addr, alen);
620 return device_get_mac_addr(dev, "address", addr, alen);
622 EXPORT_SYMBOL(device_get_mac_address);