blob: 2e8cd147f02d847c3c8f1bd89da070fc76e93027 [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>
Jeremy Linton4c96b7d2015-08-12 17:06:26 -050019#include <linux/etherdevice.h>
20#include <linux/phy.h>
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020021
22/**
23 * device_add_property_set - Add a collection of properties to a device object.
24 * @dev: Device to add properties to.
25 * @pset: Collection of properties to add.
26 *
27 * Associate a collection of device properties represented by @pset with @dev
28 * as its secondary firmware node.
29 */
30void device_add_property_set(struct device *dev, struct property_set *pset)
31{
32 if (pset)
33 pset->fwnode.type = FWNODE_PDATA;
34
35 set_secondary_fwnode(dev, &pset->fwnode);
36}
37EXPORT_SYMBOL_GPL(device_add_property_set);
38
39static inline bool is_pset(struct fwnode_handle *fwnode)
40{
41 return fwnode && fwnode->type == FWNODE_PDATA;
42}
43
44static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
45{
46 return is_pset(fwnode) ?
47 container_of(fwnode, struct property_set, fwnode) : NULL;
48}
49
50static struct property_entry *pset_prop_get(struct property_set *pset,
51 const char *name)
52{
53 struct property_entry *prop;
54
55 if (!pset || !pset->properties)
56 return NULL;
57
58 for (prop = pset->properties; prop->name; prop++)
59 if (!strcmp(name, prop->name))
60 return prop;
61
62 return NULL;
63}
64
65static int pset_prop_read_array(struct property_set *pset, const char *name,
66 enum dev_prop_type type, void *val, size_t nval)
67{
68 struct property_entry *prop;
69 unsigned int item_size;
70
71 prop = pset_prop_get(pset, name);
72 if (!prop)
73 return -ENODATA;
74
75 if (prop->type != type)
76 return -EPROTO;
77
78 if (!val)
79 return prop->nval;
80
81 if (prop->nval < nval)
82 return -EOVERFLOW;
83
84 switch (type) {
85 case DEV_PROP_U8:
86 item_size = sizeof(u8);
87 break;
88 case DEV_PROP_U16:
89 item_size = sizeof(u16);
90 break;
91 case DEV_PROP_U32:
92 item_size = sizeof(u32);
93 break;
94 case DEV_PROP_U64:
95 item_size = sizeof(u64);
96 break;
97 case DEV_PROP_STRING:
98 item_size = sizeof(const char *);
99 break;
100 default:
101 return -EINVAL;
102 }
103 memcpy(val, prop->value.raw_data, nval * item_size);
104 return 0;
105}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100106
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100107static inline struct fwnode_handle *dev_fwnode(struct device *dev)
108{
109 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
110 &dev->of_node->fwnode : dev->fwnode;
111}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100112
113/**
114 * device_property_present - check if a property of a device is present
115 * @dev: Device whose property is being checked
116 * @propname: Name of the property
117 *
118 * Check if property @propname is present in the device firmware description.
119 */
120bool device_property_present(struct device *dev, const char *propname)
121{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100122 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100123}
124EXPORT_SYMBOL_GPL(device_property_present);
125
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100126/**
127 * fwnode_property_present - check if a property of a firmware node is present
128 * @fwnode: Firmware node whose property to check
129 * @propname: Name of the property
130 */
131bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
132{
133 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200134 return of_property_read_bool(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100135 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200136 return !acpi_dev_prop_get(to_acpi_node(fwnode), propname, NULL);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100137
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200138 return !!pset_prop_get(to_pset(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100139}
140EXPORT_SYMBOL_GPL(fwnode_property_present);
141
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100142/**
143 * device_property_read_u8_array - return a u8 array property of a device
144 * @dev: Device to get the property of
145 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200146 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100147 * @nval: Size of the @val array
148 *
149 * Function reads an array of u8 properties with @propname from the device
150 * firmware description and stores them to @val if found.
151 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200152 * Return: number of values if @val was %NULL,
153 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100154 * %-EINVAL if given arguments are not valid,
155 * %-ENODATA if the property does not have a value,
156 * %-EPROTO if the property is not an array of numbers,
157 * %-EOVERFLOW if the size of the property is not as expected.
158 */
159int device_property_read_u8_array(struct device *dev, const char *propname,
160 u8 *val, size_t nval)
161{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100162 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100163}
164EXPORT_SYMBOL_GPL(device_property_read_u8_array);
165
166/**
167 * device_property_read_u16_array - return a u16 array property of a device
168 * @dev: Device to get the property of
169 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200170 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100171 * @nval: Size of the @val array
172 *
173 * Function reads an array of u16 properties with @propname from the device
174 * firmware description and stores them to @val if found.
175 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200176 * Return: number of values if @val was %NULL,
177 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100178 * %-EINVAL if given arguments are not valid,
179 * %-ENODATA if the property does not have a value,
180 * %-EPROTO if the property is not an array of numbers,
181 * %-EOVERFLOW if the size of the property is not as expected.
182 */
183int device_property_read_u16_array(struct device *dev, const char *propname,
184 u16 *val, size_t nval)
185{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100186 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100187}
188EXPORT_SYMBOL_GPL(device_property_read_u16_array);
189
190/**
191 * device_property_read_u32_array - return a u32 array property of a device
192 * @dev: Device to get the property of
193 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200194 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100195 * @nval: Size of the @val array
196 *
197 * Function reads an array of u32 properties with @propname from the device
198 * firmware description and stores them to @val if found.
199 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200200 * Return: number of values if @val was %NULL,
201 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100202 * %-EINVAL if given arguments are not valid,
203 * %-ENODATA if the property does not have a value,
204 * %-EPROTO if the property is not an array of numbers,
205 * %-EOVERFLOW if the size of the property is not as expected.
206 */
207int device_property_read_u32_array(struct device *dev, const char *propname,
208 u32 *val, size_t nval)
209{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100210 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100211}
212EXPORT_SYMBOL_GPL(device_property_read_u32_array);
213
214/**
215 * device_property_read_u64_array - return a u64 array property of a device
216 * @dev: Device to get the property of
217 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200218 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100219 * @nval: Size of the @val array
220 *
221 * Function reads an array of u64 properties with @propname from the device
222 * firmware description and stores them to @val if found.
223 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200224 * Return: number of values if @val was %NULL,
225 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100226 * %-EINVAL if given arguments are not valid,
227 * %-ENODATA if the property does not have a value,
228 * %-EPROTO if the property is not an array of numbers,
229 * %-EOVERFLOW if the size of the property is not as expected.
230 */
231int device_property_read_u64_array(struct device *dev, const char *propname,
232 u64 *val, size_t nval)
233{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100234 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100235}
236EXPORT_SYMBOL_GPL(device_property_read_u64_array);
237
238/**
239 * device_property_read_string_array - return a string array property of device
240 * @dev: Device to get the property of
241 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200242 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100243 * @nval: Size of the @val array
244 *
245 * Function reads an array of string properties with @propname from the device
246 * firmware description and stores them to @val if found.
247 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200248 * Return: number of values if @val was %NULL,
249 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100250 * %-EINVAL if given arguments are not valid,
251 * %-ENODATA if the property does not have a value,
252 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
253 * %-EOVERFLOW if the size of the property is not as expected.
254 */
255int device_property_read_string_array(struct device *dev, const char *propname,
256 const char **val, size_t nval)
257{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100258 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100259}
260EXPORT_SYMBOL_GPL(device_property_read_string_array);
261
262/**
263 * device_property_read_string - return a string property of a device
264 * @dev: Device to get the property of
265 * @propname: Name of the property
266 * @val: The value is stored here
267 *
268 * Function reads property @propname from the device firmware description and
269 * stores the value into @val if found. The value is checked to be a string.
270 *
271 * Return: %0 if the property was found (success),
272 * %-EINVAL if given arguments are not valid,
273 * %-ENODATA if the property does not have a value,
274 * %-EPROTO or %-EILSEQ if the property type is not a string.
275 */
276int device_property_read_string(struct device *dev, const char *propname,
277 const char **val)
278{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100279 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100280}
281EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100282
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100283#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
284 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
285 : of_property_count_elems_of_size((node), (propname), sizeof(type))
286
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100287#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
288({ \
289 int _ret_; \
290 if (is_of_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200291 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100292 _type_, _val_, _nval_); \
293 else if (is_acpi_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200294 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100295 _proptype_, _val_, _nval_); \
296 else \
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200297 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
298 _proptype_, _val_, _nval_); \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100299 _ret_; \
300})
301
302/**
303 * fwnode_property_read_u8_array - return a u8 array property of firmware node
304 * @fwnode: Firmware node to get the property of
305 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200306 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100307 * @nval: Size of the @val array
308 *
309 * Read an array of u8 properties with @propname from @fwnode and stores them to
310 * @val if found.
311 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200312 * Return: number of values if @val was %NULL,
313 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100314 * %-EINVAL if given arguments are not valid,
315 * %-ENODATA if the property does not have a value,
316 * %-EPROTO if the property is not an array of numbers,
317 * %-EOVERFLOW if the size of the property is not as expected,
318 * %-ENXIO if no suitable firmware interface is present.
319 */
320int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
321 const char *propname, u8 *val, size_t nval)
322{
323 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
324 val, nval);
325}
326EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
327
328/**
329 * fwnode_property_read_u16_array - return a u16 array property of firmware node
330 * @fwnode: Firmware node to get the property of
331 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200332 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100333 * @nval: Size of the @val array
334 *
335 * Read an array of u16 properties with @propname from @fwnode and store them to
336 * @val if found.
337 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200338 * Return: number of values if @val was %NULL,
339 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100340 * %-EINVAL if given arguments are not valid,
341 * %-ENODATA if the property does not have a value,
342 * %-EPROTO if the property is not an array of numbers,
343 * %-EOVERFLOW if the size of the property is not as expected,
344 * %-ENXIO if no suitable firmware interface is present.
345 */
346int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
347 const char *propname, u16 *val, size_t nval)
348{
349 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
350 val, nval);
351}
352EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
353
354/**
355 * fwnode_property_read_u32_array - return a u32 array property of firmware node
356 * @fwnode: Firmware node to get the property of
357 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200358 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100359 * @nval: Size of the @val array
360 *
361 * Read an array of u32 properties with @propname from @fwnode store them to
362 * @val if found.
363 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200364 * Return: number of values if @val was %NULL,
365 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100366 * %-EINVAL if given arguments are not valid,
367 * %-ENODATA if the property does not have a value,
368 * %-EPROTO if the property is not an array of numbers,
369 * %-EOVERFLOW if the size of the property is not as expected,
370 * %-ENXIO if no suitable firmware interface is present.
371 */
372int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
373 const char *propname, u32 *val, size_t nval)
374{
375 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
376 val, nval);
377}
378EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
379
380/**
381 * fwnode_property_read_u64_array - return a u64 array property firmware node
382 * @fwnode: Firmware node to get the property of
383 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200384 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100385 * @nval: Size of the @val array
386 *
387 * Read an array of u64 properties with @propname from @fwnode and store them to
388 * @val if found.
389 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200390 * Return: number of values if @val was %NULL,
391 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100392 * %-EINVAL if given arguments are not valid,
393 * %-ENODATA if the property does not have a value,
394 * %-EPROTO if the property is not an array of numbers,
395 * %-EOVERFLOW if the size of the property is not as expected,
396 * %-ENXIO if no suitable firmware interface is present.
397 */
398int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
399 const char *propname, u64 *val, size_t nval)
400{
401 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
402 val, nval);
403}
404EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
405
406/**
407 * fwnode_property_read_string_array - return string array property of a node
408 * @fwnode: Firmware node to get the property of
409 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200410 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100411 * @nval: Size of the @val array
412 *
413 * Read an string list property @propname from the given firmware node and store
414 * them to @val if found.
415 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200416 * Return: number of values if @val was %NULL,
417 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100418 * %-EINVAL if given arguments are not valid,
419 * %-ENODATA if the property does not have a value,
420 * %-EPROTO if the property is not an array of strings,
421 * %-EOVERFLOW if the size of the property is not as expected,
422 * %-ENXIO if no suitable firmware interface is present.
423 */
424int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
425 const char *propname, const char **val,
426 size_t nval)
427{
428 if (is_of_node(fwnode))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100429 return val ?
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200430 of_property_read_string_array(to_of_node(fwnode),
431 propname, val, nval) :
432 of_property_count_strings(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100433 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200434 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100435 DEV_PROP_STRING, val, nval);
436
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200437 return pset_prop_read_array(to_pset(fwnode), propname,
438 DEV_PROP_STRING, val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100439}
440EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
441
442/**
443 * fwnode_property_read_string - return a string property of a firmware node
444 * @fwnode: Firmware node to get the property of
445 * @propname: Name of the property
446 * @val: The value is stored here
447 *
448 * Read property @propname from the given firmware node and store the value into
449 * @val if found. The value is checked to be a string.
450 *
451 * Return: %0 if the property was found (success),
452 * %-EINVAL if given arguments are not valid,
453 * %-ENODATA if the property does not have a value,
454 * %-EPROTO or %-EILSEQ if the property is not a string,
455 * %-ENXIO if no suitable firmware interface is present.
456 */
457int fwnode_property_read_string(struct fwnode_handle *fwnode,
458 const char *propname, const char **val)
459{
460 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200461 return of_property_read_string(to_of_node(fwnode), propname, val);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100462 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200463 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100464 DEV_PROP_STRING, val, 1);
465
466 return -ENXIO;
467}
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);
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500538
539/**
540 * device_get_phy_mode - Get phy mode for given device_node
541 * @dev: Pointer to the given device
542 *
543 * The function gets phy interface string from property 'phy-mode' or
544 * 'phy-connection-type', and return its index in phy_modes table, or errno in
545 * error case.
546 */
547int device_get_phy_mode(struct device *dev)
548{
549 const char *pm;
550 int err, i;
551
552 err = device_property_read_string(dev, "phy-mode", &pm);
553 if (err < 0)
554 err = device_property_read_string(dev,
555 "phy-connection-type", &pm);
556 if (err < 0)
557 return err;
558
559 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
560 if (!strcasecmp(pm, phy_modes(i)))
561 return i;
562
563 return -ENODEV;
564}
565EXPORT_SYMBOL_GPL(device_get_phy_mode);
566
567static void *device_get_mac_addr(struct device *dev,
568 const char *name, char *addr,
569 int alen)
570{
571 int ret = device_property_read_u8_array(dev, name, addr, alen);
572
573 if (ret == 0 && is_valid_ether_addr(addr))
574 return addr;
575 return NULL;
576}
577
578/**
579 * Search the device tree for the best MAC address to use. 'mac-address' is
580 * checked first, because that is supposed to contain to "most recent" MAC
581 * address. If that isn't set, then 'local-mac-address' is checked next,
582 * because that is the default address. If that isn't set, then the obsolete
583 * 'address' is checked, just in case we're using an old device tree.
584 *
585 * Note that the 'address' property is supposed to contain a virtual address of
586 * the register set, but some DTS files have redefined that property to be the
587 * MAC address.
588 *
589 * All-zero MAC addresses are rejected, because those could be properties that
590 * exist in the device tree, but were not set by U-Boot. For example, the
591 * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
592 * addresses. Some older U-Boots only initialized 'local-mac-address'. In
593 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
594 * but is all zeros.
595*/
596void *device_get_mac_address(struct device *dev, char *addr, int alen)
597{
598 addr = device_get_mac_addr(dev, "mac-address", addr, alen);
599 if (addr)
600 return addr;
601
602 addr = device_get_mac_addr(dev, "local-mac-address", addr, alen);
603 if (addr)
604 return addr;
605
606 return device_get_mac_addr(dev, "address", addr, alen);
607}
608EXPORT_SYMBOL(device_get_mac_address);