blob: f86be5594a154c862a5b1a881f3a70a2497ec88b [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>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100021#include <linux/module.h>
22#include <linux/of.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100023#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070025#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100026
Shawn Guo611cad72011-08-15 15:28:14 +080027/**
28 * struct alias_prop - Alias property in 'aliases' node
29 * @link: List node to link the structure in aliases_lookup list
30 * @alias: Alias property name
31 * @np: Pointer to device_node that the alias stands for
32 * @id: Index value from end of alias name
33 * @stem: Alias string without the index
34 *
35 * The structure represents one alias property of 'aliases' node as
36 * an entry in aliases_lookup list.
37 */
38struct alias_prop {
39 struct list_head link;
40 const char *alias;
41 struct device_node *np;
42 int id;
43 char stem[0];
44};
45
46static LIST_HEAD(aliases_lookup);
47
Randy Dunlap465aac62012-11-30 10:01:51 +000048struct device_node *of_allnodes;
49EXPORT_SYMBOL(of_allnodes);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070050struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080051struct device_node *of_aliases;
52
53static DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100054
Stephen Rothwell581b6052007-04-24 16:46:53 +100055/* use when traversing tree through the allnext, child, sibling,
56 * or parent members of struct device_node.
57 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050058DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100059
60int of_n_addr_cells(struct device_node *np)
61{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060062 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100063
64 do {
65 if (np->parent)
66 np = np->parent;
67 ip = of_get_property(np, "#address-cells", NULL);
68 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070069 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100070 } while (np->parent);
71 /* No #address-cells property for the root node */
72 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
73}
74EXPORT_SYMBOL(of_n_addr_cells);
75
76int of_n_size_cells(struct device_node *np)
77{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060078 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100079
80 do {
81 if (np->parent)
82 np = np->parent;
83 ip = of_get_property(np, "#size-cells", NULL);
84 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070085 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100086 } while (np->parent);
87 /* No #size-cells property for the root node */
88 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
89}
90EXPORT_SYMBOL(of_n_size_cells);
91
Grant Likely0f22dd32012-02-15 20:38:40 -070092#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070093/**
94 * of_node_get - Increment refcount of a node
95 * @node: Node to inc refcount, NULL is supported to
96 * simplify writing of callers
97 *
98 * Returns node.
99 */
100struct device_node *of_node_get(struct device_node *node)
101{
102 if (node)
103 kref_get(&node->kref);
104 return node;
105}
106EXPORT_SYMBOL(of_node_get);
107
108static inline struct device_node *kref_to_device_node(struct kref *kref)
109{
110 return container_of(kref, struct device_node, kref);
111}
112
113/**
114 * of_node_release - release a dynamically allocated node
115 * @kref: kref element of the node to be released
116 *
117 * In of_node_put() this function is passed to kref_put()
118 * as the destructor.
119 */
120static void of_node_release(struct kref *kref)
121{
122 struct device_node *node = kref_to_device_node(kref);
123 struct property *prop = node->properties;
124
125 /* We should never be releasing nodes that haven't been detached. */
126 if (!of_node_check_flag(node, OF_DETACHED)) {
127 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
128 dump_stack();
129 kref_init(&node->kref);
130 return;
131 }
132
133 if (!of_node_check_flag(node, OF_DYNAMIC))
134 return;
135
136 while (prop) {
137 struct property *next = prop->next;
138 kfree(prop->name);
139 kfree(prop->value);
140 kfree(prop);
141 prop = next;
142
143 if (!prop) {
144 prop = node->deadprops;
145 node->deadprops = NULL;
146 }
147 }
148 kfree(node->full_name);
149 kfree(node->data);
150 kfree(node);
151}
152
153/**
154 * of_node_put - Decrement refcount of a node
155 * @node: Node to dec refcount, NULL is supported to
156 * simplify writing of callers
157 *
158 */
159void of_node_put(struct device_node *node)
160{
161 if (node)
162 kref_put(&node->kref, of_node_release);
163}
164EXPORT_SYMBOL(of_node_put);
Grant Likely0f22dd32012-02-15 20:38:40 -0700165#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700166
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500167static struct property *__of_find_property(const struct device_node *np,
168 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000169{
170 struct property *pp;
171
Timur Tabi64e45662008-05-08 05:19:59 +1000172 if (!np)
173 return NULL;
174
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530175 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000176 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530177 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000178 *lenp = pp->length;
179 break;
180 }
181 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500182
183 return pp;
184}
185
186struct property *of_find_property(const struct device_node *np,
187 const char *name,
188 int *lenp)
189{
190 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500191 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500192
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500193 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500194 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500195 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000196
197 return pp;
198}
199EXPORT_SYMBOL(of_find_property);
200
Grant Likelye91edcf2009-10-15 10:58:09 -0600201/**
202 * of_find_all_nodes - Get next node in global list
203 * @prev: Previous node or NULL to start iteration
204 * of_node_put() will be called on it
205 *
206 * Returns a node pointer with refcount incremented, use
207 * of_node_put() on it when done.
208 */
209struct device_node *of_find_all_nodes(struct device_node *prev)
210{
211 struct device_node *np;
212
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500213 raw_spin_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000214 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600215 for (; np != NULL; np = np->allnext)
216 if (of_node_get(np))
217 break;
218 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500219 raw_spin_unlock(&devtree_lock);
Grant Likelye91edcf2009-10-15 10:58:09 -0600220 return np;
221}
222EXPORT_SYMBOL(of_find_all_nodes);
223
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000224/*
225 * Find a property with a given name for a given node
226 * and return the value.
227 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500228static const void *__of_get_property(const struct device_node *np,
229 const char *name, int *lenp)
230{
231 struct property *pp = __of_find_property(np, name, lenp);
232
233 return pp ? pp->value : NULL;
234}
235
236/*
237 * Find a property with a given name for a given node
238 * and return the value.
239 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000240const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500241 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000242{
243 struct property *pp = of_find_property(np, name, lenp);
244
245 return pp ? pp->value : NULL;
246}
247EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000248
249/** Checks if the given "compat" string matches one of the strings in
250 * the device's "compatible" property
251 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500252static int __of_device_is_compatible(const struct device_node *device,
253 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000254{
255 const char* cp;
256 int cplen, l;
257
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500258 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000259 if (cp == NULL)
260 return 0;
261 while (cplen > 0) {
262 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
263 return 1;
264 l = strlen(cp) + 1;
265 cp += l;
266 cplen -= l;
267 }
268
269 return 0;
270}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500271
272/** Checks if the given "compat" string matches one of the strings in
273 * the device's "compatible" property
274 */
275int of_device_is_compatible(const struct device_node *device,
276 const char *compat)
277{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500278 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500279 int res;
280
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500281 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500282 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500283 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500284 return res;
285}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000286EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000287
288/**
Grant Likely71a157e8e2010-02-01 21:34:14 -0700289 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700290 * @compat: compatible string to look for in root node's compatible property.
291 *
292 * Returns true if the root node has the given value in its
293 * compatible property.
294 */
Grant Likely71a157e8e2010-02-01 21:34:14 -0700295int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700296{
297 struct device_node *root;
298 int rc = 0;
299
300 root = of_find_node_by_path("/");
301 if (root) {
302 rc = of_device_is_compatible(root, compat);
303 of_node_put(root);
304 }
305 return rc;
306}
Grant Likely71a157e8e2010-02-01 21:34:14 -0700307EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700308
309/**
Josh Boyer834d97d2008-03-27 00:33:14 +1100310 * of_device_is_available - check if a device is available for use
311 *
312 * @device: Node to check for availability
313 *
314 * Returns 1 if the status property is absent or set to "okay" or "ok",
315 * 0 otherwise
316 */
317int of_device_is_available(const struct device_node *device)
318{
319 const char *status;
320 int statlen;
321
322 status = of_get_property(device, "status", &statlen);
323 if (status == NULL)
324 return 1;
325
326 if (statlen > 0) {
327 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
328 return 1;
329 }
330
331 return 0;
332}
333EXPORT_SYMBOL(of_device_is_available);
334
335/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000336 * of_get_parent - Get a node's parent if any
337 * @node: Node to get parent
338 *
339 * Returns a node pointer with refcount incremented, use
340 * of_node_put() on it when done.
341 */
342struct device_node *of_get_parent(const struct device_node *node)
343{
344 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500345 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000346
347 if (!node)
348 return NULL;
349
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500350 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000351 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500352 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000353 return np;
354}
355EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000356
357/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000358 * of_get_next_parent - Iterate to a node's parent
359 * @node: Node to get parent of
360 *
361 * This is like of_get_parent() except that it drops the
362 * refcount on the passed node, making it suitable for iterating
363 * through a node's parents.
364 *
365 * Returns a node pointer with refcount incremented, use
366 * of_node_put() on it when done.
367 */
368struct device_node *of_get_next_parent(struct device_node *node)
369{
370 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500371 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000372
373 if (!node)
374 return NULL;
375
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500376 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000377 parent = of_node_get(node->parent);
378 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500379 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000380 return parent;
381}
382
383/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000384 * of_get_next_child - Iterate a node childs
385 * @node: parent node
386 * @prev: previous child of the parent node, or NULL to get first
387 *
388 * Returns a node pointer with refcount incremented, use
389 * of_node_put() on it when done.
390 */
391struct device_node *of_get_next_child(const struct device_node *node,
392 struct device_node *prev)
393{
394 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500395 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000396
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500397 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000398 next = prev ? prev->sibling : node->child;
399 for (; next; next = next->sibling)
400 if (of_node_get(next))
401 break;
402 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500403 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000404 return next;
405}
406EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000407
408/**
Timur Tabi32961932012-08-14 13:20:23 +0000409 * of_get_next_available_child - Find the next available child node
410 * @node: parent node
411 * @prev: previous child of the parent node, or NULL to get first
412 *
413 * This function is like of_get_next_child(), except that it
414 * automatically skips any disabled nodes (i.e. status = "disabled").
415 */
416struct device_node *of_get_next_available_child(const struct device_node *node,
417 struct device_node *prev)
418{
419 struct device_node *next;
420
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500421 raw_spin_lock(&devtree_lock);
Timur Tabi32961932012-08-14 13:20:23 +0000422 next = prev ? prev->sibling : node->child;
423 for (; next; next = next->sibling) {
424 if (!of_device_is_available(next))
425 continue;
426 if (of_node_get(next))
427 break;
428 }
429 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500430 raw_spin_unlock(&devtree_lock);
Timur Tabi32961932012-08-14 13:20:23 +0000431 return next;
432}
433EXPORT_SYMBOL(of_get_next_available_child);
434
435/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100436 * of_get_child_by_name - Find the child node by name for a given parent
437 * @node: parent node
438 * @name: child name to look for.
439 *
440 * This function looks for child node for given matching name
441 *
442 * Returns a node pointer if found, with refcount incremented, use
443 * of_node_put() on it when done.
444 * Returns NULL if node is not found.
445 */
446struct device_node *of_get_child_by_name(const struct device_node *node,
447 const char *name)
448{
449 struct device_node *child;
450
451 for_each_child_of_node(node, child)
452 if (child->name && (of_node_cmp(child->name, name) == 0))
453 break;
454 return child;
455}
456EXPORT_SYMBOL(of_get_child_by_name);
457
458/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000459 * of_find_node_by_path - Find a node matching a full OF path
460 * @path: The full path to match
461 *
462 * Returns a node pointer with refcount incremented, use
463 * of_node_put() on it when done.
464 */
465struct device_node *of_find_node_by_path(const char *path)
466{
Randy Dunlap465aac62012-11-30 10:01:51 +0000467 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500468 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000469
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500470 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000471 for (; np; np = np->allnext) {
472 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
473 && of_node_get(np))
474 break;
475 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500476 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000477 return np;
478}
479EXPORT_SYMBOL(of_find_node_by_path);
480
481/**
482 * of_find_node_by_name - Find a node by its "name" property
483 * @from: The node to start searching from or NULL, the node
484 * you pass will not be searched, only the next one
485 * will; typically, you pass what the previous call
486 * returned. of_node_put() will be called on it
487 * @name: The name string to match against
488 *
489 * Returns a node pointer with refcount incremented, use
490 * of_node_put() on it when done.
491 */
492struct device_node *of_find_node_by_name(struct device_node *from,
493 const char *name)
494{
495 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500496 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000497
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500498 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000499 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000500 for (; np; np = np->allnext)
501 if (np->name && (of_node_cmp(np->name, name) == 0)
502 && of_node_get(np))
503 break;
504 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500505 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000506 return np;
507}
508EXPORT_SYMBOL(of_find_node_by_name);
509
510/**
511 * of_find_node_by_type - Find a node by its "device_type" property
512 * @from: The node to start searching from, or NULL to start searching
513 * the entire device tree. The node you pass will not be
514 * searched, only the next one will; typically, you pass
515 * what the previous call returned. of_node_put() will be
516 * called on from for you.
517 * @type: The type string to match against
518 *
519 * Returns a node pointer with refcount incremented, use
520 * of_node_put() on it when done.
521 */
522struct device_node *of_find_node_by_type(struct device_node *from,
523 const char *type)
524{
525 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500526 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000527
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500528 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000529 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000530 for (; np; np = np->allnext)
531 if (np->type && (of_node_cmp(np->type, type) == 0)
532 && of_node_get(np))
533 break;
534 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500535 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000536 return np;
537}
538EXPORT_SYMBOL(of_find_node_by_type);
539
540/**
541 * of_find_compatible_node - Find a node based on type and one of the
542 * tokens in its "compatible" property
543 * @from: The node to start searching from or NULL, the node
544 * you pass will not be searched, only the next one
545 * will; typically, you pass what the previous call
546 * returned. of_node_put() will be called on it
547 * @type: The type string to match "device_type" or NULL to ignore
548 * @compatible: The string to match to one of the tokens in the device
549 * "compatible" list.
550 *
551 * Returns a node pointer with refcount incremented, use
552 * of_node_put() on it when done.
553 */
554struct device_node *of_find_compatible_node(struct device_node *from,
555 const char *type, const char *compatible)
556{
557 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500558 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000559
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500560 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000561 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000562 for (; np; np = np->allnext) {
563 if (type
564 && !(np->type && (of_node_cmp(np->type, type) == 0)))
565 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500566 if (__of_device_is_compatible(np, compatible) &&
567 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000568 break;
569 }
570 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500571 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000572 return np;
573}
574EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100575
576/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000577 * of_find_node_with_property - Find a node which has a property with
578 * the given name.
579 * @from: The node to start searching from or NULL, the node
580 * you pass will not be searched, only the next one
581 * will; typically, you pass what the previous call
582 * returned. of_node_put() will be called on it
583 * @prop_name: The name of the property to look for.
584 *
585 * Returns a node pointer with refcount incremented, use
586 * of_node_put() on it when done.
587 */
588struct device_node *of_find_node_with_property(struct device_node *from,
589 const char *prop_name)
590{
591 struct device_node *np;
592 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500593 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000594
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500595 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000596 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000597 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530598 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000599 if (of_prop_cmp(pp->name, prop_name) == 0) {
600 of_node_get(np);
601 goto out;
602 }
603 }
604 }
605out:
606 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500607 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000608 return np;
609}
610EXPORT_SYMBOL(of_find_node_with_property);
611
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500612static
613const struct of_device_id *__of_match_node(const struct of_device_id *matches,
614 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100615{
Grant Likelya52f07e2011-03-18 10:21:29 -0600616 if (!matches)
617 return NULL;
618
Grant Likely283029d2008-01-09 06:20:40 +1100619 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
620 int match = 1;
621 if (matches->name[0])
622 match &= node->name
623 && !strcmp(matches->name, node->name);
624 if (matches->type[0])
625 match &= node->type
626 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700627 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500628 match &= __of_device_is_compatible(node,
629 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700630 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100631 return matches;
632 matches++;
633 }
634 return NULL;
635}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500636
637/**
638 * of_match_node - Tell if an device_node has a matching of_match structure
639 * @matches: array of of device match structures to search in
640 * @node: the of device structure to match against
641 *
642 * Low level utility function used by device matching.
643 */
644const struct of_device_id *of_match_node(const struct of_device_id *matches,
645 const struct device_node *node)
646{
647 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500648 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500649
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500650 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500651 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500652 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500653 return match;
654}
Grant Likely283029d2008-01-09 06:20:40 +1100655EXPORT_SYMBOL(of_match_node);
656
657/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700658 * of_find_matching_node_and_match - Find a node based on an of_device_id
659 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100660 * @from: The node to start searching from or NULL, the node
661 * you pass will not be searched, only the next one
662 * will; typically, you pass what the previous call
663 * returned. of_node_put() will be called on it
664 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700665 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100666 *
667 * Returns a node pointer with refcount incremented, use
668 * of_node_put() on it when done.
669 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700670struct device_node *of_find_matching_node_and_match(struct device_node *from,
671 const struct of_device_id *matches,
672 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100673{
674 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800675 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500676 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100677
Stephen Warren50c8af42012-11-20 16:12:20 -0700678 if (match)
679 *match = NULL;
680
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500681 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000682 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100683 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500684 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800685 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700686 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800687 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100688 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700689 }
Grant Likely283029d2008-01-09 06:20:40 +1100690 }
691 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500692 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100693 return np;
694}
Grant Likely80c20222012-12-19 10:45:36 +0000695EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400696
697/**
Grant Likely3f07af42008-07-25 22:25:13 -0400698 * of_modalias_node - Lookup appropriate modalias for a device node
699 * @node: pointer to a device tree node
700 * @modalias: Pointer to buffer that modalias value will be copied into
701 * @len: Length of modalias value
702 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600703 * Based on the value of the compatible property, this routine will attempt
704 * to choose an appropriate modalias value for a particular device tree node.
705 * It does this by stripping the manufacturer prefix (as delimited by a ',')
706 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400707 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600708 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400709 */
710int of_modalias_node(struct device_node *node, char *modalias, int len)
711{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600712 const char *compatible, *p;
713 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400714
715 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600716 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400717 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400718 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600719 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400720 return 0;
721}
722EXPORT_SYMBOL_GPL(of_modalias_node);
723
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000724/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700725 * of_find_node_by_phandle - Find a node given a phandle
726 * @handle: phandle of the node to find
727 *
728 * Returns a node pointer with refcount incremented, use
729 * of_node_put() on it when done.
730 */
731struct device_node *of_find_node_by_phandle(phandle handle)
732{
733 struct device_node *np;
734
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500735 raw_spin_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000736 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700737 if (np->phandle == handle)
738 break;
739 of_node_get(np);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500740 raw_spin_unlock(&devtree_lock);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700741 return np;
742}
743EXPORT_SYMBOL(of_find_node_by_phandle);
744
745/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530746 * of_property_read_u8_array - Find and read an array of u8 from a property.
747 *
748 * @np: device node from which the property value is to be read.
749 * @propname: name of the property to be searched.
750 * @out_value: pointer to return value, modified only if return value is 0.
751 * @sz: number of array elements to read
752 *
753 * Search for a property in a device node and read 8-bit value(s) from
754 * it. Returns 0 on success, -EINVAL if the property does not exist,
755 * -ENODATA if property does not have a value, and -EOVERFLOW if the
756 * property data isn't large enough.
757 *
758 * dts entry of array should be like:
759 * property = /bits/ 8 <0x50 0x60 0x70>;
760 *
761 * The out_value is modified only if a valid u8 value can be decoded.
762 */
763int of_property_read_u8_array(const struct device_node *np,
764 const char *propname, u8 *out_values, size_t sz)
765{
766 struct property *prop = of_find_property(np, propname, NULL);
767 const u8 *val;
768
769 if (!prop)
770 return -EINVAL;
771 if (!prop->value)
772 return -ENODATA;
773 if ((sz * sizeof(*out_values)) > prop->length)
774 return -EOVERFLOW;
775
776 val = prop->value;
777 while (sz--)
778 *out_values++ = *val++;
779 return 0;
780}
781EXPORT_SYMBOL_GPL(of_property_read_u8_array);
782
783/**
784 * of_property_read_u16_array - Find and read an array of u16 from a property.
785 *
786 * @np: device node from which the property value is to be read.
787 * @propname: name of the property to be searched.
788 * @out_value: pointer to return value, modified only if return value is 0.
789 * @sz: number of array elements to read
790 *
791 * Search for a property in a device node and read 16-bit value(s) from
792 * it. Returns 0 on success, -EINVAL if the property does not exist,
793 * -ENODATA if property does not have a value, and -EOVERFLOW if the
794 * property data isn't large enough.
795 *
796 * dts entry of array should be like:
797 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
798 *
799 * The out_value is modified only if a valid u16 value can be decoded.
800 */
801int of_property_read_u16_array(const struct device_node *np,
802 const char *propname, u16 *out_values, size_t sz)
803{
804 struct property *prop = of_find_property(np, propname, NULL);
805 const __be16 *val;
806
807 if (!prop)
808 return -EINVAL;
809 if (!prop->value)
810 return -ENODATA;
811 if ((sz * sizeof(*out_values)) > prop->length)
812 return -EOVERFLOW;
813
814 val = prop->value;
815 while (sz--)
816 *out_values++ = be16_to_cpup(val++);
817 return 0;
818}
819EXPORT_SYMBOL_GPL(of_property_read_u16_array);
820
821/**
Rob Herring0e373632011-07-06 15:42:58 -0500822 * of_property_read_u32_array - Find and read an array of 32 bit integers
823 * from a property.
824 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530825 * @np: device node from which the property value is to be read.
826 * @propname: name of the property to be searched.
827 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530828 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530829 *
Rob Herring0e373632011-07-06 15:42:58 -0500830 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530831 * it. Returns 0 on success, -EINVAL if the property does not exist,
832 * -ENODATA if property does not have a value, and -EOVERFLOW if the
833 * property data isn't large enough.
834 *
835 * The out_value is modified only if a valid u32 value can be decoded.
836 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100837int of_property_read_u32_array(const struct device_node *np,
838 const char *propname, u32 *out_values,
839 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530840{
841 struct property *prop = of_find_property(np, propname, NULL);
Rob Herring0e373632011-07-06 15:42:58 -0500842 const __be32 *val;
Thomas Abrahama3b85362011-06-30 21:26:10 +0530843
844 if (!prop)
845 return -EINVAL;
846 if (!prop->value)
847 return -ENODATA;
Rob Herring0e373632011-07-06 15:42:58 -0500848 if ((sz * sizeof(*out_values)) > prop->length)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530849 return -EOVERFLOW;
Rob Herring0e373632011-07-06 15:42:58 -0500850
851 val = prop->value;
852 while (sz--)
853 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530854 return 0;
855}
Rob Herring0e373632011-07-06 15:42:58 -0500856EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530857
858/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100859 * of_property_read_u64 - Find and read a 64 bit integer from a property
860 * @np: device node from which the property value is to be read.
861 * @propname: name of the property to be searched.
862 * @out_value: pointer to return value, modified only if return value is 0.
863 *
864 * Search for a property in a device node and read a 64-bit value from
865 * it. Returns 0 on success, -EINVAL if the property does not exist,
866 * -ENODATA if property does not have a value, and -EOVERFLOW if the
867 * property data isn't large enough.
868 *
869 * The out_value is modified only if a valid u64 value can be decoded.
870 */
871int of_property_read_u64(const struct device_node *np, const char *propname,
872 u64 *out_value)
873{
874 struct property *prop = of_find_property(np, propname, NULL);
875
876 if (!prop)
877 return -EINVAL;
878 if (!prop->value)
879 return -ENODATA;
880 if (sizeof(*out_value) > prop->length)
881 return -EOVERFLOW;
882 *out_value = of_read_number(prop->value, 2);
883 return 0;
884}
885EXPORT_SYMBOL_GPL(of_property_read_u64);
886
887/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530888 * of_property_read_string - Find and read a string from a property
889 * @np: device node from which the property value is to be read.
890 * @propname: name of the property to be searched.
891 * @out_string: pointer to null terminated return string, modified only if
892 * return value is 0.
893 *
894 * Search for a property in a device tree node and retrieve a null
895 * terminated string value (pointer to data, not a copy). Returns 0 on
896 * success, -EINVAL if the property does not exist, -ENODATA if property
897 * does not have a value, and -EILSEQ if the string is not null-terminated
898 * within the length of the property data.
899 *
900 * The out_string pointer is modified only if a valid string can be decoded.
901 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100902int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800903 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530904{
905 struct property *prop = of_find_property(np, propname, NULL);
906 if (!prop)
907 return -EINVAL;
908 if (!prop->value)
909 return -ENODATA;
910 if (strnlen(prop->value, prop->length) >= prop->length)
911 return -EILSEQ;
912 *out_string = prop->value;
913 return 0;
914}
915EXPORT_SYMBOL_GPL(of_property_read_string);
916
917/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200918 * of_property_read_string_index - Find and read a string from a multiple
919 * strings property.
920 * @np: device node from which the property value is to be read.
921 * @propname: name of the property to be searched.
922 * @index: index of the string in the list of strings
923 * @out_string: pointer to null terminated return string, modified only if
924 * return value is 0.
925 *
926 * Search for a property in a device tree node and retrieve a null
927 * terminated string value (pointer to data, not a copy) in the list of strings
928 * contained in that property.
929 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
930 * property does not have a value, and -EILSEQ if the string is not
931 * null-terminated within the length of the property data.
932 *
933 * The out_string pointer is modified only if a valid string can be decoded.
934 */
935int of_property_read_string_index(struct device_node *np, const char *propname,
936 int index, const char **output)
937{
938 struct property *prop = of_find_property(np, propname, NULL);
939 int i = 0;
940 size_t l = 0, total = 0;
941 const char *p;
942
943 if (!prop)
944 return -EINVAL;
945 if (!prop->value)
946 return -ENODATA;
947 if (strnlen(prop->value, prop->length) >= prop->length)
948 return -EILSEQ;
949
950 p = prop->value;
951
952 for (i = 0; total < prop->length; total += l, p += l) {
953 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100954 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200955 *output = p;
956 return 0;
957 }
958 }
959 return -ENODATA;
960}
961EXPORT_SYMBOL_GPL(of_property_read_string_index);
962
Grant Likely7aff0fe2011-12-12 09:25:58 -0700963/**
964 * of_property_match_string() - Find string in a list and return index
965 * @np: pointer to node containing string list property
966 * @propname: string list property name
967 * @string: pointer to string to search for in string list
968 *
969 * This function searches a string list property and returns the index
970 * of a specific string value.
971 */
972int of_property_match_string(struct device_node *np, const char *propname,
973 const char *string)
974{
975 struct property *prop = of_find_property(np, propname, NULL);
976 size_t l;
977 int i;
978 const char *p, *end;
979
980 if (!prop)
981 return -EINVAL;
982 if (!prop->value)
983 return -ENODATA;
984
985 p = prop->value;
986 end = p + prop->length;
987
988 for (i = 0; p < end; i++, p += l) {
989 l = strlen(p) + 1;
990 if (p + l > end)
991 return -EILSEQ;
992 pr_debug("comparing %s with %s\n", string, p);
993 if (strcmp(string, p) == 0)
994 return i; /* Found it; return index */
995 }
996 return -ENODATA;
997}
998EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200999
1000/**
1001 * of_property_count_strings - Find and return the number of strings from a
1002 * multiple strings property.
1003 * @np: device node from which the property value is to be read.
1004 * @propname: name of the property to be searched.
1005 *
1006 * Search for a property in a device tree node and retrieve the number of null
1007 * terminated string contain in it. Returns the number of strings on
1008 * success, -EINVAL if the property does not exist, -ENODATA if property
1009 * does not have a value, and -EILSEQ if the string is not null-terminated
1010 * within the length of the property data.
1011 */
1012int of_property_count_strings(struct device_node *np, const char *propname)
1013{
1014 struct property *prop = of_find_property(np, propname, NULL);
1015 int i = 0;
1016 size_t l = 0, total = 0;
1017 const char *p;
1018
1019 if (!prop)
1020 return -EINVAL;
1021 if (!prop->value)
1022 return -ENODATA;
1023 if (strnlen(prop->value, prop->length) >= prop->length)
1024 return -EILSEQ;
1025
1026 p = prop->value;
1027
Benoit Cousson88af7f52011-12-05 15:23:54 +01001028 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001029 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001030
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001031 return i;
1032}
1033EXPORT_SYMBOL_GPL(of_property_count_strings);
1034
1035/**
Grant Likely739649c2009-04-25 12:52:40 +00001036 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1037 * @np: Pointer to device node holding phandle property
1038 * @phandle_name: Name of property holding a phandle value
1039 * @index: For properties holding a table of phandles, this is the index into
1040 * the table
1041 *
1042 * Returns the device_node pointer with refcount incremented. Use
1043 * of_node_put() on it when done.
1044 */
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +01001045struct device_node *of_parse_phandle(const struct device_node *np,
1046 const char *phandle_name, int index)
Grant Likely739649c2009-04-25 12:52:40 +00001047{
Grant Likely9a6b2e52010-07-23 01:48:25 -06001048 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +00001049 int size;
1050
1051 phandle = of_get_property(np, phandle_name, &size);
1052 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1053 return NULL;
1054
Grant Likely9a6b2e52010-07-23 01:48:25 -06001055 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +00001056}
1057EXPORT_SYMBOL(of_parse_phandle);
1058
1059/**
Grant Likely15c9a0a2011-12-12 09:25:57 -07001060 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001061 * @np: pointer to a device tree node containing a list
1062 * @list_name: property name that contains a list
1063 * @cells_name: property name that specifies phandles' arguments count
1064 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -07001065 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001066 *
1067 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001068 * Returns 0 on success and fills out_args, on error returns appropriate
1069 * errno value.
1070 *
1071 * Caller is responsible to call of_node_put() on the returned out_args->node
1072 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001073 *
1074 * Example:
1075 *
1076 * phandle1: node1 {
1077 * #list-cells = <2>;
1078 * }
1079 *
1080 * phandle2: node2 {
1081 * #list-cells = <1>;
1082 * }
1083 *
1084 * node3 {
1085 * list = <&phandle1 1 2 &phandle2 3>;
1086 * }
1087 *
1088 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001089 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001090 */
Guennadi Liakhovetski93c667c2012-12-10 20:41:30 +01001091int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001092 const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -07001093 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001094{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001095 const __be32 *list, *list_end;
1096 int size, cur_index = 0;
1097 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001098 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001099 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001100
Grant Likely15c9a0a2011-12-12 09:25:57 -07001101 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001102 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001103 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001104 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001105 list_end = list + size / sizeof(*list);
1106
Grant Likely15c9a0a2011-12-12 09:25:57 -07001107 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001108 while (list < list_end) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001109 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001110
Grant Likely15c9a0a2011-12-12 09:25:57 -07001111 /*
1112 * If phandle is 0, then it is an empty entry with no
1113 * arguments. Skip forward to the next entry.
1114 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001115 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001116 if (phandle) {
1117 /*
1118 * Find the provider node and parse the #*-cells
1119 * property to determine the argument length
1120 */
1121 node = of_find_node_by_phandle(phandle);
1122 if (!node) {
1123 pr_err("%s: could not find phandle\n",
1124 np->full_name);
1125 break;
1126 }
1127 if (of_property_read_u32(node, cells_name, &count)) {
1128 pr_err("%s: could not get %s for %s\n",
1129 np->full_name, cells_name,
1130 node->full_name);
1131 break;
1132 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001133
Grant Likely15c9a0a2011-12-12 09:25:57 -07001134 /*
1135 * Make sure that the arguments actually fit in the
1136 * remaining property data length
1137 */
1138 if (list + count > list_end) {
1139 pr_err("%s: arguments longer than property\n",
1140 np->full_name);
1141 break;
1142 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001143 }
1144
Grant Likely15c9a0a2011-12-12 09:25:57 -07001145 /*
1146 * All of the error cases above bail out of the loop, so at
1147 * this point, the parsing is successful. If the requested
1148 * index matches, then fill the out_args structure and return,
1149 * or return -ENOENT for an empty entry.
1150 */
1151 if (cur_index == index) {
1152 if (!phandle)
1153 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001154
Grant Likely15c9a0a2011-12-12 09:25:57 -07001155 if (out_args) {
1156 int i;
1157 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1158 count = MAX_PHANDLE_ARGS;
1159 out_args->np = node;
1160 out_args->args_count = count;
1161 for (i = 0; i < count; i++)
1162 out_args->args[i] = be32_to_cpup(list++);
1163 }
1164 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001165 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001166
1167 of_node_put(node);
1168 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001169 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001170 cur_index++;
1171 }
1172
Grant Likely15c9a0a2011-12-12 09:25:57 -07001173 /* Loop exited without finding a valid entry; return an error */
1174 if (node)
1175 of_node_put(node);
1176 return -EINVAL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001177}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001178EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001179
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001180#if defined(CONFIG_OF_DYNAMIC)
1181static int of_property_notify(int action, struct device_node *np,
1182 struct property *prop)
1183{
1184 struct of_prop_reconfig pr;
1185
1186 pr.dn = np;
1187 pr.prop = prop;
1188 return of_reconfig_notify(action, &pr);
1189}
1190#else
1191static int of_property_notify(int action, struct device_node *np,
1192 struct property *prop)
1193{
1194 return 0;
1195}
1196#endif
1197
Grant Likely02af11b2009-11-23 20:16:45 -07001198/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001199 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001200 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001201int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001202{
1203 struct property **next;
1204 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001205 int rc;
1206
1207 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1208 if (rc)
1209 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001210
1211 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001212 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001213 next = &np->properties;
1214 while (*next) {
1215 if (strcmp(prop->name, (*next)->name) == 0) {
1216 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001217 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001218 return -1;
1219 }
1220 next = &(*next)->next;
1221 }
1222 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001223 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001224
1225#ifdef CONFIG_PROC_DEVICETREE
1226 /* try to add to proc as well if it was initialized */
1227 if (np->pde)
1228 proc_device_tree_add_prop(np->pde, prop);
1229#endif /* CONFIG_PROC_DEVICETREE */
1230
1231 return 0;
1232}
1233
1234/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001235 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001236 *
1237 * Note that we don't actually remove it, since we have given out
1238 * who-knows-how-many pointers to the data using get-property.
1239 * Instead we just move the property to the "dead properties"
1240 * list, so it won't be found any more.
1241 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001242int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001243{
1244 struct property **next;
1245 unsigned long flags;
1246 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001247 int rc;
1248
1249 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1250 if (rc)
1251 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001252
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001253 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001254 next = &np->properties;
1255 while (*next) {
1256 if (*next == prop) {
1257 /* found the node */
1258 *next = prop->next;
1259 prop->next = np->deadprops;
1260 np->deadprops = prop;
1261 found = 1;
1262 break;
1263 }
1264 next = &(*next)->next;
1265 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001266 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001267
1268 if (!found)
1269 return -ENODEV;
1270
1271#ifdef CONFIG_PROC_DEVICETREE
1272 /* try to remove the proc node as well */
1273 if (np->pde)
1274 proc_device_tree_remove_prop(np->pde, prop);
1275#endif /* CONFIG_PROC_DEVICETREE */
1276
1277 return 0;
1278}
1279
1280/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001281 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001282 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001283 *
1284 * Note that we don't actually remove it, since we have given out
1285 * who-knows-how-many pointers to the data using get-property.
1286 * Instead we just move the property to the "dead properties" list,
1287 * and add the new property to the property list
1288 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001289int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001290{
Dong Aisheng475d0092012-07-11 15:16:37 +10001291 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001292 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001293 int rc, found = 0;
1294
1295 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1296 if (rc)
1297 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001298
Dong Aisheng475d0092012-07-11 15:16:37 +10001299 if (!newprop->name)
1300 return -EINVAL;
1301
1302 oldprop = of_find_property(np, newprop->name, NULL);
1303 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001304 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001305
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001306 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001307 next = &np->properties;
1308 while (*next) {
1309 if (*next == oldprop) {
1310 /* found the node */
1311 newprop->next = oldprop->next;
1312 *next = newprop;
1313 oldprop->next = np->deadprops;
1314 np->deadprops = oldprop;
1315 found = 1;
1316 break;
1317 }
1318 next = &(*next)->next;
1319 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001320 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001321
1322 if (!found)
1323 return -ENODEV;
1324
1325#ifdef CONFIG_PROC_DEVICETREE
1326 /* try to add to proc as well if it was initialized */
1327 if (np->pde)
1328 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1329#endif /* CONFIG_PROC_DEVICETREE */
1330
1331 return 0;
1332}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001333
1334#if defined(CONFIG_OF_DYNAMIC)
1335/*
1336 * Support for dynamic device trees.
1337 *
1338 * On some platforms, the device tree can be manipulated at runtime.
1339 * The routines in this section support adding, removing and changing
1340 * device tree nodes.
1341 */
1342
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001343static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1344
1345int of_reconfig_notifier_register(struct notifier_block *nb)
1346{
1347 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1348}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001349EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001350
1351int of_reconfig_notifier_unregister(struct notifier_block *nb)
1352{
1353 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1354}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001355EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001356
1357int of_reconfig_notify(unsigned long action, void *p)
1358{
1359 int rc;
1360
1361 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1362 return notifier_to_errno(rc);
1363}
1364
Nathan Fontenote81b3292012-10-02 16:55:01 +00001365#ifdef CONFIG_PROC_DEVICETREE
1366static void of_add_proc_dt_entry(struct device_node *dn)
1367{
1368 struct proc_dir_entry *ent;
1369
1370 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1371 if (ent)
1372 proc_device_tree_add_node(dn, ent);
1373}
1374#else
1375static void of_add_proc_dt_entry(struct device_node *dn)
1376{
1377 return;
1378}
1379#endif
1380
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001381/**
1382 * of_attach_node - Plug a device node into the tree and global list.
1383 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001384int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001385{
1386 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001387 int rc;
1388
1389 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1390 if (rc)
1391 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001392
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001393 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001394 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001395 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001396 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001397 of_allnodes = np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001398 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001399
1400 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001401 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001402}
1403
Nathan Fontenote81b3292012-10-02 16:55:01 +00001404#ifdef CONFIG_PROC_DEVICETREE
1405static void of_remove_proc_dt_entry(struct device_node *dn)
1406{
1407 struct device_node *parent = dn->parent;
1408 struct property *prop = dn->properties;
1409
1410 while (prop) {
1411 remove_proc_entry(prop->name, dn->pde);
1412 prop = prop->next;
1413 }
1414
1415 if (dn->pde)
1416 remove_proc_entry(dn->pde->name, parent->pde);
1417}
1418#else
1419static void of_remove_proc_dt_entry(struct device_node *dn)
1420{
1421 return;
1422}
1423#endif
1424
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001425/**
1426 * of_detach_node - "Unplug" a node from the device tree.
1427 *
1428 * The caller must hold a reference to the node. The memory associated with
1429 * the node is not freed until its refcount goes to zero.
1430 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001431int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001432{
1433 struct device_node *parent;
1434 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001435 int rc = 0;
1436
1437 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1438 if (rc)
1439 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001440
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001441 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001442
Nathan Fontenote81b3292012-10-02 16:55:01 +00001443 if (of_node_check_flag(np, OF_DETACHED)) {
1444 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001445 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001446 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001447 }
1448
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001449 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001450 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001451 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001452 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001453 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001454
Randy Dunlap465aac62012-11-30 10:01:51 +00001455 if (of_allnodes == np)
1456 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001457 else {
1458 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001459 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001460 prev->allnext != np;
1461 prev = prev->allnext)
1462 ;
1463 prev->allnext = np->allnext;
1464 }
1465
1466 if (parent->child == np)
1467 parent->child = np->sibling;
1468 else {
1469 struct device_node *prevsib;
1470 for (prevsib = np->parent->child;
1471 prevsib->sibling != np;
1472 prevsib = prevsib->sibling)
1473 ;
1474 prevsib->sibling = np->sibling;
1475 }
1476
1477 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001478 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001479
1480 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001481 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001482}
1483#endif /* defined(CONFIG_OF_DYNAMIC) */
1484
Shawn Guo611cad72011-08-15 15:28:14 +08001485static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1486 int id, const char *stem, int stem_len)
1487{
1488 ap->np = np;
1489 ap->id = id;
1490 strncpy(ap->stem, stem, stem_len);
1491 ap->stem[stem_len] = 0;
1492 list_add_tail(&ap->link, &aliases_lookup);
1493 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001494 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001495}
1496
1497/**
1498 * of_alias_scan - Scan all properties of 'aliases' node
1499 *
1500 * The function scans all the properties of 'aliases' node and populate
1501 * the the global lookup table with the properties. It returns the
1502 * number of alias_prop found, or error code in error case.
1503 *
1504 * @dt_alloc: An allocator that provides a virtual address to memory
1505 * for the resulting tree
1506 */
1507void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1508{
1509 struct property *pp;
1510
1511 of_chosen = of_find_node_by_path("/chosen");
1512 if (of_chosen == NULL)
1513 of_chosen = of_find_node_by_path("/chosen@0");
1514 of_aliases = of_find_node_by_path("/aliases");
1515 if (!of_aliases)
1516 return;
1517
Dong Aisheng8af0da92011-12-22 20:19:24 +08001518 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001519 const char *start = pp->name;
1520 const char *end = start + strlen(start);
1521 struct device_node *np;
1522 struct alias_prop *ap;
1523 int id, len;
1524
1525 /* Skip those we do not want to proceed */
1526 if (!strcmp(pp->name, "name") ||
1527 !strcmp(pp->name, "phandle") ||
1528 !strcmp(pp->name, "linux,phandle"))
1529 continue;
1530
1531 np = of_find_node_by_path(pp->value);
1532 if (!np)
1533 continue;
1534
1535 /* walk the alias backwards to extract the id and work out
1536 * the 'stem' string */
1537 while (isdigit(*(end-1)) && end > start)
1538 end--;
1539 len = end - start;
1540
1541 if (kstrtoint(end, 10, &id) < 0)
1542 continue;
1543
1544 /* Allocate an alias_prop with enough space for the stem */
1545 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1546 if (!ap)
1547 continue;
1548 ap->alias = start;
1549 of_alias_add(ap, np, id, start, len);
1550 }
1551}
1552
1553/**
1554 * of_alias_get_id - Get alias id for the given device_node
1555 * @np: Pointer to the given device_node
1556 * @stem: Alias stem of the given device_node
1557 *
1558 * The function travels the lookup table to get alias id for the given
1559 * device_node and alias stem. It returns the alias id if find it.
1560 */
1561int of_alias_get_id(struct device_node *np, const char *stem)
1562{
1563 struct alias_prop *app;
1564 int id = -ENODEV;
1565
1566 mutex_lock(&of_aliases_mutex);
1567 list_for_each_entry(app, &aliases_lookup, link) {
1568 if (strcmp(app->stem, stem) != 0)
1569 continue;
1570
1571 if (np == app->np) {
1572 id = app->id;
1573 break;
1574 }
1575 }
1576 mutex_unlock(&of_aliases_mutex);
1577
1578 return id;
1579}
1580EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001581
1582const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1583 u32 *pu)
1584{
1585 const void *curv = cur;
1586
1587 if (!prop)
1588 return NULL;
1589
1590 if (!cur) {
1591 curv = prop->value;
1592 goto out_val;
1593 }
1594
1595 curv += sizeof(*cur);
1596 if (curv >= prop->value + prop->length)
1597 return NULL;
1598
1599out_val:
1600 *pu = be32_to_cpup(curv);
1601 return curv;
1602}
1603EXPORT_SYMBOL_GPL(of_prop_next_u32);
1604
1605const char *of_prop_next_string(struct property *prop, const char *cur)
1606{
1607 const void *curv = cur;
1608
1609 if (!prop)
1610 return NULL;
1611
1612 if (!cur)
1613 return prop->value;
1614
1615 curv += strlen(cur) + 1;
1616 if (curv >= prop->value + prop->length)
1617 return NULL;
1618
1619 return curv;
1620}
1621EXPORT_SYMBOL_GPL(of_prop_next_string);