ofnode: add ofnode_by_prop_value()
authorJens Wiklander <jens.wiklander@linaro.org>
Mon, 20 Aug 2018 09:09:58 +0000 (11:09 +0200)
committerSimon Glass <sjg@chromium.org>
Tue, 18 Sep 2018 14:12:21 +0000 (08:12 -0600)
Adds ofnode_by_prop_value() to search for nodes with a given property
and value, an ofnode version of fdt_node_offset_by_prop_value().

Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
drivers/core/of_access.c
drivers/core/ofnode.c
include/dm/of_access.h
include/dm/ofnode.h

index 0729dfcdb3b82e7d62d6fbb7a98a6e07035dfea9..14c020a687b75c97d6fc85e8d8f33b8e95f652c6 100644 (file)
@@ -376,6 +376,33 @@ struct device_node *of_find_compatible_node(struct device_node *from,
        return np;
 }
 
+static int of_device_has_prop_value(const struct device_node *device,
+                                   const char *propname, const void *propval,
+                                   int proplen)
+{
+       struct property *prop = of_find_property(device, propname, NULL);
+
+       if (!prop || !prop->value || prop->length != proplen)
+               return 0;
+       return !memcmp(prop->value, propval, proplen);
+}
+
+struct device_node *of_find_node_by_prop_value(struct device_node *from,
+                                              const char *propname,
+                                              const void *propval, int proplen)
+{
+       struct device_node *np;
+
+       for_each_of_allnodes_from(from, np) {
+               if (of_device_has_prop_value(np, propname, propval, proplen) &&
+                   of_node_get(np))
+                       break;
+       }
+       of_node_put(from);
+
+       return np;
+}
+
 struct device_node *of_find_node_by_phandle(phandle handle)
 {
        struct device_node *np;
index 0cfb0fbabb008f45798c49ddbf2ce9148ba57b15..a7e192772324753f09b82e459793275c46b41165 100644 (file)
@@ -777,3 +777,17 @@ ofnode ofnode_by_compatible(ofnode from, const char *compat)
                                gd->fdt_blob, ofnode_to_offset(from), compat));
        }
 }
+
+ofnode ofnode_by_prop_value(ofnode from, const char *propname,
+                           const void *propval, int proplen)
+{
+       if (of_live_active()) {
+               return np_to_ofnode(of_find_node_by_prop_value(
+                       (struct device_node *)ofnode_to_np(from), propname,
+                       propval, proplen));
+       } else {
+               return offset_to_ofnode(fdt_node_offset_by_prop_value(
+                               gd->fdt_blob, ofnode_to_offset(from),
+                               propname, propval, proplen));
+       }
+}
index dd1abb8e97b467a72b578c556dbb01492b877f54..5ed1a0cdb42799750f65cd3f3de4a1e41e0b93b3 100644 (file)
@@ -193,6 +193,22 @@ static inline struct device_node *of_find_node_by_path(const char *path)
 struct device_node *of_find_compatible_node(struct device_node *from,
                                const char *type, const char *compatible);
 
+/**
+ * of_find_node_by_prop_value() - find a node with a given property value
+ *
+ * Find a node based on a property value.
+ * @from: Node to start searching from or NULL. the node you pass will not be
+ *     searched, only the next one will; typically, you pass what the previous
+ *     call returned.
+ * @propname: property name to check
+ * @propval: property value to search for
+ * @proplen: length of the value in propval
+ * @return node pointer or NULL if not found
+ */
+struct device_node *of_find_node_by_prop_value(struct device_node *from,
+                                              const char *propname,
+                                              const void *propval,
+                                              int proplen);
 /**
  * of_find_node_by_phandle() - Find a node given a phandle
  *
index ab36b74c4ca415dee7dcfdcbe3040b062854cd37..c06d77849c73ee9cd988bb777eb20b3bb07d47d6 100644 (file)
@@ -702,6 +702,20 @@ int ofnode_read_resource_byname(ofnode node, const char *name,
  */
 ofnode ofnode_by_compatible(ofnode from, const char *compat);
 
+/**
+ * ofnode_by_prop_value() - Find the next node with given property value
+ *
+ * Find the next node after @from that has a @propname with a value
+ * @propval and a length @proplen.
+ *
+ * @from: ofnode to start from (use ofnode_null() to start at the
+ * beginning) @propname: property name to check @propval: property value to
+ * search for @proplen: length of the value in propval @return ofnode
+ * found, or ofnode_null() if none
+ */
+ofnode ofnode_by_prop_value(ofnode from, const char *propname,
+                           const void *propval, int proplen);
+
 /**
  * ofnode_for_each_subnode() - iterate over all subnodes of a parent
  *