blob: 8fde824ad41883263496bf59a85ea70a7972e4db [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
Sakari Ailus39e5aee2017-07-21 14:39:35 +030030static inline bool is_pset_node(const 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
Sakari Ailus39e5aee2017-07-21 14:39:35 +030035#define to_pset_node(__fwnode) \
36 ({ \
37 typeof(__fwnode) __to_pset_node_fwnode = __fwnode; \
38 \
39 is_pset_node(__to_pset_node_fwnode) ? \
40 container_of(__to_pset_node_fwnode, \
41 struct property_set, fwnode) : \
42 NULL; \
43 })
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020044
Sakari Ailus39e5aee2017-07-21 14:39:35 +030045static const struct property_entry *
46pset_prop_get(const struct property_set *pset, const char *name)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020047{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080048 const struct property_entry *prop;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020049
50 if (!pset || !pset->properties)
51 return NULL;
52
53 for (prop = pset->properties; prop->name; prop++)
54 if (!strcmp(name, prop->name))
55 return prop;
56
57 return NULL;
58}
59
Sakari Ailus39e5aee2017-07-21 14:39:35 +030060static const void *pset_prop_find(const struct property_set *pset,
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080061 const char *propname, size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020062{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080063 const struct property_entry *prop;
64 const void *pointer;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020065
Andy Shevchenko318a19712015-11-30 17:11:31 +020066 prop = pset_prop_get(pset, propname);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020067 if (!prop)
Andy Shevchenko318a19712015-11-30 17:11:31 +020068 return ERR_PTR(-EINVAL);
Andy Shevchenko66586ba2015-11-30 17:11:32 +020069 if (prop->is_array)
70 pointer = prop->pointer.raw_data;
71 else
72 pointer = &prop->value.raw_data;
Andy Shevchenko318a19712015-11-30 17:11:31 +020073 if (!pointer)
74 return ERR_PTR(-ENODATA);
75 if (length > prop->length)
76 return ERR_PTR(-EOVERFLOW);
77 return pointer;
78}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020079
Sakari Ailus39e5aee2017-07-21 14:39:35 +030080static int pset_prop_read_u8_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +020081 const char *propname,
82 u8 *values, size_t nval)
83{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080084 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020085 size_t length = nval * sizeof(*values);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020086
Andy Shevchenko318a19712015-11-30 17:11:31 +020087 pointer = pset_prop_find(pset, propname, length);
88 if (IS_ERR(pointer))
89 return PTR_ERR(pointer);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020090
Andy Shevchenko318a19712015-11-30 17:11:31 +020091 memcpy(values, pointer, length);
92 return 0;
93}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020094
Sakari Ailus39e5aee2017-07-21 14:39:35 +030095static int pset_prop_read_u16_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +020096 const char *propname,
97 u16 *values, size_t nval)
98{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080099 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200100 size_t length = nval * sizeof(*values);
101
102 pointer = pset_prop_find(pset, propname, length);
103 if (IS_ERR(pointer))
104 return PTR_ERR(pointer);
105
106 memcpy(values, pointer, length);
107 return 0;
108}
109
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300110static int pset_prop_read_u32_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200111 const char *propname,
112 u32 *values, size_t nval)
113{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800114 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200115 size_t length = nval * sizeof(*values);
116
117 pointer = pset_prop_find(pset, propname, length);
118 if (IS_ERR(pointer))
119 return PTR_ERR(pointer);
120
121 memcpy(values, pointer, length);
122 return 0;
123}
124
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300125static int pset_prop_read_u64_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200126 const char *propname,
127 u64 *values, size_t nval)
128{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800129 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200130 size_t length = nval * sizeof(*values);
131
132 pointer = pset_prop_find(pset, propname, length);
133 if (IS_ERR(pointer))
134 return PTR_ERR(pointer);
135
136 memcpy(values, pointer, length);
137 return 0;
138}
139
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300140static int pset_prop_count_elems_of_size(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200141 const char *propname, size_t length)
142{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800143 const struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200144
145 prop = pset_prop_get(pset, propname);
146 if (!prop)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200147 return -EINVAL;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200148
149 return prop->length / length;
150}
151
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300152static int pset_prop_read_string_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200153 const char *propname,
154 const char **strings, size_t nval)
155{
Sakari Ailus0f194992017-03-28 15:22:18 +0300156 const struct property_entry *prop;
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800157 const void *pointer;
Sakari Ailus0f194992017-03-28 15:22:18 +0300158 size_t array_len, length;
159
160 /* Find out the array length. */
161 prop = pset_prop_get(pset, propname);
162 if (!prop)
163 return -EINVAL;
164
165 if (!prop->is_array)
166 /* The array length for a non-array string property is 1. */
167 array_len = 1;
168 else
169 /* Find the length of an array. */
170 array_len = pset_prop_count_elems_of_size(pset, propname,
171 sizeof(const char *));
172
173 /* Return how many there are if strings is NULL. */
174 if (!strings)
175 return array_len;
176
177 array_len = min(nval, array_len);
178 length = array_len * sizeof(*strings);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200179
180 pointer = pset_prop_find(pset, propname, length);
181 if (IS_ERR(pointer))
182 return PTR_ERR(pointer);
183
184 memcpy(strings, pointer, length);
Sakari Ailus0f194992017-03-28 15:22:18 +0300185
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300186 return array_len;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200187}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100188
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300189struct fwnode_handle *dev_fwnode(struct device *dev)
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100190{
191 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
192 &dev->of_node->fwnode : dev->fwnode;
193}
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300194EXPORT_SYMBOL_GPL(dev_fwnode);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100195
Sakari Ailus37081842017-06-06 12:37:37 +0300196static bool pset_fwnode_property_present(struct fwnode_handle *fwnode,
197 const char *propname)
198{
199 return !!pset_prop_get(to_pset_node(fwnode), propname);
200}
201
202static int pset_fwnode_read_int_array(struct fwnode_handle *fwnode,
203 const char *propname,
204 unsigned int elem_size, void *val,
205 size_t nval)
206{
207 struct property_set *node = to_pset_node(fwnode);
208
209 if (!val)
210 return pset_prop_count_elems_of_size(node, propname, elem_size);
211
212 switch (elem_size) {
213 case sizeof(u8):
214 return pset_prop_read_u8_array(node, propname, val, nval);
215 case sizeof(u16):
216 return pset_prop_read_u16_array(node, propname, val, nval);
217 case sizeof(u32):
218 return pset_prop_read_u32_array(node, propname, val, nval);
219 case sizeof(u64):
220 return pset_prop_read_u64_array(node, propname, val, nval);
221 }
222
223 return -ENXIO;
224}
225
226static int pset_fwnode_property_read_string_array(struct fwnode_handle *fwnode,
227 const char *propname,
228 const char **val, size_t nval)
229{
230 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
231 val, nval);
232}
233
234static const struct fwnode_operations pset_fwnode_ops = {
235 .property_present = pset_fwnode_property_present,
236 .property_read_int_array = pset_fwnode_read_int_array,
237 .property_read_string_array = pset_fwnode_property_read_string_array,
238};
239
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100240/**
241 * device_property_present - check if a property of a device is present
242 * @dev: Device whose property is being checked
243 * @propname: Name of the property
244 *
245 * Check if property @propname is present in the device firmware description.
246 */
247bool device_property_present(struct device *dev, const char *propname)
248{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100249 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100250}
251EXPORT_SYMBOL_GPL(device_property_present);
252
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200253/**
254 * fwnode_property_present - check if a property of a firmware node is present
255 * @fwnode: Firmware node whose property to check
256 * @propname: Name of the property
257 */
258bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
259{
260 bool ret;
261
Sakari Ailuse8158b482017-07-11 18:20:20 +0300262 ret = fwnode_call_bool_op(fwnode, property_present, propname);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200263 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
264 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailuse8158b482017-07-11 18:20:20 +0300265 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
Sakari Ailus37081842017-06-06 12:37:37 +0300266 propname);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200267 return ret;
268}
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100269EXPORT_SYMBOL_GPL(fwnode_property_present);
270
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100271/**
272 * device_property_read_u8_array - return a u8 array property of a device
273 * @dev: Device to get the property of
274 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200275 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100276 * @nval: Size of the @val array
277 *
278 * Function reads an array of u8 properties with @propname from the device
279 * firmware description and stores them to @val if found.
280 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200281 * Return: number of values if @val was %NULL,
282 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100283 * %-EINVAL if given arguments are not valid,
284 * %-ENODATA if the property does not have a value,
285 * %-EPROTO if the property is not an array of numbers,
286 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700287 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100288 */
289int device_property_read_u8_array(struct device *dev, const char *propname,
290 u8 *val, size_t nval)
291{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100292 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100293}
294EXPORT_SYMBOL_GPL(device_property_read_u8_array);
295
296/**
297 * device_property_read_u16_array - return a u16 array property of a device
298 * @dev: Device to get the property of
299 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200300 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100301 * @nval: Size of the @val array
302 *
303 * Function reads an array of u16 properties with @propname from the device
304 * firmware description and stores them to @val if found.
305 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200306 * Return: number of values if @val was %NULL,
307 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100308 * %-EINVAL if given arguments are not valid,
309 * %-ENODATA if the property does not have a value,
310 * %-EPROTO if the property is not an array of numbers,
311 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700312 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100313 */
314int device_property_read_u16_array(struct device *dev, const char *propname,
315 u16 *val, size_t nval)
316{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100317 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100318}
319EXPORT_SYMBOL_GPL(device_property_read_u16_array);
320
321/**
322 * device_property_read_u32_array - return a u32 array property of a device
323 * @dev: Device to get the property of
324 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200325 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100326 * @nval: Size of the @val array
327 *
328 * Function reads an array of u32 properties with @propname from the device
329 * firmware description and stores them to @val if found.
330 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200331 * Return: number of values if @val was %NULL,
332 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100333 * %-EINVAL if given arguments are not valid,
334 * %-ENODATA if the property does not have a value,
335 * %-EPROTO if the property is not an array of numbers,
336 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700337 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100338 */
339int device_property_read_u32_array(struct device *dev, const char *propname,
340 u32 *val, size_t nval)
341{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100342 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100343}
344EXPORT_SYMBOL_GPL(device_property_read_u32_array);
345
346/**
347 * device_property_read_u64_array - return a u64 array property of a device
348 * @dev: Device to get the property of
349 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200350 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100351 * @nval: Size of the @val array
352 *
353 * Function reads an array of u64 properties with @propname from the device
354 * firmware description and stores them to @val if found.
355 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200356 * Return: number of values if @val was %NULL,
357 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100358 * %-EINVAL if given arguments are not valid,
359 * %-ENODATA if the property does not have a value,
360 * %-EPROTO if the property is not an array of numbers,
361 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700362 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100363 */
364int device_property_read_u64_array(struct device *dev, const char *propname,
365 u64 *val, size_t nval)
366{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100367 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100368}
369EXPORT_SYMBOL_GPL(device_property_read_u64_array);
370
371/**
372 * device_property_read_string_array - return a string array property of device
373 * @dev: Device to get the property of
374 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200375 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100376 * @nval: Size of the @val array
377 *
378 * Function reads an array of string properties with @propname from the device
379 * firmware description and stores them to @val if found.
380 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300381 * Return: number of values read on success if @val is non-NULL,
382 * number of values available on success if @val is NULL,
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100383 * %-EINVAL if given arguments are not valid,
384 * %-ENODATA if the property does not have a value,
385 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
386 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700387 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100388 */
389int device_property_read_string_array(struct device *dev, const char *propname,
390 const char **val, size_t nval)
391{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100392 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100393}
394EXPORT_SYMBOL_GPL(device_property_read_string_array);
395
396/**
397 * device_property_read_string - return a string property of a device
398 * @dev: Device to get the property of
399 * @propname: Name of the property
400 * @val: The value is stored here
401 *
402 * Function reads property @propname from the device firmware description and
403 * stores the value into @val if found. The value is checked to be a string.
404 *
405 * Return: %0 if the property was found (success),
406 * %-EINVAL if given arguments are not valid,
407 * %-ENODATA if the property does not have a value,
408 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700409 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100410 */
411int device_property_read_string(struct device *dev, const char *propname,
412 const char **val)
413{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100414 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100415}
416EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100417
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300418/**
419 * device_property_match_string - find a string in an array and return index
420 * @dev: Device to get the property of
421 * @propname: Name of the property holding the array
422 * @string: String to look for
423 *
424 * Find a given string in a string array and if it is found return the
425 * index back.
426 *
427 * Return: %0 if the property was found (success),
428 * %-EINVAL if given arguments are not valid,
429 * %-ENODATA if the property does not have a value,
430 * %-EPROTO if the property is not an array of strings,
431 * %-ENXIO if no suitable firmware interface is present.
432 */
433int device_property_match_string(struct device *dev, const char *propname,
434 const char *string)
435{
436 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
437}
438EXPORT_SYMBOL_GPL(device_property_match_string);
439
Sakari Ailus37081842017-06-06 12:37:37 +0300440static int fwnode_property_read_int_array(struct fwnode_handle *fwnode,
441 const char *propname,
442 unsigned int elem_size, void *val,
443 size_t nval)
444{
445 int ret;
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100446
Sakari Ailus37081842017-06-06 12:37:37 +0300447 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
448 elem_size, val, nval);
449 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
450 !IS_ERR_OR_NULL(fwnode->secondary))
451 ret = fwnode_call_int_op(
452 fwnode->secondary, property_read_int_array, propname,
453 elem_size, val, nval);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200454
Sakari Ailus37081842017-06-06 12:37:37 +0300455 return ret;
456}
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200457
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100458/**
459 * fwnode_property_read_u8_array - return a u8 array property of firmware node
460 * @fwnode: Firmware node to get the property of
461 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200462 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100463 * @nval: Size of the @val array
464 *
465 * Read an array of u8 properties with @propname from @fwnode and stores them to
466 * @val if found.
467 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200468 * Return: number of values if @val was %NULL,
469 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100470 * %-EINVAL if given arguments are not valid,
471 * %-ENODATA if the property does not have a value,
472 * %-EPROTO if the property is not an array of numbers,
473 * %-EOVERFLOW if the size of the property is not as expected,
474 * %-ENXIO if no suitable firmware interface is present.
475 */
476int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
477 const char *propname, u8 *val, size_t nval)
478{
Sakari Ailus37081842017-06-06 12:37:37 +0300479 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
480 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100481}
482EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
483
484/**
485 * fwnode_property_read_u16_array - return a u16 array property of firmware node
486 * @fwnode: Firmware node to get the property of
487 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200488 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100489 * @nval: Size of the @val array
490 *
491 * Read an array of u16 properties with @propname from @fwnode and store them to
492 * @val if found.
493 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200494 * Return: number of values if @val was %NULL,
495 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100496 * %-EINVAL if given arguments are not valid,
497 * %-ENODATA if the property does not have a value,
498 * %-EPROTO if the property is not an array of numbers,
499 * %-EOVERFLOW if the size of the property is not as expected,
500 * %-ENXIO if no suitable firmware interface is present.
501 */
502int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
503 const char *propname, u16 *val, size_t nval)
504{
Sakari Ailus37081842017-06-06 12:37:37 +0300505 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
506 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100507}
508EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
509
510/**
511 * fwnode_property_read_u32_array - return a u32 array property of firmware node
512 * @fwnode: Firmware node to get the property of
513 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200514 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100515 * @nval: Size of the @val array
516 *
517 * Read an array of u32 properties with @propname from @fwnode store them to
518 * @val if found.
519 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200520 * Return: number of values if @val was %NULL,
521 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100522 * %-EINVAL if given arguments are not valid,
523 * %-ENODATA if the property does not have a value,
524 * %-EPROTO if the property is not an array of numbers,
525 * %-EOVERFLOW if the size of the property is not as expected,
526 * %-ENXIO if no suitable firmware interface is present.
527 */
528int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
529 const char *propname, u32 *val, size_t nval)
530{
Sakari Ailus37081842017-06-06 12:37:37 +0300531 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
532 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100533}
534EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
535
536/**
537 * fwnode_property_read_u64_array - return a u64 array property firmware node
538 * @fwnode: Firmware node to get the property of
539 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200540 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100541 * @nval: Size of the @val array
542 *
543 * Read an array of u64 properties with @propname from @fwnode and store them to
544 * @val if found.
545 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200546 * Return: number of values if @val was %NULL,
547 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100548 * %-EINVAL if given arguments are not valid,
549 * %-ENODATA if the property does not have a value,
550 * %-EPROTO if the property is not an array of numbers,
551 * %-EOVERFLOW if the size of the property is not as expected,
552 * %-ENXIO if no suitable firmware interface is present.
553 */
554int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
555 const char *propname, u64 *val, size_t nval)
556{
Sakari Ailus37081842017-06-06 12:37:37 +0300557 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
558 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100559}
560EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
561
562/**
563 * fwnode_property_read_string_array - return string array property of a node
564 * @fwnode: Firmware node to get the property of
565 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200566 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100567 * @nval: Size of the @val array
568 *
569 * Read an string list property @propname from the given firmware node and store
570 * them to @val if found.
571 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300572 * Return: number of values read on success if @val is non-NULL,
573 * number of values available on success if @val is NULL,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100574 * %-EINVAL if given arguments are not valid,
575 * %-ENODATA if the property does not have a value,
Sakari Ailus026b8212017-03-28 15:22:17 +0300576 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100577 * %-EOVERFLOW if the size of the property is not as expected,
578 * %-ENXIO if no suitable firmware interface is present.
579 */
580int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
581 const char *propname, const char **val,
582 size_t nval)
583{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200584 int ret;
585
Sakari Ailus37081842017-06-06 12:37:37 +0300586 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
587 val, nval);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200588 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
589 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailus37081842017-06-06 12:37:37 +0300590 ret = fwnode_call_int_op(fwnode->secondary,
591 property_read_string_array, propname,
592 val, nval);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200593 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100594}
595EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
596
597/**
598 * fwnode_property_read_string - return a string property of a firmware node
599 * @fwnode: Firmware node to get the property of
600 * @propname: Name of the property
601 * @val: The value is stored here
602 *
603 * Read property @propname from the given firmware node and store the value into
604 * @val if found. The value is checked to be a string.
605 *
606 * Return: %0 if the property was found (success),
607 * %-EINVAL if given arguments are not valid,
608 * %-ENODATA if the property does not have a value,
609 * %-EPROTO or %-EILSEQ if the property is not a string,
610 * %-ENXIO if no suitable firmware interface is present.
611 */
612int fwnode_property_read_string(struct fwnode_handle *fwnode,
613 const char *propname, const char **val)
614{
Sakari Ailuse4817472017-03-28 15:26:22 +0300615 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200616
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300617 return ret < 0 ? ret : 0;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100618}
619EXPORT_SYMBOL_GPL(fwnode_property_read_string);
620
621/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300622 * fwnode_property_match_string - find a string in an array and return index
623 * @fwnode: Firmware node to get the property of
624 * @propname: Name of the property holding the array
625 * @string: String to look for
626 *
627 * Find a given string in a string array and if it is found return the
628 * index back.
629 *
630 * Return: %0 if the property was found (success),
631 * %-EINVAL if given arguments are not valid,
632 * %-ENODATA if the property does not have a value,
633 * %-EPROTO if the property is not an array of strings,
634 * %-ENXIO if no suitable firmware interface is present.
635 */
636int fwnode_property_match_string(struct fwnode_handle *fwnode,
637 const char *propname, const char *string)
638{
639 const char **values;
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700640 int nval, ret;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300641
642 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
643 if (nval < 0)
644 return nval;
645
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200646 if (nval == 0)
647 return -ENODATA;
648
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300649 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
650 if (!values)
651 return -ENOMEM;
652
653 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
654 if (ret < 0)
655 goto out;
656
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700657 ret = match_string(values, nval, string);
658 if (ret < 0)
659 ret = -ENODATA;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300660out:
661 kfree(values);
662 return ret;
663}
664EXPORT_SYMBOL_GPL(fwnode_property_match_string);
665
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800666static int property_copy_string_array(struct property_entry *dst,
667 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200668{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800669 char **d;
670 size_t nval = src->length / sizeof(*d);
671 int i;
Mika Westerberg13141e12015-11-30 17:11:37 +0200672
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800673 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
674 if (!d)
675 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200676
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800677 for (i = 0; i < nval; i++) {
678 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
679 if (!d[i] && src->pointer.str[i]) {
680 while (--i >= 0)
681 kfree(d[i]);
682 kfree(d);
683 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200684 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200685 }
686
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800687 dst->pointer.raw_data = d;
688 return 0;
Mika Westerberg13141e12015-11-30 17:11:37 +0200689}
690
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800691static int property_entry_copy_data(struct property_entry *dst,
692 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200693{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800694 int error;
Mika Westerberg13141e12015-11-30 17:11:37 +0200695
696 dst->name = kstrdup(src->name, GFP_KERNEL);
697 if (!dst->name)
698 return -ENOMEM;
699
700 if (src->is_array) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800701 if (!src->length) {
702 error = -ENODATA;
703 goto out_free_name;
704 }
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200705
Mika Westerberg13141e12015-11-30 17:11:37 +0200706 if (src->is_string) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800707 error = property_copy_string_array(dst, src);
708 if (error)
709 goto out_free_name;
Mika Westerberg13141e12015-11-30 17:11:37 +0200710 } else {
711 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
712 src->length, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800713 if (!dst->pointer.raw_data) {
714 error = -ENOMEM;
715 goto out_free_name;
716 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200717 }
718 } else if (src->is_string) {
719 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800720 if (!dst->value.str && src->value.str) {
721 error = -ENOMEM;
722 goto out_free_name;
723 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200724 } else {
725 dst->value.raw_data = src->value.raw_data;
726 }
727
728 dst->length = src->length;
729 dst->is_array = src->is_array;
730 dst->is_string = src->is_string;
731
732 return 0;
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800733
734out_free_name:
735 kfree(dst->name);
736 return error;
737}
738
739static void property_entry_free_data(const struct property_entry *p)
740{
741 size_t i, nval;
742
743 if (p->is_array) {
744 if (p->is_string && p->pointer.str) {
745 nval = p->length / sizeof(const char *);
746 for (i = 0; i < nval; i++)
747 kfree(p->pointer.str[i]);
748 }
749 kfree(p->pointer.raw_data);
750 } else if (p->is_string) {
751 kfree(p->value.str);
752 }
753 kfree(p->name);
754}
755
756/**
757 * property_entries_dup - duplicate array of properties
758 * @properties: array of properties to copy
759 *
760 * This function creates a deep copy of the given NULL-terminated array
761 * of property entries.
762 */
763struct property_entry *
764property_entries_dup(const struct property_entry *properties)
765{
766 struct property_entry *p;
767 int i, n = 0;
768
769 while (properties[n].name)
770 n++;
771
772 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
773 if (!p)
774 return ERR_PTR(-ENOMEM);
775
776 for (i = 0; i < n; i++) {
777 int ret = property_entry_copy_data(&p[i], &properties[i]);
778 if (ret) {
779 while (--i >= 0)
780 property_entry_free_data(&p[i]);
781 kfree(p);
782 return ERR_PTR(ret);
783 }
784 }
785
786 return p;
787}
788EXPORT_SYMBOL_GPL(property_entries_dup);
789
790/**
791 * property_entries_free - free previously allocated array of properties
792 * @properties: array of properties to destroy
793 *
794 * This function frees given NULL-terminated array of property entries,
795 * along with their data.
796 */
797void property_entries_free(const struct property_entry *properties)
798{
799 const struct property_entry *p;
800
801 for (p = properties; p->name; p++)
802 property_entry_free_data(p);
803
804 kfree(properties);
805}
806EXPORT_SYMBOL_GPL(property_entries_free);
807
808/**
809 * pset_free_set - releases memory allocated for copied property set
810 * @pset: Property set to release
811 *
812 * Function takes previously copied property set and releases all the
813 * memory allocated to it.
814 */
815static void pset_free_set(struct property_set *pset)
816{
817 if (!pset)
818 return;
819
820 property_entries_free(pset->properties);
821 kfree(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200822}
823
824/**
825 * pset_copy_set - copies property set
826 * @pset: Property set to copy
827 *
828 * This function takes a deep copy of the given property set and returns
829 * pointer to the copy. Call device_free_property_set() to free resources
830 * allocated in this function.
831 *
832 * Return: Pointer to the new property set or error pointer.
833 */
834static struct property_set *pset_copy_set(const struct property_set *pset)
835{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800836 struct property_entry *properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200837 struct property_set *p;
Mika Westerberg13141e12015-11-30 17:11:37 +0200838
839 p = kzalloc(sizeof(*p), GFP_KERNEL);
840 if (!p)
841 return ERR_PTR(-ENOMEM);
842
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800843 properties = property_entries_dup(pset->properties);
844 if (IS_ERR(properties)) {
Mika Westerberg13141e12015-11-30 17:11:37 +0200845 kfree(p);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800846 return ERR_CAST(properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200847 }
848
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800849 p->properties = properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200850 return p;
851}
852
853/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300854 * device_remove_properties - Remove properties from a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200855 * @dev: Device whose properties to remove.
856 *
857 * The function removes properties previously associated to the device
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300858 * secondary firmware node with device_add_properties(). Memory allocated
Mika Westerberg13141e12015-11-30 17:11:37 +0200859 * to the properties will also be released.
860 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300861void device_remove_properties(struct device *dev)
Mika Westerberg13141e12015-11-30 17:11:37 +0200862{
863 struct fwnode_handle *fwnode;
864
865 fwnode = dev_fwnode(dev);
866 if (!fwnode)
867 return;
868 /*
869 * Pick either primary or secondary node depending which one holds
870 * the pset. If there is no real firmware node (ACPI/DT) primary
871 * will hold the pset.
872 */
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200873 if (is_pset_node(fwnode)) {
874 set_primary_fwnode(dev, NULL);
Mika Westerberg13141e12015-11-30 17:11:37 +0200875 pset_free_set(to_pset_node(fwnode));
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200876 } else {
877 fwnode = fwnode->secondary;
878 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
879 set_secondary_fwnode(dev, NULL);
880 pset_free_set(to_pset_node(fwnode));
881 }
882 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200883}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300884EXPORT_SYMBOL_GPL(device_remove_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200885
886/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300887 * device_add_properties - Add a collection of properties to a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200888 * @dev: Device to add properties to.
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300889 * @properties: Collection of properties to add.
Mika Westerberg13141e12015-11-30 17:11:37 +0200890 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300891 * Associate a collection of device properties represented by @properties with
892 * @dev as its secondary firmware node. The function takes a copy of
893 * @properties.
Mika Westerberg13141e12015-11-30 17:11:37 +0200894 */
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800895int device_add_properties(struct device *dev,
896 const struct property_entry *properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200897{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300898 struct property_set *p, pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200899
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300900 if (!properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200901 return -EINVAL;
902
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300903 pset.properties = properties;
904
905 p = pset_copy_set(&pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200906 if (IS_ERR(p))
907 return PTR_ERR(p);
908
Sakari Ailus37081842017-06-06 12:37:37 +0300909 p->fwnode.ops = &pset_fwnode_ops;
Mika Westerberg13141e12015-11-30 17:11:37 +0200910 set_secondary_fwnode(dev, &p->fwnode);
911 return 0;
912}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300913EXPORT_SYMBOL_GPL(device_add_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200914
915/**
Sakari Ailus23387252017-03-28 10:52:26 +0300916 * fwnode_get_next_parent - Iterate to the node's parent
917 * @fwnode: Firmware whose parent is retrieved
918 *
919 * This is like fwnode_get_parent() except that it drops the refcount
920 * on the passed node, making it suitable for iterating through a
921 * node's parents.
922 *
923 * Returns a node pointer with refcount incremented, use
924 * fwnode_handle_node() on it when done.
925 */
926struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
927{
928 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
929
930 fwnode_handle_put(fwnode);
931
932 return parent;
933}
934EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
935
936/**
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300937 * fwnode_get_parent - Return parent firwmare node
938 * @fwnode: Firmware whose parent is retrieved
939 *
940 * Return parent firmware node of the given node if possible or %NULL if no
941 * parent was available.
942 */
943struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode)
944{
Sakari Ailus37081842017-06-06 12:37:37 +0300945 return fwnode_call_ptr_op(fwnode, get_parent);
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300946}
947EXPORT_SYMBOL_GPL(fwnode_get_parent);
948
949/**
Mika Westerberg34055192017-03-28 10:52:18 +0300950 * fwnode_get_next_child_node - Return the next child node handle for a node
951 * @fwnode: Firmware node to find the next child node for.
952 * @child: Handle to one of the node's child nodes or a %NULL handle.
953 */
954struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode,
955 struct fwnode_handle *child)
956{
Sakari Ailus37081842017-06-06 12:37:37 +0300957 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
Mika Westerberg34055192017-03-28 10:52:18 +0300958}
959EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
960
961/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100962 * device_get_next_child_node - Return the next child node handle for a device
963 * @dev: Device to find the next child node for.
964 * @child: Handle to one of the device's child nodes or a null handle.
965 */
966struct fwnode_handle *device_get_next_child_node(struct device *dev,
967 struct fwnode_handle *child)
968{
Mika Westerberg34055192017-03-28 10:52:18 +0300969 struct acpi_device *adev = ACPI_COMPANION(dev);
970 struct fwnode_handle *fwnode = NULL;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100971
Mika Westerberg34055192017-03-28 10:52:18 +0300972 if (dev->of_node)
973 fwnode = &dev->of_node->fwnode;
974 else if (adev)
975 fwnode = acpi_fwnode_handle(adev);
976
977 return fwnode_get_next_child_node(fwnode, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100978}
979EXPORT_SYMBOL_GPL(device_get_next_child_node);
980
981/**
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300982 * fwnode_get_named_child_node - Return first matching named child node handle
983 * @fwnode: Firmware node to find the named child node for.
Adam Thomson613e9722016-06-21 18:50:20 +0100984 * @childname: String to match child node name against.
985 */
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300986struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode,
Adam Thomson613e9722016-06-21 18:50:20 +0100987 const char *childname)
988{
Sakari Ailus37081842017-06-06 12:37:37 +0300989 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
Adam Thomson613e9722016-06-21 18:50:20 +0100990}
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300991EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
992
993/**
994 * device_get_named_child_node - Return first matching named child node handle
995 * @dev: Device to find the named child node for.
996 * @childname: String to match child node name against.
997 */
998struct fwnode_handle *device_get_named_child_node(struct device *dev,
999 const char *childname)
1000{
1001 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1002}
Adam Thomson613e9722016-06-21 18:50:20 +01001003EXPORT_SYMBOL_GPL(device_get_named_child_node);
1004
1005/**
Sakari Ailuse7887c22017-03-28 10:52:22 +03001006 * fwnode_handle_get - Obtain a reference to a device node
1007 * @fwnode: Pointer to the device node to obtain the reference to.
1008 */
1009void fwnode_handle_get(struct fwnode_handle *fwnode)
1010{
Sakari Ailus37081842017-06-06 12:37:37 +03001011 fwnode_call_void_op(fwnode, get);
Sakari Ailuse7887c22017-03-28 10:52:22 +03001012}
1013EXPORT_SYMBOL_GPL(fwnode_handle_get);
1014
1015/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001016 * fwnode_handle_put - Drop reference to a device node
1017 * @fwnode: Pointer to the device node to drop the reference to.
1018 *
1019 * This has to be used when terminating device_for_each_child_node() iteration
1020 * with break or return to prevent stale device node references from being left
1021 * behind.
1022 */
1023void fwnode_handle_put(struct fwnode_handle *fwnode)
1024{
Sakari Ailus37081842017-06-06 12:37:37 +03001025 fwnode_call_void_op(fwnode, put);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001026}
1027EXPORT_SYMBOL_GPL(fwnode_handle_put);
1028
1029/**
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001030 * fwnode_device_is_available - check if a device is available for use
1031 * @fwnode: Pointer to the fwnode of the device.
1032 */
1033bool fwnode_device_is_available(struct fwnode_handle *fwnode)
1034{
Sakari Ailuse8158b482017-07-11 18:20:20 +03001035 return fwnode_call_bool_op(fwnode, device_is_available);
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001036}
1037EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1038
1039/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001040 * device_get_child_node_count - return the number of child nodes for device
1041 * @dev: Device to cound the child nodes for
1042 */
1043unsigned int device_get_child_node_count(struct device *dev)
1044{
1045 struct fwnode_handle *child;
1046 unsigned int count = 0;
1047
1048 device_for_each_child_node(dev, child)
1049 count++;
1050
1051 return count;
1052}
1053EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -05001054
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -07001055bool device_dma_supported(struct device *dev)
1056{
1057 /* For DT, this is always supported.
1058 * For ACPI, this depends on CCA, which
1059 * is determined by the acpi_dma_supported().
1060 */
1061 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1062 return true;
1063
1064 return acpi_dma_supported(ACPI_COMPANION(dev));
1065}
1066EXPORT_SYMBOL_GPL(device_dma_supported);
1067
1068enum dev_dma_attr device_get_dma_attr(struct device *dev)
1069{
1070 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1071
1072 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1073 if (of_dma_is_coherent(dev->of_node))
1074 attr = DEV_DMA_COHERENT;
1075 else
1076 attr = DEV_DMA_NON_COHERENT;
1077 } else
1078 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1079
1080 return attr;
1081}
1082EXPORT_SYMBOL_GPL(device_get_dma_attr);
1083
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001084/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001085 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001086 * @dev: Pointer to the given device
1087 *
1088 * The function gets phy interface string from property 'phy-mode' or
1089 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1090 * error case.
1091 */
1092int device_get_phy_mode(struct device *dev)
1093{
1094 const char *pm;
1095 int err, i;
1096
1097 err = device_property_read_string(dev, "phy-mode", &pm);
1098 if (err < 0)
1099 err = device_property_read_string(dev,
1100 "phy-connection-type", &pm);
1101 if (err < 0)
1102 return err;
1103
1104 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1105 if (!strcasecmp(pm, phy_modes(i)))
1106 return i;
1107
1108 return -ENODEV;
1109}
1110EXPORT_SYMBOL_GPL(device_get_phy_mode);
1111
1112static void *device_get_mac_addr(struct device *dev,
1113 const char *name, char *addr,
1114 int alen)
1115{
1116 int ret = device_property_read_u8_array(dev, name, addr, alen);
1117
Jeremy Linton2f710a32015-08-19 11:46:42 -05001118 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001119 return addr;
1120 return NULL;
1121}
1122
1123/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001124 * device_get_mac_address - Get the MAC for a given device
1125 * @dev: Pointer to the device
1126 * @addr: Address of buffer to store the MAC in
1127 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1128 *
1129 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001130 * checked first, because that is supposed to contain to "most recent" MAC
1131 * address. If that isn't set, then 'local-mac-address' is checked next,
1132 * because that is the default address. If that isn't set, then the obsolete
1133 * 'address' is checked, just in case we're using an old device tree.
1134 *
1135 * Note that the 'address' property is supposed to contain a virtual address of
1136 * the register set, but some DTS files have redefined that property to be the
1137 * MAC address.
1138 *
1139 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -05001140 * exist in the firmware tables, but were not updated by the firmware. For
1141 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1142 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1143 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1144 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001145*/
1146void *device_get_mac_address(struct device *dev, char *addr, int alen)
1147{
Julien Grall5b902d62015-09-03 23:59:50 +01001148 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001149
Julien Grall5b902d62015-09-03 23:59:50 +01001150 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1151 if (res)
1152 return res;
1153
1154 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1155 if (res)
1156 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001157
1158 return device_get_mac_addr(dev, "address", addr, alen);
1159}
1160EXPORT_SYMBOL(device_get_mac_address);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001161
1162/**
1163 * device_graph_get_next_endpoint - Get next endpoint firmware node
1164 * @fwnode: Pointer to the parent firmware node
1165 * @prev: Previous endpoint node or %NULL to get the first
1166 *
1167 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1168 * are available.
1169 */
1170struct fwnode_handle *
1171fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode,
1172 struct fwnode_handle *prev)
1173{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001174 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001175}
1176EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1177
1178/**
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001179 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1180 * @endpoint: Endpoint firmware node of the port
1181 *
1182 * Return: the firmware node of the device the @endpoint belongs to.
1183 */
1184struct fwnode_handle *
1185fwnode_graph_get_port_parent(struct fwnode_handle *endpoint)
1186{
1187 struct fwnode_handle *port, *parent;
1188
1189 port = fwnode_get_parent(endpoint);
1190 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1191
1192 fwnode_handle_put(port);
1193
1194 return parent;
1195}
1196EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1197
1198/**
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001199 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1200 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1201 *
1202 * Extracts firmware node of a remote device the @fwnode points to.
1203 */
1204struct fwnode_handle *
1205fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode)
1206{
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001207 struct fwnode_handle *endpoint, *parent;
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001208
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001209 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1210 parent = fwnode_graph_get_port_parent(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001211
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001212 fwnode_handle_put(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001213
1214 return parent;
1215}
1216EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1217
1218/**
1219 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1220 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1221 *
1222 * Extracts firmware node of a remote port the @fwnode points to.
1223 */
1224struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode)
1225{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001226 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001227}
1228EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1229
1230/**
1231 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1232 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1233 *
1234 * Extracts firmware node of a remote endpoint the @fwnode points to.
1235 */
1236struct fwnode_handle *
1237fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode)
1238{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001239 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001240}
1241EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001242
1243/**
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001244 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1245 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1246 * @port_id: identifier of the parent port node
1247 * @endpoint_id: identifier of the endpoint node
1248 *
1249 * Return: Remote fwnode handle associated with remote endpoint node linked
1250 * to @node. Use fwnode_node_put() on it when done.
1251 */
1252struct fwnode_handle *fwnode_graph_get_remote_node(struct fwnode_handle *fwnode,
1253 u32 port_id, u32 endpoint_id)
1254{
1255 struct fwnode_handle *endpoint = NULL;
1256
1257 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1258 struct fwnode_endpoint fwnode_ep;
1259 struct fwnode_handle *remote;
1260 int ret;
1261
1262 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1263 if (ret < 0)
1264 continue;
1265
1266 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1267 continue;
1268
1269 remote = fwnode_graph_get_remote_port_parent(endpoint);
1270 if (!remote)
1271 return NULL;
1272
1273 return fwnode_device_is_available(remote) ? remote : NULL;
1274 }
1275
1276 return NULL;
1277}
1278EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1279
1280/**
Sakari Ailus2bd54522017-03-28 10:52:25 +03001281 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1282 * @fwnode: pointer to endpoint fwnode_handle
1283 * @endpoint: pointer to the fwnode endpoint data structure
1284 *
1285 * Parse @fwnode representing a graph endpoint node and store the
1286 * information in @endpoint. The caller must hold a reference to
1287 * @fwnode.
1288 */
1289int fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode,
1290 struct fwnode_endpoint *endpoint)
1291{
Sakari Ailus2bd54522017-03-28 10:52:25 +03001292 memset(endpoint, 0, sizeof(*endpoint));
1293
Sakari Ailus3b27d002017-06-06 12:37:38 +03001294 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001295}
1296EXPORT_SYMBOL(fwnode_graph_parse_endpoint);