]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/of/base.c
of: Reduce indentation in of_graph_get_next_endpoint
[karo-tx-linux.git] / drivers / of / base.c
index 89e888a78899e2b61281f7007406e5f937cc28a0..a8e47d37cc7f03b03d08970785d1b0120c4838c8 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/cpu.h>
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/of_graph.h>
 #include <linux/spinlock.h>
 #include <linux/slab.h>
 #include <linux/proc_fs.h>
@@ -1982,3 +1983,122 @@ struct device_node *of_find_next_cache_node(const struct device_node *np)
 
        return NULL;
 }
+
+/**
+ * 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.
+ */
+struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
+                                       struct device_node *prev)
+{
+       struct device_node *endpoint;
+       struct device_node *port = NULL;
+
+       if (!parent)
+               return NULL;
+
+       if (!prev) {
+               struct device_node *node;
+               /*
+                * It's the first call, we have to find a port subnode
+                * within this node or within an optional 'ports' node.
+                */
+               node = of_get_child_by_name(parent, "ports");
+               if (node)
+                       parent = node;
+
+               port = of_get_child_by_name(parent, "port");
+
+               if (port) {
+                       /* Found a port, get an endpoint. */
+                       endpoint = of_get_next_child(port, NULL);
+                       of_node_put(port);
+               } else {
+                       endpoint = NULL;
+               }
+
+               if (!endpoint)
+                       pr_err("%s(): no endpoint nodes specified for %s\n",
+                              __func__, parent->full_name);
+               of_node_put(node);
+
+               return endpoint;
+       }
+
+       port = of_get_parent(prev);
+       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. */
+       of_node_get(prev);
+       endpoint = of_get_next_child(port, prev);
+       if (endpoint) {
+               of_node_put(port);
+               return endpoint;
+       }
+
+       /* No more endpoints under this port, try the next one. */
+       do {
+               port = of_get_next_child(parent, port);
+               if (!port)
+                       return NULL;
+       } while (of_node_cmp(port->name, "port"));
+
+       /* Pick up the first endpoint in this port. */
+       endpoint = of_get_next_child(port, NULL);
+       of_node_put(port);
+
+       return endpoint;
+}
+EXPORT_SYMBOL(of_graph_get_next_endpoint);
+
+/**
+ * of_graph_get_remote_port_parent() - get remote port's parent node
+ * @node: pointer to a local endpoint device_node
+ *
+ * Return: Remote device node associated with remote endpoint node linked
+ *        to @node. Use of_node_put() on it when done.
+ */
+struct device_node *of_graph_get_remote_port_parent(
+                              const struct device_node *node)
+{
+       struct device_node *np;
+       unsigned int depth;
+
+       /* Get remote endpoint node. */
+       np = of_parse_phandle(node, "remote-endpoint", 0);
+
+       /* Walk 3 levels up only if there is 'ports' node. */
+       for (depth = 3; depth && np; depth--) {
+               np = of_get_next_parent(np);
+               if (depth == 2 && of_node_cmp(np->name, "ports"))
+                       break;
+       }
+       return np;
+}
+EXPORT_SYMBOL(of_graph_get_remote_port_parent);
+
+/**
+ * of_graph_get_remote_port() - get remote port node
+ * @node: pointer to a local endpoint device_node
+ *
+ * Return: Remote port node associated with remote endpoint node linked
+ *        to @node. Use of_node_put() on it when done.
+ */
+struct device_node *of_graph_get_remote_port(const struct device_node *node)
+{
+       struct device_node *np;
+
+       /* Get remote endpoint node. */
+       np = of_parse_phandle(node, "remote-endpoint", 0);
+       if (!np)
+               return NULL;
+       return of_get_next_parent(np);
+}
+EXPORT_SYMBOL(of_graph_get_remote_port);