[U-Boot] [PATCH 14/22] dm: core: Add a way to find a device by its live-tree node

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


Add function which looks up a device by its device tree node in the live
tree.

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

 drivers/core/uclass.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 include/dm/uclass-internal.h | 17 +++++++++++++++++
 include/dm/uclass.h          | 19 +++++++++++++++++++
 3 files changed, 78 insertions(+)

diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index 7de370644d7..f7430f00789 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_ref.h>
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
@@ -287,6 +288,33 @@ int uclass_find_device_by_of_offset(enum uclass_id id, int node,
 	return -ENODEV;
 }
 
+#ifdef CONFIG_OF_LIVE
+int uclass_find_device_by_of_node(enum uclass_id id,
+				  const struct device_node *np,
+				  struct udevice **devp)
+{
+	struct uclass *uc;
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	if (!np)
+		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->node_ref, of_node_to_ref(np))) {
+			*devp = dev;
+			return 0;
+		}
+	}
+
+	return -ENODEV;
+}
+#endif
+
 #if CONFIG_IS_ENABLED(OF_CONTROL)
 static int uclass_find_device_by_phandle(enum uclass_id id,
 					 struct udevice *parent,
@@ -407,6 +435,20 @@ int uclass_get_device_by_of_offset(enum uclass_id id, int node,
 	return uclass_get_device_tail(dev, ret, devp);
 }
 
+#ifdef CONFIG_OF_LIVE
+int uclass_get_device_by_of_node(enum uclass_id id,
+				 const struct device_node *np,
+				 struct udevice **devp)
+{
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	ret = uclass_find_device_by_of_node(id, np, &dev);
+	return uclass_get_device_tail(dev, ret, devp);
+}
+#endif
+
 #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 ad284b8445a..103656fed0d 100644
--- a/include/dm/uclass-internal.h
+++ b/include/dm/uclass-internal.h
@@ -115,6 +115,23 @@ int uclass_find_device_by_of_offset(enum uclass_id id, int node,
 				    struct udevice **devp);
 
 /**
+ * uclass_find_device_by_of_node() - Find a uclass device by device tree node
+ *
+ * This searches the devices in the uclass for one attached to the given
+ * device tree node.
+ *
+ * The device is NOT probed, it is merely returned.
+ *
+ * @id: ID to look up
+ * @node: Device tree offset to search for (if NULL then -ENODEV is returned)
+ * @devp: Returns pointer to device (there is only one for each node)
+ * @return 0 if OK, -ve on error
+ */
+int uclass_find_device_by_of_node(enum uclass_id id,
+				  const struct device_node *np,
+				  struct udevice **devp);
+
+/**
  * uclass_bind_device() - Associate device with a uclass
  *
  * Connect the device into uclass's list of devices.
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index b583aa869b3..2d4958d37b6 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -14,6 +14,8 @@
 #include <linker_lists.h>
 #include <linux/list.h>
 
+struct device_node;
+
 /**
  * struct uclass - a U-Boot drive class, collecting together similar drivers
  *
@@ -186,6 +188,23 @@ int uclass_get_device_by_of_offset(enum uclass_id id, int node,
 				   struct udevice **devp);
 
 /**
+ * uclass_get_device_by_of_node() - Get a uclass device by device tree node
+ *
+ * This searches the devices in the uclass for one attached to the given
+ * device tree node.
+ *
+ * The device is probed to activate it ready for use.
+ *
+ * @id: ID to look up
+ * @np: Device tree node to search for (if NULL then -ENODEV is returned)
+ * @devp: Returns pointer to device (there is only one for each node)
+ * @return 0 if OK, -ve on error
+ */
+int uclass_get_device_by_of_node(enum uclass_id id,
+				 const struct device_node *np,
+				 struct udevice **devp);
+
+/**
  * uclass_get_device_by_phandle() - Get a uclass device by phandle
  *
  * This searches the devices in the uclass for one with the given phandle.
-- 
2.11.0.483.g087da7b7c-goog



More information about the U-Boot mailing list