mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 4/7] of: introduce some new helpers
Date: Wed, 14 Aug 2013 10:11:22 +0200	[thread overview]
Message-ID: <1376467885-29489-5-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1376467885-29489-1-git-send-email-s.hauer@pengutronix.de>

of_get_tree_max_phandle - get the maximum phandle of a tree. Needed for
                          creating new phandles without conflicts.
of_node_create_phandle - create a phandle for a node which doesn't have one.
of_find_node_by_alias - find a node by alias name
of_find_node_by_path_or_alias - find a node by full path or alias name
of_find_root_node - find the root node for a given device node

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/of/base.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/of.h      |  14 ++++++++
 2 files changed, 115 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 3ebd672..8e9d384 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -241,6 +241,32 @@ const char *of_alias_get(struct device_node *np)
 EXPORT_SYMBOL_GPL(of_alias_get);
 
 /*
+ * of_find_node_by_alias - Find a node given an alias name
+ * @root:    the root node of the tree. If NULL, use internal tree
+ * @alias:   the alias name to find
+ */
+struct device_node *of_find_node_by_alias(struct device_node *root, const char *alias)
+{
+	struct device_node *aliasnp;
+	int ret;
+	const char *path;
+
+	if (!root)
+		root = root_node;
+
+	aliasnp = of_find_node_by_path_from(root, "/aliases");
+	if (!aliasnp)
+		return NULL;
+
+	ret = of_property_read_string(aliasnp, alias, &path);
+	if (ret)
+		return NULL;
+
+	return of_find_node_by_path_from(root, path);
+}
+EXPORT_SYMBOL_GPL(of_find_node_by_alias);
+
+/*
  * of_find_node_by_phandle - Find a node given a phandle
  * @handle:    phandle of the node to find
  */
@@ -256,6 +282,62 @@ struct device_node *of_find_node_by_phandle(phandle phandle)
 EXPORT_SYMBOL(of_find_node_by_phandle);
 
 /*
+ * of_get_tree_max_phandle - Find the maximum phandle of a tree
+ * @root:    root node of the tree to search in. If NULL use the
+ *           internal tree.
+ */
+phandle of_get_tree_max_phandle(struct device_node *root)
+{
+	struct device_node *n;
+	phandle max;
+
+	if (!root)
+		root = root_node;
+
+	if (!root)
+		return 0;
+
+	max = root->phandle;
+
+	of_tree_for_each_node_from(n, root) {
+		if (n->phandle > max)
+			max = n->phandle;
+	}
+
+	return max;
+}
+EXPORT_SYMBOL(of_get_tree_max_phandle);
+
+/*
+ * of_node_create_phandle - create a phandle for a node
+ * @node:    The node to create a phandle in
+ *
+ * returns the new phandle or the existing phandle if the node
+ * already has a phandle.
+ */
+phandle of_node_create_phandle(struct device_node *node)
+{
+	phandle p;
+	struct device_node *root;
+
+	if (node->phandle)
+		return node->phandle;
+
+	root = of_find_root_node(node);
+
+	p = of_get_tree_max_phandle(root) + 1;
+
+	node->phandle = p;
+
+	p = cpu_to_be32(p);
+
+	of_set_property(node, "phandle", &p, sizeof(p), 1);
+
+	return node->phandle;
+}
+EXPORT_SYMBOL(of_node_create_phandle);
+
+/*
  * Find a property with a given name for a given node
  * and return the value.
  */
@@ -1258,6 +1340,25 @@ struct device_node *of_find_node_by_path(const char *path)
 EXPORT_SYMBOL(of_find_node_by_path);
 
 /**
+ *	of_find_node_by_path_or_alias - Find a node matching a full OF path
+ *	or an alias
+ *	@root:	The root node. If NULL the internal tree is used
+ *	@str:	the full path or alias
+ *
+ *	Returns a pointer to the node found or NULL.
+ */
+struct device_node *of_find_node_by_path_or_alias(struct device_node *root,
+		const char *str)
+{
+	if (*str ==  '/')
+		return of_find_node_by_path_from(root, str);
+	else
+		return of_find_node_by_alias(root, str);
+
+}
+EXPORT_SYMBOL(of_find_node_by_path_or_alias);
+
+/**
  * of_modalias_node - Lookup appropriate modalias for a device node
  * @node:	pointer to a device tree node
  * @modalias:	Pointer to buffer that modalias value will be copied into
diff --git a/include/of.h b/include/of.h
index b99f0b2..2c77ee6 100644
--- a/include/of.h
+++ b/include/of.h
@@ -689,4 +689,18 @@ int of_device_enable_path(const char *path);
 int of_device_disable(struct device_node *node);
 int of_device_disable_path(const char *path);
 
+phandle of_get_tree_max_phandle(struct device_node *root);
+phandle of_node_create_phandle(struct device_node *node);
+struct device_node *of_find_node_by_alias(struct device_node *root,
+		const char *alias);
+struct device_node *of_find_node_by_path_or_alias(struct device_node *root,
+		const char *str);
+
+static inline struct device_node *of_find_root_node(struct device_node *node)
+{
+	while (node->parent)
+		node = node->parent;
+
+	return node;
+}
 #endif /* __OF_H */
-- 
1.8.4.rc2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2013-08-14  8:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
2013-08-14  8:11 ` [PATCH 1/7] of: fix merge mode in of_unflatten_dtb Sascha Hauer
2013-08-14  8:11 ` [PATCH 2/7] of: parse phandles during unflatten Sascha Hauer
2013-08-14  8:11 ` [PATCH 3/7] of: default to internal tree in of_find_node_by_path_from Sascha Hauer
2013-08-14  8:11 ` Sascha Hauer [this message]
2013-08-14  8:11 ` [PATCH 5/7] of_property command: allow to specify a node by alias Sascha Hauer
2013-08-14  8:11 ` [PATCH 6/7] of_property command: allow to set phandles Sascha Hauer
2013-08-14  8:11 ` [PATCH 7/7] oftree command: Allow to specify node by alias Sascha Hauer
2013-08-14  9:06 ` [PATCH] of_property and phandles Jan Lübbe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1376467885-29489-5-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox