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/property.h>
20 * device_add_property_set - Add a collection of properties to a device object.
21 * @dev: Device to add properties to.
22 * @pset: Collection of properties to add.
24 * Associate a collection of device properties represented by @pset with @dev
25 * as its secondary firmware node.
27 void device_add_property_set(struct device
*dev
, struct property_set
*pset
)
32 pset
->fwnode
.type
= FWNODE_PDATA
;
33 set_secondary_fwnode(dev
, &pset
->fwnode
);
35 EXPORT_SYMBOL_GPL(device_add_property_set
);
37 static inline bool is_pset(struct fwnode_handle
*fwnode
)
39 return fwnode
&& fwnode
->type
== FWNODE_PDATA
;
42 static inline struct property_set
*to_pset(struct fwnode_handle
*fwnode
)
44 return is_pset(fwnode
) ?
45 container_of(fwnode
, struct property_set
, fwnode
) : NULL
;
48 static struct property_entry
*pset_prop_get(struct property_set
*pset
,
51 struct property_entry
*prop
;
53 if (!pset
|| !pset
->properties
)
56 for (prop
= pset
->properties
; prop
->name
; prop
++)
57 if (!strcmp(name
, prop
->name
))
63 static int pset_prop_read_array(struct property_set
*pset
, const char *name
,
64 enum dev_prop_type type
, void *val
, size_t nval
)
66 struct property_entry
*prop
;
67 unsigned int item_size
;
69 prop
= pset_prop_get(pset
, name
);
73 if (prop
->type
!= type
)
79 if (prop
->nval
< nval
)
84 item_size
= sizeof(u8
);
87 item_size
= sizeof(u16
);
90 item_size
= sizeof(u32
);
93 item_size
= sizeof(u64
);
96 item_size
= sizeof(const char *);
101 memcpy(val
, prop
->value
.raw_data
, nval
* item_size
);
105 static inline struct fwnode_handle
*dev_fwnode(struct device
*dev
)
107 return IS_ENABLED(CONFIG_OF
) && dev
->of_node
?
108 &dev
->of_node
->fwnode
: dev
->fwnode
;
112 * device_property_present - check if a property of a device is present
113 * @dev: Device whose property is being checked
114 * @propname: Name of the property
116 * Check if property @propname is present in the device firmware description.
118 bool device_property_present(struct device
*dev
, const char *propname
)
120 return fwnode_property_present(dev_fwnode(dev
), propname
);
122 EXPORT_SYMBOL_GPL(device_property_present
);
125 * fwnode_property_present - check if a property of a firmware node is present
126 * @fwnode: Firmware node whose property to check
127 * @propname: Name of the property
129 bool fwnode_property_present(struct fwnode_handle
*fwnode
, const char *propname
)
131 if (is_of_node(fwnode
))
132 return of_property_read_bool(of_node(fwnode
), propname
);
133 else if (is_acpi_node(fwnode
))
134 return !acpi_dev_prop_get(acpi_node(fwnode
), propname
, NULL
);
136 return !!pset_prop_get(to_pset(fwnode
), propname
);
138 EXPORT_SYMBOL_GPL(fwnode_property_present
);
141 * device_property_read_u8_array - return a u8 array property of a device
142 * @dev: Device to get the property of
143 * @propname: Name of the property
144 * @val: The values are stored here or %NULL to return the number of values
145 * @nval: Size of the @val array
147 * Function reads an array of u8 properties with @propname from the device
148 * firmware description and stores them to @val if found.
150 * Return: number of values if @val was %NULL,
151 * %0 if the property was found (success),
152 * %-EINVAL if given arguments are not valid,
153 * %-ENODATA if the property does not have a value,
154 * %-EPROTO if the property is not an array of numbers,
155 * %-EOVERFLOW if the size of the property is not as expected.
157 int device_property_read_u8_array(struct device
*dev
, const char *propname
,
158 u8
*val
, size_t nval
)
160 return fwnode_property_read_u8_array(dev_fwnode(dev
), propname
, val
, nval
);
162 EXPORT_SYMBOL_GPL(device_property_read_u8_array
);
165 * device_property_read_u16_array - return a u16 array property of a device
166 * @dev: Device to get the property of
167 * @propname: Name of the property
168 * @val: The values are stored here or %NULL to return the number of values
169 * @nval: Size of the @val array
171 * Function reads an array of u16 properties with @propname from the device
172 * firmware description and stores them to @val if found.
174 * Return: number of values if @val was %NULL,
175 * %0 if the property was found (success),
176 * %-EINVAL if given arguments are not valid,
177 * %-ENODATA if the property does not have a value,
178 * %-EPROTO if the property is not an array of numbers,
179 * %-EOVERFLOW if the size of the property is not as expected.
181 int device_property_read_u16_array(struct device
*dev
, const char *propname
,
182 u16
*val
, size_t nval
)
184 return fwnode_property_read_u16_array(dev_fwnode(dev
), propname
, val
, nval
);
186 EXPORT_SYMBOL_GPL(device_property_read_u16_array
);
189 * device_property_read_u32_array - return a u32 array property of a device
190 * @dev: Device to get the property of
191 * @propname: Name of the property
192 * @val: The values are stored here or %NULL to return the number of values
193 * @nval: Size of the @val array
195 * Function reads an array of u32 properties with @propname from the device
196 * firmware description and stores them to @val if found.
198 * Return: number of values if @val was %NULL,
199 * %0 if the property was found (success),
200 * %-EINVAL if given arguments are not valid,
201 * %-ENODATA if the property does not have a value,
202 * %-EPROTO if the property is not an array of numbers,
203 * %-EOVERFLOW if the size of the property is not as expected.
205 int device_property_read_u32_array(struct device
*dev
, const char *propname
,
206 u32
*val
, size_t nval
)
208 return fwnode_property_read_u32_array(dev_fwnode(dev
), propname
, val
, nval
);
210 EXPORT_SYMBOL_GPL(device_property_read_u32_array
);
213 * device_property_read_u64_array - return a u64 array property of a device
214 * @dev: Device to get the property of
215 * @propname: Name of the property
216 * @val: The values are stored here or %NULL to return the number of values
217 * @nval: Size of the @val array
219 * Function reads an array of u64 properties with @propname from the device
220 * firmware description and stores them to @val if found.
222 * Return: number of values if @val was %NULL,
223 * %0 if the property was found (success),
224 * %-EINVAL if given arguments are not valid,
225 * %-ENODATA if the property does not have a value,
226 * %-EPROTO if the property is not an array of numbers,
227 * %-EOVERFLOW if the size of the property is not as expected.
229 int device_property_read_u64_array(struct device
*dev
, const char *propname
,
230 u64
*val
, size_t nval
)
232 return fwnode_property_read_u64_array(dev_fwnode(dev
), propname
, val
, nval
);
234 EXPORT_SYMBOL_GPL(device_property_read_u64_array
);
237 * device_property_read_string_array - return a string array property of device
238 * @dev: Device to get the property of
239 * @propname: Name of the property
240 * @val: The values are stored here or %NULL to return the number of values
241 * @nval: Size of the @val array
243 * Function reads an array of string properties with @propname from the device
244 * firmware description and stores them to @val if found.
246 * Return: number of values if @val was %NULL,
247 * %0 if the property was found (success),
248 * %-EINVAL if given arguments are not valid,
249 * %-ENODATA if the property does not have a value,
250 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
251 * %-EOVERFLOW if the size of the property is not as expected.
253 int device_property_read_string_array(struct device
*dev
, const char *propname
,
254 const char **val
, size_t nval
)
256 return fwnode_property_read_string_array(dev_fwnode(dev
), propname
, val
, nval
);
258 EXPORT_SYMBOL_GPL(device_property_read_string_array
);
261 * device_property_read_string - return a string property of a device
262 * @dev: Device to get the property of
263 * @propname: Name of the property
264 * @val: The value is stored here
266 * Function reads property @propname from the device firmware description and
267 * stores the value into @val if found. The value is checked to be a string.
269 * Return: %0 if the property was found (success),
270 * %-EINVAL if given arguments are not valid,
271 * %-ENODATA if the property does not have a value,
272 * %-EPROTO or %-EILSEQ if the property type is not a string.
274 int device_property_read_string(struct device
*dev
, const char *propname
,
277 return fwnode_property_read_string(dev_fwnode(dev
), propname
, val
);
279 EXPORT_SYMBOL_GPL(device_property_read_string
);
281 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
282 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
283 : of_property_count_elems_of_size((node), (propname), sizeof(type))
285 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
288 if (is_of_node(_fwnode_)) \
289 _ret_ = OF_DEV_PROP_READ_ARRAY(of_node(_fwnode_), _propname_, \
290 _type_, _val_, _nval_); \
291 else if (is_acpi_node(_fwnode_)) \
292 _ret_ = acpi_dev_prop_read(acpi_node(_fwnode_), _propname_, \
293 _proptype_, _val_, _nval_); \
295 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
296 _proptype_, _val_, _nval_); \
301 * fwnode_property_read_u8_array - return a u8 array property of firmware node
302 * @fwnode: Firmware node to get the property of
303 * @propname: Name of the property
304 * @val: The values are stored here or %NULL to return the number of values
305 * @nval: Size of the @val array
307 * Read an array of u8 properties with @propname from @fwnode and stores them to
310 * Return: number of values if @val was %NULL,
311 * %0 if the property was found (success),
312 * %-EINVAL if given arguments are not valid,
313 * %-ENODATA if the property does not have a value,
314 * %-EPROTO if the property is not an array of numbers,
315 * %-EOVERFLOW if the size of the property is not as expected,
316 * %-ENXIO if no suitable firmware interface is present.
318 int fwnode_property_read_u8_array(struct fwnode_handle
*fwnode
,
319 const char *propname
, u8
*val
, size_t nval
)
321 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u8
, DEV_PROP_U8
,
324 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array
);
327 * fwnode_property_read_u16_array - return a u16 array property of firmware node
328 * @fwnode: Firmware node to get the property of
329 * @propname: Name of the property
330 * @val: The values are stored here or %NULL to return the number of values
331 * @nval: Size of the @val array
333 * Read an array of u16 properties with @propname from @fwnode and store them to
336 * Return: number of values if @val was %NULL,
337 * %0 if the property was found (success),
338 * %-EINVAL if given arguments are not valid,
339 * %-ENODATA if the property does not have a value,
340 * %-EPROTO if the property is not an array of numbers,
341 * %-EOVERFLOW if the size of the property is not as expected,
342 * %-ENXIO if no suitable firmware interface is present.
344 int fwnode_property_read_u16_array(struct fwnode_handle
*fwnode
,
345 const char *propname
, u16
*val
, size_t nval
)
347 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u16
, DEV_PROP_U16
,
350 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array
);
353 * fwnode_property_read_u32_array - return a u32 array property of firmware node
354 * @fwnode: Firmware node to get the property of
355 * @propname: Name of the property
356 * @val: The values are stored here or %NULL to return the number of values
357 * @nval: Size of the @val array
359 * Read an array of u32 properties with @propname from @fwnode store them to
362 * Return: number of values if @val was %NULL,
363 * %0 if the property was found (success),
364 * %-EINVAL if given arguments are not valid,
365 * %-ENODATA if the property does not have a value,
366 * %-EPROTO if the property is not an array of numbers,
367 * %-EOVERFLOW if the size of the property is not as expected,
368 * %-ENXIO if no suitable firmware interface is present.
370 int fwnode_property_read_u32_array(struct fwnode_handle
*fwnode
,
371 const char *propname
, u32
*val
, size_t nval
)
373 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u32
, DEV_PROP_U32
,
376 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array
);
379 * fwnode_property_read_u64_array - return a u64 array property firmware node
380 * @fwnode: Firmware node to get the property of
381 * @propname: Name of the property
382 * @val: The values are stored here or %NULL to return the number of values
383 * @nval: Size of the @val array
385 * Read an array of u64 properties with @propname from @fwnode and store them to
388 * Return: number of values if @val was %NULL,
389 * %0 if the property was found (success),
390 * %-EINVAL if given arguments are not valid,
391 * %-ENODATA if the property does not have a value,
392 * %-EPROTO if the property is not an array of numbers,
393 * %-EOVERFLOW if the size of the property is not as expected,
394 * %-ENXIO if no suitable firmware interface is present.
396 int fwnode_property_read_u64_array(struct fwnode_handle
*fwnode
,
397 const char *propname
, u64
*val
, size_t nval
)
399 return FWNODE_PROP_READ_ARRAY(fwnode
, propname
, u64
, DEV_PROP_U64
,
402 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array
);
405 * fwnode_property_read_string_array - return string array property of a node
406 * @fwnode: Firmware node to get the property of
407 * @propname: Name of the property
408 * @val: The values are stored here or %NULL to return the number of values
409 * @nval: Size of the @val array
411 * Read an string list property @propname from the given firmware node and store
412 * them to @val if found.
414 * Return: number of values if @val was %NULL,
415 * %0 if the property was found (success),
416 * %-EINVAL if given arguments are not valid,
417 * %-ENODATA if the property does not have a value,
418 * %-EPROTO if the property is not an array of strings,
419 * %-EOVERFLOW if the size of the property is not as expected,
420 * %-ENXIO if no suitable firmware interface is present.
422 int fwnode_property_read_string_array(struct fwnode_handle
*fwnode
,
423 const char *propname
, const char **val
,
426 if (is_of_node(fwnode
))
428 of_property_read_string_array(of_node(fwnode
), propname
,
430 of_property_count_strings(of_node(fwnode
), propname
);
431 else if (is_acpi_node(fwnode
))
432 return acpi_dev_prop_read(acpi_node(fwnode
), propname
,
433 DEV_PROP_STRING
, val
, nval
);
435 return pset_prop_read_array(to_pset(fwnode
), propname
,
436 DEV_PROP_STRING
, val
, nval
);
438 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array
);
441 * fwnode_property_read_string - return a string property of a firmware node
442 * @fwnode: Firmware node to get the property of
443 * @propname: Name of the property
444 * @val: The value is stored here
446 * Read property @propname from the given firmware node and store the value into
447 * @val if found. The value is checked to be a string.
449 * Return: %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 or %-EILSEQ if the property is not a string,
453 * %-ENXIO if no suitable firmware interface is present.
455 int fwnode_property_read_string(struct fwnode_handle
*fwnode
,
456 const char *propname
, const char **val
)
458 if (is_of_node(fwnode
))
459 return of_property_read_string(of_node(fwnode
), propname
, val
);
460 else if (is_acpi_node(fwnode
))
461 return acpi_dev_prop_read(acpi_node(fwnode
), propname
,
462 DEV_PROP_STRING
, val
, 1);
466 EXPORT_SYMBOL_GPL(fwnode_property_read_string
);
469 * device_get_next_child_node - Return the next child node handle for a device
470 * @dev: Device to find the next child node for.
471 * @child: Handle to one of the device's child nodes or a null handle.
473 struct fwnode_handle
*device_get_next_child_node(struct device
*dev
,
474 struct fwnode_handle
*child
)
476 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
477 struct device_node
*node
;
479 node
= of_get_next_available_child(dev
->of_node
, of_node(child
));
481 return &node
->fwnode
;
482 } else if (IS_ENABLED(CONFIG_ACPI
)) {
483 struct acpi_device
*node
;
485 node
= acpi_get_next_child(dev
, acpi_node(child
));
487 return acpi_fwnode_handle(node
);
491 EXPORT_SYMBOL_GPL(device_get_next_child_node
);
494 * fwnode_handle_put - Drop reference to a device node
495 * @fwnode: Pointer to the device node to drop the reference to.
497 * This has to be used when terminating device_for_each_child_node() iteration
498 * with break or return to prevent stale device node references from being left
501 void fwnode_handle_put(struct fwnode_handle
*fwnode
)
503 if (is_of_node(fwnode
))
504 of_node_put(of_node(fwnode
));
506 EXPORT_SYMBOL_GPL(fwnode_handle_put
);
509 * device_get_child_node_count - return the number of child nodes for device
510 * @dev: Device to cound the child nodes for
512 unsigned int device_get_child_node_count(struct device
*dev
)
514 struct fwnode_handle
*child
;
515 unsigned int count
= 0;
517 device_for_each_child_node(dev
, child
)
522 EXPORT_SYMBOL_GPL(device_get_child_node_count
);