blob: 1733081eb87310172eddc17b9e691d385ff95926 [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}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300385EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000386
387/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000388 * of_get_next_child - Iterate a node childs
389 * @node: parent node
390 * @prev: previous child of the parent node, or NULL to get first
391 *
392 * Returns a node pointer with refcount incremented, use
393 * of_node_put() on it when done.
394 */
395struct device_node *of_get_next_child(const struct device_node *node,
396 struct device_node *prev)
397{
398 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500399 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000400
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500401 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000402 next = prev ? prev->sibling : node->child;
403 for (; next; next = next->sibling)
404 if (of_node_get(next))
405 break;
406 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500407 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000408 return next;
409}
410EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000411
412/**
Timur Tabi32961932012-08-14 13:20:23 +0000413 * of_get_next_available_child - Find the next available child node
414 * @node: parent node
415 * @prev: previous child of the parent node, or NULL to get first
416 *
417 * This function is like of_get_next_child(), except that it
418 * automatically skips any disabled nodes (i.e. status = "disabled").
419 */
420struct device_node *of_get_next_available_child(const struct device_node *node,
421 struct device_node *prev)
422{
423 struct device_node *next;
424
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500425 raw_spin_lock(&devtree_lock);
Timur Tabi32961932012-08-14 13:20:23 +0000426 next = prev ? prev->sibling : node->child;
427 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700428 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000429 continue;
430 if (of_node_get(next))
431 break;
432 }
433 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500434 raw_spin_unlock(&devtree_lock);
Timur Tabi32961932012-08-14 13:20:23 +0000435 return next;
436}
437EXPORT_SYMBOL(of_get_next_available_child);
438
439/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100440 * of_get_child_by_name - Find the child node by name for a given parent
441 * @node: parent node
442 * @name: child name to look for.
443 *
444 * This function looks for child node for given matching name
445 *
446 * Returns a node pointer if found, with refcount incremented, use
447 * of_node_put() on it when done.
448 * Returns NULL if node is not found.
449 */
450struct device_node *of_get_child_by_name(const struct device_node *node,
451 const char *name)
452{
453 struct device_node *child;
454
455 for_each_child_of_node(node, child)
456 if (child->name && (of_node_cmp(child->name, name) == 0))
457 break;
458 return child;
459}
460EXPORT_SYMBOL(of_get_child_by_name);
461
462/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000463 * of_find_node_by_path - Find a node matching a full OF path
464 * @path: The full path to match
465 *
466 * Returns a node pointer with refcount incremented, use
467 * of_node_put() on it when done.
468 */
469struct device_node *of_find_node_by_path(const char *path)
470{
Randy Dunlap465aac62012-11-30 10:01:51 +0000471 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500472 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000473
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500474 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000475 for (; np; np = np->allnext) {
476 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
477 && of_node_get(np))
478 break;
479 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500480 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000481 return np;
482}
483EXPORT_SYMBOL(of_find_node_by_path);
484
485/**
486 * of_find_node_by_name - Find a node by its "name" property
487 * @from: The node to start searching from or NULL, the node
488 * you pass will not be searched, only the next one
489 * will; typically, you pass what the previous call
490 * returned. of_node_put() will be called on it
491 * @name: The name string to match against
492 *
493 * Returns a node pointer with refcount incremented, use
494 * of_node_put() on it when done.
495 */
496struct device_node *of_find_node_by_name(struct device_node *from,
497 const char *name)
498{
499 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500500 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000501
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500502 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000503 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000504 for (; np; np = np->allnext)
505 if (np->name && (of_node_cmp(np->name, name) == 0)
506 && of_node_get(np))
507 break;
508 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500509 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000510 return np;
511}
512EXPORT_SYMBOL(of_find_node_by_name);
513
514/**
515 * of_find_node_by_type - Find a node by its "device_type" property
516 * @from: The node to start searching from, or NULL to start searching
517 * the entire device tree. The node you pass will not be
518 * searched, only the next one will; typically, you pass
519 * what the previous call returned. of_node_put() will be
520 * called on from for you.
521 * @type: The type string to match against
522 *
523 * Returns a node pointer with refcount incremented, use
524 * of_node_put() on it when done.
525 */
526struct device_node *of_find_node_by_type(struct device_node *from,
527 const char *type)
528{
529 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500530 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000531
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500532 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000533 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000534 for (; np; np = np->allnext)
535 if (np->type && (of_node_cmp(np->type, type) == 0)
536 && of_node_get(np))
537 break;
538 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500539 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000540 return np;
541}
542EXPORT_SYMBOL(of_find_node_by_type);
543
544/**
545 * of_find_compatible_node - Find a node based on type and one of the
546 * tokens in its "compatible" property
547 * @from: The node to start searching from or NULL, the node
548 * you pass will not be searched, only the next one
549 * will; typically, you pass what the previous call
550 * returned. of_node_put() will be called on it
551 * @type: The type string to match "device_type" or NULL to ignore
552 * @compatible: The string to match to one of the tokens in the device
553 * "compatible" list.
554 *
555 * Returns a node pointer with refcount incremented, use
556 * of_node_put() on it when done.
557 */
558struct device_node *of_find_compatible_node(struct device_node *from,
559 const char *type, const char *compatible)
560{
561 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500562 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000563
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500564 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000565 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000566 for (; np; np = np->allnext) {
567 if (type
568 && !(np->type && (of_node_cmp(np->type, type) == 0)))
569 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500570 if (__of_device_is_compatible(np, compatible) &&
571 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000572 break;
573 }
574 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500575 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000576 return np;
577}
578EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100579
580/**
Michael Ellerman1e291b142008-11-12 18:54:42 +0000581 * of_find_node_with_property - Find a node which has a property with
582 * the given name.
583 * @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 * @prop_name: The name of the property to look for.
588 *
589 * Returns a node pointer with refcount incremented, use
590 * of_node_put() on it when done.
591 */
592struct device_node *of_find_node_with_property(struct device_node *from,
593 const char *prop_name)
594{
595 struct device_node *np;
596 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500597 unsigned long flags;
Michael Ellerman1e291b142008-11-12 18:54:42 +0000598
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500599 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000600 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b142008-11-12 18:54:42 +0000601 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530602 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b142008-11-12 18:54:42 +0000603 if (of_prop_cmp(pp->name, prop_name) == 0) {
604 of_node_get(np);
605 goto out;
606 }
607 }
608 }
609out:
610 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500611 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000612 return np;
613}
614EXPORT_SYMBOL(of_find_node_with_property);
615
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500616static
617const struct of_device_id *__of_match_node(const struct of_device_id *matches,
618 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100619{
Grant Likelya52f07e2011-03-18 10:21:29 -0600620 if (!matches)
621 return NULL;
622
Grant Likely283029d2008-01-09 06:20:40 +1100623 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
624 int match = 1;
625 if (matches->name[0])
626 match &= node->name
627 && !strcmp(matches->name, node->name);
628 if (matches->type[0])
629 match &= node->type
630 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700631 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500632 match &= __of_device_is_compatible(node,
633 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700634 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100635 return matches;
636 matches++;
637 }
638 return NULL;
639}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500640
641/**
642 * of_match_node - Tell if an device_node has a matching of_match structure
643 * @matches: array of of device match structures to search in
644 * @node: the of device structure to match against
645 *
646 * Low level utility function used by device matching.
647 */
648const struct of_device_id *of_match_node(const struct of_device_id *matches,
649 const struct device_node *node)
650{
651 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500652 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500653
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500654 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500655 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500656 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500657 return match;
658}
Grant Likely283029d2008-01-09 06:20:40 +1100659EXPORT_SYMBOL(of_match_node);
660
661/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700662 * of_find_matching_node_and_match - Find a node based on an of_device_id
663 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100664 * @from: The node to start searching from or NULL, the node
665 * you pass will not be searched, only the next one
666 * will; typically, you pass what the previous call
667 * returned. of_node_put() will be called on it
668 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700669 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100670 *
671 * Returns a node pointer with refcount incremented, use
672 * of_node_put() on it when done.
673 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700674struct device_node *of_find_matching_node_and_match(struct device_node *from,
675 const struct of_device_id *matches,
676 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100677{
678 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800679 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500680 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100681
Stephen Warren50c8af42012-11-20 16:12:20 -0700682 if (match)
683 *match = NULL;
684
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500685 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000686 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100687 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500688 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800689 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700690 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800691 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100692 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700693 }
Grant Likely283029d2008-01-09 06:20:40 +1100694 }
695 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500696 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100697 return np;
698}
Grant Likely80c20222012-12-19 10:45:36 +0000699EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400700
701/**
Grant Likely3f07af42008-07-25 22:25:13 -0400702 * of_modalias_node - Lookup appropriate modalias for a device node
703 * @node: pointer to a device tree node
704 * @modalias: Pointer to buffer that modalias value will be copied into
705 * @len: Length of modalias value
706 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600707 * Based on the value of the compatible property, this routine will attempt
708 * to choose an appropriate modalias value for a particular device tree node.
709 * It does this by stripping the manufacturer prefix (as delimited by a ',')
710 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400711 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600712 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400713 */
714int of_modalias_node(struct device_node *node, char *modalias, int len)
715{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600716 const char *compatible, *p;
717 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400718
719 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600720 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400721 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400722 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600723 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400724 return 0;
725}
726EXPORT_SYMBOL_GPL(of_modalias_node);
727
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000728/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700729 * of_find_node_by_phandle - Find a node given a phandle
730 * @handle: phandle of the node to find
731 *
732 * Returns a node pointer with refcount incremented, use
733 * of_node_put() on it when done.
734 */
735struct device_node *of_find_node_by_phandle(phandle handle)
736{
737 struct device_node *np;
738
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500739 raw_spin_lock(&devtree_lock);
Randy Dunlap465aac62012-11-30 10:01:51 +0000740 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700741 if (np->phandle == handle)
742 break;
743 of_node_get(np);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500744 raw_spin_unlock(&devtree_lock);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700745 return np;
746}
747EXPORT_SYMBOL(of_find_node_by_phandle);
748
749/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530750 * of_property_read_u8_array - Find and read an array of u8 from a property.
751 *
752 * @np: device node from which the property value is to be read.
753 * @propname: name of the property to be searched.
754 * @out_value: pointer to return value, modified only if return value is 0.
755 * @sz: number of array elements to read
756 *
757 * Search for a property in a device node and read 8-bit value(s) from
758 * it. Returns 0 on success, -EINVAL if the property does not exist,
759 * -ENODATA if property does not have a value, and -EOVERFLOW if the
760 * property data isn't large enough.
761 *
762 * dts entry of array should be like:
763 * property = /bits/ 8 <0x50 0x60 0x70>;
764 *
765 * The out_value is modified only if a valid u8 value can be decoded.
766 */
767int of_property_read_u8_array(const struct device_node *np,
768 const char *propname, u8 *out_values, size_t sz)
769{
770 struct property *prop = of_find_property(np, propname, NULL);
771 const u8 *val;
772
773 if (!prop)
774 return -EINVAL;
775 if (!prop->value)
776 return -ENODATA;
777 if ((sz * sizeof(*out_values)) > prop->length)
778 return -EOVERFLOW;
779
780 val = prop->value;
781 while (sz--)
782 *out_values++ = *val++;
783 return 0;
784}
785EXPORT_SYMBOL_GPL(of_property_read_u8_array);
786
787/**
788 * of_property_read_u16_array - Find and read an array of u16 from a property.
789 *
790 * @np: device node from which the property value is to be read.
791 * @propname: name of the property to be searched.
792 * @out_value: pointer to return value, modified only if return value is 0.
793 * @sz: number of array elements to read
794 *
795 * Search for a property in a device node and read 16-bit value(s) from
796 * it. Returns 0 on success, -EINVAL if the property does not exist,
797 * -ENODATA if property does not have a value, and -EOVERFLOW if the
798 * property data isn't large enough.
799 *
800 * dts entry of array should be like:
801 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
802 *
803 * The out_value is modified only if a valid u16 value can be decoded.
804 */
805int of_property_read_u16_array(const struct device_node *np,
806 const char *propname, u16 *out_values, size_t sz)
807{
808 struct property *prop = of_find_property(np, propname, NULL);
809 const __be16 *val;
810
811 if (!prop)
812 return -EINVAL;
813 if (!prop->value)
814 return -ENODATA;
815 if ((sz * sizeof(*out_values)) > prop->length)
816 return -EOVERFLOW;
817
818 val = prop->value;
819 while (sz--)
820 *out_values++ = be16_to_cpup(val++);
821 return 0;
822}
823EXPORT_SYMBOL_GPL(of_property_read_u16_array);
824
825/**
Rob Herring0e373632011-07-06 15:42:58 -0500826 * of_property_read_u32_array - Find and read an array of 32 bit integers
827 * from a property.
828 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530829 * @np: device node from which the property value is to be read.
830 * @propname: name of the property to be searched.
831 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530832 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530833 *
Rob Herring0e373632011-07-06 15:42:58 -0500834 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530835 * it. Returns 0 on success, -EINVAL if the property does not exist,
836 * -ENODATA if property does not have a value, and -EOVERFLOW if the
837 * property data isn't large enough.
838 *
839 * The out_value is modified only if a valid u32 value can be decoded.
840 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100841int of_property_read_u32_array(const struct device_node *np,
842 const char *propname, u32 *out_values,
843 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530844{
845 struct property *prop = of_find_property(np, propname, NULL);
Rob Herring0e373632011-07-06 15:42:58 -0500846 const __be32 *val;
Thomas Abrahama3b85362011-06-30 21:26:10 +0530847
848 if (!prop)
849 return -EINVAL;
850 if (!prop->value)
851 return -ENODATA;
Rob Herring0e373632011-07-06 15:42:58 -0500852 if ((sz * sizeof(*out_values)) > prop->length)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530853 return -EOVERFLOW;
Rob Herring0e373632011-07-06 15:42:58 -0500854
855 val = prop->value;
856 while (sz--)
857 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530858 return 0;
859}
Rob Herring0e373632011-07-06 15:42:58 -0500860EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530861
862/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100863 * of_property_read_u64 - Find and read a 64 bit integer from a property
864 * @np: device node from which the property value is to be read.
865 * @propname: name of the property to be searched.
866 * @out_value: pointer to return value, modified only if return value is 0.
867 *
868 * Search for a property in a device node and read a 64-bit value from
869 * it. Returns 0 on success, -EINVAL if the property does not exist,
870 * -ENODATA if property does not have a value, and -EOVERFLOW if the
871 * property data isn't large enough.
872 *
873 * The out_value is modified only if a valid u64 value can be decoded.
874 */
875int of_property_read_u64(const struct device_node *np, const char *propname,
876 u64 *out_value)
877{
878 struct property *prop = of_find_property(np, propname, NULL);
879
880 if (!prop)
881 return -EINVAL;
882 if (!prop->value)
883 return -ENODATA;
884 if (sizeof(*out_value) > prop->length)
885 return -EOVERFLOW;
886 *out_value = of_read_number(prop->value, 2);
887 return 0;
888}
889EXPORT_SYMBOL_GPL(of_property_read_u64);
890
891/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530892 * of_property_read_string - Find and read a string from a property
893 * @np: device node from which the property value is to be read.
894 * @propname: name of the property to be searched.
895 * @out_string: pointer to null terminated return string, modified only if
896 * return value is 0.
897 *
898 * Search for a property in a device tree node and retrieve a null
899 * terminated string value (pointer to data, not a copy). Returns 0 on
900 * success, -EINVAL if the property does not exist, -ENODATA if property
901 * does not have a value, and -EILSEQ if the string is not null-terminated
902 * within the length of the property data.
903 *
904 * The out_string pointer is modified only if a valid string can be decoded.
905 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100906int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800907 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530908{
909 struct property *prop = of_find_property(np, propname, NULL);
910 if (!prop)
911 return -EINVAL;
912 if (!prop->value)
913 return -ENODATA;
914 if (strnlen(prop->value, prop->length) >= prop->length)
915 return -EILSEQ;
916 *out_string = prop->value;
917 return 0;
918}
919EXPORT_SYMBOL_GPL(of_property_read_string);
920
921/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200922 * of_property_read_string_index - Find and read a string from a multiple
923 * strings property.
924 * @np: device node from which the property value is to be read.
925 * @propname: name of the property to be searched.
926 * @index: index of the string in the list of strings
927 * @out_string: pointer to null terminated return string, modified only if
928 * return value is 0.
929 *
930 * Search for a property in a device tree node and retrieve a null
931 * terminated string value (pointer to data, not a copy) in the list of strings
932 * contained in that property.
933 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
934 * property does not have a value, and -EILSEQ if the string is not
935 * null-terminated within the length of the property data.
936 *
937 * The out_string pointer is modified only if a valid string can be decoded.
938 */
939int of_property_read_string_index(struct device_node *np, const char *propname,
940 int index, const char **output)
941{
942 struct property *prop = of_find_property(np, propname, NULL);
943 int i = 0;
944 size_t l = 0, total = 0;
945 const char *p;
946
947 if (!prop)
948 return -EINVAL;
949 if (!prop->value)
950 return -ENODATA;
951 if (strnlen(prop->value, prop->length) >= prop->length)
952 return -EILSEQ;
953
954 p = prop->value;
955
956 for (i = 0; total < prop->length; total += l, p += l) {
957 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +0100958 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200959 *output = p;
960 return 0;
961 }
962 }
963 return -ENODATA;
964}
965EXPORT_SYMBOL_GPL(of_property_read_string_index);
966
Grant Likely7aff0fe2011-12-12 09:25:58 -0700967/**
968 * of_property_match_string() - Find string in a list and return index
969 * @np: pointer to node containing string list property
970 * @propname: string list property name
971 * @string: pointer to string to search for in string list
972 *
973 * This function searches a string list property and returns the index
974 * of a specific string value.
975 */
976int of_property_match_string(struct device_node *np, const char *propname,
977 const char *string)
978{
979 struct property *prop = of_find_property(np, propname, NULL);
980 size_t l;
981 int i;
982 const char *p, *end;
983
984 if (!prop)
985 return -EINVAL;
986 if (!prop->value)
987 return -ENODATA;
988
989 p = prop->value;
990 end = p + prop->length;
991
992 for (i = 0; p < end; i++, p += l) {
993 l = strlen(p) + 1;
994 if (p + l > end)
995 return -EILSEQ;
996 pr_debug("comparing %s with %s\n", string, p);
997 if (strcmp(string, p) == 0)
998 return i; /* Found it; return index */
999 }
1000 return -ENODATA;
1001}
1002EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001003
1004/**
1005 * of_property_count_strings - Find and return the number of strings from a
1006 * multiple strings property.
1007 * @np: device node from which the property value is to be read.
1008 * @propname: name of the property to be searched.
1009 *
1010 * Search for a property in a device tree node and retrieve the number of null
1011 * terminated string contain in it. Returns the number of strings on
1012 * success, -EINVAL if the property does not exist, -ENODATA if property
1013 * does not have a value, and -EILSEQ if the string is not null-terminated
1014 * within the length of the property data.
1015 */
1016int of_property_count_strings(struct device_node *np, const char *propname)
1017{
1018 struct property *prop = of_find_property(np, propname, NULL);
1019 int i = 0;
1020 size_t l = 0, total = 0;
1021 const char *p;
1022
1023 if (!prop)
1024 return -EINVAL;
1025 if (!prop->value)
1026 return -ENODATA;
1027 if (strnlen(prop->value, prop->length) >= prop->length)
1028 return -EILSEQ;
1029
1030 p = prop->value;
1031
Benoit Cousson88af7f52011-12-05 15:23:54 +01001032 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001033 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001034
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001035 return i;
1036}
1037EXPORT_SYMBOL_GPL(of_property_count_strings);
1038
1039/**
Grant Likely739649c2009-04-25 12:52:40 +00001040 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1041 * @np: Pointer to device node holding phandle property
1042 * @phandle_name: Name of property holding a phandle value
1043 * @index: For properties holding a table of phandles, this is the index into
1044 * the table
1045 *
1046 * Returns the device_node pointer with refcount incremented. Use
1047 * of_node_put() on it when done.
1048 */
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +01001049struct device_node *of_parse_phandle(const struct device_node *np,
1050 const char *phandle_name, int index)
Grant Likely739649c2009-04-25 12:52:40 +00001051{
Grant Likely9a6b2e52010-07-23 01:48:25 -06001052 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +00001053 int size;
1054
1055 phandle = of_get_property(np, phandle_name, &size);
1056 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1057 return NULL;
1058
Grant Likely9a6b2e52010-07-23 01:48:25 -06001059 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +00001060}
1061EXPORT_SYMBOL(of_parse_phandle);
1062
1063/**
Grant Likely15c9a0a2011-12-12 09:25:57 -07001064 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001065 * @np: pointer to a device tree node containing a list
1066 * @list_name: property name that contains a list
1067 * @cells_name: property name that specifies phandles' arguments count
1068 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -07001069 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001070 *
1071 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001072 * Returns 0 on success and fills out_args, on error returns appropriate
1073 * errno value.
1074 *
1075 * Caller is responsible to call of_node_put() on the returned out_args->node
1076 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001077 *
1078 * Example:
1079 *
1080 * phandle1: node1 {
1081 * #list-cells = <2>;
1082 * }
1083 *
1084 * phandle2: node2 {
1085 * #list-cells = <1>;
1086 * }
1087 *
1088 * node3 {
1089 * list = <&phandle1 1 2 &phandle2 3>;
1090 * }
1091 *
1092 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001093 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001094 */
Grant Likelybd69f732013-02-10 22:57:21 +00001095static int __of_parse_phandle_with_args(const struct device_node *np,
1096 const char *list_name,
1097 const char *cells_name, int index,
1098 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001099{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001100 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001101 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001102 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001103 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001104 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001105
Grant Likely15c9a0a2011-12-12 09:25:57 -07001106 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001107 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001108 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001109 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001110 list_end = list + size / sizeof(*list);
1111
Grant Likely15c9a0a2011-12-12 09:25:57 -07001112 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001113 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001114 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001115 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001116
Grant Likely15c9a0a2011-12-12 09:25:57 -07001117 /*
1118 * If phandle is 0, then it is an empty entry with no
1119 * arguments. Skip forward to the next entry.
1120 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001121 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001122 if (phandle) {
1123 /*
1124 * Find the provider node and parse the #*-cells
1125 * property to determine the argument length
1126 */
1127 node = of_find_node_by_phandle(phandle);
1128 if (!node) {
1129 pr_err("%s: could not find phandle\n",
1130 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001131 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001132 }
1133 if (of_property_read_u32(node, cells_name, &count)) {
1134 pr_err("%s: could not get %s for %s\n",
1135 np->full_name, cells_name,
1136 node->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001137 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001138 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001139
Grant Likely15c9a0a2011-12-12 09:25:57 -07001140 /*
1141 * Make sure that the arguments actually fit in the
1142 * remaining property data length
1143 */
1144 if (list + count > list_end) {
1145 pr_err("%s: arguments longer than property\n",
1146 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001147 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001148 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001149 }
1150
Grant Likely15c9a0a2011-12-12 09:25:57 -07001151 /*
1152 * All of the error cases above bail out of the loop, so at
1153 * this point, the parsing is successful. If the requested
1154 * index matches, then fill the out_args structure and return,
1155 * or return -ENOENT for an empty entry.
1156 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001157 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001158 if (cur_index == index) {
1159 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001160 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001161
Grant Likely15c9a0a2011-12-12 09:25:57 -07001162 if (out_args) {
1163 int i;
1164 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1165 count = MAX_PHANDLE_ARGS;
1166 out_args->np = node;
1167 out_args->args_count = count;
1168 for (i = 0; i < count; i++)
1169 out_args->args[i] = be32_to_cpup(list++);
1170 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001171
1172 /* Found it! return success */
1173 if (node)
1174 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001175 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001176 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001177
1178 of_node_put(node);
1179 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001180 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001181 cur_index++;
1182 }
1183
Grant Likely23ce04c2013-02-12 21:21:49 +00001184 /*
1185 * Unlock node before returning result; will be one of:
1186 * -ENOENT : index is for empty phandle
1187 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001188 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001189 */
Grant Likelybd69f732013-02-10 22:57:21 +00001190 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001191 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001192 if (node)
1193 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001194 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001195}
Grant Likelybd69f732013-02-10 22:57:21 +00001196
1197int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1198 const char *cells_name, int index,
1199 struct of_phandle_args *out_args)
1200{
1201 if (index < 0)
1202 return -EINVAL;
1203 return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
1204}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001205EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001206
Grant Likelybd69f732013-02-10 22:57:21 +00001207/**
1208 * of_count_phandle_with_args() - Find the number of phandles references in a property
1209 * @np: pointer to a device tree node containing a list
1210 * @list_name: property name that contains a list
1211 * @cells_name: property name that specifies phandles' arguments count
1212 *
1213 * Returns the number of phandle + argument tuples within a property. It
1214 * is a typical pattern to encode a list of phandle and variable
1215 * arguments into a single property. The number of arguments is encoded
1216 * by a property in the phandle-target node. For example, a gpios
1217 * property would contain a list of GPIO specifies consisting of a
1218 * phandle and 1 or more arguments. The number of arguments are
1219 * determined by the #gpio-cells property in the node pointed to by the
1220 * phandle.
1221 */
1222int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1223 const char *cells_name)
1224{
1225 return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
1226}
1227EXPORT_SYMBOL(of_count_phandle_with_args);
1228
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001229#if defined(CONFIG_OF_DYNAMIC)
1230static int of_property_notify(int action, struct device_node *np,
1231 struct property *prop)
1232{
1233 struct of_prop_reconfig pr;
1234
1235 pr.dn = np;
1236 pr.prop = prop;
1237 return of_reconfig_notify(action, &pr);
1238}
1239#else
1240static int of_property_notify(int action, struct device_node *np,
1241 struct property *prop)
1242{
1243 return 0;
1244}
1245#endif
1246
Grant Likely02af11b2009-11-23 20:16:45 -07001247/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001248 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001249 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001250int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001251{
1252 struct property **next;
1253 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001254 int rc;
1255
1256 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1257 if (rc)
1258 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001259
1260 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001261 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001262 next = &np->properties;
1263 while (*next) {
1264 if (strcmp(prop->name, (*next)->name) == 0) {
1265 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001266 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001267 return -1;
1268 }
1269 next = &(*next)->next;
1270 }
1271 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001272 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001273
1274#ifdef CONFIG_PROC_DEVICETREE
1275 /* try to add to proc as well if it was initialized */
1276 if (np->pde)
1277 proc_device_tree_add_prop(np->pde, prop);
1278#endif /* CONFIG_PROC_DEVICETREE */
1279
1280 return 0;
1281}
1282
1283/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001284 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001285 *
1286 * Note that we don't actually remove it, since we have given out
1287 * who-knows-how-many pointers to the data using get-property.
1288 * Instead we just move the property to the "dead properties"
1289 * list, so it won't be found any more.
1290 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001291int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001292{
1293 struct property **next;
1294 unsigned long flags;
1295 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001296 int rc;
1297
1298 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1299 if (rc)
1300 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001301
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001302 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001303 next = &np->properties;
1304 while (*next) {
1305 if (*next == prop) {
1306 /* found the node */
1307 *next = prop->next;
1308 prop->next = np->deadprops;
1309 np->deadprops = prop;
1310 found = 1;
1311 break;
1312 }
1313 next = &(*next)->next;
1314 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001315 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001316
1317 if (!found)
1318 return -ENODEV;
1319
1320#ifdef CONFIG_PROC_DEVICETREE
1321 /* try to remove the proc node as well */
1322 if (np->pde)
1323 proc_device_tree_remove_prop(np->pde, prop);
1324#endif /* CONFIG_PROC_DEVICETREE */
1325
1326 return 0;
1327}
1328
1329/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001330 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001331 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001332 *
1333 * Note that we don't actually remove it, since we have given out
1334 * who-knows-how-many pointers to the data using get-property.
1335 * Instead we just move the property to the "dead properties" list,
1336 * and add the new property to the property list
1337 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001338int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001339{
Dong Aisheng475d0092012-07-11 15:16:37 +10001340 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001341 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001342 int rc, found = 0;
1343
1344 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1345 if (rc)
1346 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001347
Dong Aisheng475d0092012-07-11 15:16:37 +10001348 if (!newprop->name)
1349 return -EINVAL;
1350
1351 oldprop = of_find_property(np, newprop->name, NULL);
1352 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001353 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001354
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001355 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001356 next = &np->properties;
1357 while (*next) {
1358 if (*next == oldprop) {
1359 /* found the node */
1360 newprop->next = oldprop->next;
1361 *next = newprop;
1362 oldprop->next = np->deadprops;
1363 np->deadprops = oldprop;
1364 found = 1;
1365 break;
1366 }
1367 next = &(*next)->next;
1368 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001369 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001370
1371 if (!found)
1372 return -ENODEV;
1373
1374#ifdef CONFIG_PROC_DEVICETREE
1375 /* try to add to proc as well if it was initialized */
1376 if (np->pde)
1377 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1378#endif /* CONFIG_PROC_DEVICETREE */
1379
1380 return 0;
1381}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001382
1383#if defined(CONFIG_OF_DYNAMIC)
1384/*
1385 * Support for dynamic device trees.
1386 *
1387 * On some platforms, the device tree can be manipulated at runtime.
1388 * The routines in this section support adding, removing and changing
1389 * device tree nodes.
1390 */
1391
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001392static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1393
1394int of_reconfig_notifier_register(struct notifier_block *nb)
1395{
1396 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1397}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001398EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001399
1400int of_reconfig_notifier_unregister(struct notifier_block *nb)
1401{
1402 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1403}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001404EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001405
1406int of_reconfig_notify(unsigned long action, void *p)
1407{
1408 int rc;
1409
1410 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1411 return notifier_to_errno(rc);
1412}
1413
Nathan Fontenote81b3292012-10-02 16:55:01 +00001414#ifdef CONFIG_PROC_DEVICETREE
1415static void of_add_proc_dt_entry(struct device_node *dn)
1416{
1417 struct proc_dir_entry *ent;
1418
1419 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1420 if (ent)
1421 proc_device_tree_add_node(dn, ent);
1422}
1423#else
1424static void of_add_proc_dt_entry(struct device_node *dn)
1425{
1426 return;
1427}
1428#endif
1429
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001430/**
1431 * of_attach_node - Plug a device node into the tree and global list.
1432 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001433int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001434{
1435 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001436 int rc;
1437
1438 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1439 if (rc)
1440 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001441
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001442 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001443 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001444 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001445 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001446 of_allnodes = np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001447 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001448
1449 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001450 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001451}
1452
Nathan Fontenote81b3292012-10-02 16:55:01 +00001453#ifdef CONFIG_PROC_DEVICETREE
1454static void of_remove_proc_dt_entry(struct device_node *dn)
1455{
1456 struct device_node *parent = dn->parent;
1457 struct property *prop = dn->properties;
1458
1459 while (prop) {
1460 remove_proc_entry(prop->name, dn->pde);
1461 prop = prop->next;
1462 }
1463
1464 if (dn->pde)
1465 remove_proc_entry(dn->pde->name, parent->pde);
1466}
1467#else
1468static void of_remove_proc_dt_entry(struct device_node *dn)
1469{
1470 return;
1471}
1472#endif
1473
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001474/**
1475 * of_detach_node - "Unplug" a node from the device tree.
1476 *
1477 * The caller must hold a reference to the node. The memory associated with
1478 * the node is not freed until its refcount goes to zero.
1479 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001480int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001481{
1482 struct device_node *parent;
1483 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001484 int rc = 0;
1485
1486 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1487 if (rc)
1488 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001489
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001490 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001491
Nathan Fontenote81b3292012-10-02 16:55:01 +00001492 if (of_node_check_flag(np, OF_DETACHED)) {
1493 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001494 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001495 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001496 }
1497
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001498 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001499 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001500 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001501 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001502 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001503
Randy Dunlap465aac62012-11-30 10:01:51 +00001504 if (of_allnodes == np)
1505 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001506 else {
1507 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001508 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001509 prev->allnext != np;
1510 prev = prev->allnext)
1511 ;
1512 prev->allnext = np->allnext;
1513 }
1514
1515 if (parent->child == np)
1516 parent->child = np->sibling;
1517 else {
1518 struct device_node *prevsib;
1519 for (prevsib = np->parent->child;
1520 prevsib->sibling != np;
1521 prevsib = prevsib->sibling)
1522 ;
1523 prevsib->sibling = np->sibling;
1524 }
1525
1526 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001527 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001528
1529 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001530 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001531}
1532#endif /* defined(CONFIG_OF_DYNAMIC) */
1533
Shawn Guo611cad72011-08-15 15:28:14 +08001534static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1535 int id, const char *stem, int stem_len)
1536{
1537 ap->np = np;
1538 ap->id = id;
1539 strncpy(ap->stem, stem, stem_len);
1540 ap->stem[stem_len] = 0;
1541 list_add_tail(&ap->link, &aliases_lookup);
1542 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001543 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001544}
1545
1546/**
1547 * of_alias_scan - Scan all properties of 'aliases' node
1548 *
1549 * The function scans all the properties of 'aliases' node and populate
1550 * the the global lookup table with the properties. It returns the
1551 * number of alias_prop found, or error code in error case.
1552 *
1553 * @dt_alloc: An allocator that provides a virtual address to memory
1554 * for the resulting tree
1555 */
1556void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1557{
1558 struct property *pp;
1559
1560 of_chosen = of_find_node_by_path("/chosen");
1561 if (of_chosen == NULL)
1562 of_chosen = of_find_node_by_path("/chosen@0");
1563 of_aliases = of_find_node_by_path("/aliases");
1564 if (!of_aliases)
1565 return;
1566
Dong Aisheng8af0da92011-12-22 20:19:24 +08001567 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001568 const char *start = pp->name;
1569 const char *end = start + strlen(start);
1570 struct device_node *np;
1571 struct alias_prop *ap;
1572 int id, len;
1573
1574 /* Skip those we do not want to proceed */
1575 if (!strcmp(pp->name, "name") ||
1576 !strcmp(pp->name, "phandle") ||
1577 !strcmp(pp->name, "linux,phandle"))
1578 continue;
1579
1580 np = of_find_node_by_path(pp->value);
1581 if (!np)
1582 continue;
1583
1584 /* walk the alias backwards to extract the id and work out
1585 * the 'stem' string */
1586 while (isdigit(*(end-1)) && end > start)
1587 end--;
1588 len = end - start;
1589
1590 if (kstrtoint(end, 10, &id) < 0)
1591 continue;
1592
1593 /* Allocate an alias_prop with enough space for the stem */
1594 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1595 if (!ap)
1596 continue;
1597 ap->alias = start;
1598 of_alias_add(ap, np, id, start, len);
1599 }
1600}
1601
1602/**
1603 * of_alias_get_id - Get alias id for the given device_node
1604 * @np: Pointer to the given device_node
1605 * @stem: Alias stem of the given device_node
1606 *
1607 * The function travels the lookup table to get alias id for the given
1608 * device_node and alias stem. It returns the alias id if find it.
1609 */
1610int of_alias_get_id(struct device_node *np, const char *stem)
1611{
1612 struct alias_prop *app;
1613 int id = -ENODEV;
1614
1615 mutex_lock(&of_aliases_mutex);
1616 list_for_each_entry(app, &aliases_lookup, link) {
1617 if (strcmp(app->stem, stem) != 0)
1618 continue;
1619
1620 if (np == app->np) {
1621 id = app->id;
1622 break;
1623 }
1624 }
1625 mutex_unlock(&of_aliases_mutex);
1626
1627 return id;
1628}
1629EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001630
1631const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1632 u32 *pu)
1633{
1634 const void *curv = cur;
1635
1636 if (!prop)
1637 return NULL;
1638
1639 if (!cur) {
1640 curv = prop->value;
1641 goto out_val;
1642 }
1643
1644 curv += sizeof(*cur);
1645 if (curv >= prop->value + prop->length)
1646 return NULL;
1647
1648out_val:
1649 *pu = be32_to_cpup(curv);
1650 return curv;
1651}
1652EXPORT_SYMBOL_GPL(of_prop_next_u32);
1653
1654const char *of_prop_next_string(struct property *prop, const char *cur)
1655{
1656 const void *curv = cur;
1657
1658 if (!prop)
1659 return NULL;
1660
1661 if (!cur)
1662 return prop->value;
1663
1664 curv += strlen(cur) + 1;
1665 if (curv >= prop->value + prop->length)
1666 return NULL;
1667
1668 return curv;
1669}
1670EXPORT_SYMBOL_GPL(of_prop_next_string);