Add two convenience functions for accessing node's parents: fwnode_count_parents() returns the number of parent nodes a given node has. fwnode_get_nth_parent() returns node's parent at a given distance from the node itself.
Unified · Split
| @@ -590,6 +590,52 @@ struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode) · 589 unmodified lines | ||
| 590 | 590 | } |
| 591 | 591 | EXPORT_SYMBOL_GPL(fwnode_get_next_parent); |
| 592 | 592 | |
| 593 | +/** | |
| 594 | + * fwnode_count_parents - Return the number of parents a node has | |
| 595 | + * @fwnode: The node the parents of which are to be counted | |
| 596 | + * | |
| 597 | + * Returns the number of parents a node has. | |
| 598 | + */ | |
| 599 | +unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode) | |
| 600 | +{ | |
| 601 | + struct fwnode_handle *__fwnode; | |
| 602 | + unsigned int count; | |
| 603 | + | |
| 604 | + __fwnode = fwnode_get_parent(fwnode); | |
| 605 | + | |
| 606 | + for (count = 0; __fwnode; count++) | |
| 607 | + __fwnode = fwnode_get_next_parent(__fwnode); | |
| 608 | + | |
| 609 | + return count; | |
| 610 | +} | |
| 611 | +EXPORT_SYMBOL_GPL(fwnode_count_parents); | |
| 612 | + | |
| 613 | +/** | |
| 614 | + * fwnode_get_nth_parent - Return an nth parent of a node | |
| 615 | + * @fwnode: The node the parent of which is requested | |
| 616 | + * @depth: Distance of the parent from the node | |
| 617 | + * | |
| 618 | + * Returns the nth parent of a node. If there is no parent at the requested | |
| 619 | + * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to | |
| 620 | + * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on. | |
| 621 | + * | |
| 622 | + * The caller is responsible for calling fwnode_handle_put() for the returned | |
| 623 | + * node. | |
| 624 | + */ | |
| 625 | +struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode, | |
| 626 | + unsigned int depth) | |
| 627 | +{ | |
| 628 | + unsigned int i; | |
| 629 | + | |
| 630 | + fwnode_handle_get(fwnode); | |
| 631 | + | |
| 632 | + for (i = 0; i < depth && fwnode; i++) | |
| 633 | + fwnode = fwnode_get_next_parent(fwnode); | |
| 634 | + | |
| 635 | + return fwnode; | |
| 636 | +} | |
| 637 | +EXPORT_SYMBOL_GPL(fwnode_get_nth_parent); | |
| 638 | + | |
| 593 | 639 | /** |
| 594 | 640 | * fwnode_get_next_child_node - Return the next child node handle for a node |
| 595 | 641 | * @fwnode: Firmware node to find the next child node for. |
| @@ -83,6 +83,9 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode, · 82 unmodified lines | ||
| 83 | 83 | struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode); |
| 84 | 84 | struct fwnode_handle *fwnode_get_next_parent( |
| 85 | 85 | struct fwnode_handle *fwnode); |
| 86 | +unsigned int fwnode_count_parents(const struct fwnode_handle *fwn); | |
| 87 | +struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn, | |
| 88 | + unsigned int depth); | |
| 86 | 89 | struct fwnode_handle *fwnode_get_next_child_node( |
| 87 | 90 | const struct fwnode_handle *fwnode, struct fwnode_handle *child); |
| 88 | 91 | struct fwnode_handle *fwnode_get_next_available_child_node( |