blob: 3b70a468c8ab1045b674039cf0d86d42dca58a3c [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;
249 kobject_init(&np->kobj, &of_node_ktype);
250 mutex_lock(&of_aliases_mutex);
251 if (of_kset)
252 rc = __of_node_add(np);
253 mutex_unlock(&of_aliases_mutex);
254 return rc;
255}
256
257#if defined(CONFIG_OF_DYNAMIC)
258static void of_node_remove(struct device_node *np)
259{
260 struct property *pp;
261
262 for_each_property_of_node(np, pp)
263 sysfs_remove_bin_file(&np->kobj, &pp->attr);
264
265 kobject_del(&np->kobj);
266}
267#endif
268
269static int __init of_init(void)
270{
271 struct device_node *np;
272
273 /* Create the kset, and register existing nodes */
274 mutex_lock(&of_aliases_mutex);
275 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
276 if (!of_kset) {
277 mutex_unlock(&of_aliases_mutex);
278 return -ENOMEM;
279 }
280 for_each_of_allnodes(np)
281 __of_node_add(np);
282 mutex_unlock(&of_aliases_mutex);
283
284#if !defined(CONFIG_PROC_DEVICETREE)
285 /* Symlink to the new tree when PROC_DEVICETREE is disabled */
286 if (of_allnodes)
287 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
288#endif /* CONFIG_PROC_DEVICETREE */
289
290 return 0;
291}
292core_initcall(of_init);
293
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500294static struct property *__of_find_property(const struct device_node *np,
295 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000296{
297 struct property *pp;
298
Timur Tabi64e45662008-05-08 05:19:59 +1000299 if (!np)
300 return NULL;
301
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530302 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000303 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530304 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000305 *lenp = pp->length;
306 break;
307 }
308 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500309
310 return pp;
311}
312
313struct property *of_find_property(const struct device_node *np,
314 const char *name,
315 int *lenp)
316{
317 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500318 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500319
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500320 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500321 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500322 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000323
324 return pp;
325}
326EXPORT_SYMBOL(of_find_property);
327
Grant Likelye91edcf2009-10-15 10:58:09 -0600328/**
329 * of_find_all_nodes - Get next node in global list
330 * @prev: Previous node or NULL to start iteration
331 * of_node_put() will be called on it
332 *
333 * Returns a node pointer with refcount incremented, use
334 * of_node_put() on it when done.
335 */
336struct device_node *of_find_all_nodes(struct device_node *prev)
337{
338 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000339 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600340
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000341 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000342 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600343 for (; np != NULL; np = np->allnext)
344 if (of_node_get(np))
345 break;
346 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000347 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600348 return np;
349}
350EXPORT_SYMBOL(of_find_all_nodes);
351
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000352/*
353 * Find a property with a given name for a given node
354 * and return the value.
355 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500356static const void *__of_get_property(const struct device_node *np,
357 const char *name, int *lenp)
358{
359 struct property *pp = __of_find_property(np, name, lenp);
360
361 return pp ? pp->value : NULL;
362}
363
364/*
365 * Find a property with a given name for a given node
366 * and return the value.
367 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000368const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500369 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000370{
371 struct property *pp = of_find_property(np, name, lenp);
372
373 return pp ? pp->value : NULL;
374}
375EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000376
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100377/*
378 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
379 *
380 * @cpu: logical cpu index of a core/thread
381 * @phys_id: physical identifier of a core/thread
382 *
383 * CPU logical to physical index mapping is architecture specific.
384 * However this __weak function provides a default match of physical
385 * id to logical cpu index. phys_id provided here is usually values read
386 * from the device tree which must match the hardware internal registers.
387 *
388 * Returns true if the physical identifier and the logical cpu index
389 * correspond to the same core/thread, false otherwise.
390 */
391bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
392{
393 return (u32)phys_id == cpu;
394}
395
396/**
397 * Checks if the given "prop_name" property holds the physical id of the
398 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
399 * NULL, local thread number within the core is returned in it.
400 */
401static bool __of_find_n_match_cpu_property(struct device_node *cpun,
402 const char *prop_name, int cpu, unsigned int *thread)
403{
404 const __be32 *cell;
405 int ac, prop_len, tid;
406 u64 hwid;
407
408 ac = of_n_addr_cells(cpun);
409 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100410 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100411 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100412 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100413 for (tid = 0; tid < prop_len; tid++) {
414 hwid = of_read_number(cell, ac);
415 if (arch_match_cpu_phys_id(cpu, hwid)) {
416 if (thread)
417 *thread = tid;
418 return true;
419 }
420 cell += ac;
421 }
422 return false;
423}
424
David Millerd1cb9d12013-10-03 17:24:51 -0400425/*
426 * arch_find_n_match_cpu_physical_id - See if the given device node is
427 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
428 * else false. If 'thread' is non-NULL, the local thread number within the
429 * core is returned in it.
430 */
431bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
432 int cpu, unsigned int *thread)
433{
434 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
435 * for thread ids on PowerPC. If it doesn't exist fallback to
436 * standard "reg" property.
437 */
438 if (IS_ENABLED(CONFIG_PPC) &&
439 __of_find_n_match_cpu_property(cpun,
440 "ibm,ppc-interrupt-server#s",
441 cpu, thread))
442 return true;
443
444 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
445 return true;
446
447 return false;
448}
449
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100450/**
451 * of_get_cpu_node - Get device node associated with the given logical CPU
452 *
453 * @cpu: CPU number(logical index) for which device node is required
454 * @thread: if not NULL, local thread number within the physical core is
455 * returned
456 *
457 * The main purpose of this function is to retrieve the device node for the
458 * given logical CPU index. It should be used to initialize the of_node in
459 * cpu device. Once of_node in cpu device is populated, all the further
460 * references can use that instead.
461 *
462 * CPU logical to physical index mapping is architecture specific and is built
463 * before booting secondary cores. This function uses arch_match_cpu_phys_id
464 * which can be overridden by architecture specific implementation.
465 *
466 * Returns a node pointer for the logical cpu if found, else NULL.
467 */
468struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
469{
David Millerd1cb9d12013-10-03 17:24:51 -0400470 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100471
David Millerd1cb9d12013-10-03 17:24:51 -0400472 for_each_node_by_type(cpun, "cpu") {
473 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100474 return cpun;
475 }
476 return NULL;
477}
478EXPORT_SYMBOL(of_get_cpu_node);
479
Kevin Hao215a14c2014-02-19 16:15:45 +0800480/**
481 * __of_device_is_compatible() - Check if the node matches given constraints
482 * @device: pointer to node
483 * @compat: required compatible string, NULL or "" for any match
484 * @type: required device_type value, NULL or "" for any match
485 * @name: required node name, NULL or "" for any match
486 *
487 * Checks if the given @compat, @type and @name strings match the
488 * properties of the given @device. A constraints can be skipped by
489 * passing NULL or an empty string as the constraint.
490 *
491 * Returns 0 for no match, and a positive integer on match. The return
492 * value is a relative score with larger values indicating better
493 * matches. The score is weighted for the most specific compatible value
494 * to get the highest score. Matching type is next, followed by matching
495 * name. Practically speaking, this results in the following priority
496 * order for matches:
497 *
498 * 1. specific compatible && type && name
499 * 2. specific compatible && type
500 * 3. specific compatible && name
501 * 4. specific compatible
502 * 5. general compatible && type && name
503 * 6. general compatible && type
504 * 7. general compatible && name
505 * 8. general compatible
506 * 9. type && name
507 * 10. type
508 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000509 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500510static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800511 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000512{
Kevin Hao215a14c2014-02-19 16:15:45 +0800513 struct property *prop;
514 const char *cp;
515 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000516
Kevin Hao215a14c2014-02-19 16:15:45 +0800517 /* Compatible match has highest priority */
518 if (compat && compat[0]) {
519 prop = __of_find_property(device, "compatible", NULL);
520 for (cp = of_prop_next_string(prop, NULL); cp;
521 cp = of_prop_next_string(prop, cp), index++) {
522 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
523 score = INT_MAX/2 - (index << 2);
524 break;
525 }
526 }
527 if (!score)
528 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000529 }
530
Kevin Hao215a14c2014-02-19 16:15:45 +0800531 /* Matching type is better than matching name */
532 if (type && type[0]) {
533 if (!device->type || of_node_cmp(type, device->type))
534 return 0;
535 score += 2;
536 }
537
538 /* Matching name is a bit better than not */
539 if (name && name[0]) {
540 if (!device->name || of_node_cmp(name, device->name))
541 return 0;
542 score++;
543 }
544
545 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000546}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500547
548/** Checks if the given "compat" string matches one of the strings in
549 * the device's "compatible" property
550 */
551int of_device_is_compatible(const struct device_node *device,
552 const char *compat)
553{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500554 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500555 int res;
556
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500557 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800558 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500559 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500560 return res;
561}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000562EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000563
564/**
Grant Likely71a157e2010-02-01 21:34:14 -0700565 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700566 * @compat: compatible string to look for in root node's compatible property.
567 *
568 * Returns true if the root node has the given value in its
569 * compatible property.
570 */
Grant Likely71a157e2010-02-01 21:34:14 -0700571int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700572{
573 struct device_node *root;
574 int rc = 0;
575
576 root = of_find_node_by_path("/");
577 if (root) {
578 rc = of_device_is_compatible(root, compat);
579 of_node_put(root);
580 }
581 return rc;
582}
Grant Likely71a157e2010-02-01 21:34:14 -0700583EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700584
585/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700586 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100587 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700588 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100589 *
590 * Returns 1 if the status property is absent or set to "okay" or "ok",
591 * 0 otherwise
592 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700593static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100594{
595 const char *status;
596 int statlen;
597
Xiubo Li42ccd782014-01-13 11:07:28 +0800598 if (!device)
599 return 0;
600
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700601 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100602 if (status == NULL)
603 return 1;
604
605 if (statlen > 0) {
606 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
607 return 1;
608 }
609
610 return 0;
611}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700612
613/**
614 * of_device_is_available - check if a device is available for use
615 *
616 * @device: Node to check for availability
617 *
618 * Returns 1 if the status property is absent or set to "okay" or "ok",
619 * 0 otherwise
620 */
621int of_device_is_available(const struct device_node *device)
622{
623 unsigned long flags;
624 int res;
625
626 raw_spin_lock_irqsave(&devtree_lock, flags);
627 res = __of_device_is_available(device);
628 raw_spin_unlock_irqrestore(&devtree_lock, flags);
629 return res;
630
631}
Josh Boyer834d97d2008-03-27 00:33:14 +1100632EXPORT_SYMBOL(of_device_is_available);
633
634/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000635 * of_get_parent - Get a node's parent if any
636 * @node: Node to get parent
637 *
638 * Returns a node pointer with refcount incremented, use
639 * of_node_put() on it when done.
640 */
641struct device_node *of_get_parent(const struct device_node *node)
642{
643 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500644 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000645
646 if (!node)
647 return NULL;
648
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500649 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000650 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500651 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000652 return np;
653}
654EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000655
656/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000657 * of_get_next_parent - Iterate to a node's parent
658 * @node: Node to get parent of
659 *
660 * This is like of_get_parent() except that it drops the
661 * refcount on the passed node, making it suitable for iterating
662 * through a node's parents.
663 *
664 * Returns a node pointer with refcount incremented, use
665 * of_node_put() on it when done.
666 */
667struct device_node *of_get_next_parent(struct device_node *node)
668{
669 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500670 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000671
672 if (!node)
673 return NULL;
674
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500675 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000676 parent = of_node_get(node->parent);
677 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500678 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000679 return parent;
680}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300681EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000682
683/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000684 * of_get_next_child - Iterate a node childs
685 * @node: parent node
686 * @prev: previous child of the parent node, or NULL to get first
687 *
688 * Returns a node pointer with refcount incremented, use
689 * of_node_put() on it when done.
690 */
691struct device_node *of_get_next_child(const struct device_node *node,
692 struct device_node *prev)
693{
694 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500695 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000696
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500697 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000698 next = prev ? prev->sibling : node->child;
699 for (; next; next = next->sibling)
700 if (of_node_get(next))
701 break;
702 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500703 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000704 return next;
705}
706EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000707
708/**
Timur Tabi32961932012-08-14 13:20:23 +0000709 * of_get_next_available_child - Find the next available child node
710 * @node: parent node
711 * @prev: previous child of the parent node, or NULL to get first
712 *
713 * This function is like of_get_next_child(), except that it
714 * automatically skips any disabled nodes (i.e. status = "disabled").
715 */
716struct device_node *of_get_next_available_child(const struct device_node *node,
717 struct device_node *prev)
718{
719 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000720 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000721
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000722 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000723 next = prev ? prev->sibling : node->child;
724 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700725 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000726 continue;
727 if (of_node_get(next))
728 break;
729 }
730 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000731 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000732 return next;
733}
734EXPORT_SYMBOL(of_get_next_available_child);
735
736/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100737 * of_get_child_by_name - Find the child node by name for a given parent
738 * @node: parent node
739 * @name: child name to look for.
740 *
741 * This function looks for child node for given matching name
742 *
743 * Returns a node pointer if found, with refcount incremented, use
744 * of_node_put() on it when done.
745 * Returns NULL if node is not found.
746 */
747struct device_node *of_get_child_by_name(const struct device_node *node,
748 const char *name)
749{
750 struct device_node *child;
751
752 for_each_child_of_node(node, child)
753 if (child->name && (of_node_cmp(child->name, name) == 0))
754 break;
755 return child;
756}
757EXPORT_SYMBOL(of_get_child_by_name);
758
759/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000760 * of_find_node_by_path - Find a node matching a full OF path
761 * @path: The full path to match
762 *
763 * Returns a node pointer with refcount incremented, use
764 * of_node_put() on it when done.
765 */
766struct device_node *of_find_node_by_path(const char *path)
767{
Randy Dunlap465aac62012-11-30 10:01:51 +0000768 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500769 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000770
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500771 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000772 for (; np; np = np->allnext) {
773 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
774 && of_node_get(np))
775 break;
776 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500777 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000778 return np;
779}
780EXPORT_SYMBOL(of_find_node_by_path);
781
782/**
783 * of_find_node_by_name - Find a node by its "name" property
784 * @from: The node to start searching from or NULL, the node
785 * you pass will not be searched, only the next one
786 * will; typically, you pass what the previous call
787 * returned. of_node_put() will be called on it
788 * @name: The name string to match against
789 *
790 * Returns a node pointer with refcount incremented, use
791 * of_node_put() on it when done.
792 */
793struct device_node *of_find_node_by_name(struct device_node *from,
794 const char *name)
795{
796 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500797 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000798
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500799 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000800 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000801 for (; np; np = np->allnext)
802 if (np->name && (of_node_cmp(np->name, name) == 0)
803 && of_node_get(np))
804 break;
805 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500806 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000807 return np;
808}
809EXPORT_SYMBOL(of_find_node_by_name);
810
811/**
812 * of_find_node_by_type - Find a node by its "device_type" property
813 * @from: The node to start searching from, or NULL to start searching
814 * the entire device tree. The node you pass will not be
815 * searched, only the next one will; typically, you pass
816 * what the previous call returned. of_node_put() will be
817 * called on from for you.
818 * @type: The type string to match against
819 *
820 * Returns a node pointer with refcount incremented, use
821 * of_node_put() on it when done.
822 */
823struct device_node *of_find_node_by_type(struct device_node *from,
824 const char *type)
825{
826 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500827 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000828
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500829 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000830 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000831 for (; np; np = np->allnext)
832 if (np->type && (of_node_cmp(np->type, type) == 0)
833 && of_node_get(np))
834 break;
835 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500836 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000837 return np;
838}
839EXPORT_SYMBOL(of_find_node_by_type);
840
841/**
842 * of_find_compatible_node - Find a node based on type and one of the
843 * tokens in its "compatible" property
844 * @from: The node to start searching from or NULL, the node
845 * you pass will not be searched, only the next one
846 * will; typically, you pass what the previous call
847 * returned. of_node_put() will be called on it
848 * @type: The type string to match "device_type" or NULL to ignore
849 * @compatible: The string to match to one of the tokens in the device
850 * "compatible" list.
851 *
852 * Returns a node pointer with refcount incremented, use
853 * of_node_put() on it when done.
854 */
855struct device_node *of_find_compatible_node(struct device_node *from,
856 const char *type, const char *compatible)
857{
858 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500859 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000860
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500861 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000862 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000863 for (; np; np = np->allnext) {
Kevin Hao215a14c2014-02-19 16:15:45 +0800864 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500865 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000866 break;
867 }
868 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500869 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000870 return np;
871}
872EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100873
874/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000875 * of_find_node_with_property - Find a node which has a property with
876 * the given name.
877 * @from: The node to start searching from or NULL, the node
878 * you pass will not be searched, only the next one
879 * will; typically, you pass what the previous call
880 * returned. of_node_put() will be called on it
881 * @prop_name: The name of the property to look for.
882 *
883 * Returns a node pointer with refcount incremented, use
884 * of_node_put() on it when done.
885 */
886struct device_node *of_find_node_with_property(struct device_node *from,
887 const char *prop_name)
888{
889 struct device_node *np;
890 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500891 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000892
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500893 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000894 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000895 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530896 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000897 if (of_prop_cmp(pp->name, prop_name) == 0) {
898 of_node_get(np);
899 goto out;
900 }
901 }
902 }
903out:
904 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500905 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000906 return np;
907}
908EXPORT_SYMBOL(of_find_node_with_property);
909
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500910static
911const struct of_device_id *__of_match_node(const struct of_device_id *matches,
912 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100913{
Kevin Hao215a14c2014-02-19 16:15:45 +0800914 const struct of_device_id *best_match = NULL;
915 int score, best_score = 0;
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100916
Grant Likelya52f07e2011-03-18 10:21:29 -0600917 if (!matches)
918 return NULL;
919
Kevin Hao215a14c2014-02-19 16:15:45 +0800920 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
921 score = __of_device_is_compatible(node, matches->compatible,
922 matches->type, matches->name);
923 if (score > best_score) {
924 best_match = matches;
925 best_score = score;
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100926 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +0800927 }
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100928
Kevin Hao215a14c2014-02-19 16:15:45 +0800929 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +1100930}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500931
932/**
933 * of_match_node - Tell if an device_node has a matching of_match structure
934 * @matches: array of of device match structures to search in
935 * @node: the of device structure to match against
936 *
Kevin Hao71c54982014-02-18 15:57:29 +0800937 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500938 */
939const struct of_device_id *of_match_node(const struct of_device_id *matches,
940 const struct device_node *node)
941{
942 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500943 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500944
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500945 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500946 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500947 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500948 return match;
949}
Grant Likely283029d2008-01-09 06:20:40 +1100950EXPORT_SYMBOL(of_match_node);
951
952/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700953 * of_find_matching_node_and_match - Find a node based on an of_device_id
954 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100955 * @from: The node to start searching from or NULL, the node
956 * you pass will not be searched, only the next one
957 * will; typically, you pass what the previous call
958 * returned. of_node_put() will be called on it
959 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700960 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100961 *
962 * Returns a node pointer with refcount incremented, use
963 * of_node_put() on it when done.
964 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700965struct device_node *of_find_matching_node_and_match(struct device_node *from,
966 const struct of_device_id *matches,
967 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100968{
969 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800970 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500971 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100972
Stephen Warren50c8af42012-11-20 16:12:20 -0700973 if (match)
974 *match = NULL;
975
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500976 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000977 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100978 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500979 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800980 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700981 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800982 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100983 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700984 }
Grant Likely283029d2008-01-09 06:20:40 +1100985 }
986 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500987 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100988 return np;
989}
Grant Likely80c20222012-12-19 10:45:36 +0000990EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400991
992/**
Grant Likely3f07af42008-07-25 22:25:13 -0400993 * of_modalias_node - Lookup appropriate modalias for a device node
994 * @node: pointer to a device tree node
995 * @modalias: Pointer to buffer that modalias value will be copied into
996 * @len: Length of modalias value
997 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600998 * Based on the value of the compatible property, this routine will attempt
999 * to choose an appropriate modalias value for a particular device tree node.
1000 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1001 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001002 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001003 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001004 */
1005int of_modalias_node(struct device_node *node, char *modalias, int len)
1006{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001007 const char *compatible, *p;
1008 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001009
1010 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001011 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001012 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001013 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001014 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001015 return 0;
1016}
1017EXPORT_SYMBOL_GPL(of_modalias_node);
1018
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001019/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001020 * of_find_node_by_phandle - Find a node given a phandle
1021 * @handle: phandle of the node to find
1022 *
1023 * Returns a node pointer with refcount incremented, use
1024 * of_node_put() on it when done.
1025 */
1026struct device_node *of_find_node_by_phandle(phandle handle)
1027{
1028 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001029 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001030
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001031 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001032 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001033 if (np->phandle == handle)
1034 break;
1035 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001036 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001037 return np;
1038}
1039EXPORT_SYMBOL(of_find_node_by_phandle);
1040
1041/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001042 * of_find_property_value_of_size
1043 *
1044 * @np: device node from which the property value is to be read.
1045 * @propname: name of the property to be searched.
1046 * @len: requested length of property value
1047 *
1048 * Search for a property in a device node and valid the requested size.
1049 * Returns the property value on success, -EINVAL if the property does not
1050 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1051 * property data isn't large enough.
1052 *
1053 */
1054static void *of_find_property_value_of_size(const struct device_node *np,
1055 const char *propname, u32 len)
1056{
1057 struct property *prop = of_find_property(np, propname, NULL);
1058
1059 if (!prop)
1060 return ERR_PTR(-EINVAL);
1061 if (!prop->value)
1062 return ERR_PTR(-ENODATA);
1063 if (len > prop->length)
1064 return ERR_PTR(-EOVERFLOW);
1065
1066 return prop->value;
1067}
1068
1069/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001070 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1071 *
1072 * @np: device node from which the property value is to be read.
1073 * @propname: name of the property to be searched.
1074 * @index: index of the u32 in the list of values
1075 * @out_value: pointer to return value, modified only if no error.
1076 *
1077 * Search for a property in a device node and read nth 32-bit value from
1078 * it. Returns 0 on success, -EINVAL if the property does not exist,
1079 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1080 * property data isn't large enough.
1081 *
1082 * The out_value is modified only if a valid u32 value can be decoded.
1083 */
1084int of_property_read_u32_index(const struct device_node *np,
1085 const char *propname,
1086 u32 index, u32 *out_value)
1087{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001088 const u32 *val = of_find_property_value_of_size(np, propname,
1089 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +13001090
Tony Priskdaeec1f2013-04-03 17:57:11 +13001091 if (IS_ERR(val))
1092 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001093
Tony Priskdaeec1f2013-04-03 17:57:11 +13001094 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001095 return 0;
1096}
1097EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1098
1099/**
Viresh Kumarbe193242012-11-20 10:15:19 +05301100 * of_property_read_u8_array - Find and read an array of u8 from a property.
1101 *
1102 * @np: device node from which the property value is to be read.
1103 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301104 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301105 * @sz: number of array elements to read
1106 *
1107 * Search for a property in a device node and read 8-bit value(s) from
1108 * it. Returns 0 on success, -EINVAL if the property does not exist,
1109 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1110 * property data isn't large enough.
1111 *
1112 * dts entry of array should be like:
1113 * property = /bits/ 8 <0x50 0x60 0x70>;
1114 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301115 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301116 */
1117int of_property_read_u8_array(const struct device_node *np,
1118 const char *propname, u8 *out_values, size_t sz)
1119{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001120 const u8 *val = of_find_property_value_of_size(np, propname,
1121 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301122
Tony Priskdaeec1f2013-04-03 17:57:11 +13001123 if (IS_ERR(val))
1124 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301125
Viresh Kumarbe193242012-11-20 10:15:19 +05301126 while (sz--)
1127 *out_values++ = *val++;
1128 return 0;
1129}
1130EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1131
1132/**
1133 * of_property_read_u16_array - Find and read an array of u16 from a property.
1134 *
1135 * @np: device node from which the property value is to be read.
1136 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301137 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301138 * @sz: number of array elements to read
1139 *
1140 * Search for a property in a device node and read 16-bit value(s) from
1141 * it. Returns 0 on success, -EINVAL if the property does not exist,
1142 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1143 * property data isn't large enough.
1144 *
1145 * dts entry of array should be like:
1146 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1147 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301148 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301149 */
1150int of_property_read_u16_array(const struct device_node *np,
1151 const char *propname, u16 *out_values, size_t sz)
1152{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001153 const __be16 *val = of_find_property_value_of_size(np, propname,
1154 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301155
Tony Priskdaeec1f2013-04-03 17:57:11 +13001156 if (IS_ERR(val))
1157 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301158
Viresh Kumarbe193242012-11-20 10:15:19 +05301159 while (sz--)
1160 *out_values++ = be16_to_cpup(val++);
1161 return 0;
1162}
1163EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1164
1165/**
Rob Herring0e373632011-07-06 15:42:58 -05001166 * of_property_read_u32_array - Find and read an array of 32 bit integers
1167 * from a property.
1168 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301169 * @np: device node from which the property value is to be read.
1170 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301171 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301172 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301173 *
Rob Herring0e373632011-07-06 15:42:58 -05001174 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301175 * it. Returns 0 on success, -EINVAL if the property does not exist,
1176 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1177 * property data isn't large enough.
1178 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301179 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301180 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001181int of_property_read_u32_array(const struct device_node *np,
1182 const char *propname, u32 *out_values,
1183 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301184{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001185 const __be32 *val = of_find_property_value_of_size(np, propname,
1186 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301187
Tony Priskdaeec1f2013-04-03 17:57:11 +13001188 if (IS_ERR(val))
1189 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001190
Rob Herring0e373632011-07-06 15:42:58 -05001191 while (sz--)
1192 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301193 return 0;
1194}
Rob Herring0e373632011-07-06 15:42:58 -05001195EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301196
1197/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001198 * of_property_read_u64 - Find and read a 64 bit integer from a property
1199 * @np: device node from which the property value is to be read.
1200 * @propname: name of the property to be searched.
1201 * @out_value: pointer to return value, modified only if return value is 0.
1202 *
1203 * Search for a property in a device node and read a 64-bit value from
1204 * it. Returns 0 on success, -EINVAL if the property does not exist,
1205 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1206 * property data isn't large enough.
1207 *
1208 * The out_value is modified only if a valid u64 value can be decoded.
1209 */
1210int of_property_read_u64(const struct device_node *np, const char *propname,
1211 u64 *out_value)
1212{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001213 const __be32 *val = of_find_property_value_of_size(np, propname,
1214 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001215
Tony Priskdaeec1f2013-04-03 17:57:11 +13001216 if (IS_ERR(val))
1217 return PTR_ERR(val);
1218
1219 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001220 return 0;
1221}
1222EXPORT_SYMBOL_GPL(of_property_read_u64);
1223
1224/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301225 * of_property_read_string - Find and read a string from a property
1226 * @np: device node from which the property value is to be read.
1227 * @propname: name of the property to be searched.
1228 * @out_string: pointer to null terminated return string, modified only if
1229 * return value is 0.
1230 *
1231 * Search for a property in a device tree node and retrieve a null
1232 * terminated string value (pointer to data, not a copy). Returns 0 on
1233 * success, -EINVAL if the property does not exist, -ENODATA if property
1234 * does not have a value, and -EILSEQ if the string is not null-terminated
1235 * within the length of the property data.
1236 *
1237 * The out_string pointer is modified only if a valid string can be decoded.
1238 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001239int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001240 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301241{
1242 struct property *prop = of_find_property(np, propname, NULL);
1243 if (!prop)
1244 return -EINVAL;
1245 if (!prop->value)
1246 return -ENODATA;
1247 if (strnlen(prop->value, prop->length) >= prop->length)
1248 return -EILSEQ;
1249 *out_string = prop->value;
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(of_property_read_string);
1253
1254/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001255 * of_property_read_string_index - Find and read a string from a multiple
1256 * strings property.
1257 * @np: device node from which the property value is to be read.
1258 * @propname: name of the property to be searched.
1259 * @index: index of the string in the list of strings
1260 * @out_string: pointer to null terminated return string, modified only if
1261 * return value is 0.
1262 *
1263 * Search for a property in a device tree node and retrieve a null
1264 * terminated string value (pointer to data, not a copy) in the list of strings
1265 * contained in that property.
1266 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1267 * property does not have a value, and -EILSEQ if the string is not
1268 * null-terminated within the length of the property data.
1269 *
1270 * The out_string pointer is modified only if a valid string can be decoded.
1271 */
1272int of_property_read_string_index(struct device_node *np, const char *propname,
1273 int index, const char **output)
1274{
1275 struct property *prop = of_find_property(np, propname, NULL);
1276 int i = 0;
1277 size_t l = 0, total = 0;
1278 const char *p;
1279
1280 if (!prop)
1281 return -EINVAL;
1282 if (!prop->value)
1283 return -ENODATA;
1284 if (strnlen(prop->value, prop->length) >= prop->length)
1285 return -EILSEQ;
1286
1287 p = prop->value;
1288
1289 for (i = 0; total < prop->length; total += l, p += l) {
1290 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001291 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001292 *output = p;
1293 return 0;
1294 }
1295 }
1296 return -ENODATA;
1297}
1298EXPORT_SYMBOL_GPL(of_property_read_string_index);
1299
Grant Likely7aff0fe2011-12-12 09:25:58 -07001300/**
1301 * of_property_match_string() - Find string in a list and return index
1302 * @np: pointer to node containing string list property
1303 * @propname: string list property name
1304 * @string: pointer to string to search for in string list
1305 *
1306 * This function searches a string list property and returns the index
1307 * of a specific string value.
1308 */
1309int of_property_match_string(struct device_node *np, const char *propname,
1310 const char *string)
1311{
1312 struct property *prop = of_find_property(np, propname, NULL);
1313 size_t l;
1314 int i;
1315 const char *p, *end;
1316
1317 if (!prop)
1318 return -EINVAL;
1319 if (!prop->value)
1320 return -ENODATA;
1321
1322 p = prop->value;
1323 end = p + prop->length;
1324
1325 for (i = 0; p < end; i++, p += l) {
1326 l = strlen(p) + 1;
1327 if (p + l > end)
1328 return -EILSEQ;
1329 pr_debug("comparing %s with %s\n", string, p);
1330 if (strcmp(string, p) == 0)
1331 return i; /* Found it; return index */
1332 }
1333 return -ENODATA;
1334}
1335EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001336
1337/**
1338 * of_property_count_strings - Find and return the number of strings from a
1339 * multiple strings property.
1340 * @np: device node from which the property value is to be read.
1341 * @propname: name of the property to be searched.
1342 *
1343 * Search for a property in a device tree node and retrieve the number of null
1344 * terminated string contain in it. Returns the number of strings on
1345 * success, -EINVAL if the property does not exist, -ENODATA if property
1346 * does not have a value, and -EILSEQ if the string is not null-terminated
1347 * within the length of the property data.
1348 */
1349int of_property_count_strings(struct device_node *np, const char *propname)
1350{
1351 struct property *prop = of_find_property(np, propname, NULL);
1352 int i = 0;
1353 size_t l = 0, total = 0;
1354 const char *p;
1355
1356 if (!prop)
1357 return -EINVAL;
1358 if (!prop->value)
1359 return -ENODATA;
1360 if (strnlen(prop->value, prop->length) >= prop->length)
1361 return -EILSEQ;
1362
1363 p = prop->value;
1364
Benoit Cousson88af7f52011-12-05 15:23:54 +01001365 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001366 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001367
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001368 return i;
1369}
1370EXPORT_SYMBOL_GPL(of_property_count_strings);
1371
Grant Likely624cfca2013-10-11 22:05:10 +01001372void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1373{
1374 int i;
1375 printk("%s %s", msg, of_node_full_name(args->np));
1376 for (i = 0; i < args->args_count; i++)
1377 printk(i ? ",%08x" : ":%08x", args->args[i]);
1378 printk("\n");
1379}
1380
Grant Likelybd69f732013-02-10 22:57:21 +00001381static int __of_parse_phandle_with_args(const struct device_node *np,
1382 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001383 const char *cells_name,
1384 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001385 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001386{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001387 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001388 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001389 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001390 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001391 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001392
Grant Likely15c9a0a2011-12-12 09:25:57 -07001393 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001394 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001395 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001396 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001397 list_end = list + size / sizeof(*list);
1398
Grant Likely15c9a0a2011-12-12 09:25:57 -07001399 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001400 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001401 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001402 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001403
Grant Likely15c9a0a2011-12-12 09:25:57 -07001404 /*
1405 * If phandle is 0, then it is an empty entry with no
1406 * arguments. Skip forward to the next entry.
1407 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001408 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001409 if (phandle) {
1410 /*
1411 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001412 * property to determine the argument length.
1413 *
1414 * This is not needed if the cell count is hard-coded
1415 * (i.e. cells_name not set, but cell_count is set),
1416 * except when we're going to return the found node
1417 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001418 */
Stephen Warren91d99422013-08-14 15:27:11 -06001419 if (cells_name || cur_index == index) {
1420 node = of_find_node_by_phandle(phandle);
1421 if (!node) {
1422 pr_err("%s: could not find phandle\n",
1423 np->full_name);
1424 goto err;
1425 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001426 }
Stephen Warren035fd942013-08-14 15:27:10 -06001427
1428 if (cells_name) {
1429 if (of_property_read_u32(node, cells_name,
1430 &count)) {
1431 pr_err("%s: could not get %s for %s\n",
1432 np->full_name, cells_name,
1433 node->full_name);
1434 goto err;
1435 }
1436 } else {
1437 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001438 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001439
Grant Likely15c9a0a2011-12-12 09:25:57 -07001440 /*
1441 * Make sure that the arguments actually fit in the
1442 * remaining property data length
1443 */
1444 if (list + count > list_end) {
1445 pr_err("%s: arguments longer than property\n",
1446 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001447 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001448 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001449 }
1450
Grant Likely15c9a0a2011-12-12 09:25:57 -07001451 /*
1452 * All of the error cases above bail out of the loop, so at
1453 * this point, the parsing is successful. If the requested
1454 * index matches, then fill the out_args structure and return,
1455 * or return -ENOENT for an empty entry.
1456 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001457 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001458 if (cur_index == index) {
1459 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001460 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001461
Grant Likely15c9a0a2011-12-12 09:25:57 -07001462 if (out_args) {
1463 int i;
1464 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1465 count = MAX_PHANDLE_ARGS;
1466 out_args->np = node;
1467 out_args->args_count = count;
1468 for (i = 0; i < count; i++)
1469 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001470 } else {
1471 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001472 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001473
1474 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001475 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001476 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001477
1478 of_node_put(node);
1479 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001480 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001481 cur_index++;
1482 }
1483
Grant Likely23ce04c2013-02-12 21:21:49 +00001484 /*
1485 * Unlock node before returning result; will be one of:
1486 * -ENOENT : index is for empty phandle
1487 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001488 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001489 */
Grant Likelybd69f732013-02-10 22:57:21 +00001490 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001491 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001492 if (node)
1493 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001494 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001495}
Grant Likelybd69f732013-02-10 22:57:21 +00001496
Stephen Warreneded9dd2013-08-14 15:27:08 -06001497/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001498 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1499 * @np: Pointer to device node holding phandle property
1500 * @phandle_name: Name of property holding a phandle value
1501 * @index: For properties holding a table of phandles, this is the index into
1502 * the table
1503 *
1504 * Returns the device_node pointer with refcount incremented. Use
1505 * of_node_put() on it when done.
1506 */
1507struct device_node *of_parse_phandle(const struct device_node *np,
1508 const char *phandle_name, int index)
1509{
Stephen Warren91d99422013-08-14 15:27:11 -06001510 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001511
Stephen Warren91d99422013-08-14 15:27:11 -06001512 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001513 return NULL;
1514
Stephen Warren91d99422013-08-14 15:27:11 -06001515 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1516 index, &args))
1517 return NULL;
1518
1519 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001520}
1521EXPORT_SYMBOL(of_parse_phandle);
1522
1523/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001524 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1525 * @np: pointer to a device tree node containing a list
1526 * @list_name: property name that contains a list
1527 * @cells_name: property name that specifies phandles' arguments count
1528 * @index: index of a phandle to parse out
1529 * @out_args: optional pointer to output arguments structure (will be filled)
1530 *
1531 * This function is useful to parse lists of phandles and their arguments.
1532 * Returns 0 on success and fills out_args, on error returns appropriate
1533 * errno value.
1534 *
1535 * Caller is responsible to call of_node_put() on the returned out_args->node
1536 * pointer.
1537 *
1538 * Example:
1539 *
1540 * phandle1: node1 {
1541 * #list-cells = <2>;
1542 * }
1543 *
1544 * phandle2: node2 {
1545 * #list-cells = <1>;
1546 * }
1547 *
1548 * node3 {
1549 * list = <&phandle1 1 2 &phandle2 3>;
1550 * }
1551 *
1552 * To get a device_node of the `node2' node you may call this:
1553 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1554 */
Grant Likelybd69f732013-02-10 22:57:21 +00001555int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1556 const char *cells_name, int index,
1557 struct of_phandle_args *out_args)
1558{
1559 if (index < 0)
1560 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001561 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1562 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001563}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001564EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001565
Grant Likelybd69f732013-02-10 22:57:21 +00001566/**
Stephen Warren035fd942013-08-14 15:27:10 -06001567 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1568 * @np: pointer to a device tree node containing a list
1569 * @list_name: property name that contains a list
1570 * @cell_count: number of argument cells following the phandle
1571 * @index: index of a phandle to parse out
1572 * @out_args: optional pointer to output arguments structure (will be filled)
1573 *
1574 * This function is useful to parse lists of phandles and their arguments.
1575 * Returns 0 on success and fills out_args, on error returns appropriate
1576 * errno value.
1577 *
1578 * Caller is responsible to call of_node_put() on the returned out_args->node
1579 * pointer.
1580 *
1581 * Example:
1582 *
1583 * phandle1: node1 {
1584 * }
1585 *
1586 * phandle2: node2 {
1587 * }
1588 *
1589 * node3 {
1590 * list = <&phandle1 0 2 &phandle2 2 3>;
1591 * }
1592 *
1593 * To get a device_node of the `node2' node you may call this:
1594 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1595 */
1596int of_parse_phandle_with_fixed_args(const struct device_node *np,
1597 const char *list_name, int cell_count,
1598 int index, struct of_phandle_args *out_args)
1599{
1600 if (index < 0)
1601 return -EINVAL;
1602 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1603 index, out_args);
1604}
1605EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1606
1607/**
Grant Likelybd69f732013-02-10 22:57:21 +00001608 * of_count_phandle_with_args() - Find the number of phandles references in a property
1609 * @np: pointer to a device tree node containing a list
1610 * @list_name: property name that contains a list
1611 * @cells_name: property name that specifies phandles' arguments count
1612 *
1613 * Returns the number of phandle + argument tuples within a property. It
1614 * is a typical pattern to encode a list of phandle and variable
1615 * arguments into a single property. The number of arguments is encoded
1616 * by a property in the phandle-target node. For example, a gpios
1617 * property would contain a list of GPIO specifies consisting of a
1618 * phandle and 1 or more arguments. The number of arguments are
1619 * determined by the #gpio-cells property in the node pointed to by the
1620 * phandle.
1621 */
1622int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1623 const char *cells_name)
1624{
Stephen Warren035fd942013-08-14 15:27:10 -06001625 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1626 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001627}
1628EXPORT_SYMBOL(of_count_phandle_with_args);
1629
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001630#if defined(CONFIG_OF_DYNAMIC)
1631static int of_property_notify(int action, struct device_node *np,
1632 struct property *prop)
1633{
1634 struct of_prop_reconfig pr;
1635
1636 pr.dn = np;
1637 pr.prop = prop;
1638 return of_reconfig_notify(action, &pr);
1639}
1640#else
1641static int of_property_notify(int action, struct device_node *np,
1642 struct property *prop)
1643{
1644 return 0;
1645}
1646#endif
1647
Grant Likely02af11b2009-11-23 20:16:45 -07001648/**
Xiubo Li62664f62014-01-22 13:57:39 +08001649 * __of_add_property - Add a property to a node without lock operations
1650 */
1651static int __of_add_property(struct device_node *np, struct property *prop)
1652{
1653 struct property **next;
1654
1655 prop->next = NULL;
1656 next = &np->properties;
1657 while (*next) {
1658 if (strcmp(prop->name, (*next)->name) == 0)
1659 /* duplicate ! don't insert it */
1660 return -EEXIST;
1661
1662 next = &(*next)->next;
1663 }
1664 *next = prop;
1665
1666 return 0;
1667}
1668
1669/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001670 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001671 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001672int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001673{
Grant Likely02af11b2009-11-23 20:16:45 -07001674 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001675 int rc;
1676
1677 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1678 if (rc)
1679 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001680
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001681 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001682 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001683 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001684 if (rc)
1685 return rc;
1686
1687 /* at early boot, bail hear and defer setup to of_init() */
1688 if (!of_kset)
1689 return 0;
1690
1691 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001692
1693#ifdef CONFIG_PROC_DEVICETREE
1694 /* try to add to proc as well if it was initialized */
Xiubo Li62664f62014-01-22 13:57:39 +08001695 if (!rc && np->pde)
Grant Likely02af11b2009-11-23 20:16:45 -07001696 proc_device_tree_add_prop(np->pde, prop);
1697#endif /* CONFIG_PROC_DEVICETREE */
1698
Xiubo Li62664f62014-01-22 13:57:39 +08001699 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001700}
1701
1702/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001703 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001704 *
1705 * Note that we don't actually remove it, since we have given out
1706 * who-knows-how-many pointers to the data using get-property.
1707 * Instead we just move the property to the "dead properties"
1708 * list, so it won't be found any more.
1709 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001710int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001711{
1712 struct property **next;
1713 unsigned long flags;
1714 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001715 int rc;
1716
1717 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1718 if (rc)
1719 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001720
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001721 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001722 next = &np->properties;
1723 while (*next) {
1724 if (*next == prop) {
1725 /* found the node */
1726 *next = prop->next;
1727 prop->next = np->deadprops;
1728 np->deadprops = prop;
1729 found = 1;
1730 break;
1731 }
1732 next = &(*next)->next;
1733 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001734 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001735
1736 if (!found)
1737 return -ENODEV;
1738
Grant Likely75b57ec2014-02-20 18:02:11 +00001739 /* at early boot, bail hear and defer setup to of_init() */
1740 if (!of_kset)
1741 return 0;
1742
1743 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1744
Grant Likely02af11b2009-11-23 20:16:45 -07001745#ifdef CONFIG_PROC_DEVICETREE
1746 /* try to remove the proc node as well */
1747 if (np->pde)
1748 proc_device_tree_remove_prop(np->pde, prop);
1749#endif /* CONFIG_PROC_DEVICETREE */
1750
1751 return 0;
1752}
1753
1754/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001755 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001756 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001757 *
1758 * Note that we don't actually remove it, since we have given out
1759 * who-knows-how-many pointers to the data using get-property.
1760 * Instead we just move the property to the "dead properties" list,
1761 * and add the new property to the property list
1762 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001763int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001764{
Dong Aisheng475d0092012-07-11 15:16:37 +10001765 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001766 unsigned long flags;
Grant Likelya3dbeb52014-03-04 16:07:17 +08001767 int rc, found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001768
1769 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1770 if (rc)
1771 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001772
Dong Aisheng475d0092012-07-11 15:16:37 +10001773 if (!newprop->name)
1774 return -EINVAL;
1775
Grant Likelya3dbeb52014-03-04 16:07:17 +08001776 oldprop = of_find_property(np, newprop->name, NULL);
1777 if (!oldprop)
1778 return of_add_property(np, newprop);
1779
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001780 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelya3dbeb52014-03-04 16:07:17 +08001781 next = &np->properties;
1782 while (*next) {
1783 if (*next == oldprop) {
1784 /* found the node */
1785 newprop->next = oldprop->next;
1786 *next = newprop;
1787 oldprop->next = np->deadprops;
1788 np->deadprops = oldprop;
1789 found = 1;
1790 break;
1791 }
1792 next = &(*next)->next;
Grant Likely02af11b2009-11-23 20:16:45 -07001793 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001794 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001795 if (rc)
1796 return rc;
1797
1798 /* Update the sysfs attribute */
1799 if (oldprop)
1800 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1801 __of_add_property_sysfs(np, newprop);
Grant Likely02af11b2009-11-23 20:16:45 -07001802
Grant Likelya3dbeb52014-03-04 16:07:17 +08001803 if (!found)
1804 return -ENODEV;
1805
Grant Likely02af11b2009-11-23 20:16:45 -07001806#ifdef CONFIG_PROC_DEVICETREE
1807 /* try to add to proc as well if it was initialized */
Grant Likely75b57ec2014-02-20 18:02:11 +00001808 if (np->pde)
Grant Likely02af11b2009-11-23 20:16:45 -07001809 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1810#endif /* CONFIG_PROC_DEVICETREE */
1811
Grant Likelya3dbeb52014-03-04 16:07:17 +08001812 return 0;
Grant Likely02af11b2009-11-23 20:16:45 -07001813}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001814
1815#if defined(CONFIG_OF_DYNAMIC)
1816/*
1817 * Support for dynamic device trees.
1818 *
1819 * On some platforms, the device tree can be manipulated at runtime.
1820 * The routines in this section support adding, removing and changing
1821 * device tree nodes.
1822 */
1823
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001824static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1825
1826int of_reconfig_notifier_register(struct notifier_block *nb)
1827{
1828 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1829}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001830EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001831
1832int of_reconfig_notifier_unregister(struct notifier_block *nb)
1833{
1834 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1835}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001836EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001837
1838int of_reconfig_notify(unsigned long action, void *p)
1839{
1840 int rc;
1841
1842 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1843 return notifier_to_errno(rc);
1844}
1845
Nathan Fontenote81b3292012-10-02 16:55:01 +00001846#ifdef CONFIG_PROC_DEVICETREE
1847static void of_add_proc_dt_entry(struct device_node *dn)
1848{
1849 struct proc_dir_entry *ent;
1850
1851 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1852 if (ent)
1853 proc_device_tree_add_node(dn, ent);
1854}
1855#else
1856static void of_add_proc_dt_entry(struct device_node *dn)
1857{
1858 return;
1859}
1860#endif
1861
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001862/**
1863 * of_attach_node - Plug a device node into the tree and global list.
1864 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001865int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001866{
1867 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001868 int rc;
1869
1870 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1871 if (rc)
1872 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001873
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001874 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001875 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001876 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001877 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001878 of_allnodes = np;
Pantelis Antonioue3963fd2013-11-08 17:03:57 +02001879 of_node_clear_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001880 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001881
Grant Likely75b57ec2014-02-20 18:02:11 +00001882 of_node_add(np);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001883 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001884 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001885}
1886
Nathan Fontenote81b3292012-10-02 16:55:01 +00001887#ifdef CONFIG_PROC_DEVICETREE
1888static void of_remove_proc_dt_entry(struct device_node *dn)
1889{
David Howellsa8ca16e2013-04-12 17:27:28 +01001890 proc_remove(dn->pde);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001891}
1892#else
1893static void of_remove_proc_dt_entry(struct device_node *dn)
1894{
1895 return;
1896}
1897#endif
1898
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001899/**
1900 * of_detach_node - "Unplug" a node from the device tree.
1901 *
1902 * The caller must hold a reference to the node. The memory associated with
1903 * the node is not freed until its refcount goes to zero.
1904 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001905int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001906{
1907 struct device_node *parent;
1908 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001909 int rc = 0;
1910
1911 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1912 if (rc)
1913 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001914
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001915 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001916
Nathan Fontenote81b3292012-10-02 16:55:01 +00001917 if (of_node_check_flag(np, OF_DETACHED)) {
1918 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001919 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001920 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001921 }
1922
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001923 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001924 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001925 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001926 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001927 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001928
Randy Dunlap465aac62012-11-30 10:01:51 +00001929 if (of_allnodes == np)
1930 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001931 else {
1932 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001933 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001934 prev->allnext != np;
1935 prev = prev->allnext)
1936 ;
1937 prev->allnext = np->allnext;
1938 }
1939
1940 if (parent->child == np)
1941 parent->child = np->sibling;
1942 else {
1943 struct device_node *prevsib;
1944 for (prevsib = np->parent->child;
1945 prevsib->sibling != np;
1946 prevsib = prevsib->sibling)
1947 ;
1948 prevsib->sibling = np->sibling;
1949 }
1950
1951 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001952 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001953
1954 of_remove_proc_dt_entry(np);
Grant Likely75b57ec2014-02-20 18:02:11 +00001955 of_node_remove(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001956 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001957}
1958#endif /* defined(CONFIG_OF_DYNAMIC) */
1959
Shawn Guo611cad72011-08-15 15:28:14 +08001960static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1961 int id, const char *stem, int stem_len)
1962{
1963 ap->np = np;
1964 ap->id = id;
1965 strncpy(ap->stem, stem, stem_len);
1966 ap->stem[stem_len] = 0;
1967 list_add_tail(&ap->link, &aliases_lookup);
1968 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001969 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001970}
1971
1972/**
1973 * of_alias_scan - Scan all properties of 'aliases' node
1974 *
1975 * The function scans all the properties of 'aliases' node and populate
1976 * the the global lookup table with the properties. It returns the
1977 * number of alias_prop found, or error code in error case.
1978 *
1979 * @dt_alloc: An allocator that provides a virtual address to memory
1980 * for the resulting tree
1981 */
1982void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1983{
1984 struct property *pp;
1985
1986 of_chosen = of_find_node_by_path("/chosen");
1987 if (of_chosen == NULL)
1988 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001989
1990 if (of_chosen) {
1991 const char *name;
1992
1993 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1994 if (name)
1995 of_stdout = of_find_node_by_path(name);
1996 }
1997
Shawn Guo611cad72011-08-15 15:28:14 +08001998 of_aliases = of_find_node_by_path("/aliases");
1999 if (!of_aliases)
2000 return;
2001
Dong Aisheng8af0da92011-12-22 20:19:24 +08002002 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08002003 const char *start = pp->name;
2004 const char *end = start + strlen(start);
2005 struct device_node *np;
2006 struct alias_prop *ap;
2007 int id, len;
2008
2009 /* Skip those we do not want to proceed */
2010 if (!strcmp(pp->name, "name") ||
2011 !strcmp(pp->name, "phandle") ||
2012 !strcmp(pp->name, "linux,phandle"))
2013 continue;
2014
2015 np = of_find_node_by_path(pp->value);
2016 if (!np)
2017 continue;
2018
2019 /* walk the alias backwards to extract the id and work out
2020 * the 'stem' string */
2021 while (isdigit(*(end-1)) && end > start)
2022 end--;
2023 len = end - start;
2024
2025 if (kstrtoint(end, 10, &id) < 0)
2026 continue;
2027
2028 /* Allocate an alias_prop with enough space for the stem */
2029 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
2030 if (!ap)
2031 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01002032 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08002033 ap->alias = start;
2034 of_alias_add(ap, np, id, start, len);
2035 }
2036}
2037
2038/**
2039 * of_alias_get_id - Get alias id for the given device_node
2040 * @np: Pointer to the given device_node
2041 * @stem: Alias stem of the given device_node
2042 *
2043 * The function travels the lookup table to get alias id for the given
2044 * device_node and alias stem. It returns the alias id if find it.
2045 */
2046int of_alias_get_id(struct device_node *np, const char *stem)
2047{
2048 struct alias_prop *app;
2049 int id = -ENODEV;
2050
2051 mutex_lock(&of_aliases_mutex);
2052 list_for_each_entry(app, &aliases_lookup, link) {
2053 if (strcmp(app->stem, stem) != 0)
2054 continue;
2055
2056 if (np == app->np) {
2057 id = app->id;
2058 break;
2059 }
2060 }
2061 mutex_unlock(&of_aliases_mutex);
2062
2063 return id;
2064}
2065EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06002066
2067const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2068 u32 *pu)
2069{
2070 const void *curv = cur;
2071
2072 if (!prop)
2073 return NULL;
2074
2075 if (!cur) {
2076 curv = prop->value;
2077 goto out_val;
2078 }
2079
2080 curv += sizeof(*cur);
2081 if (curv >= prop->value + prop->length)
2082 return NULL;
2083
2084out_val:
2085 *pu = be32_to_cpup(curv);
2086 return curv;
2087}
2088EXPORT_SYMBOL_GPL(of_prop_next_u32);
2089
2090const char *of_prop_next_string(struct property *prop, const char *cur)
2091{
2092 const void *curv = cur;
2093
2094 if (!prop)
2095 return NULL;
2096
2097 if (!cur)
2098 return prop->value;
2099
2100 curv += strlen(cur) + 1;
2101 if (curv >= prop->value + prop->length)
2102 return NULL;
2103
2104 return curv;
2105}
2106EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002107
2108/**
2109 * of_device_is_stdout_path - check if a device node matches the
2110 * linux,stdout-path property
2111 *
2112 * Check if this device node matches the linux,stdout-path property
2113 * in the chosen node. return true if yes, false otherwise.
2114 */
2115int of_device_is_stdout_path(struct device_node *dn)
2116{
2117 if (!of_stdout)
2118 return false;
2119
2120 return of_stdout == dn;
2121}
2122EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002123
2124/**
2125 * of_find_next_cache_node - Find a node's subsidiary cache
2126 * @np: node of type "cpu" or "cache"
2127 *
2128 * Returns a node pointer with refcount incremented, use
2129 * of_node_put() on it when done. Caller should hold a reference
2130 * to np.
2131 */
2132struct device_node *of_find_next_cache_node(const struct device_node *np)
2133{
2134 struct device_node *child;
2135 const phandle *handle;
2136
2137 handle = of_get_property(np, "l2-cache", NULL);
2138 if (!handle)
2139 handle = of_get_property(np, "next-level-cache", NULL);
2140
2141 if (handle)
2142 return of_find_node_by_phandle(be32_to_cpup(handle));
2143
2144 /* OF on pmac has nodes instead of properties named "l2-cache"
2145 * beneath CPU nodes.
2146 */
2147 if (!strcmp(np->type, "cpu"))
2148 for_each_child_of_node(np, child)
2149 if (!strcmp(child->type, "cache"))
2150 return child;
2151
2152 return NULL;
2153}