[U-Boot] [PATCH v3 19/29] dm: Allow a device to be found by its FDT offset

Simon Glass sjg at chromium.org
Wed Jul 23 14:55:14 CEST 2014


Each device that was bound from a device tree has an node that caused it to
be bound. Add functions that find and return a device based on a device tree
offset.

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

Changes in v3: None
Changes in v2: None

 doc/driver-model/README.txt |  3 ++-
 drivers/core/uclass.c       | 35 +++++++++++++++++++++++++++++++++++
 include/dm/uclass.h         | 16 ++++++++++++++++
 test/dm/test-fdt.c          | 30 ++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+), 1 deletion(-)

diff --git a/doc/driver-model/README.txt b/doc/driver-model/README.txt
index 84d21cf..346cf08 100644
--- a/doc/driver-model/README.txt
+++ b/doc/driver-model/README.txt
@@ -95,12 +95,13 @@ are provided in test/dm. To run them, try:
 You should see something like this:
 
     <...U-Boot banner...>
-    Running 15 driver model tests
+    Running 16 driver model tests
     Test: dm_test_autobind
     Test: dm_test_autoprobe
     Test: dm_test_children
     Test: dm_test_fdt
     Device 'd-test': seq 3 is in use by 'b-test'
+    Test: dm_test_fdt_offset
     Test: dm_test_fdt_pre_reloc
     Test: dm_test_fdt_uclass_seq
     Device 'd-test': seq 3 is in use by 'b-test'
diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index c28cf67..a27f3d5 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -187,6 +187,30 @@ int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
 	return -ENODEV;
 }
 
+static int uclass_find_device_by_of_offset(enum uclass_id id, int node,
+					   struct udevice **devp)
+{
+	struct uclass *uc;
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	if (node < 0)
+		return -ENODEV;
+	ret = uclass_get(id, &uc);
+	if (ret)
+		return ret;
+
+	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
+		if (dev->of_offset == node) {
+			*devp = dev;
+			return 0;
+		}
+	}
+
+	return -ENODEV;
+}
+
 /**
  * uclass_get_device_tail() - handle the end of a get_device call
  *
@@ -239,6 +263,17 @@ int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
 	return uclass_get_device_tail(dev, ret, devp);
 }
 
+int uclass_get_device_by_of_offset(enum uclass_id id, int node,
+				   struct udevice **devp)
+{
+	struct udevice *dev;
+	int ret;
+
+	*devp = NULL;
+	ret = uclass_find_device_by_of_offset(id, node, &dev);
+	return uclass_get_device_tail(dev, ret, devp);
+}
+
 int uclass_first_device(enum uclass_id id, struct udevice **devp)
 {
 	struct uclass *uc;
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index 48ae242..0b5ade6 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -122,6 +122,22 @@ int uclass_get_device(enum uclass_id id, int index, struct udevice **devp);
 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
 
 /**
+ * uclass_get_device_by_of_offset() - 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
+ * @node: Device tree offset to search for (if -ve 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_offset(enum uclass_id id, int node,
+				   struct udevice **devp);
+
+/**
  * uclass_first_device() - Get the first device in a uclass
  *
  * @id: Uclass ID to look up
diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c
index d8e94d8..7980a68 100644
--- a/test/dm/test-fdt.c
+++ b/test/dm/test-fdt.c
@@ -215,3 +215,33 @@ static int dm_test_fdt_uclass_seq(struct dm_test_state *dms)
 	return 0;
 }
 DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test that we can find a device by device tree offset */
+static int dm_test_fdt_offset(struct dm_test_state *dms)
+{
+	const void *blob = gd->fdt_blob;
+	struct udevice *dev;
+	int node;
+
+	node = fdt_path_offset(blob, "/e-test");
+	ut_assert(node > 0);
+	ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
+						   &dev));
+	ut_asserteq_str("e-test", dev->name);
+
+	/* This node should not be bound */
+	node = fdt_path_offset(blob, "/junk");
+	ut_assert(node > 0);
+	ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
+							    node, &dev));
+
+	/* This is not a top level node so should not be probed */
+	node = fdt_path_offset(blob, "/some-bus/c-test");
+	ut_assert(node > 0);
+	ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
+							    node, &dev));
+
+	return 0;
+}
+
+DM_TEST(dm_test_fdt_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.0.0.526.g5318336



More information about the U-Boot mailing list