[PATCH] dt: add of_get_property_value32 helper function

Rob Herring robherring2 at gmail.com
Fri Jun 24 02:38:33 EST 2011


From: Rob Herring <rob.herring at calxeda.com>

A common use of of_get_property in driver is to just get a 32-bit integer
value, but the interface is a bit complicated in that case. Add a helper
function that just fills in the value.

So something like this:

	int len;
	u32 blah;
	void *prop = of_get_property(node, "blah", &len);
	if (prop && len == sizeof(blah))
		blah = be32_to_cpup(prop);

can become:

	u32 blah;
	of_get_property_value32(node, "blah", &blah);

The caller can check the return value if the property is required to be
present.

Signed-off-by: Rob Herring <rob.herring at calxeda.com>
---
Grant,

I noticed most callers of of_get_property don't do be32_to_cpup on the
returned pointer. Most instances appear to be powerpc only drivers, but
should of_get_property return ptr be __be32?

Rob

 drivers/of/base.c  |   20 +++++++++++++++++++-
 include/linux/of.h |    9 +++++++++
 2 files changed, 28 insertions(+), 1 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 632ebae..58fe34e 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -186,7 +186,7 @@ EXPORT_SYMBOL(of_find_all_nodes);
 
 /*
  * Find a property with a given name for a given node
- * and return the value.
+ * and return a pointer to the value.
  */
 const void *of_get_property(const struct device_node *np, const char *name,
 			 int *lenp)
@@ -197,6 +197,24 @@ const void *of_get_property(const struct device_node *np, const char *name,
 }
 EXPORT_SYMBOL(of_get_property);
 
+/*
+ * Find a property with a given name for a given node
+ * and return the 32-bit integer value. Returns non-zero if property with
+ * valid length was found.
+ */
+int of_get_property_value32(const struct device_node *np, const char *name,
+			 u32 *valuep)
+{
+	int len;
+	u32 *p = of_get_property(np, name, &len);
+	if (!p || len != sizeof(*p))
+		return 0;
+
+	*value = be32_to_cpup(p);
+	return 1;
+}
+EXPORT_SYMBOL(of_get_property_value32);
+
 /** Checks if the given "compat" string matches one of the strings in
  * the device's "compatible" property
  */
diff --git a/include/linux/of.h b/include/linux/of.h
index bfc0ed1..fd10d37 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -201,6 +201,9 @@ extern int of_device_is_available(const struct device_node *device);
 extern const void *of_get_property(const struct device_node *node,
 				const char *name,
 				int *lenp);
+extern int of_get_property_value32(const struct device_node *node,
+				   const char *name,
+				   u32 *valuep);
 extern int of_n_addr_cells(struct device_node *np);
 extern int of_n_size_cells(struct device_node *np);
 extern const struct of_device_id *of_match_node(
@@ -234,5 +237,11 @@ static inline bool of_have_populated_dt(void)
 	return false;
 }
 
+static inline int of_get_property_value32(const struct device_node *node,
+					  const char *name, u32 *valuep)
+{
+	return 0;
+}
+
 #endif /* CONFIG_OF */
 #endif /* _LINUX_OF_H */
-- 
1.7.4.1



More information about the devicetree-discuss mailing list