dm: Allow a device to be found by its FDT offset
authorSimon Glass <sjg@chromium.org>
Wed, 23 Jul 2014 12:55:14 +0000 (06:55 -0600)
committerSimon Glass <sjg@chromium.org>
Wed, 23 Jul 2014 13:07:25 +0000 (14:07 +0100)
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@chromium.org>
doc/driver-model/README.txt
drivers/core/uclass.c
include/dm/uclass.h
test/dm/test-fdt.c

index 84d21cf..346cf08 100644 (file)
@@ -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'
index c28cf67..a27f3d5 100644 (file)
@@ -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;
index 48ae242..0b5ade6 100644 (file)
@@ -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
index d8e94d8..7980a68 100644 (file)
@@ -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);