blob: 715aef2e0732da7b76c75d273f4171279859c822 [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/**
350 * of_find_node_by_path - Find a node matching a full OF path
351 * @path: The full path to match
352 *
353 * Returns a node pointer with refcount incremented, use
354 * of_node_put() on it when done.
355 */
356struct device_node *of_find_node_by_path(const char *path)
357{
358 struct device_node *np = allnodes;
359
360 read_lock(&devtree_lock);
361 for (; np; np = np->allnext) {
362 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
363 && of_node_get(np))
364 break;
365 }
366 read_unlock(&devtree_lock);
367 return np;
368}
369EXPORT_SYMBOL(of_find_node_by_path);
370
371/**
372 * of_find_node_by_name - Find a node by its "name" property
373 * @from: The node to start searching from or NULL, the node
374 * you pass will not be searched, only the next one
375 * will; typically, you pass what the previous call
376 * returned. of_node_put() will be called on it
377 * @name: The name string to match against
378 *
379 * Returns a node pointer with refcount incremented, use
380 * of_node_put() on it when done.
381 */
382struct device_node *of_find_node_by_name(struct device_node *from,
383 const char *name)
384{
385 struct device_node *np;
386
387 read_lock(&devtree_lock);
388 np = from ? from->allnext : allnodes;
389 for (; np; np = np->allnext)
390 if (np->name && (of_node_cmp(np->name, name) == 0)
391 && of_node_get(np))
392 break;
393 of_node_put(from);
394 read_unlock(&devtree_lock);
395 return np;
396}
397EXPORT_SYMBOL(of_find_node_by_name);
398
399/**
400 * of_find_node_by_type - Find a node by its "device_type" property
401 * @from: The node to start searching from, or NULL to start searching
402 * the entire device tree. The node you pass will not be
403 * searched, only the next one will; typically, you pass
404 * what the previous call returned. of_node_put() will be
405 * called on from for you.
406 * @type: The type string to match against
407 *
408 * Returns a node pointer with refcount incremented, use
409 * of_node_put() on it when done.
410 */
411struct device_node *of_find_node_by_type(struct device_node *from,
412 const char *type)
413{
414 struct device_node *np;
415
416 read_lock(&devtree_lock);
417 np = from ? from->allnext : allnodes;
418 for (; np; np = np->allnext)
419 if (np->type && (of_node_cmp(np->type, type) == 0)
420 && of_node_get(np))
421 break;
422 of_node_put(from);
423 read_unlock(&devtree_lock);
424 return np;
425}
426EXPORT_SYMBOL(of_find_node_by_type);
427
428/**
429 * of_find_compatible_node - Find a node based on type and one of the
430 * tokens in its "compatible" property
431 * @from: The node to start searching from or NULL, the node
432 * you pass will not be searched, only the next one
433 * will; typically, you pass what the previous call
434 * returned. of_node_put() will be called on it
435 * @type: The type string to match "device_type" or NULL to ignore
436 * @compatible: The string to match to one of the tokens in the device
437 * "compatible" list.
438 *
439 * Returns a node pointer with refcount incremented, use
440 * of_node_put() on it when done.
441 */
442struct device_node *of_find_compatible_node(struct device_node *from,
443 const char *type, const char *compatible)
444{
445 struct device_node *np;
446
447 read_lock(&devtree_lock);
448 np = from ? from->allnext : allnodes;
449 for (; np; np = np->allnext) {
450 if (type
451 && !(np->type && (of_node_cmp(np->type, type) == 0)))
452 continue;
453 if (of_device_is_compatible(np, compatible) && of_node_get(np))
454 break;
455 }
456 of_node_put(from);
457 read_unlock(&devtree_lock);
458 return np;
459}
460EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100461
462/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000463 * of_find_node_with_property - Find a node which has a property with
464 * the given name.
465 * @from: The node to start searching from or NULL, the node
466 * you pass will not be searched, only the next one
467 * will; typically, you pass what the previous call
468 * returned. of_node_put() will be called on it
469 * @prop_name: The name of the property to look for.
470 *
471 * Returns a node pointer with refcount incremented, use
472 * of_node_put() on it when done.
473 */
474struct device_node *of_find_node_with_property(struct device_node *from,
475 const char *prop_name)
476{
477 struct device_node *np;
478 struct property *pp;
479
480 read_lock(&devtree_lock);
481 np = from ? from->allnext : allnodes;
482 for (; np; np = np->allnext) {
483 for (pp = np->properties; pp != 0; pp = pp->next) {
484 if (of_prop_cmp(pp->name, prop_name) == 0) {
485 of_node_get(np);
486 goto out;
487 }
488 }
489 }
490out:
491 of_node_put(from);
492 read_unlock(&devtree_lock);
493 return np;
494}
495EXPORT_SYMBOL(of_find_node_with_property);
496
497/**
Grant Likely283029d2008-01-09 06:20:40 +1100498 * of_match_node - Tell if an device_node has a matching of_match structure
499 * @matches: array of of device match structures to search in
500 * @node: the of device structure to match against
501 *
502 * Low level utility function used by device matching.
503 */
504const struct of_device_id *of_match_node(const struct of_device_id *matches,
505 const struct device_node *node)
506{
Grant Likelya52f07e2011-03-18 10:21:29 -0600507 if (!matches)
508 return NULL;
509
Grant Likely283029d2008-01-09 06:20:40 +1100510 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
511 int match = 1;
512 if (matches->name[0])
513 match &= node->name
514 && !strcmp(matches->name, node->name);
515 if (matches->type[0])
516 match &= node->type
517 && !strcmp(matches->type, node->type);
518 if (matches->compatible[0])
519 match &= of_device_is_compatible(node,
520 matches->compatible);
521 if (match)
522 return matches;
523 matches++;
524 }
525 return NULL;
526}
527EXPORT_SYMBOL(of_match_node);
528
529/**
530 * of_find_matching_node - Find a node based on an of_device_id match
531 * table.
532 * @from: The node to start searching from or NULL, the node
533 * you pass will not be searched, only the next one
534 * will; typically, you pass what the previous call
535 * returned. of_node_put() will be called on it
536 * @matches: array of of device match structures to search in
537 *
538 * Returns a node pointer with refcount incremented, use
539 * of_node_put() on it when done.
540 */
541struct device_node *of_find_matching_node(struct device_node *from,
542 const struct of_device_id *matches)
543{
544 struct device_node *np;
545
546 read_lock(&devtree_lock);
547 np = from ? from->allnext : allnodes;
548 for (; np; np = np->allnext) {
549 if (of_match_node(matches, np) && of_node_get(np))
550 break;
551 }
552 of_node_put(from);
553 read_unlock(&devtree_lock);
554 return np;
555}
556EXPORT_SYMBOL(of_find_matching_node);
Grant Likely3f07af42008-07-25 22:25:13 -0400557
558/**
Grant Likely3f07af42008-07-25 22:25:13 -0400559 * of_modalias_node - Lookup appropriate modalias for a device node
560 * @node: pointer to a device tree node
561 * @modalias: Pointer to buffer that modalias value will be copied into
562 * @len: Length of modalias value
563 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600564 * Based on the value of the compatible property, this routine will attempt
565 * to choose an appropriate modalias value for a particular device tree node.
566 * It does this by stripping the manufacturer prefix (as delimited by a ',')
567 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400568 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600569 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400570 */
571int of_modalias_node(struct device_node *node, char *modalias, int len)
572{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600573 const char *compatible, *p;
574 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400575
576 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600577 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400578 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400579 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600580 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400581 return 0;
582}
583EXPORT_SYMBOL_GPL(of_modalias_node);
584
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000585/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700586 * of_find_node_by_phandle - Find a node given a phandle
587 * @handle: phandle of the node to find
588 *
589 * Returns a node pointer with refcount incremented, use
590 * of_node_put() on it when done.
591 */
592struct device_node *of_find_node_by_phandle(phandle handle)
593{
594 struct device_node *np;
595
596 read_lock(&devtree_lock);
597 for (np = allnodes; np; np = np->allnext)
598 if (np->phandle == handle)
599 break;
600 of_node_get(np);
601 read_unlock(&devtree_lock);
602 return np;
603}
604EXPORT_SYMBOL(of_find_node_by_phandle);
605
606/**
Rob Herring0e373632011-07-06 15:42:58 -0500607 * of_property_read_u32_array - Find and read an array of 32 bit integers
608 * from a property.
609 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530610 * @np: device node from which the property value is to be read.
611 * @propname: name of the property to be searched.
612 * @out_value: pointer to return value, modified only if return value is 0.
613 *
Rob Herring0e373632011-07-06 15:42:58 -0500614 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530615 * it. Returns 0 on success, -EINVAL if the property does not exist,
616 * -ENODATA if property does not have a value, and -EOVERFLOW if the
617 * property data isn't large enough.
618 *
619 * The out_value is modified only if a valid u32 value can be decoded.
620 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100621int of_property_read_u32_array(const struct device_node *np,
622 const char *propname, u32 *out_values,
623 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530624{
625 struct property *prop = of_find_property(np, propname, NULL);
Rob Herring0e373632011-07-06 15:42:58 -0500626 const __be32 *val;
Thomas Abrahama3b85362011-06-30 21:26:10 +0530627
628 if (!prop)
629 return -EINVAL;
630 if (!prop->value)
631 return -ENODATA;
Rob Herring0e373632011-07-06 15:42:58 -0500632 if ((sz * sizeof(*out_values)) > prop->length)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530633 return -EOVERFLOW;
Rob Herring0e373632011-07-06 15:42:58 -0500634
635 val = prop->value;
636 while (sz--)
637 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530638 return 0;
639}
Rob Herring0e373632011-07-06 15:42:58 -0500640EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530641
642/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100643 * of_property_read_u64 - Find and read a 64 bit integer from a property
644 * @np: device node from which the property value is to be read.
645 * @propname: name of the property to be searched.
646 * @out_value: pointer to return value, modified only if return value is 0.
647 *
648 * Search for a property in a device node and read a 64-bit value from
649 * it. Returns 0 on success, -EINVAL if the property does not exist,
650 * -ENODATA if property does not have a value, and -EOVERFLOW if the
651 * property data isn't large enough.
652 *
653 * The out_value is modified only if a valid u64 value can be decoded.
654 */
655int of_property_read_u64(const struct device_node *np, const char *propname,
656 u64 *out_value)
657{
658 struct property *prop = of_find_property(np, propname, NULL);
659
660 if (!prop)
661 return -EINVAL;
662 if (!prop->value)
663 return -ENODATA;
664 if (sizeof(*out_value) > prop->length)
665 return -EOVERFLOW;
666 *out_value = of_read_number(prop->value, 2);
667 return 0;
668}
669EXPORT_SYMBOL_GPL(of_property_read_u64);
670
671/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530672 * of_property_read_string - Find and read a string from a property
673 * @np: device node from which the property value is to be read.
674 * @propname: name of the property to be searched.
675 * @out_string: pointer to null terminated return string, modified only if
676 * return value is 0.
677 *
678 * Search for a property in a device tree node and retrieve a null
679 * terminated string value (pointer to data, not a copy). Returns 0 on
680 * success, -EINVAL if the property does not exist, -ENODATA if property
681 * does not have a value, and -EILSEQ if the string is not null-terminated
682 * within the length of the property data.
683 *
684 * The out_string pointer is modified only if a valid string can be decoded.
685 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100686int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800687 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530688{
689 struct property *prop = of_find_property(np, propname, NULL);
690 if (!prop)
691 return -EINVAL;
692 if (!prop->value)
693 return -ENODATA;
694 if (strnlen(prop->value, prop->length) >= prop->length)
695 return -EILSEQ;
696 *out_string = prop->value;
697 return 0;
698}
699EXPORT_SYMBOL_GPL(of_property_read_string);
700
701/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200702 * of_property_read_string_index - Find and read a string from a multiple
703 * strings property.
704 * @np: device node from which the property value is to be read.
705 * @propname: name of the property to be searched.
706 * @index: index of the string in the list of strings
707 * @out_string: pointer to null terminated return string, modified only if
708 * return value is 0.
709 *
710 * Search for a property in a device tree node and retrieve a null
711 * terminated string value (pointer to data, not a copy) in the list of strings
712 * contained in that property.
713 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
714 * property does not have a value, and -EILSEQ if the string is not
715 * null-terminated within the length of the property data.
716 *
717 * The out_string pointer is modified only if a valid string can be decoded.
718 */
719int of_property_read_string_index(struct device_node *np, const char *propname,
720 int index, const char **output)
721{
722 struct property *prop = of_find_property(np, propname, NULL);
723 int i = 0;
724 size_t l = 0, total = 0;
725 const char *p;
726
727 if (!prop)
728 return -EINVAL;
729 if (!prop->value)
730 return -ENODATA;
731 if (strnlen(prop->value, prop->length) >= prop->length)
732 return -EILSEQ;
733
734 p = prop->value;
735
736 for (i = 0; total < prop->length; total += l, p += l) {
737 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100738 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200739 *output = p;
740 return 0;
741 }
742 }
743 return -ENODATA;
744}
745EXPORT_SYMBOL_GPL(of_property_read_string_index);
746
Grant Likely7aff0fe2011-12-12 09:25:58 -0700747/**
748 * of_property_match_string() - Find string in a list and return index
749 * @np: pointer to node containing string list property
750 * @propname: string list property name
751 * @string: pointer to string to search for in string list
752 *
753 * This function searches a string list property and returns the index
754 * of a specific string value.
755 */
756int of_property_match_string(struct device_node *np, const char *propname,
757 const char *string)
758{
759 struct property *prop = of_find_property(np, propname, NULL);
760 size_t l;
761 int i;
762 const char *p, *end;
763
764 if (!prop)
765 return -EINVAL;
766 if (!prop->value)
767 return -ENODATA;
768
769 p = prop->value;
770 end = p + prop->length;
771
772 for (i = 0; p < end; i++, p += l) {
773 l = strlen(p) + 1;
774 if (p + l > end)
775 return -EILSEQ;
776 pr_debug("comparing %s with %s\n", string, p);
777 if (strcmp(string, p) == 0)
778 return i; /* Found it; return index */
779 }
780 return -ENODATA;
781}
782EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200783
784/**
785 * of_property_count_strings - Find and return the number of strings from a
786 * multiple strings property.
787 * @np: device node from which the property value is to be read.
788 * @propname: name of the property to be searched.
789 *
790 * Search for a property in a device tree node and retrieve the number of null
791 * terminated string contain in it. Returns the number of strings on
792 * success, -EINVAL if the property does not exist, -ENODATA if property
793 * does not have a value, and -EILSEQ if the string is not null-terminated
794 * within the length of the property data.
795 */
796int of_property_count_strings(struct device_node *np, const char *propname)
797{
798 struct property *prop = of_find_property(np, propname, NULL);
799 int i = 0;
800 size_t l = 0, total = 0;
801 const char *p;
802
803 if (!prop)
804 return -EINVAL;
805 if (!prop->value)
806 return -ENODATA;
807 if (strnlen(prop->value, prop->length) >= prop->length)
808 return -EILSEQ;
809
810 p = prop->value;
811
Benoit Cousson88af7f52011-12-05 15:23:54 +0100812 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200813 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100814
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200815 return i;
816}
817EXPORT_SYMBOL_GPL(of_property_count_strings);
818
819/**
Grant Likely739649c2009-04-25 12:52:40 +0000820 * of_parse_phandle - Resolve a phandle property to a device_node pointer
821 * @np: Pointer to device node holding phandle property
822 * @phandle_name: Name of property holding a phandle value
823 * @index: For properties holding a table of phandles, this is the index into
824 * the table
825 *
826 * Returns the device_node pointer with refcount incremented. Use
827 * of_node_put() on it when done.
828 */
829struct device_node *
830of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
831{
Grant Likely9a6b2e52010-07-23 01:48:25 -0600832 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +0000833 int size;
834
835 phandle = of_get_property(np, phandle_name, &size);
836 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
837 return NULL;
838
Grant Likely9a6b2e52010-07-23 01:48:25 -0600839 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +0000840}
841EXPORT_SYMBOL(of_parse_phandle);
842
843/**
Grant Likely15c9a0a2011-12-12 09:25:57 -0700844 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000845 * @np: pointer to a device tree node containing a list
846 * @list_name: property name that contains a list
847 * @cells_name: property name that specifies phandles' arguments count
848 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -0700849 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000850 *
851 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -0700852 * Returns 0 on success and fills out_args, on error returns appropriate
853 * errno value.
854 *
855 * Caller is responsible to call of_node_put() on the returned out_args->node
856 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000857 *
858 * Example:
859 *
860 * phandle1: node1 {
861 * #list-cells = <2>;
862 * }
863 *
864 * phandle2: node2 {
865 * #list-cells = <1>;
866 * }
867 *
868 * node3 {
869 * list = <&phandle1 1 2 &phandle2 3>;
870 * }
871 *
872 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -0700873 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000874 */
Grant Likely15c9a0a2011-12-12 09:25:57 -0700875int of_parse_phandle_with_args(struct device_node *np, const char *list_name,
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000876 const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -0700877 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000878{
Grant Likely15c9a0a2011-12-12 09:25:57 -0700879 const __be32 *list, *list_end;
880 int size, cur_index = 0;
881 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000882 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -0700883 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000884
Grant Likely15c9a0a2011-12-12 09:25:57 -0700885 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000886 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -0700887 if (!list)
888 return -EINVAL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000889 list_end = list + size / sizeof(*list);
890
Grant Likely15c9a0a2011-12-12 09:25:57 -0700891 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000892 while (list < list_end) {
Grant Likely15c9a0a2011-12-12 09:25:57 -0700893 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000894
Grant Likely15c9a0a2011-12-12 09:25:57 -0700895 /*
896 * If phandle is 0, then it is an empty entry with no
897 * arguments. Skip forward to the next entry.
898 */
Grant Likely9a6b2e52010-07-23 01:48:25 -0600899 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -0700900 if (phandle) {
901 /*
902 * Find the provider node and parse the #*-cells
903 * property to determine the argument length
904 */
905 node = of_find_node_by_phandle(phandle);
906 if (!node) {
907 pr_err("%s: could not find phandle\n",
908 np->full_name);
909 break;
910 }
911 if (of_property_read_u32(node, cells_name, &count)) {
912 pr_err("%s: could not get %s for %s\n",
913 np->full_name, cells_name,
914 node->full_name);
915 break;
916 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000917
Grant Likely15c9a0a2011-12-12 09:25:57 -0700918 /*
919 * Make sure that the arguments actually fit in the
920 * remaining property data length
921 */
922 if (list + count > list_end) {
923 pr_err("%s: arguments longer than property\n",
924 np->full_name);
925 break;
926 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000927 }
928
Grant Likely15c9a0a2011-12-12 09:25:57 -0700929 /*
930 * All of the error cases above bail out of the loop, so at
931 * this point, the parsing is successful. If the requested
932 * index matches, then fill the out_args structure and return,
933 * or return -ENOENT for an empty entry.
934 */
935 if (cur_index == index) {
936 if (!phandle)
937 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000938
Grant Likely15c9a0a2011-12-12 09:25:57 -0700939 if (out_args) {
940 int i;
941 if (WARN_ON(count > MAX_PHANDLE_ARGS))
942 count = MAX_PHANDLE_ARGS;
943 out_args->np = node;
944 out_args->args_count = count;
945 for (i = 0; i < count; i++)
946 out_args->args[i] = be32_to_cpup(list++);
947 }
948 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000949 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000950
951 of_node_put(node);
952 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -0700953 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000954 cur_index++;
955 }
956
Grant Likely15c9a0a2011-12-12 09:25:57 -0700957 /* Loop exited without finding a valid entry; return an error */
958 if (node)
959 of_node_put(node);
960 return -EINVAL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000961}
Grant Likely15c9a0a2011-12-12 09:25:57 -0700962EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -0700963
964/**
965 * prom_add_property - Add a property to a node
966 */
967int prom_add_property(struct device_node *np, struct property *prop)
968{
969 struct property **next;
970 unsigned long flags;
971
972 prop->next = NULL;
973 write_lock_irqsave(&devtree_lock, flags);
974 next = &np->properties;
975 while (*next) {
976 if (strcmp(prop->name, (*next)->name) == 0) {
977 /* duplicate ! don't insert it */
978 write_unlock_irqrestore(&devtree_lock, flags);
979 return -1;
980 }
981 next = &(*next)->next;
982 }
983 *next = prop;
984 write_unlock_irqrestore(&devtree_lock, flags);
985
986#ifdef CONFIG_PROC_DEVICETREE
987 /* try to add to proc as well if it was initialized */
988 if (np->pde)
989 proc_device_tree_add_prop(np->pde, prop);
990#endif /* CONFIG_PROC_DEVICETREE */
991
992 return 0;
993}
994
995/**
996 * prom_remove_property - Remove a property from a node.
997 *
998 * Note that we don't actually remove it, since we have given out
999 * who-knows-how-many pointers to the data using get-property.
1000 * Instead we just move the property to the "dead properties"
1001 * list, so it won't be found any more.
1002 */
1003int prom_remove_property(struct device_node *np, struct property *prop)
1004{
1005 struct property **next;
1006 unsigned long flags;
1007 int found = 0;
1008
1009 write_lock_irqsave(&devtree_lock, flags);
1010 next = &np->properties;
1011 while (*next) {
1012 if (*next == prop) {
1013 /* found the node */
1014 *next = prop->next;
1015 prop->next = np->deadprops;
1016 np->deadprops = prop;
1017 found = 1;
1018 break;
1019 }
1020 next = &(*next)->next;
1021 }
1022 write_unlock_irqrestore(&devtree_lock, flags);
1023
1024 if (!found)
1025 return -ENODEV;
1026
1027#ifdef CONFIG_PROC_DEVICETREE
1028 /* try to remove the proc node as well */
1029 if (np->pde)
1030 proc_device_tree_remove_prop(np->pde, prop);
1031#endif /* CONFIG_PROC_DEVICETREE */
1032
1033 return 0;
1034}
1035
1036/*
1037 * prom_update_property - Update a property in a node.
1038 *
1039 * Note that we don't actually remove it, since we have given out
1040 * who-knows-how-many pointers to the data using get-property.
1041 * Instead we just move the property to the "dead properties" list,
1042 * and add the new property to the property list
1043 */
1044int prom_update_property(struct device_node *np,
1045 struct property *newprop,
1046 struct property *oldprop)
1047{
1048 struct property **next;
1049 unsigned long flags;
1050 int found = 0;
1051
1052 write_lock_irqsave(&devtree_lock, flags);
1053 next = &np->properties;
1054 while (*next) {
1055 if (*next == oldprop) {
1056 /* found the node */
1057 newprop->next = oldprop->next;
1058 *next = newprop;
1059 oldprop->next = np->deadprops;
1060 np->deadprops = oldprop;
1061 found = 1;
1062 break;
1063 }
1064 next = &(*next)->next;
1065 }
1066 write_unlock_irqrestore(&devtree_lock, flags);
1067
1068 if (!found)
1069 return -ENODEV;
1070
1071#ifdef CONFIG_PROC_DEVICETREE
1072 /* try to add to proc as well if it was initialized */
1073 if (np->pde)
1074 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1075#endif /* CONFIG_PROC_DEVICETREE */
1076
1077 return 0;
1078}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001079
1080#if defined(CONFIG_OF_DYNAMIC)
1081/*
1082 * Support for dynamic device trees.
1083 *
1084 * On some platforms, the device tree can be manipulated at runtime.
1085 * The routines in this section support adding, removing and changing
1086 * device tree nodes.
1087 */
1088
1089/**
1090 * of_attach_node - Plug a device node into the tree and global list.
1091 */
1092void of_attach_node(struct device_node *np)
1093{
1094 unsigned long flags;
1095
1096 write_lock_irqsave(&devtree_lock, flags);
1097 np->sibling = np->parent->child;
1098 np->allnext = allnodes;
1099 np->parent->child = np;
1100 allnodes = np;
1101 write_unlock_irqrestore(&devtree_lock, flags);
1102}
1103
1104/**
1105 * of_detach_node - "Unplug" a node from the device tree.
1106 *
1107 * The caller must hold a reference to the node. The memory associated with
1108 * the node is not freed until its refcount goes to zero.
1109 */
1110void of_detach_node(struct device_node *np)
1111{
1112 struct device_node *parent;
1113 unsigned long flags;
1114
1115 write_lock_irqsave(&devtree_lock, flags);
1116
1117 parent = np->parent;
1118 if (!parent)
1119 goto out_unlock;
1120
1121 if (allnodes == np)
1122 allnodes = np->allnext;
1123 else {
1124 struct device_node *prev;
1125 for (prev = allnodes;
1126 prev->allnext != np;
1127 prev = prev->allnext)
1128 ;
1129 prev->allnext = np->allnext;
1130 }
1131
1132 if (parent->child == np)
1133 parent->child = np->sibling;
1134 else {
1135 struct device_node *prevsib;
1136 for (prevsib = np->parent->child;
1137 prevsib->sibling != np;
1138 prevsib = prevsib->sibling)
1139 ;
1140 prevsib->sibling = np->sibling;
1141 }
1142
1143 of_node_set_flag(np, OF_DETACHED);
1144
1145out_unlock:
1146 write_unlock_irqrestore(&devtree_lock, flags);
1147}
1148#endif /* defined(CONFIG_OF_DYNAMIC) */
1149
Shawn Guo611cad72011-08-15 15:28:14 +08001150static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1151 int id, const char *stem, int stem_len)
1152{
1153 ap->np = np;
1154 ap->id = id;
1155 strncpy(ap->stem, stem, stem_len);
1156 ap->stem[stem_len] = 0;
1157 list_add_tail(&ap->link, &aliases_lookup);
1158 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1159 ap->alias, ap->stem, ap->id, np ? np->full_name : NULL);
1160}
1161
1162/**
1163 * of_alias_scan - Scan all properties of 'aliases' node
1164 *
1165 * The function scans all the properties of 'aliases' node and populate
1166 * the the global lookup table with the properties. It returns the
1167 * number of alias_prop found, or error code in error case.
1168 *
1169 * @dt_alloc: An allocator that provides a virtual address to memory
1170 * for the resulting tree
1171 */
1172void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1173{
1174 struct property *pp;
1175
1176 of_chosen = of_find_node_by_path("/chosen");
1177 if (of_chosen == NULL)
1178 of_chosen = of_find_node_by_path("/chosen@0");
1179 of_aliases = of_find_node_by_path("/aliases");
1180 if (!of_aliases)
1181 return;
1182
Dong Aisheng8af0da92011-12-22 20:19:24 +08001183 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001184 const char *start = pp->name;
1185 const char *end = start + strlen(start);
1186 struct device_node *np;
1187 struct alias_prop *ap;
1188 int id, len;
1189
1190 /* Skip those we do not want to proceed */
1191 if (!strcmp(pp->name, "name") ||
1192 !strcmp(pp->name, "phandle") ||
1193 !strcmp(pp->name, "linux,phandle"))
1194 continue;
1195
1196 np = of_find_node_by_path(pp->value);
1197 if (!np)
1198 continue;
1199
1200 /* walk the alias backwards to extract the id and work out
1201 * the 'stem' string */
1202 while (isdigit(*(end-1)) && end > start)
1203 end--;
1204 len = end - start;
1205
1206 if (kstrtoint(end, 10, &id) < 0)
1207 continue;
1208
1209 /* Allocate an alias_prop with enough space for the stem */
1210 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1211 if (!ap)
1212 continue;
1213 ap->alias = start;
1214 of_alias_add(ap, np, id, start, len);
1215 }
1216}
1217
1218/**
1219 * of_alias_get_id - Get alias id for the given device_node
1220 * @np: Pointer to the given device_node
1221 * @stem: Alias stem of the given device_node
1222 *
1223 * The function travels the lookup table to get alias id for the given
1224 * device_node and alias stem. It returns the alias id if find it.
1225 */
1226int of_alias_get_id(struct device_node *np, const char *stem)
1227{
1228 struct alias_prop *app;
1229 int id = -ENODEV;
1230
1231 mutex_lock(&of_aliases_mutex);
1232 list_for_each_entry(app, &aliases_lookup, link) {
1233 if (strcmp(app->stem, stem) != 0)
1234 continue;
1235
1236 if (np == app->np) {
1237 id = app->id;
1238 break;
1239 }
1240 }
1241 mutex_unlock(&of_aliases_mutex);
1242
1243 return id;
1244}
1245EXPORT_SYMBOL_GPL(of_alias_get_id);