[PATCH 3/5] of: Add of_find_node_by_name() with Linux semantics

Sascha Hauer s.hauer at pengutronix.de
Tue Mar 8 03:51:01 PST 2022


Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/of/base.c | 38 ++++++++++++++++++++++++++++++++++++++
 include/of.h      | 20 ++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index d5cbb12a29..83291c4785 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -23,6 +23,21 @@
 
 static struct device_node *root_node;
 
+bool of_node_name_eq(const struct device_node *np, const char *name)
+{
+	const char *node_name;
+	size_t len;
+
+	if (!np)
+		return false;
+
+	node_name = kbasename(np->full_name);
+		len = strchrnul(node_name, '@') - node_name;
+
+	return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
+}
+EXPORT_SYMBOL(of_node_name_eq);
+
 /*
  * Iterate over all nodes of a tree. As a devicetree does not
  * have a dedicated list head, the start node (usually the root
@@ -560,6 +575,29 @@ struct device_node *of_find_node_by_name_address(struct device_node *from,
 }
 EXPORT_SYMBOL(of_find_node_by_name_address);
 
+/**
+ *	of_find_node_by_name - Find a node by its "name" property
+ *	@from:	The node to start searching from or NULL, the node
+ *		you pass will not be searched, only the next one
+ *		will; typically, you pass what the previous call
+ *		returned.
+ *	@name:	The name string to match against
+ *
+ *	Returns a pointer to the node found or NULL.
+ */
+struct device_node *of_find_node_by_name(struct device_node *from,
+	const char *name)
+{
+	struct device_node *np;
+
+	of_tree_for_each_node_from(np, from)
+		if (np->name && of_node_name_eq(np, name))
+			return np;
+
+	return NULL;
+}
+EXPORT_SYMBOL(of_find_node_by_name);
+
 /**
  *	of_find_node_by_type - Find a node by its "device_type" property
  *	@from:  The node to start searching from, or NULL to start searching
diff --git a/include/of.h b/include/of.h
index 2c1a3f5510..9089409f9f 100644
--- a/include/of.h
+++ b/include/of.h
@@ -120,6 +120,7 @@ extern int of_bus_n_addr_cells(struct device_node *np);
 extern int of_n_addr_cells(struct device_node *np);
 extern int of_bus_n_size_cells(struct device_node *np);
 extern int of_n_size_cells(struct device_node *np);
+extern bool of_node_name_eq(const struct device_node *np, const char *name);
 
 extern struct property *of_find_property(const struct device_node *np,
 					const char *name, int *lenp);
@@ -136,6 +137,8 @@ extern struct property *of_new_property_const(struct device_node *node,
 					      const void *data, int len);
 extern void of_delete_property(struct property *pp);
 
+extern struct device_node *of_find_node_by_name(struct device_node *from,
+	const char *name);
 extern struct device_node *of_find_node_by_name_address(struct device_node *from,
 	const char *name);
 extern struct device_node *of_find_node_by_path_from(struct device_node *from,
@@ -313,6 +316,11 @@ struct device_node *of_find_node_by_path_or_alias(struct device_node *root,
 int of_autoenable_device_by_path(char *path);
 int of_autoenable_i2c_by_component(char *path);
 #else
+static inline bool of_node_name_eq(const struct device_node *np, const char *name)
+{
+	return false;
+}
+
 static inline int of_parse_partitions(struct cdev *cdev,
 					  struct device_node *node)
 {
@@ -653,6 +661,12 @@ static inline struct device_node *of_find_node_by_path(const char *path)
 	return NULL;
 }
 
+static inline struct device_node *of_find_node_by_name(struct device_node *from,
+	const char *name)
+{
+	return NULL;
+}
+
 static inline struct device_node *of_find_node_by_name_address(struct device_node *from,
 	const char *name)
 {
@@ -824,12 +838,18 @@ static inline int of_autoenable_i2c_by_component(char *path)
 
 #define for_each_property_of_node(dn, pp) \
 	list_for_each_entry(pp, &dn->properties, list)
+#define for_each_node_by_name(dn, name) \
+	for (dn = of_find_node_by_name(NULL, name); dn; \
+	     dn = of_find_node_by_name(dn, name))
 #define for_each_node_by_name_address(dn, name) \
 	for (dn = of_find_node_by_name_address(NULL, name); dn; \
 	     dn = of_find_node_by_name_address(dn, name))
 #define for_each_node_by_type(dn, type) \
 	for (dn = of_find_node_by_type(NULL, type); dn; \
 	     dn = of_find_node_by_type(dn, type))
+#define for_each_node_by_name_from(dn, root, name) \
+	for (dn = of_find_node_by_name(root, name); dn; \
+	     dn = of_find_node_by_name(dn, name))
 #define for_each_node_by_name_address_from(dn, root, name) \
 	for (dn = of_find_node_by_name_address(root, name); dn; \
 	     dn = of_find_node_by_name_address(dn, name))
-- 
2.30.2




More information about the barebox mailing list