blob: d0b65bbe7e1513f140f198aa839fd03ef925e5e6 [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 Ailus37ba9832017-07-21 14:39:36 +0300196static bool pset_fwnode_property_present(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300197 const char *propname)
198{
199 return !!pset_prop_get(to_pset_node(fwnode), propname);
200}
201
Sakari Ailus37ba9832017-07-21 14:39:36 +0300202static int pset_fwnode_read_int_array(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300203 const char *propname,
204 unsigned int elem_size, void *val,
205 size_t nval)
206{
Sakari Ailus37ba9832017-07-21 14:39:36 +0300207 const struct property_set *node = to_pset_node(fwnode);
Sakari Ailus37081842017-06-06 12:37:37 +0300208
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
Sakari Ailus37ba9832017-07-21 14:39:36 +0300226static int
227pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
228 const char *propname,
229 const char **val, size_t nval)
Sakari Ailus37081842017-06-06 12:37:37 +0300230{
231 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
232 val, nval);
233}
234
235static const struct fwnode_operations pset_fwnode_ops = {
236 .property_present = pset_fwnode_property_present,
237 .property_read_int_array = pset_fwnode_read_int_array,
238 .property_read_string_array = pset_fwnode_property_read_string_array,
239};
240
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100241/**
242 * device_property_present - check if a property of a device is present
243 * @dev: Device whose property is being checked
244 * @propname: Name of the property
245 *
246 * Check if property @propname is present in the device firmware description.
247 */
248bool device_property_present(struct device *dev, const char *propname)
249{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100250 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100251}
252EXPORT_SYMBOL_GPL(device_property_present);
253
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200254/**
255 * fwnode_property_present - check if a property of a firmware node is present
256 * @fwnode: Firmware node whose property to check
257 * @propname: Name of the property
258 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300259bool fwnode_property_present(const struct fwnode_handle *fwnode,
260 const char *propname)
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200261{
262 bool ret;
263
Sakari Ailuse8158b482017-07-11 18:20:20 +0300264 ret = fwnode_call_bool_op(fwnode, property_present, propname);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200265 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
266 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailuse8158b482017-07-11 18:20:20 +0300267 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
Sakari Ailus37081842017-06-06 12:37:37 +0300268 propname);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200269 return ret;
270}
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100271EXPORT_SYMBOL_GPL(fwnode_property_present);
272
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100273/**
274 * device_property_read_u8_array - return a u8 array property of a device
275 * @dev: Device to get the property of
276 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200277 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100278 * @nval: Size of the @val array
279 *
280 * Function reads an array of u8 properties with @propname from the device
281 * firmware description and stores them to @val if found.
282 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200283 * Return: number of values if @val was %NULL,
284 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100285 * %-EINVAL if given arguments are not valid,
286 * %-ENODATA if the property does not have a value,
287 * %-EPROTO if the property is not an array of numbers,
288 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700289 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100290 */
291int device_property_read_u8_array(struct device *dev, const char *propname,
292 u8 *val, size_t nval)
293{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100294 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100295}
296EXPORT_SYMBOL_GPL(device_property_read_u8_array);
297
298/**
299 * device_property_read_u16_array - return a u16 array property of a device
300 * @dev: Device to get the property of
301 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200302 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100303 * @nval: Size of the @val array
304 *
305 * Function reads an array of u16 properties with @propname from the device
306 * firmware description and stores them to @val if found.
307 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200308 * Return: number of values if @val was %NULL,
309 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100310 * %-EINVAL if given arguments are not valid,
311 * %-ENODATA if the property does not have a value,
312 * %-EPROTO if the property is not an array of numbers,
313 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700314 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100315 */
316int device_property_read_u16_array(struct device *dev, const char *propname,
317 u16 *val, size_t nval)
318{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100319 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100320}
321EXPORT_SYMBOL_GPL(device_property_read_u16_array);
322
323/**
324 * device_property_read_u32_array - return a u32 array property of a device
325 * @dev: Device to get the property of
326 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200327 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100328 * @nval: Size of the @val array
329 *
330 * Function reads an array of u32 properties with @propname from the device
331 * firmware description and stores them to @val if found.
332 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200333 * Return: number of values if @val was %NULL,
334 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100335 * %-EINVAL if given arguments are not valid,
336 * %-ENODATA if the property does not have a value,
337 * %-EPROTO if the property is not an array of numbers,
338 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700339 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100340 */
341int device_property_read_u32_array(struct device *dev, const char *propname,
342 u32 *val, size_t nval)
343{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100344 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100345}
346EXPORT_SYMBOL_GPL(device_property_read_u32_array);
347
348/**
349 * device_property_read_u64_array - return a u64 array property of a device
350 * @dev: Device to get the property of
351 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200352 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100353 * @nval: Size of the @val array
354 *
355 * Function reads an array of u64 properties with @propname from the device
356 * firmware description and stores them to @val if found.
357 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200358 * Return: number of values if @val was %NULL,
359 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100360 * %-EINVAL if given arguments are not valid,
361 * %-ENODATA if the property does not have a value,
362 * %-EPROTO if the property is not an array of numbers,
363 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700364 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100365 */
366int device_property_read_u64_array(struct device *dev, const char *propname,
367 u64 *val, size_t nval)
368{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100369 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100370}
371EXPORT_SYMBOL_GPL(device_property_read_u64_array);
372
373/**
374 * device_property_read_string_array - return a string array property of device
375 * @dev: Device to get the property of
376 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200377 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100378 * @nval: Size of the @val array
379 *
380 * Function reads an array of string properties with @propname from the device
381 * firmware description and stores them to @val if found.
382 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300383 * Return: number of values read on success if @val is non-NULL,
384 * number of values available on success if @val is NULL,
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100385 * %-EINVAL if given arguments are not valid,
386 * %-ENODATA if the property does not have a value,
387 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
388 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700389 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100390 */
391int device_property_read_string_array(struct device *dev, const char *propname,
392 const char **val, size_t nval)
393{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100394 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100395}
396EXPORT_SYMBOL_GPL(device_property_read_string_array);
397
398/**
399 * device_property_read_string - return a string property of a device
400 * @dev: Device to get the property of
401 * @propname: Name of the property
402 * @val: The value is stored here
403 *
404 * Function reads property @propname from the device firmware description and
405 * stores the value into @val if found. The value is checked to be a string.
406 *
407 * Return: %0 if the property was found (success),
408 * %-EINVAL if given arguments are not valid,
409 * %-ENODATA if the property does not have a value,
410 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700411 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100412 */
413int device_property_read_string(struct device *dev, const char *propname,
414 const char **val)
415{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100416 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100417}
418EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100419
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300420/**
421 * device_property_match_string - find a string in an array and return index
422 * @dev: Device to get the property of
423 * @propname: Name of the property holding the array
424 * @string: String to look for
425 *
426 * Find a given string in a string array and if it is found return the
427 * index back.
428 *
429 * Return: %0 if the property was found (success),
430 * %-EINVAL if given arguments are not valid,
431 * %-ENODATA if the property does not have a value,
432 * %-EPROTO if the property is not an array of strings,
433 * %-ENXIO if no suitable firmware interface is present.
434 */
435int device_property_match_string(struct device *dev, const char *propname,
436 const char *string)
437{
438 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
439}
440EXPORT_SYMBOL_GPL(device_property_match_string);
441
Sakari Ailus37ba9832017-07-21 14:39:36 +0300442static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300443 const char *propname,
444 unsigned int elem_size, void *val,
445 size_t nval)
446{
447 int ret;
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100448
Sakari Ailus37081842017-06-06 12:37:37 +0300449 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
450 elem_size, val, nval);
451 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
452 !IS_ERR_OR_NULL(fwnode->secondary))
453 ret = fwnode_call_int_op(
454 fwnode->secondary, property_read_int_array, propname,
455 elem_size, val, nval);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200456
Sakari Ailus37081842017-06-06 12:37:37 +0300457 return ret;
458}
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200459
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100460/**
461 * fwnode_property_read_u8_array - return a u8 array property of firmware node
462 * @fwnode: Firmware node to get the property of
463 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200464 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100465 * @nval: Size of the @val array
466 *
467 * Read an array of u8 properties with @propname from @fwnode and stores them to
468 * @val if found.
469 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200470 * Return: number of values if @val was %NULL,
471 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100472 * %-EINVAL if given arguments are not valid,
473 * %-ENODATA if the property does not have a value,
474 * %-EPROTO if the property is not an array of numbers,
475 * %-EOVERFLOW if the size of the property is not as expected,
476 * %-ENXIO if no suitable firmware interface is present.
477 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300478int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100479 const char *propname, u8 *val, size_t nval)
480{
Sakari Ailus37081842017-06-06 12:37:37 +0300481 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
482 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100483}
484EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
485
486/**
487 * fwnode_property_read_u16_array - return a u16 array property of firmware node
488 * @fwnode: Firmware node to get the property of
489 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200490 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100491 * @nval: Size of the @val array
492 *
493 * Read an array of u16 properties with @propname from @fwnode and store them to
494 * @val if found.
495 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200496 * Return: number of values if @val was %NULL,
497 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100498 * %-EINVAL if given arguments are not valid,
499 * %-ENODATA if the property does not have a value,
500 * %-EPROTO if the property is not an array of numbers,
501 * %-EOVERFLOW if the size of the property is not as expected,
502 * %-ENXIO if no suitable firmware interface is present.
503 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300504int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100505 const char *propname, u16 *val, size_t nval)
506{
Sakari Ailus37081842017-06-06 12:37:37 +0300507 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
508 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100509}
510EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
511
512/**
513 * fwnode_property_read_u32_array - return a u32 array property of firmware node
514 * @fwnode: Firmware node to get the property of
515 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200516 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100517 * @nval: Size of the @val array
518 *
519 * Read an array of u32 properties with @propname from @fwnode store them to
520 * @val if found.
521 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200522 * Return: number of values if @val was %NULL,
523 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100524 * %-EINVAL if given arguments are not valid,
525 * %-ENODATA if the property does not have a value,
526 * %-EPROTO if the property is not an array of numbers,
527 * %-EOVERFLOW if the size of the property is not as expected,
528 * %-ENXIO if no suitable firmware interface is present.
529 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300530int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100531 const char *propname, u32 *val, size_t nval)
532{
Sakari Ailus37081842017-06-06 12:37:37 +0300533 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
534 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100535}
536EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
537
538/**
539 * fwnode_property_read_u64_array - return a u64 array property firmware node
540 * @fwnode: Firmware node to get the property of
541 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200542 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100543 * @nval: Size of the @val array
544 *
545 * Read an array of u64 properties with @propname from @fwnode and store them to
546 * @val if found.
547 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200548 * Return: number of values if @val was %NULL,
549 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100550 * %-EINVAL if given arguments are not valid,
551 * %-ENODATA if the property does not have a value,
552 * %-EPROTO if the property is not an array of numbers,
553 * %-EOVERFLOW if the size of the property is not as expected,
554 * %-ENXIO if no suitable firmware interface is present.
555 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300556int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100557 const char *propname, u64 *val, size_t nval)
558{
Sakari Ailus37081842017-06-06 12:37:37 +0300559 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
560 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100561}
562EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
563
564/**
565 * fwnode_property_read_string_array - return string array property of a node
566 * @fwnode: Firmware node to get the property of
567 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200568 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100569 * @nval: Size of the @val array
570 *
571 * Read an string list property @propname from the given firmware node and store
572 * them to @val if found.
573 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300574 * Return: number of values read on success if @val is non-NULL,
575 * number of values available on success if @val is NULL,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100576 * %-EINVAL if given arguments are not valid,
577 * %-ENODATA if the property does not have a value,
Sakari Ailus026b8212017-03-28 15:22:17 +0300578 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100579 * %-EOVERFLOW if the size of the property is not as expected,
580 * %-ENXIO if no suitable firmware interface is present.
581 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300582int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100583 const char *propname, const char **val,
584 size_t nval)
585{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200586 int ret;
587
Sakari Ailus37081842017-06-06 12:37:37 +0300588 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
589 val, nval);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200590 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
591 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailus37081842017-06-06 12:37:37 +0300592 ret = fwnode_call_int_op(fwnode->secondary,
593 property_read_string_array, propname,
594 val, nval);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200595 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100596}
597EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
598
599/**
600 * fwnode_property_read_string - return a string property of a firmware node
601 * @fwnode: Firmware node to get the property of
602 * @propname: Name of the property
603 * @val: The value is stored here
604 *
605 * Read property @propname from the given firmware node and store the value into
606 * @val if found. The value is checked to be a string.
607 *
608 * Return: %0 if the property was found (success),
609 * %-EINVAL if given arguments are not valid,
610 * %-ENODATA if the property does not have a value,
611 * %-EPROTO or %-EILSEQ if the property is not a string,
612 * %-ENXIO if no suitable firmware interface is present.
613 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300614int fwnode_property_read_string(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100615 const char *propname, const char **val)
616{
Sakari Ailuse4817472017-03-28 15:26:22 +0300617 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200618
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300619 return ret < 0 ? ret : 0;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100620}
621EXPORT_SYMBOL_GPL(fwnode_property_read_string);
622
623/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300624 * fwnode_property_match_string - find a string in an array and return index
625 * @fwnode: Firmware node to get the property of
626 * @propname: Name of the property holding the array
627 * @string: String to look for
628 *
629 * Find a given string in a string array and if it is found return the
630 * index back.
631 *
632 * Return: %0 if the property was found (success),
633 * %-EINVAL if given arguments are not valid,
634 * %-ENODATA if the property does not have a value,
635 * %-EPROTO if the property is not an array of strings,
636 * %-ENXIO if no suitable firmware interface is present.
637 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300638int fwnode_property_match_string(const struct fwnode_handle *fwnode,
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300639 const char *propname, const char *string)
640{
641 const char **values;
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700642 int nval, ret;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300643
644 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
645 if (nval < 0)
646 return nval;
647
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200648 if (nval == 0)
649 return -ENODATA;
650
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300651 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
652 if (!values)
653 return -ENOMEM;
654
655 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
656 if (ret < 0)
657 goto out;
658
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700659 ret = match_string(values, nval, string);
660 if (ret < 0)
661 ret = -ENODATA;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300662out:
663 kfree(values);
664 return ret;
665}
666EXPORT_SYMBOL_GPL(fwnode_property_match_string);
667
Sakari Ailus3e3119d2017-07-21 15:11:49 +0300668/**
669 * fwnode_property_get_reference_args() - Find a reference with arguments
670 * @fwnode: Firmware node where to look for the reference
671 * @prop: The name of the property
672 * @nargs_prop: The name of the property telling the number of
673 * arguments in the referred node. NULL if @nargs is known,
674 * otherwise @nargs is ignored. Only relevant on OF.
675 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
676 * @index: Index of the reference, from zero onwards.
677 * @args: Result structure with reference and integer arguments.
678 *
679 * Obtain a reference based on a named property in an fwnode, with
680 * integer arguments.
681 *
682 * Caller is responsible to call fwnode_handle_put() on the returned
683 * args->fwnode pointer.
684 *
685 */
686int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
687 const char *prop, const char *nargs_prop,
688 unsigned int nargs, unsigned int index,
689 struct fwnode_reference_args *args)
690{
691 return fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
692 nargs, index, args);
693}
694EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
695
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800696static int property_copy_string_array(struct property_entry *dst,
697 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200698{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800699 char **d;
700 size_t nval = src->length / sizeof(*d);
701 int i;
Mika Westerberg13141e12015-11-30 17:11:37 +0200702
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800703 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
704 if (!d)
705 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200706
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800707 for (i = 0; i < nval; i++) {
708 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
709 if (!d[i] && src->pointer.str[i]) {
710 while (--i >= 0)
711 kfree(d[i]);
712 kfree(d);
713 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200714 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200715 }
716
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800717 dst->pointer.raw_data = d;
718 return 0;
Mika Westerberg13141e12015-11-30 17:11:37 +0200719}
720
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800721static int property_entry_copy_data(struct property_entry *dst,
722 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200723{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800724 int error;
Mika Westerberg13141e12015-11-30 17:11:37 +0200725
726 dst->name = kstrdup(src->name, GFP_KERNEL);
727 if (!dst->name)
728 return -ENOMEM;
729
730 if (src->is_array) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800731 if (!src->length) {
732 error = -ENODATA;
733 goto out_free_name;
734 }
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200735
Mika Westerberg13141e12015-11-30 17:11:37 +0200736 if (src->is_string) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800737 error = property_copy_string_array(dst, src);
738 if (error)
739 goto out_free_name;
Mika Westerberg13141e12015-11-30 17:11:37 +0200740 } else {
741 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
742 src->length, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800743 if (!dst->pointer.raw_data) {
744 error = -ENOMEM;
745 goto out_free_name;
746 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200747 }
748 } else if (src->is_string) {
749 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800750 if (!dst->value.str && src->value.str) {
751 error = -ENOMEM;
752 goto out_free_name;
753 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200754 } else {
755 dst->value.raw_data = src->value.raw_data;
756 }
757
758 dst->length = src->length;
759 dst->is_array = src->is_array;
760 dst->is_string = src->is_string;
761
762 return 0;
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800763
764out_free_name:
765 kfree(dst->name);
766 return error;
767}
768
769static void property_entry_free_data(const struct property_entry *p)
770{
771 size_t i, nval;
772
773 if (p->is_array) {
774 if (p->is_string && p->pointer.str) {
775 nval = p->length / sizeof(const char *);
776 for (i = 0; i < nval; i++)
777 kfree(p->pointer.str[i]);
778 }
779 kfree(p->pointer.raw_data);
780 } else if (p->is_string) {
781 kfree(p->value.str);
782 }
783 kfree(p->name);
784}
785
786/**
787 * property_entries_dup - duplicate array of properties
788 * @properties: array of properties to copy
789 *
790 * This function creates a deep copy of the given NULL-terminated array
791 * of property entries.
792 */
793struct property_entry *
794property_entries_dup(const struct property_entry *properties)
795{
796 struct property_entry *p;
797 int i, n = 0;
798
799 while (properties[n].name)
800 n++;
801
802 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
803 if (!p)
804 return ERR_PTR(-ENOMEM);
805
806 for (i = 0; i < n; i++) {
807 int ret = property_entry_copy_data(&p[i], &properties[i]);
808 if (ret) {
809 while (--i >= 0)
810 property_entry_free_data(&p[i]);
811 kfree(p);
812 return ERR_PTR(ret);
813 }
814 }
815
816 return p;
817}
818EXPORT_SYMBOL_GPL(property_entries_dup);
819
820/**
821 * property_entries_free - free previously allocated array of properties
822 * @properties: array of properties to destroy
823 *
824 * This function frees given NULL-terminated array of property entries,
825 * along with their data.
826 */
827void property_entries_free(const struct property_entry *properties)
828{
829 const struct property_entry *p;
830
831 for (p = properties; p->name; p++)
832 property_entry_free_data(p);
833
834 kfree(properties);
835}
836EXPORT_SYMBOL_GPL(property_entries_free);
837
838/**
839 * pset_free_set - releases memory allocated for copied property set
840 * @pset: Property set to release
841 *
842 * Function takes previously copied property set and releases all the
843 * memory allocated to it.
844 */
845static void pset_free_set(struct property_set *pset)
846{
847 if (!pset)
848 return;
849
850 property_entries_free(pset->properties);
851 kfree(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200852}
853
854/**
855 * pset_copy_set - copies property set
856 * @pset: Property set to copy
857 *
858 * This function takes a deep copy of the given property set and returns
859 * pointer to the copy. Call device_free_property_set() to free resources
860 * allocated in this function.
861 *
862 * Return: Pointer to the new property set or error pointer.
863 */
864static struct property_set *pset_copy_set(const struct property_set *pset)
865{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800866 struct property_entry *properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200867 struct property_set *p;
Mika Westerberg13141e12015-11-30 17:11:37 +0200868
869 p = kzalloc(sizeof(*p), GFP_KERNEL);
870 if (!p)
871 return ERR_PTR(-ENOMEM);
872
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800873 properties = property_entries_dup(pset->properties);
874 if (IS_ERR(properties)) {
Mika Westerberg13141e12015-11-30 17:11:37 +0200875 kfree(p);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800876 return ERR_CAST(properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200877 }
878
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800879 p->properties = properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200880 return p;
881}
882
883/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300884 * device_remove_properties - Remove properties from a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200885 * @dev: Device whose properties to remove.
886 *
887 * The function removes properties previously associated to the device
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300888 * secondary firmware node with device_add_properties(). Memory allocated
Mika Westerberg13141e12015-11-30 17:11:37 +0200889 * to the properties will also be released.
890 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300891void device_remove_properties(struct device *dev)
Mika Westerberg13141e12015-11-30 17:11:37 +0200892{
893 struct fwnode_handle *fwnode;
894
895 fwnode = dev_fwnode(dev);
896 if (!fwnode)
897 return;
898 /*
899 * Pick either primary or secondary node depending which one holds
900 * the pset. If there is no real firmware node (ACPI/DT) primary
901 * will hold the pset.
902 */
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200903 if (is_pset_node(fwnode)) {
904 set_primary_fwnode(dev, NULL);
Mika Westerberg13141e12015-11-30 17:11:37 +0200905 pset_free_set(to_pset_node(fwnode));
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200906 } else {
907 fwnode = fwnode->secondary;
908 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
909 set_secondary_fwnode(dev, NULL);
910 pset_free_set(to_pset_node(fwnode));
911 }
912 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200913}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300914EXPORT_SYMBOL_GPL(device_remove_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200915
916/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300917 * device_add_properties - Add a collection of properties to a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200918 * @dev: Device to add properties to.
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300919 * @properties: Collection of properties to add.
Mika Westerberg13141e12015-11-30 17:11:37 +0200920 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300921 * Associate a collection of device properties represented by @properties with
922 * @dev as its secondary firmware node. The function takes a copy of
923 * @properties.
Mika Westerberg13141e12015-11-30 17:11:37 +0200924 */
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800925int device_add_properties(struct device *dev,
926 const struct property_entry *properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200927{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300928 struct property_set *p, pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200929
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300930 if (!properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200931 return -EINVAL;
932
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300933 pset.properties = properties;
934
935 p = pset_copy_set(&pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200936 if (IS_ERR(p))
937 return PTR_ERR(p);
938
Sakari Ailus37081842017-06-06 12:37:37 +0300939 p->fwnode.ops = &pset_fwnode_ops;
Mika Westerberg13141e12015-11-30 17:11:37 +0200940 set_secondary_fwnode(dev, &p->fwnode);
941 return 0;
942}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300943EXPORT_SYMBOL_GPL(device_add_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200944
945/**
Sakari Ailus23387252017-03-28 10:52:26 +0300946 * fwnode_get_next_parent - Iterate to the node's parent
947 * @fwnode: Firmware whose parent is retrieved
948 *
949 * This is like fwnode_get_parent() except that it drops the refcount
950 * on the passed node, making it suitable for iterating through a
951 * node's parents.
952 *
953 * Returns a node pointer with refcount incremented, use
954 * fwnode_handle_node() on it when done.
955 */
956struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
957{
958 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
959
960 fwnode_handle_put(fwnode);
961
962 return parent;
963}
964EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
965
966/**
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300967 * fwnode_get_parent - Return parent firwmare node
968 * @fwnode: Firmware whose parent is retrieved
969 *
970 * Return parent firmware node of the given node if possible or %NULL if no
971 * parent was available.
972 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300973struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300974{
Sakari Ailus37081842017-06-06 12:37:37 +0300975 return fwnode_call_ptr_op(fwnode, get_parent);
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300976}
977EXPORT_SYMBOL_GPL(fwnode_get_parent);
978
979/**
Mika Westerberg34055192017-03-28 10:52:18 +0300980 * fwnode_get_next_child_node - Return the next child node handle for a node
981 * @fwnode: Firmware node to find the next child node for.
982 * @child: Handle to one of the node's child nodes or a %NULL handle.
983 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300984struct fwnode_handle *
985fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
986 struct fwnode_handle *child)
Mika Westerberg34055192017-03-28 10:52:18 +0300987{
Sakari Ailus37081842017-06-06 12:37:37 +0300988 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
Mika Westerberg34055192017-03-28 10:52:18 +0300989}
990EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
991
992/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100993 * device_get_next_child_node - Return the next child node handle for a device
994 * @dev: Device to find the next child node for.
995 * @child: Handle to one of the device's child nodes or a null handle.
996 */
997struct fwnode_handle *device_get_next_child_node(struct device *dev,
998 struct fwnode_handle *child)
999{
Mika Westerberg34055192017-03-28 10:52:18 +03001000 struct acpi_device *adev = ACPI_COMPANION(dev);
1001 struct fwnode_handle *fwnode = NULL;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001002
Mika Westerberg34055192017-03-28 10:52:18 +03001003 if (dev->of_node)
1004 fwnode = &dev->of_node->fwnode;
1005 else if (adev)
1006 fwnode = acpi_fwnode_handle(adev);
1007
1008 return fwnode_get_next_child_node(fwnode, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001009}
1010EXPORT_SYMBOL_GPL(device_get_next_child_node);
1011
1012/**
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001013 * fwnode_get_named_child_node - Return first matching named child node handle
1014 * @fwnode: Firmware node to find the named child node for.
Adam Thomson613e9722016-06-21 18:50:20 +01001015 * @childname: String to match child node name against.
1016 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001017struct fwnode_handle *
1018fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
1019 const char *childname)
Adam Thomson613e9722016-06-21 18:50:20 +01001020{
Sakari Ailus37081842017-06-06 12:37:37 +03001021 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
Adam Thomson613e9722016-06-21 18:50:20 +01001022}
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001023EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1024
1025/**
1026 * device_get_named_child_node - Return first matching named child node handle
1027 * @dev: Device to find the named child node for.
1028 * @childname: String to match child node name against.
1029 */
1030struct fwnode_handle *device_get_named_child_node(struct device *dev,
1031 const char *childname)
1032{
1033 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1034}
Adam Thomson613e9722016-06-21 18:50:20 +01001035EXPORT_SYMBOL_GPL(device_get_named_child_node);
1036
1037/**
Sakari Ailuse7887c22017-03-28 10:52:22 +03001038 * fwnode_handle_get - Obtain a reference to a device node
1039 * @fwnode: Pointer to the device node to obtain the reference to.
1040 */
1041void fwnode_handle_get(struct fwnode_handle *fwnode)
1042{
Sakari Ailus37081842017-06-06 12:37:37 +03001043 fwnode_call_void_op(fwnode, get);
Sakari Ailuse7887c22017-03-28 10:52:22 +03001044}
1045EXPORT_SYMBOL_GPL(fwnode_handle_get);
1046
1047/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001048 * fwnode_handle_put - Drop reference to a device node
1049 * @fwnode: Pointer to the device node to drop the reference to.
1050 *
1051 * This has to be used when terminating device_for_each_child_node() iteration
1052 * with break or return to prevent stale device node references from being left
1053 * behind.
1054 */
1055void fwnode_handle_put(struct fwnode_handle *fwnode)
1056{
Sakari Ailus37081842017-06-06 12:37:37 +03001057 fwnode_call_void_op(fwnode, put);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001058}
1059EXPORT_SYMBOL_GPL(fwnode_handle_put);
1060
1061/**
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001062 * fwnode_device_is_available - check if a device is available for use
1063 * @fwnode: Pointer to the fwnode of the device.
1064 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001065bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001066{
Sakari Ailuse8158b482017-07-11 18:20:20 +03001067 return fwnode_call_bool_op(fwnode, device_is_available);
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001068}
1069EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1070
1071/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001072 * device_get_child_node_count - return the number of child nodes for device
1073 * @dev: Device to cound the child nodes for
1074 */
1075unsigned int device_get_child_node_count(struct device *dev)
1076{
1077 struct fwnode_handle *child;
1078 unsigned int count = 0;
1079
1080 device_for_each_child_node(dev, child)
1081 count++;
1082
1083 return count;
1084}
1085EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -05001086
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -07001087bool device_dma_supported(struct device *dev)
1088{
1089 /* For DT, this is always supported.
1090 * For ACPI, this depends on CCA, which
1091 * is determined by the acpi_dma_supported().
1092 */
1093 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1094 return true;
1095
1096 return acpi_dma_supported(ACPI_COMPANION(dev));
1097}
1098EXPORT_SYMBOL_GPL(device_dma_supported);
1099
1100enum dev_dma_attr device_get_dma_attr(struct device *dev)
1101{
1102 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1103
1104 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1105 if (of_dma_is_coherent(dev->of_node))
1106 attr = DEV_DMA_COHERENT;
1107 else
1108 attr = DEV_DMA_NON_COHERENT;
1109 } else
1110 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1111
1112 return attr;
1113}
1114EXPORT_SYMBOL_GPL(device_get_dma_attr);
1115
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001116/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001117 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001118 * @dev: Pointer to the given device
1119 *
1120 * The function gets phy interface string from property 'phy-mode' or
1121 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1122 * error case.
1123 */
1124int device_get_phy_mode(struct device *dev)
1125{
1126 const char *pm;
1127 int err, i;
1128
1129 err = device_property_read_string(dev, "phy-mode", &pm);
1130 if (err < 0)
1131 err = device_property_read_string(dev,
1132 "phy-connection-type", &pm);
1133 if (err < 0)
1134 return err;
1135
1136 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1137 if (!strcasecmp(pm, phy_modes(i)))
1138 return i;
1139
1140 return -ENODEV;
1141}
1142EXPORT_SYMBOL_GPL(device_get_phy_mode);
1143
1144static void *device_get_mac_addr(struct device *dev,
1145 const char *name, char *addr,
1146 int alen)
1147{
1148 int ret = device_property_read_u8_array(dev, name, addr, alen);
1149
Jeremy Linton2f710a32015-08-19 11:46:42 -05001150 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001151 return addr;
1152 return NULL;
1153}
1154
1155/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001156 * device_get_mac_address - Get the MAC for a given device
1157 * @dev: Pointer to the device
1158 * @addr: Address of buffer to store the MAC in
1159 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1160 *
1161 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001162 * checked first, because that is supposed to contain to "most recent" MAC
1163 * address. If that isn't set, then 'local-mac-address' is checked next,
1164 * because that is the default address. If that isn't set, then the obsolete
1165 * 'address' is checked, just in case we're using an old device tree.
1166 *
1167 * Note that the 'address' property is supposed to contain a virtual address of
1168 * the register set, but some DTS files have redefined that property to be the
1169 * MAC address.
1170 *
1171 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -05001172 * exist in the firmware tables, but were not updated by the firmware. For
1173 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1174 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1175 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1176 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001177*/
1178void *device_get_mac_address(struct device *dev, char *addr, int alen)
1179{
Julien Grall5b902d62015-09-03 23:59:50 +01001180 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001181
Julien Grall5b902d62015-09-03 23:59:50 +01001182 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1183 if (res)
1184 return res;
1185
1186 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1187 if (res)
1188 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001189
1190 return device_get_mac_addr(dev, "address", addr, alen);
1191}
1192EXPORT_SYMBOL(device_get_mac_address);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001193
1194/**
1195 * device_graph_get_next_endpoint - Get next endpoint firmware node
1196 * @fwnode: Pointer to the parent firmware node
1197 * @prev: Previous endpoint node or %NULL to get the first
1198 *
1199 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1200 * are available.
1201 */
1202struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001203fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001204 struct fwnode_handle *prev)
1205{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001206 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001207}
1208EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1209
1210/**
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001211 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1212 * @endpoint: Endpoint firmware node of the port
1213 *
1214 * Return: the firmware node of the device the @endpoint belongs to.
1215 */
1216struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001217fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001218{
1219 struct fwnode_handle *port, *parent;
1220
1221 port = fwnode_get_parent(endpoint);
1222 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1223
1224 fwnode_handle_put(port);
1225
1226 return parent;
1227}
1228EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1229
1230/**
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001231 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1232 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1233 *
1234 * Extracts firmware node of a remote device the @fwnode points to.
1235 */
1236struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001237fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001238{
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001239 struct fwnode_handle *endpoint, *parent;
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001240
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001241 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1242 parent = fwnode_graph_get_port_parent(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001243
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001244 fwnode_handle_put(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001245
1246 return parent;
1247}
1248EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1249
1250/**
1251 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1252 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1253 *
1254 * Extracts firmware node of a remote port the @fwnode points to.
1255 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001256struct fwnode_handle *
1257fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001258{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001259 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001260}
1261EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1262
1263/**
1264 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1265 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1266 *
1267 * Extracts firmware node of a remote endpoint the @fwnode points to.
1268 */
1269struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001270fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001271{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001272 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001273}
1274EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001275
1276/**
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001277 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1278 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1279 * @port_id: identifier of the parent port node
1280 * @endpoint_id: identifier of the endpoint node
1281 *
1282 * Return: Remote fwnode handle associated with remote endpoint node linked
1283 * to @node. Use fwnode_node_put() on it when done.
1284 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001285struct fwnode_handle *
1286fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1287 u32 endpoint_id)
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001288{
1289 struct fwnode_handle *endpoint = NULL;
1290
1291 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1292 struct fwnode_endpoint fwnode_ep;
1293 struct fwnode_handle *remote;
1294 int ret;
1295
1296 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1297 if (ret < 0)
1298 continue;
1299
1300 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1301 continue;
1302
1303 remote = fwnode_graph_get_remote_port_parent(endpoint);
1304 if (!remote)
1305 return NULL;
1306
1307 return fwnode_device_is_available(remote) ? remote : NULL;
1308 }
1309
1310 return NULL;
1311}
1312EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1313
1314/**
Sakari Ailus2bd54522017-03-28 10:52:25 +03001315 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1316 * @fwnode: pointer to endpoint fwnode_handle
1317 * @endpoint: pointer to the fwnode endpoint data structure
1318 *
1319 * Parse @fwnode representing a graph endpoint node and store the
1320 * information in @endpoint. The caller must hold a reference to
1321 * @fwnode.
1322 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001323int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus2bd54522017-03-28 10:52:25 +03001324 struct fwnode_endpoint *endpoint)
1325{
Sakari Ailus2bd54522017-03-28 10:52:25 +03001326 memset(endpoint, 0, sizeof(*endpoint));
1327
Sakari Ailus3b27d002017-06-06 12:37:38 +03001328 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001329}
1330EXPORT_SYMBOL(fwnode_graph_parse_endpoint);