Merge branches 'pm-cpu', 'pm-cpuidle' and 'pm-domains'
[linux-2.6-block.git] / drivers / base / property.c
CommitLineData
b31384fa
RW
1/*
2 * property.c - Unified device property interface.
3 *
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
7 *
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.
11 */
12
b31384fa 13#include <linux/acpi.h>
16ba08d5
RW
14#include <linux/export.h>
15#include <linux/kernel.h>
b31384fa 16#include <linux/of.h>
05ca5560 17#include <linux/of_address.h>
16ba08d5
RW
18#include <linux/property.h>
19
20/**
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.
24 *
25 * Associate a collection of device properties represented by @pset with @dev
26 * as its secondary firmware node.
27 */
28void device_add_property_set(struct device *dev, struct property_set *pset)
29{
ecc87eed
AS
30 if (!pset)
31 return;
16ba08d5 32
ecc87eed 33 pset->fwnode.type = FWNODE_PDATA;
16ba08d5
RW
34 set_secondary_fwnode(dev, &pset->fwnode);
35}
36EXPORT_SYMBOL_GPL(device_add_property_set);
37
38static inline bool is_pset(struct fwnode_handle *fwnode)
39{
40 return fwnode && fwnode->type == FWNODE_PDATA;
41}
42
43static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
44{
45 return is_pset(fwnode) ?
46 container_of(fwnode, struct property_set, fwnode) : NULL;
47}
48
49static struct property_entry *pset_prop_get(struct property_set *pset,
50 const char *name)
51{
52 struct property_entry *prop;
53
54 if (!pset || !pset->properties)
55 return NULL;
56
57 for (prop = pset->properties; prop->name; prop++)
58 if (!strcmp(name, prop->name))
59 return prop;
60
61 return NULL;
62}
63
64static int pset_prop_read_array(struct property_set *pset, const char *name,
65 enum dev_prop_type type, void *val, size_t nval)
66{
67 struct property_entry *prop;
68 unsigned int item_size;
69
70 prop = pset_prop_get(pset, name);
71 if (!prop)
72 return -ENODATA;
73
74 if (prop->type != type)
75 return -EPROTO;
76
77 if (!val)
78 return prop->nval;
79
80 if (prop->nval < nval)
81 return -EOVERFLOW;
82
83 switch (type) {
84 case DEV_PROP_U8:
85 item_size = sizeof(u8);
86 break;
87 case DEV_PROP_U16:
88 item_size = sizeof(u16);
89 break;
90 case DEV_PROP_U32:
91 item_size = sizeof(u32);
92 break;
93 case DEV_PROP_U64:
94 item_size = sizeof(u64);
95 break;
96 case DEV_PROP_STRING:
97 item_size = sizeof(const char *);
98 break;
99 default:
100 return -EINVAL;
101 }
102 memcpy(val, prop->value.raw_data, nval * item_size);
103 return 0;
104}
b31384fa 105
9017f252
RW
106static inline struct fwnode_handle *dev_fwnode(struct device *dev)
107{
108 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
109 &dev->of_node->fwnode : dev->fwnode;
110}
b31384fa
RW
111
112/**
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
116 *
117 * Check if property @propname is present in the device firmware description.
118 */
119bool device_property_present(struct device *dev, const char *propname)
120{
9017f252 121 return fwnode_property_present(dev_fwnode(dev), propname);
b31384fa
RW
122}
123EXPORT_SYMBOL_GPL(device_property_present);
124
8a0662d9
RW
125/**
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
129 */
130bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
131{
132 if (is_of_node(fwnode))
c181fb3e 133 return of_property_read_bool(to_of_node(fwnode), propname);
8a0662d9 134 else if (is_acpi_node(fwnode))
c181fb3e 135 return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL);
8a0662d9 136
16ba08d5 137 return !!pset_prop_get(to_pset(fwnode), propname);
8a0662d9
RW
138}
139EXPORT_SYMBOL_GPL(fwnode_property_present);
140
b31384fa
RW
141/**
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
5c0acf3b 145 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
146 * @nval: Size of the @val array
147 *
148 * Function reads an array of u8 properties with @propname from the device
149 * firmware description and stores them to @val if found.
150 *
5c0acf3b
AH
151 * Return: number of values if @val was %NULL,
152 * %0 if the property was found (success),
b31384fa
RW
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.
157 */
158int device_property_read_u8_array(struct device *dev, const char *propname,
159 u8 *val, size_t nval)
160{
9017f252 161 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
162}
163EXPORT_SYMBOL_GPL(device_property_read_u8_array);
164
165/**
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
5c0acf3b 169 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
170 * @nval: Size of the @val array
171 *
172 * Function reads an array of u16 properties with @propname from the device
173 * firmware description and stores them to @val if found.
174 *
5c0acf3b
AH
175 * Return: number of values if @val was %NULL,
176 * %0 if the property was found (success),
b31384fa
RW
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.
181 */
182int device_property_read_u16_array(struct device *dev, const char *propname,
183 u16 *val, size_t nval)
184{
9017f252 185 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
186}
187EXPORT_SYMBOL_GPL(device_property_read_u16_array);
188
189/**
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
5c0acf3b 193 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
194 * @nval: Size of the @val array
195 *
196 * Function reads an array of u32 properties with @propname from the device
197 * firmware description and stores them to @val if found.
198 *
5c0acf3b
AH
199 * Return: number of values if @val was %NULL,
200 * %0 if the property was found (success),
b31384fa
RW
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.
205 */
206int device_property_read_u32_array(struct device *dev, const char *propname,
207 u32 *val, size_t nval)
208{
9017f252 209 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
210}
211EXPORT_SYMBOL_GPL(device_property_read_u32_array);
212
213/**
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
5c0acf3b 217 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
218 * @nval: Size of the @val array
219 *
220 * Function reads an array of u64 properties with @propname from the device
221 * firmware description and stores them to @val if found.
222 *
5c0acf3b
AH
223 * Return: number of values if @val was %NULL,
224 * %0 if the property was found (success),
b31384fa
RW
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.
229 */
230int device_property_read_u64_array(struct device *dev, const char *propname,
231 u64 *val, size_t nval)
232{
9017f252 233 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
234}
235EXPORT_SYMBOL_GPL(device_property_read_u64_array);
236
237/**
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
5c0acf3b 241 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
242 * @nval: Size of the @val array
243 *
244 * Function reads an array of string properties with @propname from the device
245 * firmware description and stores them to @val if found.
246 *
5c0acf3b
AH
247 * Return: number of values if @val was %NULL,
248 * %0 if the property was found (success),
b31384fa
RW
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.
253 */
254int device_property_read_string_array(struct device *dev, const char *propname,
255 const char **val, size_t nval)
256{
9017f252 257 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
258}
259EXPORT_SYMBOL_GPL(device_property_read_string_array);
260
261/**
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
266 *
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.
269 *
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.
274 */
275int device_property_read_string(struct device *dev, const char *propname,
276 const char **val)
277{
9017f252 278 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
b31384fa
RW
279}
280EXPORT_SYMBOL_GPL(device_property_read_string);
8a0662d9 281
9017f252
RW
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))
285
8a0662d9
RW
286#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
287({ \
288 int _ret_; \
289 if (is_of_node(_fwnode_)) \
c181fb3e 290 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
8a0662d9
RW
291 _type_, _val_, _nval_); \
292 else if (is_acpi_node(_fwnode_)) \
c181fb3e 293 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
8a0662d9
RW
294 _proptype_, _val_, _nval_); \
295 else \
16ba08d5
RW
296 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
297 _proptype_, _val_, _nval_); \
8a0662d9
RW
298 _ret_; \
299})
300
301/**
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
5c0acf3b 305 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
306 * @nval: Size of the @val array
307 *
308 * Read an array of u8 properties with @propname from @fwnode and stores them to
309 * @val if found.
310 *
5c0acf3b
AH
311 * Return: number of values if @val was %NULL,
312 * %0 if the property was found (success),
8a0662d9
RW
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.
318 */
319int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
320 const char *propname, u8 *val, size_t nval)
321{
322 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
323 val, nval);
324}
325EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
326
327/**
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
5c0acf3b 331 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
332 * @nval: Size of the @val array
333 *
334 * Read an array of u16 properties with @propname from @fwnode and store them to
335 * @val if found.
336 *
5c0acf3b
AH
337 * Return: number of values if @val was %NULL,
338 * %0 if the property was found (success),
8a0662d9
RW
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.
344 */
345int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
346 const char *propname, u16 *val, size_t nval)
347{
348 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
349 val, nval);
350}
351EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
352
353/**
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
5c0acf3b 357 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
358 * @nval: Size of the @val array
359 *
360 * Read an array of u32 properties with @propname from @fwnode store them to
361 * @val if found.
362 *
5c0acf3b
AH
363 * Return: number of values if @val was %NULL,
364 * %0 if the property was found (success),
8a0662d9
RW
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.
370 */
371int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
372 const char *propname, u32 *val, size_t nval)
373{
374 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
375 val, nval);
376}
377EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
378
379/**
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
5c0acf3b 383 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
384 * @nval: Size of the @val array
385 *
386 * Read an array of u64 properties with @propname from @fwnode and store them to
387 * @val if found.
388 *
5c0acf3b
AH
389 * Return: number of values if @val was %NULL,
390 * %0 if the property was found (success),
8a0662d9
RW
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.
396 */
397int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
398 const char *propname, u64 *val, size_t nval)
399{
400 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
401 val, nval);
402}
403EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
404
405/**
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
5c0acf3b 409 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
410 * @nval: Size of the @val array
411 *
412 * Read an string list property @propname from the given firmware node and store
413 * them to @val if found.
414 *
5c0acf3b
AH
415 * Return: number of values if @val was %NULL,
416 * %0 if the property was found (success),
8a0662d9
RW
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.
422 */
423int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
424 const char *propname, const char **val,
425 size_t nval)
426{
427 if (is_of_node(fwnode))
f42712a9 428 return val ?
c181fb3e
AS
429 of_property_read_string_array(to_of_node(fwnode),
430 propname, val, nval) :
431 of_property_count_strings(to_of_node(fwnode), propname);
8a0662d9 432 else if (is_acpi_node(fwnode))
c181fb3e 433 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
8a0662d9
RW
434 DEV_PROP_STRING, val, nval);
435
16ba08d5
RW
436 return pset_prop_read_array(to_pset(fwnode), propname,
437 DEV_PROP_STRING, val, nval);
8a0662d9
RW
438}
439EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
440
441/**
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
446 *
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.
449 *
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.
455 */
456int fwnode_property_read_string(struct fwnode_handle *fwnode,
457 const char *propname, const char **val)
458{
459 if (is_of_node(fwnode))
c181fb3e 460 return of_property_read_string(to_of_node(fwnode), propname, val);
8a0662d9 461 else if (is_acpi_node(fwnode))
c181fb3e 462 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
8a0662d9
RW
463 DEV_PROP_STRING, val, 1);
464
4f73b065
AS
465 return pset_prop_read_array(to_pset(fwnode), propname,
466 DEV_PROP_STRING, val, 1);
8a0662d9
RW
467}
468EXPORT_SYMBOL_GPL(fwnode_property_read_string);
469
470/**
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.
474 */
475struct fwnode_handle *device_get_next_child_node(struct device *dev,
476 struct fwnode_handle *child)
477{
478 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
479 struct device_node *node;
480
c181fb3e 481 node = of_get_next_available_child(dev->of_node, to_of_node(child));
8a0662d9
RW
482 if (node)
483 return &node->fwnode;
484 } else if (IS_ENABLED(CONFIG_ACPI)) {
485 struct acpi_device *node;
486
c181fb3e 487 node = acpi_get_next_child(dev, to_acpi_node(child));
8a0662d9
RW
488 if (node)
489 return acpi_fwnode_handle(node);
490 }
491 return NULL;
492}
493EXPORT_SYMBOL_GPL(device_get_next_child_node);
494
495/**
496 * fwnode_handle_put - Drop reference to a device node
497 * @fwnode: Pointer to the device node to drop the reference to.
498 *
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
501 * behind.
502 */
503void fwnode_handle_put(struct fwnode_handle *fwnode)
504{
505 if (is_of_node(fwnode))
c181fb3e 506 of_node_put(to_of_node(fwnode));
8a0662d9
RW
507}
508EXPORT_SYMBOL_GPL(fwnode_handle_put);
509
510/**
511 * device_get_child_node_count - return the number of child nodes for device
512 * @dev: Device to cound the child nodes for
513 */
514unsigned int device_get_child_node_count(struct device *dev)
515{
516 struct fwnode_handle *child;
517 unsigned int count = 0;
518
519 device_for_each_child_node(dev, child)
520 count++;
521
522 return count;
523}
524EXPORT_SYMBOL_GPL(device_get_child_node_count);
05ca5560
SS
525
526bool device_dma_is_coherent(struct device *dev)
527{
528 bool coherent = false;
529
530 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
531 coherent = of_dma_is_coherent(dev->of_node);
532 else
533 acpi_check_dma(ACPI_COMPANION(dev), &coherent);
534
535 return coherent;
536}
537EXPORT_SYMBOL_GPL(device_dma_is_coherent);