2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
19 #include <linux/module.h>
21 #include <linux/spinlock.h>
23 /* use when traversing tree through the allnext, child, sibling,
24 * or parent members of struct device_node.
26 DEFINE_RWLOCK(devtree_lock);
28 int of_n_addr_cells(struct device_node *np)
35 ip = of_get_property(np, "#address-cells", NULL);
39 /* No #address-cells property for the root node */
40 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
42 EXPORT_SYMBOL(of_n_addr_cells);
44 int of_n_size_cells(struct device_node *np)
51 ip = of_get_property(np, "#size-cells", NULL);
55 /* No #size-cells property for the root node */
56 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
58 EXPORT_SYMBOL(of_n_size_cells);
60 struct property *of_find_property(const struct device_node *np,
66 read_lock(&devtree_lock);
67 for (pp = np->properties; pp != 0; pp = pp->next) {
68 if (of_prop_cmp(pp->name, name) == 0) {
74 read_unlock(&devtree_lock);
78 EXPORT_SYMBOL(of_find_property);
81 * Find a property with a given name for a given node
82 * and return the value.
84 const void *of_get_property(const struct device_node *np, const char *name,
87 struct property *pp = of_find_property(np, name, lenp);
89 return pp ? pp->value : NULL;
91 EXPORT_SYMBOL(of_get_property);
93 /** Checks if the given "compat" string matches one of the strings in
94 * the device's "compatible" property
96 int of_device_is_compatible(const struct device_node *device,
102 cp = of_get_property(device, "compatible", &cplen);
106 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
115 EXPORT_SYMBOL(of_device_is_compatible);
118 * of_get_parent - Get a node's parent if any
119 * @node: Node to get parent
121 * Returns a node pointer with refcount incremented, use
122 * of_node_put() on it when done.
124 struct device_node *of_get_parent(const struct device_node *node)
126 struct device_node *np;
131 read_lock(&devtree_lock);
132 np = of_node_get(node->parent);
133 read_unlock(&devtree_lock);
136 EXPORT_SYMBOL(of_get_parent);
139 * of_get_next_child - Iterate a node childs
141 * @prev: previous child of the parent node, or NULL to get first
143 * Returns a node pointer with refcount incremented, use
144 * of_node_put() on it when done.
146 struct device_node *of_get_next_child(const struct device_node *node,
147 struct device_node *prev)
149 struct device_node *next;
151 read_lock(&devtree_lock);
152 next = prev ? prev->sibling : node->child;
153 for (; next; next = next->sibling)
154 if (of_node_get(next))
157 read_unlock(&devtree_lock);
160 EXPORT_SYMBOL(of_get_next_child);