blob: f3f6d167f3f1f015fec8e36ede66b7e8e274338f [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{
30 if (pset)
31 pset->fwnode.type = FWNODE_PDATA;
32
33 set_secondary_fwnode(dev, &pset->fwnode);
34}
35EXPORT_SYMBOL_GPL(device_add_property_set);
36
37static inline bool is_pset(struct fwnode_handle *fwnode)
38{
39 return fwnode && fwnode->type == FWNODE_PDATA;
40}
41
42static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
43{
44 return is_pset(fwnode) ?
45 container_of(fwnode, struct property_set, fwnode) : NULL;
46}
47
48static struct property_entry *pset_prop_get(struct property_set *pset,
49 const char *name)
50{
51 struct property_entry *prop;
52
53 if (!pset || !pset->properties)
54 return NULL;
55
56 for (prop = pset->properties; prop->name; prop++)
57 if (!strcmp(name, prop->name))
58 return prop;
59
60 return NULL;
61}
62
63static int pset_prop_read_array(struct property_set *pset, const char *name,
64 enum dev_prop_type type, void *val, size_t nval)
65{
66 struct property_entry *prop;
67 unsigned int item_size;
68
69 prop = pset_prop_get(pset, name);
70 if (!prop)
71 return -ENODATA;
72
73 if (prop->type != type)
74 return -EPROTO;
75
76 if (!val)
77 return prop->nval;
78
79 if (prop->nval < nval)
80 return -EOVERFLOW;
81
82 switch (type) {
83 case DEV_PROP_U8:
84 item_size = sizeof(u8);
85 break;
86 case DEV_PROP_U16:
87 item_size = sizeof(u16);
88 break;
89 case DEV_PROP_U32:
90 item_size = sizeof(u32);
91 break;
92 case DEV_PROP_U64:
93 item_size = sizeof(u64);
94 break;
95 case DEV_PROP_STRING:
96 item_size = sizeof(const char *);
97 break;
98 default:
99 return -EINVAL;
100 }
101 memcpy(val, prop->value.raw_data, nval * item_size);
102 return 0;
103}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100104
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100105static inline struct fwnode_handle *dev_fwnode(struct device *dev)
106{
107 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
108 &dev->of_node->fwnode : dev->fwnode;
109}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100110
111/**
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
115 *
116 * Check if property @propname is present in the device firmware description.
117 */
118bool device_property_present(struct device *dev, const char *propname)
119{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100120 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100121}
122EXPORT_SYMBOL_GPL(device_property_present);
123
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100124/**
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
128 */
129bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
130{
131 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200132 return of_property_read_bool(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100133 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200134 return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100135
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200136 return !!pset_prop_get(to_pset(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100137}
138EXPORT_SYMBOL_GPL(fwnode_property_present);
139
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100140/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200144 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100145 * @nval: Size of the @val array
146 *
147 * Function reads an array of u8 properties with @propname from the device
148 * firmware description and stores them to @val if found.
149 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200150 * Return: number of values if @val was %NULL,
151 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100152 * %-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.
156 */
157int device_property_read_u8_array(struct device *dev, const char *propname,
158 u8 *val, size_t nval)
159{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100160 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100161}
162EXPORT_SYMBOL_GPL(device_property_read_u8_array);
163
164/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200168 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100169 * @nval: Size of the @val array
170 *
171 * Function reads an array of u16 properties with @propname from the device
172 * firmware description and stores them to @val if found.
173 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200174 * Return: number of values if @val was %NULL,
175 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100176 * %-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.
180 */
181int device_property_read_u16_array(struct device *dev, const char *propname,
182 u16 *val, size_t nval)
183{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100184 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100185}
186EXPORT_SYMBOL_GPL(device_property_read_u16_array);
187
188/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200192 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100193 * @nval: Size of the @val array
194 *
195 * Function reads an array of u32 properties with @propname from the device
196 * firmware description and stores them to @val if found.
197 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200198 * Return: number of values if @val was %NULL,
199 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100200 * %-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.
204 */
205int device_property_read_u32_array(struct device *dev, const char *propname,
206 u32 *val, size_t nval)
207{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100208 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100209}
210EXPORT_SYMBOL_GPL(device_property_read_u32_array);
211
212/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200216 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100217 * @nval: Size of the @val array
218 *
219 * Function reads an array of u64 properties with @propname from the device
220 * firmware description and stores them to @val if found.
221 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200222 * Return: number of values if @val was %NULL,
223 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100224 * %-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.
228 */
229int device_property_read_u64_array(struct device *dev, const char *propname,
230 u64 *val, size_t nval)
231{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100232 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100233}
234EXPORT_SYMBOL_GPL(device_property_read_u64_array);
235
236/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200240 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100241 * @nval: Size of the @val array
242 *
243 * Function reads an array of string properties with @propname from the device
244 * firmware description and stores them to @val if found.
245 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200246 * Return: number of values if @val was %NULL,
247 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100248 * %-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.
252 */
253int device_property_read_string_array(struct device *dev, const char *propname,
254 const char **val, size_t nval)
255{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100256 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100257}
258EXPORT_SYMBOL_GPL(device_property_read_string_array);
259
260/**
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
265 *
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.
268 *
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.
273 */
274int device_property_read_string(struct device *dev, const char *propname,
275 const char **val)
276{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100277 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100278}
279EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100280
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100281#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))
284
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100285#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
286({ \
287 int _ret_; \
288 if (is_of_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200289 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100290 _type_, _val_, _nval_); \
291 else if (is_acpi_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200292 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100293 _proptype_, _val_, _nval_); \
294 else \
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200295 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
296 _proptype_, _val_, _nval_); \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100297 _ret_; \
298})
299
300/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200304 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100305 * @nval: Size of the @val array
306 *
307 * Read an array of u8 properties with @propname from @fwnode and stores them to
308 * @val if found.
309 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200310 * Return: number of values if @val was %NULL,
311 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100312 * %-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.
317 */
318int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
319 const char *propname, u8 *val, size_t nval)
320{
321 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
322 val, nval);
323}
324EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
325
326/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200330 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100331 * @nval: Size of the @val array
332 *
333 * Read an array of u16 properties with @propname from @fwnode and store them to
334 * @val if found.
335 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200336 * Return: number of values if @val was %NULL,
337 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100338 * %-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.
343 */
344int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
345 const char *propname, u16 *val, size_t nval)
346{
347 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
348 val, nval);
349}
350EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
351
352/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200356 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100357 * @nval: Size of the @val array
358 *
359 * Read an array of u32 properties with @propname from @fwnode store them to
360 * @val if found.
361 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200362 * Return: number of values if @val was %NULL,
363 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100364 * %-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.
369 */
370int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
371 const char *propname, u32 *val, size_t nval)
372{
373 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
374 val, nval);
375}
376EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
377
378/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200382 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100383 * @nval: Size of the @val array
384 *
385 * Read an array of u64 properties with @propname from @fwnode and store them to
386 * @val if found.
387 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200388 * Return: number of values if @val was %NULL,
389 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100390 * %-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.
395 */
396int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
397 const char *propname, u64 *val, size_t nval)
398{
399 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
400 val, nval);
401}
402EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
403
404/**
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
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200408 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100409 * @nval: Size of the @val array
410 *
411 * Read an string list property @propname from the given firmware node and store
412 * them to @val if found.
413 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200414 * Return: number of values if @val was %NULL,
415 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100416 * %-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.
421 */
422int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
423 const char *propname, const char **val,
424 size_t nval)
425{
426 if (is_of_node(fwnode))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100427 return val ?
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200428 of_property_read_string_array(to_of_node(fwnode),
429 propname, val, nval) :
430 of_property_count_strings(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100431 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200432 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100433 DEV_PROP_STRING, val, nval);
434
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200435 return pset_prop_read_array(to_pset(fwnode), propname,
436 DEV_PROP_STRING, val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100437}
438EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
439
440/**
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
445 *
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.
448 *
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.
454 */
455int fwnode_property_read_string(struct fwnode_handle *fwnode,
456 const char *propname, const char **val)
457{
458 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200459 return of_property_read_string(to_of_node(fwnode), propname, val);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100460 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200461 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100462 DEV_PROP_STRING, val, 1);
463
464 return -ENXIO;
465}
466EXPORT_SYMBOL_GPL(fwnode_property_read_string);
467
468/**
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.
472 */
473struct fwnode_handle *device_get_next_child_node(struct device *dev,
474 struct fwnode_handle *child)
475{
476 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
477 struct device_node *node;
478
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200479 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100480 if (node)
481 return &node->fwnode;
482 } else if (IS_ENABLED(CONFIG_ACPI)) {
483 struct acpi_device *node;
484
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200485 node = acpi_get_next_child(dev, to_acpi_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100486 if (node)
487 return acpi_fwnode_handle(node);
488 }
489 return NULL;
490}
491EXPORT_SYMBOL_GPL(device_get_next_child_node);
492
493/**
494 * fwnode_handle_put - Drop reference to a device node
495 * @fwnode: Pointer to the device node to drop the reference to.
496 *
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
499 * behind.
500 */
501void fwnode_handle_put(struct fwnode_handle *fwnode)
502{
503 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200504 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100505}
506EXPORT_SYMBOL_GPL(fwnode_handle_put);
507
508/**
509 * device_get_child_node_count - return the number of child nodes for device
510 * @dev: Device to cound the child nodes for
511 */
512unsigned int device_get_child_node_count(struct device *dev)
513{
514 struct fwnode_handle *child;
515 unsigned int count = 0;
516
517 device_for_each_child_node(dev, child)
518 count++;
519
520 return count;
521}
522EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -0500523
524bool device_dma_is_coherent(struct device *dev)
525{
526 bool coherent = false;
527
528 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
529 coherent = of_dma_is_coherent(dev->of_node);
530 else
531 acpi_check_dma(ACPI_COMPANION(dev), &coherent);
532
533 return coherent;
534}
535EXPORT_SYMBOL_GPL(device_dma_is_coherent);