blob: cf3b2160e34ecc964b327dd492587966a2a48ce4 [file] [log] [blame]
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001/*
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/property.h>
14#include <linux/export.h>
15#include <linux/acpi.h>
16#include <linux/of.h>
17
18/**
19 * device_property_present - check if a property of a device is present
20 * @dev: Device whose property is being checked
21 * @propname: Name of the property
22 *
23 * Check if property @propname is present in the device firmware description.
24 */
25bool device_property_present(struct device *dev, const char *propname)
26{
27 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
28 return of_property_read_bool(dev->of_node, propname);
29
30 return !acpi_dev_prop_get(ACPI_COMPANION(dev), propname, NULL);
31}
32EXPORT_SYMBOL_GPL(device_property_present);
33
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +010034/**
35 * fwnode_property_present - check if a property of a firmware node is present
36 * @fwnode: Firmware node whose property to check
37 * @propname: Name of the property
38 */
39bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
40{
41 if (is_of_node(fwnode))
42 return of_property_read_bool(of_node(fwnode), propname);
43 else if (is_acpi_node(fwnode))
44 return !acpi_dev_prop_get(acpi_node(fwnode), propname, NULL);
45
46 return false;
47}
48EXPORT_SYMBOL_GPL(fwnode_property_present);
49
Rafael J. Wysockib31384f2014-11-04 01:28:56 +010050#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
51 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
52 : of_property_count_elems_of_size((node), (propname), sizeof(type))
53
54#define DEV_PROP_READ_ARRAY(_dev_, _propname_, _type_, _proptype_, _val_, _nval_) \
55 IS_ENABLED(CONFIG_OF) && _dev_->of_node ? \
56 (OF_DEV_PROP_READ_ARRAY(_dev_->of_node, _propname_, _type_, \
57 _val_, _nval_)) : \
58 acpi_dev_prop_read(ACPI_COMPANION(_dev_), _propname_, \
59 _proptype_, _val_, _nval_)
60
61/**
62 * device_property_read_u8_array - return a u8 array property of a device
63 * @dev: Device to get the property of
64 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +020065 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +010066 * @nval: Size of the @val array
67 *
68 * Function reads an array of u8 properties with @propname from the device
69 * firmware description and stores them to @val if found.
70 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +020071 * Return: number of values if @val was %NULL,
72 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +010073 * %-EINVAL if given arguments are not valid,
74 * %-ENODATA if the property does not have a value,
75 * %-EPROTO if the property is not an array of numbers,
76 * %-EOVERFLOW if the size of the property is not as expected.
77 */
78int device_property_read_u8_array(struct device *dev, const char *propname,
79 u8 *val, size_t nval)
80{
81 return DEV_PROP_READ_ARRAY(dev, propname, u8, DEV_PROP_U8, val, nval);
82}
83EXPORT_SYMBOL_GPL(device_property_read_u8_array);
84
85/**
86 * device_property_read_u16_array - return a u16 array property of a device
87 * @dev: Device to get the property of
88 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +020089 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +010090 * @nval: Size of the @val array
91 *
92 * Function reads an array of u16 properties with @propname from the device
93 * firmware description and stores them to @val if found.
94 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +020095 * Return: number of values if @val was %NULL,
96 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +010097 * %-EINVAL if given arguments are not valid,
98 * %-ENODATA if the property does not have a value,
99 * %-EPROTO if the property is not an array of numbers,
100 * %-EOVERFLOW if the size of the property is not as expected.
101 */
102int device_property_read_u16_array(struct device *dev, const char *propname,
103 u16 *val, size_t nval)
104{
105 return DEV_PROP_READ_ARRAY(dev, propname, u16, DEV_PROP_U16, val, nval);
106}
107EXPORT_SYMBOL_GPL(device_property_read_u16_array);
108
109/**
110 * device_property_read_u32_array - return a u32 array property of a device
111 * @dev: Device to get the property of
112 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200113 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100114 * @nval: Size of the @val array
115 *
116 * Function reads an array of u32 properties with @propname from the device
117 * firmware description and stores them to @val if found.
118 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200119 * Return: number of values if @val was %NULL,
120 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100121 * %-EINVAL if given arguments are not valid,
122 * %-ENODATA if the property does not have a value,
123 * %-EPROTO if the property is not an array of numbers,
124 * %-EOVERFLOW if the size of the property is not as expected.
125 */
126int device_property_read_u32_array(struct device *dev, const char *propname,
127 u32 *val, size_t nval)
128{
129 return DEV_PROP_READ_ARRAY(dev, propname, u32, DEV_PROP_U32, val, nval);
130}
131EXPORT_SYMBOL_GPL(device_property_read_u32_array);
132
133/**
134 * device_property_read_u64_array - return a u64 array property of a device
135 * @dev: Device to get the property of
136 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200137 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100138 * @nval: Size of the @val array
139 *
140 * Function reads an array of u64 properties with @propname from the device
141 * firmware description and stores them to @val if found.
142 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200143 * Return: number of values if @val was %NULL,
144 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100145 * %-EINVAL if given arguments are not valid,
146 * %-ENODATA if the property does not have a value,
147 * %-EPROTO if the property is not an array of numbers,
148 * %-EOVERFLOW if the size of the property is not as expected.
149 */
150int device_property_read_u64_array(struct device *dev, const char *propname,
151 u64 *val, size_t nval)
152{
153 return DEV_PROP_READ_ARRAY(dev, propname, u64, DEV_PROP_U64, val, nval);
154}
155EXPORT_SYMBOL_GPL(device_property_read_u64_array);
156
157/**
158 * device_property_read_string_array - return a string array property of device
159 * @dev: Device to get the property of
160 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200161 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100162 * @nval: Size of the @val array
163 *
164 * Function reads an array of string properties with @propname from the device
165 * firmware description and stores them to @val if found.
166 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200167 * Return: number of values if @val was %NULL,
168 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100169 * %-EINVAL if given arguments are not valid,
170 * %-ENODATA if the property does not have a value,
171 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
172 * %-EOVERFLOW if the size of the property is not as expected.
173 */
174int device_property_read_string_array(struct device *dev, const char *propname,
175 const char **val, size_t nval)
176{
177 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200178 (val ? of_property_read_string_array(dev->of_node, propname, val, nval)
179 : of_property_count_strings(dev->of_node, propname)) :
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100180 acpi_dev_prop_read(ACPI_COMPANION(dev), propname,
181 DEV_PROP_STRING, val, nval);
182}
183EXPORT_SYMBOL_GPL(device_property_read_string_array);
184
185/**
186 * device_property_read_string - return a string property of a device
187 * @dev: Device to get the property of
188 * @propname: Name of the property
189 * @val: The value is stored here
190 *
191 * Function reads property @propname from the device firmware description and
192 * stores the value into @val if found. The value is checked to be a string.
193 *
194 * Return: %0 if the property was found (success),
195 * %-EINVAL if given arguments are not valid,
196 * %-ENODATA if the property does not have a value,
197 * %-EPROTO or %-EILSEQ if the property type is not a string.
198 */
199int device_property_read_string(struct device *dev, const char *propname,
200 const char **val)
201{
202 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
203 of_property_read_string(dev->of_node, propname, val) :
204 acpi_dev_prop_read(ACPI_COMPANION(dev), propname,
205 DEV_PROP_STRING, val, 1);
206}
207EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100208
209#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
210({ \
211 int _ret_; \
212 if (is_of_node(_fwnode_)) \
213 _ret_ = OF_DEV_PROP_READ_ARRAY(of_node(_fwnode_), _propname_, \
214 _type_, _val_, _nval_); \
215 else if (is_acpi_node(_fwnode_)) \
216 _ret_ = acpi_dev_prop_read(acpi_node(_fwnode_), _propname_, \
217 _proptype_, _val_, _nval_); \
218 else \
219 _ret_ = -ENXIO; \
220 _ret_; \
221})
222
223/**
224 * fwnode_property_read_u8_array - return a u8 array property of firmware node
225 * @fwnode: Firmware node to get the property of
226 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200227 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100228 * @nval: Size of the @val array
229 *
230 * Read an array of u8 properties with @propname from @fwnode and stores them to
231 * @val if found.
232 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200233 * Return: number of values if @val was %NULL,
234 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100235 * %-EINVAL if given arguments are not valid,
236 * %-ENODATA if the property does not have a value,
237 * %-EPROTO if the property is not an array of numbers,
238 * %-EOVERFLOW if the size of the property is not as expected,
239 * %-ENXIO if no suitable firmware interface is present.
240 */
241int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
242 const char *propname, u8 *val, size_t nval)
243{
244 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
245 val, nval);
246}
247EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
248
249/**
250 * fwnode_property_read_u16_array - return a u16 array property of firmware node
251 * @fwnode: Firmware node to get the property of
252 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200253 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100254 * @nval: Size of the @val array
255 *
256 * Read an array of u16 properties with @propname from @fwnode and store them to
257 * @val if found.
258 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200259 * Return: number of values if @val was %NULL,
260 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100261 * %-EINVAL if given arguments are not valid,
262 * %-ENODATA if the property does not have a value,
263 * %-EPROTO if the property is not an array of numbers,
264 * %-EOVERFLOW if the size of the property is not as expected,
265 * %-ENXIO if no suitable firmware interface is present.
266 */
267int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
268 const char *propname, u16 *val, size_t nval)
269{
270 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
271 val, nval);
272}
273EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
274
275/**
276 * fwnode_property_read_u32_array - return a u32 array property of firmware node
277 * @fwnode: Firmware node to get the property of
278 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200279 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100280 * @nval: Size of the @val array
281 *
282 * Read an array of u32 properties with @propname from @fwnode store them to
283 * @val if found.
284 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200285 * Return: number of values if @val was %NULL,
286 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100287 * %-EINVAL if given arguments are not valid,
288 * %-ENODATA if the property does not have a value,
289 * %-EPROTO if the property is not an array of numbers,
290 * %-EOVERFLOW if the size of the property is not as expected,
291 * %-ENXIO if no suitable firmware interface is present.
292 */
293int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
294 const char *propname, u32 *val, size_t nval)
295{
296 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
297 val, nval);
298}
299EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
300
301/**
302 * fwnode_property_read_u64_array - return a u64 array property firmware node
303 * @fwnode: Firmware node to get the property of
304 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200305 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100306 * @nval: Size of the @val array
307 *
308 * Read an array of u64 properties with @propname from @fwnode and store them to
309 * @val if found.
310 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200311 * Return: number of values if @val was %NULL,
312 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100313 * %-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_u64_array(struct fwnode_handle *fwnode,
320 const char *propname, u64 *val, size_t nval)
321{
322 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
323 val, nval);
324}
325EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
326
327/**
328 * fwnode_property_read_string_array - return string array property of a node
329 * @fwnode: Firmware node to get the property of
330 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200331 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100332 * @nval: Size of the @val array
333 *
334 * Read an string list property @propname from the given firmware node and store
335 * them to @val if found.
336 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200337 * Return: number of values if @val was %NULL,
338 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100339 * %-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 strings,
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_string_array(struct fwnode_handle *fwnode,
346 const char *propname, const char **val,
347 size_t nval)
348{
349 if (is_of_node(fwnode))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100350 return val ?
351 of_property_read_string_array(of_node(fwnode), propname,
352 val, nval) :
353 of_property_count_strings(of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100354 else if (is_acpi_node(fwnode))
355 return acpi_dev_prop_read(acpi_node(fwnode), propname,
356 DEV_PROP_STRING, val, nval);
357
358 return -ENXIO;
359}
360EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
361
362/**
363 * fwnode_property_read_string - return a string property of a firmware node
364 * @fwnode: Firmware node to get the property of
365 * @propname: Name of the property
366 * @val: The value is stored here
367 *
368 * Read property @propname from the given firmware node and store the value into
369 * @val if found. The value is checked to be a string.
370 *
371 * Return: %0 if the property was found (success),
372 * %-EINVAL if given arguments are not valid,
373 * %-ENODATA if the property does not have a value,
374 * %-EPROTO or %-EILSEQ if the property is not a string,
375 * %-ENXIO if no suitable firmware interface is present.
376 */
377int fwnode_property_read_string(struct fwnode_handle *fwnode,
378 const char *propname, const char **val)
379{
380 if (is_of_node(fwnode))
381 return of_property_read_string(of_node(fwnode),propname, val);
382 else if (is_acpi_node(fwnode))
383 return acpi_dev_prop_read(acpi_node(fwnode), propname,
384 DEV_PROP_STRING, val, 1);
385
386 return -ENXIO;
387}
388EXPORT_SYMBOL_GPL(fwnode_property_read_string);
389
390/**
391 * device_get_next_child_node - Return the next child node handle for a device
392 * @dev: Device to find the next child node for.
393 * @child: Handle to one of the device's child nodes or a null handle.
394 */
395struct fwnode_handle *device_get_next_child_node(struct device *dev,
396 struct fwnode_handle *child)
397{
398 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
399 struct device_node *node;
400
401 node = of_get_next_available_child(dev->of_node, of_node(child));
402 if (node)
403 return &node->fwnode;
404 } else if (IS_ENABLED(CONFIG_ACPI)) {
405 struct acpi_device *node;
406
407 node = acpi_get_next_child(dev, acpi_node(child));
408 if (node)
409 return acpi_fwnode_handle(node);
410 }
411 return NULL;
412}
413EXPORT_SYMBOL_GPL(device_get_next_child_node);
414
415/**
416 * fwnode_handle_put - Drop reference to a device node
417 * @fwnode: Pointer to the device node to drop the reference to.
418 *
419 * This has to be used when terminating device_for_each_child_node() iteration
420 * with break or return to prevent stale device node references from being left
421 * behind.
422 */
423void fwnode_handle_put(struct fwnode_handle *fwnode)
424{
425 if (is_of_node(fwnode))
426 of_node_put(of_node(fwnode));
427}
428EXPORT_SYMBOL_GPL(fwnode_handle_put);
429
430/**
431 * device_get_child_node_count - return the number of child nodes for device
432 * @dev: Device to cound the child nodes for
433 */
434unsigned int device_get_child_node_count(struct device *dev)
435{
436 struct fwnode_handle *child;
437 unsigned int count = 0;
438
439 device_for_each_child_node(dev, child)
440 count++;
441
442 return count;
443}
444EXPORT_SYMBOL_GPL(device_get_child_node_count);