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>
21 * device_add_property_set - Add a collection of properties to a device object.
22 * @dev: Device to add properties to.
23 * @pset: Collection of properties to add.
25 * Associate a collection of device properties represented by @pset with @dev
26 * as its secondary firmware node.
28 void device_add_property_set(struct device *dev, struct property_set *pset)
33 pset->fwnode.type = FWNODE_PDATA;
34 set_secondary_fwnode(dev, &pset->fwnode);
36 EXPORT_SYMBOL_GPL(device_add_property_set);
38 static inline bool is_pset(struct fwnode_handle *fwnode)
40 return fwnode && fwnode->type == FWNODE_PDATA;
43 static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
45 return is_pset(fwnode) ?
46 container_of(fwnode, struct property_set, fwnode) : NULL;
49 static struct property_entry *pset_prop_get(struct property_set *pset,
52 struct property_entry *prop;
54 if (!pset || !pset->properties)
57 for (prop = pset->properties; prop->name; prop++)
58 if (!strcmp(name, prop->name))
64 static int pset_prop_read_array(struct property_set *pset, const char *name,
65 enum dev_prop_type type, void *val, size_t nval)
67 struct property_entry *prop;
68 unsigned int item_size;
70 prop = pset_prop_get(pset, name);
74 if (prop->type != type)
80 if (prop->nval < nval)
85 item_size = sizeof(u8);
88 item_size = sizeof(u16);
91 item_size = sizeof(u32);
94 item_size = sizeof(u64);
97 item_size = sizeof(const char *);
102 memcpy(val, prop->value.raw_data, nval * item_size);
106 static inline struct fwnode_handle *dev_fwnode(struct device *dev)
108 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
109 &dev->of_node->fwnode : dev->fwnode;
113 * device_property_present - check if a property of a device is present
114 * @dev: Device whose property is being checked
115 * @propname: Name of the property
117 * Check if property @propname is present in the device firmware description.
119 bool device_property_present(struct device *dev, const char *propname)
121 return fwnode_property_present(dev_fwnode(dev), propname);
123 EXPORT_SYMBOL_GPL(device_property_present);
126 * fwnode_property_present - check if a property of a firmware node is present
127 * @fwnode: Firmware node whose property to check
128 * @propname: Name of the property
130 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
132 if (is_of_node(fwnode))
133 return of_property_read_bool(to_of_node(fwnode), propname);
134 else if (is_acpi_node(fwnode))
135 return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL);
137 return !!pset_prop_get(to_pset(fwnode), propname);
139 EXPORT_SYMBOL_GPL(fwnode_property_present);
142 * device_property_read_u8_array - return a u8 array property of a device
143 * @dev: Device to get the property of
144 * @propname: Name of the property
145 * @val: The values are stored here or %NULL to return the number of values
146 * @nval: Size of the @val array
148 * Function reads an array of u8 properties with @propname from the device
149 * firmware description and stores them to @val if found.
151 * Return: number of values if @val was %NULL,
152 * %0 if the property was found (success),
153 * %-EINVAL if given arguments are not valid,
154 * %-ENODATA if the property does not have a value,
155 * %-EPROTO if the property is not an array of numbers,
156 * %-EOVERFLOW if the size of the property is not as expected.
158 int device_property_read_u8_array(struct device *dev, const char *propname,
159 u8 *val, size_t nval)
161 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
163 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
166 * device_property_read_u16_array - return a u16 array property of a device
167 * @dev: Device to get the property of
168 * @propname: Name of the property
169 * @val: The values are stored here or %NULL to return the number of values
170 * @nval: Size of the @val array
172 * Function reads an array of u16 properties with @propname from the device
173 * firmware description and stores them to @val if found.
175 * Return: number of values if @val was %NULL,
176 * %0 if the property was found (success),
177 * %-EINVAL if given arguments are not valid,
178 * %-ENODATA if the property does not have a value,
179 * %-EPROTO if the property is not an array of numbers,
180 * %-EOVERFLOW if the size of the property is not as expected.
182 int device_property_read_u16_array(struct device *dev, const char *propname,
183 u16 *val, size_t nval)
185 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
187 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
190 * device_property_read_u32_array - return a u32 array property of a device
191 * @dev: Device to get the property of
192 * @propname: Name of the property
193 * @val: The values are stored here or %NULL to return the number of values
194 * @nval: Size of the @val array
196 * Function reads an array of u32 properties with @propname from the device
197 * firmware description and stores them to @val if found.
199 * Return: number of values if @val was %NULL,
200 * %0 if the property was found (success),
201 * %-EINVAL if given arguments are not valid,
202 * %-ENODATA if the property does not have a value,
203 * %-EPROTO if the property is not an array of numbers,
204 * %-EOVERFLOW if the size of the property is not as expected.
206 int device_property_read_u32_array(struct device *dev, const char *propname,
207 u32 *val, size_t nval)
209 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
211 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
214 * device_property_read_u64_array - return a u64 array property of a device
215 * @dev: Device to get the property of
216 * @propname: Name of the property
217 * @val: The values are stored here or %NULL to return the number of values
218 * @nval: Size of the @val array
220 * Function reads an array of u64 properties with @propname from the device
221 * firmware description and stores them to @val if found.
223 * Return: number of values if @val was %NULL,
224 * %0 if the property was found (success),
225 * %-EINVAL if given arguments are not valid,
226 * %-ENODATA if the property does not have a value,
227 * %-EPROTO if the property is not an array of numbers,
228 * %-EOVERFLOW if the size of the property is not as expected.
230 int device_property_read_u64_array(struct device *dev, const char *propname,
231 u64 *val, size_t nval)
233 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
235 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
238 * device_property_read_string_array - return a string array property of device
239 * @dev: Device to get the property of
240 * @propname: Name of the property
241 * @val: The values are stored here or %NULL to return the number of values
242 * @nval: Size of the @val array
244 * Function reads an array of string properties with @propname from the device
245 * firmware description and stores them to @val if found.
247 * Return: number of values if @val was %NULL,
248 * %0 if the property was found (success),
249 * %-EINVAL if given arguments are not valid,
250 * %-ENODATA if the property does not have a value,
251 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
252 * %-EOVERFLOW if the size of the property is not as expected.
254 int device_property_read_string_array(struct device *dev, const char *propname,
255 const char **val, size_t nval)
257 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
259 EXPORT_SYMBOL_GPL(device_property_read_string_array);
262 * device_property_read_string - return a string property of a device
263 * @dev: Device to get the property of
264 * @propname: Name of the property
265 * @val: The value is stored here
267 * Function reads property @propname from the device firmware description and
268 * stores the value into @val if found. The value is checked to be a string.
270 * Return: %0 if the property was found (success),
271 * %-EINVAL if given arguments are not valid,
272 * %-ENODATA if the property does not have a value,
273 * %-EPROTO or %-EILSEQ if the property type is not a string.
275 int device_property_read_string(struct device *dev, const char *propname,
278 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
280 EXPORT_SYMBOL_GPL(device_property_read_string);
282 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
283 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
284 : of_property_count_elems_of_size((node), (propname), sizeof(type))
286 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
289 if (is_of_node(_fwnode_)) \
290 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
291 _type_, _val_, _nval_); \
292 else if (is_acpi_node(_fwnode_)) \
293 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
294 _proptype_, _val_, _nval_); \
296 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
297 _proptype_, _val_, _nval_); \
302 * fwnode_property_read_u8_array - return a u8 array property of firmware node
303 * @fwnode: Firmware node to get the property of
304 * @propname: Name of the property
305 * @val: The values are stored here or %NULL to return the number of values
306 * @nval: Size of the @val array
308 * Read an array of u8 properties with @propname from @fwnode and stores them to
311 * Return: number of values if @val was %NULL,
312 * %0 if the property was found (success),
313 * %-EINVAL if given arguments are not valid,
314 * %-ENODATA if the property does not have a value,
315 * %-EPROTO if the property is not an array of numbers,
316 * %-EOVERFLOW if the size of the property is not as expected,
317 * %-ENXIO if no suitable firmware interface is present.
319 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
320 const char *propname, u8 *val, size_t nval)
322 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
325 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
328 * fwnode_property_read_u16_array - return a u16 array property of firmware node
329 * @fwnode: Firmware node to get the property of
330 * @propname: Name of the property
331 * @val: The values are stored here or %NULL to return the number of values
332 * @nval: Size of the @val array
334 * Read an array of u16 properties with @propname from @fwnode and store them to
337 * Return: number of values if @val was %NULL,
338 * %0 if the property was found (success),
339 * %-EINVAL if given arguments are not valid,
340 * %-ENODATA if the property does not have a value,
341 * %-EPROTO if the property is not an array of numbers,
342 * %-EOVERFLOW if the size of the property is not as expected,
343 * %-ENXIO if no suitable firmware interface is present.
345 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
346 const char *propname, u16 *val, size_t nval)
348 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
351 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
354 * fwnode_property_read_u32_array - return a u32 array property of firmware node
355 * @fwnode: Firmware node to get the property of
356 * @propname: Name of the property
357 * @val: The values are stored here or %NULL to return the number of values
358 * @nval: Size of the @val array
360 * Read an array of u32 properties with @propname from @fwnode store them to
363 * Return: number of values if @val was %NULL,
364 * %0 if the property was found (success),
365 * %-EINVAL if given arguments are not valid,
366 * %-ENODATA if the property does not have a value,
367 * %-EPROTO if the property is not an array of numbers,
368 * %-EOVERFLOW if the size of the property is not as expected,
369 * %-ENXIO if no suitable firmware interface is present.
371 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
372 const char *propname, u32 *val, size_t nval)
374 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
377 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
380 * fwnode_property_read_u64_array - return a u64 array property firmware node
381 * @fwnode: Firmware node to get the property of
382 * @propname: Name of the property
383 * @val: The values are stored here or %NULL to return the number of values
384 * @nval: Size of the @val array
386 * Read an array of u64 properties with @propname from @fwnode and store them to
389 * Return: number of values if @val was %NULL,
390 * %0 if the property was found (success),
391 * %-EINVAL if given arguments are not valid,
392 * %-ENODATA if the property does not have a value,
393 * %-EPROTO if the property is not an array of numbers,
394 * %-EOVERFLOW if the size of the property is not as expected,
395 * %-ENXIO if no suitable firmware interface is present.
397 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
398 const char *propname, u64 *val, size_t nval)
400 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
403 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
406 * fwnode_property_read_string_array - return string array property of a node
407 * @fwnode: Firmware node to get the property of
408 * @propname: Name of the property
409 * @val: The values are stored here or %NULL to return the number of values
410 * @nval: Size of the @val array
412 * Read an string list property @propname from the given firmware node and store
413 * them to @val if found.
415 * Return: number of values if @val was %NULL,
416 * %0 if the property was found (success),
417 * %-EINVAL if given arguments are not valid,
418 * %-ENODATA if the property does not have a value,
419 * %-EPROTO if the property is not an array of strings,
420 * %-EOVERFLOW if the size of the property is not as expected,
421 * %-ENXIO if no suitable firmware interface is present.
423 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
424 const char *propname, const char **val,
427 if (is_of_node(fwnode))
429 of_property_read_string_array(to_of_node(fwnode),
430 propname, val, nval) :
431 of_property_count_strings(to_of_node(fwnode), propname);
432 else if (is_acpi_node(fwnode))
433 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
434 DEV_PROP_STRING, val, nval);
436 return pset_prop_read_array(to_pset(fwnode), propname,
437 DEV_PROP_STRING, val, nval);
439 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
442 * fwnode_property_read_string - return a string property of a firmware node
443 * @fwnode: Firmware node to get the property of
444 * @propname: Name of the property
445 * @val: The value is stored here
447 * Read property @propname from the given firmware node and store the value into
448 * @val if found. The value is checked to be a string.
450 * Return: %0 if the property was found (success),
451 * %-EINVAL if given arguments are not valid,
452 * %-ENODATA if the property does not have a value,
453 * %-EPROTO or %-EILSEQ if the property is not a string,
454 * %-ENXIO if no suitable firmware interface is present.
456 int fwnode_property_read_string(struct fwnode_handle *fwnode,
457 const char *propname, const char **val)
459 if (is_of_node(fwnode))
460 return of_property_read_string(to_of_node(fwnode), propname, val);
461 else if (is_acpi_node(fwnode))
462 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
463 DEV_PROP_STRING, val, 1);
465 return pset_prop_read_array(to_pset(fwnode), propname,
466 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);