blob: a181a61b222dea9ef6704044241ba35830804824 [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;
Sascha Hauerca257782013-08-05 14:40:44 +020035static struct device_node *of_stdout;
Shawn Guo611cad72011-08-15 15:28:14 +080036
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080037DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100038
Stephen Rothwell581b6052007-04-24 16:46:53 +100039/* use when traversing tree through the allnext, child, sibling,
40 * or parent members of struct device_node.
41 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050042DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100043
44int of_n_addr_cells(struct device_node *np)
45{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060046 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100047
48 do {
49 if (np->parent)
50 np = np->parent;
51 ip = of_get_property(np, "#address-cells", NULL);
52 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070053 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100054 } while (np->parent);
55 /* No #address-cells property for the root node */
56 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
57}
58EXPORT_SYMBOL(of_n_addr_cells);
59
60int of_n_size_cells(struct device_node *np)
61{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060062 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100063
64 do {
65 if (np->parent)
66 np = np->parent;
67 ip = of_get_property(np, "#size-cells", NULL);
68 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070069 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100070 } while (np->parent);
71 /* No #size-cells property for the root node */
72 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
73}
74EXPORT_SYMBOL(of_n_size_cells);
75
Grant Likely0f22dd32012-02-15 20:38:40 -070076#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070077/**
78 * of_node_get - Increment refcount of a node
79 * @node: Node to inc refcount, NULL is supported to
80 * simplify writing of callers
81 *
82 * Returns node.
83 */
84struct device_node *of_node_get(struct device_node *node)
85{
86 if (node)
87 kref_get(&node->kref);
88 return node;
89}
90EXPORT_SYMBOL(of_node_get);
91
92static inline struct device_node *kref_to_device_node(struct kref *kref)
93{
94 return container_of(kref, struct device_node, kref);
95}
96
97/**
98 * of_node_release - release a dynamically allocated node
99 * @kref: kref element of the node to be released
100 *
101 * In of_node_put() this function is passed to kref_put()
102 * as the destructor.
103 */
104static void of_node_release(struct kref *kref)
105{
106 struct device_node *node = kref_to_device_node(kref);
107 struct property *prop = node->properties;
108
109 /* We should never be releasing nodes that haven't been detached. */
110 if (!of_node_check_flag(node, OF_DETACHED)) {
111 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
112 dump_stack();
113 kref_init(&node->kref);
114 return;
115 }
116
117 if (!of_node_check_flag(node, OF_DYNAMIC))
118 return;
119
120 while (prop) {
121 struct property *next = prop->next;
122 kfree(prop->name);
123 kfree(prop->value);
124 kfree(prop);
125 prop = next;
126
127 if (!prop) {
128 prop = node->deadprops;
129 node->deadprops = NULL;
130 }
131 }
132 kfree(node->full_name);
133 kfree(node->data);
134 kfree(node);
135}
136
137/**
138 * of_node_put - Decrement refcount of a node
139 * @node: Node to dec refcount, NULL is supported to
140 * simplify writing of callers
141 *
142 */
143void of_node_put(struct device_node *node)
144{
145 if (node)
146 kref_put(&node->kref, of_node_release);
147}
148EXPORT_SYMBOL(of_node_put);
Grant Likely0f22dd32012-02-15 20:38:40 -0700149#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700150
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500151static struct property *__of_find_property(const struct device_node *np,
152 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000153{
154 struct property *pp;
155
Timur Tabi64e45662008-05-08 05:19:59 +1000156 if (!np)
157 return NULL;
158
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530159 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000160 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530161 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000162 *lenp = pp->length;
163 break;
164 }
165 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500166
167 return pp;
168}
169
170struct property *of_find_property(const struct device_node *np,
171 const char *name,
172 int *lenp)
173{
174 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500175 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500176
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500177 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500178 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500179 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000180
181 return pp;
182}
183EXPORT_SYMBOL(of_find_property);
184
Grant Likelye91edcf2009-10-15 10:58:09 -0600185/**
186 * of_find_all_nodes - Get next node in global list
187 * @prev: Previous node or NULL to start iteration
188 * of_node_put() will be called on it
189 *
190 * Returns a node pointer with refcount incremented, use
191 * of_node_put() on it when done.
192 */
193struct device_node *of_find_all_nodes(struct device_node *prev)
194{
195 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000196 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600197
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000198 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000199 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600200 for (; np != NULL; np = np->allnext)
201 if (of_node_get(np))
202 break;
203 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000204 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600205 return np;
206}
207EXPORT_SYMBOL(of_find_all_nodes);
208
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000209/*
210 * Find a property with a given name for a given node
211 * and return the value.
212 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500213static const void *__of_get_property(const struct device_node *np,
214 const char *name, int *lenp)
215{
216 struct property *pp = __of_find_property(np, name, lenp);
217
218 return pp ? pp->value : NULL;
219}
220
221/*
222 * Find a property with a given name for a given node
223 * and return the value.
224 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000225const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500226 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000227{
228 struct property *pp = of_find_property(np, name, lenp);
229
230 return pp ? pp->value : NULL;
231}
232EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000233
234/** Checks if the given "compat" string matches one of the strings in
235 * the device's "compatible" property
236 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500237static int __of_device_is_compatible(const struct device_node *device,
238 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000239{
240 const char* cp;
241 int cplen, l;
242
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500243 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000244 if (cp == NULL)
245 return 0;
246 while (cplen > 0) {
247 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
248 return 1;
249 l = strlen(cp) + 1;
250 cp += l;
251 cplen -= l;
252 }
253
254 return 0;
255}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500256
257/** Checks if the given "compat" string matches one of the strings in
258 * the device's "compatible" property
259 */
260int of_device_is_compatible(const struct device_node *device,
261 const char *compat)
262{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500263 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500264 int res;
265
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500266 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500267 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500268 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500269 return res;
270}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000271EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000272
273/**
Grant Likely71a157e2010-02-01 21:34:14 -0700274 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700275 * @compat: compatible string to look for in root node's compatible property.
276 *
277 * Returns true if the root node has the given value in its
278 * compatible property.
279 */
Grant Likely71a157e2010-02-01 21:34:14 -0700280int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700281{
282 struct device_node *root;
283 int rc = 0;
284
285 root = of_find_node_by_path("/");
286 if (root) {
287 rc = of_device_is_compatible(root, compat);
288 of_node_put(root);
289 }
290 return rc;
291}
Grant Likely71a157e2010-02-01 21:34:14 -0700292EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700293
294/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700295 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100296 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700297 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100298 *
299 * Returns 1 if the status property is absent or set to "okay" or "ok",
300 * 0 otherwise
301 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700302static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100303{
304 const char *status;
305 int statlen;
306
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700307 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100308 if (status == NULL)
309 return 1;
310
311 if (statlen > 0) {
312 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
313 return 1;
314 }
315
316 return 0;
317}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700318
319/**
320 * of_device_is_available - check if a device is available for use
321 *
322 * @device: Node to check for availability
323 *
324 * Returns 1 if the status property is absent or set to "okay" or "ok",
325 * 0 otherwise
326 */
327int of_device_is_available(const struct device_node *device)
328{
329 unsigned long flags;
330 int res;
331
332 raw_spin_lock_irqsave(&devtree_lock, flags);
333 res = __of_device_is_available(device);
334 raw_spin_unlock_irqrestore(&devtree_lock, flags);
335 return res;
336
337}
Josh Boyer834d97d2008-03-27 00:33:14 +1100338EXPORT_SYMBOL(of_device_is_available);
339
340/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000341 * of_get_parent - Get a node's parent if any
342 * @node: Node to get parent
343 *
344 * Returns a node pointer with refcount incremented, use
345 * of_node_put() on it when done.
346 */
347struct device_node *of_get_parent(const struct device_node *node)
348{
349 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500350 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000351
352 if (!node)
353 return NULL;
354
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500355 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000356 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500357 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000358 return np;
359}
360EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000361
362/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000363 * of_get_next_parent - Iterate to a node's parent
364 * @node: Node to get parent of
365 *
366 * This is like of_get_parent() except that it drops the
367 * refcount on the passed node, making it suitable for iterating
368 * through a node's parents.
369 *
370 * Returns a node pointer with refcount incremented, use
371 * of_node_put() on it when done.
372 */
373struct device_node *of_get_next_parent(struct device_node *node)
374{
375 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500376 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000377
378 if (!node)
379 return NULL;
380
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500381 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000382 parent = of_node_get(node->parent);
383 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500384 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000385 return parent;
386}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300387EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000388
389/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000390 * of_get_next_child - Iterate a node childs
391 * @node: parent node
392 * @prev: previous child of the parent node, or NULL to get first
393 *
394 * Returns a node pointer with refcount incremented, use
395 * of_node_put() on it when done.
396 */
397struct device_node *of_get_next_child(const struct device_node *node,
398 struct device_node *prev)
399{
400 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500401 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000402
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500403 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000404 next = prev ? prev->sibling : node->child;
405 for (; next; next = next->sibling)
406 if (of_node_get(next))
407 break;
408 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500409 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000410 return next;
411}
412EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000413
414/**
Timur Tabi32961932012-08-14 13:20:23 +0000415 * of_get_next_available_child - Find the next available child node
416 * @node: parent node
417 * @prev: previous child of the parent node, or NULL to get first
418 *
419 * This function is like of_get_next_child(), except that it
420 * automatically skips any disabled nodes (i.e. status = "disabled").
421 */
422struct device_node *of_get_next_available_child(const struct device_node *node,
423 struct device_node *prev)
424{
425 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000426 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000427
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000428 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000429 next = prev ? prev->sibling : node->child;
430 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700431 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000432 continue;
433 if (of_node_get(next))
434 break;
435 }
436 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000437 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000438 return next;
439}
440EXPORT_SYMBOL(of_get_next_available_child);
441
442/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100443 * of_get_child_by_name - Find the child node by name for a given parent
444 * @node: parent node
445 * @name: child name to look for.
446 *
447 * This function looks for child node for given matching name
448 *
449 * Returns a node pointer if found, with refcount incremented, use
450 * of_node_put() on it when done.
451 * Returns NULL if node is not found.
452 */
453struct device_node *of_get_child_by_name(const struct device_node *node,
454 const char *name)
455{
456 struct device_node *child;
457
458 for_each_child_of_node(node, child)
459 if (child->name && (of_node_cmp(child->name, name) == 0))
460 break;
461 return child;
462}
463EXPORT_SYMBOL(of_get_child_by_name);
464
465/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000466 * of_find_node_by_path - Find a node matching a full OF path
467 * @path: The full path to match
468 *
469 * Returns a node pointer with refcount incremented, use
470 * of_node_put() on it when done.
471 */
472struct device_node *of_find_node_by_path(const char *path)
473{
Randy Dunlap465aac62012-11-30 10:01:51 +0000474 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500475 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000476
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500477 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000478 for (; np; np = np->allnext) {
479 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
480 && of_node_get(np))
481 break;
482 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500483 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000484 return np;
485}
486EXPORT_SYMBOL(of_find_node_by_path);
487
488/**
489 * of_find_node_by_name - Find a node by its "name" property
490 * @from: The node to start searching from or NULL, the node
491 * you pass will not be searched, only the next one
492 * will; typically, you pass what the previous call
493 * returned. of_node_put() will be called on it
494 * @name: The name string to match against
495 *
496 * Returns a node pointer with refcount incremented, use
497 * of_node_put() on it when done.
498 */
499struct device_node *of_find_node_by_name(struct device_node *from,
500 const char *name)
501{
502 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500503 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000504
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500505 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000506 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000507 for (; np; np = np->allnext)
508 if (np->name && (of_node_cmp(np->name, name) == 0)
509 && of_node_get(np))
510 break;
511 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500512 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000513 return np;
514}
515EXPORT_SYMBOL(of_find_node_by_name);
516
517/**
518 * of_find_node_by_type - Find a node by its "device_type" property
519 * @from: The node to start searching from, or NULL to start searching
520 * the entire device tree. The node you pass will not be
521 * searched, only the next one will; typically, you pass
522 * what the previous call returned. of_node_put() will be
523 * called on from for you.
524 * @type: The type string to match against
525 *
526 * Returns a node pointer with refcount incremented, use
527 * of_node_put() on it when done.
528 */
529struct device_node *of_find_node_by_type(struct device_node *from,
530 const char *type)
531{
532 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500533 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000534
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500535 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000536 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000537 for (; np; np = np->allnext)
538 if (np->type && (of_node_cmp(np->type, type) == 0)
539 && of_node_get(np))
540 break;
541 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500542 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000543 return np;
544}
545EXPORT_SYMBOL(of_find_node_by_type);
546
547/**
548 * of_find_compatible_node - Find a node based on type and one of the
549 * tokens in its "compatible" property
550 * @from: The node to start searching from or NULL, the node
551 * you pass will not be searched, only the next one
552 * will; typically, you pass what the previous call
553 * returned. of_node_put() will be called on it
554 * @type: The type string to match "device_type" or NULL to ignore
555 * @compatible: The string to match to one of the tokens in the device
556 * "compatible" list.
557 *
558 * Returns a node pointer with refcount incremented, use
559 * of_node_put() on it when done.
560 */
561struct device_node *of_find_compatible_node(struct device_node *from,
562 const char *type, const char *compatible)
563{
564 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500565 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000566
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500567 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000568 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000569 for (; np; np = np->allnext) {
570 if (type
571 && !(np->type && (of_node_cmp(np->type, type) == 0)))
572 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500573 if (__of_device_is_compatible(np, compatible) &&
574 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000575 break;
576 }
577 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500578 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000579 return np;
580}
581EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100582
583/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000584 * of_find_node_with_property - Find a node which has a property with
585 * the given name.
586 * @from: The node to start searching from or NULL, the node
587 * you pass will not be searched, only the next one
588 * will; typically, you pass what the previous call
589 * returned. of_node_put() will be called on it
590 * @prop_name: The name of the property to look for.
591 *
592 * Returns a node pointer with refcount incremented, use
593 * of_node_put() on it when done.
594 */
595struct device_node *of_find_node_with_property(struct device_node *from,
596 const char *prop_name)
597{
598 struct device_node *np;
599 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500600 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000601
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500602 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000603 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000604 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530605 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000606 if (of_prop_cmp(pp->name, prop_name) == 0) {
607 of_node_get(np);
608 goto out;
609 }
610 }
611 }
612out:
613 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500614 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000615 return np;
616}
617EXPORT_SYMBOL(of_find_node_with_property);
618
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500619static
620const struct of_device_id *__of_match_node(const struct of_device_id *matches,
621 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100622{
Grant Likelya52f07e2011-03-18 10:21:29 -0600623 if (!matches)
624 return NULL;
625
Grant Likely283029d2008-01-09 06:20:40 +1100626 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
627 int match = 1;
628 if (matches->name[0])
629 match &= node->name
630 && !strcmp(matches->name, node->name);
631 if (matches->type[0])
632 match &= node->type
633 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700634 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500635 match &= __of_device_is_compatible(node,
636 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700637 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100638 return matches;
639 matches++;
640 }
641 return NULL;
642}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500643
644/**
645 * of_match_node - Tell if an device_node has a matching of_match structure
646 * @matches: array of of device match structures to search in
647 * @node: the of device structure to match against
648 *
649 * Low level utility function used by device matching.
650 */
651const struct of_device_id *of_match_node(const struct of_device_id *matches,
652 const struct device_node *node)
653{
654 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500655 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500656
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500657 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500658 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500659 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500660 return match;
661}
Grant Likely283029d2008-01-09 06:20:40 +1100662EXPORT_SYMBOL(of_match_node);
663
664/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700665 * of_find_matching_node_and_match - Find a node based on an of_device_id
666 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100667 * @from: The node to start searching from or NULL, the node
668 * you pass will not be searched, only the next one
669 * will; typically, you pass what the previous call
670 * returned. of_node_put() will be called on it
671 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700672 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100673 *
674 * Returns a node pointer with refcount incremented, use
675 * of_node_put() on it when done.
676 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700677struct device_node *of_find_matching_node_and_match(struct device_node *from,
678 const struct of_device_id *matches,
679 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100680{
681 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800682 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500683 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100684
Stephen Warren50c8af42012-11-20 16:12:20 -0700685 if (match)
686 *match = NULL;
687
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500688 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000689 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100690 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500691 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800692 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700693 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800694 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100695 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700696 }
Grant Likely283029d2008-01-09 06:20:40 +1100697 }
698 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500699 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100700 return np;
701}
Grant Likely80c20222012-12-19 10:45:36 +0000702EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400703
704/**
Grant Likely3f07af42008-07-25 22:25:13 -0400705 * of_modalias_node - Lookup appropriate modalias for a device node
706 * @node: pointer to a device tree node
707 * @modalias: Pointer to buffer that modalias value will be copied into
708 * @len: Length of modalias value
709 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600710 * Based on the value of the compatible property, this routine will attempt
711 * to choose an appropriate modalias value for a particular device tree node.
712 * It does this by stripping the manufacturer prefix (as delimited by a ',')
713 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400714 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600715 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400716 */
717int of_modalias_node(struct device_node *node, char *modalias, int len)
718{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600719 const char *compatible, *p;
720 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400721
722 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600723 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400724 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400725 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600726 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400727 return 0;
728}
729EXPORT_SYMBOL_GPL(of_modalias_node);
730
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000731/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700732 * of_find_node_by_phandle - Find a node given a phandle
733 * @handle: phandle of the node to find
734 *
735 * Returns a node pointer with refcount incremented, use
736 * of_node_put() on it when done.
737 */
738struct device_node *of_find_node_by_phandle(phandle handle)
739{
740 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000741 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -0700742
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000743 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000744 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700745 if (np->phandle == handle)
746 break;
747 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000748 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700749 return np;
750}
751EXPORT_SYMBOL(of_find_node_by_phandle);
752
753/**
Tony Priskdaeec1f2013-04-03 17:57:11 +1300754 * of_find_property_value_of_size
755 *
756 * @np: device node from which the property value is to be read.
757 * @propname: name of the property to be searched.
758 * @len: requested length of property value
759 *
760 * Search for a property in a device node and valid the requested size.
761 * Returns the property value on success, -EINVAL if the property does not
762 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
763 * property data isn't large enough.
764 *
765 */
766static void *of_find_property_value_of_size(const struct device_node *np,
767 const char *propname, u32 len)
768{
769 struct property *prop = of_find_property(np, propname, NULL);
770
771 if (!prop)
772 return ERR_PTR(-EINVAL);
773 if (!prop->value)
774 return ERR_PTR(-ENODATA);
775 if (len > prop->length)
776 return ERR_PTR(-EOVERFLOW);
777
778 return prop->value;
779}
780
781/**
Tony Prisk3daf3722013-03-23 17:02:15 +1300782 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
783 *
784 * @np: device node from which the property value is to be read.
785 * @propname: name of the property to be searched.
786 * @index: index of the u32 in the list of values
787 * @out_value: pointer to return value, modified only if no error.
788 *
789 * Search for a property in a device node and read nth 32-bit value from
790 * it. Returns 0 on success, -EINVAL if the property does not exist,
791 * -ENODATA if property does not have a value, and -EOVERFLOW if the
792 * property data isn't large enough.
793 *
794 * The out_value is modified only if a valid u32 value can be decoded.
795 */
796int of_property_read_u32_index(const struct device_node *np,
797 const char *propname,
798 u32 index, u32 *out_value)
799{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300800 const u32 *val = of_find_property_value_of_size(np, propname,
801 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +1300802
Tony Priskdaeec1f2013-04-03 17:57:11 +1300803 if (IS_ERR(val))
804 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +1300805
Tony Priskdaeec1f2013-04-03 17:57:11 +1300806 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +1300807 return 0;
808}
809EXPORT_SYMBOL_GPL(of_property_read_u32_index);
810
811/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530812 * of_property_read_u8_array - Find and read an array of u8 from a property.
813 *
814 * @np: device node from which the property value is to be read.
815 * @propname: name of the property to be searched.
816 * @out_value: pointer to return value, modified only if return value is 0.
817 * @sz: number of array elements to read
818 *
819 * Search for a property in a device node and read 8-bit value(s) from
820 * it. Returns 0 on success, -EINVAL if the property does not exist,
821 * -ENODATA if property does not have a value, and -EOVERFLOW if the
822 * property data isn't large enough.
823 *
824 * dts entry of array should be like:
825 * property = /bits/ 8 <0x50 0x60 0x70>;
826 *
827 * The out_value is modified only if a valid u8 value can be decoded.
828 */
829int of_property_read_u8_array(const struct device_node *np,
830 const char *propname, u8 *out_values, size_t sz)
831{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300832 const u8 *val = of_find_property_value_of_size(np, propname,
833 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530834
Tony Priskdaeec1f2013-04-03 17:57:11 +1300835 if (IS_ERR(val))
836 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530837
Viresh Kumarbe193242012-11-20 10:15:19 +0530838 while (sz--)
839 *out_values++ = *val++;
840 return 0;
841}
842EXPORT_SYMBOL_GPL(of_property_read_u8_array);
843
844/**
845 * of_property_read_u16_array - Find and read an array of u16 from a property.
846 *
847 * @np: device node from which the property value is to be read.
848 * @propname: name of the property to be searched.
849 * @out_value: pointer to return value, modified only if return value is 0.
850 * @sz: number of array elements to read
851 *
852 * Search for a property in a device node and read 16-bit value(s) from
853 * it. Returns 0 on success, -EINVAL if the property does not exist,
854 * -ENODATA if property does not have a value, and -EOVERFLOW if the
855 * property data isn't large enough.
856 *
857 * dts entry of array should be like:
858 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
859 *
860 * The out_value is modified only if a valid u16 value can be decoded.
861 */
862int of_property_read_u16_array(const struct device_node *np,
863 const char *propname, u16 *out_values, size_t sz)
864{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300865 const __be16 *val = of_find_property_value_of_size(np, propname,
866 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530867
Tony Priskdaeec1f2013-04-03 17:57:11 +1300868 if (IS_ERR(val))
869 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530870
Viresh Kumarbe193242012-11-20 10:15:19 +0530871 while (sz--)
872 *out_values++ = be16_to_cpup(val++);
873 return 0;
874}
875EXPORT_SYMBOL_GPL(of_property_read_u16_array);
876
877/**
Rob Herring0e373632011-07-06 15:42:58 -0500878 * of_property_read_u32_array - Find and read an array of 32 bit integers
879 * from a property.
880 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530881 * @np: device node from which the property value is to be read.
882 * @propname: name of the property to be searched.
883 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530884 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530885 *
Rob Herring0e373632011-07-06 15:42:58 -0500886 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530887 * it. Returns 0 on success, -EINVAL if the property does not exist,
888 * -ENODATA if property does not have a value, and -EOVERFLOW if the
889 * property data isn't large enough.
890 *
891 * The out_value is modified only if a valid u32 value can be decoded.
892 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100893int of_property_read_u32_array(const struct device_node *np,
894 const char *propname, u32 *out_values,
895 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530896{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300897 const __be32 *val = of_find_property_value_of_size(np, propname,
898 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +0530899
Tony Priskdaeec1f2013-04-03 17:57:11 +1300900 if (IS_ERR(val))
901 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -0500902
Rob Herring0e373632011-07-06 15:42:58 -0500903 while (sz--)
904 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530905 return 0;
906}
Rob Herring0e373632011-07-06 15:42:58 -0500907EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530908
909/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100910 * of_property_read_u64 - Find and read a 64 bit integer from a property
911 * @np: device node from which the property value is to be read.
912 * @propname: name of the property to be searched.
913 * @out_value: pointer to return value, modified only if return value is 0.
914 *
915 * Search for a property in a device node and read a 64-bit value from
916 * it. Returns 0 on success, -EINVAL if the property does not exist,
917 * -ENODATA if property does not have a value, and -EOVERFLOW if the
918 * property data isn't large enough.
919 *
920 * The out_value is modified only if a valid u64 value can be decoded.
921 */
922int of_property_read_u64(const struct device_node *np, const char *propname,
923 u64 *out_value)
924{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300925 const __be32 *val = of_find_property_value_of_size(np, propname,
926 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100927
Tony Priskdaeec1f2013-04-03 17:57:11 +1300928 if (IS_ERR(val))
929 return PTR_ERR(val);
930
931 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100932 return 0;
933}
934EXPORT_SYMBOL_GPL(of_property_read_u64);
935
936/**
Thomas Abrahama3b85362011-06-30 21:26:10 +0530937 * of_property_read_string - Find and read a string from a property
938 * @np: device node from which the property value is to be read.
939 * @propname: name of the property to be searched.
940 * @out_string: pointer to null terminated return string, modified only if
941 * return value is 0.
942 *
943 * Search for a property in a device tree node and retrieve a null
944 * terminated string value (pointer to data, not a copy). Returns 0 on
945 * success, -EINVAL if the property does not exist, -ENODATA if property
946 * does not have a value, and -EILSEQ if the string is not null-terminated
947 * within the length of the property data.
948 *
949 * The out_string pointer is modified only if a valid string can be decoded.
950 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100951int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +0800952 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530953{
954 struct property *prop = of_find_property(np, propname, NULL);
955 if (!prop)
956 return -EINVAL;
957 if (!prop->value)
958 return -ENODATA;
959 if (strnlen(prop->value, prop->length) >= prop->length)
960 return -EILSEQ;
961 *out_string = prop->value;
962 return 0;
963}
964EXPORT_SYMBOL_GPL(of_property_read_string);
965
966/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200967 * of_property_read_string_index - Find and read a string from a multiple
968 * strings property.
969 * @np: device node from which the property value is to be read.
970 * @propname: name of the property to be searched.
971 * @index: index of the string in the list of strings
972 * @out_string: pointer to null terminated return string, modified only if
973 * return value is 0.
974 *
975 * Search for a property in a device tree node and retrieve a null
976 * terminated string value (pointer to data, not a copy) in the list of strings
977 * contained in that property.
978 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
979 * property does not have a value, and -EILSEQ if the string is not
980 * null-terminated within the length of the property data.
981 *
982 * The out_string pointer is modified only if a valid string can be decoded.
983 */
984int of_property_read_string_index(struct device_node *np, const char *propname,
985 int index, const char **output)
986{
987 struct property *prop = of_find_property(np, propname, NULL);
988 int i = 0;
989 size_t l = 0, total = 0;
990 const char *p;
991
992 if (!prop)
993 return -EINVAL;
994 if (!prop->value)
995 return -ENODATA;
996 if (strnlen(prop->value, prop->length) >= prop->length)
997 return -EILSEQ;
998
999 p = prop->value;
1000
1001 for (i = 0; total < prop->length; total += l, p += l) {
1002 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001003 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001004 *output = p;
1005 return 0;
1006 }
1007 }
1008 return -ENODATA;
1009}
1010EXPORT_SYMBOL_GPL(of_property_read_string_index);
1011
Grant Likely7aff0fe2011-12-12 09:25:58 -07001012/**
1013 * of_property_match_string() - Find string in a list and return index
1014 * @np: pointer to node containing string list property
1015 * @propname: string list property name
1016 * @string: pointer to string to search for in string list
1017 *
1018 * This function searches a string list property and returns the index
1019 * of a specific string value.
1020 */
1021int of_property_match_string(struct device_node *np, const char *propname,
1022 const char *string)
1023{
1024 struct property *prop = of_find_property(np, propname, NULL);
1025 size_t l;
1026 int i;
1027 const char *p, *end;
1028
1029 if (!prop)
1030 return -EINVAL;
1031 if (!prop->value)
1032 return -ENODATA;
1033
1034 p = prop->value;
1035 end = p + prop->length;
1036
1037 for (i = 0; p < end; i++, p += l) {
1038 l = strlen(p) + 1;
1039 if (p + l > end)
1040 return -EILSEQ;
1041 pr_debug("comparing %s with %s\n", string, p);
1042 if (strcmp(string, p) == 0)
1043 return i; /* Found it; return index */
1044 }
1045 return -ENODATA;
1046}
1047EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001048
1049/**
1050 * of_property_count_strings - Find and return the number of strings from a
1051 * multiple strings property.
1052 * @np: device node from which the property value is to be read.
1053 * @propname: name of the property to be searched.
1054 *
1055 * Search for a property in a device tree node and retrieve the number of null
1056 * terminated string contain in it. Returns the number of strings on
1057 * success, -EINVAL if the property does not exist, -ENODATA if property
1058 * does not have a value, and -EILSEQ if the string is not null-terminated
1059 * within the length of the property data.
1060 */
1061int of_property_count_strings(struct device_node *np, const char *propname)
1062{
1063 struct property *prop = of_find_property(np, propname, NULL);
1064 int i = 0;
1065 size_t l = 0, total = 0;
1066 const char *p;
1067
1068 if (!prop)
1069 return -EINVAL;
1070 if (!prop->value)
1071 return -ENODATA;
1072 if (strnlen(prop->value, prop->length) >= prop->length)
1073 return -EILSEQ;
1074
1075 p = prop->value;
1076
Benoit Cousson88af7f52011-12-05 15:23:54 +01001077 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001078 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001079
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001080 return i;
1081}
1082EXPORT_SYMBOL_GPL(of_property_count_strings);
1083
Grant Likelybd69f732013-02-10 22:57:21 +00001084static int __of_parse_phandle_with_args(const struct device_node *np,
1085 const char *list_name,
Stephen Warrenf35c0932013-08-14 15:27:10 -06001086 const char *cells_name,
1087 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001088 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001089{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001090 const __be32 *list, *list_end;
Grant Likely23ce04c02013-02-12 21:21:49 +00001091 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001092 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001093 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001094 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001095
Grant Likely15c9a0a2011-12-12 09:25:57 -07001096 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001097 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001098 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001099 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001100 list_end = list + size / sizeof(*list);
1101
Grant Likely15c9a0a2011-12-12 09:25:57 -07001102 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001103 while (list < list_end) {
Grant Likely23ce04c02013-02-12 21:21:49 +00001104 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001105 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001106
Grant Likely15c9a0a2011-12-12 09:25:57 -07001107 /*
1108 * If phandle is 0, then it is an empty entry with no
1109 * arguments. Skip forward to the next entry.
1110 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001111 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001112 if (phandle) {
1113 /*
1114 * Find the provider node and parse the #*-cells
Stephen Warren3a9560b2013-08-14 15:27:11 -06001115 * property to determine the argument length.
1116 *
1117 * This is not needed if the cell count is hard-coded
1118 * (i.e. cells_name not set, but cell_count is set),
1119 * except when we're going to return the found node
1120 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001121 */
Stephen Warren3a9560b2013-08-14 15:27:11 -06001122 if (cells_name || cur_index == index) {
1123 node = of_find_node_by_phandle(phandle);
1124 if (!node) {
1125 pr_err("%s: could not find phandle\n",
1126 np->full_name);
1127 goto err;
1128 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001129 }
Stephen Warrenf35c0932013-08-14 15:27:10 -06001130
1131 if (cells_name) {
1132 if (of_property_read_u32(node, cells_name,
1133 &count)) {
1134 pr_err("%s: could not get %s for %s\n",
1135 np->full_name, cells_name,
1136 node->full_name);
1137 goto err;
1138 }
1139 } else {
1140 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001141 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001142
Grant Likely15c9a0a2011-12-12 09:25:57 -07001143 /*
1144 * Make sure that the arguments actually fit in the
1145 * remaining property data length
1146 */
1147 if (list + count > list_end) {
1148 pr_err("%s: arguments longer than property\n",
1149 np->full_name);
Grant Likely23ce04c02013-02-12 21:21:49 +00001150 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001151 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001152 }
1153
Grant Likely15c9a0a2011-12-12 09:25:57 -07001154 /*
1155 * All of the error cases above bail out of the loop, so at
1156 * this point, the parsing is successful. If the requested
1157 * index matches, then fill the out_args structure and return,
1158 * or return -ENOENT for an empty entry.
1159 */
Grant Likely23ce04c02013-02-12 21:21:49 +00001160 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001161 if (cur_index == index) {
1162 if (!phandle)
Grant Likely23ce04c02013-02-12 21:21:49 +00001163 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001164
Grant Likely15c9a0a2011-12-12 09:25:57 -07001165 if (out_args) {
1166 int i;
1167 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1168 count = MAX_PHANDLE_ARGS;
1169 out_args->np = node;
1170 out_args->args_count = count;
1171 for (i = 0; i < count; i++)
1172 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001173 } else {
1174 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001175 }
Grant Likely23ce04c02013-02-12 21:21:49 +00001176
1177 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001178 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001179 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001180
1181 of_node_put(node);
1182 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001183 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001184 cur_index++;
1185 }
1186
Grant Likely23ce04c02013-02-12 21:21:49 +00001187 /*
1188 * Unlock node before returning result; will be one of:
1189 * -ENOENT : index is for empty phandle
1190 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001191 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c02013-02-12 21:21:49 +00001192 */
Grant Likelybd69f732013-02-10 22:57:21 +00001193 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c02013-02-12 21:21:49 +00001194 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001195 if (node)
1196 of_node_put(node);
Grant Likely23ce04c02013-02-12 21:21:49 +00001197 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001198}
Grant Likelybd69f732013-02-10 22:57:21 +00001199
Stephen Warrend2621342013-08-14 15:27:08 -06001200/**
Stephen Warren73ed9312013-08-14 15:27:09 -06001201 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1202 * @np: Pointer to device node holding phandle property
1203 * @phandle_name: Name of property holding a phandle value
1204 * @index: For properties holding a table of phandles, this is the index into
1205 * the table
1206 *
1207 * Returns the device_node pointer with refcount incremented. Use
1208 * of_node_put() on it when done.
1209 */
1210struct device_node *of_parse_phandle(const struct device_node *np,
1211 const char *phandle_name, int index)
1212{
Stephen Warren3a9560b2013-08-14 15:27:11 -06001213 struct of_phandle_args args;
Stephen Warren73ed9312013-08-14 15:27:09 -06001214
Stephen Warren3a9560b2013-08-14 15:27:11 -06001215 if (index < 0)
Stephen Warren73ed9312013-08-14 15:27:09 -06001216 return NULL;
1217
Stephen Warren3a9560b2013-08-14 15:27:11 -06001218 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1219 index, &args))
1220 return NULL;
1221
1222 return args.np;
Stephen Warren73ed9312013-08-14 15:27:09 -06001223}
1224EXPORT_SYMBOL(of_parse_phandle);
1225
1226/**
Stephen Warrend2621342013-08-14 15:27:08 -06001227 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1228 * @np: pointer to a device tree node containing a list
1229 * @list_name: property name that contains a list
1230 * @cells_name: property name that specifies phandles' arguments count
1231 * @index: index of a phandle to parse out
1232 * @out_args: optional pointer to output arguments structure (will be filled)
1233 *
1234 * This function is useful to parse lists of phandles and their arguments.
1235 * Returns 0 on success and fills out_args, on error returns appropriate
1236 * errno value.
1237 *
1238 * Caller is responsible to call of_node_put() on the returned out_args->node
1239 * pointer.
1240 *
1241 * Example:
1242 *
1243 * phandle1: node1 {
1244 * #list-cells = <2>;
1245 * }
1246 *
1247 * phandle2: node2 {
1248 * #list-cells = <1>;
1249 * }
1250 *
1251 * node3 {
1252 * list = <&phandle1 1 2 &phandle2 3>;
1253 * }
1254 *
1255 * To get a device_node of the `node2' node you may call this:
1256 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1257 */
Grant Likelybd69f732013-02-10 22:57:21 +00001258int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1259 const char *cells_name, int index,
1260 struct of_phandle_args *out_args)
1261{
1262 if (index < 0)
1263 return -EINVAL;
Stephen Warrenf35c0932013-08-14 15:27:10 -06001264 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1265 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001266}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001267EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001268
Grant Likelybd69f732013-02-10 22:57:21 +00001269/**
Stephen Warrenf35c0932013-08-14 15:27:10 -06001270 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1271 * @np: pointer to a device tree node containing a list
1272 * @list_name: property name that contains a list
1273 * @cell_count: number of argument cells following the phandle
1274 * @index: index of a phandle to parse out
1275 * @out_args: optional pointer to output arguments structure (will be filled)
1276 *
1277 * This function is useful to parse lists of phandles and their arguments.
1278 * Returns 0 on success and fills out_args, on error returns appropriate
1279 * errno value.
1280 *
1281 * Caller is responsible to call of_node_put() on the returned out_args->node
1282 * pointer.
1283 *
1284 * Example:
1285 *
1286 * phandle1: node1 {
1287 * }
1288 *
1289 * phandle2: node2 {
1290 * }
1291 *
1292 * node3 {
1293 * list = <&phandle1 0 2 &phandle2 2 3>;
1294 * }
1295 *
1296 * To get a device_node of the `node2' node you may call this:
1297 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1298 */
1299int of_parse_phandle_with_fixed_args(const struct device_node *np,
1300 const char *list_name, int cell_count,
1301 int index, struct of_phandle_args *out_args)
1302{
1303 if (index < 0)
1304 return -EINVAL;
1305 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1306 index, out_args);
1307}
1308EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1309
1310/**
Grant Likelybd69f732013-02-10 22:57:21 +00001311 * of_count_phandle_with_args() - Find the number of phandles references in a property
1312 * @np: pointer to a device tree node containing a list
1313 * @list_name: property name that contains a list
1314 * @cells_name: property name that specifies phandles' arguments count
1315 *
1316 * Returns the number of phandle + argument tuples within a property. It
1317 * is a typical pattern to encode a list of phandle and variable
1318 * arguments into a single property. The number of arguments is encoded
1319 * by a property in the phandle-target node. For example, a gpios
1320 * property would contain a list of GPIO specifies consisting of a
1321 * phandle and 1 or more arguments. The number of arguments are
1322 * determined by the #gpio-cells property in the node pointed to by the
1323 * phandle.
1324 */
1325int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1326 const char *cells_name)
1327{
Stephen Warrenf35c0932013-08-14 15:27:10 -06001328 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1329 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001330}
1331EXPORT_SYMBOL(of_count_phandle_with_args);
1332
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001333#if defined(CONFIG_OF_DYNAMIC)
1334static int of_property_notify(int action, struct device_node *np,
1335 struct property *prop)
1336{
1337 struct of_prop_reconfig pr;
1338
1339 pr.dn = np;
1340 pr.prop = prop;
1341 return of_reconfig_notify(action, &pr);
1342}
1343#else
1344static int of_property_notify(int action, struct device_node *np,
1345 struct property *prop)
1346{
1347 return 0;
1348}
1349#endif
1350
Grant Likely02af11b2009-11-23 20:16:45 -07001351/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001352 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001353 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001354int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001355{
1356 struct property **next;
1357 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001358 int rc;
1359
1360 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1361 if (rc)
1362 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001363
1364 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001365 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001366 next = &np->properties;
1367 while (*next) {
1368 if (strcmp(prop->name, (*next)->name) == 0) {
1369 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001370 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001371 return -1;
1372 }
1373 next = &(*next)->next;
1374 }
1375 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001376 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001377
1378#ifdef CONFIG_PROC_DEVICETREE
1379 /* try to add to proc as well if it was initialized */
1380 if (np->pde)
1381 proc_device_tree_add_prop(np->pde, prop);
1382#endif /* CONFIG_PROC_DEVICETREE */
1383
1384 return 0;
1385}
1386
1387/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001388 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001389 *
1390 * Note that we don't actually remove it, since we have given out
1391 * who-knows-how-many pointers to the data using get-property.
1392 * Instead we just move the property to the "dead properties"
1393 * list, so it won't be found any more.
1394 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001395int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001396{
1397 struct property **next;
1398 unsigned long flags;
1399 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001400 int rc;
1401
1402 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1403 if (rc)
1404 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001405
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001406 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001407 next = &np->properties;
1408 while (*next) {
1409 if (*next == prop) {
1410 /* found the node */
1411 *next = prop->next;
1412 prop->next = np->deadprops;
1413 np->deadprops = prop;
1414 found = 1;
1415 break;
1416 }
1417 next = &(*next)->next;
1418 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001419 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001420
1421 if (!found)
1422 return -ENODEV;
1423
1424#ifdef CONFIG_PROC_DEVICETREE
1425 /* try to remove the proc node as well */
1426 if (np->pde)
1427 proc_device_tree_remove_prop(np->pde, prop);
1428#endif /* CONFIG_PROC_DEVICETREE */
1429
1430 return 0;
1431}
1432
1433/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001434 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001435 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001436 *
1437 * Note that we don't actually remove it, since we have given out
1438 * who-knows-how-many pointers to the data using get-property.
1439 * Instead we just move the property to the "dead properties" list,
1440 * and add the new property to the property list
1441 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001442int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001443{
Dong Aisheng475d0092012-07-11 15:16:37 +10001444 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001445 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001446 int rc, found = 0;
1447
1448 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1449 if (rc)
1450 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001451
Dong Aisheng475d0092012-07-11 15:16:37 +10001452 if (!newprop->name)
1453 return -EINVAL;
1454
1455 oldprop = of_find_property(np, newprop->name, NULL);
1456 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001457 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001458
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001459 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001460 next = &np->properties;
1461 while (*next) {
1462 if (*next == oldprop) {
1463 /* found the node */
1464 newprop->next = oldprop->next;
1465 *next = newprop;
1466 oldprop->next = np->deadprops;
1467 np->deadprops = oldprop;
1468 found = 1;
1469 break;
1470 }
1471 next = &(*next)->next;
1472 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001473 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001474
1475 if (!found)
1476 return -ENODEV;
1477
1478#ifdef CONFIG_PROC_DEVICETREE
1479 /* try to add to proc as well if it was initialized */
1480 if (np->pde)
1481 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1482#endif /* CONFIG_PROC_DEVICETREE */
1483
1484 return 0;
1485}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001486
1487#if defined(CONFIG_OF_DYNAMIC)
1488/*
1489 * Support for dynamic device trees.
1490 *
1491 * On some platforms, the device tree can be manipulated at runtime.
1492 * The routines in this section support adding, removing and changing
1493 * device tree nodes.
1494 */
1495
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001496static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1497
1498int of_reconfig_notifier_register(struct notifier_block *nb)
1499{
1500 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1501}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001502EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001503
1504int of_reconfig_notifier_unregister(struct notifier_block *nb)
1505{
1506 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1507}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001508EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001509
1510int of_reconfig_notify(unsigned long action, void *p)
1511{
1512 int rc;
1513
1514 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1515 return notifier_to_errno(rc);
1516}
1517
Nathan Fontenote81b3292012-10-02 16:55:01 +00001518#ifdef CONFIG_PROC_DEVICETREE
1519static void of_add_proc_dt_entry(struct device_node *dn)
1520{
1521 struct proc_dir_entry *ent;
1522
1523 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1524 if (ent)
1525 proc_device_tree_add_node(dn, ent);
1526}
1527#else
1528static void of_add_proc_dt_entry(struct device_node *dn)
1529{
1530 return;
1531}
1532#endif
1533
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001534/**
1535 * of_attach_node - Plug a device node into the tree and global list.
1536 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001537int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001538{
1539 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001540 int rc;
1541
1542 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1543 if (rc)
1544 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001545
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001546 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001547 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001548 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001549 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001550 of_allnodes = np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001551 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001552
1553 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001554 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001555}
1556
Nathan Fontenote81b3292012-10-02 16:55:01 +00001557#ifdef CONFIG_PROC_DEVICETREE
1558static void of_remove_proc_dt_entry(struct device_node *dn)
1559{
David Howellsa8ca16e2013-04-12 17:27:28 +01001560 proc_remove(dn->pde);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001561}
1562#else
1563static void of_remove_proc_dt_entry(struct device_node *dn)
1564{
1565 return;
1566}
1567#endif
1568
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001569/**
1570 * of_detach_node - "Unplug" a node from the device tree.
1571 *
1572 * The caller must hold a reference to the node. The memory associated with
1573 * the node is not freed until its refcount goes to zero.
1574 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001575int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001576{
1577 struct device_node *parent;
1578 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001579 int rc = 0;
1580
1581 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1582 if (rc)
1583 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001584
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001585 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001586
Nathan Fontenote81b3292012-10-02 16:55:01 +00001587 if (of_node_check_flag(np, OF_DETACHED)) {
1588 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001589 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001590 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001591 }
1592
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001593 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001594 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001595 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001596 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001597 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001598
Randy Dunlap465aac62012-11-30 10:01:51 +00001599 if (of_allnodes == np)
1600 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001601 else {
1602 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001603 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001604 prev->allnext != np;
1605 prev = prev->allnext)
1606 ;
1607 prev->allnext = np->allnext;
1608 }
1609
1610 if (parent->child == np)
1611 parent->child = np->sibling;
1612 else {
1613 struct device_node *prevsib;
1614 for (prevsib = np->parent->child;
1615 prevsib->sibling != np;
1616 prevsib = prevsib->sibling)
1617 ;
1618 prevsib->sibling = np->sibling;
1619 }
1620
1621 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001622 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001623
1624 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001625 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001626}
1627#endif /* defined(CONFIG_OF_DYNAMIC) */
1628
Shawn Guo611cad72011-08-15 15:28:14 +08001629static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1630 int id, const char *stem, int stem_len)
1631{
1632 ap->np = np;
1633 ap->id = id;
1634 strncpy(ap->stem, stem, stem_len);
1635 ap->stem[stem_len] = 0;
1636 list_add_tail(&ap->link, &aliases_lookup);
1637 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001638 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001639}
1640
1641/**
1642 * of_alias_scan - Scan all properties of 'aliases' node
1643 *
1644 * The function scans all the properties of 'aliases' node and populate
1645 * the the global lookup table with the properties. It returns the
1646 * number of alias_prop found, or error code in error case.
1647 *
1648 * @dt_alloc: An allocator that provides a virtual address to memory
1649 * for the resulting tree
1650 */
1651void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1652{
1653 struct property *pp;
1654
1655 of_chosen = of_find_node_by_path("/chosen");
1656 if (of_chosen == NULL)
1657 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauerca257782013-08-05 14:40:44 +02001658
1659 if (of_chosen) {
1660 const char *name;
1661
1662 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1663 if (name)
1664 of_stdout = of_find_node_by_path(name);
1665 }
1666
Shawn Guo611cad72011-08-15 15:28:14 +08001667 of_aliases = of_find_node_by_path("/aliases");
1668 if (!of_aliases)
1669 return;
1670
Dong Aisheng8af0da92011-12-22 20:19:24 +08001671 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001672 const char *start = pp->name;
1673 const char *end = start + strlen(start);
1674 struct device_node *np;
1675 struct alias_prop *ap;
1676 int id, len;
1677
1678 /* Skip those we do not want to proceed */
1679 if (!strcmp(pp->name, "name") ||
1680 !strcmp(pp->name, "phandle") ||
1681 !strcmp(pp->name, "linux,phandle"))
1682 continue;
1683
1684 np = of_find_node_by_path(pp->value);
1685 if (!np)
1686 continue;
1687
1688 /* walk the alias backwards to extract the id and work out
1689 * the 'stem' string */
1690 while (isdigit(*(end-1)) && end > start)
1691 end--;
1692 len = end - start;
1693
1694 if (kstrtoint(end, 10, &id) < 0)
1695 continue;
1696
1697 /* Allocate an alias_prop with enough space for the stem */
1698 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1699 if (!ap)
1700 continue;
1701 ap->alias = start;
1702 of_alias_add(ap, np, id, start, len);
1703 }
1704}
1705
1706/**
1707 * of_alias_get_id - Get alias id for the given device_node
1708 * @np: Pointer to the given device_node
1709 * @stem: Alias stem of the given device_node
1710 *
1711 * The function travels the lookup table to get alias id for the given
1712 * device_node and alias stem. It returns the alias id if find it.
1713 */
1714int of_alias_get_id(struct device_node *np, const char *stem)
1715{
1716 struct alias_prop *app;
1717 int id = -ENODEV;
1718
1719 mutex_lock(&of_aliases_mutex);
1720 list_for_each_entry(app, &aliases_lookup, link) {
1721 if (strcmp(app->stem, stem) != 0)
1722 continue;
1723
1724 if (np == app->np) {
1725 id = app->id;
1726 break;
1727 }
1728 }
1729 mutex_unlock(&of_aliases_mutex);
1730
1731 return id;
1732}
1733EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001734
1735const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1736 u32 *pu)
1737{
1738 const void *curv = cur;
1739
1740 if (!prop)
1741 return NULL;
1742
1743 if (!cur) {
1744 curv = prop->value;
1745 goto out_val;
1746 }
1747
1748 curv += sizeof(*cur);
1749 if (curv >= prop->value + prop->length)
1750 return NULL;
1751
1752out_val:
1753 *pu = be32_to_cpup(curv);
1754 return curv;
1755}
1756EXPORT_SYMBOL_GPL(of_prop_next_u32);
1757
1758const char *of_prop_next_string(struct property *prop, const char *cur)
1759{
1760 const void *curv = cur;
1761
1762 if (!prop)
1763 return NULL;
1764
1765 if (!cur)
1766 return prop->value;
1767
1768 curv += strlen(cur) + 1;
1769 if (curv >= prop->value + prop->length)
1770 return NULL;
1771
1772 return curv;
1773}
1774EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauerca257782013-08-05 14:40:44 +02001775
1776/**
1777 * of_device_is_stdout_path - check if a device node matches the
1778 * linux,stdout-path property
1779 *
1780 * Check if this device node matches the linux,stdout-path property
1781 * in the chosen node. return true if yes, false otherwise.
1782 */
1783int of_device_is_stdout_path(struct device_node *dn)
1784{
1785 if (!of_stdout)
1786 return false;
1787
1788 return of_stdout == dn;
1789}
1790EXPORT_SYMBOL_GPL(of_device_is_stdout_path);