[PATCH master 1/2] of: split part of of_get_stdoutpath into of_find_node_by_chosen

Ahmad Fatoum ahmad at a3f.at
Mon Feb 20 23:07:34 PST 2023


Follow-up commit will also lookup the value of a chosen property by full
path or alias, so factor this out into a helper function.

Signed-off-by: Ahmad Fatoum <ahmad at a3f.at>
---
 drivers/of/base.c | 52 +++++++++++++++++++++++++++++++++--------------
 include/of.h      |  8 ++++++++
 2 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 937847f44ab7..da621ad5d1a9 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2667,30 +2667,52 @@ void of_delete_node(struct device_node *node)
 	free(node);
 }
 
-struct device_node *of_get_stdoutpath(unsigned int *baudrate)
+/*
+ * of_find_node_by_chosen - Find a node given a chosen property pointing at it
+ * @propname:   the name of the property containing a path or alias
+ *              The function will lookup the first string in the property
+ *              value up to the first : character or till \0.
+ * @options     The Remainder (without : or \0 at the end) will be written
+ *              to *options if not NULL.
+ */
+struct device_node *of_find_node_by_chosen(const char *propname,
+					   const char **options)
 {
+	const char *value, *p;
+	char *buf = NULL;
 	struct device_node *dn;
-	const char *name;
-	const char *p;
-	char *q;
 
-	name = of_get_property(of_chosen, "stdout-path", NULL);
-	if (!name)
-		name = of_get_property(of_chosen, "linux,stdout-path", NULL);
+	value = of_get_property(of_chosen, propname, NULL);
+	if (!value)
+		return NULL;
 
-	if (!name)
-		return 0;
+	p = strchrnul(value, ':');
+	if (*p)
+		buf = xstrndup(value, p - value);
 
-	p = strchrnul(name, ':');
+	dn = of_find_node_by_path_or_alias(NULL, buf);
 
-	q = xstrndup(name, p - name);
+	free(buf);
 
-	dn = of_find_node_by_path_or_alias(NULL, q);
+	if (options && *p)
+		*options = p + 1;
 
-	free(q);
+	return dn;
+}
+
+struct device_node *of_get_stdoutpath(unsigned int *baudrate)
+{
+	const char *opts = NULL;
+	struct device_node *dn;
+
+	dn = of_find_node_by_chosen("stdout-path", &opts);
+	if (!dn)
+		dn = of_find_node_by_chosen("linux,stdout-path", &opts);
+	if (!dn)
+		return NULL;
 
-	if (baudrate && *p) {
-		unsigned rate = simple_strtoul(p + 1, NULL, 10);
+	if (baudrate && opts) {
+		unsigned rate = simple_strtoul(opts, NULL, 10);
 		if (rate)
 			*baudrate = rate;
 	}
diff --git a/include/of.h b/include/of.h
index 7ee1304b932b..6d0aca0102c6 100644
--- a/include/of.h
+++ b/include/of.h
@@ -314,6 +314,8 @@ struct cdev *of_parse_partition(struct cdev *cdev, struct device_node *node);
 int of_parse_partitions(struct cdev *cdev, struct device_node *node);
 int of_fixup_partitions(struct device_node *np, struct cdev *cdev);
 int of_partitions_register_fixup(struct cdev *cdev);
+struct device_node *of_find_node_by_chosen(const char *propname,
+					   const char **options);
 struct device_node *of_get_stdoutpath(unsigned int *);
 int of_device_is_stdout_path(struct device *dev, unsigned int *baudrate);
 const char *of_get_model(void);
@@ -369,6 +371,12 @@ static inline int of_partitions_register_fixup(struct cdev *cdev)
 	return -ENOSYS;
 }
 
+static inline struct device_node *of_find_node_by_chosen(const char *propname,
+							 const char **options)
+{
+	return NULL;
+}
+
 static inline struct device_node *of_get_stdoutpath(unsigned int *rate)
 {
 	return NULL;
-- 
2.38.3




More information about the barebox mailing list