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_RAW_SPINLOCK(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 static struct property
*__of_find_property(const struct device_node
*np
,
151 const char *name
, int *lenp
)
158 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
159 if (of_prop_cmp(pp
->name
, name
) == 0) {
169 struct property
*of_find_property(const struct device_node
*np
,
176 raw_spin_lock_irqsave(&devtree_lock
, flags
);
177 pp
= __of_find_property(np
, name
, lenp
);
178 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
182 EXPORT_SYMBOL(of_find_property
);
185 * of_find_all_nodes - Get next node in global list
186 * @prev: Previous node or NULL to start iteration
187 * of_node_put() will be called on it
189 * Returns a node pointer with refcount incremented, use
190 * of_node_put() on it when done.
192 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
194 struct device_node
*np
;
197 raw_spin_lock_irqsave(&devtree_lock
, flags
);
198 np
= prev
? prev
->allnext
: of_allnodes
;
199 for (; np
!= NULL
; np
= np
->allnext
)
203 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
206 EXPORT_SYMBOL(of_find_all_nodes
);
209 * Find a property with a given name for a given node
210 * and return the value.
212 static const void *__of_get_property(const struct device_node
*np
,
213 const char *name
, int *lenp
)
215 struct property
*pp
= __of_find_property(np
, name
, lenp
);
217 return pp
? pp
->value
: NULL
;
221 * Find a property with a given name for a given node
222 * and return the value.
224 const void *of_get_property(const struct device_node
*np
, const char *name
,
227 struct property
*pp
= of_find_property(np
, name
, lenp
);
229 return pp
? pp
->value
: NULL
;
231 EXPORT_SYMBOL(of_get_property
);
233 /** Checks if the given "compat" string matches one of the strings in
234 * the device's "compatible" property
236 static int __of_device_is_compatible(const struct device_node
*device
,
242 cp
= __of_get_property(device
, "compatible", &cplen
);
246 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
256 /** Checks if the given "compat" string matches one of the strings in
257 * the device's "compatible" property
259 int of_device_is_compatible(const struct device_node
*device
,
265 raw_spin_lock_irqsave(&devtree_lock
, flags
);
266 res
= __of_device_is_compatible(device
, compat
);
267 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
270 EXPORT_SYMBOL(of_device_is_compatible
);
273 * of_machine_is_compatible - Test root of device tree for a given compatible value
274 * @compat: compatible string to look for in root node's compatible property.
276 * Returns true if the root node has the given value in its
277 * compatible property.
279 int of_machine_is_compatible(const char *compat
)
281 struct device_node
*root
;
284 root
= of_find_node_by_path("/");
286 rc
= of_device_is_compatible(root
, compat
);
291 EXPORT_SYMBOL(of_machine_is_compatible
);
294 * __of_device_is_available - check if a device is available for use
296 * @device: Node to check for availability, with locks already held
298 * Returns 1 if the status property is absent or set to "okay" or "ok",
301 static int __of_device_is_available(const struct device_node
*device
)
306 status
= __of_get_property(device
, "status", &statlen
);
311 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
319 * of_device_is_available - check if a device is available for use
321 * @device: Node to check for availability
323 * Returns 1 if the status property is absent or set to "okay" or "ok",
326 int of_device_is_available(const struct device_node
*device
)
331 raw_spin_lock_irqsave(&devtree_lock
, flags
);
332 res
= __of_device_is_available(device
);
333 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
337 EXPORT_SYMBOL(of_device_is_available
);
340 * of_get_parent - Get a node's parent if any
341 * @node: Node to get parent
343 * Returns a node pointer with refcount incremented, use
344 * of_node_put() on it when done.
346 struct device_node
*of_get_parent(const struct device_node
*node
)
348 struct device_node
*np
;
354 raw_spin_lock_irqsave(&devtree_lock
, flags
);
355 np
= of_node_get(node
->parent
);
356 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
359 EXPORT_SYMBOL(of_get_parent
);
362 * of_get_next_parent - Iterate to a node's parent
363 * @node: Node to get parent of
365 * This is like of_get_parent() except that it drops the
366 * refcount on the passed node, making it suitable for iterating
367 * through a node's parents.
369 * Returns a node pointer with refcount incremented, use
370 * of_node_put() on it when done.
372 struct device_node
*of_get_next_parent(struct device_node
*node
)
374 struct device_node
*parent
;
380 raw_spin_lock_irqsave(&devtree_lock
, flags
);
381 parent
= of_node_get(node
->parent
);
383 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
386 EXPORT_SYMBOL(of_get_next_parent
);
389 * of_get_next_child - Iterate a node childs
391 * @prev: previous child of the parent node, or NULL to get first
393 * Returns a node pointer with refcount incremented, use
394 * of_node_put() on it when done.
396 struct device_node
*of_get_next_child(const struct device_node
*node
,
397 struct device_node
*prev
)
399 struct device_node
*next
;
402 raw_spin_lock_irqsave(&devtree_lock
, flags
);
403 next
= prev
? prev
->sibling
: node
->child
;
404 for (; next
; next
= next
->sibling
)
405 if (of_node_get(next
))
408 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
411 EXPORT_SYMBOL(of_get_next_child
);
414 * of_get_next_available_child - Find the next available child node
416 * @prev: previous child of the parent node, or NULL to get first
418 * This function is like of_get_next_child(), except that it
419 * automatically skips any disabled nodes (i.e. status = "disabled").
421 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
422 struct device_node
*prev
)
424 struct device_node
*next
;
427 raw_spin_lock_irqsave(&devtree_lock
, flags
);
428 next
= prev
? prev
->sibling
: node
->child
;
429 for (; next
; next
= next
->sibling
) {
430 if (!__of_device_is_available(next
))
432 if (of_node_get(next
))
436 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
439 EXPORT_SYMBOL(of_get_next_available_child
);
442 * of_get_child_by_name - Find the child node by name for a given parent
444 * @name: child name to look for.
446 * This function looks for child node for given matching name
448 * Returns a node pointer if found, with refcount incremented, use
449 * of_node_put() on it when done.
450 * Returns NULL if node is not found.
452 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
455 struct device_node
*child
;
457 for_each_child_of_node(node
, child
)
458 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
462 EXPORT_SYMBOL(of_get_child_by_name
);
465 * of_find_node_by_path - Find a node matching a full OF path
466 * @path: The full path to match
468 * Returns a node pointer with refcount incremented, use
469 * of_node_put() on it when done.
471 struct device_node
*of_find_node_by_path(const char *path
)
473 struct device_node
*np
= of_allnodes
;
476 raw_spin_lock_irqsave(&devtree_lock
, flags
);
477 for (; np
; np
= np
->allnext
) {
478 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
482 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
485 EXPORT_SYMBOL(of_find_node_by_path
);
488 * of_find_node_by_name - Find a node by its "name" property
489 * @from: The node to start searching from or NULL, the node
490 * you pass will not be searched, only the next one
491 * will; typically, you pass what the previous call
492 * returned. of_node_put() will be called on it
493 * @name: The name string to match against
495 * Returns a node pointer with refcount incremented, use
496 * of_node_put() on it when done.
498 struct device_node
*of_find_node_by_name(struct device_node
*from
,
501 struct device_node
*np
;
504 raw_spin_lock_irqsave(&devtree_lock
, flags
);
505 np
= from
? from
->allnext
: of_allnodes
;
506 for (; np
; np
= np
->allnext
)
507 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
511 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
514 EXPORT_SYMBOL(of_find_node_by_name
);
517 * of_find_node_by_type - Find a node by its "device_type" property
518 * @from: The node to start searching from, or NULL to start searching
519 * the entire device tree. The node you pass will not be
520 * searched, only the next one will; typically, you pass
521 * what the previous call returned. of_node_put() will be
522 * called on from for you.
523 * @type: The type string to match against
525 * Returns a node pointer with refcount incremented, use
526 * of_node_put() on it when done.
528 struct device_node
*of_find_node_by_type(struct device_node
*from
,
531 struct device_node
*np
;
534 raw_spin_lock_irqsave(&devtree_lock
, flags
);
535 np
= from
? from
->allnext
: of_allnodes
;
536 for (; np
; np
= np
->allnext
)
537 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
541 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
544 EXPORT_SYMBOL(of_find_node_by_type
);
547 * of_find_compatible_node - Find a node based on type and one of the
548 * tokens in its "compatible" property
549 * @from: The node to start searching from or NULL, the node
550 * you pass will not be searched, only the next one
551 * will; typically, you pass what the previous call
552 * returned. of_node_put() will be called on it
553 * @type: The type string to match "device_type" or NULL to ignore
554 * @compatible: The string to match to one of the tokens in the device
557 * Returns a node pointer with refcount incremented, use
558 * of_node_put() on it when done.
560 struct device_node
*of_find_compatible_node(struct device_node
*from
,
561 const char *type
, const char *compatible
)
563 struct device_node
*np
;
566 raw_spin_lock_irqsave(&devtree_lock
, flags
);
567 np
= from
? from
->allnext
: of_allnodes
;
568 for (; np
; np
= np
->allnext
) {
570 && !(np
->type
&& (of_node_cmp(np
->type
, type
) == 0)))
572 if (__of_device_is_compatible(np
, compatible
) &&
577 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
580 EXPORT_SYMBOL(of_find_compatible_node
);
583 * of_find_node_with_property - Find a node which has a property with
585 * @from: The node to start searching from or NULL, the node
586 * you pass will not be searched, only the next one
587 * will; typically, you pass what the previous call
588 * returned. of_node_put() will be called on it
589 * @prop_name: The name of the property to look for.
591 * Returns a node pointer with refcount incremented, use
592 * of_node_put() on it when done.
594 struct device_node
*of_find_node_with_property(struct device_node
*from
,
595 const char *prop_name
)
597 struct device_node
*np
;
601 raw_spin_lock_irqsave(&devtree_lock
, flags
);
602 np
= from
? from
->allnext
: of_allnodes
;
603 for (; np
; np
= np
->allnext
) {
604 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
605 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
613 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
616 EXPORT_SYMBOL(of_find_node_with_property
);
619 const struct of_device_id
*__of_match_node(const struct of_device_id
*matches
,
620 const struct device_node
*node
)
625 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
627 if (matches
->name
[0])
629 && !strcmp(matches
->name
, node
->name
);
630 if (matches
->type
[0])
632 && !strcmp(matches
->type
, node
->type
);
633 if (matches
->compatible
[0])
634 match
&= __of_device_is_compatible(node
,
635 matches
->compatible
);
644 * of_match_node - Tell if an device_node has a matching of_match structure
645 * @matches: array of of device match structures to search in
646 * @node: the of device structure to match against
648 * Low level utility function used by device matching.
650 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
651 const struct device_node
*node
)
653 const struct of_device_id
*match
;
656 raw_spin_lock_irqsave(&devtree_lock
, flags
);
657 match
= __of_match_node(matches
, node
);
658 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
661 EXPORT_SYMBOL(of_match_node
);
664 * of_find_matching_node_and_match - Find a node based on an of_device_id
666 * @from: The node to start searching from or NULL, the node
667 * you pass will not be searched, only the next one
668 * will; typically, you pass what the previous call
669 * returned. of_node_put() will be called on it
670 * @matches: array of of device match structures to search in
671 * @match Updated to point at the matches entry which matched
673 * Returns a node pointer with refcount incremented, use
674 * of_node_put() on it when done.
676 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
677 const struct of_device_id
*matches
,
678 const struct of_device_id
**match
)
680 struct device_node
*np
;
681 const struct of_device_id
*m
;
687 raw_spin_lock_irqsave(&devtree_lock
, flags
);
688 np
= from
? from
->allnext
: of_allnodes
;
689 for (; np
; np
= np
->allnext
) {
690 m
= __of_match_node(matches
, np
);
691 if (m
&& of_node_get(np
)) {
698 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
701 EXPORT_SYMBOL(of_find_matching_node_and_match
);
704 * of_modalias_node - Lookup appropriate modalias for a device node
705 * @node: pointer to a device tree node
706 * @modalias: Pointer to buffer that modalias value will be copied into
707 * @len: Length of modalias value
709 * Based on the value of the compatible property, this routine will attempt
710 * to choose an appropriate modalias value for a particular device tree node.
711 * It does this by stripping the manufacturer prefix (as delimited by a ',')
712 * from the first entry in the compatible list property.
714 * This routine returns 0 on success, <0 on failure.
716 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
718 const char *compatible
, *p
;
721 compatible
= of_get_property(node
, "compatible", &cplen
);
722 if (!compatible
|| strlen(compatible
) > cplen
)
724 p
= strchr(compatible
, ',');
725 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
728 EXPORT_SYMBOL_GPL(of_modalias_node
);
731 * of_find_node_by_phandle - Find a node given a phandle
732 * @handle: phandle of the node to find
734 * Returns a node pointer with refcount incremented, use
735 * of_node_put() on it when done.
737 struct device_node
*of_find_node_by_phandle(phandle handle
)
739 struct device_node
*np
;
742 raw_spin_lock_irqsave(&devtree_lock
, flags
);
743 for (np
= of_allnodes
; np
; np
= np
->allnext
)
744 if (np
->phandle
== handle
)
747 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
750 EXPORT_SYMBOL(of_find_node_by_phandle
);
753 * of_find_property_value_of_size
755 * @np: device node from which the property value is to be read.
756 * @propname: name of the property to be searched.
757 * @len: requested length of property value
759 * Search for a property in a device node and valid the requested size.
760 * Returns the property value on success, -EINVAL if the property does not
761 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
762 * property data isn't large enough.
765 static void *of_find_property_value_of_size(const struct device_node
*np
,
766 const char *propname
, u32 len
)
768 struct property
*prop
= of_find_property(np
, propname
, NULL
);
771 return ERR_PTR(-EINVAL
);
773 return ERR_PTR(-ENODATA
);
774 if (len
> prop
->length
)
775 return ERR_PTR(-EOVERFLOW
);
781 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
783 * @np: device node from which the property value is to be read.
784 * @propname: name of the property to be searched.
785 * @index: index of the u32 in the list of values
786 * @out_value: pointer to return value, modified only if no error.
788 * Search for a property in a device node and read nth 32-bit value from
789 * it. Returns 0 on success, -EINVAL if the property does not exist,
790 * -ENODATA if property does not have a value, and -EOVERFLOW if the
791 * property data isn't large enough.
793 * The out_value is modified only if a valid u32 value can be decoded.
795 int of_property_read_u32_index(const struct device_node
*np
,
796 const char *propname
,
797 u32 index
, u32
*out_value
)
799 const u32
*val
= of_find_property_value_of_size(np
, propname
,
800 ((index
+ 1) * sizeof(*out_value
)));
805 *out_value
= be32_to_cpup(((__be32
*)val
) + index
);
808 EXPORT_SYMBOL_GPL(of_property_read_u32_index
);
811 * of_property_read_u8_array - Find and read an array of u8 from a property.
813 * @np: device node from which the property value is to be read.
814 * @propname: name of the property to be searched.
815 * @out_value: pointer to return value, modified only if return value is 0.
816 * @sz: number of array elements to read
818 * Search for a property in a device node and read 8-bit value(s) from
819 * it. Returns 0 on success, -EINVAL if the property does not exist,
820 * -ENODATA if property does not have a value, and -EOVERFLOW if the
821 * property data isn't large enough.
823 * dts entry of array should be like:
824 * property = /bits/ 8 <0x50 0x60 0x70>;
826 * The out_value is modified only if a valid u8 value can be decoded.
828 int of_property_read_u8_array(const struct device_node
*np
,
829 const char *propname
, u8
*out_values
, size_t sz
)
831 const u8
*val
= of_find_property_value_of_size(np
, propname
,
832 (sz
* sizeof(*out_values
)));
838 *out_values
++ = *val
++;
841 EXPORT_SYMBOL_GPL(of_property_read_u8_array
);
844 * of_property_read_u16_array - Find and read an array of u16 from a property.
846 * @np: device node from which the property value is to be read.
847 * @propname: name of the property to be searched.
848 * @out_value: pointer to return value, modified only if return value is 0.
849 * @sz: number of array elements to read
851 * Search for a property in a device node and read 16-bit value(s) from
852 * it. Returns 0 on success, -EINVAL if the property does not exist,
853 * -ENODATA if property does not have a value, and -EOVERFLOW if the
854 * property data isn't large enough.
856 * dts entry of array should be like:
857 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
859 * The out_value is modified only if a valid u16 value can be decoded.
861 int of_property_read_u16_array(const struct device_node
*np
,
862 const char *propname
, u16
*out_values
, size_t sz
)
864 const __be16
*val
= of_find_property_value_of_size(np
, propname
,
865 (sz
* sizeof(*out_values
)));
871 *out_values
++ = be16_to_cpup(val
++);
874 EXPORT_SYMBOL_GPL(of_property_read_u16_array
);
877 * of_property_read_u32_array - Find and read an array of 32 bit integers
880 * @np: device node from which the property value is to be read.
881 * @propname: name of the property to be searched.
882 * @out_value: pointer to return value, modified only if return value is 0.
883 * @sz: number of array elements to read
885 * Search for a property in a device node and read 32-bit value(s) from
886 * it. Returns 0 on success, -EINVAL if the property does not exist,
887 * -ENODATA if property does not have a value, and -EOVERFLOW if the
888 * property data isn't large enough.
890 * The out_value is modified only if a valid u32 value can be decoded.
892 int of_property_read_u32_array(const struct device_node
*np
,
893 const char *propname
, u32
*out_values
,
896 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
897 (sz
* sizeof(*out_values
)));
903 *out_values
++ = be32_to_cpup(val
++);
906 EXPORT_SYMBOL_GPL(of_property_read_u32_array
);
909 * of_property_read_u64 - Find and read a 64 bit integer from a property
910 * @np: device node from which the property value is to be read.
911 * @propname: name of the property to be searched.
912 * @out_value: pointer to return value, modified only if return value is 0.
914 * Search for a property in a device node and read a 64-bit value from
915 * it. Returns 0 on success, -EINVAL if the property does not exist,
916 * -ENODATA if property does not have a value, and -EOVERFLOW if the
917 * property data isn't large enough.
919 * The out_value is modified only if a valid u64 value can be decoded.
921 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
924 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
930 *out_value
= of_read_number(val
, 2);
933 EXPORT_SYMBOL_GPL(of_property_read_u64
);
936 * of_property_read_string - Find and read a string from a property
937 * @np: device node from which the property value is to be read.
938 * @propname: name of the property to be searched.
939 * @out_string: pointer to null terminated return string, modified only if
942 * Search for a property in a device tree node and retrieve a null
943 * terminated string value (pointer to data, not a copy). Returns 0 on
944 * success, -EINVAL if the property does not exist, -ENODATA if property
945 * does not have a value, and -EILSEQ if the string is not null-terminated
946 * within the length of the property data.
948 * The out_string pointer is modified only if a valid string can be decoded.
950 int of_property_read_string(struct device_node
*np
, const char *propname
,
951 const char **out_string
)
953 struct property
*prop
= of_find_property(np
, propname
, NULL
);
958 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
960 *out_string
= prop
->value
;
963 EXPORT_SYMBOL_GPL(of_property_read_string
);
966 * of_property_match_string() - Find string in a list and return index
967 * @np: pointer to node containing string list property
968 * @propname: string list property name
969 * @string: pointer to string to search for in string list
971 * This function searches a string list property and returns the index
972 * of a specific string value.
974 int of_property_match_string(struct device_node
*np
, const char *propname
,
977 struct property
*prop
= of_find_property(np
, propname
, NULL
);
988 end
= p
+ prop
->length
;
990 for (i
= 0; p
< end
; i
++, p
+= l
) {
991 l
= strnlen(p
, end
- p
) + 1;
994 pr_debug("comparing %s with %s\n", string
, p
);
995 if (strcmp(string
, p
) == 0)
996 return i
; /* Found it; return index */
1000 EXPORT_SYMBOL_GPL(of_property_match_string
);
1003 * of_property_read_string_util() - Utility helper for parsing string properties
1004 * @np: device node from which the property value is to be read.
1005 * @propname: name of the property to be searched.
1006 * @out_strs: output array of string pointers.
1007 * @sz: number of array elements to read.
1008 * @skip: Number of strings to skip over at beginning of list.
1010 * Don't call this function directly. It is a utility helper for the
1011 * of_property_read_string*() family of functions.
1013 int of_property_read_string_helper(struct device_node
*np
, const char *propname
,
1014 const char **out_strs
, size_t sz
, int skip
)
1016 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1018 const char *p
, *end
;
1025 end
= p
+ prop
->length
;
1027 for (i
= 0; p
< end
&& (!out_strs
|| i
< skip
+ sz
); i
++, p
+= l
) {
1028 l
= strnlen(p
, end
- p
) + 1;
1031 if (out_strs
&& i
>= skip
)
1035 return i
<= 0 ? -ENODATA
: i
;
1037 EXPORT_SYMBOL_GPL(of_property_read_string_helper
);
1040 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1041 * @np: Pointer to device node holding phandle property
1042 * @phandle_name: Name of property holding a phandle value
1043 * @index: For properties holding a table of phandles, this is the index into
1046 * Returns the device_node pointer with refcount incremented. Use
1047 * of_node_put() on it when done.
1049 struct device_node
*of_parse_phandle(const struct device_node
*np
,
1050 const char *phandle_name
, int index
)
1052 const __be32
*phandle
;
1055 phandle
= of_get_property(np
, phandle_name
, &size
);
1056 if ((!phandle
) || (size
< sizeof(*phandle
) * (index
+ 1)))
1059 return of_find_node_by_phandle(be32_to_cpup(phandle
+ index
));
1061 EXPORT_SYMBOL(of_parse_phandle
);
1064 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1065 * @np: pointer to a device tree node containing a list
1066 * @list_name: property name that contains a list
1067 * @cells_name: property name that specifies phandles' arguments count
1068 * @index: index of a phandle to parse out
1069 * @out_args: optional pointer to output arguments structure (will be filled)
1071 * This function is useful to parse lists of phandles and their arguments.
1072 * Returns 0 on success and fills out_args, on error returns appropriate
1075 * Caller is responsible to call of_node_put() on the returned out_args->node
1081 * #list-cells = <2>;
1085 * #list-cells = <1>;
1089 * list = <&phandle1 1 2 &phandle2 3>;
1092 * To get a device_node of the `node2' node you may call this:
1093 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1095 static int __of_parse_phandle_with_args(const struct device_node
*np
,
1096 const char *list_name
,
1097 const char *cells_name
, int index
,
1098 struct of_phandle_args
*out_args
)
1100 const __be32
*list
, *list_end
;
1101 int rc
= 0, size
, cur_index
= 0;
1103 struct device_node
*node
= NULL
;
1106 /* Retrieve the phandle list property */
1107 list
= of_get_property(np
, list_name
, &size
);
1110 list_end
= list
+ size
/ sizeof(*list
);
1112 /* Loop over the phandles until all the requested entry is found */
1113 while (list
< list_end
) {
1118 * If phandle is 0, then it is an empty entry with no
1119 * arguments. Skip forward to the next entry.
1121 phandle
= be32_to_cpup(list
++);
1124 * Find the provider node and parse the #*-cells
1125 * property to determine the argument length
1127 node
= of_find_node_by_phandle(phandle
);
1129 pr_err("%s: could not find phandle\n",
1133 if (of_property_read_u32(node
, cells_name
, &count
)) {
1134 pr_err("%s: could not get %s for %s\n",
1135 np
->full_name
, cells_name
,
1141 * Make sure that the arguments actually fit in the
1142 * remaining property data length
1144 if (list
+ count
> list_end
) {
1145 pr_err("%s: arguments longer than property\n",
1152 * All of the error cases above bail out of the loop, so at
1153 * this point, the parsing is successful. If the requested
1154 * index matches, then fill the out_args structure and return,
1155 * or return -ENOENT for an empty entry.
1158 if (cur_index
== index
) {
1164 if (WARN_ON(count
> MAX_PHANDLE_ARGS
))
1165 count
= MAX_PHANDLE_ARGS
;
1166 out_args
->np
= node
;
1167 out_args
->args_count
= count
;
1168 for (i
= 0; i
< count
; i
++)
1169 out_args
->args
[i
] = be32_to_cpup(list
++);
1174 /* Found it! return success */
1185 * Unlock node before returning result; will be one of:
1186 * -ENOENT : index is for empty phandle
1187 * -EINVAL : parsing error on data
1188 * [1..n] : Number of phandle (count mode; when index = -1)
1190 rc
= index
< 0 ? cur_index
: -ENOENT
;
1197 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1198 const char *cells_name
, int index
,
1199 struct of_phandle_args
*out_args
)
1203 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, index
, out_args
);
1205 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1208 * of_count_phandle_with_args() - Find the number of phandles references in a property
1209 * @np: pointer to a device tree node containing a list
1210 * @list_name: property name that contains a list
1211 * @cells_name: property name that specifies phandles' arguments count
1213 * Returns the number of phandle + argument tuples within a property. It
1214 * is a typical pattern to encode a list of phandle and variable
1215 * arguments into a single property. The number of arguments is encoded
1216 * by a property in the phandle-target node. For example, a gpios
1217 * property would contain a list of GPIO specifies consisting of a
1218 * phandle and 1 or more arguments. The number of arguments are
1219 * determined by the #gpio-cells property in the node pointed to by the
1222 int of_count_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1223 const char *cells_name
)
1225 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, -1, NULL
);
1227 EXPORT_SYMBOL(of_count_phandle_with_args
);
1229 #if defined(CONFIG_OF_DYNAMIC)
1230 static int of_property_notify(int action
, struct device_node
*np
,
1231 struct property
*prop
)
1233 struct of_prop_reconfig pr
;
1237 return of_reconfig_notify(action
, &pr
);
1240 static int of_property_notify(int action
, struct device_node
*np
,
1241 struct property
*prop
)
1248 * of_add_property - Add a property to a node
1250 int of_add_property(struct device_node
*np
, struct property
*prop
)
1252 struct property
**next
;
1253 unsigned long flags
;
1256 rc
= of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
);
1261 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1262 next
= &np
->properties
;
1264 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
1265 /* duplicate ! don't insert it */
1266 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1269 next
= &(*next
)->next
;
1272 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1274 #ifdef CONFIG_PROC_DEVICETREE
1275 /* try to add to proc as well if it was initialized */
1277 proc_device_tree_add_prop(np
->pde
, prop
);
1278 #endif /* CONFIG_PROC_DEVICETREE */
1284 * of_remove_property - Remove a property from a node.
1286 * Note that we don't actually remove it, since we have given out
1287 * who-knows-how-many pointers to the data using get-property.
1288 * Instead we just move the property to the "dead properties"
1289 * list, so it won't be found any more.
1291 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1293 struct property
**next
;
1294 unsigned long flags
;
1298 rc
= of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
);
1302 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1303 next
= &np
->properties
;
1305 if (*next
== prop
) {
1306 /* found the node */
1308 prop
->next
= np
->deadprops
;
1309 np
->deadprops
= prop
;
1313 next
= &(*next
)->next
;
1315 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1320 #ifdef CONFIG_PROC_DEVICETREE
1321 /* try to remove the proc node as well */
1323 proc_device_tree_remove_prop(np
->pde
, prop
);
1324 #endif /* CONFIG_PROC_DEVICETREE */
1330 * of_update_property - Update a property in a node, if the property does
1331 * not exist, add it.
1333 * Note that we don't actually remove it, since we have given out
1334 * who-knows-how-many pointers to the data using get-property.
1335 * Instead we just move the property to the "dead properties" list,
1336 * and add the new property to the property list
1338 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1340 struct property
**next
, *oldprop
;
1341 unsigned long flags
;
1344 rc
= of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
);
1351 oldprop
= of_find_property(np
, newprop
->name
, NULL
);
1353 return of_add_property(np
, newprop
);
1355 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1356 next
= &np
->properties
;
1358 if (*next
== oldprop
) {
1359 /* found the node */
1360 newprop
->next
= oldprop
->next
;
1362 oldprop
->next
= np
->deadprops
;
1363 np
->deadprops
= oldprop
;
1367 next
= &(*next
)->next
;
1369 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1374 #ifdef CONFIG_PROC_DEVICETREE
1375 /* try to add to proc as well if it was initialized */
1377 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
1378 #endif /* CONFIG_PROC_DEVICETREE */
1383 #if defined(CONFIG_OF_DYNAMIC)
1385 * Support for dynamic device trees.
1387 * On some platforms, the device tree can be manipulated at runtime.
1388 * The routines in this section support adding, removing and changing
1389 * device tree nodes.
1392 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain
);
1394 int of_reconfig_notifier_register(struct notifier_block
*nb
)
1396 return blocking_notifier_chain_register(&of_reconfig_chain
, nb
);
1398 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register
);
1400 int of_reconfig_notifier_unregister(struct notifier_block
*nb
)
1402 return blocking_notifier_chain_unregister(&of_reconfig_chain
, nb
);
1404 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister
);
1406 int of_reconfig_notify(unsigned long action
, void *p
)
1410 rc
= blocking_notifier_call_chain(&of_reconfig_chain
, action
, p
);
1411 return notifier_to_errno(rc
);
1414 #ifdef CONFIG_PROC_DEVICETREE
1415 static void of_add_proc_dt_entry(struct device_node
*dn
)
1417 struct proc_dir_entry
*ent
;
1419 ent
= proc_mkdir(strrchr(dn
->full_name
, '/') + 1, dn
->parent
->pde
);
1421 proc_device_tree_add_node(dn
, ent
);
1424 static void of_add_proc_dt_entry(struct device_node
*dn
)
1431 * of_attach_node - Plug a device node into the tree and global list.
1433 int of_attach_node(struct device_node
*np
)
1435 unsigned long flags
;
1438 rc
= of_reconfig_notify(OF_RECONFIG_ATTACH_NODE
, np
);
1442 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1443 np
->sibling
= np
->parent
->child
;
1444 np
->allnext
= of_allnodes
;
1445 np
->parent
->child
= np
;
1447 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1449 of_add_proc_dt_entry(np
);
1453 #ifdef CONFIG_PROC_DEVICETREE
1454 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1456 proc_remove(dn
->pde
);
1459 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1466 * of_detach_node - "Unplug" a node from the device tree.
1468 * The caller must hold a reference to the node. The memory associated with
1469 * the node is not freed until its refcount goes to zero.
1471 int of_detach_node(struct device_node
*np
)
1473 struct device_node
*parent
;
1474 unsigned long flags
;
1477 rc
= of_reconfig_notify(OF_RECONFIG_DETACH_NODE
, np
);
1481 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1483 if (of_node_check_flag(np
, OF_DETACHED
)) {
1484 /* someone already detached it */
1485 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1489 parent
= np
->parent
;
1491 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1495 if (of_allnodes
== np
)
1496 of_allnodes
= np
->allnext
;
1498 struct device_node
*prev
;
1499 for (prev
= of_allnodes
;
1500 prev
->allnext
!= np
;
1501 prev
= prev
->allnext
)
1503 prev
->allnext
= np
->allnext
;
1506 if (parent
->child
== np
)
1507 parent
->child
= np
->sibling
;
1509 struct device_node
*prevsib
;
1510 for (prevsib
= np
->parent
->child
;
1511 prevsib
->sibling
!= np
;
1512 prevsib
= prevsib
->sibling
)
1514 prevsib
->sibling
= np
->sibling
;
1517 of_node_set_flag(np
, OF_DETACHED
);
1518 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1520 of_remove_proc_dt_entry(np
);
1523 #endif /* defined(CONFIG_OF_DYNAMIC) */
1525 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1526 int id
, const char *stem
, int stem_len
)
1530 strncpy(ap
->stem
, stem
, stem_len
);
1531 ap
->stem
[stem_len
] = 0;
1532 list_add_tail(&ap
->link
, &aliases_lookup
);
1533 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1534 ap
->alias
, ap
->stem
, ap
->id
, of_node_full_name(np
));
1538 * of_alias_scan - Scan all properties of 'aliases' node
1540 * The function scans all the properties of 'aliases' node and populate
1541 * the the global lookup table with the properties. It returns the
1542 * number of alias_prop found, or error code in error case.
1544 * @dt_alloc: An allocator that provides a virtual address to memory
1545 * for the resulting tree
1547 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
1549 struct property
*pp
;
1551 of_chosen
= of_find_node_by_path("/chosen");
1552 if (of_chosen
== NULL
)
1553 of_chosen
= of_find_node_by_path("/chosen@0");
1554 of_aliases
= of_find_node_by_path("/aliases");
1558 for_each_property_of_node(of_aliases
, pp
) {
1559 const char *start
= pp
->name
;
1560 const char *end
= start
+ strlen(start
);
1561 struct device_node
*np
;
1562 struct alias_prop
*ap
;
1565 /* Skip those we do not want to proceed */
1566 if (!strcmp(pp
->name
, "name") ||
1567 !strcmp(pp
->name
, "phandle") ||
1568 !strcmp(pp
->name
, "linux,phandle"))
1571 np
= of_find_node_by_path(pp
->value
);
1575 /* walk the alias backwards to extract the id and work out
1576 * the 'stem' string */
1577 while (isdigit(*(end
-1)) && end
> start
)
1581 if (kstrtoint(end
, 10, &id
) < 0)
1584 /* Allocate an alias_prop with enough space for the stem */
1585 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, 4);
1588 memset(ap
, 0, sizeof(*ap
) + len
+ 1);
1590 of_alias_add(ap
, np
, id
, start
, len
);
1595 * of_alias_get_id - Get alias id for the given device_node
1596 * @np: Pointer to the given device_node
1597 * @stem: Alias stem of the given device_node
1599 * The function travels the lookup table to get alias id for the given
1600 * device_node and alias stem. It returns the alias id if find it.
1602 int of_alias_get_id(struct device_node
*np
, const char *stem
)
1604 struct alias_prop
*app
;
1607 mutex_lock(&of_aliases_mutex
);
1608 list_for_each_entry(app
, &aliases_lookup
, link
) {
1609 if (strcmp(app
->stem
, stem
) != 0)
1612 if (np
== app
->np
) {
1617 mutex_unlock(&of_aliases_mutex
);
1621 EXPORT_SYMBOL_GPL(of_alias_get_id
);
1623 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
1626 const void *curv
= cur
;
1636 curv
+= sizeof(*cur
);
1637 if (curv
>= prop
->value
+ prop
->length
)
1641 *pu
= be32_to_cpup(curv
);
1644 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
1646 const char *of_prop_next_string(struct property
*prop
, const char *cur
)
1648 const void *curv
= cur
;
1656 curv
+= strlen(cur
) + 1;
1657 if (curv
>= prop
->value
+ prop
->length
)
1662 EXPORT_SYMBOL_GPL(of_prop_next_string
);