dm: core: Support finding a device by phandle
authorSimon Glass <sjg@chromium.org>
Fri, 3 Jul 2015 00:15:38 +0000 (18:15 -0600)
committerSimon Glass <sjg@chromium.org>
Thu, 6 Aug 2015 02:57:51 +0000 (20:57 -0600)
It is common for one node to reference another via a phandle. Add support
for obtaining an attached device by this method. As an example, a node may
have a 'power-supply' property which references a regulator, allowing the
driver to turn on its power.

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

index ffe69956d591c058e3a4410f5a1bbee71a63eec0..5c4a66dd8cf6e9616e917e8ba1d1bdc64cdb8e17 100644 (file)
@@ -273,6 +273,37 @@ static int uclass_find_device_by_of_offset(enum uclass_id id, int node,
        return -ENODEV;
 }
 
+static int uclass_find_device_by_phandle(enum uclass_id id,
+                                        struct udevice *parent,
+                                        const char *name,
+                                        struct udevice **devp)
+{
+       struct udevice *dev;
+       struct uclass *uc;
+       int find_phandle;
+       int ret;
+
+       *devp = NULL;
+       find_phandle = fdtdec_get_int(gd->fdt_blob, parent->of_offset, name,
+                                     -1);
+       if (find_phandle <= 0)
+               return -ENOENT;
+       ret = uclass_get(id, &uc);
+       if (ret)
+               return ret;
+
+       list_for_each_entry(dev, &uc->dev_head, uclass_node) {
+               uint phandle = fdt_get_phandle(gd->fdt_blob, dev->of_offset);
+
+               if (phandle == find_phandle) {
+                       *devp = dev;
+                       return 0;
+               }
+       }
+
+       return -ENODEV;
+}
+
 int uclass_get_device_tail(struct udevice *dev, int ret,
                                  struct udevice **devp)
 {
@@ -338,6 +369,17 @@ int uclass_get_device_by_of_offset(enum uclass_id id, int node,
        return uclass_get_device_tail(dev, ret, devp);
 }
 
+int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
+                                const char *name, struct udevice **devp)
+{
+       struct udevice *dev;
+       int ret;
+
+       *devp = NULL;
+       ret = uclass_find_device_by_phandle(id, parent, name, &dev);
+       return uclass_get_device_tail(dev, ret, devp);
+}
+
 int uclass_first_device(enum uclass_id id, struct udevice **devp)
 {
        struct udevice *dev;
index 4cfc0df84c21bac942c633279ea358e968527f53..3fe17397e1202c76315c9f0f13d6cd63259ac032 100644 (file)
@@ -176,6 +176,23 @@ int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
                                   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.
+ *
+ * The device is probed to activate it ready for use.
+ *
+ * @id: uclass ID to look up
+ * @parent: Parent device containing the phandle pointer
+ * @name: Name of property in the parent device node
+ * @devp: Returns pointer to device (there is only one for each node)
+ * @return 0 if OK, -ENOENT if there is no @name present in the node, other
+ *     -ve on error
+ */
+int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
+                                const char *name, struct udevice **devp);
+
 /**
  * uclass_first_device() - Get the first device in a uclass
  *