blob: 0b22c8a5b5db50d2a751ff39dc61df93d5bd8b69 [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
Andy Shevchenko61f5e292015-11-30 17:11:30 +020022static inline bool is_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020023{
24 return fwnode && fwnode->type == FWNODE_PDATA;
25}
26
Andy Shevchenko61f5e292015-11-30 17:11:30 +020027static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020028{
Andy Shevchenko61f5e292015-11-30 17:11:30 +020029 return is_pset_node(fwnode) ?
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020030 container_of(fwnode, struct property_set, fwnode) : NULL;
31}
32
33static struct property_entry *pset_prop_get(struct property_set *pset,
34 const char *name)
35{
36 struct property_entry *prop;
37
38 if (!pset || !pset->properties)
39 return NULL;
40
41 for (prop = pset->properties; prop->name; prop++)
42 if (!strcmp(name, prop->name))
43 return prop;
44
45 return NULL;
46}
47
Andy Shevchenko318a19712015-11-30 17:11:31 +020048static void *pset_prop_find(struct property_set *pset, const char *propname,
49 size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020050{
51 struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +020052 void *pointer;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020053
Andy Shevchenko318a19712015-11-30 17:11:31 +020054 prop = pset_prop_get(pset, propname);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020055 if (!prop)
Andy Shevchenko318a19712015-11-30 17:11:31 +020056 return ERR_PTR(-EINVAL);
Andy Shevchenko66586ba2015-11-30 17:11:32 +020057 if (prop->is_array)
58 pointer = prop->pointer.raw_data;
59 else
60 pointer = &prop->value.raw_data;
Andy Shevchenko318a19712015-11-30 17:11:31 +020061 if (!pointer)
62 return ERR_PTR(-ENODATA);
63 if (length > prop->length)
64 return ERR_PTR(-EOVERFLOW);
65 return pointer;
66}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020067
Andy Shevchenko318a19712015-11-30 17:11:31 +020068static int pset_prop_read_u8_array(struct property_set *pset,
69 const char *propname,
70 u8 *values, size_t nval)
71{
72 void *pointer;
73 size_t length = nval * sizeof(*values);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020074
Andy Shevchenko318a19712015-11-30 17:11:31 +020075 pointer = pset_prop_find(pset, propname, length);
76 if (IS_ERR(pointer))
77 return PTR_ERR(pointer);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020078
Andy Shevchenko318a19712015-11-30 17:11:31 +020079 memcpy(values, pointer, length);
80 return 0;
81}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020082
Andy Shevchenko318a19712015-11-30 17:11:31 +020083static int pset_prop_read_u16_array(struct property_set *pset,
84 const char *propname,
85 u16 *values, size_t nval)
86{
87 void *pointer;
88 size_t length = nval * sizeof(*values);
89
90 pointer = pset_prop_find(pset, propname, length);
91 if (IS_ERR(pointer))
92 return PTR_ERR(pointer);
93
94 memcpy(values, pointer, length);
95 return 0;
96}
97
98static int pset_prop_read_u32_array(struct property_set *pset,
99 const char *propname,
100 u32 *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_u64_array(struct property_set *pset,
114 const char *propname,
115 u64 *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_count_elems_of_size(struct property_set *pset,
129 const char *propname, size_t length)
130{
131 struct property_entry *prop;
132
133 prop = pset_prop_get(pset, propname);
134 if (!prop)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200135 return -EINVAL;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200136
137 return prop->length / length;
138}
139
140static int pset_prop_read_string_array(struct property_set *pset,
141 const char *propname,
142 const char **strings, size_t nval)
143{
144 void *pointer;
145 size_t length = nval * sizeof(*strings);
146
147 pointer = pset_prop_find(pset, propname, length);
148 if (IS_ERR(pointer))
149 return PTR_ERR(pointer);
150
151 memcpy(strings, pointer, length);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200152 return 0;
153}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100154
Andy Shevchenko66586ba2015-11-30 17:11:32 +0200155static int pset_prop_read_string(struct property_set *pset,
156 const char *propname, const char **strings)
157{
158 struct property_entry *prop;
159 const char **pointer;
160
161 prop = pset_prop_get(pset, propname);
162 if (!prop)
163 return -EINVAL;
164 if (!prop->is_string)
165 return -EILSEQ;
166 if (prop->is_array) {
167 pointer = prop->pointer.str;
168 if (!pointer)
169 return -ENODATA;
170 } else {
171 pointer = &prop->value.str;
172 if (*pointer && strnlen(*pointer, prop->length) >= prop->length)
173 return -EILSEQ;
174 }
175
176 *strings = *pointer;
177 return 0;
178}
179
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100180static inline struct fwnode_handle *dev_fwnode(struct device *dev)
181{
182 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
183 &dev->of_node->fwnode : dev->fwnode;
184}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100185
186/**
187 * device_property_present - check if a property of a device is present
188 * @dev: Device whose property is being checked
189 * @propname: Name of the property
190 *
191 * Check if property @propname is present in the device firmware description.
192 */
193bool device_property_present(struct device *dev, const char *propname)
194{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100195 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100196}
197EXPORT_SYMBOL_GPL(device_property_present);
198
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200199static bool __fwnode_property_present(struct fwnode_handle *fwnode,
200 const char *propname)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100201{
202 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200203 return of_property_read_bool(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100204 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200205 return !acpi_node_prop_get(fwnode, propname, NULL);
Andy Shevchenko61f5e292015-11-30 17:11:30 +0200206 else if (is_pset_node(fwnode))
207 return !!pset_prop_get(to_pset_node(fwnode), propname);
Andy Shevchenkoe3f9e292015-11-30 17:11:29 +0200208 return false;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100209}
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200210
211/**
212 * fwnode_property_present - check if a property of a firmware node is present
213 * @fwnode: Firmware node whose property to check
214 * @propname: Name of the property
215 */
216bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
217{
218 bool ret;
219
220 ret = __fwnode_property_present(fwnode, propname);
221 if (ret == false && fwnode->secondary)
222 ret = __fwnode_property_present(fwnode->secondary, propname);
223 return ret;
224}
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100225EXPORT_SYMBOL_GPL(fwnode_property_present);
226
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100227/**
228 * device_property_read_u8_array - return a u8 array property of a device
229 * @dev: Device to get the property of
230 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200231 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100232 * @nval: Size of the @val array
233 *
234 * Function reads an array of u8 properties with @propname from the device
235 * firmware description and stores them to @val if found.
236 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200237 * Return: number of values if @val was %NULL,
238 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100239 * %-EINVAL if given arguments are not valid,
240 * %-ENODATA if the property does not have a value,
241 * %-EPROTO if the property is not an array of numbers,
242 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700243 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100244 */
245int device_property_read_u8_array(struct device *dev, const char *propname,
246 u8 *val, size_t nval)
247{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100248 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100249}
250EXPORT_SYMBOL_GPL(device_property_read_u8_array);
251
252/**
253 * device_property_read_u16_array - return a u16 array property of a device
254 * @dev: Device to get the property of
255 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200256 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100257 * @nval: Size of the @val array
258 *
259 * Function reads an array of u16 properties with @propname from the device
260 * firmware description and stores them to @val if found.
261 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200262 * Return: number of values if @val was %NULL,
263 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100264 * %-EINVAL if given arguments are not valid,
265 * %-ENODATA if the property does not have a value,
266 * %-EPROTO if the property is not an array of numbers,
267 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700268 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100269 */
270int device_property_read_u16_array(struct device *dev, const char *propname,
271 u16 *val, size_t nval)
272{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100273 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100274}
275EXPORT_SYMBOL_GPL(device_property_read_u16_array);
276
277/**
278 * device_property_read_u32_array - return a u32 array property of a device
279 * @dev: Device to get the property of
280 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200281 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100282 * @nval: Size of the @val array
283 *
284 * Function reads an array of u32 properties with @propname from the device
285 * firmware description and stores them to @val if found.
286 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200287 * Return: number of values if @val was %NULL,
288 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100289 * %-EINVAL if given arguments are not valid,
290 * %-ENODATA if the property does not have a value,
291 * %-EPROTO if the property is not an array of numbers,
292 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700293 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100294 */
295int device_property_read_u32_array(struct device *dev, const char *propname,
296 u32 *val, size_t nval)
297{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100298 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100299}
300EXPORT_SYMBOL_GPL(device_property_read_u32_array);
301
302/**
303 * device_property_read_u64_array - return a u64 array property of a device
304 * @dev: Device 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. Wysockib31384f2014-11-04 01:28:56 +0100307 * @nval: Size of the @val array
308 *
309 * Function reads an array of u64 properties with @propname from the device
310 * firmware description and stores them to @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. Wysockib31384f2014-11-04 01:28:56 +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.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700318 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100319 */
320int device_property_read_u64_array(struct device *dev, const char *propname,
321 u64 *val, size_t nval)
322{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100323 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100324}
325EXPORT_SYMBOL_GPL(device_property_read_u64_array);
326
327/**
328 * device_property_read_string_array - return a string array property of device
329 * @dev: Device to get the property of
330 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200331 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100332 * @nval: Size of the @val array
333 *
334 * Function reads an array of string properties with @propname from the device
335 * firmware description and stores them to @val if found.
336 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200337 * Return: number of values if @val was %NULL,
338 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100339 * %-EINVAL if given arguments are not valid,
340 * %-ENODATA if the property does not have a value,
341 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
342 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700343 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100344 */
345int device_property_read_string_array(struct device *dev, const char *propname,
346 const char **val, size_t nval)
347{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100348 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100349}
350EXPORT_SYMBOL_GPL(device_property_read_string_array);
351
352/**
353 * device_property_read_string - return a string property of a device
354 * @dev: Device to get the property of
355 * @propname: Name of the property
356 * @val: The value is stored here
357 *
358 * Function reads property @propname from the device firmware description and
359 * stores the value into @val if found. The value is checked to be a string.
360 *
361 * Return: %0 if the property was found (success),
362 * %-EINVAL if given arguments are not valid,
363 * %-ENODATA if the property does not have a value,
364 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700365 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100366 */
367int device_property_read_string(struct device *dev, const char *propname,
368 const char **val)
369{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100370 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100371}
372EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100373
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300374/**
375 * device_property_match_string - find a string in an array and return index
376 * @dev: Device to get the property of
377 * @propname: Name of the property holding the array
378 * @string: String to look for
379 *
380 * Find a given string in a string array and if it is found return the
381 * index back.
382 *
383 * Return: %0 if the property was found (success),
384 * %-EINVAL if given arguments are not valid,
385 * %-ENODATA if the property does not have a value,
386 * %-EPROTO if the property is not an array of strings,
387 * %-ENXIO if no suitable firmware interface is present.
388 */
389int device_property_match_string(struct device *dev, const char *propname,
390 const char *string)
391{
392 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
393}
394EXPORT_SYMBOL_GPL(device_property_match_string);
395
Andy Shevchenko1d656fb2015-11-30 17:11:34 +0200396#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
397 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100398 : of_property_count_elems_of_size((node), (propname), sizeof(type))
399
Andy Shevchenko318a19712015-11-30 17:11:31 +0200400#define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
401 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
402 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
403
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200404#define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
Andy Shevchenko1d656fb2015-11-30 17:11:34 +0200405({ \
406 int _ret_; \
407 if (is_of_node(_fwnode_)) \
408 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
409 _type_, _val_, _nval_); \
410 else if (is_acpi_node(_fwnode_)) \
411 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
412 _val_, _nval_); \
Andy Shevchenko61f5e292015-11-30 17:11:30 +0200413 else if (is_pset_node(_fwnode_)) \
Andy Shevchenko318a19712015-11-30 17:11:31 +0200414 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
415 _type_, _val_, _nval_); \
Andy Shevchenko1d656fb2015-11-30 17:11:34 +0200416 else \
417 _ret_ = -ENXIO; \
418 _ret_; \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100419})
420
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200421#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
422({ \
423 int _ret_; \
424 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
425 _val_, _nval_); \
426 if (_ret_ == -EINVAL && _fwnode_->secondary) \
427 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \
428 _proptype_, _val_, _nval_); \
429 _ret_; \
430})
431
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100432/**
433 * fwnode_property_read_u8_array - return a u8 array property of firmware node
434 * @fwnode: Firmware node to get the property of
435 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200436 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100437 * @nval: Size of the @val array
438 *
439 * Read an array of u8 properties with @propname from @fwnode and stores them to
440 * @val if found.
441 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200442 * Return: number of values if @val was %NULL,
443 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100444 * %-EINVAL if given arguments are not valid,
445 * %-ENODATA if the property does not have a value,
446 * %-EPROTO if the property is not an array of numbers,
447 * %-EOVERFLOW if the size of the property is not as expected,
448 * %-ENXIO if no suitable firmware interface is present.
449 */
450int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
451 const char *propname, u8 *val, size_t nval)
452{
453 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
454 val, nval);
455}
456EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
457
458/**
459 * fwnode_property_read_u16_array - return a u16 array property of firmware node
460 * @fwnode: Firmware node to get the property of
461 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200462 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100463 * @nval: Size of the @val array
464 *
465 * Read an array of u16 properties with @propname from @fwnode and store them to
466 * @val if found.
467 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200468 * Return: number of values if @val was %NULL,
469 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100470 * %-EINVAL if given arguments are not valid,
471 * %-ENODATA if the property does not have a value,
472 * %-EPROTO if the property is not an array of numbers,
473 * %-EOVERFLOW if the size of the property is not as expected,
474 * %-ENXIO if no suitable firmware interface is present.
475 */
476int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
477 const char *propname, u16 *val, size_t nval)
478{
479 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
480 val, nval);
481}
482EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
483
484/**
485 * fwnode_property_read_u32_array - return a u32 array property of firmware node
486 * @fwnode: Firmware node to get the property of
487 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200488 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100489 * @nval: Size of the @val array
490 *
491 * Read an array of u32 properties with @propname from @fwnode store them to
492 * @val if found.
493 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200494 * Return: number of values if @val was %NULL,
495 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100496 * %-EINVAL if given arguments are not valid,
497 * %-ENODATA if the property does not have a value,
498 * %-EPROTO if the property is not an array of numbers,
499 * %-EOVERFLOW if the size of the property is not as expected,
500 * %-ENXIO if no suitable firmware interface is present.
501 */
502int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
503 const char *propname, u32 *val, size_t nval)
504{
505 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
506 val, nval);
507}
508EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
509
510/**
511 * fwnode_property_read_u64_array - return a u64 array property firmware node
512 * @fwnode: Firmware node to get the property of
513 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200514 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100515 * @nval: Size of the @val array
516 *
517 * Read an array of u64 properties with @propname from @fwnode and store them to
518 * @val if found.
519 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200520 * Return: number of values if @val was %NULL,
521 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100522 * %-EINVAL if given arguments are not valid,
523 * %-ENODATA if the property does not have a value,
524 * %-EPROTO if the property is not an array of numbers,
525 * %-EOVERFLOW if the size of the property is not as expected,
526 * %-ENXIO if no suitable firmware interface is present.
527 */
528int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
529 const char *propname, u64 *val, size_t nval)
530{
531 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
532 val, nval);
533}
534EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
535
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200536static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
537 const char *propname,
538 const char **val, size_t nval)
539{
540 if (is_of_node(fwnode))
541 return val ?
542 of_property_read_string_array(to_of_node(fwnode),
543 propname, val, nval) :
544 of_property_count_strings(to_of_node(fwnode), propname);
545 else if (is_acpi_node(fwnode))
546 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
547 val, nval);
548 else if (is_pset_node(fwnode))
549 return val ?
550 pset_prop_read_string_array(to_pset_node(fwnode),
551 propname, val, nval) :
552 pset_prop_count_elems_of_size(to_pset_node(fwnode),
553 propname,
554 sizeof(const char *));
555 return -ENXIO;
556}
557
558static int __fwnode_property_read_string(struct fwnode_handle *fwnode,
559 const char *propname, const char **val)
560{
561 if (is_of_node(fwnode))
562 return of_property_read_string(to_of_node(fwnode), propname, val);
563 else if (is_acpi_node(fwnode))
564 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
565 val, 1);
566 else if (is_pset_node(fwnode))
567 return pset_prop_read_string(to_pset_node(fwnode), propname, val);
568 return -ENXIO;
569}
570
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100571/**
572 * fwnode_property_read_string_array - return string array property of a node
573 * @fwnode: Firmware node to get the property of
574 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200575 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100576 * @nval: Size of the @val array
577 *
578 * Read an string list property @propname from the given firmware node and store
579 * them to @val if found.
580 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200581 * Return: number of values if @val was %NULL,
582 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100583 * %-EINVAL if given arguments are not valid,
584 * %-ENODATA if the property does not have a value,
585 * %-EPROTO if the property is not an array of strings,
586 * %-EOVERFLOW if the size of the property is not as expected,
587 * %-ENXIO if no suitable firmware interface is present.
588 */
589int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
590 const char *propname, const char **val,
591 size_t nval)
592{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200593 int ret;
594
595 ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
596 if (ret == -EINVAL && fwnode->secondary)
597 ret = __fwnode_property_read_string_array(fwnode->secondary,
598 propname, val, nval);
599 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100600}
601EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
602
603/**
604 * fwnode_property_read_string - return a string property of a firmware node
605 * @fwnode: Firmware node to get the property of
606 * @propname: Name of the property
607 * @val: The value is stored here
608 *
609 * Read property @propname from the given firmware node and store the value into
610 * @val if found. The value is checked to be a string.
611 *
612 * Return: %0 if the property was found (success),
613 * %-EINVAL if given arguments are not valid,
614 * %-ENODATA if the property does not have a value,
615 * %-EPROTO or %-EILSEQ if the property is not a string,
616 * %-ENXIO if no suitable firmware interface is present.
617 */
618int fwnode_property_read_string(struct fwnode_handle *fwnode,
619 const char *propname, const char **val)
620{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200621 int ret;
622
623 ret = __fwnode_property_read_string(fwnode, propname, val);
624 if (ret == -EINVAL && fwnode->secondary)
625 ret = __fwnode_property_read_string(fwnode->secondary,
626 propname, val);
627 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100628}
629EXPORT_SYMBOL_GPL(fwnode_property_read_string);
630
631/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300632 * fwnode_property_match_string - find a string in an array and return index
633 * @fwnode: Firmware node to get the property of
634 * @propname: Name of the property holding the array
635 * @string: String to look for
636 *
637 * Find a given string in a string array and if it is found return the
638 * index back.
639 *
640 * Return: %0 if the property was found (success),
641 * %-EINVAL if given arguments are not valid,
642 * %-ENODATA if the property does not have a value,
643 * %-EPROTO if the property is not an array of strings,
644 * %-ENXIO if no suitable firmware interface is present.
645 */
646int fwnode_property_match_string(struct fwnode_handle *fwnode,
647 const char *propname, const char *string)
648{
649 const char **values;
650 int nval, ret, i;
651
652 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
653 if (nval < 0)
654 return nval;
655
656 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
657 if (!values)
658 return -ENOMEM;
659
660 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
661 if (ret < 0)
662 goto out;
663
664 ret = -ENODATA;
665 for (i = 0; i < nval; i++) {
666 if (!strcmp(values[i], string)) {
667 ret = i;
668 break;
669 }
670 }
671out:
672 kfree(values);
673 return ret;
674}
675EXPORT_SYMBOL_GPL(fwnode_property_match_string);
676
677/**
Mika Westerberg13141e12015-11-30 17:11:37 +0200678 * pset_free_set - releases memory allocated for copied property set
679 * @pset: Property set to release
680 *
681 * Function takes previously copied property set and releases all the
682 * memory allocated to it.
683 */
684static void pset_free_set(struct property_set *pset)
685{
686 const struct property_entry *prop;
687 size_t i, nval;
688
689 if (!pset)
690 return;
691
692 for (prop = pset->properties; prop->name; prop++) {
693 if (prop->is_array) {
694 if (prop->is_string && prop->pointer.str) {
695 nval = prop->length / sizeof(const char *);
696 for (i = 0; i < nval; i++)
697 kfree(prop->pointer.str[i]);
698 }
699 kfree(prop->pointer.raw_data);
700 } else if (prop->is_string) {
701 kfree(prop->value.str);
702 }
703 kfree(prop->name);
704 }
705
706 kfree(pset->properties);
707 kfree(pset);
708}
709
710static int pset_copy_entry(struct property_entry *dst,
711 const struct property_entry *src)
712{
713 const char **d, **s;
714 size_t i, nval;
715
716 dst->name = kstrdup(src->name, GFP_KERNEL);
717 if (!dst->name)
718 return -ENOMEM;
719
720 if (src->is_array) {
721 if (src->is_string) {
722 nval = src->length / sizeof(const char *);
723 dst->pointer.str = kcalloc(nval, sizeof(const char *),
724 GFP_KERNEL);
725 if (!dst->pointer.str)
726 return -ENOMEM;
727
728 d = dst->pointer.str;
729 s = src->pointer.str;
730 for (i = 0; i < nval; i++) {
731 d[i] = kstrdup(s[i], GFP_KERNEL);
732 if (!d[i] && s[i])
733 return -ENOMEM;
734 }
735 } else {
736 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
737 src->length, GFP_KERNEL);
738 if (!dst->pointer.raw_data)
739 return -ENOMEM;
740 }
741 } else if (src->is_string) {
742 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
743 if (!dst->value.str && src->value.str)
744 return -ENOMEM;
745 } else {
746 dst->value.raw_data = src->value.raw_data;
747 }
748
749 dst->length = src->length;
750 dst->is_array = src->is_array;
751 dst->is_string = src->is_string;
752
753 return 0;
754}
755
756/**
757 * pset_copy_set - copies property set
758 * @pset: Property set to copy
759 *
760 * This function takes a deep copy of the given property set and returns
761 * pointer to the copy. Call device_free_property_set() to free resources
762 * allocated in this function.
763 *
764 * Return: Pointer to the new property set or error pointer.
765 */
766static struct property_set *pset_copy_set(const struct property_set *pset)
767{
768 const struct property_entry *entry;
769 struct property_set *p;
770 size_t i, n = 0;
771
772 p = kzalloc(sizeof(*p), GFP_KERNEL);
773 if (!p)
774 return ERR_PTR(-ENOMEM);
775
776 while (pset->properties[n].name)
777 n++;
778
779 p->properties = kcalloc(n + 1, sizeof(*entry), GFP_KERNEL);
780 if (!p->properties) {
781 kfree(p);
782 return ERR_PTR(-ENOMEM);
783 }
784
785 for (i = 0; i < n; i++) {
786 int ret = pset_copy_entry(&p->properties[i],
787 &pset->properties[i]);
788 if (ret) {
789 pset_free_set(p);
790 return ERR_PTR(ret);
791 }
792 }
793
794 return p;
795}
796
797/**
798 * device_remove_property_set - Remove properties from a device object.
799 * @dev: Device whose properties to remove.
800 *
801 * The function removes properties previously associated to the device
802 * secondary firmware node with device_add_property_set(). Memory allocated
803 * to the properties will also be released.
804 */
805void device_remove_property_set(struct device *dev)
806{
807 struct fwnode_handle *fwnode;
808
809 fwnode = dev_fwnode(dev);
810 if (!fwnode)
811 return;
812 /*
813 * Pick either primary or secondary node depending which one holds
814 * the pset. If there is no real firmware node (ACPI/DT) primary
815 * will hold the pset.
816 */
817 if (!is_pset_node(fwnode))
818 fwnode = fwnode->secondary;
819 if (!IS_ERR(fwnode) && is_pset_node(fwnode))
820 pset_free_set(to_pset_node(fwnode));
821 set_secondary_fwnode(dev, NULL);
822}
823EXPORT_SYMBOL_GPL(device_remove_property_set);
824
825/**
826 * device_add_property_set - Add a collection of properties to a device object.
827 * @dev: Device to add properties to.
828 * @pset: Collection of properties to add.
829 *
830 * Associate a collection of device properties represented by @pset with @dev
831 * as its secondary firmware node. The function takes a copy of @pset.
832 */
833int device_add_property_set(struct device *dev, const struct property_set *pset)
834{
835 struct property_set *p;
836
837 if (!pset)
838 return -EINVAL;
839
840 p = pset_copy_set(pset);
841 if (IS_ERR(p))
842 return PTR_ERR(p);
843
844 p->fwnode.type = FWNODE_PDATA;
845 set_secondary_fwnode(dev, &p->fwnode);
846 return 0;
847}
848EXPORT_SYMBOL_GPL(device_add_property_set);
849
850/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100851 * device_get_next_child_node - Return the next child node handle for a device
852 * @dev: Device to find the next child node for.
853 * @child: Handle to one of the device's child nodes or a null handle.
854 */
855struct fwnode_handle *device_get_next_child_node(struct device *dev,
856 struct fwnode_handle *child)
857{
858 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
859 struct device_node *node;
860
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200861 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100862 if (node)
863 return &node->fwnode;
864 } else if (IS_ENABLED(CONFIG_ACPI)) {
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200865 return acpi_get_next_subnode(dev, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100866 }
867 return NULL;
868}
869EXPORT_SYMBOL_GPL(device_get_next_child_node);
870
871/**
872 * fwnode_handle_put - Drop reference to a device node
873 * @fwnode: Pointer to the device node to drop the reference to.
874 *
875 * This has to be used when terminating device_for_each_child_node() iteration
876 * with break or return to prevent stale device node references from being left
877 * behind.
878 */
879void fwnode_handle_put(struct fwnode_handle *fwnode)
880{
881 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200882 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100883}
884EXPORT_SYMBOL_GPL(fwnode_handle_put);
885
886/**
887 * device_get_child_node_count - return the number of child nodes for device
888 * @dev: Device to cound the child nodes for
889 */
890unsigned int device_get_child_node_count(struct device *dev)
891{
892 struct fwnode_handle *child;
893 unsigned int count = 0;
894
895 device_for_each_child_node(dev, child)
896 count++;
897
898 return count;
899}
900EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -0500901
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -0700902bool device_dma_supported(struct device *dev)
903{
904 /* For DT, this is always supported.
905 * For ACPI, this depends on CCA, which
906 * is determined by the acpi_dma_supported().
907 */
908 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
909 return true;
910
911 return acpi_dma_supported(ACPI_COMPANION(dev));
912}
913EXPORT_SYMBOL_GPL(device_dma_supported);
914
915enum dev_dma_attr device_get_dma_attr(struct device *dev)
916{
917 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
918
919 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
920 if (of_dma_is_coherent(dev->of_node))
921 attr = DEV_DMA_COHERENT;
922 else
923 attr = DEV_DMA_NON_COHERENT;
924 } else
925 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
926
927 return attr;
928}
929EXPORT_SYMBOL_GPL(device_get_dma_attr);
930
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500931/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500932 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500933 * @dev: Pointer to the given device
934 *
935 * The function gets phy interface string from property 'phy-mode' or
936 * 'phy-connection-type', and return its index in phy_modes table, or errno in
937 * error case.
938 */
939int device_get_phy_mode(struct device *dev)
940{
941 const char *pm;
942 int err, i;
943
944 err = device_property_read_string(dev, "phy-mode", &pm);
945 if (err < 0)
946 err = device_property_read_string(dev,
947 "phy-connection-type", &pm);
948 if (err < 0)
949 return err;
950
951 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
952 if (!strcasecmp(pm, phy_modes(i)))
953 return i;
954
955 return -ENODEV;
956}
957EXPORT_SYMBOL_GPL(device_get_phy_mode);
958
959static void *device_get_mac_addr(struct device *dev,
960 const char *name, char *addr,
961 int alen)
962{
963 int ret = device_property_read_u8_array(dev, name, addr, alen);
964
Jeremy Linton2f710a32015-08-19 11:46:42 -0500965 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500966 return addr;
967 return NULL;
968}
969
970/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500971 * device_get_mac_address - Get the MAC for a given device
972 * @dev: Pointer to the device
973 * @addr: Address of buffer to store the MAC in
974 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
975 *
976 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500977 * checked first, because that is supposed to contain to "most recent" MAC
978 * address. If that isn't set, then 'local-mac-address' is checked next,
979 * because that is the default address. If that isn't set, then the obsolete
980 * 'address' is checked, just in case we're using an old device tree.
981 *
982 * Note that the 'address' property is supposed to contain a virtual address of
983 * the register set, but some DTS files have redefined that property to be the
984 * MAC address.
985 *
986 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -0500987 * exist in the firmware tables, but were not updated by the firmware. For
988 * example, the DTS could define 'mac-address' and 'local-mac-address', with
989 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
990 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
991 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500992*/
993void *device_get_mac_address(struct device *dev, char *addr, int alen)
994{
Julien Grall5b902d62015-09-03 23:59:50 +0100995 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500996
Julien Grall5b902d62015-09-03 23:59:50 +0100997 res = device_get_mac_addr(dev, "mac-address", addr, alen);
998 if (res)
999 return res;
1000
1001 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1002 if (res)
1003 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001004
1005 return device_get_mac_addr(dev, "address", addr, alen);
1006}
1007EXPORT_SYMBOL(device_get_mac_address);