blob: 7b313b567f4c3b71c81eb48ee740eac79d636511 [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
Heikki Krogerusf4d05262016-03-29 14:52:23 +030022struct property_set {
Jarkko Nikula2a077f72017-10-09 16:28:37 +030023 struct device *dev;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030024 struct fwnode_handle fwnode;
25 struct property_entry *properties;
26};
27
Andy Shevchenko61f5e292015-11-30 17:11:30 +020028static inline bool is_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020029{
Heikki Krogerus0224a4a2016-04-27 14:04:20 +030030 return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020031}
32
Andy Shevchenko61f5e292015-11-30 17:11:30 +020033static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020034{
Andy Shevchenko61f5e292015-11-30 17:11:30 +020035 return is_pset_node(fwnode) ?
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020036 container_of(fwnode, struct property_set, fwnode) : NULL;
37}
38
39static struct property_entry *pset_prop_get(struct property_set *pset,
40 const char *name)
41{
42 struct property_entry *prop;
43
44 if (!pset || !pset->properties)
45 return NULL;
46
47 for (prop = pset->properties; prop->name; prop++)
48 if (!strcmp(name, prop->name))
49 return prop;
50
51 return NULL;
52}
53
Andy Shevchenko318a19712015-11-30 17:11:31 +020054static void *pset_prop_find(struct property_set *pset, const char *propname,
55 size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020056{
57 struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +020058 void *pointer;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020059
Andy Shevchenko318a19712015-11-30 17:11:31 +020060 prop = pset_prop_get(pset, propname);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020061 if (!prop)
Andy Shevchenko318a19712015-11-30 17:11:31 +020062 return ERR_PTR(-EINVAL);
Andy Shevchenko66586ba2015-11-30 17:11:32 +020063 if (prop->is_array)
64 pointer = prop->pointer.raw_data;
65 else
66 pointer = &prop->value.raw_data;
Andy Shevchenko318a19712015-11-30 17:11:31 +020067 if (!pointer)
68 return ERR_PTR(-ENODATA);
69 if (length > prop->length)
70 return ERR_PTR(-EOVERFLOW);
71 return pointer;
72}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020073
Andy Shevchenko318a19712015-11-30 17:11:31 +020074static int pset_prop_read_u8_array(struct property_set *pset,
75 const char *propname,
76 u8 *values, size_t nval)
77{
78 void *pointer;
79 size_t length = nval * sizeof(*values);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020080
Andy Shevchenko318a19712015-11-30 17:11:31 +020081 pointer = pset_prop_find(pset, propname, length);
82 if (IS_ERR(pointer))
83 return PTR_ERR(pointer);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020084
Andy Shevchenko318a19712015-11-30 17:11:31 +020085 memcpy(values, pointer, length);
86 return 0;
87}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020088
Andy Shevchenko318a19712015-11-30 17:11:31 +020089static int pset_prop_read_u16_array(struct property_set *pset,
90 const char *propname,
91 u16 *values, size_t nval)
92{
93 void *pointer;
94 size_t length = nval * sizeof(*values);
95
96 pointer = pset_prop_find(pset, propname, length);
97 if (IS_ERR(pointer))
98 return PTR_ERR(pointer);
99
100 memcpy(values, pointer, length);
101 return 0;
102}
103
104static int pset_prop_read_u32_array(struct property_set *pset,
105 const char *propname,
106 u32 *values, size_t nval)
107{
108 void *pointer;
109 size_t length = nval * sizeof(*values);
110
111 pointer = pset_prop_find(pset, propname, length);
112 if (IS_ERR(pointer))
113 return PTR_ERR(pointer);
114
115 memcpy(values, pointer, length);
116 return 0;
117}
118
119static int pset_prop_read_u64_array(struct property_set *pset,
120 const char *propname,
121 u64 *values, size_t nval)
122{
123 void *pointer;
124 size_t length = nval * sizeof(*values);
125
126 pointer = pset_prop_find(pset, propname, length);
127 if (IS_ERR(pointer))
128 return PTR_ERR(pointer);
129
130 memcpy(values, pointer, length);
131 return 0;
132}
133
134static int pset_prop_count_elems_of_size(struct property_set *pset,
135 const char *propname, size_t length)
136{
137 struct property_entry *prop;
138
139 prop = pset_prop_get(pset, propname);
140 if (!prop)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200141 return -EINVAL;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200142
143 return prop->length / length;
144}
145
146static int pset_prop_read_string_array(struct property_set *pset,
147 const char *propname,
148 const char **strings, size_t nval)
149{
150 void *pointer;
151 size_t length = nval * sizeof(*strings);
152
153 pointer = pset_prop_find(pset, propname, length);
154 if (IS_ERR(pointer))
155 return PTR_ERR(pointer);
156
157 memcpy(strings, pointer, length);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200158 return 0;
159}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100160
Andy Shevchenko66586ba2015-11-30 17:11:32 +0200161static int pset_prop_read_string(struct property_set *pset,
162 const char *propname, const char **strings)
163{
164 struct property_entry *prop;
165 const char **pointer;
166
167 prop = pset_prop_get(pset, propname);
168 if (!prop)
169 return -EINVAL;
170 if (!prop->is_string)
171 return -EILSEQ;
172 if (prop->is_array) {
173 pointer = prop->pointer.str;
174 if (!pointer)
175 return -ENODATA;
176 } else {
177 pointer = &prop->value.str;
178 if (*pointer && strnlen(*pointer, prop->length) >= prop->length)
179 return -EILSEQ;
180 }
181
182 *strings = *pointer;
183 return 0;
184}
185
Sakari Ailus1f32e672017-03-28 10:52:24 +0300186struct fwnode_handle *dev_fwnode(struct device *dev)
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100187{
188 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
189 &dev->of_node->fwnode : dev->fwnode;
190}
Sakari Ailus1f32e672017-03-28 10:52:24 +0300191EXPORT_SYMBOL_GPL(dev_fwnode);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100192
193/**
194 * device_property_present - check if a property of a device is present
195 * @dev: Device whose property is being checked
196 * @propname: Name of the property
197 *
198 * Check if property @propname is present in the device firmware description.
199 */
200bool device_property_present(struct device *dev, const char *propname)
201{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100202 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100203}
204EXPORT_SYMBOL_GPL(device_property_present);
205
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200206static bool __fwnode_property_present(struct fwnode_handle *fwnode,
207 const char *propname)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100208{
209 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200210 return of_property_read_bool(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100211 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200212 return !acpi_node_prop_get(fwnode, propname, NULL);
Andy Shevchenko61f5e292015-11-30 17:11:30 +0200213 else if (is_pset_node(fwnode))
214 return !!pset_prop_get(to_pset_node(fwnode), propname);
Andy Shevchenkoe3f9e292015-11-30 17:11:29 +0200215 return false;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100216}
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200217
218/**
219 * fwnode_property_present - check if a property of a firmware node is present
220 * @fwnode: Firmware node whose property to check
221 * @propname: Name of the property
222 */
223bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
224{
225 bool ret;
226
227 ret = __fwnode_property_present(fwnode, propname);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200228 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
229 !IS_ERR_OR_NULL(fwnode->secondary))
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200230 ret = __fwnode_property_present(fwnode->secondary, propname);
231 return ret;
232}
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100233EXPORT_SYMBOL_GPL(fwnode_property_present);
234
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100235/**
236 * device_property_read_u8_array - return a u8 array property of a device
237 * @dev: Device to get the property of
238 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200239 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100240 * @nval: Size of the @val array
241 *
242 * Function reads an array of u8 properties with @propname from the device
243 * firmware description and stores them to @val if found.
244 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200245 * Return: number of values if @val was %NULL,
246 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100247 * %-EINVAL if given arguments are not valid,
248 * %-ENODATA if the property does not have a value,
249 * %-EPROTO if the property is not an array of numbers,
250 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700251 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100252 */
253int device_property_read_u8_array(struct device *dev, const char *propname,
254 u8 *val, size_t nval)
255{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100256 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100257}
258EXPORT_SYMBOL_GPL(device_property_read_u8_array);
259
260/**
261 * device_property_read_u16_array - return a u16 array property of a device
262 * @dev: Device to get the property of
263 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200264 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100265 * @nval: Size of the @val array
266 *
267 * Function reads an array of u16 properties with @propname from the device
268 * firmware description and stores them to @val if found.
269 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200270 * Return: number of values if @val was %NULL,
271 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100272 * %-EINVAL if given arguments are not valid,
273 * %-ENODATA if the property does not have a value,
274 * %-EPROTO if the property is not an array of numbers,
275 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700276 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100277 */
278int device_property_read_u16_array(struct device *dev, const char *propname,
279 u16 *val, size_t nval)
280{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100281 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100282}
283EXPORT_SYMBOL_GPL(device_property_read_u16_array);
284
285/**
286 * device_property_read_u32_array - return a u32 array property of a device
287 * @dev: Device to get the property of
288 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200289 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100290 * @nval: Size of the @val array
291 *
292 * Function reads an array of u32 properties with @propname from the device
293 * firmware description and stores them to @val if found.
294 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200295 * Return: number of values if @val was %NULL,
296 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100297 * %-EINVAL if given arguments are not valid,
298 * %-ENODATA if the property does not have a value,
299 * %-EPROTO if the property is not an array of numbers,
300 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700301 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100302 */
303int device_property_read_u32_array(struct device *dev, const char *propname,
304 u32 *val, size_t nval)
305{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100306 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100307}
308EXPORT_SYMBOL_GPL(device_property_read_u32_array);
309
310/**
311 * device_property_read_u64_array - return a u64 array property of a device
312 * @dev: Device 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. Wysockib31384f2014-11-04 01:28:56 +0100315 * @nval: Size of the @val array
316 *
317 * Function reads an array of u64 properties with @propname from the device
318 * firmware description and stores them to @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. Wysockib31384f2014-11-04 01:28:56 +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.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700326 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100327 */
328int device_property_read_u64_array(struct device *dev, const char *propname,
329 u64 *val, size_t nval)
330{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100331 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100332}
333EXPORT_SYMBOL_GPL(device_property_read_u64_array);
334
335/**
336 * device_property_read_string_array - return a string array property of device
337 * @dev: Device to get the property of
338 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200339 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100340 * @nval: Size of the @val array
341 *
342 * Function reads an array of string properties with @propname from the device
343 * firmware description and stores them to @val if found.
344 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200345 * Return: number of values if @val was %NULL,
346 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100347 * %-EINVAL if given arguments are not valid,
348 * %-ENODATA if the property does not have a value,
349 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
350 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700351 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100352 */
353int device_property_read_string_array(struct device *dev, const char *propname,
354 const char **val, size_t nval)
355{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100356 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100357}
358EXPORT_SYMBOL_GPL(device_property_read_string_array);
359
360/**
361 * device_property_read_string - return a string property of a device
362 * @dev: Device to get the property of
363 * @propname: Name of the property
364 * @val: The value is stored here
365 *
366 * Function reads property @propname from the device firmware description and
367 * stores the value into @val if found. The value is checked to be a string.
368 *
369 * Return: %0 if the property was found (success),
370 * %-EINVAL if given arguments are not valid,
371 * %-ENODATA if the property does not have a value,
372 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700373 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100374 */
375int device_property_read_string(struct device *dev, const char *propname,
376 const char **val)
377{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100378 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100379}
380EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100381
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300382/**
383 * device_property_match_string - find a string in an array and return index
384 * @dev: Device to get the property of
385 * @propname: Name of the property holding the array
386 * @string: String to look for
387 *
388 * Find a given string in a string array and if it is found return the
389 * index back.
390 *
391 * Return: %0 if the property was found (success),
392 * %-EINVAL if given arguments are not valid,
393 * %-ENODATA if the property does not have a value,
394 * %-EPROTO if the property is not an array of strings,
395 * %-ENXIO if no suitable firmware interface is present.
396 */
397int device_property_match_string(struct device *dev, const char *propname,
398 const char *string)
399{
400 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
401}
402EXPORT_SYMBOL_GPL(device_property_match_string);
403
Andy Shevchenko1d656fb2015-11-30 17:11:34 +0200404#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
405 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100406 : of_property_count_elems_of_size((node), (propname), sizeof(type))
407
Andy Shevchenko318a19712015-11-30 17:11:31 +0200408#define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \
409 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \
410 : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
411
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200412#define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
Andy Shevchenko1d656fb2015-11-30 17:11:34 +0200413({ \
414 int _ret_; \
415 if (is_of_node(_fwnode_)) \
416 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
417 _type_, _val_, _nval_); \
418 else if (is_acpi_node(_fwnode_)) \
419 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
420 _val_, _nval_); \
Andy Shevchenko61f5e292015-11-30 17:11:30 +0200421 else if (is_pset_node(_fwnode_)) \
Andy Shevchenko318a19712015-11-30 17:11:31 +0200422 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \
423 _type_, _val_, _nval_); \
Andy Shevchenko1d656fb2015-11-30 17:11:34 +0200424 else \
425 _ret_ = -ENXIO; \
426 _ret_; \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100427})
428
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200429#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
430({ \
431 int _ret_; \
432 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \
433 _val_, _nval_); \
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200434 if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) && \
435 !IS_ERR_OR_NULL(_fwnode_->secondary)) \
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200436 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \
437 _proptype_, _val_, _nval_); \
438 _ret_; \
439})
440
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100441/**
442 * fwnode_property_read_u8_array - return a u8 array property of firmware node
443 * @fwnode: Firmware node to get the property of
444 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200445 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100446 * @nval: Size of the @val array
447 *
448 * Read an array of u8 properties with @propname from @fwnode and stores them to
449 * @val if found.
450 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200451 * Return: number of values if @val was %NULL,
452 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100453 * %-EINVAL if given arguments are not valid,
454 * %-ENODATA if the property does not have a value,
455 * %-EPROTO if the property is not an array of numbers,
456 * %-EOVERFLOW if the size of the property is not as expected,
457 * %-ENXIO if no suitable firmware interface is present.
458 */
459int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
460 const char *propname, u8 *val, size_t nval)
461{
462 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
463 val, nval);
464}
465EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
466
467/**
468 * fwnode_property_read_u16_array - return a u16 array property of firmware node
469 * @fwnode: Firmware node to get the property of
470 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200471 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100472 * @nval: Size of the @val array
473 *
474 * Read an array of u16 properties with @propname from @fwnode and store them to
475 * @val if found.
476 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200477 * Return: number of values if @val was %NULL,
478 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100479 * %-EINVAL if given arguments are not valid,
480 * %-ENODATA if the property does not have a value,
481 * %-EPROTO if the property is not an array of numbers,
482 * %-EOVERFLOW if the size of the property is not as expected,
483 * %-ENXIO if no suitable firmware interface is present.
484 */
485int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
486 const char *propname, u16 *val, size_t nval)
487{
488 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
489 val, nval);
490}
491EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
492
493/**
494 * fwnode_property_read_u32_array - return a u32 array property of firmware node
495 * @fwnode: Firmware node to get the property of
496 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200497 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100498 * @nval: Size of the @val array
499 *
500 * Read an array of u32 properties with @propname from @fwnode store them to
501 * @val if found.
502 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200503 * Return: number of values if @val was %NULL,
504 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100505 * %-EINVAL if given arguments are not valid,
506 * %-ENODATA if the property does not have a value,
507 * %-EPROTO if the property is not an array of numbers,
508 * %-EOVERFLOW if the size of the property is not as expected,
509 * %-ENXIO if no suitable firmware interface is present.
510 */
511int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
512 const char *propname, u32 *val, size_t nval)
513{
514 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
515 val, nval);
516}
517EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
518
519/**
520 * fwnode_property_read_u64_array - return a u64 array property firmware node
521 * @fwnode: Firmware node to get the property of
522 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200523 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100524 * @nval: Size of the @val array
525 *
526 * Read an array of u64 properties with @propname from @fwnode and store them to
527 * @val if found.
528 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200529 * Return: number of values if @val was %NULL,
530 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100531 * %-EINVAL if given arguments are not valid,
532 * %-ENODATA if the property does not have a value,
533 * %-EPROTO if the property is not an array of numbers,
534 * %-EOVERFLOW if the size of the property is not as expected,
535 * %-ENXIO if no suitable firmware interface is present.
536 */
537int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
538 const char *propname, u64 *val, size_t nval)
539{
540 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
541 val, nval);
542}
543EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
544
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200545static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
546 const char *propname,
547 const char **val, size_t nval)
548{
549 if (is_of_node(fwnode))
550 return val ?
551 of_property_read_string_array(to_of_node(fwnode),
552 propname, val, nval) :
553 of_property_count_strings(to_of_node(fwnode), propname);
554 else if (is_acpi_node(fwnode))
555 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
556 val, nval);
557 else if (is_pset_node(fwnode))
558 return val ?
559 pset_prop_read_string_array(to_pset_node(fwnode),
560 propname, val, nval) :
561 pset_prop_count_elems_of_size(to_pset_node(fwnode),
562 propname,
563 sizeof(const char *));
564 return -ENXIO;
565}
566
567static int __fwnode_property_read_string(struct fwnode_handle *fwnode,
568 const char *propname, const char **val)
569{
570 if (is_of_node(fwnode))
571 return of_property_read_string(to_of_node(fwnode), propname, val);
572 else if (is_acpi_node(fwnode))
573 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
574 val, 1);
575 else if (is_pset_node(fwnode))
576 return pset_prop_read_string(to_pset_node(fwnode), propname, val);
577 return -ENXIO;
578}
579
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100580/**
581 * fwnode_property_read_string_array - return string array property of a node
582 * @fwnode: Firmware node to get the property of
583 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200584 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100585 * @nval: Size of the @val array
586 *
587 * Read an string list property @propname from the given firmware node and store
588 * them to @val if found.
589 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200590 * Return: number of values if @val was %NULL,
591 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100592 * %-EINVAL if given arguments are not valid,
593 * %-ENODATA if the property does not have a value,
594 * %-EPROTO if the property is not an array of strings,
595 * %-EOVERFLOW if the size of the property is not as expected,
596 * %-ENXIO if no suitable firmware interface is present.
597 */
598int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
599 const char *propname, const char **val,
600 size_t nval)
601{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200602 int ret;
603
604 ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200605 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
606 !IS_ERR_OR_NULL(fwnode->secondary))
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200607 ret = __fwnode_property_read_string_array(fwnode->secondary,
608 propname, val, nval);
609 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100610}
611EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
612
613/**
614 * fwnode_property_read_string - return a string property of a firmware node
615 * @fwnode: Firmware node to get the property of
616 * @propname: Name of the property
617 * @val: The value is stored here
618 *
619 * Read property @propname from the given firmware node and store the value into
620 * @val if found. The value is checked to be a string.
621 *
622 * Return: %0 if the property was found (success),
623 * %-EINVAL if given arguments are not valid,
624 * %-ENODATA if the property does not have a value,
625 * %-EPROTO or %-EILSEQ if the property is not a string,
626 * %-ENXIO if no suitable firmware interface is present.
627 */
628int fwnode_property_read_string(struct fwnode_handle *fwnode,
629 const char *propname, const char **val)
630{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200631 int ret;
632
633 ret = __fwnode_property_read_string(fwnode, propname, val);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200634 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
635 !IS_ERR_OR_NULL(fwnode->secondary))
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200636 ret = __fwnode_property_read_string(fwnode->secondary,
637 propname, val);
638 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100639}
640EXPORT_SYMBOL_GPL(fwnode_property_read_string);
641
642/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300643 * fwnode_property_match_string - find a string in an array and return index
644 * @fwnode: Firmware node to get the property of
645 * @propname: Name of the property holding the array
646 * @string: String to look for
647 *
648 * Find a given string in a string array and if it is found return the
649 * index back.
650 *
651 * Return: %0 if the property was found (success),
652 * %-EINVAL if given arguments are not valid,
653 * %-ENODATA if the property does not have a value,
654 * %-EPROTO if the property is not an array of strings,
655 * %-ENXIO if no suitable firmware interface is present.
656 */
657int fwnode_property_match_string(struct fwnode_handle *fwnode,
658 const char *propname, const char *string)
659{
660 const char **values;
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700661 int nval, ret;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300662
663 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
664 if (nval < 0)
665 return nval;
666
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200667 if (nval == 0)
668 return -ENODATA;
669
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300670 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
671 if (!values)
672 return -ENOMEM;
673
674 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
675 if (ret < 0)
676 goto out;
677
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700678 ret = match_string(values, nval, string);
679 if (ret < 0)
680 ret = -ENODATA;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300681out:
682 kfree(values);
683 return ret;
684}
685EXPORT_SYMBOL_GPL(fwnode_property_match_string);
686
687/**
Mika Westerberg13141e12015-11-30 17:11:37 +0200688 * pset_free_set - releases memory allocated for copied property set
689 * @pset: Property set to release
690 *
691 * Function takes previously copied property set and releases all the
692 * memory allocated to it.
693 */
694static void pset_free_set(struct property_set *pset)
695{
696 const struct property_entry *prop;
697 size_t i, nval;
698
699 if (!pset)
700 return;
701
702 for (prop = pset->properties; prop->name; prop++) {
703 if (prop->is_array) {
704 if (prop->is_string && prop->pointer.str) {
705 nval = prop->length / sizeof(const char *);
706 for (i = 0; i < nval; i++)
707 kfree(prop->pointer.str[i]);
708 }
709 kfree(prop->pointer.raw_data);
710 } else if (prop->is_string) {
711 kfree(prop->value.str);
712 }
713 kfree(prop->name);
714 }
715
716 kfree(pset->properties);
717 kfree(pset);
718}
719
720static int pset_copy_entry(struct property_entry *dst,
721 const struct property_entry *src)
722{
723 const char **d, **s;
724 size_t i, nval;
725
726 dst->name = kstrdup(src->name, GFP_KERNEL);
727 if (!dst->name)
728 return -ENOMEM;
729
730 if (src->is_array) {
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200731 if (!src->length)
732 return -ENODATA;
733
Mika Westerberg13141e12015-11-30 17:11:37 +0200734 if (src->is_string) {
735 nval = src->length / sizeof(const char *);
736 dst->pointer.str = kcalloc(nval, sizeof(const char *),
737 GFP_KERNEL);
738 if (!dst->pointer.str)
739 return -ENOMEM;
740
741 d = dst->pointer.str;
742 s = src->pointer.str;
743 for (i = 0; i < nval; i++) {
744 d[i] = kstrdup(s[i], GFP_KERNEL);
745 if (!d[i] && s[i])
746 return -ENOMEM;
747 }
748 } else {
749 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
750 src->length, GFP_KERNEL);
751 if (!dst->pointer.raw_data)
752 return -ENOMEM;
753 }
754 } else if (src->is_string) {
755 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
756 if (!dst->value.str && src->value.str)
757 return -ENOMEM;
758 } else {
759 dst->value.raw_data = src->value.raw_data;
760 }
761
762 dst->length = src->length;
763 dst->is_array = src->is_array;
764 dst->is_string = src->is_string;
765
766 return 0;
767}
768
769/**
770 * pset_copy_set - copies property set
771 * @pset: Property set to copy
772 *
773 * This function takes a deep copy of the given property set and returns
774 * pointer to the copy. Call device_free_property_set() to free resources
775 * allocated in this function.
776 *
777 * Return: Pointer to the new property set or error pointer.
778 */
779static struct property_set *pset_copy_set(const struct property_set *pset)
780{
781 const struct property_entry *entry;
782 struct property_set *p;
783 size_t i, n = 0;
784
785 p = kzalloc(sizeof(*p), GFP_KERNEL);
786 if (!p)
787 return ERR_PTR(-ENOMEM);
788
789 while (pset->properties[n].name)
790 n++;
791
792 p->properties = kcalloc(n + 1, sizeof(*entry), GFP_KERNEL);
793 if (!p->properties) {
794 kfree(p);
795 return ERR_PTR(-ENOMEM);
796 }
797
798 for (i = 0; i < n; i++) {
799 int ret = pset_copy_entry(&p->properties[i],
800 &pset->properties[i]);
801 if (ret) {
802 pset_free_set(p);
803 return ERR_PTR(ret);
804 }
805 }
806
807 return p;
808}
809
810/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300811 * device_remove_properties - Remove properties from a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200812 * @dev: Device whose properties to remove.
813 *
814 * The function removes properties previously associated to the device
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300815 * secondary firmware node with device_add_properties(). Memory allocated
Mika Westerberg13141e12015-11-30 17:11:37 +0200816 * to the properties will also be released.
817 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300818void device_remove_properties(struct device *dev)
Mika Westerberg13141e12015-11-30 17:11:37 +0200819{
820 struct fwnode_handle *fwnode;
Jarkko Nikula2a077f72017-10-09 16:28:37 +0300821 struct property_set *pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200822
823 fwnode = dev_fwnode(dev);
824 if (!fwnode)
825 return;
826 /*
827 * Pick either primary or secondary node depending which one holds
828 * the pset. If there is no real firmware node (ACPI/DT) primary
829 * will hold the pset.
830 */
Jarkko Nikula2a077f72017-10-09 16:28:37 +0300831 pset = to_pset_node(fwnode);
832 if (pset) {
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200833 set_primary_fwnode(dev, NULL);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200834 } else {
Jarkko Nikula2a077f72017-10-09 16:28:37 +0300835 pset = to_pset_node(fwnode->secondary);
836 if (pset && dev == pset->dev)
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200837 set_secondary_fwnode(dev, NULL);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200838 }
Jarkko Nikula2a077f72017-10-09 16:28:37 +0300839 if (pset && dev == pset->dev)
840 pset_free_set(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200841}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300842EXPORT_SYMBOL_GPL(device_remove_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200843
844/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300845 * device_add_properties - Add a collection of properties to a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200846 * @dev: Device to add properties to.
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300847 * @properties: Collection of properties to add.
Mika Westerberg13141e12015-11-30 17:11:37 +0200848 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300849 * Associate a collection of device properties represented by @properties with
850 * @dev as its secondary firmware node. The function takes a copy of
851 * @properties.
Mika Westerberg13141e12015-11-30 17:11:37 +0200852 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300853int device_add_properties(struct device *dev, struct property_entry *properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200854{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300855 struct property_set *p, pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200856
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300857 if (!properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200858 return -EINVAL;
859
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300860 pset.properties = properties;
861
862 p = pset_copy_set(&pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200863 if (IS_ERR(p))
864 return PTR_ERR(p);
865
866 p->fwnode.type = FWNODE_PDATA;
867 set_secondary_fwnode(dev, &p->fwnode);
Jarkko Nikula2a077f72017-10-09 16:28:37 +0300868 p->dev = dev;
Mika Westerberg13141e12015-11-30 17:11:37 +0200869 return 0;
870}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300871EXPORT_SYMBOL_GPL(device_add_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200872
873/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100874 * device_get_next_child_node - Return the next child node handle for a device
875 * @dev: Device to find the next child node for.
876 * @child: Handle to one of the device's child nodes or a null handle.
877 */
878struct fwnode_handle *device_get_next_child_node(struct device *dev,
879 struct fwnode_handle *child)
880{
881 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
882 struct device_node *node;
883
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200884 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100885 if (node)
886 return &node->fwnode;
887 } else if (IS_ENABLED(CONFIG_ACPI)) {
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200888 return acpi_get_next_subnode(dev, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100889 }
890 return NULL;
891}
892EXPORT_SYMBOL_GPL(device_get_next_child_node);
893
894/**
Adam Thomson613e9722016-06-21 18:50:20 +0100895 * device_get_named_child_node - Return first matching named child node handle
896 * @dev: Device to find the named child node for.
897 * @childname: String to match child node name against.
898 */
899struct fwnode_handle *device_get_named_child_node(struct device *dev,
900 const char *childname)
901{
902 struct fwnode_handle *child;
903
904 /*
905 * Find first matching named child node of this device.
906 * For ACPI this will be a data only sub-node.
907 */
908 device_for_each_child_node(dev, child) {
909 if (is_of_node(child)) {
910 if (!of_node_cmp(to_of_node(child)->name, childname))
911 return child;
912 } else if (is_acpi_data_node(child)) {
913 if (acpi_data_node_match(child, childname))
914 return child;
915 }
916 }
917
918 return NULL;
919}
920EXPORT_SYMBOL_GPL(device_get_named_child_node);
921
922/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100923 * fwnode_handle_put - Drop reference to a device node
924 * @fwnode: Pointer to the device node to drop the reference to.
925 *
926 * This has to be used when terminating device_for_each_child_node() iteration
927 * with break or return to prevent stale device node references from being left
928 * behind.
929 */
930void fwnode_handle_put(struct fwnode_handle *fwnode)
931{
932 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200933 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100934}
935EXPORT_SYMBOL_GPL(fwnode_handle_put);
936
937/**
938 * device_get_child_node_count - return the number of child nodes for device
939 * @dev: Device to cound the child nodes for
940 */
941unsigned int device_get_child_node_count(struct device *dev)
942{
943 struct fwnode_handle *child;
944 unsigned int count = 0;
945
946 device_for_each_child_node(dev, child)
947 count++;
948
949 return count;
950}
951EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -0500952
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -0700953bool device_dma_supported(struct device *dev)
954{
955 /* For DT, this is always supported.
956 * For ACPI, this depends on CCA, which
957 * is determined by the acpi_dma_supported().
958 */
959 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
960 return true;
961
962 return acpi_dma_supported(ACPI_COMPANION(dev));
963}
964EXPORT_SYMBOL_GPL(device_dma_supported);
965
966enum dev_dma_attr device_get_dma_attr(struct device *dev)
967{
968 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
969
970 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
971 if (of_dma_is_coherent(dev->of_node))
972 attr = DEV_DMA_COHERENT;
973 else
974 attr = DEV_DMA_NON_COHERENT;
975 } else
976 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
977
978 return attr;
979}
980EXPORT_SYMBOL_GPL(device_get_dma_attr);
981
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500982/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500983 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500984 * @dev: Pointer to the given device
985 *
986 * The function gets phy interface string from property 'phy-mode' or
987 * 'phy-connection-type', and return its index in phy_modes table, or errno in
988 * error case.
989 */
990int device_get_phy_mode(struct device *dev)
991{
992 const char *pm;
993 int err, i;
994
995 err = device_property_read_string(dev, "phy-mode", &pm);
996 if (err < 0)
997 err = device_property_read_string(dev,
998 "phy-connection-type", &pm);
999 if (err < 0)
1000 return err;
1001
1002 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1003 if (!strcasecmp(pm, phy_modes(i)))
1004 return i;
1005
1006 return -ENODEV;
1007}
1008EXPORT_SYMBOL_GPL(device_get_phy_mode);
1009
1010static void *device_get_mac_addr(struct device *dev,
1011 const char *name, char *addr,
1012 int alen)
1013{
1014 int ret = device_property_read_u8_array(dev, name, addr, alen);
1015
Jeremy Linton2f710a32015-08-19 11:46:42 -05001016 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001017 return addr;
1018 return NULL;
1019}
1020
1021/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001022 * device_get_mac_address - Get the MAC for a given device
1023 * @dev: Pointer to the device
1024 * @addr: Address of buffer to store the MAC in
1025 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1026 *
1027 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001028 * checked first, because that is supposed to contain to "most recent" MAC
1029 * address. If that isn't set, then 'local-mac-address' is checked next,
1030 * because that is the default address. If that isn't set, then the obsolete
1031 * 'address' is checked, just in case we're using an old device tree.
1032 *
1033 * Note that the 'address' property is supposed to contain a virtual address of
1034 * the register set, but some DTS files have redefined that property to be the
1035 * MAC address.
1036 *
1037 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -05001038 * exist in the firmware tables, but were not updated by the firmware. For
1039 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1040 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1041 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1042 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001043*/
1044void *device_get_mac_address(struct device *dev, char *addr, int alen)
1045{
Julien Grall5b902d62015-09-03 23:59:50 +01001046 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001047
Julien Grall5b902d62015-09-03 23:59:50 +01001048 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1049 if (res)
1050 return res;
1051
1052 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1053 if (res)
1054 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001055
1056 return device_get_mac_addr(dev, "address", addr, alen);
1057}
1058EXPORT_SYMBOL(device_get_mac_address);