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 struct device_node
*allnodes
;
25 /* use when traversing tree through the allnext, child, sibling,
26 * or parent members of struct device_node.
28 DEFINE_RWLOCK(devtree_lock
);
30 int of_n_addr_cells(struct device_node
*np
)
37 ip
= of_get_property(np
, "#address-cells", NULL
);
41 /* No #address-cells property for the root node */
42 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
44 EXPORT_SYMBOL(of_n_addr_cells
);
46 int of_n_size_cells(struct device_node
*np
)
53 ip
= of_get_property(np
, "#size-cells", NULL
);
57 /* No #size-cells property for the root node */
58 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
60 EXPORT_SYMBOL(of_n_size_cells
);
62 struct property
*of_find_property(const struct device_node
*np
,
71 read_lock(&devtree_lock
);
72 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
73 if (of_prop_cmp(pp
->name
, name
) == 0) {
79 read_unlock(&devtree_lock
);
83 EXPORT_SYMBOL(of_find_property
);
86 * Find a property with a given name for a given node
87 * and return the value.
89 const void *of_get_property(const struct device_node
*np
, const char *name
,
92 struct property
*pp
= of_find_property(np
, name
, lenp
);
94 return pp
? pp
->value
: NULL
;
96 EXPORT_SYMBOL(of_get_property
);
98 /** Checks if the given "compat" string matches one of the strings in
99 * the device's "compatible" property
101 int of_device_is_compatible(const struct device_node
*device
,
107 cp
= of_get_property(device
, "compatible", &cplen
);
111 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
120 EXPORT_SYMBOL(of_device_is_compatible
);
123 * of_device_is_available - check if a device is available for use
125 * @device: Node to check for availability
127 * Returns 1 if the status property is absent or set to "okay" or "ok",
130 int of_device_is_available(const struct device_node
*device
)
135 status
= of_get_property(device
, "status", &statlen
);
140 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
146 EXPORT_SYMBOL(of_device_is_available
);
149 * of_get_parent - Get a node's parent if any
150 * @node: Node to get parent
152 * Returns a node pointer with refcount incremented, use
153 * of_node_put() on it when done.
155 struct device_node
*of_get_parent(const struct device_node
*node
)
157 struct device_node
*np
;
162 read_lock(&devtree_lock
);
163 np
= of_node_get(node
->parent
);
164 read_unlock(&devtree_lock
);
167 EXPORT_SYMBOL(of_get_parent
);
170 * of_get_next_parent - Iterate to a node's parent
171 * @node: Node to get parent of
173 * This is like of_get_parent() except that it drops the
174 * refcount on the passed node, making it suitable for iterating
175 * through a node's parents.
177 * Returns a node pointer with refcount incremented, use
178 * of_node_put() on it when done.
180 struct device_node
*of_get_next_parent(struct device_node
*node
)
182 struct device_node
*parent
;
187 read_lock(&devtree_lock
);
188 parent
= of_node_get(node
->parent
);
190 read_unlock(&devtree_lock
);
195 * of_get_next_child - Iterate a node childs
197 * @prev: previous child of the parent node, or NULL to get first
199 * Returns a node pointer with refcount incremented, use
200 * of_node_put() on it when done.
202 struct device_node
*of_get_next_child(const struct device_node
*node
,
203 struct device_node
*prev
)
205 struct device_node
*next
;
207 read_lock(&devtree_lock
);
208 next
= prev
? prev
->sibling
: node
->child
;
209 for (; next
; next
= next
->sibling
)
210 if (of_node_get(next
))
213 read_unlock(&devtree_lock
);
216 EXPORT_SYMBOL(of_get_next_child
);
219 * of_find_node_by_path - Find a node matching a full OF path
220 * @path: The full path to match
222 * Returns a node pointer with refcount incremented, use
223 * of_node_put() on it when done.
225 struct device_node
*of_find_node_by_path(const char *path
)
227 struct device_node
*np
= allnodes
;
229 read_lock(&devtree_lock
);
230 for (; np
; np
= np
->allnext
) {
231 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
235 read_unlock(&devtree_lock
);
238 EXPORT_SYMBOL(of_find_node_by_path
);
241 * of_find_node_by_name - Find a node by its "name" property
242 * @from: The node to start searching from or NULL, the node
243 * you pass will not be searched, only the next one
244 * will; typically, you pass what the previous call
245 * returned. of_node_put() will be called on it
246 * @name: The name string to match against
248 * Returns a node pointer with refcount incremented, use
249 * of_node_put() on it when done.
251 struct device_node
*of_find_node_by_name(struct device_node
*from
,
254 struct device_node
*np
;
256 read_lock(&devtree_lock
);
257 np
= from
? from
->allnext
: allnodes
;
258 for (; np
; np
= np
->allnext
)
259 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
263 read_unlock(&devtree_lock
);
266 EXPORT_SYMBOL(of_find_node_by_name
);
269 * of_find_node_by_type - Find a node by its "device_type" property
270 * @from: The node to start searching from, or NULL to start searching
271 * the entire device tree. The node you pass will not be
272 * searched, only the next one will; typically, you pass
273 * what the previous call returned. of_node_put() will be
274 * called on from for you.
275 * @type: The type string to match against
277 * Returns a node pointer with refcount incremented, use
278 * of_node_put() on it when done.
280 struct device_node
*of_find_node_by_type(struct device_node
*from
,
283 struct device_node
*np
;
285 read_lock(&devtree_lock
);
286 np
= from
? from
->allnext
: allnodes
;
287 for (; np
; np
= np
->allnext
)
288 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
292 read_unlock(&devtree_lock
);
295 EXPORT_SYMBOL(of_find_node_by_type
);
298 * of_find_compatible_node - Find a node based on type and one of the
299 * tokens in its "compatible" property
300 * @from: The node to start searching from or NULL, the node
301 * you pass will not be searched, only the next one
302 * will; typically, you pass what the previous call
303 * returned. of_node_put() will be called on it
304 * @type: The type string to match "device_type" or NULL to ignore
305 * @compatible: The string to match to one of the tokens in the device
308 * Returns a node pointer with refcount incremented, use
309 * of_node_put() on it when done.
311 struct device_node
*of_find_compatible_node(struct device_node
*from
,
312 const char *type
, const char *compatible
)
314 struct device_node
*np
;
316 read_lock(&devtree_lock
);
317 np
= from
? from
->allnext
: allnodes
;
318 for (; np
; np
= np
->allnext
) {
320 && !(np
->type
&& (of_node_cmp(np
->type
, type
) == 0)))
322 if (of_device_is_compatible(np
, compatible
) && of_node_get(np
))
326 read_unlock(&devtree_lock
);
329 EXPORT_SYMBOL(of_find_compatible_node
);
332 * of_match_node - Tell if an device_node has a matching of_match structure
333 * @matches: array of of device match structures to search in
334 * @node: the of device structure to match against
336 * Low level utility function used by device matching.
338 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
339 const struct device_node
*node
)
341 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
343 if (matches
->name
[0])
345 && !strcmp(matches
->name
, node
->name
);
346 if (matches
->type
[0])
348 && !strcmp(matches
->type
, node
->type
);
349 if (matches
->compatible
[0])
350 match
&= of_device_is_compatible(node
,
351 matches
->compatible
);
358 EXPORT_SYMBOL(of_match_node
);
361 * of_find_matching_node - Find a node based on an of_device_id match
363 * @from: The node to start searching from or NULL, the node
364 * you pass will not be searched, only the next one
365 * will; typically, you pass what the previous call
366 * returned. of_node_put() will be called on it
367 * @matches: array of of device match structures to search in
369 * Returns a node pointer with refcount incremented, use
370 * of_node_put() on it when done.
372 struct device_node
*of_find_matching_node(struct device_node
*from
,
373 const struct of_device_id
*matches
)
375 struct device_node
*np
;
377 read_lock(&devtree_lock
);
378 np
= from
? from
->allnext
: allnodes
;
379 for (; np
; np
= np
->allnext
) {
380 if (of_match_node(matches
, np
) && of_node_get(np
))
384 read_unlock(&devtree_lock
);
387 EXPORT_SYMBOL(of_find_matching_node
);