blob: 321d3ef050065e12b2d639e02fada00ec6b9e658 [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 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050041DEFINE_RAW_SPINLOCK(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
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500150static struct property *__of_find_property(const struct device_node *np,
151 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000152{
153 struct property *pp;
154
Timur Tabi64e45662008-05-08 05:19:59 +1000155 if (!np)
156 return NULL;
157
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530158 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000159 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530160 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000161 *lenp = pp->length;
162 break;
163 }
164 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500165
166 return pp;
167}
168
169struct property *of_find_property(const struct device_node *np,
170 const char *name,
171 int *lenp)
172{
173 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500174 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500175
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500176 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500177 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500178 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000179
180 return pp;
181}
182EXPORT_SYMBOL(of_find_property);
183
Grant Likelye91edcf2009-10-15 10:58:09 -0600184/**
185 * of_find_all_nodes - Get next node in global list
186 * @prev: Previous node or NULL to start iteration
187 * of_node_put() will be called on it
188 *
189 * Returns a node pointer with refcount incremented, use
190 * of_node_put() on it when done.
191 */
192struct device_node *of_find_all_nodes(struct device_node *prev)
193{
194 struct device_node *np;
195
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500196 raw_spin_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000197 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600198 for (; np != NULL; np = np->allnext)
199 if (of_node_get(np))
200 break;
201 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500202 raw_spin_unlock(&devtree_lock);
Grant Likelye91edcf2009-10-15 10:58:09 -0600203 return np;
204}
205EXPORT_SYMBOL(of_find_all_nodes);
206
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000207/*
208 * Find a property with a given name for a given node
209 * and return the value.
210 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500211static const void *__of_get_property(const struct device_node *np,
212 const char *name, int *lenp)
213{
214 struct property *pp = __of_find_property(np, name, lenp);
215
216 return pp ? pp->value : NULL;
217}
218
219/*
220 * Find a property with a given name for a given node
221 * and return the value.
222 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000223const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500224 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000225{
226 struct property *pp = of_find_property(np, name, lenp);
227
228 return pp ? pp->value : NULL;
229}
230EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000231
232/** Checks if the given "compat" string matches one of the strings in
233 * the device's "compatible" property
234 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500235static int __of_device_is_compatible(const struct device_node *device,
236 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000237{
238 const char* cp;
239 int cplen, l;
240
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500241 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000242 if (cp == NULL)
243 return 0;
244 while (cplen > 0) {
245 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
246 return 1;
247 l = strlen(cp) + 1;
248 cp += l;
249 cplen -= l;
250 }
251
252 return 0;
253}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500254
255/** Checks if the given "compat" string matches one of the strings in
256 * the device's "compatible" property
257 */
258int of_device_is_compatible(const struct device_node *device,
259 const char *compat)
260{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500261 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500262 int res;
263
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500264 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500265 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500266 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500267 return res;
268}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000269EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000270
271/**
Grant Likely71a157e2010-02-01 21:34:14 -0700272 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700273 * @compat: compatible string to look for in root node's compatible property.
274 *
275 * Returns true if the root node has the given value in its
276 * compatible property.
277 */
Grant Likely71a157e2010-02-01 21:34:14 -0700278int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700279{
280 struct device_node *root;
281 int rc = 0;
282
283 root = of_find_node_by_path("/");
284 if (root) {
285 rc = of_device_is_compatible(root, compat);
286 of_node_put(root);
287 }
288 return rc;
289}
Grant Likely71a157e2010-02-01 21:34:14 -0700290EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700291
292/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700293 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100294 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700295 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100296 *
297 * Returns 1 if the status property is absent or set to "okay" or "ok",
298 * 0 otherwise
299 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700300static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100301{
302 const char *status;
303 int statlen;
304
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700305 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100306 if (status == NULL)
307 return 1;
308
309 if (statlen > 0) {
310 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
311 return 1;
312 }
313
314 return 0;
315}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700316
317/**
318 * of_device_is_available - check if a device is available for use
319 *
320 * @device: Node to check for availability
321 *
322 * Returns 1 if the status property is absent or set to "okay" or "ok",
323 * 0 otherwise
324 */
325int of_device_is_available(const struct device_node *device)
326{
327 unsigned long flags;
328 int res;
329
330 raw_spin_lock_irqsave(&devtree_lock, flags);
331 res = __of_device_is_available(device);
332 raw_spin_unlock_irqrestore(&devtree_lock, flags);
333 return res;
334
335}
Josh Boyer834d97d2008-03-27 00:33:14 +1100336EXPORT_SYMBOL(of_device_is_available);
337
338/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000339 * of_get_parent - Get a node's parent if any
340 * @node: Node to get parent
341 *
342 * Returns a node pointer with refcount incremented, use
343 * of_node_put() on it when done.
344 */
345struct device_node *of_get_parent(const struct device_node *node)
346{
347 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500348 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000349
350 if (!node)
351 return NULL;
352
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500353 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000354 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500355 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000356 return np;
357}
358EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000359
360/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000361 * of_get_next_parent - Iterate to a node's parent
362 * @node: Node to get parent of
363 *
364 * This is like of_get_parent() except that it drops the
365 * refcount on the passed node, making it suitable for iterating
366 * through a node's parents.
367 *
368 * Returns a node pointer with refcount incremented, use
369 * of_node_put() on it when done.
370 */
371struct device_node *of_get_next_parent(struct device_node *node)
372{
373 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500374 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000375
376 if (!node)
377 return NULL;
378
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500379 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000380 parent = of_node_get(node->parent);
381 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500382 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000383 return parent;
384}
385
386/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000387 * of_get_next_child - Iterate a node childs
388 * @node: parent node
389 * @prev: previous child of the parent node, or NULL to get first
390 *
391 * Returns a node pointer with refcount incremented, use
392 * of_node_put() on it when done.
393 */
394struct device_node *of_get_next_child(const struct device_node *node,
395 struct device_node *prev)
396{
397 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500398 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000399
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500400 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000401 next = prev ? prev->sibling : node->child;
402 for (; next; next = next->sibling)
403 if (of_node_get(next))
404 break;
405 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500406 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000407 return next;
408}
409EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000410
411/**
Timur Tabi32961932012-08-14 13:20:23 +0000412 * of_get_next_available_child - Find the next available child node
413 * @node: parent node
414 * @prev: previous child of the parent node, or NULL to get first
415 *
416 * This function is like of_get_next_child(), except that it
417 * automatically skips any disabled nodes (i.e. status = "disabled").
418 */
419struct device_node *of_get_next_available_child(const struct device_node *node,
420 struct device_node *prev)
421{
422 struct device_node *next;
423
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500424 raw_spin_lock(&devtree_lock);
Timur Tabi32961932012-08-14 13:20:23 +0000425 next = prev ? prev->sibling : node->child;
426 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700427 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000428 continue;
429 if (of_node_get(next))
430 break;
431 }
432 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500433 raw_spin_unlock(&devtree_lock);
Timur Tabi32961932012-08-14 13:20:23 +0000434 return next;
435}
436EXPORT_SYMBOL(of_get_next_available_child);
437
438/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100439 * of_get_child_by_name - Find the child node by name for a given parent
440 * @node: parent node
441 * @name: child name to look for.
442 *
443 * This function looks for child node for given matching name
444 *
445 * Returns a node pointer if found, with refcount incremented, use
446 * of_node_put() on it when done.
447 * Returns NULL if node is not found.
448 */
449struct device_node *of_get_child_by_name(const struct device_node *node,
450 const char *name)
451{
452 struct device_node *child;
453
454 for_each_child_of_node(node, child)
455 if (child->name && (of_node_cmp(child->name, name) == 0))
456 break;
457 return child;
458}
459EXPORT_SYMBOL(of_get_child_by_name);
460
461/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000462 * of_find_node_by_path - Find a node matching a full OF path
463 * @path: The full path to match
464 *
465 * Returns a node pointer with refcount incremented, use
466 * of_node_put() on it when done.
467 */
468struct device_node *of_find_node_by_path(const char *path)
469{
Randy Dunlap465aac62012-11-30 10:01:51 +0000470 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500471 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000472
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500473 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000474 for (; np; np = np->allnext) {
475 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
476 && of_node_get(np))
477 break;
478 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500479 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000480 return np;
481}
482EXPORT_SYMBOL(of_find_node_by_path);
483
484/**
485 * of_find_node_by_name - Find a node by its "name" property
486 * @from: The node to start searching from or NULL, the node
487 * you pass will not be searched, only the next one
488 * will; typically, you pass what the previous call
489 * returned. of_node_put() will be called on it
490 * @name: The name string to match against
491 *
492 * Returns a node pointer with refcount incremented, use
493 * of_node_put() on it when done.
494 */
495struct device_node *of_find_node_by_name(struct device_node *from,
496 const char *name)
497{
498 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500499 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000500
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500501 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000502 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000503 for (; np; np = np->allnext)
504 if (np->name && (of_node_cmp(np->name, name) == 0)
505 && of_node_get(np))
506 break;
507 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500508 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000509 return np;
510}
511EXPORT_SYMBOL(of_find_node_by_name);
512
513/**
514 * of_find_node_by_type - Find a node by its "device_type" property
515 * @from: The node to start searching from, or NULL to start searching
516 * the entire device tree. The node you pass will not be
517 * searched, only the next one will; typically, you pass
518 * what the previous call returned. of_node_put() will be
519 * called on from for you.
520 * @type: The type string to match against
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_by_type(struct device_node *from,
526 const char *type)
527{
528 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500529 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000530
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500531 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000532 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000533 for (; np; np = np->allnext)
534 if (np->type && (of_node_cmp(np->type, type) == 0)
535 && of_node_get(np))
536 break;
537 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500538 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000539 return np;
540}
541EXPORT_SYMBOL(of_find_node_by_type);
542
543/**
544 * of_find_compatible_node - Find a node based on type and one of the
545 * tokens in its "compatible" property
546 * @from: The node to start searching from or NULL, the node
547 * you pass will not be searched, only the next one
548 * will; typically, you pass what the previous call
549 * returned. of_node_put() will be called on it
550 * @type: The type string to match "device_type" or NULL to ignore
551 * @compatible: The string to match to one of the tokens in the device
552 * "compatible" list.
553 *
554 * Returns a node pointer with refcount incremented, use
555 * of_node_put() on it when done.
556 */
557struct device_node *of_find_compatible_node(struct device_node *from,
558 const char *type, const char *compatible)
559{
560 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500561 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000562
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500563 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000564 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000565 for (; np; np = np->allnext) {
566 if (type
567 && !(np->type && (of_node_cmp(np->type, type) == 0)))
568 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500569 if (__of_device_is_compatible(np, compatible) &&
570 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000571 break;
572 }
573 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500574 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000575 return np;
576}
577EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100578
579/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000580 * of_find_node_with_property - Find a node which has a property with
581 * the given name.
582 * @from: The node to start searching from or NULL, the node
583 * you pass will not be searched, only the next one
584 * will; typically, you pass what the previous call
585 * returned. of_node_put() will be called on it
586 * @prop_name: The name of the property to look for.
587 *
588 * Returns a node pointer with refcount incremented, use
589 * of_node_put() on it when done.
590 */
591struct device_node *of_find_node_with_property(struct device_node *from,
592 const char *prop_name)
593{
594 struct device_node *np;
595 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500596 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000597
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500598 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000599 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000600 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530601 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000602 if (of_prop_cmp(pp->name, prop_name) == 0) {
603 of_node_get(np);
604 goto out;
605 }
606 }
607 }
608out:
609 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500610 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000611 return np;
612}
613EXPORT_SYMBOL(of_find_node_with_property);
614
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500615static
616const struct of_device_id *__of_match_node(const struct of_device_id *matches,
617 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100618{
Grant Likelya52f07e2011-03-18 10:21:29 -0600619 if (!matches)
620 return NULL;
621
Grant Likely283029d2008-01-09 06:20:40 +1100622 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
623 int match = 1;
624 if (matches->name[0])
625 match &= node->name
626 && !strcmp(matches->name, node->name);
627 if (matches->type[0])
628 match &= node->type
629 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700630 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500631 match &= __of_device_is_compatible(node,
632 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700633 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100634 return matches;
635 matches++;
636 }
637 return NULL;
638}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500639
640/**
641 * of_match_node - Tell if an device_node has a matching of_match structure
642 * @matches: array of of device match structures to search in
643 * @node: the of device structure to match against
644 *
645 * Low level utility function used by device matching.
646 */
647const struct of_device_id *of_match_node(const struct of_device_id *matches,
648 const struct device_node *node)
649{
650 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500651 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500652
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500653 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500654 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500655 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500656 return match;
657}
Grant Likely283029d2008-01-09 06:20:40 +1100658EXPORT_SYMBOL(of_match_node);
659
660/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700661 * of_find_matching_node_and_match - Find a node based on an of_device_id
662 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100663 * @from: The node to start searching from or NULL, the node
664 * you pass will not be searched, only the next one
665 * will; typically, you pass what the previous call
666 * returned. of_node_put() will be called on it
667 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700668 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100669 *
670 * Returns a node pointer with refcount incremented, use
671 * of_node_put() on it when done.
672 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700673struct device_node *of_find_matching_node_and_match(struct device_node *from,
674 const struct of_device_id *matches,
675 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100676{
677 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800678 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500679 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100680
Stephen Warren50c8af42012-11-20 16:12:20 -0700681 if (match)
682 *match = NULL;
683
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500684 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000685 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100686 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500687 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800688 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700689 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800690 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100691 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700692 }
Grant Likely283029d2008-01-09 06:20:40 +1100693 }
694 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500695 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100696 return np;
697}
Grant Likely80c20222012-12-19 10:45:36 +0000698EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400699
700/**
Grant Likely3f07af42008-07-25 22:25:13 -0400701 * of_modalias_node - Lookup appropriate modalias for a device node
702 * @node: pointer to a device tree node
703 * @modalias: Pointer to buffer that modalias value will be copied into
704 * @len: Length of modalias value
705 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600706 * Based on the value of the compatible property, this routine will attempt
707 * to choose an appropriate modalias value for a particular device tree node.
708 * It does this by stripping the manufacturer prefix (as delimited by a ',')
709 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400710 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600711 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400712 */
713int of_modalias_node(struct device_node *node, char *modalias, int len)
714{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600715 const char *compatible, *p;
716 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400717
718 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600719 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400720 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400721 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600722 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400723 return 0;
724}
725EXPORT_SYMBOL_GPL(of_modalias_node);
726
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000727/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700728 * of_find_node_by_phandle - Find a node given a phandle
729 * @handle: phandle of the node to find
730 *
731 * Returns a node pointer with refcount incremented, use
732 * of_node_put() on it when done.
733 */
734struct device_node *of_find_node_by_phandle(phandle handle)
735{
736 struct device_node *np;
737
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500738 raw_spin_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000739 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700740 if (np->phandle == handle)
741 break;
742 of_node_get(np);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500743 raw_spin_unlock(&devtree_lock);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700744 return np;
745}
746EXPORT_SYMBOL(of_find_node_by_phandle);
747
748/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530749 * of_property_read_u8_array - Find and read an array of u8 from a property.
750 *
751 * @np: device node from which the property value is to be read.
752 * @propname: name of the property to be searched.
753 * @out_value: pointer to return value, modified only if return value is 0.
754 * @sz: number of array elements to read
755 *
756 * Search for a property in a device node and read 8-bit value(s) from
757 * it. Returns 0 on success, -EINVAL if the property does not exist,
758 * -ENODATA if property does not have a value, and -EOVERFLOW if the
759 * property data isn't large enough.
760 *
761 * dts entry of array should be like:
762 * property = /bits/ 8 <0x50 0x60 0x70>;
763 *
764 * The out_value is modified only if a valid u8 value can be decoded.
765 */
766int of_property_read_u8_array(const struct device_node *np,
767 const char *propname, u8 *out_values, size_t sz)
768{
769 struct property *prop = of_find_property(np, propname, NULL);
770 const u8 *val;
771
772 if (!prop)
773 return -EINVAL;
774 if (!prop->value)
775 return -ENODATA;
776 if ((sz * sizeof(*out_values)) > prop->length)
777 return -EOVERFLOW;
778
779 val = prop->value;
780 while (sz--)
781 *out_values++ = *val++;
782 return 0;
783}
784EXPORT_SYMBOL_GPL(of_property_read_u8_array);
785
786/**
787 * of_property_read_u16_array - Find and read an array of u16 from a property.
788 *
789 * @np: device node from which the property value is to be read.
790 * @propname: name of the property to be searched.
791 * @out_value: pointer to return value, modified only if return value is 0.
792 * @sz: number of array elements to read
793 *
794 * Search for a property in a device node and read 16-bit value(s) from
795 * it. Returns 0 on success, -EINVAL if the property does not exist,
796 * -ENODATA if property does not have a value, and -EOVERFLOW if the
797 * property data isn't large enough.
798 *
799 * dts entry of array should be like:
800 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
801 *
802 * The out_value is modified only if a valid u16 value can be decoded.
803 */
804int of_property_read_u16_array(const struct device_node *np,
805 const char *propname, u16 *out_values, size_t sz)
806{
807 struct property *prop = of_find_property(np, propname, NULL);
808 const __be16 *val;
809
810 if (!prop)
811 return -EINVAL;
812 if (!prop->value)
813 return -ENODATA;
814 if ((sz * sizeof(*out_values)) > prop->length)
815 return -EOVERFLOW;
816
817 val = prop->value;
818 while (sz--)
819 *out_values++ = be16_to_cpup(val++);
820 return 0;
821}
822EXPORT_SYMBOL_GPL(of_property_read_u16_array);
823
824/**
Rob Herring0e373632011-07-06 15:42:58 -0500825 * of_property_read_u32_array - Find and read an array of 32 bit integers
826 * from a property.
827 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530828 * @np: device node from which the property value is to be read.
829 * @propname: name of the property to be searched.
830 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530831 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530832 *
Rob Herring0e373632011-07-06 15:42:58 -0500833 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530834 * it. Returns 0 on success, -EINVAL if the property does not exist,
835 * -ENODATA if property does not have a value, and -EOVERFLOW if the
836 * property data isn't large enough.
837 *
838 * The out_value is modified only if a valid u32 value can be decoded.
839 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100840int of_property_read_u32_array(const struct device_node *np,
841 const char *propname, u32 *out_values,
842 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530843{
844 struct property *prop = of_find_property(np, propname, NULL);
Rob Herring0e373632011-07-06 15:42:58 -0500845 const __be32 *val;
Thomas Abrahama3b85362011-06-30 21:26:10 +0530846
847 if (!prop)
848 return -EINVAL;
849 if (!prop->value)
850 return -ENODATA;
Rob Herring0e373632011-07-06 15:42:58 -0500851 if ((sz * sizeof(*out_values)) > prop->length)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530852 return -EOVERFLOW;
Rob Herring0e373632011-07-06 15:42:58 -0500853
854 val = prop->value;
855 while (sz--)
856 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530857 return 0;
858}
Rob Herring0e373632011-07-06 15:42:58 -0500859EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530860
861/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100862 * of_property_read_u64 - Find and read a 64 bit integer from a property
863 * @np: device node from which the property value is to be read.
864 * @propname: name of the property to be searched.
865 * @out_value: pointer to return value, modified only if return value is 0.
866 *
867 * Search for a property in a device node and read a 64-bit value from
868 * it. Returns 0 on success, -EINVAL if the property does not exist,
869 * -ENODATA if property does not have a value, and -EOVERFLOW if the
870 * property data isn't large enough.
871 *
872 * The out_value is modified only if a valid u64 value can be decoded.
873 */
874int of_property_read_u64(const struct device_node *np, const char *propname,
875 u64 *out_value)
876{
877 struct property *prop = of_find_property(np, propname, NULL);
878
879 if (!prop)
880 return -EINVAL;
881 if (!prop->value)
882 return -ENODATA;
883 if (sizeof(*out_value) > prop->length)
884 return -EOVERFLOW;
885 *out_value = of_read_number(prop->value, 2);
886 return 0;
887}
888EXPORT_SYMBOL_GPL(of_property_read_u64);
889
890/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530891 * of_property_read_string - Find and read a string from a property
892 * @np: device node from which the property value is to be read.
893 * @propname: name of the property to be searched.
894 * @out_string: pointer to null terminated return string, modified only if
895 * return value is 0.
896 *
897 * Search for a property in a device tree node and retrieve a null
898 * terminated string value (pointer to data, not a copy). Returns 0 on
899 * success, -EINVAL if the property does not exist, -ENODATA if property
900 * does not have a value, and -EILSEQ if the string is not null-terminated
901 * within the length of the property data.
902 *
903 * The out_string pointer is modified only if a valid string can be decoded.
904 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100905int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800906 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530907{
908 struct property *prop = of_find_property(np, propname, NULL);
909 if (!prop)
910 return -EINVAL;
911 if (!prop->value)
912 return -ENODATA;
913 if (strnlen(prop->value, prop->length) >= prop->length)
914 return -EILSEQ;
915 *out_string = prop->value;
916 return 0;
917}
918EXPORT_SYMBOL_GPL(of_property_read_string);
919
920/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200921 * of_property_read_string_index - Find and read a string from a multiple
922 * 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 * @index: index of the string in the list of strings
926 * @out_string: pointer to null terminated return string, modified only if
927 * return value is 0.
928 *
929 * Search for a property in a device tree node and retrieve a null
930 * terminated string value (pointer to data, not a copy) in the list of strings
931 * contained in that property.
932 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
933 * property does not have a value, and -EILSEQ if the string is not
934 * null-terminated within the length of the property data.
935 *
936 * The out_string pointer is modified only if a valid string can be decoded.
937 */
938int of_property_read_string_index(struct device_node *np, const char *propname,
939 int index, const char **output)
940{
941 struct property *prop = of_find_property(np, propname, NULL);
942 int i = 0;
943 size_t l = 0, total = 0;
944 const char *p;
945
946 if (!prop)
947 return -EINVAL;
948 if (!prop->value)
949 return -ENODATA;
950 if (strnlen(prop->value, prop->length) >= prop->length)
951 return -EILSEQ;
952
953 p = prop->value;
954
955 for (i = 0; total < prop->length; total += l, p += l) {
956 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100957 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200958 *output = p;
959 return 0;
960 }
961 }
962 return -ENODATA;
963}
964EXPORT_SYMBOL_GPL(of_property_read_string_index);
965
Grant Likely7aff0fe2011-12-12 09:25:58 -0700966/**
967 * of_property_match_string() - Find string in a list and return index
968 * @np: pointer to node containing string list property
969 * @propname: string list property name
970 * @string: pointer to string to search for in string list
971 *
972 * This function searches a string list property and returns the index
973 * of a specific string value.
974 */
975int of_property_match_string(struct device_node *np, const char *propname,
976 const char *string)
977{
978 struct property *prop = of_find_property(np, propname, NULL);
979 size_t l;
980 int i;
981 const char *p, *end;
982
983 if (!prop)
984 return -EINVAL;
985 if (!prop->value)
986 return -ENODATA;
987
988 p = prop->value;
989 end = p + prop->length;
990
991 for (i = 0; p < end; i++, p += l) {
992 l = strlen(p) + 1;
993 if (p + l > end)
994 return -EILSEQ;
995 pr_debug("comparing %s with %s\n", string, p);
996 if (strcmp(string, p) == 0)
997 return i; /* Found it; return index */
998 }
999 return -ENODATA;
1000}
1001EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001002
1003/**
1004 * of_property_count_strings - Find and return the number of strings from a
1005 * multiple strings property.
1006 * @np: device node from which the property value is to be read.
1007 * @propname: name of the property to be searched.
1008 *
1009 * Search for a property in a device tree node and retrieve the number of null
1010 * terminated string contain in it. Returns the number of strings on
1011 * success, -EINVAL if the property does not exist, -ENODATA if property
1012 * does not have a value, and -EILSEQ if the string is not null-terminated
1013 * within the length of the property data.
1014 */
1015int of_property_count_strings(struct device_node *np, const char *propname)
1016{
1017 struct property *prop = of_find_property(np, propname, NULL);
1018 int i = 0;
1019 size_t l = 0, total = 0;
1020 const char *p;
1021
1022 if (!prop)
1023 return -EINVAL;
1024 if (!prop->value)
1025 return -ENODATA;
1026 if (strnlen(prop->value, prop->length) >= prop->length)
1027 return -EILSEQ;
1028
1029 p = prop->value;
1030
Benoit Cousson88af7f52011-12-05 15:23:54 +01001031 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001032 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001033
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001034 return i;
1035}
1036EXPORT_SYMBOL_GPL(of_property_count_strings);
1037
1038/**
Grant Likely739649c2009-04-25 12:52:40 +00001039 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1040 * @np: Pointer to device node holding phandle property
1041 * @phandle_name: Name of property holding a phandle value
1042 * @index: For properties holding a table of phandles, this is the index into
1043 * the table
1044 *
1045 * Returns the device_node pointer with refcount incremented. Use
1046 * of_node_put() on it when done.
1047 */
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +01001048struct device_node *of_parse_phandle(const struct device_node *np,
1049 const char *phandle_name, int index)
Grant Likely739649c2009-04-25 12:52:40 +00001050{
Grant Likely9a6b2e52010-07-23 01:48:25 -06001051 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +00001052 int size;
1053
1054 phandle = of_get_property(np, phandle_name, &size);
1055 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1056 return NULL;
1057
Grant Likely9a6b2e52010-07-23 01:48:25 -06001058 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +00001059}
1060EXPORT_SYMBOL(of_parse_phandle);
1061
1062/**
Grant Likely15c9a0a2011-12-12 09:25:57 -07001063 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001064 * @np: pointer to a device tree node containing a list
1065 * @list_name: property name that contains a list
1066 * @cells_name: property name that specifies phandles' arguments count
1067 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -07001068 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001069 *
1070 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001071 * Returns 0 on success and fills out_args, on error returns appropriate
1072 * errno value.
1073 *
1074 * Caller is responsible to call of_node_put() on the returned out_args->node
1075 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001076 *
1077 * Example:
1078 *
1079 * phandle1: node1 {
1080 * #list-cells = <2>;
1081 * }
1082 *
1083 * phandle2: node2 {
1084 * #list-cells = <1>;
1085 * }
1086 *
1087 * node3 {
1088 * list = <&phandle1 1 2 &phandle2 3>;
1089 * }
1090 *
1091 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001092 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001093 */
Grant Likelybd69f732013-02-10 22:57:21 +00001094static int __of_parse_phandle_with_args(const struct device_node *np,
1095 const char *list_name,
1096 const char *cells_name, int index,
1097 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001098{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001099 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001100 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001101 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001102 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001103 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001104
Grant Likely15c9a0a2011-12-12 09:25:57 -07001105 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001106 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001107 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001108 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001109 list_end = list + size / sizeof(*list);
1110
Grant Likely15c9a0a2011-12-12 09:25:57 -07001111 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001112 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001113 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001114 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001115
Grant Likely15c9a0a2011-12-12 09:25:57 -07001116 /*
1117 * If phandle is 0, then it is an empty entry with no
1118 * arguments. Skip forward to the next entry.
1119 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001120 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001121 if (phandle) {
1122 /*
1123 * Find the provider node and parse the #*-cells
1124 * property to determine the argument length
1125 */
1126 node = of_find_node_by_phandle(phandle);
1127 if (!node) {
1128 pr_err("%s: could not find phandle\n",
1129 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001130 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001131 }
1132 if (of_property_read_u32(node, cells_name, &count)) {
1133 pr_err("%s: could not get %s for %s\n",
1134 np->full_name, cells_name,
1135 node->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001136 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001137 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001138
Grant Likely15c9a0a2011-12-12 09:25:57 -07001139 /*
1140 * Make sure that the arguments actually fit in the
1141 * remaining property data length
1142 */
1143 if (list + count > list_end) {
1144 pr_err("%s: arguments longer than property\n",
1145 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001146 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001147 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001148 }
1149
Grant Likely15c9a0a2011-12-12 09:25:57 -07001150 /*
1151 * All of the error cases above bail out of the loop, so at
1152 * this point, the parsing is successful. If the requested
1153 * index matches, then fill the out_args structure and return,
1154 * or return -ENOENT for an empty entry.
1155 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001156 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001157 if (cur_index == index) {
1158 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001159 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001160
Grant Likely15c9a0a2011-12-12 09:25:57 -07001161 if (out_args) {
1162 int i;
1163 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1164 count = MAX_PHANDLE_ARGS;
1165 out_args->np = node;
1166 out_args->args_count = count;
1167 for (i = 0; i < count; i++)
1168 out_args->args[i] = be32_to_cpup(list++);
1169 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001170
1171 /* Found it! return success */
1172 if (node)
1173 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001174 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001175 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001176
1177 of_node_put(node);
1178 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001179 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001180 cur_index++;
1181 }
1182
Grant Likely23ce04c2013-02-12 21:21:49 +00001183 /*
1184 * Unlock node before returning result; will be one of:
1185 * -ENOENT : index is for empty phandle
1186 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001187 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001188 */
Grant Likelybd69f732013-02-10 22:57:21 +00001189 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001190 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001191 if (node)
1192 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001193 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001194}
Grant Likelybd69f732013-02-10 22:57:21 +00001195
1196int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1197 const char *cells_name, int index,
1198 struct of_phandle_args *out_args)
1199{
1200 if (index < 0)
1201 return -EINVAL;
1202 return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
1203}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001204EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001205
Grant Likelybd69f732013-02-10 22:57:21 +00001206/**
1207 * of_count_phandle_with_args() - Find the number of phandles references in a property
1208 * @np: pointer to a device tree node containing a list
1209 * @list_name: property name that contains a list
1210 * @cells_name: property name that specifies phandles' arguments count
1211 *
1212 * Returns the number of phandle + argument tuples within a property. It
1213 * is a typical pattern to encode a list of phandle and variable
1214 * arguments into a single property. The number of arguments is encoded
1215 * by a property in the phandle-target node. For example, a gpios
1216 * property would contain a list of GPIO specifies consisting of a
1217 * phandle and 1 or more arguments. The number of arguments are
1218 * determined by the #gpio-cells property in the node pointed to by the
1219 * phandle.
1220 */
1221int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1222 const char *cells_name)
1223{
1224 return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
1225}
1226EXPORT_SYMBOL(of_count_phandle_with_args);
1227
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001228#if defined(CONFIG_OF_DYNAMIC)
1229static int of_property_notify(int action, struct device_node *np,
1230 struct property *prop)
1231{
1232 struct of_prop_reconfig pr;
1233
1234 pr.dn = np;
1235 pr.prop = prop;
1236 return of_reconfig_notify(action, &pr);
1237}
1238#else
1239static int of_property_notify(int action, struct device_node *np,
1240 struct property *prop)
1241{
1242 return 0;
1243}
1244#endif
1245
Grant Likely02af11b2009-11-23 20:16:45 -07001246/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001247 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001248 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001249int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001250{
1251 struct property **next;
1252 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001253 int rc;
1254
1255 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1256 if (rc)
1257 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001258
1259 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001260 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001261 next = &np->properties;
1262 while (*next) {
1263 if (strcmp(prop->name, (*next)->name) == 0) {
1264 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001265 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001266 return -1;
1267 }
1268 next = &(*next)->next;
1269 }
1270 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001271 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001272
1273#ifdef CONFIG_PROC_DEVICETREE
1274 /* try to add to proc as well if it was initialized */
1275 if (np->pde)
1276 proc_device_tree_add_prop(np->pde, prop);
1277#endif /* CONFIG_PROC_DEVICETREE */
1278
1279 return 0;
1280}
1281
1282/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001283 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001284 *
1285 * Note that we don't actually remove it, since we have given out
1286 * who-knows-how-many pointers to the data using get-property.
1287 * Instead we just move the property to the "dead properties"
1288 * list, so it won't be found any more.
1289 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001290int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001291{
1292 struct property **next;
1293 unsigned long flags;
1294 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001295 int rc;
1296
1297 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1298 if (rc)
1299 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001300
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001301 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001302 next = &np->properties;
1303 while (*next) {
1304 if (*next == prop) {
1305 /* found the node */
1306 *next = prop->next;
1307 prop->next = np->deadprops;
1308 np->deadprops = prop;
1309 found = 1;
1310 break;
1311 }
1312 next = &(*next)->next;
1313 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001314 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001315
1316 if (!found)
1317 return -ENODEV;
1318
1319#ifdef CONFIG_PROC_DEVICETREE
1320 /* try to remove the proc node as well */
1321 if (np->pde)
1322 proc_device_tree_remove_prop(np->pde, prop);
1323#endif /* CONFIG_PROC_DEVICETREE */
1324
1325 return 0;
1326}
1327
1328/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001329 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001330 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001331 *
1332 * Note that we don't actually remove it, since we have given out
1333 * who-knows-how-many pointers to the data using get-property.
1334 * Instead we just move the property to the "dead properties" list,
1335 * and add the new property to the property list
1336 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001337int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001338{
Dong Aisheng475d0092012-07-11 15:16:37 +10001339 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001340 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001341 int rc, found = 0;
1342
1343 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1344 if (rc)
1345 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001346
Dong Aisheng475d0092012-07-11 15:16:37 +10001347 if (!newprop->name)
1348 return -EINVAL;
1349
1350 oldprop = of_find_property(np, newprop->name, NULL);
1351 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001352 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001353
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001354 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001355 next = &np->properties;
1356 while (*next) {
1357 if (*next == oldprop) {
1358 /* found the node */
1359 newprop->next = oldprop->next;
1360 *next = newprop;
1361 oldprop->next = np->deadprops;
1362 np->deadprops = oldprop;
1363 found = 1;
1364 break;
1365 }
1366 next = &(*next)->next;
1367 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001368 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001369
1370 if (!found)
1371 return -ENODEV;
1372
1373#ifdef CONFIG_PROC_DEVICETREE
1374 /* try to add to proc as well if it was initialized */
1375 if (np->pde)
1376 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1377#endif /* CONFIG_PROC_DEVICETREE */
1378
1379 return 0;
1380}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001381
1382#if defined(CONFIG_OF_DYNAMIC)
1383/*
1384 * Support for dynamic device trees.
1385 *
1386 * On some platforms, the device tree can be manipulated at runtime.
1387 * The routines in this section support adding, removing and changing
1388 * device tree nodes.
1389 */
1390
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001391static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1392
1393int of_reconfig_notifier_register(struct notifier_block *nb)
1394{
1395 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1396}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001397EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001398
1399int of_reconfig_notifier_unregister(struct notifier_block *nb)
1400{
1401 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1402}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001403EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001404
1405int of_reconfig_notify(unsigned long action, void *p)
1406{
1407 int rc;
1408
1409 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1410 return notifier_to_errno(rc);
1411}
1412
Nathan Fontenote81b3292012-10-02 16:55:01 +00001413#ifdef CONFIG_PROC_DEVICETREE
1414static void of_add_proc_dt_entry(struct device_node *dn)
1415{
1416 struct proc_dir_entry *ent;
1417
1418 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1419 if (ent)
1420 proc_device_tree_add_node(dn, ent);
1421}
1422#else
1423static void of_add_proc_dt_entry(struct device_node *dn)
1424{
1425 return;
1426}
1427#endif
1428
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001429/**
1430 * of_attach_node - Plug a device node into the tree and global list.
1431 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001432int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001433{
1434 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001435 int rc;
1436
1437 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1438 if (rc)
1439 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001440
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001441 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001442 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001443 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001444 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001445 of_allnodes = np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001446 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001447
1448 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001449 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001450}
1451
Nathan Fontenote81b3292012-10-02 16:55:01 +00001452#ifdef CONFIG_PROC_DEVICETREE
1453static void of_remove_proc_dt_entry(struct device_node *dn)
1454{
1455 struct device_node *parent = dn->parent;
1456 struct property *prop = dn->properties;
1457
1458 while (prop) {
1459 remove_proc_entry(prop->name, dn->pde);
1460 prop = prop->next;
1461 }
1462
1463 if (dn->pde)
1464 remove_proc_entry(dn->pde->name, parent->pde);
1465}
1466#else
1467static void of_remove_proc_dt_entry(struct device_node *dn)
1468{
1469 return;
1470}
1471#endif
1472
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001473/**
1474 * of_detach_node - "Unplug" a node from the device tree.
1475 *
1476 * The caller must hold a reference to the node. The memory associated with
1477 * the node is not freed until its refcount goes to zero.
1478 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001479int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001480{
1481 struct device_node *parent;
1482 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001483 int rc = 0;
1484
1485 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1486 if (rc)
1487 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001488
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001489 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001490
Nathan Fontenote81b3292012-10-02 16:55:01 +00001491 if (of_node_check_flag(np, OF_DETACHED)) {
1492 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001493 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001494 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001495 }
1496
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001497 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001498 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001499 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001500 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001501 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001502
Randy Dunlap465aac62012-11-30 10:01:51 +00001503 if (of_allnodes == np)
1504 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001505 else {
1506 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001507 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001508 prev->allnext != np;
1509 prev = prev->allnext)
1510 ;
1511 prev->allnext = np->allnext;
1512 }
1513
1514 if (parent->child == np)
1515 parent->child = np->sibling;
1516 else {
1517 struct device_node *prevsib;
1518 for (prevsib = np->parent->child;
1519 prevsib->sibling != np;
1520 prevsib = prevsib->sibling)
1521 ;
1522 prevsib->sibling = np->sibling;
1523 }
1524
1525 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001526 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001527
1528 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001529 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001530}
1531#endif /* defined(CONFIG_OF_DYNAMIC) */
1532
Shawn Guo611cad72011-08-15 15:28:14 +08001533static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1534 int id, const char *stem, int stem_len)
1535{
1536 ap->np = np;
1537 ap->id = id;
1538 strncpy(ap->stem, stem, stem_len);
1539 ap->stem[stem_len] = 0;
1540 list_add_tail(&ap->link, &aliases_lookup);
1541 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001542 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001543}
1544
1545/**
1546 * of_alias_scan - Scan all properties of 'aliases' node
1547 *
1548 * The function scans all the properties of 'aliases' node and populate
1549 * the the global lookup table with the properties. It returns the
1550 * number of alias_prop found, or error code in error case.
1551 *
1552 * @dt_alloc: An allocator that provides a virtual address to memory
1553 * for the resulting tree
1554 */
1555void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1556{
1557 struct property *pp;
1558
1559 of_chosen = of_find_node_by_path("/chosen");
1560 if (of_chosen == NULL)
1561 of_chosen = of_find_node_by_path("/chosen@0");
1562 of_aliases = of_find_node_by_path("/aliases");
1563 if (!of_aliases)
1564 return;
1565
Dong Aisheng8af0da92011-12-22 20:19:24 +08001566 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001567 const char *start = pp->name;
1568 const char *end = start + strlen(start);
1569 struct device_node *np;
1570 struct alias_prop *ap;
1571 int id, len;
1572
1573 /* Skip those we do not want to proceed */
1574 if (!strcmp(pp->name, "name") ||
1575 !strcmp(pp->name, "phandle") ||
1576 !strcmp(pp->name, "linux,phandle"))
1577 continue;
1578
1579 np = of_find_node_by_path(pp->value);
1580 if (!np)
1581 continue;
1582
1583 /* walk the alias backwards to extract the id and work out
1584 * the 'stem' string */
1585 while (isdigit(*(end-1)) && end > start)
1586 end--;
1587 len = end - start;
1588
1589 if (kstrtoint(end, 10, &id) < 0)
1590 continue;
1591
1592 /* Allocate an alias_prop with enough space for the stem */
1593 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1594 if (!ap)
1595 continue;
1596 ap->alias = start;
1597 of_alias_add(ap, np, id, start, len);
1598 }
1599}
1600
1601/**
1602 * of_alias_get_id - Get alias id for the given device_node
1603 * @np: Pointer to the given device_node
1604 * @stem: Alias stem of the given device_node
1605 *
1606 * The function travels the lookup table to get alias id for the given
1607 * device_node and alias stem. It returns the alias id if find it.
1608 */
1609int of_alias_get_id(struct device_node *np, const char *stem)
1610{
1611 struct alias_prop *app;
1612 int id = -ENODEV;
1613
1614 mutex_lock(&of_aliases_mutex);
1615 list_for_each_entry(app, &aliases_lookup, link) {
1616 if (strcmp(app->stem, stem) != 0)
1617 continue;
1618
1619 if (np == app->np) {
1620 id = app->id;
1621 break;
1622 }
1623 }
1624 mutex_unlock(&of_aliases_mutex);
1625
1626 return id;
1627}
1628EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001629
1630const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1631 u32 *pu)
1632{
1633 const void *curv = cur;
1634
1635 if (!prop)
1636 return NULL;
1637
1638 if (!cur) {
1639 curv = prop->value;
1640 goto out_val;
1641 }
1642
1643 curv += sizeof(*cur);
1644 if (curv >= prop->value + prop->length)
1645 return NULL;
1646
1647out_val:
1648 *pu = be32_to_cpup(curv);
1649 return curv;
1650}
1651EXPORT_SYMBOL_GPL(of_prop_next_u32);
1652
1653const char *of_prop_next_string(struct property *prop, const char *cur)
1654{
1655 const void *curv = cur;
1656
1657 if (!prop)
1658 return NULL;
1659
1660 if (!cur)
1661 return prop->value;
1662
1663 curv += strlen(cur) + 1;
1664 if (curv >= prop->value + prop->length)
1665 return NULL;
1666
1667 return curv;
1668}
1669EXPORT_SYMBOL_GPL(of_prop_next_string);