blob: be2861d69b025fa0bf0e44f94c5211d10d831232 [file] [log] [blame]
Stephen Rothwell97e873e2007-05-01 16:26:07 +10001/*
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 *
Grant Likelye91edcf2009-10-15 10:58:09 -060012 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
Stephen Rothwell97e873e2007-05-01 16:26:07 +100014 *
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 */
Shawn Guo611cad72011-08-15 15:28:14 +080020#include <linux/ctype.h>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010021#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100022#include <linux/module.h>
23#include <linux/of.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100024#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000026#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070027#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100028
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080029#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080030
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080031LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080032
Randy Dunlap465aac62012-11-30 10:01:51 +000033struct device_node *of_allnodes;
34EXPORT_SYMBOL(of_allnodes);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070035struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080036struct device_node *of_aliases;
Sascha Hauer5c19e952013-08-05 14:40:44 +020037static struct device_node *of_stdout;
Shawn Guo611cad72011-08-15 15:28:14 +080038
Grant Likely75b57ec2014-02-20 18:02:11 +000039static struct kset *of_kset;
40
41/*
42 * Used to protect the of_aliases; but also overloaded to hold off addition of
43 * nodes to sysfs
44 */
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080045DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100046
Stephen Rothwell581b6052007-04-24 16:46:53 +100047/* use when traversing tree through the allnext, child, sibling,
48 * or parent members of struct device_node.
49 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050050DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100051
52int of_n_addr_cells(struct device_node *np)
53{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060054 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100055
56 do {
57 if (np->parent)
58 np = np->parent;
59 ip = of_get_property(np, "#address-cells", NULL);
60 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070061 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100062 } while (np->parent);
63 /* No #address-cells property for the root node */
64 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
65}
66EXPORT_SYMBOL(of_n_addr_cells);
67
68int of_n_size_cells(struct device_node *np)
69{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060070 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100071
72 do {
73 if (np->parent)
74 np = np->parent;
75 ip = of_get_property(np, "#size-cells", NULL);
76 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070077 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100078 } while (np->parent);
79 /* No #size-cells property for the root node */
80 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
81}
82EXPORT_SYMBOL(of_n_size_cells);
83
Rob Herring0c3f0612013-09-17 10:42:50 -050084#ifdef CONFIG_NUMA
85int __weak of_node_to_nid(struct device_node *np)
86{
87 return numa_node_id();
88}
89#endif
90
Grant Likely0f22dd32012-02-15 20:38:40 -070091#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070092/**
93 * of_node_get - Increment refcount of a node
94 * @node: Node to inc refcount, NULL is supported to
95 * simplify writing of callers
96 *
97 * Returns node.
98 */
99struct device_node *of_node_get(struct device_node *node)
100{
101 if (node)
Grant Likely75b57ec2014-02-20 18:02:11 +0000102 kobject_get(&node->kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700103 return node;
104}
105EXPORT_SYMBOL(of_node_get);
106
Grant Likely75b57ec2014-02-20 18:02:11 +0000107static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -0700108{
Grant Likely75b57ec2014-02-20 18:02:11 +0000109 return container_of(kobj, struct device_node, kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700110}
111
112/**
113 * of_node_release - release a dynamically allocated node
114 * @kref: kref element of the node to be released
115 *
116 * In of_node_put() this function is passed to kref_put()
117 * as the destructor.
118 */
Grant Likely75b57ec2014-02-20 18:02:11 +0000119static void of_node_release(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -0700120{
Grant Likely75b57ec2014-02-20 18:02:11 +0000121 struct device_node *node = kobj_to_device_node(kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700122 struct property *prop = node->properties;
123
124 /* We should never be releasing nodes that haven't been detached. */
125 if (!of_node_check_flag(node, OF_DETACHED)) {
126 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
127 dump_stack();
Grant Likely923f7e32010-01-28 13:52:53 -0700128 return;
129 }
130
131 if (!of_node_check_flag(node, OF_DYNAMIC))
132 return;
133
134 while (prop) {
135 struct property *next = prop->next;
136 kfree(prop->name);
137 kfree(prop->value);
138 kfree(prop);
139 prop = next;
140
141 if (!prop) {
142 prop = node->deadprops;
143 node->deadprops = NULL;
144 }
145 }
146 kfree(node->full_name);
147 kfree(node->data);
148 kfree(node);
149}
150
151/**
152 * of_node_put - Decrement refcount of a node
153 * @node: Node to dec refcount, NULL is supported to
154 * simplify writing of callers
155 *
156 */
157void of_node_put(struct device_node *node)
158{
159 if (node)
Grant Likely75b57ec2014-02-20 18:02:11 +0000160 kobject_put(&node->kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700161}
162EXPORT_SYMBOL(of_node_put);
Grant Likely75b57ec2014-02-20 18:02:11 +0000163#else
164static void of_node_release(struct kobject *kobj)
165{
166 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
167}
Grant Likely0f22dd32012-02-15 20:38:40 -0700168#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700169
Grant Likely75b57ec2014-02-20 18:02:11 +0000170struct kobj_type of_node_ktype = {
171 .release = of_node_release,
172};
173
174static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
175 struct bin_attribute *bin_attr, char *buf,
176 loff_t offset, size_t count)
177{
178 struct property *pp = container_of(bin_attr, struct property, attr);
179 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
180}
181
182static const char *safe_name(struct kobject *kobj, const char *orig_name)
183{
184 const char *name = orig_name;
185 struct kernfs_node *kn;
186 int i = 0;
187
188 /* don't be a hero. After 16 tries give up */
189 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
190 sysfs_put(kn);
191 if (name != orig_name)
192 kfree(name);
193 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
194 }
195
196 if (name != orig_name)
197 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
198 kobject_name(kobj), name);
199 return name;
200}
201
202static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
203{
204 int rc;
205
206 /* Important: Don't leak passwords */
207 bool secure = strncmp(pp->name, "security-", 9) == 0;
208
209 sysfs_bin_attr_init(&pp->attr);
210 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
211 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
212 pp->attr.size = secure ? 0 : pp->length;
213 pp->attr.read = of_node_property_read;
214
215 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
216 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
217 return rc;
218}
219
220static int __of_node_add(struct device_node *np)
221{
222 const char *name;
223 struct property *pp;
224 int rc;
225
226 np->kobj.kset = of_kset;
227 if (!np->parent) {
228 /* Nodes without parents are new top level trees */
229 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
230 } else {
231 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
232 if (!name || !name[0])
233 return -EINVAL;
234
235 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
236 }
237 if (rc)
238 return rc;
239
240 for_each_property_of_node(np, pp)
241 __of_add_property_sysfs(np, pp);
242
243 return 0;
244}
245
246int of_node_add(struct device_node *np)
247{
248 int rc = 0;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200249
250 BUG_ON(!of_node_is_initialized(np));
251
252 /*
253 * Grab the mutex here so that in a race condition between of_init() and
254 * of_node_add(), node addition will still be consistent.
255 */
Grant Likely75b57ec2014-02-20 18:02:11 +0000256 mutex_lock(&of_aliases_mutex);
257 if (of_kset)
258 rc = __of_node_add(np);
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200259 else
260 /* This scenario may be perfectly valid, but report it anyway */
261 pr_info("of_node_add(%s) before of_init()\n", np->full_name);
Grant Likely75b57ec2014-02-20 18:02:11 +0000262 mutex_unlock(&of_aliases_mutex);
263 return rc;
264}
265
266#if defined(CONFIG_OF_DYNAMIC)
267static void of_node_remove(struct device_node *np)
268{
269 struct property *pp;
270
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200271 BUG_ON(!of_node_is_initialized(np));
Grant Likely75b57ec2014-02-20 18:02:11 +0000272
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200273 /* only remove properties if on sysfs */
274 if (of_node_is_attached(np)) {
275 for_each_property_of_node(np, pp)
276 sysfs_remove_bin_file(&np->kobj, &pp->attr);
277 kobject_del(&np->kobj);
278 }
279
280 /* finally remove the kobj_init ref */
281 of_node_put(np);
Grant Likely75b57ec2014-02-20 18:02:11 +0000282}
283#endif
284
285static int __init of_init(void)
286{
287 struct device_node *np;
288
289 /* Create the kset, and register existing nodes */
290 mutex_lock(&of_aliases_mutex);
291 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
292 if (!of_kset) {
293 mutex_unlock(&of_aliases_mutex);
294 return -ENOMEM;
295 }
296 for_each_of_allnodes(np)
297 __of_node_add(np);
298 mutex_unlock(&of_aliases_mutex);
299
Grant Likely83570412012-11-06 21:03:27 +0000300 /* Symlink in /proc as required by userspace ABI */
Grant Likely75b57ec2014-02-20 18:02:11 +0000301 if (of_allnodes)
302 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000303
304 return 0;
305}
306core_initcall(of_init);
307
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500308static struct property *__of_find_property(const struct device_node *np,
309 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000310{
311 struct property *pp;
312
Timur Tabi64e45662008-05-08 05:19:59 +1000313 if (!np)
314 return NULL;
315
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530316 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000317 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530318 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000319 *lenp = pp->length;
320 break;
321 }
322 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500323
324 return pp;
325}
326
327struct property *of_find_property(const struct device_node *np,
328 const char *name,
329 int *lenp)
330{
331 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500332 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500333
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500334 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500335 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500336 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000337
338 return pp;
339}
340EXPORT_SYMBOL(of_find_property);
341
Grant Likelye91edcf2009-10-15 10:58:09 -0600342/**
343 * of_find_all_nodes - Get next node in global list
344 * @prev: Previous node or NULL to start iteration
345 * of_node_put() will be called on it
346 *
347 * Returns a node pointer with refcount incremented, use
348 * of_node_put() on it when done.
349 */
350struct device_node *of_find_all_nodes(struct device_node *prev)
351{
352 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000353 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600354
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000355 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000356 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600357 for (; np != NULL; np = np->allnext)
358 if (of_node_get(np))
359 break;
360 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000361 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600362 return np;
363}
364EXPORT_SYMBOL(of_find_all_nodes);
365
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000366/*
367 * Find a property with a given name for a given node
368 * and return the value.
369 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500370static const void *__of_get_property(const struct device_node *np,
371 const char *name, int *lenp)
372{
373 struct property *pp = __of_find_property(np, name, lenp);
374
375 return pp ? pp->value : NULL;
376}
377
378/*
379 * Find a property with a given name for a given node
380 * and return the value.
381 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000382const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500383 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000384{
385 struct property *pp = of_find_property(np, name, lenp);
386
387 return pp ? pp->value : NULL;
388}
389EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000390
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100391/*
392 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
393 *
394 * @cpu: logical cpu index of a core/thread
395 * @phys_id: physical identifier of a core/thread
396 *
397 * CPU logical to physical index mapping is architecture specific.
398 * However this __weak function provides a default match of physical
399 * id to logical cpu index. phys_id provided here is usually values read
400 * from the device tree which must match the hardware internal registers.
401 *
402 * Returns true if the physical identifier and the logical cpu index
403 * correspond to the same core/thread, false otherwise.
404 */
405bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
406{
407 return (u32)phys_id == cpu;
408}
409
410/**
411 * Checks if the given "prop_name" property holds the physical id of the
412 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
413 * NULL, local thread number within the core is returned in it.
414 */
415static bool __of_find_n_match_cpu_property(struct device_node *cpun,
416 const char *prop_name, int cpu, unsigned int *thread)
417{
418 const __be32 *cell;
419 int ac, prop_len, tid;
420 u64 hwid;
421
422 ac = of_n_addr_cells(cpun);
423 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100424 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100425 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100426 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100427 for (tid = 0; tid < prop_len; tid++) {
428 hwid = of_read_number(cell, ac);
429 if (arch_match_cpu_phys_id(cpu, hwid)) {
430 if (thread)
431 *thread = tid;
432 return true;
433 }
434 cell += ac;
435 }
436 return false;
437}
438
David Millerd1cb9d12013-10-03 17:24:51 -0400439/*
440 * arch_find_n_match_cpu_physical_id - See if the given device node is
441 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
442 * else false. If 'thread' is non-NULL, the local thread number within the
443 * core is returned in it.
444 */
445bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
446 int cpu, unsigned int *thread)
447{
448 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
449 * for thread ids on PowerPC. If it doesn't exist fallback to
450 * standard "reg" property.
451 */
452 if (IS_ENABLED(CONFIG_PPC) &&
453 __of_find_n_match_cpu_property(cpun,
454 "ibm,ppc-interrupt-server#s",
455 cpu, thread))
456 return true;
457
458 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
459 return true;
460
461 return false;
462}
463
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100464/**
465 * of_get_cpu_node - Get device node associated with the given logical CPU
466 *
467 * @cpu: CPU number(logical index) for which device node is required
468 * @thread: if not NULL, local thread number within the physical core is
469 * returned
470 *
471 * The main purpose of this function is to retrieve the device node for the
472 * given logical CPU index. It should be used to initialize the of_node in
473 * cpu device. Once of_node in cpu device is populated, all the further
474 * references can use that instead.
475 *
476 * CPU logical to physical index mapping is architecture specific and is built
477 * before booting secondary cores. This function uses arch_match_cpu_phys_id
478 * which can be overridden by architecture specific implementation.
479 *
480 * Returns a node pointer for the logical cpu if found, else NULL.
481 */
482struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
483{
David Millerd1cb9d12013-10-03 17:24:51 -0400484 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100485
David Millerd1cb9d12013-10-03 17:24:51 -0400486 for_each_node_by_type(cpun, "cpu") {
487 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100488 return cpun;
489 }
490 return NULL;
491}
492EXPORT_SYMBOL(of_get_cpu_node);
493
Kevin Hao215a14c2014-02-19 16:15:45 +0800494/**
495 * __of_device_is_compatible() - Check if the node matches given constraints
496 * @device: pointer to node
497 * @compat: required compatible string, NULL or "" for any match
498 * @type: required device_type value, NULL or "" for any match
499 * @name: required node name, NULL or "" for any match
500 *
501 * Checks if the given @compat, @type and @name strings match the
502 * properties of the given @device. A constraints can be skipped by
503 * passing NULL or an empty string as the constraint.
504 *
505 * Returns 0 for no match, and a positive integer on match. The return
506 * value is a relative score with larger values indicating better
507 * matches. The score is weighted for the most specific compatible value
508 * to get the highest score. Matching type is next, followed by matching
509 * name. Practically speaking, this results in the following priority
510 * order for matches:
511 *
512 * 1. specific compatible && type && name
513 * 2. specific compatible && type
514 * 3. specific compatible && name
515 * 4. specific compatible
516 * 5. general compatible && type && name
517 * 6. general compatible && type
518 * 7. general compatible && name
519 * 8. general compatible
520 * 9. type && name
521 * 10. type
522 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000523 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500524static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800525 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000526{
Kevin Hao215a14c2014-02-19 16:15:45 +0800527 struct property *prop;
528 const char *cp;
529 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000530
Kevin Hao215a14c2014-02-19 16:15:45 +0800531 /* Compatible match has highest priority */
532 if (compat && compat[0]) {
533 prop = __of_find_property(device, "compatible", NULL);
534 for (cp = of_prop_next_string(prop, NULL); cp;
535 cp = of_prop_next_string(prop, cp), index++) {
536 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
537 score = INT_MAX/2 - (index << 2);
538 break;
539 }
540 }
541 if (!score)
542 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000543 }
544
Kevin Hao215a14c2014-02-19 16:15:45 +0800545 /* Matching type is better than matching name */
546 if (type && type[0]) {
547 if (!device->type || of_node_cmp(type, device->type))
548 return 0;
549 score += 2;
550 }
551
552 /* Matching name is a bit better than not */
553 if (name && name[0]) {
554 if (!device->name || of_node_cmp(name, device->name))
555 return 0;
556 score++;
557 }
558
559 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000560}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500561
562/** Checks if the given "compat" string matches one of the strings in
563 * the device's "compatible" property
564 */
565int of_device_is_compatible(const struct device_node *device,
566 const char *compat)
567{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500568 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500569 int res;
570
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500571 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800572 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500573 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500574 return res;
575}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000576EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000577
578/**
Grant Likely71a157e2010-02-01 21:34:14 -0700579 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700580 * @compat: compatible string to look for in root node's compatible property.
581 *
582 * Returns true if the root node has the given value in its
583 * compatible property.
584 */
Grant Likely71a157e2010-02-01 21:34:14 -0700585int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700586{
587 struct device_node *root;
588 int rc = 0;
589
590 root = of_find_node_by_path("/");
591 if (root) {
592 rc = of_device_is_compatible(root, compat);
593 of_node_put(root);
594 }
595 return rc;
596}
Grant Likely71a157e2010-02-01 21:34:14 -0700597EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700598
599/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700600 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100601 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700602 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100603 *
604 * Returns 1 if the status property is absent or set to "okay" or "ok",
605 * 0 otherwise
606 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700607static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100608{
609 const char *status;
610 int statlen;
611
Xiubo Li42ccd782014-01-13 11:07:28 +0800612 if (!device)
613 return 0;
614
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700615 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100616 if (status == NULL)
617 return 1;
618
619 if (statlen > 0) {
620 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
621 return 1;
622 }
623
624 return 0;
625}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700626
627/**
628 * of_device_is_available - check if a device is available for use
629 *
630 * @device: Node to check for availability
631 *
632 * Returns 1 if the status property is absent or set to "okay" or "ok",
633 * 0 otherwise
634 */
635int of_device_is_available(const struct device_node *device)
636{
637 unsigned long flags;
638 int res;
639
640 raw_spin_lock_irqsave(&devtree_lock, flags);
641 res = __of_device_is_available(device);
642 raw_spin_unlock_irqrestore(&devtree_lock, flags);
643 return res;
644
645}
Josh Boyer834d97d2008-03-27 00:33:14 +1100646EXPORT_SYMBOL(of_device_is_available);
647
648/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000649 * of_get_parent - Get a node's parent if any
650 * @node: Node to get parent
651 *
652 * Returns a node pointer with refcount incremented, use
653 * of_node_put() on it when done.
654 */
655struct device_node *of_get_parent(const struct device_node *node)
656{
657 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500658 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000659
660 if (!node)
661 return NULL;
662
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500663 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000664 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500665 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000666 return np;
667}
668EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000669
670/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000671 * of_get_next_parent - Iterate to a node's parent
672 * @node: Node to get parent of
673 *
674 * This is like of_get_parent() except that it drops the
675 * refcount on the passed node, making it suitable for iterating
676 * through a node's parents.
677 *
678 * Returns a node pointer with refcount incremented, use
679 * of_node_put() on it when done.
680 */
681struct device_node *of_get_next_parent(struct device_node *node)
682{
683 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500684 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000685
686 if (!node)
687 return NULL;
688
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500689 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000690 parent = of_node_get(node->parent);
691 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500692 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000693 return parent;
694}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300695EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000696
697/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000698 * of_get_next_child - Iterate a node childs
699 * @node: parent node
700 * @prev: previous child of the parent node, or NULL to get first
701 *
702 * Returns a node pointer with refcount incremented, use
703 * of_node_put() on it when done.
704 */
705struct device_node *of_get_next_child(const struct device_node *node,
706 struct device_node *prev)
707{
708 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500709 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000710
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500711 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000712 next = prev ? prev->sibling : node->child;
713 for (; next; next = next->sibling)
714 if (of_node_get(next))
715 break;
716 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500717 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000718 return next;
719}
720EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000721
722/**
Timur Tabi32961932012-08-14 13:20:23 +0000723 * of_get_next_available_child - Find the next available child node
724 * @node: parent node
725 * @prev: previous child of the parent node, or NULL to get first
726 *
727 * This function is like of_get_next_child(), except that it
728 * automatically skips any disabled nodes (i.e. status = "disabled").
729 */
730struct device_node *of_get_next_available_child(const struct device_node *node,
731 struct device_node *prev)
732{
733 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000734 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000735
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000736 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000737 next = prev ? prev->sibling : node->child;
738 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700739 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000740 continue;
741 if (of_node_get(next))
742 break;
743 }
744 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000745 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000746 return next;
747}
748EXPORT_SYMBOL(of_get_next_available_child);
749
750/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100751 * of_get_child_by_name - Find the child node by name for a given parent
752 * @node: parent node
753 * @name: child name to look for.
754 *
755 * This function looks for child node for given matching name
756 *
757 * Returns a node pointer if found, with refcount incremented, use
758 * of_node_put() on it when done.
759 * Returns NULL if node is not found.
760 */
761struct device_node *of_get_child_by_name(const struct device_node *node,
762 const char *name)
763{
764 struct device_node *child;
765
766 for_each_child_of_node(node, child)
767 if (child->name && (of_node_cmp(child->name, name) == 0))
768 break;
769 return child;
770}
771EXPORT_SYMBOL(of_get_child_by_name);
772
773/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000774 * of_find_node_by_path - Find a node matching a full OF path
775 * @path: The full path to match
776 *
777 * Returns a node pointer with refcount incremented, use
778 * of_node_put() on it when done.
779 */
780struct device_node *of_find_node_by_path(const char *path)
781{
Randy Dunlap465aac62012-11-30 10:01:51 +0000782 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500783 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000784
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500785 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000786 for (; np; np = np->allnext) {
787 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
788 && of_node_get(np))
789 break;
790 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500791 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000792 return np;
793}
794EXPORT_SYMBOL(of_find_node_by_path);
795
796/**
797 * of_find_node_by_name - Find a node by its "name" property
798 * @from: The node to start searching from or NULL, the node
799 * you pass will not be searched, only the next one
800 * will; typically, you pass what the previous call
801 * returned. of_node_put() will be called on it
802 * @name: The name string to match against
803 *
804 * Returns a node pointer with refcount incremented, use
805 * of_node_put() on it when done.
806 */
807struct device_node *of_find_node_by_name(struct device_node *from,
808 const char *name)
809{
810 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500811 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000812
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500813 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000814 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000815 for (; np; np = np->allnext)
816 if (np->name && (of_node_cmp(np->name, name) == 0)
817 && of_node_get(np))
818 break;
819 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500820 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000821 return np;
822}
823EXPORT_SYMBOL(of_find_node_by_name);
824
825/**
826 * of_find_node_by_type - Find a node by its "device_type" property
827 * @from: The node to start searching from, or NULL to start searching
828 * the entire device tree. The node you pass will not be
829 * searched, only the next one will; typically, you pass
830 * what the previous call returned. of_node_put() will be
831 * called on from for you.
832 * @type: The type string to match against
833 *
834 * Returns a node pointer with refcount incremented, use
835 * of_node_put() on it when done.
836 */
837struct device_node *of_find_node_by_type(struct device_node *from,
838 const char *type)
839{
840 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500841 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000842
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500843 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000844 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000845 for (; np; np = np->allnext)
846 if (np->type && (of_node_cmp(np->type, type) == 0)
847 && of_node_get(np))
848 break;
849 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500850 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000851 return np;
852}
853EXPORT_SYMBOL(of_find_node_by_type);
854
855/**
856 * of_find_compatible_node - Find a node based on type and one of the
857 * tokens in its "compatible" property
858 * @from: The node to start searching from or NULL, the node
859 * you pass will not be searched, only the next one
860 * will; typically, you pass what the previous call
861 * returned. of_node_put() will be called on it
862 * @type: The type string to match "device_type" or NULL to ignore
863 * @compatible: The string to match to one of the tokens in the device
864 * "compatible" list.
865 *
866 * Returns a node pointer with refcount incremented, use
867 * of_node_put() on it when done.
868 */
869struct device_node *of_find_compatible_node(struct device_node *from,
870 const char *type, const char *compatible)
871{
872 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500873 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000874
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500875 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000876 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000877 for (; np; np = np->allnext) {
Kevin Hao215a14c2014-02-19 16:15:45 +0800878 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500879 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000880 break;
881 }
882 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500883 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000884 return np;
885}
886EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100887
888/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000889 * of_find_node_with_property - Find a node which has a property with
890 * the given name.
891 * @from: The node to start searching from or NULL, the node
892 * you pass will not be searched, only the next one
893 * will; typically, you pass what the previous call
894 * returned. of_node_put() will be called on it
895 * @prop_name: The name of the property to look for.
896 *
897 * Returns a node pointer with refcount incremented, use
898 * of_node_put() on it when done.
899 */
900struct device_node *of_find_node_with_property(struct device_node *from,
901 const char *prop_name)
902{
903 struct device_node *np;
904 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500905 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000906
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500907 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000908 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000909 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530910 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000911 if (of_prop_cmp(pp->name, prop_name) == 0) {
912 of_node_get(np);
913 goto out;
914 }
915 }
916 }
917out:
918 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500919 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000920 return np;
921}
922EXPORT_SYMBOL(of_find_node_with_property);
923
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500924static
925const struct of_device_id *__of_match_node(const struct of_device_id *matches,
926 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100927{
Kevin Hao215a14c2014-02-19 16:15:45 +0800928 const struct of_device_id *best_match = NULL;
929 int score, best_score = 0;
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100930
Grant Likelya52f07e2011-03-18 10:21:29 -0600931 if (!matches)
932 return NULL;
933
Kevin Hao215a14c2014-02-19 16:15:45 +0800934 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
935 score = __of_device_is_compatible(node, matches->compatible,
936 matches->type, matches->name);
937 if (score > best_score) {
938 best_match = matches;
939 best_score = score;
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100940 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +0800941 }
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100942
Kevin Hao215a14c2014-02-19 16:15:45 +0800943 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +1100944}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500945
946/**
947 * of_match_node - Tell if an device_node has a matching of_match structure
948 * @matches: array of of device match structures to search in
949 * @node: the of device structure to match against
950 *
Kevin Hao71c54982014-02-18 15:57:29 +0800951 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500952 */
953const struct of_device_id *of_match_node(const struct of_device_id *matches,
954 const struct device_node *node)
955{
956 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500957 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500958
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500959 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500960 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500961 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500962 return match;
963}
Grant Likely283029d2008-01-09 06:20:40 +1100964EXPORT_SYMBOL(of_match_node);
965
966/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700967 * of_find_matching_node_and_match - Find a node based on an of_device_id
968 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100969 * @from: The node to start searching from or NULL, the node
970 * you pass will not be searched, only the next one
971 * will; typically, you pass what the previous call
972 * returned. of_node_put() will be called on it
973 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700974 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100975 *
976 * Returns a node pointer with refcount incremented, use
977 * of_node_put() on it when done.
978 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700979struct device_node *of_find_matching_node_and_match(struct device_node *from,
980 const struct of_device_id *matches,
981 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100982{
983 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800984 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500985 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100986
Stephen Warren50c8af42012-11-20 16:12:20 -0700987 if (match)
988 *match = NULL;
989
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500990 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000991 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100992 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500993 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800994 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700995 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800996 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100997 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700998 }
Grant Likely283029d2008-01-09 06:20:40 +1100999 }
1000 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001001 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001002 return np;
1003}
Grant Likely80c20222012-12-19 10:45:36 +00001004EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001005
1006/**
Grant Likely3f07af42008-07-25 22:25:13 -04001007 * of_modalias_node - Lookup appropriate modalias for a device node
1008 * @node: pointer to a device tree node
1009 * @modalias: Pointer to buffer that modalias value will be copied into
1010 * @len: Length of modalias value
1011 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001012 * Based on the value of the compatible property, this routine will attempt
1013 * to choose an appropriate modalias value for a particular device tree node.
1014 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1015 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001016 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001017 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001018 */
1019int of_modalias_node(struct device_node *node, char *modalias, int len)
1020{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001021 const char *compatible, *p;
1022 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001023
1024 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001025 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001026 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001027 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001028 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001029 return 0;
1030}
1031EXPORT_SYMBOL_GPL(of_modalias_node);
1032
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001033/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001034 * of_find_node_by_phandle - Find a node given a phandle
1035 * @handle: phandle of the node to find
1036 *
1037 * Returns a node pointer with refcount incremented, use
1038 * of_node_put() on it when done.
1039 */
1040struct device_node *of_find_node_by_phandle(phandle handle)
1041{
1042 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001043 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001044
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001045 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001046 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001047 if (np->phandle == handle)
1048 break;
1049 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001050 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001051 return np;
1052}
1053EXPORT_SYMBOL(of_find_node_by_phandle);
1054
1055/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001056 * of_find_property_value_of_size
1057 *
1058 * @np: device node from which the property value is to be read.
1059 * @propname: name of the property to be searched.
1060 * @len: requested length of property value
1061 *
1062 * Search for a property in a device node and valid the requested size.
1063 * Returns the property value on success, -EINVAL if the property does not
1064 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1065 * property data isn't large enough.
1066 *
1067 */
1068static void *of_find_property_value_of_size(const struct device_node *np,
1069 const char *propname, u32 len)
1070{
1071 struct property *prop = of_find_property(np, propname, NULL);
1072
1073 if (!prop)
1074 return ERR_PTR(-EINVAL);
1075 if (!prop->value)
1076 return ERR_PTR(-ENODATA);
1077 if (len > prop->length)
1078 return ERR_PTR(-EOVERFLOW);
1079
1080 return prop->value;
1081}
1082
1083/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001084 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1085 *
1086 * @np: device node from which the property value is to be read.
1087 * @propname: name of the property to be searched.
1088 * @index: index of the u32 in the list of values
1089 * @out_value: pointer to return value, modified only if no error.
1090 *
1091 * Search for a property in a device node and read nth 32-bit value from
1092 * it. Returns 0 on success, -EINVAL if the property does not exist,
1093 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1094 * property data isn't large enough.
1095 *
1096 * The out_value is modified only if a valid u32 value can be decoded.
1097 */
1098int of_property_read_u32_index(const struct device_node *np,
1099 const char *propname,
1100 u32 index, u32 *out_value)
1101{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001102 const u32 *val = of_find_property_value_of_size(np, propname,
1103 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +13001104
Tony Priskdaeec1f2013-04-03 17:57:11 +13001105 if (IS_ERR(val))
1106 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001107
Tony Priskdaeec1f2013-04-03 17:57:11 +13001108 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001109 return 0;
1110}
1111EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1112
1113/**
Viresh Kumarbe193242012-11-20 10:15:19 +05301114 * of_property_read_u8_array - Find and read an array of u8 from a property.
1115 *
1116 * @np: device node from which the property value is to be read.
1117 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301118 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301119 * @sz: number of array elements to read
1120 *
1121 * Search for a property in a device node and read 8-bit value(s) from
1122 * it. Returns 0 on success, -EINVAL if the property does not exist,
1123 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1124 * property data isn't large enough.
1125 *
1126 * dts entry of array should be like:
1127 * property = /bits/ 8 <0x50 0x60 0x70>;
1128 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301129 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301130 */
1131int of_property_read_u8_array(const struct device_node *np,
1132 const char *propname, u8 *out_values, size_t sz)
1133{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001134 const u8 *val = of_find_property_value_of_size(np, propname,
1135 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301136
Tony Priskdaeec1f2013-04-03 17:57:11 +13001137 if (IS_ERR(val))
1138 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301139
Viresh Kumarbe193242012-11-20 10:15:19 +05301140 while (sz--)
1141 *out_values++ = *val++;
1142 return 0;
1143}
1144EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1145
1146/**
1147 * of_property_read_u16_array - Find and read an array of u16 from a property.
1148 *
1149 * @np: device node from which the property value is to be read.
1150 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301151 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301152 * @sz: number of array elements to read
1153 *
1154 * Search for a property in a device node and read 16-bit value(s) from
1155 * it. Returns 0 on success, -EINVAL if the property does not exist,
1156 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1157 * property data isn't large enough.
1158 *
1159 * dts entry of array should be like:
1160 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1161 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301162 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301163 */
1164int of_property_read_u16_array(const struct device_node *np,
1165 const char *propname, u16 *out_values, size_t sz)
1166{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001167 const __be16 *val = of_find_property_value_of_size(np, propname,
1168 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301169
Tony Priskdaeec1f2013-04-03 17:57:11 +13001170 if (IS_ERR(val))
1171 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301172
Viresh Kumarbe193242012-11-20 10:15:19 +05301173 while (sz--)
1174 *out_values++ = be16_to_cpup(val++);
1175 return 0;
1176}
1177EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1178
1179/**
Rob Herring0e373632011-07-06 15:42:58 -05001180 * of_property_read_u32_array - Find and read an array of 32 bit integers
1181 * from a property.
1182 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301183 * @np: device node from which the property value is to be read.
1184 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301185 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301186 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301187 *
Rob Herring0e373632011-07-06 15:42:58 -05001188 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301189 * it. Returns 0 on success, -EINVAL if the property does not exist,
1190 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1191 * property data isn't large enough.
1192 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301193 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301194 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001195int of_property_read_u32_array(const struct device_node *np,
1196 const char *propname, u32 *out_values,
1197 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301198{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001199 const __be32 *val = of_find_property_value_of_size(np, propname,
1200 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301201
Tony Priskdaeec1f2013-04-03 17:57:11 +13001202 if (IS_ERR(val))
1203 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001204
Rob Herring0e373632011-07-06 15:42:58 -05001205 while (sz--)
1206 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301207 return 0;
1208}
Rob Herring0e373632011-07-06 15:42:58 -05001209EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301210
1211/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001212 * of_property_read_u64 - Find and read a 64 bit integer from a property
1213 * @np: device node from which the property value is to be read.
1214 * @propname: name of the property to be searched.
1215 * @out_value: pointer to return value, modified only if return value is 0.
1216 *
1217 * Search for a property in a device node and read a 64-bit value from
1218 * it. Returns 0 on success, -EINVAL if the property does not exist,
1219 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1220 * property data isn't large enough.
1221 *
1222 * The out_value is modified only if a valid u64 value can be decoded.
1223 */
1224int of_property_read_u64(const struct device_node *np, const char *propname,
1225 u64 *out_value)
1226{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001227 const __be32 *val = of_find_property_value_of_size(np, propname,
1228 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001229
Tony Priskdaeec1f2013-04-03 17:57:11 +13001230 if (IS_ERR(val))
1231 return PTR_ERR(val);
1232
1233 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001234 return 0;
1235}
1236EXPORT_SYMBOL_GPL(of_property_read_u64);
1237
1238/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301239 * of_property_read_string - Find and read a string from a property
1240 * @np: device node from which the property value is to be read.
1241 * @propname: name of the property to be searched.
1242 * @out_string: pointer to null terminated return string, modified only if
1243 * return value is 0.
1244 *
1245 * Search for a property in a device tree node and retrieve a null
1246 * terminated string value (pointer to data, not a copy). Returns 0 on
1247 * success, -EINVAL if the property does not exist, -ENODATA if property
1248 * does not have a value, and -EILSEQ if the string is not null-terminated
1249 * within the length of the property data.
1250 *
1251 * The out_string pointer is modified only if a valid string can be decoded.
1252 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001253int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001254 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301255{
1256 struct property *prop = of_find_property(np, propname, NULL);
1257 if (!prop)
1258 return -EINVAL;
1259 if (!prop->value)
1260 return -ENODATA;
1261 if (strnlen(prop->value, prop->length) >= prop->length)
1262 return -EILSEQ;
1263 *out_string = prop->value;
1264 return 0;
1265}
1266EXPORT_SYMBOL_GPL(of_property_read_string);
1267
1268/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001269 * of_property_read_string_index - Find and read a string from a multiple
1270 * strings property.
1271 * @np: device node from which the property value is to be read.
1272 * @propname: name of the property to be searched.
1273 * @index: index of the string in the list of strings
1274 * @out_string: pointer to null terminated return string, modified only if
1275 * return value is 0.
1276 *
1277 * Search for a property in a device tree node and retrieve a null
1278 * terminated string value (pointer to data, not a copy) in the list of strings
1279 * contained in that property.
1280 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1281 * property does not have a value, and -EILSEQ if the string is not
1282 * null-terminated within the length of the property data.
1283 *
1284 * The out_string pointer is modified only if a valid string can be decoded.
1285 */
1286int of_property_read_string_index(struct device_node *np, const char *propname,
1287 int index, const char **output)
1288{
1289 struct property *prop = of_find_property(np, propname, NULL);
1290 int i = 0;
1291 size_t l = 0, total = 0;
1292 const char *p;
1293
1294 if (!prop)
1295 return -EINVAL;
1296 if (!prop->value)
1297 return -ENODATA;
1298 if (strnlen(prop->value, prop->length) >= prop->length)
1299 return -EILSEQ;
1300
1301 p = prop->value;
1302
1303 for (i = 0; total < prop->length; total += l, p += l) {
1304 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001305 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001306 *output = p;
1307 return 0;
1308 }
1309 }
1310 return -ENODATA;
1311}
1312EXPORT_SYMBOL_GPL(of_property_read_string_index);
1313
Grant Likely7aff0fe2011-12-12 09:25:58 -07001314/**
1315 * of_property_match_string() - Find string in a list and return index
1316 * @np: pointer to node containing string list property
1317 * @propname: string list property name
1318 * @string: pointer to string to search for in string list
1319 *
1320 * This function searches a string list property and returns the index
1321 * of a specific string value.
1322 */
1323int of_property_match_string(struct device_node *np, const char *propname,
1324 const char *string)
1325{
1326 struct property *prop = of_find_property(np, propname, NULL);
1327 size_t l;
1328 int i;
1329 const char *p, *end;
1330
1331 if (!prop)
1332 return -EINVAL;
1333 if (!prop->value)
1334 return -ENODATA;
1335
1336 p = prop->value;
1337 end = p + prop->length;
1338
1339 for (i = 0; p < end; i++, p += l) {
1340 l = strlen(p) + 1;
1341 if (p + l > end)
1342 return -EILSEQ;
1343 pr_debug("comparing %s with %s\n", string, p);
1344 if (strcmp(string, p) == 0)
1345 return i; /* Found it; return index */
1346 }
1347 return -ENODATA;
1348}
1349EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001350
1351/**
1352 * of_property_count_strings - Find and return the number of strings from a
1353 * multiple strings property.
1354 * @np: device node from which the property value is to be read.
1355 * @propname: name of the property to be searched.
1356 *
1357 * Search for a property in a device tree node and retrieve the number of null
1358 * terminated string contain in it. Returns the number of strings on
1359 * success, -EINVAL if the property does not exist, -ENODATA if property
1360 * does not have a value, and -EILSEQ if the string is not null-terminated
1361 * within the length of the property data.
1362 */
1363int of_property_count_strings(struct device_node *np, const char *propname)
1364{
1365 struct property *prop = of_find_property(np, propname, NULL);
1366 int i = 0;
1367 size_t l = 0, total = 0;
1368 const char *p;
1369
1370 if (!prop)
1371 return -EINVAL;
1372 if (!prop->value)
1373 return -ENODATA;
1374 if (strnlen(prop->value, prop->length) >= prop->length)
1375 return -EILSEQ;
1376
1377 p = prop->value;
1378
Benoit Cousson88af7f52011-12-05 15:23:54 +01001379 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001380 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001381
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001382 return i;
1383}
1384EXPORT_SYMBOL_GPL(of_property_count_strings);
1385
Grant Likely624cfca2013-10-11 22:05:10 +01001386void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1387{
1388 int i;
1389 printk("%s %s", msg, of_node_full_name(args->np));
1390 for (i = 0; i < args->args_count; i++)
1391 printk(i ? ",%08x" : ":%08x", args->args[i]);
1392 printk("\n");
1393}
1394
Grant Likelybd69f732013-02-10 22:57:21 +00001395static int __of_parse_phandle_with_args(const struct device_node *np,
1396 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001397 const char *cells_name,
1398 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001399 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001400{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001401 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001402 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001403 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001404 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001405 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001406
Grant Likely15c9a0a2011-12-12 09:25:57 -07001407 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001408 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001409 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001410 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001411 list_end = list + size / sizeof(*list);
1412
Grant Likely15c9a0a2011-12-12 09:25:57 -07001413 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001414 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001415 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001416 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001417
Grant Likely15c9a0a2011-12-12 09:25:57 -07001418 /*
1419 * If phandle is 0, then it is an empty entry with no
1420 * arguments. Skip forward to the next entry.
1421 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001422 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001423 if (phandle) {
1424 /*
1425 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001426 * property to determine the argument length.
1427 *
1428 * This is not needed if the cell count is hard-coded
1429 * (i.e. cells_name not set, but cell_count is set),
1430 * except when we're going to return the found node
1431 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001432 */
Stephen Warren91d99422013-08-14 15:27:11 -06001433 if (cells_name || cur_index == index) {
1434 node = of_find_node_by_phandle(phandle);
1435 if (!node) {
1436 pr_err("%s: could not find phandle\n",
1437 np->full_name);
1438 goto err;
1439 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001440 }
Stephen Warren035fd942013-08-14 15:27:10 -06001441
1442 if (cells_name) {
1443 if (of_property_read_u32(node, cells_name,
1444 &count)) {
1445 pr_err("%s: could not get %s for %s\n",
1446 np->full_name, cells_name,
1447 node->full_name);
1448 goto err;
1449 }
1450 } else {
1451 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001452 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001453
Grant Likely15c9a0a2011-12-12 09:25:57 -07001454 /*
1455 * Make sure that the arguments actually fit in the
1456 * remaining property data length
1457 */
1458 if (list + count > list_end) {
1459 pr_err("%s: arguments longer than property\n",
1460 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001461 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001462 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001463 }
1464
Grant Likely15c9a0a2011-12-12 09:25:57 -07001465 /*
1466 * All of the error cases above bail out of the loop, so at
1467 * this point, the parsing is successful. If the requested
1468 * index matches, then fill the out_args structure and return,
1469 * or return -ENOENT for an empty entry.
1470 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001471 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001472 if (cur_index == index) {
1473 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001474 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001475
Grant Likely15c9a0a2011-12-12 09:25:57 -07001476 if (out_args) {
1477 int i;
1478 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1479 count = MAX_PHANDLE_ARGS;
1480 out_args->np = node;
1481 out_args->args_count = count;
1482 for (i = 0; i < count; i++)
1483 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001484 } else {
1485 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001486 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001487
1488 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001489 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001490 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001491
1492 of_node_put(node);
1493 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001494 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001495 cur_index++;
1496 }
1497
Grant Likely23ce04c2013-02-12 21:21:49 +00001498 /*
1499 * Unlock node before returning result; will be one of:
1500 * -ENOENT : index is for empty phandle
1501 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001502 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001503 */
Grant Likelybd69f732013-02-10 22:57:21 +00001504 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001505 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001506 if (node)
1507 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001508 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001509}
Grant Likelybd69f732013-02-10 22:57:21 +00001510
Stephen Warreneded9dd2013-08-14 15:27:08 -06001511/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001512 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1513 * @np: Pointer to device node holding phandle property
1514 * @phandle_name: Name of property holding a phandle value
1515 * @index: For properties holding a table of phandles, this is the index into
1516 * the table
1517 *
1518 * Returns the device_node pointer with refcount incremented. Use
1519 * of_node_put() on it when done.
1520 */
1521struct device_node *of_parse_phandle(const struct device_node *np,
1522 const char *phandle_name, int index)
1523{
Stephen Warren91d99422013-08-14 15:27:11 -06001524 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001525
Stephen Warren91d99422013-08-14 15:27:11 -06001526 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001527 return NULL;
1528
Stephen Warren91d99422013-08-14 15:27:11 -06001529 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1530 index, &args))
1531 return NULL;
1532
1533 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001534}
1535EXPORT_SYMBOL(of_parse_phandle);
1536
1537/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001538 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1539 * @np: pointer to a device tree node containing a list
1540 * @list_name: property name that contains a list
1541 * @cells_name: property name that specifies phandles' arguments count
1542 * @index: index of a phandle to parse out
1543 * @out_args: optional pointer to output arguments structure (will be filled)
1544 *
1545 * This function is useful to parse lists of phandles and their arguments.
1546 * Returns 0 on success and fills out_args, on error returns appropriate
1547 * errno value.
1548 *
1549 * Caller is responsible to call of_node_put() on the returned out_args->node
1550 * pointer.
1551 *
1552 * Example:
1553 *
1554 * phandle1: node1 {
1555 * #list-cells = <2>;
1556 * }
1557 *
1558 * phandle2: node2 {
1559 * #list-cells = <1>;
1560 * }
1561 *
1562 * node3 {
1563 * list = <&phandle1 1 2 &phandle2 3>;
1564 * }
1565 *
1566 * To get a device_node of the `node2' node you may call this:
1567 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1568 */
Grant Likelybd69f732013-02-10 22:57:21 +00001569int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1570 const char *cells_name, int index,
1571 struct of_phandle_args *out_args)
1572{
1573 if (index < 0)
1574 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001575 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1576 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001577}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001578EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001579
Grant Likelybd69f732013-02-10 22:57:21 +00001580/**
Stephen Warren035fd942013-08-14 15:27:10 -06001581 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1582 * @np: pointer to a device tree node containing a list
1583 * @list_name: property name that contains a list
1584 * @cell_count: number of argument cells following the phandle
1585 * @index: index of a phandle to parse out
1586 * @out_args: optional pointer to output arguments structure (will be filled)
1587 *
1588 * This function is useful to parse lists of phandles and their arguments.
1589 * Returns 0 on success and fills out_args, on error returns appropriate
1590 * errno value.
1591 *
1592 * Caller is responsible to call of_node_put() on the returned out_args->node
1593 * pointer.
1594 *
1595 * Example:
1596 *
1597 * phandle1: node1 {
1598 * }
1599 *
1600 * phandle2: node2 {
1601 * }
1602 *
1603 * node3 {
1604 * list = <&phandle1 0 2 &phandle2 2 3>;
1605 * }
1606 *
1607 * To get a device_node of the `node2' node you may call this:
1608 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1609 */
1610int of_parse_phandle_with_fixed_args(const struct device_node *np,
1611 const char *list_name, int cell_count,
1612 int index, struct of_phandle_args *out_args)
1613{
1614 if (index < 0)
1615 return -EINVAL;
1616 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1617 index, out_args);
1618}
1619EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1620
1621/**
Grant Likelybd69f732013-02-10 22:57:21 +00001622 * of_count_phandle_with_args() - Find the number of phandles references in a property
1623 * @np: pointer to a device tree node containing a list
1624 * @list_name: property name that contains a list
1625 * @cells_name: property name that specifies phandles' arguments count
1626 *
1627 * Returns the number of phandle + argument tuples within a property. It
1628 * is a typical pattern to encode a list of phandle and variable
1629 * arguments into a single property. The number of arguments is encoded
1630 * by a property in the phandle-target node. For example, a gpios
1631 * property would contain a list of GPIO specifies consisting of a
1632 * phandle and 1 or more arguments. The number of arguments are
1633 * determined by the #gpio-cells property in the node pointed to by the
1634 * phandle.
1635 */
1636int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1637 const char *cells_name)
1638{
Stephen Warren035fd942013-08-14 15:27:10 -06001639 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1640 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001641}
1642EXPORT_SYMBOL(of_count_phandle_with_args);
1643
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001644#if defined(CONFIG_OF_DYNAMIC)
1645static int of_property_notify(int action, struct device_node *np,
1646 struct property *prop)
1647{
1648 struct of_prop_reconfig pr;
1649
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001650 /* only call notifiers if the node is attached */
1651 if (!of_node_is_attached(np))
1652 return 0;
1653
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001654 pr.dn = np;
1655 pr.prop = prop;
1656 return of_reconfig_notify(action, &pr);
1657}
1658#else
1659static int of_property_notify(int action, struct device_node *np,
1660 struct property *prop)
1661{
1662 return 0;
1663}
1664#endif
1665
Grant Likely02af11b2009-11-23 20:16:45 -07001666/**
Xiubo Li62664f62014-01-22 13:57:39 +08001667 * __of_add_property - Add a property to a node without lock operations
1668 */
1669static int __of_add_property(struct device_node *np, struct property *prop)
1670{
1671 struct property **next;
1672
1673 prop->next = NULL;
1674 next = &np->properties;
1675 while (*next) {
1676 if (strcmp(prop->name, (*next)->name) == 0)
1677 /* duplicate ! don't insert it */
1678 return -EEXIST;
1679
1680 next = &(*next)->next;
1681 }
1682 *next = prop;
1683
1684 return 0;
1685}
1686
1687/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001688 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001689 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001690int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001691{
Grant Likely02af11b2009-11-23 20:16:45 -07001692 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001693 int rc;
1694
1695 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1696 if (rc)
1697 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001698
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001699 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001700 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001701 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001702 if (rc)
1703 return rc;
1704
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001705 if (of_node_is_attached(np))
1706 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001707
Xiubo Li62664f62014-01-22 13:57:39 +08001708 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001709}
1710
1711/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001712 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001713 *
1714 * Note that we don't actually remove it, since we have given out
1715 * who-knows-how-many pointers to the data using get-property.
1716 * Instead we just move the property to the "dead properties"
1717 * list, so it won't be found any more.
1718 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001719int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001720{
1721 struct property **next;
1722 unsigned long flags;
1723 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001724 int rc;
1725
1726 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1727 if (rc)
1728 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001729
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001730 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001731 next = &np->properties;
1732 while (*next) {
1733 if (*next == prop) {
1734 /* found the node */
1735 *next = prop->next;
1736 prop->next = np->deadprops;
1737 np->deadprops = prop;
1738 found = 1;
1739 break;
1740 }
1741 next = &(*next)->next;
1742 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001743 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001744
1745 if (!found)
1746 return -ENODEV;
1747
Grant Likely75b57ec2014-02-20 18:02:11 +00001748 /* at early boot, bail hear and defer setup to of_init() */
1749 if (!of_kset)
1750 return 0;
1751
1752 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1753
Grant Likely02af11b2009-11-23 20:16:45 -07001754 return 0;
1755}
1756
1757/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001758 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001759 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001760 *
1761 * Note that we don't actually remove it, since we have given out
1762 * who-knows-how-many pointers to the data using get-property.
1763 * Instead we just move the property to the "dead properties" list,
1764 * and add the new property to the property list
1765 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001766int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001767{
Dong Aisheng475d0092012-07-11 15:16:37 +10001768 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001769 unsigned long flags;
Grant Likelya3dbeb52014-03-04 16:07:17 +08001770 int rc, found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001771
1772 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1773 if (rc)
1774 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001775
Dong Aisheng475d0092012-07-11 15:16:37 +10001776 if (!newprop->name)
1777 return -EINVAL;
1778
Grant Likelya3dbeb52014-03-04 16:07:17 +08001779 oldprop = of_find_property(np, newprop->name, NULL);
1780 if (!oldprop)
1781 return of_add_property(np, newprop);
1782
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001783 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelya3dbeb52014-03-04 16:07:17 +08001784 next = &np->properties;
1785 while (*next) {
1786 if (*next == oldprop) {
1787 /* found the node */
1788 newprop->next = oldprop->next;
1789 *next = newprop;
1790 oldprop->next = np->deadprops;
1791 np->deadprops = oldprop;
1792 found = 1;
1793 break;
1794 }
1795 next = &(*next)->next;
Grant Likely02af11b2009-11-23 20:16:45 -07001796 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001797 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001798 if (rc)
1799 return rc;
1800
1801 /* Update the sysfs attribute */
1802 if (oldprop)
1803 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1804 __of_add_property_sysfs(np, newprop);
Grant Likely02af11b2009-11-23 20:16:45 -07001805
Grant Likelya3dbeb52014-03-04 16:07:17 +08001806 if (!found)
1807 return -ENODEV;
1808
Grant Likelya3dbeb52014-03-04 16:07:17 +08001809 return 0;
Grant Likely02af11b2009-11-23 20:16:45 -07001810}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001811
1812#if defined(CONFIG_OF_DYNAMIC)
1813/*
1814 * Support for dynamic device trees.
1815 *
1816 * On some platforms, the device tree can be manipulated at runtime.
1817 * The routines in this section support adding, removing and changing
1818 * device tree nodes.
1819 */
1820
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001821static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1822
1823int of_reconfig_notifier_register(struct notifier_block *nb)
1824{
1825 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1826}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001827EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001828
1829int of_reconfig_notifier_unregister(struct notifier_block *nb)
1830{
1831 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1832}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001833EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001834
1835int of_reconfig_notify(unsigned long action, void *p)
1836{
1837 int rc;
1838
1839 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1840 return notifier_to_errno(rc);
1841}
1842
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001843/**
1844 * of_attach_node - Plug a device node into the tree and global list.
1845 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001846int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001847{
1848 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001849 int rc;
1850
1851 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1852 if (rc)
1853 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001854
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001855 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001856 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001857 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001858 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001859 of_allnodes = np;
Pantelis Antonioue3963fd2013-11-08 17:03:57 +02001860 of_node_clear_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001861 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001862
Grant Likely75b57ec2014-02-20 18:02:11 +00001863 of_node_add(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001864 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001865}
1866
1867/**
1868 * of_detach_node - "Unplug" a node from the device tree.
1869 *
1870 * The caller must hold a reference to the node. The memory associated with
1871 * the node is not freed until its refcount goes to zero.
1872 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001873int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001874{
1875 struct device_node *parent;
1876 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001877 int rc = 0;
1878
1879 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1880 if (rc)
1881 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001882
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001883 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001884
Nathan Fontenote81b3292012-10-02 16:55:01 +00001885 if (of_node_check_flag(np, OF_DETACHED)) {
1886 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001887 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001888 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001889 }
1890
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001891 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001892 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001893 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001894 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001895 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001896
Randy Dunlap465aac62012-11-30 10:01:51 +00001897 if (of_allnodes == np)
1898 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001899 else {
1900 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001901 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001902 prev->allnext != np;
1903 prev = prev->allnext)
1904 ;
1905 prev->allnext = np->allnext;
1906 }
1907
1908 if (parent->child == np)
1909 parent->child = np->sibling;
1910 else {
1911 struct device_node *prevsib;
1912 for (prevsib = np->parent->child;
1913 prevsib->sibling != np;
1914 prevsib = prevsib->sibling)
1915 ;
1916 prevsib->sibling = np->sibling;
1917 }
1918
1919 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001920 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001921
Grant Likely75b57ec2014-02-20 18:02:11 +00001922 of_node_remove(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001923 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001924}
1925#endif /* defined(CONFIG_OF_DYNAMIC) */
1926
Shawn Guo611cad72011-08-15 15:28:14 +08001927static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1928 int id, const char *stem, int stem_len)
1929{
1930 ap->np = np;
1931 ap->id = id;
1932 strncpy(ap->stem, stem, stem_len);
1933 ap->stem[stem_len] = 0;
1934 list_add_tail(&ap->link, &aliases_lookup);
1935 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001936 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001937}
1938
1939/**
1940 * of_alias_scan - Scan all properties of 'aliases' node
1941 *
1942 * The function scans all the properties of 'aliases' node and populate
1943 * the the global lookup table with the properties. It returns the
1944 * number of alias_prop found, or error code in error case.
1945 *
1946 * @dt_alloc: An allocator that provides a virtual address to memory
1947 * for the resulting tree
1948 */
1949void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1950{
1951 struct property *pp;
1952
1953 of_chosen = of_find_node_by_path("/chosen");
1954 if (of_chosen == NULL)
1955 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001956
1957 if (of_chosen) {
Grant Likely676e1b22014-03-27 17:11:23 -07001958 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
1959 if (!name)
1960 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001961 if (name)
1962 of_stdout = of_find_node_by_path(name);
1963 }
1964
Shawn Guo611cad72011-08-15 15:28:14 +08001965 of_aliases = of_find_node_by_path("/aliases");
1966 if (!of_aliases)
1967 return;
1968
Dong Aisheng8af0da92011-12-22 20:19:24 +08001969 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001970 const char *start = pp->name;
1971 const char *end = start + strlen(start);
1972 struct device_node *np;
1973 struct alias_prop *ap;
1974 int id, len;
1975
1976 /* Skip those we do not want to proceed */
1977 if (!strcmp(pp->name, "name") ||
1978 !strcmp(pp->name, "phandle") ||
1979 !strcmp(pp->name, "linux,phandle"))
1980 continue;
1981
1982 np = of_find_node_by_path(pp->value);
1983 if (!np)
1984 continue;
1985
1986 /* walk the alias backwards to extract the id and work out
1987 * the 'stem' string */
1988 while (isdigit(*(end-1)) && end > start)
1989 end--;
1990 len = end - start;
1991
1992 if (kstrtoint(end, 10, &id) < 0)
1993 continue;
1994
1995 /* Allocate an alias_prop with enough space for the stem */
1996 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1997 if (!ap)
1998 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001999 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08002000 ap->alias = start;
2001 of_alias_add(ap, np, id, start, len);
2002 }
2003}
2004
2005/**
2006 * of_alias_get_id - Get alias id for the given device_node
2007 * @np: Pointer to the given device_node
2008 * @stem: Alias stem of the given device_node
2009 *
2010 * The function travels the lookup table to get alias id for the given
2011 * device_node and alias stem. It returns the alias id if find it.
2012 */
2013int of_alias_get_id(struct device_node *np, const char *stem)
2014{
2015 struct alias_prop *app;
2016 int id = -ENODEV;
2017
2018 mutex_lock(&of_aliases_mutex);
2019 list_for_each_entry(app, &aliases_lookup, link) {
2020 if (strcmp(app->stem, stem) != 0)
2021 continue;
2022
2023 if (np == app->np) {
2024 id = app->id;
2025 break;
2026 }
2027 }
2028 mutex_unlock(&of_aliases_mutex);
2029
2030 return id;
2031}
2032EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06002033
2034const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2035 u32 *pu)
2036{
2037 const void *curv = cur;
2038
2039 if (!prop)
2040 return NULL;
2041
2042 if (!cur) {
2043 curv = prop->value;
2044 goto out_val;
2045 }
2046
2047 curv += sizeof(*cur);
2048 if (curv >= prop->value + prop->length)
2049 return NULL;
2050
2051out_val:
2052 *pu = be32_to_cpup(curv);
2053 return curv;
2054}
2055EXPORT_SYMBOL_GPL(of_prop_next_u32);
2056
2057const char *of_prop_next_string(struct property *prop, const char *cur)
2058{
2059 const void *curv = cur;
2060
2061 if (!prop)
2062 return NULL;
2063
2064 if (!cur)
2065 return prop->value;
2066
2067 curv += strlen(cur) + 1;
2068 if (curv >= prop->value + prop->length)
2069 return NULL;
2070
2071 return curv;
2072}
2073EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002074
2075/**
2076 * of_device_is_stdout_path - check if a device node matches the
2077 * linux,stdout-path property
2078 *
2079 * Check if this device node matches the linux,stdout-path property
2080 * in the chosen node. return true if yes, false otherwise.
2081 */
2082int of_device_is_stdout_path(struct device_node *dn)
2083{
2084 if (!of_stdout)
2085 return false;
2086
2087 return of_stdout == dn;
2088}
2089EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002090
2091/**
2092 * of_find_next_cache_node - Find a node's subsidiary cache
2093 * @np: node of type "cpu" or "cache"
2094 *
2095 * Returns a node pointer with refcount incremented, use
2096 * of_node_put() on it when done. Caller should hold a reference
2097 * to np.
2098 */
2099struct device_node *of_find_next_cache_node(const struct device_node *np)
2100{
2101 struct device_node *child;
2102 const phandle *handle;
2103
2104 handle = of_get_property(np, "l2-cache", NULL);
2105 if (!handle)
2106 handle = of_get_property(np, "next-level-cache", NULL);
2107
2108 if (handle)
2109 return of_find_node_by_phandle(be32_to_cpup(handle));
2110
2111 /* OF on pmac has nodes instead of properties named "l2-cache"
2112 * beneath CPU nodes.
2113 */
2114 if (!strcmp(np->type, "cpu"))
2115 for_each_child_of_node(np, child)
2116 if (!strcmp(child->type, "cache"))
2117 return child;
2118
2119 return NULL;
2120}