Merge tag 'of-graph-for-4.0' of git://git.pengutronix.de/git/pza/linux into for-next
[linux-2.6-microblaze.git] / drivers / of / base.c
index 0a8aeb8..69566b6 100644 (file)
@@ -714,16 +714,12 @@ static struct device_node *__of_find_node_by_path(struct device_node *parent,
                                                const char *path)
 {
        struct device_node *child;
-       int len = strchrnul(path, '/') - path;
-       int term;
+       int len;
 
+       len = strcspn(path, "/:");
        if (!len)
                return NULL;
 
-       term = strchrnul(path, ':') - path;
-       if (term < len)
-               len = term;
-
        __for_each_child_of_node(parent, child) {
                const char *name = strrchr(child->full_name, '/');
                if (WARN(!name, "malformed device_node %s\n", child->full_name))
@@ -768,8 +764,12 @@ struct device_node *of_find_node_opts_by_path(const char *path, const char **opt
 
        /* The path could begin with an alias */
        if (*path != '/') {
-               char *p = strchrnul(path, '/');
-               int len = separator ? separator - path : p - path;
+               int len;
+               const char *p = separator;
+
+               if (!p)
+                       p = strchrnul(path, '/');
+               len = p - path;
 
                /* of_aliases must not be NULL */
                if (!of_aliases)
@@ -794,6 +794,8 @@ struct device_node *of_find_node_opts_by_path(const char *path, const char **opt
                path++; /* Increment past '/' delimiter */
                np = __of_find_node_by_path(np, path);
                path = strchrnul(path, '/');
+               if (separator && separator < path)
+                       break;
        }
        raw_spin_unlock_irqrestore(&devtree_lock, flags);
        return np;
@@ -2080,14 +2082,45 @@ int of_graph_parse_endpoint(const struct device_node *node,
 }
 EXPORT_SYMBOL(of_graph_parse_endpoint);
 
+/**
+ * of_graph_get_port_by_id() - get the port matching a given id
+ * @parent: pointer to the parent device node
+ * @id: id of the port
+ *
+ * Return: A 'port' node pointer with refcount incremented. The caller
+ * has to use of_node_put() on it when done.
+ */
+struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
+{
+       struct device_node *node, *port;
+
+       node = of_get_child_by_name(parent, "ports");
+       if (node)
+               parent = node;
+
+       for_each_child_of_node(parent, port) {
+               u32 port_id = 0;
+
+               if (of_node_cmp(port->name, "port") != 0)
+                       continue;
+               of_property_read_u32(port, "reg", &port_id);
+               if (id == port_id)
+                       break;
+       }
+
+       of_node_put(node);
+
+       return port;
+}
+EXPORT_SYMBOL(of_graph_get_port_by_id);
+
 /**
  * of_graph_get_next_endpoint() - get next endpoint node
  * @parent: pointer to the parent device node
  * @prev: previous endpoint node, or NULL to get first
  *
  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
- * of the passed @prev node is not decremented, the caller have to use
- * of_node_put() on it when done.
+ * of the passed @prev node is decremented.
  */
 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
                                        struct device_node *prev)
@@ -2123,12 +2156,6 @@ struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
                if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
                              __func__, prev->full_name))
                        return NULL;
-
-               /*
-                * Avoid dropping prev node refcount to 0 when getting the next
-                * child below.
-                */
-               of_node_get(prev);
        }
 
        while (1) {