blob: edf02c1b5845968bb94844f51057c6f414b83d64 [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
Andy Shevchenko61f5e292015-11-30 17:11:30 +020028static inline bool is_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020029{
Heikki Krogerus0224a4a2016-04-27 14:04:20 +030030 return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020031}
32
Andy Shevchenko61f5e292015-11-30 17:11:30 +020033static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020034{
Andy Shevchenko61f5e292015-11-30 17:11:30 +020035 return is_pset_node(fwnode) ?
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020036 container_of(fwnode, struct property_set, fwnode) : NULL;
37}
38
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080039static const struct property_entry *pset_prop_get(struct property_set *pset,
40 const char *name)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020041{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080042 const struct property_entry *prop;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020043
44 if (!pset || !pset->properties)
45 return NULL;
46
47 for (prop = pset->properties; prop->name; prop++)
48 if (!strcmp(name, prop->name))
49 return prop;
50
51 return NULL;
52}
53
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080054static const void *pset_prop_find(struct property_set *pset,
55 const char *propname, size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020056{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080057 const struct property_entry *prop;
58 const void *pointer;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020059
Andy Shevchenko318a19712015-11-30 17:11:31 +020060 prop = pset_prop_get(pset, propname);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020061 if (!prop)
Andy Shevchenko318a19712015-11-30 17:11:31 +020062 return ERR_PTR(-EINVAL);
Andy Shevchenko66586ba2015-11-30 17:11:32 +020063 if (prop->is_array)
64 pointer = prop->pointer.raw_data;
65 else
66 pointer = &prop->value.raw_data;
Andy Shevchenko318a19712015-11-30 17:11:31 +020067 if (!pointer)
68 return ERR_PTR(-ENODATA);
69 if (length > prop->length)
70 return ERR_PTR(-EOVERFLOW);
71 return pointer;
72}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020073
Andy Shevchenko318a19712015-11-30 17:11:31 +020074static int pset_prop_read_u8_array(struct property_set *pset,
75 const char *propname,
76 u8 *values, size_t nval)
77{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080078 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020079 size_t length = nval * sizeof(*values);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020080
Andy Shevchenko318a19712015-11-30 17:11:31 +020081 pointer = pset_prop_find(pset, propname, length);
82 if (IS_ERR(pointer))
83 return PTR_ERR(pointer);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020084
Andy Shevchenko318a19712015-11-30 17:11:31 +020085 memcpy(values, pointer, length);
86 return 0;
87}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020088
Andy Shevchenko318a19712015-11-30 17:11:31 +020089static int pset_prop_read_u16_array(struct property_set *pset,
90 const char *propname,
91 u16 *values, size_t nval)
92{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080093 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020094 size_t length = nval * sizeof(*values);
95
96 pointer = pset_prop_find(pset, propname, length);
97 if (IS_ERR(pointer))
98 return PTR_ERR(pointer);
99
100 memcpy(values, pointer, length);
101 return 0;
102}
103
104static int pset_prop_read_u32_array(struct property_set *pset,
105 const char *propname,
106 u32 *values, size_t nval)
107{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800108 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200109 size_t length = nval * sizeof(*values);
110
111 pointer = pset_prop_find(pset, propname, length);
112 if (IS_ERR(pointer))
113 return PTR_ERR(pointer);
114
115 memcpy(values, pointer, length);
116 return 0;
117}
118
119static int pset_prop_read_u64_array(struct property_set *pset,
120 const char *propname,
121 u64 *values, size_t nval)
122{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800123 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200124 size_t length = nval * sizeof(*values);
125
126 pointer = pset_prop_find(pset, propname, length);
127 if (IS_ERR(pointer))
128 return PTR_ERR(pointer);
129
130 memcpy(values, pointer, length);
131 return 0;
132}
133
134static int pset_prop_count_elems_of_size(struct property_set *pset,
135 const char *propname, size_t length)
136{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800137 const struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200138
139 prop = pset_prop_get(pset, propname);
140 if (!prop)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200141 return -EINVAL;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200142
143 return prop->length / length;
144}
145
146static int pset_prop_read_string_array(struct property_set *pset,
147 const char *propname,
148 const char **strings, size_t nval)
149{
Sakari Ailus0f194992017-03-28 15:22:18 +0300150 const struct property_entry *prop;
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800151 const void *pointer;
Sakari Ailus0f194992017-03-28 15:22:18 +0300152 size_t array_len, length;
153
154 /* Find out the array length. */
155 prop = pset_prop_get(pset, propname);
156 if (!prop)
157 return -EINVAL;
158
159 if (!prop->is_array)
160 /* The array length for a non-array string property is 1. */
161 array_len = 1;
162 else
163 /* Find the length of an array. */
164 array_len = pset_prop_count_elems_of_size(pset, propname,
165 sizeof(const char *));
166
167 /* Return how many there are if strings is NULL. */
168 if (!strings)
169 return array_len;
170
171 array_len = min(nval, array_len);
172 length = array_len * sizeof(*strings);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200173
174 pointer = pset_prop_find(pset, propname, length);
175 if (IS_ERR(pointer))
176 return PTR_ERR(pointer);
177
178 memcpy(strings, pointer, length);
Sakari Ailus0f194992017-03-28 15:22:18 +0300179
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300180 return array_len;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200181}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100182
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300183struct fwnode_handle *dev_fwnode(struct device *dev)
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100184{
185 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
186 &dev->of_node->fwnode : dev->fwnode;
187}
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300188EXPORT_SYMBOL_GPL(dev_fwnode);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100189
Sakari Ailus37081842017-06-06 12:37:37 +0300190static bool pset_fwnode_property_present(struct fwnode_handle *fwnode,
191 const char *propname)
192{
193 return !!pset_prop_get(to_pset_node(fwnode), propname);
194}
195
196static int pset_fwnode_read_int_array(struct fwnode_handle *fwnode,
197 const char *propname,
198 unsigned int elem_size, void *val,
199 size_t nval)
200{
201 struct property_set *node = to_pset_node(fwnode);
202
203 if (!val)
204 return pset_prop_count_elems_of_size(node, propname, elem_size);
205
206 switch (elem_size) {
207 case sizeof(u8):
208 return pset_prop_read_u8_array(node, propname, val, nval);
209 case sizeof(u16):
210 return pset_prop_read_u16_array(node, propname, val, nval);
211 case sizeof(u32):
212 return pset_prop_read_u32_array(node, propname, val, nval);
213 case sizeof(u64):
214 return pset_prop_read_u64_array(node, propname, val, nval);
215 }
216
217 return -ENXIO;
218}
219
220static int pset_fwnode_property_read_string_array(struct fwnode_handle *fwnode,
221 const char *propname,
222 const char **val, size_t nval)
223{
224 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
225 val, nval);
226}
227
228static const struct fwnode_operations pset_fwnode_ops = {
229 .property_present = pset_fwnode_property_present,
230 .property_read_int_array = pset_fwnode_read_int_array,
231 .property_read_string_array = pset_fwnode_property_read_string_array,
232};
233
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100234/**
235 * device_property_present - check if a property of a device is present
236 * @dev: Device whose property is being checked
237 * @propname: Name of the property
238 *
239 * Check if property @propname is present in the device firmware description.
240 */
241bool device_property_present(struct device *dev, const char *propname)
242{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100243 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100244}
245EXPORT_SYMBOL_GPL(device_property_present);
246
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200247/**
248 * fwnode_property_present - check if a property of a firmware node is present
249 * @fwnode: Firmware node whose property to check
250 * @propname: Name of the property
251 */
252bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname)
253{
254 bool ret;
255
Sakari Ailuse8158b42017-07-11 18:20:20 +0300256 ret = fwnode_call_bool_op(fwnode, property_present, propname);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200257 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
258 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailuse8158b42017-07-11 18:20:20 +0300259 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
Sakari Ailus37081842017-06-06 12:37:37 +0300260 propname);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200261 return ret;
262}
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100263EXPORT_SYMBOL_GPL(fwnode_property_present);
264
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100265/**
266 * device_property_read_u8_array - return a u8 array property of a device
267 * @dev: Device to get the property of
268 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200269 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100270 * @nval: Size of the @val array
271 *
272 * Function reads an array of u8 properties with @propname from the device
273 * firmware description and stores them to @val if found.
274 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200275 * Return: number of values if @val was %NULL,
276 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100277 * %-EINVAL if given arguments are not valid,
278 * %-ENODATA if the property does not have a value,
279 * %-EPROTO if the property is not an array of numbers,
280 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700281 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100282 */
283int device_property_read_u8_array(struct device *dev, const char *propname,
284 u8 *val, size_t nval)
285{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100286 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100287}
288EXPORT_SYMBOL_GPL(device_property_read_u8_array);
289
290/**
291 * device_property_read_u16_array - return a u16 array property of a device
292 * @dev: Device to get the property of
293 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200294 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100295 * @nval: Size of the @val array
296 *
297 * Function reads an array of u16 properties with @propname from the device
298 * firmware description and stores them to @val if found.
299 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200300 * Return: number of values if @val was %NULL,
301 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100302 * %-EINVAL if given arguments are not valid,
303 * %-ENODATA if the property does not have a value,
304 * %-EPROTO if the property is not an array of numbers,
305 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700306 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100307 */
308int device_property_read_u16_array(struct device *dev, const char *propname,
309 u16 *val, size_t nval)
310{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100311 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100312}
313EXPORT_SYMBOL_GPL(device_property_read_u16_array);
314
315/**
316 * device_property_read_u32_array - return a u32 array property of a device
317 * @dev: Device to get the property of
318 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200319 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100320 * @nval: Size of the @val array
321 *
322 * Function reads an array of u32 properties with @propname from the device
323 * firmware description and stores them to @val if found.
324 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200325 * Return: number of values if @val was %NULL,
326 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100327 * %-EINVAL if given arguments are not valid,
328 * %-ENODATA if the property does not have a value,
329 * %-EPROTO if the property is not an array of numbers,
330 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700331 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100332 */
333int device_property_read_u32_array(struct device *dev, const char *propname,
334 u32 *val, size_t nval)
335{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100336 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100337}
338EXPORT_SYMBOL_GPL(device_property_read_u32_array);
339
340/**
341 * device_property_read_u64_array - return a u64 array property of a device
342 * @dev: Device to get the property of
343 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200344 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100345 * @nval: Size of the @val array
346 *
347 * Function reads an array of u64 properties with @propname from the device
348 * firmware description and stores them to @val if found.
349 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200350 * Return: number of values if @val was %NULL,
351 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100352 * %-EINVAL if given arguments are not valid,
353 * %-ENODATA if the property does not have a value,
354 * %-EPROTO if the property is not an array of numbers,
355 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700356 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100357 */
358int device_property_read_u64_array(struct device *dev, const char *propname,
359 u64 *val, size_t nval)
360{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100361 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100362}
363EXPORT_SYMBOL_GPL(device_property_read_u64_array);
364
365/**
366 * device_property_read_string_array - return a string array property of device
367 * @dev: Device to get the property of
368 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200369 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100370 * @nval: Size of the @val array
371 *
372 * Function reads an array of string properties with @propname from the device
373 * firmware description and stores them to @val if found.
374 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300375 * Return: number of values read on success if @val is non-NULL,
376 * number of values available on success if @val is NULL,
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100377 * %-EINVAL if given arguments are not valid,
378 * %-ENODATA if the property does not have a value,
379 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
380 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700381 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100382 */
383int device_property_read_string_array(struct device *dev, const char *propname,
384 const char **val, size_t nval)
385{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100386 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100387}
388EXPORT_SYMBOL_GPL(device_property_read_string_array);
389
390/**
391 * device_property_read_string - return a string property of a device
392 * @dev: Device to get the property of
393 * @propname: Name of the property
394 * @val: The value is stored here
395 *
396 * Function reads property @propname from the device firmware description and
397 * stores the value into @val if found. The value is checked to be a string.
398 *
399 * Return: %0 if the property was found (success),
400 * %-EINVAL if given arguments are not valid,
401 * %-ENODATA if the property does not have a value,
402 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700403 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100404 */
405int device_property_read_string(struct device *dev, const char *propname,
406 const char **val)
407{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100408 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100409}
410EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100411
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300412/**
413 * device_property_match_string - find a string in an array and return index
414 * @dev: Device to get the property of
415 * @propname: Name of the property holding the array
416 * @string: String to look for
417 *
418 * Find a given string in a string array and if it is found return the
419 * index back.
420 *
421 * Return: %0 if the property was found (success),
422 * %-EINVAL if given arguments are not valid,
423 * %-ENODATA if the property does not have a value,
424 * %-EPROTO if the property is not an array of strings,
425 * %-ENXIO if no suitable firmware interface is present.
426 */
427int device_property_match_string(struct device *dev, const char *propname,
428 const char *string)
429{
430 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
431}
432EXPORT_SYMBOL_GPL(device_property_match_string);
433
Sakari Ailus37081842017-06-06 12:37:37 +0300434static int fwnode_property_read_int_array(struct fwnode_handle *fwnode,
435 const char *propname,
436 unsigned int elem_size, void *val,
437 size_t nval)
438{
439 int ret;
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100440
Sakari Ailus37081842017-06-06 12:37:37 +0300441 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
442 elem_size, val, nval);
443 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
444 !IS_ERR_OR_NULL(fwnode->secondary))
445 ret = fwnode_call_int_op(
446 fwnode->secondary, property_read_int_array, propname,
447 elem_size, val, nval);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200448
Sakari Ailus37081842017-06-06 12:37:37 +0300449 return ret;
450}
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200451
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100452/**
453 * fwnode_property_read_u8_array - return a u8 array property of firmware node
454 * @fwnode: Firmware node to get the property of
455 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200456 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100457 * @nval: Size of the @val array
458 *
459 * Read an array of u8 properties with @propname from @fwnode and stores them to
460 * @val if found.
461 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200462 * Return: number of values if @val was %NULL,
463 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100464 * %-EINVAL if given arguments are not valid,
465 * %-ENODATA if the property does not have a value,
466 * %-EPROTO if the property is not an array of numbers,
467 * %-EOVERFLOW if the size of the property is not as expected,
468 * %-ENXIO if no suitable firmware interface is present.
469 */
470int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
471 const char *propname, u8 *val, size_t nval)
472{
Sakari Ailus37081842017-06-06 12:37:37 +0300473 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
474 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100475}
476EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
477
478/**
479 * fwnode_property_read_u16_array - return a u16 array property of firmware node
480 * @fwnode: Firmware node to get the property of
481 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200482 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100483 * @nval: Size of the @val array
484 *
485 * Read an array of u16 properties with @propname from @fwnode and store them to
486 * @val if found.
487 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200488 * Return: number of values if @val was %NULL,
489 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100490 * %-EINVAL if given arguments are not valid,
491 * %-ENODATA if the property does not have a value,
492 * %-EPROTO if the property is not an array of numbers,
493 * %-EOVERFLOW if the size of the property is not as expected,
494 * %-ENXIO if no suitable firmware interface is present.
495 */
496int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
497 const char *propname, u16 *val, size_t nval)
498{
Sakari Ailus37081842017-06-06 12:37:37 +0300499 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
500 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100501}
502EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
503
504/**
505 * fwnode_property_read_u32_array - return a u32 array property of firmware node
506 * @fwnode: Firmware node to get the property of
507 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200508 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100509 * @nval: Size of the @val array
510 *
511 * Read an array of u32 properties with @propname from @fwnode store them to
512 * @val if found.
513 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200514 * Return: number of values if @val was %NULL,
515 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100516 * %-EINVAL if given arguments are not valid,
517 * %-ENODATA if the property does not have a value,
518 * %-EPROTO if the property is not an array of numbers,
519 * %-EOVERFLOW if the size of the property is not as expected,
520 * %-ENXIO if no suitable firmware interface is present.
521 */
522int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
523 const char *propname, u32 *val, size_t nval)
524{
Sakari Ailus37081842017-06-06 12:37:37 +0300525 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
526 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100527}
528EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
529
530/**
531 * fwnode_property_read_u64_array - return a u64 array property firmware node
532 * @fwnode: Firmware node to get the property of
533 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200534 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100535 * @nval: Size of the @val array
536 *
537 * Read an array of u64 properties with @propname from @fwnode and store them to
538 * @val if found.
539 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200540 * Return: number of values if @val was %NULL,
541 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100542 * %-EINVAL if given arguments are not valid,
543 * %-ENODATA if the property does not have a value,
544 * %-EPROTO if the property is not an array of numbers,
545 * %-EOVERFLOW if the size of the property is not as expected,
546 * %-ENXIO if no suitable firmware interface is present.
547 */
548int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
549 const char *propname, u64 *val, size_t nval)
550{
Sakari Ailus37081842017-06-06 12:37:37 +0300551 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
552 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100553}
554EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
555
556/**
557 * fwnode_property_read_string_array - return string array property of a node
558 * @fwnode: Firmware node to get the property of
559 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200560 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100561 * @nval: Size of the @val array
562 *
563 * Read an string list property @propname from the given firmware node and store
564 * them to @val if found.
565 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300566 * Return: number of values read on success if @val is non-NULL,
567 * number of values available on success if @val is NULL,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100568 * %-EINVAL if given arguments are not valid,
569 * %-ENODATA if the property does not have a value,
Sakari Ailus026b8212017-03-28 15:22:17 +0300570 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100571 * %-EOVERFLOW if the size of the property is not as expected,
572 * %-ENXIO if no suitable firmware interface is present.
573 */
574int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
575 const char *propname, const char **val,
576 size_t nval)
577{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200578 int ret;
579
Sakari Ailus37081842017-06-06 12:37:37 +0300580 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
581 val, nval);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200582 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
583 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailus37081842017-06-06 12:37:37 +0300584 ret = fwnode_call_int_op(fwnode->secondary,
585 property_read_string_array, propname,
586 val, nval);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200587 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100588}
589EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
590
591/**
592 * fwnode_property_read_string - return a string property of a firmware node
593 * @fwnode: Firmware node to get the property of
594 * @propname: Name of the property
595 * @val: The value is stored here
596 *
597 * Read property @propname from the given firmware node and store the value into
598 * @val if found. The value is checked to be a string.
599 *
600 * Return: %0 if the property was found (success),
601 * %-EINVAL if given arguments are not valid,
602 * %-ENODATA if the property does not have a value,
603 * %-EPROTO or %-EILSEQ if the property is not a string,
604 * %-ENXIO if no suitable firmware interface is present.
605 */
606int fwnode_property_read_string(struct fwnode_handle *fwnode,
607 const char *propname, const char **val)
608{
Sakari Ailuse4817472017-03-28 15:26:22 +0300609 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200610
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300611 return ret < 0 ? ret : 0;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100612}
613EXPORT_SYMBOL_GPL(fwnode_property_read_string);
614
615/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300616 * fwnode_property_match_string - find a string in an array and return index
617 * @fwnode: Firmware node to get the property of
618 * @propname: Name of the property holding the array
619 * @string: String to look for
620 *
621 * Find a given string in a string array and if it is found return the
622 * index back.
623 *
624 * Return: %0 if the property was found (success),
625 * %-EINVAL if given arguments are not valid,
626 * %-ENODATA if the property does not have a value,
627 * %-EPROTO if the property is not an array of strings,
628 * %-ENXIO if no suitable firmware interface is present.
629 */
630int fwnode_property_match_string(struct fwnode_handle *fwnode,
631 const char *propname, const char *string)
632{
633 const char **values;
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700634 int nval, ret;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300635
636 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
637 if (nval < 0)
638 return nval;
639
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200640 if (nval == 0)
641 return -ENODATA;
642
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300643 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
644 if (!values)
645 return -ENOMEM;
646
647 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
648 if (ret < 0)
649 goto out;
650
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700651 ret = match_string(values, nval, string);
652 if (ret < 0)
653 ret = -ENODATA;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300654out:
655 kfree(values);
656 return ret;
657}
658EXPORT_SYMBOL_GPL(fwnode_property_match_string);
659
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800660static int property_copy_string_array(struct property_entry *dst,
661 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200662{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800663 char **d;
664 size_t nval = src->length / sizeof(*d);
665 int i;
Mika Westerberg13141e12015-11-30 17:11:37 +0200666
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800667 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
668 if (!d)
669 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200670
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800671 for (i = 0; i < nval; i++) {
672 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
673 if (!d[i] && src->pointer.str[i]) {
674 while (--i >= 0)
675 kfree(d[i]);
676 kfree(d);
677 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200678 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200679 }
680
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800681 dst->pointer.raw_data = d;
682 return 0;
Mika Westerberg13141e12015-11-30 17:11:37 +0200683}
684
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800685static int property_entry_copy_data(struct property_entry *dst,
686 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200687{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800688 int error;
Mika Westerberg13141e12015-11-30 17:11:37 +0200689
690 dst->name = kstrdup(src->name, GFP_KERNEL);
691 if (!dst->name)
692 return -ENOMEM;
693
694 if (src->is_array) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800695 if (!src->length) {
696 error = -ENODATA;
697 goto out_free_name;
698 }
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200699
Mika Westerberg13141e12015-11-30 17:11:37 +0200700 if (src->is_string) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800701 error = property_copy_string_array(dst, src);
702 if (error)
703 goto out_free_name;
Mika Westerberg13141e12015-11-30 17:11:37 +0200704 } else {
705 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
706 src->length, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800707 if (!dst->pointer.raw_data) {
708 error = -ENOMEM;
709 goto out_free_name;
710 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200711 }
712 } else if (src->is_string) {
713 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800714 if (!dst->value.str && src->value.str) {
715 error = -ENOMEM;
716 goto out_free_name;
717 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200718 } else {
719 dst->value.raw_data = src->value.raw_data;
720 }
721
722 dst->length = src->length;
723 dst->is_array = src->is_array;
724 dst->is_string = src->is_string;
725
726 return 0;
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800727
728out_free_name:
729 kfree(dst->name);
730 return error;
731}
732
733static void property_entry_free_data(const struct property_entry *p)
734{
735 size_t i, nval;
736
737 if (p->is_array) {
738 if (p->is_string && p->pointer.str) {
739 nval = p->length / sizeof(const char *);
740 for (i = 0; i < nval; i++)
741 kfree(p->pointer.str[i]);
742 }
743 kfree(p->pointer.raw_data);
744 } else if (p->is_string) {
745 kfree(p->value.str);
746 }
747 kfree(p->name);
748}
749
750/**
751 * property_entries_dup - duplicate array of properties
752 * @properties: array of properties to copy
753 *
754 * This function creates a deep copy of the given NULL-terminated array
755 * of property entries.
756 */
757struct property_entry *
758property_entries_dup(const struct property_entry *properties)
759{
760 struct property_entry *p;
761 int i, n = 0;
762
763 while (properties[n].name)
764 n++;
765
766 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
767 if (!p)
768 return ERR_PTR(-ENOMEM);
769
770 for (i = 0; i < n; i++) {
771 int ret = property_entry_copy_data(&p[i], &properties[i]);
772 if (ret) {
773 while (--i >= 0)
774 property_entry_free_data(&p[i]);
775 kfree(p);
776 return ERR_PTR(ret);
777 }
778 }
779
780 return p;
781}
782EXPORT_SYMBOL_GPL(property_entries_dup);
783
784/**
785 * property_entries_free - free previously allocated array of properties
786 * @properties: array of properties to destroy
787 *
788 * This function frees given NULL-terminated array of property entries,
789 * along with their data.
790 */
791void property_entries_free(const struct property_entry *properties)
792{
793 const struct property_entry *p;
794
795 for (p = properties; p->name; p++)
796 property_entry_free_data(p);
797
798 kfree(properties);
799}
800EXPORT_SYMBOL_GPL(property_entries_free);
801
802/**
803 * pset_free_set - releases memory allocated for copied property set
804 * @pset: Property set to release
805 *
806 * Function takes previously copied property set and releases all the
807 * memory allocated to it.
808 */
809static void pset_free_set(struct property_set *pset)
810{
811 if (!pset)
812 return;
813
814 property_entries_free(pset->properties);
815 kfree(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200816}
817
818/**
819 * pset_copy_set - copies property set
820 * @pset: Property set to copy
821 *
822 * This function takes a deep copy of the given property set and returns
823 * pointer to the copy. Call device_free_property_set() to free resources
824 * allocated in this function.
825 *
826 * Return: Pointer to the new property set or error pointer.
827 */
828static struct property_set *pset_copy_set(const struct property_set *pset)
829{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800830 struct property_entry *properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200831 struct property_set *p;
Mika Westerberg13141e12015-11-30 17:11:37 +0200832
833 p = kzalloc(sizeof(*p), GFP_KERNEL);
834 if (!p)
835 return ERR_PTR(-ENOMEM);
836
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800837 properties = property_entries_dup(pset->properties);
838 if (IS_ERR(properties)) {
Mika Westerberg13141e12015-11-30 17:11:37 +0200839 kfree(p);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800840 return ERR_CAST(properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200841 }
842
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800843 p->properties = properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200844 return p;
845}
846
847/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300848 * device_remove_properties - Remove properties from a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200849 * @dev: Device whose properties to remove.
850 *
851 * The function removes properties previously associated to the device
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300852 * secondary firmware node with device_add_properties(). Memory allocated
Mika Westerberg13141e12015-11-30 17:11:37 +0200853 * to the properties will also be released.
854 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300855void device_remove_properties(struct device *dev)
Mika Westerberg13141e12015-11-30 17:11:37 +0200856{
857 struct fwnode_handle *fwnode;
858
859 fwnode = dev_fwnode(dev);
860 if (!fwnode)
861 return;
862 /*
863 * Pick either primary or secondary node depending which one holds
864 * the pset. If there is no real firmware node (ACPI/DT) primary
865 * will hold the pset.
866 */
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200867 if (is_pset_node(fwnode)) {
868 set_primary_fwnode(dev, NULL);
Mika Westerberg13141e12015-11-30 17:11:37 +0200869 pset_free_set(to_pset_node(fwnode));
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200870 } else {
871 fwnode = fwnode->secondary;
872 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
873 set_secondary_fwnode(dev, NULL);
874 pset_free_set(to_pset_node(fwnode));
875 }
876 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200877}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300878EXPORT_SYMBOL_GPL(device_remove_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200879
880/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300881 * device_add_properties - Add a collection of properties to a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200882 * @dev: Device to add properties to.
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300883 * @properties: Collection of properties to add.
Mika Westerberg13141e12015-11-30 17:11:37 +0200884 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300885 * Associate a collection of device properties represented by @properties with
886 * @dev as its secondary firmware node. The function takes a copy of
887 * @properties.
Mika Westerberg13141e12015-11-30 17:11:37 +0200888 */
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800889int device_add_properties(struct device *dev,
890 const struct property_entry *properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200891{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300892 struct property_set *p, pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200893
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300894 if (!properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200895 return -EINVAL;
896
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300897 pset.properties = properties;
898
899 p = pset_copy_set(&pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200900 if (IS_ERR(p))
901 return PTR_ERR(p);
902
903 p->fwnode.type = FWNODE_PDATA;
Sakari Ailus37081842017-06-06 12:37:37 +0300904 p->fwnode.ops = &pset_fwnode_ops;
Mika Westerberg13141e12015-11-30 17:11:37 +0200905 set_secondary_fwnode(dev, &p->fwnode);
906 return 0;
907}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300908EXPORT_SYMBOL_GPL(device_add_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200909
910/**
Sakari Ailus23387252017-03-28 10:52:26 +0300911 * fwnode_get_next_parent - Iterate to the node's parent
912 * @fwnode: Firmware whose parent is retrieved
913 *
914 * This is like fwnode_get_parent() except that it drops the refcount
915 * on the passed node, making it suitable for iterating through a
916 * node's parents.
917 *
918 * Returns a node pointer with refcount incremented, use
919 * fwnode_handle_node() on it when done.
920 */
921struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
922{
923 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
924
925 fwnode_handle_put(fwnode);
926
927 return parent;
928}
929EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
930
931/**
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300932 * fwnode_get_parent - Return parent firwmare node
933 * @fwnode: Firmware whose parent is retrieved
934 *
935 * Return parent firmware node of the given node if possible or %NULL if no
936 * parent was available.
937 */
938struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode)
939{
Sakari Ailus37081842017-06-06 12:37:37 +0300940 return fwnode_call_ptr_op(fwnode, get_parent);
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300941}
942EXPORT_SYMBOL_GPL(fwnode_get_parent);
943
944/**
Mika Westerberg34055192017-03-28 10:52:18 +0300945 * fwnode_get_next_child_node - Return the next child node handle for a node
946 * @fwnode: Firmware node to find the next child node for.
947 * @child: Handle to one of the node's child nodes or a %NULL handle.
948 */
949struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode,
950 struct fwnode_handle *child)
951{
Sakari Ailus37081842017-06-06 12:37:37 +0300952 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
Mika Westerberg34055192017-03-28 10:52:18 +0300953}
954EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
955
956/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100957 * device_get_next_child_node - Return the next child node handle for a device
958 * @dev: Device to find the next child node for.
959 * @child: Handle to one of the device's child nodes or a null handle.
960 */
961struct fwnode_handle *device_get_next_child_node(struct device *dev,
962 struct fwnode_handle *child)
963{
Mika Westerberg34055192017-03-28 10:52:18 +0300964 struct acpi_device *adev = ACPI_COMPANION(dev);
965 struct fwnode_handle *fwnode = NULL;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100966
Mika Westerberg34055192017-03-28 10:52:18 +0300967 if (dev->of_node)
968 fwnode = &dev->of_node->fwnode;
969 else if (adev)
970 fwnode = acpi_fwnode_handle(adev);
971
972 return fwnode_get_next_child_node(fwnode, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100973}
974EXPORT_SYMBOL_GPL(device_get_next_child_node);
975
976/**
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300977 * fwnode_get_named_child_node - Return first matching named child node handle
978 * @fwnode: Firmware node to find the named child node for.
Adam Thomson613e9722016-06-21 18:50:20 +0100979 * @childname: String to match child node name against.
980 */
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300981struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode,
Adam Thomson613e9722016-06-21 18:50:20 +0100982 const char *childname)
983{
Sakari Ailus37081842017-06-06 12:37:37 +0300984 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
Adam Thomson613e9722016-06-21 18:50:20 +0100985}
Mika Westerberg21ea73f2017-03-28 10:52:19 +0300986EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
987
988/**
989 * device_get_named_child_node - Return first matching named child node handle
990 * @dev: Device to find the named child node for.
991 * @childname: String to match child node name against.
992 */
993struct fwnode_handle *device_get_named_child_node(struct device *dev,
994 const char *childname)
995{
996 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
997}
Adam Thomson613e9722016-06-21 18:50:20 +0100998EXPORT_SYMBOL_GPL(device_get_named_child_node);
999
1000/**
Sakari Ailuse7887c22017-03-28 10:52:22 +03001001 * fwnode_handle_get - Obtain a reference to a device node
1002 * @fwnode: Pointer to the device node to obtain the reference to.
1003 */
1004void fwnode_handle_get(struct fwnode_handle *fwnode)
1005{
Sakari Ailus37081842017-06-06 12:37:37 +03001006 fwnode_call_void_op(fwnode, get);
Sakari Ailuse7887c22017-03-28 10:52:22 +03001007}
1008EXPORT_SYMBOL_GPL(fwnode_handle_get);
1009
1010/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001011 * fwnode_handle_put - Drop reference to a device node
1012 * @fwnode: Pointer to the device node to drop the reference to.
1013 *
1014 * This has to be used when terminating device_for_each_child_node() iteration
1015 * with break or return to prevent stale device node references from being left
1016 * behind.
1017 */
1018void fwnode_handle_put(struct fwnode_handle *fwnode)
1019{
Sakari Ailus37081842017-06-06 12:37:37 +03001020 fwnode_call_void_op(fwnode, put);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001021}
1022EXPORT_SYMBOL_GPL(fwnode_handle_put);
1023
1024/**
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001025 * fwnode_device_is_available - check if a device is available for use
1026 * @fwnode: Pointer to the fwnode of the device.
1027 */
1028bool fwnode_device_is_available(struct fwnode_handle *fwnode)
1029{
Sakari Ailuse8158b42017-07-11 18:20:20 +03001030 return fwnode_call_bool_op(fwnode, device_is_available);
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001031}
1032EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1033
1034/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001035 * device_get_child_node_count - return the number of child nodes for device
1036 * @dev: Device to cound the child nodes for
1037 */
1038unsigned int device_get_child_node_count(struct device *dev)
1039{
1040 struct fwnode_handle *child;
1041 unsigned int count = 0;
1042
1043 device_for_each_child_node(dev, child)
1044 count++;
1045
1046 return count;
1047}
1048EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -05001049
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -07001050bool device_dma_supported(struct device *dev)
1051{
1052 /* For DT, this is always supported.
1053 * For ACPI, this depends on CCA, which
1054 * is determined by the acpi_dma_supported().
1055 */
1056 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1057 return true;
1058
1059 return acpi_dma_supported(ACPI_COMPANION(dev));
1060}
1061EXPORT_SYMBOL_GPL(device_dma_supported);
1062
1063enum dev_dma_attr device_get_dma_attr(struct device *dev)
1064{
1065 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1066
1067 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1068 if (of_dma_is_coherent(dev->of_node))
1069 attr = DEV_DMA_COHERENT;
1070 else
1071 attr = DEV_DMA_NON_COHERENT;
1072 } else
1073 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1074
1075 return attr;
1076}
1077EXPORT_SYMBOL_GPL(device_get_dma_attr);
1078
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001079/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001080 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001081 * @dev: Pointer to the given device
1082 *
1083 * The function gets phy interface string from property 'phy-mode' or
1084 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1085 * error case.
1086 */
1087int device_get_phy_mode(struct device *dev)
1088{
1089 const char *pm;
1090 int err, i;
1091
1092 err = device_property_read_string(dev, "phy-mode", &pm);
1093 if (err < 0)
1094 err = device_property_read_string(dev,
1095 "phy-connection-type", &pm);
1096 if (err < 0)
1097 return err;
1098
1099 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1100 if (!strcasecmp(pm, phy_modes(i)))
1101 return i;
1102
1103 return -ENODEV;
1104}
1105EXPORT_SYMBOL_GPL(device_get_phy_mode);
1106
1107static void *device_get_mac_addr(struct device *dev,
1108 const char *name, char *addr,
1109 int alen)
1110{
1111 int ret = device_property_read_u8_array(dev, name, addr, alen);
1112
Jeremy Linton2f710a32015-08-19 11:46:42 -05001113 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001114 return addr;
1115 return NULL;
1116}
1117
1118/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001119 * device_get_mac_address - Get the MAC for a given device
1120 * @dev: Pointer to the device
1121 * @addr: Address of buffer to store the MAC in
1122 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1123 *
1124 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001125 * checked first, because that is supposed to contain to "most recent" MAC
1126 * address. If that isn't set, then 'local-mac-address' is checked next,
1127 * because that is the default address. If that isn't set, then the obsolete
1128 * 'address' is checked, just in case we're using an old device tree.
1129 *
1130 * Note that the 'address' property is supposed to contain a virtual address of
1131 * the register set, but some DTS files have redefined that property to be the
1132 * MAC address.
1133 *
1134 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -05001135 * exist in the firmware tables, but were not updated by the firmware. For
1136 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1137 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1138 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1139 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001140*/
1141void *device_get_mac_address(struct device *dev, char *addr, int alen)
1142{
Julien Grall5b902d62015-09-03 23:59:50 +01001143 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001144
Julien Grall5b902d62015-09-03 23:59:50 +01001145 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1146 if (res)
1147 return res;
1148
1149 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1150 if (res)
1151 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001152
1153 return device_get_mac_addr(dev, "address", addr, alen);
1154}
1155EXPORT_SYMBOL(device_get_mac_address);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001156
1157/**
1158 * device_graph_get_next_endpoint - Get next endpoint firmware node
1159 * @fwnode: Pointer to the parent firmware node
1160 * @prev: Previous endpoint node or %NULL to get the first
1161 *
1162 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1163 * are available.
1164 */
1165struct fwnode_handle *
1166fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode,
1167 struct fwnode_handle *prev)
1168{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001169 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001170}
1171EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1172
1173/**
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001174 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1175 * @endpoint: Endpoint firmware node of the port
1176 *
1177 * Return: the firmware node of the device the @endpoint belongs to.
1178 */
1179struct fwnode_handle *
1180fwnode_graph_get_port_parent(struct fwnode_handle *endpoint)
1181{
1182 struct fwnode_handle *port, *parent;
1183
1184 port = fwnode_get_parent(endpoint);
1185 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1186
1187 fwnode_handle_put(port);
1188
1189 return parent;
1190}
1191EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1192
1193/**
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001194 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1195 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1196 *
1197 * Extracts firmware node of a remote device the @fwnode points to.
1198 */
1199struct fwnode_handle *
1200fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode)
1201{
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001202 struct fwnode_handle *endpoint, *parent;
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001203
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001204 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1205 parent = fwnode_graph_get_port_parent(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001206
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001207 fwnode_handle_put(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001208
1209 return parent;
1210}
1211EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1212
1213/**
1214 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1215 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1216 *
1217 * Extracts firmware node of a remote port the @fwnode points to.
1218 */
1219struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode)
1220{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001221 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001222}
1223EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1224
1225/**
1226 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1227 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1228 *
1229 * Extracts firmware node of a remote endpoint the @fwnode points to.
1230 */
1231struct fwnode_handle *
1232fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode)
1233{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001234 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001235}
1236EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001237
1238/**
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001239 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1240 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1241 * @port_id: identifier of the parent port node
1242 * @endpoint_id: identifier of the endpoint node
1243 *
1244 * Return: Remote fwnode handle associated with remote endpoint node linked
1245 * to @node. Use fwnode_node_put() on it when done.
1246 */
1247struct fwnode_handle *fwnode_graph_get_remote_node(struct fwnode_handle *fwnode,
1248 u32 port_id, u32 endpoint_id)
1249{
1250 struct fwnode_handle *endpoint = NULL;
1251
1252 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1253 struct fwnode_endpoint fwnode_ep;
1254 struct fwnode_handle *remote;
1255 int ret;
1256
1257 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1258 if (ret < 0)
1259 continue;
1260
1261 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1262 continue;
1263
1264 remote = fwnode_graph_get_remote_port_parent(endpoint);
1265 if (!remote)
1266 return NULL;
1267
1268 return fwnode_device_is_available(remote) ? remote : NULL;
1269 }
1270
1271 return NULL;
1272}
1273EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1274
1275/**
Sakari Ailus2bd54522017-03-28 10:52:25 +03001276 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1277 * @fwnode: pointer to endpoint fwnode_handle
1278 * @endpoint: pointer to the fwnode endpoint data structure
1279 *
1280 * Parse @fwnode representing a graph endpoint node and store the
1281 * information in @endpoint. The caller must hold a reference to
1282 * @fwnode.
1283 */
1284int fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode,
1285 struct fwnode_endpoint *endpoint)
1286{
Sakari Ailus2bd54522017-03-28 10:52:25 +03001287 memset(endpoint, 0, sizeof(*endpoint));
1288
Sakari Ailus3b27d002017-06-06 12:37:38 +03001289 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001290}
1291EXPORT_SYMBOL(fwnode_graph_parse_endpoint);