blob: 1325ff225cc4a5b37a7c0b3fe0b3d7946c37811e [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
40static inline bool is_pset(struct fwnode_handle *fwnode)
41{
42 return fwnode && fwnode->type == FWNODE_PDATA;
43}
44
45static inline struct property_set *to_pset(struct fwnode_handle *fwnode)
46{
47 return is_pset(fwnode) ?
48 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
66static int pset_prop_read_array(struct property_set *pset, const char *name,
67 enum dev_prop_type type, void *val, size_t nval)
68{
69 struct property_entry *prop;
70 unsigned int item_size;
71
72 prop = pset_prop_get(pset, name);
73 if (!prop)
74 return -ENODATA;
75
76 if (prop->type != type)
77 return -EPROTO;
78
79 if (!val)
80 return prop->nval;
81
82 if (prop->nval < nval)
83 return -EOVERFLOW;
84
85 switch (type) {
86 case DEV_PROP_U8:
87 item_size = sizeof(u8);
88 break;
89 case DEV_PROP_U16:
90 item_size = sizeof(u16);
91 break;
92 case DEV_PROP_U32:
93 item_size = sizeof(u32);
94 break;
95 case DEV_PROP_U64:
96 item_size = sizeof(u64);
97 break;
98 case DEV_PROP_STRING:
99 item_size = sizeof(const char *);
100 break;
101 default:
102 return -EINVAL;
103 }
104 memcpy(val, prop->value.raw_data, nval * item_size);
105 return 0;
106}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100107
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100108static inline struct fwnode_handle *dev_fwnode(struct device *dev)
109{
110 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
111 &dev->of_node->fwnode : dev->fwnode;
112}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100113
114/**
115 * device_property_present - check if a property of a device is present
116 * @dev: Device whose property is being checked
117 * @propname: Name of the property
118 *
119 * Check if property @propname is present in the device firmware description.
120 */
121bool device_property_present(struct device *dev, const char *propname)
122{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100123 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100124}
125EXPORT_SYMBOL_GPL(device_property_present);
126
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100127/**
128 * fwnode_property_present - check if a property of a firmware node is present
129 * @fwnode: Firmware node whose property to check
130 * @propname: Name of the property
131 */
132bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
133{
134 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200135 return of_property_read_bool(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100136 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200137 return !acpi_node_prop_get(fwnode, propname, NULL);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100138
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200139 return !!pset_prop_get(to_pset(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100140}
141EXPORT_SYMBOL_GPL(fwnode_property_present);
142
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100143/**
144 * device_property_read_u8_array - return a u8 array property of a device
145 * @dev: Device to get the property of
146 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200147 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100148 * @nval: Size of the @val array
149 *
150 * Function reads an array of u8 properties with @propname from the device
151 * firmware description and stores them to @val if found.
152 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200153 * Return: number of values if @val was %NULL,
154 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100155 * %-EINVAL if given arguments are not valid,
156 * %-ENODATA if the property does not have a value,
157 * %-EPROTO if the property is not an array of numbers,
158 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700159 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100160 */
161int device_property_read_u8_array(struct device *dev, const char *propname,
162 u8 *val, size_t nval)
163{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100164 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100165}
166EXPORT_SYMBOL_GPL(device_property_read_u8_array);
167
168/**
169 * device_property_read_u16_array - return a u16 array property of a device
170 * @dev: Device to get the property of
171 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200172 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100173 * @nval: Size of the @val array
174 *
175 * Function reads an array of u16 properties with @propname from the device
176 * firmware description and stores them to @val if found.
177 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200178 * Return: number of values if @val was %NULL,
179 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100180 * %-EINVAL if given arguments are not valid,
181 * %-ENODATA if the property does not have a value,
182 * %-EPROTO if the property is not an array of numbers,
183 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700184 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100185 */
186int device_property_read_u16_array(struct device *dev, const char *propname,
187 u16 *val, size_t nval)
188{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100189 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100190}
191EXPORT_SYMBOL_GPL(device_property_read_u16_array);
192
193/**
194 * device_property_read_u32_array - return a u32 array property of a device
195 * @dev: Device to get the property of
196 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200197 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100198 * @nval: Size of the @val array
199 *
200 * Function reads an array of u32 properties with @propname from the device
201 * firmware description and stores them to @val if found.
202 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200203 * Return: number of values if @val was %NULL,
204 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100205 * %-EINVAL if given arguments are not valid,
206 * %-ENODATA if the property does not have a value,
207 * %-EPROTO if the property is not an array of numbers,
208 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700209 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100210 */
211int device_property_read_u32_array(struct device *dev, const char *propname,
212 u32 *val, size_t nval)
213{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100214 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100215}
216EXPORT_SYMBOL_GPL(device_property_read_u32_array);
217
218/**
219 * device_property_read_u64_array - return a u64 array property of a device
220 * @dev: Device to get the property of
221 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200222 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100223 * @nval: Size of the @val array
224 *
225 * Function reads an array of u64 properties with @propname from the device
226 * firmware description and stores them to @val if found.
227 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200228 * Return: number of values if @val was %NULL,
229 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100230 * %-EINVAL if given arguments are not valid,
231 * %-ENODATA if the property does not have a value,
232 * %-EPROTO if the property is not an array of numbers,
233 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700234 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100235 */
236int device_property_read_u64_array(struct device *dev, const char *propname,
237 u64 *val, size_t nval)
238{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100239 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100240}
241EXPORT_SYMBOL_GPL(device_property_read_u64_array);
242
243/**
244 * device_property_read_string_array - return a string array property of device
245 * @dev: Device to get the property of
246 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200247 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100248 * @nval: Size of the @val array
249 *
250 * Function reads an array of string properties with @propname from the device
251 * firmware description and stores them to @val if found.
252 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200253 * Return: number of values if @val was %NULL,
254 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100255 * %-EINVAL if given arguments are not valid,
256 * %-ENODATA if the property does not have a value,
257 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
258 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700259 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100260 */
261int device_property_read_string_array(struct device *dev, const char *propname,
262 const char **val, size_t nval)
263{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100264 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100265}
266EXPORT_SYMBOL_GPL(device_property_read_string_array);
267
268/**
269 * device_property_read_string - return a string property of a device
270 * @dev: Device to get the property of
271 * @propname: Name of the property
272 * @val: The value is stored here
273 *
274 * Function reads property @propname from the device firmware description and
275 * stores the value into @val if found. The value is checked to be a string.
276 *
277 * Return: %0 if the property was found (success),
278 * %-EINVAL if given arguments are not valid,
279 * %-ENODATA if the property does not have a value,
280 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700281 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100282 */
283int device_property_read_string(struct device *dev, const char *propname,
284 const char **val)
285{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100286 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100287}
288EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100289
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300290/**
291 * device_property_match_string - find a string in an array and return index
292 * @dev: Device to get the property of
293 * @propname: Name of the property holding the array
294 * @string: String to look for
295 *
296 * Find a given string in a string array and if it is found return the
297 * index back.
298 *
299 * Return: %0 if the property was found (success),
300 * %-EINVAL if given arguments are not valid,
301 * %-ENODATA if the property does not have a value,
302 * %-EPROTO if the property is not an array of strings,
303 * %-ENXIO if no suitable firmware interface is present.
304 */
305int device_property_match_string(struct device *dev, const char *propname,
306 const char *string)
307{
308 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
309}
310EXPORT_SYMBOL_GPL(device_property_match_string);
311
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100312#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
313 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
314 : of_property_count_elems_of_size((node), (propname), sizeof(type))
315
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100316#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
317({ \
318 int _ret_; \
319 if (is_of_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200320 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100321 _type_, _val_, _nval_); \
322 else if (is_acpi_node(_fwnode_)) \
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200323 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
324 _val_, _nval_); \
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700325 else if (is_pset(_fwnode_)) \
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200326 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
327 _proptype_, _val_, _nval_); \
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700328 else \
329 _ret_ = -ENXIO; \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100330 _ret_; \
331})
332
333/**
334 * fwnode_property_read_u8_array - return a u8 array property of firmware node
335 * @fwnode: Firmware node to get the property of
336 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200337 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100338 * @nval: Size of the @val array
339 *
340 * Read an array of u8 properties with @propname from @fwnode and stores them to
341 * @val if found.
342 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200343 * Return: number of values if @val was %NULL,
344 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100345 * %-EINVAL if given arguments are not valid,
346 * %-ENODATA if the property does not have a value,
347 * %-EPROTO if the property is not an array of numbers,
348 * %-EOVERFLOW if the size of the property is not as expected,
349 * %-ENXIO if no suitable firmware interface is present.
350 */
351int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
352 const char *propname, u8 *val, size_t nval)
353{
354 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
355 val, nval);
356}
357EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
358
359/**
360 * fwnode_property_read_u16_array - return a u16 array property of firmware node
361 * @fwnode: Firmware node to get the property of
362 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200363 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100364 * @nval: Size of the @val array
365 *
366 * Read an array of u16 properties with @propname from @fwnode and store them to
367 * @val if found.
368 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200369 * Return: number of values if @val was %NULL,
370 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100371 * %-EINVAL if given arguments are not valid,
372 * %-ENODATA if the property does not have a value,
373 * %-EPROTO if the property is not an array of numbers,
374 * %-EOVERFLOW if the size of the property is not as expected,
375 * %-ENXIO if no suitable firmware interface is present.
376 */
377int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
378 const char *propname, u16 *val, size_t nval)
379{
380 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
381 val, nval);
382}
383EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
384
385/**
386 * fwnode_property_read_u32_array - return a u32 array property of firmware node
387 * @fwnode: Firmware node to get the property of
388 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200389 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100390 * @nval: Size of the @val array
391 *
392 * Read an array of u32 properties with @propname from @fwnode store them to
393 * @val if found.
394 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200395 * Return: number of values if @val was %NULL,
396 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100397 * %-EINVAL if given arguments are not valid,
398 * %-ENODATA if the property does not have a value,
399 * %-EPROTO if the property is not an array of numbers,
400 * %-EOVERFLOW if the size of the property is not as expected,
401 * %-ENXIO if no suitable firmware interface is present.
402 */
403int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
404 const char *propname, u32 *val, size_t nval)
405{
406 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
407 val, nval);
408}
409EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
410
411/**
412 * fwnode_property_read_u64_array - return a u64 array property firmware node
413 * @fwnode: Firmware node to get the property of
414 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200415 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100416 * @nval: Size of the @val array
417 *
418 * Read an array of u64 properties with @propname from @fwnode and store them to
419 * @val if found.
420 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200421 * Return: number of values if @val was %NULL,
422 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100423 * %-EINVAL if given arguments are not valid,
424 * %-ENODATA if the property does not have a value,
425 * %-EPROTO if the property is not an array of numbers,
426 * %-EOVERFLOW if the size of the property is not as expected,
427 * %-ENXIO if no suitable firmware interface is present.
428 */
429int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
430 const char *propname, u64 *val, size_t nval)
431{
432 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
433 val, nval);
434}
435EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
436
437/**
438 * fwnode_property_read_string_array - return string array property of a node
439 * @fwnode: Firmware node to get the property of
440 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200441 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100442 * @nval: Size of the @val array
443 *
444 * Read an string list property @propname from the given firmware node and store
445 * them to @val if found.
446 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200447 * Return: number of values if @val was %NULL,
448 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100449 * %-EINVAL if given arguments are not valid,
450 * %-ENODATA if the property does not have a value,
451 * %-EPROTO if the property is not an array of strings,
452 * %-EOVERFLOW if the size of the property is not as expected,
453 * %-ENXIO if no suitable firmware interface is present.
454 */
455int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
456 const char *propname, const char **val,
457 size_t nval)
458{
459 if (is_of_node(fwnode))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100460 return val ?
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200461 of_property_read_string_array(to_of_node(fwnode),
462 propname, val, nval) :
463 of_property_count_strings(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100464 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200465 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
466 val, nval);
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700467 else if (is_pset(fwnode))
468 return pset_prop_read_array(to_pset(fwnode), propname,
469 DEV_PROP_STRING, val, nval);
470 return -ENXIO;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100471}
472EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
473
474/**
475 * fwnode_property_read_string - return a string property of a firmware node
476 * @fwnode: Firmware node to get the property of
477 * @propname: Name of the property
478 * @val: The value is stored here
479 *
480 * Read property @propname from the given firmware node and store the value into
481 * @val if found. The value is checked to be a string.
482 *
483 * Return: %0 if the property was found (success),
484 * %-EINVAL if given arguments are not valid,
485 * %-ENODATA if the property does not have a value,
486 * %-EPROTO or %-EILSEQ if the property is not a string,
487 * %-ENXIO if no suitable firmware interface is present.
488 */
489int fwnode_property_read_string(struct fwnode_handle *fwnode,
490 const char *propname, const char **val)
491{
492 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200493 return of_property_read_string(to_of_node(fwnode), propname, val);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100494 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200495 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
496 val, 1);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100497
Andy Shevchenko4f73b062015-08-10 19:56:47 +0300498 return pset_prop_read_array(to_pset(fwnode), propname,
499 DEV_PROP_STRING, val, 1);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100500}
501EXPORT_SYMBOL_GPL(fwnode_property_read_string);
502
503/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300504 * fwnode_property_match_string - find a string in an array and return index
505 * @fwnode: Firmware node to get the property of
506 * @propname: Name of the property holding the array
507 * @string: String to look for
508 *
509 * Find a given string in a string array and if it is found return the
510 * index back.
511 *
512 * Return: %0 if the property was found (success),
513 * %-EINVAL if given arguments are not valid,
514 * %-ENODATA if the property does not have a value,
515 * %-EPROTO if the property is not an array of strings,
516 * %-ENXIO if no suitable firmware interface is present.
517 */
518int fwnode_property_match_string(struct fwnode_handle *fwnode,
519 const char *propname, const char *string)
520{
521 const char **values;
522 int nval, ret, i;
523
524 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
525 if (nval < 0)
526 return nval;
527
528 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
529 if (!values)
530 return -ENOMEM;
531
532 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
533 if (ret < 0)
534 goto out;
535
536 ret = -ENODATA;
537 for (i = 0; i < nval; i++) {
538 if (!strcmp(values[i], string)) {
539 ret = i;
540 break;
541 }
542 }
543out:
544 kfree(values);
545 return ret;
546}
547EXPORT_SYMBOL_GPL(fwnode_property_match_string);
548
549/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100550 * device_get_next_child_node - Return the next child node handle for a device
551 * @dev: Device to find the next child node for.
552 * @child: Handle to one of the device's child nodes or a null handle.
553 */
554struct fwnode_handle *device_get_next_child_node(struct device *dev,
555 struct fwnode_handle *child)
556{
557 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
558 struct device_node *node;
559
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200560 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100561 if (node)
562 return &node->fwnode;
563 } else if (IS_ENABLED(CONFIG_ACPI)) {
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200564 return acpi_get_next_subnode(dev, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100565 }
566 return NULL;
567}
568EXPORT_SYMBOL_GPL(device_get_next_child_node);
569
570/**
571 * fwnode_handle_put - Drop reference to a device node
572 * @fwnode: Pointer to the device node to drop the reference to.
573 *
574 * This has to be used when terminating device_for_each_child_node() iteration
575 * with break or return to prevent stale device node references from being left
576 * behind.
577 */
578void fwnode_handle_put(struct fwnode_handle *fwnode)
579{
580 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200581 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100582}
583EXPORT_SYMBOL_GPL(fwnode_handle_put);
584
585/**
586 * device_get_child_node_count - return the number of child nodes for device
587 * @dev: Device to cound the child nodes for
588 */
589unsigned int device_get_child_node_count(struct device *dev)
590{
591 struct fwnode_handle *child;
592 unsigned int count = 0;
593
594 device_for_each_child_node(dev, child)
595 count++;
596
597 return count;
598}
599EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -0500600
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -0700601bool device_dma_supported(struct device *dev)
602{
603 /* For DT, this is always supported.
604 * For ACPI, this depends on CCA, which
605 * is determined by the acpi_dma_supported().
606 */
607 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
608 return true;
609
610 return acpi_dma_supported(ACPI_COMPANION(dev));
611}
612EXPORT_SYMBOL_GPL(device_dma_supported);
613
614enum dev_dma_attr device_get_dma_attr(struct device *dev)
615{
616 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
617
618 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
619 if (of_dma_is_coherent(dev->of_node))
620 attr = DEV_DMA_COHERENT;
621 else
622 attr = DEV_DMA_NON_COHERENT;
623 } else
624 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
625
626 return attr;
627}
628EXPORT_SYMBOL_GPL(device_get_dma_attr);
629
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500630/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500631 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500632 * @dev: Pointer to the given device
633 *
634 * The function gets phy interface string from property 'phy-mode' or
635 * 'phy-connection-type', and return its index in phy_modes table, or errno in
636 * error case.
637 */
638int device_get_phy_mode(struct device *dev)
639{
640 const char *pm;
641 int err, i;
642
643 err = device_property_read_string(dev, "phy-mode", &pm);
644 if (err < 0)
645 err = device_property_read_string(dev,
646 "phy-connection-type", &pm);
647 if (err < 0)
648 return err;
649
650 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
651 if (!strcasecmp(pm, phy_modes(i)))
652 return i;
653
654 return -ENODEV;
655}
656EXPORT_SYMBOL_GPL(device_get_phy_mode);
657
658static void *device_get_mac_addr(struct device *dev,
659 const char *name, char *addr,
660 int alen)
661{
662 int ret = device_property_read_u8_array(dev, name, addr, alen);
663
Jeremy Linton2f710a32015-08-19 11:46:42 -0500664 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500665 return addr;
666 return NULL;
667}
668
669/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500670 * device_get_mac_address - Get the MAC for a given device
671 * @dev: Pointer to the device
672 * @addr: Address of buffer to store the MAC in
673 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
674 *
675 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500676 * checked first, because that is supposed to contain to "most recent" MAC
677 * address. If that isn't set, then 'local-mac-address' is checked next,
678 * because that is the default address. If that isn't set, then the obsolete
679 * 'address' is checked, just in case we're using an old device tree.
680 *
681 * Note that the 'address' property is supposed to contain a virtual address of
682 * the register set, but some DTS files have redefined that property to be the
683 * MAC address.
684 *
685 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -0500686 * exist in the firmware tables, but were not updated by the firmware. For
687 * example, the DTS could define 'mac-address' and 'local-mac-address', with
688 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
689 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
690 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500691*/
692void *device_get_mac_address(struct device *dev, char *addr, int alen)
693{
Julien Grall5b902d62015-09-03 23:59:50 +0100694 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500695
Julien Grall5b902d62015-09-03 23:59:50 +0100696 res = device_get_mac_addr(dev, "mac-address", addr, alen);
697 if (res)
698 return res;
699
700 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
701 if (res)
702 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500703
704 return device_get_mac_addr(dev, "address", addr, alen);
705}
706EXPORT_SYMBOL(device_get_mac_address);