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