blob: df8547debd057aaecd0db6a8cfa5e9ef13ad249d [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
Stepan Moskovchenko97d7b4d2012-12-03 19:19:38 -080027#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080028
Stepan Moskovchenko97d7b4d2012-12-03 19:19:38 -080029LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080030
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100031struct device_node *allnodes;
Grant Likelyfc0bdae2010-02-14 07:13:55 -070032struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080033struct device_node *of_aliases;
34
Stepan Moskovchenko97d7b4d2012-12-03 19:19:38 -080035DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100036
Stephen Rothwell581b6052007-04-24 16:46:53 +100037/* use when traversing tree through the allnext, child, sibling,
38 * or parent members of struct device_node.
39 */
40DEFINE_RWLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100041
42int of_n_addr_cells(struct device_node *np)
43{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060044 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100045
46 do {
47 if (np->parent)
48 np = np->parent;
49 ip = of_get_property(np, "#address-cells", NULL);
50 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070051 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100052 } while (np->parent);
53 /* No #address-cells property for the root node */
54 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
55}
56EXPORT_SYMBOL(of_n_addr_cells);
57
58int of_n_size_cells(struct device_node *np)
59{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060060 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100061
62 do {
63 if (np->parent)
64 np = np->parent;
65 ip = of_get_property(np, "#size-cells", NULL);
66 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070067 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100068 } while (np->parent);
69 /* No #size-cells property for the root node */
70 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
71}
72EXPORT_SYMBOL(of_n_size_cells);
73
Grant Likely0f22dd32012-02-15 20:38:40 -070074#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070075/**
76 * of_node_get - Increment refcount of a node
77 * @node: Node to inc refcount, NULL is supported to
78 * simplify writing of callers
79 *
80 * Returns node.
81 */
82struct device_node *of_node_get(struct device_node *node)
83{
84 if (node)
85 kref_get(&node->kref);
86 return node;
87}
88EXPORT_SYMBOL(of_node_get);
89
90static inline struct device_node *kref_to_device_node(struct kref *kref)
91{
92 return container_of(kref, struct device_node, kref);
93}
94
95/**
96 * of_node_release - release a dynamically allocated node
97 * @kref: kref element of the node to be released
98 *
99 * In of_node_put() this function is passed to kref_put()
100 * as the destructor.
101 */
102static void of_node_release(struct kref *kref)
103{
104 struct device_node *node = kref_to_device_node(kref);
105 struct property *prop = node->properties;
106
107 /* We should never be releasing nodes that haven't been detached. */
108 if (!of_node_check_flag(node, OF_DETACHED)) {
109 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
110 dump_stack();
111 kref_init(&node->kref);
112 return;
113 }
114
115 if (!of_node_check_flag(node, OF_DYNAMIC))
116 return;
117
118 while (prop) {
119 struct property *next = prop->next;
120 kfree(prop->name);
121 kfree(prop->value);
122 kfree(prop);
123 prop = next;
124
125 if (!prop) {
126 prop = node->deadprops;
127 node->deadprops = NULL;
128 }
129 }
130 kfree(node->full_name);
131 kfree(node->data);
132 kfree(node);
133}
134
135/**
136 * of_node_put - Decrement refcount of a node
137 * @node: Node to dec refcount, NULL is supported to
138 * simplify writing of callers
139 *
140 */
141void of_node_put(struct device_node *node)
142{
143 if (node)
144 kref_put(&node->kref, of_node_release);
145}
146EXPORT_SYMBOL(of_node_put);
Grant Likely0f22dd32012-02-15 20:38:40 -0700147#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700148
Stephen Rothwell581b6052007-04-24 16:46:53 +1000149struct property *of_find_property(const struct device_node *np,
150 const char *name,
151 int *lenp)
152{
153 struct property *pp;
154
Timur Tabi64e45662008-05-08 05:19:59 +1000155 if (!np)
156 return NULL;
157
Stephen Rothwell581b6052007-04-24 16:46:53 +1000158 read_lock(&devtree_lock);
159 for (pp = np->properties; pp != 0; pp = pp->next) {
160 if (of_prop_cmp(pp->name, name) == 0) {
161 if (lenp != 0)
162 *lenp = pp->length;
163 break;
164 }
165 }
166 read_unlock(&devtree_lock);
167
168 return pp;
169}
170EXPORT_SYMBOL(of_find_property);
171
Grant Likelye91edcf2009-10-15 10:58:09 -0600172/**
173 * of_find_all_nodes - Get next node in global list
174 * @prev: Previous node or NULL to start iteration
175 * of_node_put() will be called on it
176 *
177 * Returns a node pointer with refcount incremented, use
178 * of_node_put() on it when done.
179 */
180struct device_node *of_find_all_nodes(struct device_node *prev)
181{
182 struct device_node *np;
183
184 read_lock(&devtree_lock);
185 np = prev ? prev->allnext : allnodes;
186 for (; np != NULL; np = np->allnext)
187 if (of_node_get(np))
188 break;
189 of_node_put(prev);
190 read_unlock(&devtree_lock);
191 return np;
192}
193EXPORT_SYMBOL(of_find_all_nodes);
194
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000195/*
196 * Find a property with a given name for a given node
197 * and return the value.
198 */
199const void *of_get_property(const struct device_node *np, const char *name,
200 int *lenp)
201{
202 struct property *pp = of_find_property(np, name, lenp);
203
204 return pp ? pp->value : NULL;
205}
206EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000207
208/** Checks if the given "compat" string matches one of the strings in
209 * the device's "compatible" property
210 */
211int of_device_is_compatible(const struct device_node *device,
212 const char *compat)
213{
214 const char* cp;
215 int cplen, l;
216
217 cp = of_get_property(device, "compatible", &cplen);
218 if (cp == NULL)
219 return 0;
220 while (cplen > 0) {
221 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
222 return 1;
223 l = strlen(cp) + 1;
224 cp += l;
225 cplen -= l;
226 }
227
228 return 0;
229}
230EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000231
232/**
Grant Likely71a157e2010-02-01 21:34:14 -0700233 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700234 * @compat: compatible string to look for in root node's compatible property.
235 *
236 * Returns true if the root node has the given value in its
237 * compatible property.
238 */
Grant Likely71a157e2010-02-01 21:34:14 -0700239int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700240{
241 struct device_node *root;
242 int rc = 0;
243
244 root = of_find_node_by_path("/");
245 if (root) {
246 rc = of_device_is_compatible(root, compat);
247 of_node_put(root);
248 }
249 return rc;
250}
Grant Likely71a157e2010-02-01 21:34:14 -0700251EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700252
253/**
Josh Boyer834d97d2008-03-27 00:33:14 +1100254 * of_device_is_available - check if a device is available for use
255 *
256 * @device: Node to check for availability
257 *
258 * Returns 1 if the status property is absent or set to "okay" or "ok",
259 * 0 otherwise
260 */
261int of_device_is_available(const struct device_node *device)
262{
263 const char *status;
264 int statlen;
265
266 status = of_get_property(device, "status", &statlen);
267 if (status == NULL)
268 return 1;
269
270 if (statlen > 0) {
271 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
272 return 1;
273 }
274
275 return 0;
276}
277EXPORT_SYMBOL(of_device_is_available);
278
279/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000280 * of_get_parent - Get a node's parent if any
281 * @node: Node to get parent
282 *
283 * Returns a node pointer with refcount incremented, use
284 * of_node_put() on it when done.
285 */
286struct device_node *of_get_parent(const struct device_node *node)
287{
288 struct device_node *np;
289
290 if (!node)
291 return NULL;
292
293 read_lock(&devtree_lock);
294 np = of_node_get(node->parent);
295 read_unlock(&devtree_lock);
296 return np;
297}
298EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000299
300/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000301 * of_get_next_parent - Iterate to a node's parent
302 * @node: Node to get parent of
303 *
304 * This is like of_get_parent() except that it drops the
305 * refcount on the passed node, making it suitable for iterating
306 * through a node's parents.
307 *
308 * Returns a node pointer with refcount incremented, use
309 * of_node_put() on it when done.
310 */
311struct device_node *of_get_next_parent(struct device_node *node)
312{
313 struct device_node *parent;
314
315 if (!node)
316 return NULL;
317
318 read_lock(&devtree_lock);
319 parent = of_node_get(node->parent);
320 of_node_put(node);
321 read_unlock(&devtree_lock);
322 return parent;
323}
324
325/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000326 * of_get_next_child - Iterate a node childs
327 * @node: parent node
328 * @prev: previous child of the parent node, or NULL to get first
329 *
330 * Returns a node pointer with refcount incremented, use
331 * of_node_put() on it when done.
332 */
333struct device_node *of_get_next_child(const struct device_node *node,
334 struct device_node *prev)
335{
336 struct device_node *next;
337
338 read_lock(&devtree_lock);
339 next = prev ? prev->sibling : node->child;
340 for (; next; next = next->sibling)
341 if (of_node_get(next))
342 break;
343 of_node_put(prev);
344 read_unlock(&devtree_lock);
345 return next;
346}
347EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000348
349/**
Timur Tabi770e0df2012-08-14 13:20:23 +0000350 * of_get_next_available_child - Find the next available child node
351 * @node: parent node
352 * @prev: previous child of the parent node, or NULL to get first
353 *
354 * This function is like of_get_next_child(), except that it
355 * automatically skips any disabled nodes (i.e. status = "disabled").
356 */
357struct device_node *of_get_next_available_child(const struct device_node *node,
358 struct device_node *prev)
359{
360 struct device_node *next;
361
362 read_lock(&devtree_lock);
363 next = prev ? prev->sibling : node->child;
364 for (; next; next = next->sibling) {
365 if (!of_device_is_available(next))
366 continue;
367 if (of_node_get(next))
368 break;
369 }
370 of_node_put(prev);
371 read_unlock(&devtree_lock);
372 return next;
373}
374EXPORT_SYMBOL(of_get_next_available_child);
375
376/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000377 * of_find_node_by_path - Find a node matching a full OF path
378 * @path: The full path to match
379 *
380 * Returns a node pointer with refcount incremented, use
381 * of_node_put() on it when done.
382 */
383struct device_node *of_find_node_by_path(const char *path)
384{
385 struct device_node *np = allnodes;
386
387 read_lock(&devtree_lock);
388 for (; np; np = np->allnext) {
389 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
390 && of_node_get(np))
391 break;
392 }
393 read_unlock(&devtree_lock);
394 return np;
395}
396EXPORT_SYMBOL(of_find_node_by_path);
397
398/**
399 * of_find_node_by_name - Find a node by its "name" property
400 * @from: The node to start searching from or NULL, the node
401 * you pass will not be searched, only the next one
402 * will; typically, you pass what the previous call
403 * returned. of_node_put() will be called on it
404 * @name: The name string to match against
405 *
406 * Returns a node pointer with refcount incremented, use
407 * of_node_put() on it when done.
408 */
409struct device_node *of_find_node_by_name(struct device_node *from,
410 const char *name)
411{
412 struct device_node *np;
413
414 read_lock(&devtree_lock);
415 np = from ? from->allnext : allnodes;
416 for (; np; np = np->allnext)
417 if (np->name && (of_node_cmp(np->name, name) == 0)
418 && of_node_get(np))
419 break;
420 of_node_put(from);
421 read_unlock(&devtree_lock);
422 return np;
423}
424EXPORT_SYMBOL(of_find_node_by_name);
425
426/**
427 * of_find_node_by_type - Find a node by its "device_type" property
428 * @from: The node to start searching from, or NULL to start searching
429 * the entire device tree. The node you pass will not be
430 * searched, only the next one will; typically, you pass
431 * what the previous call returned. of_node_put() will be
432 * called on from for you.
433 * @type: The type string to match against
434 *
435 * Returns a node pointer with refcount incremented, use
436 * of_node_put() on it when done.
437 */
438struct device_node *of_find_node_by_type(struct device_node *from,
439 const char *type)
440{
441 struct device_node *np;
442
443 read_lock(&devtree_lock);
444 np = from ? from->allnext : allnodes;
445 for (; np; np = np->allnext)
446 if (np->type && (of_node_cmp(np->type, type) == 0)
447 && of_node_get(np))
448 break;
449 of_node_put(from);
450 read_unlock(&devtree_lock);
451 return np;
452}
453EXPORT_SYMBOL(of_find_node_by_type);
454
455/**
456 * of_find_compatible_node - Find a node based on type and one of the
457 * tokens in its "compatible" property
458 * @from: The node to start searching from or NULL, the node
459 * you pass will not be searched, only the next one
460 * will; typically, you pass what the previous call
461 * returned. of_node_put() will be called on it
462 * @type: The type string to match "device_type" or NULL to ignore
463 * @compatible: The string to match to one of the tokens in the device
464 * "compatible" list.
465 *
466 * Returns a node pointer with refcount incremented, use
467 * of_node_put() on it when done.
468 */
469struct device_node *of_find_compatible_node(struct device_node *from,
470 const char *type, const char *compatible)
471{
472 struct device_node *np;
473
474 read_lock(&devtree_lock);
475 np = from ? from->allnext : allnodes;
476 for (; np; np = np->allnext) {
477 if (type
478 && !(np->type && (of_node_cmp(np->type, type) == 0)))
479 continue;
480 if (of_device_is_compatible(np, compatible) && of_node_get(np))
481 break;
482 }
483 of_node_put(from);
484 read_unlock(&devtree_lock);
485 return np;
486}
487EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100488
489/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000490 * of_find_node_with_property - Find a node which has a property with
491 * the given name.
492 * @from: The node to start searching from or NULL, the node
493 * you pass will not be searched, only the next one
494 * will; typically, you pass what the previous call
495 * returned. of_node_put() will be called on it
496 * @prop_name: The name of the property to look for.
497 *
498 * Returns a node pointer with refcount incremented, use
499 * of_node_put() on it when done.
500 */
501struct device_node *of_find_node_with_property(struct device_node *from,
502 const char *prop_name)
503{
504 struct device_node *np;
505 struct property *pp;
506
507 read_lock(&devtree_lock);
508 np = from ? from->allnext : allnodes;
509 for (; np; np = np->allnext) {
510 for (pp = np->properties; pp != 0; pp = pp->next) {
511 if (of_prop_cmp(pp->name, prop_name) == 0) {
512 of_node_get(np);
513 goto out;
514 }
515 }
516 }
517out:
518 of_node_put(from);
519 read_unlock(&devtree_lock);
520 return np;
521}
522EXPORT_SYMBOL(of_find_node_with_property);
523
524/**
Grant Likely283029d2008-01-09 06:20:40 +1100525 * of_match_node - Tell if an device_node has a matching of_match structure
526 * @matches: array of of device match structures to search in
527 * @node: the of device structure to match against
528 *
529 * Low level utility function used by device matching.
530 */
531const struct of_device_id *of_match_node(const struct of_device_id *matches,
532 const struct device_node *node)
533{
Grant Likelya52f07e2011-03-18 10:21:29 -0600534 if (!matches)
535 return NULL;
536
Grant Likely283029d2008-01-09 06:20:40 +1100537 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
538 int match = 1;
539 if (matches->name[0])
540 match &= node->name
541 && !strcmp(matches->name, node->name);
542 if (matches->type[0])
543 match &= node->type
544 && !strcmp(matches->type, node->type);
545 if (matches->compatible[0])
546 match &= of_device_is_compatible(node,
547 matches->compatible);
548 if (match)
549 return matches;
550 matches++;
551 }
552 return NULL;
553}
554EXPORT_SYMBOL(of_match_node);
555
556/**
557 * of_find_matching_node - Find a node based on an of_device_id match
558 * table.
559 * @from: The node to start searching from or NULL, the node
560 * you pass will not be searched, only the next one
561 * will; typically, you pass what the previous call
562 * returned. of_node_put() will be called on it
563 * @matches: array of of device match structures to search in
564 *
565 * Returns a node pointer with refcount incremented, use
566 * of_node_put() on it when done.
567 */
568struct device_node *of_find_matching_node(struct device_node *from,
569 const struct of_device_id *matches)
570{
571 struct device_node *np;
572
573 read_lock(&devtree_lock);
574 np = from ? from->allnext : allnodes;
575 for (; np; np = np->allnext) {
576 if (of_match_node(matches, np) && of_node_get(np))
577 break;
578 }
579 of_node_put(from);
580 read_unlock(&devtree_lock);
581 return np;
582}
583EXPORT_SYMBOL(of_find_matching_node);
Grant Likely3f07af42008-07-25 22:25:13 -0400584
585/**
Grant Likely3f07af42008-07-25 22:25:13 -0400586 * of_modalias_node - Lookup appropriate modalias for a device node
587 * @node: pointer to a device tree node
588 * @modalias: Pointer to buffer that modalias value will be copied into
589 * @len: Length of modalias value
590 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600591 * Based on the value of the compatible property, this routine will attempt
592 * to choose an appropriate modalias value for a particular device tree node.
593 * It does this by stripping the manufacturer prefix (as delimited by a ',')
594 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400595 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600596 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400597 */
598int of_modalias_node(struct device_node *node, char *modalias, int len)
599{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600600 const char *compatible, *p;
601 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400602
603 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600604 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400605 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400606 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600607 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400608 return 0;
609}
610EXPORT_SYMBOL_GPL(of_modalias_node);
611
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000612/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700613 * of_find_node_by_phandle - Find a node given a phandle
614 * @handle: phandle of the node to find
615 *
616 * Returns a node pointer with refcount incremented, use
617 * of_node_put() on it when done.
618 */
619struct device_node *of_find_node_by_phandle(phandle handle)
620{
621 struct device_node *np;
622
623 read_lock(&devtree_lock);
624 for (np = allnodes; np; np = np->allnext)
625 if (np->phandle == handle)
626 break;
627 of_node_get(np);
628 read_unlock(&devtree_lock);
629 return np;
630}
631EXPORT_SYMBOL(of_find_node_by_phandle);
632
633/**
Rob Herring0e373632011-07-06 15:42:58 -0500634 * of_property_read_u32_array - Find and read an array of 32 bit integers
635 * from a property.
636 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530637 * @np: device node from which the property value is to be read.
638 * @propname: name of the property to be searched.
639 * @out_value: pointer to return value, modified only if return value is 0.
640 *
Rob Herring0e373632011-07-06 15:42:58 -0500641 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530642 * it. Returns 0 on success, -EINVAL if the property does not exist,
643 * -ENODATA if property does not have a value, and -EOVERFLOW if the
644 * property data isn't large enough.
645 *
646 * The out_value is modified only if a valid u32 value can be decoded.
647 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100648int of_property_read_u32_array(const struct device_node *np,
649 const char *propname, u32 *out_values,
650 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530651{
652 struct property *prop = of_find_property(np, propname, NULL);
Rob Herring0e373632011-07-06 15:42:58 -0500653 const __be32 *val;
Thomas Abrahama3b85362011-06-30 21:26:10 +0530654
655 if (!prop)
656 return -EINVAL;
657 if (!prop->value)
658 return -ENODATA;
Rob Herring0e373632011-07-06 15:42:58 -0500659 if ((sz * sizeof(*out_values)) > prop->length)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530660 return -EOVERFLOW;
Rob Herring0e373632011-07-06 15:42:58 -0500661
662 val = prop->value;
663 while (sz--)
664 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530665 return 0;
666}
Rob Herring0e373632011-07-06 15:42:58 -0500667EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530668
669/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100670 * of_property_read_u64 - Find and read a 64 bit integer from a property
671 * @np: device node from which the property value is to be read.
672 * @propname: name of the property to be searched.
673 * @out_value: pointer to return value, modified only if return value is 0.
674 *
675 * Search for a property in a device node and read a 64-bit value from
676 * it. Returns 0 on success, -EINVAL if the property does not exist,
677 * -ENODATA if property does not have a value, and -EOVERFLOW if the
678 * property data isn't large enough.
679 *
680 * The out_value is modified only if a valid u64 value can be decoded.
681 */
682int of_property_read_u64(const struct device_node *np, const char *propname,
683 u64 *out_value)
684{
685 struct property *prop = of_find_property(np, propname, NULL);
686
687 if (!prop)
688 return -EINVAL;
689 if (!prop->value)
690 return -ENODATA;
691 if (sizeof(*out_value) > prop->length)
692 return -EOVERFLOW;
693 *out_value = of_read_number(prop->value, 2);
694 return 0;
695}
696EXPORT_SYMBOL_GPL(of_property_read_u64);
697
698/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530699 * of_property_read_string - Find and read a string from a property
700 * @np: device node from which the property value is to be read.
701 * @propname: name of the property to be searched.
702 * @out_string: pointer to null terminated return string, modified only if
703 * return value is 0.
704 *
705 * Search for a property in a device tree node and retrieve a null
706 * terminated string value (pointer to data, not a copy). Returns 0 on
707 * success, -EINVAL if the property does not exist, -ENODATA if property
708 * does not have a value, and -EILSEQ if the string is not null-terminated
709 * within the length of the property data.
710 *
711 * The out_string pointer is modified only if a valid string can be decoded.
712 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100713int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800714 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530715{
716 struct property *prop = of_find_property(np, propname, NULL);
717 if (!prop)
718 return -EINVAL;
719 if (!prop->value)
720 return -ENODATA;
721 if (strnlen(prop->value, prop->length) >= prop->length)
722 return -EILSEQ;
723 *out_string = prop->value;
724 return 0;
725}
726EXPORT_SYMBOL_GPL(of_property_read_string);
727
728/**
Grant Likely7aff0fe2011-12-12 09:25:58 -0700729 * of_property_match_string() - Find string in a list and return index
730 * @np: pointer to node containing string list property
731 * @propname: string list property name
732 * @string: pointer to string to search for in string list
733 *
734 * This function searches a string list property and returns the index
735 * of a specific string value.
736 */
737int of_property_match_string(struct device_node *np, const char *propname,
738 const char *string)
739{
740 struct property *prop = of_find_property(np, propname, NULL);
741 size_t l;
742 int i;
743 const char *p, *end;
744
745 if (!prop)
746 return -EINVAL;
747 if (!prop->value)
748 return -ENODATA;
749
750 p = prop->value;
751 end = p + prop->length;
752
753 for (i = 0; p < end; i++, p += l) {
Grant Likelycbf81c42014-11-03 15:15:35 +0000754 l = strnlen(p, end - p) + 1;
Grant Likely7aff0fe2011-12-12 09:25:58 -0700755 if (p + l > end)
756 return -EILSEQ;
757 pr_debug("comparing %s with %s\n", string, p);
758 if (strcmp(string, p) == 0)
759 return i; /* Found it; return index */
760 }
761 return -ENODATA;
762}
763EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200764
765/**
Grant Likelycbf81c42014-11-03 15:15:35 +0000766 * of_property_read_string_util() - Utility helper for parsing string properties
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200767 * @np: device node from which the property value is to be read.
768 * @propname: name of the property to be searched.
Grant Likelycbf81c42014-11-03 15:15:35 +0000769 * @out_strs: output array of string pointers.
770 * @sz: number of array elements to read.
771 * @skip: Number of strings to skip over at beginning of list.
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200772 *
Grant Likelycbf81c42014-11-03 15:15:35 +0000773 * Don't call this function directly. It is a utility helper for the
774 * of_property_read_string*() family of functions.
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200775 */
Grant Likelycbf81c42014-11-03 15:15:35 +0000776int of_property_read_string_helper(struct device_node *np, const char *propname,
777 const char **out_strs, size_t sz, int skip)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200778{
779 struct property *prop = of_find_property(np, propname, NULL);
Grant Likelycbf81c42014-11-03 15:15:35 +0000780 int l = 0, i = 0;
781 const char *p, *end;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200782
783 if (!prop)
784 return -EINVAL;
785 if (!prop->value)
786 return -ENODATA;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200787 p = prop->value;
Grant Likelycbf81c42014-11-03 15:15:35 +0000788 end = p + prop->length;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200789
Grant Likelycbf81c42014-11-03 15:15:35 +0000790 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
791 l = strnlen(p, end - p) + 1;
792 if (p + l > end)
793 return -EILSEQ;
794 if (out_strs && i >= skip)
795 *out_strs++ = p;
796 }
797 i -= skip;
798 return i <= 0 ? -ENODATA : i;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200799}
Grant Likelycbf81c42014-11-03 15:15:35 +0000800EXPORT_SYMBOL_GPL(of_property_read_string_helper);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200801
802/**
Grant Likely739649c2009-04-25 12:52:40 +0000803 * of_parse_phandle - Resolve a phandle property to a device_node pointer
804 * @np: Pointer to device node holding phandle property
805 * @phandle_name: Name of property holding a phandle value
806 * @index: For properties holding a table of phandles, this is the index into
807 * the table
808 *
809 * Returns the device_node pointer with refcount incremented. Use
810 * of_node_put() on it when done.
811 */
812struct device_node *
813of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
814{
Grant Likely9a6b2e52010-07-23 01:48:25 -0600815 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +0000816 int size;
817
818 phandle = of_get_property(np, phandle_name, &size);
819 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
820 return NULL;
821
Grant Likely9a6b2e52010-07-23 01:48:25 -0600822 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +0000823}
824EXPORT_SYMBOL(of_parse_phandle);
825
826/**
Grant Likely15c9a0a2011-12-12 09:25:57 -0700827 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000828 * @np: pointer to a device tree node containing a list
829 * @list_name: property name that contains a list
830 * @cells_name: property name that specifies phandles' arguments count
831 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -0700832 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000833 *
834 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -0700835 * Returns 0 on success and fills out_args, on error returns appropriate
836 * errno value.
837 *
838 * Caller is responsible to call of_node_put() on the returned out_args->node
839 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000840 *
841 * Example:
842 *
843 * phandle1: node1 {
844 * #list-cells = <2>;
845 * }
846 *
847 * phandle2: node2 {
848 * #list-cells = <1>;
849 * }
850 *
851 * node3 {
852 * list = <&phandle1 1 2 &phandle2 3>;
853 * }
854 *
855 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -0700856 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000857 */
Grant Likely15c9a0a2011-12-12 09:25:57 -0700858int of_parse_phandle_with_args(struct device_node *np, const char *list_name,
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000859 const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -0700860 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000861{
Grant Likely15c9a0a2011-12-12 09:25:57 -0700862 const __be32 *list, *list_end;
863 int size, cur_index = 0;
864 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000865 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -0700866 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000867
Grant Likely15c9a0a2011-12-12 09:25:57 -0700868 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000869 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -0700870 if (!list)
871 return -EINVAL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000872 list_end = list + size / sizeof(*list);
873
Grant Likely15c9a0a2011-12-12 09:25:57 -0700874 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000875 while (list < list_end) {
Grant Likely15c9a0a2011-12-12 09:25:57 -0700876 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000877
Grant Likely15c9a0a2011-12-12 09:25:57 -0700878 /*
879 * If phandle is 0, then it is an empty entry with no
880 * arguments. Skip forward to the next entry.
881 */
Grant Likely9a6b2e52010-07-23 01:48:25 -0600882 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -0700883 if (phandle) {
884 /*
885 * Find the provider node and parse the #*-cells
886 * property to determine the argument length
887 */
888 node = of_find_node_by_phandle(phandle);
889 if (!node) {
890 pr_err("%s: could not find phandle\n",
891 np->full_name);
892 break;
893 }
894 if (of_property_read_u32(node, cells_name, &count)) {
895 pr_err("%s: could not get %s for %s\n",
896 np->full_name, cells_name,
897 node->full_name);
898 break;
899 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000900
Grant Likely15c9a0a2011-12-12 09:25:57 -0700901 /*
902 * Make sure that the arguments actually fit in the
903 * remaining property data length
904 */
905 if (list + count > list_end) {
906 pr_err("%s: arguments longer than property\n",
907 np->full_name);
908 break;
909 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000910 }
911
Grant Likely15c9a0a2011-12-12 09:25:57 -0700912 /*
913 * All of the error cases above bail out of the loop, so at
914 * this point, the parsing is successful. If the requested
915 * index matches, then fill the out_args structure and return,
916 * or return -ENOENT for an empty entry.
917 */
918 if (cur_index == index) {
919 if (!phandle)
920 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000921
Grant Likely15c9a0a2011-12-12 09:25:57 -0700922 if (out_args) {
923 int i;
924 if (WARN_ON(count > MAX_PHANDLE_ARGS))
925 count = MAX_PHANDLE_ARGS;
926 out_args->np = node;
927 out_args->args_count = count;
928 for (i = 0; i < count; i++)
929 out_args->args[i] = be32_to_cpup(list++);
930 }
931 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000932 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000933
934 of_node_put(node);
935 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -0700936 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000937 cur_index++;
938 }
939
Grant Likely15c9a0a2011-12-12 09:25:57 -0700940 /* Loop exited without finding a valid entry; return an error */
941 if (node)
942 of_node_put(node);
943 return -EINVAL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000944}
Grant Likely15c9a0a2011-12-12 09:25:57 -0700945EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -0700946
947/**
948 * prom_add_property - Add a property to a node
949 */
950int prom_add_property(struct device_node *np, struct property *prop)
951{
952 struct property **next;
953 unsigned long flags;
954
955 prop->next = NULL;
956 write_lock_irqsave(&devtree_lock, flags);
957 next = &np->properties;
958 while (*next) {
959 if (strcmp(prop->name, (*next)->name) == 0) {
960 /* duplicate ! don't insert it */
961 write_unlock_irqrestore(&devtree_lock, flags);
962 return -1;
963 }
964 next = &(*next)->next;
965 }
966 *next = prop;
967 write_unlock_irqrestore(&devtree_lock, flags);
968
969#ifdef CONFIG_PROC_DEVICETREE
970 /* try to add to proc as well if it was initialized */
971 if (np->pde)
972 proc_device_tree_add_prop(np->pde, prop);
973#endif /* CONFIG_PROC_DEVICETREE */
974
975 return 0;
976}
977
978/**
979 * prom_remove_property - Remove a property from a node.
980 *
981 * Note that we don't actually remove it, since we have given out
982 * who-knows-how-many pointers to the data using get-property.
983 * Instead we just move the property to the "dead properties"
984 * list, so it won't be found any more.
985 */
986int prom_remove_property(struct device_node *np, struct property *prop)
987{
988 struct property **next;
989 unsigned long flags;
990 int found = 0;
991
992 write_lock_irqsave(&devtree_lock, flags);
993 next = &np->properties;
994 while (*next) {
995 if (*next == prop) {
996 /* found the node */
997 *next = prop->next;
998 prop->next = np->deadprops;
999 np->deadprops = prop;
1000 found = 1;
1001 break;
1002 }
1003 next = &(*next)->next;
1004 }
1005 write_unlock_irqrestore(&devtree_lock, flags);
1006
1007 if (!found)
1008 return -ENODEV;
1009
1010#ifdef CONFIG_PROC_DEVICETREE
1011 /* try to remove the proc node as well */
1012 if (np->pde)
1013 proc_device_tree_remove_prop(np->pde, prop);
1014#endif /* CONFIG_PROC_DEVICETREE */
1015
1016 return 0;
1017}
1018
1019/*
1020 * prom_update_property - Update a property in a node.
1021 *
1022 * Note that we don't actually remove it, since we have given out
1023 * who-knows-how-many pointers to the data using get-property.
1024 * Instead we just move the property to the "dead properties" list,
1025 * and add the new property to the property list
1026 */
1027int prom_update_property(struct device_node *np,
1028 struct property *newprop,
1029 struct property *oldprop)
1030{
1031 struct property **next;
1032 unsigned long flags;
1033 int found = 0;
1034
1035 write_lock_irqsave(&devtree_lock, flags);
1036 next = &np->properties;
1037 while (*next) {
1038 if (*next == oldprop) {
1039 /* found the node */
1040 newprop->next = oldprop->next;
1041 *next = newprop;
1042 oldprop->next = np->deadprops;
1043 np->deadprops = oldprop;
1044 found = 1;
1045 break;
1046 }
1047 next = &(*next)->next;
1048 }
1049 write_unlock_irqrestore(&devtree_lock, flags);
1050
1051 if (!found)
1052 return -ENODEV;
1053
1054#ifdef CONFIG_PROC_DEVICETREE
1055 /* try to add to proc as well if it was initialized */
1056 if (np->pde)
1057 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1058#endif /* CONFIG_PROC_DEVICETREE */
1059
1060 return 0;
1061}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001062
1063#if defined(CONFIG_OF_DYNAMIC)
1064/*
1065 * Support for dynamic device trees.
1066 *
1067 * On some platforms, the device tree can be manipulated at runtime.
1068 * The routines in this section support adding, removing and changing
1069 * device tree nodes.
1070 */
1071
1072/**
1073 * of_attach_node - Plug a device node into the tree and global list.
1074 */
1075void of_attach_node(struct device_node *np)
1076{
1077 unsigned long flags;
1078
1079 write_lock_irqsave(&devtree_lock, flags);
1080 np->sibling = np->parent->child;
1081 np->allnext = allnodes;
1082 np->parent->child = np;
1083 allnodes = np;
1084 write_unlock_irqrestore(&devtree_lock, flags);
1085}
1086
1087/**
1088 * of_detach_node - "Unplug" a node from the device tree.
1089 *
1090 * The caller must hold a reference to the node. The memory associated with
1091 * the node is not freed until its refcount goes to zero.
1092 */
1093void of_detach_node(struct device_node *np)
1094{
1095 struct device_node *parent;
1096 unsigned long flags;
1097
1098 write_lock_irqsave(&devtree_lock, flags);
1099
1100 parent = np->parent;
1101 if (!parent)
1102 goto out_unlock;
1103
1104 if (allnodes == np)
1105 allnodes = np->allnext;
1106 else {
1107 struct device_node *prev;
1108 for (prev = allnodes;
1109 prev->allnext != np;
1110 prev = prev->allnext)
1111 ;
1112 prev->allnext = np->allnext;
1113 }
1114
1115 if (parent->child == np)
1116 parent->child = np->sibling;
1117 else {
1118 struct device_node *prevsib;
1119 for (prevsib = np->parent->child;
1120 prevsib->sibling != np;
1121 prevsib = prevsib->sibling)
1122 ;
1123 prevsib->sibling = np->sibling;
1124 }
1125
1126 of_node_set_flag(np, OF_DETACHED);
1127
1128out_unlock:
1129 write_unlock_irqrestore(&devtree_lock, flags);
1130}
1131#endif /* defined(CONFIG_OF_DYNAMIC) */
1132
Shawn Guo611cad72011-08-15 15:28:14 +08001133static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1134 int id, const char *stem, int stem_len)
1135{
1136 ap->np = np;
1137 ap->id = id;
1138 strncpy(ap->stem, stem, stem_len);
1139 ap->stem[stem_len] = 0;
1140 list_add_tail(&ap->link, &aliases_lookup);
1141 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1142 ap->alias, ap->stem, ap->id, np ? np->full_name : NULL);
1143}
1144
1145/**
1146 * of_alias_scan - Scan all properties of 'aliases' node
1147 *
1148 * The function scans all the properties of 'aliases' node and populate
1149 * the the global lookup table with the properties. It returns the
1150 * number of alias_prop found, or error code in error case.
1151 *
1152 * @dt_alloc: An allocator that provides a virtual address to memory
1153 * for the resulting tree
1154 */
1155void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1156{
1157 struct property *pp;
1158
1159 of_chosen = of_find_node_by_path("/chosen");
1160 if (of_chosen == NULL)
1161 of_chosen = of_find_node_by_path("/chosen@0");
1162 of_aliases = of_find_node_by_path("/aliases");
1163 if (!of_aliases)
1164 return;
1165
Dong Aisheng8af0da92011-12-22 20:19:24 +08001166 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001167 const char *start = pp->name;
1168 const char *end = start + strlen(start);
1169 struct device_node *np;
1170 struct alias_prop *ap;
1171 int id, len;
1172
1173 /* Skip those we do not want to proceed */
1174 if (!strcmp(pp->name, "name") ||
1175 !strcmp(pp->name, "phandle") ||
1176 !strcmp(pp->name, "linux,phandle"))
1177 continue;
1178
1179 np = of_find_node_by_path(pp->value);
1180 if (!np)
1181 continue;
1182
1183 /* walk the alias backwards to extract the id and work out
1184 * the 'stem' string */
1185 while (isdigit(*(end-1)) && end > start)
1186 end--;
1187 len = end - start;
1188
1189 if (kstrtoint(end, 10, &id) < 0)
1190 continue;
1191
1192 /* Allocate an alias_prop with enough space for the stem */
1193 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1194 if (!ap)
1195 continue;
Grant Likelydb7bd702013-08-28 21:24:17 +01001196 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001197 ap->alias = start;
1198 of_alias_add(ap, np, id, start, len);
1199 }
1200}
1201
1202/**
1203 * of_alias_get_id - Get alias id for the given device_node
1204 * @np: Pointer to the given device_node
1205 * @stem: Alias stem of the given device_node
1206 *
1207 * The function travels the lookup table to get alias id for the given
1208 * device_node and alias stem. It returns the alias id if find it.
1209 */
1210int of_alias_get_id(struct device_node *np, const char *stem)
1211{
1212 struct alias_prop *app;
1213 int id = -ENODEV;
1214
1215 mutex_lock(&of_aliases_mutex);
1216 list_for_each_entry(app, &aliases_lookup, link) {
1217 if (strcmp(app->stem, stem) != 0)
1218 continue;
1219
1220 if (np == app->np) {
1221 id = app->id;
1222 break;
1223 }
1224 }
1225 mutex_unlock(&of_aliases_mutex);
1226
1227 return id;
1228}
1229EXPORT_SYMBOL_GPL(of_alias_get_id);