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>
28 * struct alias_prop - Alias property in 'aliases' node
29 * @link: List node to link the structure in aliases_lookup list
30 * @alias: Alias property name
31 * @np: Pointer to device_node that the alias stands for
32 * @id: Index value from end of alias name
33 * @stem: Alias string without the index
35 * The structure represents one alias property of 'aliases' node as
36 * an entry in aliases_lookup list.
39 struct list_head link
;
41 struct device_node
*np
;
46 static LIST_HEAD(aliases_lookup
);
48 struct device_node
*of_allnodes
;
49 EXPORT_SYMBOL(of_allnodes
);
50 struct device_node
*of_chosen
;
51 struct device_node
*of_aliases
;
53 static DEFINE_MUTEX(of_aliases_mutex
);
55 /* use when traversing tree through the allnext, child, sibling,
56 * or parent members of struct device_node.
58 DEFINE_RWLOCK(devtree_lock
);
60 int of_n_addr_cells(struct device_node
*np
)
67 ip
= of_get_property(np
, "#address-cells", NULL
);
69 return be32_to_cpup(ip
);
71 /* No #address-cells property for the root node */
72 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
74 EXPORT_SYMBOL(of_n_addr_cells
);
76 int of_n_size_cells(struct device_node
*np
)
83 ip
= of_get_property(np
, "#size-cells", NULL
);
85 return be32_to_cpup(ip
);
87 /* No #size-cells property for the root node */
88 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
90 EXPORT_SYMBOL(of_n_size_cells
);
92 #if defined(CONFIG_OF_DYNAMIC)
94 * of_node_get - Increment refcount of a node
95 * @node: Node to inc refcount, NULL is supported to
96 * simplify writing of callers
100 struct device_node
*of_node_get(struct device_node
*node
)
103 kref_get(&node
->kref
);
106 EXPORT_SYMBOL(of_node_get
);
108 static inline struct device_node
*kref_to_device_node(struct kref
*kref
)
110 return container_of(kref
, struct device_node
, kref
);
114 * of_node_release - release a dynamically allocated node
115 * @kref: kref element of the node to be released
117 * In of_node_put() this function is passed to kref_put()
120 static void of_node_release(struct kref
*kref
)
122 struct device_node
*node
= kref_to_device_node(kref
);
123 struct property
*prop
= node
->properties
;
125 /* We should never be releasing nodes that haven't been detached. */
126 if (!of_node_check_flag(node
, OF_DETACHED
)) {
127 pr_err("ERROR: Bad of_node_put() on %s\n", node
->full_name
);
129 kref_init(&node
->kref
);
133 if (!of_node_check_flag(node
, OF_DYNAMIC
))
137 struct property
*next
= prop
->next
;
144 prop
= node
->deadprops
;
145 node
->deadprops
= NULL
;
148 kfree(node
->full_name
);
154 * of_node_put - Decrement refcount of a node
155 * @node: Node to dec refcount, NULL is supported to
156 * simplify writing of callers
159 void of_node_put(struct device_node
*node
)
162 kref_put(&node
->kref
, of_node_release
);
164 EXPORT_SYMBOL(of_node_put
);
165 #endif /* CONFIG_OF_DYNAMIC */
167 struct property
*of_find_property(const struct device_node
*np
,
176 read_lock(&devtree_lock
);
177 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
178 if (of_prop_cmp(pp
->name
, name
) == 0) {
184 read_unlock(&devtree_lock
);
188 EXPORT_SYMBOL(of_find_property
);
191 * of_find_all_nodes - Get next node in global list
192 * @prev: Previous node or NULL to start iteration
193 * of_node_put() will be called on it
195 * Returns a node pointer with refcount incremented, use
196 * of_node_put() on it when done.
198 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
200 struct device_node
*np
;
202 read_lock(&devtree_lock
);
203 np
= prev
? prev
->allnext
: of_allnodes
;
204 for (; np
!= NULL
; np
= np
->allnext
)
208 read_unlock(&devtree_lock
);
211 EXPORT_SYMBOL(of_find_all_nodes
);
214 * Find a property with a given name for a given node
215 * and return the value.
217 const void *of_get_property(const struct device_node
*np
, const char *name
,
220 struct property
*pp
= of_find_property(np
, name
, lenp
);
222 return pp
? pp
->value
: NULL
;
224 EXPORT_SYMBOL(of_get_property
);
226 /** Checks if the given "compat" string matches one of the strings in
227 * the device's "compatible" property
229 int of_device_is_compatible(const struct device_node
*device
,
235 cp
= of_get_property(device
, "compatible", &cplen
);
239 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
248 EXPORT_SYMBOL(of_device_is_compatible
);
251 * of_machine_is_compatible - Test root of device tree for a given compatible value
252 * @compat: compatible string to look for in root node's compatible property.
254 * Returns true if the root node has the given value in its
255 * compatible property.
257 int of_machine_is_compatible(const char *compat
)
259 struct device_node
*root
;
262 root
= of_find_node_by_path("/");
264 rc
= of_device_is_compatible(root
, compat
);
269 EXPORT_SYMBOL(of_machine_is_compatible
);
272 * of_device_is_available - check if a device is available for use
274 * @device: Node to check for availability
276 * Returns 1 if the status property is absent or set to "okay" or "ok",
279 int of_device_is_available(const struct device_node
*device
)
284 status
= of_get_property(device
, "status", &statlen
);
289 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
295 EXPORT_SYMBOL(of_device_is_available
);
298 * of_get_parent - Get a node's parent if any
299 * @node: Node to get parent
301 * Returns a node pointer with refcount incremented, use
302 * of_node_put() on it when done.
304 struct device_node
*of_get_parent(const struct device_node
*node
)
306 struct device_node
*np
;
311 read_lock(&devtree_lock
);
312 np
= of_node_get(node
->parent
);
313 read_unlock(&devtree_lock
);
316 EXPORT_SYMBOL(of_get_parent
);
319 * of_get_next_parent - Iterate to a node's parent
320 * @node: Node to get parent of
322 * This is like of_get_parent() except that it drops the
323 * refcount on the passed node, making it suitable for iterating
324 * through a node's parents.
326 * Returns a node pointer with refcount incremented, use
327 * of_node_put() on it when done.
329 struct device_node
*of_get_next_parent(struct device_node
*node
)
331 struct device_node
*parent
;
336 read_lock(&devtree_lock
);
337 parent
= of_node_get(node
->parent
);
339 read_unlock(&devtree_lock
);
344 * of_get_next_child - Iterate a node childs
346 * @prev: previous child of the parent node, or NULL to get first
348 * Returns a node pointer with refcount incremented, use
349 * of_node_put() on it when done.
351 struct device_node
*of_get_next_child(const struct device_node
*node
,
352 struct device_node
*prev
)
354 struct device_node
*next
;
356 read_lock(&devtree_lock
);
357 next
= prev
? prev
->sibling
: node
->child
;
358 for (; next
; next
= next
->sibling
)
359 if (of_node_get(next
))
362 read_unlock(&devtree_lock
);
365 EXPORT_SYMBOL(of_get_next_child
);
368 * of_get_next_available_child - Find the next available child node
370 * @prev: previous child of the parent node, or NULL to get first
372 * This function is like of_get_next_child(), except that it
373 * automatically skips any disabled nodes (i.e. status = "disabled").
375 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
376 struct device_node
*prev
)
378 struct device_node
*next
;
380 read_lock(&devtree_lock
);
381 next
= prev
? prev
->sibling
: node
->child
;
382 for (; next
; next
= next
->sibling
) {
383 if (!of_device_is_available(next
))
385 if (of_node_get(next
))
389 read_unlock(&devtree_lock
);
392 EXPORT_SYMBOL(of_get_next_available_child
);
395 * of_get_child_by_name - Find the child node by name for a given parent
397 * @name: child name to look for.
399 * This function looks for child node for given matching name
401 * Returns a node pointer if found, with refcount incremented, use
402 * of_node_put() on it when done.
403 * Returns NULL if node is not found.
405 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
408 struct device_node
*child
;
410 for_each_child_of_node(node
, child
)
411 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
415 EXPORT_SYMBOL(of_get_child_by_name
);
418 * of_find_node_by_path - Find a node matching a full OF path
419 * @path: The full path to match
421 * Returns a node pointer with refcount incremented, use
422 * of_node_put() on it when done.
424 struct device_node
*of_find_node_by_path(const char *path
)
426 struct device_node
*np
= of_allnodes
;
428 read_lock(&devtree_lock
);
429 for (; np
; np
= np
->allnext
) {
430 if (np
->full_name
&& (of_node_cmp(np
->full_name
, path
) == 0)
434 read_unlock(&devtree_lock
);
437 EXPORT_SYMBOL(of_find_node_by_path
);
440 * of_find_node_by_name - Find a node by its "name" property
441 * @from: The node to start searching from or NULL, the node
442 * you pass will not be searched, only the next one
443 * will; typically, you pass what the previous call
444 * returned. of_node_put() will be called on it
445 * @name: The name string to match against
447 * Returns a node pointer with refcount incremented, use
448 * of_node_put() on it when done.
450 struct device_node
*of_find_node_by_name(struct device_node
*from
,
453 struct device_node
*np
;
455 read_lock(&devtree_lock
);
456 np
= from
? from
->allnext
: of_allnodes
;
457 for (; np
; np
= np
->allnext
)
458 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
462 read_unlock(&devtree_lock
);
465 EXPORT_SYMBOL(of_find_node_by_name
);
468 * of_find_node_by_type - Find a node by its "device_type" property
469 * @from: The node to start searching from, or NULL to start searching
470 * the entire device tree. The node you pass will not be
471 * searched, only the next one will; typically, you pass
472 * what the previous call returned. of_node_put() will be
473 * called on from for you.
474 * @type: The type string to match against
476 * Returns a node pointer with refcount incremented, use
477 * of_node_put() on it when done.
479 struct device_node
*of_find_node_by_type(struct device_node
*from
,
482 struct device_node
*np
;
484 read_lock(&devtree_lock
);
485 np
= from
? from
->allnext
: of_allnodes
;
486 for (; np
; np
= np
->allnext
)
487 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
491 read_unlock(&devtree_lock
);
494 EXPORT_SYMBOL(of_find_node_by_type
);
497 * of_find_compatible_node - Find a node based on type and one of the
498 * tokens in its "compatible" property
499 * @from: The node to start searching from or NULL, the node
500 * you pass will not be searched, only the next one
501 * will; typically, you pass what the previous call
502 * returned. of_node_put() will be called on it
503 * @type: The type string to match "device_type" or NULL to ignore
504 * @compatible: The string to match to one of the tokens in the device
507 * Returns a node pointer with refcount incremented, use
508 * of_node_put() on it when done.
510 struct device_node
*of_find_compatible_node(struct device_node
*from
,
511 const char *type
, const char *compatible
)
513 struct device_node
*np
;
515 read_lock(&devtree_lock
);
516 np
= from
? from
->allnext
: of_allnodes
;
517 for (; np
; np
= np
->allnext
) {
519 && !(np
->type
&& (of_node_cmp(np
->type
, type
) == 0)))
521 if (of_device_is_compatible(np
, compatible
) && of_node_get(np
))
525 read_unlock(&devtree_lock
);
528 EXPORT_SYMBOL(of_find_compatible_node
);
531 * of_find_node_with_property - Find a node which has a property with
533 * @from: The node to start searching from or NULL, the node
534 * you pass will not be searched, only the next one
535 * will; typically, you pass what the previous call
536 * returned. of_node_put() will be called on it
537 * @prop_name: The name of the property to look for.
539 * Returns a node pointer with refcount incremented, use
540 * of_node_put() on it when done.
542 struct device_node
*of_find_node_with_property(struct device_node
*from
,
543 const char *prop_name
)
545 struct device_node
*np
;
548 read_lock(&devtree_lock
);
549 np
= from
? from
->allnext
: of_allnodes
;
550 for (; np
; np
= np
->allnext
) {
551 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
552 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
560 read_unlock(&devtree_lock
);
563 EXPORT_SYMBOL(of_find_node_with_property
);
566 * of_match_node - Tell if an device_node has a matching of_match structure
567 * @matches: array of of device match structures to search in
568 * @node: the of device structure to match against
570 * Low level utility function used by device matching.
572 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
573 const struct device_node
*node
)
578 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
580 if (matches
->name
[0])
582 && !strcmp(matches
->name
, node
->name
);
583 if (matches
->type
[0])
585 && !strcmp(matches
->type
, node
->type
);
586 if (matches
->compatible
[0])
587 match
&= of_device_is_compatible(node
,
588 matches
->compatible
);
595 EXPORT_SYMBOL(of_match_node
);
598 * of_find_matching_node_and_match - Find a node based on an of_device_id
600 * @from: The node to start searching from or NULL, the node
601 * you pass will not be searched, only the next one
602 * will; typically, you pass what the previous call
603 * returned. of_node_put() will be called on it
604 * @matches: array of of device match structures to search in
605 * @match Updated to point at the matches entry which matched
607 * Returns a node pointer with refcount incremented, use
608 * of_node_put() on it when done.
610 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
611 const struct of_device_id
*matches
,
612 const struct of_device_id
**match
)
614 struct device_node
*np
;
619 read_lock(&devtree_lock
);
620 np
= from
? from
->allnext
: of_allnodes
;
621 for (; np
; np
= np
->allnext
) {
622 if (of_match_node(matches
, np
) && of_node_get(np
)) {
629 read_unlock(&devtree_lock
);
632 EXPORT_SYMBOL(of_find_matching_node_and_match
);
635 * of_modalias_node - Lookup appropriate modalias for a device node
636 * @node: pointer to a device tree node
637 * @modalias: Pointer to buffer that modalias value will be copied into
638 * @len: Length of modalias value
640 * Based on the value of the compatible property, this routine will attempt
641 * to choose an appropriate modalias value for a particular device tree node.
642 * It does this by stripping the manufacturer prefix (as delimited by a ',')
643 * from the first entry in the compatible list property.
645 * This routine returns 0 on success, <0 on failure.
647 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
649 const char *compatible
, *p
;
652 compatible
= of_get_property(node
, "compatible", &cplen
);
653 if (!compatible
|| strlen(compatible
) > cplen
)
655 p
= strchr(compatible
, ',');
656 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
659 EXPORT_SYMBOL_GPL(of_modalias_node
);
662 * of_find_node_by_phandle - Find a node given a phandle
663 * @handle: phandle of the node to find
665 * Returns a node pointer with refcount incremented, use
666 * of_node_put() on it when done.
668 struct device_node
*of_find_node_by_phandle(phandle handle
)
670 struct device_node
*np
;
672 read_lock(&devtree_lock
);
673 for (np
= of_allnodes
; np
; np
= np
->allnext
)
674 if (np
->phandle
== handle
)
677 read_unlock(&devtree_lock
);
680 EXPORT_SYMBOL(of_find_node_by_phandle
);
683 * of_property_read_u8_array - Find and read an array of u8 from a property.
685 * @np: device node from which the property value is to be read.
686 * @propname: name of the property to be searched.
687 * @out_value: pointer to return value, modified only if return value is 0.
688 * @sz: number of array elements to read
690 * Search for a property in a device node and read 8-bit value(s) from
691 * it. Returns 0 on success, -EINVAL if the property does not exist,
692 * -ENODATA if property does not have a value, and -EOVERFLOW if the
693 * property data isn't large enough.
695 * dts entry of array should be like:
696 * property = /bits/ 8 <0x50 0x60 0x70>;
698 * The out_value is modified only if a valid u8 value can be decoded.
700 int of_property_read_u8_array(const struct device_node
*np
,
701 const char *propname
, u8
*out_values
, size_t sz
)
703 struct property
*prop
= of_find_property(np
, propname
, NULL
);
710 if ((sz
* sizeof(*out_values
)) > prop
->length
)
715 *out_values
++ = *val
++;
718 EXPORT_SYMBOL_GPL(of_property_read_u8_array
);
721 * of_property_read_u16_array - Find and read an array of u16 from a property.
723 * @np: device node from which the property value is to be read.
724 * @propname: name of the property to be searched.
725 * @out_value: pointer to return value, modified only if return value is 0.
726 * @sz: number of array elements to read
728 * Search for a property in a device node and read 16-bit value(s) from
729 * it. Returns 0 on success, -EINVAL if the property does not exist,
730 * -ENODATA if property does not have a value, and -EOVERFLOW if the
731 * property data isn't large enough.
733 * dts entry of array should be like:
734 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
736 * The out_value is modified only if a valid u16 value can be decoded.
738 int of_property_read_u16_array(const struct device_node
*np
,
739 const char *propname
, u16
*out_values
, size_t sz
)
741 struct property
*prop
= of_find_property(np
, propname
, NULL
);
748 if ((sz
* sizeof(*out_values
)) > prop
->length
)
753 *out_values
++ = be16_to_cpup(val
++);
756 EXPORT_SYMBOL_GPL(of_property_read_u16_array
);
759 * of_property_read_u32_array - Find and read an array of 32 bit integers
762 * @np: device node from which the property value is to be read.
763 * @propname: name of the property to be searched.
764 * @out_value: pointer to return value, modified only if return value is 0.
765 * @sz: number of array elements to read
767 * Search for a property in a device node and read 32-bit value(s) from
768 * it. Returns 0 on success, -EINVAL if the property does not exist,
769 * -ENODATA if property does not have a value, and -EOVERFLOW if the
770 * property data isn't large enough.
772 * The out_value is modified only if a valid u32 value can be decoded.
774 int of_property_read_u32_array(const struct device_node
*np
,
775 const char *propname
, u32
*out_values
,
778 struct property
*prop
= of_find_property(np
, propname
, NULL
);
785 if ((sz
* sizeof(*out_values
)) > prop
->length
)
790 *out_values
++ = be32_to_cpup(val
++);
793 EXPORT_SYMBOL_GPL(of_property_read_u32_array
);
796 * of_property_read_u64 - Find and read a 64 bit integer from a property
797 * @np: device node from which the property value is to be read.
798 * @propname: name of the property to be searched.
799 * @out_value: pointer to return value, modified only if return value is 0.
801 * Search for a property in a device node and read a 64-bit value from
802 * it. Returns 0 on success, -EINVAL if the property does not exist,
803 * -ENODATA if property does not have a value, and -EOVERFLOW if the
804 * property data isn't large enough.
806 * The out_value is modified only if a valid u64 value can be decoded.
808 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
811 struct property
*prop
= of_find_property(np
, propname
, NULL
);
817 if (sizeof(*out_value
) > prop
->length
)
819 *out_value
= of_read_number(prop
->value
, 2);
822 EXPORT_SYMBOL_GPL(of_property_read_u64
);
825 * of_property_read_string - Find and read a string from a property
826 * @np: device node from which the property value is to be read.
827 * @propname: name of the property to be searched.
828 * @out_string: pointer to null terminated return string, modified only if
831 * Search for a property in a device tree node and retrieve a null
832 * terminated string value (pointer to data, not a copy). Returns 0 on
833 * success, -EINVAL if the property does not exist, -ENODATA if property
834 * does not have a value, and -EILSEQ if the string is not null-terminated
835 * within the length of the property data.
837 * The out_string pointer is modified only if a valid string can be decoded.
839 int of_property_read_string(struct device_node
*np
, const char *propname
,
840 const char **out_string
)
842 struct property
*prop
= of_find_property(np
, propname
, NULL
);
847 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
849 *out_string
= prop
->value
;
852 EXPORT_SYMBOL_GPL(of_property_read_string
);
855 * of_property_read_string_index - Find and read a string from a multiple
857 * @np: device node from which the property value is to be read.
858 * @propname: name of the property to be searched.
859 * @index: index of the string in the list of strings
860 * @out_string: pointer to null terminated return string, modified only if
863 * Search for a property in a device tree node and retrieve a null
864 * terminated string value (pointer to data, not a copy) in the list of strings
865 * contained in that property.
866 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
867 * property does not have a value, and -EILSEQ if the string is not
868 * null-terminated within the length of the property data.
870 * The out_string pointer is modified only if a valid string can be decoded.
872 int of_property_read_string_index(struct device_node
*np
, const char *propname
,
873 int index
, const char **output
)
875 struct property
*prop
= of_find_property(np
, propname
, NULL
);
877 size_t l
= 0, total
= 0;
884 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
889 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
) {
898 EXPORT_SYMBOL_GPL(of_property_read_string_index
);
901 * of_property_match_string() - Find string in a list and return index
902 * @np: pointer to node containing string list property
903 * @propname: string list property name
904 * @string: pointer to string to search for in string list
906 * This function searches a string list property and returns the index
907 * of a specific string value.
909 int of_property_match_string(struct device_node
*np
, const char *propname
,
912 struct property
*prop
= of_find_property(np
, propname
, NULL
);
923 end
= p
+ prop
->length
;
925 for (i
= 0; p
< end
; i
++, p
+= l
) {
929 pr_debug("comparing %s with %s\n", string
, p
);
930 if (strcmp(string
, p
) == 0)
931 return i
; /* Found it; return index */
935 EXPORT_SYMBOL_GPL(of_property_match_string
);
938 * of_property_count_strings - Find and return the number of strings from a
939 * multiple strings property.
940 * @np: device node from which the property value is to be read.
941 * @propname: name of the property to be searched.
943 * Search for a property in a device tree node and retrieve the number of null
944 * terminated string contain in it. Returns the number of strings on
945 * success, -EINVAL if the property does not exist, -ENODATA if property
946 * does not have a value, and -EILSEQ if the string is not null-terminated
947 * within the length of the property data.
949 int of_property_count_strings(struct device_node
*np
, const char *propname
)
951 struct property
*prop
= of_find_property(np
, propname
, NULL
);
953 size_t l
= 0, total
= 0;
960 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
965 for (i
= 0; total
< prop
->length
; total
+= l
, p
+= l
, i
++)
970 EXPORT_SYMBOL_GPL(of_property_count_strings
);
973 * of_parse_phandle - Resolve a phandle property to a device_node pointer
974 * @np: Pointer to device node holding phandle property
975 * @phandle_name: Name of property holding a phandle value
976 * @index: For properties holding a table of phandles, this is the index into
979 * Returns the device_node pointer with refcount incremented. Use
980 * of_node_put() on it when done.
982 struct device_node
*of_parse_phandle(const struct device_node
*np
,
983 const char *phandle_name
, int index
)
985 const __be32
*phandle
;
988 phandle
= of_get_property(np
, phandle_name
, &size
);
989 if ((!phandle
) || (size
< sizeof(*phandle
) * (index
+ 1)))
992 return of_find_node_by_phandle(be32_to_cpup(phandle
+ index
));
994 EXPORT_SYMBOL(of_parse_phandle
);
997 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
998 * @np: pointer to a device tree node containing a list
999 * @list_name: property name that contains a list
1000 * @cells_name: property name that specifies phandles' arguments count
1001 * @index: index of a phandle to parse out
1002 * @out_args: optional pointer to output arguments structure (will be filled)
1004 * This function is useful to parse lists of phandles and their arguments.
1005 * Returns 0 on success and fills out_args, on error returns appropriate
1008 * Caller is responsible to call of_node_put() on the returned out_args->node
1014 * #list-cells = <2>;
1018 * #list-cells = <1>;
1022 * list = <&phandle1 1 2 &phandle2 3>;
1025 * To get a device_node of the `node2' node you may call this:
1026 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1028 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1029 const char *cells_name
, int index
,
1030 struct of_phandle_args
*out_args
)
1032 const __be32
*list
, *list_end
;
1033 int size
, cur_index
= 0;
1035 struct device_node
*node
= NULL
;
1038 /* Retrieve the phandle list property */
1039 list
= of_get_property(np
, list_name
, &size
);
1042 list_end
= list
+ size
/ sizeof(*list
);
1044 /* Loop over the phandles until all the requested entry is found */
1045 while (list
< list_end
) {
1049 * If phandle is 0, then it is an empty entry with no
1050 * arguments. Skip forward to the next entry.
1052 phandle
= be32_to_cpup(list
++);
1055 * Find the provider node and parse the #*-cells
1056 * property to determine the argument length
1058 node
= of_find_node_by_phandle(phandle
);
1060 pr_err("%s: could not find phandle\n",
1064 if (of_property_read_u32(node
, cells_name
, &count
)) {
1065 pr_err("%s: could not get %s for %s\n",
1066 np
->full_name
, cells_name
,
1072 * Make sure that the arguments actually fit in the
1073 * remaining property data length
1075 if (list
+ count
> list_end
) {
1076 pr_err("%s: arguments longer than property\n",
1083 * All of the error cases above bail out of the loop, so at
1084 * this point, the parsing is successful. If the requested
1085 * index matches, then fill the out_args structure and return,
1086 * or return -ENOENT for an empty entry.
1088 if (cur_index
== index
) {
1094 if (WARN_ON(count
> MAX_PHANDLE_ARGS
))
1095 count
= MAX_PHANDLE_ARGS
;
1096 out_args
->np
= node
;
1097 out_args
->args_count
= count
;
1098 for (i
= 0; i
< count
; i
++)
1099 out_args
->args
[i
] = be32_to_cpup(list
++);
1110 /* Loop exited without finding a valid entry; return an error */
1115 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1117 #if defined(CONFIG_OF_DYNAMIC)
1118 static int of_property_notify(int action
, struct device_node
*np
,
1119 struct property
*prop
)
1121 struct of_prop_reconfig pr
;
1125 return of_reconfig_notify(action
, &pr
);
1128 static int of_property_notify(int action
, struct device_node
*np
,
1129 struct property
*prop
)
1136 * of_add_property - Add a property to a node
1138 int of_add_property(struct device_node
*np
, struct property
*prop
)
1140 struct property
**next
;
1141 unsigned long flags
;
1144 rc
= of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
);
1149 write_lock_irqsave(&devtree_lock
, flags
);
1150 next
= &np
->properties
;
1152 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
1153 /* duplicate ! don't insert it */
1154 write_unlock_irqrestore(&devtree_lock
, flags
);
1157 next
= &(*next
)->next
;
1160 write_unlock_irqrestore(&devtree_lock
, flags
);
1162 #ifdef CONFIG_PROC_DEVICETREE
1163 /* try to add to proc as well if it was initialized */
1165 proc_device_tree_add_prop(np
->pde
, prop
);
1166 #endif /* CONFIG_PROC_DEVICETREE */
1172 * of_remove_property - Remove a property from a node.
1174 * Note that we don't actually remove it, since we have given out
1175 * who-knows-how-many pointers to the data using get-property.
1176 * Instead we just move the property to the "dead properties"
1177 * list, so it won't be found any more.
1179 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1181 struct property
**next
;
1182 unsigned long flags
;
1186 rc
= of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
);
1190 write_lock_irqsave(&devtree_lock
, flags
);
1191 next
= &np
->properties
;
1193 if (*next
== prop
) {
1194 /* found the node */
1196 prop
->next
= np
->deadprops
;
1197 np
->deadprops
= prop
;
1201 next
= &(*next
)->next
;
1203 write_unlock_irqrestore(&devtree_lock
, flags
);
1208 #ifdef CONFIG_PROC_DEVICETREE
1209 /* try to remove the proc node as well */
1211 proc_device_tree_remove_prop(np
->pde
, prop
);
1212 #endif /* CONFIG_PROC_DEVICETREE */
1218 * of_update_property - Update a property in a node, if the property does
1219 * not exist, add it.
1221 * Note that we don't actually remove it, since we have given out
1222 * who-knows-how-many pointers to the data using get-property.
1223 * Instead we just move the property to the "dead properties" list,
1224 * and add the new property to the property list
1226 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1228 struct property
**next
, *oldprop
;
1229 unsigned long flags
;
1232 rc
= of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
);
1239 oldprop
= of_find_property(np
, newprop
->name
, NULL
);
1241 return of_add_property(np
, newprop
);
1243 write_lock_irqsave(&devtree_lock
, flags
);
1244 next
= &np
->properties
;
1246 if (*next
== oldprop
) {
1247 /* found the node */
1248 newprop
->next
= oldprop
->next
;
1250 oldprop
->next
= np
->deadprops
;
1251 np
->deadprops
= oldprop
;
1255 next
= &(*next
)->next
;
1257 write_unlock_irqrestore(&devtree_lock
, flags
);
1262 #ifdef CONFIG_PROC_DEVICETREE
1263 /* try to add to proc as well if it was initialized */
1265 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
1266 #endif /* CONFIG_PROC_DEVICETREE */
1271 #if defined(CONFIG_OF_DYNAMIC)
1273 * Support for dynamic device trees.
1275 * On some platforms, the device tree can be manipulated at runtime.
1276 * The routines in this section support adding, removing and changing
1277 * device tree nodes.
1280 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain
);
1282 int of_reconfig_notifier_register(struct notifier_block
*nb
)
1284 return blocking_notifier_chain_register(&of_reconfig_chain
, nb
);
1286 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register
);
1288 int of_reconfig_notifier_unregister(struct notifier_block
*nb
)
1290 return blocking_notifier_chain_unregister(&of_reconfig_chain
, nb
);
1292 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister
);
1294 int of_reconfig_notify(unsigned long action
, void *p
)
1298 rc
= blocking_notifier_call_chain(&of_reconfig_chain
, action
, p
);
1299 return notifier_to_errno(rc
);
1302 #ifdef CONFIG_PROC_DEVICETREE
1303 static void of_add_proc_dt_entry(struct device_node
*dn
)
1305 struct proc_dir_entry
*ent
;
1307 ent
= proc_mkdir(strrchr(dn
->full_name
, '/') + 1, dn
->parent
->pde
);
1309 proc_device_tree_add_node(dn
, ent
);
1312 static void of_add_proc_dt_entry(struct device_node
*dn
)
1319 * of_attach_node - Plug a device node into the tree and global list.
1321 int of_attach_node(struct device_node
*np
)
1323 unsigned long flags
;
1326 rc
= of_reconfig_notify(OF_RECONFIG_ATTACH_NODE
, np
);
1330 write_lock_irqsave(&devtree_lock
, flags
);
1331 np
->sibling
= np
->parent
->child
;
1332 np
->allnext
= of_allnodes
;
1333 np
->parent
->child
= np
;
1335 write_unlock_irqrestore(&devtree_lock
, flags
);
1337 of_add_proc_dt_entry(np
);
1341 #ifdef CONFIG_PROC_DEVICETREE
1342 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1344 struct device_node
*parent
= dn
->parent
;
1345 struct property
*prop
= dn
->properties
;
1348 remove_proc_entry(prop
->name
, dn
->pde
);
1353 remove_proc_entry(dn
->pde
->name
, parent
->pde
);
1356 static void of_remove_proc_dt_entry(struct device_node
*dn
)
1363 * of_detach_node - "Unplug" a node from the device tree.
1365 * The caller must hold a reference to the node. The memory associated with
1366 * the node is not freed until its refcount goes to zero.
1368 int of_detach_node(struct device_node
*np
)
1370 struct device_node
*parent
;
1371 unsigned long flags
;
1374 rc
= of_reconfig_notify(OF_RECONFIG_DETACH_NODE
, np
);
1378 write_lock_irqsave(&devtree_lock
, flags
);
1380 if (of_node_check_flag(np
, OF_DETACHED
)) {
1381 /* someone already detached it */
1382 write_unlock_irqrestore(&devtree_lock
, flags
);
1386 parent
= np
->parent
;
1388 write_unlock_irqrestore(&devtree_lock
, flags
);
1392 if (of_allnodes
== np
)
1393 of_allnodes
= np
->allnext
;
1395 struct device_node
*prev
;
1396 for (prev
= of_allnodes
;
1397 prev
->allnext
!= np
;
1398 prev
= prev
->allnext
)
1400 prev
->allnext
= np
->allnext
;
1403 if (parent
->child
== np
)
1404 parent
->child
= np
->sibling
;
1406 struct device_node
*prevsib
;
1407 for (prevsib
= np
->parent
->child
;
1408 prevsib
->sibling
!= np
;
1409 prevsib
= prevsib
->sibling
)
1411 prevsib
->sibling
= np
->sibling
;
1414 of_node_set_flag(np
, OF_DETACHED
);
1415 write_unlock_irqrestore(&devtree_lock
, flags
);
1417 of_remove_proc_dt_entry(np
);
1420 #endif /* defined(CONFIG_OF_DYNAMIC) */
1422 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1423 int id
, const char *stem
, int stem_len
)
1427 strncpy(ap
->stem
, stem
, stem_len
);
1428 ap
->stem
[stem_len
] = 0;
1429 list_add_tail(&ap
->link
, &aliases_lookup
);
1430 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1431 ap
->alias
, ap
->stem
, ap
->id
, of_node_full_name(np
));
1435 * of_alias_scan - Scan all properties of 'aliases' node
1437 * The function scans all the properties of 'aliases' node and populate
1438 * the the global lookup table with the properties. It returns the
1439 * number of alias_prop found, or error code in error case.
1441 * @dt_alloc: An allocator that provides a virtual address to memory
1442 * for the resulting tree
1444 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
1446 struct property
*pp
;
1448 of_chosen
= of_find_node_by_path("/chosen");
1449 if (of_chosen
== NULL
)
1450 of_chosen
= of_find_node_by_path("/chosen@0");
1451 of_aliases
= of_find_node_by_path("/aliases");
1455 for_each_property_of_node(of_aliases
, pp
) {
1456 const char *start
= pp
->name
;
1457 const char *end
= start
+ strlen(start
);
1458 struct device_node
*np
;
1459 struct alias_prop
*ap
;
1462 /* Skip those we do not want to proceed */
1463 if (!strcmp(pp
->name
, "name") ||
1464 !strcmp(pp
->name
, "phandle") ||
1465 !strcmp(pp
->name
, "linux,phandle"))
1468 np
= of_find_node_by_path(pp
->value
);
1472 /* walk the alias backwards to extract the id and work out
1473 * the 'stem' string */
1474 while (isdigit(*(end
-1)) && end
> start
)
1478 if (kstrtoint(end
, 10, &id
) < 0)
1481 /* Allocate an alias_prop with enough space for the stem */
1482 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, 4);
1486 of_alias_add(ap
, np
, id
, start
, len
);
1491 * of_alias_get_id - Get alias id for the given device_node
1492 * @np: Pointer to the given device_node
1493 * @stem: Alias stem of the given device_node
1495 * The function travels the lookup table to get alias id for the given
1496 * device_node and alias stem. It returns the alias id if find it.
1498 int of_alias_get_id(struct device_node
*np
, const char *stem
)
1500 struct alias_prop
*app
;
1503 mutex_lock(&of_aliases_mutex
);
1504 list_for_each_entry(app
, &aliases_lookup
, link
) {
1505 if (strcmp(app
->stem
, stem
) != 0)
1508 if (np
== app
->np
) {
1513 mutex_unlock(&of_aliases_mutex
);
1517 EXPORT_SYMBOL_GPL(of_alias_get_id
);
1519 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
1522 const void *curv
= cur
;
1532 curv
+= sizeof(*cur
);
1533 if (curv
>= prop
->value
+ prop
->length
)
1537 *pu
= be32_to_cpup(curv
);
1540 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
1542 const char *of_prop_next_string(struct property
*prop
, const char *cur
)
1544 const void *curv
= cur
;
1552 curv
+= strlen(cur
) + 1;
1553 if (curv
>= prop
->value
+ prop
->length
)
1558 EXPORT_SYMBOL_GPL(of_prop_next_string
);