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/string.h>
26 #include <linux/proc_fs.h>
28 #include "of_private.h"
30 LIST_HEAD(aliases_lookup);
32 struct device_node *of_allnodes;
33 EXPORT_SYMBOL(of_allnodes);
34 struct device_node *of_chosen;
35 struct device_node *of_aliases;
36 static struct device_node *of_stdout;
41 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
42 * This mutex must be held whenever modifications are being made to the
43 * device tree. The of_{attach,detach}_node() and
44 * of_{add,remove,update}_property() helpers make sure this happens.
46 DEFINE_MUTEX(of_mutex);
48 /* use when traversing tree through the allnext, child, sibling,
49 * or parent members of struct device_node.
51 DEFINE_RAW_SPINLOCK(devtree_lock);
53 int of_n_addr_cells(struct device_node *np)
60 ip = of_get_property(np, "#address-cells", NULL);
62 return be32_to_cpup(ip);
64 /* No #address-cells property for the root node */
65 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
67 EXPORT_SYMBOL(of_n_addr_cells);
69 int of_n_size_cells(struct device_node *np)
76 ip = of_get_property(np, "#size-cells", NULL);
78 return be32_to_cpup(ip);
80 /* No #size-cells property for the root node */
81 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
83 EXPORT_SYMBOL(of_n_size_cells);
85 #ifndef CONFIG_OF_DYNAMIC
86 static void of_node_release(struct kobject *kobj)
88 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
90 #endif /* CONFIG_OF_DYNAMIC */
92 struct kobj_type of_node_ktype = {
93 .release = of_node_release,
96 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
97 struct bin_attribute *bin_attr, char *buf,
98 loff_t offset, size_t count)
100 struct property *pp = container_of(bin_attr, struct property, attr);
101 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
104 static const char *safe_name(struct kobject *kobj, const char *orig_name)
106 const char *name = orig_name;
107 struct sysfs_dirent *kn;
110 /* don't be a hero. After 16 tries give up */
111 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, NULL, name))) {
113 if (name != orig_name)
115 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
118 if (name != orig_name)
119 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
120 kobject_name(kobj), name);
124 int __of_add_property_sysfs(struct device_node *np, struct property *pp)
128 /* Important: Don't leak passwords */
129 bool secure = strncmp(pp->name, "security-", 9) == 0;
131 if (!of_kset || !of_node_is_attached(np))
134 sysfs_bin_attr_init(&pp->attr);
135 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
136 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
137 pp->attr.size = secure ? 0 : pp->length;
138 pp->attr.read = of_node_property_read;
140 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
141 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
145 int __of_attach_node_sysfs(struct device_node *np)
154 np->kobj.kset = of_kset;
156 /* Nodes without parents are new top level trees */
157 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
159 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
160 if (!name || !name[0])
163 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
168 for_each_property_of_node(np, pp)
169 __of_add_property_sysfs(np, pp);
174 static int __init of_init(void)
176 struct device_node *np;
178 /* Create the kset, and register existing nodes */
179 mutex_lock(&of_mutex);
180 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
182 mutex_unlock(&of_mutex);
185 for_each_of_allnodes(np)
186 __of_attach_node_sysfs(np);
187 mutex_unlock(&of_mutex);
189 /* Symlink in /proc as required by userspace ABI */
191 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
195 core_initcall(of_init);
197 static struct property *__of_find_property(const struct device_node *np,
198 const char *name, int *lenp)
205 for (pp = np->properties; pp; pp = pp->next) {
206 if (of_prop_cmp(pp->name, name) == 0) {
216 struct property *of_find_property(const struct device_node *np,
223 raw_spin_lock_irqsave(&devtree_lock, flags);
224 pp = __of_find_property(np, name, lenp);
225 raw_spin_unlock_irqrestore(&devtree_lock, flags);
229 EXPORT_SYMBOL(of_find_property);
232 * of_find_all_nodes - Get next node in global list
233 * @prev: Previous node or NULL to start iteration
234 * of_node_put() will be called on it
236 * Returns a node pointer with refcount incremented, use
237 * of_node_put() on it when done.
239 struct device_node *of_find_all_nodes(struct device_node *prev)
241 struct device_node *np;
244 raw_spin_lock_irqsave(&devtree_lock, flags);
245 np = prev ? prev->allnext : of_allnodes;
246 for (; np != NULL; np = np->allnext)
250 raw_spin_unlock_irqrestore(&devtree_lock, flags);
253 EXPORT_SYMBOL(of_find_all_nodes);
256 * Find a property with a given name for a given node
257 * and return the value.
259 const void *__of_get_property(const struct device_node *np,
260 const char *name, int *lenp)
262 struct property *pp = __of_find_property(np, name, lenp);
264 return pp ? pp->value : NULL;
268 * Find a property with a given name for a given node
269 * and return the value.
271 const void *of_get_property(const struct device_node *np, const char *name,
274 struct property *pp = of_find_property(np, name, lenp);
276 return pp ? pp->value : NULL;
278 EXPORT_SYMBOL(of_get_property);
280 /** Checks if the given "compat" string matches one of the strings in
281 * the device's "compatible" property
283 static int __of_device_is_compatible(const struct device_node *device,
289 cp = __of_get_property(device, "compatible", &cplen);
293 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
303 /** Checks if the given "compat" string matches one of the strings in
304 * the device's "compatible" property
306 int of_device_is_compatible(const struct device_node *device,
312 raw_spin_lock_irqsave(&devtree_lock, flags);
313 res = __of_device_is_compatible(device, compat);
314 raw_spin_unlock_irqrestore(&devtree_lock, flags);
317 EXPORT_SYMBOL(of_device_is_compatible);
320 * of_machine_is_compatible - Test root of device tree for a given compatible value
321 * @compat: compatible string to look for in root node's compatible property.
323 * Returns true if the root node has the given value in its
324 * compatible property.
326 int of_machine_is_compatible(const char *compat)
328 struct device_node *root;
331 root = of_find_node_by_path("/");
333 rc = of_device_is_compatible(root, compat);
338 EXPORT_SYMBOL(of_machine_is_compatible);
341 * __of_device_is_available - check if a device is available for use
343 * @device: Node to check for availability, with locks already held
345 * Returns 1 if the status property is absent or set to "okay" or "ok",
348 static int __of_device_is_available(const struct device_node *device)
353 status = __of_get_property(device, "status", &statlen);
358 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
366 * of_device_is_available - check if a device is available for use
368 * @device: Node to check for availability
370 * Returns 1 if the status property is absent or set to "okay" or "ok",
373 int of_device_is_available(const struct device_node *device)
378 raw_spin_lock_irqsave(&devtree_lock, flags);
379 res = __of_device_is_available(device);
380 raw_spin_unlock_irqrestore(&devtree_lock, flags);
384 EXPORT_SYMBOL(of_device_is_available);
387 * of_get_parent - Get a node's parent if any
388 * @node: Node to get parent
390 * Returns a node pointer with refcount incremented, use
391 * of_node_put() on it when done.
393 struct device_node *of_get_parent(const struct device_node *node)
395 struct device_node *np;
401 raw_spin_lock_irqsave(&devtree_lock, flags);
402 np = of_node_get(node->parent);
403 raw_spin_unlock_irqrestore(&devtree_lock, flags);
406 EXPORT_SYMBOL(of_get_parent);
409 * of_get_next_parent - Iterate to a node's parent
410 * @node: Node to get parent of
412 * This is like of_get_parent() except that it drops the
413 * refcount on the passed node, making it suitable for iterating
414 * through a node's parents.
416 * Returns a node pointer with refcount incremented, use
417 * of_node_put() on it when done.
419 struct device_node *of_get_next_parent(struct device_node *node)
421 struct device_node *parent;
427 raw_spin_lock_irqsave(&devtree_lock, flags);
428 parent = of_node_get(node->parent);
430 raw_spin_unlock_irqrestore(&devtree_lock, flags);
433 EXPORT_SYMBOL(of_get_next_parent);
436 * of_get_next_child - Iterate a node childs
438 * @prev: previous child of the parent node, or NULL to get first
440 * Returns a node pointer with refcount incremented, use
441 * of_node_put() on it when done.
443 struct device_node *of_get_next_child(const struct device_node *node,
444 struct device_node *prev)
446 struct device_node *next;
449 raw_spin_lock_irqsave(&devtree_lock, flags);
450 next = prev ? prev->sibling : node->child;
451 for (; next; next = next->sibling)
452 if (of_node_get(next))
455 raw_spin_unlock_irqrestore(&devtree_lock, flags);
458 EXPORT_SYMBOL(of_get_next_child);
461 * of_get_next_available_child - Find the next available child node
463 * @prev: previous child of the parent node, or NULL to get first
465 * This function is like of_get_next_child(), except that it
466 * automatically skips any disabled nodes (i.e. status = "disabled").
468 struct device_node *of_get_next_available_child(const struct device_node *node,
469 struct device_node *prev)
471 struct device_node *next;
474 raw_spin_lock_irqsave(&devtree_lock, flags);
475 next = prev ? prev->sibling : node->child;
476 for (; next; next = next->sibling) {
477 if (!__of_device_is_available(next))
479 if (of_node_get(next))
483 raw_spin_unlock_irqrestore(&devtree_lock, flags);
486 EXPORT_SYMBOL(of_get_next_available_child);
489 * of_get_child_by_name - Find the child node by name for a given parent
491 * @name: child name to look for.
493 * This function looks for child node for given matching name
495 * Returns a node pointer if found, with refcount incremented, use
496 * of_node_put() on it when done.
497 * Returns NULL if node is not found.
499 struct device_node *of_get_child_by_name(const struct device_node *node,
502 struct device_node *child;
504 for_each_child_of_node(node, child)
505 if (child->name && (of_node_cmp(child->name, name) == 0))
509 EXPORT_SYMBOL(of_get_child_by_name);
512 * of_find_node_by_path - Find a node matching a full OF path
513 * @path: The full path to match
515 * Returns a node pointer with refcount incremented, use
516 * of_node_put() on it when done.
518 struct device_node *of_find_node_by_path(const char *path)
520 struct device_node *np = of_allnodes;
523 raw_spin_lock_irqsave(&devtree_lock, flags);
524 for (; np; np = np->allnext) {
525 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
529 raw_spin_unlock_irqrestore(&devtree_lock, flags);
532 EXPORT_SYMBOL(of_find_node_by_path);
535 * of_find_node_by_name - Find a node by its "name" property
536 * @from: The node to start searching from or NULL, the node
537 * you pass will not be searched, only the next one
538 * will; typically, you pass what the previous call
539 * returned. of_node_put() will be called on it
540 * @name: The name string to match against
542 * Returns a node pointer with refcount incremented, use
543 * of_node_put() on it when done.
545 struct device_node *of_find_node_by_name(struct device_node *from,
548 struct device_node *np;
551 raw_spin_lock_irqsave(&devtree_lock, flags);
552 np = from ? from->allnext : of_allnodes;
553 for (; np; np = np->allnext)
554 if (np->name && (of_node_cmp(np->name, name) == 0)
558 raw_spin_unlock_irqrestore(&devtree_lock, flags);
561 EXPORT_SYMBOL(of_find_node_by_name);
564 * of_find_node_by_type - Find a node by its "device_type" property
565 * @from: The node to start searching from, or NULL to start searching
566 * the entire device tree. The node you pass will not be
567 * searched, only the next one will; typically, you pass
568 * what the previous call returned. of_node_put() will be
569 * called on from for you.
570 * @type: The type string to match against
572 * Returns a node pointer with refcount incremented, use
573 * of_node_put() on it when done.
575 struct device_node *of_find_node_by_type(struct device_node *from,
578 struct device_node *np;
581 raw_spin_lock_irqsave(&devtree_lock, flags);
582 np = from ? from->allnext : of_allnodes;
583 for (; np; np = np->allnext)
584 if (np->type && (of_node_cmp(np->type, type) == 0)
588 raw_spin_unlock_irqrestore(&devtree_lock, flags);
591 EXPORT_SYMBOL(of_find_node_by_type);
594 * of_find_compatible_node - Find a node based on type and one of the
595 * tokens in its "compatible" property
596 * @from: The node to start searching from or NULL, the node
597 * you pass will not be searched, only the next one
598 * will; typically, you pass what the previous call
599 * returned. of_node_put() will be called on it
600 * @type: The type string to match "device_type" or NULL to ignore
601 * @compatible: The string to match to one of the tokens in the device
604 * Returns a node pointer with refcount incremented, use
605 * of_node_put() on it when done.
607 struct device_node *of_find_compatible_node(struct device_node *from,
608 const char *type, const char *compatible)
610 struct device_node *np;
613 raw_spin_lock_irqsave(&devtree_lock, flags);
614 np = from ? from->allnext : of_allnodes;
615 for (; np; np = np->allnext) {
617 && !(np->type && (of_node_cmp(np->type, type) == 0)))
619 if (__of_device_is_compatible(np, compatible) &&
624 raw_spin_unlock_irqrestore(&devtree_lock, flags);
627 EXPORT_SYMBOL(of_find_compatible_node);
630 * of_find_node_with_property - Find a node which has a property with
632 * @from: The node to start searching from or NULL, the node
633 * you pass will not be searched, only the next one
634 * will; typically, you pass what the previous call
635 * returned. of_node_put() will be called on it
636 * @prop_name: The name of the property to look for.
638 * Returns a node pointer with refcount incremented, use
639 * of_node_put() on it when done.
641 struct device_node *of_find_node_with_property(struct device_node *from,
642 const char *prop_name)
644 struct device_node *np;
648 raw_spin_lock_irqsave(&devtree_lock, flags);
649 np = from ? from->allnext : of_allnodes;
650 for (; np; np = np->allnext) {
651 for (pp = np->properties; pp; pp = pp->next) {
652 if (of_prop_cmp(pp->name, prop_name) == 0) {
660 raw_spin_unlock_irqrestore(&devtree_lock, flags);
663 EXPORT_SYMBOL(of_find_node_with_property);
666 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
667 const struct device_node *node)
672 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
674 if (matches->name[0])
676 && !strcmp(matches->name, node->name);
677 if (matches->type[0])
679 && !strcmp(matches->type, node->type);
680 if (matches->compatible[0])
681 match &= __of_device_is_compatible(node,
682 matches->compatible);
691 * of_match_node - Tell if an device_node has a matching of_match structure
692 * @matches: array of of device match structures to search in
693 * @node: the of device structure to match against
695 * Low level utility function used by device matching.
697 const struct of_device_id *of_match_node(const struct of_device_id *matches,
698 const struct device_node *node)
700 const struct of_device_id *match;
703 raw_spin_lock_irqsave(&devtree_lock, flags);
704 match = __of_match_node(matches, node);
705 raw_spin_unlock_irqrestore(&devtree_lock, flags);
708 EXPORT_SYMBOL(of_match_node);
711 * of_find_matching_node_and_match - Find a node based on an of_device_id
713 * @from: The node to start searching from or NULL, the node
714 * you pass will not be searched, only the next one
715 * will; typically, you pass what the previous call
716 * returned. of_node_put() will be called on it
717 * @matches: array of of device match structures to search in
718 * @match Updated to point at the matches entry which matched
720 * Returns a node pointer with refcount incremented, use
721 * of_node_put() on it when done.
723 struct device_node *of_find_matching_node_and_match(struct device_node *from,
724 const struct of_device_id *matches,
725 const struct of_device_id **match)
727 struct device_node *np;
728 const struct of_device_id *m;
734 raw_spin_lock_irqsave(&devtree_lock, flags);
735 np = from ? from->allnext : of_allnodes;
736 for (; np; np = np->allnext) {
737 m = __of_match_node(matches, np);
738 if (m && of_node_get(np)) {
745 raw_spin_unlock_irqrestore(&devtree_lock, flags);
748 EXPORT_SYMBOL(of_find_matching_node_and_match);
751 * of_modalias_node - Lookup appropriate modalias for a device node
752 * @node: pointer to a device tree node
753 * @modalias: Pointer to buffer that modalias value will be copied into
754 * @len: Length of modalias value
756 * Based on the value of the compatible property, this routine will attempt
757 * to choose an appropriate modalias value for a particular device tree node.
758 * It does this by stripping the manufacturer prefix (as delimited by a ',')
759 * from the first entry in the compatible list property.
761 * This routine returns 0 on success, <0 on failure.
763 int of_modalias_node(struct device_node *node, char *modalias, int len)
765 const char *compatible, *p;
768 compatible = of_get_property(node, "compatible", &cplen);
769 if (!compatible || strlen(compatible) > cplen)
771 p = strchr(compatible, ',');
772 strlcpy(modalias, p ? p + 1 : compatible, len);
775 EXPORT_SYMBOL_GPL(of_modalias_node);
778 * of_find_node_by_phandle - Find a node given a phandle
779 * @handle: phandle of the node to find
781 * Returns a node pointer with refcount incremented, use
782 * of_node_put() on it when done.
784 struct device_node *of_find_node_by_phandle(phandle handle)
786 struct device_node *np;
789 raw_spin_lock_irqsave(&devtree_lock, flags);
790 for (np = of_allnodes; np; np = np->allnext)
791 if (np->phandle == handle)
794 raw_spin_unlock_irqrestore(&devtree_lock, flags);
797 EXPORT_SYMBOL(of_find_node_by_phandle);
800 * of_property_count_elems_of_size - Count the number of elements in a property
802 * @np: device node from which the property value is to be read.
803 * @propname: name of the property to be searched.
804 * @elem_size: size of the individual element
806 * Search for a property in a device node and count the number of elements of
807 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
808 * property does not exist or its length does not match a multiple of elem_size
809 * and -ENODATA if the property does not have a value.
811 int of_property_count_elems_of_size(const struct device_node *np,
812 const char *propname, int elem_size)
814 struct property *prop = of_find_property(np, propname, NULL);
821 if (prop->length % elem_size != 0) {
822 pr_err("size of %s in node %s is not a multiple of %d\n",
823 propname, np->full_name, elem_size);
827 return prop->length / elem_size;
829 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
832 * of_find_property_value_of_size
834 * @np: device node from which the property value is to be read.
835 * @propname: name of the property to be searched.
836 * @len: requested length of property value
838 * Search for a property in a device node and valid the requested size.
839 * Returns the property value on success, -EINVAL if the property does not
840 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
841 * property data isn't large enough.
844 static void *of_find_property_value_of_size(const struct device_node *np,
845 const char *propname, u32 len)
847 struct property *prop = of_find_property(np, propname, NULL);
850 return ERR_PTR(-EINVAL);
852 return ERR_PTR(-ENODATA);
853 if (len > prop->length)
854 return ERR_PTR(-EOVERFLOW);
860 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
862 * @np: device node from which the property value is to be read.
863 * @propname: name of the property to be searched.
864 * @index: index of the u32 in the list of values
865 * @out_value: pointer to return value, modified only if no error.
867 * Search for a property in a device node and read nth 32-bit value from
868 * it. Returns 0 on success, -EINVAL if the property does not exist,
869 * -ENODATA if property does not have a value, and -EOVERFLOW if the
870 * property data isn't large enough.
872 * The out_value is modified only if a valid u32 value can be decoded.
874 int of_property_read_u32_index(const struct device_node *np,
875 const char *propname,
876 u32 index, u32 *out_value)
878 const u32 *val = of_find_property_value_of_size(np, propname,
879 ((index + 1) * sizeof(*out_value)));
884 *out_value = be32_to_cpup(((__be32 *)val) + index);
887 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
890 * of_property_read_u8_array - Find and read an array of u8 from a property.
892 * @np: device node from which the property value is to be read.
893 * @propname: name of the property to be searched.
894 * @out_value: pointer to return value, modified only if return value is 0.
895 * @sz: number of array elements to read
897 * Search for a property in a device node and read 8-bit value(s) from
898 * it. Returns 0 on success, -EINVAL if the property does not exist,
899 * -ENODATA if property does not have a value, and -EOVERFLOW if the
900 * property data isn't large enough.
902 * dts entry of array should be like:
903 * property = /bits/ 8 <0x50 0x60 0x70>;
905 * The out_value is modified only if a valid u8 value can be decoded.
907 int of_property_read_u8_array(const struct device_node *np,
908 const char *propname, u8 *out_values, size_t sz)
910 const u8 *val = of_find_property_value_of_size(np, propname,
911 (sz * sizeof(*out_values)));
917 *out_values++ = *val++;
920 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
923 * of_property_read_u16_array - Find and read an array of u16 from a property.
925 * @np: device node from which the property value is to be read.
926 * @propname: name of the property to be searched.
927 * @out_value: pointer to return value, modified only if return value is 0.
928 * @sz: number of array elements to read
930 * Search for a property in a device node and read 16-bit value(s) from
931 * it. Returns 0 on success, -EINVAL if the property does not exist,
932 * -ENODATA if property does not have a value, and -EOVERFLOW if the
933 * property data isn't large enough.
935 * dts entry of array should be like:
936 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
938 * The out_value is modified only if a valid u16 value can be decoded.
940 int of_property_read_u16_array(const struct device_node *np,
941 const char *propname, u16 *out_values, size_t sz)
943 const __be16 *val = of_find_property_value_of_size(np, propname,
944 (sz * sizeof(*out_values)));
950 *out_values++ = be16_to_cpup(val++);
953 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
956 * of_property_read_u32_array - Find and read an array of 32 bit integers
959 * @np: device node from which the property value is to be read.
960 * @propname: name of the property to be searched.
961 * @out_value: pointer to return value, modified only if return value is 0.
962 * @sz: number of array elements to read
964 * Search for a property in a device node and read 32-bit value(s) from
965 * it. Returns 0 on success, -EINVAL if the property does not exist,
966 * -ENODATA if property does not have a value, and -EOVERFLOW if the
967 * property data isn't large enough.
969 * The out_value is modified only if a valid u32 value can be decoded.
971 int of_property_read_u32_array(const struct device_node *np,
972 const char *propname, u32 *out_values,
975 const __be32 *val = of_find_property_value_of_size(np, propname,
976 (sz * sizeof(*out_values)));
982 *out_values++ = be32_to_cpup(val++);
985 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
988 * of_property_read_u64 - Find and read a 64 bit integer from a property
989 * @np: device node from which the property value is to be read.
990 * @propname: name of the property to be searched.
991 * @out_value: pointer to return value, modified only if return value is 0.
993 * Search for a property in a device node and read a 64-bit value from
994 * it. Returns 0 on success, -EINVAL if the property does not exist,
995 * -ENODATA if property does not have a value, and -EOVERFLOW if the
996 * property data isn't large enough.
998 * The out_value is modified only if a valid u64 value can be decoded.
1000 int of_property_read_u64(const struct device_node *np, const char *propname,
1003 const __be32 *val = of_find_property_value_of_size(np, propname,
1004 sizeof(*out_value));
1007 return PTR_ERR(val);
1009 *out_value = of_read_number(val, 2);
1012 EXPORT_SYMBOL_GPL(of_property_read_u64);
1015 * of_property_read_string - Find and read a string from a property
1016 * @np: device node from which the property value is to be read.
1017 * @propname: name of the property to be searched.
1018 * @out_string: pointer to null terminated return string, modified only if
1019 * return value is 0.
1021 * Search for a property in a device tree node and retrieve a null
1022 * terminated string value (pointer to data, not a copy). Returns 0 on
1023 * success, -EINVAL if the property does not exist, -ENODATA if property
1024 * does not have a value, and -EILSEQ if the string is not null-terminated
1025 * within the length of the property data.
1027 * The out_string pointer is modified only if a valid string can be decoded.
1029 int of_property_read_string(struct device_node *np, const char *propname,
1030 const char **out_string)
1032 struct property *prop = of_find_property(np, propname, NULL);
1037 if (strnlen(prop->value, prop->length) >= prop->length)
1039 *out_string = prop->value;
1042 EXPORT_SYMBOL_GPL(of_property_read_string);
1045 * of_property_read_string_index - Find and read a string from a multiple
1047 * @np: device node from which the property value is to be read.
1048 * @propname: name of the property to be searched.
1049 * @index: index of the string in the list of strings
1050 * @out_string: pointer to null terminated return string, modified only if
1051 * return value is 0.
1053 * Search for a property in a device tree node and retrieve a null
1054 * terminated string value (pointer to data, not a copy) in the list of strings
1055 * contained in that property.
1056 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1057 * property does not have a value, and -EILSEQ if the string is not
1058 * null-terminated within the length of the property data.
1060 * The out_string pointer is modified only if a valid string can be decoded.
1062 int of_property_read_string_index(struct device_node *np, const char *propname,
1063 int index, const char **output)
1065 struct property *prop = of_find_property(np, propname, NULL);
1067 size_t l = 0, total = 0;
1074 if (strnlen(prop->value, prop->length) >= prop->length)
1079 for (i = 0; total < prop->length; total += l, p += l) {
1088 EXPORT_SYMBOL_GPL(of_property_read_string_index);
1091 * of_property_match_string() - Find string in a list and return index
1092 * @np: pointer to node containing string list property
1093 * @propname: string list property name
1094 * @string: pointer to string to search for in string list
1096 * This function searches a string list property and returns the index
1097 * of a specific string value.
1099 int of_property_match_string(struct device_node *np, const char *propname,
1102 struct property *prop = of_find_property(np, propname, NULL);
1105 const char *p, *end;
1113 end = p + prop->length;
1115 for (i = 0; p < end; i++, p += l) {
1119 pr_debug("comparing %s with %s\n", string, p);
1120 if (strcmp(string, p) == 0)
1121 return i; /* Found it; return index */
1125 EXPORT_SYMBOL_GPL(of_property_match_string);
1128 * of_property_count_strings - Find and return the number of strings from a
1129 * multiple strings property.
1130 * @np: device node from which the property value is to be read.
1131 * @propname: name of the property to be searched.
1133 * Search for a property in a device tree node and retrieve the number of null
1134 * terminated string contain in it. Returns the number of strings on
1135 * success, -EINVAL if the property does not exist, -ENODATA if property
1136 * does not have a value, and -EILSEQ if the string is not null-terminated
1137 * within the length of the property data.
1139 int of_property_count_strings(struct device_node *np, const char *propname)
1141 struct property *prop = of_find_property(np, propname, NULL);
1143 size_t l = 0, total = 0;
1150 if (strnlen(prop->value, prop->length) >= prop->length)
1155 for (i = 0; total < prop->length; total += l, p += l, i++)
1160 EXPORT_SYMBOL_GPL(of_property_count_strings);
1162 static int __of_parse_phandle_with_args(const struct device_node *np,
1163 const char *list_name,
1164 const char *cells_name,
1165 int cell_count, int index,
1166 struct of_phandle_args *out_args)
1168 const __be32 *list, *list_end;
1169 int rc = 0, size, cur_index = 0;
1171 struct device_node *node = NULL;
1174 /* Retrieve the phandle list property */
1175 list = of_get_property(np, list_name, &size);
1178 list_end = list + size / sizeof(*list);
1180 /* Loop over the phandles until all the requested entry is found */
1181 while (list < list_end) {
1186 * If phandle is 0, then it is an empty entry with no
1187 * arguments. Skip forward to the next entry.
1189 phandle = be32_to_cpup(list++);
1192 * Find the provider node and parse the #*-cells
1193 * property to determine the argument length.
1195 * This is not needed if the cell count is hard-coded
1196 * (i.e. cells_name not set, but cell_count is set),
1197 * except when we're going to return the found node
1200 if (cells_name || cur_index == index) {
1201 node = of_find_node_by_phandle(phandle);
1203 pr_err("%s: could not find phandle\n",
1210 if (of_property_read_u32(node, cells_name,
1212 pr_err("%s: could not get %s for %s\n",
1213 np->full_name, cells_name,
1222 * Make sure that the arguments actually fit in the
1223 * remaining property data length
1225 if (list + count > list_end) {
1226 pr_err("%s: arguments longer than property\n",
1233 * All of the error cases above bail out of the loop, so at
1234 * this point, the parsing is successful. If the requested
1235 * index matches, then fill the out_args structure and return,
1236 * or return -ENOENT for an empty entry.
1239 if (cur_index == index) {
1245 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1246 count = MAX_PHANDLE_ARGS;
1247 out_args->np = node;
1248 out_args->args_count = count;
1249 for (i = 0; i < count; i++)
1250 out_args->args[i] = be32_to_cpup(list++);
1255 /* Found it! return success */
1266 * Unlock node before returning result; will be one of:
1267 * -ENOENT : index is for empty phandle
1268 * -EINVAL : parsing error on data
1269 * [1..n] : Number of phandle (count mode; when index = -1)
1271 rc = index < 0 ? cur_index : -ENOENT;
1279 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1280 * @np: Pointer to device node holding phandle property
1281 * @phandle_name: Name of property holding a phandle value
1282 * @index: For properties holding a table of phandles, this is the index into
1285 * Returns the device_node pointer with refcount incremented. Use
1286 * of_node_put() on it when done.
1288 struct device_node *of_parse_phandle(const struct device_node *np,
1289 const char *phandle_name, int index)
1291 struct of_phandle_args args;
1296 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1302 EXPORT_SYMBOL(of_parse_phandle);
1305 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1306 * @np: pointer to a device tree node containing a list
1307 * @list_name: property name that contains a list
1308 * @cells_name: property name that specifies phandles' arguments count
1309 * @index: index of a phandle to parse out
1310 * @out_args: optional pointer to output arguments structure (will be filled)
1312 * This function is useful to parse lists of phandles and their arguments.
1313 * Returns 0 on success and fills out_args, on error returns appropriate
1316 * Caller is responsible to call of_node_put() on the returned out_args->node
1322 * #list-cells = <2>;
1326 * #list-cells = <1>;
1330 * list = <&phandle1 1 2 &phandle2 3>;
1333 * To get a device_node of the `node2' node you may call this:
1334 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1336 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1337 const char *cells_name, int index,
1338 struct of_phandle_args *out_args)
1342 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1345 EXPORT_SYMBOL(of_parse_phandle_with_args);
1348 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1349 * @np: pointer to a device tree node containing a list
1350 * @list_name: property name that contains a list
1351 * @cell_count: number of argument cells following the phandle
1352 * @index: index of a phandle to parse out
1353 * @out_args: optional pointer to output arguments structure (will be filled)
1355 * This function is useful to parse lists of phandles and their arguments.
1356 * Returns 0 on success and fills out_args, on error returns appropriate
1359 * Caller is responsible to call of_node_put() on the returned out_args->node
1371 * list = <&phandle1 0 2 &phandle2 2 3>;
1374 * To get a device_node of the `node2' node you may call this:
1375 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1377 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1378 const char *list_name, int cell_count,
1379 int index, struct of_phandle_args *out_args)
1383 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1386 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1389 * of_count_phandle_with_args() - Find the number of phandles references in a property
1390 * @np: pointer to a device tree node containing a list
1391 * @list_name: property name that contains a list
1392 * @cells_name: property name that specifies phandles' arguments count
1394 * Returns the number of phandle + argument tuples within a property. It
1395 * is a typical pattern to encode a list of phandle and variable
1396 * arguments into a single property. The number of arguments is encoded
1397 * by a property in the phandle-target node. For example, a gpios
1398 * property would contain a list of GPIO specifies consisting of a
1399 * phandle and 1 or more arguments. The number of arguments are
1400 * determined by the #gpio-cells property in the node pointed to by the
1403 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1404 const char *cells_name)
1406 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1409 EXPORT_SYMBOL(of_count_phandle_with_args);
1412 * __of_add_property - Add a property to a node without lock operations
1414 int __of_add_property(struct device_node *np, struct property *prop)
1416 struct property **next;
1419 next = &np->properties;
1421 if (strcmp(prop->name, (*next)->name) == 0)
1422 /* duplicate ! don't insert it */
1425 next = &(*next)->next;
1433 * of_add_property - Add a property to a node
1435 int of_add_property(struct device_node *np, struct property *prop)
1437 unsigned long flags;
1440 mutex_lock(&of_mutex);
1442 raw_spin_lock_irqsave(&devtree_lock, flags);
1443 rc = __of_add_property(np, prop);
1444 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1447 __of_add_property_sysfs(np, prop);
1449 mutex_unlock(&of_mutex);
1452 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1457 int __of_remove_property(struct device_node *np, struct property *prop)
1459 struct property **next;
1461 for (next = &np->properties; *next; next = &(*next)->next) {
1468 /* found the node */
1470 prop->next = np->deadprops;
1471 np->deadprops = prop;
1476 void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1478 /* at early boot, bail here and defer setup to of_init() */
1479 if (of_kset && of_node_is_attached(np))
1480 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1484 * of_remove_property - Remove a property from a node.
1486 * Note that we don't actually remove it, since we have given out
1487 * who-knows-how-many pointers to the data using get-property.
1488 * Instead we just move the property to the "dead properties"
1489 * list, so it won't be found any more.
1491 int of_remove_property(struct device_node *np, struct property *prop)
1493 unsigned long flags;
1496 mutex_lock(&of_mutex);
1498 raw_spin_lock_irqsave(&devtree_lock, flags);
1499 rc = __of_remove_property(np, prop);
1500 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1503 __of_remove_property_sysfs(np, prop);
1505 mutex_unlock(&of_mutex);
1508 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1513 int __of_update_property(struct device_node *np, struct property *newprop,
1514 struct property **oldpropp)
1516 struct property **next, *oldprop;
1518 for (next = &np->properties; *next; next = &(*next)->next) {
1519 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1522 *oldpropp = oldprop = *next;
1525 /* replace the node */
1526 newprop->next = oldprop->next;
1528 oldprop->next = np->deadprops;
1529 np->deadprops = oldprop;
1532 newprop->next = NULL;
1539 void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1540 struct property *oldprop)
1542 /* At early boot, bail out and defer setup to of_init() */
1547 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1548 __of_add_property_sysfs(np, newprop);
1552 * of_update_property - Update a property in a node, if the property does
1553 * not exist, add it.
1555 * Note that we don't actually remove it, since we have given out
1556 * who-knows-how-many pointers to the data using get-property.
1557 * Instead we just move the property to the "dead properties" list,
1558 * and add the new property to the property list
1560 int of_update_property(struct device_node *np, struct property *newprop)
1562 struct property *oldprop;
1563 unsigned long flags;
1569 mutex_lock(&of_mutex);
1571 raw_spin_lock_irqsave(&devtree_lock, flags);
1572 rc = __of_update_property(np, newprop, &oldprop);
1573 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1576 __of_update_property_sysfs(np, newprop, oldprop);
1578 mutex_unlock(&of_mutex);
1581 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1586 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1587 int id, const char *stem, int stem_len)
1591 strncpy(ap->stem, stem, stem_len);
1592 ap->stem[stem_len] = 0;
1593 list_add_tail(&ap->link, &aliases_lookup);
1594 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1595 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1599 * of_alias_scan - Scan all properties of 'aliases' node
1601 * The function scans all the properties of 'aliases' node and populate
1602 * the the global lookup table with the properties. It returns the
1603 * number of alias_prop found, or error code in error case.
1605 * @dt_alloc: An allocator that provides a virtual address to memory
1606 * for the resulting tree
1608 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1610 struct property *pp;
1612 of_chosen = of_find_node_by_path("/chosen");
1613 if (of_chosen == NULL)
1614 of_chosen = of_find_node_by_path("/chosen@0");
1619 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1621 of_stdout = of_find_node_by_path(name);
1624 of_aliases = of_find_node_by_path("/aliases");
1628 for_each_property_of_node(of_aliases, pp) {
1629 const char *start = pp->name;
1630 const char *end = start + strlen(start);
1631 struct device_node *np;
1632 struct alias_prop *ap;
1635 /* Skip those we do not want to proceed */
1636 if (!strcmp(pp->name, "name") ||
1637 !strcmp(pp->name, "phandle") ||
1638 !strcmp(pp->name, "linux,phandle"))
1641 np = of_find_node_by_path(pp->value);
1645 /* walk the alias backwards to extract the id and work out
1646 * the 'stem' string */
1647 while (isdigit(*(end-1)) && end > start)
1651 if (kstrtoint(end, 10, &id) < 0)
1654 /* Allocate an alias_prop with enough space for the stem */
1655 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1659 of_alias_add(ap, np, id, start, len);
1664 * of_alias_get_id - Get alias id for the given device_node
1665 * @np: Pointer to the given device_node
1666 * @stem: Alias stem of the given device_node
1668 * The function travels the lookup table to get alias id for the given
1669 * device_node and alias stem. It returns the alias id if find it.
1671 int of_alias_get_id(struct device_node *np, const char *stem)
1673 struct alias_prop *app;
1676 mutex_lock(&of_mutex);
1677 list_for_each_entry(app, &aliases_lookup, link) {
1678 if (strcmp(app->stem, stem) != 0)
1681 if (np == app->np) {
1686 mutex_unlock(&of_mutex);
1690 EXPORT_SYMBOL_GPL(of_alias_get_id);
1692 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1695 const void *curv = cur;
1705 curv += sizeof(*cur);
1706 if (curv >= prop->value + prop->length)
1710 *pu = be32_to_cpup(curv);
1713 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1715 const char *of_prop_next_string(struct property *prop, const char *cur)
1717 const void *curv = cur;
1725 curv += strlen(cur) + 1;
1726 if (curv >= prop->value + prop->length)
1731 EXPORT_SYMBOL_GPL(of_prop_next_string);
1734 * of_device_is_stdout_path - check if a device node matches the
1735 * linux,stdout-path property
1737 * Check if this device node matches the linux,stdout-path property
1738 * in the chosen node. return true if yes, false otherwise.
1740 int of_device_is_stdout_path(struct device_node *dn)
1745 return of_stdout == dn;
1747 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);