Skip to content

Commit 1fcc67e

Browse files
wensWolfram Sang
authored and
Wolfram Sang
committed
of: base: Add for_each_child_of_node_with_prefix()
There are cases where drivers would go through child device nodes and operate on only the ones whose node name starts with a given prefix. Provide a helper for these users. This will mainly be used in a subsequent patch that implements a hardware component prober for I2C busses. Signed-off-by: Chen-Yu Tsai <[email protected]> Reviewed-by: Rob Herring (Arm) <[email protected]> Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent 81de291 commit 1fcc67e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

drivers/of/base.c

+35
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,41 @@ struct device_node *of_get_next_child(const struct device_node *node,
644644
}
645645
EXPORT_SYMBOL(of_get_next_child);
646646

647+
/**
648+
* of_get_next_child_with_prefix - Find the next child node with prefix
649+
* @node: parent node
650+
* @prev: previous child of the parent node, or NULL to get first
651+
*
652+
* This function is like of_get_next_child(), except that it automatically
653+
* skips any nodes whose name doesn't have the given prefix.
654+
*
655+
* Return: A node pointer with refcount incremented, use
656+
* of_node_put() on it when done.
657+
*/
658+
struct device_node *of_get_next_child_with_prefix(const struct device_node *node,
659+
struct device_node *prev,
660+
const char *prefix)
661+
{
662+
struct device_node *next;
663+
unsigned long flags;
664+
665+
if (!node)
666+
return NULL;
667+
668+
raw_spin_lock_irqsave(&devtree_lock, flags);
669+
next = prev ? prev->sibling : node->child;
670+
for (; next; next = next->sibling) {
671+
if (!of_node_name_prefix(next, prefix))
672+
continue;
673+
if (of_node_get(next))
674+
break;
675+
}
676+
of_node_put(prev);
677+
raw_spin_unlock_irqrestore(&devtree_lock, flags);
678+
return next;
679+
}
680+
EXPORT_SYMBOL(of_get_next_child_with_prefix);
681+
647682
static struct device_node *of_get_next_status_child(const struct device_node *node,
648683
struct device_node *prev,
649684
bool (*checker)(const struct device_node *))

include/linux/of.h

+9
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ extern struct device_node *of_get_parent(const struct device_node *node);
289289
extern struct device_node *of_get_next_parent(struct device_node *node);
290290
extern struct device_node *of_get_next_child(const struct device_node *node,
291291
struct device_node *prev);
292+
extern struct device_node *of_get_next_child_with_prefix(const struct device_node *node,
293+
struct device_node *prev,
294+
const char *prefix);
292295
extern struct device_node *of_get_next_available_child(
293296
const struct device_node *node, struct device_node *prev);
294297
extern struct device_node *of_get_next_reserved_child(
@@ -1468,6 +1471,12 @@ static inline int of_property_read_s32(const struct device_node *np,
14681471
child != NULL; \
14691472
child = of_get_next_child(parent, child))
14701473

1474+
#define for_each_child_of_node_with_prefix(parent, child, prefix) \
1475+
for (struct device_node *child __free(device_node) = \
1476+
of_get_next_child_with_prefix(parent, NULL, prefix); \
1477+
child != NULL; \
1478+
child = of_get_next_child_with_prefix(parent, child, prefix))
1479+
14711480
#define for_each_available_child_of_node(parent, child) \
14721481
for (child = of_get_next_available_child(parent, NULL); child != NULL; \
14731482
child = of_get_next_available_child(parent, child))

0 commit comments

Comments
 (0)