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.
159 int device_property_read_u8_array(struct device *dev, const char *propname,
160 u8 *val, size_t nval)
162 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
164 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
167 * device_property_read_u16_array - return a u16 array property of a device
168 * @dev: Device to get the property of
169 * @propname: Name of the property
170 * @val: The values are stored here or %NULL to return the number of values
171 * @nval: Size of the @val array
173 * Function reads an array of u16 properties with @propname from the device
174 * firmware description and stores them to @val if found.
176 * Return: number of values if @val was %NULL,
177 * %0 if the property was found (success),
178 * %-EINVAL if given arguments are not valid,
179 * %-ENODATA if the property does not have a value,
180 * %-EPROTO if the property is not an array of numbers,
181 * %-EOVERFLOW if the size of the property is not as expected.
183 int device_property_read_u16_array(struct device *dev, const char *propname,
184 u16 *val, size_t nval)
186 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
188 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
191 * device_property_read_u32_array - return a u32 array property of a device
192 * @dev: Device to get the property of
193 * @propname: Name of the property
194 * @val: The values are stored here or %NULL to return the number of values
195 * @nval: Size of the @val array
197 * Function reads an array of u32 properties with @propname from the device
198 * firmware description and stores them to @val if found.
200 * Return: number of values if @val was %NULL,
201 * %0 if the property was found (success),
202 * %-EINVAL if given arguments are not valid,
203 * %-ENODATA if the property does not have a value,
204 * %-EPROTO if the property is not an array of numbers,
205 * %-EOVERFLOW if the size of the property is not as expected.
207 int device_property_read_u32_array(struct device *dev, const char *propname,
208 u32 *val, size_t nval)
210 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
212 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
215 * device_property_read_u64_array - return a u64 array property of a device
216 * @dev: Device to get the property of
217 * @propname: Name of the property
218 * @val: The values are stored here or %NULL to return the number of values
219 * @nval: Size of the @val array
221 * Function reads an array of u64 properties with @propname from the device
222 * firmware description and stores them to @val if found.
224 * Return: number of values if @val was %NULL,
225 * %0 if the property was found (success),
226 * %-EINVAL if given arguments are not valid,
227 * %-ENODATA if the property does not have a value,
228 * %-EPROTO if the property is not an array of numbers,
229 * %-EOVERFLOW if the size of the property is not as expected.
231 int device_property_read_u64_array(struct device *dev, const char *propname,
232 u64 *val, size_t nval)
234 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
236 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
239 * device_property_read_string_array - return a string array property of device
240 * @dev: Device to get the property of
241 * @propname: Name of the property
242 * @val: The values are stored here or %NULL to return the number of values
243 * @nval: Size of the @val array
245 * Function reads an array of string properties with @propname from the device
246 * firmware description and stores them to @val if found.
248 * Return: number of values if @val was %NULL,
249 * %0 if the property was found (success),
250 * %-EINVAL if given arguments are not valid,
251 * %-ENODATA if the property does not have a value,
252 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
253 * %-EOVERFLOW if the size of the property is not as expected.
255 int device_property_read_string_array(struct device *dev, const char *propname,
256 const char **val, size_t nval)
258 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
260 EXPORT_SYMBOL_GPL(device_property_read_string_array);
263 * device_property_read_string - return a string property of a device
264 * @dev: Device to get the property of
265 * @propname: Name of the property
266 * @val: The value is stored here
268 * Function reads property @propname from the device firmware description and
269 * stores the value into @val if found. The value is checked to be a string.
271 * Return: %0 if the property was found (success),
272 * %-EINVAL if given arguments are not valid,
273 * %-ENODATA if the property does not have a value,
274 * %-EPROTO or %-EILSEQ if the property type is not a string.
276 int device_property_read_string(struct device *dev, const char *propname,
279 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
281 EXPORT_SYMBOL_GPL(device_property_read_string);
283 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
284 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
285 : of_property_count_elems_of_size((node), (propname), sizeof(type))
287 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
290 if (is_of_node(_fwnode_)) \
291 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
292 _type_, _val_, _nval_); \
293 else if (is_acpi_node(_fwnode_)) \
294 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
295 _proptype_, _val_, _nval_); \
297 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
298 _proptype_, _val_, _nval_); \
303 * fwnode_property_read_u8_array - return a u8 array property of firmware node
304 * @fwnode: Firmware node to get the property of
305 * @propname: Name of the property
306 * @val: The values are stored here or %NULL to return the number of values
307 * @nval: Size of the @val array
309 * Read an array of u8 properties with @propname from @fwnode and stores them to
312 * Return: number of values if @val was %NULL,
313 * %0 if the property was found (success),
314 * %-EINVAL if given arguments are not valid,
315 * %-ENODATA if the property does not have a value,
316 * %-EPROTO if the property is not an array of numbers,
317 * %-EOVERFLOW if the size of the property is not as expected,
318 * %-ENXIO if no suitable firmware interface is present.
320 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
321 const char *propname, u8 *val, size_t nval)
323 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
326 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
329 * fwnode_property_read_u16_array - return a u16 array property of firmware node
330 * @fwnode: Firmware node to get the property of
331 * @propname: Name of the property
332 * @val: The values are stored here or %NULL to return the number of values
333 * @nval: Size of the @val array
335 * Read an array of u16 properties with @propname from @fwnode and store them to
338 * Return: number of values if @val was %NULL,
339 * %0 if the property was found (success),
340 * %-EINVAL if given arguments are not valid,
341 * %-ENODATA if the property does not have a value,
342 * %-EPROTO if the property is not an array of numbers,
343 * %-EOVERFLOW if the size of the property is not as expected,
344 * %-ENXIO if no suitable firmware interface is present.
346 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
347 const char *propname, u16 *val, size_t nval)
349 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
352 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
355 * fwnode_property_read_u32_array - return a u32 array property of firmware node
356 * @fwnode: Firmware node to get the property of
357 * @propname: Name of the property
358 * @val: The values are stored here or %NULL to return the number of values
359 * @nval: Size of the @val array
361 * Read an array of u32 properties with @propname from @fwnode store them to
364 * Return: number of values if @val was %NULL,
365 * %0 if the property was found (success),
366 * %-EINVAL if given arguments are not valid,
367 * %-ENODATA if the property does not have a value,
368 * %-EPROTO if the property is not an array of numbers,
369 * %-EOVERFLOW if the size of the property is not as expected,
370 * %-ENXIO if no suitable firmware interface is present.
372 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
373 const char *propname, u32 *val, size_t nval)
375 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
378 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
381 * fwnode_property_read_u64_array - return a u64 array property firmware node
382 * @fwnode: Firmware node to get the property of
383 * @propname: Name of the property
384 * @val: The values are stored here or %NULL to return the number of values
385 * @nval: Size of the @val array
387 * Read an array of u64 properties with @propname from @fwnode and store them to
390 * Return: number of values if @val was %NULL,
391 * %0 if the property was found (success),
392 * %-EINVAL if given arguments are not valid,
393 * %-ENODATA if the property does not have a value,
394 * %-EPROTO if the property is not an array of numbers,
395 * %-EOVERFLOW if the size of the property is not as expected,
396 * %-ENXIO if no suitable firmware interface is present.
398 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
399 const char *propname, u64 *val, size_t nval)
401 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
404 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
407 * fwnode_property_read_string_array - return string array property of a node
408 * @fwnode: Firmware node to get the property of
409 * @propname: Name of the property
410 * @val: The values are stored here or %NULL to return the number of values
411 * @nval: Size of the @val array
413 * Read an string list property @propname from the given firmware node and store
414 * them to @val if found.
416 * Return: number of values if @val was %NULL,
417 * %0 if the property was found (success),
418 * %-EINVAL if given arguments are not valid,
419 * %-ENODATA if the property does not have a value,
420 * %-EPROTO if the property is not an array of strings,
421 * %-EOVERFLOW if the size of the property is not as expected,
422 * %-ENXIO if no suitable firmware interface is present.
424 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
425 const char *propname, const char **val,
428 if (is_of_node(fwnode))
430 of_property_read_string_array(to_of_node(fwnode),
431 propname, val, nval) :
432 of_property_count_strings(to_of_node(fwnode), propname);
433 else if (is_acpi_node(fwnode))
434 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
435 DEV_PROP_STRING, val, nval);
437 return pset_prop_read_array(to_pset(fwnode), propname,
438 DEV_PROP_STRING, val, nval);
440 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
443 * fwnode_property_read_string - return a string property of a firmware node
444 * @fwnode: Firmware node to get the property of
445 * @propname: Name of the property
446 * @val: The value is stored here
448 * Read property @propname from the given firmware node and store the value into
449 * @val if found. The value is checked to be a string.
451 * Return: %0 if the property was found (success),
452 * %-EINVAL if given arguments are not valid,
453 * %-ENODATA if the property does not have a value,
454 * %-EPROTO or %-EILSEQ if the property is not a string,
455 * %-ENXIO if no suitable firmware interface is present.
457 int fwnode_property_read_string(struct fwnode_handle *fwnode,
458 const char *propname, const char **val)
460 if (is_of_node(fwnode))
461 return of_property_read_string(to_of_node(fwnode), propname, val);
462 else if (is_acpi_node(fwnode))
463 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
464 DEV_PROP_STRING, val, 1);
468 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
471 * device_get_next_child_node - Return the next child node handle for a device
472 * @dev: Device to find the next child node for.
473 * @child: Handle to one of the device's child nodes or a null handle.
475 struct fwnode_handle *device_get_next_child_node(struct device *dev,
476 struct fwnode_handle *child)
478 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
479 struct device_node *node;
481 node = of_get_next_available_child(dev->of_node, to_of_node(child));
483 return &node->fwnode;
484 } else if (IS_ENABLED(CONFIG_ACPI)) {
485 struct acpi_device *node;
487 node = acpi_get_next_child(dev, to_acpi_node(child));
489 return acpi_fwnode_handle(node);
493 EXPORT_SYMBOL_GPL(device_get_next_child_node);
496 * fwnode_handle_put - Drop reference to a device node
497 * @fwnode: Pointer to the device node to drop the reference to.
499 * This has to be used when terminating device_for_each_child_node() iteration
500 * with break or return to prevent stale device node references from being left
503 void fwnode_handle_put(struct fwnode_handle *fwnode)
505 if (is_of_node(fwnode))
506 of_node_put(to_of_node(fwnode));
508 EXPORT_SYMBOL_GPL(fwnode_handle_put);
511 * device_get_child_node_count - return the number of child nodes for device
512 * @dev: Device to cound the child nodes for
514 unsigned int device_get_child_node_count(struct device *dev)
516 struct fwnode_handle *child;
517 unsigned int count = 0;
519 device_for_each_child_node(dev, child)
524 EXPORT_SYMBOL_GPL(device_get_child_node_count);
526 bool device_dma_is_coherent(struct device *dev)
528 bool coherent = false;
530 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
531 coherent = of_dma_is_coherent(dev->of_node);
533 acpi_check_dma(ACPI_COMPANION(dev), &coherent);
537 EXPORT_SYMBOL_GPL(device_dma_is_coherent);
540 * device_get_phy_mode - Get phy mode for given device
541 * @dev: Pointer to the given device
543 * The function gets phy interface string from property 'phy-mode' or
544 * 'phy-connection-type', and return its index in phy_modes table, or errno in
547 int device_get_phy_mode(struct device *dev)
552 err = device_property_read_string(dev, "phy-mode", &pm);
554 err = device_property_read_string(dev,
555 "phy-connection-type", &pm);
559 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
560 if (!strcasecmp(pm, phy_modes(i)))
565 EXPORT_SYMBOL_GPL(device_get_phy_mode);
567 static void *device_get_mac_addr(struct device *dev,
568 const char *name, char *addr,
571 int ret = device_property_read_u8_array(dev, name, addr, alen);
573 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
579 * device_get_mac_address - Get the MAC for a given device
580 * @dev: Pointer to the device
581 * @addr: Address of buffer to store the MAC in
582 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
584 * Search the firmware node for the best MAC address to use. 'mac-address' is
585 * checked first, because that is supposed to contain to "most recent" MAC
586 * address. If that isn't set, then 'local-mac-address' is checked next,
587 * because that is the default address. If that isn't set, then the obsolete
588 * 'address' is checked, just in case we're using an old device tree.
590 * Note that the 'address' property is supposed to contain a virtual address of
591 * the register set, but some DTS files have redefined that property to be the
594 * All-zero MAC addresses are rejected, because those could be properties that
595 * exist in the firmware tables, but were not updated by the firmware. For
596 * example, the DTS could define 'mac-address' and 'local-mac-address', with
597 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
598 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
599 * exists but is all zeros.
601 void *device_get_mac_address(struct device *dev, char *addr, int alen)
603 addr = device_get_mac_addr(dev, "mac-address", addr, alen);
607 addr = device_get_mac_addr(dev, "local-mac-address", addr, alen);
611 return device_get_mac_addr(dev, "address", addr, alen);
613 EXPORT_SYMBOL(device_get_mac_address);