blob: 841b15c5c058e9ec00d82bb203056bacc3f2d86e [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
Rafael J. Wysockib31384f2014-11-04 01:28:56 +010013#include <linux/acpi.h>
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020014#include <linux/export.h>
15#include <linux/kernel.h>
Rafael J. Wysockib31384f2014-11-04 01:28:56 +010016#include <linux/of.h>
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -050017#include <linux/of_address.h>
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020018#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{
Andy Shevchenkoecc87ee2015-08-05 16:51:11 +030030 if (!pset)
31 return;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020032
Andy Shevchenkoecc87ee2015-08-05 16:51:11 +030033 pset->fwnode.type = FWNODE_PDATA;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020034 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}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100105
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100106static 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}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100111
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{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100121 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100122}
123EXPORT_SYMBOL_GPL(device_property_present);
124
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100125/**
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))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200133 return of_property_read_bool(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100134 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200135 return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100136
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200137 return !!pset_prop_get(to_pset(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100138}
139EXPORT_SYMBOL_GPL(fwnode_property_present);
140
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100141/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200145 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100146 * @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 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200151 * Return: number of values if @val was %NULL,
152 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100153 * %-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{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100161 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100162}
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200169 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100170 * @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 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200175 * Return: number of values if @val was %NULL,
176 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100177 * %-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{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100185 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100186}
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200193 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100194 * @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 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200199 * Return: number of values if @val was %NULL,
200 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100201 * %-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{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100209 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100210}
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200217 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100218 * @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 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200223 * Return: number of values if @val was %NULL,
224 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100225 * %-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{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100233 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100234}
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200241 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100242 * @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 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200247 * Return: number of values if @val was %NULL,
248 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100249 * %-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{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100257 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100258}
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{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100278 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100279}
280EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100281
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100282#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
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100286#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
287({ \
288 int _ret_; \
289 if (is_of_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200290 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100291 _type_, _val_, _nval_); \
292 else if (is_acpi_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200293 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100294 _proptype_, _val_, _nval_); \
295 else \
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200296 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
297 _proptype_, _val_, _nval_); \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100298 _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
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 u8 properties with @propname from @fwnode and stores 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_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
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 array of u16 properties with @propname from @fwnode and store them to
335 * @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 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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200357 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100358 * @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 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200363 * Return: number of values if @val was %NULL,
364 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100365 * %-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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200383 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100384 * @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 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200389 * Return: number of values if @val was %NULL,
390 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100391 * %-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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200409 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100410 * @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 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200415 * Return: number of values if @val was %NULL,
416 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100417 * %-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))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100428 return val ?
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200429 of_property_read_string_array(to_of_node(fwnode),
430 propname, val, nval) :
431 of_property_count_strings(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100432 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200433 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100434 DEV_PROP_STRING, val, nval);
435
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200436 return pset_prop_read_array(to_pset(fwnode), propname,
437 DEV_PROP_STRING, val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100438}
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))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200460 return of_property_read_string(to_of_node(fwnode), propname, val);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100461 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200462 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100463 DEV_PROP_STRING, val, 1);
464
Andy Shevchenko4f73b062015-08-10 19:56:47 +0300465 return pset_prop_read_array(to_pset(fwnode), propname,
466 DEV_PROP_STRING, val, 1);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100467}
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
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200481 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100482 if (node)
483 return &node->fwnode;
484 } else if (IS_ENABLED(CONFIG_ACPI)) {
485 struct acpi_device *node;
486
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200487 node = acpi_get_next_child(dev, to_acpi_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100488 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))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200506 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100507}
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);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -0500525
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);