[U-Boot] [PATCH 15/22] dm: core: Add a way to find a device by node reference

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


Add a function which looks up a device by its node (either in live tree
or flat tree).

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

 drivers/core/uclass.c        | 36 ++++++++++++++++++++++++++++++++++++
 include/dm/uclass-internal.h |  5 +++++
 include/dm/uclass.h          |  4 ++++
 3 files changed, 45 insertions(+)

diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index f7430f00789..c327aaf594d 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -13,6 +13,7 @@
 #include <dm/device.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
+#include <dm/of_dev.h>
 #include <dm/of_ref.h>
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
@@ -315,6 +316,30 @@ int uclass_find_device_by_of_node(enum uclass_id id,
 }
 #endif
 
+int uclass_find_device_by_of_ref(enum uclass_id id, of_node_ref node_ref,
+				 struct udevice **devp)
+{
+	struct uclass *uc;
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	if (!of_ref_valid(node_ref))
+		return -ENODEV;
+	ret = uclass_get(id, &uc);
+	if (ret)
+		return ret;
+
+	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
+		if (of_ref_equal(dev_of_node_ref(dev), node_ref)) {
+			*devp = dev;
+			return 0;
+		}
+	}
+
+	return -ENODEV;
+}
+
 #if CONFIG_IS_ENABLED(OF_CONTROL)
 static int uclass_find_device_by_phandle(enum uclass_id id,
 					 struct udevice *parent,
@@ -449,6 +474,17 @@ int uclass_get_device_by_of_node(enum uclass_id id,
 }
 #endif
 
+int uclass_get_device_by_of_ref(enum uclass_id id, of_node_ref node_ref,
+				struct udevice **devp)
+{
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	ret = uclass_find_device_by_of_ref(id, node_ref, &dev);
+	return uclass_get_device_tail(dev, ret, devp);
+}
+
 #if CONFIG_IS_ENABLED(OF_CONTROL)
 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
 				 const char *name, struct udevice **devp)
diff --git a/include/dm/uclass-internal.h b/include/dm/uclass-internal.h
index 103656fed0d..bb7819b8590 100644
--- a/include/dm/uclass-internal.h
+++ b/include/dm/uclass-internal.h
@@ -10,6 +10,8 @@
 #ifndef _DM_UCLASS_INTERNAL_H
 #define _DM_UCLASS_INTERNAL_H
 
+#include <dm/of_ref.h>
+
 /**
  * uclass_get_device_tail() - handle the end of a get_device call
  *
@@ -131,6 +133,9 @@ int uclass_find_device_by_of_node(enum uclass_id id,
 				  const struct device_node *np,
 				  struct udevice **devp);
 
+int uclass_find_device_by_of_ref(enum uclass_id id, of_node_ref node_ref,
+				 struct udevice **devp);
+
 /**
  * uclass_bind_device() - Associate device with a uclass
  *
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index 2d4958d37b6..ed438f699c2 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -10,6 +10,7 @@
 #ifndef _DM_UCLASS_H
 #define _DM_UCLASS_H
 
+#include <dm/of_ref.h>
 #include <dm/uclass-id.h>
 #include <linker_lists.h>
 #include <linux/list.h>
@@ -204,6 +205,9 @@ int uclass_get_device_by_of_node(enum uclass_id id,
 				 const struct device_node *np,
 				 struct udevice **devp);
 
+int uclass_get_device_by_of_ref(enum uclass_id id, of_node_ref node_ref,
+				struct udevice **devp);
+
 /**
  * uclass_get_device_by_phandle() - Get a uclass device by phandle
  *
-- 
2.11.0.483.g087da7b7c-goog



More information about the U-Boot mailing list