1 // SPDX-License-Identifier: GPL-2.0
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>
8 */
9
10 #include <linux/acpi.h>
11 #include <linux/export.h>
12 #include <linux/kernel.h>
13 #include <linux/of.h>
14 #include <linux/of_address.h>
15 #include <linux/of_graph.h>
16 #include <linux/of_irq.h>
17 #include <linux/property.h>
18 #include <linux/etherdevice.h>
19 #include <linux/phy.h>
20
dev_fwnode(struct device * dev)21 struct fwnode_handle *dev_fwnode(struct device *dev)
22 {
23 return IS_ENABLED(CONFIG_OF) && dev->of_node ?
24 of_fwnode_handle(dev->of_node) : dev->fwnode;
25 }
26 EXPORT_SYMBOL_GPL(dev_fwnode);
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 */
device_property_present(struct device * dev,const char * propname)35 bool device_property_present(struct device *dev, const char *propname)
36 {
37 return fwnode_property_present(dev_fwnode(dev), propname);
38 }
39 EXPORT_SYMBOL_GPL(device_property_present);
40
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 */
fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)46 bool fwnode_property_present(const struct fwnode_handle *fwnode,
47 const char *propname)
48 {
49 bool ret;
50
51 if (IS_ERR_OR_NULL(fwnode))
52 return false;
53
54 ret = fwnode_call_bool_op(fwnode, property_present, propname);
55 if (ret)
56 return ret;
57
58 return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
59 }
60 EXPORT_SYMBOL_GPL(fwnode_property_present);
61
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
66 * @val: The values are stored here or %NULL to return the number of values
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 *
72 * Return: number of values if @val was %NULL,
73 * %0 if the property was found (success),
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.
78 * %-ENXIO if no suitable firmware interface is present.
79 */
device_property_read_u8_array(struct device * dev,const char * propname,u8 * val,size_t nval)80 int device_property_read_u8_array(struct device *dev, const char *propname,
81 u8 *val, size_t nval)
82 {
83 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
84 }
85 EXPORT_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
91 * @val: The values are stored here or %NULL to return the number of values
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 *
97 * Return: number of values if @val was %NULL,
98 * %0 if the property was found (success),
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.
103 * %-ENXIO if no suitable firmware interface is present.
104 */
device_property_read_u16_array(struct device * dev,const char * propname,u16 * val,size_t nval)105 int device_property_read_u16_array(struct device *dev, const char *propname,
106 u16 *val, size_t nval)
107 {
108 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
109 }
110 EXPORT_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
116 * @val: The values are stored here or %NULL to return the number of values
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 *
122 * Return: number of values if @val was %NULL,
123 * %0 if the property was found (success),
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.
128 * %-ENXIO if no suitable firmware interface is present.
129 */
device_property_read_u32_array(struct device * dev,const char * propname,u32 * val,size_t nval)130 int device_property_read_u32_array(struct device *dev, const char *propname,
131 u32 *val, size_t nval)
132 {
133 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
134 }
135 EXPORT_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
141 * @val: The values are stored here or %NULL to return the number of values
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 *
147 * Return: number of values if @val was %NULL,
148 * %0 if the property was found (success),
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.
153 * %-ENXIO if no suitable firmware interface is present.
154 */
device_property_read_u64_array(struct device * dev,const char * propname,u64 * val,size_t nval)155 int device_property_read_u64_array(struct device *dev, const char *propname,
156 u64 *val, size_t nval)
157 {
158 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
159 }
160 EXPORT_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
166 * @val: The values are stored here or %NULL to return the number of values
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 *
172 * Return: number of values read on success if @val is non-NULL,
173 * number of values available on success if @val is NULL,
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.
178 * %-ENXIO if no suitable firmware interface is present.
179 */
device_property_read_string_array(struct device * dev,const char * propname,const char ** val,size_t nval)180 int device_property_read_string_array(struct device *dev, const char *propname,
181 const char **val, size_t nval)
182 {
183 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
184 }
185 EXPORT_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.
200 * %-ENXIO if no suitable firmware interface is present.
201 */
device_property_read_string(struct device * dev,const char * propname,const char ** val)202 int device_property_read_string(struct device *dev, const char *propname,
203 const char **val)
204 {
205 return fwnode_property_read_string(dev_fwnode(dev), propname, val);
206 }
207 EXPORT_SYMBOL_GPL(device_property_read_string);
208
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 */
device_property_match_string(struct device * dev,const char * propname,const char * string)224 int 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 }
229 EXPORT_SYMBOL_GPL(device_property_match_string);
230
fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)231 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
232 const char *propname,
233 unsigned int elem_size, void *val,
234 size_t nval)
235 {
236 int ret;
237
238 if (IS_ERR_OR_NULL(fwnode))
239 return -EINVAL;
240
241 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
242 elem_size, val, nval);
243 if (ret != -EINVAL)
244 return ret;
245
246 return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
247 elem_size, val, nval);
248 }
249
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
254 * @val: The values are stored here or %NULL to return the number of values
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 *
260 * Return: number of values if @val was %NULL,
261 * %0 if the property was found (success),
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 */
fwnode_property_read_u8_array(const struct fwnode_handle * fwnode,const char * propname,u8 * val,size_t nval)268 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
269 const char *propname, u8 *val, size_t nval)
270 {
271 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
272 val, nval);
273 }
274 EXPORT_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
280 * @val: The values are stored here or %NULL to return the number of values
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 *
286 * Return: number of values if @val was %NULL,
287 * %0 if the property was found (success),
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 */
fwnode_property_read_u16_array(const struct fwnode_handle * fwnode,const char * propname,u16 * val,size_t nval)294 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
295 const char *propname, u16 *val, size_t nval)
296 {
297 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
298 val, nval);
299 }
300 EXPORT_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
306 * @val: The values are stored here or %NULL to return the number of values
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 *
312 * Return: number of values if @val was %NULL,
313 * %0 if the property was found (success),
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 */
fwnode_property_read_u32_array(const struct fwnode_handle * fwnode,const char * propname,u32 * val,size_t nval)320 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
321 const char *propname, u32 *val, size_t nval)
322 {
323 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
324 val, nval);
325 }
326 EXPORT_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
332 * @val: The values are stored here or %NULL to return the number of values
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 *
338 * Return: number of values if @val was %NULL,
339 * %0 if the property was found (success),
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 */
fwnode_property_read_u64_array(const struct fwnode_handle * fwnode,const char * propname,u64 * val,size_t nval)346 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
347 const char *propname, u64 *val, size_t nval)
348 {
349 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
350 val, nval);
351 }
352 EXPORT_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
358 * @val: The values are stored here or %NULL to return the number of values
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 *
364 * Return: number of values read on success if @val is non-NULL,
365 * number of values available on success if @val is NULL,
366 * %-EINVAL if given arguments are not valid,
367 * %-ENODATA if the property does not have a value,
368 * %-EPROTO or %-EILSEQ if the property is not an array of strings,
369 * %-EOVERFLOW if the size of the property is not as expected,
370 * %-ENXIO if no suitable firmware interface is present.
371 */
fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)372 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
373 const char *propname, const char **val,
374 size_t nval)
375 {
376 int ret;
377
378 if (IS_ERR_OR_NULL(fwnode))
379 return -EINVAL;
380
381 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
382 val, nval);
383 if (ret != -EINVAL)
384 return ret;
385
386 return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
387 val, nval);
388 }
389 EXPORT_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 */
fwnode_property_read_string(const struct fwnode_handle * fwnode,const char * propname,const char ** val)406 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
407 const char *propname, const char **val)
408 {
409 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
410
411 return ret < 0 ? ret : 0;
412 }
413 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
414
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 */
fwnode_property_match_string(const struct fwnode_handle * fwnode,const char * propname,const char * string)430 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
431 const char *propname, const char *string)
432 {
433 const char **values;
434 int nval, ret;
435
436 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
437 if (nval < 0)
438 return nval;
439
440 if (nval == 0)
441 return -ENODATA;
442
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
451 ret = match_string(values, nval, string);
452 if (ret < 0)
453 ret = -ENODATA;
454 out:
455 kfree(values);
456 return ret;
457 }
458 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
459
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 *
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
481 */
fwnode_property_get_reference_args(const struct fwnode_handle * fwnode,const char * prop,const char * nargs_prop,unsigned int nargs,unsigned int index,struct fwnode_reference_args * args)482 int 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 {
487 int ret;
488
489 if (IS_ERR_OR_NULL(fwnode))
490 return -ENOENT;
491
492 ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
493 nargs, index, args);
494 if (ret == 0)
495 return ret;
496
497 if (IS_ERR_OR_NULL(fwnode->secondary))
498 return ret;
499
500 return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
501 nargs, index, args);
502 }
503 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
504
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 */
fwnode_find_reference(const struct fwnode_handle * fwnode,const char * name,unsigned int index)516 struct 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 }
527 EXPORT_SYMBOL_GPL(fwnode_find_reference);
528
529 /**
530 * device_remove_properties - Remove properties from a device object.
531 * @dev: Device whose properties to remove.
532 *
533 * The function removes properties previously associated to the device
534 * firmware node with device_add_properties(). Memory allocated to the
535 * properties will also be released.
536 */
device_remove_properties(struct device * dev)537 void device_remove_properties(struct device *dev)
538 {
539 struct fwnode_handle *fwnode = dev_fwnode(dev);
540
541 if (!fwnode)
542 return;
543
544 if (is_software_node(fwnode->secondary)) {
545 fwnode_remove_software_node(fwnode->secondary);
546 set_secondary_fwnode(dev, NULL);
547 }
548 }
549 EXPORT_SYMBOL_GPL(device_remove_properties);
550
551 /**
552 * device_add_properties - Add a collection of properties to a device object.
553 * @dev: Device to add properties to.
554 * @properties: Collection of properties to add.
555 *
556 * Associate a collection of device properties represented by @properties with
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.
562 */
device_add_properties(struct device * dev,const struct property_entry * properties)563 int device_add_properties(struct device *dev,
564 const struct property_entry *properties)
565 {
566 struct fwnode_handle *fwnode;
567
568 fwnode = fwnode_create_software_node(properties, NULL);
569 if (IS_ERR(fwnode))
570 return PTR_ERR(fwnode);
571
572 set_secondary_fwnode(dev, fwnode);
573 return 0;
574 }
575 EXPORT_SYMBOL_GPL(device_add_properties);
576
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 */
fwnode_get_name(const struct fwnode_handle * fwnode)583 const char *fwnode_get_name(const struct fwnode_handle *fwnode)
584 {
585 return fwnode_call_ptr_op(fwnode, get_name);
586 }
587 EXPORT_SYMBOL_GPL(fwnode_get_name);
588
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 */
fwnode_get_name_prefix(const struct fwnode_handle * fwnode)596 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
597 {
598 return fwnode_call_ptr_op(fwnode, get_name_prefix);
599 }
600
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 */
fwnode_get_parent(const struct fwnode_handle * fwnode)608 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
609 {
610 return fwnode_call_ptr_op(fwnode, get_parent);
611 }
612 EXPORT_SYMBOL_GPL(fwnode_get_parent);
613
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 */
fwnode_get_next_parent(struct fwnode_handle * fwnode)625 struct 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 }
633 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
634
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 */
fwnode_get_next_parent_dev(struct fwnode_handle * fwnode)646 struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
647 {
648 struct device *dev;
649
650 fwnode_handle_get(fwnode);
651 do {
652 fwnode = fwnode_get_next_parent(fwnode);
653 if (!fwnode)
654 return NULL;
655 dev = get_dev_from_fwnode(fwnode);
656 } while (!dev);
657 fwnode_handle_put(fwnode);
658 return dev;
659 }
660
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 */
fwnode_count_parents(const struct fwnode_handle * fwnode)667 unsigned 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 }
679 EXPORT_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 */
fwnode_get_nth_parent(struct fwnode_handle * fwnode,unsigned int depth)693 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
694 unsigned int depth)
695 {
696 fwnode_handle_get(fwnode);
697
698 do {
699 if (depth-- == 0)
700 break;
701 fwnode = fwnode_get_next_parent(fwnode);
702 } while (fwnode);
703
704 return fwnode;
705 }
706 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
707
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 */
fwnode_is_ancestor_of(struct fwnode_handle * test_ancestor,struct fwnode_handle * test_child)718 bool fwnode_is_ancestor_of(struct fwnode_handle *test_ancestor,
719 struct fwnode_handle *test_child)
720 {
721 if (IS_ERR_OR_NULL(test_ancestor))
722 return false;
723
724 fwnode_handle_get(test_child);
725 do {
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);
731 } while (test_child);
732 return false;
733 }
734
735 /**
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.
739 */
740 struct fwnode_handle *
fwnode_get_next_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)741 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
742 struct fwnode_handle *child)
743 {
744 return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
745 }
746 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
747
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 */
754 struct fwnode_handle *
fwnode_get_next_available_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)755 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
756 struct fwnode_handle *child)
757 {
758 struct fwnode_handle *next_child = child;
759
760 if (IS_ERR_OR_NULL(fwnode))
761 return NULL;
762
763 do {
764 next_child = fwnode_get_next_child_node(fwnode, next_child);
765 if (!next_child)
766 return NULL;
767 } while (!fwnode_device_is_available(next_child));
768
769 return next_child;
770 }
771 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
772
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 */
device_get_next_child_node(struct device * dev,struct fwnode_handle * child)778 struct fwnode_handle *device_get_next_child_node(struct device *dev,
779 struct fwnode_handle *child)
780 {
781 const struct fwnode_handle *fwnode = dev_fwnode(dev);
782 struct fwnode_handle *next;
783
784 if (IS_ERR_OR_NULL(fwnode))
785 return NULL;
786
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 */
793 return fwnode_get_next_child_node(fwnode->secondary, child);
794 }
795 EXPORT_SYMBOL_GPL(device_get_next_child_node);
796
797 /**
798 * fwnode_get_named_child_node - Return first matching named child node handle
799 * @fwnode: Firmware node to find the named child node for.
800 * @childname: String to match child node name against.
801 */
802 struct fwnode_handle *
fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)803 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
804 const char *childname)
805 {
806 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
807 }
808 EXPORT_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 */
device_get_named_child_node(struct device * dev,const char * childname)815 struct 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 }
820 EXPORT_SYMBOL_GPL(device_get_named_child_node);
821
822 /**
823 * fwnode_handle_get - Obtain a reference to a device node
824 * @fwnode: Pointer to the device node to obtain the reference to.
825 *
826 * Returns the fwnode handle.
827 */
fwnode_handle_get(struct fwnode_handle * fwnode)828 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
829 {
830 if (!fwnode_has_op(fwnode, get))
831 return fwnode;
832
833 return fwnode_call_ptr_op(fwnode, get);
834 }
835 EXPORT_SYMBOL_GPL(fwnode_handle_get);
836
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 */
fwnode_handle_put(struct fwnode_handle * fwnode)845 void fwnode_handle_put(struct fwnode_handle *fwnode)
846 {
847 fwnode_call_void_op(fwnode, put);
848 }
849 EXPORT_SYMBOL_GPL(fwnode_handle_put);
850
851 /**
852 * fwnode_device_is_available - check if a device is available for use
853 * @fwnode: Pointer to the fwnode of the device.
854 *
855 * For fwnode node types that don't implement the .device_is_available()
856 * operation, this function returns true.
857 */
fwnode_device_is_available(const struct fwnode_handle * fwnode)858 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
859 {
860 if (IS_ERR_OR_NULL(fwnode))
861 return false;
862
863 if (!fwnode_has_op(fwnode, device_is_available))
864 return true;
865
866 return fwnode_call_bool_op(fwnode, device_is_available);
867 }
868 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
869
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 */
device_get_child_node_count(struct device * dev)874 unsigned 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 }
884 EXPORT_SYMBOL_GPL(device_get_child_node_count);
885
device_dma_supported(struct device * dev)886 bool device_dma_supported(struct device *dev)
887 {
888 const struct fwnode_handle *fwnode = dev_fwnode(dev);
889
890 /* For DT, this is always supported.
891 * For ACPI, this depends on CCA, which
892 * is determined by the acpi_dma_supported().
893 */
894 if (is_of_node(fwnode))
895 return true;
896
897 return acpi_dma_supported(to_acpi_device_node(fwnode));
898 }
899 EXPORT_SYMBOL_GPL(device_dma_supported);
900
device_get_dma_attr(struct device * dev)901 enum dev_dma_attr device_get_dma_attr(struct device *dev)
902 {
903 const struct fwnode_handle *fwnode = dev_fwnode(dev);
904 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
905
906 if (is_of_node(fwnode)) {
907 if (of_dma_is_coherent(to_of_node(fwnode)))
908 attr = DEV_DMA_COHERENT;
909 else
910 attr = DEV_DMA_NON_COHERENT;
911 } else
912 attr = acpi_get_dma_attr(to_acpi_device_node(fwnode));
913
914 return attr;
915 }
916 EXPORT_SYMBOL_GPL(device_get_dma_attr);
917
918 /**
919 * fwnode_get_phy_mode - Get phy mode for given firmware node
920 * @fwnode: Pointer to the given node
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 */
fwnode_get_phy_mode(struct fwnode_handle * fwnode)926 int fwnode_get_phy_mode(struct fwnode_handle *fwnode)
927 {
928 const char *pm;
929 int err, i;
930
931 err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
932 if (err < 0)
933 err = fwnode_property_read_string(fwnode,
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 }
944 EXPORT_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 */
device_get_phy_mode(struct device * dev)954 int device_get_phy_mode(struct device *dev)
955 {
956 return fwnode_get_phy_mode(dev_fwnode(dev));
957 }
958 EXPORT_SYMBOL_GPL(device_get_phy_mode);
959
fwnode_get_mac_addr(struct fwnode_handle * fwnode,const char * name,char * addr,int alen)960 static void *fwnode_get_mac_addr(struct fwnode_handle *fwnode,
961 const char *name, char *addr,
962 int alen)
963 {
964 int ret = fwnode_property_read_u8_array(fwnode, name, addr, alen);
965
966 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
967 return addr;
968 return NULL;
969 }
970
971 /**
972 * fwnode_get_mac_address - Get the MAC from the firmware node
973 * @fwnode: Pointer to the firmware node
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
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
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.
993 */
fwnode_get_mac_address(struct fwnode_handle * fwnode,char * addr,int alen)994 void *fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr, int alen)
995 {
996 char *res;
997
998 res = fwnode_get_mac_addr(fwnode, "mac-address", addr, alen);
999 if (res)
1000 return res;
1001
1002 res = fwnode_get_mac_addr(fwnode, "local-mac-address", addr, alen);
1003 if (res)
1004 return res;
1005
1006 return fwnode_get_mac_addr(fwnode, "address", addr, alen);
1007 }
1008 EXPORT_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 */
device_get_mac_address(struct device * dev,char * addr,int alen)1016 void *device_get_mac_address(struct device *dev, char *addr, int alen)
1017 {
1018 return fwnode_get_mac_address(dev_fwnode(dev), addr, alen);
1019 }
1020 EXPORT_SYMBOL(device_get_mac_address);
1021
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 */
fwnode_irq_get(const struct fwnode_handle * fwnode,unsigned int index)1030 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
1031 {
1032 struct resource res;
1033 int ret;
1034
1035 if (is_of_node(fwnode))
1036 return of_irq_get(to_of_node(fwnode), index);
1037
1038 ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
1039 if (ret)
1040 return ret;
1041
1042 return res.start;
1043 }
1044 EXPORT_SYMBOL(fwnode_irq_get);
1045
1046 /**
1047 * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
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 */
1054 struct fwnode_handle *
fwnode_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)1055 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1056 struct fwnode_handle *prev)
1057 {
1058 struct fwnode_handle *ep, *port_parent = NULL;
1059 const struct fwnode_handle *parent;
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 port_parent = fwnode_graph_get_port_parent(prev);
1068 parent = port_parent;
1069 } else {
1070 parent = fwnode;
1071 }
1072 if (IS_ERR_OR_NULL(parent))
1073 return NULL;
1074
1075 ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
1076 if (ep)
1077 goto out_put_port_parent;
1078
1079 ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);
1080
1081 out_put_port_parent:
1082 fwnode_handle_put(port_parent);
1083 return ep;
1084 }
1085 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1086
1087 /**
1088 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1089 * @endpoint: Endpoint firmware node of the port
1090 *
1091 * Return: the firmware node of the device the @endpoint belongs to.
1092 */
1093 struct fwnode_handle *
fwnode_graph_get_port_parent(const struct fwnode_handle * endpoint)1094 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1095 {
1096 struct fwnode_handle *port, *parent;
1097
1098 port = fwnode_get_parent(endpoint);
1099 parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1100
1101 fwnode_handle_put(port);
1102
1103 return parent;
1104 }
1105 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1106
1107 /**
1108 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1109 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1110 *
1111 * Extracts firmware node of a remote device the @fwnode points to.
1112 */
1113 struct fwnode_handle *
fwnode_graph_get_remote_port_parent(const struct fwnode_handle * fwnode)1114 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1115 {
1116 struct fwnode_handle *endpoint, *parent;
1117
1118 endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1119 parent = fwnode_graph_get_port_parent(endpoint);
1120
1121 fwnode_handle_put(endpoint);
1122
1123 return parent;
1124 }
1125 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1126
1127 /**
1128 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1129 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1130 *
1131 * Extracts firmware node of a remote port the @fwnode points to.
1132 */
1133 struct fwnode_handle *
fwnode_graph_get_remote_port(const struct fwnode_handle * fwnode)1134 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1135 {
1136 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1137 }
1138 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1139
1140 /**
1141 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1142 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1143 *
1144 * Extracts firmware node of a remote endpoint the @fwnode points to.
1145 */
1146 struct fwnode_handle *
fwnode_graph_get_remote_endpoint(const struct fwnode_handle * fwnode)1147 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1148 {
1149 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1150 }
1151 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1152
1153 /**
1154 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1155 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1156 * @port_id: identifier of the parent port node
1157 * @endpoint_id: identifier of the endpoint node
1158 *
1159 * Return: Remote fwnode handle associated with remote endpoint node linked
1160 * to @node. Use fwnode_node_put() on it when done.
1161 */
1162 struct fwnode_handle *
fwnode_graph_get_remote_node(const struct fwnode_handle * fwnode,u32 port_id,u32 endpoint_id)1163 fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1164 u32 endpoint_id)
1165 {
1166 struct fwnode_handle *endpoint = NULL;
1167
1168 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1169 struct fwnode_endpoint fwnode_ep;
1170 struct fwnode_handle *remote;
1171 int ret;
1172
1173 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1174 if (ret < 0)
1175 continue;
1176
1177 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1178 continue;
1179
1180 remote = fwnode_graph_get_remote_port_parent(endpoint);
1181 if (!remote)
1182 return NULL;
1183
1184 return fwnode_device_is_available(remote) ? remote : NULL;
1185 }
1186
1187 return NULL;
1188 }
1189 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1190
1191 /**
1192 * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1193 * @fwnode: parent fwnode_handle containing the graph
1194 * @port: identifier of the port node
1195 * @endpoint: identifier of the endpoint node under the port node
1196 * @flags: fwnode lookup flags
1197 *
1198 * Return the fwnode handle of the local endpoint corresponding the port and
1199 * endpoint IDs or NULL if not found.
1200 *
1201 * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1202 * has not been found, look for the closest endpoint ID greater than the
1203 * specified one and return the endpoint that corresponds to it, if present.
1204 *
1205 * Do not return endpoints that belong to disabled devices, unless
1206 * FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1207 *
1208 * The returned endpoint needs to be released by calling fwnode_handle_put() on
1209 * it when it is not needed any more.
1210 */
1211 struct fwnode_handle *
fwnode_graph_get_endpoint_by_id(const struct fwnode_handle * fwnode,u32 port,u32 endpoint,unsigned long flags)1212 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1213 u32 port, u32 endpoint, unsigned long flags)
1214 {
1215 struct fwnode_handle *ep = NULL, *best_ep = NULL;
1216 unsigned int best_ep_id = 0;
1217 bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1218 bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1219
1220 while ((ep = fwnode_graph_get_next_endpoint(fwnode, ep))) {
1221 struct fwnode_endpoint fwnode_ep = { 0 };
1222 int ret;
1223
1224 if (enabled_only) {
1225 struct fwnode_handle *dev_node;
1226 bool available;
1227
1228 dev_node = fwnode_graph_get_remote_port_parent(ep);
1229 available = fwnode_device_is_available(dev_node);
1230 fwnode_handle_put(dev_node);
1231 if (!available)
1232 continue;
1233 }
1234
1235 ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1236 if (ret < 0)
1237 continue;
1238
1239 if (fwnode_ep.port != port)
1240 continue;
1241
1242 if (fwnode_ep.id == endpoint)
1243 return ep;
1244
1245 if (!endpoint_next)
1246 continue;
1247
1248 /*
1249 * If the endpoint that has just been found is not the first
1250 * matching one and the ID of the one found previously is closer
1251 * to the requested endpoint ID, skip it.
1252 */
1253 if (fwnode_ep.id < endpoint ||
1254 (best_ep && best_ep_id < fwnode_ep.id))
1255 continue;
1256
1257 fwnode_handle_put(best_ep);
1258 best_ep = fwnode_handle_get(ep);
1259 best_ep_id = fwnode_ep.id;
1260 }
1261
1262 return best_ep;
1263 }
1264 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1265
1266 /**
1267 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1268 * @fwnode: pointer to endpoint fwnode_handle
1269 * @endpoint: pointer to the fwnode endpoint data structure
1270 *
1271 * Parse @fwnode representing a graph endpoint node and store the
1272 * information in @endpoint. The caller must hold a reference to
1273 * @fwnode.
1274 */
fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)1275 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1276 struct fwnode_endpoint *endpoint)
1277 {
1278 memset(endpoint, 0, sizeof(*endpoint));
1279
1280 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1281 }
1282 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1283
device_get_match_data(struct device * dev)1284 const void *device_get_match_data(struct device *dev)
1285 {
1286 return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1287 }
1288 EXPORT_SYMBOL_GPL(device_get_match_data);
1289
1290 static void *
fwnode_graph_devcon_match(struct fwnode_handle * fwnode,const char * con_id,void * data,devcon_match_fn_t match)1291 fwnode_graph_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
1292 void *data, devcon_match_fn_t match)
1293 {
1294 struct fwnode_handle *node;
1295 struct fwnode_handle *ep;
1296 void *ret;
1297
1298 fwnode_graph_for_each_endpoint(fwnode, ep) {
1299 node = fwnode_graph_get_remote_port_parent(ep);
1300 if (!fwnode_device_is_available(node)) {
1301 fwnode_handle_put(node);
1302 continue;
1303 }
1304
1305 ret = match(node, con_id, data);
1306 fwnode_handle_put(node);
1307 if (ret) {
1308 fwnode_handle_put(ep);
1309 return ret;
1310 }
1311 }
1312 return NULL;
1313 }
1314
1315 static void *
fwnode_devcon_match(struct fwnode_handle * fwnode,const char * con_id,void * data,devcon_match_fn_t match)1316 fwnode_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
1317 void *data, devcon_match_fn_t match)
1318 {
1319 struct fwnode_handle *node;
1320 void *ret;
1321 int i;
1322
1323 for (i = 0; ; i++) {
1324 node = fwnode_find_reference(fwnode, con_id, i);
1325 if (IS_ERR(node))
1326 break;
1327
1328 ret = match(node, NULL, data);
1329 fwnode_handle_put(node);
1330 if (ret)
1331 return ret;
1332 }
1333
1334 return NULL;
1335 }
1336
1337 /**
1338 * fwnode_connection_find_match - Find connection from a device node
1339 * @fwnode: Device node with the connection
1340 * @con_id: Identifier for the connection
1341 * @data: Data for the match function
1342 * @match: Function to check and convert the connection description
1343 *
1344 * Find a connection with unique identifier @con_id between @fwnode and another
1345 * device node. @match will be used to convert the connection description to
1346 * data the caller is expecting to be returned.
1347 */
fwnode_connection_find_match(struct fwnode_handle * fwnode,const char * con_id,void * data,devcon_match_fn_t match)1348 void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
1349 const char *con_id, void *data,
1350 devcon_match_fn_t match)
1351 {
1352 void *ret;
1353
1354 if (!fwnode || !match)
1355 return NULL;
1356
1357 ret = fwnode_graph_devcon_match(fwnode, con_id, data, match);
1358 if (ret)
1359 return ret;
1360
1361 return fwnode_devcon_match(fwnode, con_id, data, match);
1362 }
1363 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
1364