]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/base/property.c
Linux 4.14-rc4
[mirror_ubuntu-bionic-kernel.git] / drivers / base / property.c
CommitLineData
b31384fa
RW
1/*
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
b31384fa 13#include <linux/acpi.h>
16ba08d5
RW
14#include <linux/export.h>
15#include <linux/kernel.h>
b31384fa 16#include <linux/of.h>
05ca5560 17#include <linux/of_address.h>
07bb80d4 18#include <linux/of_graph.h>
16ba08d5 19#include <linux/property.h>
4c96b7dc
JL
20#include <linux/etherdevice.h>
21#include <linux/phy.h>
16ba08d5 22
f4d05266
HK
23struct property_set {
24 struct fwnode_handle fwnode;
bec84da8 25 const struct property_entry *properties;
f4d05266
HK
26};
27
db3e50f3
SA
28static const struct fwnode_operations pset_fwnode_ops;
29
39e5aeed 30static inline bool is_pset_node(const struct fwnode_handle *fwnode)
16ba08d5 31{
db3e50f3 32 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
16ba08d5
RW
33}
34
39e5aeed
SA
35#define to_pset_node(__fwnode) \
36 ({ \
37 typeof(__fwnode) __to_pset_node_fwnode = __fwnode; \
38 \
39 is_pset_node(__to_pset_node_fwnode) ? \
40 container_of(__to_pset_node_fwnode, \
41 struct property_set, fwnode) : \
42 NULL; \
43 })
44
45static const struct property_entry *
46pset_prop_get(const struct property_set *pset, const char *name)
16ba08d5 47{
bec84da8 48 const struct property_entry *prop;
16ba08d5
RW
49
50 if (!pset || !pset->properties)
51 return NULL;
52
53 for (prop = pset->properties; prop->name; prop++)
54 if (!strcmp(name, prop->name))
55 return prop;
56
57 return NULL;
58}
59
39e5aeed 60static const void *pset_prop_find(const struct property_set *pset,
bec84da8 61 const char *propname, size_t length)
16ba08d5 62{
bec84da8
DT
63 const struct property_entry *prop;
64 const void *pointer;
16ba08d5 65
318a1971
AS
66 prop = pset_prop_get(pset, propname);
67 if (!prop)
68 return ERR_PTR(-EINVAL);
66586bab
AS
69 if (prop->is_array)
70 pointer = prop->pointer.raw_data;
71 else
72 pointer = &prop->value.raw_data;
318a1971
AS
73 if (!pointer)
74 return ERR_PTR(-ENODATA);
75 if (length > prop->length)
76 return ERR_PTR(-EOVERFLOW);
77 return pointer;
78}
79
39e5aeed 80static int pset_prop_read_u8_array(const struct property_set *pset,
318a1971
AS
81 const char *propname,
82 u8 *values, size_t nval)
83{
bec84da8 84 const void *pointer;
318a1971
AS
85 size_t length = nval * sizeof(*values);
86
87 pointer = pset_prop_find(pset, propname, length);
88 if (IS_ERR(pointer))
89 return PTR_ERR(pointer);
90
91 memcpy(values, pointer, length);
92 return 0;
93}
94
39e5aeed 95static int pset_prop_read_u16_array(const struct property_set *pset,
318a1971
AS
96 const char *propname,
97 u16 *values, size_t nval)
98{
bec84da8 99 const void *pointer;
318a1971
AS
100 size_t length = nval * sizeof(*values);
101
102 pointer = pset_prop_find(pset, propname, length);
103 if (IS_ERR(pointer))
104 return PTR_ERR(pointer);
105
106 memcpy(values, pointer, length);
107 return 0;
108}
109
39e5aeed 110static int pset_prop_read_u32_array(const struct property_set *pset,
318a1971
AS
111 const char *propname,
112 u32 *values, size_t nval)
113{
bec84da8 114 const void *pointer;
318a1971
AS
115 size_t length = nval * sizeof(*values);
116
117 pointer = pset_prop_find(pset, propname, length);
118 if (IS_ERR(pointer))
119 return PTR_ERR(pointer);
120
121 memcpy(values, pointer, length);
122 return 0;
123}
124
39e5aeed 125static int pset_prop_read_u64_array(const struct property_set *pset,
318a1971
AS
126 const char *propname,
127 u64 *values, size_t nval)
128{
bec84da8 129 const void *pointer;
318a1971
AS
130 size_t length = nval * sizeof(*values);
131
132 pointer = pset_prop_find(pset, propname, length);
133 if (IS_ERR(pointer))
134 return PTR_ERR(pointer);
135
136 memcpy(values, pointer, length);
137 return 0;
138}
139
39e5aeed 140static int pset_prop_count_elems_of_size(const struct property_set *pset,
318a1971
AS
141 const char *propname, size_t length)
142{
bec84da8 143 const struct property_entry *prop;
318a1971
AS
144
145 prop = pset_prop_get(pset, propname);
16ba08d5 146 if (!prop)
16ba08d5 147 return -EINVAL;
318a1971
AS
148
149 return prop->length / length;
150}
151
39e5aeed 152static int pset_prop_read_string_array(const struct property_set *pset,
318a1971
AS
153 const char *propname,
154 const char **strings, size_t nval)
155{
0f194992 156 const struct property_entry *prop;
bec84da8 157 const void *pointer;
0f194992
SA
158 size_t array_len, length;
159
160 /* Find out the array length. */
161 prop = pset_prop_get(pset, propname);
162 if (!prop)
163 return -EINVAL;
164
165 if (!prop->is_array)
166 /* The array length for a non-array string property is 1. */
167 array_len = 1;
168 else
169 /* Find the length of an array. */
170 array_len = pset_prop_count_elems_of_size(pset, propname,
171 sizeof(const char *));
172
173 /* Return how many there are if strings is NULL. */
174 if (!strings)
175 return array_len;
176
177 array_len = min(nval, array_len);
178 length = array_len * sizeof(*strings);
318a1971
AS
179
180 pointer = pset_prop_find(pset, propname, length);
181 if (IS_ERR(pointer))
182 return PTR_ERR(pointer);
183
184 memcpy(strings, pointer, length);
0f194992 185
b0b027ce 186 return array_len;
16ba08d5 187}
b31384fa 188
e44bb0cb 189struct fwnode_handle *dev_fwnode(struct device *dev)
9017f252
RW
190{
191 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
192 &dev->of_node->fwnode : dev->fwnode;
193}
e44bb0cb 194EXPORT_SYMBOL_GPL(dev_fwnode);
b31384fa 195
37ba983c 196static bool pset_fwnode_property_present(const struct fwnode_handle *fwnode,
3708184a
SA
197 const char *propname)
198{
199 return !!pset_prop_get(to_pset_node(fwnode), propname);
200}
201
37ba983c 202static int pset_fwnode_read_int_array(const struct fwnode_handle *fwnode,
3708184a
SA
203 const char *propname,
204 unsigned int elem_size, void *val,
205 size_t nval)
206{
37ba983c 207 const struct property_set *node = to_pset_node(fwnode);
3708184a
SA
208
209 if (!val)
210 return pset_prop_count_elems_of_size(node, propname, elem_size);
211
212 switch (elem_size) {
213 case sizeof(u8):
214 return pset_prop_read_u8_array(node, propname, val, nval);
215 case sizeof(u16):
216 return pset_prop_read_u16_array(node, propname, val, nval);
217 case sizeof(u32):
218 return pset_prop_read_u32_array(node, propname, val, nval);
219 case sizeof(u64):
220 return pset_prop_read_u64_array(node, propname, val, nval);
221 }
222
223 return -ENXIO;
224}
225
37ba983c
SA
226static int
227pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
228 const char *propname,
229 const char **val, size_t nval)
3708184a
SA
230{
231 return pset_prop_read_string_array(to_pset_node(fwnode), propname,
232 val, nval);
233}
234
235static const struct fwnode_operations pset_fwnode_ops = {
236 .property_present = pset_fwnode_property_present,
237 .property_read_int_array = pset_fwnode_read_int_array,
238 .property_read_string_array = pset_fwnode_property_read_string_array,
239};
240
b31384fa
RW
241/**
242 * device_property_present - check if a property of a device is present
243 * @dev: Device whose property is being checked
244 * @propname: Name of the property
245 *
246 * Check if property @propname is present in the device firmware description.
247 */
248bool device_property_present(struct device *dev, const char *propname)
249{
9017f252 250 return fwnode_property_present(dev_fwnode(dev), propname);
b31384fa
RW
251}
252EXPORT_SYMBOL_GPL(device_property_present);
253
362c0b30
AS
254/**
255 * fwnode_property_present - check if a property of a firmware node is present
256 * @fwnode: Firmware node whose property to check
257 * @propname: Name of the property
258 */
37ba983c
SA
259bool fwnode_property_present(const struct fwnode_handle *fwnode,
260 const char *propname)
362c0b30
AS
261{
262 bool ret;
263
e8158b48 264 ret = fwnode_call_bool_op(fwnode, property_present, propname);
0d67e0fa
HK
265 if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
266 !IS_ERR_OR_NULL(fwnode->secondary))
e8158b48 267 ret = fwnode_call_bool_op(fwnode->secondary, property_present,
3708184a 268 propname);
362c0b30
AS
269 return ret;
270}
8a0662d9
RW
271EXPORT_SYMBOL_GPL(fwnode_property_present);
272
b31384fa
RW
273/**
274 * device_property_read_u8_array - return a u8 array property of a device
275 * @dev: Device to get the property of
276 * @propname: Name of the property
5c0acf3b 277 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
278 * @nval: Size of the @val array
279 *
280 * Function reads an array of u8 properties with @propname from the device
281 * firmware description and stores them to @val if found.
282 *
5c0acf3b
AH
283 * Return: number of values if @val was %NULL,
284 * %0 if the property was found (success),
b31384fa
RW
285 * %-EINVAL if given arguments are not valid,
286 * %-ENODATA if the property does not have a value,
287 * %-EPROTO if the property is not an array of numbers,
288 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 289 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
290 */
291int device_property_read_u8_array(struct device *dev, const char *propname,
292 u8 *val, size_t nval)
293{
9017f252 294 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
295}
296EXPORT_SYMBOL_GPL(device_property_read_u8_array);
297
298/**
299 * device_property_read_u16_array - return a u16 array property of a device
300 * @dev: Device to get the property of
301 * @propname: Name of the property
5c0acf3b 302 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
303 * @nval: Size of the @val array
304 *
305 * Function reads an array of u16 properties with @propname from the device
306 * firmware description and stores them to @val if found.
307 *
5c0acf3b
AH
308 * Return: number of values if @val was %NULL,
309 * %0 if the property was found (success),
b31384fa
RW
310 * %-EINVAL if given arguments are not valid,
311 * %-ENODATA if the property does not have a value,
312 * %-EPROTO if the property is not an array of numbers,
313 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 314 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
315 */
316int device_property_read_u16_array(struct device *dev, const char *propname,
317 u16 *val, size_t nval)
318{
9017f252 319 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
320}
321EXPORT_SYMBOL_GPL(device_property_read_u16_array);
322
323/**
324 * device_property_read_u32_array - return a u32 array property of a device
325 * @dev: Device to get the property of
326 * @propname: Name of the property
5c0acf3b 327 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
328 * @nval: Size of the @val array
329 *
330 * Function reads an array of u32 properties with @propname from the device
331 * firmware description and stores them to @val if found.
332 *
5c0acf3b
AH
333 * Return: number of values if @val was %NULL,
334 * %0 if the property was found (success),
b31384fa
RW
335 * %-EINVAL if given arguments are not valid,
336 * %-ENODATA if the property does not have a value,
337 * %-EPROTO if the property is not an array of numbers,
338 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 339 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
340 */
341int device_property_read_u32_array(struct device *dev, const char *propname,
342 u32 *val, size_t nval)
343{
9017f252 344 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
345}
346EXPORT_SYMBOL_GPL(device_property_read_u32_array);
347
348/**
349 * device_property_read_u64_array - return a u64 array property of a device
350 * @dev: Device to get the property of
351 * @propname: Name of the property
5c0acf3b 352 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
353 * @nval: Size of the @val array
354 *
355 * Function reads an array of u64 properties with @propname from the device
356 * firmware description and stores them to @val if found.
357 *
5c0acf3b
AH
358 * Return: number of values if @val was %NULL,
359 * %0 if the property was found (success),
b31384fa
RW
360 * %-EINVAL if given arguments are not valid,
361 * %-ENODATA if the property does not have a value,
362 * %-EPROTO if the property is not an array of numbers,
363 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 364 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
365 */
366int device_property_read_u64_array(struct device *dev, const char *propname,
367 u64 *val, size_t nval)
368{
9017f252 369 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
370}
371EXPORT_SYMBOL_GPL(device_property_read_u64_array);
372
373/**
374 * device_property_read_string_array - return a string array property of device
375 * @dev: Device to get the property of
376 * @propname: Name of the property
5c0acf3b 377 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
378 * @nval: Size of the @val array
379 *
380 * Function reads an array of string properties with @propname from the device
381 * firmware description and stores them to @val if found.
382 *
b0b027ce
SA
383 * Return: number of values read on success if @val is non-NULL,
384 * number of values available on success if @val is NULL,
b31384fa
RW
385 * %-EINVAL if given arguments are not valid,
386 * %-ENODATA if the property does not have a value,
387 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
388 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 389 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
390 */
391int device_property_read_string_array(struct device *dev, const char *propname,
392 const char **val, size_t nval)
393{
9017f252 394 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
395}
396EXPORT_SYMBOL_GPL(device_property_read_string_array);
397
398/**
399 * device_property_read_string - return a string property of a device
400 * @dev: Device to get the property of
401 * @propname: Name of the property
402 * @val: The value is stored here
403 *
404 * Function reads property @propname from the device firmware description and
405 * stores the value into @val if found. The value is checked to be a string.
406 *
407 * Return: %0 if the property was found (success),
408 * %-EINVAL if given arguments are not valid,
409 * %-ENODATA if the property does not have a value,
410 * %-EPROTO or %-EILSEQ if the property type is not a string.
4fa7508e 411 * %-ENXIO if no suitable firmware interface is present.
b31384fa
RW
412 */
413int device_property_read_string(struct device *dev, const char *propname,
414 const char **val)
415{
9017f252 416 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
b31384fa
RW
417}
418EXPORT_SYMBOL_GPL(device_property_read_string);
8a0662d9 419
3f5c8d31
MW
420/**
421 * device_property_match_string - find a string in an array and return index
422 * @dev: Device to get the property of
423 * @propname: Name of the property holding the array
424 * @string: String to look for
425 *
426 * Find a given string in a string array and if it is found return the
427 * index back.
428 *
429 * Return: %0 if the property was found (success),
430 * %-EINVAL if given arguments are not valid,
431 * %-ENODATA if the property does not have a value,
432 * %-EPROTO if the property is not an array of strings,
433 * %-ENXIO if no suitable firmware interface is present.
434 */
435int device_property_match_string(struct device *dev, const char *propname,
436 const char *string)
437{
438 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
439}
440EXPORT_SYMBOL_GPL(device_property_match_string);
441
37ba983c 442static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
3708184a
SA
443 const char *propname,
444 unsigned int elem_size, void *val,
445 size_t nval)
446{
447 int ret;
448
449 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
450 elem_size, val, nval);
451 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
452 !IS_ERR_OR_NULL(fwnode->secondary))
453 ret = fwnode_call_int_op(
454 fwnode->secondary, property_read_int_array, propname,
455 elem_size, val, nval);
456
457 return ret;
458}
362c0b30 459
8a0662d9
RW
460/**
461 * fwnode_property_read_u8_array - return a u8 array property of firmware node
462 * @fwnode: Firmware node to get the property of
463 * @propname: Name of the property
5c0acf3b 464 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
465 * @nval: Size of the @val array
466 *
467 * Read an array of u8 properties with @propname from @fwnode and stores them to
468 * @val if found.
469 *
5c0acf3b
AH
470 * Return: number of values if @val was %NULL,
471 * %0 if the property was found (success),
8a0662d9
RW
472 * %-EINVAL if given arguments are not valid,
473 * %-ENODATA if the property does not have a value,
474 * %-EPROTO if the property is not an array of numbers,
475 * %-EOVERFLOW if the size of the property is not as expected,
476 * %-ENXIO if no suitable firmware interface is present.
477 */
37ba983c 478int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
479 const char *propname, u8 *val, size_t nval)
480{
3708184a
SA
481 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
482 val, nval);
8a0662d9
RW
483}
484EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
485
486/**
487 * fwnode_property_read_u16_array - return a u16 array property of firmware node
488 * @fwnode: Firmware node to get the property of
489 * @propname: Name of the property
5c0acf3b 490 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
491 * @nval: Size of the @val array
492 *
493 * Read an array of u16 properties with @propname from @fwnode and store them to
494 * @val if found.
495 *
5c0acf3b
AH
496 * Return: number of values if @val was %NULL,
497 * %0 if the property was found (success),
8a0662d9
RW
498 * %-EINVAL if given arguments are not valid,
499 * %-ENODATA if the property does not have a value,
500 * %-EPROTO if the property is not an array of numbers,
501 * %-EOVERFLOW if the size of the property is not as expected,
502 * %-ENXIO if no suitable firmware interface is present.
503 */
37ba983c 504int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
505 const char *propname, u16 *val, size_t nval)
506{
3708184a
SA
507 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
508 val, nval);
8a0662d9
RW
509}
510EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
511
512/**
513 * fwnode_property_read_u32_array - return a u32 array property of firmware node
514 * @fwnode: Firmware node to get the property of
515 * @propname: Name of the property
5c0acf3b 516 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
517 * @nval: Size of the @val array
518 *
519 * Read an array of u32 properties with @propname from @fwnode store them to
520 * @val if found.
521 *
5c0acf3b
AH
522 * Return: number of values if @val was %NULL,
523 * %0 if the property was found (success),
8a0662d9
RW
524 * %-EINVAL if given arguments are not valid,
525 * %-ENODATA if the property does not have a value,
526 * %-EPROTO if the property is not an array of numbers,
527 * %-EOVERFLOW if the size of the property is not as expected,
528 * %-ENXIO if no suitable firmware interface is present.
529 */
37ba983c 530int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
531 const char *propname, u32 *val, size_t nval)
532{
3708184a
SA
533 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
534 val, nval);
8a0662d9
RW
535}
536EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
537
538/**
539 * fwnode_property_read_u64_array - return a u64 array property firmware node
540 * @fwnode: Firmware node to get the property of
541 * @propname: Name of the property
5c0acf3b 542 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
543 * @nval: Size of the @val array
544 *
545 * Read an array of u64 properties with @propname from @fwnode and store them to
546 * @val if found.
547 *
5c0acf3b
AH
548 * Return: number of values if @val was %NULL,
549 * %0 if the property was found (success),
8a0662d9
RW
550 * %-EINVAL if given arguments are not valid,
551 * %-ENODATA if the property does not have a value,
552 * %-EPROTO if the property is not an array of numbers,
553 * %-EOVERFLOW if the size of the property is not as expected,
554 * %-ENXIO if no suitable firmware interface is present.
555 */
37ba983c 556int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
557 const char *propname, u64 *val, size_t nval)
558{
3708184a
SA
559 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
560 val, nval);
8a0662d9
RW
561}
562EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
563
564/**
565 * fwnode_property_read_string_array - return string array property of a node
566 * @fwnode: Firmware node to get the property of
567 * @propname: Name of the property
5c0acf3b 568 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
569 * @nval: Size of the @val array
570 *
571 * Read an string list property @propname from the given firmware node and store
572 * them to @val if found.
573 *
b0b027ce
SA
574 * Return: number of values read on success if @val is non-NULL,
575 * number of values available on success if @val is NULL,
8a0662d9
RW
576 * %-EINVAL if given arguments are not valid,
577 * %-ENODATA if the property does not have a value,
026b8217 578 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
8a0662d9
RW
579 * %-EOVERFLOW if the size of the property is not as expected,
580 * %-ENXIO if no suitable firmware interface is present.
581 */
37ba983c 582int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
583 const char *propname, const char **val,
584 size_t nval)
585{
362c0b30
AS
586 int ret;
587
3708184a
SA
588 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
589 val, nval);
0d67e0fa
HK
590 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
591 !IS_ERR_OR_NULL(fwnode->secondary))
3708184a
SA
592 ret = fwnode_call_int_op(fwnode->secondary,
593 property_read_string_array, propname,
594 val, nval);
362c0b30 595 return ret;
8a0662d9
RW
596}
597EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
598
599/**
600 * fwnode_property_read_string - return a string property of a firmware node
601 * @fwnode: Firmware node to get the property of
602 * @propname: Name of the property
603 * @val: The value is stored here
604 *
605 * Read property @propname from the given firmware node and store the value into
606 * @val if found. The value is checked to be a string.
607 *
608 * Return: %0 if the property was found (success),
609 * %-EINVAL if given arguments are not valid,
610 * %-ENODATA if the property does not have a value,
611 * %-EPROTO or %-EILSEQ if the property is not a string,
612 * %-ENXIO if no suitable firmware interface is present.
613 */
37ba983c 614int fwnode_property_read_string(const struct fwnode_handle *fwnode,
8a0662d9
RW
615 const char *propname, const char **val)
616{
e4817477 617 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
362c0b30 618
b0b027ce 619 return ret < 0 ? ret : 0;
8a0662d9
RW
620}
621EXPORT_SYMBOL_GPL(fwnode_property_read_string);
622
3f5c8d31
MW
623/**
624 * fwnode_property_match_string - find a string in an array and return index
625 * @fwnode: Firmware node to get the property of
626 * @propname: Name of the property holding the array
627 * @string: String to look for
628 *
629 * Find a given string in a string array and if it is found return the
630 * index back.
631 *
632 * Return: %0 if the property was found (success),
633 * %-EINVAL if given arguments are not valid,
634 * %-ENODATA if the property does not have a value,
635 * %-EPROTO if the property is not an array of strings,
636 * %-ENXIO if no suitable firmware interface is present.
637 */
37ba983c 638int fwnode_property_match_string(const struct fwnode_handle *fwnode,
3f5c8d31
MW
639 const char *propname, const char *string)
640{
641 const char **values;
a7c1d0a9 642 int nval, ret;
3f5c8d31
MW
643
644 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
645 if (nval < 0)
646 return nval;
647
f6740c18
AS
648 if (nval == 0)
649 return -ENODATA;
650
3f5c8d31
MW
651 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
652 if (!values)
653 return -ENOMEM;
654
655 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
656 if (ret < 0)
657 goto out;
658
a7c1d0a9
AS
659 ret = match_string(values, nval, string);
660 if (ret < 0)
661 ret = -ENODATA;
3f5c8d31
MW
662out:
663 kfree(values);
664 return ret;
665}
666EXPORT_SYMBOL_GPL(fwnode_property_match_string);
667
3e3119d3
SA
668/**
669 * fwnode_property_get_reference_args() - Find a reference with arguments
670 * @fwnode: Firmware node where to look for the reference
671 * @prop: The name of the property
672 * @nargs_prop: The name of the property telling the number of
673 * arguments in the referred node. NULL if @nargs is known,
674 * otherwise @nargs is ignored. Only relevant on OF.
675 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
676 * @index: Index of the reference, from zero onwards.
677 * @args: Result structure with reference and integer arguments.
678 *
679 * Obtain a reference based on a named property in an fwnode, with
680 * integer arguments.
681 *
682 * Caller is responsible to call fwnode_handle_put() on the returned
683 * args->fwnode pointer.
684 *
685 */
686int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
687 const char *prop, const char *nargs_prop,
688 unsigned int nargs, unsigned int index,
689 struct fwnode_reference_args *args)
690{
691 return fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
692 nargs, index, args);
693}
694EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
695
2d479e1f
DT
696static int property_copy_string_array(struct property_entry *dst,
697 const struct property_entry *src)
13141e1c 698{
2d479e1f
DT
699 char **d;
700 size_t nval = src->length / sizeof(*d);
701 int i;
13141e1c 702
2d479e1f
DT
703 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
704 if (!d)
705 return -ENOMEM;
13141e1c 706
2d479e1f
DT
707 for (i = 0; i < nval; i++) {
708 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
709 if (!d[i] && src->pointer.str[i]) {
710 while (--i >= 0)
711 kfree(d[i]);
712 kfree(d);
713 return -ENOMEM;
13141e1c 714 }
13141e1c
MW
715 }
716
2d479e1f
DT
717 dst->pointer.raw_data = d;
718 return 0;
13141e1c
MW
719}
720
2d479e1f
DT
721static int property_entry_copy_data(struct property_entry *dst,
722 const struct property_entry *src)
13141e1c 723{
2d479e1f 724 int error;
13141e1c
MW
725
726 dst->name = kstrdup(src->name, GFP_KERNEL);
727 if (!dst->name)
728 return -ENOMEM;
729
730 if (src->is_array) {
2d479e1f
DT
731 if (!src->length) {
732 error = -ENODATA;
733 goto out_free_name;
734 }
f6740c18 735
13141e1c 736 if (src->is_string) {
2d479e1f
DT
737 error = property_copy_string_array(dst, src);
738 if (error)
739 goto out_free_name;
13141e1c
MW
740 } else {
741 dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
742 src->length, GFP_KERNEL);
2d479e1f
DT
743 if (!dst->pointer.raw_data) {
744 error = -ENOMEM;
745 goto out_free_name;
746 }
13141e1c
MW
747 }
748 } else if (src->is_string) {
749 dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
2d479e1f
DT
750 if (!dst->value.str && src->value.str) {
751 error = -ENOMEM;
752 goto out_free_name;
753 }
13141e1c
MW
754 } else {
755 dst->value.raw_data = src->value.raw_data;
756 }
757
758 dst->length = src->length;
759 dst->is_array = src->is_array;
760 dst->is_string = src->is_string;
761
762 return 0;
2d479e1f
DT
763
764out_free_name:
765 kfree(dst->name);
766 return error;
767}
768
769static void property_entry_free_data(const struct property_entry *p)
770{
771 size_t i, nval;
772
773 if (p->is_array) {
774 if (p->is_string && p->pointer.str) {
775 nval = p->length / sizeof(const char *);
776 for (i = 0; i < nval; i++)
777 kfree(p->pointer.str[i]);
778 }
779 kfree(p->pointer.raw_data);
780 } else if (p->is_string) {
781 kfree(p->value.str);
782 }
783 kfree(p->name);
784}
785
786/**
787 * property_entries_dup - duplicate array of properties
788 * @properties: array of properties to copy
789 *
790 * This function creates a deep copy of the given NULL-terminated array
791 * of property entries.
792 */
793struct property_entry *
794property_entries_dup(const struct property_entry *properties)
795{
796 struct property_entry *p;
797 int i, n = 0;
798
799 while (properties[n].name)
800 n++;
801
802 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
803 if (!p)
804 return ERR_PTR(-ENOMEM);
805
806 for (i = 0; i < n; i++) {
807 int ret = property_entry_copy_data(&p[i], &properties[i]);
808 if (ret) {
809 while (--i >= 0)
810 property_entry_free_data(&p[i]);
811 kfree(p);
812 return ERR_PTR(ret);
813 }
814 }
815
816 return p;
817}
818EXPORT_SYMBOL_GPL(property_entries_dup);
819
820/**
821 * property_entries_free - free previously allocated array of properties
822 * @properties: array of properties to destroy
823 *
824 * This function frees given NULL-terminated array of property entries,
825 * along with their data.
826 */
827void property_entries_free(const struct property_entry *properties)
828{
829 const struct property_entry *p;
830
831 for (p = properties; p->name; p++)
832 property_entry_free_data(p);
833
834 kfree(properties);
835}
836EXPORT_SYMBOL_GPL(property_entries_free);
837
838/**
839 * pset_free_set - releases memory allocated for copied property set
840 * @pset: Property set to release
841 *
842 * Function takes previously copied property set and releases all the
843 * memory allocated to it.
844 */
845static void pset_free_set(struct property_set *pset)
846{
847 if (!pset)
848 return;
849
850 property_entries_free(pset->properties);
851 kfree(pset);
13141e1c
MW
852}
853
854/**
855 * pset_copy_set - copies property set
856 * @pset: Property set to copy
857 *
858 * This function takes a deep copy of the given property set and returns
859 * pointer to the copy. Call device_free_property_set() to free resources
860 * allocated in this function.
861 *
862 * Return: Pointer to the new property set or error pointer.
863 */
864static struct property_set *pset_copy_set(const struct property_set *pset)
865{
2d479e1f 866 struct property_entry *properties;
13141e1c 867 struct property_set *p;
13141e1c
MW
868
869 p = kzalloc(sizeof(*p), GFP_KERNEL);
870 if (!p)
871 return ERR_PTR(-ENOMEM);
872
2d479e1f
DT
873 properties = property_entries_dup(pset->properties);
874 if (IS_ERR(properties)) {
13141e1c 875 kfree(p);
2d479e1f 876 return ERR_CAST(properties);
13141e1c
MW
877 }
878
2d479e1f 879 p->properties = properties;
13141e1c
MW
880 return p;
881}
882
883/**
f4d05266 884 * device_remove_properties - Remove properties from a device object.
13141e1c
MW
885 * @dev: Device whose properties to remove.
886 *
887 * The function removes properties previously associated to the device
f4d05266 888 * secondary firmware node with device_add_properties(). Memory allocated
13141e1c
MW
889 * to the properties will also be released.
890 */
f4d05266 891void device_remove_properties(struct device *dev)
13141e1c
MW
892{
893 struct fwnode_handle *fwnode;
894
895 fwnode = dev_fwnode(dev);
896 if (!fwnode)
897 return;
898 /*
899 * Pick either primary or secondary node depending which one holds
900 * the pset. If there is no real firmware node (ACPI/DT) primary
901 * will hold the pset.
902 */
0d67e0fa
HK
903 if (is_pset_node(fwnode)) {
904 set_primary_fwnode(dev, NULL);
13141e1c 905 pset_free_set(to_pset_node(fwnode));
0d67e0fa
HK
906 } else {
907 fwnode = fwnode->secondary;
908 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
909 set_secondary_fwnode(dev, NULL);
910 pset_free_set(to_pset_node(fwnode));
911 }
912 }
13141e1c 913}
f4d05266 914EXPORT_SYMBOL_GPL(device_remove_properties);
13141e1c
MW
915
916/**
f4d05266 917 * device_add_properties - Add a collection of properties to a device object.
13141e1c 918 * @dev: Device to add properties to.
f4d05266 919 * @properties: Collection of properties to add.
13141e1c 920 *
f4d05266
HK
921 * Associate a collection of device properties represented by @properties with
922 * @dev as its secondary firmware node. The function takes a copy of
923 * @properties.
13141e1c 924 */
bec84da8
DT
925int device_add_properties(struct device *dev,
926 const struct property_entry *properties)
13141e1c 927{
f4d05266 928 struct property_set *p, pset;
13141e1c 929
f4d05266 930 if (!properties)
13141e1c
MW
931 return -EINVAL;
932
f4d05266
HK
933 pset.properties = properties;
934
935 p = pset_copy_set(&pset);
13141e1c
MW
936 if (IS_ERR(p))
937 return PTR_ERR(p);
938
3708184a 939 p->fwnode.ops = &pset_fwnode_ops;
13141e1c
MW
940 set_secondary_fwnode(dev, &p->fwnode);
941 return 0;
942}
f4d05266 943EXPORT_SYMBOL_GPL(device_add_properties);
13141e1c 944
23387258
SA
945/**
946 * fwnode_get_next_parent - Iterate to the node's parent
947 * @fwnode: Firmware whose parent is retrieved
948 *
949 * This is like fwnode_get_parent() except that it drops the refcount
950 * on the passed node, making it suitable for iterating through a
951 * node's parents.
952 *
953 * Returns a node pointer with refcount incremented, use
954 * fwnode_handle_node() on it when done.
955 */
956struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
957{
958 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
959
960 fwnode_handle_put(fwnode);
961
962 return parent;
963}
964EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
965
afaf26fd
MW
966/**
967 * fwnode_get_parent - Return parent firwmare node
968 * @fwnode: Firmware whose parent is retrieved
969 *
970 * Return parent firmware node of the given node if possible or %NULL if no
971 * parent was available.
972 */
37ba983c 973struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
afaf26fd 974{
3708184a 975 return fwnode_call_ptr_op(fwnode, get_parent);
afaf26fd
MW
976}
977EXPORT_SYMBOL_GPL(fwnode_get_parent);
978
8a0662d9 979/**
34055190
MW
980 * fwnode_get_next_child_node - Return the next child node handle for a node
981 * @fwnode: Firmware node to find the next child node for.
982 * @child: Handle to one of the node's child nodes or a %NULL handle.
8a0662d9 983 */
37ba983c
SA
984struct fwnode_handle *
985fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
986 struct fwnode_handle *child)
8a0662d9 987{
3708184a 988 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
8a0662d9 989}
34055190
MW
990EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
991
992/**
993 * device_get_next_child_node - Return the next child node handle for a device
994 * @dev: Device to find the next child node for.
995 * @child: Handle to one of the device's child nodes or a null handle.
996 */
997struct fwnode_handle *device_get_next_child_node(struct device *dev,
998 struct fwnode_handle *child)
999{
1000 struct acpi_device *adev = ACPI_COMPANION(dev);
1001 struct fwnode_handle *fwnode = NULL;
1002
1003 if (dev->of_node)
1004 fwnode = &dev->of_node->fwnode;
1005 else if (adev)
1006 fwnode = acpi_fwnode_handle(adev);
1007
1008 return fwnode_get_next_child_node(fwnode, child);
1009}
8a0662d9
RW
1010EXPORT_SYMBOL_GPL(device_get_next_child_node);
1011
613e9721 1012/**
21ea73f5
MW
1013 * fwnode_get_named_child_node - Return first matching named child node handle
1014 * @fwnode: Firmware node to find the named child node for.
613e9721
AT
1015 * @childname: String to match child node name against.
1016 */
37ba983c
SA
1017struct fwnode_handle *
1018fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
1019 const char *childname)
613e9721 1020{
3708184a 1021 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
613e9721 1022}
21ea73f5
MW
1023EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
1024
1025/**
1026 * device_get_named_child_node - Return first matching named child node handle
1027 * @dev: Device to find the named child node for.
1028 * @childname: String to match child node name against.
1029 */
1030struct fwnode_handle *device_get_named_child_node(struct device *dev,
1031 const char *childname)
1032{
1033 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
1034}
613e9721
AT
1035EXPORT_SYMBOL_GPL(device_get_named_child_node);
1036
e7887c28
SA
1037/**
1038 * fwnode_handle_get - Obtain a reference to a device node
1039 * @fwnode: Pointer to the device node to obtain the reference to.
1040 */
1041void fwnode_handle_get(struct fwnode_handle *fwnode)
1042{
3708184a 1043 fwnode_call_void_op(fwnode, get);
e7887c28
SA
1044}
1045EXPORT_SYMBOL_GPL(fwnode_handle_get);
1046
8a0662d9
RW
1047/**
1048 * fwnode_handle_put - Drop reference to a device node
1049 * @fwnode: Pointer to the device node to drop the reference to.
1050 *
1051 * This has to be used when terminating device_for_each_child_node() iteration
1052 * with break or return to prevent stale device node references from being left
1053 * behind.
1054 */
1055void fwnode_handle_put(struct fwnode_handle *fwnode)
1056{
3708184a 1057 fwnode_call_void_op(fwnode, put);
8a0662d9
RW
1058}
1059EXPORT_SYMBOL_GPL(fwnode_handle_put);
1060
2294b3af
SA
1061/**
1062 * fwnode_device_is_available - check if a device is available for use
1063 * @fwnode: Pointer to the fwnode of the device.
1064 */
37ba983c 1065bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
2294b3af 1066{
e8158b48 1067 return fwnode_call_bool_op(fwnode, device_is_available);
2294b3af
SA
1068}
1069EXPORT_SYMBOL_GPL(fwnode_device_is_available);
1070
8a0662d9
RW
1071/**
1072 * device_get_child_node_count - return the number of child nodes for device
1073 * @dev: Device to cound the child nodes for
1074 */
1075unsigned int device_get_child_node_count(struct device *dev)
1076{
1077 struct fwnode_handle *child;
1078 unsigned int count = 0;
1079
1080 device_for_each_child_node(dev, child)
1081 count++;
1082
1083 return count;
1084}
1085EXPORT_SYMBOL_GPL(device_get_child_node_count);
05ca5560 1086
e5e55864
SS
1087bool device_dma_supported(struct device *dev)
1088{
1089 /* For DT, this is always supported.
1090 * For ACPI, this depends on CCA, which
1091 * is determined by the acpi_dma_supported().
1092 */
1093 if (IS_ENABLED(CONFIG_OF) && dev->of_node)
1094 return true;
1095
1096 return acpi_dma_supported(ACPI_COMPANION(dev));
1097}
1098EXPORT_SYMBOL_GPL(device_dma_supported);
1099
1100enum dev_dma_attr device_get_dma_attr(struct device *dev)
1101{
1102 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
1103
1104 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1105 if (of_dma_is_coherent(dev->of_node))
1106 attr = DEV_DMA_COHERENT;
1107 else
1108 attr = DEV_DMA_NON_COHERENT;
1109 } else
1110 attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
1111
1112 return attr;
1113}
1114EXPORT_SYMBOL_GPL(device_get_dma_attr);
1115
4c96b7dc 1116/**
2f710a3a 1117 * device_get_phy_mode - Get phy mode for given device
4c96b7dc
JL
1118 * @dev: Pointer to the given device
1119 *
1120 * The function gets phy interface string from property 'phy-mode' or
1121 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1122 * error case.
1123 */
1124int device_get_phy_mode(struct device *dev)
1125{
1126 const char *pm;
1127 int err, i;
1128
1129 err = device_property_read_string(dev, "phy-mode", &pm);
1130 if (err < 0)
1131 err = device_property_read_string(dev,
1132 "phy-connection-type", &pm);
1133 if (err < 0)
1134 return err;
1135
1136 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1137 if (!strcasecmp(pm, phy_modes(i)))
1138 return i;
1139
1140 return -ENODEV;
1141}
1142EXPORT_SYMBOL_GPL(device_get_phy_mode);
1143
1144static void *device_get_mac_addr(struct device *dev,
1145 const char *name, char *addr,
1146 int alen)
1147{
1148 int ret = device_property_read_u8_array(dev, name, addr, alen);
1149
2f710a3a 1150 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
4c96b7dc
JL
1151 return addr;
1152 return NULL;
1153}
1154
1155/**
2f710a3a
JL
1156 * device_get_mac_address - Get the MAC for a given device
1157 * @dev: Pointer to the device
1158 * @addr: Address of buffer to store the MAC in
1159 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
1160 *
1161 * Search the firmware node for the best MAC address to use. 'mac-address' is
4c96b7dc
JL
1162 * checked first, because that is supposed to contain to "most recent" MAC
1163 * address. If that isn't set, then 'local-mac-address' is checked next,
1164 * because that is the default address. If that isn't set, then the obsolete
1165 * 'address' is checked, just in case we're using an old device tree.
1166 *
1167 * Note that the 'address' property is supposed to contain a virtual address of
1168 * the register set, but some DTS files have redefined that property to be the
1169 * MAC address.
1170 *
1171 * All-zero MAC addresses are rejected, because those could be properties that
2f710a3a
JL
1172 * exist in the firmware tables, but were not updated by the firmware. For
1173 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1174 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
1175 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1176 * exists but is all zeros.
4c96b7dc
JL
1177*/
1178void *device_get_mac_address(struct device *dev, char *addr, int alen)
1179{
5b902d6f 1180 char *res;
4c96b7dc 1181
5b902d6f
JG
1182 res = device_get_mac_addr(dev, "mac-address", addr, alen);
1183 if (res)
1184 return res;
1185
1186 res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1187 if (res)
1188 return res;
4c96b7dc
JL
1189
1190 return device_get_mac_addr(dev, "address", addr, alen);
1191}
1192EXPORT_SYMBOL(device_get_mac_address);
07bb80d4
MW
1193
1194/**
1195 * device_graph_get_next_endpoint - Get next endpoint firmware node
1196 * @fwnode: Pointer to the parent firmware node
1197 * @prev: Previous endpoint node or %NULL to get the first
1198 *
1199 * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1200 * are available.
1201 */
1202struct fwnode_handle *
37ba983c 1203fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
07bb80d4
MW
1204 struct fwnode_handle *prev)
1205{
3b27d00e 1206 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
07bb80d4
MW
1207}
1208EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1209
6a71d8d7
KB
1210/**
1211 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1212 * @endpoint: Endpoint firmware node of the port
1213 *
1214 * Return: the firmware node of the device the @endpoint belongs to.
1215 */
1216struct fwnode_handle *
37ba983c 1217fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
6a71d8d7
KB
1218{
1219 struct fwnode_handle *port, *parent;
1220
1221 port = fwnode_get_parent(endpoint);
1222 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1223
1224 fwnode_handle_put(port);
1225
1226 return parent;
1227}
1228EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1229
07bb80d4
MW
1230/**
1231 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1232 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1233 *
1234 * Extracts firmware node of a remote device the @fwnode points to.
1235 */
1236struct fwnode_handle *
37ba983c 1237fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
07bb80d4 1238{
6a71d8d7 1239 struct fwnode_handle *endpoint, *parent;
07bb80d4 1240
6a71d8d7
KB
1241 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1242 parent = fwnode_graph_get_port_parent(endpoint);
07bb80d4 1243
6a71d8d7 1244 fwnode_handle_put(endpoint);
07bb80d4
MW
1245
1246 return parent;
1247}
1248EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1249
1250/**
1251 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1252 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1253 *
1254 * Extracts firmware node of a remote port the @fwnode points to.
1255 */
37ba983c
SA
1256struct fwnode_handle *
1257fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
07bb80d4 1258{
3b27d00e 1259 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
07bb80d4
MW
1260}
1261EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1262
1263/**
1264 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1265 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1266 *
1267 * Extracts firmware node of a remote endpoint the @fwnode points to.
1268 */
1269struct fwnode_handle *
37ba983c 1270fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
07bb80d4 1271{
3b27d00e 1272 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
07bb80d4
MW
1273}
1274EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
2bd5452d 1275
125ee6b3
SA
1276/**
1277 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1278 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1279 * @port_id: identifier of the parent port node
1280 * @endpoint_id: identifier of the endpoint node
1281 *
1282 * Return: Remote fwnode handle associated with remote endpoint node linked
1283 * to @node. Use fwnode_node_put() on it when done.
1284 */
37ba983c
SA
1285struct fwnode_handle *
1286fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1287 u32 endpoint_id)
125ee6b3
SA
1288{
1289 struct fwnode_handle *endpoint = NULL;
1290
1291 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1292 struct fwnode_endpoint fwnode_ep;
1293 struct fwnode_handle *remote;
1294 int ret;
1295
1296 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1297 if (ret < 0)
1298 continue;
1299
1300 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1301 continue;
1302
1303 remote = fwnode_graph_get_remote_port_parent(endpoint);
1304 if (!remote)
1305 return NULL;
1306
1307 return fwnode_device_is_available(remote) ? remote : NULL;
1308 }
1309
1310 return NULL;
1311}
1312EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1313
2bd5452d
SA
1314/**
1315 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1316 * @fwnode: pointer to endpoint fwnode_handle
1317 * @endpoint: pointer to the fwnode endpoint data structure
1318 *
1319 * Parse @fwnode representing a graph endpoint node and store the
1320 * information in @endpoint. The caller must hold a reference to
1321 * @fwnode.
1322 */
37ba983c 1323int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
2bd5452d
SA
1324 struct fwnode_endpoint *endpoint)
1325{
2bd5452d
SA
1326 memset(endpoint, 0, sizeof(*endpoint));
1327
3b27d00e 1328 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
2bd5452d
SA
1329}
1330EXPORT_SYMBOL(fwnode_graph_parse_endpoint);