blob: c69bde8086cc21aa1e79bdaa397959a4d419a3bb [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>
Mika Westerberg07bb80d2017-03-28 10:52:21 +030018#include <linux/of_graph.h>
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020019#include <linux/property.h>
Jeremy Linton4c96b7d2015-08-12 17:06:26 -050020#include <linux/etherdevice.h>
21#include <linux/phy.h>
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020022
Heikki Krogerusf4d05262016-03-29 14:52:23 +030023struct property_set {
24 struct fwnode_handle fwnode;
Dmitry Torokhovbec84da82017-02-02 17:41:25 -080025 const struct property_entry *properties;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030026};
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
Dmitry Torokhovbec84da82017-02-02 17:41:25 -080039static const struct property_entry *pset_prop_get(struct property_set *pset,
40 const char *name)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020041{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -080042 const struct property_entry *prop;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020043
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
Dmitry Torokhovbec84da82017-02-02 17:41:25 -080054static const void *pset_prop_find(struct property_set *pset,
55 const char *propname, size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020056{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -080057 const struct property_entry *prop;
58 const 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{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -080078 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020079 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{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -080093 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020094 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{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -0800108 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200109 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{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -0800123 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200124 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{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -0800137 const struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200138
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{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -0800150 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200151 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{
Dmitry Torokhovbec84da82017-02-02 17:41:25 -0800164 const struct property_entry *prop;
165 const char * const *pointer;
Andy Shevchenko66586ba2015-11-30 17:11:32 +0200166
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 Ailuse44bb0c2017-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 Ailuse44bb0c2017-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,
Sakari Ailus026b8212017-03-28 15:22:17 +0300594 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100595 * %-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
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800687static int property_copy_string_array(struct property_entry *dst,
688 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200689{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800690 char **d;
691 size_t nval = src->length / sizeof(*d);
692 int i;
Mika Westerberg13141e12015-11-30 17:11:37 +0200693
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800694 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
695 if (!d)
696 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200697
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800698 for (i = 0; i < nval; i++) {
699 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
700 if (!d[i] && src->pointer.str[i]) {
701 while (--i >= 0)
702 kfree(d[i]);
703 kfree(d);
704 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200705 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200706 }
707
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800708 dst->pointer.raw_data = d;
709 return 0;
Mika Westerberg13141e12015-11-30 17:11:37 +0200710}
711
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800712static int property_entry_copy_data(struct property_entry *dst,
713 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200714{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800715 int error;
Mika Westerberg13141e12015-11-30 17:11:37 +0200716
717 dst->name = kstrdup(src->name, GFP_KERNEL);
718 if (!dst->name)
719 return -ENOMEM;
720
721 if (src->is_array) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800722 if (!src->length) {
723 error = -ENODATA;
724 goto out_free_name;
725 }
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200726
Mika Westerberg13141e12015-11-30 17:11:37 +0200727 if (src->is_string) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800728 error = property_copy_string_array(dst, src);
729 if (error)
730 goto out_free_name;
Mika Westerberg13141e12015-11-30 17:11:37 +0200731 } else {
732 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
733 src->length, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800734 if (!dst->pointer.raw_data) {
735 error = -ENOMEM;
736 goto out_free_name;
737 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200738 }
739 } else if (src->is_string) {
740 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800741 if (!dst->value.str && src->value.str) {
742 error = -ENOMEM;
743 goto out_free_name;
744 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200745 } else {
746 dst->value.raw_data = src->value.raw_data;
747 }
748
749 dst->length = src->length;
750 dst->is_array = src->is_array;
751 dst->is_string = src->is_string;
752
753 return 0;
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800754
755out_free_name:
756 kfree(dst->name);
757 return error;
758}
759
760static void property_entry_free_data(const struct property_entry *p)
761{
762 size_t i, nval;
763
764 if (p->is_array) {
765 if (p->is_string && p->pointer.str) {
766 nval = p->length / sizeof(const char *);
767 for (i = 0; i < nval; i++)
768 kfree(p->pointer.str[i]);
769 }
770 kfree(p->pointer.raw_data);
771 } else if (p->is_string) {
772 kfree(p->value.str);
773 }
774 kfree(p->name);
775}
776
777/**
778 * property_entries_dup - duplicate array of properties
779 * @properties: array of properties to copy
780 *
781 * This function creates a deep copy of the given NULL-terminated array
782 * of property entries.
783 */
784struct property_entry *
785property_entries_dup(const struct property_entry *properties)
786{
787 struct property_entry *p;
788 int i, n = 0;
789
790 while (properties[n].name)
791 n++;
792
793 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
794 if (!p)
795 return ERR_PTR(-ENOMEM);
796
797 for (i = 0; i < n; i++) {
798 int ret = property_entry_copy_data(&p[i], &properties[i]);
799 if (ret) {
800 while (--i >= 0)
801 property_entry_free_data(&p[i]);
802 kfree(p);
803 return ERR_PTR(ret);
804 }
805 }
806
807 return p;
808}
809EXPORT_SYMBOL_GPL(property_entries_dup);
810
811/**
812 * property_entries_free - free previously allocated array of properties
813 * @properties: array of properties to destroy
814 *
815 * This function frees given NULL-terminated array of property entries,
816 * along with their data.
817 */
818void property_entries_free(const struct property_entry *properties)
819{
820 const struct property_entry *p;
821
822 for (p = properties; p->name; p++)
823 property_entry_free_data(p);
824
825 kfree(properties);
826}
827EXPORT_SYMBOL_GPL(property_entries_free);
828
829/**
830 * pset_free_set - releases memory allocated for copied property set
831 * @pset: Property set to release
832 *
833 * Function takes previously copied property set and releases all the
834 * memory allocated to it.
835 */
836static void pset_free_set(struct property_set *pset)
837{
838 if (!pset)
839 return;
840
841 property_entries_free(pset->properties);
842 kfree(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200843}
844
845/**
846 * pset_copy_set - copies property set
847 * @pset: Property set to copy
848 *
849 * This function takes a deep copy of the given property set and returns
850 * pointer to the copy. Call device_free_property_set() to free resources
851 * allocated in this function.
852 *
853 * Return: Pointer to the new property set or error pointer.
854 */
855static struct property_set *pset_copy_set(const struct property_set *pset)
856{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800857 struct property_entry *properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200858 struct property_set *p;
Mika Westerberg13141e12015-11-30 17:11:37 +0200859
860 p = kzalloc(sizeof(*p), GFP_KERNEL);
861 if (!p)
862 return ERR_PTR(-ENOMEM);
863
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800864 properties = property_entries_dup(pset->properties);
865 if (IS_ERR(properties)) {
Mika Westerberg13141e12015-11-30 17:11:37 +0200866 kfree(p);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800867 return ERR_CAST(properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200868 }
869
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800870 p->properties = properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200871 return p;
872}
873
874/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300875 * device_remove_properties - Remove properties from a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200876 * @dev: Device whose properties to remove.
877 *
878 * The function removes properties previously associated to the device
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300879 * secondary firmware node with device_add_properties(). Memory allocated
Mika Westerberg13141e12015-11-30 17:11:37 +0200880 * to the properties will also be released.
881 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300882void device_remove_properties(struct device *dev)
Mika Westerberg13141e12015-11-30 17:11:37 +0200883{
884 struct fwnode_handle *fwnode;
885
886 fwnode = dev_fwnode(dev);
887 if (!fwnode)
888 return;
889 /*
890 * Pick either primary or secondary node depending which one holds
891 * the pset. If there is no real firmware node (ACPI/DT) primary
892 * will hold the pset.
893 */
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200894 if (is_pset_node(fwnode)) {
895 set_primary_fwnode(dev, NULL);
Mika Westerberg13141e12015-11-30 17:11:37 +0200896 pset_free_set(to_pset_node(fwnode));
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200897 } else {
898 fwnode = fwnode->secondary;
899 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
900 set_secondary_fwnode(dev, NULL);
901 pset_free_set(to_pset_node(fwnode));
902 }
903 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200904}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300905EXPORT_SYMBOL_GPL(device_remove_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200906
907/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300908 * device_add_properties - Add a collection of properties to a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200909 * @dev: Device to add properties to.
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300910 * @properties: Collection of properties to add.
Mika Westerberg13141e12015-11-30 17:11:37 +0200911 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300912 * Associate a collection of device properties represented by @properties with
913 * @dev as its secondary firmware node. The function takes a copy of
914 * @properties.
Mika Westerberg13141e12015-11-30 17:11:37 +0200915 */
Dmitry Torokhovbec84da82017-02-02 17:41:25 -0800916int device_add_properties(struct device *dev,
917 const struct property_entry *properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200918{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300919 struct property_set *p, pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200920
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300921 if (!properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200922 return -EINVAL;
923
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300924 pset.properties = properties;
925
926 p = pset_copy_set(&pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200927 if (IS_ERR(p))
928 return PTR_ERR(p);
929
930 p->fwnode.type = FWNODE_PDATA;
931 set_secondary_fwnode(dev, &p->fwnode);
932 return 0;
933}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300934EXPORT_SYMBOL_GPL(device_add_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200935
936/**
Sakari Ailus23387252017-03-28 10:52:26 +0300937 * fwnode_get_next_parent - Iterate to the node's parent
938 * @fwnode: Firmware whose parent is retrieved
939 *
940 * This is like fwnode_get_parent() except that it drops the refcount
941 * on the passed node, making it suitable for iterating through a
942 * node's parents.
943 *
944 * Returns a node pointer with refcount incremented, use
945 * fwnode_handle_node() on it when done.
946 */
947struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
948{
949 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
950
951 fwnode_handle_put(fwnode);
952
953 return parent;
954}
955EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
956
957/**
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300958 * fwnode_get_parent - Return parent firwmare node
959 * @fwnode: Firmware whose parent is retrieved
960 *
961 * Return parent firmware node of the given node if possible or %NULL if no
962 * parent was available.
963 */
964struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode)
965{
966 struct fwnode_handle *parent = NULL;
967
968 if (is_of_node(fwnode)) {
969 struct device_node *node;
970
971 node = of_get_parent(to_of_node(fwnode));
972 if (node)
973 parent = &node->fwnode;
974 } else if (is_acpi_node(fwnode)) {
975 parent = acpi_node_get_parent(fwnode);
976 }
977
978 return parent;
979}
980EXPORT_SYMBOL_GPL(fwnode_get_parent);
981
982/**
Mika Westerberg34055192017-03-28 10:52:18 +0300983 * fwnode_get_next_child_node - Return the next child node handle for a node
984 * @fwnode: Firmware node to find the next child node for.
985 * @child: Handle to one of the node's child nodes or a %NULL handle.
986 */
987struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode,
988 struct fwnode_handle *child)
989{
990 if (is_of_node(fwnode)) {
991 struct device_node *node;
992
993 node = of_get_next_available_child(to_of_node(fwnode),
994 to_of_node(child));
995 if (node)
996 return &node->fwnode;
997 } else if (is_acpi_node(fwnode)) {
998 return acpi_get_next_subnode(fwnode, child);
999 }
1000
1001 return NULL;
1002}
1003EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
1004
1005/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001006 * device_get_next_child_node - Return the next child node handle for a device
1007 * @dev: Device to find the next child node for.
1008 * @child: Handle to one of the device's child nodes or a null handle.
1009 */
1010struct fwnode_handle *device_get_next_child_node(struct device *dev,
1011 struct fwnode_handle *child)
1012{
Mika Westerberg34055192017-03-28 10:52:18 +03001013 struct acpi_device *adev = ACPI_COMPANION(dev);
1014 struct fwnode_handle *fwnode = NULL;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001015
Mika Westerberg34055192017-03-28 10:52:18 +03001016 if (dev->of_node)
1017 fwnode = &dev->of_node->fwnode;
1018 else if (adev)
1019 fwnode = acpi_fwnode_handle(adev);
1020
1021 return fwnode_get_next_child_node(fwnode, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001022}
1023EXPORT_SYMBOL_GPL(device_get_next_child_node);
1024
1025/**
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001026 * fwnode_get_named_child_node - Return first matching named child node handle
1027 * @fwnode: Firmware node to find the named child node for.
Adam Thomson613e9722016-06-21 18:50:20 +01001028 * @childname: String to match child node name against.
1029 */
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001030struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode,
Adam Thomson613e9722016-06-21 18:50:20 +01001031 const char *childname)
1032{
1033 struct fwnode_handle *child;
1034
1035 /*
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001036 * Find first matching named child node of this fwnode.
Adam Thomson613e9722016-06-21 18:50:20 +01001037 * For ACPI this will be a data only sub-node.
1038 */
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001039 fwnode_for_each_child_node(fwnode, child) {
Adam Thomson613e9722016-06-21 18:50:20 +01001040 if (is_of_node(child)) {
1041 if (!of_node_cmp(to_of_node(child)->name, childname))
1042 return child;
1043 } else if (is_acpi_data_node(child)) {
1044 if (acpi_data_node_match(child, childname))
1045 return child;
1046 }
1047 }
1048
1049 return NULL;
1050}
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001051EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1052
1053/**
1054 * device_get_named_child_node - Return first matching named child node handle
1055 * @dev: Device to find the named child node for.
1056 * @childname: String to match child node name against.
1057 */
1058struct fwnode_handle *device_get_named_child_node(struct device *dev,
1059 const char *childname)
1060{
1061 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1062}
Adam Thomson613e9722016-06-21 18:50:20 +01001063EXPORT_SYMBOL_GPL(device_get_named_child_node);
1064
1065/**
Sakari Ailuse7887c22017-03-28 10:52:22 +03001066 * fwnode_handle_get - Obtain a reference to a device node
1067 * @fwnode: Pointer to the device node to obtain the reference to.
1068 */
1069void fwnode_handle_get(struct fwnode_handle *fwnode)
1070{
1071 if (is_of_node(fwnode))
1072 of_node_get(to_of_node(fwnode));
1073}
1074EXPORT_SYMBOL_GPL(fwnode_handle_get);
1075
1076/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001077 * fwnode_handle_put - Drop reference to a device node
1078 * @fwnode: Pointer to the device node to drop the reference to.
1079 *
1080 * This has to be used when terminating device_for_each_child_node() iteration
1081 * with break or return to prevent stale device node references from being left
1082 * behind.
1083 */
1084void fwnode_handle_put(struct fwnode_handle *fwnode)
1085{
1086 if (is_of_node(fwnode))
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02001087 of_node_put(to_of_node(fwnode));
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001088}
1089EXPORT_SYMBOL_GPL(fwnode_handle_put);
1090
1091/**
1092 * device_get_child_node_count - return the number of child nodes for device
1093 * @dev: Device to cound the child nodes for
1094 */
1095unsigned int device_get_child_node_count(struct device *dev)
1096{
1097 struct fwnode_handle *child;
1098 unsigned int count = 0;
1099
1100 device_for_each_child_node(dev, child)
1101 count++;
1102
1103 return count;
1104}
1105EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -05001106
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -07001107bool device_dma_supported(struct device *dev)
1108{
1109 /* For DT, this is always supported.
1110 * For ACPI, this depends on CCA, which
1111 * is determined by the acpi_dma_supported().
1112 */
1113 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1114 return true;
1115
1116 return acpi_dma_supported(ACPI_COMPANION(dev));
1117}
1118EXPORT_SYMBOL_GPL(device_dma_supported);
1119
1120enum dev_dma_attr device_get_dma_attr(struct device *dev)
1121{
1122 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1123
1124 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1125 if (of_dma_is_coherent(dev->of_node))
1126 attr = DEV_DMA_COHERENT;
1127 else
1128 attr = DEV_DMA_NON_COHERENT;
1129 } else
1130 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1131
1132 return attr;
1133}
1134EXPORT_SYMBOL_GPL(device_get_dma_attr);
1135
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001136/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001137 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001138 * @dev: Pointer to the given device
1139 *
1140 * The function gets phy interface string from property 'phy-mode' or
1141 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1142 * error case.
1143 */
1144int device_get_phy_mode(struct device *dev)
1145{
1146 const char *pm;
1147 int err, i;
1148
1149 err = device_property_read_string(dev, "phy-mode", &pm);
1150 if (err < 0)
1151 err = device_property_read_string(dev,
1152 "phy-connection-type", &pm);
1153 if (err < 0)
1154 return err;
1155
1156 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1157 if (!strcasecmp(pm, phy_modes(i)))
1158 return i;
1159
1160 return -ENODEV;
1161}
1162EXPORT_SYMBOL_GPL(device_get_phy_mode);
1163
1164static void *device_get_mac_addr(struct device *dev,
1165 const char *name, char *addr,
1166 int alen)
1167{
1168 int ret = device_property_read_u8_array(dev, name, addr, alen);
1169
Jeremy Linton2f710a32015-08-19 11:46:42 -05001170 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001171 return addr;
1172 return NULL;
1173}
1174
1175/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001176 * device_get_mac_address - Get the MAC for a given device
1177 * @dev: Pointer to the device
1178 * @addr: Address of buffer to store the MAC in
1179 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1180 *
1181 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001182 * checked first, because that is supposed to contain to "most recent" MAC
1183 * address. If that isn't set, then 'local-mac-address' is checked next,
1184 * because that is the default address. If that isn't set, then the obsolete
1185 * 'address' is checked, just in case we're using an old device tree.
1186 *
1187 * Note that the 'address' property is supposed to contain a virtual address of
1188 * the register set, but some DTS files have redefined that property to be the
1189 * MAC address.
1190 *
1191 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -05001192 * exist in the firmware tables, but were not updated by the firmware. For
1193 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1194 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1195 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1196 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001197*/
1198void *device_get_mac_address(struct device *dev, char *addr, int alen)
1199{
Julien Grall5b902d62015-09-03 23:59:50 +01001200 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001201
Julien Grall5b902d62015-09-03 23:59:50 +01001202 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1203 if (res)
1204 return res;
1205
1206 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1207 if (res)
1208 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001209
1210 return device_get_mac_addr(dev, "address", addr, alen);
1211}
1212EXPORT_SYMBOL(device_get_mac_address);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001213
1214/**
1215 * device_graph_get_next_endpoint - Get next endpoint firmware node
1216 * @fwnode: Pointer to the parent firmware node
1217 * @prev: Previous endpoint node or %NULL to get the first
1218 *
1219 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1220 * are available.
1221 */
1222struct fwnode_handle *
1223fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode,
1224 struct fwnode_handle *prev)
1225{
1226 struct fwnode_handle *endpoint = NULL;
1227
1228 if (is_of_node(fwnode)) {
1229 struct device_node *node;
1230
1231 node = of_graph_get_next_endpoint(to_of_node(fwnode),
1232 to_of_node(prev));
1233
1234 if (node)
1235 endpoint = &node->fwnode;
1236 } else if (is_acpi_node(fwnode)) {
1237 endpoint = acpi_graph_get_next_endpoint(fwnode, prev);
1238 if (IS_ERR(endpoint))
1239 endpoint = NULL;
1240 }
1241
1242 return endpoint;
1243
1244}
1245EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1246
1247/**
1248 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1249 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1250 *
1251 * Extracts firmware node of a remote device the @fwnode points to.
1252 */
1253struct fwnode_handle *
1254fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode)
1255{
1256 struct fwnode_handle *parent = NULL;
1257
1258 if (is_of_node(fwnode)) {
1259 struct device_node *node;
1260
1261 node = of_graph_get_remote_port_parent(to_of_node(fwnode));
1262 if (node)
1263 parent = &node->fwnode;
1264 } else if (is_acpi_node(fwnode)) {
1265 int ret;
1266
1267 ret = acpi_graph_get_remote_endpoint(fwnode, &parent, NULL,
1268 NULL);
1269 if (ret)
1270 return NULL;
1271 }
1272
1273 return parent;
1274}
1275EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1276
1277/**
1278 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1279 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1280 *
1281 * Extracts firmware node of a remote port the @fwnode points to.
1282 */
1283struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode)
1284{
1285 struct fwnode_handle *port = NULL;
1286
1287 if (is_of_node(fwnode)) {
1288 struct device_node *node;
1289
1290 node = of_graph_get_remote_port(to_of_node(fwnode));
1291 if (node)
1292 port = &node->fwnode;
1293 } else if (is_acpi_node(fwnode)) {
1294 int ret;
1295
1296 ret = acpi_graph_get_remote_endpoint(fwnode, NULL, &port, NULL);
1297 if (ret)
1298 return NULL;
1299 }
1300
1301 return port;
1302}
1303EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1304
1305/**
1306 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1307 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1308 *
1309 * Extracts firmware node of a remote endpoint the @fwnode points to.
1310 */
1311struct fwnode_handle *
1312fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode)
1313{
1314 struct fwnode_handle *endpoint = NULL;
1315
1316 if (is_of_node(fwnode)) {
1317 struct device_node *node;
1318
1319 node = of_parse_phandle(to_of_node(fwnode), "remote-endpoint",
1320 0);
1321 if (node)
1322 endpoint = &node->fwnode;
1323 } else if (is_acpi_node(fwnode)) {
1324 int ret;
1325
1326 ret = acpi_graph_get_remote_endpoint(fwnode, NULL, NULL,
1327 &endpoint);
1328 if (ret)
1329 return NULL;
1330 }
1331
1332 return endpoint;
1333}
1334EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001335
1336/**
1337 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1338 * @fwnode: pointer to endpoint fwnode_handle
1339 * @endpoint: pointer to the fwnode endpoint data structure
1340 *
1341 * Parse @fwnode representing a graph endpoint node and store the
1342 * information in @endpoint. The caller must hold a reference to
1343 * @fwnode.
1344 */
1345int fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode,
1346 struct fwnode_endpoint *endpoint)
1347{
1348 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1349
1350 memset(endpoint, 0, sizeof(*endpoint));
1351
1352 endpoint->local_fwnode = fwnode;
1353
1354 if (is_acpi_node(port_fwnode)) {
1355 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1356 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1357 } else {
1358 fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port);
1359 fwnode_property_read_u32(fwnode, "reg", &endpoint->id);
1360 }
1361
1362 fwnode_handle_put(port_fwnode);
1363
1364 return 0;
1365}
1366EXPORT_SYMBOL(fwnode_graph_parse_endpoint);