blob: 16ee7a08e044b8c4dfb2a34d7dde456987892f0e [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 */
58DEFINE_RWLOCK(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;
191
192 read_lock(&devtree_lock);
193 pp = __of_find_property(np, name, lenp);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000194 read_unlock(&devtree_lock);
195
196 return pp;
197}
198EXPORT_SYMBOL(of_find_property);
199
Grant Likelye91edcf2009-10-15 10:58:09 -0600200/**
201 * of_find_all_nodes - Get next node in global list
202 * @prev: Previous node or NULL to start iteration
203 * of_node_put() will be called on it
204 *
205 * Returns a node pointer with refcount incremented, use
206 * of_node_put() on it when done.
207 */
208struct device_node *of_find_all_nodes(struct device_node *prev)
209{
210 struct device_node *np;
211
212 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000213 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600214 for (; np != NULL; np = np->allnext)
215 if (of_node_get(np))
216 break;
217 of_node_put(prev);
218 read_unlock(&devtree_lock);
219 return np;
220}
221EXPORT_SYMBOL(of_find_all_nodes);
222
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000223/*
224 * Find a property with a given name for a given node
225 * and return the value.
226 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500227static const void *__of_get_property(const struct device_node *np,
228 const char *name, int *lenp)
229{
230 struct property *pp = __of_find_property(np, name, lenp);
231
232 return pp ? pp->value : NULL;
233}
234
235/*
236 * Find a property with a given name for a given node
237 * and return the value.
238 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000239const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500240 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000241{
242 struct property *pp = of_find_property(np, name, lenp);
243
244 return pp ? pp->value : NULL;
245}
246EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000247
248/** Checks if the given "compat" string matches one of the strings in
249 * the device's "compatible" property
250 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500251static int __of_device_is_compatible(const struct device_node *device,
252 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000253{
254 const char* cp;
255 int cplen, l;
256
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500257 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000258 if (cp == NULL)
259 return 0;
260 while (cplen > 0) {
261 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
262 return 1;
263 l = strlen(cp) + 1;
264 cp += l;
265 cplen -= l;
266 }
267
268 return 0;
269}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500270
271/** Checks if the given "compat" string matches one of the strings in
272 * the device's "compatible" property
273 */
274int of_device_is_compatible(const struct device_node *device,
275 const char *compat)
276{
277 int res;
278
279 read_lock(&devtree_lock);
280 res = __of_device_is_compatible(device, compat);
281 read_unlock(&devtree_lock);
282 return res;
283}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000284EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000285
286/**
Grant Likely71a157e2010-02-01 21:34:14 -0700287 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700288 * @compat: compatible string to look for in root node's compatible property.
289 *
290 * Returns true if the root node has the given value in its
291 * compatible property.
292 */
Grant Likely71a157e2010-02-01 21:34:14 -0700293int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700294{
295 struct device_node *root;
296 int rc = 0;
297
298 root = of_find_node_by_path("/");
299 if (root) {
300 rc = of_device_is_compatible(root, compat);
301 of_node_put(root);
302 }
303 return rc;
304}
Grant Likely71a157e2010-02-01 21:34:14 -0700305EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700306
307/**
Josh Boyer834d97d2008-03-27 00:33:14 +1100308 * of_device_is_available - check if a device is available for use
309 *
310 * @device: Node to check for availability
311 *
312 * Returns 1 if the status property is absent or set to "okay" or "ok",
313 * 0 otherwise
314 */
315int of_device_is_available(const struct device_node *device)
316{
317 const char *status;
318 int statlen;
319
320 status = of_get_property(device, "status", &statlen);
321 if (status == NULL)
322 return 1;
323
324 if (statlen > 0) {
325 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
326 return 1;
327 }
328
329 return 0;
330}
331EXPORT_SYMBOL(of_device_is_available);
332
333/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000334 * of_get_parent - Get a node's parent if any
335 * @node: Node to get parent
336 *
337 * Returns a node pointer with refcount incremented, use
338 * of_node_put() on it when done.
339 */
340struct device_node *of_get_parent(const struct device_node *node)
341{
342 struct device_node *np;
343
344 if (!node)
345 return NULL;
346
347 read_lock(&devtree_lock);
348 np = of_node_get(node->parent);
349 read_unlock(&devtree_lock);
350 return np;
351}
352EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000353
354/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000355 * of_get_next_parent - Iterate to a node's parent
356 * @node: Node to get parent of
357 *
358 * This is like of_get_parent() except that it drops the
359 * refcount on the passed node, making it suitable for iterating
360 * through a node's parents.
361 *
362 * Returns a node pointer with refcount incremented, use
363 * of_node_put() on it when done.
364 */
365struct device_node *of_get_next_parent(struct device_node *node)
366{
367 struct device_node *parent;
368
369 if (!node)
370 return NULL;
371
372 read_lock(&devtree_lock);
373 parent = of_node_get(node->parent);
374 of_node_put(node);
375 read_unlock(&devtree_lock);
376 return parent;
377}
378
379/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000380 * of_get_next_child - Iterate a node childs
381 * @node: parent node
382 * @prev: previous child of the parent node, or NULL to get first
383 *
384 * Returns a node pointer with refcount incremented, use
385 * of_node_put() on it when done.
386 */
387struct device_node *of_get_next_child(const struct device_node *node,
388 struct device_node *prev)
389{
390 struct device_node *next;
391
392 read_lock(&devtree_lock);
393 next = prev ? prev->sibling : node->child;
394 for (; next; next = next->sibling)
395 if (of_node_get(next))
396 break;
397 of_node_put(prev);
398 read_unlock(&devtree_lock);
399 return next;
400}
401EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000402
403/**
Timur Tabi32961932012-08-14 13:20:23 +0000404 * of_get_next_available_child - Find the next available child node
405 * @node: parent node
406 * @prev: previous child of the parent node, or NULL to get first
407 *
408 * This function is like of_get_next_child(), except that it
409 * automatically skips any disabled nodes (i.e. status = "disabled").
410 */
411struct device_node *of_get_next_available_child(const struct device_node *node,
412 struct device_node *prev)
413{
414 struct device_node *next;
415
416 read_lock(&devtree_lock);
417 next = prev ? prev->sibling : node->child;
418 for (; next; next = next->sibling) {
419 if (!of_device_is_available(next))
420 continue;
421 if (of_node_get(next))
422 break;
423 }
424 of_node_put(prev);
425 read_unlock(&devtree_lock);
426 return next;
427}
428EXPORT_SYMBOL(of_get_next_available_child);
429
430/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100431 * of_get_child_by_name - Find the child node by name for a given parent
432 * @node: parent node
433 * @name: child name to look for.
434 *
435 * This function looks for child node for given matching name
436 *
437 * Returns a node pointer if found, with refcount incremented, use
438 * of_node_put() on it when done.
439 * Returns NULL if node is not found.
440 */
441struct device_node *of_get_child_by_name(const struct device_node *node,
442 const char *name)
443{
444 struct device_node *child;
445
446 for_each_child_of_node(node, child)
447 if (child->name && (of_node_cmp(child->name, name) == 0))
448 break;
449 return child;
450}
451EXPORT_SYMBOL(of_get_child_by_name);
452
453/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000454 * of_find_node_by_path - Find a node matching a full OF path
455 * @path: The full path to match
456 *
457 * Returns a node pointer with refcount incremented, use
458 * of_node_put() on it when done.
459 */
460struct device_node *of_find_node_by_path(const char *path)
461{
Randy Dunlap465aac62012-11-30 10:01:51 +0000462 struct device_node *np = of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000463
464 read_lock(&devtree_lock);
465 for (; np; np = np->allnext) {
466 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
467 && of_node_get(np))
468 break;
469 }
470 read_unlock(&devtree_lock);
471 return np;
472}
473EXPORT_SYMBOL(of_find_node_by_path);
474
475/**
476 * of_find_node_by_name - Find a node by its "name" property
477 * @from: The node to start searching from or NULL, the node
478 * you pass will not be searched, only the next one
479 * will; typically, you pass what the previous call
480 * returned. of_node_put() will be called on it
481 * @name: The name string to match against
482 *
483 * Returns a node pointer with refcount incremented, use
484 * of_node_put() on it when done.
485 */
486struct device_node *of_find_node_by_name(struct device_node *from,
487 const char *name)
488{
489 struct device_node *np;
490
491 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000492 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000493 for (; np; np = np->allnext)
494 if (np->name && (of_node_cmp(np->name, name) == 0)
495 && of_node_get(np))
496 break;
497 of_node_put(from);
498 read_unlock(&devtree_lock);
499 return np;
500}
501EXPORT_SYMBOL(of_find_node_by_name);
502
503/**
504 * of_find_node_by_type - Find a node by its "device_type" property
505 * @from: The node to start searching from, or NULL to start searching
506 * the entire device tree. The node you pass will not be
507 * searched, only the next one will; typically, you pass
508 * what the previous call returned. of_node_put() will be
509 * called on from for you.
510 * @type: The type string to match against
511 *
512 * Returns a node pointer with refcount incremented, use
513 * of_node_put() on it when done.
514 */
515struct device_node *of_find_node_by_type(struct device_node *from,
516 const char *type)
517{
518 struct device_node *np;
519
520 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000521 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000522 for (; np; np = np->allnext)
523 if (np->type && (of_node_cmp(np->type, type) == 0)
524 && of_node_get(np))
525 break;
526 of_node_put(from);
527 read_unlock(&devtree_lock);
528 return np;
529}
530EXPORT_SYMBOL(of_find_node_by_type);
531
532/**
533 * of_find_compatible_node - Find a node based on type and one of the
534 * tokens in its "compatible" property
535 * @from: The node to start searching from or NULL, the node
536 * you pass will not be searched, only the next one
537 * will; typically, you pass what the previous call
538 * returned. of_node_put() will be called on it
539 * @type: The type string to match "device_type" or NULL to ignore
540 * @compatible: The string to match to one of the tokens in the device
541 * "compatible" list.
542 *
543 * Returns a node pointer with refcount incremented, use
544 * of_node_put() on it when done.
545 */
546struct device_node *of_find_compatible_node(struct device_node *from,
547 const char *type, const char *compatible)
548{
549 struct device_node *np;
550
551 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000552 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000553 for (; np; np = np->allnext) {
554 if (type
555 && !(np->type && (of_node_cmp(np->type, type) == 0)))
556 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500557 if (__of_device_is_compatible(np, compatible) &&
558 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000559 break;
560 }
561 of_node_put(from);
562 read_unlock(&devtree_lock);
563 return np;
564}
565EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100566
567/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000568 * of_find_node_with_property - Find a node which has a property with
569 * the given name.
570 * @from: The node to start searching from or NULL, the node
571 * you pass will not be searched, only the next one
572 * will; typically, you pass what the previous call
573 * returned. of_node_put() will be called on it
574 * @prop_name: The name of the property to look for.
575 *
576 * Returns a node pointer with refcount incremented, use
577 * of_node_put() on it when done.
578 */
579struct device_node *of_find_node_with_property(struct device_node *from,
580 const char *prop_name)
581{
582 struct device_node *np;
583 struct property *pp;
584
585 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000586 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000587 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530588 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000589 if (of_prop_cmp(pp->name, prop_name) == 0) {
590 of_node_get(np);
591 goto out;
592 }
593 }
594 }
595out:
596 of_node_put(from);
597 read_unlock(&devtree_lock);
598 return np;
599}
600EXPORT_SYMBOL(of_find_node_with_property);
601
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500602static
603const struct of_device_id *__of_match_node(const struct of_device_id *matches,
604 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100605{
Grant Likelya52f07e2011-03-18 10:21:29 -0600606 if (!matches)
607 return NULL;
608
Grant Likely283029d2008-01-09 06:20:40 +1100609 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
610 int match = 1;
611 if (matches->name[0])
612 match &= node->name
613 && !strcmp(matches->name, node->name);
614 if (matches->type[0])
615 match &= node->type
616 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700617 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500618 match &= __of_device_is_compatible(node,
619 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700620 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100621 return matches;
622 matches++;
623 }
624 return NULL;
625}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500626
627/**
628 * of_match_node - Tell if an device_node has a matching of_match structure
629 * @matches: array of of device match structures to search in
630 * @node: the of device structure to match against
631 *
632 * Low level utility function used by device matching.
633 */
634const struct of_device_id *of_match_node(const struct of_device_id *matches,
635 const struct device_node *node)
636{
637 const struct of_device_id *match;
638
639 read_lock(&devtree_lock);
640 match = __of_match_node(matches, node);
641 read_unlock(&devtree_lock);
642 return match;
643}
Grant Likely283029d2008-01-09 06:20:40 +1100644EXPORT_SYMBOL(of_match_node);
645
646/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700647 * of_find_matching_node_and_match - Find a node based on an of_device_id
648 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100649 * @from: The node to start searching from or NULL, the node
650 * you pass will not be searched, only the next one
651 * will; typically, you pass what the previous call
652 * returned. of_node_put() will be called on it
653 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700654 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100655 *
656 * Returns a node pointer with refcount incremented, use
657 * of_node_put() on it when done.
658 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700659struct device_node *of_find_matching_node_and_match(struct device_node *from,
660 const struct of_device_id *matches,
661 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100662{
663 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800664 const struct of_device_id *m;
Grant Likely283029d2008-01-09 06:20:40 +1100665
Stephen Warren50c8af42012-11-20 16:12:20 -0700666 if (match)
667 *match = NULL;
668
Grant Likely283029d2008-01-09 06:20:40 +1100669 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000670 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100671 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500672 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800673 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700674 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800675 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100676 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700677 }
Grant Likely283029d2008-01-09 06:20:40 +1100678 }
679 of_node_put(from);
680 read_unlock(&devtree_lock);
681 return np;
682}
Grant Likely80c20222012-12-19 10:45:36 +0000683EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400684
685/**
Grant Likely3f07af42008-07-25 22:25:13 -0400686 * of_modalias_node - Lookup appropriate modalias for a device node
687 * @node: pointer to a device tree node
688 * @modalias: Pointer to buffer that modalias value will be copied into
689 * @len: Length of modalias value
690 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600691 * Based on the value of the compatible property, this routine will attempt
692 * to choose an appropriate modalias value for a particular device tree node.
693 * It does this by stripping the manufacturer prefix (as delimited by a ',')
694 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400695 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600696 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400697 */
698int of_modalias_node(struct device_node *node, char *modalias, int len)
699{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600700 const char *compatible, *p;
701 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400702
703 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600704 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400705 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400706 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600707 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400708 return 0;
709}
710EXPORT_SYMBOL_GPL(of_modalias_node);
711
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000712/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700713 * of_find_node_by_phandle - Find a node given a phandle
714 * @handle: phandle of the node to find
715 *
716 * Returns a node pointer with refcount incremented, use
717 * of_node_put() on it when done.
718 */
719struct device_node *of_find_node_by_phandle(phandle handle)
720{
721 struct device_node *np;
722
723 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000724 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700725 if (np->phandle == handle)
726 break;
727 of_node_get(np);
728 read_unlock(&devtree_lock);
729 return np;
730}
731EXPORT_SYMBOL(of_find_node_by_phandle);
732
733/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530734 * of_property_read_u8_array - Find and read an array of u8 from a property.
735 *
736 * @np: device node from which the property value is to be read.
737 * @propname: name of the property to be searched.
738 * @out_value: pointer to return value, modified only if return value is 0.
739 * @sz: number of array elements to read
740 *
741 * Search for a property in a device node and read 8-bit value(s) from
742 * it. Returns 0 on success, -EINVAL if the property does not exist,
743 * -ENODATA if property does not have a value, and -EOVERFLOW if the
744 * property data isn't large enough.
745 *
746 * dts entry of array should be like:
747 * property = /bits/ 8 <0x50 0x60 0x70>;
748 *
749 * The out_value is modified only if a valid u8 value can be decoded.
750 */
751int of_property_read_u8_array(const struct device_node *np,
752 const char *propname, u8 *out_values, size_t sz)
753{
754 struct property *prop = of_find_property(np, propname, NULL);
755 const u8 *val;
756
757 if (!prop)
758 return -EINVAL;
759 if (!prop->value)
760 return -ENODATA;
761 if ((sz * sizeof(*out_values)) > prop->length)
762 return -EOVERFLOW;
763
764 val = prop->value;
765 while (sz--)
766 *out_values++ = *val++;
767 return 0;
768}
769EXPORT_SYMBOL_GPL(of_property_read_u8_array);
770
771/**
772 * of_property_read_u16_array - Find and read an array of u16 from a property.
773 *
774 * @np: device node from which the property value is to be read.
775 * @propname: name of the property to be searched.
776 * @out_value: pointer to return value, modified only if return value is 0.
777 * @sz: number of array elements to read
778 *
779 * Search for a property in a device node and read 16-bit value(s) from
780 * it. Returns 0 on success, -EINVAL if the property does not exist,
781 * -ENODATA if property does not have a value, and -EOVERFLOW if the
782 * property data isn't large enough.
783 *
784 * dts entry of array should be like:
785 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
786 *
787 * The out_value is modified only if a valid u16 value can be decoded.
788 */
789int of_property_read_u16_array(const struct device_node *np,
790 const char *propname, u16 *out_values, size_t sz)
791{
792 struct property *prop = of_find_property(np, propname, NULL);
793 const __be16 *val;
794
795 if (!prop)
796 return -EINVAL;
797 if (!prop->value)
798 return -ENODATA;
799 if ((sz * sizeof(*out_values)) > prop->length)
800 return -EOVERFLOW;
801
802 val = prop->value;
803 while (sz--)
804 *out_values++ = be16_to_cpup(val++);
805 return 0;
806}
807EXPORT_SYMBOL_GPL(of_property_read_u16_array);
808
809/**
Rob Herring0e373632011-07-06 15:42:58 -0500810 * of_property_read_u32_array - Find and read an array of 32 bit integers
811 * from a property.
812 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530813 * @np: device node from which the property value is to be read.
814 * @propname: name of the property to be searched.
815 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530816 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530817 *
Rob Herring0e373632011-07-06 15:42:58 -0500818 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530819 * it. Returns 0 on success, -EINVAL if the property does not exist,
820 * -ENODATA if property does not have a value, and -EOVERFLOW if the
821 * property data isn't large enough.
822 *
823 * The out_value is modified only if a valid u32 value can be decoded.
824 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100825int of_property_read_u32_array(const struct device_node *np,
826 const char *propname, u32 *out_values,
827 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530828{
829 struct property *prop = of_find_property(np, propname, NULL);
Rob Herring0e373632011-07-06 15:42:58 -0500830 const __be32 *val;
Thomas Abrahama3b85362011-06-30 21:26:10 +0530831
832 if (!prop)
833 return -EINVAL;
834 if (!prop->value)
835 return -ENODATA;
Rob Herring0e373632011-07-06 15:42:58 -0500836 if ((sz * sizeof(*out_values)) > prop->length)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530837 return -EOVERFLOW;
Rob Herring0e373632011-07-06 15:42:58 -0500838
839 val = prop->value;
840 while (sz--)
841 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530842 return 0;
843}
Rob Herring0e373632011-07-06 15:42:58 -0500844EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530845
846/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100847 * of_property_read_u64 - Find and read a 64 bit integer from a property
848 * @np: device node from which the property value is to be read.
849 * @propname: name of the property to be searched.
850 * @out_value: pointer to return value, modified only if return value is 0.
851 *
852 * Search for a property in a device node and read a 64-bit value from
853 * it. Returns 0 on success, -EINVAL if the property does not exist,
854 * -ENODATA if property does not have a value, and -EOVERFLOW if the
855 * property data isn't large enough.
856 *
857 * The out_value is modified only if a valid u64 value can be decoded.
858 */
859int of_property_read_u64(const struct device_node *np, const char *propname,
860 u64 *out_value)
861{
862 struct property *prop = of_find_property(np, propname, NULL);
863
864 if (!prop)
865 return -EINVAL;
866 if (!prop->value)
867 return -ENODATA;
868 if (sizeof(*out_value) > prop->length)
869 return -EOVERFLOW;
870 *out_value = of_read_number(prop->value, 2);
871 return 0;
872}
873EXPORT_SYMBOL_GPL(of_property_read_u64);
874
875/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530876 * of_property_read_string - Find and read a string from a property
877 * @np: device node from which the property value is to be read.
878 * @propname: name of the property to be searched.
879 * @out_string: pointer to null terminated return string, modified only if
880 * return value is 0.
881 *
882 * Search for a property in a device tree node and retrieve a null
883 * terminated string value (pointer to data, not a copy). Returns 0 on
884 * success, -EINVAL if the property does not exist, -ENODATA if property
885 * does not have a value, and -EILSEQ if the string is not null-terminated
886 * within the length of the property data.
887 *
888 * The out_string pointer is modified only if a valid string can be decoded.
889 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100890int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800891 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530892{
893 struct property *prop = of_find_property(np, propname, NULL);
894 if (!prop)
895 return -EINVAL;
896 if (!prop->value)
897 return -ENODATA;
898 if (strnlen(prop->value, prop->length) >= prop->length)
899 return -EILSEQ;
900 *out_string = prop->value;
901 return 0;
902}
903EXPORT_SYMBOL_GPL(of_property_read_string);
904
905/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200906 * of_property_read_string_index - Find and read a string from a multiple
907 * strings property.
908 * @np: device node from which the property value is to be read.
909 * @propname: name of the property to be searched.
910 * @index: index of the string in the list of strings
911 * @out_string: pointer to null terminated return string, modified only if
912 * return value is 0.
913 *
914 * Search for a property in a device tree node and retrieve a null
915 * terminated string value (pointer to data, not a copy) in the list of strings
916 * contained in that property.
917 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
918 * property does not have a value, and -EILSEQ if the string is not
919 * null-terminated within the length of the property data.
920 *
921 * The out_string pointer is modified only if a valid string can be decoded.
922 */
923int of_property_read_string_index(struct device_node *np, const char *propname,
924 int index, const char **output)
925{
926 struct property *prop = of_find_property(np, propname, NULL);
927 int i = 0;
928 size_t l = 0, total = 0;
929 const char *p;
930
931 if (!prop)
932 return -EINVAL;
933 if (!prop->value)
934 return -ENODATA;
935 if (strnlen(prop->value, prop->length) >= prop->length)
936 return -EILSEQ;
937
938 p = prop->value;
939
940 for (i = 0; total < prop->length; total += l, p += l) {
941 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100942 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200943 *output = p;
944 return 0;
945 }
946 }
947 return -ENODATA;
948}
949EXPORT_SYMBOL_GPL(of_property_read_string_index);
950
Grant Likely7aff0fe2011-12-12 09:25:58 -0700951/**
952 * of_property_match_string() - Find string in a list and return index
953 * @np: pointer to node containing string list property
954 * @propname: string list property name
955 * @string: pointer to string to search for in string list
956 *
957 * This function searches a string list property and returns the index
958 * of a specific string value.
959 */
960int of_property_match_string(struct device_node *np, const char *propname,
961 const char *string)
962{
963 struct property *prop = of_find_property(np, propname, NULL);
964 size_t l;
965 int i;
966 const char *p, *end;
967
968 if (!prop)
969 return -EINVAL;
970 if (!prop->value)
971 return -ENODATA;
972
973 p = prop->value;
974 end = p + prop->length;
975
976 for (i = 0; p < end; i++, p += l) {
977 l = strlen(p) + 1;
978 if (p + l > end)
979 return -EILSEQ;
980 pr_debug("comparing %s with %s\n", string, p);
981 if (strcmp(string, p) == 0)
982 return i; /* Found it; return index */
983 }
984 return -ENODATA;
985}
986EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200987
988/**
989 * of_property_count_strings - Find and return the number of strings from a
990 * multiple strings property.
991 * @np: device node from which the property value is to be read.
992 * @propname: name of the property to be searched.
993 *
994 * Search for a property in a device tree node and retrieve the number of null
995 * terminated string contain in it. Returns the number of strings on
996 * success, -EINVAL if the property does not exist, -ENODATA if property
997 * does not have a value, and -EILSEQ if the string is not null-terminated
998 * within the length of the property data.
999 */
1000int of_property_count_strings(struct device_node *np, const char *propname)
1001{
1002 struct property *prop = of_find_property(np, propname, NULL);
1003 int i = 0;
1004 size_t l = 0, total = 0;
1005 const char *p;
1006
1007 if (!prop)
1008 return -EINVAL;
1009 if (!prop->value)
1010 return -ENODATA;
1011 if (strnlen(prop->value, prop->length) >= prop->length)
1012 return -EILSEQ;
1013
1014 p = prop->value;
1015
Benoit Cousson88af7f52011-12-05 15:23:54 +01001016 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001017 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001018
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001019 return i;
1020}
1021EXPORT_SYMBOL_GPL(of_property_count_strings);
1022
1023/**
Grant Likely739649c2009-04-25 12:52:40 +00001024 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1025 * @np: Pointer to device node holding phandle property
1026 * @phandle_name: Name of property holding a phandle value
1027 * @index: For properties holding a table of phandles, this is the index into
1028 * the table
1029 *
1030 * Returns the device_node pointer with refcount incremented. Use
1031 * of_node_put() on it when done.
1032 */
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +01001033struct device_node *of_parse_phandle(const struct device_node *np,
1034 const char *phandle_name, int index)
Grant Likely739649c2009-04-25 12:52:40 +00001035{
Grant Likely9a6b2e52010-07-23 01:48:25 -06001036 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +00001037 int size;
1038
1039 phandle = of_get_property(np, phandle_name, &size);
1040 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1041 return NULL;
1042
Grant Likely9a6b2e52010-07-23 01:48:25 -06001043 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +00001044}
1045EXPORT_SYMBOL(of_parse_phandle);
1046
1047/**
Grant Likely15c9a0a2011-12-12 09:25:57 -07001048 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001049 * @np: pointer to a device tree node containing a list
1050 * @list_name: property name that contains a list
1051 * @cells_name: property name that specifies phandles' arguments count
1052 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -07001053 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001054 *
1055 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001056 * Returns 0 on success and fills out_args, on error returns appropriate
1057 * errno value.
1058 *
1059 * Caller is responsible to call of_node_put() on the returned out_args->node
1060 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001061 *
1062 * Example:
1063 *
1064 * phandle1: node1 {
1065 * #list-cells = <2>;
1066 * }
1067 *
1068 * phandle2: node2 {
1069 * #list-cells = <1>;
1070 * }
1071 *
1072 * node3 {
1073 * list = <&phandle1 1 2 &phandle2 3>;
1074 * }
1075 *
1076 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001077 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001078 */
Guennadi Liakhovetski93c667c2012-12-10 20:41:30 +01001079int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001080 const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -07001081 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001082{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001083 const __be32 *list, *list_end;
1084 int size, cur_index = 0;
1085 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001086 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001087 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001088
Grant Likely15c9a0a2011-12-12 09:25:57 -07001089 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001090 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001091 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001092 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001093 list_end = list + size / sizeof(*list);
1094
Grant Likely15c9a0a2011-12-12 09:25:57 -07001095 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001096 while (list < list_end) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001097 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001098
Grant Likely15c9a0a2011-12-12 09:25:57 -07001099 /*
1100 * If phandle is 0, then it is an empty entry with no
1101 * arguments. Skip forward to the next entry.
1102 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001103 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001104 if (phandle) {
1105 /*
1106 * Find the provider node and parse the #*-cells
1107 * property to determine the argument length
1108 */
1109 node = of_find_node_by_phandle(phandle);
1110 if (!node) {
1111 pr_err("%s: could not find phandle\n",
1112 np->full_name);
1113 break;
1114 }
1115 if (of_property_read_u32(node, cells_name, &count)) {
1116 pr_err("%s: could not get %s for %s\n",
1117 np->full_name, cells_name,
1118 node->full_name);
1119 break;
1120 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001121
Grant Likely15c9a0a2011-12-12 09:25:57 -07001122 /*
1123 * Make sure that the arguments actually fit in the
1124 * remaining property data length
1125 */
1126 if (list + count > list_end) {
1127 pr_err("%s: arguments longer than property\n",
1128 np->full_name);
1129 break;
1130 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001131 }
1132
Grant Likely15c9a0a2011-12-12 09:25:57 -07001133 /*
1134 * All of the error cases above bail out of the loop, so at
1135 * this point, the parsing is successful. If the requested
1136 * index matches, then fill the out_args structure and return,
1137 * or return -ENOENT for an empty entry.
1138 */
1139 if (cur_index == index) {
1140 if (!phandle)
1141 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001142
Grant Likely15c9a0a2011-12-12 09:25:57 -07001143 if (out_args) {
1144 int i;
1145 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1146 count = MAX_PHANDLE_ARGS;
1147 out_args->np = node;
1148 out_args->args_count = count;
1149 for (i = 0; i < count; i++)
1150 out_args->args[i] = be32_to_cpup(list++);
1151 }
1152 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001153 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001154
1155 of_node_put(node);
1156 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001157 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001158 cur_index++;
1159 }
1160
Grant Likely15c9a0a2011-12-12 09:25:57 -07001161 /* Loop exited without finding a valid entry; return an error */
1162 if (node)
1163 of_node_put(node);
1164 return -EINVAL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001165}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001166EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001167
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001168#if defined(CONFIG_OF_DYNAMIC)
1169static int of_property_notify(int action, struct device_node *np,
1170 struct property *prop)
1171{
1172 struct of_prop_reconfig pr;
1173
1174 pr.dn = np;
1175 pr.prop = prop;
1176 return of_reconfig_notify(action, &pr);
1177}
1178#else
1179static int of_property_notify(int action, struct device_node *np,
1180 struct property *prop)
1181{
1182 return 0;
1183}
1184#endif
1185
Grant Likely02af11b2009-11-23 20:16:45 -07001186/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001187 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001188 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001189int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001190{
1191 struct property **next;
1192 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001193 int rc;
1194
1195 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1196 if (rc)
1197 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001198
1199 prop->next = NULL;
1200 write_lock_irqsave(&devtree_lock, flags);
1201 next = &np->properties;
1202 while (*next) {
1203 if (strcmp(prop->name, (*next)->name) == 0) {
1204 /* duplicate ! don't insert it */
1205 write_unlock_irqrestore(&devtree_lock, flags);
1206 return -1;
1207 }
1208 next = &(*next)->next;
1209 }
1210 *next = prop;
1211 write_unlock_irqrestore(&devtree_lock, flags);
1212
1213#ifdef CONFIG_PROC_DEVICETREE
1214 /* try to add to proc as well if it was initialized */
1215 if (np->pde)
1216 proc_device_tree_add_prop(np->pde, prop);
1217#endif /* CONFIG_PROC_DEVICETREE */
1218
1219 return 0;
1220}
1221
1222/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001223 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001224 *
1225 * Note that we don't actually remove it, since we have given out
1226 * who-knows-how-many pointers to the data using get-property.
1227 * Instead we just move the property to the "dead properties"
1228 * list, so it won't be found any more.
1229 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001230int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001231{
1232 struct property **next;
1233 unsigned long flags;
1234 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001235 int rc;
1236
1237 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1238 if (rc)
1239 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001240
1241 write_lock_irqsave(&devtree_lock, flags);
1242 next = &np->properties;
1243 while (*next) {
1244 if (*next == prop) {
1245 /* found the node */
1246 *next = prop->next;
1247 prop->next = np->deadprops;
1248 np->deadprops = prop;
1249 found = 1;
1250 break;
1251 }
1252 next = &(*next)->next;
1253 }
1254 write_unlock_irqrestore(&devtree_lock, flags);
1255
1256 if (!found)
1257 return -ENODEV;
1258
1259#ifdef CONFIG_PROC_DEVICETREE
1260 /* try to remove the proc node as well */
1261 if (np->pde)
1262 proc_device_tree_remove_prop(np->pde, prop);
1263#endif /* CONFIG_PROC_DEVICETREE */
1264
1265 return 0;
1266}
1267
1268/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001269 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001270 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001271 *
1272 * Note that we don't actually remove it, since we have given out
1273 * who-knows-how-many pointers to the data using get-property.
1274 * Instead we just move the property to the "dead properties" list,
1275 * and add the new property to the property list
1276 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001277int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001278{
Dong Aisheng475d0092012-07-11 15:16:37 +10001279 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001280 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001281 int rc, found = 0;
1282
1283 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1284 if (rc)
1285 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001286
Dong Aisheng475d0092012-07-11 15:16:37 +10001287 if (!newprop->name)
1288 return -EINVAL;
1289
1290 oldprop = of_find_property(np, newprop->name, NULL);
1291 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001292 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001293
Grant Likely02af11b2009-11-23 20:16:45 -07001294 write_lock_irqsave(&devtree_lock, flags);
1295 next = &np->properties;
1296 while (*next) {
1297 if (*next == oldprop) {
1298 /* found the node */
1299 newprop->next = oldprop->next;
1300 *next = newprop;
1301 oldprop->next = np->deadprops;
1302 np->deadprops = oldprop;
1303 found = 1;
1304 break;
1305 }
1306 next = &(*next)->next;
1307 }
1308 write_unlock_irqrestore(&devtree_lock, flags);
1309
1310 if (!found)
1311 return -ENODEV;
1312
1313#ifdef CONFIG_PROC_DEVICETREE
1314 /* try to add to proc as well if it was initialized */
1315 if (np->pde)
1316 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1317#endif /* CONFIG_PROC_DEVICETREE */
1318
1319 return 0;
1320}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001321
1322#if defined(CONFIG_OF_DYNAMIC)
1323/*
1324 * Support for dynamic device trees.
1325 *
1326 * On some platforms, the device tree can be manipulated at runtime.
1327 * The routines in this section support adding, removing and changing
1328 * device tree nodes.
1329 */
1330
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001331static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1332
1333int of_reconfig_notifier_register(struct notifier_block *nb)
1334{
1335 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1336}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001337EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001338
1339int of_reconfig_notifier_unregister(struct notifier_block *nb)
1340{
1341 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1342}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001343EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001344
1345int of_reconfig_notify(unsigned long action, void *p)
1346{
1347 int rc;
1348
1349 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1350 return notifier_to_errno(rc);
1351}
1352
Nathan Fontenote81b3292012-10-02 16:55:01 +00001353#ifdef CONFIG_PROC_DEVICETREE
1354static void of_add_proc_dt_entry(struct device_node *dn)
1355{
1356 struct proc_dir_entry *ent;
1357
1358 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1359 if (ent)
1360 proc_device_tree_add_node(dn, ent);
1361}
1362#else
1363static void of_add_proc_dt_entry(struct device_node *dn)
1364{
1365 return;
1366}
1367#endif
1368
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001369/**
1370 * of_attach_node - Plug a device node into the tree and global list.
1371 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001372int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001373{
1374 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001375 int rc;
1376
1377 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1378 if (rc)
1379 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001380
1381 write_lock_irqsave(&devtree_lock, flags);
1382 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001383 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001384 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001385 of_allnodes = np;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001386 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001387
1388 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001389 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001390}
1391
Nathan Fontenote81b3292012-10-02 16:55:01 +00001392#ifdef CONFIG_PROC_DEVICETREE
1393static void of_remove_proc_dt_entry(struct device_node *dn)
1394{
1395 struct device_node *parent = dn->parent;
1396 struct property *prop = dn->properties;
1397
1398 while (prop) {
1399 remove_proc_entry(prop->name, dn->pde);
1400 prop = prop->next;
1401 }
1402
1403 if (dn->pde)
1404 remove_proc_entry(dn->pde->name, parent->pde);
1405}
1406#else
1407static void of_remove_proc_dt_entry(struct device_node *dn)
1408{
1409 return;
1410}
1411#endif
1412
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001413/**
1414 * of_detach_node - "Unplug" a node from the device tree.
1415 *
1416 * The caller must hold a reference to the node. The memory associated with
1417 * the node is not freed until its refcount goes to zero.
1418 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001419int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001420{
1421 struct device_node *parent;
1422 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001423 int rc = 0;
1424
1425 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1426 if (rc)
1427 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001428
1429 write_lock_irqsave(&devtree_lock, flags);
1430
Nathan Fontenote81b3292012-10-02 16:55:01 +00001431 if (of_node_check_flag(np, OF_DETACHED)) {
1432 /* someone already detached it */
1433 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001434 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001435 }
1436
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001437 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001438 if (!parent) {
1439 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001440 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001441 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001442
Randy Dunlap465aac62012-11-30 10:01:51 +00001443 if (of_allnodes == np)
1444 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001445 else {
1446 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001447 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001448 prev->allnext != np;
1449 prev = prev->allnext)
1450 ;
1451 prev->allnext = np->allnext;
1452 }
1453
1454 if (parent->child == np)
1455 parent->child = np->sibling;
1456 else {
1457 struct device_node *prevsib;
1458 for (prevsib = np->parent->child;
1459 prevsib->sibling != np;
1460 prevsib = prevsib->sibling)
1461 ;
1462 prevsib->sibling = np->sibling;
1463 }
1464
1465 of_node_set_flag(np, OF_DETACHED);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001466 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001467
1468 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001469 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001470}
1471#endif /* defined(CONFIG_OF_DYNAMIC) */
1472
Shawn Guo611cad72011-08-15 15:28:14 +08001473static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1474 int id, const char *stem, int stem_len)
1475{
1476 ap->np = np;
1477 ap->id = id;
1478 strncpy(ap->stem, stem, stem_len);
1479 ap->stem[stem_len] = 0;
1480 list_add_tail(&ap->link, &aliases_lookup);
1481 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001482 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001483}
1484
1485/**
1486 * of_alias_scan - Scan all properties of 'aliases' node
1487 *
1488 * The function scans all the properties of 'aliases' node and populate
1489 * the the global lookup table with the properties. It returns the
1490 * number of alias_prop found, or error code in error case.
1491 *
1492 * @dt_alloc: An allocator that provides a virtual address to memory
1493 * for the resulting tree
1494 */
1495void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1496{
1497 struct property *pp;
1498
1499 of_chosen = of_find_node_by_path("/chosen");
1500 if (of_chosen == NULL)
1501 of_chosen = of_find_node_by_path("/chosen@0");
1502 of_aliases = of_find_node_by_path("/aliases");
1503 if (!of_aliases)
1504 return;
1505
Dong Aisheng8af0da92011-12-22 20:19:24 +08001506 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001507 const char *start = pp->name;
1508 const char *end = start + strlen(start);
1509 struct device_node *np;
1510 struct alias_prop *ap;
1511 int id, len;
1512
1513 /* Skip those we do not want to proceed */
1514 if (!strcmp(pp->name, "name") ||
1515 !strcmp(pp->name, "phandle") ||
1516 !strcmp(pp->name, "linux,phandle"))
1517 continue;
1518
1519 np = of_find_node_by_path(pp->value);
1520 if (!np)
1521 continue;
1522
1523 /* walk the alias backwards to extract the id and work out
1524 * the 'stem' string */
1525 while (isdigit(*(end-1)) && end > start)
1526 end--;
1527 len = end - start;
1528
1529 if (kstrtoint(end, 10, &id) < 0)
1530 continue;
1531
1532 /* Allocate an alias_prop with enough space for the stem */
1533 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1534 if (!ap)
1535 continue;
1536 ap->alias = start;
1537 of_alias_add(ap, np, id, start, len);
1538 }
1539}
1540
1541/**
1542 * of_alias_get_id - Get alias id for the given device_node
1543 * @np: Pointer to the given device_node
1544 * @stem: Alias stem of the given device_node
1545 *
1546 * The function travels the lookup table to get alias id for the given
1547 * device_node and alias stem. It returns the alias id if find it.
1548 */
1549int of_alias_get_id(struct device_node *np, const char *stem)
1550{
1551 struct alias_prop *app;
1552 int id = -ENODEV;
1553
1554 mutex_lock(&of_aliases_mutex);
1555 list_for_each_entry(app, &aliases_lookup, link) {
1556 if (strcmp(app->stem, stem) != 0)
1557 continue;
1558
1559 if (np == app->np) {
1560 id = app->id;
1561 break;
1562 }
1563 }
1564 mutex_unlock(&of_aliases_mutex);
1565
1566 return id;
1567}
1568EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001569
1570const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1571 u32 *pu)
1572{
1573 const void *curv = cur;
1574
1575 if (!prop)
1576 return NULL;
1577
1578 if (!cur) {
1579 curv = prop->value;
1580 goto out_val;
1581 }
1582
1583 curv += sizeof(*cur);
1584 if (curv >= prop->value + prop->length)
1585 return NULL;
1586
1587out_val:
1588 *pu = be32_to_cpup(curv);
1589 return curv;
1590}
1591EXPORT_SYMBOL_GPL(of_prop_next_u32);
1592
1593const char *of_prop_next_string(struct property *prop, const char *cur)
1594{
1595 const void *curv = cur;
1596
1597 if (!prop)
1598 return NULL;
1599
1600 if (!cur)
1601 return prop->value;
1602
1603 curv += strlen(cur) + 1;
1604 if (curv >= prop->value + prop->length)
1605 return NULL;
1606
1607 return curv;
1608}
1609EXPORT_SYMBOL_GPL(of_prop_next_string);