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
;
196 raw_spin_lock(&devtree_lock
);
197 np
= prev
? prev
->allnext
: of_allnodes
;
198 for (; np
!= NULL
; np
= np
->allnext
)
202 raw_spin_unlock(&devtree_lock
);
205 EXPORT_SYMBOL(of_find_all_nodes
);
208 * Find a property with a given name for a given node
209 * and return the value.
211 static const void *__of_get_property(const struct device_node
*np
,
212 const char *name
, int *lenp
)
214 struct property
*pp
= __of_find_property(np
, name
, lenp
);
216 return pp
? pp
->value
: NULL
;
220 * Find a property with a given name for a given node
221 * and return the value.
223 const void *of_get_property(const struct device_node
*np
, const char *name
,
226 struct property
*pp
= of_find_property(np
, name
, lenp
);
228 return pp
? pp
->value
: NULL
;
230 EXPORT_SYMBOL(of_get_property
);
232 /** Checks if the given "compat" string matches one of the strings in
233 * the device's "compatible" property
235 static int __of_device_is_compatible(const struct device_node
*device
,
241 cp
= __of_get_property(device
, "compatible", &cplen
);
245 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
255 /** Checks if the given "compat" string matches one of the strings in
256 * the device's "compatible" property
258 int of_device_is_compatible(const struct device_node
*device
,
264 raw_spin_lock_irqsave(&devtree_lock
, flags
);
265 res
= __of_device_is_compatible(device
, compat
);
266 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
269 EXPORT_SYMBOL(of_device_is_compatible
);
272 * of_machine_is_compatible - Test root of device tree for a given compatible value
273 * @compat: compatible string to look for in root node's compatible property.
275 * Returns true if the root node has the given value in its
276 * compatible property.
278 int of_machine_is_compatible(const char *compat
)
280 struct device_node
*root
;
283 root
= of_find_node_by_path("/");
285 rc
= of_device_is_compatible(root
, compat
);
290 EXPORT_SYMBOL(of_machine_is_compatible
);
293 * __of_device_is_available - check if a device is available for use
295 * @device: Node to check for availability, with locks already held
297 * Returns 1 if the status property is absent or set to "okay" or "ok",
300 static int __of_device_is_available(const struct device_node
*device
)
305 status
= __of_get_property(device
, "status", &statlen
);
310 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
318 * of_device_is_available - check if a device is available for use
320 * @device: Node to check for availability
322 * Returns 1 if the status property is absent or set to "okay" or "ok",
325 int of_device_is_available(const struct device_node
*device
)
330 raw_spin_lock_irqsave(&devtree_lock
, flags
);
331 res
= __of_device_is_available(device
);
332 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
336 EXPORT_SYMBOL(of_device_is_available
);
339 * of_get_parent - Get a node's parent if any
340 * @node: Node to get parent
342 * Returns a node pointer with refcount incremented, use
343 * of_node_put() on it when done.
345 struct device_node
*of_get_parent(const struct device_node
*node
)
347 struct device_node
*np
;
353 raw_spin_lock_irqsave(&devtree_lock
, flags
);
354 np
= of_node_get(node
->parent
);
355 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
358 EXPORT_SYMBOL(of_get_parent
);
361 * of_get_next_parent - Iterate to a node's parent
362 * @node: Node to get parent of
364 * This is like of_get_parent() except that it drops the
365 * refcount on the passed node, making it suitable for iterating
366 * through a node's parents.
368 * Returns a node pointer with refcount incremented, use
369 * of_node_put() on it when done.
371 struct device_node
*of_get_next_parent(struct device_node
*node
)
373 struct device_node
*parent
;
379 raw_spin_lock_irqsave(&devtree_lock
, flags
);
380 parent
= of_node_get(node
->parent
);
382 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
387 * of_get_next_child - Iterate a node childs
389 * @prev: previous child of the parent node, or NULL to get first
391 * Returns a node pointer with refcount incremented, use
392 * of_node_put() on it when done.
394 struct device_node
*of_get_next_child(const struct device_node
*node
,
395 struct device_node
*prev
)
397 struct device_node
*next
;
400 raw_spin_lock_irqsave(&devtree_lock
, flags
);
401 next
= prev
? prev
->sibling
: node
->child
;
402 for (; next
; next
= next
->sibling
)
403 if (of_node_get(next
))
406 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
409 EXPORT_SYMBOL(of_get_next_child
);
412 * of_get_next_available_child - Find the next available child node
414 * @prev: previous child of the parent node, or NULL to get first
416 * This function is like of_get_next_child(), except that it
417 * automatically skips any disabled nodes (i.e. status = "disabled").
419 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
420 struct device_node
*prev
)
422 struct device_node
*next
;
424 raw_spin_lock(&devtree_lock
);
425 next
= prev
? prev
->sibling
: node
->child
;
426 for (; next
; next
= next
->sibling
) {
427 if (!__of_device_is_available(next
))
429 if (of_node_get(next
))
433 raw_spin_unlock(&devtree_lock
);
436 EXPORT_SYMBOL(of_get_next_available_child
);
439 * of_get_child_by_name - Find the child node by name for a given parent
441 * @name: child name to look for.
443 * This function looks for child node for given matching name
445 * Returns a node pointer if found, with refcount incremented, use
446 * of_node_put() on it when done.
447 * Returns NULL if node is not found.
449 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
452 struct device_node
*child
;
454 for_each_child_of_node(node
, child
)
455 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
459 EXPORT_SYMBOL(of_get_child_by_name
);
462 * of_find_node_by_path - Find a node matching a full OF path
463 * @path: The full path to match
465 * Returns a node pointer with refcount incremented, use
466 * of_node_put() on it when done.
468 struct device_node
*of_find_node_by_path(const char *path
)
470 struct device_node
*np
= of_allnodes
;
473 raw_spin_lock_irqsave(&devtree_lock
, flags
);
474 for (; np
; np
= np
->allnext
) {
475 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
479 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
482 EXPORT_SYMBOL(of_find_node_by_path
);
485 * of_find_node_by_name - Find a node by its "name" property
486 * @from: The node to start searching from or NULL, the node
487 * you pass will not be searched, only the next one
488 * will; typically, you pass what the previous call
489 * returned. of_node_put() will be called on it
490 * @name: The name string to match against
492 * Returns a node pointer with refcount incremented, use
493 * of_node_put() on it when done.
495 struct device_node
*of_find_node_by_name(struct device_node
*from
,
498 struct device_node
*np
;
501 raw_spin_lock_irqsave(&devtree_lock
, flags
);
502 np
= from
? from
->allnext
: of_allnodes
;
503 for (; np
; np
= np
->allnext
)
504 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
508 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
511 EXPORT_SYMBOL(of_find_node_by_name
);
514 * of_find_node_by_type - Find a node by its "device_type" property
515 * @from: The node to start searching from, or NULL to start searching
516 * the entire device tree. The node you pass will not be
517 * searched, only the next one will; typically, you pass
518 * what the previous call returned. of_node_put() will be
519 * called on from for you.
520 * @type: The type string to match against
522 * Returns a node pointer with refcount incremented, use
523 * of_node_put() on it when done.
525 struct device_node
*of_find_node_by_type(struct device_node
*from
,
528 struct device_node
*np
;
531 raw_spin_lock_irqsave(&devtree_lock
, flags
);
532 np
= from
? from
->allnext
: of_allnodes
;
533 for (; np
; np
= np
->allnext
)
534 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
538 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
541 EXPORT_SYMBOL(of_find_node_by_type
);
544 * of_find_compatible_node - Find a node based on type and one of the
545 * tokens in its "compatible" property
546 * @from: The node to start searching from or NULL, the node
547 * you pass will not be searched, only the next one
548 * will; typically, you pass what the previous call
549 * returned. of_node_put() will be called on it
550 * @type: The type string to match "device_type" or NULL to ignore
551 * @compatible: The string to match to one of the tokens in the device
554 * Returns a node pointer with refcount incremented, use
555 * of_node_put() on it when done.
557 struct device_node
*of_find_compatible_node(struct device_node
*from
,
558 const char *type
, const char *compatible
)
560 struct device_node
*np
;
563 raw_spin_lock_irqsave(&devtree_lock
, flags
);
564 np
= from
? from
->allnext
: of_allnodes
;
565 for (; np
; np
= np
->allnext
) {
567 && !(np
->type
&& (of_node_cmp(np
->type
, type
) == 0)))
569 if (__of_device_is_compatible(np
, compatible
) &&
574 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
577 EXPORT_SYMBOL(of_find_compatible_node
);
580 * of_find_node_with_property - Find a node which has a property with
582 * @from: The node to start searching from or NULL, the node
583 * you pass will not be searched, only the next one
584 * will; typically, you pass what the previous call
585 * returned. of_node_put() will be called on it
586 * @prop_name: The name of the property to look for.
588 * Returns a node pointer with refcount incremented, use
589 * of_node_put() on it when done.
591 struct device_node
*of_find_node_with_property(struct device_node
*from
,
592 const char *prop_name
)
594 struct device_node
*np
;
598 raw_spin_lock_irqsave(&devtree_lock
, flags
);
599 np
= from
? from
->allnext
: of_allnodes
;
600 for (; np
; np
= np
->allnext
) {
601 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
602 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
610 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
613 EXPORT_SYMBOL(of_find_node_with_property
);
616 const struct of_device_id
*__of_match_node(const struct of_device_id
*matches
,
617 const struct device_node
*node
)
622 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
624 if (matches
->name
[0])
626 && !strcmp(matches
->name
, node
->name
);
627 if (matches
->type
[0])
629 && !strcmp(matches
->type
, node
->type
);
630 if (matches
->compatible
[0])
631 match
&= __of_device_is_compatible(node
,
632 matches
->compatible
);
641 * of_match_node - Tell if an device_node has a matching of_match structure
642 * @matches: array of of device match structures to search in
643 * @node: the of device structure to match against
645 * Low level utility function used by device matching.
647 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
648 const struct device_node
*node
)
650 const struct of_device_id
*match
;
653 raw_spin_lock_irqsave(&devtree_lock
, flags
);
654 match
= __of_match_node(matches
, node
);
655 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
658 EXPORT_SYMBOL(of_match_node
);
661 * of_find_matching_node_and_match - Find a node based on an of_device_id
663 * @from: The node to start searching from or NULL, the node
664 * you pass will not be searched, only the next one
665 * will; typically, you pass what the previous call
666 * returned. of_node_put() will be called on it
667 * @matches: array of of device match structures to search in
668 * @match Updated to point at the matches entry which matched
670 * Returns a node pointer with refcount incremented, use
671 * of_node_put() on it when done.
673 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
674 const struct of_device_id
*matches
,
675 const struct of_device_id
**match
)
677 struct device_node
*np
;
678 const struct of_device_id
*m
;
684 raw_spin_lock_irqsave(&devtree_lock
, flags
);
685 np
= from
? from
->allnext
: of_allnodes
;
686 for (; np
; np
= np
->allnext
) {
687 m
= __of_match_node(matches
, np
);
688 if (m
&& of_node_get(np
)) {
695 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
698 EXPORT_SYMBOL(of_find_matching_node_and_match
);
701 * of_modalias_node - Lookup appropriate modalias for a device node
702 * @node: pointer to a device tree node
703 * @modalias: Pointer to buffer that modalias value will be copied into
704 * @len: Length of modalias value
706 * Based on the value of the compatible property, this routine will attempt
707 * to choose an appropriate modalias value for a particular device tree node.
708 * It does this by stripping the manufacturer prefix (as delimited by a ',')
709 * from the first entry in the compatible list property.
711 * This routine returns 0 on success, <0 on failure.
713 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
715 const char *compatible
, *p
;
718 compatible
= of_get_property(node
, "compatible", &cplen
);
719 if (!compatible
|| strlen(compatible
) > cplen
)
721 p
= strchr(compatible
, ',');
722 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
725 EXPORT_SYMBOL_GPL(of_modalias_node
);
728 * of_find_node_by_phandle - Find a node given a phandle
729 * @handle: phandle of the node to find
731 * Returns a node pointer with refcount incremented, use
732 * of_node_put() on it when done.
734 struct device_node
*of_find_node_by_phandle(phandle handle
)
736 struct device_node
*np
;
738 raw_spin_lock(&devtree_lock
);
739 for (np
= of_allnodes
; np
; np
= np
->allnext
)
740 if (np
->phandle
== handle
)
743 raw_spin_unlock(&devtree_lock
);
746 EXPORT_SYMBOL(of_find_node_by_phandle
);
749 * of_property_read_u8_array - Find and read an array of u8 from a property.
751 * @np: device node from which the property value is to be read.
752 * @propname: name of the property to be searched.
753 * @out_value: pointer to return value, modified only if return value is 0.
754 * @sz: number of array elements to read
756 * Search for a property in a device node and read 8-bit value(s) from
757 * it. Returns 0 on success, -EINVAL if the property does not exist,
758 * -ENODATA if property does not have a value, and -EOVERFLOW if the
759 * property data isn't large enough.
761 * dts entry of array should be like:
762 * property = /bits/ 8 <0x50 0x60 0x70>;
764 * The out_value is modified only if a valid u8 value can be decoded.
766 int of_property_read_u8_array(const struct device_node
*np
,
767 const char *propname
, u8
*out_values
, size_t sz
)
769 struct property
*prop
= of_find_property(np
, propname
, NULL
);
776 if ((sz
* sizeof(*out_values
)) > prop
->length
)
781 *out_values
++ = *val
++;
784 EXPORT_SYMBOL_GPL(of_property_read_u8_array
);
787 * of_property_read_u16_array - Find and read an array of u16 from a property.
789 * @np: device node from which the property value is to be read.
790 * @propname: name of the property to be searched.
791 * @out_value: pointer to return value, modified only if return value is 0.
792 * @sz: number of array elements to read
794 * Search for a property in a device node and read 16-bit value(s) from
795 * it. Returns 0 on success, -EINVAL if the property does not exist,
796 * -ENODATA if property does not have a value, and -EOVERFLOW if the
797 * property data isn't large enough.
799 * dts entry of array should be like:
800 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
802 * The out_value is modified only if a valid u16 value can be decoded.
804 int of_property_read_u16_array(const struct device_node
*np
,
805 const char *propname
, u16
*out_values
, size_t sz
)
807 struct property
*prop
= of_find_property(np
, propname
, NULL
);
814 if ((sz
* sizeof(*out_values
)) > prop
->length
)
819 *out_values
++ = be16_to_cpup(val
++);
822 EXPORT_SYMBOL_GPL(of_property_read_u16_array
);
825 * of_property_read_u32_array - Find and read an array of 32 bit integers
828 * @np: device node from which the property value is to be read.
829 * @propname: name of the property to be searched.
830 * @out_value: pointer to return value, modified only if return value is 0.
831 * @sz: number of array elements to read
833 * Search for a property in a device node and read 32-bit value(s) from
834 * it. Returns 0 on success, -EINVAL if the property does not exist,
835 * -ENODATA if property does not have a value, and -EOVERFLOW if the
836 * property data isn't large enough.
838 * The out_value is modified only if a valid u32 value can be decoded.
840 int of_property_read_u32_array(const struct device_node
*np
,
841 const char *propname
, u32
*out_values
,
844 struct property
*prop
= of_find_property(np
, propname
, NULL
);
851 if ((sz
* sizeof(*out_values
)) > prop
->length
)
856 *out_values
++ = be32_to_cpup(val
++);
859 EXPORT_SYMBOL_GPL(of_property_read_u32_array
);
862 * of_property_read_u64 - Find and read a 64 bit integer from a property
863 * @np: device node from which the property value is to be read.
864 * @propname: name of the property to be searched.
865 * @out_value: pointer to return value, modified only if return value is 0.
867 * Search for a property in a device node and read a 64-bit value from
868 * it. Returns 0 on success, -EINVAL if the property does not exist,
869 * -ENODATA if property does not have a value, and -EOVERFLOW if the
870 * property data isn't large enough.
872 * The out_value is modified only if a valid u64 value can be decoded.
874 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
877 struct property
*prop
= of_find_property(np
, propname
, NULL
);
883 if (sizeof(*out_value
) > prop
->length
)
885 *out_value
= of_read_number(prop
->value
, 2);
888 EXPORT_SYMBOL_GPL(of_property_read_u64
);
891 * of_property_read_string - Find and read a string from a property
892 * @np: device node from which the property value is to be read.
893 * @propname: name of the property to be searched.
894 * @out_string: pointer to null terminated return string, modified only if
897 * Search for a property in a device tree node and retrieve a null
898 * terminated string value (pointer to data, not a copy). Returns 0 on
899 * success, -EINVAL if the property does not exist, -ENODATA if property
900 * does not have a value, and -EILSEQ if the string is not null-terminated
901 * within the length of the property data.
903 * The out_string pointer is modified only if a valid string can be decoded.
905 int of_property_read_string(struct device_node
*np
, const char *propname
,
906 const char **out_string
)
908 struct property
*prop
= of_find_property(np
, propname
, NULL
);
913 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
915 *out_string
= prop
->value
;
918 EXPORT_SYMBOL_GPL(of_property_read_string
);
921 * of_property_read_string_index - Find and read a string from a multiple
923 * @np: device node from which the property value is to be read.
924 * @propname: name of the property to be searched.
925 * @index: index of the string in the list of strings
926 * @out_string: pointer to null terminated return string, modified only if
929 * Search for a property in a device tree node and retrieve a null
930 * terminated string value (pointer to data, not a copy) in the list of strings
931 * contained in that property.
932 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
933 * property does not have a value, and -EILSEQ if the string is not
934 * null-terminated within the length of the property data.
936 * The out_string pointer is modified only if a valid string can be decoded.
938 int of_property_read_string_index(struct device_node
*np
, const char *propname
,
939 int index
, const char **output
)
941 struct property
*prop
= of_find_property(np
, propname
, NULL
);
943 size_t l
= 0, total
= 0;
950 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
955 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
) {
964 EXPORT_SYMBOL_GPL(of_property_read_string_index
);
967 * of_property_match_string() - Find string in a list and return index
968 * @np: pointer to node containing string list property
969 * @propname: string list property name
970 * @string: pointer to string to search for in string list
972 * This function searches a string list property and returns the index
973 * of a specific string value.
975 int of_property_match_string(struct device_node
*np
, const char *propname
,
978 struct property
*prop
= of_find_property(np
, propname
, NULL
);
989 end
= p
+ prop
->length
;
991 for (i
= 0; p
< end
; i
++, p
+= l
) {
995 pr_debug("comparing %s with %s\n", string
, p
);
996 if (strcmp(string
, p
) == 0)
997 return i
; /* Found it; return index */
1001 EXPORT_SYMBOL_GPL(of_property_match_string
);
1004 * of_property_count_strings - Find and return the number of strings from a
1005 * multiple strings property.
1006 * @np: device node from which the property value is to be read.
1007 * @propname: name of the property to be searched.
1009 * Search for a property in a device tree node and retrieve the number of null
1010 * terminated string contain in it. Returns the number of strings on
1011 * success, -EINVAL if the property does not exist, -ENODATA if property
1012 * does not have a value, and -EILSEQ if the string is not null-terminated
1013 * within the length of the property data.
1015 int of_property_count_strings(struct device_node
*np
, const char *propname
)
1017 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1019 size_t l
= 0, total
= 0;
1026 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1031 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
, i
++)
1036 EXPORT_SYMBOL_GPL(of_property_count_strings
);
1039 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1040 * @np: Pointer to device node holding phandle property
1041 * @phandle_name: Name of property holding a phandle value
1042 * @index: For properties holding a table of phandles, this is the index into
1045 * Returns the device_node pointer with refcount incremented. Use
1046 * of_node_put() on it when done.
1048 struct device_node
*of_parse_phandle(const struct device_node
*np
,
1049 const char *phandle_name
, int index
)
1051 const __be32
*phandle
;
1054 phandle
= of_get_property(np
, phandle_name
, &size
);
1055 if ((!phandle
) || (size
< sizeof(*phandle
) * (index
+ 1)))
1058 return of_find_node_by_phandle(be32_to_cpup(phandle
+ index
));
1060 EXPORT_SYMBOL(of_parse_phandle
);
1063 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1064 * @np: pointer to a device tree node containing a list
1065 * @list_name: property name that contains a list
1066 * @cells_name: property name that specifies phandles' arguments count
1067 * @index: index of a phandle to parse out
1068 * @out_args: optional pointer to output arguments structure (will be filled)
1070 * This function is useful to parse lists of phandles and their arguments.
1071 * Returns 0 on success and fills out_args, on error returns appropriate
1074 * Caller is responsible to call of_node_put() on the returned out_args->node
1080 * #list-cells = <2>;
1084 * #list-cells = <1>;
1088 * list = <&phandle1 1 2 &phandle2 3>;
1091 * To get a device_node of the `node2' node you may call this:
1092 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1094 static int __of_parse_phandle_with_args(const struct device_node
*np
,
1095 const char *list_name
,
1096 const char *cells_name
, int index
,
1097 struct of_phandle_args
*out_args
)
1099 const __be32
*list
, *list_end
;
1100 int rc
= 0, size
, cur_index
= 0;
1102 struct device_node
*node
= NULL
;
1105 /* Retrieve the phandle list property */
1106 list
= of_get_property(np
, list_name
, &size
);
1109 list_end
= list
+ size
/ sizeof(*list
);
1111 /* Loop over the phandles until all the requested entry is found */
1112 while (list
< list_end
) {
1117 * If phandle is 0, then it is an empty entry with no
1118 * arguments. Skip forward to the next entry.
1120 phandle
= be32_to_cpup(list
++);
1123 * Find the provider node and parse the #*-cells
1124 * property to determine the argument length
1126 node
= of_find_node_by_phandle(phandle
);
1128 pr_err("%s: could not find phandle\n",
1132 if (of_property_read_u32(node
, cells_name
, &count
)) {
1133 pr_err("%s: could not get %s for %s\n",
1134 np
->full_name
, cells_name
,
1140 * Make sure that the arguments actually fit in the
1141 * remaining property data length
1143 if (list
+ count
> list_end
) {
1144 pr_err("%s: arguments longer than property\n",
1151 * All of the error cases above bail out of the loop, so at
1152 * this point, the parsing is successful. If the requested
1153 * index matches, then fill the out_args structure and return,
1154 * or return -ENOENT for an empty entry.
1157 if (cur_index
== index
) {
1163 if (WARN_ON(count
> MAX_PHANDLE_ARGS
))
1164 count
= MAX_PHANDLE_ARGS
;
1165 out_args
->np
= node
;
1166 out_args
->args_count
= count
;
1167 for (i
= 0; i
< count
; i
++)
1168 out_args
->args
[i
] = be32_to_cpup(list
++);
1171 /* Found it! return success */
1184 * Unlock node before returning result; will be one of:
1185 * -ENOENT : index is for empty phandle
1186 * -EINVAL : parsing error on data
1187 * [1..n] : Number of phandle (count mode; when index = -1)
1189 rc
= index
< 0 ? cur_index
: -ENOENT
;
1196 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1197 const char *cells_name
, int index
,
1198 struct of_phandle_args
*out_args
)
1202 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, index
, out_args
);
1204 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1207 * of_count_phandle_with_args() - Find the number of phandles references in a property
1208 * @np: pointer to a device tree node containing a list
1209 * @list_name: property name that contains a list
1210 * @cells_name: property name that specifies phandles' arguments count
1212 * Returns the number of phandle + argument tuples within a property. It
1213 * is a typical pattern to encode a list of phandle and variable
1214 * arguments into a single property. The number of arguments is encoded
1215 * by a property in the phandle-target node. For example, a gpios
1216 * property would contain a list of GPIO specifies consisting of a
1217 * phandle and 1 or more arguments. The number of arguments are
1218 * determined by the #gpio-cells property in the node pointed to by the
1221 int of_count_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1222 const char *cells_name
)
1224 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, -1, NULL
);
1226 EXPORT_SYMBOL(of_count_phandle_with_args
);
1228 #if defined(CONFIG_OF_DYNAMIC)
1229 static int of_property_notify(int action
, struct device_node
*np
,
1230 struct property
*prop
)
1232 struct of_prop_reconfig pr
;
1236 return of_reconfig_notify(action
, &pr
);
1239 static int of_property_notify(int action
, struct device_node
*np
,
1240 struct property
*prop
)
1247 * of_add_property - Add a property to a node
1249 int of_add_property(struct device_node
*np
, struct property
*prop
)
1251 struct property
**next
;
1252 unsigned long flags
;
1255 rc
= of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
);
1260 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1261 next
= &np
->properties
;
1263 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
1264 /* duplicate ! don't insert it */
1265 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1268 next
= &(*next
)->next
;
1271 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1273 #ifdef CONFIG_PROC_DEVICETREE
1274 /* try to add to proc as well if it was initialized */
1276 proc_device_tree_add_prop(np
->pde
, prop
);
1277 #endif /* CONFIG_PROC_DEVICETREE */
1283 * of_remove_property - Remove a property from a node.
1285 * Note that we don't actually remove it, since we have given out
1286 * who-knows-how-many pointers to the data using get-property.
1287 * Instead we just move the property to the "dead properties"
1288 * list, so it won't be found any more.
1290 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1292 struct property
**next
;
1293 unsigned long flags
;
1297 rc
= of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
);
1301 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1302 next
= &np
->properties
;
1304 if (*next
== prop
) {
1305 /* found the node */
1307 prop
->next
= np
->deadprops
;
1308 np
->deadprops
= prop
;
1312 next
= &(*next
)->next
;
1314 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1319 #ifdef CONFIG_PROC_DEVICETREE
1320 /* try to remove the proc node as well */
1322 proc_device_tree_remove_prop(np
->pde
, prop
);
1323 #endif /* CONFIG_PROC_DEVICETREE */
1329 * of_update_property - Update a property in a node, if the property does
1330 * not exist, add it.
1332 * Note that we don't actually remove it, since we have given out
1333 * who-knows-how-many pointers to the data using get-property.
1334 * Instead we just move the property to the "dead properties" list,
1335 * and add the new property to the property list
1337 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1339 struct property
**next
, *oldprop
;
1340 unsigned long flags
;
1343 rc
= of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
);
1350 oldprop
= of_find_property(np
, newprop
->name
, NULL
);
1352 return of_add_property(np
, newprop
);
1354 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1355 next
= &np
->properties
;
1357 if (*next
== oldprop
) {
1358 /* found the node */
1359 newprop
->next
= oldprop
->next
;
1361 oldprop
->next
= np
->deadprops
;
1362 np
->deadprops
= oldprop
;
1366 next
= &(*next
)->next
;
1368 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1373 #ifdef CONFIG_PROC_DEVICETREE
1374 /* try to add to proc as well if it was initialized */
1376 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
1377 #endif /* CONFIG_PROC_DEVICETREE */
1382 #if defined(CONFIG_OF_DYNAMIC)
1384 * Support for dynamic device trees.
1386 * On some platforms, the device tree can be manipulated at runtime.
1387 * The routines in this section support adding, removing and changing
1388 * device tree nodes.
1391 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain
);
1393 int of_reconfig_notifier_register(struct notifier_block
*nb
)
1395 return blocking_notifier_chain_register(&of_reconfig_chain
, nb
);
1397 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register
);
1399 int of_reconfig_notifier_unregister(struct notifier_block
*nb
)
1401 return blocking_notifier_chain_unregister(&of_reconfig_chain
, nb
);
1403 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister
);
1405 int of_reconfig_notify(unsigned long action
, void *p
)
1409 rc
= blocking_notifier_call_chain(&of_reconfig_chain
, action
, p
);
1410 return notifier_to_errno(rc
);
1413 #ifdef CONFIG_PROC_DEVICETREE
1414 static void of_add_proc_dt_entry(struct device_node
*dn
)
1416 struct proc_dir_entry
*ent
;
1418 ent
= proc_mkdir(strrchr(dn
->full_name
, '/') + 1, dn
->parent
->pde
);
1420 proc_device_tree_add_node(dn
, ent
);
1423 static void of_add_proc_dt_entry(struct device_node
*dn
)
1430 * of_attach_node - Plug a device node into the tree and global list.
1432 int of_attach_node(struct device_node
*np
)
1434 unsigned long flags
;
1437 rc
= of_reconfig_notify(OF_RECONFIG_ATTACH_NODE
, np
);
1441 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1442 np
->sibling
= np
->parent
->child
;
1443 np
->allnext
= of_allnodes
;
1444 np
->parent
->child
= np
;
1446 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1448 of_add_proc_dt_entry(np
);
1452 #ifdef CONFIG_PROC_DEVICETREE
1453 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1455 struct device_node
*parent
= dn
->parent
;
1456 struct property
*prop
= dn
->properties
;
1459 remove_proc_entry(prop
->name
, dn
->pde
);
1464 remove_proc_entry(dn
->pde
->name
, parent
->pde
);
1467 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1474 * of_detach_node - "Unplug" a node from the device tree.
1476 * The caller must hold a reference to the node. The memory associated with
1477 * the node is not freed until its refcount goes to zero.
1479 int of_detach_node(struct device_node
*np
)
1481 struct device_node
*parent
;
1482 unsigned long flags
;
1485 rc
= of_reconfig_notify(OF_RECONFIG_DETACH_NODE
, np
);
1489 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1491 if (of_node_check_flag(np
, OF_DETACHED
)) {
1492 /* someone already detached it */
1493 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1497 parent
= np
->parent
;
1499 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1503 if (of_allnodes
== np
)
1504 of_allnodes
= np
->allnext
;
1506 struct device_node
*prev
;
1507 for (prev
= of_allnodes
;
1508 prev
->allnext
!= np
;
1509 prev
= prev
->allnext
)
1511 prev
->allnext
= np
->allnext
;
1514 if (parent
->child
== np
)
1515 parent
->child
= np
->sibling
;
1517 struct device_node
*prevsib
;
1518 for (prevsib
= np
->parent
->child
;
1519 prevsib
->sibling
!= np
;
1520 prevsib
= prevsib
->sibling
)
1522 prevsib
->sibling
= np
->sibling
;
1525 of_node_set_flag(np
, OF_DETACHED
);
1526 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1528 of_remove_proc_dt_entry(np
);
1531 #endif /* defined(CONFIG_OF_DYNAMIC) */
1533 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1534 int id
, const char *stem
, int stem_len
)
1538 strncpy(ap
->stem
, stem
, stem_len
);
1539 ap
->stem
[stem_len
] = 0;
1540 list_add_tail(&ap
->link
, &aliases_lookup
);
1541 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1542 ap
->alias
, ap
->stem
, ap
->id
, of_node_full_name(np
));
1546 * of_alias_scan - Scan all properties of 'aliases' node
1548 * The function scans all the properties of 'aliases' node and populate
1549 * the the global lookup table with the properties. It returns the
1550 * number of alias_prop found, or error code in error case.
1552 * @dt_alloc: An allocator that provides a virtual address to memory
1553 * for the resulting tree
1555 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
1557 struct property
*pp
;
1559 of_chosen
= of_find_node_by_path("/chosen");
1560 if (of_chosen
== NULL
)
1561 of_chosen
= of_find_node_by_path("/chosen@0");
1562 of_aliases
= of_find_node_by_path("/aliases");
1566 for_each_property_of_node(of_aliases
, pp
) {
1567 const char *start
= pp
->name
;
1568 const char *end
= start
+ strlen(start
);
1569 struct device_node
*np
;
1570 struct alias_prop
*ap
;
1573 /* Skip those we do not want to proceed */
1574 if (!strcmp(pp
->name
, "name") ||
1575 !strcmp(pp
->name
, "phandle") ||
1576 !strcmp(pp
->name
, "linux,phandle"))
1579 np
= of_find_node_by_path(pp
->value
);
1583 /* walk the alias backwards to extract the id and work out
1584 * the 'stem' string */
1585 while (isdigit(*(end
-1)) && end
> start
)
1589 if (kstrtoint(end
, 10, &id
) < 0)
1592 /* Allocate an alias_prop with enough space for the stem */
1593 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, 4);
1597 of_alias_add(ap
, np
, id
, start
, len
);
1602 * of_alias_get_id - Get alias id for the given device_node
1603 * @np: Pointer to the given device_node
1604 * @stem: Alias stem of the given device_node
1606 * The function travels the lookup table to get alias id for the given
1607 * device_node and alias stem. It returns the alias id if find it.
1609 int of_alias_get_id(struct device_node
*np
, const char *stem
)
1611 struct alias_prop
*app
;
1614 mutex_lock(&of_aliases_mutex
);
1615 list_for_each_entry(app
, &aliases_lookup
, link
) {
1616 if (strcmp(app
->stem
, stem
) != 0)
1619 if (np
== app
->np
) {
1624 mutex_unlock(&of_aliases_mutex
);
1628 EXPORT_SYMBOL_GPL(of_alias_get_id
);
1630 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
1633 const void *curv
= cur
;
1643 curv
+= sizeof(*cur
);
1644 if (curv
>= prop
->value
+ prop
->length
)
1648 *pu
= be32_to_cpup(curv
);
1651 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
1653 const char *of_prop_next_string(struct property
*prop
, const char *cur
)
1655 const void *curv
= cur
;
1663 curv
+= strlen(cur
) + 1;
1664 if (curv
>= prop
->value
+ prop
->length
)
1669 EXPORT_SYMBOL_GPL(of_prop_next_string
);