blob: d1bb50719ed7aa16ca57647d2745976c79c2f087 [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 Moskovchenkoced4eec2012-12-06 14:55:41 -080027#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080028
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080029LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080030
Randy Dunlap465aac62012-11-30 10:01:51 +000031struct device_node *of_allnodes;
32EXPORT_SYMBOL(of_allnodes);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070033struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080034struct device_node *of_aliases;
35
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080036DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100037
Stephen Rothwell581b6052007-04-24 16:46:53 +100038/* use when traversing tree through the allnext, child, sibling,
39 * or parent members of struct device_node.
40 */
41DEFINE_RWLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100042
43int of_n_addr_cells(struct device_node *np)
44{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060045 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100046
47 do {
48 if (np->parent)
49 np = np->parent;
50 ip = of_get_property(np, "#address-cells", NULL);
51 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070052 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100053 } while (np->parent);
54 /* No #address-cells property for the root node */
55 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
56}
57EXPORT_SYMBOL(of_n_addr_cells);
58
59int of_n_size_cells(struct device_node *np)
60{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060061 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100062
63 do {
64 if (np->parent)
65 np = np->parent;
66 ip = of_get_property(np, "#size-cells", NULL);
67 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070068 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100069 } while (np->parent);
70 /* No #size-cells property for the root node */
71 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
72}
73EXPORT_SYMBOL(of_n_size_cells);
74
Grant Likely0f22dd32012-02-15 20:38:40 -070075#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070076/**
77 * of_node_get - Increment refcount of a node
78 * @node: Node to inc refcount, NULL is supported to
79 * simplify writing of callers
80 *
81 * Returns node.
82 */
83struct device_node *of_node_get(struct device_node *node)
84{
85 if (node)
86 kref_get(&node->kref);
87 return node;
88}
89EXPORT_SYMBOL(of_node_get);
90
91static inline struct device_node *kref_to_device_node(struct kref *kref)
92{
93 return container_of(kref, struct device_node, kref);
94}
95
96/**
97 * of_node_release - release a dynamically allocated node
98 * @kref: kref element of the node to be released
99 *
100 * In of_node_put() this function is passed to kref_put()
101 * as the destructor.
102 */
103static void of_node_release(struct kref *kref)
104{
105 struct device_node *node = kref_to_device_node(kref);
106 struct property *prop = node->properties;
107
108 /* We should never be releasing nodes that haven't been detached. */
109 if (!of_node_check_flag(node, OF_DETACHED)) {
110 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
111 dump_stack();
112 kref_init(&node->kref);
113 return;
114 }
115
116 if (!of_node_check_flag(node, OF_DYNAMIC))
117 return;
118
119 while (prop) {
120 struct property *next = prop->next;
121 kfree(prop->name);
122 kfree(prop->value);
123 kfree(prop);
124 prop = next;
125
126 if (!prop) {
127 prop = node->deadprops;
128 node->deadprops = NULL;
129 }
130 }
131 kfree(node->full_name);
132 kfree(node->data);
133 kfree(node);
134}
135
136/**
137 * of_node_put - Decrement refcount of a node
138 * @node: Node to dec refcount, NULL is supported to
139 * simplify writing of callers
140 *
141 */
142void of_node_put(struct device_node *node)
143{
144 if (node)
145 kref_put(&node->kref, of_node_release);
146}
147EXPORT_SYMBOL(of_node_put);
Grant Likely0f22dd32012-02-15 20:38:40 -0700148#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700149
Stephen Rothwell581b6052007-04-24 16:46:53 +1000150struct property *of_find_property(const struct device_node *np,
151 const char *name,
152 int *lenp)
153{
154 struct property *pp;
155
Timur Tabi64e45662008-05-08 05:19:59 +1000156 if (!np)
157 return NULL;
158
Stephen Rothwell581b6052007-04-24 16:46:53 +1000159 read_lock(&devtree_lock);
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530160 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000161 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530162 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000163 *lenp = pp->length;
164 break;
165 }
166 }
167 read_unlock(&devtree_lock);
168
169 return pp;
170}
171EXPORT_SYMBOL(of_find_property);
172
Grant Likelye91edcf2009-10-15 10:58:09 -0600173/**
174 * of_find_all_nodes - Get next node in global list
175 * @prev: Previous node or NULL to start iteration
176 * of_node_put() will be called on it
177 *
178 * Returns a node pointer with refcount incremented, use
179 * of_node_put() on it when done.
180 */
181struct device_node *of_find_all_nodes(struct device_node *prev)
182{
183 struct device_node *np;
184
185 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000186 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600187 for (; np != NULL; np = np->allnext)
188 if (of_node_get(np))
189 break;
190 of_node_put(prev);
191 read_unlock(&devtree_lock);
192 return np;
193}
194EXPORT_SYMBOL(of_find_all_nodes);
195
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000196/*
197 * Find a property with a given name for a given node
198 * and return the value.
199 */
200const void *of_get_property(const struct device_node *np, const char *name,
201 int *lenp)
202{
203 struct property *pp = of_find_property(np, name, lenp);
204
205 return pp ? pp->value : NULL;
206}
207EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000208
209/** Checks if the given "compat" string matches one of the strings in
210 * the device's "compatible" property
211 */
212int of_device_is_compatible(const struct device_node *device,
213 const char *compat)
214{
215 const char* cp;
216 int cplen, l;
217
218 cp = of_get_property(device, "compatible", &cplen);
219 if (cp == NULL)
220 return 0;
221 while (cplen > 0) {
222 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
223 return 1;
224 l = strlen(cp) + 1;
225 cp += l;
226 cplen -= l;
227 }
228
229 return 0;
230}
231EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000232
233/**
Grant Likely71a157e8e2010-02-01 21:34:14 -0700234 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700235 * @compat: compatible string to look for in root node's compatible property.
236 *
237 * Returns true if the root node has the given value in its
238 * compatible property.
239 */
Grant Likely71a157e8e2010-02-01 21:34:14 -0700240int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700241{
242 struct device_node *root;
243 int rc = 0;
244
245 root = of_find_node_by_path("/");
246 if (root) {
247 rc = of_device_is_compatible(root, compat);
248 of_node_put(root);
249 }
250 return rc;
251}
Grant Likely71a157e8e2010-02-01 21:34:14 -0700252EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700253
254/**
Josh Boyer834d97d2008-03-27 00:33:14 +1100255 * of_device_is_available - check if a device is available for use
256 *
257 * @device: Node to check for availability
258 *
259 * Returns 1 if the status property is absent or set to "okay" or "ok",
260 * 0 otherwise
261 */
262int of_device_is_available(const struct device_node *device)
263{
264 const char *status;
265 int statlen;
266
267 status = of_get_property(device, "status", &statlen);
268 if (status == NULL)
269 return 1;
270
271 if (statlen > 0) {
272 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
273 return 1;
274 }
275
276 return 0;
277}
278EXPORT_SYMBOL(of_device_is_available);
279
280/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000281 * of_get_parent - Get a node's parent if any
282 * @node: Node to get parent
283 *
284 * Returns a node pointer with refcount incremented, use
285 * of_node_put() on it when done.
286 */
287struct device_node *of_get_parent(const struct device_node *node)
288{
289 struct device_node *np;
290
291 if (!node)
292 return NULL;
293
294 read_lock(&devtree_lock);
295 np = of_node_get(node->parent);
296 read_unlock(&devtree_lock);
297 return np;
298}
299EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000300
301/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000302 * of_get_next_parent - Iterate to a node's parent
303 * @node: Node to get parent of
304 *
305 * This is like of_get_parent() except that it drops the
306 * refcount on the passed node, making it suitable for iterating
307 * through a node's parents.
308 *
309 * Returns a node pointer with refcount incremented, use
310 * of_node_put() on it when done.
311 */
312struct device_node *of_get_next_parent(struct device_node *node)
313{
314 struct device_node *parent;
315
316 if (!node)
317 return NULL;
318
319 read_lock(&devtree_lock);
320 parent = of_node_get(node->parent);
321 of_node_put(node);
322 read_unlock(&devtree_lock);
323 return parent;
324}
325
326/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000327 * of_get_next_child - Iterate a node childs
328 * @node: parent node
329 * @prev: previous child of the parent node, or NULL to get first
330 *
331 * Returns a node pointer with refcount incremented, use
332 * of_node_put() on it when done.
333 */
334struct device_node *of_get_next_child(const struct device_node *node,
335 struct device_node *prev)
336{
337 struct device_node *next;
338
339 read_lock(&devtree_lock);
340 next = prev ? prev->sibling : node->child;
341 for (; next; next = next->sibling)
342 if (of_node_get(next))
343 break;
344 of_node_put(prev);
345 read_unlock(&devtree_lock);
346 return next;
347}
348EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000349
350/**
Timur Tabi32961932012-08-14 13:20:23 +0000351 * of_get_next_available_child - Find the next available child node
352 * @node: parent node
353 * @prev: previous child of the parent node, or NULL to get first
354 *
355 * This function is like of_get_next_child(), except that it
356 * automatically skips any disabled nodes (i.e. status = "disabled").
357 */
358struct device_node *of_get_next_available_child(const struct device_node *node,
359 struct device_node *prev)
360{
361 struct device_node *next;
362
363 read_lock(&devtree_lock);
364 next = prev ? prev->sibling : node->child;
365 for (; next; next = next->sibling) {
366 if (!of_device_is_available(next))
367 continue;
368 if (of_node_get(next))
369 break;
370 }
371 of_node_put(prev);
372 read_unlock(&devtree_lock);
373 return next;
374}
375EXPORT_SYMBOL(of_get_next_available_child);
376
377/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100378 * of_get_child_by_name - Find the child node by name for a given parent
379 * @node: parent node
380 * @name: child name to look for.
381 *
382 * This function looks for child node for given matching name
383 *
384 * Returns a node pointer if found, with refcount incremented, use
385 * of_node_put() on it when done.
386 * Returns NULL if node is not found.
387 */
388struct device_node *of_get_child_by_name(const struct device_node *node,
389 const char *name)
390{
391 struct device_node *child;
392
393 for_each_child_of_node(node, child)
394 if (child->name && (of_node_cmp(child->name, name) == 0))
395 break;
396 return child;
397}
398EXPORT_SYMBOL(of_get_child_by_name);
399
400/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000401 * of_find_node_by_path - Find a node matching a full OF path
402 * @path: The full path to match
403 *
404 * Returns a node pointer with refcount incremented, use
405 * of_node_put() on it when done.
406 */
407struct device_node *of_find_node_by_path(const char *path)
408{
Randy Dunlap465aac62012-11-30 10:01:51 +0000409 struct device_node *np = of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000410
411 read_lock(&devtree_lock);
412 for (; np; np = np->allnext) {
413 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
414 && of_node_get(np))
415 break;
416 }
417 read_unlock(&devtree_lock);
418 return np;
419}
420EXPORT_SYMBOL(of_find_node_by_path);
421
422/**
423 * of_find_node_by_name - Find a node by its "name" property
424 * @from: The node to start searching from or NULL, the node
425 * you pass will not be searched, only the next one
426 * will; typically, you pass what the previous call
427 * returned. of_node_put() will be called on it
428 * @name: The name string to match against
429 *
430 * Returns a node pointer with refcount incremented, use
431 * of_node_put() on it when done.
432 */
433struct device_node *of_find_node_by_name(struct device_node *from,
434 const char *name)
435{
436 struct device_node *np;
437
438 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000439 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000440 for (; np; np = np->allnext)
441 if (np->name && (of_node_cmp(np->name, name) == 0)
442 && of_node_get(np))
443 break;
444 of_node_put(from);
445 read_unlock(&devtree_lock);
446 return np;
447}
448EXPORT_SYMBOL(of_find_node_by_name);
449
450/**
451 * of_find_node_by_type - Find a node by its "device_type" property
452 * @from: The node to start searching from, or NULL to start searching
453 * the entire device tree. The node you pass will not be
454 * searched, only the next one will; typically, you pass
455 * what the previous call returned. of_node_put() will be
456 * called on from for you.
457 * @type: The type string to match against
458 *
459 * Returns a node pointer with refcount incremented, use
460 * of_node_put() on it when done.
461 */
462struct device_node *of_find_node_by_type(struct device_node *from,
463 const char *type)
464{
465 struct device_node *np;
466
467 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000468 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000469 for (; np; np = np->allnext)
470 if (np->type && (of_node_cmp(np->type, type) == 0)
471 && of_node_get(np))
472 break;
473 of_node_put(from);
474 read_unlock(&devtree_lock);
475 return np;
476}
477EXPORT_SYMBOL(of_find_node_by_type);
478
479/**
480 * of_find_compatible_node - Find a node based on type and one of the
481 * tokens in its "compatible" property
482 * @from: The node to start searching from or NULL, the node
483 * you pass will not be searched, only the next one
484 * will; typically, you pass what the previous call
485 * returned. of_node_put() will be called on it
486 * @type: The type string to match "device_type" or NULL to ignore
487 * @compatible: The string to match to one of the tokens in the device
488 * "compatible" list.
489 *
490 * Returns a node pointer with refcount incremented, use
491 * of_node_put() on it when done.
492 */
493struct device_node *of_find_compatible_node(struct device_node *from,
494 const char *type, const char *compatible)
495{
496 struct device_node *np;
497
498 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000499 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000500 for (; np; np = np->allnext) {
501 if (type
502 && !(np->type && (of_node_cmp(np->type, type) == 0)))
503 continue;
504 if (of_device_is_compatible(np, compatible) && of_node_get(np))
505 break;
506 }
507 of_node_put(from);
508 read_unlock(&devtree_lock);
509 return np;
510}
511EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100512
513/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000514 * of_find_node_with_property - Find a node which has a property with
515 * the given name.
516 * @from: The node to start searching from or NULL, the node
517 * you pass will not be searched, only the next one
518 * will; typically, you pass what the previous call
519 * returned. of_node_put() will be called on it
520 * @prop_name: The name of the property to look for.
521 *
522 * Returns a node pointer with refcount incremented, use
523 * of_node_put() on it when done.
524 */
525struct device_node *of_find_node_with_property(struct device_node *from,
526 const char *prop_name)
527{
528 struct device_node *np;
529 struct property *pp;
530
531 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000532 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000533 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530534 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000535 if (of_prop_cmp(pp->name, prop_name) == 0) {
536 of_node_get(np);
537 goto out;
538 }
539 }
540 }
541out:
542 of_node_put(from);
543 read_unlock(&devtree_lock);
544 return np;
545}
546EXPORT_SYMBOL(of_find_node_with_property);
547
548/**
Grant Likely283029d2008-01-09 06:20:40 +1100549 * of_match_node - Tell if an device_node has a matching of_match structure
550 * @matches: array of of device match structures to search in
551 * @node: the of device structure to match against
552 *
553 * Low level utility function used by device matching.
554 */
555const struct of_device_id *of_match_node(const struct of_device_id *matches,
556 const struct device_node *node)
557{
Grant Likelya52f07e2011-03-18 10:21:29 -0600558 if (!matches)
559 return NULL;
560
Grant Likely283029d2008-01-09 06:20:40 +1100561 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
562 int match = 1;
563 if (matches->name[0])
564 match &= node->name
565 && !strcmp(matches->name, node->name);
566 if (matches->type[0])
567 match &= node->type
568 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700569 if (matches->compatible[0])
570 match &= of_device_is_compatible(node,
571 matches->compatible);
572 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100573 return matches;
574 matches++;
575 }
576 return NULL;
577}
578EXPORT_SYMBOL(of_match_node);
579
580/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700581 * of_find_matching_node_and_match - Find a node based on an of_device_id
582 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100583 * @from: The node to start searching from or NULL, the node
584 * you pass will not be searched, only the next one
585 * will; typically, you pass what the previous call
586 * returned. of_node_put() will be called on it
587 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700588 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100589 *
590 * Returns a node pointer with refcount incremented, use
591 * of_node_put() on it when done.
592 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700593struct device_node *of_find_matching_node_and_match(struct device_node *from,
594 const struct of_device_id *matches,
595 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100596{
597 struct device_node *np;
598
Stephen Warren50c8af42012-11-20 16:12:20 -0700599 if (match)
600 *match = NULL;
601
Grant Likely283029d2008-01-09 06:20:40 +1100602 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000603 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100604 for (; np; np = np->allnext) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700605 if (of_match_node(matches, np) && of_node_get(np)) {
606 if (match)
607 *match = matches;
Grant Likely283029d2008-01-09 06:20:40 +1100608 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700609 }
Grant Likely283029d2008-01-09 06:20:40 +1100610 }
611 of_node_put(from);
612 read_unlock(&devtree_lock);
613 return np;
614}
Grant Likely80c20222012-12-19 10:45:36 +0000615EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400616
617/**
Grant Likely3f07af42008-07-25 22:25:13 -0400618 * of_modalias_node - Lookup appropriate modalias for a device node
619 * @node: pointer to a device tree node
620 * @modalias: Pointer to buffer that modalias value will be copied into
621 * @len: Length of modalias value
622 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600623 * Based on the value of the compatible property, this routine will attempt
624 * to choose an appropriate modalias value for a particular device tree node.
625 * It does this by stripping the manufacturer prefix (as delimited by a ',')
626 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400627 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600628 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400629 */
630int of_modalias_node(struct device_node *node, char *modalias, int len)
631{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600632 const char *compatible, *p;
633 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400634
635 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600636 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400637 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400638 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600639 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400640 return 0;
641}
642EXPORT_SYMBOL_GPL(of_modalias_node);
643
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000644/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700645 * of_find_node_by_phandle - Find a node given a phandle
646 * @handle: phandle of the node to find
647 *
648 * Returns a node pointer with refcount incremented, use
649 * of_node_put() on it when done.
650 */
651struct device_node *of_find_node_by_phandle(phandle handle)
652{
653 struct device_node *np;
654
655 read_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000656 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700657 if (np->phandle == handle)
658 break;
659 of_node_get(np);
660 read_unlock(&devtree_lock);
661 return np;
662}
663EXPORT_SYMBOL(of_find_node_by_phandle);
664
665/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530666 * of_property_read_u8_array - Find and read an array of u8 from a property.
667 *
668 * @np: device node from which the property value is to be read.
669 * @propname: name of the property to be searched.
670 * @out_value: pointer to return value, modified only if return value is 0.
671 * @sz: number of array elements to read
672 *
673 * Search for a property in a device node and read 8-bit value(s) from
674 * it. Returns 0 on success, -EINVAL if the property does not exist,
675 * -ENODATA if property does not have a value, and -EOVERFLOW if the
676 * property data isn't large enough.
677 *
678 * dts entry of array should be like:
679 * property = /bits/ 8 <0x50 0x60 0x70>;
680 *
681 * The out_value is modified only if a valid u8 value can be decoded.
682 */
683int of_property_read_u8_array(const struct device_node *np,
684 const char *propname, u8 *out_values, size_t sz)
685{
686 struct property *prop = of_find_property(np, propname, NULL);
687 const u8 *val;
688
689 if (!prop)
690 return -EINVAL;
691 if (!prop->value)
692 return -ENODATA;
693 if ((sz * sizeof(*out_values)) > prop->length)
694 return -EOVERFLOW;
695
696 val = prop->value;
697 while (sz--)
698 *out_values++ = *val++;
699 return 0;
700}
701EXPORT_SYMBOL_GPL(of_property_read_u8_array);
702
703/**
704 * of_property_read_u16_array - Find and read an array of u16 from a property.
705 *
706 * @np: device node from which the property value is to be read.
707 * @propname: name of the property to be searched.
708 * @out_value: pointer to return value, modified only if return value is 0.
709 * @sz: number of array elements to read
710 *
711 * Search for a property in a device node and read 16-bit value(s) from
712 * it. Returns 0 on success, -EINVAL if the property does not exist,
713 * -ENODATA if property does not have a value, and -EOVERFLOW if the
714 * property data isn't large enough.
715 *
716 * dts entry of array should be like:
717 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
718 *
719 * The out_value is modified only if a valid u16 value can be decoded.
720 */
721int of_property_read_u16_array(const struct device_node *np,
722 const char *propname, u16 *out_values, size_t sz)
723{
724 struct property *prop = of_find_property(np, propname, NULL);
725 const __be16 *val;
726
727 if (!prop)
728 return -EINVAL;
729 if (!prop->value)
730 return -ENODATA;
731 if ((sz * sizeof(*out_values)) > prop->length)
732 return -EOVERFLOW;
733
734 val = prop->value;
735 while (sz--)
736 *out_values++ = be16_to_cpup(val++);
737 return 0;
738}
739EXPORT_SYMBOL_GPL(of_property_read_u16_array);
740
741/**
Rob Herring0e373632011-07-06 15:42:58 -0500742 * of_property_read_u32_array - Find and read an array of 32 bit integers
743 * from a property.
744 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530745 * @np: device node from which the property value is to be read.
746 * @propname: name of the property to be searched.
747 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530748 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530749 *
Rob Herring0e373632011-07-06 15:42:58 -0500750 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530751 * it. Returns 0 on success, -EINVAL if the property does not exist,
752 * -ENODATA if property does not have a value, and -EOVERFLOW if the
753 * property data isn't large enough.
754 *
755 * The out_value is modified only if a valid u32 value can be decoded.
756 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100757int of_property_read_u32_array(const struct device_node *np,
758 const char *propname, u32 *out_values,
759 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530760{
761 struct property *prop = of_find_property(np, propname, NULL);
Rob Herring0e373632011-07-06 15:42:58 -0500762 const __be32 *val;
Thomas Abrahama3b85362011-06-30 21:26:10 +0530763
764 if (!prop)
765 return -EINVAL;
766 if (!prop->value)
767 return -ENODATA;
Rob Herring0e373632011-07-06 15:42:58 -0500768 if ((sz * sizeof(*out_values)) > prop->length)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530769 return -EOVERFLOW;
Rob Herring0e373632011-07-06 15:42:58 -0500770
771 val = prop->value;
772 while (sz--)
773 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530774 return 0;
775}
Rob Herring0e373632011-07-06 15:42:58 -0500776EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530777
778/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100779 * of_property_read_u64 - Find and read a 64 bit integer from a property
780 * @np: device node from which the property value is to be read.
781 * @propname: name of the property to be searched.
782 * @out_value: pointer to return value, modified only if return value is 0.
783 *
784 * Search for a property in a device node and read a 64-bit value from
785 * it. Returns 0 on success, -EINVAL if the property does not exist,
786 * -ENODATA if property does not have a value, and -EOVERFLOW if the
787 * property data isn't large enough.
788 *
789 * The out_value is modified only if a valid u64 value can be decoded.
790 */
791int of_property_read_u64(const struct device_node *np, const char *propname,
792 u64 *out_value)
793{
794 struct property *prop = of_find_property(np, propname, NULL);
795
796 if (!prop)
797 return -EINVAL;
798 if (!prop->value)
799 return -ENODATA;
800 if (sizeof(*out_value) > prop->length)
801 return -EOVERFLOW;
802 *out_value = of_read_number(prop->value, 2);
803 return 0;
804}
805EXPORT_SYMBOL_GPL(of_property_read_u64);
806
807/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530808 * of_property_read_string - Find and read a string from a property
809 * @np: device node from which the property value is to be read.
810 * @propname: name of the property to be searched.
811 * @out_string: pointer to null terminated return string, modified only if
812 * return value is 0.
813 *
814 * Search for a property in a device tree node and retrieve a null
815 * terminated string value (pointer to data, not a copy). Returns 0 on
816 * success, -EINVAL if the property does not exist, -ENODATA if property
817 * does not have a value, and -EILSEQ if the string is not null-terminated
818 * within the length of the property data.
819 *
820 * The out_string pointer is modified only if a valid string can be decoded.
821 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100822int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800823 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530824{
825 struct property *prop = of_find_property(np, propname, NULL);
826 if (!prop)
827 return -EINVAL;
828 if (!prop->value)
829 return -ENODATA;
830 if (strnlen(prop->value, prop->length) >= prop->length)
831 return -EILSEQ;
832 *out_string = prop->value;
833 return 0;
834}
835EXPORT_SYMBOL_GPL(of_property_read_string);
836
837/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200838 * of_property_read_string_index - Find and read a string from a multiple
839 * strings property.
840 * @np: device node from which the property value is to be read.
841 * @propname: name of the property to be searched.
842 * @index: index of the string in the list of strings
843 * @out_string: pointer to null terminated return string, modified only if
844 * return value is 0.
845 *
846 * Search for a property in a device tree node and retrieve a null
847 * terminated string value (pointer to data, not a copy) in the list of strings
848 * contained in that property.
849 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
850 * property does not have a value, and -EILSEQ if the string is not
851 * null-terminated within the length of the property data.
852 *
853 * The out_string pointer is modified only if a valid string can be decoded.
854 */
855int of_property_read_string_index(struct device_node *np, const char *propname,
856 int index, const char **output)
857{
858 struct property *prop = of_find_property(np, propname, NULL);
859 int i = 0;
860 size_t l = 0, total = 0;
861 const char *p;
862
863 if (!prop)
864 return -EINVAL;
865 if (!prop->value)
866 return -ENODATA;
867 if (strnlen(prop->value, prop->length) >= prop->length)
868 return -EILSEQ;
869
870 p = prop->value;
871
872 for (i = 0; total < prop->length; total += l, p += l) {
873 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100874 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200875 *output = p;
876 return 0;
877 }
878 }
879 return -ENODATA;
880}
881EXPORT_SYMBOL_GPL(of_property_read_string_index);
882
Grant Likely7aff0fe2011-12-12 09:25:58 -0700883/**
884 * of_property_match_string() - Find string in a list and return index
885 * @np: pointer to node containing string list property
886 * @propname: string list property name
887 * @string: pointer to string to search for in string list
888 *
889 * This function searches a string list property and returns the index
890 * of a specific string value.
891 */
892int of_property_match_string(struct device_node *np, const char *propname,
893 const char *string)
894{
895 struct property *prop = of_find_property(np, propname, NULL);
896 size_t l;
897 int i;
898 const char *p, *end;
899
900 if (!prop)
901 return -EINVAL;
902 if (!prop->value)
903 return -ENODATA;
904
905 p = prop->value;
906 end = p + prop->length;
907
908 for (i = 0; p < end; i++, p += l) {
909 l = strlen(p) + 1;
910 if (p + l > end)
911 return -EILSEQ;
912 pr_debug("comparing %s with %s\n", string, p);
913 if (strcmp(string, p) == 0)
914 return i; /* Found it; return index */
915 }
916 return -ENODATA;
917}
918EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200919
920/**
921 * of_property_count_strings - Find and return the number of strings from a
922 * multiple strings property.
923 * @np: device node from which the property value is to be read.
924 * @propname: name of the property to be searched.
925 *
926 * Search for a property in a device tree node and retrieve the number of null
927 * terminated string contain in it. Returns the number of strings on
928 * success, -EINVAL if the property does not exist, -ENODATA if property
929 * does not have a value, and -EILSEQ if the string is not null-terminated
930 * within the length of the property data.
931 */
932int of_property_count_strings(struct device_node *np, const char *propname)
933{
934 struct property *prop = of_find_property(np, propname, NULL);
935 int i = 0;
936 size_t l = 0, total = 0;
937 const char *p;
938
939 if (!prop)
940 return -EINVAL;
941 if (!prop->value)
942 return -ENODATA;
943 if (strnlen(prop->value, prop->length) >= prop->length)
944 return -EILSEQ;
945
946 p = prop->value;
947
Benoit Cousson88af7f52011-12-05 15:23:54 +0100948 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200949 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100950
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200951 return i;
952}
953EXPORT_SYMBOL_GPL(of_property_count_strings);
954
955/**
Grant Likely739649c2009-04-25 12:52:40 +0000956 * of_parse_phandle - Resolve a phandle property to a device_node pointer
957 * @np: Pointer to device node holding phandle property
958 * @phandle_name: Name of property holding a phandle value
959 * @index: For properties holding a table of phandles, this is the index into
960 * the table
961 *
962 * Returns the device_node pointer with refcount incremented. Use
963 * of_node_put() on it when done.
964 */
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100965struct device_node *of_parse_phandle(const struct device_node *np,
966 const char *phandle_name, int index)
Grant Likely739649c2009-04-25 12:52:40 +0000967{
Grant Likely9a6b2e52010-07-23 01:48:25 -0600968 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +0000969 int size;
970
971 phandle = of_get_property(np, phandle_name, &size);
972 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
973 return NULL;
974
Grant Likely9a6b2e52010-07-23 01:48:25 -0600975 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +0000976}
977EXPORT_SYMBOL(of_parse_phandle);
978
979/**
Grant Likely15c9a0a2011-12-12 09:25:57 -0700980 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000981 * @np: pointer to a device tree node containing a list
982 * @list_name: property name that contains a list
983 * @cells_name: property name that specifies phandles' arguments count
984 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -0700985 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000986 *
987 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -0700988 * Returns 0 on success and fills out_args, on error returns appropriate
989 * errno value.
990 *
991 * Caller is responsible to call of_node_put() on the returned out_args->node
992 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000993 *
994 * Example:
995 *
996 * phandle1: node1 {
997 * #list-cells = <2>;
998 * }
999 *
1000 * phandle2: node2 {
1001 * #list-cells = <1>;
1002 * }
1003 *
1004 * node3 {
1005 * list = <&phandle1 1 2 &phandle2 3>;
1006 * }
1007 *
1008 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001009 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001010 */
Guennadi Liakhovetski93c667c2012-12-10 20:41:30 +01001011int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001012 const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -07001013 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001014{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001015 const __be32 *list, *list_end;
1016 int size, cur_index = 0;
1017 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001018 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001019 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001020
Grant Likely15c9a0a2011-12-12 09:25:57 -07001021 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001022 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001023 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001024 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001025 list_end = list + size / sizeof(*list);
1026
Grant Likely15c9a0a2011-12-12 09:25:57 -07001027 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001028 while (list < list_end) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001029 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001030
Grant Likely15c9a0a2011-12-12 09:25:57 -07001031 /*
1032 * If phandle is 0, then it is an empty entry with no
1033 * arguments. Skip forward to the next entry.
1034 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001035 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001036 if (phandle) {
1037 /*
1038 * Find the provider node and parse the #*-cells
1039 * property to determine the argument length
1040 */
1041 node = of_find_node_by_phandle(phandle);
1042 if (!node) {
1043 pr_err("%s: could not find phandle\n",
1044 np->full_name);
1045 break;
1046 }
1047 if (of_property_read_u32(node, cells_name, &count)) {
1048 pr_err("%s: could not get %s for %s\n",
1049 np->full_name, cells_name,
1050 node->full_name);
1051 break;
1052 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001053
Grant Likely15c9a0a2011-12-12 09:25:57 -07001054 /*
1055 * Make sure that the arguments actually fit in the
1056 * remaining property data length
1057 */
1058 if (list + count > list_end) {
1059 pr_err("%s: arguments longer than property\n",
1060 np->full_name);
1061 break;
1062 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001063 }
1064
Grant Likely15c9a0a2011-12-12 09:25:57 -07001065 /*
1066 * All of the error cases above bail out of the loop, so at
1067 * this point, the parsing is successful. If the requested
1068 * index matches, then fill the out_args structure and return,
1069 * or return -ENOENT for an empty entry.
1070 */
1071 if (cur_index == index) {
1072 if (!phandle)
1073 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001074
Grant Likely15c9a0a2011-12-12 09:25:57 -07001075 if (out_args) {
1076 int i;
1077 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1078 count = MAX_PHANDLE_ARGS;
1079 out_args->np = node;
1080 out_args->args_count = count;
1081 for (i = 0; i < count; i++)
1082 out_args->args[i] = be32_to_cpup(list++);
1083 }
1084 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001085 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001086
1087 of_node_put(node);
1088 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001089 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001090 cur_index++;
1091 }
1092
Grant Likely15c9a0a2011-12-12 09:25:57 -07001093 /* Loop exited without finding a valid entry; return an error */
1094 if (node)
1095 of_node_put(node);
1096 return -EINVAL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001097}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001098EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001099
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001100#if defined(CONFIG_OF_DYNAMIC)
1101static int of_property_notify(int action, struct device_node *np,
1102 struct property *prop)
1103{
1104 struct of_prop_reconfig pr;
1105
1106 pr.dn = np;
1107 pr.prop = prop;
1108 return of_reconfig_notify(action, &pr);
1109}
1110#else
1111static int of_property_notify(int action, struct device_node *np,
1112 struct property *prop)
1113{
1114 return 0;
1115}
1116#endif
1117
Grant Likely02af11b2009-11-23 20:16:45 -07001118/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001119 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001120 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001121int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001122{
1123 struct property **next;
1124 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001125 int rc;
1126
1127 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1128 if (rc)
1129 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001130
1131 prop->next = NULL;
1132 write_lock_irqsave(&devtree_lock, flags);
1133 next = &np->properties;
1134 while (*next) {
1135 if (strcmp(prop->name, (*next)->name) == 0) {
1136 /* duplicate ! don't insert it */
1137 write_unlock_irqrestore(&devtree_lock, flags);
1138 return -1;
1139 }
1140 next = &(*next)->next;
1141 }
1142 *next = prop;
1143 write_unlock_irqrestore(&devtree_lock, flags);
1144
1145#ifdef CONFIG_PROC_DEVICETREE
1146 /* try to add to proc as well if it was initialized */
1147 if (np->pde)
1148 proc_device_tree_add_prop(np->pde, prop);
1149#endif /* CONFIG_PROC_DEVICETREE */
1150
1151 return 0;
1152}
1153
1154/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001155 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001156 *
1157 * Note that we don't actually remove it, since we have given out
1158 * who-knows-how-many pointers to the data using get-property.
1159 * Instead we just move the property to the "dead properties"
1160 * list, so it won't be found any more.
1161 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001162int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001163{
1164 struct property **next;
1165 unsigned long flags;
1166 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001167 int rc;
1168
1169 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1170 if (rc)
1171 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001172
1173 write_lock_irqsave(&devtree_lock, flags);
1174 next = &np->properties;
1175 while (*next) {
1176 if (*next == prop) {
1177 /* found the node */
1178 *next = prop->next;
1179 prop->next = np->deadprops;
1180 np->deadprops = prop;
1181 found = 1;
1182 break;
1183 }
1184 next = &(*next)->next;
1185 }
1186 write_unlock_irqrestore(&devtree_lock, flags);
1187
1188 if (!found)
1189 return -ENODEV;
1190
1191#ifdef CONFIG_PROC_DEVICETREE
1192 /* try to remove the proc node as well */
1193 if (np->pde)
1194 proc_device_tree_remove_prop(np->pde, prop);
1195#endif /* CONFIG_PROC_DEVICETREE */
1196
1197 return 0;
1198}
1199
1200/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001201 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001202 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001203 *
1204 * Note that we don't actually remove it, since we have given out
1205 * who-knows-how-many pointers to the data using get-property.
1206 * Instead we just move the property to the "dead properties" list,
1207 * and add the new property to the property list
1208 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001209int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001210{
Dong Aisheng475d0092012-07-11 15:16:37 +10001211 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001212 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001213 int rc, found = 0;
1214
1215 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1216 if (rc)
1217 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001218
Dong Aisheng475d0092012-07-11 15:16:37 +10001219 if (!newprop->name)
1220 return -EINVAL;
1221
1222 oldprop = of_find_property(np, newprop->name, NULL);
1223 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001224 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001225
Grant Likely02af11b2009-11-23 20:16:45 -07001226 write_lock_irqsave(&devtree_lock, flags);
1227 next = &np->properties;
1228 while (*next) {
1229 if (*next == oldprop) {
1230 /* found the node */
1231 newprop->next = oldprop->next;
1232 *next = newprop;
1233 oldprop->next = np->deadprops;
1234 np->deadprops = oldprop;
1235 found = 1;
1236 break;
1237 }
1238 next = &(*next)->next;
1239 }
1240 write_unlock_irqrestore(&devtree_lock, flags);
1241
1242 if (!found)
1243 return -ENODEV;
1244
1245#ifdef CONFIG_PROC_DEVICETREE
1246 /* try to add to proc as well if it was initialized */
1247 if (np->pde)
1248 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1249#endif /* CONFIG_PROC_DEVICETREE */
1250
1251 return 0;
1252}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001253
1254#if defined(CONFIG_OF_DYNAMIC)
1255/*
1256 * Support for dynamic device trees.
1257 *
1258 * On some platforms, the device tree can be manipulated at runtime.
1259 * The routines in this section support adding, removing and changing
1260 * device tree nodes.
1261 */
1262
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001263static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1264
1265int of_reconfig_notifier_register(struct notifier_block *nb)
1266{
1267 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1268}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001269EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001270
1271int of_reconfig_notifier_unregister(struct notifier_block *nb)
1272{
1273 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1274}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001275EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001276
1277int of_reconfig_notify(unsigned long action, void *p)
1278{
1279 int rc;
1280
1281 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1282 return notifier_to_errno(rc);
1283}
1284
Nathan Fontenote81b3292012-10-02 16:55:01 +00001285#ifdef CONFIG_PROC_DEVICETREE
1286static void of_add_proc_dt_entry(struct device_node *dn)
1287{
1288 struct proc_dir_entry *ent;
1289
1290 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1291 if (ent)
1292 proc_device_tree_add_node(dn, ent);
1293}
1294#else
1295static void of_add_proc_dt_entry(struct device_node *dn)
1296{
1297 return;
1298}
1299#endif
1300
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001301/**
1302 * of_attach_node - Plug a device node into the tree and global list.
1303 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001304int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001305{
1306 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001307 int rc;
1308
1309 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1310 if (rc)
1311 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001312
1313 write_lock_irqsave(&devtree_lock, flags);
1314 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001315 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001316 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001317 of_allnodes = np;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001318 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001319
1320 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001321 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001322}
1323
Nathan Fontenote81b3292012-10-02 16:55:01 +00001324#ifdef CONFIG_PROC_DEVICETREE
1325static void of_remove_proc_dt_entry(struct device_node *dn)
1326{
1327 struct device_node *parent = dn->parent;
1328 struct property *prop = dn->properties;
1329
1330 while (prop) {
1331 remove_proc_entry(prop->name, dn->pde);
1332 prop = prop->next;
1333 }
1334
1335 if (dn->pde)
1336 remove_proc_entry(dn->pde->name, parent->pde);
1337}
1338#else
1339static void of_remove_proc_dt_entry(struct device_node *dn)
1340{
1341 return;
1342}
1343#endif
1344
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001345/**
1346 * of_detach_node - "Unplug" a node from the device tree.
1347 *
1348 * The caller must hold a reference to the node. The memory associated with
1349 * the node is not freed until its refcount goes to zero.
1350 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001351int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001352{
1353 struct device_node *parent;
1354 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001355 int rc = 0;
1356
1357 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1358 if (rc)
1359 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001360
1361 write_lock_irqsave(&devtree_lock, flags);
1362
Nathan Fontenote81b3292012-10-02 16:55:01 +00001363 if (of_node_check_flag(np, OF_DETACHED)) {
1364 /* someone already detached it */
1365 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001366 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001367 }
1368
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001369 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001370 if (!parent) {
1371 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001372 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001373 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001374
Randy Dunlap465aac62012-11-30 10:01:51 +00001375 if (of_allnodes == np)
1376 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001377 else {
1378 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001379 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001380 prev->allnext != np;
1381 prev = prev->allnext)
1382 ;
1383 prev->allnext = np->allnext;
1384 }
1385
1386 if (parent->child == np)
1387 parent->child = np->sibling;
1388 else {
1389 struct device_node *prevsib;
1390 for (prevsib = np->parent->child;
1391 prevsib->sibling != np;
1392 prevsib = prevsib->sibling)
1393 ;
1394 prevsib->sibling = np->sibling;
1395 }
1396
1397 of_node_set_flag(np, OF_DETACHED);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001398 write_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001399
1400 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001401 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001402}
1403#endif /* defined(CONFIG_OF_DYNAMIC) */
1404
Shawn Guo611cad72011-08-15 15:28:14 +08001405static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1406 int id, const char *stem, int stem_len)
1407{
1408 ap->np = np;
1409 ap->id = id;
1410 strncpy(ap->stem, stem, stem_len);
1411 ap->stem[stem_len] = 0;
1412 list_add_tail(&ap->link, &aliases_lookup);
1413 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001414 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001415}
1416
1417/**
1418 * of_alias_scan - Scan all properties of 'aliases' node
1419 *
1420 * The function scans all the properties of 'aliases' node and populate
1421 * the the global lookup table with the properties. It returns the
1422 * number of alias_prop found, or error code in error case.
1423 *
1424 * @dt_alloc: An allocator that provides a virtual address to memory
1425 * for the resulting tree
1426 */
1427void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1428{
1429 struct property *pp;
1430
1431 of_chosen = of_find_node_by_path("/chosen");
1432 if (of_chosen == NULL)
1433 of_chosen = of_find_node_by_path("/chosen@0");
1434 of_aliases = of_find_node_by_path("/aliases");
1435 if (!of_aliases)
1436 return;
1437
Dong Aisheng8af0da92011-12-22 20:19:24 +08001438 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001439 const char *start = pp->name;
1440 const char *end = start + strlen(start);
1441 struct device_node *np;
1442 struct alias_prop *ap;
1443 int id, len;
1444
1445 /* Skip those we do not want to proceed */
1446 if (!strcmp(pp->name, "name") ||
1447 !strcmp(pp->name, "phandle") ||
1448 !strcmp(pp->name, "linux,phandle"))
1449 continue;
1450
1451 np = of_find_node_by_path(pp->value);
1452 if (!np)
1453 continue;
1454
1455 /* walk the alias backwards to extract the id and work out
1456 * the 'stem' string */
1457 while (isdigit(*(end-1)) && end > start)
1458 end--;
1459 len = end - start;
1460
1461 if (kstrtoint(end, 10, &id) < 0)
1462 continue;
1463
1464 /* Allocate an alias_prop with enough space for the stem */
1465 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1466 if (!ap)
1467 continue;
1468 ap->alias = start;
1469 of_alias_add(ap, np, id, start, len);
1470 }
1471}
1472
1473/**
1474 * of_alias_get_id - Get alias id for the given device_node
1475 * @np: Pointer to the given device_node
1476 * @stem: Alias stem of the given device_node
1477 *
1478 * The function travels the lookup table to get alias id for the given
1479 * device_node and alias stem. It returns the alias id if find it.
1480 */
1481int of_alias_get_id(struct device_node *np, const char *stem)
1482{
1483 struct alias_prop *app;
1484 int id = -ENODEV;
1485
1486 mutex_lock(&of_aliases_mutex);
1487 list_for_each_entry(app, &aliases_lookup, link) {
1488 if (strcmp(app->stem, stem) != 0)
1489 continue;
1490
1491 if (np == app->np) {
1492 id = app->id;
1493 break;
1494 }
1495 }
1496 mutex_unlock(&of_aliases_mutex);
1497
1498 return id;
1499}
1500EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001501
1502const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1503 u32 *pu)
1504{
1505 const void *curv = cur;
1506
1507 if (!prop)
1508 return NULL;
1509
1510 if (!cur) {
1511 curv = prop->value;
1512 goto out_val;
1513 }
1514
1515 curv += sizeof(*cur);
1516 if (curv >= prop->value + prop->length)
1517 return NULL;
1518
1519out_val:
1520 *pu = be32_to_cpup(curv);
1521 return curv;
1522}
1523EXPORT_SYMBOL_GPL(of_prop_next_u32);
1524
1525const char *of_prop_next_string(struct property *prop, const char *cur)
1526{
1527 const void *curv = cur;
1528
1529 if (!prop)
1530 return NULL;
1531
1532 if (!cur)
1533 return prop->value;
1534
1535 curv += strlen(cur) + 1;
1536 if (curv >= prop->value + prop->length)
1537 return NULL;
1538
1539 return curv;
1540}
1541EXPORT_SYMBOL_GPL(of_prop_next_string);