blob: 857e4d39add6e8fdecf67d2cb999581281439260 [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 Torokhovbec84da2017-02-02 17:41:25 -080025 const struct property_entry *properties;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030026};
27
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030028static const struct fwnode_operations pset_fwnode_ops;
29
Andy Shevchenko61f5e292015-11-30 17:11:30 +020030static inline bool is_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020031{
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030032 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020033}
34
Andy Shevchenko61f5e292015-11-30 17:11:30 +020035static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020036{
Andy Shevchenko61f5e292015-11-30 17:11:30 +020037 return is_pset_node(fwnode) ?
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020038 container_of(fwnode, struct property_set, fwnode) : NULL;
39}
40
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080041static const struct property_entry *pset_prop_get(struct property_set *pset,
42 const char *name)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020043{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080044 const struct property_entry *prop;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020045
46 if (!pset || !pset->properties)
47 return NULL;
48
49 for (prop = pset->properties; prop->name; prop++)
50 if (!strcmp(name, prop->name))
51 return prop;
52
53 return NULL;
54}
55
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080056static const void *pset_prop_find(struct property_set *pset,
57 const char *propname, size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020058{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080059 const struct property_entry *prop;
60 const void *pointer;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020061
Andy Shevchenko318a19712015-11-30 17:11:31 +020062 prop = pset_prop_get(pset, propname);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020063 if (!prop)
Andy Shevchenko318a19712015-11-30 17:11:31 +020064 return ERR_PTR(-EINVAL);
Andy Shevchenko66586ba2015-11-30 17:11:32 +020065 if (prop->is_array)
66 pointer = prop->pointer.raw_data;
67 else
68 pointer = &prop->value.raw_data;
Andy Shevchenko318a19712015-11-30 17:11:31 +020069 if (!pointer)
70 return ERR_PTR(-ENODATA);
71 if (length > prop->length)
72 return ERR_PTR(-EOVERFLOW);
73 return pointer;
74}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020075
Andy Shevchenko318a19712015-11-30 17:11:31 +020076static int pset_prop_read_u8_array(struct property_set *pset,
77 const char *propname,
78 u8 *values, size_t nval)
79{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080080 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020081 size_t length = nval * sizeof(*values);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020082
Andy Shevchenko318a19712015-11-30 17:11:31 +020083 pointer = pset_prop_find(pset, propname, length);
84 if (IS_ERR(pointer))
85 return PTR_ERR(pointer);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020086
Andy Shevchenko318a19712015-11-30 17:11:31 +020087 memcpy(values, pointer, length);
88 return 0;
89}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020090
Andy Shevchenko318a19712015-11-30 17:11:31 +020091static int pset_prop_read_u16_array(struct property_set *pset,
92 const char *propname,
93 u16 *values, size_t nval)
94{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080095 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020096 size_t length = nval * sizeof(*values);
97
98 pointer = pset_prop_find(pset, propname, length);
99 if (IS_ERR(pointer))
100 return PTR_ERR(pointer);
101
102 memcpy(values, pointer, length);
103 return 0;
104}
105
106static int pset_prop_read_u32_array(struct property_set *pset,
107 const char *propname,
108 u32 *values, size_t nval)
109{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800110 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200111 size_t length = nval * sizeof(*values);
112
113 pointer = pset_prop_find(pset, propname, length);
114 if (IS_ERR(pointer))
115 return PTR_ERR(pointer);
116
117 memcpy(values, pointer, length);
118 return 0;
119}
120
121static int pset_prop_read_u64_array(struct property_set *pset,
122 const char *propname,
123 u64 *values, size_t nval)
124{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800125 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200126 size_t length = nval * sizeof(*values);
127
128 pointer = pset_prop_find(pset, propname, length);
129 if (IS_ERR(pointer))
130 return PTR_ERR(pointer);
131
132 memcpy(values, pointer, length);
133 return 0;
134}
135
136static int pset_prop_count_elems_of_size(struct property_set *pset,
137 const char *propname, size_t length)
138{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800139 const struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200140
141 prop = pset_prop_get(pset, propname);
142 if (!prop)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200143 return -EINVAL;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200144
145 return prop->length / length;
146}
147
148static int pset_prop_read_string_array(struct property_set *pset,
149 const char *propname,
150 const char **strings, size_t nval)
151{
Sakari Ailus0f194992017-03-28 15:22:18 +0300152 const struct property_entry *prop;
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800153 const void *pointer;
Sakari Ailus0f194992017-03-28 15:22:18 +0300154 size_t array_len, length;
155
156 /* Find out the array length. */
157 prop = pset_prop_get(pset, propname);
158 if (!prop)
159 return -EINVAL;
160
161 if (!prop->is_array)
162 /* The array length for a non-array string property is 1. */
163 array_len = 1;
164 else
165 /* Find the length of an array. */
166 array_len = pset_prop_count_elems_of_size(pset, propname,
167 sizeof(const char *));
168
169 /* Return how many there are if strings is NULL. */
170 if (!strings)
171 return array_len;
172
173 array_len = min(nval, array_len);
174 length = array_len * sizeof(*strings);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200175
176 pointer = pset_prop_find(pset, propname, length);
177 if (IS_ERR(pointer))
178 return PTR_ERR(pointer);
179
180 memcpy(strings, pointer, length);
Sakari Ailus0f194992017-03-28 15:22:18 +0300181
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300182 return array_len;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200183}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100184
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300185struct fwnode_handle *dev_fwnode(struct device *dev)
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100186{
187 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
188 &dev->of_node->fwnode : dev->fwnode;
189}
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300190EXPORT_SYMBOL_GPL(dev_fwnode);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100191
Sakari Ailus37081842017-06-06 12:37:37 +0300192static bool pset_fwnode_property_present(struct fwnode_handle *fwnode,
193 const char *propname)
194{
195 return !!pset_prop_get(to_pset_node(fwnode), propname);
196}
197
198static int pset_fwnode_read_int_array(struct fwnode_handle *fwnode,
199 const char *propname,
200 unsigned int elem_size, void *val,
201 size_t nval)
202{
203 struct property_set *node = to_pset_node(fwnode);
204
205 if (!val)
206 return pset_prop_count_elems_of_size(node, propname, elem_size);
207
208 switch (elem_size) {
209 case sizeof(u8):
210 return pset_prop_read_u8_array(node, propname, val, nval);
211 case sizeof(u16):
212 return pset_prop_read_u16_array(node, propname, val, nval);
213 case sizeof(u32):
214 return pset_prop_read_u32_array(node, propname, val, nval);
215 case sizeof(u64):
216 return pset_prop_read_u64_array(node, propname, val, nval);
217 }
218
219 return -ENXIO;
220}
221
222static int pset_fwnode_property_read_string_array(struct fwnode_handle *fwnode,
223 const char *propname,
224 const char **val, size_t nval)
225{
226 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
227 val, nval);
228}
229
230static const struct fwnode_operations pset_fwnode_ops = {
231 .property_present = pset_fwnode_property_present,
232 .property_read_int_array = pset_fwnode_read_int_array,
233 .property_read_string_array = pset_fwnode_property_read_string_array,
234};
235
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100236/**
237 * device_property_present - check if a property of a device is present
238 * @dev: Device whose property is being checked
239 * @propname: Name of the property
240 *
241 * Check if property @propname is present in the device firmware description.
242 */
243bool device_property_present(struct device *dev, const char *propname)
244{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100245 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100246}
247EXPORT_SYMBOL_GPL(device_property_present);
248
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200249/**
250 * fwnode_property_present - check if a property of a firmware node is present
251 * @fwnode: Firmware node whose property to check
252 * @propname: Name of the property
253 */
254bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
255{
256 bool ret;
257
Sakari Ailuse8158b482017-07-11 18:20:20 +0300258 ret = fwnode_call_bool_op(fwnode, property_present, propname);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200259 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
260 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailuse8158b482017-07-11 18:20:20 +0300261 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
Sakari Ailus37081842017-06-06 12:37:37 +0300262 propname);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200263 return ret;
264}
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100265EXPORT_SYMBOL_GPL(fwnode_property_present);
266
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100267/**
268 * device_property_read_u8_array - return a u8 array property of a device
269 * @dev: Device to get the property of
270 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200271 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100272 * @nval: Size of the @val array
273 *
274 * Function reads an array of u8 properties with @propname from the device
275 * firmware description and stores them to @val if found.
276 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200277 * Return: number of values if @val was %NULL,
278 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100279 * %-EINVAL if given arguments are not valid,
280 * %-ENODATA if the property does not have a value,
281 * %-EPROTO if the property is not an array of numbers,
282 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700283 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100284 */
285int device_property_read_u8_array(struct device *dev, const char *propname,
286 u8 *val, size_t nval)
287{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100288 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100289}
290EXPORT_SYMBOL_GPL(device_property_read_u8_array);
291
292/**
293 * device_property_read_u16_array - return a u16 array property of a device
294 * @dev: Device to get the property of
295 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200296 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100297 * @nval: Size of the @val array
298 *
299 * Function reads an array of u16 properties with @propname from the device
300 * firmware description and stores them to @val if found.
301 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200302 * Return: number of values if @val was %NULL,
303 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100304 * %-EINVAL if given arguments are not valid,
305 * %-ENODATA if the property does not have a value,
306 * %-EPROTO if the property is not an array of numbers,
307 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700308 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100309 */
310int device_property_read_u16_array(struct device *dev, const char *propname,
311 u16 *val, size_t nval)
312{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100313 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100314}
315EXPORT_SYMBOL_GPL(device_property_read_u16_array);
316
317/**
318 * device_property_read_u32_array - return a u32 array property of a device
319 * @dev: Device to get the property of
320 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200321 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100322 * @nval: Size of the @val array
323 *
324 * Function reads an array of u32 properties with @propname from the device
325 * firmware description and stores them to @val if found.
326 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200327 * Return: number of values if @val was %NULL,
328 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100329 * %-EINVAL if given arguments are not valid,
330 * %-ENODATA if the property does not have a value,
331 * %-EPROTO if the property is not an array of numbers,
332 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700333 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100334 */
335int device_property_read_u32_array(struct device *dev, const char *propname,
336 u32 *val, size_t nval)
337{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100338 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100339}
340EXPORT_SYMBOL_GPL(device_property_read_u32_array);
341
342/**
343 * device_property_read_u64_array - return a u64 array property of a device
344 * @dev: Device to get the property of
345 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200346 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100347 * @nval: Size of the @val array
348 *
349 * Function reads an array of u64 properties with @propname from the device
350 * firmware description and stores them to @val if found.
351 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200352 * Return: number of values if @val was %NULL,
353 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100354 * %-EINVAL if given arguments are not valid,
355 * %-ENODATA if the property does not have a value,
356 * %-EPROTO if the property is not an array of numbers,
357 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700358 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100359 */
360int device_property_read_u64_array(struct device *dev, const char *propname,
361 u64 *val, size_t nval)
362{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100363 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100364}
365EXPORT_SYMBOL_GPL(device_property_read_u64_array);
366
367/**
368 * device_property_read_string_array - return a string array property of device
369 * @dev: Device to get the property of
370 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200371 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100372 * @nval: Size of the @val array
373 *
374 * Function reads an array of string properties with @propname from the device
375 * firmware description and stores them to @val if found.
376 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300377 * Return: number of values read on success if @val is non-NULL,
378 * number of values available on success if @val is NULL,
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100379 * %-EINVAL if given arguments are not valid,
380 * %-ENODATA if the property does not have a value,
381 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
382 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700383 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100384 */
385int device_property_read_string_array(struct device *dev, const char *propname,
386 const char **val, size_t nval)
387{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100388 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100389}
390EXPORT_SYMBOL_GPL(device_property_read_string_array);
391
392/**
393 * device_property_read_string - return a string property of a device
394 * @dev: Device to get the property of
395 * @propname: Name of the property
396 * @val: The value is stored here
397 *
398 * Function reads property @propname from the device firmware description and
399 * stores the value into @val if found. The value is checked to be a string.
400 *
401 * Return: %0 if the property was found (success),
402 * %-EINVAL if given arguments are not valid,
403 * %-ENODATA if the property does not have a value,
404 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700405 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100406 */
407int device_property_read_string(struct device *dev, const char *propname,
408 const char **val)
409{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100410 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100411}
412EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100413
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300414/**
415 * device_property_match_string - find a string in an array and return index
416 * @dev: Device to get the property of
417 * @propname: Name of the property holding the array
418 * @string: String to look for
419 *
420 * Find a given string in a string array and if it is found return the
421 * index back.
422 *
423 * Return: %0 if the property was found (success),
424 * %-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 strings,
427 * %-ENXIO if no suitable firmware interface is present.
428 */
429int device_property_match_string(struct device *dev, const char *propname,
430 const char *string)
431{
432 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
433}
434EXPORT_SYMBOL_GPL(device_property_match_string);
435
Sakari Ailus37081842017-06-06 12:37:37 +0300436static int fwnode_property_read_int_array(struct fwnode_handle *fwnode,
437 const char *propname,
438 unsigned int elem_size, void *val,
439 size_t nval)
440{
441 int ret;
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100442
Sakari Ailus37081842017-06-06 12:37:37 +0300443 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
444 elem_size, val, nval);
445 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
446 !IS_ERR_OR_NULL(fwnode->secondary))
447 ret = fwnode_call_int_op(
448 fwnode->secondary, property_read_int_array, propname,
449 elem_size, val, nval);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200450
Sakari Ailus37081842017-06-06 12:37:37 +0300451 return ret;
452}
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200453
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100454/**
455 * fwnode_property_read_u8_array - return a u8 array property of firmware node
456 * @fwnode: Firmware node to get the property of
457 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200458 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100459 * @nval: Size of the @val array
460 *
461 * Read an array of u8 properties with @propname from @fwnode and stores them to
462 * @val if found.
463 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200464 * Return: number of values if @val was %NULL,
465 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100466 * %-EINVAL if given arguments are not valid,
467 * %-ENODATA if the property does not have a value,
468 * %-EPROTO if the property is not an array of numbers,
469 * %-EOVERFLOW if the size of the property is not as expected,
470 * %-ENXIO if no suitable firmware interface is present.
471 */
472int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
473 const char *propname, u8 *val, size_t nval)
474{
Sakari Ailus37081842017-06-06 12:37:37 +0300475 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
476 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100477}
478EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
479
480/**
481 * fwnode_property_read_u16_array - return a u16 array property of firmware node
482 * @fwnode: Firmware node to get the property of
483 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200484 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100485 * @nval: Size of the @val array
486 *
487 * Read an array of u16 properties with @propname from @fwnode and store them to
488 * @val if found.
489 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200490 * Return: number of values if @val was %NULL,
491 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100492 * %-EINVAL if given arguments are not valid,
493 * %-ENODATA if the property does not have a value,
494 * %-EPROTO if the property is not an array of numbers,
495 * %-EOVERFLOW if the size of the property is not as expected,
496 * %-ENXIO if no suitable firmware interface is present.
497 */
498int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
499 const char *propname, u16 *val, size_t nval)
500{
Sakari Ailus37081842017-06-06 12:37:37 +0300501 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
502 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100503}
504EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
505
506/**
507 * fwnode_property_read_u32_array - return a u32 array property of firmware node
508 * @fwnode: Firmware node to get the property of
509 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200510 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100511 * @nval: Size of the @val array
512 *
513 * Read an array of u32 properties with @propname from @fwnode store them to
514 * @val if found.
515 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200516 * Return: number of values if @val was %NULL,
517 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100518 * %-EINVAL if given arguments are not valid,
519 * %-ENODATA if the property does not have a value,
520 * %-EPROTO if the property is not an array of numbers,
521 * %-EOVERFLOW if the size of the property is not as expected,
522 * %-ENXIO if no suitable firmware interface is present.
523 */
524int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
525 const char *propname, u32 *val, size_t nval)
526{
Sakari Ailus37081842017-06-06 12:37:37 +0300527 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
528 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100529}
530EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
531
532/**
533 * fwnode_property_read_u64_array - return a u64 array property firmware node
534 * @fwnode: Firmware node to get the property of
535 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200536 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100537 * @nval: Size of the @val array
538 *
539 * Read an array of u64 properties with @propname from @fwnode and store them to
540 * @val if found.
541 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200542 * Return: number of values if @val was %NULL,
543 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100544 * %-EINVAL if given arguments are not valid,
545 * %-ENODATA if the property does not have a value,
546 * %-EPROTO if the property is not an array of numbers,
547 * %-EOVERFLOW if the size of the property is not as expected,
548 * %-ENXIO if no suitable firmware interface is present.
549 */
550int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
551 const char *propname, u64 *val, size_t nval)
552{
Sakari Ailus37081842017-06-06 12:37:37 +0300553 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
554 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100555}
556EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
557
558/**
559 * fwnode_property_read_string_array - return string array property of a node
560 * @fwnode: Firmware node to get the property of
561 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200562 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100563 * @nval: Size of the @val array
564 *
565 * Read an string list property @propname from the given firmware node and store
566 * them to @val if found.
567 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300568 * Return: number of values read on success if @val is non-NULL,
569 * number of values available on success if @val is NULL,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100570 * %-EINVAL if given arguments are not valid,
571 * %-ENODATA if the property does not have a value,
Sakari Ailus026b8212017-03-28 15:22:17 +0300572 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100573 * %-EOVERFLOW if the size of the property is not as expected,
574 * %-ENXIO if no suitable firmware interface is present.
575 */
576int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
577 const char *propname, const char **val,
578 size_t nval)
579{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200580 int ret;
581
Sakari Ailus37081842017-06-06 12:37:37 +0300582 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
583 val, nval);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200584 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
585 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailus37081842017-06-06 12:37:37 +0300586 ret = fwnode_call_int_op(fwnode->secondary,
587 property_read_string_array, propname,
588 val, nval);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200589 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100590}
591EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
592
593/**
594 * fwnode_property_read_string - return a string property of a firmware node
595 * @fwnode: Firmware node to get the property of
596 * @propname: Name of the property
597 * @val: The value is stored here
598 *
599 * Read property @propname from the given firmware node and store the value into
600 * @val if found. The value is checked to be a string.
601 *
602 * Return: %0 if the property was found (success),
603 * %-EINVAL if given arguments are not valid,
604 * %-ENODATA if the property does not have a value,
605 * %-EPROTO or %-EILSEQ if the property is not a string,
606 * %-ENXIO if no suitable firmware interface is present.
607 */
608int fwnode_property_read_string(struct fwnode_handle *fwnode,
609 const char *propname, const char **val)
610{
Sakari Ailuse4817472017-03-28 15:26:22 +0300611 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200612
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300613 return ret < 0 ? ret : 0;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100614}
615EXPORT_SYMBOL_GPL(fwnode_property_read_string);
616
617/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300618 * fwnode_property_match_string - find a string in an array and return index
619 * @fwnode: Firmware node to get the property of
620 * @propname: Name of the property holding the array
621 * @string: String to look for
622 *
623 * Find a given string in a string array and if it is found return the
624 * index back.
625 *
626 * Return: %0 if the property was found (success),
627 * %-EINVAL if given arguments are not valid,
628 * %-ENODATA if the property does not have a value,
629 * %-EPROTO if the property is not an array of strings,
630 * %-ENXIO if no suitable firmware interface is present.
631 */
632int fwnode_property_match_string(struct fwnode_handle *fwnode,
633 const char *propname, const char *string)
634{
635 const char **values;
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700636 int nval, ret;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300637
638 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
639 if (nval < 0)
640 return nval;
641
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200642 if (nval == 0)
643 return -ENODATA;
644
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300645 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
646 if (!values)
647 return -ENOMEM;
648
649 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
650 if (ret < 0)
651 goto out;
652
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700653 ret = match_string(values, nval, string);
654 if (ret < 0)
655 ret = -ENODATA;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300656out:
657 kfree(values);
658 return ret;
659}
660EXPORT_SYMBOL_GPL(fwnode_property_match_string);
661
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800662static int property_copy_string_array(struct property_entry *dst,
663 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200664{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800665 char **d;
666 size_t nval = src->length / sizeof(*d);
667 int i;
Mika Westerberg13141e12015-11-30 17:11:37 +0200668
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800669 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
670 if (!d)
671 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200672
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800673 for (i = 0; i < nval; i++) {
674 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
675 if (!d[i] && src->pointer.str[i]) {
676 while (--i >= 0)
677 kfree(d[i]);
678 kfree(d);
679 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200680 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200681 }
682
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800683 dst->pointer.raw_data = d;
684 return 0;
Mika Westerberg13141e12015-11-30 17:11:37 +0200685}
686
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800687static int property_entry_copy_data(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 int error;
Mika Westerberg13141e12015-11-30 17:11:37 +0200691
692 dst->name = kstrdup(src->name, GFP_KERNEL);
693 if (!dst->name)
694 return -ENOMEM;
695
696 if (src->is_array) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800697 if (!src->length) {
698 error = -ENODATA;
699 goto out_free_name;
700 }
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200701
Mika Westerberg13141e12015-11-30 17:11:37 +0200702 if (src->is_string) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800703 error = property_copy_string_array(dst, src);
704 if (error)
705 goto out_free_name;
Mika Westerberg13141e12015-11-30 17:11:37 +0200706 } else {
707 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
708 src->length, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800709 if (!dst->pointer.raw_data) {
710 error = -ENOMEM;
711 goto out_free_name;
712 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200713 }
714 } else if (src->is_string) {
715 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800716 if (!dst->value.str && src->value.str) {
717 error = -ENOMEM;
718 goto out_free_name;
719 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200720 } else {
721 dst->value.raw_data = src->value.raw_data;
722 }
723
724 dst->length = src->length;
725 dst->is_array = src->is_array;
726 dst->is_string = src->is_string;
727
728 return 0;
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800729
730out_free_name:
731 kfree(dst->name);
732 return error;
733}
734
735static void property_entry_free_data(const struct property_entry *p)
736{
737 size_t i, nval;
738
739 if (p->is_array) {
740 if (p->is_string && p->pointer.str) {
741 nval = p->length / sizeof(const char *);
742 for (i = 0; i < nval; i++)
743 kfree(p->pointer.str[i]);
744 }
745 kfree(p->pointer.raw_data);
746 } else if (p->is_string) {
747 kfree(p->value.str);
748 }
749 kfree(p->name);
750}
751
752/**
753 * property_entries_dup - duplicate array of properties
754 * @properties: array of properties to copy
755 *
756 * This function creates a deep copy of the given NULL-terminated array
757 * of property entries.
758 */
759struct property_entry *
760property_entries_dup(const struct property_entry *properties)
761{
762 struct property_entry *p;
763 int i, n = 0;
764
765 while (properties[n].name)
766 n++;
767
768 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
769 if (!p)
770 return ERR_PTR(-ENOMEM);
771
772 for (i = 0; i < n; i++) {
773 int ret = property_entry_copy_data(&p[i], &properties[i]);
774 if (ret) {
775 while (--i >= 0)
776 property_entry_free_data(&p[i]);
777 kfree(p);
778 return ERR_PTR(ret);
779 }
780 }
781
782 return p;
783}
784EXPORT_SYMBOL_GPL(property_entries_dup);
785
786/**
787 * property_entries_free - free previously allocated array of properties
788 * @properties: array of properties to destroy
789 *
790 * This function frees given NULL-terminated array of property entries,
791 * along with their data.
792 */
793void property_entries_free(const struct property_entry *properties)
794{
795 const struct property_entry *p;
796
797 for (p = properties; p->name; p++)
798 property_entry_free_data(p);
799
800 kfree(properties);
801}
802EXPORT_SYMBOL_GPL(property_entries_free);
803
804/**
805 * pset_free_set - releases memory allocated for copied property set
806 * @pset: Property set to release
807 *
808 * Function takes previously copied property set and releases all the
809 * memory allocated to it.
810 */
811static void pset_free_set(struct property_set *pset)
812{
813 if (!pset)
814 return;
815
816 property_entries_free(pset->properties);
817 kfree(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200818}
819
820/**
821 * pset_copy_set - copies property set
822 * @pset: Property set to copy
823 *
824 * This function takes a deep copy of the given property set and returns
825 * pointer to the copy. Call device_free_property_set() to free resources
826 * allocated in this function.
827 *
828 * Return: Pointer to the new property set or error pointer.
829 */
830static struct property_set *pset_copy_set(const struct property_set *pset)
831{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800832 struct property_entry *properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200833 struct property_set *p;
Mika Westerberg13141e12015-11-30 17:11:37 +0200834
835 p = kzalloc(sizeof(*p), GFP_KERNEL);
836 if (!p)
837 return ERR_PTR(-ENOMEM);
838
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800839 properties = property_entries_dup(pset->properties);
840 if (IS_ERR(properties)) {
Mika Westerberg13141e12015-11-30 17:11:37 +0200841 kfree(p);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800842 return ERR_CAST(properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200843 }
844
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800845 p->properties = properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200846 return p;
847}
848
849/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300850 * device_remove_properties - Remove properties from a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200851 * @dev: Device whose properties to remove.
852 *
853 * The function removes properties previously associated to the device
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300854 * secondary firmware node with device_add_properties(). Memory allocated
Mika Westerberg13141e12015-11-30 17:11:37 +0200855 * to the properties will also be released.
856 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300857void device_remove_properties(struct device *dev)
Mika Westerberg13141e12015-11-30 17:11:37 +0200858{
859 struct fwnode_handle *fwnode;
860
861 fwnode = dev_fwnode(dev);
862 if (!fwnode)
863 return;
864 /*
865 * Pick either primary or secondary node depending which one holds
866 * the pset. If there is no real firmware node (ACPI/DT) primary
867 * will hold the pset.
868 */
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200869 if (is_pset_node(fwnode)) {
870 set_primary_fwnode(dev, NULL);
Mika Westerberg13141e12015-11-30 17:11:37 +0200871 pset_free_set(to_pset_node(fwnode));
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200872 } else {
873 fwnode = fwnode->secondary;
874 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
875 set_secondary_fwnode(dev, NULL);
876 pset_free_set(to_pset_node(fwnode));
877 }
878 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200879}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300880EXPORT_SYMBOL_GPL(device_remove_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200881
882/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300883 * device_add_properties - Add a collection of properties to a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200884 * @dev: Device to add properties to.
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300885 * @properties: Collection of properties to add.
Mika Westerberg13141e12015-11-30 17:11:37 +0200886 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300887 * Associate a collection of device properties represented by @properties with
888 * @dev as its secondary firmware node. The function takes a copy of
889 * @properties.
Mika Westerberg13141e12015-11-30 17:11:37 +0200890 */
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800891int device_add_properties(struct device *dev,
892 const struct property_entry *properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200893{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300894 struct property_set *p, pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200895
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300896 if (!properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200897 return -EINVAL;
898
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300899 pset.properties = properties;
900
901 p = pset_copy_set(&pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200902 if (IS_ERR(p))
903 return PTR_ERR(p);
904
Sakari Ailus37081842017-06-06 12:37:37 +0300905 p->fwnode.ops = &pset_fwnode_ops;
Mika Westerberg13141e12015-11-30 17:11:37 +0200906 set_secondary_fwnode(dev, &p->fwnode);
907 return 0;
908}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300909EXPORT_SYMBOL_GPL(device_add_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200910
911/**
Sakari Ailus23387252017-03-28 10:52:26 +0300912 * fwnode_get_next_parent - Iterate to the node's parent
913 * @fwnode: Firmware whose parent is retrieved
914 *
915 * This is like fwnode_get_parent() except that it drops the refcount
916 * on the passed node, making it suitable for iterating through a
917 * node's parents.
918 *
919 * Returns a node pointer with refcount incremented, use
920 * fwnode_handle_node() on it when done.
921 */
922struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
923{
924 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
925
926 fwnode_handle_put(fwnode);
927
928 return parent;
929}
930EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
931
932/**
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300933 * fwnode_get_parent - Return parent firwmare node
934 * @fwnode: Firmware whose parent is retrieved
935 *
936 * Return parent firmware node of the given node if possible or %NULL if no
937 * parent was available.
938 */
939struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode)
940{
Sakari Ailus37081842017-06-06 12:37:37 +0300941 return fwnode_call_ptr_op(fwnode, get_parent);
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300942}
943EXPORT_SYMBOL_GPL(fwnode_get_parent);
944
945/**
Mika Westerberg34055192017-03-28 10:52:18 +0300946 * fwnode_get_next_child_node - Return the next child node handle for a node
947 * @fwnode: Firmware node to find the next child node for.
948 * @child: Handle to one of the node's child nodes or a %NULL handle.
949 */
950struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode,
951 struct fwnode_handle *child)
952{
Sakari Ailus37081842017-06-06 12:37:37 +0300953 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
Mika Westerberg34055192017-03-28 10:52:18 +0300954}
955EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
956
957/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100958 * device_get_next_child_node - Return the next child node handle for a device
959 * @dev: Device to find the next child node for.
960 * @child: Handle to one of the device's child nodes or a null handle.
961 */
962struct fwnode_handle *device_get_next_child_node(struct device *dev,
963 struct fwnode_handle *child)
964{
Mika Westerberg34055192017-03-28 10:52:18 +0300965 struct acpi_device *adev = ACPI_COMPANION(dev);
966 struct fwnode_handle *fwnode = NULL;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100967
Mika Westerberg34055192017-03-28 10:52:18 +0300968 if (dev->of_node)
969 fwnode = &dev->of_node->fwnode;
970 else if (adev)
971 fwnode = acpi_fwnode_handle(adev);
972
973 return fwnode_get_next_child_node(fwnode, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100974}
975EXPORT_SYMBOL_GPL(device_get_next_child_node);
976
977/**
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300978 * fwnode_get_named_child_node - Return first matching named child node handle
979 * @fwnode: Firmware node to find the named child node for.
Adam Thomson613e9722016-06-21 18:50:20 +0100980 * @childname: String to match child node name against.
981 */
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300982struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode,
Adam Thomson613e9722016-06-21 18:50:20 +0100983 const char *childname)
984{
Sakari Ailus37081842017-06-06 12:37:37 +0300985 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
Adam Thomson613e9722016-06-21 18:50:20 +0100986}
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300987EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
988
989/**
990 * device_get_named_child_node - Return first matching named child node handle
991 * @dev: Device to find the named child node for.
992 * @childname: String to match child node name against.
993 */
994struct fwnode_handle *device_get_named_child_node(struct device *dev,
995 const char *childname)
996{
997 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
998}
Adam Thomson613e9722016-06-21 18:50:20 +0100999EXPORT_SYMBOL_GPL(device_get_named_child_node);
1000
1001/**
Sakari Ailuse7887c22017-03-28 10:52:22 +03001002 * fwnode_handle_get - Obtain a reference to a device node
1003 * @fwnode: Pointer to the device node to obtain the reference to.
1004 */
1005void fwnode_handle_get(struct fwnode_handle *fwnode)
1006{
Sakari Ailus37081842017-06-06 12:37:37 +03001007 fwnode_call_void_op(fwnode, get);
Sakari Ailuse7887c22017-03-28 10:52:22 +03001008}
1009EXPORT_SYMBOL_GPL(fwnode_handle_get);
1010
1011/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001012 * fwnode_handle_put - Drop reference to a device node
1013 * @fwnode: Pointer to the device node to drop the reference to.
1014 *
1015 * This has to be used when terminating device_for_each_child_node() iteration
1016 * with break or return to prevent stale device node references from being left
1017 * behind.
1018 */
1019void fwnode_handle_put(struct fwnode_handle *fwnode)
1020{
Sakari Ailus37081842017-06-06 12:37:37 +03001021 fwnode_call_void_op(fwnode, put);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001022}
1023EXPORT_SYMBOL_GPL(fwnode_handle_put);
1024
1025/**
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001026 * fwnode_device_is_available - check if a device is available for use
1027 * @fwnode: Pointer to the fwnode of the device.
1028 */
1029bool fwnode_device_is_available(struct fwnode_handle *fwnode)
1030{
Sakari Ailuse8158b482017-07-11 18:20:20 +03001031 return fwnode_call_bool_op(fwnode, device_is_available);
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001032}
1033EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1034
1035/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001036 * device_get_child_node_count - return the number of child nodes for device
1037 * @dev: Device to cound the child nodes for
1038 */
1039unsigned int device_get_child_node_count(struct device *dev)
1040{
1041 struct fwnode_handle *child;
1042 unsigned int count = 0;
1043
1044 device_for_each_child_node(dev, child)
1045 count++;
1046
1047 return count;
1048}
1049EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -05001050
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -07001051bool device_dma_supported(struct device *dev)
1052{
1053 /* For DT, this is always supported.
1054 * For ACPI, this depends on CCA, which
1055 * is determined by the acpi_dma_supported().
1056 */
1057 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1058 return true;
1059
1060 return acpi_dma_supported(ACPI_COMPANION(dev));
1061}
1062EXPORT_SYMBOL_GPL(device_dma_supported);
1063
1064enum dev_dma_attr device_get_dma_attr(struct device *dev)
1065{
1066 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1067
1068 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1069 if (of_dma_is_coherent(dev->of_node))
1070 attr = DEV_DMA_COHERENT;
1071 else
1072 attr = DEV_DMA_NON_COHERENT;
1073 } else
1074 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1075
1076 return attr;
1077}
1078EXPORT_SYMBOL_GPL(device_get_dma_attr);
1079
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001080/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001081 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001082 * @dev: Pointer to the given device
1083 *
1084 * The function gets phy interface string from property 'phy-mode' or
1085 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1086 * error case.
1087 */
1088int device_get_phy_mode(struct device *dev)
1089{
1090 const char *pm;
1091 int err, i;
1092
1093 err = device_property_read_string(dev, "phy-mode", &pm);
1094 if (err < 0)
1095 err = device_property_read_string(dev,
1096 "phy-connection-type", &pm);
1097 if (err < 0)
1098 return err;
1099
1100 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1101 if (!strcasecmp(pm, phy_modes(i)))
1102 return i;
1103
1104 return -ENODEV;
1105}
1106EXPORT_SYMBOL_GPL(device_get_phy_mode);
1107
1108static void *device_get_mac_addr(struct device *dev,
1109 const char *name, char *addr,
1110 int alen)
1111{
1112 int ret = device_property_read_u8_array(dev, name, addr, alen);
1113
Jeremy Linton2f710a32015-08-19 11:46:42 -05001114 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001115 return addr;
1116 return NULL;
1117}
1118
1119/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001120 * device_get_mac_address - Get the MAC for a given device
1121 * @dev: Pointer to the device
1122 * @addr: Address of buffer to store the MAC in
1123 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1124 *
1125 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001126 * checked first, because that is supposed to contain to "most recent" MAC
1127 * address. If that isn't set, then 'local-mac-address' is checked next,
1128 * because that is the default address. If that isn't set, then the obsolete
1129 * 'address' is checked, just in case we're using an old device tree.
1130 *
1131 * Note that the 'address' property is supposed to contain a virtual address of
1132 * the register set, but some DTS files have redefined that property to be the
1133 * MAC address.
1134 *
1135 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -05001136 * exist in the firmware tables, but were not updated by the firmware. For
1137 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1138 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1139 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1140 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001141*/
1142void *device_get_mac_address(struct device *dev, char *addr, int alen)
1143{
Julien Grall5b902d62015-09-03 23:59:50 +01001144 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001145
Julien Grall5b902d62015-09-03 23:59:50 +01001146 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1147 if (res)
1148 return res;
1149
1150 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1151 if (res)
1152 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001153
1154 return device_get_mac_addr(dev, "address", addr, alen);
1155}
1156EXPORT_SYMBOL(device_get_mac_address);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001157
1158/**
1159 * device_graph_get_next_endpoint - Get next endpoint firmware node
1160 * @fwnode: Pointer to the parent firmware node
1161 * @prev: Previous endpoint node or %NULL to get the first
1162 *
1163 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1164 * are available.
1165 */
1166struct fwnode_handle *
1167fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode,
1168 struct fwnode_handle *prev)
1169{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001170 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001171}
1172EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1173
1174/**
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001175 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1176 * @endpoint: Endpoint firmware node of the port
1177 *
1178 * Return: the firmware node of the device the @endpoint belongs to.
1179 */
1180struct fwnode_handle *
1181fwnode_graph_get_port_parent(struct fwnode_handle *endpoint)
1182{
1183 struct fwnode_handle *port, *parent;
1184
1185 port = fwnode_get_parent(endpoint);
1186 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1187
1188 fwnode_handle_put(port);
1189
1190 return parent;
1191}
1192EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1193
1194/**
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001195 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1196 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1197 *
1198 * Extracts firmware node of a remote device the @fwnode points to.
1199 */
1200struct fwnode_handle *
1201fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode)
1202{
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001203 struct fwnode_handle *endpoint, *parent;
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001204
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001205 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1206 parent = fwnode_graph_get_port_parent(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001207
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001208 fwnode_handle_put(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001209
1210 return parent;
1211}
1212EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1213
1214/**
1215 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1216 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1217 *
1218 * Extracts firmware node of a remote port the @fwnode points to.
1219 */
1220struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode)
1221{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001222 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001223}
1224EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1225
1226/**
1227 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1228 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1229 *
1230 * Extracts firmware node of a remote endpoint the @fwnode points to.
1231 */
1232struct fwnode_handle *
1233fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode)
1234{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001235 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001236}
1237EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001238
1239/**
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001240 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1241 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1242 * @port_id: identifier of the parent port node
1243 * @endpoint_id: identifier of the endpoint node
1244 *
1245 * Return: Remote fwnode handle associated with remote endpoint node linked
1246 * to @node. Use fwnode_node_put() on it when done.
1247 */
1248struct fwnode_handle *fwnode_graph_get_remote_node(struct fwnode_handle *fwnode,
1249 u32 port_id, u32 endpoint_id)
1250{
1251 struct fwnode_handle *endpoint = NULL;
1252
1253 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1254 struct fwnode_endpoint fwnode_ep;
1255 struct fwnode_handle *remote;
1256 int ret;
1257
1258 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1259 if (ret < 0)
1260 continue;
1261
1262 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1263 continue;
1264
1265 remote = fwnode_graph_get_remote_port_parent(endpoint);
1266 if (!remote)
1267 return NULL;
1268
1269 return fwnode_device_is_available(remote) ? remote : NULL;
1270 }
1271
1272 return NULL;
1273}
1274EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1275
1276/**
Sakari Ailus2bd54522017-03-28 10:52:25 +03001277 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1278 * @fwnode: pointer to endpoint fwnode_handle
1279 * @endpoint: pointer to the fwnode endpoint data structure
1280 *
1281 * Parse @fwnode representing a graph endpoint node and store the
1282 * information in @endpoint. The caller must hold a reference to
1283 * @fwnode.
1284 */
1285int fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode,
1286 struct fwnode_endpoint *endpoint)
1287{
Sakari Ailus2bd54522017-03-28 10:52:25 +03001288 memset(endpoint, 0, sizeof(*endpoint));
1289
Sakari Ailus3b27d002017-06-06 12:37:38 +03001290 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001291}
1292EXPORT_SYMBOL(fwnode_graph_parse_endpoint);