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