]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
dm: core: Add a function to find any device from device tree
authorSimon Glass <sjg@chromium.org>
Tue, 23 Jun 2015 21:38:37 +0000 (15:38 -0600)
committerLothar Waßmann <LW@KARO-electronics.de>
Wed, 9 Sep 2015 11:36:29 +0000 (13:36 +0200)
In some rare cases it is useful to be able to locate a device given a device
tree node offset. An example is when you have an alias that points to a node
and you want to find the associated device. The device may be SPI, MMC or
something else, but you don't need to know the uclass to find it.

Add a function to do a global search for a device, given its device tree
offset.

Signed-off-by: Simon Glass <sjg@chromium.org>
drivers/core/device.c
include/dm/device.h

index 85fd1fc7350331d48721539cbb42ca5ae33f8206..43aff5405977718163f6b08b20bf7f17799eb650 100644 (file)
@@ -470,6 +470,31 @@ int device_get_child_by_of_offset(struct udevice *parent, int seq,
        return device_get_device_tail(dev, ret, devp);
 }
 
+static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
+                                                       int of_offset)
+{
+       struct udevice *dev, *found;
+
+       if (parent->of_offset == of_offset)
+               return parent;
+
+       list_for_each_entry(dev, &parent->child_head, sibling_node) {
+               found = _device_find_global_by_of_offset(dev, of_offset);
+               if (found)
+                       return found;
+       }
+
+       return NULL;
+}
+
+int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
+{
+       struct udevice *dev;
+
+       dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
+       return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
+}
+
 int device_find_first_child(struct udevice *parent, struct udevice **devp)
 {
        if (list_empty(&parent->child_head)) {
index 18296bb68614b9aeae25c46b5ff3c8306e8a36d1..9a94ee19d9cdb58e95a3f0074c78ab6db41b5319 100644 (file)
@@ -389,6 +389,20 @@ int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
 int device_get_child_by_of_offset(struct udevice *parent, int seq,
                                  struct udevice **devp);
 
+/**
+ * device_get_global_by_of_offset() - Get a device based on FDT offset
+ *
+ * Locates a device by its device tree offset, searching globally throughout
+ * the all driver model devices.
+ *
+ * The device is probed to activate it ready for use.
+ *
+ * @of_offset: Device tree offset to find
+ * @devp: Returns pointer to device if found, otherwise this is set to NULL
+ * @return 0 if OK, -ve on error
+ */
+int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
+
 /**
  * device_find_first_child() - Find the first child of a device
  *