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