Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / drivers / base / property.c
CommitLineData
989d42e8 1// SPDX-License-Identifier: GPL-2.0
b31384fa
RW
2/*
3 * property.c - Unified device property interface.
4 *
5 * Copyright (C) 2014, Intel Corporation
6 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
b31384fa
RW
8 */
9
4dc3d612
AS
10#include <linux/device.h>
11#include <linux/err.h>
16ba08d5 12#include <linux/export.h>
4dc3d612 13#include <linux/kconfig.h>
b31384fa 14#include <linux/of.h>
16ba08d5 15#include <linux/property.h>
4c96b7dc 16#include <linux/phy.h>
4dc3d612
AS
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/types.h>
16ba08d5 20
b295d484 21struct fwnode_handle *__dev_fwnode(struct device *dev)
9017f252
RW
22{
23 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
3cd80150 24 of_fwnode_handle(dev->of_node) : dev->fwnode;
9017f252 25}
b295d484
AS
26EXPORT_SYMBOL_GPL(__dev_fwnode);
27
28const struct fwnode_handle *__dev_fwnode_const(const struct device *dev)
29{
30 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
31 of_fwnode_handle(dev->of_node) : dev->fwnode;
32}
33EXPORT_SYMBOL_GPL(__dev_fwnode_const);
b31384fa
RW
34
35/**
36 * device_property_present - check if a property of a device is present
37 * @dev: Device whose property is being checked
38 * @propname: Name of the property
39 *
40 * Check if property @propname is present in the device firmware description.
295209ca
AS
41 *
42 * Return: true if property @propname is present. Otherwise, returns false.
b31384fa 43 */
046b6a17 44bool device_property_present(const struct device *dev, const char *propname)
b31384fa 45{
9017f252 46 return fwnode_property_present(dev_fwnode(dev), propname);
b31384fa
RW
47}
48EXPORT_SYMBOL_GPL(device_property_present);
49
362c0b30
AS
50/**
51 * fwnode_property_present - check if a property of a firmware node is present
52 * @fwnode: Firmware node whose property to check
53 * @propname: Name of the property
295209ca
AS
54 *
55 * Return: true if property @propname is present. Otherwise, returns false.
362c0b30 56 */
37ba983c
SA
57bool fwnode_property_present(const struct fwnode_handle *fwnode,
58 const char *propname)
362c0b30
AS
59{
60 bool ret;
61
002752af
AS
62 if (IS_ERR_OR_NULL(fwnode))
63 return false;
64
e8158b48 65 ret = fwnode_call_bool_op(fwnode, property_present, propname);
002752af
AS
66 if (ret)
67 return ret;
68
69 return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
362c0b30 70}
8a0662d9
RW
71EXPORT_SYMBOL_GPL(fwnode_property_present);
72
bb391410
RHA
73/**
74 * device_property_read_bool - Return the value for a boolean property of a device
75 * @dev: Device whose property is being checked
76 * @propname: Name of the property
77 *
78 * Return if property @propname is true or false in the device firmware description.
79 *
80 * Return: true if property @propname is present. Otherwise, returns false.
81 */
82bool device_property_read_bool(const struct device *dev, const char *propname)
83{
84 return fwnode_property_read_bool(dev_fwnode(dev), propname);
85}
86EXPORT_SYMBOL_GPL(device_property_read_bool);
87
88/**
89 * fwnode_property_read_bool - Return the value for a boolean property of a firmware node
90 * @fwnode: Firmware node whose property to check
91 * @propname: Name of the property
92 *
93 * Return if property @propname is true or false in the firmware description.
94 */
95bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
96 const char *propname)
97{
98 bool ret;
99
100 if (IS_ERR_OR_NULL(fwnode))
101 return false;
102
103 ret = fwnode_call_bool_op(fwnode, property_read_bool, propname);
104 if (ret)
105 return ret;
106
107 return fwnode_call_bool_op(fwnode->secondary, property_read_bool, propname);
108}
109EXPORT_SYMBOL_GPL(fwnode_property_read_bool);
110
b31384fa
RW
111/**
112 * device_property_read_u8_array - return a u8 array property of a device
113 * @dev: Device to get the property of
114 * @propname: Name of the property
5c0acf3b 115 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
116 * @nval: Size of the @val array
117 *
118 * Function reads an array of u8 properties with @propname from the device
119 * firmware description and stores them to @val if found.
120 *
f6e109a0
AS
121 * It's recommended to call device_property_count_u8() instead of calling
122 * this function with @val equals %NULL and @nval equals 0.
123 *
5c0acf3b
AH
124 * Return: number of values if @val was %NULL,
125 * %0 if the property was found (success),
b31384fa
RW
126 * %-EINVAL if given arguments are not valid,
127 * %-ENODATA if the property does not have a value,
128 * %-EPROTO if the property is not an array of numbers,
129 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 130 * %-ENXIO if no suitable firmware interface is present.
b31384fa 131 */
046b6a17 132int device_property_read_u8_array(const struct device *dev, const char *propname,
b31384fa
RW
133 u8 *val, size_t nval)
134{
9017f252 135 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
136}
137EXPORT_SYMBOL_GPL(device_property_read_u8_array);
138
139/**
140 * device_property_read_u16_array - return a u16 array property of a device
141 * @dev: Device to get the property of
142 * @propname: Name of the property
5c0acf3b 143 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
144 * @nval: Size of the @val array
145 *
146 * Function reads an array of u16 properties with @propname from the device
147 * firmware description and stores them to @val if found.
148 *
f6e109a0
AS
149 * It's recommended to call device_property_count_u16() instead of calling
150 * this function with @val equals %NULL and @nval equals 0.
151 *
5c0acf3b
AH
152 * Return: number of values if @val was %NULL,
153 * %0 if the property was found (success),
b31384fa
RW
154 * %-EINVAL if given arguments are not valid,
155 * %-ENODATA if the property does not have a value,
156 * %-EPROTO if the property is not an array of numbers,
157 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 158 * %-ENXIO if no suitable firmware interface is present.
b31384fa 159 */
046b6a17 160int device_property_read_u16_array(const struct device *dev, const char *propname,
b31384fa
RW
161 u16 *val, size_t nval)
162{
9017f252 163 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
164}
165EXPORT_SYMBOL_GPL(device_property_read_u16_array);
166
167/**
168 * device_property_read_u32_array - return a u32 array property of a device
169 * @dev: Device to get the property of
170 * @propname: Name of the property
5c0acf3b 171 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
172 * @nval: Size of the @val array
173 *
174 * Function reads an array of u32 properties with @propname from the device
175 * firmware description and stores them to @val if found.
176 *
f6e109a0
AS
177 * It's recommended to call device_property_count_u32() instead of calling
178 * this function with @val equals %NULL and @nval equals 0.
179 *
5c0acf3b
AH
180 * Return: number of values if @val was %NULL,
181 * %0 if the property was found (success),
b31384fa
RW
182 * %-EINVAL if given arguments are not valid,
183 * %-ENODATA if the property does not have a value,
184 * %-EPROTO if the property is not an array of numbers,
185 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 186 * %-ENXIO if no suitable firmware interface is present.
b31384fa 187 */
046b6a17 188int device_property_read_u32_array(const struct device *dev, const char *propname,
b31384fa
RW
189 u32 *val, size_t nval)
190{
9017f252 191 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
192}
193EXPORT_SYMBOL_GPL(device_property_read_u32_array);
194
195/**
196 * device_property_read_u64_array - return a u64 array property of a device
197 * @dev: Device to get the property of
198 * @propname: Name of the property
5c0acf3b 199 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
200 * @nval: Size of the @val array
201 *
202 * Function reads an array of u64 properties with @propname from the device
203 * firmware description and stores them to @val if found.
204 *
f6e109a0
AS
205 * It's recommended to call device_property_count_u64() instead of calling
206 * this function with @val equals %NULL and @nval equals 0.
207 *
5c0acf3b
AH
208 * Return: number of values if @val was %NULL,
209 * %0 if the property was found (success),
b31384fa
RW
210 * %-EINVAL if given arguments are not valid,
211 * %-ENODATA if the property does not have a value,
212 * %-EPROTO if the property is not an array of numbers,
213 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 214 * %-ENXIO if no suitable firmware interface is present.
b31384fa 215 */
046b6a17 216int device_property_read_u64_array(const struct device *dev, const char *propname,
b31384fa
RW
217 u64 *val, size_t nval)
218{
9017f252 219 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
220}
221EXPORT_SYMBOL_GPL(device_property_read_u64_array);
222
223/**
224 * device_property_read_string_array - return a string array property of device
225 * @dev: Device to get the property of
226 * @propname: Name of the property
5c0acf3b 227 * @val: The values are stored here or %NULL to return the number of values
b31384fa
RW
228 * @nval: Size of the @val array
229 *
230 * Function reads an array of string properties with @propname from the device
231 * firmware description and stores them to @val if found.
232 *
f6e109a0
AS
233 * It's recommended to call device_property_string_array_count() instead of calling
234 * this function with @val equals %NULL and @nval equals 0.
235 *
b0b027ce
SA
236 * Return: number of values read on success if @val is non-NULL,
237 * number of values available on success if @val is NULL,
b31384fa
RW
238 * %-EINVAL if given arguments are not valid,
239 * %-ENODATA if the property does not have a value,
240 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
241 * %-EOVERFLOW if the size of the property is not as expected.
4fa7508e 242 * %-ENXIO if no suitable firmware interface is present.
b31384fa 243 */
046b6a17 244int device_property_read_string_array(const struct device *dev, const char *propname,
b31384fa
RW
245 const char **val, size_t nval)
246{
9017f252 247 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
b31384fa
RW
248}
249EXPORT_SYMBOL_GPL(device_property_read_string_array);
250
251/**
252 * device_property_read_string - return a string property of a device
253 * @dev: Device to get the property of
254 * @propname: Name of the property
255 * @val: The value is stored here
256 *
257 * Function reads property @propname from the device firmware description and
258 * stores the value into @val if found. The value is checked to be a string.
259 *
260 * Return: %0 if the property was found (success),
261 * %-EINVAL if given arguments are not valid,
262 * %-ENODATA if the property does not have a value,
263 * %-EPROTO or %-EILSEQ if the property type is not a string.
4fa7508e 264 * %-ENXIO if no suitable firmware interface is present.
b31384fa 265 */
046b6a17 266int device_property_read_string(const struct device *dev, const char *propname,
b31384fa
RW
267 const char **val)
268{
9017f252 269 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
b31384fa
RW
270}
271EXPORT_SYMBOL_GPL(device_property_read_string);
8a0662d9 272
3f5c8d31
MW
273/**
274 * device_property_match_string - find a string in an array and return index
275 * @dev: Device to get the property of
276 * @propname: Name of the property holding the array
277 * @string: String to look for
278 *
279 * Find a given string in a string array and if it is found return the
280 * index back.
281 *
92e10465 282 * Return: index, starting from %0, if the property was found (success),
3f5c8d31
MW
283 * %-EINVAL if given arguments are not valid,
284 * %-ENODATA if the property does not have a value,
285 * %-EPROTO if the property is not an array of strings,
286 * %-ENXIO if no suitable firmware interface is present.
287 */
046b6a17 288int device_property_match_string(const struct device *dev, const char *propname,
3f5c8d31
MW
289 const char *string)
290{
291 return fwnode_property_match_string(dev_fwnode(dev), propname, string);
292}
293EXPORT_SYMBOL_GPL(device_property_match_string);
294
37ba983c 295static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
3708184a
SA
296 const char *propname,
297 unsigned int elem_size, void *val,
298 size_t nval)
299{
300 int ret;
301
002752af
AS
302 if (IS_ERR_OR_NULL(fwnode))
303 return -EINVAL;
304
3708184a
SA
305 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
306 elem_size, val, nval);
002752af
AS
307 if (ret != -EINVAL)
308 return ret;
3708184a 309
002752af
AS
310 return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
311 elem_size, val, nval);
3708184a 312}
362c0b30 313
8a0662d9
RW
314/**
315 * fwnode_property_read_u8_array - return a u8 array property of firmware node
316 * @fwnode: Firmware node to get the property of
317 * @propname: Name of the property
5c0acf3b 318 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
319 * @nval: Size of the @val array
320 *
321 * Read an array of u8 properties with @propname from @fwnode and stores them to
322 * @val if found.
323 *
f6e109a0
AS
324 * It's recommended to call fwnode_property_count_u8() instead of calling
325 * this function with @val equals %NULL and @nval equals 0.
326 *
5c0acf3b
AH
327 * Return: number of values if @val was %NULL,
328 * %0 if the property was found (success),
8a0662d9
RW
329 * %-EINVAL if given arguments are not valid,
330 * %-ENODATA if the property does not have a value,
331 * %-EPROTO if the property is not an array of numbers,
332 * %-EOVERFLOW if the size of the property is not as expected,
333 * %-ENXIO if no suitable firmware interface is present.
334 */
37ba983c 335int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
336 const char *propname, u8 *val, size_t nval)
337{
3708184a
SA
338 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
339 val, nval);
8a0662d9
RW
340}
341EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
342
343/**
344 * fwnode_property_read_u16_array - return a u16 array property of firmware node
345 * @fwnode: Firmware node to get the property of
346 * @propname: Name of the property
5c0acf3b 347 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
348 * @nval: Size of the @val array
349 *
350 * Read an array of u16 properties with @propname from @fwnode and store them to
351 * @val if found.
352 *
f6e109a0
AS
353 * It's recommended to call fwnode_property_count_u16() instead of calling
354 * this function with @val equals %NULL and @nval equals 0.
355 *
5c0acf3b
AH
356 * Return: number of values if @val was %NULL,
357 * %0 if the property was found (success),
8a0662d9
RW
358 * %-EINVAL if given arguments are not valid,
359 * %-ENODATA if the property does not have a value,
360 * %-EPROTO if the property is not an array of numbers,
361 * %-EOVERFLOW if the size of the property is not as expected,
362 * %-ENXIO if no suitable firmware interface is present.
363 */
37ba983c 364int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
365 const char *propname, u16 *val, size_t nval)
366{
3708184a
SA
367 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
368 val, nval);
8a0662d9
RW
369}
370EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
371
372/**
373 * fwnode_property_read_u32_array - return a u32 array property of firmware node
374 * @fwnode: Firmware node to get the property of
375 * @propname: Name of the property
5c0acf3b 376 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
377 * @nval: Size of the @val array
378 *
379 * Read an array of u32 properties with @propname from @fwnode store them to
380 * @val if found.
381 *
f6e109a0
AS
382 * It's recommended to call fwnode_property_count_u32() instead of calling
383 * this function with @val equals %NULL and @nval equals 0.
384 *
5c0acf3b
AH
385 * Return: number of values if @val was %NULL,
386 * %0 if the property was found (success),
8a0662d9
RW
387 * %-EINVAL if given arguments are not valid,
388 * %-ENODATA if the property does not have a value,
389 * %-EPROTO if the property is not an array of numbers,
390 * %-EOVERFLOW if the size of the property is not as expected,
391 * %-ENXIO if no suitable firmware interface is present.
392 */
37ba983c 393int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
394 const char *propname, u32 *val, size_t nval)
395{
3708184a
SA
396 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
397 val, nval);
8a0662d9
RW
398}
399EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
400
401/**
402 * fwnode_property_read_u64_array - return a u64 array property firmware node
403 * @fwnode: Firmware node to get the property of
404 * @propname: Name of the property
5c0acf3b 405 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
406 * @nval: Size of the @val array
407 *
408 * Read an array of u64 properties with @propname from @fwnode and store them to
409 * @val if found.
410 *
f6e109a0
AS
411 * It's recommended to call fwnode_property_count_u64() instead of calling
412 * this function with @val equals %NULL and @nval equals 0.
413 *
5c0acf3b
AH
414 * Return: number of values if @val was %NULL,
415 * %0 if the property was found (success),
8a0662d9
RW
416 * %-EINVAL if given arguments are not valid,
417 * %-ENODATA if the property does not have a value,
418 * %-EPROTO if the property is not an array of numbers,
419 * %-EOVERFLOW if the size of the property is not as expected,
420 * %-ENXIO if no suitable firmware interface is present.
421 */
37ba983c 422int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
423 const char *propname, u64 *val, size_t nval)
424{
3708184a
SA
425 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
426 val, nval);
8a0662d9
RW
427}
428EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
429
430/**
431 * fwnode_property_read_string_array - return string array property of a node
432 * @fwnode: Firmware node to get the property of
433 * @propname: Name of the property
5c0acf3b 434 * @val: The values are stored here or %NULL to return the number of values
8a0662d9
RW
435 * @nval: Size of the @val array
436 *
437 * Read an string list property @propname from the given firmware node and store
438 * them to @val if found.
439 *
f6e109a0
AS
440 * It's recommended to call fwnode_property_string_array_count() instead of calling
441 * this function with @val equals %NULL and @nval equals 0.
442 *
b0b027ce
SA
443 * Return: number of values read on success if @val is non-NULL,
444 * number of values available on success if @val is NULL,
8a0662d9
RW
445 * %-EINVAL if given arguments are not valid,
446 * %-ENODATA if the property does not have a value,
026b8217 447 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
8a0662d9
RW
448 * %-EOVERFLOW if the size of the property is not as expected,
449 * %-ENXIO if no suitable firmware interface is present.
450 */
37ba983c 451int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
452 const char *propname, const char **val,
453 size_t nval)
454{
362c0b30
AS
455 int ret;
456
002752af
AS
457 if (IS_ERR_OR_NULL(fwnode))
458 return -EINVAL;
459
3708184a
SA
460 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
461 val, nval);
002752af
AS
462 if (ret != -EINVAL)
463 return ret;
464
465 return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
466 val, nval);
8a0662d9
RW
467}
468EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
469
470/**
471 * fwnode_property_read_string - return a string property of a firmware node
472 * @fwnode: Firmware node to get the property of
473 * @propname: Name of the property
474 * @val: The value is stored here
475 *
476 * Read property @propname from the given firmware node and store the value into
477 * @val if found. The value is checked to be a string.
478 *
479 * Return: %0 if the property was found (success),
480 * %-EINVAL if given arguments are not valid,
481 * %-ENODATA if the property does not have a value,
482 * %-EPROTO or %-EILSEQ if the property is not a string,
483 * %-ENXIO if no suitable firmware interface is present.
484 */
37ba983c 485int fwnode_property_read_string(const struct fwnode_handle *fwnode,
8a0662d9
RW
486 const char *propname, const char **val)
487{
e4817477 488 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
362c0b30 489
b0b027ce 490 return ret < 0 ? ret : 0;
8a0662d9
RW
491}
492EXPORT_SYMBOL_GPL(fwnode_property_read_string);
493
3f5c8d31
MW
494/**
495 * fwnode_property_match_string - find a string in an array and return index
496 * @fwnode: Firmware node to get the property of
497 * @propname: Name of the property holding the array
498 * @string: String to look for
499 *
500 * Find a given string in a string array and if it is found return the
501 * index back.
502 *
92e10465 503 * Return: index, starting from %0, if the property was found (success),
3f5c8d31
MW
504 * %-EINVAL if given arguments are not valid,
505 * %-ENODATA if the property does not have a value,
506 * %-EPROTO if the property is not an array of strings,
507 * %-ENXIO if no suitable firmware interface is present.
508 */
37ba983c 509int fwnode_property_match_string(const struct fwnode_handle *fwnode,
3f5c8d31
MW
510 const char *propname, const char *string)
511{
512 const char **values;
a7c1d0a9 513 int nval, ret;
3f5c8d31 514
08638631 515 nval = fwnode_property_string_array_count(fwnode, propname);
3f5c8d31
MW
516 if (nval < 0)
517 return nval;
518
f6740c18
AS
519 if (nval == 0)
520 return -ENODATA;
521
3f5c8d31
MW
522 values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
523 if (!values)
524 return -ENOMEM;
525
526 ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
527 if (ret < 0)
4d57b4f2 528 goto out_free;
3f5c8d31 529
a7c1d0a9
AS
530 ret = match_string(values, nval, string);
531 if (ret < 0)
532 ret = -ENODATA;
4d57b4f2
AS
533
534out_free:
3f5c8d31
MW
535 kfree(values);
536 return ret;
537}
538EXPORT_SYMBOL_GPL(fwnode_property_match_string);
539
fac4a535
AS
540/**
541 * fwnode_property_match_property_string - find a property string value in an array and return index
542 * @fwnode: Firmware node to get the property of
543 * @propname: Name of the property holding the string value
544 * @array: String array to search in
545 * @n: Size of the @array
546 *
547 * Find a property string value in a given @array and if it is found return
548 * the index back.
549 *
550 * Return: index, starting from %0, if the string value was found in the @array (success),
551 * %-ENOENT when the string value was not found in the @array,
552 * %-EINVAL if given arguments are not valid,
553 * %-ENODATA if the property does not have a value,
554 * %-EPROTO or %-EILSEQ if the property is not a string,
555 * %-ENXIO if no suitable firmware interface is present.
556 */
557int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
558 const char *propname, const char * const *array, size_t n)
559{
560 const char *string;
561 int ret;
562
563 ret = fwnode_property_read_string(fwnode, propname, &string);
564 if (ret)
565 return ret;
566
567 ret = match_string(array, n, string);
568 if (ret < 0)
569 ret = -ENOENT;
570
571 return ret;
572}
573EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);
574
3e3119d3
SA
575/**
576 * fwnode_property_get_reference_args() - Find a reference with arguments
577 * @fwnode: Firmware node where to look for the reference
578 * @prop: The name of the property
579 * @nargs_prop: The name of the property telling the number of
580 * arguments in the referred node. NULL if @nargs is known,
581 * otherwise @nargs is ignored. Only relevant on OF.
582 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
583 * @index: Index of the reference, from zero onwards.
584 * @args: Result structure with reference and integer arguments.
3babbf61 585 * May be NULL.
3e3119d3
SA
586 *
587 * Obtain a reference based on a named property in an fwnode, with
588 * integer arguments.
589 *
295209ca
AS
590 * The caller is responsible for calling fwnode_handle_put() on the returned
591 * @args->fwnode pointer.
3e3119d3 592 *
295209ca 593 * Return: %0 on success
c343bc2c
SA
594 * %-ENOENT when the index is out of bounds, the index has an empty
595 * reference or the property was not found
596 * %-EINVAL on parse error
3e3119d3
SA
597 */
598int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
599 const char *prop, const char *nargs_prop,
600 unsigned int nargs, unsigned int index,
601 struct fwnode_reference_args *args)
602{
c097af1d
DS
603 int ret;
604
002752af
AS
605 if (IS_ERR_OR_NULL(fwnode))
606 return -ENOENT;
607
c097af1d
DS
608 ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
609 nargs, index, args);
002752af
AS
610 if (ret == 0)
611 return ret;
c097af1d 612
002752af
AS
613 if (IS_ERR_OR_NULL(fwnode->secondary))
614 return ret;
c097af1d 615
002752af
AS
616 return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
617 nargs, index, args);
3e3119d3
SA
618}
619EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
620
83b34afb
HK
621/**
622 * fwnode_find_reference - Find named reference to a fwnode_handle
623 * @fwnode: Firmware node where to look for the reference
624 * @name: The name of the reference
625 * @index: Index of the reference
626 *
627 * @index can be used when the named reference holds a table of references.
628 *
295209ca
AS
629 * The caller is responsible for calling fwnode_handle_put() on the returned
630 * fwnode pointer.
631 *
632 * Return: a pointer to the reference fwnode, when found. Otherwise,
633 * returns an error pointer.
83b34afb
HK
634 */
635struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
636 const char *name,
637 unsigned int index)
638{
639 struct fwnode_reference_args args;
640 int ret;
641
642 ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
643 &args);
644 return ret ? ERR_PTR(ret) : args.fwnode;
645}
646EXPORT_SYMBOL_GPL(fwnode_find_reference);
647
bc0500c1
SA
648/**
649 * fwnode_get_name - Return the name of a node
650 * @fwnode: The firmware node
651 *
295209ca 652 * Return: a pointer to the node name, or %NULL.
bc0500c1
SA
653 */
654const char *fwnode_get_name(const struct fwnode_handle *fwnode)
655{
656 return fwnode_call_ptr_op(fwnode, get_name);
657}
6fafbbe8 658EXPORT_SYMBOL_GPL(fwnode_get_name);
bc0500c1 659
e7e242bc
SA
660/**
661 * fwnode_get_name_prefix - Return the prefix of node for printing purposes
662 * @fwnode: The firmware node
663 *
295209ca 664 * Return: the prefix of a node, intended to be printed right before the node.
e7e242bc
SA
665 * The prefix works also as a separator between the nodes.
666 */
667const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
668{
669 return fwnode_call_ptr_op(fwnode, get_name_prefix);
670}
671
a9c8c738
SA
672/**
673 * fwnode_name_eq - Return true if node name is equal
674 * @fwnode: The firmware node
675 * @name: The name to which to compare the node name
676 *
677 * Compare the name provided as an argument to the name of the node, stopping
678 * the comparison at either NUL or '@' character, whichever comes first. This
679 * function is generally used for comparing node names while ignoring the
680 * possible unit address of the node.
681 *
682 * Return: true if the node name matches with the name provided in the @name
683 * argument, false otherwise.
684 */
685bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)
686{
687 const char *node_name;
688 ptrdiff_t len;
689
690 node_name = fwnode_get_name(fwnode);
691 if (!node_name)
692 return false;
693
694 len = strchrnul(node_name, '@') - node_name;
695
696 return str_has_prefix(node_name, name) == len;
697}
698EXPORT_SYMBOL_GPL(fwnode_name_eq);
699
a57b7fb7
SA
700/**
701 * fwnode_get_parent - Return parent firwmare node
702 * @fwnode: Firmware whose parent is retrieved
703 *
295209ca
AS
704 * The caller is responsible for calling fwnode_handle_put() on the returned
705 * fwnode pointer.
706 *
707 * Return: parent firmware node of the given node if possible or %NULL if no
a57b7fb7
SA
708 * parent was available.
709 */
710struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
711{
712 return fwnode_call_ptr_op(fwnode, get_parent);
713}
714EXPORT_SYMBOL_GPL(fwnode_get_parent);
715
23387258
SA
716/**
717 * fwnode_get_next_parent - Iterate to the node's parent
718 * @fwnode: Firmware whose parent is retrieved
719 *
720 * This is like fwnode_get_parent() except that it drops the refcount
721 * on the passed node, making it suitable for iterating through a
722 * node's parents.
723 *
295209ca
AS
724 * The caller is responsible for calling fwnode_handle_put() on the returned
725 * fwnode pointer. Note that this function also puts a reference to @fwnode
726 * unconditionally.
727 *
728 * Return: parent firmware node of the given node if possible or %NULL if no
729 * parent was available.
23387258
SA
730 */
731struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
732{
733 struct fwnode_handle *parent = fwnode_get_parent(fwnode);
734
735 fwnode_handle_put(fwnode);
736
737 return parent;
738}
739EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
740
87e5e95d
SA
741/**
742 * fwnode_count_parents - Return the number of parents a node has
743 * @fwnode: The node the parents of which are to be counted
744 *
295209ca 745 * Return: the number of parents a node has.
87e5e95d
SA
746 */
747unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
748{
87ffea09
AS
749 struct fwnode_handle *parent;
750 unsigned int count = 0;
87e5e95d 751
87ffea09
AS
752 fwnode_for_each_parent_node(fwnode, parent)
753 count++;
87e5e95d
SA
754
755 return count;
756}
757EXPORT_SYMBOL_GPL(fwnode_count_parents);
758
759/**
760 * fwnode_get_nth_parent - Return an nth parent of a node
761 * @fwnode: The node the parent of which is requested
762 * @depth: Distance of the parent from the node
763 *
295209ca
AS
764 * The caller is responsible for calling fwnode_handle_put() on the returned
765 * fwnode pointer.
766 *
767 * Return: the nth parent of a node. If there is no parent at the requested
87e5e95d
SA
768 * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
769 * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
87e5e95d
SA
770 */
771struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
772 unsigned int depth)
773{
87ffea09 774 struct fwnode_handle *parent;
87e5e95d 775
87ffea09
AS
776 if (depth == 0)
777 return fwnode_handle_get(fwnode);
87e5e95d 778
87ffea09
AS
779 fwnode_for_each_parent_node(fwnode, parent) {
780 if (--depth == 0)
781 return parent;
782 }
783 return NULL;
87e5e95d
SA
784}
785EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
786
8a0662d9 787/**
34055190
MW
788 * fwnode_get_next_child_node - Return the next child node handle for a node
789 * @fwnode: Firmware node to find the next child node for.
790 * @child: Handle to one of the node's child nodes or a %NULL handle.
295209ca
AS
791 *
792 * The caller is responsible for calling fwnode_handle_put() on the returned
793 * fwnode pointer. Note that this function also puts a reference to @child
794 * unconditionally.
8a0662d9 795 */
37ba983c
SA
796struct fwnode_handle *
797fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
798 struct fwnode_handle *child)
8a0662d9 799{
3708184a 800 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
8a0662d9 801}
34055190
MW
802EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
803
3395de96 804/**
295209ca 805 * fwnode_get_next_available_child_node - Return the next available child node handle for a node
3395de96
MW
806 * @fwnode: Firmware node to find the next child node for.
807 * @child: Handle to one of the node's child nodes or a %NULL handle.
295209ca
AS
808 *
809 * The caller is responsible for calling fwnode_handle_put() on the returned
810 * fwnode pointer. Note that this function also puts a reference to @child
811 * unconditionally.
3395de96
MW
812 */
813struct fwnode_handle *
814fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
815 struct fwnode_handle *child)
816{
817 struct fwnode_handle *next_child = child;
818
002752af 819 if (IS_ERR_OR_NULL(fwnode))
3395de96
MW
820 return NULL;
821
822 do {
823 next_child = fwnode_get_next_child_node(fwnode, next_child);
7e7ba9b3
AS
824 if (!next_child)
825 return NULL;
826 } while (!fwnode_device_is_available(next_child));
3395de96
MW
827
828 return next_child;
829}
830EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
831
34055190
MW
832/**
833 * device_get_next_child_node - Return the next child node handle for a device
834 * @dev: Device to find the next child node for.
295209ca
AS
835 * @child: Handle to one of the device's child nodes or a %NULL handle.
836 *
837 * The caller is responsible for calling fwnode_handle_put() on the returned
838 * fwnode pointer. Note that this function also puts a reference to @child
839 * unconditionally.
34055190 840 */
7952cd2b 841struct fwnode_handle *device_get_next_child_node(const struct device *dev,
34055190
MW
842 struct fwnode_handle *child)
843{
fb38f314
AS
844 const struct fwnode_handle *fwnode = dev_fwnode(dev);
845 struct fwnode_handle *next;
34055190 846
002752af
AS
847 if (IS_ERR_OR_NULL(fwnode))
848 return NULL;
849
114dbb4f
AS
850 /* Try to find a child in primary fwnode */
851 next = fwnode_get_next_child_node(fwnode, child);
852 if (next)
853 return next;
854
855 /* When no more children in primary, continue with secondary */
002752af 856 return fwnode_get_next_child_node(fwnode->secondary, child);
34055190 857}
8a0662d9
RW
858EXPORT_SYMBOL_GPL(device_get_next_child_node);
859
613e9721 860/**
21ea73f5
MW
861 * fwnode_get_named_child_node - Return first matching named child node handle
862 * @fwnode: Firmware node to find the named child node for.
613e9721 863 * @childname: String to match child node name against.
295209ca
AS
864 *
865 * The caller is responsible for calling fwnode_handle_put() on the returned
866 * fwnode pointer.
613e9721 867 */
37ba983c
SA
868struct fwnode_handle *
869fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
870 const char *childname)
613e9721 871{
3708184a 872 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
613e9721 873}
21ea73f5
MW
874EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
875
876/**
877 * device_get_named_child_node - Return first matching named child node handle
878 * @dev: Device to find the named child node for.
879 * @childname: String to match child node name against.
295209ca
AS
880 *
881 * The caller is responsible for calling fwnode_handle_put() on the returned
882 * fwnode pointer.
21ea73f5 883 */
7952cd2b 884struct fwnode_handle *device_get_named_child_node(const struct device *dev,
21ea73f5
MW
885 const char *childname)
886{
887 return fwnode_get_named_child_node(dev_fwnode(dev), childname);
888}
613e9721
AT
889EXPORT_SYMBOL_GPL(device_get_named_child_node);
890
e7887c28
SA
891/**
892 * fwnode_handle_get - Obtain a reference to a device node
893 * @fwnode: Pointer to the device node to obtain the reference to.
cf89a31c 894 *
295209ca
AS
895 * The caller is responsible for calling fwnode_handle_put() on the returned
896 * fwnode pointer.
897 *
898 * Return: the fwnode handle.
e7887c28 899 */
cf89a31c 900struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
e7887c28 901{
cf89a31c
SA
902 if (!fwnode_has_op(fwnode, get))
903 return fwnode;
904
905 return fwnode_call_ptr_op(fwnode, get);
e7887c28
SA
906}
907EXPORT_SYMBOL_GPL(fwnode_handle_get);
908
2294b3af
SA
909/**
910 * fwnode_device_is_available - check if a device is available for use
911 * @fwnode: Pointer to the fwnode of the device.
5273382d 912 *
295209ca
AS
913 * Return: true if device is available for use. Otherwise, returns false.
914 *
5273382d
DS
915 * For fwnode node types that don't implement the .device_is_available()
916 * operation, this function returns true.
2294b3af 917 */
37ba983c 918bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
2294b3af 919{
002752af
AS
920 if (IS_ERR_OR_NULL(fwnode))
921 return false;
922
5273382d
DS
923 if (!fwnode_has_op(fwnode, device_is_available))
924 return true;
925
e8158b48 926 return fwnode_call_bool_op(fwnode, device_is_available);
2294b3af
SA
927}
928EXPORT_SYMBOL_GPL(fwnode_device_is_available);
929
8a0662d9 930/**
1490cbb9
AS
931 * fwnode_get_child_node_count - return the number of child nodes for a given firmware node
932 * @fwnode: Pointer to the parent firmware node
295209ca 933 *
1490cbb9 934 * Return: the number of child nodes for a given firmware node.
8a0662d9 935 */
1490cbb9 936unsigned int fwnode_get_child_node_count(const struct fwnode_handle *fwnode)
8a0662d9
RW
937{
938 struct fwnode_handle *child;
939 unsigned int count = 0;
940
1490cbb9 941 fwnode_for_each_child_node(fwnode, child)
8a0662d9
RW
942 count++;
943
944 return count;
945}
1490cbb9 946EXPORT_SYMBOL_GPL(fwnode_get_child_node_count);
05ca5560 947
f2430363
MV
948/**
949 * fwnode_get_named_child_node_count - number of child nodes with given name
950 * @fwnode: Node which child nodes are counted.
951 * @name: String to match child node name against.
952 *
953 * Scan child nodes and count all the nodes with a specific name. Potential
954 * 'number' -ending after the 'at sign' for scanned names is ignored.
955 * E.g.::
956 * fwnode_get_named_child_node_count(fwnode, "channel");
957 * would match all the nodes::
958 * channel { }, channel@0 {}, channel@0xabba {}...
959 *
960 * Return: the number of child nodes with a matching name for a given device.
961 */
962unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,
963 const char *name)
964{
965 struct fwnode_handle *child;
966 unsigned int count = 0;
967
968 fwnode_for_each_named_child_node(fwnode, child, name)
969 count++;
970
971 return count;
972}
973EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count);
974
59789f34 975bool device_dma_supported(const struct device *dev)
e5e55864 976{
8c756a0a 977 return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
e5e55864
SS
978}
979EXPORT_SYMBOL_GPL(device_dma_supported);
980
59789f34 981enum dev_dma_attr device_get_dma_attr(const struct device *dev)
e5e55864 982{
8c756a0a
SA
983 if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))
984 return DEV_DMA_NOT_SUPPORTED;
e5e55864 985
8c756a0a 986 return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);
e5e55864
SS
987}
988EXPORT_SYMBOL_GPL(device_get_dma_attr);
989
4c96b7dc 990/**
b28f263b
MW
991 * fwnode_get_phy_mode - Get phy mode for given firmware node
992 * @fwnode: Pointer to the given node
4c96b7dc
JL
993 *
994 * The function gets phy interface string from property 'phy-mode' or
995 * 'phy-connection-type', and return its index in phy_modes table, or errno in
996 * error case.
997 */
0a392354 998int fwnode_get_phy_mode(const struct fwnode_handle *fwnode)
4c96b7dc
JL
999{
1000 const char *pm;
1001 int err, i;
1002
b28f263b 1003 err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
4c96b7dc 1004 if (err < 0)
b28f263b 1005 err = fwnode_property_read_string(fwnode,
4c96b7dc
JL
1006 "phy-connection-type", &pm);
1007 if (err < 0)
1008 return err;
1009
1010 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1011 if (!strcasecmp(pm, phy_modes(i)))
1012 return i;
1013
1014 return -ENODEV;
1015}
b28f263b
MW
1016EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
1017
1018/**
1019 * device_get_phy_mode - Get phy mode for given device
1020 * @dev: Pointer to the given device
1021 *
1022 * The function gets phy interface string from property 'phy-mode' or
1023 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1024 * error case.
1025 */
1026int device_get_phy_mode(struct device *dev)
1027{
1028 return fwnode_get_phy_mode(dev_fwnode(dev));
1029}
4c96b7dc
JL
1030EXPORT_SYMBOL_GPL(device_get_phy_mode);
1031
b2638e56
AS
1032/**
1033 * fwnode_iomap - Maps the memory mapped IO for a given fwnode
1034 * @fwnode: Pointer to the firmware node
1035 * @index: Index of the IO range
1036 *
295209ca 1037 * Return: a pointer to the mapped memory.
b2638e56
AS
1038 */
1039void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)
1040{
68b979d0 1041 return fwnode_call_ptr_op(fwnode, iomap, index);
b2638e56
AS
1042}
1043EXPORT_SYMBOL(fwnode_iomap);
1044
7c6c57f2
MW
1045/**
1046 * fwnode_irq_get - Get IRQ directly from a fwnode
1047 * @fwnode: Pointer to the firmware node
1048 * @index: Zero-based index of the IRQ
1049 *
39d42255 1050 * Return: Linux IRQ number on success. Negative errno on failure.
7c6c57f2 1051 */
fb38f314 1052int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
7c6c57f2 1053{
39d42255
MV
1054 int ret;
1055
1056 ret = fwnode_call_int_op(fwnode, irq_get, index);
1057 /* We treat mapping errors as invalid case */
1058 if (ret == 0)
1059 return -EINVAL;
1060
1061 return ret;
7c6c57f2
MW
1062}
1063EXPORT_SYMBOL(fwnode_irq_get);
1064
ca0acb51
A
1065/**
1066 * fwnode_irq_get_byname - Get IRQ from a fwnode using its name
1067 * @fwnode: Pointer to the firmware node
1068 * @name: IRQ name
1069 *
1070 * Description:
1071 * Find a match to the string @name in the 'interrupt-names' string array
1072 * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ
1073 * number of the IRQ resource corresponding to the index of the matched
1074 * string.
1075 *
295209ca 1076 * Return: Linux IRQ number on success, or negative errno otherwise.
ca0acb51
A
1077 */
1078int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
1079{
1080 int index;
1081
1082 if (!name)
1083 return -EINVAL;
1084
1085 index = fwnode_property_match_string(fwnode, "interrupt-names", name);
1086 if (index < 0)
1087 return index;
1088
1089 return fwnode_irq_get(fwnode, index);
1090}
1091EXPORT_SYMBOL(fwnode_irq_get_byname);
1092
07bb80d4 1093/**
f569da8c 1094 * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
07bb80d4
MW
1095 * @fwnode: Pointer to the parent firmware node
1096 * @prev: Previous endpoint node or %NULL to get the first
1097 *
295209ca
AS
1098 * The caller is responsible for calling fwnode_handle_put() on the returned
1099 * fwnode pointer. Note that this function also puts a reference to @prev
1100 * unconditionally.
1101 *
1102 * Return: an endpoint firmware node pointer or %NULL if no more endpoints
07bb80d4
MW
1103 * are available.
1104 */
1105struct fwnode_handle *
37ba983c 1106fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
07bb80d4
MW
1107 struct fwnode_handle *prev)
1108{
39af7286 1109 struct fwnode_handle *ep, *port_parent = NULL;
b5b41ab6 1110 const struct fwnode_handle *parent;
b5b41ab6
DS
1111
1112 /*
1113 * If this function is in a loop and the previous iteration returned
1114 * an endpoint from fwnode->secondary, then we need to use the secondary
1115 * as parent rather than @fwnode.
1116 */
39af7286
YY
1117 if (prev) {
1118 port_parent = fwnode_graph_get_port_parent(prev);
1119 parent = port_parent;
1120 } else {
b5b41ab6 1121 parent = fwnode;
39af7286 1122 }
002752af
AS
1123 if (IS_ERR_OR_NULL(parent))
1124 return NULL;
b5b41ab6
DS
1125
1126 ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
002752af 1127 if (ep)
39af7286
YY
1128 goto out_put_port_parent;
1129
1130 ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);
b5b41ab6 1131
39af7286
YY
1132out_put_port_parent:
1133 fwnode_handle_put(port_parent);
1134 return ep;
07bb80d4
MW
1135}
1136EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1137
6a71d8d7
KB
1138/**
1139 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1140 * @endpoint: Endpoint firmware node of the port
1141 *
295209ca
AS
1142 * The caller is responsible for calling fwnode_handle_put() on the returned
1143 * fwnode pointer.
1144 *
6a71d8d7
KB
1145 * Return: the firmware node of the device the @endpoint belongs to.
1146 */
1147struct fwnode_handle *
37ba983c 1148fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
6a71d8d7
KB
1149{
1150 struct fwnode_handle *port, *parent;
1151
1152 port = fwnode_get_parent(endpoint);
1153 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1154
1155 fwnode_handle_put(port);
1156
1157 return parent;
1158}
1159EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1160
07bb80d4
MW
1161/**
1162 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1163 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1164 *
1165 * Extracts firmware node of a remote device the @fwnode points to.
295209ca
AS
1166 *
1167 * The caller is responsible for calling fwnode_handle_put() on the returned
1168 * fwnode pointer.
07bb80d4
MW
1169 */
1170struct fwnode_handle *
37ba983c 1171fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
07bb80d4 1172{
6a71d8d7 1173 struct fwnode_handle *endpoint, *parent;
07bb80d4 1174
6a71d8d7
KB
1175 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1176 parent = fwnode_graph_get_port_parent(endpoint);
07bb80d4 1177
6a71d8d7 1178 fwnode_handle_put(endpoint);
07bb80d4
MW
1179
1180 return parent;
1181}
1182EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1183
1184/**
1185 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1186 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1187 *
1188 * Extracts firmware node of a remote port the @fwnode points to.
295209ca
AS
1189 *
1190 * The caller is responsible for calling fwnode_handle_put() on the returned
1191 * fwnode pointer.
07bb80d4 1192 */
37ba983c
SA
1193struct fwnode_handle *
1194fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
07bb80d4 1195{
3b27d00e 1196 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
07bb80d4
MW
1197}
1198EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1199
1200/**
1201 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1202 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1203 *
1204 * Extracts firmware node of a remote endpoint the @fwnode points to.
295209ca
AS
1205 *
1206 * The caller is responsible for calling fwnode_handle_put() on the returned
1207 * fwnode pointer.
07bb80d4
MW
1208 */
1209struct fwnode_handle *
37ba983c 1210fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
07bb80d4 1211{
3b27d00e 1212 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
07bb80d4
MW
1213}
1214EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
2bd5452d 1215
c87b8fc5
SA
1216static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
1217{
1218 struct fwnode_handle *dev_node;
1219 bool available;
1220
1221 dev_node = fwnode_graph_get_remote_port_parent(ep);
1222 available = fwnode_device_is_available(dev_node);
1223 fwnode_handle_put(dev_node);
1224
1225 return available;
1226}
1227
0fcc2bdc
SA
1228/**
1229 * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1230 * @fwnode: parent fwnode_handle containing the graph
1231 * @port: identifier of the port node
1232 * @endpoint: identifier of the endpoint node under the port node
1233 * @flags: fwnode lookup flags
1234 *
295209ca
AS
1235 * The caller is responsible for calling fwnode_handle_put() on the returned
1236 * fwnode pointer.
1237 *
1238 * Return: the fwnode handle of the local endpoint corresponding the port and
1239 * endpoint IDs or %NULL if not found.
0fcc2bdc
SA
1240 *
1241 * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1242 * has not been found, look for the closest endpoint ID greater than the
1243 * specified one and return the endpoint that corresponds to it, if present.
1244 *
49f39cb0
SA
1245 * Does not return endpoints that belong to disabled devices or endpoints that
1246 * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
0fcc2bdc
SA
1247 */
1248struct fwnode_handle *
1249fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1250 u32 port, u32 endpoint, unsigned long flags)
1251{
0d82017b 1252 struct fwnode_handle *ep, *best_ep = NULL;
0fcc2bdc
SA
1253 unsigned int best_ep_id = 0;
1254 bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1255 bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1256
0d82017b 1257 fwnode_graph_for_each_endpoint(fwnode, ep) {
0fcc2bdc
SA
1258 struct fwnode_endpoint fwnode_ep = { 0 };
1259 int ret;
1260
c87b8fc5
SA
1261 if (enabled_only && !fwnode_graph_remote_available(ep))
1262 continue;
0fcc2bdc
SA
1263
1264 ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1265 if (ret < 0)
1266 continue;
1267
1268 if (fwnode_ep.port != port)
1269 continue;
1270
1271 if (fwnode_ep.id == endpoint)
1272 return ep;
1273
1274 if (!endpoint_next)
1275 continue;
1276
1277 /*
1278 * If the endpoint that has just been found is not the first
1279 * matching one and the ID of the one found previously is closer
1280 * to the requested endpoint ID, skip it.
1281 */
1282 if (fwnode_ep.id < endpoint ||
1283 (best_ep && best_ep_id < fwnode_ep.id))
1284 continue;
1285
1286 fwnode_handle_put(best_ep);
1287 best_ep = fwnode_handle_get(ep);
1288 best_ep_id = fwnode_ep.id;
1289 }
1290
a9088770 1291 return best_ep;
0fcc2bdc
SA
1292}
1293EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1294
c87b8fc5
SA
1295/**
1296 * fwnode_graph_get_endpoint_count - Count endpoints on a device node
1297 * @fwnode: The node related to a device
1298 * @flags: fwnode lookup flags
1299 * Count endpoints in a device node.
1300 *
1301 * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
1302 * and endpoints connected to disabled devices are counted.
1303 */
5b9ff0ba 1304unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
c87b8fc5
SA
1305 unsigned long flags)
1306{
1307 struct fwnode_handle *ep;
1308 unsigned int count = 0;
1309
1310 fwnode_graph_for_each_endpoint(fwnode, ep) {
1311 if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
1312 fwnode_graph_remote_available(ep))
1313 count++;
1314 }
1315
1316 return count;
1317}
1318EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
1319
2bd5452d
SA
1320/**
1321 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1322 * @fwnode: pointer to endpoint fwnode_handle
1323 * @endpoint: pointer to the fwnode endpoint data structure
1324 *
1325 * Parse @fwnode representing a graph endpoint node and store the
1326 * information in @endpoint. The caller must hold a reference to
1327 * @fwnode.
1328 */
37ba983c 1329int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
2bd5452d
SA
1330 struct fwnode_endpoint *endpoint)
1331{
2bd5452d
SA
1332 memset(endpoint, 0, sizeof(*endpoint));
1333
3b27d00e 1334 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
2bd5452d
SA
1335}
1336EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
b283f157 1337
aade55c8 1338const void *device_get_match_data(const struct device *dev)
b283f157 1339{
67dcc26d 1340 return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
b283f157
SK
1341}
1342EXPORT_SYMBOL_GPL(device_get_match_data);
d7cf5590 1343
23ead33b 1344static unsigned int fwnode_graph_devcon_matches(const struct fwnode_handle *fwnode,
7a20917d
BA
1345 const char *con_id, void *data,
1346 devcon_match_fn_t match,
1347 void **matches,
1348 unsigned int matches_len)
d7cf5590
HK
1349{
1350 struct fwnode_handle *node;
1351 struct fwnode_handle *ep;
7a20917d 1352 unsigned int count = 0;
d7cf5590
HK
1353 void *ret;
1354
1355 fwnode_graph_for_each_endpoint(fwnode, ep) {
7a20917d
BA
1356 if (matches && count >= matches_len) {
1357 fwnode_handle_put(ep);
1358 break;
1359 }
1360
d7cf5590 1361 node = fwnode_graph_get_remote_port_parent(ep);
4a7f4110
SA
1362 if (!fwnode_device_is_available(node)) {
1363 fwnode_handle_put(node);
d7cf5590 1364 continue;
4a7f4110 1365 }
d7cf5590
HK
1366
1367 ret = match(node, con_id, data);
1368 fwnode_handle_put(node);
1369 if (ret) {
7a20917d
BA
1370 if (matches)
1371 matches[count] = ret;
1372 count++;
d7cf5590
HK
1373 }
1374 }
7a20917d 1375 return count;
d7cf5590
HK
1376}
1377
23ead33b 1378static unsigned int fwnode_devcon_matches(const struct fwnode_handle *fwnode,
7a20917d
BA
1379 const char *con_id, void *data,
1380 devcon_match_fn_t match,
1381 void **matches,
1382 unsigned int matches_len)
d7cf5590
HK
1383{
1384 struct fwnode_handle *node;
7a20917d
BA
1385 unsigned int count = 0;
1386 unsigned int i;
d7cf5590 1387 void *ret;
d7cf5590
HK
1388
1389 for (i = 0; ; i++) {
7a20917d
BA
1390 if (matches && count >= matches_len)
1391 break;
1392
d7cf5590
HK
1393 node = fwnode_find_reference(fwnode, con_id, i);
1394 if (IS_ERR(node))
1395 break;
1396
1397 ret = match(node, NULL, data);
1398 fwnode_handle_put(node);
7a20917d
BA
1399 if (ret) {
1400 if (matches)
1401 matches[count] = ret;
1402 count++;
1403 }
d7cf5590
HK
1404 }
1405
7a20917d 1406 return count;
d7cf5590
HK
1407}
1408
1409/**
1410 * fwnode_connection_find_match - Find connection from a device node
1411 * @fwnode: Device node with the connection
1412 * @con_id: Identifier for the connection
1413 * @data: Data for the match function
1414 * @match: Function to check and convert the connection description
1415 *
1416 * Find a connection with unique identifier @con_id between @fwnode and another
1417 * device node. @match will be used to convert the connection description to
1418 * data the caller is expecting to be returned.
1419 */
23ead33b 1420void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
d7cf5590
HK
1421 const char *con_id, void *data,
1422 devcon_match_fn_t match)
1423{
bcd6a517 1424 unsigned int count;
d7cf5590
HK
1425 void *ret;
1426
1427 if (!fwnode || !match)
1428 return NULL;
1429
bcd6a517
BA
1430 count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1431 if (count)
d7cf5590
HK
1432 return ret;
1433
bcd6a517
BA
1434 count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1435 return count ? ret : NULL;
d7cf5590
HK
1436}
1437EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
7a20917d
BA
1438
1439/**
1440 * fwnode_connection_find_matches - Find connections from a device node
1441 * @fwnode: Device node with the connection
1442 * @con_id: Identifier for the connection
1443 * @data: Data for the match function
1444 * @match: Function to check and convert the connection description
1445 * @matches: (Optional) array of pointers to fill with matches
1446 * @matches_len: Length of @matches
1447 *
1448 * Find up to @matches_len connections with unique identifier @con_id between
1449 * @fwnode and other device nodes. @match will be used to convert the
1450 * connection description to data the caller is expecting to be returned
1451 * through the @matches array.
295209ca
AS
1452 *
1453 * If @matches is %NULL @matches_len is ignored and the total number of resolved
7a20917d
BA
1454 * matches is returned.
1455 *
1456 * Return: Number of matches resolved, or negative errno.
1457 */
23ead33b 1458int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
7a20917d
BA
1459 const char *con_id, void *data,
1460 devcon_match_fn_t match,
1461 void **matches, unsigned int matches_len)
1462{
1463 unsigned int count_graph;
1464 unsigned int count_ref;
1465
1466 if (!fwnode || !match)
1467 return -EINVAL;
1468
1469 count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,
1470 matches, matches_len);
1471
1472 if (matches) {
1473 matches += count_graph;
1474 matches_len -= count_graph;
1475 }
1476
1477 count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,
1478 matches, matches_len);
1479
1480 return count_graph + count_ref;
1481}
1482EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);