Skip to content

Commit 87ffea0

Browse files
andy-shevrafaeljw
authored andcommitted
device property: Introduce fwnode_for_each_parent_node()
In a few cases the functionality of fwnode_for_each_parent_node() is already in use. Introduce a common helper macro for it. It may be used by others as well in the future. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 002752a commit 87ffea0

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

drivers/base/property.c

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -596,17 +596,17 @@ EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
596596
*/
597597
struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
598598
{
599+
struct fwnode_handle *parent;
599600
struct device *dev;
600601

601-
fwnode_handle_get(fwnode);
602-
do {
603-
fwnode = fwnode_get_next_parent(fwnode);
604-
if (!fwnode)
605-
return NULL;
602+
fwnode_for_each_parent_node(fwnode, parent) {
606603
dev = get_dev_from_fwnode(fwnode);
607-
} while (!dev);
608-
fwnode_handle_put(fwnode);
609-
return dev;
604+
if (dev) {
605+
fwnode_handle_put(parent);
606+
return dev;
607+
}
608+
}
609+
return NULL;
610610
}
611611

612612
/**
@@ -617,13 +617,11 @@ struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
617617
*/
618618
unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
619619
{
620-
struct fwnode_handle *__fwnode;
621-
unsigned int count;
622-
623-
__fwnode = fwnode_get_parent(fwnode);
620+
struct fwnode_handle *parent;
621+
unsigned int count = 0;
624622

625-
for (count = 0; __fwnode; count++)
626-
__fwnode = fwnode_get_next_parent(__fwnode);
623+
fwnode_for_each_parent_node(fwnode, parent)
624+
count++;
627625

628626
return count;
629627
}
@@ -644,15 +642,16 @@ EXPORT_SYMBOL_GPL(fwnode_count_parents);
644642
struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
645643
unsigned int depth)
646644
{
647-
fwnode_handle_get(fwnode);
645+
struct fwnode_handle *parent;
648646

649-
do {
650-
if (depth-- == 0)
651-
break;
652-
fwnode = fwnode_get_next_parent(fwnode);
653-
} while (fwnode);
647+
if (depth == 0)
648+
return fwnode_handle_get(fwnode);
654649

655-
return fwnode;
650+
fwnode_for_each_parent_node(fwnode, parent) {
651+
if (--depth == 0)
652+
return parent;
653+
}
654+
return NULL;
656655
}
657656
EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
658657

@@ -669,17 +668,20 @@ EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
669668
bool fwnode_is_ancestor_of(struct fwnode_handle *test_ancestor,
670669
struct fwnode_handle *test_child)
671670
{
671+
struct fwnode_handle *parent;
672+
672673
if (IS_ERR_OR_NULL(test_ancestor))
673674
return false;
674675

675-
fwnode_handle_get(test_child);
676-
do {
677-
if (test_child == test_ancestor) {
678-
fwnode_handle_put(test_child);
676+
if (test_child == test_ancestor)
677+
return true;
678+
679+
fwnode_for_each_parent_node(test_child, parent) {
680+
if (parent == test_ancestor) {
681+
fwnode_handle_put(parent);
679682
return true;
680683
}
681-
test_child = fwnode_get_next_parent(test_child);
682-
} while (test_child);
684+
}
683685
return false;
684686
}
685687

include/linux/property.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,14 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
8383

8484
const char *fwnode_get_name(const struct fwnode_handle *fwnode);
8585
const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
86+
8687
struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
87-
struct fwnode_handle *fwnode_get_next_parent(
88-
struct fwnode_handle *fwnode);
88+
struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
89+
90+
#define fwnode_for_each_parent_node(fwnode, parent) \
91+
for (parent = fwnode_get_parent(fwnode); parent; \
92+
parent = fwnode_get_next_parent(parent))
93+
8994
struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode);
9095
unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
9196
struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,

0 commit comments

Comments
 (0)