Skip to content

Commit 87e5e95

Browse files
Sakari Ailusrafaeljw
authored andcommitted
device property: Add functions for accessing node's parents
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. Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent a57b7fb commit 87e5e95

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎drivers/base/property.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,52 @@ struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
590590
}
591591
EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
592592

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+
593639
/**
594640
* fwnode_get_next_child_node - Return the next child node handle for a node
595641
* @fwnode: Firmware node to find the next child node for.

‎include/linux/property.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
8383
struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
8484
struct fwnode_handle *fwnode_get_next_parent(
8585
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);
8689
struct fwnode_handle *fwnode_get_next_child_node(
8790
const struct fwnode_handle *fwnode, struct fwnode_handle *child);
8891
struct fwnode_handle *fwnode_get_next_available_child_node(

0 commit comments

Comments
 (0)