]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - drivers/of/base.c
Consolidate of_get_parent
[karo-tx-linux.git] / drivers / of / base.c
index 723d80d704e063575d667b031c5251a14f6054e1..82bb78680ff6ca73117898d5f2354dd497f6d37f 100644 (file)
  */
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/spinlock.h>
+
+/* use when traversing tree through the allnext, child, sibling,
+ * or parent members of struct device_node.
+ */
+DEFINE_RWLOCK(devtree_lock);
 
 int of_n_addr_cells(struct device_node *np)
 {
@@ -51,6 +57,26 @@ int of_n_size_cells(struct device_node *np)
 }
 EXPORT_SYMBOL(of_n_size_cells);
 
+struct property *of_find_property(const struct device_node *np,
+                                 const char *name,
+                                 int *lenp)
+{
+       struct property *pp;
+
+       read_lock(&devtree_lock);
+       for (pp = np->properties; pp != 0; pp = pp->next) {
+               if (of_prop_cmp(pp->name, name) == 0) {
+                       if (lenp != 0)
+                               *lenp = pp->length;
+                       break;
+               }
+       }
+       read_unlock(&devtree_lock);
+
+       return pp;
+}
+EXPORT_SYMBOL(of_find_property);
+
 /*
  * Find a property with a given name for a given node
  * and return the value.
@@ -63,3 +89,48 @@ const void *of_get_property(const struct device_node *np, const char *name,
        return pp ? pp->value : NULL;
 }
 EXPORT_SYMBOL(of_get_property);
+
+/** Checks if the given "compat" string matches one of the strings in
+ * the device's "compatible" property
+ */
+int of_device_is_compatible(const struct device_node *device,
+               const char *compat)
+{
+       const char* cp;
+       int cplen, l;
+
+       cp = of_get_property(device, "compatible", &cplen);
+       if (cp == NULL)
+               return 0;
+       while (cplen > 0) {
+               if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
+                       return 1;
+               l = strlen(cp) + 1;
+               cp += l;
+               cplen -= l;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL(of_device_is_compatible);
+
+/**
+ *     of_get_parent - Get a node's parent if any
+ *     @node:  Node to get parent
+ *
+ *     Returns a node pointer with refcount incremented, use
+ *     of_node_put() on it when done.
+ */
+struct device_node *of_get_parent(const struct device_node *node)
+{
+       struct device_node *np;
+
+       if (!node)
+               return NULL;
+
+       read_lock(&devtree_lock);
+       np = of_node_get(node->parent);
+       read_unlock(&devtree_lock);
+       return np;
+}
+EXPORT_SYMBOL(of_get_parent);