[PATCH 6.1.y-cip 03/17] of: base: Add of_get_available_child_by_name()

Tommaso Merciai
2025-06-23T14:47:22Z

From: Biju Das <biju.das.jz@bp.renesas.com>

commit 8d3bbe4355aded32961b9009b31de6d41b7352e9 upstream.

There are lot of drivers using of_get_child_by_name() followed by
of_device_is_available() to find the available child node by name for a
given parent. Provide a helper for these users to simplify the code.

Suggested-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Biju Das <biju.das.jz@bp.renesas.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>
---
drivers/of/base.c | 27 +++++++++++++++++++++++++++
include/linux/of.h | 9 +++++++++
2 files changed, 36 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 90c934bb91da..788706579419 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -904,6 +904,33 @@ struct device_node *of_get_child_by_name(const struct device_node *node,
}
EXPORT_SYMBOL(of_get_child_by_name);

+/**
+ * of_get_available_child_by_name - Find the available child node by name for a given parent
+ * @node: parent node
+ * @name: child name to look for.
+ *
+ * This function looks for child node for given matching name and checks the
+ * device's availability for use.
+ *
+ * Return: A node pointer if found, with refcount incremented, use
+ * of_node_put() on it when done.
+ * Returns NULL if node is not found.
+ */
+struct device_node *of_get_available_child_by_name(const struct device_node *node,
+ const char *name)
+{
+ struct device_node *child;
+
+ child = of_get_child_by_name(node, name);
+ if (child && !of_device_is_available(child)) {
+ of_node_put(child);
+ return NULL;
+ }
+
+ return child;
+}
+EXPORT_SYMBOL(of_get_available_child_by_name);
+
struct device_node *__of_find_node_by_path(struct device_node *parent,
const char *path)
{
diff --git a/include/linux/of.h b/include/linux/of.h
index 2960e609ca05..44e432dbf67f 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -296,6 +296,8 @@ extern struct device_node *of_get_compatible_child(const struct device_node *par
const char *compatible);
extern struct device_node *of_get_child_by_name(const struct device_node *node,
const char *name);
+extern struct device_node *of_get_available_child_by_name(const struct device_node *node,
+ const char *name);

/* cache lookup */
extern struct device_node *of_find_next_cache_node(const struct device_node *);
@@ -558,6 +560,13 @@ static inline struct device_node *of_get_child_by_name(
return NULL;
}

+static inline struct device_node *of_get_available_child_by_name(
+ const struct device_node *node,
+ const char *name)
+{
+ return NULL;
+}
+
static inline int of_device_is_compatible(const struct device_node *device,
const char *name)
{
--
2.43.0