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/ctype.h>
21 #include <linux/module.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/proc_fs.h>
27 #include "of_private.h"
29 LIST_HEAD(aliases_lookup
);
31 struct device_node
*of_allnodes
;
32 EXPORT_SYMBOL(of_allnodes
);
33 struct device_node
*of_chosen
;
34 struct device_node
*of_aliases
;
36 DEFINE_MUTEX(of_aliases_mutex
);
38 /* use when traversing tree through the allnext, child, sibling,
39 * or parent members of struct device_node.
41 DEFINE_RWLOCK(devtree_lock
);
43 int of_n_addr_cells(struct device_node
*np
)
50 ip
= of_get_property(np
, "#address-cells", NULL
);
52 return be32_to_cpup(ip
);
54 /* No #address-cells property for the root node */
55 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
57 EXPORT_SYMBOL(of_n_addr_cells
);
59 int of_n_size_cells(struct device_node
*np
)
66 ip
= of_get_property(np
, "#size-cells", NULL
);
68 return be32_to_cpup(ip
);
70 /* No #size-cells property for the root node */
71 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
73 EXPORT_SYMBOL(of_n_size_cells
);
75 #if defined(CONFIG_OF_DYNAMIC)
77 * of_node_get - Increment refcount of a node
78 * @node: Node to inc refcount, NULL is supported to
79 * simplify writing of callers
83 struct device_node
*of_node_get(struct device_node
*node
)
86 kref_get(&node
->kref
);
89 EXPORT_SYMBOL(of_node_get
);
91 static inline struct device_node
*kref_to_device_node(struct kref
*kref
)
93 return container_of(kref
, struct device_node
, kref
);
97 * of_node_release - release a dynamically allocated node
98 * @kref: kref element of the node to be released
100 * In of_node_put() this function is passed to kref_put()
103 static void of_node_release(struct kref
*kref
)
105 struct device_node
*node
= kref_to_device_node(kref
);
106 struct property
*prop
= node
->properties
;
108 /* We should never be releasing nodes that haven't been detached. */
109 if (!of_node_check_flag(node
, OF_DETACHED
)) {
110 pr_err("ERROR: Bad of_node_put() on %s\n", node
->full_name
);
112 kref_init(&node
->kref
);
116 if (!of_node_check_flag(node
, OF_DYNAMIC
))
120 struct property
*next
= prop
->next
;
127 prop
= node
->deadprops
;
128 node
->deadprops
= NULL
;
131 kfree(node
->full_name
);
137 * of_node_put - Decrement refcount of a node
138 * @node: Node to dec refcount, NULL is supported to
139 * simplify writing of callers
142 void of_node_put(struct device_node
*node
)
145 kref_put(&node
->kref
, of_node_release
);
147 EXPORT_SYMBOL(of_node_put
);
148 #endif /* CONFIG_OF_DYNAMIC */
150 struct property
*of_find_property(const struct device_node
*np
,
159 read_lock(&devtree_lock
);
160 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
161 if (of_prop_cmp(pp
->name
, name
) == 0) {
167 read_unlock(&devtree_lock
);
171 EXPORT_SYMBOL(of_find_property
);
174 * of_find_all_nodes - Get next node in global list
175 * @prev: Previous node or NULL to start iteration
176 * of_node_put() will be called on it
178 * Returns a node pointer with refcount incremented, use
179 * of_node_put() on it when done.
181 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
183 struct device_node
*np
;
185 read_lock(&devtree_lock
);
186 np
= prev
? prev
->allnext
: of_allnodes
;
187 for (; np
!= NULL
; np
= np
->allnext
)
191 read_unlock(&devtree_lock
);
194 EXPORT_SYMBOL(of_find_all_nodes
);
197 * Find a property with a given name for a given node
198 * and return the value.
200 const void *of_get_property(const struct device_node
*np
, const char *name
,
203 struct property
*pp
= of_find_property(np
, name
, lenp
);
205 return pp
? pp
->value
: NULL
;
207 EXPORT_SYMBOL(of_get_property
);
209 /** Checks if the given "compat" string matches one of the strings in
210 * the device's "compatible" property
212 int of_device_is_compatible(const struct device_node
*device
,
218 cp
= of_get_property(device
, "compatible", &cplen
);
222 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
231 EXPORT_SYMBOL(of_device_is_compatible
);
234 * of_machine_is_compatible - Test root of device tree for a given compatible value
235 * @compat: compatible string to look for in root node's compatible property.
237 * Returns true if the root node has the given value in its
238 * compatible property.
240 int of_machine_is_compatible(const char *compat
)
242 struct device_node
*root
;
245 root
= of_find_node_by_path("/");
247 rc
= of_device_is_compatible(root
, compat
);
252 EXPORT_SYMBOL(of_machine_is_compatible
);
255 * of_device_is_available - check if a device is available for use
257 * @device: Node to check for availability
259 * Returns 1 if the status property is absent or set to "okay" or "ok",
262 int of_device_is_available(const struct device_node
*device
)
267 status
= of_get_property(device
, "status", &statlen
);
272 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
278 EXPORT_SYMBOL(of_device_is_available
);
281 * of_get_parent - Get a node's parent if any
282 * @node: Node to get parent
284 * Returns a node pointer with refcount incremented, use
285 * of_node_put() on it when done.
287 struct device_node
*of_get_parent(const struct device_node
*node
)
289 struct device_node
*np
;
294 read_lock(&devtree_lock
);
295 np
= of_node_get(node
->parent
);
296 read_unlock(&devtree_lock
);
299 EXPORT_SYMBOL(of_get_parent
);
302 * of_get_next_parent - Iterate to a node's parent
303 * @node: Node to get parent of
305 * This is like of_get_parent() except that it drops the
306 * refcount on the passed node, making it suitable for iterating
307 * through a node's parents.
309 * Returns a node pointer with refcount incremented, use
310 * of_node_put() on it when done.
312 struct device_node
*of_get_next_parent(struct device_node
*node
)
314 struct device_node
*parent
;
319 read_lock(&devtree_lock
);
320 parent
= of_node_get(node
->parent
);
322 read_unlock(&devtree_lock
);
327 * of_get_next_child - Iterate a node childs
329 * @prev: previous child of the parent node, or NULL to get first
331 * Returns a node pointer with refcount incremented, use
332 * of_node_put() on it when done.
334 struct device_node
*of_get_next_child(const struct device_node
*node
,
335 struct device_node
*prev
)
337 struct device_node
*next
;
339 read_lock(&devtree_lock
);
340 next
= prev
? prev
->sibling
: node
->child
;
341 for (; next
; next
= next
->sibling
)
342 if (of_node_get(next
))
345 read_unlock(&devtree_lock
);
348 EXPORT_SYMBOL(of_get_next_child
);
351 * of_get_next_available_child - Find the next available child node
353 * @prev: previous child of the parent node, or NULL to get first
355 * This function is like of_get_next_child(), except that it
356 * automatically skips any disabled nodes (i.e. status = "disabled").
358 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
359 struct device_node
*prev
)
361 struct device_node
*next
;
363 read_lock(&devtree_lock
);
364 next
= prev
? prev
->sibling
: node
->child
;
365 for (; next
; next
= next
->sibling
) {
366 if (!of_device_is_available(next
))
368 if (of_node_get(next
))
372 read_unlock(&devtree_lock
);
375 EXPORT_SYMBOL(of_get_next_available_child
);
378 * of_get_child_by_name - Find the child node by name for a given parent
380 * @name: child name to look for.
382 * This function looks for child node for given matching name
384 * Returns a node pointer if found, with refcount incremented, use
385 * of_node_put() on it when done.
386 * Returns NULL if node is not found.
388 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
391 struct device_node
*child
;
393 for_each_child_of_node(node
, child
)
394 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
398 EXPORT_SYMBOL(of_get_child_by_name
);
401 * of_find_node_by_path - Find a node matching a full OF path
402 * @path: The full path to match
404 * Returns a node pointer with refcount incremented, use
405 * of_node_put() on it when done.
407 struct device_node
*of_find_node_by_path(const char *path
)
409 struct device_node
*np
= of_allnodes
;
411 read_lock(&devtree_lock
);
412 for (; np
; np
= np
->allnext
) {
413 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
417 read_unlock(&devtree_lock
);
420 EXPORT_SYMBOL(of_find_node_by_path
);
423 * of_find_node_by_name - Find a node by its "name" property
424 * @from: The node to start searching from or NULL, the node
425 * you pass will not be searched, only the next one
426 * will; typically, you pass what the previous call
427 * returned. of_node_put() will be called on it
428 * @name: The name string to match against
430 * Returns a node pointer with refcount incremented, use
431 * of_node_put() on it when done.
433 struct device_node
*of_find_node_by_name(struct device_node
*from
,
436 struct device_node
*np
;
438 read_lock(&devtree_lock
);
439 np
= from
? from
->allnext
: of_allnodes
;
440 for (; np
; np
= np
->allnext
)
441 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
445 read_unlock(&devtree_lock
);
448 EXPORT_SYMBOL(of_find_node_by_name
);
451 * of_find_node_by_type - Find a node by its "device_type" property
452 * @from: The node to start searching from, or NULL to start searching
453 * the entire device tree. The node you pass will not be
454 * searched, only the next one will; typically, you pass
455 * what the previous call returned. of_node_put() will be
456 * called on from for you.
457 * @type: The type string to match against
459 * Returns a node pointer with refcount incremented, use
460 * of_node_put() on it when done.
462 struct device_node
*of_find_node_by_type(struct device_node
*from
,
465 struct device_node
*np
;
467 read_lock(&devtree_lock
);
468 np
= from
? from
->allnext
: of_allnodes
;
469 for (; np
; np
= np
->allnext
)
470 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
474 read_unlock(&devtree_lock
);
477 EXPORT_SYMBOL(of_find_node_by_type
);
480 * of_find_compatible_node - Find a node based on type and one of the
481 * tokens in its "compatible" property
482 * @from: The node to start searching from or NULL, the node
483 * you pass will not be searched, only the next one
484 * will; typically, you pass what the previous call
485 * returned. of_node_put() will be called on it
486 * @type: The type string to match "device_type" or NULL to ignore
487 * @compatible: The string to match to one of the tokens in the device
490 * Returns a node pointer with refcount incremented, use
491 * of_node_put() on it when done.
493 struct device_node
*of_find_compatible_node(struct device_node
*from
,
494 const char *type
, const char *compatible
)
496 struct device_node
*np
;
498 read_lock(&devtree_lock
);
499 np
= from
? from
->allnext
: of_allnodes
;
500 for (; np
; np
= np
->allnext
) {
502 && !(np
->type
&& (of_node_cmp(np
->type
, type
) == 0)))
504 if (of_device_is_compatible(np
, compatible
) && of_node_get(np
))
508 read_unlock(&devtree_lock
);
511 EXPORT_SYMBOL(of_find_compatible_node
);
514 * of_find_node_with_property - Find a node which has a property with
516 * @from: The node to start searching from or NULL, the node
517 * you pass will not be searched, only the next one
518 * will; typically, you pass what the previous call
519 * returned. of_node_put() will be called on it
520 * @prop_name: The name of the property to look for.
522 * Returns a node pointer with refcount incremented, use
523 * of_node_put() on it when done.
525 struct device_node
*of_find_node_with_property(struct device_node
*from
,
526 const char *prop_name
)
528 struct device_node
*np
;
531 read_lock(&devtree_lock
);
532 np
= from
? from
->allnext
: of_allnodes
;
533 for (; np
; np
= np
->allnext
) {
534 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
535 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
543 read_unlock(&devtree_lock
);
546 EXPORT_SYMBOL(of_find_node_with_property
);
549 * of_match_node - Tell if an device_node has a matching of_match structure
550 * @matches: array of of device match structures to search in
551 * @node: the of device structure to match against
553 * Low level utility function used by device matching.
555 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
556 const struct device_node
*node
)
561 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
563 if (matches
->name
[0])
565 && !strcmp(matches
->name
, node
->name
);
566 if (matches
->type
[0])
568 && !strcmp(matches
->type
, node
->type
);
569 if (matches
->compatible
[0])
570 match
&= of_device_is_compatible(node
,
571 matches
->compatible
);
578 EXPORT_SYMBOL(of_match_node
);
581 * of_find_matching_node_and_match - Find a node based on an of_device_id
583 * @from: The node to start searching from or NULL, the node
584 * you pass will not be searched, only the next one
585 * will; typically, you pass what the previous call
586 * returned. of_node_put() will be called on it
587 * @matches: array of of device match structures to search in
588 * @match Updated to point at the matches entry which matched
590 * Returns a node pointer with refcount incremented, use
591 * of_node_put() on it when done.
593 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
594 const struct of_device_id
*matches
,
595 const struct of_device_id
**match
)
597 struct device_node
*np
;
602 read_lock(&devtree_lock
);
603 np
= from
? from
->allnext
: of_allnodes
;
604 for (; np
; np
= np
->allnext
) {
605 if (of_match_node(matches
, np
) && of_node_get(np
)) {
612 read_unlock(&devtree_lock
);
615 EXPORT_SYMBOL(of_find_matching_node_and_match
);
618 * of_modalias_node - Lookup appropriate modalias for a device node
619 * @node: pointer to a device tree node
620 * @modalias: Pointer to buffer that modalias value will be copied into
621 * @len: Length of modalias value
623 * Based on the value of the compatible property, this routine will attempt
624 * to choose an appropriate modalias value for a particular device tree node.
625 * It does this by stripping the manufacturer prefix (as delimited by a ',')
626 * from the first entry in the compatible list property.
628 * This routine returns 0 on success, <0 on failure.
630 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
632 const char *compatible
, *p
;
635 compatible
= of_get_property(node
, "compatible", &cplen
);
636 if (!compatible
|| strlen(compatible
) > cplen
)
638 p
= strchr(compatible
, ',');
639 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
642 EXPORT_SYMBOL_GPL(of_modalias_node
);
645 * of_find_node_by_phandle - Find a node given a phandle
646 * @handle: phandle of the node to find
648 * Returns a node pointer with refcount incremented, use
649 * of_node_put() on it when done.
651 struct device_node
*of_find_node_by_phandle(phandle handle
)
653 struct device_node
*np
;
655 read_lock(&devtree_lock
);
656 for (np
= of_allnodes
; np
; np
= np
->allnext
)
657 if (np
->phandle
== handle
)
660 read_unlock(&devtree_lock
);
663 EXPORT_SYMBOL(of_find_node_by_phandle
);
666 * of_property_read_u8_array - Find and read an array of u8 from a property.
668 * @np: device node from which the property value is to be read.
669 * @propname: name of the property to be searched.
670 * @out_value: pointer to return value, modified only if return value is 0.
671 * @sz: number of array elements to read
673 * Search for a property in a device node and read 8-bit value(s) from
674 * it. Returns 0 on success, -EINVAL if the property does not exist,
675 * -ENODATA if property does not have a value, and -EOVERFLOW if the
676 * property data isn't large enough.
678 * dts entry of array should be like:
679 * property = /bits/ 8 <0x50 0x60 0x70>;
681 * The out_value is modified only if a valid u8 value can be decoded.
683 int of_property_read_u8_array(const struct device_node
*np
,
684 const char *propname
, u8
*out_values
, size_t sz
)
686 struct property
*prop
= of_find_property(np
, propname
, NULL
);
693 if ((sz
* sizeof(*out_values
)) > prop
->length
)
698 *out_values
++ = *val
++;
701 EXPORT_SYMBOL_GPL(of_property_read_u8_array
);
704 * of_property_read_u16_array - Find and read an array of u16 from a property.
706 * @np: device node from which the property value is to be read.
707 * @propname: name of the property to be searched.
708 * @out_value: pointer to return value, modified only if return value is 0.
709 * @sz: number of array elements to read
711 * Search for a property in a device node and read 16-bit value(s) from
712 * it. Returns 0 on success, -EINVAL if the property does not exist,
713 * -ENODATA if property does not have a value, and -EOVERFLOW if the
714 * property data isn't large enough.
716 * dts entry of array should be like:
717 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
719 * The out_value is modified only if a valid u16 value can be decoded.
721 int of_property_read_u16_array(const struct device_node
*np
,
722 const char *propname
, u16
*out_values
, size_t sz
)
724 struct property
*prop
= of_find_property(np
, propname
, NULL
);
731 if ((sz
* sizeof(*out_values
)) > prop
->length
)
736 *out_values
++ = be16_to_cpup(val
++);
739 EXPORT_SYMBOL_GPL(of_property_read_u16_array
);
742 * of_property_read_u32_array - Find and read an array of 32 bit integers
745 * @np: device node from which the property value is to be read.
746 * @propname: name of the property to be searched.
747 * @out_value: pointer to return value, modified only if return value is 0.
748 * @sz: number of array elements to read
750 * Search for a property in a device node and read 32-bit value(s) from
751 * it. Returns 0 on success, -EINVAL if the property does not exist,
752 * -ENODATA if property does not have a value, and -EOVERFLOW if the
753 * property data isn't large enough.
755 * The out_value is modified only if a valid u32 value can be decoded.
757 int of_property_read_u32_array(const struct device_node
*np
,
758 const char *propname
, u32
*out_values
,
761 struct property
*prop
= of_find_property(np
, propname
, NULL
);
768 if ((sz
* sizeof(*out_values
)) > prop
->length
)
773 *out_values
++ = be32_to_cpup(val
++);
776 EXPORT_SYMBOL_GPL(of_property_read_u32_array
);
779 * of_property_read_u64 - Find and read a 64 bit integer from a property
780 * @np: device node from which the property value is to be read.
781 * @propname: name of the property to be searched.
782 * @out_value: pointer to return value, modified only if return value is 0.
784 * Search for a property in a device node and read a 64-bit value from
785 * it. Returns 0 on success, -EINVAL if the property does not exist,
786 * -ENODATA if property does not have a value, and -EOVERFLOW if the
787 * property data isn't large enough.
789 * The out_value is modified only if a valid u64 value can be decoded.
791 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
794 struct property
*prop
= of_find_property(np
, propname
, NULL
);
800 if (sizeof(*out_value
) > prop
->length
)
802 *out_value
= of_read_number(prop
->value
, 2);
805 EXPORT_SYMBOL_GPL(of_property_read_u64
);
808 * of_property_read_string - Find and read a string from a property
809 * @np: device node from which the property value is to be read.
810 * @propname: name of the property to be searched.
811 * @out_string: pointer to null terminated return string, modified only if
814 * Search for a property in a device tree node and retrieve a null
815 * terminated string value (pointer to data, not a copy). Returns 0 on
816 * success, -EINVAL if the property does not exist, -ENODATA if property
817 * does not have a value, and -EILSEQ if the string is not null-terminated
818 * within the length of the property data.
820 * The out_string pointer is modified only if a valid string can be decoded.
822 int of_property_read_string(struct device_node
*np
, const char *propname
,
823 const char **out_string
)
825 struct property
*prop
= of_find_property(np
, propname
, NULL
);
830 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
832 *out_string
= prop
->value
;
835 EXPORT_SYMBOL_GPL(of_property_read_string
);
838 * of_property_read_string_index - Find and read a string from a multiple
840 * @np: device node from which the property value is to be read.
841 * @propname: name of the property to be searched.
842 * @index: index of the string in the list of strings
843 * @out_string: pointer to null terminated return string, modified only if
846 * Search for a property in a device tree node and retrieve a null
847 * terminated string value (pointer to data, not a copy) in the list of strings
848 * contained in that property.
849 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
850 * property does not have a value, and -EILSEQ if the string is not
851 * null-terminated within the length of the property data.
853 * The out_string pointer is modified only if a valid string can be decoded.
855 int of_property_read_string_index(struct device_node
*np
, const char *propname
,
856 int index
, const char **output
)
858 struct property
*prop
= of_find_property(np
, propname
, NULL
);
860 size_t l
= 0, total
= 0;
867 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
872 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
) {
881 EXPORT_SYMBOL_GPL(of_property_read_string_index
);
884 * of_property_match_string() - Find string in a list and return index
885 * @np: pointer to node containing string list property
886 * @propname: string list property name
887 * @string: pointer to string to search for in string list
889 * This function searches a string list property and returns the index
890 * of a specific string value.
892 int of_property_match_string(struct device_node
*np
, const char *propname
,
895 struct property
*prop
= of_find_property(np
, propname
, NULL
);
906 end
= p
+ prop
->length
;
908 for (i
= 0; p
< end
; i
++, p
+= l
) {
912 pr_debug("comparing %s with %s\n", string
, p
);
913 if (strcmp(string
, p
) == 0)
914 return i
; /* Found it; return index */
918 EXPORT_SYMBOL_GPL(of_property_match_string
);
921 * of_property_count_strings - Find and return the number of strings from a
922 * multiple strings property.
923 * @np: device node from which the property value is to be read.
924 * @propname: name of the property to be searched.
926 * Search for a property in a device tree node and retrieve the number of null
927 * terminated string contain in it. Returns the number of strings on
928 * success, -EINVAL if the property does not exist, -ENODATA if property
929 * does not have a value, and -EILSEQ if the string is not null-terminated
930 * within the length of the property data.
932 int of_property_count_strings(struct device_node
*np
, const char *propname
)
934 struct property
*prop
= of_find_property(np
, propname
, NULL
);
936 size_t l
= 0, total
= 0;
943 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
948 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
, i
++)
953 EXPORT_SYMBOL_GPL(of_property_count_strings
);
956 * of_parse_phandle - Resolve a phandle property to a device_node pointer
957 * @np: Pointer to device node holding phandle property
958 * @phandle_name: Name of property holding a phandle value
959 * @index: For properties holding a table of phandles, this is the index into
962 * Returns the device_node pointer with refcount incremented. Use
963 * of_node_put() on it when done.
965 struct device_node
*of_parse_phandle(const struct device_node
*np
,
966 const char *phandle_name
, int index
)
968 const __be32
*phandle
;
971 phandle
= of_get_property(np
, phandle_name
, &size
);
972 if ((!phandle
) || (size
< sizeof(*phandle
) * (index
+ 1)))
975 return of_find_node_by_phandle(be32_to_cpup(phandle
+ index
));
977 EXPORT_SYMBOL(of_parse_phandle
);
980 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
981 * @np: pointer to a device tree node containing a list
982 * @list_name: property name that contains a list
983 * @cells_name: property name that specifies phandles' arguments count
984 * @index: index of a phandle to parse out
985 * @out_args: optional pointer to output arguments structure (will be filled)
987 * This function is useful to parse lists of phandles and their arguments.
988 * Returns 0 on success and fills out_args, on error returns appropriate
991 * Caller is responsible to call of_node_put() on the returned out_args->node
1001 * #list-cells = <1>;
1005 * list = <&phandle1 1 2 &phandle2 3>;
1008 * To get a device_node of the `node2' node you may call this:
1009 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1011 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1012 const char *cells_name
, int index
,
1013 struct of_phandle_args
*out_args
)
1015 const __be32
*list
, *list_end
;
1016 int size
, cur_index
= 0;
1018 struct device_node
*node
= NULL
;
1021 /* Retrieve the phandle list property */
1022 list
= of_get_property(np
, list_name
, &size
);
1025 list_end
= list
+ size
/ sizeof(*list
);
1027 /* Loop over the phandles until all the requested entry is found */
1028 while (list
< list_end
) {
1032 * If phandle is 0, then it is an empty entry with no
1033 * arguments. Skip forward to the next entry.
1035 phandle
= be32_to_cpup(list
++);
1038 * Find the provider node and parse the #*-cells
1039 * property to determine the argument length
1041 node
= of_find_node_by_phandle(phandle
);
1043 pr_err("%s: could not find phandle\n",
1047 if (of_property_read_u32(node
, cells_name
, &count
)) {
1048 pr_err("%s: could not get %s for %s\n",
1049 np
->full_name
, cells_name
,
1055 * Make sure that the arguments actually fit in the
1056 * remaining property data length
1058 if (list
+ count
> list_end
) {
1059 pr_err("%s: arguments longer than property\n",
1066 * All of the error cases above bail out of the loop, so at
1067 * this point, the parsing is successful. If the requested
1068 * index matches, then fill the out_args structure and return,
1069 * or return -ENOENT for an empty entry.
1071 if (cur_index
== index
) {
1077 if (WARN_ON(count
> MAX_PHANDLE_ARGS
))
1078 count
= MAX_PHANDLE_ARGS
;
1079 out_args
->np
= node
;
1080 out_args
->args_count
= count
;
1081 for (i
= 0; i
< count
; i
++)
1082 out_args
->args
[i
] = be32_to_cpup(list
++);
1093 /* Loop exited without finding a valid entry; return an error */
1098 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1100 #if defined(CONFIG_OF_DYNAMIC)
1101 static int of_property_notify(int action
, struct device_node
*np
,
1102 struct property
*prop
)
1104 struct of_prop_reconfig pr
;
1108 return of_reconfig_notify(action
, &pr
);
1111 static int of_property_notify(int action
, struct device_node
*np
,
1112 struct property
*prop
)
1119 * of_add_property - Add a property to a node
1121 int of_add_property(struct device_node
*np
, struct property
*prop
)
1123 struct property
**next
;
1124 unsigned long flags
;
1127 rc
= of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
);
1132 write_lock_irqsave(&devtree_lock
, flags
);
1133 next
= &np
->properties
;
1135 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
1136 /* duplicate ! don't insert it */
1137 write_unlock_irqrestore(&devtree_lock
, flags
);
1140 next
= &(*next
)->next
;
1143 write_unlock_irqrestore(&devtree_lock
, flags
);
1145 #ifdef CONFIG_PROC_DEVICETREE
1146 /* try to add to proc as well if it was initialized */
1148 proc_device_tree_add_prop(np
->pde
, prop
);
1149 #endif /* CONFIG_PROC_DEVICETREE */
1155 * of_remove_property - Remove a property from a node.
1157 * Note that we don't actually remove it, since we have given out
1158 * who-knows-how-many pointers to the data using get-property.
1159 * Instead we just move the property to the "dead properties"
1160 * list, so it won't be found any more.
1162 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1164 struct property
**next
;
1165 unsigned long flags
;
1169 rc
= of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
);
1173 write_lock_irqsave(&devtree_lock
, flags
);
1174 next
= &np
->properties
;
1176 if (*next
== prop
) {
1177 /* found the node */
1179 prop
->next
= np
->deadprops
;
1180 np
->deadprops
= prop
;
1184 next
= &(*next
)->next
;
1186 write_unlock_irqrestore(&devtree_lock
, flags
);
1191 #ifdef CONFIG_PROC_DEVICETREE
1192 /* try to remove the proc node as well */
1194 proc_device_tree_remove_prop(np
->pde
, prop
);
1195 #endif /* CONFIG_PROC_DEVICETREE */
1201 * of_update_property - Update a property in a node, if the property does
1202 * not exist, add it.
1204 * Note that we don't actually remove it, since we have given out
1205 * who-knows-how-many pointers to the data using get-property.
1206 * Instead we just move the property to the "dead properties" list,
1207 * and add the new property to the property list
1209 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1211 struct property
**next
, *oldprop
;
1212 unsigned long flags
;
1215 rc
= of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
);
1222 oldprop
= of_find_property(np
, newprop
->name
, NULL
);
1224 return of_add_property(np
, newprop
);
1226 write_lock_irqsave(&devtree_lock
, flags
);
1227 next
= &np
->properties
;
1229 if (*next
== oldprop
) {
1230 /* found the node */
1231 newprop
->next
= oldprop
->next
;
1233 oldprop
->next
= np
->deadprops
;
1234 np
->deadprops
= oldprop
;
1238 next
= &(*next
)->next
;
1240 write_unlock_irqrestore(&devtree_lock
, flags
);
1245 #ifdef CONFIG_PROC_DEVICETREE
1246 /* try to add to proc as well if it was initialized */
1248 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
1249 #endif /* CONFIG_PROC_DEVICETREE */
1254 #if defined(CONFIG_OF_DYNAMIC)
1256 * Support for dynamic device trees.
1258 * On some platforms, the device tree can be manipulated at runtime.
1259 * The routines in this section support adding, removing and changing
1260 * device tree nodes.
1263 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain
);
1265 int of_reconfig_notifier_register(struct notifier_block
*nb
)
1267 return blocking_notifier_chain_register(&of_reconfig_chain
, nb
);
1269 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register
);
1271 int of_reconfig_notifier_unregister(struct notifier_block
*nb
)
1273 return blocking_notifier_chain_unregister(&of_reconfig_chain
, nb
);
1275 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister
);
1277 int of_reconfig_notify(unsigned long action
, void *p
)
1281 rc
= blocking_notifier_call_chain(&of_reconfig_chain
, action
, p
);
1282 return notifier_to_errno(rc
);
1285 #ifdef CONFIG_PROC_DEVICETREE
1286 static void of_add_proc_dt_entry(struct device_node
*dn
)
1288 struct proc_dir_entry
*ent
;
1290 ent
= proc_mkdir(strrchr(dn
->full_name
, '/') + 1, dn
->parent
->pde
);
1292 proc_device_tree_add_node(dn
, ent
);
1295 static void of_add_proc_dt_entry(struct device_node
*dn
)
1302 * of_attach_node - Plug a device node into the tree and global list.
1304 int of_attach_node(struct device_node
*np
)
1306 unsigned long flags
;
1309 rc
= of_reconfig_notify(OF_RECONFIG_ATTACH_NODE
, np
);
1313 write_lock_irqsave(&devtree_lock
, flags
);
1314 np
->sibling
= np
->parent
->child
;
1315 np
->allnext
= of_allnodes
;
1316 np
->parent
->child
= np
;
1318 write_unlock_irqrestore(&devtree_lock
, flags
);
1320 of_add_proc_dt_entry(np
);
1324 #ifdef CONFIG_PROC_DEVICETREE
1325 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1327 struct device_node
*parent
= dn
->parent
;
1328 struct property
*prop
= dn
->properties
;
1331 remove_proc_entry(prop
->name
, dn
->pde
);
1336 remove_proc_entry(dn
->pde
->name
, parent
->pde
);
1339 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1346 * of_detach_node - "Unplug" a node from the device tree.
1348 * The caller must hold a reference to the node. The memory associated with
1349 * the node is not freed until its refcount goes to zero.
1351 int of_detach_node(struct device_node
*np
)
1353 struct device_node
*parent
;
1354 unsigned long flags
;
1357 rc
= of_reconfig_notify(OF_RECONFIG_DETACH_NODE
, np
);
1361 write_lock_irqsave(&devtree_lock
, flags
);
1363 if (of_node_check_flag(np
, OF_DETACHED
)) {
1364 /* someone already detached it */
1365 write_unlock_irqrestore(&devtree_lock
, flags
);
1369 parent
= np
->parent
;
1371 write_unlock_irqrestore(&devtree_lock
, flags
);
1375 if (of_allnodes
== np
)
1376 of_allnodes
= np
->allnext
;
1378 struct device_node
*prev
;
1379 for (prev
= of_allnodes
;
1380 prev
->allnext
!= np
;
1381 prev
= prev
->allnext
)
1383 prev
->allnext
= np
->allnext
;
1386 if (parent
->child
== np
)
1387 parent
->child
= np
->sibling
;
1389 struct device_node
*prevsib
;
1390 for (prevsib
= np
->parent
->child
;
1391 prevsib
->sibling
!= np
;
1392 prevsib
= prevsib
->sibling
)
1394 prevsib
->sibling
= np
->sibling
;
1397 of_node_set_flag(np
, OF_DETACHED
);
1398 write_unlock_irqrestore(&devtree_lock
, flags
);
1400 of_remove_proc_dt_entry(np
);
1403 #endif /* defined(CONFIG_OF_DYNAMIC) */
1405 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1406 int id
, const char *stem
, int stem_len
)
1410 strncpy(ap
->stem
, stem
, stem_len
);
1411 ap
->stem
[stem_len
] = 0;
1412 list_add_tail(&ap
->link
, &aliases_lookup
);
1413 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1414 ap
->alias
, ap
->stem
, ap
->id
, of_node_full_name(np
));
1418 * of_alias_scan - Scan all properties of 'aliases' node
1420 * The function scans all the properties of 'aliases' node and populate
1421 * the the global lookup table with the properties. It returns the
1422 * number of alias_prop found, or error code in error case.
1424 * @dt_alloc: An allocator that provides a virtual address to memory
1425 * for the resulting tree
1427 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
1429 struct property
*pp
;
1431 of_chosen
= of_find_node_by_path("/chosen");
1432 if (of_chosen
== NULL
)
1433 of_chosen
= of_find_node_by_path("/chosen@0");
1434 of_aliases
= of_find_node_by_path("/aliases");
1438 for_each_property_of_node(of_aliases
, pp
) {
1439 const char *start
= pp
->name
;
1440 const char *end
= start
+ strlen(start
);
1441 struct device_node
*np
;
1442 struct alias_prop
*ap
;
1445 /* Skip those we do not want to proceed */
1446 if (!strcmp(pp
->name
, "name") ||
1447 !strcmp(pp
->name
, "phandle") ||
1448 !strcmp(pp
->name
, "linux,phandle"))
1451 np
= of_find_node_by_path(pp
->value
);
1455 /* walk the alias backwards to extract the id and work out
1456 * the 'stem' string */
1457 while (isdigit(*(end
-1)) && end
> start
)
1461 if (kstrtoint(end
, 10, &id
) < 0)
1464 /* Allocate an alias_prop with enough space for the stem */
1465 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, 4);
1469 of_alias_add(ap
, np
, id
, start
, len
);
1474 * of_alias_get_id - Get alias id for the given device_node
1475 * @np: Pointer to the given device_node
1476 * @stem: Alias stem of the given device_node
1478 * The function travels the lookup table to get alias id for the given
1479 * device_node and alias stem. It returns the alias id if find it.
1481 int of_alias_get_id(struct device_node
*np
, const char *stem
)
1483 struct alias_prop
*app
;
1486 mutex_lock(&of_aliases_mutex
);
1487 list_for_each_entry(app
, &aliases_lookup
, link
) {
1488 if (strcmp(app
->stem
, stem
) != 0)
1491 if (np
== app
->np
) {
1496 mutex_unlock(&of_aliases_mutex
);
1500 EXPORT_SYMBOL_GPL(of_alias_get_id
);
1502 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
1505 const void *curv
= cur
;
1515 curv
+= sizeof(*cur
);
1516 if (curv
>= prop
->value
+ prop
->length
)
1520 *pu
= be32_to_cpup(curv
);
1523 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
1525 const char *of_prop_next_string(struct property
*prop
, const char *cur
)
1527 const void *curv
= cur
;
1535 curv
+= strlen(cur
) + 1;
1536 if (curv
>= prop
->value
+ prop
->length
)
1541 EXPORT_SYMBOL_GPL(of_prop_next_string
);