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 and
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 #include <linux/module.h>
22 #include <linux/spinlock.h>
24 struct device_node
*allnodes
;
26 /* use when traversing tree through the allnext, child, sibling,
27 * or parent members of struct device_node.
29 DEFINE_RWLOCK(devtree_lock
);
31 int of_n_addr_cells(struct device_node
*np
)
38 ip
= of_get_property(np
, "#address-cells", NULL
);
42 /* No #address-cells property for the root node */
43 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
45 EXPORT_SYMBOL(of_n_addr_cells
);
47 int of_n_size_cells(struct device_node
*np
)
54 ip
= of_get_property(np
, "#size-cells", NULL
);
58 /* No #size-cells property for the root node */
59 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
61 EXPORT_SYMBOL(of_n_size_cells
);
63 struct property
*of_find_property(const struct device_node
*np
,
72 read_lock(&devtree_lock
);
73 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
74 if (of_prop_cmp(pp
->name
, name
) == 0) {
80 read_unlock(&devtree_lock
);
84 EXPORT_SYMBOL(of_find_property
);
87 * of_find_all_nodes - Get next node in global list
88 * @prev: Previous node or NULL to start iteration
89 * of_node_put() will be called on it
91 * Returns a node pointer with refcount incremented, use
92 * of_node_put() on it when done.
94 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
96 struct device_node
*np
;
98 read_lock(&devtree_lock
);
99 np
= prev
? prev
->allnext
: allnodes
;
100 for (; np
!= NULL
; np
= np
->allnext
)
104 read_unlock(&devtree_lock
);
107 EXPORT_SYMBOL(of_find_all_nodes
);
110 * Find a property with a given name for a given node
111 * and return the value.
113 const void *of_get_property(const struct device_node
*np
, const char *name
,
116 struct property
*pp
= of_find_property(np
, name
, lenp
);
118 return pp
? pp
->value
: NULL
;
120 EXPORT_SYMBOL(of_get_property
);
122 /** Checks if the given "compat" string matches one of the strings in
123 * the device's "compatible" property
125 int of_device_is_compatible(const struct device_node
*device
,
131 cp
= of_get_property(device
, "compatible", &cplen
);
135 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
144 EXPORT_SYMBOL(of_device_is_compatible
);
147 * of_device_is_available - check if a device is available for use
149 * @device: Node to check for availability
151 * Returns 1 if the status property is absent or set to "okay" or "ok",
154 int of_device_is_available(const struct device_node
*device
)
159 status
= of_get_property(device
, "status", &statlen
);
164 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
170 EXPORT_SYMBOL(of_device_is_available
);
173 * of_get_parent - Get a node's parent if any
174 * @node: Node to get parent
176 * Returns a node pointer with refcount incremented, use
177 * of_node_put() on it when done.
179 struct device_node
*of_get_parent(const struct device_node
*node
)
181 struct device_node
*np
;
186 read_lock(&devtree_lock
);
187 np
= of_node_get(node
->parent
);
188 read_unlock(&devtree_lock
);
191 EXPORT_SYMBOL(of_get_parent
);
194 * of_get_next_parent - Iterate to a node's parent
195 * @node: Node to get parent of
197 * This is like of_get_parent() except that it drops the
198 * refcount on the passed node, making it suitable for iterating
199 * through a node's parents.
201 * Returns a node pointer with refcount incremented, use
202 * of_node_put() on it when done.
204 struct device_node
*of_get_next_parent(struct device_node
*node
)
206 struct device_node
*parent
;
211 read_lock(&devtree_lock
);
212 parent
= of_node_get(node
->parent
);
214 read_unlock(&devtree_lock
);
219 * of_get_next_child - Iterate a node childs
221 * @prev: previous child of the parent node, or NULL to get first
223 * Returns a node pointer with refcount incremented, use
224 * of_node_put() on it when done.
226 struct device_node
*of_get_next_child(const struct device_node
*node
,
227 struct device_node
*prev
)
229 struct device_node
*next
;
231 read_lock(&devtree_lock
);
232 next
= prev
? prev
->sibling
: node
->child
;
233 for (; next
; next
= next
->sibling
)
234 if (of_node_get(next
))
237 read_unlock(&devtree_lock
);
240 EXPORT_SYMBOL(of_get_next_child
);
243 * of_find_node_by_path - Find a node matching a full OF path
244 * @path: The full path to match
246 * Returns a node pointer with refcount incremented, use
247 * of_node_put() on it when done.
249 struct device_node
*of_find_node_by_path(const char *path
)
251 struct device_node
*np
= allnodes
;
253 read_lock(&devtree_lock
);
254 for (; np
; np
= np
->allnext
) {
255 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
259 read_unlock(&devtree_lock
);
262 EXPORT_SYMBOL(of_find_node_by_path
);
265 * of_find_node_by_name - Find a node by its "name" property
266 * @from: The node to start searching from or NULL, the node
267 * you pass will not be searched, only the next one
268 * will; typically, you pass what the previous call
269 * returned. of_node_put() will be called on it
270 * @name: The name string to match against
272 * Returns a node pointer with refcount incremented, use
273 * of_node_put() on it when done.
275 struct device_node
*of_find_node_by_name(struct device_node
*from
,
278 struct device_node
*np
;
280 read_lock(&devtree_lock
);
281 np
= from
? from
->allnext
: allnodes
;
282 for (; np
; np
= np
->allnext
)
283 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
287 read_unlock(&devtree_lock
);
290 EXPORT_SYMBOL(of_find_node_by_name
);
293 * of_find_node_by_type - Find a node by its "device_type" property
294 * @from: The node to start searching from, or NULL to start searching
295 * the entire device tree. The node you pass will not be
296 * searched, only the next one will; typically, you pass
297 * what the previous call returned. of_node_put() will be
298 * called on from for you.
299 * @type: The type string to match against
301 * Returns a node pointer with refcount incremented, use
302 * of_node_put() on it when done.
304 struct device_node
*of_find_node_by_type(struct device_node
*from
,
307 struct device_node
*np
;
309 read_lock(&devtree_lock
);
310 np
= from
? from
->allnext
: allnodes
;
311 for (; np
; np
= np
->allnext
)
312 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
316 read_unlock(&devtree_lock
);
319 EXPORT_SYMBOL(of_find_node_by_type
);
322 * of_find_compatible_node - Find a node based on type and one of the
323 * tokens in its "compatible" property
324 * @from: The node to start searching from or NULL, the node
325 * you pass will not be searched, only the next one
326 * will; typically, you pass what the previous call
327 * returned. of_node_put() will be called on it
328 * @type: The type string to match "device_type" or NULL to ignore
329 * @compatible: The string to match to one of the tokens in the device
332 * Returns a node pointer with refcount incremented, use
333 * of_node_put() on it when done.
335 struct device_node
*of_find_compatible_node(struct device_node
*from
,
336 const char *type
, const char *compatible
)
338 struct device_node
*np
;
340 read_lock(&devtree_lock
);
341 np
= from
? from
->allnext
: allnodes
;
342 for (; np
; np
= np
->allnext
) {
344 && !(np
->type
&& (of_node_cmp(np
->type
, type
) == 0)))
346 if (of_device_is_compatible(np
, compatible
) && of_node_get(np
))
350 read_unlock(&devtree_lock
);
353 EXPORT_SYMBOL(of_find_compatible_node
);
356 * of_find_node_with_property - Find a node which has a property with
358 * @from: The node to start searching from or NULL, the node
359 * you pass will not be searched, only the next one
360 * will; typically, you pass what the previous call
361 * returned. of_node_put() will be called on it
362 * @prop_name: The name of the property to look for.
364 * Returns a node pointer with refcount incremented, use
365 * of_node_put() on it when done.
367 struct device_node
*of_find_node_with_property(struct device_node
*from
,
368 const char *prop_name
)
370 struct device_node
*np
;
373 read_lock(&devtree_lock
);
374 np
= from
? from
->allnext
: allnodes
;
375 for (; np
; np
= np
->allnext
) {
376 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
377 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
385 read_unlock(&devtree_lock
);
388 EXPORT_SYMBOL(of_find_node_with_property
);
391 * of_match_node - Tell if an device_node has a matching of_match structure
392 * @matches: array of of device match structures to search in
393 * @node: the of device structure to match against
395 * Low level utility function used by device matching.
397 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
398 const struct device_node
*node
)
400 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
402 if (matches
->name
[0])
404 && !strcmp(matches
->name
, node
->name
);
405 if (matches
->type
[0])
407 && !strcmp(matches
->type
, node
->type
);
408 if (matches
->compatible
[0])
409 match
&= of_device_is_compatible(node
,
410 matches
->compatible
);
417 EXPORT_SYMBOL(of_match_node
);
420 * of_find_matching_node - Find a node based on an of_device_id match
422 * @from: The node to start searching from or NULL, the node
423 * you pass will not be searched, only the next one
424 * will; typically, you pass what the previous call
425 * returned. of_node_put() will be called on it
426 * @matches: array of of device match structures to search in
428 * Returns a node pointer with refcount incremented, use
429 * of_node_put() on it when done.
431 struct device_node
*of_find_matching_node(struct device_node
*from
,
432 const struct of_device_id
*matches
)
434 struct device_node
*np
;
436 read_lock(&devtree_lock
);
437 np
= from
? from
->allnext
: allnodes
;
438 for (; np
; np
= np
->allnext
) {
439 if (of_match_node(matches
, np
) && of_node_get(np
))
443 read_unlock(&devtree_lock
);
446 EXPORT_SYMBOL(of_find_matching_node
);
449 * of_modalias_table: Table of explicit compatible ==> modalias mappings
451 * This table allows particulare compatible property values to be mapped
452 * to modalias strings. This is useful for busses which do not directly
453 * understand the OF device tree but are populated based on data contained
454 * within the device tree. SPI and I2C are the two current users of this
457 * In most cases, devices do not need to be listed in this table because
458 * the modalias value can be derived directly from the compatible table.
459 * However, if for any reason a value cannot be derived, then this table
460 * provides a method to override the implicit derivation.
462 * At the moment, a single table is used for all bus types because it is
463 * assumed that the data size is small and that the compatible values
464 * should already be distinct enough to differentiate between SPI, I2C
467 struct of_modalias_table
{
471 static struct of_modalias_table of_modalias_table
[] = {
472 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
473 { "mmc-spi-slot", "mmc_spi" },
477 * of_modalias_node - Lookup appropriate modalias for a device node
478 * @node: pointer to a device tree node
479 * @modalias: Pointer to buffer that modalias value will be copied into
480 * @len: Length of modalias value
482 * Based on the value of the compatible property, this routine will determine
483 * an appropriate modalias value for a particular device tree node. Two
484 * separate methods are attempted to derive a modalias value.
486 * First method is to lookup the compatible value in of_modalias_table.
487 * Second is to strip off the manufacturer prefix from the first
488 * compatible entry and use the remainder as modalias
490 * This routine returns 0 on success
492 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
495 const char *compatible
;
498 /* 1. search for exception list entry */
499 for (i
= 0; i
< ARRAY_SIZE(of_modalias_table
); i
++) {
500 compatible
= of_modalias_table
[i
].of_device
;
501 if (!of_device_is_compatible(node
, compatible
))
503 strlcpy(modalias
, of_modalias_table
[i
].modalias
, len
);
507 compatible
= of_get_property(node
, "compatible", &cplen
);
511 /* 2. take first compatible entry and strip manufacturer */
512 p
= strchr(compatible
, ',');
516 strlcpy(modalias
, p
, len
);
519 EXPORT_SYMBOL_GPL(of_modalias_node
);
522 * of_parse_phandle - Resolve a phandle property to a device_node pointer
523 * @np: Pointer to device node holding phandle property
524 * @phandle_name: Name of property holding a phandle value
525 * @index: For properties holding a table of phandles, this is the index into
528 * Returns the device_node pointer with refcount incremented. Use
529 * of_node_put() on it when done.
532 of_parse_phandle(struct device_node
*np
, const char *phandle_name
, int index
)
534 const phandle
*phandle
;
537 phandle
= of_get_property(np
, phandle_name
, &size
);
538 if ((!phandle
) || (size
< sizeof(*phandle
) * (index
+ 1)))
541 return of_find_node_by_phandle(phandle
[index
]);
543 EXPORT_SYMBOL(of_parse_phandle
);
546 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
547 * @np: pointer to a device tree node containing a list
548 * @list_name: property name that contains a list
549 * @cells_name: property name that specifies phandles' arguments count
550 * @index: index of a phandle to parse out
551 * @out_node: optional pointer to device_node struct pointer (will be filled)
552 * @out_args: optional pointer to arguments pointer (will be filled)
554 * This function is useful to parse lists of phandles and their arguments.
555 * Returns 0 on success and fills out_node and out_args, on error returns
556 * appropriate errno value.
569 * list = <&phandle1 1 2 &phandle2 3>;
572 * To get a device_node of the `node2' node you may call this:
573 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
575 int of_parse_phandles_with_args(struct device_node
*np
, const char *list_name
,
576 const char *cells_name
, int index
,
577 struct device_node
**out_node
,
578 const void **out_args
)
585 struct device_node
*node
= NULL
;
586 const void *args
= NULL
;
588 list
= of_get_property(np
, list_name
, &size
);
593 list_end
= list
+ size
/ sizeof(*list
);
595 while (list
< list_end
) {
597 const phandle
*phandle
;
602 /* one cell hole in the list = <>; */
606 node
= of_find_node_by_phandle(*phandle
);
608 pr_debug("%s: could not find phandle\n",
613 cells
= of_get_property(node
, cells_name
, &size
);
614 if (!cells
|| size
!= sizeof(*cells
)) {
615 pr_debug("%s: could not get %s for %s\n",
616 np
->full_name
, cells_name
, node
->full_name
);
621 if (list
> list_end
) {
622 pr_debug("%s: insufficient arguments length\n",
627 if (cur_index
== index
)
638 * args w/o node indicates that the loop above has stopped at
639 * the 'hole' cell. Report this differently.
657 pr_debug("%s failed with status %d\n", __func__
, ret
);
660 EXPORT_SYMBOL(of_parse_phandles_with_args
);