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.
21 #define pr_fmt(fmt) "OF: " fmt
23 #include <linux/console.h>
24 #include <linux/ctype.h>
25 #include <linux/cpu.h>
26 #include <linux/module.h>
28 #include <linux/of_graph.h>
29 #include <linux/spinlock.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/proc_fs.h>
34 #include "of_private.h"
36 LIST_HEAD(aliases_lookup
);
38 struct device_node
*of_root
;
39 EXPORT_SYMBOL(of_root
);
40 struct device_node
*of_chosen
;
41 struct device_node
*of_aliases
;
42 struct device_node
*of_stdout
;
43 static const char *of_stdout_options
;
48 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
49 * This mutex must be held whenever modifications are being made to the
50 * device tree. The of_{attach,detach}_node() and
51 * of_{add,remove,update}_property() helpers make sure this happens.
53 DEFINE_MUTEX(of_mutex
);
55 /* use when traversing tree through the child, sibling,
56 * or parent members of struct device_node.
58 DEFINE_RAW_SPINLOCK(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
);
93 int __weak
of_node_to_nid(struct device_node
*np
)
99 #ifndef CONFIG_OF_DYNAMIC
100 static void of_node_release(struct kobject
*kobj
)
102 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
104 #endif /* CONFIG_OF_DYNAMIC */
106 struct kobj_type of_node_ktype
= {
107 .release
= of_node_release
,
110 static ssize_t
of_node_property_read(struct file
*filp
, struct kobject
*kobj
,
111 struct bin_attribute
*bin_attr
, char *buf
,
112 loff_t offset
, size_t count
)
114 struct property
*pp
= container_of(bin_attr
, struct property
, attr
);
115 return memory_read_from_buffer(buf
, count
, &offset
, pp
->value
, pp
->length
);
118 /* always return newly allocated name, caller must free after use */
119 static const char *safe_name(struct kobject
*kobj
, const char *orig_name
)
121 const char *name
= orig_name
;
122 struct kernfs_node
*kn
;
125 /* don't be a hero. After 16 tries give up */
126 while (i
< 16 && (kn
= sysfs_get_dirent(kobj
->sd
, name
))) {
128 if (name
!= orig_name
)
130 name
= kasprintf(GFP_KERNEL
, "%s#%i", orig_name
, ++i
);
133 if (name
== orig_name
) {
134 name
= kstrdup(orig_name
, GFP_KERNEL
);
136 pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
137 kobject_name(kobj
), name
);
142 int __of_add_property_sysfs(struct device_node
*np
, struct property
*pp
)
146 /* Important: Don't leak passwords */
147 bool secure
= strncmp(pp
->name
, "security-", 9) == 0;
149 if (!IS_ENABLED(CONFIG_SYSFS
))
152 if (!of_kset
|| !of_node_is_attached(np
))
155 sysfs_bin_attr_init(&pp
->attr
);
156 pp
->attr
.attr
.name
= safe_name(&np
->kobj
, pp
->name
);
157 pp
->attr
.attr
.mode
= secure
? S_IRUSR
: S_IRUGO
;
158 pp
->attr
.size
= secure
? 0 : pp
->length
;
159 pp
->attr
.read
= of_node_property_read
;
161 rc
= sysfs_create_bin_file(&np
->kobj
, &pp
->attr
);
162 WARN(rc
, "error adding attribute %s to node %s\n", pp
->name
, np
->full_name
);
166 int __of_attach_node_sysfs(struct device_node
*np
)
169 struct kobject
*parent
;
176 np
->kobj
.kset
= of_kset
;
178 /* Nodes without parents are new top level trees */
179 name
= safe_name(&of_kset
->kobj
, "base");
182 name
= safe_name(&np
->parent
->kobj
, kbasename(np
->full_name
));
183 parent
= &np
->parent
->kobj
;
187 rc
= kobject_add(&np
->kobj
, parent
, "%s", name
);
192 for_each_property_of_node(np
, pp
)
193 __of_add_property_sysfs(np
, pp
);
198 void __init
of_core_init(void)
200 struct device_node
*np
;
202 /* Create the kset, and register existing nodes */
203 mutex_lock(&of_mutex
);
204 of_kset
= kset_create_and_add("devicetree", NULL
, firmware_kobj
);
206 mutex_unlock(&of_mutex
);
207 pr_err("failed to register existing nodes\n");
210 for_each_of_allnodes(np
)
211 __of_attach_node_sysfs(np
);
212 mutex_unlock(&of_mutex
);
214 /* Symlink in /proc as required by userspace ABI */
216 proc_symlink("device-tree", NULL
, "/sys/firmware/devicetree/base");
219 static struct property
*__of_find_property(const struct device_node
*np
,
220 const char *name
, int *lenp
)
227 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
228 if (of_prop_cmp(pp
->name
, name
) == 0) {
238 struct property
*of_find_property(const struct device_node
*np
,
245 raw_spin_lock_irqsave(&devtree_lock
, flags
);
246 pp
= __of_find_property(np
, name
, lenp
);
247 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
251 EXPORT_SYMBOL(of_find_property
);
253 struct device_node
*__of_find_all_nodes(struct device_node
*prev
)
255 struct device_node
*np
;
258 } else if (prev
->child
) {
261 /* Walk back up looking for a sibling, or the end of the structure */
263 while (np
->parent
&& !np
->sibling
)
265 np
= np
->sibling
; /* Might be null at the end of the tree */
271 * of_find_all_nodes - Get next node in global list
272 * @prev: Previous node or NULL to start iteration
273 * of_node_put() will be called on it
275 * Returns a node pointer with refcount incremented, use
276 * of_node_put() on it when done.
278 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
280 struct device_node
*np
;
283 raw_spin_lock_irqsave(&devtree_lock
, flags
);
284 np
= __of_find_all_nodes(prev
);
287 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
290 EXPORT_SYMBOL(of_find_all_nodes
);
293 * Find a property with a given name for a given node
294 * and return the value.
296 const void *__of_get_property(const struct device_node
*np
,
297 const char *name
, int *lenp
)
299 struct property
*pp
= __of_find_property(np
, name
, lenp
);
301 return pp
? pp
->value
: NULL
;
305 * Find a property with a given name for a given node
306 * and return the value.
308 const void *of_get_property(const struct device_node
*np
, const char *name
,
311 struct property
*pp
= of_find_property(np
, name
, lenp
);
313 return pp
? pp
->value
: NULL
;
315 EXPORT_SYMBOL(of_get_property
);
318 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
320 * @cpu: logical cpu index of a core/thread
321 * @phys_id: physical identifier of a core/thread
323 * CPU logical to physical index mapping is architecture specific.
324 * However this __weak function provides a default match of physical
325 * id to logical cpu index. phys_id provided here is usually values read
326 * from the device tree which must match the hardware internal registers.
328 * Returns true if the physical identifier and the logical cpu index
329 * correspond to the same core/thread, false otherwise.
331 bool __weak
arch_match_cpu_phys_id(int cpu
, u64 phys_id
)
333 return (u32
)phys_id
== cpu
;
337 * Checks if the given "prop_name" property holds the physical id of the
338 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
339 * NULL, local thread number within the core is returned in it.
341 static bool __of_find_n_match_cpu_property(struct device_node
*cpun
,
342 const char *prop_name
, int cpu
, unsigned int *thread
)
345 int ac
, prop_len
, tid
;
348 ac
= of_n_addr_cells(cpun
);
349 cell
= of_get_property(cpun
, prop_name
, &prop_len
);
352 prop_len
/= sizeof(*cell
) * ac
;
353 for (tid
= 0; tid
< prop_len
; tid
++) {
354 hwid
= of_read_number(cell
, ac
);
355 if (arch_match_cpu_phys_id(cpu
, hwid
)) {
366 * arch_find_n_match_cpu_physical_id - See if the given device node is
367 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
368 * else false. If 'thread' is non-NULL, the local thread number within the
369 * core is returned in it.
371 bool __weak
arch_find_n_match_cpu_physical_id(struct device_node
*cpun
,
372 int cpu
, unsigned int *thread
)
374 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
375 * for thread ids on PowerPC. If it doesn't exist fallback to
376 * standard "reg" property.
378 if (IS_ENABLED(CONFIG_PPC
) &&
379 __of_find_n_match_cpu_property(cpun
,
380 "ibm,ppc-interrupt-server#s",
384 return __of_find_n_match_cpu_property(cpun
, "reg", cpu
, thread
);
388 * of_get_cpu_node - Get device node associated with the given logical CPU
390 * @cpu: CPU number(logical index) for which device node is required
391 * @thread: if not NULL, local thread number within the physical core is
394 * The main purpose of this function is to retrieve the device node for the
395 * given logical CPU index. It should be used to initialize the of_node in
396 * cpu device. Once of_node in cpu device is populated, all the further
397 * references can use that instead.
399 * CPU logical to physical index mapping is architecture specific and is built
400 * before booting secondary cores. This function uses arch_match_cpu_phys_id
401 * which can be overridden by architecture specific implementation.
403 * Returns a node pointer for the logical cpu with refcount incremented, use
404 * of_node_put() on it when done. Returns NULL if not found.
406 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
408 struct device_node
*cpun
;
410 for_each_node_by_type(cpun
, "cpu") {
411 if (arch_find_n_match_cpu_physical_id(cpun
, cpu
, thread
))
416 EXPORT_SYMBOL(of_get_cpu_node
);
419 * __of_device_is_compatible() - Check if the node matches given constraints
420 * @device: pointer to node
421 * @compat: required compatible string, NULL or "" for any match
422 * @type: required device_type value, NULL or "" for any match
423 * @name: required node name, NULL or "" for any match
425 * Checks if the given @compat, @type and @name strings match the
426 * properties of the given @device. A constraints can be skipped by
427 * passing NULL or an empty string as the constraint.
429 * Returns 0 for no match, and a positive integer on match. The return
430 * value is a relative score with larger values indicating better
431 * matches. The score is weighted for the most specific compatible value
432 * to get the highest score. Matching type is next, followed by matching
433 * name. Practically speaking, this results in the following priority
436 * 1. specific compatible && type && name
437 * 2. specific compatible && type
438 * 3. specific compatible && name
439 * 4. specific compatible
440 * 5. general compatible && type && name
441 * 6. general compatible && type
442 * 7. general compatible && name
443 * 8. general compatible
448 static int __of_device_is_compatible(const struct device_node
*device
,
449 const char *compat
, const char *type
, const char *name
)
451 struct property
*prop
;
453 int index
= 0, score
= 0;
455 /* Compatible match has highest priority */
456 if (compat
&& compat
[0]) {
457 prop
= __of_find_property(device
, "compatible", NULL
);
458 for (cp
= of_prop_next_string(prop
, NULL
); cp
;
459 cp
= of_prop_next_string(prop
, cp
), index
++) {
460 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0) {
461 score
= INT_MAX
/2 - (index
<< 2);
469 /* Matching type is better than matching name */
470 if (type
&& type
[0]) {
471 if (!device
->type
|| of_node_cmp(type
, device
->type
))
476 /* Matching name is a bit better than not */
477 if (name
&& name
[0]) {
478 if (!device
->name
|| of_node_cmp(name
, device
->name
))
486 /** Checks if the given "compat" string matches one of the strings in
487 * the device's "compatible" property
489 int of_device_is_compatible(const struct device_node
*device
,
495 raw_spin_lock_irqsave(&devtree_lock
, flags
);
496 res
= __of_device_is_compatible(device
, compat
, NULL
, NULL
);
497 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
500 EXPORT_SYMBOL(of_device_is_compatible
);
502 /** Checks if the device is compatible with any of the entries in
503 * a NULL terminated array of strings. Returns the best match
506 int of_device_compatible_match(struct device_node
*device
,
507 const char *const *compat
)
509 unsigned int tmp
, score
= 0;
515 tmp
= of_device_is_compatible(device
, *compat
);
525 * of_machine_is_compatible - Test root of device tree for a given compatible value
526 * @compat: compatible string to look for in root node's compatible property.
528 * Returns a positive integer if the root node has the given value in its
529 * compatible property.
531 int of_machine_is_compatible(const char *compat
)
533 struct device_node
*root
;
536 root
= of_find_node_by_path("/");
538 rc
= of_device_is_compatible(root
, compat
);
543 EXPORT_SYMBOL(of_machine_is_compatible
);
546 * __of_device_is_available - check if a device is available for use
548 * @device: Node to check for availability, with locks already held
550 * Returns true if the status property is absent or set to "okay" or "ok",
553 static bool __of_device_is_available(const struct device_node
*device
)
561 status
= __of_get_property(device
, "status", &statlen
);
566 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
574 * of_device_is_available - check if a device is available for use
576 * @device: Node to check for availability
578 * Returns true if the status property is absent or set to "okay" or "ok",
581 bool of_device_is_available(const struct device_node
*device
)
586 raw_spin_lock_irqsave(&devtree_lock
, flags
);
587 res
= __of_device_is_available(device
);
588 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
592 EXPORT_SYMBOL(of_device_is_available
);
595 * of_device_is_big_endian - check if a device has BE registers
597 * @device: Node to check for endianness
599 * Returns true if the device has a "big-endian" property, or if the kernel
600 * was compiled for BE *and* the device has a "native-endian" property.
601 * Returns false otherwise.
603 * Callers would nominally use ioread32be/iowrite32be if
604 * of_device_is_big_endian() == true, or readl/writel otherwise.
606 bool of_device_is_big_endian(const struct device_node
*device
)
608 if (of_property_read_bool(device
, "big-endian"))
610 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) &&
611 of_property_read_bool(device
, "native-endian"))
615 EXPORT_SYMBOL(of_device_is_big_endian
);
618 * of_get_parent - Get a node's parent if any
619 * @node: Node to get parent
621 * Returns a node pointer with refcount incremented, use
622 * of_node_put() on it when done.
624 struct device_node
*of_get_parent(const struct device_node
*node
)
626 struct device_node
*np
;
632 raw_spin_lock_irqsave(&devtree_lock
, flags
);
633 np
= of_node_get(node
->parent
);
634 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
637 EXPORT_SYMBOL(of_get_parent
);
640 * of_get_next_parent - Iterate to a node's parent
641 * @node: Node to get parent of
643 * This is like of_get_parent() except that it drops the
644 * refcount on the passed node, making it suitable for iterating
645 * through a node's parents.
647 * Returns a node pointer with refcount incremented, use
648 * of_node_put() on it when done.
650 struct device_node
*of_get_next_parent(struct device_node
*node
)
652 struct device_node
*parent
;
658 raw_spin_lock_irqsave(&devtree_lock
, flags
);
659 parent
= of_node_get(node
->parent
);
661 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
664 EXPORT_SYMBOL(of_get_next_parent
);
666 static struct device_node
*__of_get_next_child(const struct device_node
*node
,
667 struct device_node
*prev
)
669 struct device_node
*next
;
674 next
= prev
? prev
->sibling
: node
->child
;
675 for (; next
; next
= next
->sibling
)
676 if (of_node_get(next
))
681 #define __for_each_child_of_node(parent, child) \
682 for (child = __of_get_next_child(parent, NULL); child != NULL; \
683 child = __of_get_next_child(parent, child))
686 * of_get_next_child - Iterate a node childs
688 * @prev: previous child of the parent node, or NULL to get first
690 * Returns a node pointer with refcount incremented, use of_node_put() on
691 * it when done. Returns NULL when prev is the last child. Decrements the
694 struct device_node
*of_get_next_child(const struct device_node
*node
,
695 struct device_node
*prev
)
697 struct device_node
*next
;
700 raw_spin_lock_irqsave(&devtree_lock
, flags
);
701 next
= __of_get_next_child(node
, prev
);
702 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
705 EXPORT_SYMBOL(of_get_next_child
);
708 * of_get_next_available_child - Find the next available child node
710 * @prev: previous child of the parent node, or NULL to get first
712 * This function is like of_get_next_child(), except that it
713 * automatically skips any disabled nodes (i.e. status = "disabled").
715 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
716 struct device_node
*prev
)
718 struct device_node
*next
;
724 raw_spin_lock_irqsave(&devtree_lock
, flags
);
725 next
= prev
? prev
->sibling
: node
->child
;
726 for (; next
; next
= next
->sibling
) {
727 if (!__of_device_is_available(next
))
729 if (of_node_get(next
))
733 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
736 EXPORT_SYMBOL(of_get_next_available_child
);
739 * of_get_compatible_child - Find compatible child node
740 * @parent: parent node
741 * @compatible: compatible string
743 * Lookup child node whose compatible property contains the given compatible
746 * Returns a node pointer with refcount incremented, use of_node_put() on it
747 * when done; or NULL if not found.
749 struct device_node
*of_get_compatible_child(const struct device_node
*parent
,
750 const char *compatible
)
752 struct device_node
*child
;
754 for_each_child_of_node(parent
, child
) {
755 if (of_device_is_compatible(child
, compatible
))
761 EXPORT_SYMBOL(of_get_compatible_child
);
764 * of_get_child_by_name - Find the child node by name for a given parent
766 * @name: child name to look for.
768 * This function looks for child node for given matching name
770 * Returns a node pointer if found, with refcount incremented, use
771 * of_node_put() on it when done.
772 * Returns NULL if node is not found.
774 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
777 struct device_node
*child
;
779 for_each_child_of_node(node
, child
)
780 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
784 EXPORT_SYMBOL(of_get_child_by_name
);
786 static struct device_node
*__of_find_node_by_path(struct device_node
*parent
,
789 struct device_node
*child
;
792 len
= strcspn(path
, "/:");
796 __for_each_child_of_node(parent
, child
) {
797 const char *name
= strrchr(child
->full_name
, '/');
798 if (WARN(!name
, "malformed device_node %s\n", child
->full_name
))
801 if (strncmp(path
, name
, len
) == 0 && (strlen(name
) == len
))
808 * of_find_node_opts_by_path - Find a node matching a full OF path
809 * @path: Either the full path to match, or if the path does not
810 * start with '/', the name of a property of the /aliases
811 * node (an alias). In the case of an alias, the node
812 * matching the alias' value will be returned.
813 * @opts: Address of a pointer into which to store the start of
814 * an options string appended to the end of the path with
820 * foo/bar Valid alias + relative path
822 * Returns a node pointer with refcount incremented, use
823 * of_node_put() on it when done.
825 struct device_node
*of_find_node_opts_by_path(const char *path
, const char **opts
)
827 struct device_node
*np
= NULL
;
830 const char *separator
= strchr(path
, ':');
833 *opts
= separator
? separator
+ 1 : NULL
;
835 if (strcmp(path
, "/") == 0)
836 return of_node_get(of_root
);
838 /* The path could begin with an alias */
841 const char *p
= separator
;
844 p
= strchrnul(path
, '/');
847 /* of_aliases must not be NULL */
851 for_each_property_of_node(of_aliases
, pp
) {
852 if (strlen(pp
->name
) == len
&& !strncmp(pp
->name
, path
, len
)) {
853 np
= of_find_node_by_path(pp
->value
);
862 /* Step down the tree matching path components */
863 raw_spin_lock_irqsave(&devtree_lock
, flags
);
865 np
= of_node_get(of_root
);
866 while (np
&& *path
== '/') {
867 path
++; /* Increment past '/' delimiter */
868 np
= __of_find_node_by_path(np
, path
);
869 path
= strchrnul(path
, '/');
870 if (separator
&& separator
< path
)
873 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
876 EXPORT_SYMBOL(of_find_node_opts_by_path
);
879 * of_find_node_by_name - Find a node by its "name" property
880 * @from: The node to start searching from or NULL, the node
881 * you pass will not be searched, only the next one
882 * will; typically, you pass what the previous call
883 * returned. of_node_put() will be called on it
884 * @name: The name string to match against
886 * Returns a node pointer with refcount incremented, use
887 * of_node_put() on it when done.
889 struct device_node
*of_find_node_by_name(struct device_node
*from
,
892 struct device_node
*np
;
895 raw_spin_lock_irqsave(&devtree_lock
, flags
);
896 for_each_of_allnodes_from(from
, np
)
897 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
901 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
904 EXPORT_SYMBOL(of_find_node_by_name
);
907 * of_find_node_by_type - Find a node by its "device_type" property
908 * @from: The node to start searching from, or NULL to start searching
909 * the entire device tree. The node you pass will not be
910 * searched, only the next one will; typically, you pass
911 * what the previous call returned. of_node_put() will be
912 * called on from for you.
913 * @type: The type string to match against
915 * Returns a node pointer with refcount incremented, use
916 * of_node_put() on it when done.
918 struct device_node
*of_find_node_by_type(struct device_node
*from
,
921 struct device_node
*np
;
924 raw_spin_lock_irqsave(&devtree_lock
, flags
);
925 for_each_of_allnodes_from(from
, np
)
926 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
930 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
933 EXPORT_SYMBOL(of_find_node_by_type
);
936 * of_find_compatible_node - Find a node based on type and one of the
937 * tokens in its "compatible" property
938 * @from: The node to start searching from or NULL, the node
939 * you pass will not be searched, only the next one
940 * will; typically, you pass what the previous call
941 * returned. of_node_put() will be called on it
942 * @type: The type string to match "device_type" or NULL to ignore
943 * @compatible: The string to match to one of the tokens in the device
946 * Returns a node pointer with refcount incremented, use
947 * of_node_put() on it when done.
949 struct device_node
*of_find_compatible_node(struct device_node
*from
,
950 const char *type
, const char *compatible
)
952 struct device_node
*np
;
955 raw_spin_lock_irqsave(&devtree_lock
, flags
);
956 for_each_of_allnodes_from(from
, np
)
957 if (__of_device_is_compatible(np
, compatible
, type
, NULL
) &&
961 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
964 EXPORT_SYMBOL(of_find_compatible_node
);
967 * of_find_node_with_property - Find a node which has a property with
969 * @from: The node to start searching from or NULL, the node
970 * you pass will not be searched, only the next one
971 * will; typically, you pass what the previous call
972 * returned. of_node_put() will be called on it
973 * @prop_name: The name of the property to look for.
975 * Returns a node pointer with refcount incremented, use
976 * of_node_put() on it when done.
978 struct device_node
*of_find_node_with_property(struct device_node
*from
,
979 const char *prop_name
)
981 struct device_node
*np
;
985 raw_spin_lock_irqsave(&devtree_lock
, flags
);
986 for_each_of_allnodes_from(from
, np
) {
987 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
988 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
996 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
999 EXPORT_SYMBOL(of_find_node_with_property
);
1002 const struct of_device_id
*__of_match_node(const struct of_device_id
*matches
,
1003 const struct device_node
*node
)
1005 const struct of_device_id
*best_match
= NULL
;
1006 int score
, best_score
= 0;
1011 for (; matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]; matches
++) {
1012 score
= __of_device_is_compatible(node
, matches
->compatible
,
1013 matches
->type
, matches
->name
);
1014 if (score
> best_score
) {
1015 best_match
= matches
;
1024 * of_match_node - Tell if a device_node has a matching of_match structure
1025 * @matches: array of of device match structures to search in
1026 * @node: the of device structure to match against
1028 * Low level utility function used by device matching.
1030 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
1031 const struct device_node
*node
)
1033 const struct of_device_id
*match
;
1034 unsigned long flags
;
1036 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1037 match
= __of_match_node(matches
, node
);
1038 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1041 EXPORT_SYMBOL(of_match_node
);
1044 * of_find_matching_node_and_match - Find a node based on an of_device_id
1046 * @from: The node to start searching from or NULL, the node
1047 * you pass will not be searched, only the next one
1048 * will; typically, you pass what the previous call
1049 * returned. of_node_put() will be called on it
1050 * @matches: array of of device match structures to search in
1051 * @match Updated to point at the matches entry which matched
1053 * Returns a node pointer with refcount incremented, use
1054 * of_node_put() on it when done.
1056 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
1057 const struct of_device_id
*matches
,
1058 const struct of_device_id
**match
)
1060 struct device_node
*np
;
1061 const struct of_device_id
*m
;
1062 unsigned long flags
;
1067 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1068 for_each_of_allnodes_from(from
, np
) {
1069 m
= __of_match_node(matches
, np
);
1070 if (m
&& of_node_get(np
)) {
1077 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1080 EXPORT_SYMBOL(of_find_matching_node_and_match
);
1083 * of_modalias_node - Lookup appropriate modalias for a device node
1084 * @node: pointer to a device tree node
1085 * @modalias: Pointer to buffer that modalias value will be copied into
1086 * @len: Length of modalias value
1088 * Based on the value of the compatible property, this routine will attempt
1089 * to choose an appropriate modalias value for a particular device tree node.
1090 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1091 * from the first entry in the compatible list property.
1093 * This routine returns 0 on success, <0 on failure.
1095 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
1097 const char *compatible
, *p
;
1100 compatible
= of_get_property(node
, "compatible", &cplen
);
1101 if (!compatible
|| strlen(compatible
) > cplen
)
1103 p
= strchr(compatible
, ',');
1104 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
1107 EXPORT_SYMBOL_GPL(of_modalias_node
);
1110 * of_find_node_by_phandle - Find a node given a phandle
1111 * @handle: phandle of the node to find
1113 * Returns a node pointer with refcount incremented, use
1114 * of_node_put() on it when done.
1116 struct device_node
*of_find_node_by_phandle(phandle handle
)
1118 struct device_node
*np
;
1119 unsigned long flags
;
1124 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1125 for_each_of_allnodes(np
)
1126 if (np
->phandle
== handle
)
1129 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1132 EXPORT_SYMBOL(of_find_node_by_phandle
);
1135 * of_property_count_elems_of_size - Count the number of elements in a property
1137 * @np: device node from which the property value is to be read.
1138 * @propname: name of the property to be searched.
1139 * @elem_size: size of the individual element
1141 * Search for a property in a device node and count the number of elements of
1142 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1143 * property does not exist or its length does not match a multiple of elem_size
1144 * and -ENODATA if the property does not have a value.
1146 int of_property_count_elems_of_size(const struct device_node
*np
,
1147 const char *propname
, int elem_size
)
1149 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1156 if (prop
->length
% elem_size
!= 0) {
1157 pr_err("size of %s in node %s is not a multiple of %d\n",
1158 propname
, np
->full_name
, elem_size
);
1162 return prop
->length
/ elem_size
;
1164 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size
);
1167 * of_find_property_value_of_size
1169 * @np: device node from which the property value is to be read.
1170 * @propname: name of the property to be searched.
1171 * @min: minimum allowed length of property value
1172 * @max: maximum allowed length of property value (0 means unlimited)
1173 * @len: if !=NULL, actual length is written to here
1175 * Search for a property in a device node and valid the requested size.
1176 * Returns the property value on success, -EINVAL if the property does not
1177 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1178 * property data is too small or too large.
1181 static void *of_find_property_value_of_size(const struct device_node
*np
,
1182 const char *propname
, u32 min
, u32 max
, size_t *len
)
1184 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1187 return ERR_PTR(-EINVAL
);
1189 return ERR_PTR(-ENODATA
);
1190 if (prop
->length
< min
)
1191 return ERR_PTR(-EOVERFLOW
);
1192 if (max
&& prop
->length
> max
)
1193 return ERR_PTR(-EOVERFLOW
);
1196 *len
= prop
->length
;
1202 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1204 * @np: device node from which the property value is to be read.
1205 * @propname: name of the property to be searched.
1206 * @index: index of the u32 in the list of values
1207 * @out_value: pointer to return value, modified only if no error.
1209 * Search for a property in a device node and read nth 32-bit value from
1210 * it. Returns 0 on success, -EINVAL if the property does not exist,
1211 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1212 * property data isn't large enough.
1214 * The out_value is modified only if a valid u32 value can be decoded.
1216 int of_property_read_u32_index(const struct device_node
*np
,
1217 const char *propname
,
1218 u32 index
, u32
*out_value
)
1220 const u32
*val
= of_find_property_value_of_size(np
, propname
,
1221 ((index
+ 1) * sizeof(*out_value
)),
1226 return PTR_ERR(val
);
1228 *out_value
= be32_to_cpup(((__be32
*)val
) + index
);
1231 EXPORT_SYMBOL_GPL(of_property_read_u32_index
);
1234 * of_property_read_variable_u8_array - Find and read an array of u8 from a
1235 * property, with bounds on the minimum and maximum array size.
1237 * @np: device node from which the property value is to be read.
1238 * @propname: name of the property to be searched.
1239 * @out_values: pointer to return value, modified only if return value is 0.
1240 * @sz_min: minimum number of array elements to read
1241 * @sz_max: maximum number of array elements to read, if zero there is no
1242 * upper limit on the number of elements in the dts entry but only
1243 * sz_min will be read.
1245 * Search for a property in a device node and read 8-bit value(s) from
1246 * it. Returns number of elements read on success, -EINVAL if the property
1247 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1248 * if the property data is smaller than sz_min or longer than sz_max.
1250 * dts entry of array should be like:
1251 * property = /bits/ 8 <0x50 0x60 0x70>;
1253 * The out_values is modified only if a valid u8 value can be decoded.
1255 int of_property_read_variable_u8_array(const struct device_node
*np
,
1256 const char *propname
, u8
*out_values
,
1257 size_t sz_min
, size_t sz_max
)
1260 const u8
*val
= of_find_property_value_of_size(np
, propname
,
1261 (sz_min
* sizeof(*out_values
)),
1262 (sz_max
* sizeof(*out_values
)),
1266 return PTR_ERR(val
);
1271 sz
/= sizeof(*out_values
);
1275 *out_values
++ = *val
++;
1279 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array
);
1282 * of_property_read_variable_u16_array - Find and read an array of u16 from a
1283 * property, with bounds on the minimum and maximum array size.
1285 * @np: device node from which the property value is to be read.
1286 * @propname: name of the property to be searched.
1287 * @out_values: pointer to return value, modified only if return value is 0.
1288 * @sz_min: minimum number of array elements to read
1289 * @sz_max: maximum number of array elements to read, if zero there is no
1290 * upper limit on the number of elements in the dts entry but only
1291 * sz_min will be read.
1293 * Search for a property in a device node and read 16-bit value(s) from
1294 * it. Returns number of elements read on success, -EINVAL if the property
1295 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1296 * if the property data is smaller than sz_min or longer than sz_max.
1298 * dts entry of array should be like:
1299 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1301 * The out_values is modified only if a valid u16 value can be decoded.
1303 int of_property_read_variable_u16_array(const struct device_node
*np
,
1304 const char *propname
, u16
*out_values
,
1305 size_t sz_min
, size_t sz_max
)
1308 const __be16
*val
= of_find_property_value_of_size(np
, propname
,
1309 (sz_min
* sizeof(*out_values
)),
1310 (sz_max
* sizeof(*out_values
)),
1314 return PTR_ERR(val
);
1319 sz
/= sizeof(*out_values
);
1323 *out_values
++ = be16_to_cpup(val
++);
1327 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array
);
1330 * of_property_read_variable_u32_array - Find and read an array of 32 bit
1331 * integers from a property, with bounds on the minimum and maximum array size.
1333 * @np: device node from which the property value is to be read.
1334 * @propname: name of the property to be searched.
1335 * @out_values: pointer to return value, modified only if return value is 0.
1336 * @sz_min: minimum number of array elements to read
1337 * @sz_max: maximum number of array elements to read, if zero there is no
1338 * upper limit on the number of elements in the dts entry but only
1339 * sz_min will be read.
1341 * Search for a property in a device node and read 32-bit value(s) from
1342 * it. Returns number of elements read on success, -EINVAL if the property
1343 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1344 * if the property data is smaller than sz_min or longer than sz_max.
1346 * The out_values is modified only if a valid u32 value can be decoded.
1348 int of_property_read_variable_u32_array(const struct device_node
*np
,
1349 const char *propname
, u32
*out_values
,
1350 size_t sz_min
, size_t sz_max
)
1353 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1354 (sz_min
* sizeof(*out_values
)),
1355 (sz_max
* sizeof(*out_values
)),
1359 return PTR_ERR(val
);
1364 sz
/= sizeof(*out_values
);
1368 *out_values
++ = be32_to_cpup(val
++);
1372 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array
);
1375 * of_property_read_u64 - Find and read a 64 bit integer from a property
1376 * @np: device node from which the property value is to be read.
1377 * @propname: name of the property to be searched.
1378 * @out_value: pointer to return value, modified only if return value is 0.
1380 * Search for a property in a device node and read a 64-bit value from
1381 * it. Returns 0 on success, -EINVAL if the property does not exist,
1382 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1383 * property data isn't large enough.
1385 * The out_value is modified only if a valid u64 value can be decoded.
1387 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
1390 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1396 return PTR_ERR(val
);
1398 *out_value
= of_read_number(val
, 2);
1401 EXPORT_SYMBOL_GPL(of_property_read_u64
);
1404 * of_property_read_variable_u64_array - Find and read an array of 64 bit
1405 * integers from a property, with bounds on the minimum and maximum array size.
1407 * @np: device node from which the property value is to be read.
1408 * @propname: name of the property to be searched.
1409 * @out_values: pointer to return value, modified only if return value is 0.
1410 * @sz_min: minimum number of array elements to read
1411 * @sz_max: maximum number of array elements to read, if zero there is no
1412 * upper limit on the number of elements in the dts entry but only
1413 * sz_min will be read.
1415 * Search for a property in a device node and read 64-bit value(s) from
1416 * it. Returns number of elements read on success, -EINVAL if the property
1417 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1418 * if the property data is smaller than sz_min or longer than sz_max.
1420 * The out_values is modified only if a valid u64 value can be decoded.
1422 int of_property_read_variable_u64_array(const struct device_node
*np
,
1423 const char *propname
, u64
*out_values
,
1424 size_t sz_min
, size_t sz_max
)
1427 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
1428 (sz_min
* sizeof(*out_values
)),
1429 (sz_max
* sizeof(*out_values
)),
1433 return PTR_ERR(val
);
1438 sz
/= sizeof(*out_values
);
1442 *out_values
++ = of_read_number(val
, 2);
1448 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array
);
1451 * of_property_read_string - Find and read a string from a property
1452 * @np: device node from which the property value is to be read.
1453 * @propname: name of the property to be searched.
1454 * @out_string: pointer to null terminated return string, modified only if
1455 * return value is 0.
1457 * Search for a property in a device tree node and retrieve a null
1458 * terminated string value (pointer to data, not a copy). Returns 0 on
1459 * success, -EINVAL if the property does not exist, -ENODATA if property
1460 * does not have a value, and -EILSEQ if the string is not null-terminated
1461 * within the length of the property data.
1463 * The out_string pointer is modified only if a valid string can be decoded.
1465 int of_property_read_string(const struct device_node
*np
, const char *propname
,
1466 const char **out_string
)
1468 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
1473 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
1475 *out_string
= prop
->value
;
1478 EXPORT_SYMBOL_GPL(of_property_read_string
);
1481 * of_property_match_string() - Find string in a list and return index
1482 * @np: pointer to node containing string list property
1483 * @propname: string list property name
1484 * @string: pointer to string to search for in string list
1486 * This function searches a string list property and returns the index
1487 * of a specific string value.
1489 int of_property_match_string(const struct device_node
*np
, const char *propname
,
1492 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
1495 const char *p
, *end
;
1503 end
= p
+ prop
->length
;
1505 for (i
= 0; p
< end
; i
++, p
+= l
) {
1506 l
= strnlen(p
, end
- p
) + 1;
1509 pr_debug("comparing %s with %s\n", string
, p
);
1510 if (strcmp(string
, p
) == 0)
1511 return i
; /* Found it; return index */
1515 EXPORT_SYMBOL_GPL(of_property_match_string
);
1518 * of_property_read_string_helper() - Utility helper for parsing string properties
1519 * @np: device node from which the property value is to be read.
1520 * @propname: name of the property to be searched.
1521 * @out_strs: output array of string pointers.
1522 * @sz: number of array elements to read.
1523 * @skip: Number of strings to skip over at beginning of list.
1525 * Don't call this function directly. It is a utility helper for the
1526 * of_property_read_string*() family of functions.
1528 int of_property_read_string_helper(const struct device_node
*np
,
1529 const char *propname
, const char **out_strs
,
1530 size_t sz
, int skip
)
1532 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
1534 const char *p
, *end
;
1541 end
= p
+ prop
->length
;
1543 for (i
= 0; p
< end
&& (!out_strs
|| i
< skip
+ sz
); i
++, p
+= l
) {
1544 l
= strnlen(p
, end
- p
) + 1;
1547 if (out_strs
&& i
>= skip
)
1551 return i
<= 0 ? -ENODATA
: i
;
1553 EXPORT_SYMBOL_GPL(of_property_read_string_helper
);
1555 void of_print_phandle_args(const char *msg
, const struct of_phandle_args
*args
)
1558 printk("%s %s", msg
, of_node_full_name(args
->np
));
1559 for (i
= 0; i
< args
->args_count
; i
++)
1560 printk(i
? ",%08x" : ":%08x", args
->args
[i
]);
1564 int of_phandle_iterator_init(struct of_phandle_iterator
*it
,
1565 const struct device_node
*np
,
1566 const char *list_name
,
1567 const char *cells_name
,
1573 memset(it
, 0, sizeof(*it
));
1575 list
= of_get_property(np
, list_name
, &size
);
1579 it
->cells_name
= cells_name
;
1580 it
->cell_count
= cell_count
;
1582 it
->list_end
= list
+ size
/ sizeof(*list
);
1583 it
->phandle_end
= list
;
1589 int of_phandle_iterator_next(struct of_phandle_iterator
*it
)
1594 of_node_put(it
->node
);
1598 if (!it
->cur
|| it
->phandle_end
>= it
->list_end
)
1601 it
->cur
= it
->phandle_end
;
1603 /* If phandle is 0, then it is an empty entry with no arguments. */
1604 it
->phandle
= be32_to_cpup(it
->cur
++);
1609 * Find the provider node and parse the #*-cells property to
1610 * determine the argument length.
1612 it
->node
= of_find_node_by_phandle(it
->phandle
);
1614 if (it
->cells_name
) {
1616 pr_err("%s: could not find phandle\n",
1617 it
->parent
->full_name
);
1621 if (of_property_read_u32(it
->node
, it
->cells_name
,
1623 pr_err("%s: could not get %s for %s\n",
1624 it
->parent
->full_name
,
1626 it
->node
->full_name
);
1630 count
= it
->cell_count
;
1634 * Make sure that the arguments actually fit in the remaining
1635 * property data length
1637 if (it
->cur
+ count
> it
->list_end
) {
1638 pr_err("%s: arguments longer than property\n",
1639 it
->parent
->full_name
);
1644 it
->phandle_end
= it
->cur
+ count
;
1645 it
->cur_count
= count
;
1651 of_node_put(it
->node
);
1658 int of_phandle_iterator_args(struct of_phandle_iterator
*it
,
1664 count
= it
->cur_count
;
1666 if (WARN_ON(size
< count
))
1669 for (i
= 0; i
< count
; i
++)
1670 args
[i
] = be32_to_cpup(it
->cur
++);
1675 static int __of_parse_phandle_with_args(const struct device_node
*np
,
1676 const char *list_name
,
1677 const char *cells_name
,
1678 int cell_count
, int index
,
1679 struct of_phandle_args
*out_args
)
1681 struct of_phandle_iterator it
;
1682 int rc
, cur_index
= 0;
1684 /* Loop over the phandles until all the requested entry is found */
1685 of_for_each_phandle(&it
, rc
, np
, list_name
, cells_name
, cell_count
) {
1687 * All of the error cases bail out of the loop, so at
1688 * this point, the parsing is successful. If the requested
1689 * index matches, then fill the out_args structure and return,
1690 * or return -ENOENT for an empty entry.
1693 if (cur_index
== index
) {
1700 c
= of_phandle_iterator_args(&it
,
1703 out_args
->np
= it
.node
;
1704 out_args
->args_count
= c
;
1706 of_node_put(it
.node
);
1709 /* Found it! return success */
1717 * Unlock node before returning result; will be one of:
1718 * -ENOENT : index is for empty phandle
1719 * -EINVAL : parsing error on data
1723 of_node_put(it
.node
);
1728 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1729 * @np: Pointer to device node holding phandle property
1730 * @phandle_name: Name of property holding a phandle value
1731 * @index: For properties holding a table of phandles, this is the index into
1734 * Returns the device_node pointer with refcount incremented. Use
1735 * of_node_put() on it when done.
1737 struct device_node
*of_parse_phandle(const struct device_node
*np
,
1738 const char *phandle_name
, int index
)
1740 struct of_phandle_args args
;
1745 if (__of_parse_phandle_with_args(np
, phandle_name
, NULL
, 0,
1751 EXPORT_SYMBOL(of_parse_phandle
);
1754 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1755 * @np: pointer to a device tree node containing a list
1756 * @list_name: property name that contains a list
1757 * @cells_name: property name that specifies phandles' arguments count
1758 * @index: index of a phandle to parse out
1759 * @out_args: optional pointer to output arguments structure (will be filled)
1761 * This function is useful to parse lists of phandles and their arguments.
1762 * Returns 0 on success and fills out_args, on error returns appropriate
1765 * Caller is responsible to call of_node_put() on the returned out_args->np
1771 * #list-cells = <2>;
1775 * #list-cells = <1>;
1779 * list = <&phandle1 1 2 &phandle2 3>;
1782 * To get a device_node of the `node2' node you may call this:
1783 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1785 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1786 const char *cells_name
, int index
,
1787 struct of_phandle_args
*out_args
)
1791 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0,
1794 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1797 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1798 * @np: pointer to a device tree node containing a list
1799 * @list_name: property name that contains a list
1800 * @cell_count: number of argument cells following the phandle
1801 * @index: index of a phandle to parse out
1802 * @out_args: optional pointer to output arguments structure (will be filled)
1804 * This function is useful to parse lists of phandles and their arguments.
1805 * Returns 0 on success and fills out_args, on error returns appropriate
1808 * Caller is responsible to call of_node_put() on the returned out_args->np
1820 * list = <&phandle1 0 2 &phandle2 2 3>;
1823 * To get a device_node of the `node2' node you may call this:
1824 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1826 int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
1827 const char *list_name
, int cell_count
,
1828 int index
, struct of_phandle_args
*out_args
)
1832 return __of_parse_phandle_with_args(np
, list_name
, NULL
, cell_count
,
1835 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args
);
1838 * of_count_phandle_with_args() - Find the number of phandles references in a property
1839 * @np: pointer to a device tree node containing a list
1840 * @list_name: property name that contains a list
1841 * @cells_name: property name that specifies phandles' arguments count
1843 * Returns the number of phandle + argument tuples within a property. It
1844 * is a typical pattern to encode a list of phandle and variable
1845 * arguments into a single property. The number of arguments is encoded
1846 * by a property in the phandle-target node. For example, a gpios
1847 * property would contain a list of GPIO specifies consisting of a
1848 * phandle and 1 or more arguments. The number of arguments are
1849 * determined by the #gpio-cells property in the node pointed to by the
1852 int of_count_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1853 const char *cells_name
)
1855 struct of_phandle_iterator it
;
1856 int rc
, cur_index
= 0;
1858 rc
= of_phandle_iterator_init(&it
, np
, list_name
, cells_name
, 0);
1862 while ((rc
= of_phandle_iterator_next(&it
)) == 0)
1870 EXPORT_SYMBOL(of_count_phandle_with_args
);
1873 * __of_add_property - Add a property to a node without lock operations
1875 int __of_add_property(struct device_node
*np
, struct property
*prop
)
1877 struct property
**next
;
1880 next
= &np
->properties
;
1882 if (strcmp(prop
->name
, (*next
)->name
) == 0)
1883 /* duplicate ! don't insert it */
1886 next
= &(*next
)->next
;
1894 * of_add_property - Add a property to a node
1896 int of_add_property(struct device_node
*np
, struct property
*prop
)
1898 unsigned long flags
;
1901 mutex_lock(&of_mutex
);
1903 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1904 rc
= __of_add_property(np
, prop
);
1905 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1908 __of_add_property_sysfs(np
, prop
);
1910 mutex_unlock(&of_mutex
);
1913 of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
, NULL
);
1918 int __of_remove_property(struct device_node
*np
, struct property
*prop
)
1920 struct property
**next
;
1922 for (next
= &np
->properties
; *next
; next
= &(*next
)->next
) {
1929 /* found the node */
1931 prop
->next
= np
->deadprops
;
1932 np
->deadprops
= prop
;
1937 void __of_sysfs_remove_bin_file(struct device_node
*np
, struct property
*prop
)
1939 sysfs_remove_bin_file(&np
->kobj
, &prop
->attr
);
1940 kfree(prop
->attr
.attr
.name
);
1943 void __of_remove_property_sysfs(struct device_node
*np
, struct property
*prop
)
1945 if (!IS_ENABLED(CONFIG_SYSFS
))
1948 /* at early boot, bail here and defer setup to of_init() */
1949 if (of_kset
&& of_node_is_attached(np
))
1950 __of_sysfs_remove_bin_file(np
, prop
);
1954 * of_remove_property - Remove a property from a node.
1956 * Note that we don't actually remove it, since we have given out
1957 * who-knows-how-many pointers to the data using get-property.
1958 * Instead we just move the property to the "dead properties"
1959 * list, so it won't be found any more.
1961 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1963 unsigned long flags
;
1969 mutex_lock(&of_mutex
);
1971 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1972 rc
= __of_remove_property(np
, prop
);
1973 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1976 __of_remove_property_sysfs(np
, prop
);
1978 mutex_unlock(&of_mutex
);
1981 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
, NULL
);
1986 int __of_update_property(struct device_node
*np
, struct property
*newprop
,
1987 struct property
**oldpropp
)
1989 struct property
**next
, *oldprop
;
1991 for (next
= &np
->properties
; *next
; next
= &(*next
)->next
) {
1992 if (of_prop_cmp((*next
)->name
, newprop
->name
) == 0)
1995 *oldpropp
= oldprop
= *next
;
1998 /* replace the node */
1999 newprop
->next
= oldprop
->next
;
2001 oldprop
->next
= np
->deadprops
;
2002 np
->deadprops
= oldprop
;
2005 newprop
->next
= NULL
;
2012 void __of_update_property_sysfs(struct device_node
*np
, struct property
*newprop
,
2013 struct property
*oldprop
)
2015 if (!IS_ENABLED(CONFIG_SYSFS
))
2018 /* At early boot, bail out and defer setup to of_init() */
2023 __of_sysfs_remove_bin_file(np
, oldprop
);
2024 __of_add_property_sysfs(np
, newprop
);
2028 * of_update_property - Update a property in a node, if the property does
2029 * not exist, add it.
2031 * Note that we don't actually remove it, since we have given out
2032 * who-knows-how-many pointers to the data using get-property.
2033 * Instead we just move the property to the "dead properties" list,
2034 * and add the new property to the property list
2036 int of_update_property(struct device_node
*np
, struct property
*newprop
)
2038 struct property
*oldprop
;
2039 unsigned long flags
;
2045 mutex_lock(&of_mutex
);
2047 raw_spin_lock_irqsave(&devtree_lock
, flags
);
2048 rc
= __of_update_property(np
, newprop
, &oldprop
);
2049 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
2052 __of_update_property_sysfs(np
, newprop
, oldprop
);
2054 mutex_unlock(&of_mutex
);
2057 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
, oldprop
);
2062 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
2063 int id
, const char *stem
, int stem_len
)
2067 strncpy(ap
->stem
, stem
, stem_len
);
2068 ap
->stem
[stem_len
] = 0;
2069 list_add_tail(&ap
->link
, &aliases_lookup
);
2070 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
2071 ap
->alias
, ap
->stem
, ap
->id
, of_node_full_name(np
));
2075 * of_alias_scan - Scan all properties of the 'aliases' node
2077 * The function scans all the properties of the 'aliases' node and populates
2078 * the global lookup table with the properties. It returns the
2079 * number of alias properties found, or an error code in case of failure.
2081 * @dt_alloc: An allocator that provides a virtual address to memory
2082 * for storing the resulting tree
2084 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
2086 struct property
*pp
;
2088 of_aliases
= of_find_node_by_path("/aliases");
2089 of_chosen
= of_find_node_by_path("/chosen");
2090 if (of_chosen
== NULL
)
2091 of_chosen
= of_find_node_by_path("/chosen@0");
2094 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
2095 const char *name
= of_get_property(of_chosen
, "stdout-path", NULL
);
2097 name
= of_get_property(of_chosen
, "linux,stdout-path", NULL
);
2098 if (IS_ENABLED(CONFIG_PPC
) && !name
)
2099 name
= of_get_property(of_aliases
, "stdout", NULL
);
2101 of_stdout
= of_find_node_opts_by_path(name
, &of_stdout_options
);
2107 for_each_property_of_node(of_aliases
, pp
) {
2108 const char *start
= pp
->name
;
2109 const char *end
= start
+ strlen(start
);
2110 struct device_node
*np
;
2111 struct alias_prop
*ap
;
2114 /* Skip those we do not want to proceed */
2115 if (!strcmp(pp
->name
, "name") ||
2116 !strcmp(pp
->name
, "phandle") ||
2117 !strcmp(pp
->name
, "linux,phandle"))
2120 np
= of_find_node_by_path(pp
->value
);
2124 /* walk the alias backwards to extract the id and work out
2125 * the 'stem' string */
2126 while (isdigit(*(end
-1)) && end
> start
)
2130 if (kstrtoint(end
, 10, &id
) < 0)
2133 /* Allocate an alias_prop with enough space for the stem */
2134 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, __alignof__(*ap
));
2137 memset(ap
, 0, sizeof(*ap
) + len
+ 1);
2139 of_alias_add(ap
, np
, id
, start
, len
);
2144 * of_alias_get_id - Get alias id for the given device_node
2145 * @np: Pointer to the given device_node
2146 * @stem: Alias stem of the given device_node
2148 * The function travels the lookup table to get the alias id for the given
2149 * device_node and alias stem. It returns the alias id if found.
2151 int of_alias_get_id(struct device_node
*np
, const char *stem
)
2153 struct alias_prop
*app
;
2156 mutex_lock(&of_mutex
);
2157 list_for_each_entry(app
, &aliases_lookup
, link
) {
2158 if (strcmp(app
->stem
, stem
) != 0)
2161 if (np
== app
->np
) {
2166 mutex_unlock(&of_mutex
);
2170 EXPORT_SYMBOL_GPL(of_alias_get_id
);
2173 * of_alias_get_highest_id - Get highest alias id for the given stem
2174 * @stem: Alias stem to be examined
2176 * The function travels the lookup table to get the highest alias id for the
2177 * given alias stem. It returns the alias id if found.
2179 int of_alias_get_highest_id(const char *stem
)
2181 struct alias_prop
*app
;
2184 mutex_lock(&of_mutex
);
2185 list_for_each_entry(app
, &aliases_lookup
, link
) {
2186 if (strcmp(app
->stem
, stem
) != 0)
2192 mutex_unlock(&of_mutex
);
2196 EXPORT_SYMBOL_GPL(of_alias_get_highest_id
);
2198 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
2201 const void *curv
= cur
;
2211 curv
+= sizeof(*cur
);
2212 if (curv
>= prop
->value
+ prop
->length
)
2216 *pu
= be32_to_cpup(curv
);
2219 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
2221 const char *of_prop_next_string(struct property
*prop
, const char *cur
)
2223 const void *curv
= cur
;
2231 curv
+= strlen(cur
) + 1;
2232 if (curv
>= prop
->value
+ prop
->length
)
2237 EXPORT_SYMBOL_GPL(of_prop_next_string
);
2240 * of_console_check() - Test and setup console for DT setup
2241 * @dn - Pointer to device node
2242 * @name - Name to use for preferred console without index. ex. "ttyS"
2243 * @index - Index to use for preferred console.
2245 * Check if the given device node matches the stdout-path property in the
2246 * /chosen node. If it does then register it as the preferred console and return
2247 * TRUE. Otherwise return FALSE.
2249 bool of_console_check(struct device_node
*dn
, char *name
, int index
)
2251 if (!dn
|| dn
!= of_stdout
|| console_set_on_cmdline
)
2253 return !add_preferred_console(name
, index
,
2254 kstrdup(of_stdout_options
, GFP_KERNEL
));
2256 EXPORT_SYMBOL_GPL(of_console_check
);
2259 * of_find_next_cache_node - Find a node's subsidiary cache
2260 * @np: node of type "cpu" or "cache"
2262 * Returns a node pointer with refcount incremented, use
2263 * of_node_put() on it when done. Caller should hold a reference
2266 struct device_node
*of_find_next_cache_node(const struct device_node
*np
)
2268 struct device_node
*child
;
2269 const phandle
*handle
;
2271 handle
= of_get_property(np
, "l2-cache", NULL
);
2273 handle
= of_get_property(np
, "next-level-cache", NULL
);
2276 return of_find_node_by_phandle(be32_to_cpup(handle
));
2278 /* OF on pmac has nodes instead of properties named "l2-cache"
2279 * beneath CPU nodes.
2281 if (IS_ENABLED(CONFIG_PPC_PMAC
) && !strcmp(np
->type
, "cpu"))
2282 for_each_child_of_node(np
, child
)
2283 if (!strcmp(child
->type
, "cache"))
2290 * of_graph_parse_endpoint() - parse common endpoint node properties
2291 * @node: pointer to endpoint device_node
2292 * @endpoint: pointer to the OF endpoint data structure
2294 * The caller should hold a reference to @node.
2296 int of_graph_parse_endpoint(const struct device_node
*node
,
2297 struct of_endpoint
*endpoint
)
2299 struct device_node
*port_node
= of_get_parent(node
);
2301 WARN_ONCE(!port_node
, "%s(): endpoint %s has no parent node\n",
2302 __func__
, node
->full_name
);
2304 memset(endpoint
, 0, sizeof(*endpoint
));
2306 endpoint
->local_node
= node
;
2308 * It doesn't matter whether the two calls below succeed.
2309 * If they don't then the default value 0 is used.
2311 of_property_read_u32(port_node
, "reg", &endpoint
->port
);
2312 of_property_read_u32(node
, "reg", &endpoint
->id
);
2314 of_node_put(port_node
);
2318 EXPORT_SYMBOL(of_graph_parse_endpoint
);
2321 * of_graph_get_port_by_id() - get the port matching a given id
2322 * @parent: pointer to the parent device node
2323 * @id: id of the port
2325 * Return: A 'port' node pointer with refcount incremented. The caller
2326 * has to use of_node_put() on it when done.
2328 struct device_node
*of_graph_get_port_by_id(struct device_node
*parent
, u32 id
)
2330 struct device_node
*node
, *port
;
2332 node
= of_get_child_by_name(parent
, "ports");
2336 for_each_child_of_node(parent
, port
) {
2339 if (of_node_cmp(port
->name
, "port") != 0)
2341 of_property_read_u32(port
, "reg", &port_id
);
2350 EXPORT_SYMBOL(of_graph_get_port_by_id
);
2353 * of_graph_get_next_endpoint() - get next endpoint node
2354 * @parent: pointer to the parent device node
2355 * @prev: previous endpoint node, or NULL to get first
2357 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2358 * of the passed @prev node is decremented.
2360 struct device_node
*of_graph_get_next_endpoint(const struct device_node
*parent
,
2361 struct device_node
*prev
)
2363 struct device_node
*endpoint
;
2364 struct device_node
*port
;
2370 * Start by locating the port node. If no previous endpoint is specified
2371 * search for the first port node, otherwise get the previous endpoint
2375 struct device_node
*node
;
2377 node
= of_get_child_by_name(parent
, "ports");
2381 port
= of_get_child_by_name(parent
, "port");
2385 pr_err("graph: no port node found in %s\n",
2390 port
= of_get_parent(prev
);
2391 if (WARN_ONCE(!port
, "%s(): endpoint %s has no parent node\n",
2392 __func__
, prev
->full_name
))
2398 * Now that we have a port node, get the next endpoint by
2399 * getting the next child. If the previous endpoint is NULL this
2400 * will return the first child.
2402 endpoint
= of_get_next_child(port
, prev
);
2408 /* No more endpoints under this port, try the next one. */
2412 port
= of_get_next_child(parent
, port
);
2415 } while (of_node_cmp(port
->name
, "port"));
2418 EXPORT_SYMBOL(of_graph_get_next_endpoint
);
2421 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
2422 * @parent: pointer to the parent device node
2423 * @port_reg: identifier (value of reg property) of the parent port node
2424 * @reg: identifier (value of reg property) of the endpoint node
2426 * Return: An 'endpoint' node pointer which is identified by reg and at the same
2427 * is the child of a port node identified by port_reg. reg and port_reg are
2428 * ignored when they are -1.
2430 struct device_node
*of_graph_get_endpoint_by_regs(
2431 const struct device_node
*parent
, int port_reg
, int reg
)
2433 struct of_endpoint endpoint
;
2434 struct device_node
*node
= NULL
;
2436 for_each_endpoint_of_node(parent
, node
) {
2437 of_graph_parse_endpoint(node
, &endpoint
);
2438 if (((port_reg
== -1) || (endpoint
.port
== port_reg
)) &&
2439 ((reg
== -1) || (endpoint
.id
== reg
)))
2445 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs
);
2448 * of_graph_get_remote_port_parent() - get remote port's parent node
2449 * @node: pointer to a local endpoint device_node
2451 * Return: Remote device node associated with remote endpoint node linked
2452 * to @node. Use of_node_put() on it when done.
2454 struct device_node
*of_graph_get_remote_port_parent(
2455 const struct device_node
*node
)
2457 struct device_node
*np
;
2460 /* Get remote endpoint node. */
2461 np
= of_parse_phandle(node
, "remote-endpoint", 0);
2463 /* Walk 3 levels up only if there is 'ports' node. */
2464 for (depth
= 3; depth
&& np
; depth
--) {
2465 np
= of_get_next_parent(np
);
2466 if (depth
== 2 && of_node_cmp(np
->name
, "ports"))
2471 EXPORT_SYMBOL(of_graph_get_remote_port_parent
);
2474 * of_graph_get_remote_port() - get remote port node
2475 * @node: pointer to a local endpoint device_node
2477 * Return: Remote port node associated with remote endpoint node linked
2478 * to @node. Use of_node_put() on it when done.
2480 struct device_node
*of_graph_get_remote_port(const struct device_node
*node
)
2482 struct device_node
*np
;
2484 /* Get remote endpoint node. */
2485 np
= of_parse_phandle(node
, "remote-endpoint", 0);
2488 return of_get_next_parent(np
);
2490 EXPORT_SYMBOL(of_graph_get_remote_port
);