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