blob: 86834bde45858bad759da817458da3edc1a14e88 [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{
Andy Shevchenkoecc87ee2015-08-05 16:51:11 +030032 if (!pset)
33 return;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020034
Andy Shevchenkoecc87ee2015-08-05 16:51:11 +030035 pset->fwnode.type = FWNODE_PDATA;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020036 set_secondary_fwnode(dev, &pset->fwnode);
37}
38EXPORT_SYMBOL_GPL(device_add_property_set);
39
Andy Shevchenko61f5e292015-11-30 17:11:30 +020040static inline bool is_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020041{
42 return fwnode && fwnode->type == FWNODE_PDATA;
43}
44
Andy Shevchenko61f5e292015-11-30 17:11:30 +020045static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020046{
Andy Shevchenko61f5e292015-11-30 17:11:30 +020047 return is_pset_node(fwnode) ?
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020048 container_of(fwnode, struct property_set, fwnode) : NULL;
49}
50
51static struct property_entry *pset_prop_get(struct property_set *pset,
52 const char *name)
53{
54 struct property_entry *prop;
55
56 if (!pset || !pset->properties)
57 return NULL;
58
59 for (prop = pset->properties; prop->name; prop++)
60 if (!strcmp(name, prop->name))
61 return prop;
62
63 return NULL;
64}
65
Andy Shevchenko318a19712015-11-30 17:11:31 +020066static void *pset_prop_find(struct property_set *pset, const char *propname,
67 size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020068{
69 struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +020070 void *pointer;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020071
Andy Shevchenko318a19712015-11-30 17:11:31 +020072 prop = pset_prop_get(pset, propname);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020073 if (!prop)
Andy Shevchenko318a19712015-11-30 17:11:31 +020074 return ERR_PTR(-EINVAL);
75 pointer = prop->value.raw_data;
76 if (!pointer)
77 return ERR_PTR(-ENODATA);
78 if (length > prop->length)
79 return ERR_PTR(-EOVERFLOW);
80 return pointer;
81}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020082
Andy Shevchenko318a19712015-11-30 17:11:31 +020083static int pset_prop_read_u8_array(struct property_set *pset,
84 const char *propname,
85 u8 *values, size_t nval)
86{
87 void *pointer;
88 size_t length = nval * sizeof(*values);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020089
Andy Shevchenko318a19712015-11-30 17:11:31 +020090 pointer = pset_prop_find(pset, propname, length);
91 if (IS_ERR(pointer))
92 return PTR_ERR(pointer);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020093
Andy Shevchenko318a19712015-11-30 17:11:31 +020094 memcpy(values, pointer, length);
95 return 0;
96}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020097
Andy Shevchenko318a19712015-11-30 17:11:31 +020098static int pset_prop_read_u16_array(struct property_set *pset,
99 const char *propname,
100 u16 *values, size_t nval)
101{
102 void *pointer;
103 size_t length = nval * sizeof(*values);
104
105 pointer = pset_prop_find(pset, propname, length);
106 if (IS_ERR(pointer))
107 return PTR_ERR(pointer);
108
109 memcpy(values, pointer, length);
110 return 0;
111}
112
113static int pset_prop_read_u32_array(struct property_set *pset,
114 const char *propname,
115 u32 *values, size_t nval)
116{
117 void *pointer;
118 size_t length = nval * sizeof(*values);
119
120 pointer = pset_prop_find(pset, propname, length);
121 if (IS_ERR(pointer))
122 return PTR_ERR(pointer);
123
124 memcpy(values, pointer, length);
125 return 0;
126}
127
128static int pset_prop_read_u64_array(struct property_set *pset,
129 const char *propname,
130 u64 *values, size_t nval)
131{
132 void *pointer;
133 size_t length = nval * sizeof(*values);
134
135 pointer = pset_prop_find(pset, propname, length);
136 if (IS_ERR(pointer))
137 return PTR_ERR(pointer);
138
139 memcpy(values, pointer, length);
140 return 0;
141}
142
143static int pset_prop_count_elems_of_size(struct property_set *pset,
144 const char *propname, size_t length)
145{
146 struct property_entry *prop;
147
148 prop = pset_prop_get(pset, propname);
149 if (!prop)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200150 return -EINVAL;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200151
152 return prop->length / length;
153}
154
155static int pset_prop_read_string_array(struct property_set *pset,
156 const char *propname,
157 const char **strings, size_t nval)
158{
159 void *pointer;
160 size_t length = nval * sizeof(*strings);
161
162 pointer = pset_prop_find(pset, propname, length);
163 if (IS_ERR(pointer))
164 return PTR_ERR(pointer);
165
166 memcpy(strings, pointer, length);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200167 return 0;
168}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100169
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100170static inline struct fwnode_handle *dev_fwnode(struct device *dev)
171{
172 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
173 &dev->of_node->fwnode : dev->fwnode;
174}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100175
176/**
177 * device_property_present - check if a property of a device is present
178 * @dev: Device whose property is being checked
179 * @propname: Name of the property
180 *
181 * Check if property @propname is present in the device firmware description.
182 */
183bool device_property_present(struct device *dev, const char *propname)
184{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100185 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100186}
187EXPORT_SYMBOL_GPL(device_property_present);
188
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100189/**
190 * fwnode_property_present - check if a property of a firmware node is present
191 * @fwnode: Firmware node whose property to check
192 * @propname: Name of the property
193 */
194bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
195{
196 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200197 return of_property_read_bool(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100198 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200199 return !acpi_node_prop_get(fwnode, propname, NULL);
Andy Shevchenko61f5e292015-11-30 17:11:30 +0200200 else if (is_pset_node(fwnode))
201 return !!pset_prop_get(to_pset_node(fwnode), propname);
Andy Shevchenkoe3f9e292015-11-30 17:11:29 +0200202 return false;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100203}
204EXPORT_SYMBOL_GPL(fwnode_property_present);
205
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100206/**
207 * device_property_read_u8_array - return a u8 array property of a device
208 * @dev: Device to get the property of
209 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200210 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100211 * @nval: Size of the @val array
212 *
213 * Function reads an array of u8 properties with @propname from the device
214 * firmware description and stores them to @val if found.
215 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200216 * Return: number of values if @val was %NULL,
217 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100218 * %-EINVAL if given arguments are not valid,
219 * %-ENODATA if the property does not have a value,
220 * %-EPROTO if the property is not an array of numbers,
221 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700222 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100223 */
224int device_property_read_u8_array(struct device *dev, const char *propname,
225 u8 *val, size_t nval)
226{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100227 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100228}
229EXPORT_SYMBOL_GPL(device_property_read_u8_array);
230
231/**
232 * device_property_read_u16_array - return a u16 array property of a device
233 * @dev: Device to get the property of
234 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200235 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100236 * @nval: Size of the @val array
237 *
238 * Function reads an array of u16 properties with @propname from the device
239 * firmware description and stores them to @val if found.
240 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200241 * Return: number of values if @val was %NULL,
242 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100243 * %-EINVAL if given arguments are not valid,
244 * %-ENODATA if the property does not have a value,
245 * %-EPROTO if the property is not an array of numbers,
246 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700247 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100248 */
249int device_property_read_u16_array(struct device *dev, const char *propname,
250 u16 *val, size_t nval)
251{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100252 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100253}
254EXPORT_SYMBOL_GPL(device_property_read_u16_array);
255
256/**
257 * device_property_read_u32_array - return a u32 array property of a device
258 * @dev: Device to get the property of
259 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200260 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100261 * @nval: Size of the @val array
262 *
263 * Function reads an array of u32 properties with @propname from the device
264 * firmware description and stores them to @val if found.
265 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200266 * Return: number of values if @val was %NULL,
267 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100268 * %-EINVAL if given arguments are not valid,
269 * %-ENODATA if the property does not have a value,
270 * %-EPROTO if the property is not an array of numbers,
271 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700272 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100273 */
274int device_property_read_u32_array(struct device *dev, const char *propname,
275 u32 *val, size_t nval)
276{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100277 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100278}
279EXPORT_SYMBOL_GPL(device_property_read_u32_array);
280
281/**
282 * device_property_read_u64_array - return a u64 array property of a device
283 * @dev: Device to get the property of
284 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200285 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100286 * @nval: Size of the @val array
287 *
288 * Function reads an array of u64 properties with @propname from the device
289 * firmware description and stores them to @val if found.
290 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200291 * Return: number of values if @val was %NULL,
292 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100293 * %-EINVAL if given arguments are not valid,
294 * %-ENODATA if the property does not have a value,
295 * %-EPROTO if the property is not an array of numbers,
296 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700297 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100298 */
299int device_property_read_u64_array(struct device *dev, const char *propname,
300 u64 *val, size_t nval)
301{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100302 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100303}
304EXPORT_SYMBOL_GPL(device_property_read_u64_array);
305
306/**
307 * device_property_read_string_array - return a string array property of device
308 * @dev: Device to get the property of
309 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200310 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100311 * @nval: Size of the @val array
312 *
313 * Function reads an array of string properties with @propname from the device
314 * firmware description and stores them to @val if found.
315 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200316 * Return: number of values if @val was %NULL,
317 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100318 * %-EINVAL if given arguments are not valid,
319 * %-ENODATA if the property does not have a value,
320 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
321 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700322 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100323 */
324int device_property_read_string_array(struct device *dev, const char *propname,
325 const char **val, size_t nval)
326{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100327 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100328}
329EXPORT_SYMBOL_GPL(device_property_read_string_array);
330
331/**
332 * device_property_read_string - return a string property of a device
333 * @dev: Device to get the property of
334 * @propname: Name of the property
335 * @val: The value is stored here
336 *
337 * Function reads property @propname from the device firmware description and
338 * stores the value into @val if found. The value is checked to be a string.
339 *
340 * Return: %0 if the property was found (success),
341 * %-EINVAL if given arguments are not valid,
342 * %-ENODATA if the property does not have a value,
343 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700344 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100345 */
346int device_property_read_string(struct device *dev, const char *propname,
347 const char **val)
348{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100349 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100350}
351EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100352
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300353/**
354 * device_property_match_string - find a string in an array and return index
355 * @dev: Device to get the property of
356 * @propname: Name of the property holding the array
357 * @string: String to look for
358 *
359 * Find a given string in a string array and if it is found return the
360 * index back.
361 *
362 * Return: %0 if the property was found (success),
363 * %-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 strings,
366 * %-ENXIO if no suitable firmware interface is present.
367 */
368int device_property_match_string(struct device *dev, const char *propname,
369 const char *string)
370{
371 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
372}
373EXPORT_SYMBOL_GPL(device_property_match_string);
374
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100375#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
376 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
377 : of_property_count_elems_of_size((node), (propname), sizeof(type))
378
Andy Shevchenko318a19712015-11-30 17:11:31 +0200379#define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
380 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
381 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
382
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100383#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
384({ \
385 int _ret_; \
386 if (is_of_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200387 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100388 _type_, _val_, _nval_); \
389 else if (is_acpi_node(_fwnode_)) \
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200390 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
391 _val_, _nval_); \
Andy Shevchenko61f5e292015-11-30 17:11:30 +0200392 else if (is_pset_node(_fwnode_)) \
Andy Shevchenko318a19712015-11-30 17:11:31 +0200393 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
394 _type_, _val_, _nval_); \
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700395 else \
396 _ret_ = -ENXIO; \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100397 _ret_; \
398})
399
400/**
401 * fwnode_property_read_u8_array - return a u8 array property of firmware node
402 * @fwnode: Firmware node to get the property of
403 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200404 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100405 * @nval: Size of the @val array
406 *
407 * Read an array of u8 properties with @propname from @fwnode and stores them to
408 * @val if found.
409 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200410 * Return: number of values if @val was %NULL,
411 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100412 * %-EINVAL if given arguments are not valid,
413 * %-ENODATA if the property does not have a value,
414 * %-EPROTO if the property is not an array of numbers,
415 * %-EOVERFLOW if the size of the property is not as expected,
416 * %-ENXIO if no suitable firmware interface is present.
417 */
418int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
419 const char *propname, u8 *val, size_t nval)
420{
421 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
422 val, nval);
423}
424EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
425
426/**
427 * fwnode_property_read_u16_array - return a u16 array property of firmware node
428 * @fwnode: Firmware node to get the property of
429 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200430 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100431 * @nval: Size of the @val array
432 *
433 * Read an array of u16 properties with @propname from @fwnode and store them to
434 * @val if found.
435 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200436 * Return: number of values if @val was %NULL,
437 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100438 * %-EINVAL if given arguments are not valid,
439 * %-ENODATA if the property does not have a value,
440 * %-EPROTO if the property is not an array of numbers,
441 * %-EOVERFLOW if the size of the property is not as expected,
442 * %-ENXIO if no suitable firmware interface is present.
443 */
444int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
445 const char *propname, u16 *val, size_t nval)
446{
447 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
448 val, nval);
449}
450EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
451
452/**
453 * fwnode_property_read_u32_array - return a u32 array property of firmware node
454 * @fwnode: Firmware node to get the property of
455 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200456 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100457 * @nval: Size of the @val array
458 *
459 * Read an array of u32 properties with @propname from @fwnode store them to
460 * @val if found.
461 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200462 * Return: number of values if @val was %NULL,
463 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100464 * %-EINVAL if given arguments are not valid,
465 * %-ENODATA if the property does not have a value,
466 * %-EPROTO if the property is not an array of numbers,
467 * %-EOVERFLOW if the size of the property is not as expected,
468 * %-ENXIO if no suitable firmware interface is present.
469 */
470int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
471 const char *propname, u32 *val, size_t nval)
472{
473 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
474 val, nval);
475}
476EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
477
478/**
479 * fwnode_property_read_u64_array - return a u64 array property firmware node
480 * @fwnode: Firmware node to get the property of
481 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200482 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100483 * @nval: Size of the @val array
484 *
485 * Read an array of u64 properties with @propname from @fwnode and store them to
486 * @val if found.
487 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200488 * Return: number of values if @val was %NULL,
489 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100490 * %-EINVAL if given arguments are not valid,
491 * %-ENODATA if the property does not have a value,
492 * %-EPROTO if the property is not an array of numbers,
493 * %-EOVERFLOW if the size of the property is not as expected,
494 * %-ENXIO if no suitable firmware interface is present.
495 */
496int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
497 const char *propname, u64 *val, size_t nval)
498{
499 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
500 val, nval);
501}
502EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
503
504/**
505 * fwnode_property_read_string_array - return string array property of a node
506 * @fwnode: Firmware node to get the property of
507 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200508 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100509 * @nval: Size of the @val array
510 *
511 * Read an string list property @propname from the given firmware node and store
512 * them to @val if found.
513 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200514 * Return: number of values if @val was %NULL,
515 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100516 * %-EINVAL if given arguments are not valid,
517 * %-ENODATA if the property does not have a value,
518 * %-EPROTO if the property is not an array of strings,
519 * %-EOVERFLOW if the size of the property is not as expected,
520 * %-ENXIO if no suitable firmware interface is present.
521 */
522int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
523 const char *propname, const char **val,
524 size_t nval)
525{
526 if (is_of_node(fwnode))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100527 return val ?
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200528 of_property_read_string_array(to_of_node(fwnode),
529 propname, val, nval) :
530 of_property_count_strings(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100531 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200532 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
533 val, nval);
Andy Shevchenko61f5e292015-11-30 17:11:30 +0200534 else if (is_pset_node(fwnode))
Andy Shevchenko318a19712015-11-30 17:11:31 +0200535 return val ?
536 pset_prop_read_string_array(to_pset_node(fwnode),
537 propname, val, nval) :
538 pset_prop_count_elems_of_size(to_pset_node(fwnode),
539 propname,
540 sizeof(const char *));
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700541 return -ENXIO;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100542}
543EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
544
545/**
546 * fwnode_property_read_string - return a string property of a firmware node
547 * @fwnode: Firmware node to get the property of
548 * @propname: Name of the property
549 * @val: The value is stored here
550 *
551 * Read property @propname from the given firmware node and store the value into
552 * @val if found. The value is checked to be a string.
553 *
554 * Return: %0 if the property was found (success),
555 * %-EINVAL if given arguments are not valid,
556 * %-ENODATA if the property does not have a value,
557 * %-EPROTO or %-EILSEQ if the property is not a string,
558 * %-ENXIO if no suitable firmware interface is present.
559 */
560int fwnode_property_read_string(struct fwnode_handle *fwnode,
561 const char *propname, const char **val)
562{
563 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200564 return of_property_read_string(to_of_node(fwnode), propname, val);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100565 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200566 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
567 val, 1);
Andy Shevchenko61f5e292015-11-30 17:11:30 +0200568 else if (is_pset_node(fwnode))
Andy Shevchenko318a19712015-11-30 17:11:31 +0200569 return pset_prop_read_string_array(to_pset_node(fwnode),
570 propname, val, 1);
Andy Shevchenkoe3f9e292015-11-30 17:11:29 +0200571 return -ENXIO;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100572}
573EXPORT_SYMBOL_GPL(fwnode_property_read_string);
574
575/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300576 * fwnode_property_match_string - find a string in an array and return index
577 * @fwnode: Firmware node to get the property of
578 * @propname: Name of the property holding the array
579 * @string: String to look for
580 *
581 * Find a given string in a string array and if it is found return the
582 * index back.
583 *
584 * Return: %0 if the property was found (success),
585 * %-EINVAL if given arguments are not valid,
586 * %-ENODATA if the property does not have a value,
587 * %-EPROTO if the property is not an array of strings,
588 * %-ENXIO if no suitable firmware interface is present.
589 */
590int fwnode_property_match_string(struct fwnode_handle *fwnode,
591 const char *propname, const char *string)
592{
593 const char **values;
594 int nval, ret, i;
595
596 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
597 if (nval < 0)
598 return nval;
599
600 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
601 if (!values)
602 return -ENOMEM;
603
604 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
605 if (ret < 0)
606 goto out;
607
608 ret = -ENODATA;
609 for (i = 0; i < nval; i++) {
610 if (!strcmp(values[i], string)) {
611 ret = i;
612 break;
613 }
614 }
615out:
616 kfree(values);
617 return ret;
618}
619EXPORT_SYMBOL_GPL(fwnode_property_match_string);
620
621/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100622 * device_get_next_child_node - Return the next child node handle for a device
623 * @dev: Device to find the next child node for.
624 * @child: Handle to one of the device's child nodes or a null handle.
625 */
626struct fwnode_handle *device_get_next_child_node(struct device *dev,
627 struct fwnode_handle *child)
628{
629 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
630 struct device_node *node;
631
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200632 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100633 if (node)
634 return &node->fwnode;
635 } else if (IS_ENABLED(CONFIG_ACPI)) {
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200636 return acpi_get_next_subnode(dev, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100637 }
638 return NULL;
639}
640EXPORT_SYMBOL_GPL(device_get_next_child_node);
641
642/**
643 * fwnode_handle_put - Drop reference to a device node
644 * @fwnode: Pointer to the device node to drop the reference to.
645 *
646 * This has to be used when terminating device_for_each_child_node() iteration
647 * with break or return to prevent stale device node references from being left
648 * behind.
649 */
650void fwnode_handle_put(struct fwnode_handle *fwnode)
651{
652 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200653 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100654}
655EXPORT_SYMBOL_GPL(fwnode_handle_put);
656
657/**
658 * device_get_child_node_count - return the number of child nodes for device
659 * @dev: Device to cound the child nodes for
660 */
661unsigned int device_get_child_node_count(struct device *dev)
662{
663 struct fwnode_handle *child;
664 unsigned int count = 0;
665
666 device_for_each_child_node(dev, child)
667 count++;
668
669 return count;
670}
671EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -0500672
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -0700673bool device_dma_supported(struct device *dev)
674{
675 /* For DT, this is always supported.
676 * For ACPI, this depends on CCA, which
677 * is determined by the acpi_dma_supported().
678 */
679 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
680 return true;
681
682 return acpi_dma_supported(ACPI_COMPANION(dev));
683}
684EXPORT_SYMBOL_GPL(device_dma_supported);
685
686enum dev_dma_attr device_get_dma_attr(struct device *dev)
687{
688 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
689
690 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
691 if (of_dma_is_coherent(dev->of_node))
692 attr = DEV_DMA_COHERENT;
693 else
694 attr = DEV_DMA_NON_COHERENT;
695 } else
696 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
697
698 return attr;
699}
700EXPORT_SYMBOL_GPL(device_get_dma_attr);
701
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500702/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500703 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500704 * @dev: Pointer to the given device
705 *
706 * The function gets phy interface string from property 'phy-mode' or
707 * 'phy-connection-type', and return its index in phy_modes table, or errno in
708 * error case.
709 */
710int device_get_phy_mode(struct device *dev)
711{
712 const char *pm;
713 int err, i;
714
715 err = device_property_read_string(dev, "phy-mode", &pm);
716 if (err < 0)
717 err = device_property_read_string(dev,
718 "phy-connection-type", &pm);
719 if (err < 0)
720 return err;
721
722 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
723 if (!strcasecmp(pm, phy_modes(i)))
724 return i;
725
726 return -ENODEV;
727}
728EXPORT_SYMBOL_GPL(device_get_phy_mode);
729
730static void *device_get_mac_addr(struct device *dev,
731 const char *name, char *addr,
732 int alen)
733{
734 int ret = device_property_read_u8_array(dev, name, addr, alen);
735
Jeremy Linton2f710a32015-08-19 11:46:42 -0500736 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500737 return addr;
738 return NULL;
739}
740
741/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500742 * device_get_mac_address - Get the MAC for a given device
743 * @dev: Pointer to the device
744 * @addr: Address of buffer to store the MAC in
745 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
746 *
747 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500748 * checked first, because that is supposed to contain to "most recent" MAC
749 * address. If that isn't set, then 'local-mac-address' is checked next,
750 * because that is the default address. If that isn't set, then the obsolete
751 * 'address' is checked, just in case we're using an old device tree.
752 *
753 * Note that the 'address' property is supposed to contain a virtual address of
754 * the register set, but some DTS files have redefined that property to be the
755 * MAC address.
756 *
757 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -0500758 * exist in the firmware tables, but were not updated by the firmware. For
759 * example, the DTS could define 'mac-address' and 'local-mac-address', with
760 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
761 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
762 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500763*/
764void *device_get_mac_address(struct device *dev, char *addr, int alen)
765{
Julien Grall5b902d62015-09-03 23:59:50 +0100766 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500767
Julien Grall5b902d62015-09-03 23:59:50 +0100768 res = device_get_mac_addr(dev, "mac-address", addr, alen);
769 if (res)
770 return res;
771
772 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
773 if (res)
774 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500775
776 return device_get_mac_addr(dev, "address", addr, alen);
777}
778EXPORT_SYMBOL(device_get_mac_address);