blob: 09e488db59ea50a4921a16ff769b381431a00497 [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);
Andy Shevchenkoe3f9e292015-11-30 17:11:29 +0200138 else if (is_pset(fwnode))
139 return !!pset_prop_get(to_pset(fwnode), propname);
140 return false;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100141}
142EXPORT_SYMBOL_GPL(fwnode_property_present);
143
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100144/**
145 * device_property_read_u8_array - return a u8 array property of a device
146 * @dev: Device to get the property of
147 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200148 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100149 * @nval: Size of the @val array
150 *
151 * Function reads an array of u8 properties with @propname from the device
152 * firmware description and stores them to @val if found.
153 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200154 * Return: number of values if @val was %NULL,
155 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100156 * %-EINVAL if given arguments are not valid,
157 * %-ENODATA if the property does not have a value,
158 * %-EPROTO if the property is not an array of numbers,
159 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700160 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100161 */
162int device_property_read_u8_array(struct device *dev, const char *propname,
163 u8 *val, size_t nval)
164{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100165 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100166}
167EXPORT_SYMBOL_GPL(device_property_read_u8_array);
168
169/**
170 * device_property_read_u16_array - return a u16 array property of a device
171 * @dev: Device to get the property of
172 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200173 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100174 * @nval: Size of the @val array
175 *
176 * Function reads an array of u16 properties with @propname from the device
177 * firmware description and stores them to @val if found.
178 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200179 * Return: number of values if @val was %NULL,
180 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100181 * %-EINVAL if given arguments are not valid,
182 * %-ENODATA if the property does not have a value,
183 * %-EPROTO if the property is not an array of numbers,
184 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700185 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100186 */
187int device_property_read_u16_array(struct device *dev, const char *propname,
188 u16 *val, size_t nval)
189{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100190 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100191}
192EXPORT_SYMBOL_GPL(device_property_read_u16_array);
193
194/**
195 * device_property_read_u32_array - return a u32 array property of a device
196 * @dev: Device to get the property of
197 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200198 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100199 * @nval: Size of the @val array
200 *
201 * Function reads an array of u32 properties with @propname from the device
202 * firmware description and stores them to @val if found.
203 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200204 * Return: number of values if @val was %NULL,
205 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100206 * %-EINVAL if given arguments are not valid,
207 * %-ENODATA if the property does not have a value,
208 * %-EPROTO if the property is not an array of numbers,
209 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700210 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100211 */
212int device_property_read_u32_array(struct device *dev, const char *propname,
213 u32 *val, size_t nval)
214{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100215 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100216}
217EXPORT_SYMBOL_GPL(device_property_read_u32_array);
218
219/**
220 * device_property_read_u64_array - return a u64 array property of a device
221 * @dev: Device to get the property of
222 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200223 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100224 * @nval: Size of the @val array
225 *
226 * Function reads an array of u64 properties with @propname from the device
227 * firmware description and stores them to @val if found.
228 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200229 * Return: number of values if @val was %NULL,
230 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100231 * %-EINVAL if given arguments are not valid,
232 * %-ENODATA if the property does not have a value,
233 * %-EPROTO if the property is not an array of numbers,
234 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700235 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100236 */
237int device_property_read_u64_array(struct device *dev, const char *propname,
238 u64 *val, size_t nval)
239{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100240 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100241}
242EXPORT_SYMBOL_GPL(device_property_read_u64_array);
243
244/**
245 * device_property_read_string_array - return a string array property of device
246 * @dev: Device to get the property of
247 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200248 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100249 * @nval: Size of the @val array
250 *
251 * Function reads an array of string properties with @propname from the device
252 * firmware description and stores them to @val if found.
253 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200254 * Return: number of values if @val was %NULL,
255 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100256 * %-EINVAL if given arguments are not valid,
257 * %-ENODATA if the property does not have a value,
258 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
259 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700260 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100261 */
262int device_property_read_string_array(struct device *dev, const char *propname,
263 const char **val, size_t nval)
264{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100265 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100266}
267EXPORT_SYMBOL_GPL(device_property_read_string_array);
268
269/**
270 * device_property_read_string - return a string property of a device
271 * @dev: Device to get the property of
272 * @propname: Name of the property
273 * @val: The value is stored here
274 *
275 * Function reads property @propname from the device firmware description and
276 * stores the value into @val if found. The value is checked to be a string.
277 *
278 * Return: %0 if the property was found (success),
279 * %-EINVAL if given arguments are not valid,
280 * %-ENODATA if the property does not have a value,
281 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700282 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100283 */
284int device_property_read_string(struct device *dev, const char *propname,
285 const char **val)
286{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100287 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100288}
289EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100290
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300291/**
292 * device_property_match_string - find a string in an array and return index
293 * @dev: Device to get the property of
294 * @propname: Name of the property holding the array
295 * @string: String to look for
296 *
297 * Find a given string in a string array and if it is found return the
298 * index back.
299 *
300 * Return: %0 if the property was found (success),
301 * %-EINVAL if given arguments are not valid,
302 * %-ENODATA if the property does not have a value,
303 * %-EPROTO if the property is not an array of strings,
304 * %-ENXIO if no suitable firmware interface is present.
305 */
306int device_property_match_string(struct device *dev, const char *propname,
307 const char *string)
308{
309 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
310}
311EXPORT_SYMBOL_GPL(device_property_match_string);
312
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100313#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \
314 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \
315 : of_property_count_elems_of_size((node), (propname), sizeof(type))
316
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100317#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \
318({ \
319 int _ret_; \
320 if (is_of_node(_fwnode_)) \
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200321 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100322 _type_, _val_, _nval_); \
323 else if (is_acpi_node(_fwnode_)) \
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200324 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \
325 _val_, _nval_); \
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700326 else if (is_pset(_fwnode_)) \
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200327 _ret_ = pset_prop_read_array(to_pset(_fwnode_), _propname_, \
328 _proptype_, _val_, _nval_); \
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700329 else \
330 _ret_ = -ENXIO; \
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100331 _ret_; \
332})
333
334/**
335 * fwnode_property_read_u8_array - return a u8 array property of firmware node
336 * @fwnode: Firmware node to get the property of
337 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200338 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100339 * @nval: Size of the @val array
340 *
341 * Read an array of u8 properties with @propname from @fwnode and stores them to
342 * @val if found.
343 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200344 * Return: number of values if @val was %NULL,
345 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100346 * %-EINVAL if given arguments are not valid,
347 * %-ENODATA if the property does not have a value,
348 * %-EPROTO if the property is not an array of numbers,
349 * %-EOVERFLOW if the size of the property is not as expected,
350 * %-ENXIO if no suitable firmware interface is present.
351 */
352int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
353 const char *propname, u8 *val, size_t nval)
354{
355 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
356 val, nval);
357}
358EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
359
360/**
361 * fwnode_property_read_u16_array - return a u16 array property of firmware node
362 * @fwnode: Firmware node to get the property of
363 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200364 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100365 * @nval: Size of the @val array
366 *
367 * Read an array of u16 properties with @propname from @fwnode and store them to
368 * @val if found.
369 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200370 * Return: number of values if @val was %NULL,
371 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100372 * %-EINVAL if given arguments are not valid,
373 * %-ENODATA if the property does not have a value,
374 * %-EPROTO if the property is not an array of numbers,
375 * %-EOVERFLOW if the size of the property is not as expected,
376 * %-ENXIO if no suitable firmware interface is present.
377 */
378int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
379 const char *propname, u16 *val, size_t nval)
380{
381 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
382 val, nval);
383}
384EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
385
386/**
387 * fwnode_property_read_u32_array - return a u32 array property of firmware node
388 * @fwnode: Firmware node to get the property of
389 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200390 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100391 * @nval: Size of the @val array
392 *
393 * Read an array of u32 properties with @propname from @fwnode store them to
394 * @val if found.
395 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200396 * Return: number of values if @val was %NULL,
397 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100398 * %-EINVAL if given arguments are not valid,
399 * %-ENODATA if the property does not have a value,
400 * %-EPROTO if the property is not an array of numbers,
401 * %-EOVERFLOW if the size of the property is not as expected,
402 * %-ENXIO if no suitable firmware interface is present.
403 */
404int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
405 const char *propname, u32 *val, size_t nval)
406{
407 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
408 val, nval);
409}
410EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
411
412/**
413 * fwnode_property_read_u64_array - return a u64 array property firmware node
414 * @fwnode: Firmware node to get the property of
415 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200416 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100417 * @nval: Size of the @val array
418 *
419 * Read an array of u64 properties with @propname from @fwnode and store them to
420 * @val if found.
421 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200422 * Return: number of values if @val was %NULL,
423 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100424 * %-EINVAL if given arguments are not valid,
425 * %-ENODATA if the property does not have a value,
426 * %-EPROTO if the property is not an array of numbers,
427 * %-EOVERFLOW if the size of the property is not as expected,
428 * %-ENXIO if no suitable firmware interface is present.
429 */
430int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
431 const char *propname, u64 *val, size_t nval)
432{
433 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
434 val, nval);
435}
436EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
437
438/**
439 * fwnode_property_read_string_array - return string array property of a node
440 * @fwnode: Firmware node to get the property of
441 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200442 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100443 * @nval: Size of the @val array
444 *
445 * Read an string list property @propname from the given firmware node and store
446 * them to @val if found.
447 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200448 * Return: number of values if @val was %NULL,
449 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100450 * %-EINVAL if given arguments are not valid,
451 * %-ENODATA if the property does not have a value,
452 * %-EPROTO if the property is not an array of strings,
453 * %-EOVERFLOW if the size of the property is not as expected,
454 * %-ENXIO if no suitable firmware interface is present.
455 */
456int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
457 const char *propname, const char **val,
458 size_t nval)
459{
460 if (is_of_node(fwnode))
Rafael J. Wysockif42712a2015-03-24 00:18:05 +0100461 return val ?
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200462 of_property_read_string_array(to_of_node(fwnode),
463 propname, val, nval) :
464 of_property_count_strings(to_of_node(fwnode), propname);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100465 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200466 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
467 val, nval);
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700468 else if (is_pset(fwnode))
469 return pset_prop_read_array(to_pset(fwnode), propname,
470 DEV_PROP_STRING, val, nval);
471 return -ENXIO;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100472}
473EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
474
475/**
476 * fwnode_property_read_string - return a string property of a firmware node
477 * @fwnode: Firmware node to get the property of
478 * @propname: Name of the property
479 * @val: The value is stored here
480 *
481 * Read property @propname from the given firmware node and store the value into
482 * @val if found. The value is checked to be a string.
483 *
484 * Return: %0 if the property was found (success),
485 * %-EINVAL if given arguments are not valid,
486 * %-ENODATA if the property does not have a value,
487 * %-EPROTO or %-EILSEQ if the property is not a string,
488 * %-ENXIO if no suitable firmware interface is present.
489 */
490int fwnode_property_read_string(struct fwnode_handle *fwnode,
491 const char *propname, const char **val)
492{
493 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200494 return of_property_read_string(to_of_node(fwnode), propname, val);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100495 else if (is_acpi_node(fwnode))
Rafael J. Wysocki3a7a2ab2015-08-27 04:40:05 +0200496 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
497 val, 1);
Andy Shevchenkoe3f9e292015-11-30 17:11:29 +0200498 else if (is_pset(fwnode))
499 return pset_prop_read_array(to_pset(fwnode), propname,
500 DEV_PROP_STRING, val, 1);
501 return -ENXIO;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100502}
503EXPORT_SYMBOL_GPL(fwnode_property_read_string);
504
505/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300506 * fwnode_property_match_string - find a string in an array and return index
507 * @fwnode: Firmware node to get the property of
508 * @propname: Name of the property holding the array
509 * @string: String to look for
510 *
511 * Find a given string in a string array and if it is found return the
512 * index back.
513 *
514 * Return: %0 if the property was found (success),
515 * %-EINVAL if given arguments are not valid,
516 * %-ENODATA if the property does not have a value,
517 * %-EPROTO if the property is not an array of strings,
518 * %-ENXIO if no suitable firmware interface is present.
519 */
520int fwnode_property_match_string(struct fwnode_handle *fwnode,
521 const char *propname, const char *string)
522{
523 const char **values;
524 int nval, ret, i;
525
526 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
527 if (nval < 0)
528 return nval;
529
530 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
531 if (!values)
532 return -ENOMEM;
533
534 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
535 if (ret < 0)
536 goto out;
537
538 ret = -ENODATA;
539 for (i = 0; i < nval; i++) {
540 if (!strcmp(values[i], string)) {
541 ret = i;
542 break;
543 }
544 }
545out:
546 kfree(values);
547 return ret;
548}
549EXPORT_SYMBOL_GPL(fwnode_property_match_string);
550
551/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100552 * device_get_next_child_node - Return the next child node handle for a device
553 * @dev: Device to find the next child node for.
554 * @child: Handle to one of the device's child nodes or a null handle.
555 */
556struct fwnode_handle *device_get_next_child_node(struct device *dev,
557 struct fwnode_handle *child)
558{
559 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
560 struct device_node *node;
561
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200562 node = of_get_next_available_child(dev->of_node, to_of_node(child));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100563 if (node)
564 return &node->fwnode;
565 } else if (IS_ENABLED(CONFIG_ACPI)) {
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200566 return acpi_get_next_subnode(dev, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100567 }
568 return NULL;
569}
570EXPORT_SYMBOL_GPL(device_get_next_child_node);
571
572/**
573 * fwnode_handle_put - Drop reference to a device node
574 * @fwnode: Pointer to the device node to drop the reference to.
575 *
576 * This has to be used when terminating device_for_each_child_node() iteration
577 * with break or return to prevent stale device node references from being left
578 * behind.
579 */
580void fwnode_handle_put(struct fwnode_handle *fwnode)
581{
582 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +0200583 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100584}
585EXPORT_SYMBOL_GPL(fwnode_handle_put);
586
587/**
588 * device_get_child_node_count - return the number of child nodes for device
589 * @dev: Device to cound the child nodes for
590 */
591unsigned int device_get_child_node_count(struct device *dev)
592{
593 struct fwnode_handle *child;
594 unsigned int count = 0;
595
596 device_for_each_child_node(dev, child)
597 count++;
598
599 return count;
600}
601EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -0500602
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -0700603bool device_dma_supported(struct device *dev)
604{
605 /* For DT, this is always supported.
606 * For ACPI, this depends on CCA, which
607 * is determined by the acpi_dma_supported().
608 */
609 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
610 return true;
611
612 return acpi_dma_supported(ACPI_COMPANION(dev));
613}
614EXPORT_SYMBOL_GPL(device_dma_supported);
615
616enum dev_dma_attr device_get_dma_attr(struct device *dev)
617{
618 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
619
620 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
621 if (of_dma_is_coherent(dev->of_node))
622 attr = DEV_DMA_COHERENT;
623 else
624 attr = DEV_DMA_NON_COHERENT;
625 } else
626 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
627
628 return attr;
629}
630EXPORT_SYMBOL_GPL(device_get_dma_attr);
631
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500632/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500633 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500634 * @dev: Pointer to the given device
635 *
636 * The function gets phy interface string from property 'phy-mode' or
637 * 'phy-connection-type', and return its index in phy_modes table, or errno in
638 * error case.
639 */
640int device_get_phy_mode(struct device *dev)
641{
642 const char *pm;
643 int err, i;
644
645 err = device_property_read_string(dev, "phy-mode", &pm);
646 if (err < 0)
647 err = device_property_read_string(dev,
648 "phy-connection-type", &pm);
649 if (err < 0)
650 return err;
651
652 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
653 if (!strcasecmp(pm, phy_modes(i)))
654 return i;
655
656 return -ENODEV;
657}
658EXPORT_SYMBOL_GPL(device_get_phy_mode);
659
660static void *device_get_mac_addr(struct device *dev,
661 const char *name, char *addr,
662 int alen)
663{
664 int ret = device_property_read_u8_array(dev, name, addr, alen);
665
Jeremy Linton2f710a32015-08-19 11:46:42 -0500666 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500667 return addr;
668 return NULL;
669}
670
671/**
Jeremy Linton2f710a32015-08-19 11:46:42 -0500672 * device_get_mac_address - Get the MAC for a given device
673 * @dev: Pointer to the device
674 * @addr: Address of buffer to store the MAC in
675 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
676 *
677 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500678 * checked first, because that is supposed to contain to "most recent" MAC
679 * address. If that isn't set, then 'local-mac-address' is checked next,
680 * because that is the default address. If that isn't set, then the obsolete
681 * 'address' is checked, just in case we're using an old device tree.
682 *
683 * Note that the 'address' property is supposed to contain a virtual address of
684 * the register set, but some DTS files have redefined that property to be the
685 * MAC address.
686 *
687 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -0500688 * exist in the firmware tables, but were not updated by the firmware. For
689 * example, the DTS could define 'mac-address' and 'local-mac-address', with
690 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
691 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
692 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500693*/
694void *device_get_mac_address(struct device *dev, char *addr, int alen)
695{
Julien Grall5b902d62015-09-03 23:59:50 +0100696 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500697
Julien Grall5b902d62015-09-03 23:59:50 +0100698 res = device_get_mac_addr(dev, "mac-address", addr, alen);
699 if (res)
700 return res;
701
702 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
703 if (res)
704 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -0500705
706 return device_get_mac_addr(dev, "address", addr, alen);
707}
708EXPORT_SYMBOL(device_get_mac_address);