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/cpu.h>
22 #include <linux/module.h>
24 #include <linux/of_graph.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/proc_fs.h>
30 #include "of_private.h"
32 LIST_HEAD(aliases_lookup);
34 struct device_node *of_allnodes;
35 EXPORT_SYMBOL(of_allnodes);
36 struct device_node *of_chosen;
37 struct device_node *of_aliases;
38 static struct device_node *of_stdout;
43 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
44 * This mutex must be held whenever modifications are being made to the
45 * device tree. The of_{attach,detach}_node() and
46 * of_{add,remove,update}_property() helpers make sure this happens.
48 DEFINE_MUTEX(of_mutex);
50 /* use when traversing tree through the allnext, child, sibling,
51 * or parent members of struct device_node.
53 DEFINE_RAW_SPINLOCK(devtree_lock);
55 int of_n_addr_cells(struct device_node *np)
62 ip = of_get_property(np, "#address-cells", NULL);
64 return be32_to_cpup(ip);
66 /* No #address-cells property for the root node */
67 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
69 EXPORT_SYMBOL(of_n_addr_cells);
71 int of_n_size_cells(struct device_node *np)
78 ip = of_get_property(np, "#size-cells", NULL);
80 return be32_to_cpup(ip);
82 /* No #size-cells property for the root node */
83 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
85 EXPORT_SYMBOL(of_n_size_cells);
88 int __weak of_node_to_nid(struct device_node *np)
90 return numa_node_id();
94 #ifndef CONFIG_OF_DYNAMIC
95 static void of_node_release(struct kobject *kobj)
97 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
99 #endif /* CONFIG_OF_DYNAMIC */
101 struct kobj_type of_node_ktype = {
102 .release = of_node_release,
105 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
106 struct bin_attribute *bin_attr, char *buf,
107 loff_t offset, size_t count)
109 struct property *pp = container_of(bin_attr, struct property, attr);
110 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
113 static const char *safe_name(struct kobject *kobj, const char *orig_name)
115 const char *name = orig_name;
116 struct kernfs_node *kn;
119 /* don't be a hero. After 16 tries give up */
120 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
122 if (name != orig_name)
124 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
127 if (name != orig_name)
128 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
129 kobject_name(kobj), name);
133 int __of_add_property_sysfs(struct device_node *np, struct property *pp)
137 /* Important: Don't leak passwords */
138 bool secure = strncmp(pp->name, "security-", 9) == 0;
140 if (!of_kset || !of_node_is_attached(np))
143 sysfs_bin_attr_init(&pp->attr);
144 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
145 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
146 pp->attr.size = secure ? 0 : pp->length;
147 pp->attr.read = of_node_property_read;
149 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
150 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
154 int __of_attach_node_sysfs(struct device_node *np)
163 np->kobj.kset = of_kset;
165 /* Nodes without parents are new top level trees */
166 rc = kobject_add(&np->kobj, NULL, "%s",
167 safe_name(&of_kset->kobj, "base"));
169 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
170 if (!name || !name[0])
173 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
178 for_each_property_of_node(np, pp)
179 __of_add_property_sysfs(np, pp);
184 static int __init of_init(void)
186 struct device_node *np;
188 /* Create the kset, and register existing nodes */
189 mutex_lock(&of_mutex);
190 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
192 mutex_unlock(&of_mutex);
195 for_each_of_allnodes(np)
196 __of_attach_node_sysfs(np);
197 mutex_unlock(&of_mutex);
199 /* Symlink in /proc as required by userspace ABI */
201 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
205 core_initcall(of_init);
207 static struct property *__of_find_property(const struct device_node *np,
208 const char *name, int *lenp)
215 for (pp = np->properties; pp; pp = pp->next) {
216 if (of_prop_cmp(pp->name, name) == 0) {
226 struct property *of_find_property(const struct device_node *np,
233 raw_spin_lock_irqsave(&devtree_lock, flags);
234 pp = __of_find_property(np, name, lenp);
235 raw_spin_unlock_irqrestore(&devtree_lock, flags);
239 EXPORT_SYMBOL(of_find_property);
242 * of_find_all_nodes - Get next node in global list
243 * @prev: Previous node or NULL to start iteration
244 * of_node_put() will be called on it
246 * Returns a node pointer with refcount incremented, use
247 * of_node_put() on it when done.
249 struct device_node *of_find_all_nodes(struct device_node *prev)
251 struct device_node *np;
254 raw_spin_lock_irqsave(&devtree_lock, flags);
255 np = prev ? prev->allnext : of_allnodes;
256 for (; np != NULL; np = np->allnext)
260 raw_spin_unlock_irqrestore(&devtree_lock, flags);
263 EXPORT_SYMBOL(of_find_all_nodes);
266 * Find a property with a given name for a given node
267 * and return the value.
269 const void *__of_get_property(const struct device_node *np,
270 const char *name, int *lenp)
272 struct property *pp = __of_find_property(np, name, lenp);
274 return pp ? pp->value : NULL;
278 * Find a property with a given name for a given node
279 * and return the value.
281 const void *of_get_property(const struct device_node *np, const char *name,
284 struct property *pp = of_find_property(np, name, lenp);
286 return pp ? pp->value : NULL;
288 EXPORT_SYMBOL(of_get_property);
291 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
293 * @cpu: logical cpu index of a core/thread
294 * @phys_id: physical identifier of a core/thread
296 * CPU logical to physical index mapping is architecture specific.
297 * However this __weak function provides a default match of physical
298 * id to logical cpu index. phys_id provided here is usually values read
299 * from the device tree which must match the hardware internal registers.
301 * Returns true if the physical identifier and the logical cpu index
302 * correspond to the same core/thread, false otherwise.
304 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
306 return (u32)phys_id == cpu;
310 * Checks if the given "prop_name" property holds the physical id of the
311 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
312 * NULL, local thread number within the core is returned in it.
314 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
315 const char *prop_name, int cpu, unsigned int *thread)
318 int ac, prop_len, tid;
321 ac = of_n_addr_cells(cpun);
322 cell = of_get_property(cpun, prop_name, &prop_len);
325 prop_len /= sizeof(*cell) * ac;
326 for (tid = 0; tid < prop_len; tid++) {
327 hwid = of_read_number(cell, ac);
328 if (arch_match_cpu_phys_id(cpu, hwid)) {
339 * arch_find_n_match_cpu_physical_id - See if the given device node is
340 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
341 * else false. If 'thread' is non-NULL, the local thread number within the
342 * core is returned in it.
344 bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
345 int cpu, unsigned int *thread)
347 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
348 * for thread ids on PowerPC. If it doesn't exist fallback to
349 * standard "reg" property.
351 if (IS_ENABLED(CONFIG_PPC) &&
352 __of_find_n_match_cpu_property(cpun,
353 "ibm,ppc-interrupt-server#s",
357 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
364 * of_get_cpu_node - Get device node associated with the given logical CPU
366 * @cpu: CPU number(logical index) for which device node is required
367 * @thread: if not NULL, local thread number within the physical core is
370 * The main purpose of this function is to retrieve the device node for the
371 * given logical CPU index. It should be used to initialize the of_node in
372 * cpu device. Once of_node in cpu device is populated, all the further
373 * references can use that instead.
375 * CPU logical to physical index mapping is architecture specific and is built
376 * before booting secondary cores. This function uses arch_match_cpu_phys_id
377 * which can be overridden by architecture specific implementation.
379 * Returns a node pointer for the logical cpu if found, else NULL.
381 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
383 struct device_node *cpun;
385 for_each_node_by_type(cpun, "cpu") {
386 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
391 EXPORT_SYMBOL(of_get_cpu_node);
394 * __of_device_is_compatible() - Check if the node matches given constraints
395 * @device: pointer to node
396 * @compat: required compatible string, NULL or "" for any match
397 * @type: required device_type value, NULL or "" for any match
398 * @name: required node name, NULL or "" for any match
400 * Checks if the given @compat, @type and @name strings match the
401 * properties of the given @device. A constraints can be skipped by
402 * passing NULL or an empty string as the constraint.
404 * Returns 0 for no match, and a positive integer on match. The return
405 * value is a relative score with larger values indicating better
406 * matches. The score is weighted for the most specific compatible value
407 * to get the highest score. Matching type is next, followed by matching
408 * name. Practically speaking, this results in the following priority
411 * 1. specific compatible && type && name
412 * 2. specific compatible && type
413 * 3. specific compatible && name
414 * 4. specific compatible
415 * 5. general compatible && type && name
416 * 6. general compatible && type
417 * 7. general compatible && name
418 * 8. general compatible
423 static int __of_device_is_compatible(const struct device_node *device,
424 const char *compat, const char *type, const char *name)
426 struct property *prop;
428 int index = 0, score = 0;
430 /* Compatible match has highest priority */
431 if (compat && compat[0]) {
432 prop = __of_find_property(device, "compatible", NULL);
433 for (cp = of_prop_next_string(prop, NULL); cp;
434 cp = of_prop_next_string(prop, cp), index++) {
435 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
436 score = INT_MAX/2 - (index << 2);
444 /* Matching type is better than matching name */
445 if (type && type[0]) {
446 if (!device->type || of_node_cmp(type, device->type))
451 /* Matching name is a bit better than not */
452 if (name && name[0]) {
453 if (!device->name || of_node_cmp(name, device->name))
461 /** Checks if the given "compat" string matches one of the strings in
462 * the device's "compatible" property
464 int of_device_is_compatible(const struct device_node *device,
470 raw_spin_lock_irqsave(&devtree_lock, flags);
471 res = __of_device_is_compatible(device, compat, NULL, NULL);
472 raw_spin_unlock_irqrestore(&devtree_lock, flags);
475 EXPORT_SYMBOL(of_device_is_compatible);
478 * of_machine_is_compatible - Test root of device tree for a given compatible value
479 * @compat: compatible string to look for in root node's compatible property.
481 * Returns true if the root node has the given value in its
482 * compatible property.
484 int of_machine_is_compatible(const char *compat)
486 struct device_node *root;
489 root = of_find_node_by_path("/");
491 rc = of_device_is_compatible(root, compat);
496 EXPORT_SYMBOL(of_machine_is_compatible);
499 * __of_device_is_available - check if a device is available for use
501 * @device: Node to check for availability, with locks already held
503 * Returns 1 if the status property is absent or set to "okay" or "ok",
506 static int __of_device_is_available(const struct device_node *device)
514 status = __of_get_property(device, "status", &statlen);
519 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
527 * of_device_is_available - check if a device is available for use
529 * @device: Node to check for availability
531 * Returns 1 if the status property is absent or set to "okay" or "ok",
534 int of_device_is_available(const struct device_node *device)
539 raw_spin_lock_irqsave(&devtree_lock, flags);
540 res = __of_device_is_available(device);
541 raw_spin_unlock_irqrestore(&devtree_lock, flags);
545 EXPORT_SYMBOL(of_device_is_available);
548 * of_get_parent - Get a node's parent if any
549 * @node: Node to get parent
551 * Returns a node pointer with refcount incremented, use
552 * of_node_put() on it when done.
554 struct device_node *of_get_parent(const struct device_node *node)
556 struct device_node *np;
562 raw_spin_lock_irqsave(&devtree_lock, flags);
563 np = of_node_get(node->parent);
564 raw_spin_unlock_irqrestore(&devtree_lock, flags);
567 EXPORT_SYMBOL(of_get_parent);
570 * of_get_next_parent - Iterate to a node's parent
571 * @node: Node to get parent of
573 * This is like of_get_parent() except that it drops the
574 * refcount on the passed node, making it suitable for iterating
575 * through a node's parents.
577 * Returns a node pointer with refcount incremented, use
578 * of_node_put() on it when done.
580 struct device_node *of_get_next_parent(struct device_node *node)
582 struct device_node *parent;
588 raw_spin_lock_irqsave(&devtree_lock, flags);
589 parent = of_node_get(node->parent);
591 raw_spin_unlock_irqrestore(&devtree_lock, flags);
594 EXPORT_SYMBOL(of_get_next_parent);
596 static struct device_node *__of_get_next_child(const struct device_node *node,
597 struct device_node *prev)
599 struct device_node *next;
604 next = prev ? prev->sibling : node->child;
605 for (; next; next = next->sibling)
606 if (of_node_get(next))
611 #define __for_each_child_of_node(parent, child) \
612 for (child = __of_get_next_child(parent, NULL); child != NULL; \
613 child = __of_get_next_child(parent, child))
616 * of_get_next_child - Iterate a node childs
618 * @prev: previous child of the parent node, or NULL to get first
620 * Returns a node pointer with refcount incremented, use
621 * of_node_put() on it when done.
623 struct device_node *of_get_next_child(const struct device_node *node,
624 struct device_node *prev)
626 struct device_node *next;
629 raw_spin_lock_irqsave(&devtree_lock, flags);
630 next = __of_get_next_child(node, prev);
631 raw_spin_unlock_irqrestore(&devtree_lock, flags);
634 EXPORT_SYMBOL(of_get_next_child);
637 * of_get_next_available_child - Find the next available child node
639 * @prev: previous child of the parent node, or NULL to get first
641 * This function is like of_get_next_child(), except that it
642 * automatically skips any disabled nodes (i.e. status = "disabled").
644 struct device_node *of_get_next_available_child(const struct device_node *node,
645 struct device_node *prev)
647 struct device_node *next;
653 raw_spin_lock_irqsave(&devtree_lock, flags);
654 next = prev ? prev->sibling : node->child;
655 for (; next; next = next->sibling) {
656 if (!__of_device_is_available(next))
658 if (of_node_get(next))
662 raw_spin_unlock_irqrestore(&devtree_lock, flags);
665 EXPORT_SYMBOL(of_get_next_available_child);
668 * of_get_child_by_name - Find the child node by name for a given parent
670 * @name: child name to look for.
672 * This function looks for child node for given matching name
674 * Returns a node pointer if found, with refcount incremented, use
675 * of_node_put() on it when done.
676 * Returns NULL if node is not found.
678 struct device_node *of_get_child_by_name(const struct device_node *node,
681 struct device_node *child;
683 for_each_child_of_node(node, child)
684 if (child->name && (of_node_cmp(child->name, name) == 0))
688 EXPORT_SYMBOL(of_get_child_by_name);
690 static struct device_node *__of_find_node_by_path(struct device_node *parent,
693 struct device_node *child;
694 int len = strchrnul(path, '/') - path;
699 __for_each_child_of_node(parent, child) {
700 const char *name = strrchr(child->full_name, '/');
701 if (WARN(!name, "malformed device_node %s\n", child->full_name))
704 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
711 * of_find_node_by_path - Find a node matching a full OF path
712 * @path: Either the full path to match, or if the path does not
713 * start with '/', the name of a property of the /aliases
714 * node (an alias). In the case of an alias, the node
715 * matching the alias' value will be returned.
720 * foo/bar Valid alias + relative path
722 * Returns a node pointer with refcount incremented, use
723 * of_node_put() on it when done.
725 struct device_node *of_find_node_by_path(const char *path)
727 struct device_node *np = NULL;
731 if (strcmp(path, "/") == 0)
732 return of_node_get(of_allnodes);
734 /* The path could begin with an alias */
736 char *p = strchrnul(path, '/');
739 /* of_aliases must not be NULL */
743 for_each_property_of_node(of_aliases, pp) {
744 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
745 np = of_find_node_by_path(pp->value);
754 /* Step down the tree matching path components */
755 raw_spin_lock_irqsave(&devtree_lock, flags);
757 np = of_node_get(of_allnodes);
758 while (np && *path == '/') {
759 path++; /* Increment past '/' delimiter */
760 np = __of_find_node_by_path(np, path);
761 path = strchrnul(path, '/');
763 raw_spin_unlock_irqrestore(&devtree_lock, flags);
766 EXPORT_SYMBOL(of_find_node_by_path);
769 * of_find_node_by_name - Find a node by its "name" property
770 * @from: The node to start searching from or NULL, the node
771 * you pass will not be searched, only the next one
772 * will; typically, you pass what the previous call
773 * returned. of_node_put() will be called on it
774 * @name: The name string to match against
776 * Returns a node pointer with refcount incremented, use
777 * of_node_put() on it when done.
779 struct device_node *of_find_node_by_name(struct device_node *from,
782 struct device_node *np;
785 raw_spin_lock_irqsave(&devtree_lock, flags);
786 np = from ? from->allnext : of_allnodes;
787 for (; np; np = np->allnext)
788 if (np->name && (of_node_cmp(np->name, name) == 0)
792 raw_spin_unlock_irqrestore(&devtree_lock, flags);
795 EXPORT_SYMBOL(of_find_node_by_name);
798 * of_find_node_by_type - Find a node by its "device_type" property
799 * @from: The node to start searching from, or NULL to start searching
800 * the entire device tree. The node you pass will not be
801 * searched, only the next one will; typically, you pass
802 * what the previous call returned. of_node_put() will be
803 * called on from for you.
804 * @type: The type string to match against
806 * Returns a node pointer with refcount incremented, use
807 * of_node_put() on it when done.
809 struct device_node *of_find_node_by_type(struct device_node *from,
812 struct device_node *np;
815 raw_spin_lock_irqsave(&devtree_lock, flags);
816 np = from ? from->allnext : of_allnodes;
817 for (; np; np = np->allnext)
818 if (np->type && (of_node_cmp(np->type, type) == 0)
822 raw_spin_unlock_irqrestore(&devtree_lock, flags);
825 EXPORT_SYMBOL(of_find_node_by_type);
828 * of_find_compatible_node - Find a node based on type and one of the
829 * tokens in its "compatible" property
830 * @from: The node to start searching from or NULL, the node
831 * you pass will not be searched, only the next one
832 * will; typically, you pass what the previous call
833 * returned. of_node_put() will be called on it
834 * @type: The type string to match "device_type" or NULL to ignore
835 * @compatible: The string to match to one of the tokens in the device
838 * Returns a node pointer with refcount incremented, use
839 * of_node_put() on it when done.
841 struct device_node *of_find_compatible_node(struct device_node *from,
842 const char *type, const char *compatible)
844 struct device_node *np;
847 raw_spin_lock_irqsave(&devtree_lock, flags);
848 np = from ? from->allnext : of_allnodes;
849 for (; np; np = np->allnext) {
850 if (__of_device_is_compatible(np, compatible, type, NULL) &&
855 raw_spin_unlock_irqrestore(&devtree_lock, flags);
858 EXPORT_SYMBOL(of_find_compatible_node);
861 * of_find_node_with_property - Find a node which has a property with
863 * @from: The node to start searching from or NULL, the node
864 * you pass will not be searched, only the next one
865 * will; typically, you pass what the previous call
866 * returned. of_node_put() will be called on it
867 * @prop_name: The name of the property to look for.
869 * Returns a node pointer with refcount incremented, use
870 * of_node_put() on it when done.
872 struct device_node *of_find_node_with_property(struct device_node *from,
873 const char *prop_name)
875 struct device_node *np;
879 raw_spin_lock_irqsave(&devtree_lock, flags);
880 np = from ? from->allnext : of_allnodes;
881 for (; np; np = np->allnext) {
882 for (pp = np->properties; pp; pp = pp->next) {
883 if (of_prop_cmp(pp->name, prop_name) == 0) {
891 raw_spin_unlock_irqrestore(&devtree_lock, flags);
894 EXPORT_SYMBOL(of_find_node_with_property);
897 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
898 const struct device_node *node)
900 const struct of_device_id *best_match = NULL;
901 int score, best_score = 0;
906 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
907 score = __of_device_is_compatible(node, matches->compatible,
908 matches->type, matches->name);
909 if (score > best_score) {
910 best_match = matches;
919 * of_match_node - Tell if an device_node has a matching of_match structure
920 * @matches: array of of device match structures to search in
921 * @node: the of device structure to match against
923 * Low level utility function used by device matching.
925 const struct of_device_id *of_match_node(const struct of_device_id *matches,
926 const struct device_node *node)
928 const struct of_device_id *match;
931 raw_spin_lock_irqsave(&devtree_lock, flags);
932 match = __of_match_node(matches, node);
933 raw_spin_unlock_irqrestore(&devtree_lock, flags);
936 EXPORT_SYMBOL(of_match_node);
939 * of_find_matching_node_and_match - Find a node based on an of_device_id
941 * @from: The node to start searching from or NULL, the node
942 * you pass will not be searched, only the next one
943 * will; typically, you pass what the previous call
944 * returned. of_node_put() will be called on it
945 * @matches: array of of device match structures to search in
946 * @match Updated to point at the matches entry which matched
948 * Returns a node pointer with refcount incremented, use
949 * of_node_put() on it when done.
951 struct device_node *of_find_matching_node_and_match(struct device_node *from,
952 const struct of_device_id *matches,
953 const struct of_device_id **match)
955 struct device_node *np;
956 const struct of_device_id *m;
962 raw_spin_lock_irqsave(&devtree_lock, flags);
963 np = from ? from->allnext : of_allnodes;
964 for (; np; np = np->allnext) {
965 m = __of_match_node(matches, np);
966 if (m && of_node_get(np)) {
973 raw_spin_unlock_irqrestore(&devtree_lock, flags);
976 EXPORT_SYMBOL(of_find_matching_node_and_match);
979 * of_modalias_node - Lookup appropriate modalias for a device node
980 * @node: pointer to a device tree node
981 * @modalias: Pointer to buffer that modalias value will be copied into
982 * @len: Length of modalias value
984 * Based on the value of the compatible property, this routine will attempt
985 * to choose an appropriate modalias value for a particular device tree node.
986 * It does this by stripping the manufacturer prefix (as delimited by a ',')
987 * from the first entry in the compatible list property.
989 * This routine returns 0 on success, <0 on failure.
991 int of_modalias_node(struct device_node *node, char *modalias, int len)
993 const char *compatible, *p;
996 compatible = of_get_property(node, "compatible", &cplen);
997 if (!compatible || strlen(compatible) > cplen)
999 p = strchr(compatible, ',');
1000 strlcpy(modalias, p ? p + 1 : compatible, len);
1003 EXPORT_SYMBOL_GPL(of_modalias_node);
1006 * of_find_node_by_phandle - Find a node given a phandle
1007 * @handle: phandle of the node to find
1009 * Returns a node pointer with refcount incremented, use
1010 * of_node_put() on it when done.
1012 struct device_node *of_find_node_by_phandle(phandle handle)
1014 struct device_node *np;
1015 unsigned long flags;
1017 raw_spin_lock_irqsave(&devtree_lock, flags);
1018 for (np = of_allnodes; np; np = np->allnext)
1019 if (np->phandle == handle)
1022 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1025 EXPORT_SYMBOL(of_find_node_by_phandle);
1028 * of_property_count_elems_of_size - Count the number of elements in a property
1030 * @np: device node from which the property value is to be read.
1031 * @propname: name of the property to be searched.
1032 * @elem_size: size of the individual element
1034 * Search for a property in a device node and count the number of elements of
1035 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1036 * property does not exist or its length does not match a multiple of elem_size
1037 * and -ENODATA if the property does not have a value.
1039 int of_property_count_elems_of_size(const struct device_node *np,
1040 const char *propname, int elem_size)
1042 struct property *prop = of_find_property(np, propname, NULL);
1049 if (prop->length % elem_size != 0) {
1050 pr_err("size of %s in node %s is not a multiple of %d\n",
1051 propname, np->full_name, elem_size);
1055 return prop->length / elem_size;
1057 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1060 * of_find_property_value_of_size
1062 * @np: device node from which the property value is to be read.
1063 * @propname: name of the property to be searched.
1064 * @len: requested length of property value
1066 * Search for a property in a device node and valid the requested size.
1067 * Returns the property value on success, -EINVAL if the property does not
1068 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1069 * property data isn't large enough.
1072 static void *of_find_property_value_of_size(const struct device_node *np,
1073 const char *propname, u32 len)
1075 struct property *prop = of_find_property(np, propname, NULL);
1078 return ERR_PTR(-EINVAL);
1080 return ERR_PTR(-ENODATA);
1081 if (len > prop->length)
1082 return ERR_PTR(-EOVERFLOW);
1088 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1090 * @np: device node from which the property value is to be read.
1091 * @propname: name of the property to be searched.
1092 * @index: index of the u32 in the list of values
1093 * @out_value: pointer to return value, modified only if no error.
1095 * Search for a property in a device node and read nth 32-bit value from
1096 * it. Returns 0 on success, -EINVAL if the property does not exist,
1097 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1098 * property data isn't large enough.
1100 * The out_value is modified only if a valid u32 value can be decoded.
1102 int of_property_read_u32_index(const struct device_node *np,
1103 const char *propname,
1104 u32 index, u32 *out_value)
1106 const u32 *val = of_find_property_value_of_size(np, propname,
1107 ((index + 1) * sizeof(*out_value)));
1110 return PTR_ERR(val);
1112 *out_value = be32_to_cpup(((__be32 *)val) + index);
1115 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1118 * of_property_read_u8_array - Find and read an array of u8 from a property.
1120 * @np: device node from which the property value is to be read.
1121 * @propname: name of the property to be searched.
1122 * @out_values: pointer to return value, modified only if return value is 0.
1123 * @sz: number of array elements to read
1125 * Search for a property in a device node and read 8-bit value(s) from
1126 * it. Returns 0 on success, -EINVAL if the property does not exist,
1127 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1128 * property data isn't large enough.
1130 * dts entry of array should be like:
1131 * property = /bits/ 8 <0x50 0x60 0x70>;
1133 * The out_values is modified only if a valid u8 value can be decoded.
1135 int of_property_read_u8_array(const struct device_node *np,
1136 const char *propname, u8 *out_values, size_t sz)
1138 const u8 *val = of_find_property_value_of_size(np, propname,
1139 (sz * sizeof(*out_values)));
1142 return PTR_ERR(val);
1145 *out_values++ = *val++;
1148 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1151 * of_property_read_u16_array - Find and read an array of u16 from a property.
1153 * @np: device node from which the property value is to be read.
1154 * @propname: name of the property to be searched.
1155 * @out_values: pointer to return value, modified only if return value is 0.
1156 * @sz: number of array elements to read
1158 * Search for a property in a device node and read 16-bit value(s) from
1159 * it. Returns 0 on success, -EINVAL if the property does not exist,
1160 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1161 * property data isn't large enough.
1163 * dts entry of array should be like:
1164 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1166 * The out_values is modified only if a valid u16 value can be decoded.
1168 int of_property_read_u16_array(const struct device_node *np,
1169 const char *propname, u16 *out_values, size_t sz)
1171 const __be16 *val = of_find_property_value_of_size(np, propname,
1172 (sz * sizeof(*out_values)));
1175 return PTR_ERR(val);
1178 *out_values++ = be16_to_cpup(val++);
1181 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1184 * of_property_read_u32_array - Find and read an array of 32 bit integers
1187 * @np: device node from which the property value is to be read.
1188 * @propname: name of the property to be searched.
1189 * @out_values: pointer to return value, modified only if return value is 0.
1190 * @sz: number of array elements to read
1192 * Search for a property in a device node and read 32-bit value(s) from
1193 * it. Returns 0 on success, -EINVAL if the property does not exist,
1194 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1195 * property data isn't large enough.
1197 * The out_values is modified only if a valid u32 value can be decoded.
1199 int of_property_read_u32_array(const struct device_node *np,
1200 const char *propname, u32 *out_values,
1203 const __be32 *val = of_find_property_value_of_size(np, propname,
1204 (sz * sizeof(*out_values)));
1207 return PTR_ERR(val);
1210 *out_values++ = be32_to_cpup(val++);
1213 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1216 * of_property_read_u64 - Find and read a 64 bit integer from a property
1217 * @np: device node from which the property value is to be read.
1218 * @propname: name of the property to be searched.
1219 * @out_value: pointer to return value, modified only if return value is 0.
1221 * Search for a property in a device node and read a 64-bit value from
1222 * it. Returns 0 on success, -EINVAL if the property does not exist,
1223 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1224 * property data isn't large enough.
1226 * The out_value is modified only if a valid u64 value can be decoded.
1228 int of_property_read_u64(const struct device_node *np, const char *propname,
1231 const __be32 *val = of_find_property_value_of_size(np, propname,
1232 sizeof(*out_value));
1235 return PTR_ERR(val);
1237 *out_value = of_read_number(val, 2);
1240 EXPORT_SYMBOL_GPL(of_property_read_u64);
1243 * of_property_read_string - Find and read a string from a property
1244 * @np: device node from which the property value is to be read.
1245 * @propname: name of the property to be searched.
1246 * @out_string: pointer to null terminated return string, modified only if
1247 * return value is 0.
1249 * Search for a property in a device tree node and retrieve a null
1250 * terminated string value (pointer to data, not a copy). Returns 0 on
1251 * success, -EINVAL if the property does not exist, -ENODATA if property
1252 * does not have a value, and -EILSEQ if the string is not null-terminated
1253 * within the length of the property data.
1255 * The out_string pointer is modified only if a valid string can be decoded.
1257 int of_property_read_string(struct device_node *np, const char *propname,
1258 const char **out_string)
1260 struct property *prop = of_find_property(np, propname, NULL);
1265 if (strnlen(prop->value, prop->length) >= prop->length)
1267 *out_string = prop->value;
1270 EXPORT_SYMBOL_GPL(of_property_read_string);
1273 * of_property_read_string_index - Find and read a string from a multiple
1275 * @np: device node from which the property value is to be read.
1276 * @propname: name of the property to be searched.
1277 * @index: index of the string in the list of strings
1278 * @out_string: pointer to null terminated return string, modified only if
1279 * return value is 0.
1281 * Search for a property in a device tree node and retrieve a null
1282 * terminated string value (pointer to data, not a copy) in the list of strings
1283 * contained in that property.
1284 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1285 * property does not have a value, and -EILSEQ if the string is not
1286 * null-terminated within the length of the property data.
1288 * The out_string pointer is modified only if a valid string can be decoded.
1290 int of_property_read_string_index(struct device_node *np, const char *propname,
1291 int index, const char **output)
1293 struct property *prop = of_find_property(np, propname, NULL);
1295 size_t l = 0, total = 0;
1302 if (strnlen(prop->value, prop->length) >= prop->length)
1307 for (i = 0; total < prop->length; total += l, p += l) {
1316 EXPORT_SYMBOL_GPL(of_property_read_string_index);
1319 * of_property_match_string() - Find string in a list and return index
1320 * @np: pointer to node containing string list property
1321 * @propname: string list property name
1322 * @string: pointer to string to search for in string list
1324 * This function searches a string list property and returns the index
1325 * of a specific string value.
1327 int of_property_match_string(struct device_node *np, const char *propname,
1330 struct property *prop = of_find_property(np, propname, NULL);
1333 const char *p, *end;
1341 end = p + prop->length;
1343 for (i = 0; p < end; i++, p += l) {
1347 pr_debug("comparing %s with %s\n", string, p);
1348 if (strcmp(string, p) == 0)
1349 return i; /* Found it; return index */
1353 EXPORT_SYMBOL_GPL(of_property_match_string);
1356 * of_property_count_strings - Find and return the number of strings from a
1357 * multiple strings property.
1358 * @np: device node from which the property value is to be read.
1359 * @propname: name of the property to be searched.
1361 * Search for a property in a device tree node and retrieve the number of null
1362 * terminated string contain in it. Returns the number of strings on
1363 * success, -EINVAL if the property does not exist, -ENODATA if property
1364 * does not have a value, and -EILSEQ if the string is not null-terminated
1365 * within the length of the property data.
1367 int of_property_count_strings(struct device_node *np, const char *propname)
1369 struct property *prop = of_find_property(np, propname, NULL);
1371 size_t l = 0, total = 0;
1378 if (strnlen(prop->value, prop->length) >= prop->length)
1383 for (i = 0; total < prop->length; total += l, p += l, i++)
1388 EXPORT_SYMBOL_GPL(of_property_count_strings);
1390 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1393 printk("%s %s", msg, of_node_full_name(args->np));
1394 for (i = 0; i < args->args_count; i++)
1395 printk(i ? ",%08x" : ":%08x", args->args[i]);
1399 static int __of_parse_phandle_with_args(const struct device_node *np,
1400 const char *list_name,
1401 const char *cells_name,
1402 int cell_count, int index,
1403 struct of_phandle_args *out_args)
1405 const __be32 *list, *list_end;
1406 int rc = 0, size, cur_index = 0;
1408 struct device_node *node = NULL;
1411 /* Retrieve the phandle list property */
1412 list = of_get_property(np, list_name, &size);
1415 list_end = list + size / sizeof(*list);
1417 /* Loop over the phandles until all the requested entry is found */
1418 while (list < list_end) {
1423 * If phandle is 0, then it is an empty entry with no
1424 * arguments. Skip forward to the next entry.
1426 phandle = be32_to_cpup(list++);
1429 * Find the provider node and parse the #*-cells
1430 * property to determine the argument length.
1432 * This is not needed if the cell count is hard-coded
1433 * (i.e. cells_name not set, but cell_count is set),
1434 * except when we're going to return the found node
1437 if (cells_name || cur_index == index) {
1438 node = of_find_node_by_phandle(phandle);
1440 pr_err("%s: could not find phandle\n",
1447 if (of_property_read_u32(node, cells_name,
1449 pr_err("%s: could not get %s for %s\n",
1450 np->full_name, cells_name,
1459 * Make sure that the arguments actually fit in the
1460 * remaining property data length
1462 if (list + count > list_end) {
1463 pr_err("%s: arguments longer than property\n",
1470 * All of the error cases above bail out of the loop, so at
1471 * this point, the parsing is successful. If the requested
1472 * index matches, then fill the out_args structure and return,
1473 * or return -ENOENT for an empty entry.
1476 if (cur_index == index) {
1482 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1483 count = MAX_PHANDLE_ARGS;
1484 out_args->np = node;
1485 out_args->args_count = count;
1486 for (i = 0; i < count; i++)
1487 out_args->args[i] = be32_to_cpup(list++);
1492 /* Found it! return success */
1503 * Unlock node before returning result; will be one of:
1504 * -ENOENT : index is for empty phandle
1505 * -EINVAL : parsing error on data
1506 * [1..n] : Number of phandle (count mode; when index = -1)
1508 rc = index < 0 ? cur_index : -ENOENT;
1516 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1517 * @np: Pointer to device node holding phandle property
1518 * @phandle_name: Name of property holding a phandle value
1519 * @index: For properties holding a table of phandles, this is the index into
1522 * Returns the device_node pointer with refcount incremented. Use
1523 * of_node_put() on it when done.
1525 struct device_node *of_parse_phandle(const struct device_node *np,
1526 const char *phandle_name, int index)
1528 struct of_phandle_args args;
1533 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1539 EXPORT_SYMBOL(of_parse_phandle);
1542 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1543 * @np: pointer to a device tree node containing a list
1544 * @list_name: property name that contains a list
1545 * @cells_name: property name that specifies phandles' arguments count
1546 * @index: index of a phandle to parse out
1547 * @out_args: optional pointer to output arguments structure (will be filled)
1549 * This function is useful to parse lists of phandles and their arguments.
1550 * Returns 0 on success and fills out_args, on error returns appropriate
1553 * Caller is responsible to call of_node_put() on the returned out_args->node
1559 * #list-cells = <2>;
1563 * #list-cells = <1>;
1567 * list = <&phandle1 1 2 &phandle2 3>;
1570 * To get a device_node of the `node2' node you may call this:
1571 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1573 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1574 const char *cells_name, int index,
1575 struct of_phandle_args *out_args)
1579 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1582 EXPORT_SYMBOL(of_parse_phandle_with_args);
1585 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1586 * @np: pointer to a device tree node containing a list
1587 * @list_name: property name that contains a list
1588 * @cell_count: number of argument cells following the phandle
1589 * @index: index of a phandle to parse out
1590 * @out_args: optional pointer to output arguments structure (will be filled)
1592 * This function is useful to parse lists of phandles and their arguments.
1593 * Returns 0 on success and fills out_args, on error returns appropriate
1596 * Caller is responsible to call of_node_put() on the returned out_args->node
1608 * list = <&phandle1 0 2 &phandle2 2 3>;
1611 * To get a device_node of the `node2' node you may call this:
1612 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1614 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1615 const char *list_name, int cell_count,
1616 int index, struct of_phandle_args *out_args)
1620 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1623 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1626 * of_count_phandle_with_args() - Find the number of phandles references in a property
1627 * @np: pointer to a device tree node containing a list
1628 * @list_name: property name that contains a list
1629 * @cells_name: property name that specifies phandles' arguments count
1631 * Returns the number of phandle + argument tuples within a property. It
1632 * is a typical pattern to encode a list of phandle and variable
1633 * arguments into a single property. The number of arguments is encoded
1634 * by a property in the phandle-target node. For example, a gpios
1635 * property would contain a list of GPIO specifies consisting of a
1636 * phandle and 1 or more arguments. The number of arguments are
1637 * determined by the #gpio-cells property in the node pointed to by the
1640 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1641 const char *cells_name)
1643 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1646 EXPORT_SYMBOL(of_count_phandle_with_args);
1649 * __of_add_property - Add a property to a node without lock operations
1651 int __of_add_property(struct device_node *np, struct property *prop)
1653 struct property **next;
1656 next = &np->properties;
1658 if (strcmp(prop->name, (*next)->name) == 0)
1659 /* duplicate ! don't insert it */
1662 next = &(*next)->next;
1670 * of_add_property - Add a property to a node
1672 int of_add_property(struct device_node *np, struct property *prop)
1674 unsigned long flags;
1677 mutex_lock(&of_mutex);
1679 raw_spin_lock_irqsave(&devtree_lock, flags);
1680 rc = __of_add_property(np, prop);
1681 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1684 __of_add_property_sysfs(np, prop);
1686 mutex_unlock(&of_mutex);
1689 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1694 int __of_remove_property(struct device_node *np, struct property *prop)
1696 struct property **next;
1698 for (next = &np->properties; *next; next = &(*next)->next) {
1705 /* found the node */
1707 prop->next = np->deadprops;
1708 np->deadprops = prop;
1713 void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1715 /* at early boot, bail here and defer setup to of_init() */
1716 if (of_kset && of_node_is_attached(np))
1717 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1721 * of_remove_property - Remove a property from a node.
1723 * Note that we don't actually remove it, since we have given out
1724 * who-knows-how-many pointers to the data using get-property.
1725 * Instead we just move the property to the "dead properties"
1726 * list, so it won't be found any more.
1728 int of_remove_property(struct device_node *np, struct property *prop)
1730 unsigned long flags;
1733 mutex_lock(&of_mutex);
1735 raw_spin_lock_irqsave(&devtree_lock, flags);
1736 rc = __of_remove_property(np, prop);
1737 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1740 __of_remove_property_sysfs(np, prop);
1742 mutex_unlock(&of_mutex);
1745 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1750 int __of_update_property(struct device_node *np, struct property *newprop,
1751 struct property **oldpropp)
1753 struct property **next, *oldprop;
1755 for (next = &np->properties; *next; next = &(*next)->next) {
1756 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1759 *oldpropp = oldprop = *next;
1762 /* replace the node */
1763 newprop->next = oldprop->next;
1765 oldprop->next = np->deadprops;
1766 np->deadprops = oldprop;
1769 newprop->next = NULL;
1776 void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1777 struct property *oldprop)
1779 /* At early boot, bail out and defer setup to of_init() */
1784 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1785 __of_add_property_sysfs(np, newprop);
1789 * of_update_property - Update a property in a node, if the property does
1790 * not exist, add it.
1792 * Note that we don't actually remove it, since we have given out
1793 * who-knows-how-many pointers to the data using get-property.
1794 * Instead we just move the property to the "dead properties" list,
1795 * and add the new property to the property list
1797 int of_update_property(struct device_node *np, struct property *newprop)
1799 struct property *oldprop;
1800 unsigned long flags;
1806 mutex_lock(&of_mutex);
1808 raw_spin_lock_irqsave(&devtree_lock, flags);
1809 rc = __of_update_property(np, newprop, &oldprop);
1810 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1813 __of_update_property_sysfs(np, newprop, oldprop);
1815 mutex_unlock(&of_mutex);
1818 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1823 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1824 int id, const char *stem, int stem_len)
1828 strncpy(ap->stem, stem, stem_len);
1829 ap->stem[stem_len] = 0;
1830 list_add_tail(&ap->link, &aliases_lookup);
1831 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1832 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1836 * of_alias_scan - Scan all properties of 'aliases' node
1838 * The function scans all the properties of 'aliases' node and populate
1839 * the the global lookup table with the properties. It returns the
1840 * number of alias_prop found, or error code in error case.
1842 * @dt_alloc: An allocator that provides a virtual address to memory
1843 * for the resulting tree
1845 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1847 struct property *pp;
1849 of_chosen = of_find_node_by_path("/chosen");
1850 if (of_chosen == NULL)
1851 of_chosen = of_find_node_by_path("/chosen@0");
1854 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
1856 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1858 of_stdout = of_find_node_by_path(name);
1861 of_aliases = of_find_node_by_path("/aliases");
1865 for_each_property_of_node(of_aliases, pp) {
1866 const char *start = pp->name;
1867 const char *end = start + strlen(start);
1868 struct device_node *np;
1869 struct alias_prop *ap;
1872 /* Skip those we do not want to proceed */
1873 if (!strcmp(pp->name, "name") ||
1874 !strcmp(pp->name, "phandle") ||
1875 !strcmp(pp->name, "linux,phandle"))
1878 np = of_find_node_by_path(pp->value);
1882 /* walk the alias backwards to extract the id and work out
1883 * the 'stem' string */
1884 while (isdigit(*(end-1)) && end > start)
1888 if (kstrtoint(end, 10, &id) < 0)
1891 /* Allocate an alias_prop with enough space for the stem */
1892 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1895 memset(ap, 0, sizeof(*ap) + len + 1);
1897 of_alias_add(ap, np, id, start, len);
1902 * of_alias_get_id - Get alias id for the given device_node
1903 * @np: Pointer to the given device_node
1904 * @stem: Alias stem of the given device_node
1906 * The function travels the lookup table to get the alias id for the given
1907 * device_node and alias stem. It returns the alias id if found.
1909 int of_alias_get_id(struct device_node *np, const char *stem)
1911 struct alias_prop *app;
1914 mutex_lock(&of_mutex);
1915 list_for_each_entry(app, &aliases_lookup, link) {
1916 if (strcmp(app->stem, stem) != 0)
1919 if (np == app->np) {
1924 mutex_unlock(&of_mutex);
1928 EXPORT_SYMBOL_GPL(of_alias_get_id);
1930 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1933 const void *curv = cur;
1943 curv += sizeof(*cur);
1944 if (curv >= prop->value + prop->length)
1948 *pu = be32_to_cpup(curv);
1951 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1953 const char *of_prop_next_string(struct property *prop, const char *cur)
1955 const void *curv = cur;
1963 curv += strlen(cur) + 1;
1964 if (curv >= prop->value + prop->length)
1969 EXPORT_SYMBOL_GPL(of_prop_next_string);
1972 * of_device_is_stdout_path - check if a device node matches the
1973 * linux,stdout-path property
1975 * Check if this device node matches the linux,stdout-path property
1976 * in the chosen node. return true if yes, false otherwise.
1978 int of_device_is_stdout_path(struct device_node *dn)
1983 return of_stdout == dn;
1985 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
1988 * of_find_next_cache_node - Find a node's subsidiary cache
1989 * @np: node of type "cpu" or "cache"
1991 * Returns a node pointer with refcount incremented, use
1992 * of_node_put() on it when done. Caller should hold a reference
1995 struct device_node *of_find_next_cache_node(const struct device_node *np)
1997 struct device_node *child;
1998 const phandle *handle;
2000 handle = of_get_property(np, "l2-cache", NULL);
2002 handle = of_get_property(np, "next-level-cache", NULL);
2005 return of_find_node_by_phandle(be32_to_cpup(handle));
2007 /* OF on pmac has nodes instead of properties named "l2-cache"
2008 * beneath CPU nodes.
2010 if (!strcmp(np->type, "cpu"))
2011 for_each_child_of_node(np, child)
2012 if (!strcmp(child->type, "cache"))
2019 * of_graph_parse_endpoint() - parse common endpoint node properties
2020 * @node: pointer to endpoint device_node
2021 * @endpoint: pointer to the OF endpoint data structure
2023 * The caller should hold a reference to @node.
2025 int of_graph_parse_endpoint(const struct device_node *node,
2026 struct of_endpoint *endpoint)
2028 struct device_node *port_node = of_get_parent(node);
2030 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2031 __func__, node->full_name);
2033 memset(endpoint, 0, sizeof(*endpoint));
2035 endpoint->local_node = node;
2037 * It doesn't matter whether the two calls below succeed.
2038 * If they don't then the default value 0 is used.
2040 of_property_read_u32(port_node, "reg", &endpoint->port);
2041 of_property_read_u32(node, "reg", &endpoint->id);
2043 of_node_put(port_node);
2047 EXPORT_SYMBOL(of_graph_parse_endpoint);
2050 * of_graph_get_next_endpoint() - get next endpoint node
2051 * @parent: pointer to the parent device node
2052 * @prev: previous endpoint node, or NULL to get first
2054 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2055 * of the passed @prev node is not decremented, the caller have to use
2056 * of_node_put() on it when done.
2058 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2059 struct device_node *prev)
2061 struct device_node *endpoint;
2062 struct device_node *port;
2068 * Start by locating the port node. If no previous endpoint is specified
2069 * search for the first port node, otherwise get the previous endpoint
2073 struct device_node *node;
2075 node = of_get_child_by_name(parent, "ports");
2079 port = of_get_child_by_name(parent, "port");
2083 pr_err("%s(): no port node found in %s\n",
2084 __func__, parent->full_name);
2088 port = of_get_parent(prev);
2089 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2090 __func__, prev->full_name))
2094 * Avoid dropping prev node refcount to 0 when getting the next
2102 * Now that we have a port node, get the next endpoint by
2103 * getting the next child. If the previous endpoint is NULL this
2104 * will return the first child.
2106 endpoint = of_get_next_child(port, prev);
2112 /* No more endpoints under this port, try the next one. */
2116 port = of_get_next_child(parent, port);
2119 } while (of_node_cmp(port->name, "port"));
2122 EXPORT_SYMBOL(of_graph_get_next_endpoint);
2125 * of_graph_get_remote_port_parent() - get remote port's parent node
2126 * @node: pointer to a local endpoint device_node
2128 * Return: Remote device node associated with remote endpoint node linked
2129 * to @node. Use of_node_put() on it when done.
2131 struct device_node *of_graph_get_remote_port_parent(
2132 const struct device_node *node)
2134 struct device_node *np;
2137 /* Get remote endpoint node. */
2138 np = of_parse_phandle(node, "remote-endpoint", 0);
2140 /* Walk 3 levels up only if there is 'ports' node. */
2141 for (depth = 3; depth && np; depth--) {
2142 np = of_get_next_parent(np);
2143 if (depth == 2 && of_node_cmp(np->name, "ports"))
2148 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2151 * of_graph_get_remote_port() - get remote port node
2152 * @node: pointer to a local endpoint device_node
2154 * Return: Remote port node associated with remote endpoint node linked
2155 * to @node. Use of_node_put() on it when done.
2157 struct device_node *of_graph_get_remote_port(const struct device_node *node)
2159 struct device_node *np;
2161 /* Get remote endpoint node. */
2162 np = of_parse_phandle(node, "remote-endpoint", 0);
2165 return of_get_next_parent(np);
2167 EXPORT_SYMBOL(of_graph_get_remote_port);