blob: a5efb43258a9f2236371cb0253708ab1284d68d1 [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.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700158 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100159 */
160int device_property_read_u8_array(struct device *dev, const char *propname,
161 u8 *val, size_t nval)
162{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100163 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100164}
165EXPORT_SYMBOL_GPL(device_property_read_u8_array);
166
167/**
168 * device_property_read_u16_array - return a u16 array property of a device
169 * @dev: Device to get the property of
170 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200171 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100172 * @nval: Size of the @val array
173 *
174 * Function reads an array of u16 properties with @propname from the device
175 * firmware description and stores them to @val if found.
176 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200177 * Return: number of values if @val was %NULL,
178 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100179 * %-EINVAL if given arguments are not valid,
180 * %-ENODATA if the property does not have a value,
181 * %-EPROTO if the property is not an array of numbers,
182 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700183 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100184 */
185int device_property_read_u16_array(struct device *dev, const char *propname,
186 u16 *val, size_t nval)
187{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100188 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100189}
190EXPORT_SYMBOL_GPL(device_property_read_u16_array);
191
192/**
193 * device_property_read_u32_array - return a u32 array property of a device
194 * @dev: Device to get the property of
195 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200196 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100197 * @nval: Size of the @val array
198 *
199 * Function reads an array of u32 properties with @propname from the device
200 * firmware description and stores them to @val if found.
201 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200202 * Return: number of values if @val was %NULL,
203 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100204 * %-EINVAL if given arguments are not valid,
205 * %-ENODATA if the property does not have a value,
206 * %-EPROTO if the property is not an array of numbers,
207 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700208 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100209 */
210int device_property_read_u32_array(struct device *dev, const char *propname,
211 u32 *val, size_t nval)
212{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100213 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100214}
215EXPORT_SYMBOL_GPL(device_property_read_u32_array);
216
217/**
218 * device_property_read_u64_array - return a u64 array property of a device
219 * @dev: Device to get the property of
220 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200221 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100222 * @nval: Size of the @val array
223 *
224 * Function reads an array of u64 properties with @propname from the device
225 * firmware description and stores them to @val if found.
226 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200227 * Return: number of values if @val was %NULL,
228 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100229 * %-EINVAL if given arguments are not valid,
230 * %-ENODATA if the property does not have a value,
231 * %-EPROTO if the property is not an array of numbers,
232 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700233 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100234 */
235int device_property_read_u64_array(struct device *dev, const char *propname,
236 u64 *val, size_t nval)
237{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100238 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100239}
240EXPORT_SYMBOL_GPL(device_property_read_u64_array);
241
242/**
243 * device_property_read_string_array - return a string array property of device
244 * @dev: Device to get the property of
245 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200246 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100247 * @nval: Size of the @val array
248 *
249 * Function reads an array of string properties with @propname from the device
250 * firmware description and stores them to @val if found.
251 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200252 * Return: number of values if @val was %NULL,
253 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100254 * %-EINVAL if given arguments are not valid,
255 * %-ENODATA if the property does not have a value,
256 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
257 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700258 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100259 */
260int device_property_read_string_array(struct device *dev, const char *propname,
261 const char **val, size_t nval)
262{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100263 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100264}
265EXPORT_SYMBOL_GPL(device_property_read_string_array);
266
267/**
268 * device_property_read_string - return a string property of a device
269 * @dev: Device to get the property of
270 * @propname: Name of the property
271 * @val: The value is stored here
272 *
273 * Function reads property @propname from the device firmware description and
274 * stores the value into @val if found. The value is checked to be a string.
275 *
276 * Return: %0 if the property was found (success),
277 * %-EINVAL if given arguments are not valid,
278 * %-ENODATA if the property does not have a value,
279 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700280 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100281 */
282int device_property_read_string(struct device *dev, const char *propname,
283 const char **val)
284{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100285 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100286}
287EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100288
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100289#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
290 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
291 : of_property_count_elems_of_size((node), (propname), sizeof(type))
292
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100293#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
294({ \
295 int _ret_; \
296 if (is_of_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200297 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100298 _type_, _val_, _nval_); \
299 else if (is_acpi_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200300 _ret_ = acpi_dev_prop_read(to_acpi_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100301 _proptype_, _val_, _nval_); \
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700302 else if (is_pset(_fwnode_)) \
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200303 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
304 _proptype_, _val_, _nval_); \
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700305 else \
306 _ret_ = -ENXIO; \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100307 _ret_; \
308})
309
310/**
311 * fwnode_property_read_u8_array - return a u8 array property of firmware node
312 * @fwnode: Firmware node to get the property of
313 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200314 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100315 * @nval: Size of the @val array
316 *
317 * Read an array of u8 properties with @propname from @fwnode and stores them to
318 * @val if found.
319 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200320 * Return: number of values if @val was %NULL,
321 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100322 * %-EINVAL if given arguments are not valid,
323 * %-ENODATA if the property does not have a value,
324 * %-EPROTO if the property is not an array of numbers,
325 * %-EOVERFLOW if the size of the property is not as expected,
326 * %-ENXIO if no suitable firmware interface is present.
327 */
328int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
329 const char *propname, u8 *val, size_t nval)
330{
331 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
332 val, nval);
333}
334EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
335
336/**
337 * fwnode_property_read_u16_array - return a u16 array property of firmware node
338 * @fwnode: Firmware node to get the property of
339 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200340 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100341 * @nval: Size of the @val array
342 *
343 * Read an array of u16 properties with @propname from @fwnode and store them to
344 * @val if found.
345 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200346 * Return: number of values if @val was %NULL,
347 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100348 * %-EINVAL if given arguments are not valid,
349 * %-ENODATA if the property does not have a value,
350 * %-EPROTO if the property is not an array of numbers,
351 * %-EOVERFLOW if the size of the property is not as expected,
352 * %-ENXIO if no suitable firmware interface is present.
353 */
354int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
355 const char *propname, u16 *val, size_t nval)
356{
357 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
358 val, nval);
359}
360EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
361
362/**
363 * fwnode_property_read_u32_array - return a u32 array property of firmware node
364 * @fwnode: Firmware node to get the property of
365 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200366 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100367 * @nval: Size of the @val array
368 *
369 * Read an array of u32 properties with @propname from @fwnode store them to
370 * @val if found.
371 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200372 * Return: number of values if @val was %NULL,
373 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100374 * %-EINVAL if given arguments are not valid,
375 * %-ENODATA if the property does not have a value,
376 * %-EPROTO if the property is not an array of numbers,
377 * %-EOVERFLOW if the size of the property is not as expected,
378 * %-ENXIO if no suitable firmware interface is present.
379 */
380int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
381 const char *propname, u32 *val, size_t nval)
382{
383 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
384 val, nval);
385}
386EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
387
388/**
389 * fwnode_property_read_u64_array - return a u64 array property firmware node
390 * @fwnode: Firmware node to get the property of
391 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200392 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100393 * @nval: Size of the @val array
394 *
395 * Read an array of u64 properties with @propname from @fwnode and store them to
396 * @val if found.
397 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200398 * Return: number of values if @val was %NULL,
399 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100400 * %-EINVAL if given arguments are not valid,
401 * %-ENODATA if the property does not have a value,
402 * %-EPROTO if the property is not an array of numbers,
403 * %-EOVERFLOW if the size of the property is not as expected,
404 * %-ENXIO if no suitable firmware interface is present.
405 */
406int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
407 const char *propname, u64 *val, size_t nval)
408{
409 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
410 val, nval);
411}
412EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
413
414/**
415 * fwnode_property_read_string_array - return string array property of a node
416 * @fwnode: Firmware node to get the property of
417 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200418 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100419 * @nval: Size of the @val array
420 *
421 * Read an string list property @propname from the given firmware node and store
422 * them to @val if found.
423 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200424 * Return: number of values if @val was %NULL,
425 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100426 * %-EINVAL if given arguments are not valid,
427 * %-ENODATA if the property does not have a value,
428 * %-EPROTO if the property is not an array of strings,
429 * %-EOVERFLOW if the size of the property is not as expected,
430 * %-ENXIO if no suitable firmware interface is present.
431 */
432int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
433 const char *propname, const char **val,
434 size_t nval)
435{
436 if (is_of_node(fwnode))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100437 return val ?
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200438 of_property_read_string_array(to_of_node(fwnode),
439 propname, val, nval) :
440 of_property_count_strings(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100441 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200442 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100443 DEV_PROP_STRING, val, nval);
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700444 else if (is_pset(fwnode))
445 return pset_prop_read_array(to_pset(fwnode), propname,
446 DEV_PROP_STRING, val, nval);
447 return -ENXIO;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100448}
449EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
450
451/**
452 * fwnode_property_read_string - return a string property of a firmware node
453 * @fwnode: Firmware node to get the property of
454 * @propname: Name of the property
455 * @val: The value is stored here
456 *
457 * Read property @propname from the given firmware node and store the value into
458 * @val if found. The value is checked to be a string.
459 *
460 * Return: %0 if the property was found (success),
461 * %-EINVAL if given arguments are not valid,
462 * %-ENODATA if the property does not have a value,
463 * %-EPROTO or %-EILSEQ if the property is not a string,
464 * %-ENXIO if no suitable firmware interface is present.
465 */
466int fwnode_property_read_string(struct fwnode_handle *fwnode,
467 const char *propname, const char **val)
468{
469 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200470 return of_property_read_string(to_of_node(fwnode), propname, val);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100471 else if (is_acpi_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200472 return acpi_dev_prop_read(to_acpi_node(fwnode), propname,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100473 DEV_PROP_STRING, val, 1);
474
475 return -ENXIO;
476}
477EXPORT_SYMBOL_GPL(fwnode_property_read_string);
478
479/**
480 * device_get_next_child_node - Return the next child node handle for a device
481 * @dev: Device to find the next child node for.
482 * @child: Handle to one of the device's child nodes or a null handle.
483 */
484struct fwnode_handle *device_get_next_child_node(struct device *dev,
485 struct fwnode_handle *child)
486{
487 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
488 struct device_node *node;
489
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200490 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100491 if (node)
492 return &node->fwnode;
493 } else if (IS_ENABLED(CONFIG_ACPI)) {
494 struct acpi_device *node;
495
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200496 node = acpi_get_next_child(dev, to_acpi_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100497 if (node)
498 return acpi_fwnode_handle(node);
499 }
500 return NULL;
501}
502EXPORT_SYMBOL_GPL(device_get_next_child_node);
503
504/**
505 * fwnode_handle_put - Drop reference to a device node
506 * @fwnode: Pointer to the device node to drop the reference to.
507 *
508 * This has to be used when terminating device_for_each_child_node() iteration
509 * with break or return to prevent stale device node references from being left
510 * behind.
511 */
512void fwnode_handle_put(struct fwnode_handle *fwnode)
513{
514 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200515 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100516}
517EXPORT_SYMBOL_GPL(fwnode_handle_put);
518
519/**
520 * device_get_child_node_count - return the number of child nodes for device
521 * @dev: Device to cound the child nodes for
522 */
523unsigned int device_get_child_node_count(struct device *dev)
524{
525 struct fwnode_handle *child;
526 unsigned int count = 0;
527
528 device_for_each_child_node(dev, child)
529 count++;
530
531 return count;
532}
533EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -0500534
535bool device_dma_is_coherent(struct device *dev)
536{
537 bool coherent = false;
538
539 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
540 coherent = of_dma_is_coherent(dev->of_node);
541 else
542 acpi_check_dma(ACPI_COMPANION(dev), &coherent);
543
544 return coherent;
545}
546EXPORT_SYMBOL_GPL(device_dma_is_coherent);
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500547
548/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500549 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500550 * @dev: Pointer to the given device
551 *
552 * The function gets phy interface string from property 'phy-mode' or
553 * 'phy-connection-type', and return its index in phy_modes table, or errno in
554 * error case.
555 */
556int device_get_phy_mode(struct device *dev)
557{
558 const char *pm;
559 int err, i;
560
561 err = device_property_read_string(dev, "phy-mode", &pm);
562 if (err < 0)
563 err = device_property_read_string(dev,
564 "phy-connection-type", &pm);
565 if (err < 0)
566 return err;
567
568 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
569 if (!strcasecmp(pm, phy_modes(i)))
570 return i;
571
572 return -ENODEV;
573}
574EXPORT_SYMBOL_GPL(device_get_phy_mode);
575
576static void *device_get_mac_addr(struct device *dev,
577 const char *name, char *addr,
578 int alen)
579{
580 int ret = device_property_read_u8_array(dev, name, addr, alen);
581
Jeremy Linton2f710a32015-08-19 11:46:42 -0500582 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500583 return addr;
584 return NULL;
585}
586
587/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500588 * device_get_mac_address - Get the MAC for a given device
589 * @dev: Pointer to the device
590 * @addr: Address of buffer to store the MAC in
591 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
592 *
593 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500594 * checked first, because that is supposed to contain to "most recent" MAC
595 * address. If that isn't set, then 'local-mac-address' is checked next,
596 * because that is the default address. If that isn't set, then the obsolete
597 * 'address' is checked, just in case we're using an old device tree.
598 *
599 * Note that the 'address' property is supposed to contain a virtual address of
600 * the register set, but some DTS files have redefined that property to be the
601 * MAC address.
602 *
603 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -0500604 * exist in the firmware tables, but were not updated by the firmware. For
605 * example, the DTS could define 'mac-address' and 'local-mac-address', with
606 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
607 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
608 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500609*/
610void *device_get_mac_address(struct device *dev, char *addr, int alen)
611{
612 addr = device_get_mac_addr(dev, "mac-address", addr, alen);
613 if (addr)
614 return addr;
615
616 addr = device_get_mac_addr(dev, "local-mac-address", addr, alen);
617 if (addr)
618 return addr;
619
620 return device_get_mac_addr(dev, "address", addr, alen);
621}
622EXPORT_SYMBOL(device_get_mac_address);