blob: 21fcc13013a5c0aba8b919b99c52698230394fe6 [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 {
Jarkko Nikula5ab894a2017-10-09 16:28:37 +030024 struct device *dev;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030025 struct fwnode_handle fwnode;
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080026 const struct property_entry *properties;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030027};
28
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030029static const struct fwnode_operations pset_fwnode_ops;
30
Sakari Ailus39e5aee2017-07-21 14:39:35 +030031static inline bool is_pset_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020032{
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030033 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020034}
35
Sakari Ailus39e5aee2017-07-21 14:39:35 +030036#define to_pset_node(__fwnode) \
37 ({ \
38 typeof(__fwnode) __to_pset_node_fwnode = __fwnode; \
39 \
40 is_pset_node(__to_pset_node_fwnode) ? \
41 container_of(__to_pset_node_fwnode, \
42 struct property_set, fwnode) : \
43 NULL; \
44 })
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020045
Sakari Ailus39e5aee2017-07-21 14:39:35 +030046static const struct property_entry *
47pset_prop_get(const struct property_set *pset, const char *name)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020048{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080049 const struct property_entry *prop;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020050
51 if (!pset || !pset->properties)
52 return NULL;
53
54 for (prop = pset->properties; prop->name; prop++)
55 if (!strcmp(name, prop->name))
56 return prop;
57
58 return NULL;
59}
60
Sakari Ailus39e5aee2017-07-21 14:39:35 +030061static const void *pset_prop_find(const struct property_set *pset,
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080062 const char *propname, size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020063{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080064 const struct property_entry *prop;
65 const void *pointer;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020066
Andy Shevchenko318a19712015-11-30 17:11:31 +020067 prop = pset_prop_get(pset, propname);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020068 if (!prop)
Andy Shevchenko318a19712015-11-30 17:11:31 +020069 return ERR_PTR(-EINVAL);
Andy Shevchenko66586ba2015-11-30 17:11:32 +020070 if (prop->is_array)
71 pointer = prop->pointer.raw_data;
72 else
73 pointer = &prop->value.raw_data;
Andy Shevchenko318a19712015-11-30 17:11:31 +020074 if (!pointer)
75 return ERR_PTR(-ENODATA);
76 if (length > prop->length)
77 return ERR_PTR(-EOVERFLOW);
78 return pointer;
79}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020080
Sakari Ailus39e5aee2017-07-21 14:39:35 +030081static int pset_prop_read_u8_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +020082 const char *propname,
83 u8 *values, size_t nval)
84{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080085 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020086 size_t length = nval * sizeof(*values);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020087
Andy Shevchenko318a19712015-11-30 17:11:31 +020088 pointer = pset_prop_find(pset, propname, length);
89 if (IS_ERR(pointer))
90 return PTR_ERR(pointer);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020091
Andy Shevchenko318a19712015-11-30 17:11:31 +020092 memcpy(values, pointer, length);
93 return 0;
94}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020095
Sakari Ailus39e5aee2017-07-21 14:39:35 +030096static int pset_prop_read_u16_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +020097 const char *propname,
98 u16 *values, size_t nval)
99{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800100 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200101 size_t length = nval * sizeof(*values);
102
103 pointer = pset_prop_find(pset, propname, length);
104 if (IS_ERR(pointer))
105 return PTR_ERR(pointer);
106
107 memcpy(values, pointer, length);
108 return 0;
109}
110
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300111static int pset_prop_read_u32_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200112 const char *propname,
113 u32 *values, size_t nval)
114{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800115 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200116 size_t length = nval * sizeof(*values);
117
118 pointer = pset_prop_find(pset, propname, length);
119 if (IS_ERR(pointer))
120 return PTR_ERR(pointer);
121
122 memcpy(values, pointer, length);
123 return 0;
124}
125
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300126static int pset_prop_read_u64_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200127 const char *propname,
128 u64 *values, size_t nval)
129{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800130 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200131 size_t length = nval * sizeof(*values);
132
133 pointer = pset_prop_find(pset, propname, length);
134 if (IS_ERR(pointer))
135 return PTR_ERR(pointer);
136
137 memcpy(values, pointer, length);
138 return 0;
139}
140
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300141static int pset_prop_count_elems_of_size(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200142 const char *propname, size_t length)
143{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800144 const struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200145
146 prop = pset_prop_get(pset, propname);
147 if (!prop)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200148 return -EINVAL;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200149
150 return prop->length / length;
151}
152
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300153static int pset_prop_read_string_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200154 const char *propname,
155 const char **strings, size_t nval)
156{
Sakari Ailus0f194992017-03-28 15:22:18 +0300157 const struct property_entry *prop;
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800158 const void *pointer;
Sakari Ailus0f194992017-03-28 15:22:18 +0300159 size_t array_len, length;
160
161 /* Find out the array length. */
162 prop = pset_prop_get(pset, propname);
163 if (!prop)
164 return -EINVAL;
165
166 if (!prop->is_array)
167 /* The array length for a non-array string property is 1. */
168 array_len = 1;
169 else
170 /* Find the length of an array. */
171 array_len = pset_prop_count_elems_of_size(pset, propname,
172 sizeof(const char *));
173
174 /* Return how many there are if strings is NULL. */
175 if (!strings)
176 return array_len;
177
178 array_len = min(nval, array_len);
179 length = array_len * sizeof(*strings);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200180
181 pointer = pset_prop_find(pset, propname, length);
182 if (IS_ERR(pointer))
183 return PTR_ERR(pointer);
184
185 memcpy(strings, pointer, length);
Sakari Ailus0f194992017-03-28 15:22:18 +0300186
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300187 return array_len;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200188}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100189
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300190struct fwnode_handle *dev_fwnode(struct device *dev)
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100191{
192 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
193 &dev->of_node->fwnode : dev->fwnode;
194}
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300195EXPORT_SYMBOL_GPL(dev_fwnode);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100196
Sakari Ailus37ba9832017-07-21 14:39:36 +0300197static bool pset_fwnode_property_present(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300198 const char *propname)
199{
200 return !!pset_prop_get(to_pset_node(fwnode), propname);
201}
202
Sakari Ailus37ba9832017-07-21 14:39:36 +0300203static int pset_fwnode_read_int_array(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300204 const char *propname,
205 unsigned int elem_size, void *val,
206 size_t nval)
207{
Sakari Ailus37ba9832017-07-21 14:39:36 +0300208 const struct property_set *node = to_pset_node(fwnode);
Sakari Ailus37081842017-06-06 12:37:37 +0300209
210 if (!val)
211 return pset_prop_count_elems_of_size(node, propname, elem_size);
212
213 switch (elem_size) {
214 case sizeof(u8):
215 return pset_prop_read_u8_array(node, propname, val, nval);
216 case sizeof(u16):
217 return pset_prop_read_u16_array(node, propname, val, nval);
218 case sizeof(u32):
219 return pset_prop_read_u32_array(node, propname, val, nval);
220 case sizeof(u64):
221 return pset_prop_read_u64_array(node, propname, val, nval);
222 }
223
224 return -ENXIO;
225}
226
Sakari Ailus37ba9832017-07-21 14:39:36 +0300227static int
228pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
229 const char *propname,
230 const char **val, size_t nval)
Sakari Ailus37081842017-06-06 12:37:37 +0300231{
232 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
233 val, nval);
234}
235
236static const struct fwnode_operations pset_fwnode_ops = {
237 .property_present = pset_fwnode_property_present,
238 .property_read_int_array = pset_fwnode_read_int_array,
239 .property_read_string_array = pset_fwnode_property_read_string_array,
240};
241
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100242/**
243 * device_property_present - check if a property of a device is present
244 * @dev: Device whose property is being checked
245 * @propname: Name of the property
246 *
247 * Check if property @propname is present in the device firmware description.
248 */
249bool device_property_present(struct device *dev, const char *propname)
250{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100251 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100252}
253EXPORT_SYMBOL_GPL(device_property_present);
254
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200255/**
256 * fwnode_property_present - check if a property of a firmware node is present
257 * @fwnode: Firmware node whose property to check
258 * @propname: Name of the property
259 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300260bool fwnode_property_present(const struct fwnode_handle *fwnode,
261 const char *propname)
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200262{
263 bool ret;
264
Sakari Ailuse8158b42017-07-11 18:20:20 +0300265 ret = fwnode_call_bool_op(fwnode, property_present, propname);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200266 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
267 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailuse8158b42017-07-11 18:20:20 +0300268 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
Sakari Ailus37081842017-06-06 12:37:37 +0300269 propname);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200270 return ret;
271}
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100272EXPORT_SYMBOL_GPL(fwnode_property_present);
273
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100274/**
275 * device_property_read_u8_array - return a u8 array property of a device
276 * @dev: Device to get the property of
277 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200278 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100279 * @nval: Size of the @val array
280 *
281 * Function reads an array of u8 properties with @propname from the device
282 * firmware description and stores them to @val if found.
283 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200284 * Return: number of values if @val was %NULL,
285 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100286 * %-EINVAL if given arguments are not valid,
287 * %-ENODATA if the property does not have a value,
288 * %-EPROTO if the property is not an array of numbers,
289 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700290 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100291 */
292int device_property_read_u8_array(struct device *dev, const char *propname,
293 u8 *val, size_t nval)
294{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100295 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100296}
297EXPORT_SYMBOL_GPL(device_property_read_u8_array);
298
299/**
300 * device_property_read_u16_array - return a u16 array property of a device
301 * @dev: Device to get the property of
302 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200303 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100304 * @nval: Size of the @val array
305 *
306 * Function reads an array of u16 properties with @propname from the device
307 * firmware description and stores them to @val if found.
308 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200309 * Return: number of values if @val was %NULL,
310 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100311 * %-EINVAL if given arguments are not valid,
312 * %-ENODATA if the property does not have a value,
313 * %-EPROTO if the property is not an array of numbers,
314 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700315 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100316 */
317int device_property_read_u16_array(struct device *dev, const char *propname,
318 u16 *val, size_t nval)
319{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100320 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100321}
322EXPORT_SYMBOL_GPL(device_property_read_u16_array);
323
324/**
325 * device_property_read_u32_array - return a u32 array property of a device
326 * @dev: Device to get the property of
327 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200328 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100329 * @nval: Size of the @val array
330 *
331 * Function reads an array of u32 properties with @propname from the device
332 * firmware description and stores them to @val if found.
333 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200334 * Return: number of values if @val was %NULL,
335 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100336 * %-EINVAL if given arguments are not valid,
337 * %-ENODATA if the property does not have a value,
338 * %-EPROTO if the property is not an array of numbers,
339 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700340 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100341 */
342int device_property_read_u32_array(struct device *dev, const char *propname,
343 u32 *val, size_t nval)
344{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100345 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100346}
347EXPORT_SYMBOL_GPL(device_property_read_u32_array);
348
349/**
350 * device_property_read_u64_array - return a u64 array property of a device
351 * @dev: Device to get the property of
352 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200353 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100354 * @nval: Size of the @val array
355 *
356 * Function reads an array of u64 properties with @propname from the device
357 * firmware description and stores them to @val if found.
358 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200359 * Return: number of values if @val was %NULL,
360 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100361 * %-EINVAL if given arguments are not valid,
362 * %-ENODATA if the property does not have a value,
363 * %-EPROTO if the property is not an array of numbers,
364 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700365 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100366 */
367int device_property_read_u64_array(struct device *dev, const char *propname,
368 u64 *val, size_t nval)
369{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100370 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100371}
372EXPORT_SYMBOL_GPL(device_property_read_u64_array);
373
374/**
375 * device_property_read_string_array - return a string array property of device
376 * @dev: Device to get the property of
377 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200378 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100379 * @nval: Size of the @val array
380 *
381 * Function reads an array of string properties with @propname from the device
382 * firmware description and stores them to @val if found.
383 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300384 * Return: number of values read on success if @val is non-NULL,
385 * number of values available on success if @val is NULL,
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100386 * %-EINVAL if given arguments are not valid,
387 * %-ENODATA if the property does not have a value,
388 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
389 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700390 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100391 */
392int device_property_read_string_array(struct device *dev, const char *propname,
393 const char **val, size_t nval)
394{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100395 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100396}
397EXPORT_SYMBOL_GPL(device_property_read_string_array);
398
399/**
400 * device_property_read_string - return a string property of a device
401 * @dev: Device to get the property of
402 * @propname: Name of the property
403 * @val: The value is stored here
404 *
405 * Function reads property @propname from the device firmware description and
406 * stores the value into @val if found. The value is checked to be a string.
407 *
408 * Return: %0 if the property was found (success),
409 * %-EINVAL if given arguments are not valid,
410 * %-ENODATA if the property does not have a value,
411 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700412 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100413 */
414int device_property_read_string(struct device *dev, const char *propname,
415 const char **val)
416{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100417 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100418}
419EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100420
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300421/**
422 * device_property_match_string - find a string in an array and return index
423 * @dev: Device to get the property of
424 * @propname: Name of the property holding the array
425 * @string: String to look for
426 *
427 * Find a given string in a string array and if it is found return the
428 * index back.
429 *
430 * Return: %0 if the property was found (success),
431 * %-EINVAL if given arguments are not valid,
432 * %-ENODATA if the property does not have a value,
433 * %-EPROTO if the property is not an array of strings,
434 * %-ENXIO if no suitable firmware interface is present.
435 */
436int device_property_match_string(struct device *dev, const char *propname,
437 const char *string)
438{
439 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
440}
441EXPORT_SYMBOL_GPL(device_property_match_string);
442
Sakari Ailus37ba9832017-07-21 14:39:36 +0300443static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300444 const char *propname,
445 unsigned int elem_size, void *val,
446 size_t nval)
447{
448 int ret;
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100449
Sakari Ailus37081842017-06-06 12:37:37 +0300450 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
451 elem_size, val, nval);
452 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
453 !IS_ERR_OR_NULL(fwnode->secondary))
454 ret = fwnode_call_int_op(
455 fwnode->secondary, property_read_int_array, propname,
456 elem_size, val, nval);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200457
Sakari Ailus37081842017-06-06 12:37:37 +0300458 return ret;
459}
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200460
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100461/**
462 * fwnode_property_read_u8_array - return a u8 array property of firmware node
463 * @fwnode: Firmware node to get the property of
464 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200465 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100466 * @nval: Size of the @val array
467 *
468 * Read an array of u8 properties with @propname from @fwnode and stores them to
469 * @val if found.
470 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200471 * Return: number of values if @val was %NULL,
472 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100473 * %-EINVAL if given arguments are not valid,
474 * %-ENODATA if the property does not have a value,
475 * %-EPROTO if the property is not an array of numbers,
476 * %-EOVERFLOW if the size of the property is not as expected,
477 * %-ENXIO if no suitable firmware interface is present.
478 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300479int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100480 const char *propname, u8 *val, size_t nval)
481{
Sakari Ailus37081842017-06-06 12:37:37 +0300482 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
483 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100484}
485EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
486
487/**
488 * fwnode_property_read_u16_array - return a u16 array property of firmware node
489 * @fwnode: Firmware node to get the property of
490 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200491 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100492 * @nval: Size of the @val array
493 *
494 * Read an array of u16 properties with @propname from @fwnode and store them to
495 * @val if found.
496 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200497 * Return: number of values if @val was %NULL,
498 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100499 * %-EINVAL if given arguments are not valid,
500 * %-ENODATA if the property does not have a value,
501 * %-EPROTO if the property is not an array of numbers,
502 * %-EOVERFLOW if the size of the property is not as expected,
503 * %-ENXIO if no suitable firmware interface is present.
504 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300505int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100506 const char *propname, u16 *val, size_t nval)
507{
Sakari Ailus37081842017-06-06 12:37:37 +0300508 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
509 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100510}
511EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
512
513/**
514 * fwnode_property_read_u32_array - return a u32 array property of firmware node
515 * @fwnode: Firmware node to get the property of
516 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200517 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100518 * @nval: Size of the @val array
519 *
520 * Read an array of u32 properties with @propname from @fwnode store them to
521 * @val if found.
522 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200523 * Return: number of values if @val was %NULL,
524 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100525 * %-EINVAL if given arguments are not valid,
526 * %-ENODATA if the property does not have a value,
527 * %-EPROTO if the property is not an array of numbers,
528 * %-EOVERFLOW if the size of the property is not as expected,
529 * %-ENXIO if no suitable firmware interface is present.
530 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300531int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100532 const char *propname, u32 *val, size_t nval)
533{
Sakari Ailus37081842017-06-06 12:37:37 +0300534 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
535 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100536}
537EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
538
539/**
540 * fwnode_property_read_u64_array - return a u64 array property firmware node
541 * @fwnode: Firmware node to get the property of
542 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200543 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100544 * @nval: Size of the @val array
545 *
546 * Read an array of u64 properties with @propname from @fwnode and store them to
547 * @val if found.
548 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200549 * Return: number of values if @val was %NULL,
550 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100551 * %-EINVAL if given arguments are not valid,
552 * %-ENODATA if the property does not have a value,
553 * %-EPROTO if the property is not an array of numbers,
554 * %-EOVERFLOW if the size of the property is not as expected,
555 * %-ENXIO if no suitable firmware interface is present.
556 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300557int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100558 const char *propname, u64 *val, size_t nval)
559{
Sakari Ailus37081842017-06-06 12:37:37 +0300560 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
561 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100562}
563EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
564
565/**
566 * fwnode_property_read_string_array - return string array property of a node
567 * @fwnode: Firmware node to get the property of
568 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200569 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100570 * @nval: Size of the @val array
571 *
572 * Read an string list property @propname from the given firmware node and store
573 * them to @val if found.
574 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300575 * Return: number of values read on success if @val is non-NULL,
576 * number of values available on success if @val is NULL,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100577 * %-EINVAL if given arguments are not valid,
578 * %-ENODATA if the property does not have a value,
Sakari Ailus026b8212017-03-28 15:22:17 +0300579 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100580 * %-EOVERFLOW if the size of the property is not as expected,
581 * %-ENXIO if no suitable firmware interface is present.
582 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300583int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100584 const char *propname, const char **val,
585 size_t nval)
586{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200587 int ret;
588
Sakari Ailus37081842017-06-06 12:37:37 +0300589 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
590 val, nval);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200591 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
592 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailus37081842017-06-06 12:37:37 +0300593 ret = fwnode_call_int_op(fwnode->secondary,
594 property_read_string_array, propname,
595 val, nval);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200596 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100597}
598EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
599
600/**
601 * fwnode_property_read_string - return a string property of a firmware node
602 * @fwnode: Firmware node to get the property of
603 * @propname: Name of the property
604 * @val: The value is stored here
605 *
606 * Read property @propname from the given firmware node and store the value into
607 * @val if found. The value is checked to be a string.
608 *
609 * Return: %0 if the property was found (success),
610 * %-EINVAL if given arguments are not valid,
611 * %-ENODATA if the property does not have a value,
612 * %-EPROTO or %-EILSEQ if the property is not a string,
613 * %-ENXIO if no suitable firmware interface is present.
614 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300615int fwnode_property_read_string(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100616 const char *propname, const char **val)
617{
Sakari Ailuse4817472017-03-28 15:26:22 +0300618 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200619
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300620 return ret < 0 ? ret : 0;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100621}
622EXPORT_SYMBOL_GPL(fwnode_property_read_string);
623
624/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300625 * fwnode_property_match_string - find a string in an array and return index
626 * @fwnode: Firmware node to get the property of
627 * @propname: Name of the property holding the array
628 * @string: String to look for
629 *
630 * Find a given string in a string array and if it is found return the
631 * index back.
632 *
633 * Return: %0 if the property was found (success),
634 * %-EINVAL if given arguments are not valid,
635 * %-ENODATA if the property does not have a value,
636 * %-EPROTO if the property is not an array of strings,
637 * %-ENXIO if no suitable firmware interface is present.
638 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300639int fwnode_property_match_string(const struct fwnode_handle *fwnode,
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300640 const char *propname, const char *string)
641{
642 const char **values;
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700643 int nval, ret;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300644
645 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
646 if (nval < 0)
647 return nval;
648
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200649 if (nval == 0)
650 return -ENODATA;
651
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300652 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
653 if (!values)
654 return -ENOMEM;
655
656 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
657 if (ret < 0)
658 goto out;
659
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700660 ret = match_string(values, nval, string);
661 if (ret < 0)
662 ret = -ENODATA;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300663out:
664 kfree(values);
665 return ret;
666}
667EXPORT_SYMBOL_GPL(fwnode_property_match_string);
668
Sakari Ailus3e3119d2017-07-21 15:11:49 +0300669/**
670 * fwnode_property_get_reference_args() - Find a reference with arguments
671 * @fwnode: Firmware node where to look for the reference
672 * @prop: The name of the property
673 * @nargs_prop: The name of the property telling the number of
674 * arguments in the referred node. NULL if @nargs is known,
675 * otherwise @nargs is ignored. Only relevant on OF.
676 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
677 * @index: Index of the reference, from zero onwards.
678 * @args: Result structure with reference and integer arguments.
679 *
680 * Obtain a reference based on a named property in an fwnode, with
681 * integer arguments.
682 *
683 * Caller is responsible to call fwnode_handle_put() on the returned
684 * args->fwnode pointer.
685 *
686 */
687int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
688 const char *prop, const char *nargs_prop,
689 unsigned int nargs, unsigned int index,
690 struct fwnode_reference_args *args)
691{
692 return fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
693 nargs, index, args);
694}
695EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
696
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800697static int property_copy_string_array(struct property_entry *dst,
698 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200699{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800700 char **d;
701 size_t nval = src->length / sizeof(*d);
702 int i;
Mika Westerberg13141e12015-11-30 17:11:37 +0200703
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800704 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
705 if (!d)
706 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200707
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800708 for (i = 0; i < nval; i++) {
709 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
710 if (!d[i] && src->pointer.str[i]) {
711 while (--i >= 0)
712 kfree(d[i]);
713 kfree(d);
714 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200715 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200716 }
717
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800718 dst->pointer.raw_data = d;
719 return 0;
Mika Westerberg13141e12015-11-30 17:11:37 +0200720}
721
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800722static int property_entry_copy_data(struct property_entry *dst,
723 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200724{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800725 int error;
Mika Westerberg13141e12015-11-30 17:11:37 +0200726
727 dst->name = kstrdup(src->name, GFP_KERNEL);
728 if (!dst->name)
729 return -ENOMEM;
730
731 if (src->is_array) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800732 if (!src->length) {
733 error = -ENODATA;
734 goto out_free_name;
735 }
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200736
Mika Westerberg13141e12015-11-30 17:11:37 +0200737 if (src->is_string) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800738 error = property_copy_string_array(dst, src);
739 if (error)
740 goto out_free_name;
Mika Westerberg13141e12015-11-30 17:11:37 +0200741 } else {
742 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
743 src->length, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800744 if (!dst->pointer.raw_data) {
745 error = -ENOMEM;
746 goto out_free_name;
747 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200748 }
749 } else if (src->is_string) {
750 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800751 if (!dst->value.str && src->value.str) {
752 error = -ENOMEM;
753 goto out_free_name;
754 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200755 } else {
756 dst->value.raw_data = src->value.raw_data;
757 }
758
759 dst->length = src->length;
760 dst->is_array = src->is_array;
761 dst->is_string = src->is_string;
762
763 return 0;
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800764
765out_free_name:
766 kfree(dst->name);
767 return error;
768}
769
770static void property_entry_free_data(const struct property_entry *p)
771{
772 size_t i, nval;
773
774 if (p->is_array) {
775 if (p->is_string && p->pointer.str) {
776 nval = p->length / sizeof(const char *);
777 for (i = 0; i < nval; i++)
778 kfree(p->pointer.str[i]);
779 }
780 kfree(p->pointer.raw_data);
781 } else if (p->is_string) {
782 kfree(p->value.str);
783 }
784 kfree(p->name);
785}
786
787/**
788 * property_entries_dup - duplicate array of properties
789 * @properties: array of properties to copy
790 *
791 * This function creates a deep copy of the given NULL-terminated array
792 * of property entries.
793 */
794struct property_entry *
795property_entries_dup(const struct property_entry *properties)
796{
797 struct property_entry *p;
798 int i, n = 0;
799
800 while (properties[n].name)
801 n++;
802
803 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
804 if (!p)
805 return ERR_PTR(-ENOMEM);
806
807 for (i = 0; i < n; i++) {
808 int ret = property_entry_copy_data(&p[i], &properties[i]);
809 if (ret) {
810 while (--i >= 0)
811 property_entry_free_data(&p[i]);
812 kfree(p);
813 return ERR_PTR(ret);
814 }
815 }
816
817 return p;
818}
819EXPORT_SYMBOL_GPL(property_entries_dup);
820
821/**
822 * property_entries_free - free previously allocated array of properties
823 * @properties: array of properties to destroy
824 *
825 * This function frees given NULL-terminated array of property entries,
826 * along with their data.
827 */
828void property_entries_free(const struct property_entry *properties)
829{
830 const struct property_entry *p;
831
832 for (p = properties; p->name; p++)
833 property_entry_free_data(p);
834
835 kfree(properties);
836}
837EXPORT_SYMBOL_GPL(property_entries_free);
838
839/**
840 * pset_free_set - releases memory allocated for copied property set
841 * @pset: Property set to release
842 *
843 * Function takes previously copied property set and releases all the
844 * memory allocated to it.
845 */
846static void pset_free_set(struct property_set *pset)
847{
848 if (!pset)
849 return;
850
851 property_entries_free(pset->properties);
852 kfree(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200853}
854
855/**
856 * pset_copy_set - copies property set
857 * @pset: Property set to copy
858 *
859 * This function takes a deep copy of the given property set and returns
860 * pointer to the copy. Call device_free_property_set() to free resources
861 * allocated in this function.
862 *
863 * Return: Pointer to the new property set or error pointer.
864 */
865static struct property_set *pset_copy_set(const struct property_set *pset)
866{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800867 struct property_entry *properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200868 struct property_set *p;
Mika Westerberg13141e12015-11-30 17:11:37 +0200869
870 p = kzalloc(sizeof(*p), GFP_KERNEL);
871 if (!p)
872 return ERR_PTR(-ENOMEM);
873
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800874 properties = property_entries_dup(pset->properties);
875 if (IS_ERR(properties)) {
Mika Westerberg13141e12015-11-30 17:11:37 +0200876 kfree(p);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800877 return ERR_CAST(properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200878 }
879
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800880 p->properties = properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200881 return p;
882}
883
884/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300885 * device_remove_properties - Remove properties from a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200886 * @dev: Device whose properties to remove.
887 *
888 * The function removes properties previously associated to the device
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300889 * secondary firmware node with device_add_properties(). Memory allocated
Mika Westerberg13141e12015-11-30 17:11:37 +0200890 * to the properties will also be released.
891 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300892void device_remove_properties(struct device *dev)
Mika Westerberg13141e12015-11-30 17:11:37 +0200893{
894 struct fwnode_handle *fwnode;
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300895 struct property_set *pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200896
897 fwnode = dev_fwnode(dev);
898 if (!fwnode)
899 return;
900 /*
901 * Pick either primary or secondary node depending which one holds
902 * the pset. If there is no real firmware node (ACPI/DT) primary
903 * will hold the pset.
904 */
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300905 pset = to_pset_node(fwnode);
906 if (pset) {
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200907 set_primary_fwnode(dev, NULL);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200908 } else {
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300909 pset = to_pset_node(fwnode->secondary);
910 if (pset && dev == pset->dev)
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200911 set_secondary_fwnode(dev, NULL);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200912 }
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300913 if (pset && dev == pset->dev)
914 pset_free_set(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200915}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300916EXPORT_SYMBOL_GPL(device_remove_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200917
918/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300919 * device_add_properties - Add a collection of properties to a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200920 * @dev: Device to add properties to.
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300921 * @properties: Collection of properties to add.
Mika Westerberg13141e12015-11-30 17:11:37 +0200922 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300923 * Associate a collection of device properties represented by @properties with
924 * @dev as its secondary firmware node. The function takes a copy of
925 * @properties.
Mika Westerberg13141e12015-11-30 17:11:37 +0200926 */
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800927int device_add_properties(struct device *dev,
928 const struct property_entry *properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200929{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300930 struct property_set *p, pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200931
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300932 if (!properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200933 return -EINVAL;
934
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300935 pset.properties = properties;
936
937 p = pset_copy_set(&pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200938 if (IS_ERR(p))
939 return PTR_ERR(p);
940
Sakari Ailus37081842017-06-06 12:37:37 +0300941 p->fwnode.ops = &pset_fwnode_ops;
Mika Westerberg13141e12015-11-30 17:11:37 +0200942 set_secondary_fwnode(dev, &p->fwnode);
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300943 p->dev = dev;
Mika Westerberg13141e12015-11-30 17:11:37 +0200944 return 0;
945}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300946EXPORT_SYMBOL_GPL(device_add_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200947
948/**
Sakari Ailus23387252017-03-28 10:52:26 +0300949 * fwnode_get_next_parent - Iterate to the node's parent
950 * @fwnode: Firmware whose parent is retrieved
951 *
952 * This is like fwnode_get_parent() except that it drops the refcount
953 * on the passed node, making it suitable for iterating through a
954 * node's parents.
955 *
956 * Returns a node pointer with refcount incremented, use
957 * fwnode_handle_node() on it when done.
958 */
959struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
960{
961 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
962
963 fwnode_handle_put(fwnode);
964
965 return parent;
966}
967EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
968
969/**
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300970 * fwnode_get_parent - Return parent firwmare node
971 * @fwnode: Firmware whose parent is retrieved
972 *
973 * Return parent firmware node of the given node if possible or %NULL if no
974 * parent was available.
975 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300976struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300977{
Sakari Ailus37081842017-06-06 12:37:37 +0300978 return fwnode_call_ptr_op(fwnode, get_parent);
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300979}
980EXPORT_SYMBOL_GPL(fwnode_get_parent);
981
982/**
Mika Westerberg34055192017-03-28 10:52:18 +0300983 * fwnode_get_next_child_node - Return the next child node handle for a node
984 * @fwnode: Firmware node to find the next child node for.
985 * @child: Handle to one of the node's child nodes or a %NULL handle.
986 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300987struct fwnode_handle *
988fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
989 struct fwnode_handle *child)
Mika Westerberg34055192017-03-28 10:52:18 +0300990{
Sakari Ailus37081842017-06-06 12:37:37 +0300991 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
Mika Westerberg34055192017-03-28 10:52:18 +0300992}
993EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
994
995/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100996 * device_get_next_child_node - Return the next child node handle for a device
997 * @dev: Device to find the next child node for.
998 * @child: Handle to one of the device's child nodes or a null handle.
999 */
1000struct fwnode_handle *device_get_next_child_node(struct device *dev,
1001 struct fwnode_handle *child)
1002{
Mika Westerberg34055192017-03-28 10:52:18 +03001003 struct acpi_device *adev = ACPI_COMPANION(dev);
1004 struct fwnode_handle *fwnode = NULL;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001005
Mika Westerberg34055192017-03-28 10:52:18 +03001006 if (dev->of_node)
1007 fwnode = &dev->of_node->fwnode;
1008 else if (adev)
1009 fwnode = acpi_fwnode_handle(adev);
1010
1011 return fwnode_get_next_child_node(fwnode, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001012}
1013EXPORT_SYMBOL_GPL(device_get_next_child_node);
1014
1015/**
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001016 * fwnode_get_named_child_node - Return first matching named child node handle
1017 * @fwnode: Firmware node to find the named child node for.
Adam Thomson613e9722016-06-21 18:50:20 +01001018 * @childname: String to match child node name against.
1019 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001020struct fwnode_handle *
1021fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
1022 const char *childname)
Adam Thomson613e9722016-06-21 18:50:20 +01001023{
Sakari Ailus37081842017-06-06 12:37:37 +03001024 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
Adam Thomson613e9722016-06-21 18:50:20 +01001025}
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001026EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1027
1028/**
1029 * device_get_named_child_node - Return first matching named child node handle
1030 * @dev: Device to find the named child node for.
1031 * @childname: String to match child node name against.
1032 */
1033struct fwnode_handle *device_get_named_child_node(struct device *dev,
1034 const char *childname)
1035{
1036 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1037}
Adam Thomson613e9722016-06-21 18:50:20 +01001038EXPORT_SYMBOL_GPL(device_get_named_child_node);
1039
1040/**
Sakari Ailuse7887c22017-03-28 10:52:22 +03001041 * fwnode_handle_get - Obtain a reference to a device node
1042 * @fwnode: Pointer to the device node to obtain the reference to.
1043 */
1044void fwnode_handle_get(struct fwnode_handle *fwnode)
1045{
Sakari Ailus37081842017-06-06 12:37:37 +03001046 fwnode_call_void_op(fwnode, get);
Sakari Ailuse7887c22017-03-28 10:52:22 +03001047}
1048EXPORT_SYMBOL_GPL(fwnode_handle_get);
1049
1050/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001051 * fwnode_handle_put - Drop reference to a device node
1052 * @fwnode: Pointer to the device node to drop the reference to.
1053 *
1054 * This has to be used when terminating device_for_each_child_node() iteration
1055 * with break or return to prevent stale device node references from being left
1056 * behind.
1057 */
1058void fwnode_handle_put(struct fwnode_handle *fwnode)
1059{
Sakari Ailus37081842017-06-06 12:37:37 +03001060 fwnode_call_void_op(fwnode, put);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001061}
1062EXPORT_SYMBOL_GPL(fwnode_handle_put);
1063
1064/**
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001065 * fwnode_device_is_available - check if a device is available for use
1066 * @fwnode: Pointer to the fwnode of the device.
1067 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001068bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001069{
Sakari Ailuse8158b42017-07-11 18:20:20 +03001070 return fwnode_call_bool_op(fwnode, device_is_available);
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001071}
1072EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1073
1074/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001075 * device_get_child_node_count - return the number of child nodes for device
1076 * @dev: Device to cound the child nodes for
1077 */
1078unsigned int device_get_child_node_count(struct device *dev)
1079{
1080 struct fwnode_handle *child;
1081 unsigned int count = 0;
1082
1083 device_for_each_child_node(dev, child)
1084 count++;
1085
1086 return count;
1087}
1088EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -05001089
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -07001090bool device_dma_supported(struct device *dev)
1091{
1092 /* For DT, this is always supported.
1093 * For ACPI, this depends on CCA, which
1094 * is determined by the acpi_dma_supported().
1095 */
1096 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1097 return true;
1098
1099 return acpi_dma_supported(ACPI_COMPANION(dev));
1100}
1101EXPORT_SYMBOL_GPL(device_dma_supported);
1102
1103enum dev_dma_attr device_get_dma_attr(struct device *dev)
1104{
1105 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1106
1107 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1108 if (of_dma_is_coherent(dev->of_node))
1109 attr = DEV_DMA_COHERENT;
1110 else
1111 attr = DEV_DMA_NON_COHERENT;
1112 } else
1113 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1114
1115 return attr;
1116}
1117EXPORT_SYMBOL_GPL(device_get_dma_attr);
1118
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001119/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001120 * device_get_phy_mode - Get phy mode for given device
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001121 * @dev: Pointer to the given device
1122 *
1123 * The function gets phy interface string from property 'phy-mode' or
1124 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1125 * error case.
1126 */
1127int device_get_phy_mode(struct device *dev)
1128{
1129 const char *pm;
1130 int err, i;
1131
1132 err = device_property_read_string(dev, "phy-mode", &pm);
1133 if (err < 0)
1134 err = device_property_read_string(dev,
1135 "phy-connection-type", &pm);
1136 if (err < 0)
1137 return err;
1138
1139 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1140 if (!strcasecmp(pm, phy_modes(i)))
1141 return i;
1142
1143 return -ENODEV;
1144}
1145EXPORT_SYMBOL_GPL(device_get_phy_mode);
1146
1147static void *device_get_mac_addr(struct device *dev,
1148 const char *name, char *addr,
1149 int alen)
1150{
1151 int ret = device_property_read_u8_array(dev, name, addr, alen);
1152
Jeremy Linton2f710a32015-08-19 11:46:42 -05001153 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001154 return addr;
1155 return NULL;
1156}
1157
1158/**
Jeremy Linton2f710a32015-08-19 11:46:42 -05001159 * device_get_mac_address - Get the MAC for a given device
1160 * @dev: Pointer to the device
1161 * @addr: Address of buffer to store the MAC in
1162 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1163 *
1164 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001165 * checked first, because that is supposed to contain to "most recent" MAC
1166 * address. If that isn't set, then 'local-mac-address' is checked next,
1167 * because that is the default address. If that isn't set, then the obsolete
1168 * 'address' is checked, just in case we're using an old device tree.
1169 *
1170 * Note that the 'address' property is supposed to contain a virtual address of
1171 * the register set, but some DTS files have redefined that property to be the
1172 * MAC address.
1173 *
1174 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -05001175 * exist in the firmware tables, but were not updated by the firmware. For
1176 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1177 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1178 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1179 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001180*/
1181void *device_get_mac_address(struct device *dev, char *addr, int alen)
1182{
Julien Grall5b902d62015-09-03 23:59:50 +01001183 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001184
Julien Grall5b902d62015-09-03 23:59:50 +01001185 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1186 if (res)
1187 return res;
1188
1189 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1190 if (res)
1191 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001192
1193 return device_get_mac_addr(dev, "address", addr, alen);
1194}
1195EXPORT_SYMBOL(device_get_mac_address);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001196
1197/**
1198 * device_graph_get_next_endpoint - Get next endpoint firmware node
1199 * @fwnode: Pointer to the parent firmware node
1200 * @prev: Previous endpoint node or %NULL to get the first
1201 *
1202 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1203 * are available.
1204 */
1205struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001206fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001207 struct fwnode_handle *prev)
1208{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001209 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001210}
1211EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1212
1213/**
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001214 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1215 * @endpoint: Endpoint firmware node of the port
1216 *
1217 * Return: the firmware node of the device the @endpoint belongs to.
1218 */
1219struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001220fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001221{
1222 struct fwnode_handle *port, *parent;
1223
1224 port = fwnode_get_parent(endpoint);
1225 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1226
1227 fwnode_handle_put(port);
1228
1229 return parent;
1230}
1231EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1232
1233/**
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001234 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1235 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1236 *
1237 * Extracts firmware node of a remote device the @fwnode points to.
1238 */
1239struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001240fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001241{
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001242 struct fwnode_handle *endpoint, *parent;
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001243
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001244 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1245 parent = fwnode_graph_get_port_parent(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001246
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001247 fwnode_handle_put(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001248
1249 return parent;
1250}
1251EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1252
1253/**
1254 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1255 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1256 *
1257 * Extracts firmware node of a remote port the @fwnode points to.
1258 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001259struct fwnode_handle *
1260fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001261{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001262 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001263}
1264EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1265
1266/**
1267 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1268 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1269 *
1270 * Extracts firmware node of a remote endpoint the @fwnode points to.
1271 */
1272struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001273fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001274{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001275 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001276}
1277EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001278
1279/**
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001280 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1281 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1282 * @port_id: identifier of the parent port node
1283 * @endpoint_id: identifier of the endpoint node
1284 *
1285 * Return: Remote fwnode handle associated with remote endpoint node linked
1286 * to @node. Use fwnode_node_put() on it when done.
1287 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001288struct fwnode_handle *
1289fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1290 u32 endpoint_id)
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001291{
1292 struct fwnode_handle *endpoint = NULL;
1293
1294 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1295 struct fwnode_endpoint fwnode_ep;
1296 struct fwnode_handle *remote;
1297 int ret;
1298
1299 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1300 if (ret < 0)
1301 continue;
1302
1303 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1304 continue;
1305
1306 remote = fwnode_graph_get_remote_port_parent(endpoint);
1307 if (!remote)
1308 return NULL;
1309
1310 return fwnode_device_is_available(remote) ? remote : NULL;
1311 }
1312
1313 return NULL;
1314}
1315EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1316
1317/**
Sakari Ailus2bd54522017-03-28 10:52:25 +03001318 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1319 * @fwnode: pointer to endpoint fwnode_handle
1320 * @endpoint: pointer to the fwnode endpoint data structure
1321 *
1322 * Parse @fwnode representing a graph endpoint node and store the
1323 * information in @endpoint. The caller must hold a reference to
1324 * @fwnode.
1325 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001326int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus2bd54522017-03-28 10:52:25 +03001327 struct fwnode_endpoint *endpoint)
1328{
Sakari Ailus2bd54522017-03-28 10:52:25 +03001329 memset(endpoint, 0, sizeof(*endpoint));
1330
Sakari Ailus3b27d002017-06-06 12:37:38 +03001331 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001332}
1333EXPORT_SYMBOL(fwnode_graph_parse_endpoint);