[U-Boot] [PATCH 10/22] dm: core: Add device-based functions to access DT properties

Simon Glass sjg at chromium.org
Wed Jan 18 06:51:46 CET 2017


It is common to read a device-tree property from the node associated with
a device. Add convenience functions to do this so that drivers do not need
to deal with the difference between live tree and flat tree access
methods.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 drivers/core/Makefile |  2 +-
 drivers/core/of_dev.c | 55 +++++++++++++++++++++++++++++++++++++++++
 include/dm/of_dev.h   | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/core/of_dev.c
 create mode 100644 include/dm/of_dev.h

diff --git a/drivers/core/Makefile b/drivers/core/Makefile
index 6d7040c2603..be1e9da55c0 100644
--- a/drivers/core/Makefile
+++ b/drivers/core/Makefile
@@ -11,4 +11,4 @@ obj-$(CONFIG_$(SPL_)SIMPLE_BUS)	+= simple-bus.o
 obj-$(CONFIG_DM)	+= dump.o
 obj-$(CONFIG_$(SPL_)REGMAP)	+= regmap.o
 obj-$(CONFIG_$(SPL_)SYSCON)	+= syscon-uclass.o
-obj-$(CONFIG_OF_CONTROL) += of_extra.o of_ref.o
+obj-$(CONFIG_OF_CONTROL) += of_dev.o of_extra.o of_ref.o
diff --git a/drivers/core/of_dev.c b/drivers/core/of_dev.c
new file mode 100644
index 00000000000..0474ef1408b
--- /dev/null
+++ b/drivers/core/of_dev.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2017 Google, Inc
+ * Written by Simon Glass <sjg at chromium.org>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/of_dev.h>
+#include <dm/of_ref.h>
+
+/**
+ * dev_of_node_ref() - get the DT node reference associated with a udevice
+ *
+ * TODO(sjg at chromium.org): Once dev->of_offset is dropped, we can simplify
+ * this function. For now it has to look at both of_node and of_offset.
+ *
+ * @dev:	device to check
+ * @return reference of the the device's DT node
+ */
+of_node_ref dev_of_node_ref(struct udevice *dev)
+{
+#ifdef CONFIG_OF_LIVE
+	return dev->node_ref;
+#else
+	return of_offset_to_ref(dev->of_offset);
+#endif
+}
+
+int dev_read_u32_defaut(struct udevice *dev, const char *propname, int def)
+{
+	u32 val;
+
+	if (of_ref_read_u32(dev_of_node_ref(dev), propname, &val))
+		return def;
+
+	return val;
+}
+
+const char *dev_read_string(struct udevice *dev, const char *propname)
+{
+	return of_ref_read_string(dev_of_node_ref(dev), propname);
+}
+
+bool dev_read_bool(struct udevice *dev, const char *propname)
+{
+	return of_ref_read_bool(dev_of_node_ref(dev), propname);
+}
+
+of_node_ref dev_find_subnode(struct udevice *dev,
+				   const char *subnode_name)
+{
+	return of_ref_find_subnode(dev_of_node_ref(dev), subnode_name);
+}
diff --git a/include/dm/of_dev.h b/include/dm/of_dev.h
new file mode 100644
index 00000000000..e7962088106
--- /dev/null
+++ b/include/dm/of_dev.h
@@ -0,0 +1,68 @@
+/*
+ * Function to read values from the device tree node attached to a udevice.
+ *
+ * Copyright (c) 2017 Google, Inc
+ * Written by Simon Glass <sjg at chromium.org>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef _DM_OF_DEV_H
+#define _DM_OF_DEV_H
+
+#include <dm/of_ref.h>
+
+#ifdef CONFIG_OF_LIVE
+static inline const struct device_node *dev_of_node(struct udevice *dev)
+{
+	return of_ref_to_node(dev->node_ref);
+}
+#else
+static inline const struct device_node *dev_of_node(struct udevice *dev)
+{
+	return NULL;
+}
+#endif
+
+of_node_ref dev_of_node_ref(struct udevice *dev);
+
+/**
+ * dev_read_u32_defaut() - read a 32-bit integer from a device's DT property
+ *
+ * @dev:	device to read DT property from
+ * @propname:	name of the property to read from
+ * @def:	default value to return if the property has no value
+ * @return property value, or @def if not found
+ */
+int dev_read_u32_defaut(struct udevice *dev, const char *propname, int def);
+
+/**
+ * dev_read_string() - Read a string from a device's DT property
+ *
+ * @dev:	device to read DT property from
+ * @propname:	name of the property to read
+ * @return string from property value, or NULL if there is no such property
+ */
+const char *dev_read_string(struct udevice *dev, const char *propname);
+
+/**
+ * dev_read_bool() - read a boolean value from a device's DT property
+ *
+ * @dev:	device to read DT property from
+ * @propname:	name of property to read
+ * @return true if property is present (meaning true), false if not present
+ */
+bool dev_read_bool(struct udevice *dev, const char *propname);
+
+/**
+ * dev_find_subnode() - find a named subnode of a device
+ *
+ * @dev:	device whose DT node contains the subnode
+ * @subnode_name: name of subnode to find
+ * @return reference to subnode (which can be invalid if there is no such
+ * subnode)
+ */
+of_node_ref dev_find_subnode(struct udevice *dev,
+				   const char *subbnode_name);
+
+#endif
-- 
2.11.0.483.g087da7b7c-goog



More information about the U-Boot mailing list