blob: dfd4de69b67bdf77cb9f17be9459d298f7eec604 [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>
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020017#include <linux/property.h>
18
19/**
20 * device_add_property_set - Add a collection of properties to a device object.
21 * @dev: Device to add properties to.
22 * @pset: Collection of properties to add.
23 *
24 * Associate a collection of device properties represented by @pset with @dev
25 * as its secondary firmware node.
26 */
27void device_add_property_set(struct device *dev, struct property_set *pset)
28{
29 if (pset)
30 pset->fwnode.type = FWNODE_PDATA;
31
32 set_secondary_fwnode(dev, &pset->fwnode);
33}
34EXPORT_SYMBOL_GPL(device_add_property_set);
35
36static inline bool is_pset(struct fwnode_handle *fwnode)
37{
38 return fwnode && fwnode->type == FWNODE_PDATA;
39}
40
41static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
42{
43 return is_pset(fwnode) ?
44 container_of(fwnode, struct property_set, fwnode) : NULL;
45}
46
47static struct property_entry *pset_prop_get(struct property_set *pset,
48 const char *name)
49{
50 struct property_entry *prop;
51
52 if (!pset || !pset->properties)
53 return NULL;
54
55 for (prop = pset->properties; prop->name; prop++)
56 if (!strcmp(name, prop->name))
57 return prop;
58
59 return NULL;
60}
61
62static int pset_prop_read_array(struct property_set *pset, const char *name,
63 enum dev_prop_type type, void *val, size_t nval)
64{
65 struct property_entry *prop;
66 unsigned int item_size;
67
68 prop = pset_prop_get(pset, name);
69 if (!prop)
70 return -ENODATA;
71
72 if (prop->type != type)
73 return -EPROTO;
74
75 if (!val)
76 return prop->nval;
77
78 if (prop->nval < nval)
79 return -EOVERFLOW;
80
81 switch (type) {
82 case DEV_PROP_U8:
83 item_size = sizeof(u8);
84 break;
85 case DEV_PROP_U16:
86 item_size = sizeof(u16);
87 break;
88 case DEV_PROP_U32:
89 item_size = sizeof(u32);
90 break;
91 case DEV_PROP_U64:
92 item_size = sizeof(u64);
93 break;
94 case DEV_PROP_STRING:
95 item_size = sizeof(const char *);
96 break;
97 default:
98 return -EINVAL;
99 }
100 memcpy(val, prop->value.raw_data, nval * item_size);
101 return 0;
102}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100103
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100104static inline struct fwnode_handle *dev_fwnode(struct device *dev)
105{
106 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
107 &dev->of_node->fwnode : dev->fwnode;
108}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100109
110/**
111 * device_property_present - check if a property of a device is present
112 * @dev: Device whose property is being checked
113 * @propname: Name of the property
114 *
115 * Check if property @propname is present in the device firmware description.
116 */
117bool device_property_present(struct device *dev, const char *propname)
118{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100119 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100120}
121EXPORT_SYMBOL_GPL(device_property_present);
122
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100123/**
124 * fwnode_property_present - check if a property of a firmware node is present
125 * @fwnode: Firmware node whose property to check
126 * @propname: Name of the property
127 */
128bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
129{
130 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200131 return of_property_read_bool(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100132 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200133 return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100134
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200135 return !!pset_prop_get(to_pset(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100136}
137EXPORT_SYMBOL_GPL(fwnode_property_present);
138
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100139/**
140 * device_property_read_u8_array - return a u8 array property of a device
141 * @dev: Device to get the property of
142 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200143 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100144 * @nval: Size of the @val array
145 *
146 * Function reads an array of u8 properties with @propname from the device
147 * firmware description and stores them to @val if found.
148 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200149 * Return: number of values if @val was %NULL,
150 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100151 * %-EINVAL if given arguments are not valid,
152 * %-ENODATA if the property does not have a value,
153 * %-EPROTO if the property is not an array of numbers,
154 * %-EOVERFLOW if the size of the property is not as expected.
155 */
156int device_property_read_u8_array(struct device *dev, const char *propname,
157 u8 *val, size_t nval)
158{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100159 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100160}
161EXPORT_SYMBOL_GPL(device_property_read_u8_array);
162
163/**
164 * device_property_read_u16_array - return a u16 array property of a device
165 * @dev: Device to get the property of
166 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200167 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100168 * @nval: Size of the @val array
169 *
170 * Function reads an array of u16 properties with @propname from the device
171 * firmware description and stores them to @val if found.
172 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200173 * Return: number of values if @val was %NULL,
174 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100175 * %-EINVAL if given arguments are not valid,
176 * %-ENODATA if the property does not have a value,
177 * %-EPROTO if the property is not an array of numbers,
178 * %-EOVERFLOW if the size of the property is not as expected.
179 */
180int device_property_read_u16_array(struct device *dev, const char *propname,
181 u16 *val, size_t nval)
182{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100183 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100184}
185EXPORT_SYMBOL_GPL(device_property_read_u16_array);
186
187/**
188 * device_property_read_u32_array - return a u32 array property of a device
189 * @dev: Device to get the property of
190 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200191 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100192 * @nval: Size of the @val array
193 *
194 * Function reads an array of u32 properties with @propname from the device
195 * firmware description and stores them to @val if found.
196 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200197 * Return: number of values if @val was %NULL,
198 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100199 * %-EINVAL if given arguments are not valid,
200 * %-ENODATA if the property does not have a value,
201 * %-EPROTO if the property is not an array of numbers,
202 * %-EOVERFLOW if the size of the property is not as expected.
203 */
204int device_property_read_u32_array(struct device *dev, const char *propname,
205 u32 *val, size_t nval)
206{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100207 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100208}
209EXPORT_SYMBOL_GPL(device_property_read_u32_array);
210
211/**
212 * device_property_read_u64_array - return a u64 array property of a device
213 * @dev: Device to get the property of
214 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200215 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100216 * @nval: Size of the @val array
217 *
218 * Function reads an array of u64 properties with @propname from the device
219 * firmware description and stores them to @val if found.
220 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200221 * Return: number of values if @val was %NULL,
222 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100223 * %-EINVAL if given arguments are not valid,
224 * %-ENODATA if the property does not have a value,
225 * %-EPROTO if the property is not an array of numbers,
226 * %-EOVERFLOW if the size of the property is not as expected.
227 */
228int device_property_read_u64_array(struct device *dev, const char *propname,
229 u64 *val, size_t nval)
230{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100231 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100232}
233EXPORT_SYMBOL_GPL(device_property_read_u64_array);
234
235/**
236 * device_property_read_string_array - return a string array property of device
237 * @dev: Device to get the property of
238 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200239 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100240 * @nval: Size of the @val array
241 *
242 * Function reads an array of string properties with @propname from the device
243 * firmware description and stores them to @val if found.
244 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200245 * Return: number of values if @val was %NULL,
246 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100247 * %-EINVAL if given arguments are not valid,
248 * %-ENODATA if the property does not have a value,
249 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
250 * %-EOVERFLOW if the size of the property is not as expected.
251 */
252int device_property_read_string_array(struct device *dev, const char *propname,
253 const char **val, size_t nval)
254{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100255 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100256}
257EXPORT_SYMBOL_GPL(device_property_read_string_array);
258
259/**
260 * device_property_read_string - return a string property of a device
261 * @dev: Device to get the property of
262 * @propname: Name of the property
263 * @val: The value is stored here
264 *
265 * Function reads property @propname from the device firmware description and
266 * stores the value into @val if found. The value is checked to be a string.
267 *
268 * Return: %0 if the property was found (success),
269 * %-EINVAL if given arguments are not valid,
270 * %-ENODATA if the property does not have a value,
271 * %-EPROTO or %-EILSEQ if the property type is not a string.
272 */
273int device_property_read_string(struct device *dev, const char *propname,
274 const char **val)
275{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100276 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100277}
278EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100279
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100280#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
281 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
282 : of_property_count_elems_of_size((node), (propname), sizeof(type))
283
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100284#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
285({ \
286 int _ret_; \
287 if (is_of_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200288 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100289 _type_, _val_, _nval_); \
290 else if (is_acpi_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200291 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100292 _proptype_, _val_, _nval_); \
293 else \
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200294 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
295 _proptype_, _val_, _nval_); \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100296 _ret_; \
297})
298
299/**
300 * fwnode_property_read_u8_array - return a u8 array property of firmware node
301 * @fwnode: Firmware node to get the property of
302 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200303 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100304 * @nval: Size of the @val array
305 *
306 * Read an array of u8 properties with @propname from @fwnode and stores them to
307 * @val if found.
308 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200309 * Return: number of values if @val was %NULL,
310 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100311 * %-EINVAL if given arguments are not valid,
312 * %-ENODATA if the property does not have a value,
313 * %-EPROTO if the property is not an array of numbers,
314 * %-EOVERFLOW if the size of the property is not as expected,
315 * %-ENXIO if no suitable firmware interface is present.
316 */
317int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
318 const char *propname, u8 *val, size_t nval)
319{
320 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
321 val, nval);
322}
323EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
324
325/**
326 * fwnode_property_read_u16_array - return a u16 array property of firmware node
327 * @fwnode: Firmware node to get the property of
328 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200329 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100330 * @nval: Size of the @val array
331 *
332 * Read an array of u16 properties with @propname from @fwnode and store them to
333 * @val if found.
334 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200335 * Return: number of values if @val was %NULL,
336 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100337 * %-EINVAL if given arguments are not valid,
338 * %-ENODATA if the property does not have a value,
339 * %-EPROTO if the property is not an array of numbers,
340 * %-EOVERFLOW if the size of the property is not as expected,
341 * %-ENXIO if no suitable firmware interface is present.
342 */
343int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
344 const char *propname, u16 *val, size_t nval)
345{
346 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
347 val, nval);
348}
349EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
350
351/**
352 * fwnode_property_read_u32_array - return a u32 array property of firmware node
353 * @fwnode: Firmware node to get the property of
354 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200355 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100356 * @nval: Size of the @val array
357 *
358 * Read an array of u32 properties with @propname from @fwnode store them to
359 * @val if found.
360 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200361 * Return: number of values if @val was %NULL,
362 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100363 * %-EINVAL if given arguments are not valid,
364 * %-ENODATA if the property does not have a value,
365 * %-EPROTO if the property is not an array of numbers,
366 * %-EOVERFLOW if the size of the property is not as expected,
367 * %-ENXIO if no suitable firmware interface is present.
368 */
369int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
370 const char *propname, u32 *val, size_t nval)
371{
372 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
373 val, nval);
374}
375EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
376
377/**
378 * fwnode_property_read_u64_array - return a u64 array property firmware node
379 * @fwnode: Firmware node to get the property of
380 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200381 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100382 * @nval: Size of the @val array
383 *
384 * Read an array of u64 properties with @propname from @fwnode and store them to
385 * @val if found.
386 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200387 * Return: number of values if @val was %NULL,
388 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100389 * %-EINVAL if given arguments are not valid,
390 * %-ENODATA if the property does not have a value,
391 * %-EPROTO if the property is not an array of numbers,
392 * %-EOVERFLOW if the size of the property is not as expected,
393 * %-ENXIO if no suitable firmware interface is present.
394 */
395int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
396 const char *propname, u64 *val, size_t nval)
397{
398 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
399 val, nval);
400}
401EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
402
403/**
404 * fwnode_property_read_string_array - return string array property of a node
405 * @fwnode: Firmware node to get the property of
406 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200407 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100408 * @nval: Size of the @val array
409 *
410 * Read an string list property @propname from the given firmware node and store
411 * them to @val if found.
412 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200413 * Return: number of values if @val was %NULL,
414 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100415 * %-EINVAL if given arguments are not valid,
416 * %-ENODATA if the property does not have a value,
417 * %-EPROTO if the property is not an array of strings,
418 * %-EOVERFLOW if the size of the property is not as expected,
419 * %-ENXIO if no suitable firmware interface is present.
420 */
421int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
422 const char *propname, const char **val,
423 size_t nval)
424{
425 if (is_of_node(fwnode))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100426 return val ?
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200427 of_property_read_string_array(to_of_node(fwnode),
428 propname, val, nval) :
429 of_property_count_strings(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100430 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200431 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100432 DEV_PROP_STRING, val, nval);
433
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200434 return pset_prop_read_array(to_pset(fwnode), propname,
435 DEV_PROP_STRING, val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100436}
437EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
438
439/**
440 * fwnode_property_read_string - return a string property of a firmware node
441 * @fwnode: Firmware node to get the property of
442 * @propname: Name of the property
443 * @val: The value is stored here
444 *
445 * Read property @propname from the given firmware node and store the value into
446 * @val if found. The value is checked to be a string.
447 *
448 * Return: %0 if the property was found (success),
449 * %-EINVAL if given arguments are not valid,
450 * %-ENODATA if the property does not have a value,
451 * %-EPROTO or %-EILSEQ if the property is not a string,
452 * %-ENXIO if no suitable firmware interface is present.
453 */
454int fwnode_property_read_string(struct fwnode_handle *fwnode,
455 const char *propname, const char **val)
456{
457 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200458 return of_property_read_string(to_of_node(fwnode), propname, val);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100459 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200460 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100461 DEV_PROP_STRING, val, 1);
462
463 return -ENXIO;
464}
465EXPORT_SYMBOL_GPL(fwnode_property_read_string);
466
467/**
468 * device_get_next_child_node - Return the next child node handle for a device
469 * @dev: Device to find the next child node for.
470 * @child: Handle to one of the device's child nodes or a null handle.
471 */
472struct fwnode_handle *device_get_next_child_node(struct device *dev,
473 struct fwnode_handle *child)
474{
475 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
476 struct device_node *node;
477
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200478 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100479 if (node)
480 return &node->fwnode;
481 } else if (IS_ENABLED(CONFIG_ACPI)) {
482 struct acpi_device *node;
483
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200484 node = acpi_get_next_child(dev, to_acpi_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100485 if (node)
486 return acpi_fwnode_handle(node);
487 }
488 return NULL;
489}
490EXPORT_SYMBOL_GPL(device_get_next_child_node);
491
492/**
493 * fwnode_handle_put - Drop reference to a device node
494 * @fwnode: Pointer to the device node to drop the reference to.
495 *
496 * This has to be used when terminating device_for_each_child_node() iteration
497 * with break or return to prevent stale device node references from being left
498 * behind.
499 */
500void fwnode_handle_put(struct fwnode_handle *fwnode)
501{
502 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200503 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100504}
505EXPORT_SYMBOL_GPL(fwnode_handle_put);
506
507/**
508 * device_get_child_node_count - return the number of child nodes for device
509 * @dev: Device to cound the child nodes for
510 */
511unsigned int device_get_child_node_count(struct device *dev)
512{
513 struct fwnode_handle *child;
514 unsigned int count = 0;
515
516 device_for_each_child_node(dev, child)
517 count++;
518
519 return count;
520}
521EXPORT_SYMBOL_GPL(device_get_child_node_count);