]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/base/property.c
Merge branch 'pm-cpufreq'
[mirror_ubuntu-zesty-kernel.git] / drivers / base / property.c
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
13 #include <linux/acpi.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
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 */
28 void device_add_property_set(struct device *dev, struct property_set *pset)
29 {
30 if (!pset)
31 return;
32
33 pset->fwnode.type = FWNODE_PDATA;
34 set_secondary_fwnode(dev, &pset->fwnode);
35 }
36 EXPORT_SYMBOL_GPL(device_add_property_set);
37
38 static inline bool is_pset(struct fwnode_handle *fwnode)
39 {
40 return fwnode && fwnode->type == FWNODE_PDATA;
41 }
42
43 static 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
49 static 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
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)
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 }
105
106 static 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 }
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 */
119 bool device_property_present(struct device *dev, const char *propname)
120 {
121 return fwnode_property_present(dev_fwnode(dev), propname);
122 }
123 EXPORT_SYMBOL_GPL(device_property_present);
124
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 */
130 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
131 {
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);
136
137 return !!pset_prop_get(to_pset(fwnode), propname);
138 }
139 EXPORT_SYMBOL_GPL(fwnode_property_present);
140
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
145 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
157 */
158 int device_property_read_u8_array(struct device *dev, const char *propname,
159 u8 *val, size_t nval)
160 {
161 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
162 }
163 EXPORT_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
169 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
181 */
182 int device_property_read_u16_array(struct device *dev, const char *propname,
183 u16 *val, size_t nval)
184 {
185 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
186 }
187 EXPORT_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
193 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
205 */
206 int device_property_read_u32_array(struct device *dev, const char *propname,
207 u32 *val, size_t nval)
208 {
209 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
210 }
211 EXPORT_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
217 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
229 */
230 int device_property_read_u64_array(struct device *dev, const char *propname,
231 u64 *val, size_t nval)
232 {
233 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
234 }
235 EXPORT_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
241 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
253 */
254 int device_property_read_string_array(struct device *dev, const char *propname,
255 const char **val, size_t nval)
256 {
257 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
258 }
259 EXPORT_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 */
275 int device_property_read_string(struct device *dev, const char *propname,
276 const char **val)
277 {
278 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
279 }
280 EXPORT_SYMBOL_GPL(device_property_read_string);
281
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
286 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
287 ({ \
288 int _ret_; \
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_); \
295 else \
296 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
297 _proptype_, _val_, _nval_); \
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
305 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
318 */
319 int 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 }
325 EXPORT_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
331 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
344 */
345 int 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 }
351 EXPORT_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
357 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
370 */
371 int 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 }
377 EXPORT_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
383 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
396 */
397 int 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 }
403 EXPORT_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
409 * @val: The values are stored here or %NULL to return the number of values
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 *
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.
422 */
423 int 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))
428 return val ?
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);
435
436 return pset_prop_read_array(to_pset(fwnode), propname,
437 DEV_PROP_STRING, val, nval);
438 }
439 EXPORT_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 */
456 int fwnode_property_read_string(struct fwnode_handle *fwnode,
457 const char *propname, const char **val)
458 {
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);
464
465 return pset_prop_read_array(to_pset(fwnode), propname,
466 DEV_PROP_STRING, val, 1);
467 }
468 EXPORT_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 */
475 struct 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
481 node = of_get_next_available_child(dev->of_node, to_of_node(child));
482 if (node)
483 return &node->fwnode;
484 } else if (IS_ENABLED(CONFIG_ACPI)) {
485 struct acpi_device *node;
486
487 node = acpi_get_next_child(dev, to_acpi_node(child));
488 if (node)
489 return acpi_fwnode_handle(node);
490 }
491 return NULL;
492 }
493 EXPORT_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 */
503 void fwnode_handle_put(struct fwnode_handle *fwnode)
504 {
505 if (is_of_node(fwnode))
506 of_node_put(to_of_node(fwnode));
507 }
508 EXPORT_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 */
514 unsigned 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 }
524 EXPORT_SYMBOL_GPL(device_get_child_node_count);
525
526 bool 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 }
537 EXPORT_SYMBOL_GPL(device_dma_is_coherent);