blob: 1d6c9d99d72fe8b2314e377c22a6e89f03c1f1cc [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>
Marcin Wojtas7c6c57f2018-01-18 13:31:40 +010019#include <linux/of_irq.h>
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020020#include <linux/property.h>
Jeremy Linton4c96b7d2015-08-12 17:06:26 -050021#include <linux/etherdevice.h>
22#include <linux/phy.h>
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020023
Heikki Krogerusf4d05262016-03-29 14:52:23 +030024struct property_set {
Jarkko Nikula5ab894a2017-10-09 16:28:37 +030025 struct device *dev;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030026 struct fwnode_handle fwnode;
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080027 const struct property_entry *properties;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030028};
29
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030030static const struct fwnode_operations pset_fwnode_ops;
31
Sakari Ailus39e5aee2017-07-21 14:39:35 +030032static inline bool is_pset_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020033{
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030034 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020035}
36
Sakari Ailus39e5aee2017-07-21 14:39:35 +030037#define to_pset_node(__fwnode) \
38 ({ \
39 typeof(__fwnode) __to_pset_node_fwnode = __fwnode; \
40 \
41 is_pset_node(__to_pset_node_fwnode) ? \
42 container_of(__to_pset_node_fwnode, \
43 struct property_set, fwnode) : \
44 NULL; \
45 })
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020046
Sakari Ailus39e5aee2017-07-21 14:39:35 +030047static const struct property_entry *
48pset_prop_get(const struct property_set *pset, const char *name)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020049{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080050 const struct property_entry *prop;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020051
52 if (!pset || !pset->properties)
53 return NULL;
54
55 for (prop = pset->properties; prop->name; prop++)
56 if (!strcmp(name, prop->name))
57 return prop;
58
59 return NULL;
60}
61
Sakari Ailus39e5aee2017-07-21 14:39:35 +030062static const void *pset_prop_find(const struct property_set *pset,
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080063 const char *propname, size_t length)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020064{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080065 const struct property_entry *prop;
66 const void *pointer;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020067
Andy Shevchenko318a19712015-11-30 17:11:31 +020068 prop = pset_prop_get(pset, propname);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020069 if (!prop)
Andy Shevchenko318a19712015-11-30 17:11:31 +020070 return ERR_PTR(-EINVAL);
Andy Shevchenko66586ba2015-11-30 17:11:32 +020071 if (prop->is_array)
72 pointer = prop->pointer.raw_data;
73 else
74 pointer = &prop->value.raw_data;
Andy Shevchenko318a19712015-11-30 17:11:31 +020075 if (!pointer)
76 return ERR_PTR(-ENODATA);
77 if (length > prop->length)
78 return ERR_PTR(-EOVERFLOW);
79 return pointer;
80}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020081
Sakari Ailus39e5aee2017-07-21 14:39:35 +030082static int pset_prop_read_u8_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +020083 const char *propname,
84 u8 *values, size_t nval)
85{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -080086 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +020087 size_t length = nval * sizeof(*values);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020088
Andy Shevchenko318a19712015-11-30 17:11:31 +020089 pointer = pset_prop_find(pset, propname, length);
90 if (IS_ERR(pointer))
91 return PTR_ERR(pointer);
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020092
Andy Shevchenko318a19712015-11-30 17:11:31 +020093 memcpy(values, pointer, length);
94 return 0;
95}
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +020096
Sakari Ailus39e5aee2017-07-21 14:39:35 +030097static int pset_prop_read_u16_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +020098 const char *propname,
99 u16 *values, size_t nval)
100{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800101 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200102 size_t length = nval * sizeof(*values);
103
104 pointer = pset_prop_find(pset, propname, length);
105 if (IS_ERR(pointer))
106 return PTR_ERR(pointer);
107
108 memcpy(values, pointer, length);
109 return 0;
110}
111
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300112static int pset_prop_read_u32_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200113 const char *propname,
114 u32 *values, size_t nval)
115{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800116 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200117 size_t length = nval * sizeof(*values);
118
119 pointer = pset_prop_find(pset, propname, length);
120 if (IS_ERR(pointer))
121 return PTR_ERR(pointer);
122
123 memcpy(values, pointer, length);
124 return 0;
125}
126
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300127static int pset_prop_read_u64_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200128 const char *propname,
129 u64 *values, size_t nval)
130{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800131 const void *pointer;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200132 size_t length = nval * sizeof(*values);
133
134 pointer = pset_prop_find(pset, propname, length);
135 if (IS_ERR(pointer))
136 return PTR_ERR(pointer);
137
138 memcpy(values, pointer, length);
139 return 0;
140}
141
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300142static int pset_prop_count_elems_of_size(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200143 const char *propname, size_t length)
144{
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800145 const struct property_entry *prop;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200146
147 prop = pset_prop_get(pset, propname);
148 if (!prop)
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200149 return -EINVAL;
Andy Shevchenko318a19712015-11-30 17:11:31 +0200150
151 return prop->length / length;
152}
153
Sakari Ailus39e5aee2017-07-21 14:39:35 +0300154static int pset_prop_read_string_array(const struct property_set *pset,
Andy Shevchenko318a19712015-11-30 17:11:31 +0200155 const char *propname,
156 const char **strings, size_t nval)
157{
Sakari Ailus0f194992017-03-28 15:22:18 +0300158 const struct property_entry *prop;
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800159 const void *pointer;
Sakari Ailus0f194992017-03-28 15:22:18 +0300160 size_t array_len, length;
161
162 /* Find out the array length. */
163 prop = pset_prop_get(pset, propname);
164 if (!prop)
165 return -EINVAL;
166
167 if (!prop->is_array)
168 /* The array length for a non-array string property is 1. */
169 array_len = 1;
170 else
171 /* Find the length of an array. */
172 array_len = pset_prop_count_elems_of_size(pset, propname,
173 sizeof(const char *));
174
175 /* Return how many there are if strings is NULL. */
176 if (!strings)
177 return array_len;
178
179 array_len = min(nval, array_len);
180 length = array_len * sizeof(*strings);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200181
182 pointer = pset_prop_find(pset, propname, length);
183 if (IS_ERR(pointer))
184 return PTR_ERR(pointer);
185
186 memcpy(strings, pointer, length);
Sakari Ailus0f194992017-03-28 15:22:18 +0300187
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300188 return array_len;
Rafael J. Wysocki16ba08d2015-04-03 16:05:11 +0200189}
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100190
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300191struct fwnode_handle *dev_fwnode(struct device *dev)
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100192{
193 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
194 &dev->of_node->fwnode : dev->fwnode;
195}
Sakari Ailuse44bb0c2017-03-28 10:52:24 +0300196EXPORT_SYMBOL_GPL(dev_fwnode);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100197
Sakari Ailus37ba9832017-07-21 14:39:36 +0300198static bool pset_fwnode_property_present(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300199 const char *propname)
200{
201 return !!pset_prop_get(to_pset_node(fwnode), propname);
202}
203
Sakari Ailus37ba9832017-07-21 14:39:36 +0300204static int pset_fwnode_read_int_array(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300205 const char *propname,
206 unsigned int elem_size, void *val,
207 size_t nval)
208{
Sakari Ailus37ba9832017-07-21 14:39:36 +0300209 const struct property_set *node = to_pset_node(fwnode);
Sakari Ailus37081842017-06-06 12:37:37 +0300210
211 if (!val)
212 return pset_prop_count_elems_of_size(node, propname, elem_size);
213
214 switch (elem_size) {
215 case sizeof(u8):
216 return pset_prop_read_u8_array(node, propname, val, nval);
217 case sizeof(u16):
218 return pset_prop_read_u16_array(node, propname, val, nval);
219 case sizeof(u32):
220 return pset_prop_read_u32_array(node, propname, val, nval);
221 case sizeof(u64):
222 return pset_prop_read_u64_array(node, propname, val, nval);
223 }
224
225 return -ENXIO;
226}
227
Sakari Ailus37ba9832017-07-21 14:39:36 +0300228static int
229pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
230 const char *propname,
231 const char **val, size_t nval)
Sakari Ailus37081842017-06-06 12:37:37 +0300232{
233 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
234 val, nval);
235}
236
237static const struct fwnode_operations pset_fwnode_ops = {
238 .property_present = pset_fwnode_property_present,
239 .property_read_int_array = pset_fwnode_read_int_array,
240 .property_read_string_array = pset_fwnode_property_read_string_array,
241};
242
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100243/**
244 * device_property_present - check if a property of a device is present
245 * @dev: Device whose property is being checked
246 * @propname: Name of the property
247 *
248 * Check if property @propname is present in the device firmware description.
249 */
250bool device_property_present(struct device *dev, const char *propname)
251{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100252 return fwnode_property_present(dev_fwnode(dev), propname);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100253}
254EXPORT_SYMBOL_GPL(device_property_present);
255
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200256/**
257 * fwnode_property_present - check if a property of a firmware node is present
258 * @fwnode: Firmware node whose property to check
259 * @propname: Name of the property
260 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300261bool fwnode_property_present(const struct fwnode_handle *fwnode,
262 const char *propname)
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200263{
264 bool ret;
265
Sakari Ailuse8158b482017-07-11 18:20:20 +0300266 ret = fwnode_call_bool_op(fwnode, property_present, propname);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200267 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
268 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailuse8158b482017-07-11 18:20:20 +0300269 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
Sakari Ailus37081842017-06-06 12:37:37 +0300270 propname);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200271 return ret;
272}
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100273EXPORT_SYMBOL_GPL(fwnode_property_present);
274
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100275/**
276 * device_property_read_u8_array - return a u8 array property of a device
277 * @dev: Device to get the property of
278 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200279 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100280 * @nval: Size of the @val array
281 *
282 * Function reads an array of u8 properties with @propname from the device
283 * firmware description and stores them to @val if found.
284 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200285 * Return: number of values if @val was %NULL,
286 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100287 * %-EINVAL if given arguments are not valid,
288 * %-ENODATA if the property does not have a value,
289 * %-EPROTO if the property is not an array of numbers,
290 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700291 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100292 */
293int device_property_read_u8_array(struct device *dev, const char *propname,
294 u8 *val, size_t nval)
295{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100296 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100297}
298EXPORT_SYMBOL_GPL(device_property_read_u8_array);
299
300/**
301 * device_property_read_u16_array - return a u16 array property of a device
302 * @dev: Device to get the property of
303 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200304 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100305 * @nval: Size of the @val array
306 *
307 * Function reads an array of u16 properties with @propname from the device
308 * firmware description and stores them to @val if found.
309 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200310 * Return: number of values if @val was %NULL,
311 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100312 * %-EINVAL if given arguments are not valid,
313 * %-ENODATA if the property does not have a value,
314 * %-EPROTO if the property is not an array of numbers,
315 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700316 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100317 */
318int device_property_read_u16_array(struct device *dev, const char *propname,
319 u16 *val, size_t nval)
320{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100321 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100322}
323EXPORT_SYMBOL_GPL(device_property_read_u16_array);
324
325/**
326 * device_property_read_u32_array - return a u32 array property of a device
327 * @dev: Device to get the property of
328 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200329 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100330 * @nval: Size of the @val array
331 *
332 * Function reads an array of u32 properties with @propname from the device
333 * firmware description and stores them to @val if found.
334 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200335 * Return: number of values if @val was %NULL,
336 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100337 * %-EINVAL if given arguments are not valid,
338 * %-ENODATA if the property does not have a value,
339 * %-EPROTO if the property is not an array of numbers,
340 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700341 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100342 */
343int device_property_read_u32_array(struct device *dev, const char *propname,
344 u32 *val, size_t nval)
345{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100346 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100347}
348EXPORT_SYMBOL_GPL(device_property_read_u32_array);
349
350/**
351 * device_property_read_u64_array - return a u64 array property of a device
352 * @dev: Device to get the property of
353 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200354 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100355 * @nval: Size of the @val array
356 *
357 * Function reads an array of u64 properties with @propname from the device
358 * firmware description and stores them to @val if found.
359 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200360 * Return: number of values if @val was %NULL,
361 * %0 if the property was found (success),
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100362 * %-EINVAL if given arguments are not valid,
363 * %-ENODATA if the property does not have a value,
364 * %-EPROTO if the property is not an array of numbers,
365 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700366 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100367 */
368int device_property_read_u64_array(struct device *dev, const char *propname,
369 u64 *val, size_t nval)
370{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100371 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100372}
373EXPORT_SYMBOL_GPL(device_property_read_u64_array);
374
375/**
376 * device_property_read_string_array - return a string array property of device
377 * @dev: Device to get the property of
378 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200379 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100380 * @nval: Size of the @val array
381 *
382 * Function reads an array of string properties with @propname from the device
383 * firmware description and stores them to @val if found.
384 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300385 * Return: number of values read on success if @val is non-NULL,
386 * number of values available on success if @val is NULL,
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100387 * %-EINVAL if given arguments are not valid,
388 * %-ENODATA if the property does not have a value,
389 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
390 * %-EOVERFLOW if the size of the property is not as expected.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700391 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100392 */
393int device_property_read_string_array(struct device *dev, const char *propname,
394 const char **val, size_t nval)
395{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100396 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100397}
398EXPORT_SYMBOL_GPL(device_property_read_string_array);
399
400/**
401 * device_property_read_string - return a string property of a device
402 * @dev: Device to get the property of
403 * @propname: Name of the property
404 * @val: The value is stored here
405 *
406 * Function reads property @propname from the device firmware description and
407 * stores the value into @val if found. The value is checked to be a string.
408 *
409 * Return: %0 if the property was found (success),
410 * %-EINVAL if given arguments are not valid,
411 * %-ENODATA if the property does not have a value,
412 * %-EPROTO or %-EILSEQ if the property type is not a string.
Guenter Roeck4fa7508e2015-08-26 20:27:04 -0700413 * %-ENXIO if no suitable firmware interface is present.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100414 */
415int device_property_read_string(struct device *dev, const char *propname,
416 const char **val)
417{
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100418 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100419}
420EXPORT_SYMBOL_GPL(device_property_read_string);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100421
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300422/**
423 * device_property_match_string - find a string in an array and return index
424 * @dev: Device to get the property of
425 * @propname: Name of the property holding the array
426 * @string: String to look for
427 *
428 * Find a given string in a string array and if it is found return the
429 * index back.
430 *
431 * Return: %0 if the property was found (success),
432 * %-EINVAL if given arguments are not valid,
433 * %-ENODATA if the property does not have a value,
434 * %-EPROTO if the property is not an array of strings,
435 * %-ENXIO if no suitable firmware interface is present.
436 */
437int device_property_match_string(struct device *dev, const char *propname,
438 const char *string)
439{
440 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
441}
442EXPORT_SYMBOL_GPL(device_property_match_string);
443
Sakari Ailus37ba9832017-07-21 14:39:36 +0300444static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300445 const char *propname,
446 unsigned int elem_size, void *val,
447 size_t nval)
448{
449 int ret;
Rafael J. Wysocki9017f252015-03-24 00:24:16 +0100450
Sakari Ailus37081842017-06-06 12:37:37 +0300451 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
452 elem_size, val, nval);
453 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
454 !IS_ERR_OR_NULL(fwnode->secondary))
455 ret = fwnode_call_int_op(
456 fwnode->secondary, property_read_int_array, propname,
457 elem_size, val, nval);
Andy Shevchenko318a19712015-11-30 17:11:31 +0200458
Sakari Ailus37081842017-06-06 12:37:37 +0300459 return ret;
460}
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200461
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100462/**
463 * fwnode_property_read_u8_array - return a u8 array property of firmware node
464 * @fwnode: Firmware node to get the property of
465 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200466 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100467 * @nval: Size of the @val array
468 *
469 * Read an array of u8 properties with @propname from @fwnode and stores them to
470 * @val if found.
471 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200472 * Return: number of values if @val was %NULL,
473 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100474 * %-EINVAL if given arguments are not valid,
475 * %-ENODATA if the property does not have a value,
476 * %-EPROTO if the property is not an array of numbers,
477 * %-EOVERFLOW if the size of the property is not as expected,
478 * %-ENXIO if no suitable firmware interface is present.
479 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300480int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100481 const char *propname, u8 *val, size_t nval)
482{
Sakari Ailus37081842017-06-06 12:37:37 +0300483 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
484 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100485}
486EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
487
488/**
489 * fwnode_property_read_u16_array - return a u16 array property of firmware node
490 * @fwnode: Firmware node to get the property of
491 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200492 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100493 * @nval: Size of the @val array
494 *
495 * Read an array of u16 properties with @propname from @fwnode and store them to
496 * @val if found.
497 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200498 * Return: number of values if @val was %NULL,
499 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100500 * %-EINVAL if given arguments are not valid,
501 * %-ENODATA if the property does not have a value,
502 * %-EPROTO if the property is not an array of numbers,
503 * %-EOVERFLOW if the size of the property is not as expected,
504 * %-ENXIO if no suitable firmware interface is present.
505 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300506int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100507 const char *propname, u16 *val, size_t nval)
508{
Sakari Ailus37081842017-06-06 12:37:37 +0300509 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
510 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100511}
512EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
513
514/**
515 * fwnode_property_read_u32_array - return a u32 array property of firmware node
516 * @fwnode: Firmware node to get the property of
517 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200518 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100519 * @nval: Size of the @val array
520 *
521 * Read an array of u32 properties with @propname from @fwnode store them to
522 * @val if found.
523 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200524 * Return: number of values if @val was %NULL,
525 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100526 * %-EINVAL if given arguments are not valid,
527 * %-ENODATA if the property does not have a value,
528 * %-EPROTO if the property is not an array of numbers,
529 * %-EOVERFLOW if the size of the property is not as expected,
530 * %-ENXIO if no suitable firmware interface is present.
531 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300532int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100533 const char *propname, u32 *val, size_t nval)
534{
Sakari Ailus37081842017-06-06 12:37:37 +0300535 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
536 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100537}
538EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
539
540/**
541 * fwnode_property_read_u64_array - return a u64 array property firmware node
542 * @fwnode: Firmware node to get the property of
543 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200544 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100545 * @nval: Size of the @val array
546 *
547 * Read an array of u64 properties with @propname from @fwnode and store them to
548 * @val if found.
549 *
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200550 * Return: number of values if @val was %NULL,
551 * %0 if the property was found (success),
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100552 * %-EINVAL if given arguments are not valid,
553 * %-ENODATA if the property does not have a value,
554 * %-EPROTO if the property is not an array of numbers,
555 * %-EOVERFLOW if the size of the property is not as expected,
556 * %-ENXIO if no suitable firmware interface is present.
557 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300558int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100559 const char *propname, u64 *val, size_t nval)
560{
Sakari Ailus37081842017-06-06 12:37:37 +0300561 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
562 val, nval);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100563}
564EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
565
566/**
567 * fwnode_property_read_string_array - return string array property of a node
568 * @fwnode: Firmware node to get the property of
569 * @propname: Name of the property
Adrian Hunter5c0acf32015-03-17 09:58:58 +0200570 * @val: The values are stored here or %NULL to return the number of values
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100571 * @nval: Size of the @val array
572 *
573 * Read an string list property @propname from the given firmware node and store
574 * them to @val if found.
575 *
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300576 * Return: number of values read on success if @val is non-NULL,
577 * number of values available on success if @val is NULL,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100578 * %-EINVAL if given arguments are not valid,
579 * %-ENODATA if the property does not have a value,
Sakari Ailus026b8212017-03-28 15:22:17 +0300580 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100581 * %-EOVERFLOW if the size of the property is not as expected,
582 * %-ENXIO if no suitable firmware interface is present.
583 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300584int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100585 const char *propname, const char **val,
586 size_t nval)
587{
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200588 int ret;
589
Sakari Ailus37081842017-06-06 12:37:37 +0300590 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
591 val, nval);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200592 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
593 !IS_ERR_OR_NULL(fwnode->secondary))
Sakari Ailus37081842017-06-06 12:37:37 +0300594 ret = fwnode_call_int_op(fwnode->secondary,
595 property_read_string_array, propname,
596 val, nval);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200597 return ret;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100598}
599EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
600
601/**
602 * fwnode_property_read_string - return a string property of a firmware node
603 * @fwnode: Firmware node to get the property of
604 * @propname: Name of the property
605 * @val: The value is stored here
606 *
607 * Read property @propname from the given firmware node and store the value into
608 * @val if found. The value is checked to be a string.
609 *
610 * Return: %0 if the property was found (success),
611 * %-EINVAL if given arguments are not valid,
612 * %-ENODATA if the property does not have a value,
613 * %-EPROTO or %-EILSEQ if the property is not a string,
614 * %-ENXIO if no suitable firmware interface is present.
615 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300616int fwnode_property_read_string(const struct fwnode_handle *fwnode,
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100617 const char *propname, const char **val)
618{
Sakari Ailuse4817472017-03-28 15:26:22 +0300619 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
Andy Shevchenko362c0b32015-11-30 17:11:36 +0200620
Sakari Ailusb0b027c2017-03-28 15:22:19 +0300621 return ret < 0 ? ret : 0;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100622}
623EXPORT_SYMBOL_GPL(fwnode_property_read_string);
624
625/**
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300626 * fwnode_property_match_string - find a string in an array and return index
627 * @fwnode: Firmware node to get the property of
628 * @propname: Name of the property holding the array
629 * @string: String to look for
630 *
631 * Find a given string in a string array and if it is found return the
632 * index back.
633 *
634 * Return: %0 if the property was found (success),
635 * %-EINVAL if given arguments are not valid,
636 * %-ENODATA if the property does not have a value,
637 * %-EPROTO if the property is not an array of strings,
638 * %-ENXIO if no suitable firmware interface is present.
639 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300640int fwnode_property_match_string(const struct fwnode_handle *fwnode,
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300641 const char *propname, const char *string)
642{
643 const char **values;
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700644 int nval, ret;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300645
646 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
647 if (nval < 0)
648 return nval;
649
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200650 if (nval == 0)
651 return -ENODATA;
652
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300653 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
654 if (!values)
655 return -ENOMEM;
656
657 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
658 if (ret < 0)
659 goto out;
660
Andy Shevchenkoa7c1d0a2016-03-17 14:22:17 -0700661 ret = match_string(values, nval, string);
662 if (ret < 0)
663 ret = -ENODATA;
Mika Westerberg3f5c8d32015-09-14 17:37:35 +0300664out:
665 kfree(values);
666 return ret;
667}
668EXPORT_SYMBOL_GPL(fwnode_property_match_string);
669
Sakari Ailus3e3119d2017-07-21 15:11:49 +0300670/**
671 * fwnode_property_get_reference_args() - Find a reference with arguments
672 * @fwnode: Firmware node where to look for the reference
673 * @prop: The name of the property
674 * @nargs_prop: The name of the property telling the number of
675 * arguments in the referred node. NULL if @nargs is known,
676 * otherwise @nargs is ignored. Only relevant on OF.
677 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
678 * @index: Index of the reference, from zero onwards.
679 * @args: Result structure with reference and integer arguments.
680 *
681 * Obtain a reference based on a named property in an fwnode, with
682 * integer arguments.
683 *
684 * Caller is responsible to call fwnode_handle_put() on the returned
685 * args->fwnode pointer.
686 *
Sakari Ailusc343bc22017-09-26 12:08:27 +0300687 * Returns: %0 on success
688 * %-ENOENT when the index is out of bounds, the index has an empty
689 * reference or the property was not found
690 * %-EINVAL on parse error
Sakari Ailus3e3119d2017-07-21 15:11:49 +0300691 */
692int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
693 const char *prop, const char *nargs_prop,
694 unsigned int nargs, unsigned int index,
695 struct fwnode_reference_args *args)
696{
697 return fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
698 nargs, index, args);
699}
700EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
701
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800702static int property_copy_string_array(struct property_entry *dst,
703 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200704{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800705 char **d;
706 size_t nval = src->length / sizeof(*d);
707 int i;
Mika Westerberg13141e12015-11-30 17:11:37 +0200708
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800709 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
710 if (!d)
711 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200712
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800713 for (i = 0; i < nval; i++) {
714 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
715 if (!d[i] && src->pointer.str[i]) {
716 while (--i >= 0)
717 kfree(d[i]);
718 kfree(d);
719 return -ENOMEM;
Mika Westerberg13141e12015-11-30 17:11:37 +0200720 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200721 }
722
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800723 dst->pointer.raw_data = d;
724 return 0;
Mika Westerberg13141e12015-11-30 17:11:37 +0200725}
726
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800727static int property_entry_copy_data(struct property_entry *dst,
728 const struct property_entry *src)
Mika Westerberg13141e12015-11-30 17:11:37 +0200729{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800730 int error;
Mika Westerberg13141e12015-11-30 17:11:37 +0200731
732 dst->name = kstrdup(src->name, GFP_KERNEL);
733 if (!dst->name)
734 return -ENOMEM;
735
736 if (src->is_array) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800737 if (!src->length) {
738 error = -ENODATA;
739 goto out_free_name;
740 }
Andy Shevchenkof6740c12015-12-29 13:07:50 +0200741
Mika Westerberg13141e12015-11-30 17:11:37 +0200742 if (src->is_string) {
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800743 error = property_copy_string_array(dst, src);
744 if (error)
745 goto out_free_name;
Mika Westerberg13141e12015-11-30 17:11:37 +0200746 } else {
747 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
748 src->length, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800749 if (!dst->pointer.raw_data) {
750 error = -ENOMEM;
751 goto out_free_name;
752 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200753 }
754 } else if (src->is_string) {
755 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800756 if (!dst->value.str && src->value.str) {
757 error = -ENOMEM;
758 goto out_free_name;
759 }
Mika Westerberg13141e12015-11-30 17:11:37 +0200760 } else {
761 dst->value.raw_data = src->value.raw_data;
762 }
763
764 dst->length = src->length;
765 dst->is_array = src->is_array;
766 dst->is_string = src->is_string;
767
768 return 0;
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800769
770out_free_name:
771 kfree(dst->name);
772 return error;
773}
774
775static void property_entry_free_data(const struct property_entry *p)
776{
777 size_t i, nval;
778
779 if (p->is_array) {
780 if (p->is_string && p->pointer.str) {
781 nval = p->length / sizeof(const char *);
782 for (i = 0; i < nval; i++)
783 kfree(p->pointer.str[i]);
784 }
785 kfree(p->pointer.raw_data);
786 } else if (p->is_string) {
787 kfree(p->value.str);
788 }
789 kfree(p->name);
790}
791
792/**
793 * property_entries_dup - duplicate array of properties
794 * @properties: array of properties to copy
795 *
796 * This function creates a deep copy of the given NULL-terminated array
797 * of property entries.
798 */
799struct property_entry *
800property_entries_dup(const struct property_entry *properties)
801{
802 struct property_entry *p;
803 int i, n = 0;
804
805 while (properties[n].name)
806 n++;
807
808 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
809 if (!p)
810 return ERR_PTR(-ENOMEM);
811
812 for (i = 0; i < n; i++) {
813 int ret = property_entry_copy_data(&p[i], &properties[i]);
814 if (ret) {
815 while (--i >= 0)
816 property_entry_free_data(&p[i]);
817 kfree(p);
818 return ERR_PTR(ret);
819 }
820 }
821
822 return p;
823}
824EXPORT_SYMBOL_GPL(property_entries_dup);
825
826/**
827 * property_entries_free - free previously allocated array of properties
828 * @properties: array of properties to destroy
829 *
830 * This function frees given NULL-terminated array of property entries,
831 * along with their data.
832 */
833void property_entries_free(const struct property_entry *properties)
834{
835 const struct property_entry *p;
836
837 for (p = properties; p->name; p++)
838 property_entry_free_data(p);
839
840 kfree(properties);
841}
842EXPORT_SYMBOL_GPL(property_entries_free);
843
844/**
845 * pset_free_set - releases memory allocated for copied property set
846 * @pset: Property set to release
847 *
848 * Function takes previously copied property set and releases all the
849 * memory allocated to it.
850 */
851static void pset_free_set(struct property_set *pset)
852{
853 if (!pset)
854 return;
855
856 property_entries_free(pset->properties);
857 kfree(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200858}
859
860/**
861 * pset_copy_set - copies property set
862 * @pset: Property set to copy
863 *
864 * This function takes a deep copy of the given property set and returns
865 * pointer to the copy. Call device_free_property_set() to free resources
866 * allocated in this function.
867 *
868 * Return: Pointer to the new property set or error pointer.
869 */
870static struct property_set *pset_copy_set(const struct property_set *pset)
871{
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800872 struct property_entry *properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200873 struct property_set *p;
Mika Westerberg13141e12015-11-30 17:11:37 +0200874
875 p = kzalloc(sizeof(*p), GFP_KERNEL);
876 if (!p)
877 return ERR_PTR(-ENOMEM);
878
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800879 properties = property_entries_dup(pset->properties);
880 if (IS_ERR(properties)) {
Mika Westerberg13141e12015-11-30 17:11:37 +0200881 kfree(p);
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800882 return ERR_CAST(properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200883 }
884
Dmitry Torokhov2d479e12017-02-02 17:41:27 -0800885 p->properties = properties;
Mika Westerberg13141e12015-11-30 17:11:37 +0200886 return p;
887}
888
889/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300890 * device_remove_properties - Remove properties from a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200891 * @dev: Device whose properties to remove.
892 *
893 * The function removes properties previously associated to the device
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300894 * secondary firmware node with device_add_properties(). Memory allocated
Mika Westerberg13141e12015-11-30 17:11:37 +0200895 * to the properties will also be released.
896 */
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300897void device_remove_properties(struct device *dev)
Mika Westerberg13141e12015-11-30 17:11:37 +0200898{
899 struct fwnode_handle *fwnode;
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300900 struct property_set *pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200901
902 fwnode = dev_fwnode(dev);
903 if (!fwnode)
904 return;
905 /*
906 * Pick either primary or secondary node depending which one holds
907 * the pset. If there is no real firmware node (ACPI/DT) primary
908 * will hold the pset.
909 */
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300910 pset = to_pset_node(fwnode);
911 if (pset) {
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200912 set_primary_fwnode(dev, NULL);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200913 } else {
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300914 pset = to_pset_node(fwnode->secondary);
915 if (pset && dev == pset->dev)
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200916 set_secondary_fwnode(dev, NULL);
Heikki Krogerus0d67e0f2016-03-10 13:03:18 +0200917 }
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300918 if (pset && dev == pset->dev)
919 pset_free_set(pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200920}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300921EXPORT_SYMBOL_GPL(device_remove_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200922
923/**
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300924 * device_add_properties - Add a collection of properties to a device object.
Mika Westerberg13141e12015-11-30 17:11:37 +0200925 * @dev: Device to add properties to.
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300926 * @properties: Collection of properties to add.
Mika Westerberg13141e12015-11-30 17:11:37 +0200927 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300928 * Associate a collection of device properties represented by @properties with
929 * @dev as its secondary firmware node. The function takes a copy of
930 * @properties.
Mika Westerberg13141e12015-11-30 17:11:37 +0200931 */
Dmitry Torokhovbec84da2017-02-02 17:41:25 -0800932int device_add_properties(struct device *dev,
933 const struct property_entry *properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200934{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300935 struct property_set *p, pset;
Mika Westerberg13141e12015-11-30 17:11:37 +0200936
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300937 if (!properties)
Mika Westerberg13141e12015-11-30 17:11:37 +0200938 return -EINVAL;
939
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300940 pset.properties = properties;
941
942 p = pset_copy_set(&pset);
Mika Westerberg13141e12015-11-30 17:11:37 +0200943 if (IS_ERR(p))
944 return PTR_ERR(p);
945
Sakari Ailus37081842017-06-06 12:37:37 +0300946 p->fwnode.ops = &pset_fwnode_ops;
Mika Westerberg13141e12015-11-30 17:11:37 +0200947 set_secondary_fwnode(dev, &p->fwnode);
Jarkko Nikula5ab894a2017-10-09 16:28:37 +0300948 p->dev = dev;
Mika Westerberg13141e12015-11-30 17:11:37 +0200949 return 0;
950}
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300951EXPORT_SYMBOL_GPL(device_add_properties);
Mika Westerberg13141e12015-11-30 17:11:37 +0200952
953/**
Sakari Ailus23387252017-03-28 10:52:26 +0300954 * fwnode_get_next_parent - Iterate to the node's parent
955 * @fwnode: Firmware whose parent is retrieved
956 *
957 * This is like fwnode_get_parent() except that it drops the refcount
958 * on the passed node, making it suitable for iterating through a
959 * node's parents.
960 *
961 * Returns a node pointer with refcount incremented, use
962 * fwnode_handle_node() on it when done.
963 */
964struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
965{
966 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
967
968 fwnode_handle_put(fwnode);
969
970 return parent;
971}
972EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
973
974/**
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300975 * fwnode_get_parent - Return parent firwmare node
976 * @fwnode: Firmware whose parent is retrieved
977 *
978 * Return parent firmware node of the given node if possible or %NULL if no
979 * parent was available.
980 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300981struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300982{
Sakari Ailus37081842017-06-06 12:37:37 +0300983 return fwnode_call_ptr_op(fwnode, get_parent);
Mika Westerbergafaf26f2017-03-28 10:52:17 +0300984}
985EXPORT_SYMBOL_GPL(fwnode_get_parent);
986
987/**
Mika Westerberg34055192017-03-28 10:52:18 +0300988 * fwnode_get_next_child_node - Return the next child node handle for a node
989 * @fwnode: Firmware node to find the next child node for.
990 * @child: Handle to one of the node's child nodes or a %NULL handle.
991 */
Sakari Ailus37ba9832017-07-21 14:39:36 +0300992struct fwnode_handle *
993fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
994 struct fwnode_handle *child)
Mika Westerberg34055192017-03-28 10:52:18 +0300995{
Sakari Ailus37081842017-06-06 12:37:37 +0300996 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
Mika Westerberg34055192017-03-28 10:52:18 +0300997}
998EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
999
1000/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001001 * device_get_next_child_node - Return the next child node handle for a device
1002 * @dev: Device to find the next child node for.
1003 * @child: Handle to one of the device's child nodes or a null handle.
1004 */
1005struct fwnode_handle *device_get_next_child_node(struct device *dev,
1006 struct fwnode_handle *child)
1007{
Mika Westerberg34055192017-03-28 10:52:18 +03001008 struct acpi_device *adev = ACPI_COMPANION(dev);
1009 struct fwnode_handle *fwnode = NULL;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001010
Mika Westerberg34055192017-03-28 10:52:18 +03001011 if (dev->of_node)
1012 fwnode = &dev->of_node->fwnode;
1013 else if (adev)
1014 fwnode = acpi_fwnode_handle(adev);
1015
1016 return fwnode_get_next_child_node(fwnode, child);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001017}
1018EXPORT_SYMBOL_GPL(device_get_next_child_node);
1019
1020/**
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001021 * fwnode_get_named_child_node - Return first matching named child node handle
1022 * @fwnode: Firmware node to find the named child node for.
Adam Thomson613e9722016-06-21 18:50:20 +01001023 * @childname: String to match child node name against.
1024 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001025struct fwnode_handle *
1026fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
1027 const char *childname)
Adam Thomson613e9722016-06-21 18:50:20 +01001028{
Sakari Ailus37081842017-06-06 12:37:37 +03001029 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
Adam Thomson613e9722016-06-21 18:50:20 +01001030}
Mika Westerberg21ea73f2017-03-28 10:52:19 +03001031EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1032
1033/**
1034 * device_get_named_child_node - Return first matching named child node handle
1035 * @dev: Device to find the named child node for.
1036 * @childname: String to match child node name against.
1037 */
1038struct fwnode_handle *device_get_named_child_node(struct device *dev,
1039 const char *childname)
1040{
1041 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1042}
Adam Thomson613e9722016-06-21 18:50:20 +01001043EXPORT_SYMBOL_GPL(device_get_named_child_node);
1044
1045/**
Sakari Ailuse7887c22017-03-28 10:52:22 +03001046 * fwnode_handle_get - Obtain a reference to a device node
1047 * @fwnode: Pointer to the device node to obtain the reference to.
Sakari Ailuscf89a312017-09-19 12:39:11 +03001048 *
1049 * Returns the fwnode handle.
Sakari Ailuse7887c22017-03-28 10:52:22 +03001050 */
Sakari Ailuscf89a312017-09-19 12:39:11 +03001051struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
Sakari Ailuse7887c22017-03-28 10:52:22 +03001052{
Sakari Ailuscf89a312017-09-19 12:39:11 +03001053 if (!fwnode_has_op(fwnode, get))
1054 return fwnode;
1055
1056 return fwnode_call_ptr_op(fwnode, get);
Sakari Ailuse7887c22017-03-28 10:52:22 +03001057}
1058EXPORT_SYMBOL_GPL(fwnode_handle_get);
1059
1060/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001061 * fwnode_handle_put - Drop reference to a device node
1062 * @fwnode: Pointer to the device node to drop the reference to.
1063 *
1064 * This has to be used when terminating device_for_each_child_node() iteration
1065 * with break or return to prevent stale device node references from being left
1066 * behind.
1067 */
1068void fwnode_handle_put(struct fwnode_handle *fwnode)
1069{
Sakari Ailus37081842017-06-06 12:37:37 +03001070 fwnode_call_void_op(fwnode, put);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001071}
1072EXPORT_SYMBOL_GPL(fwnode_handle_put);
1073
1074/**
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001075 * fwnode_device_is_available - check if a device is available for use
1076 * @fwnode: Pointer to the fwnode of the device.
1077 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001078bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001079{
Sakari Ailuse8158b482017-07-11 18:20:20 +03001080 return fwnode_call_bool_op(fwnode, device_is_available);
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001081}
1082EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1083
1084/**
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001085 * device_get_child_node_count - return the number of child nodes for device
1086 * @dev: Device to cound the child nodes for
1087 */
1088unsigned int device_get_child_node_count(struct device *dev)
1089{
1090 struct fwnode_handle *child;
1091 unsigned int count = 0;
1092
1093 device_for_each_child_node(dev, child)
1094 count++;
1095
1096 return count;
1097}
1098EXPORT_SYMBOL_GPL(device_get_child_node_count);
Suthikulpanit, Suravee05ca5562015-06-10 11:08:54 -05001099
Suthikulpanit, Suraveee5e55862015-10-28 15:50:49 -07001100bool device_dma_supported(struct device *dev)
1101{
1102 /* For DT, this is always supported.
1103 * For ACPI, this depends on CCA, which
1104 * is determined by the acpi_dma_supported().
1105 */
1106 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1107 return true;
1108
1109 return acpi_dma_supported(ACPI_COMPANION(dev));
1110}
1111EXPORT_SYMBOL_GPL(device_dma_supported);
1112
1113enum dev_dma_attr device_get_dma_attr(struct device *dev)
1114{
1115 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1116
1117 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1118 if (of_dma_is_coherent(dev->of_node))
1119 attr = DEV_DMA_COHERENT;
1120 else
1121 attr = DEV_DMA_NON_COHERENT;
1122 } else
1123 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1124
1125 return attr;
1126}
1127EXPORT_SYMBOL_GPL(device_get_dma_attr);
1128
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001129/**
Marcin Wojtasb28f2632018-01-18 13:31:39 +01001130 * fwnode_get_phy_mode - Get phy mode for given firmware node
1131 * @fwnode: Pointer to the given node
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001132 *
1133 * The function gets phy interface string from property 'phy-mode' or
1134 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1135 * error case.
1136 */
Marcin Wojtasb28f2632018-01-18 13:31:39 +01001137int fwnode_get_phy_mode(struct fwnode_handle *fwnode)
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001138{
1139 const char *pm;
1140 int err, i;
1141
Marcin Wojtasb28f2632018-01-18 13:31:39 +01001142 err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001143 if (err < 0)
Marcin Wojtasb28f2632018-01-18 13:31:39 +01001144 err = fwnode_property_read_string(fwnode,
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001145 "phy-connection-type", &pm);
1146 if (err < 0)
1147 return err;
1148
1149 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1150 if (!strcasecmp(pm, phy_modes(i)))
1151 return i;
1152
1153 return -ENODEV;
1154}
Marcin Wojtasb28f2632018-01-18 13:31:39 +01001155EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
1156
1157/**
1158 * device_get_phy_mode - Get phy mode for given device
1159 * @dev: Pointer to the given device
1160 *
1161 * The function gets phy interface string from property 'phy-mode' or
1162 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1163 * error case.
1164 */
1165int device_get_phy_mode(struct device *dev)
1166{
1167 return fwnode_get_phy_mode(dev_fwnode(dev));
1168}
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001169EXPORT_SYMBOL_GPL(device_get_phy_mode);
1170
Marcin Wojtasbabe2db2018-01-18 13:31:38 +01001171static void *fwnode_get_mac_addr(struct fwnode_handle *fwnode,
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001172 const char *name, char *addr,
1173 int alen)
1174{
Marcin Wojtasbabe2db2018-01-18 13:31:38 +01001175 int ret = fwnode_property_read_u8_array(fwnode, name, addr, alen);
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001176
Jeremy Linton2f710a32015-08-19 11:46:42 -05001177 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001178 return addr;
1179 return NULL;
1180}
1181
1182/**
Marcin Wojtasbabe2db2018-01-18 13:31:38 +01001183 * fwnode_get_mac_address - Get the MAC from the firmware node
1184 * @fwnode: Pointer to the firmware node
Jeremy Linton2f710a32015-08-19 11:46:42 -05001185 * @addr: Address of buffer to store the MAC in
1186 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1187 *
1188 * Search the firmware node for the best MAC address to use. 'mac-address' is
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001189 * checked first, because that is supposed to contain to "most recent" MAC
1190 * address. If that isn't set, then 'local-mac-address' is checked next,
1191 * because that is the default address. If that isn't set, then the obsolete
1192 * 'address' is checked, just in case we're using an old device tree.
1193 *
1194 * Note that the 'address' property is supposed to contain a virtual address of
1195 * the register set, but some DTS files have redefined that property to be the
1196 * MAC address.
1197 *
1198 * All-zero MAC addresses are rejected, because those could be properties that
Jeremy Linton2f710a32015-08-19 11:46:42 -05001199 * exist in the firmware tables, but were not updated by the firmware. For
1200 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1201 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1202 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1203 * exists but is all zeros.
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001204*/
Marcin Wojtasbabe2db2018-01-18 13:31:38 +01001205void *fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr, int alen)
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001206{
Julien Grall5b902d62015-09-03 23:59:50 +01001207 char *res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001208
Marcin Wojtasbabe2db2018-01-18 13:31:38 +01001209 res = fwnode_get_mac_addr(fwnode, "mac-address", addr, alen);
Julien Grall5b902d62015-09-03 23:59:50 +01001210 if (res)
1211 return res;
1212
Marcin Wojtasbabe2db2018-01-18 13:31:38 +01001213 res = fwnode_get_mac_addr(fwnode, "local-mac-address", addr, alen);
Julien Grall5b902d62015-09-03 23:59:50 +01001214 if (res)
1215 return res;
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001216
Marcin Wojtasbabe2db2018-01-18 13:31:38 +01001217 return fwnode_get_mac_addr(fwnode, "address", addr, alen);
1218}
1219EXPORT_SYMBOL(fwnode_get_mac_address);
1220
1221/**
1222 * device_get_mac_address - Get the MAC for a given device
1223 * @dev: Pointer to the device
1224 * @addr: Address of buffer to store the MAC in
1225 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1226 */
1227void *device_get_mac_address(struct device *dev, char *addr, int alen)
1228{
1229 return fwnode_get_mac_address(dev_fwnode(dev), addr, alen);
Jeremy Linton4c96b7d2015-08-12 17:06:26 -05001230}
1231EXPORT_SYMBOL(device_get_mac_address);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001232
1233/**
Marcin Wojtas7c6c57f2018-01-18 13:31:40 +01001234 * fwnode_irq_get - Get IRQ directly from a fwnode
1235 * @fwnode: Pointer to the firmware node
1236 * @index: Zero-based index of the IRQ
1237 *
1238 * Returns Linux IRQ number on success. Other values are determined
1239 * accordingly to acpi_/of_ irq_get() operation.
1240 */
1241int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index)
1242{
1243 struct device_node *of_node = to_of_node(fwnode);
1244 struct resource res;
1245 int ret;
1246
1247 if (IS_ENABLED(CONFIG_OF) && of_node)
1248 return of_irq_get(of_node, index);
1249
1250 ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
1251 if (ret)
1252 return ret;
1253
1254 return res.start;
1255}
1256EXPORT_SYMBOL(fwnode_irq_get);
1257
1258/**
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001259 * device_graph_get_next_endpoint - Get next endpoint firmware node
1260 * @fwnode: Pointer to the parent firmware node
1261 * @prev: Previous endpoint node or %NULL to get the first
1262 *
1263 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1264 * are available.
1265 */
1266struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001267fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001268 struct fwnode_handle *prev)
1269{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001270 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001271}
1272EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1273
1274/**
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001275 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1276 * @endpoint: Endpoint firmware node of the port
1277 *
1278 * Return: the firmware node of the device the @endpoint belongs to.
1279 */
1280struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001281fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001282{
1283 struct fwnode_handle *port, *parent;
1284
1285 port = fwnode_get_parent(endpoint);
1286 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1287
1288 fwnode_handle_put(port);
1289
1290 return parent;
1291}
1292EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1293
1294/**
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001295 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1296 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1297 *
1298 * Extracts firmware node of a remote device the @fwnode points to.
1299 */
1300struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001301fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001302{
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001303 struct fwnode_handle *endpoint, *parent;
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001304
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001305 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1306 parent = fwnode_graph_get_port_parent(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001307
Kieran Bingham6a71d8d2017-06-06 12:37:41 +03001308 fwnode_handle_put(endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001309
1310 return parent;
1311}
1312EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1313
1314/**
1315 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1316 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1317 *
1318 * Extracts firmware node of a remote port the @fwnode points to.
1319 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001320struct fwnode_handle *
1321fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001322{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001323 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001324}
1325EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1326
1327/**
1328 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1329 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1330 *
1331 * Extracts firmware node of a remote endpoint the @fwnode points to.
1332 */
1333struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +03001334fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001335{
Sakari Ailus3b27d002017-06-06 12:37:38 +03001336 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
Mika Westerberg07bb80d2017-03-28 10:52:21 +03001337}
1338EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001339
1340/**
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001341 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1342 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1343 * @port_id: identifier of the parent port node
1344 * @endpoint_id: identifier of the endpoint node
1345 *
1346 * Return: Remote fwnode handle associated with remote endpoint node linked
1347 * to @node. Use fwnode_node_put() on it when done.
1348 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001349struct fwnode_handle *
1350fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1351 u32 endpoint_id)
Sakari Ailus125ee6b2017-06-06 12:37:40 +03001352{
1353 struct fwnode_handle *endpoint = NULL;
1354
1355 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1356 struct fwnode_endpoint fwnode_ep;
1357 struct fwnode_handle *remote;
1358 int ret;
1359
1360 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1361 if (ret < 0)
1362 continue;
1363
1364 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1365 continue;
1366
1367 remote = fwnode_graph_get_remote_port_parent(endpoint);
1368 if (!remote)
1369 return NULL;
1370
1371 return fwnode_device_is_available(remote) ? remote : NULL;
1372 }
1373
1374 return NULL;
1375}
1376EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1377
1378/**
Sakari Ailus2bd54522017-03-28 10:52:25 +03001379 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1380 * @fwnode: pointer to endpoint fwnode_handle
1381 * @endpoint: pointer to the fwnode endpoint data structure
1382 *
1383 * Parse @fwnode representing a graph endpoint node and store the
1384 * information in @endpoint. The caller must hold a reference to
1385 * @fwnode.
1386 */
Sakari Ailus37ba9832017-07-21 14:39:36 +03001387int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus2bd54522017-03-28 10:52:25 +03001388 struct fwnode_endpoint *endpoint)
1389{
Sakari Ailus2bd54522017-03-28 10:52:25 +03001390 memset(endpoint, 0, sizeof(*endpoint));
1391
Sakari Ailus3b27d002017-06-06 12:37:38 +03001392 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
Sakari Ailus2bd54522017-03-28 10:52:25 +03001393}
1394EXPORT_SYMBOL(fwnode_graph_parse_endpoint);