blob: 2390ddb22d6094d5b39809aa8f6a7510b7c25c7f [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
Stephen Rothwell581b6052007-04-24 16:46:53 +1000167struct property *of_find_property(const struct device_node *np,
168 const char *name,
169 int *lenp)
170{
171 struct property *pp;
172
Timur Tabi64e45662008-05-08 05:19:59 +1000173 if (!np)
174 return NULL;
175
Stephen Rothwell581b6052007-04-24 16:46:53 +1000176 read_lock(&devtree_lock);
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530177 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000178 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530179 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000180 *lenp = pp->length;
181 break;
182 }
183 }
184 read_unlock(&devtree_lock);
185
186 return pp;
187}
188EXPORT_SYMBOL(of_find_property);
189
Grant Likelye91edcf2009-10-15 10:58:09 -0600190/**
191 * of_find_all_nodes - Get next node in global list
192 * @prev: Previous node or NULL to start iteration
193 * of_node_put() will be called on it
194 *
195 * Returns a node pointer with refcount incremented, use
196 * of_node_put() on it when done.
197 */
198struct device_node *of_find_all_nodes(struct device_node *prev)
199{
200 struct device_node *np;
201
202 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000203 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600204 for (; np != NULL; np = np->allnext)
205 if (of_node_get(np))
206 break;
207 of_node_put(prev);
208 read_unlock(&devtree_lock);
209 return np;
210}
211EXPORT_SYMBOL(of_find_all_nodes);
212
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000213/*
214 * Find a property with a given name for a given node
215 * and return the value.
216 */
217const void *of_get_property(const struct device_node *np, const char *name,
218 int *lenp)
219{
220 struct property *pp = of_find_property(np, name, lenp);
221
222 return pp ? pp->value : NULL;
223}
224EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000225
226/** Checks if the given "compat" string matches one of the strings in
227 * the device's "compatible" property
228 */
229int of_device_is_compatible(const struct device_node *device,
230 const char *compat)
231{
232 const char* cp;
233 int cplen, l;
234
235 cp = of_get_property(device, "compatible", &cplen);
236 if (cp == NULL)
237 return 0;
238 while (cplen > 0) {
239 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
240 return 1;
241 l = strlen(cp) + 1;
242 cp += l;
243 cplen -= l;
244 }
245
246 return 0;
247}
248EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000249
250/**
Grant Likely71a157e2010-02-01 21:34:14 -0700251 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700252 * @compat: compatible string to look for in root node's compatible property.
253 *
254 * Returns true if the root node has the given value in its
255 * compatible property.
256 */
Grant Likely71a157e2010-02-01 21:34:14 -0700257int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700258{
259 struct device_node *root;
260 int rc = 0;
261
262 root = of_find_node_by_path("/");
263 if (root) {
264 rc = of_device_is_compatible(root, compat);
265 of_node_put(root);
266 }
267 return rc;
268}
Grant Likely71a157e2010-02-01 21:34:14 -0700269EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700270
271/**
Josh Boyer834d97d2008-03-27 00:33:14 +1100272 * of_device_is_available - check if a device is available for use
273 *
274 * @device: Node to check for availability
275 *
276 * Returns 1 if the status property is absent or set to "okay" or "ok",
277 * 0 otherwise
278 */
279int of_device_is_available(const struct device_node *device)
280{
281 const char *status;
282 int statlen;
283
284 status = of_get_property(device, "status", &statlen);
285 if (status == NULL)
286 return 1;
287
288 if (statlen > 0) {
289 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
290 return 1;
291 }
292
293 return 0;
294}
295EXPORT_SYMBOL(of_device_is_available);
296
297/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000298 * of_get_parent - Get a node's parent if any
299 * @node: Node to get parent
300 *
301 * Returns a node pointer with refcount incremented, use
302 * of_node_put() on it when done.
303 */
304struct device_node *of_get_parent(const struct device_node *node)
305{
306 struct device_node *np;
307
308 if (!node)
309 return NULL;
310
311 read_lock(&devtree_lock);
312 np = of_node_get(node->parent);
313 read_unlock(&devtree_lock);
314 return np;
315}
316EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000317
318/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000319 * of_get_next_parent - Iterate to a node's parent
320 * @node: Node to get parent of
321 *
322 * This is like of_get_parent() except that it drops the
323 * refcount on the passed node, making it suitable for iterating
324 * through a node's parents.
325 *
326 * Returns a node pointer with refcount incremented, use
327 * of_node_put() on it when done.
328 */
329struct device_node *of_get_next_parent(struct device_node *node)
330{
331 struct device_node *parent;
332
333 if (!node)
334 return NULL;
335
336 read_lock(&devtree_lock);
337 parent = of_node_get(node->parent);
338 of_node_put(node);
339 read_unlock(&devtree_lock);
340 return parent;
341}
342
343/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000344 * of_get_next_child - Iterate a node childs
345 * @node: parent node
346 * @prev: previous child of the parent node, or NULL to get first
347 *
348 * Returns a node pointer with refcount incremented, use
349 * of_node_put() on it when done.
350 */
351struct device_node *of_get_next_child(const struct device_node *node,
352 struct device_node *prev)
353{
354 struct device_node *next;
355
356 read_lock(&devtree_lock);
357 next = prev ? prev->sibling : node->child;
358 for (; next; next = next->sibling)
359 if (of_node_get(next))
360 break;
361 of_node_put(prev);
362 read_unlock(&devtree_lock);
363 return next;
364}
365EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000366
367/**
Timur Tabi32961932012-08-14 13:20:23 +0000368 * of_get_next_available_child - Find the next available child node
369 * @node: parent node
370 * @prev: previous child of the parent node, or NULL to get first
371 *
372 * This function is like of_get_next_child(), except that it
373 * automatically skips any disabled nodes (i.e. status = "disabled").
374 */
375struct device_node *of_get_next_available_child(const struct device_node *node,
376 struct device_node *prev)
377{
378 struct device_node *next;
379
380 read_lock(&devtree_lock);
381 next = prev ? prev->sibling : node->child;
382 for (; next; next = next->sibling) {
383 if (!of_device_is_available(next))
384 continue;
385 if (of_node_get(next))
386 break;
387 }
388 of_node_put(prev);
389 read_unlock(&devtree_lock);
390 return next;
391}
392EXPORT_SYMBOL(of_get_next_available_child);
393
394/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100395 * of_get_child_by_name - Find the child node by name for a given parent
396 * @node: parent node
397 * @name: child name to look for.
398 *
399 * This function looks for child node for given matching name
400 *
401 * Returns a node pointer if found, with refcount incremented, use
402 * of_node_put() on it when done.
403 * Returns NULL if node is not found.
404 */
405struct device_node *of_get_child_by_name(const struct device_node *node,
406 const char *name)
407{
408 struct device_node *child;
409
410 for_each_child_of_node(node, child)
411 if (child->name && (of_node_cmp(child->name, name) == 0))
412 break;
413 return child;
414}
415EXPORT_SYMBOL(of_get_child_by_name);
416
417/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000418 * of_find_node_by_path - Find a node matching a full OF path
419 * @path: The full path to match
420 *
421 * Returns a node pointer with refcount incremented, use
422 * of_node_put() on it when done.
423 */
424struct device_node *of_find_node_by_path(const char *path)
425{
Randy Dunlap465aac62012-11-30 10:01:51 +0000426 struct device_node *np = of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000427
428 read_lock(&devtree_lock);
429 for (; np; np = np->allnext) {
430 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
431 && of_node_get(np))
432 break;
433 }
434 read_unlock(&devtree_lock);
435 return np;
436}
437EXPORT_SYMBOL(of_find_node_by_path);
438
439/**
440 * of_find_node_by_name - Find a node by its "name" property
441 * @from: The node to start searching from or NULL, the node
442 * you pass will not be searched, only the next one
443 * will; typically, you pass what the previous call
444 * returned. of_node_put() will be called on it
445 * @name: The name string to match against
446 *
447 * Returns a node pointer with refcount incremented, use
448 * of_node_put() on it when done.
449 */
450struct device_node *of_find_node_by_name(struct device_node *from,
451 const char *name)
452{
453 struct device_node *np;
454
455 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000456 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000457 for (; np; np = np->allnext)
458 if (np->name && (of_node_cmp(np->name, name) == 0)
459 && of_node_get(np))
460 break;
461 of_node_put(from);
462 read_unlock(&devtree_lock);
463 return np;
464}
465EXPORT_SYMBOL(of_find_node_by_name);
466
467/**
468 * of_find_node_by_type - Find a node by its "device_type" property
469 * @from: The node to start searching from, or NULL to start searching
470 * the entire device tree. The node you pass will not be
471 * searched, only the next one will; typically, you pass
472 * what the previous call returned. of_node_put() will be
473 * called on from for you.
474 * @type: The type string to match against
475 *
476 * Returns a node pointer with refcount incremented, use
477 * of_node_put() on it when done.
478 */
479struct device_node *of_find_node_by_type(struct device_node *from,
480 const char *type)
481{
482 struct device_node *np;
483
484 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000485 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000486 for (; np; np = np->allnext)
487 if (np->type && (of_node_cmp(np->type, type) == 0)
488 && of_node_get(np))
489 break;
490 of_node_put(from);
491 read_unlock(&devtree_lock);
492 return np;
493}
494EXPORT_SYMBOL(of_find_node_by_type);
495
496/**
497 * of_find_compatible_node - Find a node based on type and one of the
498 * tokens in its "compatible" property
499 * @from: The node to start searching from or NULL, the node
500 * you pass will not be searched, only the next one
501 * will; typically, you pass what the previous call
502 * returned. of_node_put() will be called on it
503 * @type: The type string to match "device_type" or NULL to ignore
504 * @compatible: The string to match to one of the tokens in the device
505 * "compatible" list.
506 *
507 * Returns a node pointer with refcount incremented, use
508 * of_node_put() on it when done.
509 */
510struct device_node *of_find_compatible_node(struct device_node *from,
511 const char *type, const char *compatible)
512{
513 struct device_node *np;
514
515 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000516 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000517 for (; np; np = np->allnext) {
518 if (type
519 && !(np->type && (of_node_cmp(np->type, type) == 0)))
520 continue;
521 if (of_device_is_compatible(np, compatible) && of_node_get(np))
522 break;
523 }
524 of_node_put(from);
525 read_unlock(&devtree_lock);
526 return np;
527}
528EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100529
530/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000531 * of_find_node_with_property - Find a node which has a property with
532 * the given name.
533 * @from: The node to start searching from or NULL, the node
534 * you pass will not be searched, only the next one
535 * will; typically, you pass what the previous call
536 * returned. of_node_put() will be called on it
537 * @prop_name: The name of the property to look for.
538 *
539 * Returns a node pointer with refcount incremented, use
540 * of_node_put() on it when done.
541 */
542struct device_node *of_find_node_with_property(struct device_node *from,
543 const char *prop_name)
544{
545 struct device_node *np;
546 struct property *pp;
547
548 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000549 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000550 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530551 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000552 if (of_prop_cmp(pp->name, prop_name) == 0) {
553 of_node_get(np);
554 goto out;
555 }
556 }
557 }
558out:
559 of_node_put(from);
560 read_unlock(&devtree_lock);
561 return np;
562}
563EXPORT_SYMBOL(of_find_node_with_property);
564
565/**
Grant Likely283029d2008-01-09 06:20:40 +1100566 * of_match_node - Tell if an device_node has a matching of_match structure
567 * @matches: array of of device match structures to search in
568 * @node: the of device structure to match against
569 *
570 * Low level utility function used by device matching.
571 */
572const struct of_device_id *of_match_node(const struct of_device_id *matches,
573 const struct device_node *node)
574{
Grant Likelya52f07e2011-03-18 10:21:29 -0600575 if (!matches)
576 return NULL;
577
Grant Likely283029d2008-01-09 06:20:40 +1100578 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
579 int match = 1;
580 if (matches->name[0])
581 match &= node->name
582 && !strcmp(matches->name, node->name);
583 if (matches->type[0])
584 match &= node->type
585 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700586 if (matches->compatible[0])
587 match &= of_device_is_compatible(node,
588 matches->compatible);
589 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100590 return matches;
591 matches++;
592 }
593 return NULL;
594}
595EXPORT_SYMBOL(of_match_node);
596
597/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700598 * of_find_matching_node_and_match - Find a node based on an of_device_id
599 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100600 * @from: The node to start searching from or NULL, the node
601 * you pass will not be searched, only the next one
602 * will; typically, you pass what the previous call
603 * returned. of_node_put() will be called on it
604 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700605 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100606 *
607 * Returns a node pointer with refcount incremented, use
608 * of_node_put() on it when done.
609 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700610struct device_node *of_find_matching_node_and_match(struct device_node *from,
611 const struct of_device_id *matches,
612 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100613{
614 struct device_node *np;
615
Stephen Warren50c8af42012-11-20 16:12:20 -0700616 if (match)
617 *match = NULL;
618
Grant Likely283029d2008-01-09 06:20:40 +1100619 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000620 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100621 for (; np; np = np->allnext) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700622 if (of_match_node(matches, np) && of_node_get(np)) {
623 if (match)
624 *match = matches;
Grant Likely283029d2008-01-09 06:20:40 +1100625 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700626 }
Grant Likely283029d2008-01-09 06:20:40 +1100627 }
628 of_node_put(from);
629 read_unlock(&devtree_lock);
630 return np;
631}
Grant Likely80c20222012-12-19 10:45:36 +0000632EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400633
634/**
Grant Likely3f07af42008-07-25 22:25:13 -0400635 * of_modalias_node - Lookup appropriate modalias for a device node
636 * @node: pointer to a device tree node
637 * @modalias: Pointer to buffer that modalias value will be copied into
638 * @len: Length of modalias value
639 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600640 * Based on the value of the compatible property, this routine will attempt
641 * to choose an appropriate modalias value for a particular device tree node.
642 * It does this by stripping the manufacturer prefix (as delimited by a ',')
643 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400644 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600645 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400646 */
647int of_modalias_node(struct device_node *node, char *modalias, int len)
648{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600649 const char *compatible, *p;
650 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400651
652 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600653 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400654 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400655 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600656 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400657 return 0;
658}
659EXPORT_SYMBOL_GPL(of_modalias_node);
660
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000661/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700662 * of_find_node_by_phandle - Find a node given a phandle
663 * @handle: phandle of the node to find
664 *
665 * Returns a node pointer with refcount incremented, use
666 * of_node_put() on it when done.
667 */
668struct device_node *of_find_node_by_phandle(phandle handle)
669{
670 struct device_node *np;
671
672 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000673 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700674 if (np->phandle == handle)
675 break;
676 of_node_get(np);
677 read_unlock(&devtree_lock);
678 return np;
679}
680EXPORT_SYMBOL(of_find_node_by_phandle);
681
682/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530683 * of_property_read_u8_array - Find and read an array of u8 from a property.
684 *
685 * @np: device node from which the property value is to be read.
686 * @propname: name of the property to be searched.
687 * @out_value: pointer to return value, modified only if return value is 0.
688 * @sz: number of array elements to read
689 *
690 * Search for a property in a device node and read 8-bit value(s) from
691 * it. Returns 0 on success, -EINVAL if the property does not exist,
692 * -ENODATA if property does not have a value, and -EOVERFLOW if the
693 * property data isn't large enough.
694 *
695 * dts entry of array should be like:
696 * property = /bits/ 8 <0x50 0x60 0x70>;
697 *
698 * The out_value is modified only if a valid u8 value can be decoded.
699 */
700int of_property_read_u8_array(const struct device_node *np,
701 const char *propname, u8 *out_values, size_t sz)
702{
703 struct property *prop = of_find_property(np, propname, NULL);
704 const u8 *val;
705
706 if (!prop)
707 return -EINVAL;
708 if (!prop->value)
709 return -ENODATA;
710 if ((sz * sizeof(*out_values)) > prop->length)
711 return -EOVERFLOW;
712
713 val = prop->value;
714 while (sz--)
715 *out_values++ = *val++;
716 return 0;
717}
718EXPORT_SYMBOL_GPL(of_property_read_u8_array);
719
720/**
721 * of_property_read_u16_array - Find and read an array of u16 from a property.
722 *
723 * @np: device node from which the property value is to be read.
724 * @propname: name of the property to be searched.
725 * @out_value: pointer to return value, modified only if return value is 0.
726 * @sz: number of array elements to read
727 *
728 * Search for a property in a device node and read 16-bit value(s) from
729 * it. Returns 0 on success, -EINVAL if the property does not exist,
730 * -ENODATA if property does not have a value, and -EOVERFLOW if the
731 * property data isn't large enough.
732 *
733 * dts entry of array should be like:
734 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
735 *
736 * The out_value is modified only if a valid u16 value can be decoded.
737 */
738int of_property_read_u16_array(const struct device_node *np,
739 const char *propname, u16 *out_values, size_t sz)
740{
741 struct property *prop = of_find_property(np, propname, NULL);
742 const __be16 *val;
743
744 if (!prop)
745 return -EINVAL;
746 if (!prop->value)
747 return -ENODATA;
748 if ((sz * sizeof(*out_values)) > prop->length)
749 return -EOVERFLOW;
750
751 val = prop->value;
752 while (sz--)
753 *out_values++ = be16_to_cpup(val++);
754 return 0;
755}
756EXPORT_SYMBOL_GPL(of_property_read_u16_array);
757
758/**
Rob Herring0e373632011-07-06 15:42:58 -0500759 * of_property_read_u32_array - Find and read an array of 32 bit integers
760 * from a property.
761 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530762 * @np: device node from which the property value is to be read.
763 * @propname: name of the property to be searched.
764 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530765 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530766 *
Rob Herring0e373632011-07-06 15:42:58 -0500767 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530768 * it. Returns 0 on success, -EINVAL if the property does not exist,
769 * -ENODATA if property does not have a value, and -EOVERFLOW if the
770 * property data isn't large enough.
771 *
772 * The out_value is modified only if a valid u32 value can be decoded.
773 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100774int of_property_read_u32_array(const struct device_node *np,
775 const char *propname, u32 *out_values,
776 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530777{
778 struct property *prop = of_find_property(np, propname, NULL);
Rob Herring0e373632011-07-06 15:42:58 -0500779 const __be32 *val;
Thomas Abrahama3b85362011-06-30 21:26:10 +0530780
781 if (!prop)
782 return -EINVAL;
783 if (!prop->value)
784 return -ENODATA;
Rob Herring0e373632011-07-06 15:42:58 -0500785 if ((sz * sizeof(*out_values)) > prop->length)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530786 return -EOVERFLOW;
Rob Herring0e373632011-07-06 15:42:58 -0500787
788 val = prop->value;
789 while (sz--)
790 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530791 return 0;
792}
Rob Herring0e373632011-07-06 15:42:58 -0500793EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530794
795/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100796 * of_property_read_u64 - Find and read a 64 bit integer from a property
797 * @np: device node from which the property value is to be read.
798 * @propname: name of the property to be searched.
799 * @out_value: pointer to return value, modified only if return value is 0.
800 *
801 * Search for a property in a device node and read a 64-bit value from
802 * it. Returns 0 on success, -EINVAL if the property does not exist,
803 * -ENODATA if property does not have a value, and -EOVERFLOW if the
804 * property data isn't large enough.
805 *
806 * The out_value is modified only if a valid u64 value can be decoded.
807 */
808int of_property_read_u64(const struct device_node *np, const char *propname,
809 u64 *out_value)
810{
811 struct property *prop = of_find_property(np, propname, NULL);
812
813 if (!prop)
814 return -EINVAL;
815 if (!prop->value)
816 return -ENODATA;
817 if (sizeof(*out_value) > prop->length)
818 return -EOVERFLOW;
819 *out_value = of_read_number(prop->value, 2);
820 return 0;
821}
822EXPORT_SYMBOL_GPL(of_property_read_u64);
823
824/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530825 * of_property_read_string - Find and read a string from a property
826 * @np: device node from which the property value is to be read.
827 * @propname: name of the property to be searched.
828 * @out_string: pointer to null terminated return string, modified only if
829 * return value is 0.
830 *
831 * Search for a property in a device tree node and retrieve a null
832 * terminated string value (pointer to data, not a copy). Returns 0 on
833 * success, -EINVAL if the property does not exist, -ENODATA if property
834 * does not have a value, and -EILSEQ if the string is not null-terminated
835 * within the length of the property data.
836 *
837 * The out_string pointer is modified only if a valid string can be decoded.
838 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100839int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800840 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530841{
842 struct property *prop = of_find_property(np, propname, NULL);
843 if (!prop)
844 return -EINVAL;
845 if (!prop->value)
846 return -ENODATA;
847 if (strnlen(prop->value, prop->length) >= prop->length)
848 return -EILSEQ;
849 *out_string = prop->value;
850 return 0;
851}
852EXPORT_SYMBOL_GPL(of_property_read_string);
853
854/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200855 * of_property_read_string_index - Find and read a string from a multiple
856 * strings property.
857 * @np: device node from which the property value is to be read.
858 * @propname: name of the property to be searched.
859 * @index: index of the string in the list of strings
860 * @out_string: pointer to null terminated return string, modified only if
861 * return value is 0.
862 *
863 * Search for a property in a device tree node and retrieve a null
864 * terminated string value (pointer to data, not a copy) in the list of strings
865 * contained in that property.
866 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
867 * property does not have a value, and -EILSEQ if the string is not
868 * null-terminated within the length of the property data.
869 *
870 * The out_string pointer is modified only if a valid string can be decoded.
871 */
872int of_property_read_string_index(struct device_node *np, const char *propname,
873 int index, const char **output)
874{
875 struct property *prop = of_find_property(np, propname, NULL);
876 int i = 0;
877 size_t l = 0, total = 0;
878 const char *p;
879
880 if (!prop)
881 return -EINVAL;
882 if (!prop->value)
883 return -ENODATA;
884 if (strnlen(prop->value, prop->length) >= prop->length)
885 return -EILSEQ;
886
887 p = prop->value;
888
889 for (i = 0; total < prop->length; total += l, p += l) {
890 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100891 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200892 *output = p;
893 return 0;
894 }
895 }
896 return -ENODATA;
897}
898EXPORT_SYMBOL_GPL(of_property_read_string_index);
899
Grant Likely7aff0fe2011-12-12 09:25:58 -0700900/**
901 * of_property_match_string() - Find string in a list and return index
902 * @np: pointer to node containing string list property
903 * @propname: string list property name
904 * @string: pointer to string to search for in string list
905 *
906 * This function searches a string list property and returns the index
907 * of a specific string value.
908 */
909int of_property_match_string(struct device_node *np, const char *propname,
910 const char *string)
911{
912 struct property *prop = of_find_property(np, propname, NULL);
913 size_t l;
914 int i;
915 const char *p, *end;
916
917 if (!prop)
918 return -EINVAL;
919 if (!prop->value)
920 return -ENODATA;
921
922 p = prop->value;
923 end = p + prop->length;
924
925 for (i = 0; p < end; i++, p += l) {
926 l = strlen(p) + 1;
927 if (p + l > end)
928 return -EILSEQ;
929 pr_debug("comparing %s with %s\n", string, p);
930 if (strcmp(string, p) == 0)
931 return i; /* Found it; return index */
932 }
933 return -ENODATA;
934}
935EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200936
937/**
938 * of_property_count_strings - Find and return the number of strings from a
939 * multiple strings property.
940 * @np: device node from which the property value is to be read.
941 * @propname: name of the property to be searched.
942 *
943 * Search for a property in a device tree node and retrieve the number of null
944 * terminated string contain in it. Returns the number of strings on
945 * success, -EINVAL if the property does not exist, -ENODATA if property
946 * does not have a value, and -EILSEQ if the string is not null-terminated
947 * within the length of the property data.
948 */
949int of_property_count_strings(struct device_node *np, const char *propname)
950{
951 struct property *prop = of_find_property(np, propname, NULL);
952 int i = 0;
953 size_t l = 0, total = 0;
954 const char *p;
955
956 if (!prop)
957 return -EINVAL;
958 if (!prop->value)
959 return -ENODATA;
960 if (strnlen(prop->value, prop->length) >= prop->length)
961 return -EILSEQ;
962
963 p = prop->value;
964
Benoit Cousson88af7f52011-12-05 15:23:54 +0100965 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200966 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100967
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200968 return i;
969}
970EXPORT_SYMBOL_GPL(of_property_count_strings);
971
972/**
Grant Likely739649c2009-04-25 12:52:40 +0000973 * of_parse_phandle - Resolve a phandle property to a device_node pointer
974 * @np: Pointer to device node holding phandle property
975 * @phandle_name: Name of property holding a phandle value
976 * @index: For properties holding a table of phandles, this is the index into
977 * the table
978 *
979 * Returns the device_node pointer with refcount incremented. Use
980 * of_node_put() on it when done.
981 */
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100982struct device_node *of_parse_phandle(const struct device_node *np,
983 const char *phandle_name, int index)
Grant Likely739649c2009-04-25 12:52:40 +0000984{
Grant Likely9a6b2e52010-07-23 01:48:25 -0600985 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +0000986 int size;
987
988 phandle = of_get_property(np, phandle_name, &size);
989 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
990 return NULL;
991
Grant Likely9a6b2e52010-07-23 01:48:25 -0600992 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +0000993}
994EXPORT_SYMBOL(of_parse_phandle);
995
996/**
Grant Likely15c9a0a2011-12-12 09:25:57 -0700997 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000998 * @np: pointer to a device tree node containing a list
999 * @list_name: property name that contains a list
1000 * @cells_name: property name that specifies phandles' arguments count
1001 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -07001002 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001003 *
1004 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001005 * Returns 0 on success and fills out_args, on error returns appropriate
1006 * errno value.
1007 *
1008 * Caller is responsible to call of_node_put() on the returned out_args->node
1009 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001010 *
1011 * Example:
1012 *
1013 * phandle1: node1 {
1014 * #list-cells = <2>;
1015 * }
1016 *
1017 * phandle2: node2 {
1018 * #list-cells = <1>;
1019 * }
1020 *
1021 * node3 {
1022 * list = <&phandle1 1 2 &phandle2 3>;
1023 * }
1024 *
1025 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001026 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001027 */
Guennadi Liakhovetski93c667c2012-12-10 20:41:30 +01001028int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001029 const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -07001030 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001031{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001032 const __be32 *list, *list_end;
1033 int size, cur_index = 0;
1034 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001035 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001036 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001037
Grant Likely15c9a0a2011-12-12 09:25:57 -07001038 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001039 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001040 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001041 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001042 list_end = list + size / sizeof(*list);
1043
Grant Likely15c9a0a2011-12-12 09:25:57 -07001044 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001045 while (list < list_end) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001046 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001047
Grant Likely15c9a0a2011-12-12 09:25:57 -07001048 /*
1049 * If phandle is 0, then it is an empty entry with no
1050 * arguments. Skip forward to the next entry.
1051 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001052 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001053 if (phandle) {
1054 /*
1055 * Find the provider node and parse the #*-cells
1056 * property to determine the argument length
1057 */
1058 node = of_find_node_by_phandle(phandle);
1059 if (!node) {
1060 pr_err("%s: could not find phandle\n",
1061 np->full_name);
1062 break;
1063 }
1064 if (of_property_read_u32(node, cells_name, &count)) {
1065 pr_err("%s: could not get %s for %s\n",
1066 np->full_name, cells_name,
1067 node->full_name);
1068 break;
1069 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001070
Grant Likely15c9a0a2011-12-12 09:25:57 -07001071 /*
1072 * Make sure that the arguments actually fit in the
1073 * remaining property data length
1074 */
1075 if (list + count > list_end) {
1076 pr_err("%s: arguments longer than property\n",
1077 np->full_name);
1078 break;
1079 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001080 }
1081
Grant Likely15c9a0a2011-12-12 09:25:57 -07001082 /*
1083 * All of the error cases above bail out of the loop, so at
1084 * this point, the parsing is successful. If the requested
1085 * index matches, then fill the out_args structure and return,
1086 * or return -ENOENT for an empty entry.
1087 */
1088 if (cur_index == index) {
1089 if (!phandle)
1090 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001091
Grant Likely15c9a0a2011-12-12 09:25:57 -07001092 if (out_args) {
1093 int i;
1094 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1095 count = MAX_PHANDLE_ARGS;
1096 out_args->np = node;
1097 out_args->args_count = count;
1098 for (i = 0; i < count; i++)
1099 out_args->args[i] = be32_to_cpup(list++);
1100 }
1101 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001102 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001103
1104 of_node_put(node);
1105 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001106 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001107 cur_index++;
1108 }
1109
Grant Likely15c9a0a2011-12-12 09:25:57 -07001110 /* Loop exited without finding a valid entry; return an error */
1111 if (node)
1112 of_node_put(node);
1113 return -EINVAL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001114}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001115EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001116
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001117#if defined(CONFIG_OF_DYNAMIC)
1118static int of_property_notify(int action, struct device_node *np,
1119 struct property *prop)
1120{
1121 struct of_prop_reconfig pr;
1122
1123 pr.dn = np;
1124 pr.prop = prop;
1125 return of_reconfig_notify(action, &pr);
1126}
1127#else
1128static int of_property_notify(int action, struct device_node *np,
1129 struct property *prop)
1130{
1131 return 0;
1132}
1133#endif
1134
Grant Likely02af11b2009-11-23 20:16:45 -07001135/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001136 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001137 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001138int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001139{
1140 struct property **next;
1141 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001142 int rc;
1143
1144 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1145 if (rc)
1146 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001147
1148 prop->next = NULL;
1149 write_lock_irqsave(&devtree_lock, flags);
1150 next = &np->properties;
1151 while (*next) {
1152 if (strcmp(prop->name, (*next)->name) == 0) {
1153 /* duplicate ! don't insert it */
1154 write_unlock_irqrestore(&devtree_lock, flags);
1155 return -1;
1156 }
1157 next = &(*next)->next;
1158 }
1159 *next = prop;
1160 write_unlock_irqrestore(&devtree_lock, flags);
1161
1162#ifdef CONFIG_PROC_DEVICETREE
1163 /* try to add to proc as well if it was initialized */
1164 if (np->pde)
1165 proc_device_tree_add_prop(np->pde, prop);
1166#endif /* CONFIG_PROC_DEVICETREE */
1167
1168 return 0;
1169}
1170
1171/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001172 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001173 *
1174 * Note that we don't actually remove it, since we have given out
1175 * who-knows-how-many pointers to the data using get-property.
1176 * Instead we just move the property to the "dead properties"
1177 * list, so it won't be found any more.
1178 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001179int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001180{
1181 struct property **next;
1182 unsigned long flags;
1183 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001184 int rc;
1185
1186 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1187 if (rc)
1188 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001189
1190 write_lock_irqsave(&devtree_lock, flags);
1191 next = &np->properties;
1192 while (*next) {
1193 if (*next == prop) {
1194 /* found the node */
1195 *next = prop->next;
1196 prop->next = np->deadprops;
1197 np->deadprops = prop;
1198 found = 1;
1199 break;
1200 }
1201 next = &(*next)->next;
1202 }
1203 write_unlock_irqrestore(&devtree_lock, flags);
1204
1205 if (!found)
1206 return -ENODEV;
1207
1208#ifdef CONFIG_PROC_DEVICETREE
1209 /* try to remove the proc node as well */
1210 if (np->pde)
1211 proc_device_tree_remove_prop(np->pde, prop);
1212#endif /* CONFIG_PROC_DEVICETREE */
1213
1214 return 0;
1215}
1216
1217/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001218 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001219 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001220 *
1221 * Note that we don't actually remove it, since we have given out
1222 * who-knows-how-many pointers to the data using get-property.
1223 * Instead we just move the property to the "dead properties" list,
1224 * and add the new property to the property list
1225 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001226int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001227{
Dong Aisheng475d0092012-07-11 15:16:37 +10001228 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001229 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001230 int rc, found = 0;
1231
1232 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1233 if (rc)
1234 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001235
Dong Aisheng475d0092012-07-11 15:16:37 +10001236 if (!newprop->name)
1237 return -EINVAL;
1238
1239 oldprop = of_find_property(np, newprop->name, NULL);
1240 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001241 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001242
Grant Likely02af11b2009-11-23 20:16:45 -07001243 write_lock_irqsave(&devtree_lock, flags);
1244 next = &np->properties;
1245 while (*next) {
1246 if (*next == oldprop) {
1247 /* found the node */
1248 newprop->next = oldprop->next;
1249 *next = newprop;
1250 oldprop->next = np->deadprops;
1251 np->deadprops = oldprop;
1252 found = 1;
1253 break;
1254 }
1255 next = &(*next)->next;
1256 }
1257 write_unlock_irqrestore(&devtree_lock, flags);
1258
1259 if (!found)
1260 return -ENODEV;
1261
1262#ifdef CONFIG_PROC_DEVICETREE
1263 /* try to add to proc as well if it was initialized */
1264 if (np->pde)
1265 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1266#endif /* CONFIG_PROC_DEVICETREE */
1267
1268 return 0;
1269}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001270
1271#if defined(CONFIG_OF_DYNAMIC)
1272/*
1273 * Support for dynamic device trees.
1274 *
1275 * On some platforms, the device tree can be manipulated at runtime.
1276 * The routines in this section support adding, removing and changing
1277 * device tree nodes.
1278 */
1279
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001280static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1281
1282int of_reconfig_notifier_register(struct notifier_block *nb)
1283{
1284 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1285}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001286EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001287
1288int of_reconfig_notifier_unregister(struct notifier_block *nb)
1289{
1290 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1291}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001292EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001293
1294int of_reconfig_notify(unsigned long action, void *p)
1295{
1296 int rc;
1297
1298 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1299 return notifier_to_errno(rc);
1300}
1301
Nathan Fontenote81b3292012-10-02 16:55:01 +00001302#ifdef CONFIG_PROC_DEVICETREE
1303static void of_add_proc_dt_entry(struct device_node *dn)
1304{
1305 struct proc_dir_entry *ent;
1306
1307 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1308 if (ent)
1309 proc_device_tree_add_node(dn, ent);
1310}
1311#else
1312static void of_add_proc_dt_entry(struct device_node *dn)
1313{
1314 return;
1315}
1316#endif
1317
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001318/**
1319 * of_attach_node - Plug a device node into the tree and global list.
1320 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001321int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001322{
1323 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001324 int rc;
1325
1326 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1327 if (rc)
1328 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001329
1330 write_lock_irqsave(&devtree_lock, flags);
1331 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001332 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001333 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001334 of_allnodes = np;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001335 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001336
1337 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001338 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001339}
1340
Nathan Fontenote81b3292012-10-02 16:55:01 +00001341#ifdef CONFIG_PROC_DEVICETREE
1342static void of_remove_proc_dt_entry(struct device_node *dn)
1343{
1344 struct device_node *parent = dn->parent;
1345 struct property *prop = dn->properties;
1346
1347 while (prop) {
1348 remove_proc_entry(prop->name, dn->pde);
1349 prop = prop->next;
1350 }
1351
1352 if (dn->pde)
1353 remove_proc_entry(dn->pde->name, parent->pde);
1354}
1355#else
1356static void of_remove_proc_dt_entry(struct device_node *dn)
1357{
1358 return;
1359}
1360#endif
1361
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001362/**
1363 * of_detach_node - "Unplug" a node from the device tree.
1364 *
1365 * The caller must hold a reference to the node. The memory associated with
1366 * the node is not freed until its refcount goes to zero.
1367 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001368int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001369{
1370 struct device_node *parent;
1371 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001372 int rc = 0;
1373
1374 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1375 if (rc)
1376 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001377
1378 write_lock_irqsave(&devtree_lock, flags);
1379
Nathan Fontenote81b3292012-10-02 16:55:01 +00001380 if (of_node_check_flag(np, OF_DETACHED)) {
1381 /* someone already detached it */
1382 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001383 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001384 }
1385
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001386 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001387 if (!parent) {
1388 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001389 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001390 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001391
Randy Dunlap465aac62012-11-30 10:01:51 +00001392 if (of_allnodes == np)
1393 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001394 else {
1395 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001396 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001397 prev->allnext != np;
1398 prev = prev->allnext)
1399 ;
1400 prev->allnext = np->allnext;
1401 }
1402
1403 if (parent->child == np)
1404 parent->child = np->sibling;
1405 else {
1406 struct device_node *prevsib;
1407 for (prevsib = np->parent->child;
1408 prevsib->sibling != np;
1409 prevsib = prevsib->sibling)
1410 ;
1411 prevsib->sibling = np->sibling;
1412 }
1413
1414 of_node_set_flag(np, OF_DETACHED);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001415 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001416
1417 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001418 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001419}
1420#endif /* defined(CONFIG_OF_DYNAMIC) */
1421
Shawn Guo611cad72011-08-15 15:28:14 +08001422static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1423 int id, const char *stem, int stem_len)
1424{
1425 ap->np = np;
1426 ap->id = id;
1427 strncpy(ap->stem, stem, stem_len);
1428 ap->stem[stem_len] = 0;
1429 list_add_tail(&ap->link, &aliases_lookup);
1430 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001431 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001432}
1433
1434/**
1435 * of_alias_scan - Scan all properties of 'aliases' node
1436 *
1437 * The function scans all the properties of 'aliases' node and populate
1438 * the the global lookup table with the properties. It returns the
1439 * number of alias_prop found, or error code in error case.
1440 *
1441 * @dt_alloc: An allocator that provides a virtual address to memory
1442 * for the resulting tree
1443 */
1444void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1445{
1446 struct property *pp;
1447
1448 of_chosen = of_find_node_by_path("/chosen");
1449 if (of_chosen == NULL)
1450 of_chosen = of_find_node_by_path("/chosen@0");
1451 of_aliases = of_find_node_by_path("/aliases");
1452 if (!of_aliases)
1453 return;
1454
Dong Aisheng8af0da92011-12-22 20:19:24 +08001455 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001456 const char *start = pp->name;
1457 const char *end = start + strlen(start);
1458 struct device_node *np;
1459 struct alias_prop *ap;
1460 int id, len;
1461
1462 /* Skip those we do not want to proceed */
1463 if (!strcmp(pp->name, "name") ||
1464 !strcmp(pp->name, "phandle") ||
1465 !strcmp(pp->name, "linux,phandle"))
1466 continue;
1467
1468 np = of_find_node_by_path(pp->value);
1469 if (!np)
1470 continue;
1471
1472 /* walk the alias backwards to extract the id and work out
1473 * the 'stem' string */
1474 while (isdigit(*(end-1)) && end > start)
1475 end--;
1476 len = end - start;
1477
1478 if (kstrtoint(end, 10, &id) < 0)
1479 continue;
1480
1481 /* Allocate an alias_prop with enough space for the stem */
1482 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1483 if (!ap)
1484 continue;
1485 ap->alias = start;
1486 of_alias_add(ap, np, id, start, len);
1487 }
1488}
1489
1490/**
1491 * of_alias_get_id - Get alias id for the given device_node
1492 * @np: Pointer to the given device_node
1493 * @stem: Alias stem of the given device_node
1494 *
1495 * The function travels the lookup table to get alias id for the given
1496 * device_node and alias stem. It returns the alias id if find it.
1497 */
1498int of_alias_get_id(struct device_node *np, const char *stem)
1499{
1500 struct alias_prop *app;
1501 int id = -ENODEV;
1502
1503 mutex_lock(&of_aliases_mutex);
1504 list_for_each_entry(app, &aliases_lookup, link) {
1505 if (strcmp(app->stem, stem) != 0)
1506 continue;
1507
1508 if (np == app->np) {
1509 id = app->id;
1510 break;
1511 }
1512 }
1513 mutex_unlock(&of_aliases_mutex);
1514
1515 return id;
1516}
1517EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001518
1519const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1520 u32 *pu)
1521{
1522 const void *curv = cur;
1523
1524 if (!prop)
1525 return NULL;
1526
1527 if (!cur) {
1528 curv = prop->value;
1529 goto out_val;
1530 }
1531
1532 curv += sizeof(*cur);
1533 if (curv >= prop->value + prop->length)
1534 return NULL;
1535
1536out_val:
1537 *pu = be32_to_cpup(curv);
1538 return curv;
1539}
1540EXPORT_SYMBOL_GPL(of_prop_next_u32);
1541
1542const char *of_prop_next_string(struct property *prop, const char *cur)
1543{
1544 const void *curv = cur;
1545
1546 if (!prop)
1547 return NULL;
1548
1549 if (!cur)
1550 return prop->value;
1551
1552 curv += strlen(cur) + 1;
1553 if (curv >= prop->value + prop->length)
1554 return NULL;
1555
1556 return curv;
1557}
1558EXPORT_SYMBOL_GPL(of_prop_next_string);