blob: 7c75d7551eb921645498516d570e7d8c6d3d6de3 [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;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000195 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600196
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000197 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000198 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600199 for (; np != NULL; np = np->allnext)
200 if (of_node_get(np))
201 break;
202 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000203 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600204 return np;
205}
206EXPORT_SYMBOL(of_find_all_nodes);
207
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000208/*
209 * Find a property with a given name for a given node
210 * and return the value.
211 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500212static const void *__of_get_property(const struct device_node *np,
213 const char *name, int *lenp)
214{
215 struct property *pp = __of_find_property(np, name, lenp);
216
217 return pp ? pp->value : NULL;
218}
219
220/*
221 * Find a property with a given name for a given node
222 * and return the value.
223 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000224const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500225 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000226{
227 struct property *pp = of_find_property(np, name, lenp);
228
229 return pp ? pp->value : NULL;
230}
231EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000232
233/** Checks if the given "compat" string matches one of the strings in
234 * the device's "compatible" property
235 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500236static int __of_device_is_compatible(const struct device_node *device,
237 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000238{
239 const char* cp;
240 int cplen, l;
241
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500242 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000243 if (cp == NULL)
244 return 0;
245 while (cplen > 0) {
246 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
247 return 1;
248 l = strlen(cp) + 1;
249 cp += l;
250 cplen -= l;
251 }
252
253 return 0;
254}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500255
256/** Checks if the given "compat" string matches one of the strings in
257 * the device's "compatible" property
258 */
259int of_device_is_compatible(const struct device_node *device,
260 const char *compat)
261{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500262 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500263 int res;
264
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500265 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500266 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500267 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500268 return res;
269}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000270EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000271
272/**
Grant Likely71a157e2010-02-01 21:34:14 -0700273 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700274 * @compat: compatible string to look for in root node's compatible property.
275 *
276 * Returns true if the root node has the given value in its
277 * compatible property.
278 */
Grant Likely71a157e2010-02-01 21:34:14 -0700279int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700280{
281 struct device_node *root;
282 int rc = 0;
283
284 root = of_find_node_by_path("/");
285 if (root) {
286 rc = of_device_is_compatible(root, compat);
287 of_node_put(root);
288 }
289 return rc;
290}
Grant Likely71a157e2010-02-01 21:34:14 -0700291EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700292
293/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700294 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100295 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700296 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100297 *
298 * Returns 1 if the status property is absent or set to "okay" or "ok",
299 * 0 otherwise
300 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700301static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100302{
303 const char *status;
304 int statlen;
305
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700306 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100307 if (status == NULL)
308 return 1;
309
310 if (statlen > 0) {
311 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
312 return 1;
313 }
314
315 return 0;
316}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700317
318/**
319 * of_device_is_available - check if a device is available for use
320 *
321 * @device: Node to check for availability
322 *
323 * Returns 1 if the status property is absent or set to "okay" or "ok",
324 * 0 otherwise
325 */
326int of_device_is_available(const struct device_node *device)
327{
328 unsigned long flags;
329 int res;
330
331 raw_spin_lock_irqsave(&devtree_lock, flags);
332 res = __of_device_is_available(device);
333 raw_spin_unlock_irqrestore(&devtree_lock, flags);
334 return res;
335
336}
Josh Boyer834d97d2008-03-27 00:33:14 +1100337EXPORT_SYMBOL(of_device_is_available);
338
339/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000340 * of_get_parent - Get a node's parent if any
341 * @node: Node to get parent
342 *
343 * Returns a node pointer with refcount incremented, use
344 * of_node_put() on it when done.
345 */
346struct device_node *of_get_parent(const struct device_node *node)
347{
348 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500349 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000350
351 if (!node)
352 return NULL;
353
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500354 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000355 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500356 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000357 return np;
358}
359EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000360
361/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000362 * of_get_next_parent - Iterate to a node's parent
363 * @node: Node to get parent of
364 *
365 * This is like of_get_parent() except that it drops the
366 * refcount on the passed node, making it suitable for iterating
367 * through a node's parents.
368 *
369 * Returns a node pointer with refcount incremented, use
370 * of_node_put() on it when done.
371 */
372struct device_node *of_get_next_parent(struct device_node *node)
373{
374 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500375 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000376
377 if (!node)
378 return NULL;
379
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500380 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000381 parent = of_node_get(node->parent);
382 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500383 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000384 return parent;
385}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300386EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000387
388/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000389 * of_get_next_child - Iterate a node childs
390 * @node: parent node
391 * @prev: previous child of the parent node, or NULL to get first
392 *
393 * Returns a node pointer with refcount incremented, use
394 * of_node_put() on it when done.
395 */
396struct device_node *of_get_next_child(const struct device_node *node,
397 struct device_node *prev)
398{
399 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500400 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000401
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500402 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000403 next = prev ? prev->sibling : node->child;
404 for (; next; next = next->sibling)
405 if (of_node_get(next))
406 break;
407 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500408 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000409 return next;
410}
411EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000412
413/**
Timur Tabi32961932012-08-14 13:20:23 +0000414 * of_get_next_available_child - Find the next available child node
415 * @node: parent node
416 * @prev: previous child of the parent node, or NULL to get first
417 *
418 * This function is like of_get_next_child(), except that it
419 * automatically skips any disabled nodes (i.e. status = "disabled").
420 */
421struct device_node *of_get_next_available_child(const struct device_node *node,
422 struct device_node *prev)
423{
424 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000425 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000426
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000427 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000428 next = prev ? prev->sibling : node->child;
429 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700430 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000431 continue;
432 if (of_node_get(next))
433 break;
434 }
435 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000436 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000437 return next;
438}
439EXPORT_SYMBOL(of_get_next_available_child);
440
441/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100442 * of_get_child_by_name - Find the child node by name for a given parent
443 * @node: parent node
444 * @name: child name to look for.
445 *
446 * This function looks for child node for given matching name
447 *
448 * Returns a node pointer if found, with refcount incremented, use
449 * of_node_put() on it when done.
450 * Returns NULL if node is not found.
451 */
452struct device_node *of_get_child_by_name(const struct device_node *node,
453 const char *name)
454{
455 struct device_node *child;
456
457 for_each_child_of_node(node, child)
458 if (child->name && (of_node_cmp(child->name, name) == 0))
459 break;
460 return child;
461}
462EXPORT_SYMBOL(of_get_child_by_name);
463
464/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000465 * of_find_node_by_path - Find a node matching a full OF path
466 * @path: The full path to match
467 *
468 * Returns a node pointer with refcount incremented, use
469 * of_node_put() on it when done.
470 */
471struct device_node *of_find_node_by_path(const char *path)
472{
Randy Dunlap465aac62012-11-30 10:01:51 +0000473 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500474 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000475
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500476 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000477 for (; np; np = np->allnext) {
478 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
479 && of_node_get(np))
480 break;
481 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500482 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000483 return np;
484}
485EXPORT_SYMBOL(of_find_node_by_path);
486
487/**
488 * of_find_node_by_name - Find a node by its "name" property
489 * @from: The node to start searching from or NULL, the node
490 * you pass will not be searched, only the next one
491 * will; typically, you pass what the previous call
492 * returned. of_node_put() will be called on it
493 * @name: The name string to match against
494 *
495 * Returns a node pointer with refcount incremented, use
496 * of_node_put() on it when done.
497 */
498struct device_node *of_find_node_by_name(struct device_node *from,
499 const char *name)
500{
501 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500502 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000503
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500504 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000505 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000506 for (; np; np = np->allnext)
507 if (np->name && (of_node_cmp(np->name, name) == 0)
508 && of_node_get(np))
509 break;
510 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500511 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000512 return np;
513}
514EXPORT_SYMBOL(of_find_node_by_name);
515
516/**
517 * of_find_node_by_type - Find a node by its "device_type" property
518 * @from: The node to start searching from, or NULL to start searching
519 * the entire device tree. The node you pass will not be
520 * searched, only the next one will; typically, you pass
521 * what the previous call returned. of_node_put() will be
522 * called on from for you.
523 * @type: The type string to match against
524 *
525 * Returns a node pointer with refcount incremented, use
526 * of_node_put() on it when done.
527 */
528struct device_node *of_find_node_by_type(struct device_node *from,
529 const char *type)
530{
531 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500532 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000533
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500534 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000535 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000536 for (; np; np = np->allnext)
537 if (np->type && (of_node_cmp(np->type, type) == 0)
538 && of_node_get(np))
539 break;
540 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500541 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000542 return np;
543}
544EXPORT_SYMBOL(of_find_node_by_type);
545
546/**
547 * of_find_compatible_node - Find a node based on type and one of the
548 * tokens in its "compatible" property
549 * @from: The node to start searching from or NULL, the node
550 * you pass will not be searched, only the next one
551 * will; typically, you pass what the previous call
552 * returned. of_node_put() will be called on it
553 * @type: The type string to match "device_type" or NULL to ignore
554 * @compatible: The string to match to one of the tokens in the device
555 * "compatible" list.
556 *
557 * Returns a node pointer with refcount incremented, use
558 * of_node_put() on it when done.
559 */
560struct device_node *of_find_compatible_node(struct device_node *from,
561 const char *type, const char *compatible)
562{
563 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500564 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000565
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500566 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000567 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000568 for (; np; np = np->allnext) {
569 if (type
570 && !(np->type && (of_node_cmp(np->type, type) == 0)))
571 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500572 if (__of_device_is_compatible(np, compatible) &&
573 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000574 break;
575 }
576 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500577 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000578 return np;
579}
580EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100581
582/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000583 * of_find_node_with_property - Find a node which has a property with
584 * the given name.
585 * @from: The node to start searching from or NULL, the node
586 * you pass will not be searched, only the next one
587 * will; typically, you pass what the previous call
588 * returned. of_node_put() will be called on it
589 * @prop_name: The name of the property to look for.
590 *
591 * Returns a node pointer with refcount incremented, use
592 * of_node_put() on it when done.
593 */
594struct device_node *of_find_node_with_property(struct device_node *from,
595 const char *prop_name)
596{
597 struct device_node *np;
598 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500599 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000600
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500601 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000602 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000603 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530604 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000605 if (of_prop_cmp(pp->name, prop_name) == 0) {
606 of_node_get(np);
607 goto out;
608 }
609 }
610 }
611out:
612 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500613 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000614 return np;
615}
616EXPORT_SYMBOL(of_find_node_with_property);
617
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500618static
619const struct of_device_id *__of_match_node(const struct of_device_id *matches,
620 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100621{
Grant Likelya52f07e2011-03-18 10:21:29 -0600622 if (!matches)
623 return NULL;
624
Grant Likely283029d2008-01-09 06:20:40 +1100625 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
626 int match = 1;
627 if (matches->name[0])
628 match &= node->name
629 && !strcmp(matches->name, node->name);
630 if (matches->type[0])
631 match &= node->type
632 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700633 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500634 match &= __of_device_is_compatible(node,
635 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700636 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100637 return matches;
638 matches++;
639 }
640 return NULL;
641}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500642
643/**
644 * of_match_node - Tell if an device_node has a matching of_match structure
645 * @matches: array of of device match structures to search in
646 * @node: the of device structure to match against
647 *
648 * Low level utility function used by device matching.
649 */
650const struct of_device_id *of_match_node(const struct of_device_id *matches,
651 const struct device_node *node)
652{
653 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500654 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500655
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500656 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500657 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500658 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500659 return match;
660}
Grant Likely283029d2008-01-09 06:20:40 +1100661EXPORT_SYMBOL(of_match_node);
662
663/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700664 * of_find_matching_node_and_match - Find a node based on an of_device_id
665 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100666 * @from: The node to start searching from or NULL, the node
667 * you pass will not be searched, only the next one
668 * will; typically, you pass what the previous call
669 * returned. of_node_put() will be called on it
670 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700671 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100672 *
673 * Returns a node pointer with refcount incremented, use
674 * of_node_put() on it when done.
675 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700676struct device_node *of_find_matching_node_and_match(struct device_node *from,
677 const struct of_device_id *matches,
678 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100679{
680 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800681 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500682 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100683
Stephen Warren50c8af42012-11-20 16:12:20 -0700684 if (match)
685 *match = NULL;
686
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500687 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000688 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100689 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500690 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800691 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700692 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800693 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100694 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700695 }
Grant Likely283029d2008-01-09 06:20:40 +1100696 }
697 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500698 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100699 return np;
700}
Grant Likely80c20222012-12-19 10:45:36 +0000701EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400702
703/**
Grant Likely3f07af42008-07-25 22:25:13 -0400704 * of_modalias_node - Lookup appropriate modalias for a device node
705 * @node: pointer to a device tree node
706 * @modalias: Pointer to buffer that modalias value will be copied into
707 * @len: Length of modalias value
708 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600709 * Based on the value of the compatible property, this routine will attempt
710 * to choose an appropriate modalias value for a particular device tree node.
711 * It does this by stripping the manufacturer prefix (as delimited by a ',')
712 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400713 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600714 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400715 */
716int of_modalias_node(struct device_node *node, char *modalias, int len)
717{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600718 const char *compatible, *p;
719 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400720
721 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600722 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400723 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400724 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600725 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400726 return 0;
727}
728EXPORT_SYMBOL_GPL(of_modalias_node);
729
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000730/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700731 * of_find_node_by_phandle - Find a node given a phandle
732 * @handle: phandle of the node to find
733 *
734 * Returns a node pointer with refcount incremented, use
735 * of_node_put() on it when done.
736 */
737struct device_node *of_find_node_by_phandle(phandle handle)
738{
739 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000740 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -0700741
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000742 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000743 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700744 if (np->phandle == handle)
745 break;
746 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000747 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700748 return np;
749}
750EXPORT_SYMBOL(of_find_node_by_phandle);
751
752/**
Tony Priskdaeec1f2013-04-03 17:57:11 +1300753 * of_find_property_value_of_size
754 *
755 * @np: device node from which the property value is to be read.
756 * @propname: name of the property to be searched.
757 * @len: requested length of property value
758 *
759 * Search for a property in a device node and valid the requested size.
760 * Returns the property value on success, -EINVAL if the property does not
761 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
762 * property data isn't large enough.
763 *
764 */
765static void *of_find_property_value_of_size(const struct device_node *np,
766 const char *propname, u32 len)
767{
768 struct property *prop = of_find_property(np, propname, NULL);
769
770 if (!prop)
771 return ERR_PTR(-EINVAL);
772 if (!prop->value)
773 return ERR_PTR(-ENODATA);
774 if (len > prop->length)
775 return ERR_PTR(-EOVERFLOW);
776
777 return prop->value;
778}
779
780/**
Tony Prisk3daf3722013-03-23 17:02:15 +1300781 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
782 *
783 * @np: device node from which the property value is to be read.
784 * @propname: name of the property to be searched.
785 * @index: index of the u32 in the list of values
786 * @out_value: pointer to return value, modified only if no error.
787 *
788 * Search for a property in a device node and read nth 32-bit value from
789 * it. Returns 0 on success, -EINVAL if the property does not exist,
790 * -ENODATA if property does not have a value, and -EOVERFLOW if the
791 * property data isn't large enough.
792 *
793 * The out_value is modified only if a valid u32 value can be decoded.
794 */
795int of_property_read_u32_index(const struct device_node *np,
796 const char *propname,
797 u32 index, u32 *out_value)
798{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300799 const u32 *val = of_find_property_value_of_size(np, propname,
800 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +1300801
Tony Priskdaeec1f2013-04-03 17:57:11 +1300802 if (IS_ERR(val))
803 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +1300804
Tony Priskdaeec1f2013-04-03 17:57:11 +1300805 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +1300806 return 0;
807}
808EXPORT_SYMBOL_GPL(of_property_read_u32_index);
809
810/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530811 * of_property_read_u8_array - Find and read an array of u8 from a property.
812 *
813 * @np: device node from which the property value is to be read.
814 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530815 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530816 * @sz: number of array elements to read
817 *
818 * Search for a property in a device node and read 8-bit value(s) from
819 * it. Returns 0 on success, -EINVAL if the property does not exist,
820 * -ENODATA if property does not have a value, and -EOVERFLOW if the
821 * property data isn't large enough.
822 *
823 * dts entry of array should be like:
824 * property = /bits/ 8 <0x50 0x60 0x70>;
825 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530826 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +0530827 */
828int of_property_read_u8_array(const struct device_node *np,
829 const char *propname, u8 *out_values, size_t sz)
830{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300831 const u8 *val = of_find_property_value_of_size(np, propname,
832 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530833
Tony Priskdaeec1f2013-04-03 17:57:11 +1300834 if (IS_ERR(val))
835 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530836
Viresh Kumarbe193242012-11-20 10:15:19 +0530837 while (sz--)
838 *out_values++ = *val++;
839 return 0;
840}
841EXPORT_SYMBOL_GPL(of_property_read_u8_array);
842
843/**
844 * of_property_read_u16_array - Find and read an array of u16 from a property.
845 *
846 * @np: device node from which the property value is to be read.
847 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530848 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530849 * @sz: number of array elements to read
850 *
851 * Search for a property in a device node and read 16-bit value(s) from
852 * it. Returns 0 on success, -EINVAL if the property does not exist,
853 * -ENODATA if property does not have a value, and -EOVERFLOW if the
854 * property data isn't large enough.
855 *
856 * dts entry of array should be like:
857 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
858 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530859 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +0530860 */
861int of_property_read_u16_array(const struct device_node *np,
862 const char *propname, u16 *out_values, size_t sz)
863{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300864 const __be16 *val = of_find_property_value_of_size(np, propname,
865 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530866
Tony Priskdaeec1f2013-04-03 17:57:11 +1300867 if (IS_ERR(val))
868 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530869
Viresh Kumarbe193242012-11-20 10:15:19 +0530870 while (sz--)
871 *out_values++ = be16_to_cpup(val++);
872 return 0;
873}
874EXPORT_SYMBOL_GPL(of_property_read_u16_array);
875
876/**
Rob Herring0e373632011-07-06 15:42:58 -0500877 * of_property_read_u32_array - Find and read an array of 32 bit integers
878 * from a property.
879 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530880 * @np: device node from which the property value is to be read.
881 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530882 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530883 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530884 *
Rob Herring0e373632011-07-06 15:42:58 -0500885 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530886 * it. Returns 0 on success, -EINVAL if the property does not exist,
887 * -ENODATA if property does not have a value, and -EOVERFLOW if the
888 * property data isn't large enough.
889 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530890 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +0530891 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100892int of_property_read_u32_array(const struct device_node *np,
893 const char *propname, u32 *out_values,
894 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530895{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300896 const __be32 *val = of_find_property_value_of_size(np, propname,
897 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +0530898
Tony Priskdaeec1f2013-04-03 17:57:11 +1300899 if (IS_ERR(val))
900 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -0500901
Rob Herring0e373632011-07-06 15:42:58 -0500902 while (sz--)
903 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530904 return 0;
905}
Rob Herring0e373632011-07-06 15:42:58 -0500906EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530907
908/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100909 * of_property_read_u64 - Find and read a 64 bit integer from a property
910 * @np: device node from which the property value is to be read.
911 * @propname: name of the property to be searched.
912 * @out_value: pointer to return value, modified only if return value is 0.
913 *
914 * Search for a property in a device node and read a 64-bit value from
915 * it. Returns 0 on success, -EINVAL if the property does not exist,
916 * -ENODATA if property does not have a value, and -EOVERFLOW if the
917 * property data isn't large enough.
918 *
919 * The out_value is modified only if a valid u64 value can be decoded.
920 */
921int of_property_read_u64(const struct device_node *np, const char *propname,
922 u64 *out_value)
923{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300924 const __be32 *val = of_find_property_value_of_size(np, propname,
925 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100926
Tony Priskdaeec1f2013-04-03 17:57:11 +1300927 if (IS_ERR(val))
928 return PTR_ERR(val);
929
930 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100931 return 0;
932}
933EXPORT_SYMBOL_GPL(of_property_read_u64);
934
935/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530936 * of_property_read_string - Find and read a string from a property
937 * @np: device node from which the property value is to be read.
938 * @propname: name of the property to be searched.
939 * @out_string: pointer to null terminated return string, modified only if
940 * return value is 0.
941 *
942 * Search for a property in a device tree node and retrieve a null
943 * terminated string value (pointer to data, not a copy). Returns 0 on
944 * success, -EINVAL if the property does not exist, -ENODATA if property
945 * does not have a value, and -EILSEQ if the string is not null-terminated
946 * within the length of the property data.
947 *
948 * The out_string pointer is modified only if a valid string can be decoded.
949 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100950int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800951 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530952{
953 struct property *prop = of_find_property(np, propname, NULL);
954 if (!prop)
955 return -EINVAL;
956 if (!prop->value)
957 return -ENODATA;
958 if (strnlen(prop->value, prop->length) >= prop->length)
959 return -EILSEQ;
960 *out_string = prop->value;
961 return 0;
962}
963EXPORT_SYMBOL_GPL(of_property_read_string);
964
965/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200966 * of_property_read_string_index - Find and read a string from a multiple
967 * strings property.
968 * @np: device node from which the property value is to be read.
969 * @propname: name of the property to be searched.
970 * @index: index of the string in the list of strings
971 * @out_string: pointer to null terminated return string, modified only if
972 * return value is 0.
973 *
974 * Search for a property in a device tree node and retrieve a null
975 * terminated string value (pointer to data, not a copy) in the list of strings
976 * contained in that property.
977 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
978 * property does not have a value, and -EILSEQ if the string is not
979 * null-terminated within the length of the property data.
980 *
981 * The out_string pointer is modified only if a valid string can be decoded.
982 */
983int of_property_read_string_index(struct device_node *np, const char *propname,
984 int index, const char **output)
985{
986 struct property *prop = of_find_property(np, propname, NULL);
987 int i = 0;
988 size_t l = 0, total = 0;
989 const char *p;
990
991 if (!prop)
992 return -EINVAL;
993 if (!prop->value)
994 return -ENODATA;
995 if (strnlen(prop->value, prop->length) >= prop->length)
996 return -EILSEQ;
997
998 p = prop->value;
999
1000 for (i = 0; total < prop->length; total += l, p += l) {
1001 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001002 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001003 *output = p;
1004 return 0;
1005 }
1006 }
1007 return -ENODATA;
1008}
1009EXPORT_SYMBOL_GPL(of_property_read_string_index);
1010
Grant Likely7aff0fe2011-12-12 09:25:58 -07001011/**
1012 * of_property_match_string() - Find string in a list and return index
1013 * @np: pointer to node containing string list property
1014 * @propname: string list property name
1015 * @string: pointer to string to search for in string list
1016 *
1017 * This function searches a string list property and returns the index
1018 * of a specific string value.
1019 */
1020int of_property_match_string(struct device_node *np, const char *propname,
1021 const char *string)
1022{
1023 struct property *prop = of_find_property(np, propname, NULL);
1024 size_t l;
1025 int i;
1026 const char *p, *end;
1027
1028 if (!prop)
1029 return -EINVAL;
1030 if (!prop->value)
1031 return -ENODATA;
1032
1033 p = prop->value;
1034 end = p + prop->length;
1035
1036 for (i = 0; p < end; i++, p += l) {
1037 l = strlen(p) + 1;
1038 if (p + l > end)
1039 return -EILSEQ;
1040 pr_debug("comparing %s with %s\n", string, p);
1041 if (strcmp(string, p) == 0)
1042 return i; /* Found it; return index */
1043 }
1044 return -ENODATA;
1045}
1046EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001047
1048/**
1049 * of_property_count_strings - Find and return the number of strings from a
1050 * multiple strings property.
1051 * @np: device node from which the property value is to be read.
1052 * @propname: name of the property to be searched.
1053 *
1054 * Search for a property in a device tree node and retrieve the number of null
1055 * terminated string contain in it. Returns the number of strings on
1056 * success, -EINVAL if the property does not exist, -ENODATA if property
1057 * does not have a value, and -EILSEQ if the string is not null-terminated
1058 * within the length of the property data.
1059 */
1060int of_property_count_strings(struct device_node *np, const char *propname)
1061{
1062 struct property *prop = of_find_property(np, propname, NULL);
1063 int i = 0;
1064 size_t l = 0, total = 0;
1065 const char *p;
1066
1067 if (!prop)
1068 return -EINVAL;
1069 if (!prop->value)
1070 return -ENODATA;
1071 if (strnlen(prop->value, prop->length) >= prop->length)
1072 return -EILSEQ;
1073
1074 p = prop->value;
1075
Benoit Cousson88af7f52011-12-05 15:23:54 +01001076 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001077 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001078
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001079 return i;
1080}
1081EXPORT_SYMBOL_GPL(of_property_count_strings);
1082
Grant Likelybd69f732013-02-10 22:57:21 +00001083static int __of_parse_phandle_with_args(const struct device_node *np,
1084 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001085 const char *cells_name,
1086 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001087 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001088{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001089 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001090 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001091 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001092 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001093 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001094
Grant Likely15c9a0a2011-12-12 09:25:57 -07001095 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001096 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001097 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001098 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001099 list_end = list + size / sizeof(*list);
1100
Grant Likely15c9a0a2011-12-12 09:25:57 -07001101 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001102 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001103 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001104 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001105
Grant Likely15c9a0a2011-12-12 09:25:57 -07001106 /*
1107 * If phandle is 0, then it is an empty entry with no
1108 * arguments. Skip forward to the next entry.
1109 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001110 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001111 if (phandle) {
1112 /*
1113 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001114 * property to determine the argument length.
1115 *
1116 * This is not needed if the cell count is hard-coded
1117 * (i.e. cells_name not set, but cell_count is set),
1118 * except when we're going to return the found node
1119 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001120 */
Stephen Warren91d99422013-08-14 15:27:11 -06001121 if (cells_name || cur_index == index) {
1122 node = of_find_node_by_phandle(phandle);
1123 if (!node) {
1124 pr_err("%s: could not find phandle\n",
1125 np->full_name);
1126 goto err;
1127 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001128 }
Stephen Warren035fd942013-08-14 15:27:10 -06001129
1130 if (cells_name) {
1131 if (of_property_read_u32(node, cells_name,
1132 &count)) {
1133 pr_err("%s: could not get %s for %s\n",
1134 np->full_name, cells_name,
1135 node->full_name);
1136 goto err;
1137 }
1138 } else {
1139 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001140 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001141
Grant Likely15c9a0a2011-12-12 09:25:57 -07001142 /*
1143 * Make sure that the arguments actually fit in the
1144 * remaining property data length
1145 */
1146 if (list + count > list_end) {
1147 pr_err("%s: arguments longer than property\n",
1148 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001149 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001150 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001151 }
1152
Grant Likely15c9a0a2011-12-12 09:25:57 -07001153 /*
1154 * All of the error cases above bail out of the loop, so at
1155 * this point, the parsing is successful. If the requested
1156 * index matches, then fill the out_args structure and return,
1157 * or return -ENOENT for an empty entry.
1158 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001159 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001160 if (cur_index == index) {
1161 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001162 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001163
Grant Likely15c9a0a2011-12-12 09:25:57 -07001164 if (out_args) {
1165 int i;
1166 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1167 count = MAX_PHANDLE_ARGS;
1168 out_args->np = node;
1169 out_args->args_count = count;
1170 for (i = 0; i < count; i++)
1171 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001172 } else {
1173 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001174 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001175
1176 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001177 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001178 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001179
1180 of_node_put(node);
1181 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001182 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001183 cur_index++;
1184 }
1185
Grant Likely23ce04c2013-02-12 21:21:49 +00001186 /*
1187 * Unlock node before returning result; will be one of:
1188 * -ENOENT : index is for empty phandle
1189 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001190 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001191 */
Grant Likelybd69f732013-02-10 22:57:21 +00001192 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001193 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001194 if (node)
1195 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001196 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001197}
Grant Likelybd69f732013-02-10 22:57:21 +00001198
Stephen Warreneded9dd2013-08-14 15:27:08 -06001199/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001200 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1201 * @np: Pointer to device node holding phandle property
1202 * @phandle_name: Name of property holding a phandle value
1203 * @index: For properties holding a table of phandles, this is the index into
1204 * the table
1205 *
1206 * Returns the device_node pointer with refcount incremented. Use
1207 * of_node_put() on it when done.
1208 */
1209struct device_node *of_parse_phandle(const struct device_node *np,
1210 const char *phandle_name, int index)
1211{
Stephen Warren91d99422013-08-14 15:27:11 -06001212 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001213
Stephen Warren91d99422013-08-14 15:27:11 -06001214 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001215 return NULL;
1216
Stephen Warren91d99422013-08-14 15:27:11 -06001217 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1218 index, &args))
1219 return NULL;
1220
1221 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001222}
1223EXPORT_SYMBOL(of_parse_phandle);
1224
1225/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001226 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1227 * @np: pointer to a device tree node containing a list
1228 * @list_name: property name that contains a list
1229 * @cells_name: property name that specifies phandles' arguments count
1230 * @index: index of a phandle to parse out
1231 * @out_args: optional pointer to output arguments structure (will be filled)
1232 *
1233 * This function is useful to parse lists of phandles and their arguments.
1234 * Returns 0 on success and fills out_args, on error returns appropriate
1235 * errno value.
1236 *
1237 * Caller is responsible to call of_node_put() on the returned out_args->node
1238 * pointer.
1239 *
1240 * Example:
1241 *
1242 * phandle1: node1 {
1243 * #list-cells = <2>;
1244 * }
1245 *
1246 * phandle2: node2 {
1247 * #list-cells = <1>;
1248 * }
1249 *
1250 * node3 {
1251 * list = <&phandle1 1 2 &phandle2 3>;
1252 * }
1253 *
1254 * To get a device_node of the `node2' node you may call this:
1255 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1256 */
Grant Likelybd69f732013-02-10 22:57:21 +00001257int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1258 const char *cells_name, int index,
1259 struct of_phandle_args *out_args)
1260{
1261 if (index < 0)
1262 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001263 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1264 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001265}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001266EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001267
Grant Likelybd69f732013-02-10 22:57:21 +00001268/**
Stephen Warren035fd942013-08-14 15:27:10 -06001269 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1270 * @np: pointer to a device tree node containing a list
1271 * @list_name: property name that contains a list
1272 * @cell_count: number of argument cells following the phandle
1273 * @index: index of a phandle to parse out
1274 * @out_args: optional pointer to output arguments structure (will be filled)
1275 *
1276 * This function is useful to parse lists of phandles and their arguments.
1277 * Returns 0 on success and fills out_args, on error returns appropriate
1278 * errno value.
1279 *
1280 * Caller is responsible to call of_node_put() on the returned out_args->node
1281 * pointer.
1282 *
1283 * Example:
1284 *
1285 * phandle1: node1 {
1286 * }
1287 *
1288 * phandle2: node2 {
1289 * }
1290 *
1291 * node3 {
1292 * list = <&phandle1 0 2 &phandle2 2 3>;
1293 * }
1294 *
1295 * To get a device_node of the `node2' node you may call this:
1296 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1297 */
1298int of_parse_phandle_with_fixed_args(const struct device_node *np,
1299 const char *list_name, int cell_count,
1300 int index, struct of_phandle_args *out_args)
1301{
1302 if (index < 0)
1303 return -EINVAL;
1304 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1305 index, out_args);
1306}
1307EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1308
1309/**
Grant Likelybd69f732013-02-10 22:57:21 +00001310 * of_count_phandle_with_args() - Find the number of phandles references in a property
1311 * @np: pointer to a device tree node containing a list
1312 * @list_name: property name that contains a list
1313 * @cells_name: property name that specifies phandles' arguments count
1314 *
1315 * Returns the number of phandle + argument tuples within a property. It
1316 * is a typical pattern to encode a list of phandle and variable
1317 * arguments into a single property. The number of arguments is encoded
1318 * by a property in the phandle-target node. For example, a gpios
1319 * property would contain a list of GPIO specifies consisting of a
1320 * phandle and 1 or more arguments. The number of arguments are
1321 * determined by the #gpio-cells property in the node pointed to by the
1322 * phandle.
1323 */
1324int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1325 const char *cells_name)
1326{
Stephen Warren035fd942013-08-14 15:27:10 -06001327 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1328 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001329}
1330EXPORT_SYMBOL(of_count_phandle_with_args);
1331
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001332#if defined(CONFIG_OF_DYNAMIC)
1333static int of_property_notify(int action, struct device_node *np,
1334 struct property *prop)
1335{
1336 struct of_prop_reconfig pr;
1337
1338 pr.dn = np;
1339 pr.prop = prop;
1340 return of_reconfig_notify(action, &pr);
1341}
1342#else
1343static int of_property_notify(int action, struct device_node *np,
1344 struct property *prop)
1345{
1346 return 0;
1347}
1348#endif
1349
Grant Likely02af11b2009-11-23 20:16:45 -07001350/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001351 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001352 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001353int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001354{
1355 struct property **next;
1356 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001357 int rc;
1358
1359 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1360 if (rc)
1361 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001362
1363 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001364 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001365 next = &np->properties;
1366 while (*next) {
1367 if (strcmp(prop->name, (*next)->name) == 0) {
1368 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001369 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001370 return -1;
1371 }
1372 next = &(*next)->next;
1373 }
1374 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001375 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001376
1377#ifdef CONFIG_PROC_DEVICETREE
1378 /* try to add to proc as well if it was initialized */
1379 if (np->pde)
1380 proc_device_tree_add_prop(np->pde, prop);
1381#endif /* CONFIG_PROC_DEVICETREE */
1382
1383 return 0;
1384}
1385
1386/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001387 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001388 *
1389 * Note that we don't actually remove it, since we have given out
1390 * who-knows-how-many pointers to the data using get-property.
1391 * Instead we just move the property to the "dead properties"
1392 * list, so it won't be found any more.
1393 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001394int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001395{
1396 struct property **next;
1397 unsigned long flags;
1398 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001399 int rc;
1400
1401 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1402 if (rc)
1403 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001404
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001405 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001406 next = &np->properties;
1407 while (*next) {
1408 if (*next == prop) {
1409 /* found the node */
1410 *next = prop->next;
1411 prop->next = np->deadprops;
1412 np->deadprops = prop;
1413 found = 1;
1414 break;
1415 }
1416 next = &(*next)->next;
1417 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001418 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001419
1420 if (!found)
1421 return -ENODEV;
1422
1423#ifdef CONFIG_PROC_DEVICETREE
1424 /* try to remove the proc node as well */
1425 if (np->pde)
1426 proc_device_tree_remove_prop(np->pde, prop);
1427#endif /* CONFIG_PROC_DEVICETREE */
1428
1429 return 0;
1430}
1431
1432/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001433 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001434 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001435 *
1436 * Note that we don't actually remove it, since we have given out
1437 * who-knows-how-many pointers to the data using get-property.
1438 * Instead we just move the property to the "dead properties" list,
1439 * and add the new property to the property list
1440 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001441int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001442{
Dong Aisheng475d0092012-07-11 15:16:37 +10001443 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001444 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001445 int rc, found = 0;
1446
1447 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1448 if (rc)
1449 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001450
Dong Aisheng475d0092012-07-11 15:16:37 +10001451 if (!newprop->name)
1452 return -EINVAL;
1453
1454 oldprop = of_find_property(np, newprop->name, NULL);
1455 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001456 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001457
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001458 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001459 next = &np->properties;
1460 while (*next) {
1461 if (*next == oldprop) {
1462 /* found the node */
1463 newprop->next = oldprop->next;
1464 *next = newprop;
1465 oldprop->next = np->deadprops;
1466 np->deadprops = oldprop;
1467 found = 1;
1468 break;
1469 }
1470 next = &(*next)->next;
1471 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001472 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001473
1474 if (!found)
1475 return -ENODEV;
1476
1477#ifdef CONFIG_PROC_DEVICETREE
1478 /* try to add to proc as well if it was initialized */
1479 if (np->pde)
1480 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1481#endif /* CONFIG_PROC_DEVICETREE */
1482
1483 return 0;
1484}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001485
1486#if defined(CONFIG_OF_DYNAMIC)
1487/*
1488 * Support for dynamic device trees.
1489 *
1490 * On some platforms, the device tree can be manipulated at runtime.
1491 * The routines in this section support adding, removing and changing
1492 * device tree nodes.
1493 */
1494
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001495static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1496
1497int of_reconfig_notifier_register(struct notifier_block *nb)
1498{
1499 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1500}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001501EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001502
1503int of_reconfig_notifier_unregister(struct notifier_block *nb)
1504{
1505 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1506}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001507EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001508
1509int of_reconfig_notify(unsigned long action, void *p)
1510{
1511 int rc;
1512
1513 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1514 return notifier_to_errno(rc);
1515}
1516
Nathan Fontenote81b3292012-10-02 16:55:01 +00001517#ifdef CONFIG_PROC_DEVICETREE
1518static void of_add_proc_dt_entry(struct device_node *dn)
1519{
1520 struct proc_dir_entry *ent;
1521
1522 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1523 if (ent)
1524 proc_device_tree_add_node(dn, ent);
1525}
1526#else
1527static void of_add_proc_dt_entry(struct device_node *dn)
1528{
1529 return;
1530}
1531#endif
1532
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001533/**
1534 * of_attach_node - Plug a device node into the tree and global list.
1535 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001536int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001537{
1538 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001539 int rc;
1540
1541 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1542 if (rc)
1543 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001544
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001545 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001546 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001547 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001548 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001549 of_allnodes = np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001550 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001551
1552 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001553 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001554}
1555
Nathan Fontenote81b3292012-10-02 16:55:01 +00001556#ifdef CONFIG_PROC_DEVICETREE
1557static void of_remove_proc_dt_entry(struct device_node *dn)
1558{
David Howellsa8ca16e2013-04-12 17:27:28 +01001559 proc_remove(dn->pde);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001560}
1561#else
1562static void of_remove_proc_dt_entry(struct device_node *dn)
1563{
1564 return;
1565}
1566#endif
1567
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001568/**
1569 * of_detach_node - "Unplug" a node from the device tree.
1570 *
1571 * The caller must hold a reference to the node. The memory associated with
1572 * the node is not freed until its refcount goes to zero.
1573 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001574int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001575{
1576 struct device_node *parent;
1577 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001578 int rc = 0;
1579
1580 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1581 if (rc)
1582 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001583
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001584 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001585
Nathan Fontenote81b3292012-10-02 16:55:01 +00001586 if (of_node_check_flag(np, OF_DETACHED)) {
1587 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001588 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001589 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001590 }
1591
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001592 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001593 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001594 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001595 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001596 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001597
Randy Dunlap465aac62012-11-30 10:01:51 +00001598 if (of_allnodes == np)
1599 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001600 else {
1601 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001602 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001603 prev->allnext != np;
1604 prev = prev->allnext)
1605 ;
1606 prev->allnext = np->allnext;
1607 }
1608
1609 if (parent->child == np)
1610 parent->child = np->sibling;
1611 else {
1612 struct device_node *prevsib;
1613 for (prevsib = np->parent->child;
1614 prevsib->sibling != np;
1615 prevsib = prevsib->sibling)
1616 ;
1617 prevsib->sibling = np->sibling;
1618 }
1619
1620 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001621 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001622
1623 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001624 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001625}
1626#endif /* defined(CONFIG_OF_DYNAMIC) */
1627
Shawn Guo611cad72011-08-15 15:28:14 +08001628static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1629 int id, const char *stem, int stem_len)
1630{
1631 ap->np = np;
1632 ap->id = id;
1633 strncpy(ap->stem, stem, stem_len);
1634 ap->stem[stem_len] = 0;
1635 list_add_tail(&ap->link, &aliases_lookup);
1636 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001637 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001638}
1639
1640/**
1641 * of_alias_scan - Scan all properties of 'aliases' node
1642 *
1643 * The function scans all the properties of 'aliases' node and populate
1644 * the the global lookup table with the properties. It returns the
1645 * number of alias_prop found, or error code in error case.
1646 *
1647 * @dt_alloc: An allocator that provides a virtual address to memory
1648 * for the resulting tree
1649 */
1650void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1651{
1652 struct property *pp;
1653
1654 of_chosen = of_find_node_by_path("/chosen");
1655 if (of_chosen == NULL)
1656 of_chosen = of_find_node_by_path("/chosen@0");
1657 of_aliases = of_find_node_by_path("/aliases");
1658 if (!of_aliases)
1659 return;
1660
Dong Aisheng8af0da92011-12-22 20:19:24 +08001661 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001662 const char *start = pp->name;
1663 const char *end = start + strlen(start);
1664 struct device_node *np;
1665 struct alias_prop *ap;
1666 int id, len;
1667
1668 /* Skip those we do not want to proceed */
1669 if (!strcmp(pp->name, "name") ||
1670 !strcmp(pp->name, "phandle") ||
1671 !strcmp(pp->name, "linux,phandle"))
1672 continue;
1673
1674 np = of_find_node_by_path(pp->value);
1675 if (!np)
1676 continue;
1677
1678 /* walk the alias backwards to extract the id and work out
1679 * the 'stem' string */
1680 while (isdigit(*(end-1)) && end > start)
1681 end--;
1682 len = end - start;
1683
1684 if (kstrtoint(end, 10, &id) < 0)
1685 continue;
1686
1687 /* Allocate an alias_prop with enough space for the stem */
1688 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1689 if (!ap)
1690 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001691 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001692 ap->alias = start;
1693 of_alias_add(ap, np, id, start, len);
1694 }
1695}
1696
1697/**
1698 * of_alias_get_id - Get alias id for the given device_node
1699 * @np: Pointer to the given device_node
1700 * @stem: Alias stem of the given device_node
1701 *
1702 * The function travels the lookup table to get alias id for the given
1703 * device_node and alias stem. It returns the alias id if find it.
1704 */
1705int of_alias_get_id(struct device_node *np, const char *stem)
1706{
1707 struct alias_prop *app;
1708 int id = -ENODEV;
1709
1710 mutex_lock(&of_aliases_mutex);
1711 list_for_each_entry(app, &aliases_lookup, link) {
1712 if (strcmp(app->stem, stem) != 0)
1713 continue;
1714
1715 if (np == app->np) {
1716 id = app->id;
1717 break;
1718 }
1719 }
1720 mutex_unlock(&of_aliases_mutex);
1721
1722 return id;
1723}
1724EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001725
1726const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1727 u32 *pu)
1728{
1729 const void *curv = cur;
1730
1731 if (!prop)
1732 return NULL;
1733
1734 if (!cur) {
1735 curv = prop->value;
1736 goto out_val;
1737 }
1738
1739 curv += sizeof(*cur);
1740 if (curv >= prop->value + prop->length)
1741 return NULL;
1742
1743out_val:
1744 *pu = be32_to_cpup(curv);
1745 return curv;
1746}
1747EXPORT_SYMBOL_GPL(of_prop_next_u32);
1748
1749const char *of_prop_next_string(struct property *prop, const char *cur)
1750{
1751 const void *curv = cur;
1752
1753 if (!prop)
1754 return NULL;
1755
1756 if (!cur)
1757 return prop->value;
1758
1759 curv += strlen(cur) + 1;
1760 if (curv >= prop->value + prop->length)
1761 return NULL;
1762
1763 return curv;
1764}
1765EXPORT_SYMBOL_GPL(of_prop_next_string);