blob: ed3e70b84957b3ddbd24db41cfbdab3df31890f2 [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
Grant Likely83570412012-11-06 21:03:27 +0000284 /* Symlink in /proc as required by userspace ABI */
Grant Likely75b57ec2014-02-20 18:02:11 +0000285 if (of_allnodes)
286 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000287
288 return 0;
289}
290core_initcall(of_init);
291
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500292static struct property *__of_find_property(const struct device_node *np,
293 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000294{
295 struct property *pp;
296
Timur Tabi64e45662008-05-08 05:19:59 +1000297 if (!np)
298 return NULL;
299
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530300 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000301 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530302 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000303 *lenp = pp->length;
304 break;
305 }
306 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500307
308 return pp;
309}
310
311struct property *of_find_property(const struct device_node *np,
312 const char *name,
313 int *lenp)
314{
315 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500316 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500317
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500318 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500319 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500320 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000321
322 return pp;
323}
324EXPORT_SYMBOL(of_find_property);
325
Grant Likelye91edcf2009-10-15 10:58:09 -0600326/**
327 * of_find_all_nodes - Get next node in global list
328 * @prev: Previous node or NULL to start iteration
329 * of_node_put() will be called on it
330 *
331 * Returns a node pointer with refcount incremented, use
332 * of_node_put() on it when done.
333 */
334struct device_node *of_find_all_nodes(struct device_node *prev)
335{
336 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000337 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600338
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000339 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000340 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600341 for (; np != NULL; np = np->allnext)
342 if (of_node_get(np))
343 break;
344 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000345 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600346 return np;
347}
348EXPORT_SYMBOL(of_find_all_nodes);
349
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000350/*
351 * Find a property with a given name for a given node
352 * and return the value.
353 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500354static const void *__of_get_property(const struct device_node *np,
355 const char *name, int *lenp)
356{
357 struct property *pp = __of_find_property(np, name, lenp);
358
359 return pp ? pp->value : NULL;
360}
361
362/*
363 * Find a property with a given name for a given node
364 * and return the value.
365 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000366const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500367 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000368{
369 struct property *pp = of_find_property(np, name, lenp);
370
371 return pp ? pp->value : NULL;
372}
373EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000374
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100375/*
376 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
377 *
378 * @cpu: logical cpu index of a core/thread
379 * @phys_id: physical identifier of a core/thread
380 *
381 * CPU logical to physical index mapping is architecture specific.
382 * However this __weak function provides a default match of physical
383 * id to logical cpu index. phys_id provided here is usually values read
384 * from the device tree which must match the hardware internal registers.
385 *
386 * Returns true if the physical identifier and the logical cpu index
387 * correspond to the same core/thread, false otherwise.
388 */
389bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
390{
391 return (u32)phys_id == cpu;
392}
393
394/**
395 * Checks if the given "prop_name" property holds the physical id of the
396 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
397 * NULL, local thread number within the core is returned in it.
398 */
399static bool __of_find_n_match_cpu_property(struct device_node *cpun,
400 const char *prop_name, int cpu, unsigned int *thread)
401{
402 const __be32 *cell;
403 int ac, prop_len, tid;
404 u64 hwid;
405
406 ac = of_n_addr_cells(cpun);
407 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100408 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100409 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100410 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100411 for (tid = 0; tid < prop_len; tid++) {
412 hwid = of_read_number(cell, ac);
413 if (arch_match_cpu_phys_id(cpu, hwid)) {
414 if (thread)
415 *thread = tid;
416 return true;
417 }
418 cell += ac;
419 }
420 return false;
421}
422
David Millerd1cb9d12013-10-03 17:24:51 -0400423/*
424 * arch_find_n_match_cpu_physical_id - See if the given device node is
425 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
426 * else false. If 'thread' is non-NULL, the local thread number within the
427 * core is returned in it.
428 */
429bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
430 int cpu, unsigned int *thread)
431{
432 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
433 * for thread ids on PowerPC. If it doesn't exist fallback to
434 * standard "reg" property.
435 */
436 if (IS_ENABLED(CONFIG_PPC) &&
437 __of_find_n_match_cpu_property(cpun,
438 "ibm,ppc-interrupt-server#s",
439 cpu, thread))
440 return true;
441
442 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
443 return true;
444
445 return false;
446}
447
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100448/**
449 * of_get_cpu_node - Get device node associated with the given logical CPU
450 *
451 * @cpu: CPU number(logical index) for which device node is required
452 * @thread: if not NULL, local thread number within the physical core is
453 * returned
454 *
455 * The main purpose of this function is to retrieve the device node for the
456 * given logical CPU index. It should be used to initialize the of_node in
457 * cpu device. Once of_node in cpu device is populated, all the further
458 * references can use that instead.
459 *
460 * CPU logical to physical index mapping is architecture specific and is built
461 * before booting secondary cores. This function uses arch_match_cpu_phys_id
462 * which can be overridden by architecture specific implementation.
463 *
464 * Returns a node pointer for the logical cpu if found, else NULL.
465 */
466struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
467{
David Millerd1cb9d12013-10-03 17:24:51 -0400468 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100469
David Millerd1cb9d12013-10-03 17:24:51 -0400470 for_each_node_by_type(cpun, "cpu") {
471 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100472 return cpun;
473 }
474 return NULL;
475}
476EXPORT_SYMBOL(of_get_cpu_node);
477
Kevin Hao215a14c2014-02-19 16:15:45 +0800478/**
479 * __of_device_is_compatible() - Check if the node matches given constraints
480 * @device: pointer to node
481 * @compat: required compatible string, NULL or "" for any match
482 * @type: required device_type value, NULL or "" for any match
483 * @name: required node name, NULL or "" for any match
484 *
485 * Checks if the given @compat, @type and @name strings match the
486 * properties of the given @device. A constraints can be skipped by
487 * passing NULL or an empty string as the constraint.
488 *
489 * Returns 0 for no match, and a positive integer on match. The return
490 * value is a relative score with larger values indicating better
491 * matches. The score is weighted for the most specific compatible value
492 * to get the highest score. Matching type is next, followed by matching
493 * name. Practically speaking, this results in the following priority
494 * order for matches:
495 *
496 * 1. specific compatible && type && name
497 * 2. specific compatible && type
498 * 3. specific compatible && name
499 * 4. specific compatible
500 * 5. general compatible && type && name
501 * 6. general compatible && type
502 * 7. general compatible && name
503 * 8. general compatible
504 * 9. type && name
505 * 10. type
506 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000507 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500508static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800509 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000510{
Kevin Hao215a14c2014-02-19 16:15:45 +0800511 struct property *prop;
512 const char *cp;
513 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000514
Kevin Hao215a14c2014-02-19 16:15:45 +0800515 /* Compatible match has highest priority */
516 if (compat && compat[0]) {
517 prop = __of_find_property(device, "compatible", NULL);
518 for (cp = of_prop_next_string(prop, NULL); cp;
519 cp = of_prop_next_string(prop, cp), index++) {
520 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
521 score = INT_MAX/2 - (index << 2);
522 break;
523 }
524 }
525 if (!score)
526 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000527 }
528
Kevin Hao215a14c2014-02-19 16:15:45 +0800529 /* Matching type is better than matching name */
530 if (type && type[0]) {
531 if (!device->type || of_node_cmp(type, device->type))
532 return 0;
533 score += 2;
534 }
535
536 /* Matching name is a bit better than not */
537 if (name && name[0]) {
538 if (!device->name || of_node_cmp(name, device->name))
539 return 0;
540 score++;
541 }
542
543 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000544}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500545
546/** Checks if the given "compat" string matches one of the strings in
547 * the device's "compatible" property
548 */
549int of_device_is_compatible(const struct device_node *device,
550 const char *compat)
551{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500552 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500553 int res;
554
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500555 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800556 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500557 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500558 return res;
559}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000560EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000561
562/**
Grant Likely71a157e2010-02-01 21:34:14 -0700563 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700564 * @compat: compatible string to look for in root node's compatible property.
565 *
566 * Returns true if the root node has the given value in its
567 * compatible property.
568 */
Grant Likely71a157e2010-02-01 21:34:14 -0700569int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700570{
571 struct device_node *root;
572 int rc = 0;
573
574 root = of_find_node_by_path("/");
575 if (root) {
576 rc = of_device_is_compatible(root, compat);
577 of_node_put(root);
578 }
579 return rc;
580}
Grant Likely71a157e2010-02-01 21:34:14 -0700581EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700582
583/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700584 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100585 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700586 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100587 *
588 * Returns 1 if the status property is absent or set to "okay" or "ok",
589 * 0 otherwise
590 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700591static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100592{
593 const char *status;
594 int statlen;
595
Xiubo Li42ccd782014-01-13 11:07:28 +0800596 if (!device)
597 return 0;
598
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700599 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100600 if (status == NULL)
601 return 1;
602
603 if (statlen > 0) {
604 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
605 return 1;
606 }
607
608 return 0;
609}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700610
611/**
612 * of_device_is_available - check if a device is available for use
613 *
614 * @device: Node to check for availability
615 *
616 * Returns 1 if the status property is absent or set to "okay" or "ok",
617 * 0 otherwise
618 */
619int of_device_is_available(const struct device_node *device)
620{
621 unsigned long flags;
622 int res;
623
624 raw_spin_lock_irqsave(&devtree_lock, flags);
625 res = __of_device_is_available(device);
626 raw_spin_unlock_irqrestore(&devtree_lock, flags);
627 return res;
628
629}
Josh Boyer834d97d2008-03-27 00:33:14 +1100630EXPORT_SYMBOL(of_device_is_available);
631
632/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000633 * of_get_parent - Get a node's parent if any
634 * @node: Node to get parent
635 *
636 * Returns a node pointer with refcount incremented, use
637 * of_node_put() on it when done.
638 */
639struct device_node *of_get_parent(const struct device_node *node)
640{
641 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500642 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000643
644 if (!node)
645 return NULL;
646
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500647 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000648 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500649 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000650 return np;
651}
652EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000653
654/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000655 * of_get_next_parent - Iterate to a node's parent
656 * @node: Node to get parent of
657 *
658 * This is like of_get_parent() except that it drops the
659 * refcount on the passed node, making it suitable for iterating
660 * through a node's parents.
661 *
662 * Returns a node pointer with refcount incremented, use
663 * of_node_put() on it when done.
664 */
665struct device_node *of_get_next_parent(struct device_node *node)
666{
667 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500668 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000669
670 if (!node)
671 return NULL;
672
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500673 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000674 parent = of_node_get(node->parent);
675 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500676 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000677 return parent;
678}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300679EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000680
681/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000682 * of_get_next_child - Iterate a node childs
683 * @node: parent node
684 * @prev: previous child of the parent node, or NULL to get first
685 *
686 * Returns a node pointer with refcount incremented, use
687 * of_node_put() on it when done.
688 */
689struct device_node *of_get_next_child(const struct device_node *node,
690 struct device_node *prev)
691{
692 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500693 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000694
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500695 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000696 next = prev ? prev->sibling : node->child;
697 for (; next; next = next->sibling)
698 if (of_node_get(next))
699 break;
700 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500701 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000702 return next;
703}
704EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000705
706/**
Timur Tabi32961932012-08-14 13:20:23 +0000707 * of_get_next_available_child - Find the next available child node
708 * @node: parent node
709 * @prev: previous child of the parent node, or NULL to get first
710 *
711 * This function is like of_get_next_child(), except that it
712 * automatically skips any disabled nodes (i.e. status = "disabled").
713 */
714struct device_node *of_get_next_available_child(const struct device_node *node,
715 struct device_node *prev)
716{
717 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000718 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000719
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000720 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000721 next = prev ? prev->sibling : node->child;
722 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700723 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000724 continue;
725 if (of_node_get(next))
726 break;
727 }
728 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000729 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000730 return next;
731}
732EXPORT_SYMBOL(of_get_next_available_child);
733
734/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100735 * of_get_child_by_name - Find the child node by name for a given parent
736 * @node: parent node
737 * @name: child name to look for.
738 *
739 * This function looks for child node for given matching name
740 *
741 * Returns a node pointer if found, with refcount incremented, use
742 * of_node_put() on it when done.
743 * Returns NULL if node is not found.
744 */
745struct device_node *of_get_child_by_name(const struct device_node *node,
746 const char *name)
747{
748 struct device_node *child;
749
750 for_each_child_of_node(node, child)
751 if (child->name && (of_node_cmp(child->name, name) == 0))
752 break;
753 return child;
754}
755EXPORT_SYMBOL(of_get_child_by_name);
756
757/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000758 * of_find_node_by_path - Find a node matching a full OF path
759 * @path: The full path to match
760 *
761 * Returns a node pointer with refcount incremented, use
762 * of_node_put() on it when done.
763 */
764struct device_node *of_find_node_by_path(const char *path)
765{
Randy Dunlap465aac62012-11-30 10:01:51 +0000766 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500767 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000768
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500769 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000770 for (; np; np = np->allnext) {
771 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
772 && of_node_get(np))
773 break;
774 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500775 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000776 return np;
777}
778EXPORT_SYMBOL(of_find_node_by_path);
779
780/**
781 * of_find_node_by_name - Find a node by its "name" property
782 * @from: The node to start searching from or NULL, the node
783 * you pass will not be searched, only the next one
784 * will; typically, you pass what the previous call
785 * returned. of_node_put() will be called on it
786 * @name: The name string to match against
787 *
788 * Returns a node pointer with refcount incremented, use
789 * of_node_put() on it when done.
790 */
791struct device_node *of_find_node_by_name(struct device_node *from,
792 const char *name)
793{
794 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500795 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000796
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500797 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000798 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000799 for (; np; np = np->allnext)
800 if (np->name && (of_node_cmp(np->name, name) == 0)
801 && of_node_get(np))
802 break;
803 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500804 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000805 return np;
806}
807EXPORT_SYMBOL(of_find_node_by_name);
808
809/**
810 * of_find_node_by_type - Find a node by its "device_type" property
811 * @from: The node to start searching from, or NULL to start searching
812 * the entire device tree. The node you pass will not be
813 * searched, only the next one will; typically, you pass
814 * what the previous call returned. of_node_put() will be
815 * called on from for you.
816 * @type: The type string to match against
817 *
818 * Returns a node pointer with refcount incremented, use
819 * of_node_put() on it when done.
820 */
821struct device_node *of_find_node_by_type(struct device_node *from,
822 const char *type)
823{
824 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500825 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000826
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500827 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000828 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000829 for (; np; np = np->allnext)
830 if (np->type && (of_node_cmp(np->type, type) == 0)
831 && of_node_get(np))
832 break;
833 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500834 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000835 return np;
836}
837EXPORT_SYMBOL(of_find_node_by_type);
838
839/**
840 * of_find_compatible_node - Find a node based on type and one of the
841 * tokens in its "compatible" property
842 * @from: The node to start searching from or NULL, the node
843 * you pass will not be searched, only the next one
844 * will; typically, you pass what the previous call
845 * returned. of_node_put() will be called on it
846 * @type: The type string to match "device_type" or NULL to ignore
847 * @compatible: The string to match to one of the tokens in the device
848 * "compatible" list.
849 *
850 * Returns a node pointer with refcount incremented, use
851 * of_node_put() on it when done.
852 */
853struct device_node *of_find_compatible_node(struct device_node *from,
854 const char *type, const char *compatible)
855{
856 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500857 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000858
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500859 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000860 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000861 for (; np; np = np->allnext) {
Kevin Hao215a14c2014-02-19 16:15:45 +0800862 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500863 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000864 break;
865 }
866 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500867 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000868 return np;
869}
870EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100871
872/**
Michael Ellerman1e291b142008-11-12 18:54:42 +0000873 * of_find_node_with_property - Find a node which has a property with
874 * the given name.
875 * @from: The node to start searching from or NULL, the node
876 * you pass will not be searched, only the next one
877 * will; typically, you pass what the previous call
878 * returned. of_node_put() will be called on it
879 * @prop_name: The name of the property to look for.
880 *
881 * Returns a node pointer with refcount incremented, use
882 * of_node_put() on it when done.
883 */
884struct device_node *of_find_node_with_property(struct device_node *from,
885 const char *prop_name)
886{
887 struct device_node *np;
888 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500889 unsigned long flags;
Michael Ellerman1e291b142008-11-12 18:54:42 +0000890
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500891 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000892 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b142008-11-12 18:54:42 +0000893 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530894 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b142008-11-12 18:54:42 +0000895 if (of_prop_cmp(pp->name, prop_name) == 0) {
896 of_node_get(np);
897 goto out;
898 }
899 }
900 }
901out:
902 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500903 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000904 return np;
905}
906EXPORT_SYMBOL(of_find_node_with_property);
907
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500908static
909const struct of_device_id *__of_match_node(const struct of_device_id *matches,
910 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100911{
Kevin Hao215a14c2014-02-19 16:15:45 +0800912 const struct of_device_id *best_match = NULL;
913 int score, best_score = 0;
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100914
Grant Likelya52f07e2011-03-18 10:21:29 -0600915 if (!matches)
916 return NULL;
917
Kevin Hao215a14c2014-02-19 16:15:45 +0800918 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
919 score = __of_device_is_compatible(node, matches->compatible,
920 matches->type, matches->name);
921 if (score > best_score) {
922 best_match = matches;
923 best_score = score;
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100924 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +0800925 }
Sebastian Hesselbarth10535312013-12-03 14:52:00 +0100926
Kevin Hao215a14c2014-02-19 16:15:45 +0800927 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +1100928}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500929
930/**
931 * of_match_node - Tell if an device_node has a matching of_match structure
932 * @matches: array of of device match structures to search in
933 * @node: the of device structure to match against
934 *
Kevin Hao71c5498e2014-02-18 15:57:29 +0800935 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500936 */
937const struct of_device_id *of_match_node(const struct of_device_id *matches,
938 const struct device_node *node)
939{
940 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500941 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500942
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500943 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500944 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500945 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500946 return match;
947}
Grant Likely283029d2008-01-09 06:20:40 +1100948EXPORT_SYMBOL(of_match_node);
949
950/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700951 * of_find_matching_node_and_match - Find a node based on an of_device_id
952 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100953 * @from: The node to start searching from or NULL, the node
954 * you pass will not be searched, only the next one
955 * will; typically, you pass what the previous call
956 * returned. of_node_put() will be called on it
957 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700958 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100959 *
960 * Returns a node pointer with refcount incremented, use
961 * of_node_put() on it when done.
962 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700963struct device_node *of_find_matching_node_and_match(struct device_node *from,
964 const struct of_device_id *matches,
965 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100966{
967 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800968 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500969 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100970
Stephen Warren50c8af42012-11-20 16:12:20 -0700971 if (match)
972 *match = NULL;
973
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500974 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000975 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100976 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500977 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800978 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700979 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800980 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100981 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700982 }
Grant Likely283029d2008-01-09 06:20:40 +1100983 }
984 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500985 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100986 return np;
987}
Grant Likely80c20222012-12-19 10:45:36 +0000988EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400989
990/**
Grant Likely3f07af42008-07-25 22:25:13 -0400991 * of_modalias_node - Lookup appropriate modalias for a device node
992 * @node: pointer to a device tree node
993 * @modalias: Pointer to buffer that modalias value will be copied into
994 * @len: Length of modalias value
995 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600996 * Based on the value of the compatible property, this routine will attempt
997 * to choose an appropriate modalias value for a particular device tree node.
998 * It does this by stripping the manufacturer prefix (as delimited by a ',')
999 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001000 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001001 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001002 */
1003int of_modalias_node(struct device_node *node, char *modalias, int len)
1004{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001005 const char *compatible, *p;
1006 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001007
1008 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001009 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001010 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001011 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001012 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001013 return 0;
1014}
1015EXPORT_SYMBOL_GPL(of_modalias_node);
1016
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001017/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001018 * of_find_node_by_phandle - Find a node given a phandle
1019 * @handle: phandle of the node to find
1020 *
1021 * Returns a node pointer with refcount incremented, use
1022 * of_node_put() on it when done.
1023 */
1024struct device_node *of_find_node_by_phandle(phandle handle)
1025{
1026 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001027 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001028
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001029 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001030 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001031 if (np->phandle == handle)
1032 break;
1033 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001034 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001035 return np;
1036}
1037EXPORT_SYMBOL(of_find_node_by_phandle);
1038
1039/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001040 * of_find_property_value_of_size
1041 *
1042 * @np: device node from which the property value is to be read.
1043 * @propname: name of the property to be searched.
1044 * @len: requested length of property value
1045 *
1046 * Search for a property in a device node and valid the requested size.
1047 * Returns the property value on success, -EINVAL if the property does not
1048 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1049 * property data isn't large enough.
1050 *
1051 */
1052static void *of_find_property_value_of_size(const struct device_node *np,
1053 const char *propname, u32 len)
1054{
1055 struct property *prop = of_find_property(np, propname, NULL);
1056
1057 if (!prop)
1058 return ERR_PTR(-EINVAL);
1059 if (!prop->value)
1060 return ERR_PTR(-ENODATA);
1061 if (len > prop->length)
1062 return ERR_PTR(-EOVERFLOW);
1063
1064 return prop->value;
1065}
1066
1067/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001068 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1069 *
1070 * @np: device node from which the property value is to be read.
1071 * @propname: name of the property to be searched.
1072 * @index: index of the u32 in the list of values
1073 * @out_value: pointer to return value, modified only if no error.
1074 *
1075 * Search for a property in a device node and read nth 32-bit value from
1076 * it. Returns 0 on success, -EINVAL if the property does not exist,
1077 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1078 * property data isn't large enough.
1079 *
1080 * The out_value is modified only if a valid u32 value can be decoded.
1081 */
1082int of_property_read_u32_index(const struct device_node *np,
1083 const char *propname,
1084 u32 index, u32 *out_value)
1085{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001086 const u32 *val = of_find_property_value_of_size(np, propname,
1087 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +13001088
Tony Priskdaeec1f2013-04-03 17:57:11 +13001089 if (IS_ERR(val))
1090 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001091
Tony Priskdaeec1f2013-04-03 17:57:11 +13001092 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001093 return 0;
1094}
1095EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1096
1097/**
Viresh Kumarbe193242012-11-20 10:15:19 +05301098 * of_property_read_u8_array - Find and read an array of u8 from a property.
1099 *
1100 * @np: device node from which the property value is to be read.
1101 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301102 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301103 * @sz: number of array elements to read
1104 *
1105 * Search for a property in a device node and read 8-bit value(s) from
1106 * it. Returns 0 on success, -EINVAL if the property does not exist,
1107 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1108 * property data isn't large enough.
1109 *
1110 * dts entry of array should be like:
1111 * property = /bits/ 8 <0x50 0x60 0x70>;
1112 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301113 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301114 */
1115int of_property_read_u8_array(const struct device_node *np,
1116 const char *propname, u8 *out_values, size_t sz)
1117{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001118 const u8 *val = of_find_property_value_of_size(np, propname,
1119 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301120
Tony Priskdaeec1f2013-04-03 17:57:11 +13001121 if (IS_ERR(val))
1122 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301123
Viresh Kumarbe193242012-11-20 10:15:19 +05301124 while (sz--)
1125 *out_values++ = *val++;
1126 return 0;
1127}
1128EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1129
1130/**
1131 * of_property_read_u16_array - Find and read an array of u16 from a property.
1132 *
1133 * @np: device node from which the property value is to be read.
1134 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301135 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301136 * @sz: number of array elements to read
1137 *
1138 * Search for a property in a device node and read 16-bit value(s) from
1139 * it. Returns 0 on success, -EINVAL if the property does not exist,
1140 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1141 * property data isn't large enough.
1142 *
1143 * dts entry of array should be like:
1144 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1145 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301146 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301147 */
1148int of_property_read_u16_array(const struct device_node *np,
1149 const char *propname, u16 *out_values, size_t sz)
1150{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001151 const __be16 *val = of_find_property_value_of_size(np, propname,
1152 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301153
Tony Priskdaeec1f2013-04-03 17:57:11 +13001154 if (IS_ERR(val))
1155 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301156
Viresh Kumarbe193242012-11-20 10:15:19 +05301157 while (sz--)
1158 *out_values++ = be16_to_cpup(val++);
1159 return 0;
1160}
1161EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1162
1163/**
Rob Herring0e373632011-07-06 15:42:58 -05001164 * of_property_read_u32_array - Find and read an array of 32 bit integers
1165 * from a property.
1166 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301167 * @np: device node from which the property value is to be read.
1168 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301169 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301170 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301171 *
Rob Herring0e373632011-07-06 15:42:58 -05001172 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301173 * it. Returns 0 on success, -EINVAL if the property does not exist,
1174 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1175 * property data isn't large enough.
1176 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301177 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301178 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001179int of_property_read_u32_array(const struct device_node *np,
1180 const char *propname, u32 *out_values,
1181 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301182{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001183 const __be32 *val = of_find_property_value_of_size(np, propname,
1184 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301185
Tony Priskdaeec1f2013-04-03 17:57:11 +13001186 if (IS_ERR(val))
1187 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001188
Rob Herring0e373632011-07-06 15:42:58 -05001189 while (sz--)
1190 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301191 return 0;
1192}
Rob Herring0e373632011-07-06 15:42:58 -05001193EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301194
1195/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001196 * of_property_read_u64 - Find and read a 64 bit integer from a property
1197 * @np: device node from which the property value is to be read.
1198 * @propname: name of the property to be searched.
1199 * @out_value: pointer to return value, modified only if return value is 0.
1200 *
1201 * Search for a property in a device node and read a 64-bit value from
1202 * it. Returns 0 on success, -EINVAL if the property does not exist,
1203 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1204 * property data isn't large enough.
1205 *
1206 * The out_value is modified only if a valid u64 value can be decoded.
1207 */
1208int of_property_read_u64(const struct device_node *np, const char *propname,
1209 u64 *out_value)
1210{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001211 const __be32 *val = of_find_property_value_of_size(np, propname,
1212 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001213
Tony Priskdaeec1f2013-04-03 17:57:11 +13001214 if (IS_ERR(val))
1215 return PTR_ERR(val);
1216
1217 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001218 return 0;
1219}
1220EXPORT_SYMBOL_GPL(of_property_read_u64);
1221
1222/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301223 * of_property_read_string - Find and read a string from a property
1224 * @np: device node from which the property value is to be read.
1225 * @propname: name of the property to be searched.
1226 * @out_string: pointer to null terminated return string, modified only if
1227 * return value is 0.
1228 *
1229 * Search for a property in a device tree node and retrieve a null
1230 * terminated string value (pointer to data, not a copy). Returns 0 on
1231 * success, -EINVAL if the property does not exist, -ENODATA if property
1232 * does not have a value, and -EILSEQ if the string is not null-terminated
1233 * within the length of the property data.
1234 *
1235 * The out_string pointer is modified only if a valid string can be decoded.
1236 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001237int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001238 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301239{
1240 struct property *prop = of_find_property(np, propname, NULL);
1241 if (!prop)
1242 return -EINVAL;
1243 if (!prop->value)
1244 return -ENODATA;
1245 if (strnlen(prop->value, prop->length) >= prop->length)
1246 return -EILSEQ;
1247 *out_string = prop->value;
1248 return 0;
1249}
1250EXPORT_SYMBOL_GPL(of_property_read_string);
1251
1252/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001253 * of_property_read_string_index - Find and read a string from a multiple
1254 * strings property.
1255 * @np: device node from which the property value is to be read.
1256 * @propname: name of the property to be searched.
1257 * @index: index of the string in the list of strings
1258 * @out_string: pointer to null terminated return string, modified only if
1259 * return value is 0.
1260 *
1261 * Search for a property in a device tree node and retrieve a null
1262 * terminated string value (pointer to data, not a copy) in the list of strings
1263 * contained in that property.
1264 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1265 * property does not have a value, and -EILSEQ if the string is not
1266 * null-terminated within the length of the property data.
1267 *
1268 * The out_string pointer is modified only if a valid string can be decoded.
1269 */
1270int of_property_read_string_index(struct device_node *np, const char *propname,
1271 int index, const char **output)
1272{
1273 struct property *prop = of_find_property(np, propname, NULL);
1274 int i = 0;
1275 size_t l = 0, total = 0;
1276 const char *p;
1277
1278 if (!prop)
1279 return -EINVAL;
1280 if (!prop->value)
1281 return -ENODATA;
1282 if (strnlen(prop->value, prop->length) >= prop->length)
1283 return -EILSEQ;
1284
1285 p = prop->value;
1286
1287 for (i = 0; total < prop->length; total += l, p += l) {
1288 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001289 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001290 *output = p;
1291 return 0;
1292 }
1293 }
1294 return -ENODATA;
1295}
1296EXPORT_SYMBOL_GPL(of_property_read_string_index);
1297
Grant Likely7aff0fe2011-12-12 09:25:58 -07001298/**
1299 * of_property_match_string() - Find string in a list and return index
1300 * @np: pointer to node containing string list property
1301 * @propname: string list property name
1302 * @string: pointer to string to search for in string list
1303 *
1304 * This function searches a string list property and returns the index
1305 * of a specific string value.
1306 */
1307int of_property_match_string(struct device_node *np, const char *propname,
1308 const char *string)
1309{
1310 struct property *prop = of_find_property(np, propname, NULL);
1311 size_t l;
1312 int i;
1313 const char *p, *end;
1314
1315 if (!prop)
1316 return -EINVAL;
1317 if (!prop->value)
1318 return -ENODATA;
1319
1320 p = prop->value;
1321 end = p + prop->length;
1322
1323 for (i = 0; p < end; i++, p += l) {
1324 l = strlen(p) + 1;
1325 if (p + l > end)
1326 return -EILSEQ;
1327 pr_debug("comparing %s with %s\n", string, p);
1328 if (strcmp(string, p) == 0)
1329 return i; /* Found it; return index */
1330 }
1331 return -ENODATA;
1332}
1333EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001334
1335/**
1336 * of_property_count_strings - Find and return the number of strings from a
1337 * multiple strings property.
1338 * @np: device node from which the property value is to be read.
1339 * @propname: name of the property to be searched.
1340 *
1341 * Search for a property in a device tree node and retrieve the number of null
1342 * terminated string contain in it. Returns the number of strings on
1343 * success, -EINVAL if the property does not exist, -ENODATA if property
1344 * does not have a value, and -EILSEQ if the string is not null-terminated
1345 * within the length of the property data.
1346 */
1347int of_property_count_strings(struct device_node *np, const char *propname)
1348{
1349 struct property *prop = of_find_property(np, propname, NULL);
1350 int i = 0;
1351 size_t l = 0, total = 0;
1352 const char *p;
1353
1354 if (!prop)
1355 return -EINVAL;
1356 if (!prop->value)
1357 return -ENODATA;
1358 if (strnlen(prop->value, prop->length) >= prop->length)
1359 return -EILSEQ;
1360
1361 p = prop->value;
1362
Benoit Cousson88af7f52011-12-05 15:23:54 +01001363 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001364 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001365
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001366 return i;
1367}
1368EXPORT_SYMBOL_GPL(of_property_count_strings);
1369
Grant Likely624cfca2013-10-11 22:05:10 +01001370void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1371{
1372 int i;
1373 printk("%s %s", msg, of_node_full_name(args->np));
1374 for (i = 0; i < args->args_count; i++)
1375 printk(i ? ",%08x" : ":%08x", args->args[i]);
1376 printk("\n");
1377}
1378
Grant Likelybd69f732013-02-10 22:57:21 +00001379static int __of_parse_phandle_with_args(const struct device_node *np,
1380 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001381 const char *cells_name,
1382 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001383 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001384{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001385 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001386 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001387 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001388 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001389 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001390
Grant Likely15c9a0a2011-12-12 09:25:57 -07001391 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001392 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001393 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001394 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001395 list_end = list + size / sizeof(*list);
1396
Grant Likely15c9a0a2011-12-12 09:25:57 -07001397 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001398 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001399 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001400 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001401
Grant Likely15c9a0a2011-12-12 09:25:57 -07001402 /*
1403 * If phandle is 0, then it is an empty entry with no
1404 * arguments. Skip forward to the next entry.
1405 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001406 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001407 if (phandle) {
1408 /*
1409 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001410 * property to determine the argument length.
1411 *
1412 * This is not needed if the cell count is hard-coded
1413 * (i.e. cells_name not set, but cell_count is set),
1414 * except when we're going to return the found node
1415 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001416 */
Stephen Warren91d99422013-08-14 15:27:11 -06001417 if (cells_name || cur_index == index) {
1418 node = of_find_node_by_phandle(phandle);
1419 if (!node) {
1420 pr_err("%s: could not find phandle\n",
1421 np->full_name);
1422 goto err;
1423 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001424 }
Stephen Warren035fd942013-08-14 15:27:10 -06001425
1426 if (cells_name) {
1427 if (of_property_read_u32(node, cells_name,
1428 &count)) {
1429 pr_err("%s: could not get %s for %s\n",
1430 np->full_name, cells_name,
1431 node->full_name);
1432 goto err;
1433 }
1434 } else {
1435 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001436 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001437
Grant Likely15c9a0a2011-12-12 09:25:57 -07001438 /*
1439 * Make sure that the arguments actually fit in the
1440 * remaining property data length
1441 */
1442 if (list + count > list_end) {
1443 pr_err("%s: arguments longer than property\n",
1444 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001445 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001446 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001447 }
1448
Grant Likely15c9a0a2011-12-12 09:25:57 -07001449 /*
1450 * All of the error cases above bail out of the loop, so at
1451 * this point, the parsing is successful. If the requested
1452 * index matches, then fill the out_args structure and return,
1453 * or return -ENOENT for an empty entry.
1454 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001455 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001456 if (cur_index == index) {
1457 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001458 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001459
Grant Likely15c9a0a2011-12-12 09:25:57 -07001460 if (out_args) {
1461 int i;
1462 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1463 count = MAX_PHANDLE_ARGS;
1464 out_args->np = node;
1465 out_args->args_count = count;
1466 for (i = 0; i < count; i++)
1467 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001468 } else {
1469 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001470 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001471
1472 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001473 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001474 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001475
1476 of_node_put(node);
1477 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001478 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001479 cur_index++;
1480 }
1481
Grant Likely23ce04c2013-02-12 21:21:49 +00001482 /*
1483 * Unlock node before returning result; will be one of:
1484 * -ENOENT : index is for empty phandle
1485 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001486 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001487 */
Grant Likelybd69f732013-02-10 22:57:21 +00001488 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001489 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001490 if (node)
1491 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001492 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001493}
Grant Likelybd69f732013-02-10 22:57:21 +00001494
Stephen Warreneded9dd2013-08-14 15:27:08 -06001495/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001496 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1497 * @np: Pointer to device node holding phandle property
1498 * @phandle_name: Name of property holding a phandle value
1499 * @index: For properties holding a table of phandles, this is the index into
1500 * the table
1501 *
1502 * Returns the device_node pointer with refcount incremented. Use
1503 * of_node_put() on it when done.
1504 */
1505struct device_node *of_parse_phandle(const struct device_node *np,
1506 const char *phandle_name, int index)
1507{
Stephen Warren91d99422013-08-14 15:27:11 -06001508 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001509
Stephen Warren91d99422013-08-14 15:27:11 -06001510 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001511 return NULL;
1512
Stephen Warren91d99422013-08-14 15:27:11 -06001513 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1514 index, &args))
1515 return NULL;
1516
1517 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001518}
1519EXPORT_SYMBOL(of_parse_phandle);
1520
1521/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001522 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1523 * @np: pointer to a device tree node containing a list
1524 * @list_name: property name that contains a list
1525 * @cells_name: property name that specifies phandles' arguments count
1526 * @index: index of a phandle to parse out
1527 * @out_args: optional pointer to output arguments structure (will be filled)
1528 *
1529 * This function is useful to parse lists of phandles and their arguments.
1530 * Returns 0 on success and fills out_args, on error returns appropriate
1531 * errno value.
1532 *
1533 * Caller is responsible to call of_node_put() on the returned out_args->node
1534 * pointer.
1535 *
1536 * Example:
1537 *
1538 * phandle1: node1 {
1539 * #list-cells = <2>;
1540 * }
1541 *
1542 * phandle2: node2 {
1543 * #list-cells = <1>;
1544 * }
1545 *
1546 * node3 {
1547 * list = <&phandle1 1 2 &phandle2 3>;
1548 * }
1549 *
1550 * To get a device_node of the `node2' node you may call this:
1551 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1552 */
Grant Likelybd69f732013-02-10 22:57:21 +00001553int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1554 const char *cells_name, int index,
1555 struct of_phandle_args *out_args)
1556{
1557 if (index < 0)
1558 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001559 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1560 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001561}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001562EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001563
Grant Likelybd69f732013-02-10 22:57:21 +00001564/**
Stephen Warren035fd942013-08-14 15:27:10 -06001565 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1566 * @np: pointer to a device tree node containing a list
1567 * @list_name: property name that contains a list
1568 * @cell_count: number of argument cells following the phandle
1569 * @index: index of a phandle to parse out
1570 * @out_args: optional pointer to output arguments structure (will be filled)
1571 *
1572 * This function is useful to parse lists of phandles and their arguments.
1573 * Returns 0 on success and fills out_args, on error returns appropriate
1574 * errno value.
1575 *
1576 * Caller is responsible to call of_node_put() on the returned out_args->node
1577 * pointer.
1578 *
1579 * Example:
1580 *
1581 * phandle1: node1 {
1582 * }
1583 *
1584 * phandle2: node2 {
1585 * }
1586 *
1587 * node3 {
1588 * list = <&phandle1 0 2 &phandle2 2 3>;
1589 * }
1590 *
1591 * To get a device_node of the `node2' node you may call this:
1592 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1593 */
1594int of_parse_phandle_with_fixed_args(const struct device_node *np,
1595 const char *list_name, int cell_count,
1596 int index, struct of_phandle_args *out_args)
1597{
1598 if (index < 0)
1599 return -EINVAL;
1600 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1601 index, out_args);
1602}
1603EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1604
1605/**
Grant Likelybd69f732013-02-10 22:57:21 +00001606 * of_count_phandle_with_args() - Find the number of phandles references in a property
1607 * @np: pointer to a device tree node containing a list
1608 * @list_name: property name that contains a list
1609 * @cells_name: property name that specifies phandles' arguments count
1610 *
1611 * Returns the number of phandle + argument tuples within a property. It
1612 * is a typical pattern to encode a list of phandle and variable
1613 * arguments into a single property. The number of arguments is encoded
1614 * by a property in the phandle-target node. For example, a gpios
1615 * property would contain a list of GPIO specifies consisting of a
1616 * phandle and 1 or more arguments. The number of arguments are
1617 * determined by the #gpio-cells property in the node pointed to by the
1618 * phandle.
1619 */
1620int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1621 const char *cells_name)
1622{
Stephen Warren035fd942013-08-14 15:27:10 -06001623 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1624 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001625}
1626EXPORT_SYMBOL(of_count_phandle_with_args);
1627
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001628#if defined(CONFIG_OF_DYNAMIC)
1629static int of_property_notify(int action, struct device_node *np,
1630 struct property *prop)
1631{
1632 struct of_prop_reconfig pr;
1633
1634 pr.dn = np;
1635 pr.prop = prop;
1636 return of_reconfig_notify(action, &pr);
1637}
1638#else
1639static int of_property_notify(int action, struct device_node *np,
1640 struct property *prop)
1641{
1642 return 0;
1643}
1644#endif
1645
Grant Likely02af11b2009-11-23 20:16:45 -07001646/**
Xiubo Li62664f62014-01-22 13:57:39 +08001647 * __of_add_property - Add a property to a node without lock operations
1648 */
1649static int __of_add_property(struct device_node *np, struct property *prop)
1650{
1651 struct property **next;
1652
1653 prop->next = NULL;
1654 next = &np->properties;
1655 while (*next) {
1656 if (strcmp(prop->name, (*next)->name) == 0)
1657 /* duplicate ! don't insert it */
1658 return -EEXIST;
1659
1660 next = &(*next)->next;
1661 }
1662 *next = prop;
1663
1664 return 0;
1665}
1666
1667/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001668 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001669 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001670int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001671{
Grant Likely02af11b2009-11-23 20:16:45 -07001672 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001673 int rc;
1674
1675 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1676 if (rc)
1677 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001678
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001679 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001680 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001681 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001682 if (rc)
1683 return rc;
1684
1685 /* at early boot, bail hear and defer setup to of_init() */
1686 if (!of_kset)
1687 return 0;
1688
1689 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001690
Xiubo Li62664f62014-01-22 13:57:39 +08001691 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001692}
1693
1694/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001695 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001696 *
1697 * Note that we don't actually remove it, since we have given out
1698 * who-knows-how-many pointers to the data using get-property.
1699 * Instead we just move the property to the "dead properties"
1700 * list, so it won't be found any more.
1701 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001702int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001703{
1704 struct property **next;
1705 unsigned long flags;
1706 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001707 int rc;
1708
1709 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1710 if (rc)
1711 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001712
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001713 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001714 next = &np->properties;
1715 while (*next) {
1716 if (*next == prop) {
1717 /* found the node */
1718 *next = prop->next;
1719 prop->next = np->deadprops;
1720 np->deadprops = prop;
1721 found = 1;
1722 break;
1723 }
1724 next = &(*next)->next;
1725 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001726 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001727
1728 if (!found)
1729 return -ENODEV;
1730
Grant Likely75b57ec2014-02-20 18:02:11 +00001731 /* at early boot, bail hear and defer setup to of_init() */
1732 if (!of_kset)
1733 return 0;
1734
1735 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1736
Grant Likely02af11b2009-11-23 20:16:45 -07001737 return 0;
1738}
1739
1740/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001741 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001742 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001743 *
1744 * Note that we don't actually remove it, since we have given out
1745 * who-knows-how-many pointers to the data using get-property.
1746 * Instead we just move the property to the "dead properties" list,
1747 * and add the new property to the property list
1748 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001749int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001750{
Dong Aisheng475d0092012-07-11 15:16:37 +10001751 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001752 unsigned long flags;
Grant Likelya3dbeb52014-03-04 16:07:17 +08001753 int rc, found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001754
1755 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1756 if (rc)
1757 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001758
Dong Aisheng475d0092012-07-11 15:16:37 +10001759 if (!newprop->name)
1760 return -EINVAL;
1761
Grant Likelya3dbeb52014-03-04 16:07:17 +08001762 oldprop = of_find_property(np, newprop->name, NULL);
1763 if (!oldprop)
1764 return of_add_property(np, newprop);
1765
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001766 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelya3dbeb52014-03-04 16:07:17 +08001767 next = &np->properties;
1768 while (*next) {
1769 if (*next == oldprop) {
1770 /* found the node */
1771 newprop->next = oldprop->next;
1772 *next = newprop;
1773 oldprop->next = np->deadprops;
1774 np->deadprops = oldprop;
1775 found = 1;
1776 break;
1777 }
1778 next = &(*next)->next;
Grant Likely02af11b2009-11-23 20:16:45 -07001779 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001780 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001781 if (rc)
1782 return rc;
1783
1784 /* Update the sysfs attribute */
1785 if (oldprop)
1786 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1787 __of_add_property_sysfs(np, newprop);
Grant Likely02af11b2009-11-23 20:16:45 -07001788
Grant Likelya3dbeb52014-03-04 16:07:17 +08001789 if (!found)
1790 return -ENODEV;
1791
Grant Likelya3dbeb52014-03-04 16:07:17 +08001792 return 0;
Grant Likely02af11b2009-11-23 20:16:45 -07001793}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001794
1795#if defined(CONFIG_OF_DYNAMIC)
1796/*
1797 * Support for dynamic device trees.
1798 *
1799 * On some platforms, the device tree can be manipulated at runtime.
1800 * The routines in this section support adding, removing and changing
1801 * device tree nodes.
1802 */
1803
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001804static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1805
1806int of_reconfig_notifier_register(struct notifier_block *nb)
1807{
1808 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1809}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001810EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001811
1812int of_reconfig_notifier_unregister(struct notifier_block *nb)
1813{
1814 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1815}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001816EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001817
1818int of_reconfig_notify(unsigned long action, void *p)
1819{
1820 int rc;
1821
1822 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1823 return notifier_to_errno(rc);
1824}
1825
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001826/**
1827 * of_attach_node - Plug a device node into the tree and global list.
1828 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001829int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001830{
1831 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001832 int rc;
1833
1834 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1835 if (rc)
1836 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001837
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001838 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001839 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001840 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001841 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001842 of_allnodes = np;
Pantelis Antonioue3963fd2013-11-08 17:03:57 +02001843 of_node_clear_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001844 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001845
Grant Likely75b57ec2014-02-20 18:02:11 +00001846 of_node_add(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001847 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001848}
1849
1850/**
1851 * of_detach_node - "Unplug" a node from the device tree.
1852 *
1853 * The caller must hold a reference to the node. The memory associated with
1854 * the node is not freed until its refcount goes to zero.
1855 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001856int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001857{
1858 struct device_node *parent;
1859 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001860 int rc = 0;
1861
1862 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1863 if (rc)
1864 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001865
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001866 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001867
Nathan Fontenote81b3292012-10-02 16:55:01 +00001868 if (of_node_check_flag(np, OF_DETACHED)) {
1869 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001870 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001871 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001872 }
1873
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001874 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001875 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001876 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001877 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001878 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001879
Randy Dunlap465aac62012-11-30 10:01:51 +00001880 if (of_allnodes == np)
1881 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001882 else {
1883 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001884 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001885 prev->allnext != np;
1886 prev = prev->allnext)
1887 ;
1888 prev->allnext = np->allnext;
1889 }
1890
1891 if (parent->child == np)
1892 parent->child = np->sibling;
1893 else {
1894 struct device_node *prevsib;
1895 for (prevsib = np->parent->child;
1896 prevsib->sibling != np;
1897 prevsib = prevsib->sibling)
1898 ;
1899 prevsib->sibling = np->sibling;
1900 }
1901
1902 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001903 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001904
Grant Likely75b57ec2014-02-20 18:02:11 +00001905 of_node_remove(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001906 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001907}
1908#endif /* defined(CONFIG_OF_DYNAMIC) */
1909
Shawn Guo611cad72011-08-15 15:28:14 +08001910static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1911 int id, const char *stem, int stem_len)
1912{
1913 ap->np = np;
1914 ap->id = id;
1915 strncpy(ap->stem, stem, stem_len);
1916 ap->stem[stem_len] = 0;
1917 list_add_tail(&ap->link, &aliases_lookup);
1918 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001919 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001920}
1921
1922/**
1923 * of_alias_scan - Scan all properties of 'aliases' node
1924 *
1925 * The function scans all the properties of 'aliases' node and populate
1926 * the the global lookup table with the properties. It returns the
1927 * number of alias_prop found, or error code in error case.
1928 *
1929 * @dt_alloc: An allocator that provides a virtual address to memory
1930 * for the resulting tree
1931 */
1932void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1933{
1934 struct property *pp;
1935
1936 of_chosen = of_find_node_by_path("/chosen");
1937 if (of_chosen == NULL)
1938 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001939
1940 if (of_chosen) {
1941 const char *name;
1942
1943 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1944 if (name)
1945 of_stdout = of_find_node_by_path(name);
1946 }
1947
Shawn Guo611cad72011-08-15 15:28:14 +08001948 of_aliases = of_find_node_by_path("/aliases");
1949 if (!of_aliases)
1950 return;
1951
Dong Aisheng8af0da92011-12-22 20:19:24 +08001952 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001953 const char *start = pp->name;
1954 const char *end = start + strlen(start);
1955 struct device_node *np;
1956 struct alias_prop *ap;
1957 int id, len;
1958
1959 /* Skip those we do not want to proceed */
1960 if (!strcmp(pp->name, "name") ||
1961 !strcmp(pp->name, "phandle") ||
1962 !strcmp(pp->name, "linux,phandle"))
1963 continue;
1964
1965 np = of_find_node_by_path(pp->value);
1966 if (!np)
1967 continue;
1968
1969 /* walk the alias backwards to extract the id and work out
1970 * the 'stem' string */
1971 while (isdigit(*(end-1)) && end > start)
1972 end--;
1973 len = end - start;
1974
1975 if (kstrtoint(end, 10, &id) < 0)
1976 continue;
1977
1978 /* Allocate an alias_prop with enough space for the stem */
1979 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1980 if (!ap)
1981 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001982 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001983 ap->alias = start;
1984 of_alias_add(ap, np, id, start, len);
1985 }
1986}
1987
1988/**
1989 * of_alias_get_id - Get alias id for the given device_node
1990 * @np: Pointer to the given device_node
1991 * @stem: Alias stem of the given device_node
1992 *
1993 * The function travels the lookup table to get alias id for the given
1994 * device_node and alias stem. It returns the alias id if find it.
1995 */
1996int of_alias_get_id(struct device_node *np, const char *stem)
1997{
1998 struct alias_prop *app;
1999 int id = -ENODEV;
2000
2001 mutex_lock(&of_aliases_mutex);
2002 list_for_each_entry(app, &aliases_lookup, link) {
2003 if (strcmp(app->stem, stem) != 0)
2004 continue;
2005
2006 if (np == app->np) {
2007 id = app->id;
2008 break;
2009 }
2010 }
2011 mutex_unlock(&of_aliases_mutex);
2012
2013 return id;
2014}
2015EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06002016
2017const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2018 u32 *pu)
2019{
2020 const void *curv = cur;
2021
2022 if (!prop)
2023 return NULL;
2024
2025 if (!cur) {
2026 curv = prop->value;
2027 goto out_val;
2028 }
2029
2030 curv += sizeof(*cur);
2031 if (curv >= prop->value + prop->length)
2032 return NULL;
2033
2034out_val:
2035 *pu = be32_to_cpup(curv);
2036 return curv;
2037}
2038EXPORT_SYMBOL_GPL(of_prop_next_u32);
2039
2040const char *of_prop_next_string(struct property *prop, const char *cur)
2041{
2042 const void *curv = cur;
2043
2044 if (!prop)
2045 return NULL;
2046
2047 if (!cur)
2048 return prop->value;
2049
2050 curv += strlen(cur) + 1;
2051 if (curv >= prop->value + prop->length)
2052 return NULL;
2053
2054 return curv;
2055}
2056EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002057
2058/**
2059 * of_device_is_stdout_path - check if a device node matches the
2060 * linux,stdout-path property
2061 *
2062 * Check if this device node matches the linux,stdout-path property
2063 * in the chosen node. return true if yes, false otherwise.
2064 */
2065int of_device_is_stdout_path(struct device_node *dn)
2066{
2067 if (!of_stdout)
2068 return false;
2069
2070 return of_stdout == dn;
2071}
2072EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002073
2074/**
2075 * of_find_next_cache_node - Find a node's subsidiary cache
2076 * @np: node of type "cpu" or "cache"
2077 *
2078 * Returns a node pointer with refcount incremented, use
2079 * of_node_put() on it when done. Caller should hold a reference
2080 * to np.
2081 */
2082struct device_node *of_find_next_cache_node(const struct device_node *np)
2083{
2084 struct device_node *child;
2085 const phandle *handle;
2086
2087 handle = of_get_property(np, "l2-cache", NULL);
2088 if (!handle)
2089 handle = of_get_property(np, "next-level-cache", NULL);
2090
2091 if (handle)
2092 return of_find_node_by_phandle(be32_to_cpup(handle));
2093
2094 /* OF on pmac has nodes instead of properties named "l2-cache"
2095 * beneath CPU nodes.
2096 */
2097 if (!strcmp(np->type, "cpu"))
2098 for_each_child_of_node(np, child)
2099 if (!strcmp(child->type, "cache"))
2100 return child;
2101
2102 return NULL;
2103}