Merge branch 'v3.10/topic/dm-crypt' into linux-linaro-lsk-v3.10
[firefly-linux-kernel-4.4.55.git] / drivers / of / base.c
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11  *
12  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13  *  Grant Likely.
14  *
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.
19  */
20 #include <linux/ctype.h>
21 #include <linux/cpu.h>
22 #include <linux/module.h>
23 #include <linux/of.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>
29
30 #include "of_private.h"
31
32 LIST_HEAD(aliases_lookup);
33
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;
39
40 struct kset *of_kset;
41
42 /*
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.
47  */
48 DEFINE_MUTEX(of_mutex);
49
50 /* use when traversing tree through the allnext, child, sibling,
51  * or parent members of struct device_node.
52  */
53 DEFINE_RAW_SPINLOCK(devtree_lock);
54
55 int of_n_addr_cells(struct device_node *np)
56 {
57         const __be32 *ip;
58
59         do {
60                 if (np->parent)
61                         np = np->parent;
62                 ip = of_get_property(np, "#address-cells", NULL);
63                 if (ip)
64                         return be32_to_cpup(ip);
65         } while (np->parent);
66         /* No #address-cells property for the root node */
67         return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
68 }
69 EXPORT_SYMBOL(of_n_addr_cells);
70
71 int of_n_size_cells(struct device_node *np)
72 {
73         const __be32 *ip;
74
75         do {
76                 if (np->parent)
77                         np = np->parent;
78                 ip = of_get_property(np, "#size-cells", NULL);
79                 if (ip)
80                         return be32_to_cpup(ip);
81         } while (np->parent);
82         /* No #size-cells property for the root node */
83         return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
84 }
85 EXPORT_SYMBOL(of_n_size_cells);
86
87 #ifndef CONFIG_OF_DYNAMIC
88 static void of_node_release(struct kobject *kobj)
89 {
90         /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
91 }
92 #endif /* CONFIG_OF_DYNAMIC */
93
94 struct kobj_type of_node_ktype = {
95         .release = of_node_release,
96 };
97
98 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
99                                 struct bin_attribute *bin_attr, char *buf,
100                                 loff_t offset, size_t count)
101 {
102         struct property *pp = container_of(bin_attr, struct property, attr);
103         return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
104 }
105
106 static const char *safe_name(struct kobject *kobj, const char *orig_name)
107 {
108         const char *name = orig_name;
109         struct sysfs_dirent *kn;
110         int i = 0;
111
112         /* don't be a hero. After 16 tries give up */
113         while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, NULL, name))) {
114                 sysfs_put(kn);
115                 if (name != orig_name)
116                         kfree(name);
117                 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
118         }
119
120         if (name != orig_name)
121                 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
122                         kobject_name(kobj), name);
123         return name;
124 }
125
126 int __of_add_property_sysfs(struct device_node *np, struct property *pp)
127 {
128         int rc;
129
130         /* Important: Don't leak passwords */
131         bool secure = strncmp(pp->name, "security-", 9) == 0;
132
133         if (!of_kset || !of_node_is_attached(np))
134                 return 0;
135
136         sysfs_bin_attr_init(&pp->attr);
137         pp->attr.attr.name = safe_name(&np->kobj, pp->name);
138         pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
139         pp->attr.size = secure ? 0 : pp->length;
140         pp->attr.read = of_node_property_read;
141
142         rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
143         WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
144         return rc;
145 }
146
147 int __of_attach_node_sysfs(struct device_node *np)
148 {
149         const char *name;
150         struct property *pp;
151         int rc;
152
153         if (!of_kset)
154                 return 0;
155
156         np->kobj.kset = of_kset;
157         if (!np->parent) {
158                 /* Nodes without parents are new top level trees */
159                 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
160         } else {
161                 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
162                 if (!name || !name[0])
163                         return -EINVAL;
164
165                 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
166         }
167         if (rc)
168                 return rc;
169
170         for_each_property_of_node(np, pp)
171                 __of_add_property_sysfs(np, pp);
172
173         return 0;
174 }
175
176 static int __init of_init(void)
177 {
178         struct device_node *np;
179
180         /* Create the kset, and register existing nodes */
181         mutex_lock(&of_mutex);
182         of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
183         if (!of_kset) {
184                 mutex_unlock(&of_mutex);
185                 return -ENOMEM;
186         }
187         for_each_of_allnodes(np)
188                 __of_attach_node_sysfs(np);
189         mutex_unlock(&of_mutex);
190
191         /* Symlink in /proc as required by userspace ABI */
192         if (of_allnodes)
193                 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
194
195         return 0;
196 }
197 core_initcall(of_init);
198
199 static struct property *__of_find_property(const struct device_node *np,
200                                            const char *name, int *lenp)
201 {
202         struct property *pp;
203
204         if (!np)
205                 return NULL;
206
207         for (pp = np->properties; pp; pp = pp->next) {
208                 if (of_prop_cmp(pp->name, name) == 0) {
209                         if (lenp)
210                                 *lenp = pp->length;
211                         break;
212                 }
213         }
214
215         return pp;
216 }
217
218 struct property *of_find_property(const struct device_node *np,
219                                   const char *name,
220                                   int *lenp)
221 {
222         struct property *pp;
223         unsigned long flags;
224
225         raw_spin_lock_irqsave(&devtree_lock, flags);
226         pp = __of_find_property(np, name, lenp);
227         raw_spin_unlock_irqrestore(&devtree_lock, flags);
228
229         return pp;
230 }
231 EXPORT_SYMBOL(of_find_property);
232
233 /**
234  * of_find_all_nodes - Get next node in global list
235  * @prev:       Previous node or NULL to start iteration
236  *              of_node_put() will be called on it
237  *
238  * Returns a node pointer with refcount incremented, use
239  * of_node_put() on it when done.
240  */
241 struct device_node *of_find_all_nodes(struct device_node *prev)
242 {
243         struct device_node *np;
244         unsigned long flags;
245
246         raw_spin_lock_irqsave(&devtree_lock, flags);
247         np = prev ? prev->allnext : of_allnodes;
248         for (; np != NULL; np = np->allnext)
249                 if (of_node_get(np))
250                         break;
251         of_node_put(prev);
252         raw_spin_unlock_irqrestore(&devtree_lock, flags);
253         return np;
254 }
255 EXPORT_SYMBOL(of_find_all_nodes);
256
257 /*
258  * Find a property with a given name for a given node
259  * and return the value.
260  */
261 const void *__of_get_property(const struct device_node *np,
262                               const char *name, int *lenp)
263 {
264         struct property *pp = __of_find_property(np, name, lenp);
265
266         return pp ? pp->value : NULL;
267 }
268
269 /*
270  * Find a property with a given name for a given node
271  * and return the value.
272  */
273 const void *of_get_property(const struct device_node *np, const char *name,
274                             int *lenp)
275 {
276         struct property *pp = of_find_property(np, name, lenp);
277
278         return pp ? pp->value : NULL;
279 }
280 EXPORT_SYMBOL(of_get_property);
281
282 /*
283  * arch_match_cpu_phys_id - Match the given logical CPU and physical id
284  *
285  * @cpu: logical cpu index of a core/thread
286  * @phys_id: physical identifier of a core/thread
287  *
288  * CPU logical to physical index mapping is architecture specific.
289  * However this __weak function provides a default match of physical
290  * id to logical cpu index. phys_id provided here is usually values read
291  * from the device tree which must match the hardware internal registers.
292  *
293  * Returns true if the physical identifier and the logical cpu index
294  * correspond to the same core/thread, false otherwise.
295  */
296 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
297 {
298         return (u32)phys_id == cpu;
299 }
300
301 /**
302  * Checks if the given "prop_name" property holds the physical id of the
303  * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
304  * NULL, local thread number within the core is returned in it.
305  */
306 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
307                         const char *prop_name, int cpu, unsigned int *thread)
308 {
309         const __be32 *cell;
310         int ac, prop_len, tid;
311         u64 hwid;
312
313         ac = of_n_addr_cells(cpun);
314         cell = of_get_property(cpun, prop_name, &prop_len);
315         if (!cell)
316                 return false;
317         prop_len /= sizeof(*cell);
318         for (tid = 0; tid < prop_len; tid++) {
319                 hwid = of_read_number(cell, ac);
320                 if (arch_match_cpu_phys_id(cpu, hwid)) {
321                         if (thread)
322                                 *thread = tid;
323                         return true;
324                 }
325                 cell += ac;
326         }
327         return false;
328 }
329
330 /**
331  * of_get_cpu_node - Get device node associated with the given logical CPU
332  *
333  * @cpu: CPU number(logical index) for which device node is required
334  * @thread: if not NULL, local thread number within the physical core is
335  *          returned
336  *
337  * The main purpose of this function is to retrieve the device node for the
338  * given logical CPU index. It should be used to initialize the of_node in
339  * cpu device. Once of_node in cpu device is populated, all the further
340  * references can use that instead.
341  *
342  * CPU logical to physical index mapping is architecture specific and is built
343  * before booting secondary cores. This function uses arch_match_cpu_phys_id
344  * which can be overridden by architecture specific implementation.
345  *
346  * Returns a node pointer for the logical cpu if found, else NULL.
347  */
348 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
349 {
350         struct device_node *cpun, *cpus;
351
352         cpus = of_find_node_by_path("/cpus");
353         if (!cpus) {
354                 pr_warn("Missing cpus node, bailing out\n");
355                 return NULL;
356         }
357
358         for_each_child_of_node(cpus, cpun) {
359                 if (of_node_cmp(cpun->type, "cpu"))
360                         continue;
361                 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
362                  * for thread ids on PowerPC. If it doesn't exist fallback to
363                  * standard "reg" property.
364                  */
365                 if (IS_ENABLED(CONFIG_PPC) &&
366                         __of_find_n_match_cpu_property(cpun,
367                                 "ibm,ppc-interrupt-server#s", cpu, thread))
368                         return cpun;
369                 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
370                         return cpun;
371         }
372         return NULL;
373 }
374 EXPORT_SYMBOL(of_get_cpu_node);
375
376 /** Checks if the given "compat" string matches one of the strings in
377  * the device's "compatible" property
378  */
379 static int __of_device_is_compatible(const struct device_node *device,
380                                      const char *compat)
381 {
382         const char* cp;
383         int cplen, l;
384
385         cp = __of_get_property(device, "compatible", &cplen);
386         if (cp == NULL)
387                 return 0;
388         while (cplen > 0) {
389                 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
390                         return 1;
391                 l = strlen(cp) + 1;
392                 cp += l;
393                 cplen -= l;
394         }
395
396         return 0;
397 }
398
399 /** Checks if the given "compat" string matches one of the strings in
400  * the device's "compatible" property
401  */
402 int of_device_is_compatible(const struct device_node *device,
403                 const char *compat)
404 {
405         unsigned long flags;
406         int res;
407
408         raw_spin_lock_irqsave(&devtree_lock, flags);
409         res = __of_device_is_compatible(device, compat);
410         raw_spin_unlock_irqrestore(&devtree_lock, flags);
411         return res;
412 }
413 EXPORT_SYMBOL(of_device_is_compatible);
414
415 /**
416  * of_machine_is_compatible - Test root of device tree for a given compatible value
417  * @compat: compatible string to look for in root node's compatible property.
418  *
419  * Returns true if the root node has the given value in its
420  * compatible property.
421  */
422 int of_machine_is_compatible(const char *compat)
423 {
424         struct device_node *root;
425         int rc = 0;
426
427         root = of_find_node_by_path("/");
428         if (root) {
429                 rc = of_device_is_compatible(root, compat);
430                 of_node_put(root);
431         }
432         return rc;
433 }
434 EXPORT_SYMBOL(of_machine_is_compatible);
435
436 /**
437  *  __of_device_is_available - check if a device is available for use
438  *
439  *  @device: Node to check for availability, with locks already held
440  *
441  *  Returns 1 if the status property is absent or set to "okay" or "ok",
442  *  0 otherwise
443  */
444 static int __of_device_is_available(const struct device_node *device)
445 {
446         const char *status;
447         int statlen;
448
449         status = __of_get_property(device, "status", &statlen);
450         if (status == NULL)
451                 return 1;
452
453         if (statlen > 0) {
454                 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
455                         return 1;
456         }
457
458         return 0;
459 }
460
461 /**
462  *  of_device_is_available - check if a device is available for use
463  *
464  *  @device: Node to check for availability
465  *
466  *  Returns 1 if the status property is absent or set to "okay" or "ok",
467  *  0 otherwise
468  */
469 int of_device_is_available(const struct device_node *device)
470 {
471         unsigned long flags;
472         int res;
473
474         raw_spin_lock_irqsave(&devtree_lock, flags);
475         res = __of_device_is_available(device);
476         raw_spin_unlock_irqrestore(&devtree_lock, flags);
477         return res;
478
479 }
480 EXPORT_SYMBOL(of_device_is_available);
481
482 /**
483  *      of_get_parent - Get a node's parent if any
484  *      @node:  Node to get parent
485  *
486  *      Returns a node pointer with refcount incremented, use
487  *      of_node_put() on it when done.
488  */
489 struct device_node *of_get_parent(const struct device_node *node)
490 {
491         struct device_node *np;
492         unsigned long flags;
493
494         if (!node)
495                 return NULL;
496
497         raw_spin_lock_irqsave(&devtree_lock, flags);
498         np = of_node_get(node->parent);
499         raw_spin_unlock_irqrestore(&devtree_lock, flags);
500         return np;
501 }
502 EXPORT_SYMBOL(of_get_parent);
503
504 /**
505  *      of_get_next_parent - Iterate to a node's parent
506  *      @node:  Node to get parent of
507  *
508  *      This is like of_get_parent() except that it drops the
509  *      refcount on the passed node, making it suitable for iterating
510  *      through a node's parents.
511  *
512  *      Returns a node pointer with refcount incremented, use
513  *      of_node_put() on it when done.
514  */
515 struct device_node *of_get_next_parent(struct device_node *node)
516 {
517         struct device_node *parent;
518         unsigned long flags;
519
520         if (!node)
521                 return NULL;
522
523         raw_spin_lock_irqsave(&devtree_lock, flags);
524         parent = of_node_get(node->parent);
525         of_node_put(node);
526         raw_spin_unlock_irqrestore(&devtree_lock, flags);
527         return parent;
528 }
529 EXPORT_SYMBOL(of_get_next_parent);
530
531 /**
532  *      of_get_next_child - Iterate a node childs
533  *      @node:  parent node
534  *      @prev:  previous child of the parent node, or NULL to get first
535  *
536  *      Returns a node pointer with refcount incremented, use
537  *      of_node_put() on it when done.
538  */
539 struct device_node *of_get_next_child(const struct device_node *node,
540         struct device_node *prev)
541 {
542         struct device_node *next;
543         unsigned long flags;
544
545         raw_spin_lock_irqsave(&devtree_lock, flags);
546         next = prev ? prev->sibling : node->child;
547         for (; next; next = next->sibling)
548                 if (of_node_get(next))
549                         break;
550         of_node_put(prev);
551         raw_spin_unlock_irqrestore(&devtree_lock, flags);
552         return next;
553 }
554 EXPORT_SYMBOL(of_get_next_child);
555
556 /**
557  *      of_get_next_available_child - Find the next available child node
558  *      @node:  parent node
559  *      @prev:  previous child of the parent node, or NULL to get first
560  *
561  *      This function is like of_get_next_child(), except that it
562  *      automatically skips any disabled nodes (i.e. status = "disabled").
563  */
564 struct device_node *of_get_next_available_child(const struct device_node *node,
565         struct device_node *prev)
566 {
567         struct device_node *next;
568         unsigned long flags;
569
570         raw_spin_lock_irqsave(&devtree_lock, flags);
571         next = prev ? prev->sibling : node->child;
572         for (; next; next = next->sibling) {
573                 if (!__of_device_is_available(next))
574                         continue;
575                 if (of_node_get(next))
576                         break;
577         }
578         of_node_put(prev);
579         raw_spin_unlock_irqrestore(&devtree_lock, flags);
580         return next;
581 }
582 EXPORT_SYMBOL(of_get_next_available_child);
583
584 /**
585  *      of_get_child_by_name - Find the child node by name for a given parent
586  *      @node:  parent node
587  *      @name:  child name to look for.
588  *
589  *      This function looks for child node for given matching name
590  *
591  *      Returns a node pointer if found, with refcount incremented, use
592  *      of_node_put() on it when done.
593  *      Returns NULL if node is not found.
594  */
595 struct device_node *of_get_child_by_name(const struct device_node *node,
596                                 const char *name)
597 {
598         struct device_node *child;
599
600         for_each_child_of_node(node, child)
601                 if (child->name && (of_node_cmp(child->name, name) == 0))
602                         break;
603         return child;
604 }
605 EXPORT_SYMBOL(of_get_child_by_name);
606
607 /**
608  *      of_find_node_by_path - Find a node matching a full OF path
609  *      @path:  The full path to match
610  *
611  *      Returns a node pointer with refcount incremented, use
612  *      of_node_put() on it when done.
613  */
614 struct device_node *of_find_node_by_path(const char *path)
615 {
616         struct device_node *np = of_allnodes;
617         unsigned long flags;
618
619         raw_spin_lock_irqsave(&devtree_lock, flags);
620         for (; np; np = np->allnext) {
621                 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
622                     && of_node_get(np))
623                         break;
624         }
625         raw_spin_unlock_irqrestore(&devtree_lock, flags);
626         return np;
627 }
628 EXPORT_SYMBOL(of_find_node_by_path);
629
630 /**
631  *      of_find_node_by_name - Find a node by its "name" property
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  *      @name:  The name string to match against
637  *
638  *      Returns a node pointer with refcount incremented, use
639  *      of_node_put() on it when done.
640  */
641 struct device_node *of_find_node_by_name(struct device_node *from,
642         const char *name)
643 {
644         struct device_node *np;
645         unsigned long flags;
646
647         raw_spin_lock_irqsave(&devtree_lock, flags);
648         np = from ? from->allnext : of_allnodes;
649         for (; np; np = np->allnext)
650                 if (np->name && (of_node_cmp(np->name, name) == 0)
651                     && of_node_get(np))
652                         break;
653         of_node_put(from);
654         raw_spin_unlock_irqrestore(&devtree_lock, flags);
655         return np;
656 }
657 EXPORT_SYMBOL(of_find_node_by_name);
658
659 /**
660  *      of_find_node_by_type - Find a node by its "device_type" property
661  *      @from:  The node to start searching from, or NULL to start searching
662  *              the entire device tree. The node you pass will not be
663  *              searched, only the next one will; typically, you pass
664  *              what the previous call returned. of_node_put() will be
665  *              called on from for you.
666  *      @type:  The type string to match against
667  *
668  *      Returns a node pointer with refcount incremented, use
669  *      of_node_put() on it when done.
670  */
671 struct device_node *of_find_node_by_type(struct device_node *from,
672         const char *type)
673 {
674         struct device_node *np;
675         unsigned long flags;
676
677         raw_spin_lock_irqsave(&devtree_lock, flags);
678         np = from ? from->allnext : of_allnodes;
679         for (; np; np = np->allnext)
680                 if (np->type && (of_node_cmp(np->type, type) == 0)
681                     && of_node_get(np))
682                         break;
683         of_node_put(from);
684         raw_spin_unlock_irqrestore(&devtree_lock, flags);
685         return np;
686 }
687 EXPORT_SYMBOL(of_find_node_by_type);
688
689 /**
690  *      of_find_compatible_node - Find a node based on type and one of the
691  *                                tokens in its "compatible" property
692  *      @from:          The node to start searching from or NULL, the node
693  *                      you pass will not be searched, only the next one
694  *                      will; typically, you pass what the previous call
695  *                      returned. of_node_put() will be called on it
696  *      @type:          The type string to match "device_type" or NULL to ignore
697  *      @compatible:    The string to match to one of the tokens in the device
698  *                      "compatible" list.
699  *
700  *      Returns a node pointer with refcount incremented, use
701  *      of_node_put() on it when done.
702  */
703 struct device_node *of_find_compatible_node(struct device_node *from,
704         const char *type, const char *compatible)
705 {
706         struct device_node *np;
707         unsigned long flags;
708
709         raw_spin_lock_irqsave(&devtree_lock, flags);
710         np = from ? from->allnext : of_allnodes;
711         for (; np; np = np->allnext) {
712                 if (type
713                     && !(np->type && (of_node_cmp(np->type, type) == 0)))
714                         continue;
715                 if (__of_device_is_compatible(np, compatible) &&
716                     of_node_get(np))
717                         break;
718         }
719         of_node_put(from);
720         raw_spin_unlock_irqrestore(&devtree_lock, flags);
721         return np;
722 }
723 EXPORT_SYMBOL(of_find_compatible_node);
724
725 /**
726  *      of_find_node_with_property - Find a node which has a property with
727  *                                   the given name.
728  *      @from:          The node to start searching from or NULL, the node
729  *                      you pass will not be searched, only the next one
730  *                      will; typically, you pass what the previous call
731  *                      returned. of_node_put() will be called on it
732  *      @prop_name:     The name of the property to look for.
733  *
734  *      Returns a node pointer with refcount incremented, use
735  *      of_node_put() on it when done.
736  */
737 struct device_node *of_find_node_with_property(struct device_node *from,
738         const char *prop_name)
739 {
740         struct device_node *np;
741         struct property *pp;
742         unsigned long flags;
743
744         raw_spin_lock_irqsave(&devtree_lock, flags);
745         np = from ? from->allnext : of_allnodes;
746         for (; np; np = np->allnext) {
747                 for (pp = np->properties; pp; pp = pp->next) {
748                         if (of_prop_cmp(pp->name, prop_name) == 0) {
749                                 of_node_get(np);
750                                 goto out;
751                         }
752                 }
753         }
754 out:
755         of_node_put(from);
756         raw_spin_unlock_irqrestore(&devtree_lock, flags);
757         return np;
758 }
759 EXPORT_SYMBOL(of_find_node_with_property);
760
761 static
762 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
763                                            const struct device_node *node)
764 {
765         if (!matches)
766                 return NULL;
767
768         while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
769                 int match = 1;
770                 if (matches->name[0])
771                         match &= node->name
772                                 && !strcmp(matches->name, node->name);
773                 if (matches->type[0])
774                         match &= node->type
775                                 && !strcmp(matches->type, node->type);
776                 if (matches->compatible[0])
777                         match &= __of_device_is_compatible(node,
778                                                            matches->compatible);
779                 if (match)
780                         return matches;
781                 matches++;
782         }
783         return NULL;
784 }
785
786 /**
787  * of_match_node - Tell if an device_node has a matching of_match structure
788  *      @matches:       array of of device match structures to search in
789  *      @node:          the of device structure to match against
790  *
791  *      Low level utility function used by device matching.
792  */
793 const struct of_device_id *of_match_node(const struct of_device_id *matches,
794                                          const struct device_node *node)
795 {
796         const struct of_device_id *match;
797         unsigned long flags;
798
799         raw_spin_lock_irqsave(&devtree_lock, flags);
800         match = __of_match_node(matches, node);
801         raw_spin_unlock_irqrestore(&devtree_lock, flags);
802         return match;
803 }
804 EXPORT_SYMBOL(of_match_node);
805
806 /**
807  *      of_find_matching_node_and_match - Find a node based on an of_device_id
808  *                                        match table.
809  *      @from:          The node to start searching from or NULL, the node
810  *                      you pass will not be searched, only the next one
811  *                      will; typically, you pass what the previous call
812  *                      returned. of_node_put() will be called on it
813  *      @matches:       array of of device match structures to search in
814  *      @match          Updated to point at the matches entry which matched
815  *
816  *      Returns a node pointer with refcount incremented, use
817  *      of_node_put() on it when done.
818  */
819 struct device_node *of_find_matching_node_and_match(struct device_node *from,
820                                         const struct of_device_id *matches,
821                                         const struct of_device_id **match)
822 {
823         struct device_node *np;
824         const struct of_device_id *m;
825         unsigned long flags;
826
827         if (match)
828                 *match = NULL;
829
830         raw_spin_lock_irqsave(&devtree_lock, flags);
831         np = from ? from->allnext : of_allnodes;
832         for (; np; np = np->allnext) {
833                 m = __of_match_node(matches, np);
834                 if (m && of_node_get(np)) {
835                         if (match)
836                                 *match = m;
837                         break;
838                 }
839         }
840         of_node_put(from);
841         raw_spin_unlock_irqrestore(&devtree_lock, flags);
842         return np;
843 }
844 EXPORT_SYMBOL(of_find_matching_node_and_match);
845
846 /**
847  * of_modalias_node - Lookup appropriate modalias for a device node
848  * @node:       pointer to a device tree node
849  * @modalias:   Pointer to buffer that modalias value will be copied into
850  * @len:        Length of modalias value
851  *
852  * Based on the value of the compatible property, this routine will attempt
853  * to choose an appropriate modalias value for a particular device tree node.
854  * It does this by stripping the manufacturer prefix (as delimited by a ',')
855  * from the first entry in the compatible list property.
856  *
857  * This routine returns 0 on success, <0 on failure.
858  */
859 int of_modalias_node(struct device_node *node, char *modalias, int len)
860 {
861         const char *compatible, *p;
862         int cplen;
863
864         compatible = of_get_property(node, "compatible", &cplen);
865         if (!compatible || strlen(compatible) > cplen)
866                 return -ENODEV;
867         p = strchr(compatible, ',');
868         strlcpy(modalias, p ? p + 1 : compatible, len);
869         return 0;
870 }
871 EXPORT_SYMBOL_GPL(of_modalias_node);
872
873 /**
874  * of_find_node_by_phandle - Find a node given a phandle
875  * @handle:     phandle of the node to find
876  *
877  * Returns a node pointer with refcount incremented, use
878  * of_node_put() on it when done.
879  */
880 struct device_node *of_find_node_by_phandle(phandle handle)
881 {
882         struct device_node *np;
883         unsigned long flags;
884
885         raw_spin_lock_irqsave(&devtree_lock, flags);
886         for (np = of_allnodes; np; np = np->allnext)
887                 if (np->phandle == handle)
888                         break;
889         of_node_get(np);
890         raw_spin_unlock_irqrestore(&devtree_lock, flags);
891         return np;
892 }
893 EXPORT_SYMBOL(of_find_node_by_phandle);
894
895 /**
896  * of_property_count_elems_of_size - Count the number of elements in a property
897  *
898  * @np:         device node from which the property value is to be read.
899  * @propname:   name of the property to be searched.
900  * @elem_size:  size of the individual element
901  *
902  * Search for a property in a device node and count the number of elements of
903  * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
904  * property does not exist or its length does not match a multiple of elem_size
905  * and -ENODATA if the property does not have a value.
906  */
907 int of_property_count_elems_of_size(const struct device_node *np,
908                                 const char *propname, int elem_size)
909 {
910         struct property *prop = of_find_property(np, propname, NULL);
911
912         if (!prop)
913                 return -EINVAL;
914         if (!prop->value)
915                 return -ENODATA;
916
917         if (prop->length % elem_size != 0) {
918                 pr_err("size of %s in node %s is not a multiple of %d\n",
919                        propname, np->full_name, elem_size);
920                 return -EINVAL;
921         }
922
923         return prop->length / elem_size;
924 }
925 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
926
927 /**
928  * of_find_property_value_of_size
929  *
930  * @np:         device node from which the property value is to be read.
931  * @propname:   name of the property to be searched.
932  * @len:        requested length of property value
933  *
934  * Search for a property in a device node and valid the requested size.
935  * Returns the property value on success, -EINVAL if the property does not
936  *  exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
937  * property data isn't large enough.
938  *
939  */
940 static void *of_find_property_value_of_size(const struct device_node *np,
941                         const char *propname, u32 len)
942 {
943         struct property *prop = of_find_property(np, propname, NULL);
944
945         if (!prop)
946                 return ERR_PTR(-EINVAL);
947         if (!prop->value)
948                 return ERR_PTR(-ENODATA);
949         if (len > prop->length)
950                 return ERR_PTR(-EOVERFLOW);
951
952         return prop->value;
953 }
954
955 /**
956  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
957  *
958  * @np:         device node from which the property value is to be read.
959  * @propname:   name of the property to be searched.
960  * @index:      index of the u32 in the list of values
961  * @out_value:  pointer to return value, modified only if no error.
962  *
963  * Search for a property in a device node and read nth 32-bit value from
964  * it. Returns 0 on success, -EINVAL if the property does not exist,
965  * -ENODATA if property does not have a value, and -EOVERFLOW if the
966  * property data isn't large enough.
967  *
968  * The out_value is modified only if a valid u32 value can be decoded.
969  */
970 int of_property_read_u32_index(const struct device_node *np,
971                                        const char *propname,
972                                        u32 index, u32 *out_value)
973 {
974         const u32 *val = of_find_property_value_of_size(np, propname,
975                                         ((index + 1) * sizeof(*out_value)));
976
977         if (IS_ERR(val))
978                 return PTR_ERR(val);
979
980         *out_value = be32_to_cpup(((__be32 *)val) + index);
981         return 0;
982 }
983 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
984
985 /**
986  * of_property_read_u8_array - Find and read an array of u8 from a property.
987  *
988  * @np:         device node from which the property value is to be read.
989  * @propname:   name of the property to be searched.
990  * @out_value:  pointer to return value, modified only if return value is 0.
991  * @sz:         number of array elements to read
992  *
993  * Search for a property in a device node and read 8-bit value(s) 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.
997  *
998  * dts entry of array should be like:
999  *      property = /bits/ 8 <0x50 0x60 0x70>;
1000  *
1001  * The out_value is modified only if a valid u8 value can be decoded.
1002  */
1003 int of_property_read_u8_array(const struct device_node *np,
1004                         const char *propname, u8 *out_values, size_t sz)
1005 {
1006         const u8 *val = of_find_property_value_of_size(np, propname,
1007                                                 (sz * sizeof(*out_values)));
1008
1009         if (IS_ERR(val))
1010                 return PTR_ERR(val);
1011
1012         while (sz--)
1013                 *out_values++ = *val++;
1014         return 0;
1015 }
1016 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1017
1018 /**
1019  * of_property_read_u16_array - Find and read an array of u16 from a property.
1020  *
1021  * @np:         device node from which the property value is to be read.
1022  * @propname:   name of the property to be searched.
1023  * @out_value:  pointer to return value, modified only if return value is 0.
1024  * @sz:         number of array elements to read
1025  *
1026  * Search for a property in a device node and read 16-bit value(s) from
1027  * it. Returns 0 on success, -EINVAL if the property does not exist,
1028  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1029  * property data isn't large enough.
1030  *
1031  * dts entry of array should be like:
1032  *      property = /bits/ 16 <0x5000 0x6000 0x7000>;
1033  *
1034  * The out_value is modified only if a valid u16 value can be decoded.
1035  */
1036 int of_property_read_u16_array(const struct device_node *np,
1037                         const char *propname, u16 *out_values, size_t sz)
1038 {
1039         const __be16 *val = of_find_property_value_of_size(np, propname,
1040                                                 (sz * sizeof(*out_values)));
1041
1042         if (IS_ERR(val))
1043                 return PTR_ERR(val);
1044
1045         while (sz--)
1046                 *out_values++ = be16_to_cpup(val++);
1047         return 0;
1048 }
1049 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1050
1051 /**
1052  * of_property_read_u32_array - Find and read an array of 32 bit integers
1053  * from a property.
1054  *
1055  * @np:         device node from which the property value is to be read.
1056  * @propname:   name of the property to be searched.
1057  * @out_value:  pointer to return value, modified only if return value is 0.
1058  * @sz:         number of array elements to read
1059  *
1060  * Search for a property in a device node and read 32-bit value(s) from
1061  * it. Returns 0 on success, -EINVAL if the property does not exist,
1062  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1063  * property data isn't large enough.
1064  *
1065  * The out_value is modified only if a valid u32 value can be decoded.
1066  */
1067 int of_property_read_u32_array(const struct device_node *np,
1068                                const char *propname, u32 *out_values,
1069                                size_t sz)
1070 {
1071         const __be32 *val = of_find_property_value_of_size(np, propname,
1072                                                 (sz * sizeof(*out_values)));
1073
1074         if (IS_ERR(val))
1075                 return PTR_ERR(val);
1076
1077         while (sz--)
1078                 *out_values++ = be32_to_cpup(val++);
1079         return 0;
1080 }
1081 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1082
1083 /**
1084  * of_property_read_u64 - Find and read a 64 bit integer from a property
1085  * @np:         device node from which the property value is to be read.
1086  * @propname:   name of the property to be searched.
1087  * @out_value:  pointer to return value, modified only if return value is 0.
1088  *
1089  * Search for a property in a device node and read a 64-bit value from
1090  * it. Returns 0 on success, -EINVAL if the property does not exist,
1091  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1092  * property data isn't large enough.
1093  *
1094  * The out_value is modified only if a valid u64 value can be decoded.
1095  */
1096 int of_property_read_u64(const struct device_node *np, const char *propname,
1097                          u64 *out_value)
1098 {
1099         const __be32 *val = of_find_property_value_of_size(np, propname,
1100                                                 sizeof(*out_value));
1101
1102         if (IS_ERR(val))
1103                 return PTR_ERR(val);
1104
1105         *out_value = of_read_number(val, 2);
1106         return 0;
1107 }
1108 EXPORT_SYMBOL_GPL(of_property_read_u64);
1109
1110 /**
1111  * of_property_read_string - Find and read a string from a property
1112  * @np:         device node from which the property value is to be read.
1113  * @propname:   name of the property to be searched.
1114  * @out_string: pointer to null terminated return string, modified only if
1115  *              return value is 0.
1116  *
1117  * Search for a property in a device tree node and retrieve a null
1118  * terminated string value (pointer to data, not a copy). Returns 0 on
1119  * success, -EINVAL if the property does not exist, -ENODATA if property
1120  * does not have a value, and -EILSEQ if the string is not null-terminated
1121  * within the length of the property data.
1122  *
1123  * The out_string pointer is modified only if a valid string can be decoded.
1124  */
1125 int of_property_read_string(struct device_node *np, const char *propname,
1126                                 const char **out_string)
1127 {
1128         struct property *prop = of_find_property(np, propname, NULL);
1129         if (!prop)
1130                 return -EINVAL;
1131         if (!prop->value)
1132                 return -ENODATA;
1133         if (strnlen(prop->value, prop->length) >= prop->length)
1134                 return -EILSEQ;
1135         *out_string = prop->value;
1136         return 0;
1137 }
1138 EXPORT_SYMBOL_GPL(of_property_read_string);
1139
1140 /**
1141  * of_property_match_string() - Find string in a list and return index
1142  * @np: pointer to node containing string list property
1143  * @propname: string list property name
1144  * @string: pointer to string to search for in string list
1145  *
1146  * This function searches a string list property and returns the index
1147  * of a specific string value.
1148  */
1149 int of_property_match_string(struct device_node *np, const char *propname,
1150                              const char *string)
1151 {
1152         struct property *prop = of_find_property(np, propname, NULL);
1153         size_t l;
1154         int i;
1155         const char *p, *end;
1156
1157         if (!prop)
1158                 return -EINVAL;
1159         if (!prop->value)
1160                 return -ENODATA;
1161
1162         p = prop->value;
1163         end = p + prop->length;
1164
1165         for (i = 0; p < end; i++, p += l) {
1166                 l = strnlen(p, end - p) + 1;
1167                 if (p + l > end)
1168                         return -EILSEQ;
1169                 pr_debug("comparing %s with %s\n", string, p);
1170                 if (strcmp(string, p) == 0)
1171                         return i; /* Found it; return index */
1172         }
1173         return -ENODATA;
1174 }
1175 EXPORT_SYMBOL_GPL(of_property_match_string);
1176
1177 /**
1178  * of_property_read_string_util() - Utility helper for parsing string properties
1179  * @np:         device node from which the property value is to be read.
1180  * @propname:   name of the property to be searched.
1181  * @out_strs:   output array of string pointers.
1182  * @sz:         number of array elements to read.
1183  * @skip:       Number of strings to skip over at beginning of list.
1184  *
1185  * Don't call this function directly. It is a utility helper for the
1186  * of_property_read_string*() family of functions.
1187  */
1188 int of_property_read_string_helper(struct device_node *np, const char *propname,
1189                                    const char **out_strs, size_t sz, int skip)
1190 {
1191         struct property *prop = of_find_property(np, propname, NULL);
1192         int l = 0, i = 0;
1193         const char *p, *end;
1194
1195         if (!prop)
1196                 return -EINVAL;
1197         if (!prop->value)
1198                 return -ENODATA;
1199         p = prop->value;
1200         end = p + prop->length;
1201
1202         for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1203                 l = strnlen(p, end - p) + 1;
1204                 if (p + l > end)
1205                         return -EILSEQ;
1206                 if (out_strs && i >= skip)
1207                         *out_strs++ = p;
1208         }
1209         i -= skip;
1210         return i <= 0 ? -ENODATA : i;
1211 }
1212 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
1213
1214 static int __of_parse_phandle_with_args(const struct device_node *np,
1215                                         const char *list_name,
1216                                         const char *cells_name,
1217                                         int cell_count, int index,
1218                                         struct of_phandle_args *out_args)
1219 {
1220         const __be32 *list, *list_end;
1221         int rc = 0, size, cur_index = 0;
1222         uint32_t count = 0;
1223         struct device_node *node = NULL;
1224         phandle phandle;
1225
1226         /* Retrieve the phandle list property */
1227         list = of_get_property(np, list_name, &size);
1228         if (!list)
1229                 return -ENOENT;
1230         list_end = list + size / sizeof(*list);
1231
1232         /* Loop over the phandles until all the requested entry is found */
1233         while (list < list_end) {
1234                 rc = -EINVAL;
1235                 count = 0;
1236
1237                 /*
1238                  * If phandle is 0, then it is an empty entry with no
1239                  * arguments.  Skip forward to the next entry.
1240                  */
1241                 phandle = be32_to_cpup(list++);
1242                 if (phandle) {
1243                         /*
1244                          * Find the provider node and parse the #*-cells
1245                          * property to determine the argument length.
1246                          *
1247                          * This is not needed if the cell count is hard-coded
1248                          * (i.e. cells_name not set, but cell_count is set),
1249                          * except when we're going to return the found node
1250                          * below.
1251                          */
1252                         if (cells_name || cur_index == index) {
1253                                 node = of_find_node_by_phandle(phandle);
1254                                 if (!node) {
1255                                         pr_err("%s: could not find phandle\n",
1256                                                 np->full_name);
1257                                         goto err;
1258                                 }
1259                         }
1260
1261                         if (cells_name) {
1262                                 if (of_property_read_u32(node, cells_name,
1263                                                          &count)) {
1264                                         pr_err("%s: could not get %s for %s\n",
1265                                                 np->full_name, cells_name,
1266                                                 node->full_name);
1267                                         goto err;
1268                                 }
1269                         } else {
1270                                 count = cell_count;
1271                         }
1272
1273                         /*
1274                          * Make sure that the arguments actually fit in the
1275                          * remaining property data length
1276                          */
1277                         if (list + count > list_end) {
1278                                 pr_err("%s: arguments longer than property\n",
1279                                          np->full_name);
1280                                 goto err;
1281                         }
1282                 }
1283
1284                 /*
1285                  * All of the error cases above bail out of the loop, so at
1286                  * this point, the parsing is successful. If the requested
1287                  * index matches, then fill the out_args structure and return,
1288                  * or return -ENOENT for an empty entry.
1289                  */
1290                 rc = -ENOENT;
1291                 if (cur_index == index) {
1292                         if (!phandle)
1293                                 goto err;
1294
1295                         if (out_args) {
1296                                 int i;
1297                                 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1298                                         count = MAX_PHANDLE_ARGS;
1299                                 out_args->np = node;
1300                                 out_args->args_count = count;
1301                                 for (i = 0; i < count; i++)
1302                                         out_args->args[i] = be32_to_cpup(list++);
1303                         } else {
1304                                 of_node_put(node);
1305                         }
1306
1307                         /* Found it! return success */
1308                         return 0;
1309                 }
1310
1311                 of_node_put(node);
1312                 node = NULL;
1313                 list += count;
1314                 cur_index++;
1315         }
1316
1317         /*
1318          * Unlock node before returning result; will be one of:
1319          * -ENOENT : index is for empty phandle
1320          * -EINVAL : parsing error on data
1321          * [1..n]  : Number of phandle (count mode; when index = -1)
1322          */
1323         rc = index < 0 ? cur_index : -ENOENT;
1324  err:
1325         if (node)
1326                 of_node_put(node);
1327         return rc;
1328 }
1329
1330 /**
1331  * of_parse_phandle - Resolve a phandle property to a device_node pointer
1332  * @np: Pointer to device node holding phandle property
1333  * @phandle_name: Name of property holding a phandle value
1334  * @index: For properties holding a table of phandles, this is the index into
1335  *         the table
1336  *
1337  * Returns the device_node pointer with refcount incremented.  Use
1338  * of_node_put() on it when done.
1339  */
1340 struct device_node *of_parse_phandle(const struct device_node *np,
1341                                      const char *phandle_name, int index)
1342 {
1343         struct of_phandle_args args;
1344
1345         if (index < 0)
1346                 return NULL;
1347
1348         if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1349                                          index, &args))
1350                 return NULL;
1351
1352         return args.np;
1353 }
1354 EXPORT_SYMBOL(of_parse_phandle);
1355
1356 /**
1357  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1358  * @np:         pointer to a device tree node containing a list
1359  * @list_name:  property name that contains a list
1360  * @cells_name: property name that specifies phandles' arguments count
1361  * @index:      index of a phandle to parse out
1362  * @out_args:   optional pointer to output arguments structure (will be filled)
1363  *
1364  * This function is useful to parse lists of phandles and their arguments.
1365  * Returns 0 on success and fills out_args, on error returns appropriate
1366  * errno value.
1367  *
1368  * Caller is responsible to call of_node_put() on the returned out_args->node
1369  * pointer.
1370  *
1371  * Example:
1372  *
1373  * phandle1: node1 {
1374  *      #list-cells = <2>;
1375  * }
1376  *
1377  * phandle2: node2 {
1378  *      #list-cells = <1>;
1379  * }
1380  *
1381  * node3 {
1382  *      list = <&phandle1 1 2 &phandle2 3>;
1383  * }
1384  *
1385  * To get a device_node of the `node2' node you may call this:
1386  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1387  */
1388 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1389                                 const char *cells_name, int index,
1390                                 struct of_phandle_args *out_args)
1391 {
1392         if (index < 0)
1393                 return -EINVAL;
1394         return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1395                                             index, out_args);
1396 }
1397 EXPORT_SYMBOL(of_parse_phandle_with_args);
1398
1399 /**
1400  * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1401  * @np:         pointer to a device tree node containing a list
1402  * @list_name:  property name that contains a list
1403  * @cell_count: number of argument cells following the phandle
1404  * @index:      index of a phandle to parse out
1405  * @out_args:   optional pointer to output arguments structure (will be filled)
1406  *
1407  * This function is useful to parse lists of phandles and their arguments.
1408  * Returns 0 on success and fills out_args, on error returns appropriate
1409  * errno value.
1410  *
1411  * Caller is responsible to call of_node_put() on the returned out_args->node
1412  * pointer.
1413  *
1414  * Example:
1415  *
1416  * phandle1: node1 {
1417  * }
1418  *
1419  * phandle2: node2 {
1420  * }
1421  *
1422  * node3 {
1423  *      list = <&phandle1 0 2 &phandle2 2 3>;
1424  * }
1425  *
1426  * To get a device_node of the `node2' node you may call this:
1427  * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1428  */
1429 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1430                                 const char *list_name, int cell_count,
1431                                 int index, struct of_phandle_args *out_args)
1432 {
1433         if (index < 0)
1434                 return -EINVAL;
1435         return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1436                                            index, out_args);
1437 }
1438 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1439
1440 /**
1441  * of_count_phandle_with_args() - Find the number of phandles references in a property
1442  * @np:         pointer to a device tree node containing a list
1443  * @list_name:  property name that contains a list
1444  * @cells_name: property name that specifies phandles' arguments count
1445  *
1446  * Returns the number of phandle + argument tuples within a property. It
1447  * is a typical pattern to encode a list of phandle and variable
1448  * arguments into a single property. The number of arguments is encoded
1449  * by a property in the phandle-target node. For example, a gpios
1450  * property would contain a list of GPIO specifies consisting of a
1451  * phandle and 1 or more arguments. The number of arguments are
1452  * determined by the #gpio-cells property in the node pointed to by the
1453  * phandle.
1454  */
1455 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1456                                 const char *cells_name)
1457 {
1458         return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1459                                             NULL);
1460 }
1461 EXPORT_SYMBOL(of_count_phandle_with_args);
1462
1463 /**
1464  * __of_add_property - Add a property to a node without lock operations
1465  */
1466 int __of_add_property(struct device_node *np, struct property *prop)
1467 {
1468         struct property **next;
1469
1470         prop->next = NULL;
1471         next = &np->properties;
1472         while (*next) {
1473                 if (strcmp(prop->name, (*next)->name) == 0)
1474                         /* duplicate ! don't insert it */
1475                         return -EEXIST;
1476
1477                 next = &(*next)->next;
1478         }
1479         *next = prop;
1480
1481         return 0;
1482 }
1483
1484 /**
1485  * of_add_property - Add a property to a node
1486  */
1487 int of_add_property(struct device_node *np, struct property *prop)
1488 {
1489         unsigned long flags;
1490         int rc;
1491
1492         mutex_lock(&of_mutex);
1493
1494         raw_spin_lock_irqsave(&devtree_lock, flags);
1495         rc = __of_add_property(np, prop);
1496         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1497
1498         if (!rc)
1499                 __of_add_property_sysfs(np, prop);
1500
1501         mutex_unlock(&of_mutex);
1502
1503         if (!rc)
1504                 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1505
1506         return rc;
1507 }
1508
1509 int __of_remove_property(struct device_node *np, struct property *prop)
1510 {
1511         struct property **next;
1512
1513         for (next = &np->properties; *next; next = &(*next)->next) {
1514                 if (*next == prop)
1515                         break;
1516         }
1517         if (*next == NULL)
1518                 return -ENODEV;
1519
1520         /* found the node */
1521         *next = prop->next;
1522         prop->next = np->deadprops;
1523         np->deadprops = prop;
1524
1525         return 0;
1526 }
1527
1528 void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1529 {
1530         /* at early boot, bail here and defer setup to of_init() */
1531         if (of_kset && of_node_is_attached(np))
1532                 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1533 }
1534
1535 /**
1536  * of_remove_property - Remove a property from a node.
1537  *
1538  * Note that we don't actually remove it, since we have given out
1539  * who-knows-how-many pointers to the data using get-property.
1540  * Instead we just move the property to the "dead properties"
1541  * list, so it won't be found any more.
1542  */
1543 int of_remove_property(struct device_node *np, struct property *prop)
1544 {
1545         unsigned long flags;
1546         int rc;
1547
1548         mutex_lock(&of_mutex);
1549
1550         raw_spin_lock_irqsave(&devtree_lock, flags);
1551         rc = __of_remove_property(np, prop);
1552         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1553
1554         if (!rc)
1555                 __of_remove_property_sysfs(np, prop);
1556
1557         mutex_unlock(&of_mutex);
1558
1559         if (!rc)
1560                 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1561
1562         return rc;
1563 }
1564
1565 int __of_update_property(struct device_node *np, struct property *newprop,
1566                 struct property **oldpropp)
1567 {
1568         struct property **next, *oldprop;
1569
1570         for (next = &np->properties; *next; next = &(*next)->next) {
1571                 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1572                         break;
1573         }
1574         *oldpropp = oldprop = *next;
1575
1576         if (oldprop) {
1577                 /* replace the node */
1578                 newprop->next = oldprop->next;
1579                 *next = newprop;
1580                 oldprop->next = np->deadprops;
1581                 np->deadprops = oldprop;
1582         } else {
1583                 /* new node */
1584                 newprop->next = NULL;
1585                 *next = newprop;
1586         }
1587
1588         return 0;
1589 }
1590
1591 void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1592                 struct property *oldprop)
1593 {
1594         /* At early boot, bail out and defer setup to of_init() */
1595         if (!of_kset)
1596                 return;
1597
1598         if (oldprop)
1599                 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1600         __of_add_property_sysfs(np, newprop);
1601 }
1602
1603 /*
1604  * of_update_property - Update a property in a node, if the property does
1605  * not exist, add it.
1606  *
1607  * Note that we don't actually remove it, since we have given out
1608  * who-knows-how-many pointers to the data using get-property.
1609  * Instead we just move the property to the "dead properties" list,
1610  * and add the new property to the property list
1611  */
1612 int of_update_property(struct device_node *np, struct property *newprop)
1613 {
1614         struct property *oldprop;
1615         unsigned long flags;
1616         int rc;
1617
1618         if (!newprop->name)
1619                 return -EINVAL;
1620
1621         mutex_lock(&of_mutex);
1622
1623         raw_spin_lock_irqsave(&devtree_lock, flags);
1624         rc = __of_update_property(np, newprop, &oldprop);
1625         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1626
1627         if (!rc)
1628                 __of_update_property_sysfs(np, newprop, oldprop);
1629
1630         mutex_unlock(&of_mutex);
1631
1632         if (!rc)
1633                 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1634
1635         return rc;
1636 }
1637
1638 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1639                          int id, const char *stem, int stem_len)
1640 {
1641         ap->np = np;
1642         ap->id = id;
1643         strncpy(ap->stem, stem, stem_len);
1644         ap->stem[stem_len] = 0;
1645         list_add_tail(&ap->link, &aliases_lookup);
1646         pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1647                  ap->alias, ap->stem, ap->id, of_node_full_name(np));
1648 }
1649
1650 /**
1651  * of_alias_scan - Scan all properties of 'aliases' node
1652  *
1653  * The function scans all the properties of 'aliases' node and populate
1654  * the the global lookup table with the properties.  It returns the
1655  * number of alias_prop found, or error code in error case.
1656  *
1657  * @dt_alloc:   An allocator that provides a virtual address to memory
1658  *              for the resulting tree
1659  */
1660 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1661 {
1662         struct property *pp;
1663
1664         of_chosen = of_find_node_by_path("/chosen");
1665         if (of_chosen == NULL)
1666                 of_chosen = of_find_node_by_path("/chosen@0");
1667
1668         if (of_chosen) {
1669                 const char *name;
1670
1671                 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1672                 if (name)
1673                         of_stdout = of_find_node_by_path(name);
1674         }
1675
1676         of_aliases = of_find_node_by_path("/aliases");
1677         if (!of_aliases)
1678                 return;
1679
1680         for_each_property_of_node(of_aliases, pp) {
1681                 const char *start = pp->name;
1682                 const char *end = start + strlen(start);
1683                 struct device_node *np;
1684                 struct alias_prop *ap;
1685                 int id, len;
1686
1687                 /* Skip those we do not want to proceed */
1688                 if (!strcmp(pp->name, "name") ||
1689                     !strcmp(pp->name, "phandle") ||
1690                     !strcmp(pp->name, "linux,phandle"))
1691                         continue;
1692
1693                 np = of_find_node_by_path(pp->value);
1694                 if (!np)
1695                         continue;
1696
1697                 /* walk the alias backwards to extract the id and work out
1698                  * the 'stem' string */
1699                 while (isdigit(*(end-1)) && end > start)
1700                         end--;
1701                 len = end - start;
1702
1703                 if (kstrtoint(end, 10, &id) < 0)
1704                         continue;
1705
1706                 /* Allocate an alias_prop with enough space for the stem */
1707                 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1708                 if (!ap)
1709                         continue;
1710                 memset(ap, 0, sizeof(*ap) + len + 1);
1711                 ap->alias = start;
1712                 of_alias_add(ap, np, id, start, len);
1713         }
1714 }
1715
1716 /**
1717  * of_alias_get_id - Get alias id for the given device_node
1718  * @np:         Pointer to the given device_node
1719  * @stem:       Alias stem of the given device_node
1720  *
1721  * The function travels the lookup table to get alias id for the given
1722  * device_node and alias stem.  It returns the alias id if find it.
1723  */
1724 int of_alias_get_id(struct device_node *np, const char *stem)
1725 {
1726         struct alias_prop *app;
1727         int id = -ENODEV;
1728
1729         mutex_lock(&of_mutex);
1730         list_for_each_entry(app, &aliases_lookup, link) {
1731                 if (strcmp(app->stem, stem) != 0)
1732                         continue;
1733
1734                 if (np == app->np) {
1735                         id = app->id;
1736                         break;
1737                 }
1738         }
1739         mutex_unlock(&of_mutex);
1740
1741         return id;
1742 }
1743 EXPORT_SYMBOL_GPL(of_alias_get_id);
1744
1745 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1746                                u32 *pu)
1747 {
1748         const void *curv = cur;
1749
1750         if (!prop)
1751                 return NULL;
1752
1753         if (!cur) {
1754                 curv = prop->value;
1755                 goto out_val;
1756         }
1757
1758         curv += sizeof(*cur);
1759         if (curv >= prop->value + prop->length)
1760                 return NULL;
1761
1762 out_val:
1763         *pu = be32_to_cpup(curv);
1764         return curv;
1765 }
1766 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1767
1768 const char *of_prop_next_string(struct property *prop, const char *cur)
1769 {
1770         const void *curv = cur;
1771
1772         if (!prop)
1773                 return NULL;
1774
1775         if (!cur)
1776                 return prop->value;
1777
1778         curv += strlen(cur) + 1;
1779         if (curv >= prop->value + prop->length)
1780                 return NULL;
1781
1782         return curv;
1783 }
1784 EXPORT_SYMBOL_GPL(of_prop_next_string);
1785
1786 /**
1787  * of_device_is_stdout_path - check if a device node matches the
1788  *                            linux,stdout-path property
1789  *
1790  * Check if this device node matches the linux,stdout-path property
1791  * in the chosen node. return true if yes, false otherwise.
1792  */
1793 int of_device_is_stdout_path(struct device_node *dn)
1794 {
1795         if (!of_stdout)
1796                 return false;
1797
1798         return of_stdout == dn;
1799 }
1800 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
1801
1802 /**
1803  * of_graph_parse_endpoint() - parse common endpoint node properties
1804  * @node: pointer to endpoint device_node
1805  * @endpoint: pointer to the OF endpoint data structure
1806  *
1807  * The caller should hold a reference to @node.
1808  */
1809 int of_graph_parse_endpoint(const struct device_node *node,
1810                             struct of_endpoint *endpoint)
1811 {
1812         struct device_node *port_node = of_get_parent(node);
1813
1814         WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
1815                   __func__, node->full_name);
1816
1817         memset(endpoint, 0, sizeof(*endpoint));
1818
1819         endpoint->local_node = node;
1820         /*
1821          * It doesn't matter whether the two calls below succeed.
1822          * If they don't then the default value 0 is used.
1823          */
1824         of_property_read_u32(port_node, "reg", &endpoint->port);
1825         of_property_read_u32(node, "reg", &endpoint->id);
1826
1827         of_node_put(port_node);
1828
1829         return 0;
1830 }
1831 EXPORT_SYMBOL(of_graph_parse_endpoint);
1832
1833 /**
1834  * of_graph_get_next_endpoint() - get next endpoint node
1835  * @parent: pointer to the parent device node
1836  * @prev: previous endpoint node, or NULL to get first
1837  *
1838  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
1839  * of the passed @prev node is not decremented, the caller have to use
1840  * of_node_put() on it when done.
1841  */
1842 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
1843                                         struct device_node *prev)
1844 {
1845         struct device_node *endpoint;
1846         struct device_node *port;
1847
1848         if (!parent)
1849                 return NULL;
1850
1851         /*
1852          * Start by locating the port node. If no previous endpoint is specified
1853          * search for the first port node, otherwise get the previous endpoint
1854          * parent port node.
1855          */
1856         if (!prev) {
1857                 struct device_node *node;
1858
1859                 node = of_get_child_by_name(parent, "ports");
1860                 if (node)
1861                         parent = node;
1862
1863                 port = of_get_child_by_name(parent, "port");
1864                 of_node_put(node);
1865
1866                 if (!port) {
1867                         pr_err("%s(): no port node found in %s\n",
1868                                __func__, parent->full_name);
1869                         return NULL;
1870                 }
1871         } else {
1872                 port = of_get_parent(prev);
1873                 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
1874                               __func__, prev->full_name))
1875                         return NULL;
1876
1877                 /*
1878                  * Avoid dropping prev node refcount to 0 when getting the next
1879                  * child below.
1880                  */
1881                 of_node_get(prev);
1882         }
1883
1884         while (1) {
1885                 /*
1886                  * Now that we have a port node, get the next endpoint by
1887                  * getting the next child. If the previous endpoint is NULL this
1888                  * will return the first child.
1889                  */
1890                 endpoint = of_get_next_child(port, prev);
1891                 if (endpoint) {
1892                         of_node_put(port);
1893                         return endpoint;
1894                 }
1895
1896                 /* No more endpoints under this port, try the next one. */
1897                 prev = NULL;
1898
1899                 do {
1900                         port = of_get_next_child(parent, port);
1901                         if (!port)
1902                                 return NULL;
1903                 } while (of_node_cmp(port->name, "port"));
1904         }
1905 }
1906 EXPORT_SYMBOL(of_graph_get_next_endpoint);
1907
1908 /**
1909  * of_graph_get_remote_port_parent() - get remote port's parent node
1910  * @node: pointer to a local endpoint device_node
1911  *
1912  * Return: Remote device node associated with remote endpoint node linked
1913  *         to @node. Use of_node_put() on it when done.
1914  */
1915 struct device_node *of_graph_get_remote_port_parent(
1916                                const struct device_node *node)
1917 {
1918         struct device_node *np;
1919         unsigned int depth;
1920
1921         /* Get remote endpoint node. */
1922         np = of_parse_phandle(node, "remote-endpoint", 0);
1923
1924         /* Walk 3 levels up only if there is 'ports' node. */
1925         for (depth = 3; depth && np; depth--) {
1926                 np = of_get_next_parent(np);
1927                 if (depth == 2 && of_node_cmp(np->name, "ports"))
1928                         break;
1929         }
1930         return np;
1931 }
1932 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
1933
1934 /**
1935  * of_graph_get_remote_port() - get remote port node
1936  * @node: pointer to a local endpoint device_node
1937  *
1938  * Return: Remote port node associated with remote endpoint node linked
1939  *         to @node. Use of_node_put() on it when done.
1940  */
1941 struct device_node *of_graph_get_remote_port(const struct device_node *node)
1942 {
1943         struct device_node *np;
1944
1945         /* Get remote endpoint node. */
1946         np = of_parse_phandle(node, "remote-endpoint", 0);
1947         if (!np)
1948                 return NULL;
1949         return of_get_next_parent(np);
1950 }
1951 EXPORT_SYMBOL(of_graph_get_remote_port);