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