blob: 605afa9fbe5e089776f04d8c1ae24484fca52384 [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>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010021#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100022#include <linux/module.h>
23#include <linux/of.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100024#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070026#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100027
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080028#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080029
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080030LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080031
Randy Dunlap465aac62012-11-30 10:01:51 +000032struct device_node *of_allnodes;
33EXPORT_SYMBOL(of_allnodes);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070034struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080035struct device_node *of_aliases;
36
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
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100234/*
235 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
236 *
237 * @cpu: logical cpu index of a core/thread
238 * @phys_id: physical identifier of a core/thread
239 *
240 * CPU logical to physical index mapping is architecture specific.
241 * However this __weak function provides a default match of physical
242 * id to logical cpu index. phys_id provided here is usually values read
243 * from the device tree which must match the hardware internal registers.
244 *
245 * Returns true if the physical identifier and the logical cpu index
246 * correspond to the same core/thread, false otherwise.
247 */
248bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
249{
250 return (u32)phys_id == cpu;
251}
252
253/**
254 * Checks if the given "prop_name" property holds the physical id of the
255 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
256 * NULL, local thread number within the core is returned in it.
257 */
258static bool __of_find_n_match_cpu_property(struct device_node *cpun,
259 const char *prop_name, int cpu, unsigned int *thread)
260{
261 const __be32 *cell;
262 int ac, prop_len, tid;
263 u64 hwid;
264
265 ac = of_n_addr_cells(cpun);
266 cell = of_get_property(cpun, prop_name, &prop_len);
267 if (!cell)
268 return false;
269 prop_len /= sizeof(*cell);
270 for (tid = 0; tid < prop_len; tid++) {
271 hwid = of_read_number(cell, ac);
272 if (arch_match_cpu_phys_id(cpu, hwid)) {
273 if (thread)
274 *thread = tid;
275 return true;
276 }
277 cell += ac;
278 }
279 return false;
280}
281
282/**
283 * of_get_cpu_node - Get device node associated with the given logical CPU
284 *
285 * @cpu: CPU number(logical index) for which device node is required
286 * @thread: if not NULL, local thread number within the physical core is
287 * returned
288 *
289 * The main purpose of this function is to retrieve the device node for the
290 * given logical CPU index. It should be used to initialize the of_node in
291 * cpu device. Once of_node in cpu device is populated, all the further
292 * references can use that instead.
293 *
294 * CPU logical to physical index mapping is architecture specific and is built
295 * before booting secondary cores. This function uses arch_match_cpu_phys_id
296 * which can be overridden by architecture specific implementation.
297 *
298 * Returns a node pointer for the logical cpu if found, else NULL.
299 */
300struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
301{
302 struct device_node *cpun, *cpus;
303
304 cpus = of_find_node_by_path("/cpus");
305 if (!cpus) {
306 pr_warn("Missing cpus node, bailing out\n");
307 return NULL;
308 }
309
310 for_each_child_of_node(cpus, cpun) {
311 if (of_node_cmp(cpun->type, "cpu"))
312 continue;
313 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
314 * for thread ids on PowerPC. If it doesn't exist fallback to
315 * standard "reg" property.
316 */
317 if (IS_ENABLED(CONFIG_PPC) &&
318 __of_find_n_match_cpu_property(cpun,
319 "ibm,ppc-interrupt-server#s", cpu, thread))
320 return cpun;
321 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
322 return cpun;
323 }
324 return NULL;
325}
326EXPORT_SYMBOL(of_get_cpu_node);
327
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000328/** Checks if the given "compat" string matches one of the strings in
329 * the device's "compatible" property
330 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500331static int __of_device_is_compatible(const struct device_node *device,
332 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000333{
334 const char* cp;
335 int cplen, l;
336
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500337 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000338 if (cp == NULL)
339 return 0;
340 while (cplen > 0) {
341 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
342 return 1;
343 l = strlen(cp) + 1;
344 cp += l;
345 cplen -= l;
346 }
347
348 return 0;
349}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500350
351/** Checks if the given "compat" string matches one of the strings in
352 * the device's "compatible" property
353 */
354int of_device_is_compatible(const struct device_node *device,
355 const char *compat)
356{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500357 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500358 int res;
359
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500360 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500361 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500362 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500363 return res;
364}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000365EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000366
367/**
Grant Likely71a157e8e2010-02-01 21:34:14 -0700368 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700369 * @compat: compatible string to look for in root node's compatible property.
370 *
371 * Returns true if the root node has the given value in its
372 * compatible property.
373 */
Grant Likely71a157e8e2010-02-01 21:34:14 -0700374int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700375{
376 struct device_node *root;
377 int rc = 0;
378
379 root = of_find_node_by_path("/");
380 if (root) {
381 rc = of_device_is_compatible(root, compat);
382 of_node_put(root);
383 }
384 return rc;
385}
Grant Likely71a157e8e2010-02-01 21:34:14 -0700386EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700387
388/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700389 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100390 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700391 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100392 *
393 * Returns 1 if the status property is absent or set to "okay" or "ok",
394 * 0 otherwise
395 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700396static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100397{
398 const char *status;
399 int statlen;
400
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700401 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100402 if (status == NULL)
403 return 1;
404
405 if (statlen > 0) {
406 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
407 return 1;
408 }
409
410 return 0;
411}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700412
413/**
414 * of_device_is_available - check if a device is available for use
415 *
416 * @device: Node to check for availability
417 *
418 * Returns 1 if the status property is absent or set to "okay" or "ok",
419 * 0 otherwise
420 */
421int of_device_is_available(const struct device_node *device)
422{
423 unsigned long flags;
424 int res;
425
426 raw_spin_lock_irqsave(&devtree_lock, flags);
427 res = __of_device_is_available(device);
428 raw_spin_unlock_irqrestore(&devtree_lock, flags);
429 return res;
430
431}
Josh Boyer834d97d2008-03-27 00:33:14 +1100432EXPORT_SYMBOL(of_device_is_available);
433
434/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000435 * of_get_parent - Get a node's parent if any
436 * @node: Node to get parent
437 *
438 * Returns a node pointer with refcount incremented, use
439 * of_node_put() on it when done.
440 */
441struct device_node *of_get_parent(const struct device_node *node)
442{
443 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500444 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000445
446 if (!node)
447 return NULL;
448
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500449 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000450 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500451 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000452 return np;
453}
454EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000455
456/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000457 * of_get_next_parent - Iterate to a node's parent
458 * @node: Node to get parent of
459 *
460 * This is like of_get_parent() except that it drops the
461 * refcount on the passed node, making it suitable for iterating
462 * through a node's parents.
463 *
464 * Returns a node pointer with refcount incremented, use
465 * of_node_put() on it when done.
466 */
467struct device_node *of_get_next_parent(struct device_node *node)
468{
469 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500470 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000471
472 if (!node)
473 return NULL;
474
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500475 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000476 parent = of_node_get(node->parent);
477 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500478 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000479 return parent;
480}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300481EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000482
483/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000484 * of_get_next_child - Iterate a node childs
485 * @node: parent node
486 * @prev: previous child of the parent node, or NULL to get first
487 *
488 * Returns a node pointer with refcount incremented, use
489 * of_node_put() on it when done.
490 */
491struct device_node *of_get_next_child(const struct device_node *node,
492 struct device_node *prev)
493{
494 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500495 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000496
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500497 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000498 next = prev ? prev->sibling : node->child;
499 for (; next; next = next->sibling)
500 if (of_node_get(next))
501 break;
502 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500503 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000504 return next;
505}
506EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000507
508/**
Timur Tabi32961932012-08-14 13:20:23 +0000509 * of_get_next_available_child - Find the next available child node
510 * @node: parent node
511 * @prev: previous child of the parent node, or NULL to get first
512 *
513 * This function is like of_get_next_child(), except that it
514 * automatically skips any disabled nodes (i.e. status = "disabled").
515 */
516struct device_node *of_get_next_available_child(const struct device_node *node,
517 struct device_node *prev)
518{
519 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000520 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000521
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000522 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000523 next = prev ? prev->sibling : node->child;
524 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700525 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000526 continue;
527 if (of_node_get(next))
528 break;
529 }
530 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000531 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000532 return next;
533}
534EXPORT_SYMBOL(of_get_next_available_child);
535
536/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100537 * of_get_child_by_name - Find the child node by name for a given parent
538 * @node: parent node
539 * @name: child name to look for.
540 *
541 * This function looks for child node for given matching name
542 *
543 * Returns a node pointer if found, with refcount incremented, use
544 * of_node_put() on it when done.
545 * Returns NULL if node is not found.
546 */
547struct device_node *of_get_child_by_name(const struct device_node *node,
548 const char *name)
549{
550 struct device_node *child;
551
552 for_each_child_of_node(node, child)
553 if (child->name && (of_node_cmp(child->name, name) == 0))
554 break;
555 return child;
556}
557EXPORT_SYMBOL(of_get_child_by_name);
558
559/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000560 * of_find_node_by_path - Find a node matching a full OF path
561 * @path: The full path to match
562 *
563 * Returns a node pointer with refcount incremented, use
564 * of_node_put() on it when done.
565 */
566struct device_node *of_find_node_by_path(const char *path)
567{
Randy Dunlap465aac62012-11-30 10:01:51 +0000568 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500569 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000570
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500571 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000572 for (; np; np = np->allnext) {
573 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
574 && of_node_get(np))
575 break;
576 }
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_node_by_path);
581
582/**
583 * of_find_node_by_name - Find a node by its "name" property
584 * @from: The node to start searching from or NULL, the node
585 * you pass will not be searched, only the next one
586 * will; typically, you pass what the previous call
587 * returned. of_node_put() will be called on it
588 * @name: The name string to match against
589 *
590 * Returns a node pointer with refcount incremented, use
591 * of_node_put() on it when done.
592 */
593struct device_node *of_find_node_by_name(struct device_node *from,
594 const char *name)
595{
596 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500597 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000598
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500599 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000600 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000601 for (; np; np = np->allnext)
602 if (np->name && (of_node_cmp(np->name, name) == 0)
603 && of_node_get(np))
604 break;
605 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500606 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000607 return np;
608}
609EXPORT_SYMBOL(of_find_node_by_name);
610
611/**
612 * of_find_node_by_type - Find a node by its "device_type" property
613 * @from: The node to start searching from, or NULL to start searching
614 * the entire device tree. The node you pass will not be
615 * searched, only the next one will; typically, you pass
616 * what the previous call returned. of_node_put() will be
617 * called on from for you.
618 * @type: The type string to match against
619 *
620 * Returns a node pointer with refcount incremented, use
621 * of_node_put() on it when done.
622 */
623struct device_node *of_find_node_by_type(struct device_node *from,
624 const char *type)
625{
626 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500627 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000628
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500629 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000630 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000631 for (; np; np = np->allnext)
632 if (np->type && (of_node_cmp(np->type, type) == 0)
633 && of_node_get(np))
634 break;
635 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500636 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000637 return np;
638}
639EXPORT_SYMBOL(of_find_node_by_type);
640
641/**
642 * of_find_compatible_node - Find a node based on type and one of the
643 * tokens in its "compatible" property
644 * @from: The node to start searching from or NULL, the node
645 * you pass will not be searched, only the next one
646 * will; typically, you pass what the previous call
647 * returned. of_node_put() will be called on it
648 * @type: The type string to match "device_type" or NULL to ignore
649 * @compatible: The string to match to one of the tokens in the device
650 * "compatible" list.
651 *
652 * Returns a node pointer with refcount incremented, use
653 * of_node_put() on it when done.
654 */
655struct device_node *of_find_compatible_node(struct device_node *from,
656 const char *type, const char *compatible)
657{
658 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500659 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000660
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500661 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000662 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000663 for (; np; np = np->allnext) {
664 if (type
665 && !(np->type && (of_node_cmp(np->type, type) == 0)))
666 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500667 if (__of_device_is_compatible(np, compatible) &&
668 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000669 break;
670 }
671 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500672 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000673 return np;
674}
675EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100676
677/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000678 * of_find_node_with_property - Find a node which has a property with
679 * the given name.
680 * @from: The node to start searching from or NULL, the node
681 * you pass will not be searched, only the next one
682 * will; typically, you pass what the previous call
683 * returned. of_node_put() will be called on it
684 * @prop_name: The name of the property to look for.
685 *
686 * Returns a node pointer with refcount incremented, use
687 * of_node_put() on it when done.
688 */
689struct device_node *of_find_node_with_property(struct device_node *from,
690 const char *prop_name)
691{
692 struct device_node *np;
693 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500694 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000695
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500696 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000697 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000698 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530699 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000700 if (of_prop_cmp(pp->name, prop_name) == 0) {
701 of_node_get(np);
702 goto out;
703 }
704 }
705 }
706out:
707 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500708 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000709 return np;
710}
711EXPORT_SYMBOL(of_find_node_with_property);
712
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500713static
714const struct of_device_id *__of_match_node(const struct of_device_id *matches,
715 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100716{
Grant Likelya52f07e2011-03-18 10:21:29 -0600717 if (!matches)
718 return NULL;
719
Grant Likely283029d2008-01-09 06:20:40 +1100720 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
721 int match = 1;
722 if (matches->name[0])
723 match &= node->name
724 && !strcmp(matches->name, node->name);
725 if (matches->type[0])
726 match &= node->type
727 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700728 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500729 match &= __of_device_is_compatible(node,
730 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700731 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100732 return matches;
733 matches++;
734 }
735 return NULL;
736}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500737
738/**
739 * of_match_node - Tell if an device_node has a matching of_match structure
740 * @matches: array of of device match structures to search in
741 * @node: the of device structure to match against
742 *
743 * Low level utility function used by device matching.
744 */
745const struct of_device_id *of_match_node(const struct of_device_id *matches,
746 const struct device_node *node)
747{
748 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500749 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500750
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500751 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500752 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500753 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500754 return match;
755}
Grant Likely283029d2008-01-09 06:20:40 +1100756EXPORT_SYMBOL(of_match_node);
757
758/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700759 * of_find_matching_node_and_match - Find a node based on an of_device_id
760 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100761 * @from: The node to start searching from or NULL, the node
762 * you pass will not be searched, only the next one
763 * will; typically, you pass what the previous call
764 * returned. of_node_put() will be called on it
765 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700766 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100767 *
768 * Returns a node pointer with refcount incremented, use
769 * of_node_put() on it when done.
770 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700771struct device_node *of_find_matching_node_and_match(struct device_node *from,
772 const struct of_device_id *matches,
773 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100774{
775 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800776 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500777 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100778
Stephen Warren50c8af42012-11-20 16:12:20 -0700779 if (match)
780 *match = NULL;
781
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500782 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000783 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100784 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500785 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800786 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700787 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800788 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100789 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700790 }
Grant Likely283029d2008-01-09 06:20:40 +1100791 }
792 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500793 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100794 return np;
795}
Grant Likely80c20222012-12-19 10:45:36 +0000796EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400797
798/**
Grant Likely3f07af42008-07-25 22:25:13 -0400799 * of_modalias_node - Lookup appropriate modalias for a device node
800 * @node: pointer to a device tree node
801 * @modalias: Pointer to buffer that modalias value will be copied into
802 * @len: Length of modalias value
803 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600804 * Based on the value of the compatible property, this routine will attempt
805 * to choose an appropriate modalias value for a particular device tree node.
806 * It does this by stripping the manufacturer prefix (as delimited by a ',')
807 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400808 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600809 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400810 */
811int of_modalias_node(struct device_node *node, char *modalias, int len)
812{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600813 const char *compatible, *p;
814 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400815
816 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600817 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400818 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400819 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600820 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400821 return 0;
822}
823EXPORT_SYMBOL_GPL(of_modalias_node);
824
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000825/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700826 * of_find_node_by_phandle - Find a node given a phandle
827 * @handle: phandle of the node to find
828 *
829 * Returns a node pointer with refcount incremented, use
830 * of_node_put() on it when done.
831 */
832struct device_node *of_find_node_by_phandle(phandle handle)
833{
834 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000835 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -0700836
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000837 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000838 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700839 if (np->phandle == handle)
840 break;
841 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000842 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700843 return np;
844}
845EXPORT_SYMBOL(of_find_node_by_phandle);
846
847/**
Tony Priskdaeec1f2013-04-03 17:57:11 +1300848 * of_find_property_value_of_size
849 *
850 * @np: device node from which the property value is to be read.
851 * @propname: name of the property to be searched.
852 * @len: requested length of property value
853 *
854 * Search for a property in a device node and valid the requested size.
855 * Returns the property value on success, -EINVAL if the property does not
856 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
857 * property data isn't large enough.
858 *
859 */
860static void *of_find_property_value_of_size(const struct device_node *np,
861 const char *propname, u32 len)
862{
863 struct property *prop = of_find_property(np, propname, NULL);
864
865 if (!prop)
866 return ERR_PTR(-EINVAL);
867 if (!prop->value)
868 return ERR_PTR(-ENODATA);
869 if (len > prop->length)
870 return ERR_PTR(-EOVERFLOW);
871
872 return prop->value;
873}
874
875/**
Tony Prisk3daf3722013-03-23 17:02:15 +1300876 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
877 *
878 * @np: device node from which the property value is to be read.
879 * @propname: name of the property to be searched.
880 * @index: index of the u32 in the list of values
881 * @out_value: pointer to return value, modified only if no error.
882 *
883 * Search for a property in a device node and read nth 32-bit value from
884 * it. Returns 0 on success, -EINVAL if the property does not exist,
885 * -ENODATA if property does not have a value, and -EOVERFLOW if the
886 * property data isn't large enough.
887 *
888 * The out_value is modified only if a valid u32 value can be decoded.
889 */
890int of_property_read_u32_index(const struct device_node *np,
891 const char *propname,
892 u32 index, u32 *out_value)
893{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300894 const u32 *val = of_find_property_value_of_size(np, propname,
895 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +1300896
Tony Priskdaeec1f2013-04-03 17:57:11 +1300897 if (IS_ERR(val))
898 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +1300899
Tony Priskdaeec1f2013-04-03 17:57:11 +1300900 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +1300901 return 0;
902}
903EXPORT_SYMBOL_GPL(of_property_read_u32_index);
904
905/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530906 * of_property_read_u8_array - Find and read an array of u8 from a property.
907 *
908 * @np: device node from which the property value is to be read.
909 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530910 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530911 * @sz: number of array elements to read
912 *
913 * Search for a property in a device node and read 8-bit value(s) from
914 * it. Returns 0 on success, -EINVAL if the property does not exist,
915 * -ENODATA if property does not have a value, and -EOVERFLOW if the
916 * property data isn't large enough.
917 *
918 * dts entry of array should be like:
919 * property = /bits/ 8 <0x50 0x60 0x70>;
920 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530921 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +0530922 */
923int of_property_read_u8_array(const struct device_node *np,
924 const char *propname, u8 *out_values, size_t sz)
925{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300926 const u8 *val = of_find_property_value_of_size(np, propname,
927 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530928
Tony Priskdaeec1f2013-04-03 17:57:11 +1300929 if (IS_ERR(val))
930 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530931
Viresh Kumarbe193242012-11-20 10:15:19 +0530932 while (sz--)
933 *out_values++ = *val++;
934 return 0;
935}
936EXPORT_SYMBOL_GPL(of_property_read_u8_array);
937
938/**
939 * of_property_read_u16_array - Find and read an array of u16 from a property.
940 *
941 * @np: device node from which the property value is to be read.
942 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530943 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530944 * @sz: number of array elements to read
945 *
946 * Search for a property in a device node and read 16-bit value(s) from
947 * it. Returns 0 on success, -EINVAL if the property does not exist,
948 * -ENODATA if property does not have a value, and -EOVERFLOW if the
949 * property data isn't large enough.
950 *
951 * dts entry of array should be like:
952 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
953 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530954 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +0530955 */
956int of_property_read_u16_array(const struct device_node *np,
957 const char *propname, u16 *out_values, size_t sz)
958{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300959 const __be16 *val = of_find_property_value_of_size(np, propname,
960 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530961
Tony Priskdaeec1f2013-04-03 17:57:11 +1300962 if (IS_ERR(val))
963 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530964
Viresh Kumarbe193242012-11-20 10:15:19 +0530965 while (sz--)
966 *out_values++ = be16_to_cpup(val++);
967 return 0;
968}
969EXPORT_SYMBOL_GPL(of_property_read_u16_array);
970
971/**
Rob Herring0e373632011-07-06 15:42:58 -0500972 * of_property_read_u32_array - Find and read an array of 32 bit integers
973 * from a property.
974 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530975 * @np: device node from which the property value is to be read.
976 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530977 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530978 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530979 *
Rob Herring0e373632011-07-06 15:42:58 -0500980 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530981 * it. Returns 0 on success, -EINVAL if the property does not exist,
982 * -ENODATA if property does not have a value, and -EOVERFLOW if the
983 * property data isn't large enough.
984 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530985 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +0530986 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100987int of_property_read_u32_array(const struct device_node *np,
988 const char *propname, u32 *out_values,
989 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530990{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300991 const __be32 *val = of_find_property_value_of_size(np, propname,
992 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +0530993
Tony Priskdaeec1f2013-04-03 17:57:11 +1300994 if (IS_ERR(val))
995 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -0500996
Rob Herring0e373632011-07-06 15:42:58 -0500997 while (sz--)
998 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530999 return 0;
1000}
Rob Herring0e373632011-07-06 15:42:58 -05001001EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301002
1003/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001004 * of_property_read_u64 - Find and read a 64 bit integer from a property
1005 * @np: device node from which the property value is to be read.
1006 * @propname: name of the property to be searched.
1007 * @out_value: pointer to return value, modified only if return value is 0.
1008 *
1009 * Search for a property in a device node and read a 64-bit value from
1010 * it. Returns 0 on success, -EINVAL if the property does not exist,
1011 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1012 * property data isn't large enough.
1013 *
1014 * The out_value is modified only if a valid u64 value can be decoded.
1015 */
1016int of_property_read_u64(const struct device_node *np, const char *propname,
1017 u64 *out_value)
1018{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001019 const __be32 *val = of_find_property_value_of_size(np, propname,
1020 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001021
Tony Priskdaeec1f2013-04-03 17:57:11 +13001022 if (IS_ERR(val))
1023 return PTR_ERR(val);
1024
1025 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001026 return 0;
1027}
1028EXPORT_SYMBOL_GPL(of_property_read_u64);
1029
1030/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301031 * of_property_read_string - Find and read a string from a property
1032 * @np: device node from which the property value is to be read.
1033 * @propname: name of the property to be searched.
1034 * @out_string: pointer to null terminated return string, modified only if
1035 * return value is 0.
1036 *
1037 * Search for a property in a device tree node and retrieve a null
1038 * terminated string value (pointer to data, not a copy). Returns 0 on
1039 * success, -EINVAL if the property does not exist, -ENODATA if property
1040 * does not have a value, and -EILSEQ if the string is not null-terminated
1041 * within the length of the property data.
1042 *
1043 * The out_string pointer is modified only if a valid string can be decoded.
1044 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001045int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001046 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301047{
1048 struct property *prop = of_find_property(np, propname, NULL);
1049 if (!prop)
1050 return -EINVAL;
1051 if (!prop->value)
1052 return -ENODATA;
1053 if (strnlen(prop->value, prop->length) >= prop->length)
1054 return -EILSEQ;
1055 *out_string = prop->value;
1056 return 0;
1057}
1058EXPORT_SYMBOL_GPL(of_property_read_string);
1059
1060/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001061 * of_property_read_string_index - Find and read a string from a multiple
1062 * strings property.
1063 * @np: device node from which the property value is to be read.
1064 * @propname: name of the property to be searched.
1065 * @index: index of the string in the list of strings
1066 * @out_string: pointer to null terminated return string, modified only if
1067 * return value is 0.
1068 *
1069 * Search for a property in a device tree node and retrieve a null
1070 * terminated string value (pointer to data, not a copy) in the list of strings
1071 * contained in that property.
1072 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1073 * property does not have a value, and -EILSEQ if the string is not
1074 * null-terminated within the length of the property data.
1075 *
1076 * The out_string pointer is modified only if a valid string can be decoded.
1077 */
1078int of_property_read_string_index(struct device_node *np, const char *propname,
1079 int index, const char **output)
1080{
1081 struct property *prop = of_find_property(np, propname, NULL);
1082 int i = 0;
1083 size_t l = 0, total = 0;
1084 const char *p;
1085
1086 if (!prop)
1087 return -EINVAL;
1088 if (!prop->value)
1089 return -ENODATA;
1090 if (strnlen(prop->value, prop->length) >= prop->length)
1091 return -EILSEQ;
1092
1093 p = prop->value;
1094
1095 for (i = 0; total < prop->length; total += l, p += l) {
1096 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001097 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001098 *output = p;
1099 return 0;
1100 }
1101 }
1102 return -ENODATA;
1103}
1104EXPORT_SYMBOL_GPL(of_property_read_string_index);
1105
Grant Likely7aff0fe2011-12-12 09:25:58 -07001106/**
1107 * of_property_match_string() - Find string in a list and return index
1108 * @np: pointer to node containing string list property
1109 * @propname: string list property name
1110 * @string: pointer to string to search for in string list
1111 *
1112 * This function searches a string list property and returns the index
1113 * of a specific string value.
1114 */
1115int of_property_match_string(struct device_node *np, const char *propname,
1116 const char *string)
1117{
1118 struct property *prop = of_find_property(np, propname, NULL);
1119 size_t l;
1120 int i;
1121 const char *p, *end;
1122
1123 if (!prop)
1124 return -EINVAL;
1125 if (!prop->value)
1126 return -ENODATA;
1127
1128 p = prop->value;
1129 end = p + prop->length;
1130
1131 for (i = 0; p < end; i++, p += l) {
1132 l = strlen(p) + 1;
1133 if (p + l > end)
1134 return -EILSEQ;
1135 pr_debug("comparing %s with %s\n", string, p);
1136 if (strcmp(string, p) == 0)
1137 return i; /* Found it; return index */
1138 }
1139 return -ENODATA;
1140}
1141EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001142
1143/**
1144 * of_property_count_strings - Find and return the number of strings from a
1145 * multiple strings property.
1146 * @np: device node from which the property value is to be read.
1147 * @propname: name of the property to be searched.
1148 *
1149 * Search for a property in a device tree node and retrieve the number of null
1150 * terminated string contain in it. Returns the number of strings on
1151 * success, -EINVAL if the property does not exist, -ENODATA if property
1152 * does not have a value, and -EILSEQ if the string is not null-terminated
1153 * within the length of the property data.
1154 */
1155int of_property_count_strings(struct device_node *np, const char *propname)
1156{
1157 struct property *prop = of_find_property(np, propname, NULL);
1158 int i = 0;
1159 size_t l = 0, total = 0;
1160 const char *p;
1161
1162 if (!prop)
1163 return -EINVAL;
1164 if (!prop->value)
1165 return -ENODATA;
1166 if (strnlen(prop->value, prop->length) >= prop->length)
1167 return -EILSEQ;
1168
1169 p = prop->value;
1170
Benoit Cousson88af7f52011-12-05 15:23:54 +01001171 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001172 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001173
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001174 return i;
1175}
1176EXPORT_SYMBOL_GPL(of_property_count_strings);
1177
1178/**
Grant Likely739649c2009-04-25 12:52:40 +00001179 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1180 * @np: Pointer to device node holding phandle property
1181 * @phandle_name: Name of property holding a phandle value
1182 * @index: For properties holding a table of phandles, this is the index into
1183 * the table
1184 *
1185 * Returns the device_node pointer with refcount incremented. Use
1186 * of_node_put() on it when done.
1187 */
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +01001188struct device_node *of_parse_phandle(const struct device_node *np,
1189 const char *phandle_name, int index)
Grant Likely739649c2009-04-25 12:52:40 +00001190{
Grant Likely9a6b2e52010-07-23 01:48:25 -06001191 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +00001192 int size;
1193
1194 phandle = of_get_property(np, phandle_name, &size);
1195 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1196 return NULL;
1197
Grant Likely9a6b2e52010-07-23 01:48:25 -06001198 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +00001199}
1200EXPORT_SYMBOL(of_parse_phandle);
1201
1202/**
Grant Likely15c9a0a2011-12-12 09:25:57 -07001203 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001204 * @np: pointer to a device tree node containing a list
1205 * @list_name: property name that contains a list
1206 * @cells_name: property name that specifies phandles' arguments count
1207 * @index: index of a phandle to parse out
Grant Likely15c9a0a2011-12-12 09:25:57 -07001208 * @out_args: optional pointer to output arguments structure (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001209 *
1210 * This function is useful to parse lists of phandles and their arguments.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001211 * Returns 0 on success and fills out_args, on error returns appropriate
1212 * errno value.
1213 *
1214 * Caller is responsible to call of_node_put() on the returned out_args->node
1215 * pointer.
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001216 *
1217 * Example:
1218 *
1219 * phandle1: node1 {
1220 * #list-cells = <2>;
1221 * }
1222 *
1223 * phandle2: node2 {
1224 * #list-cells = <1>;
1225 * }
1226 *
1227 * node3 {
1228 * list = <&phandle1 1 2 &phandle2 3>;
1229 * }
1230 *
1231 * To get a device_node of the `node2' node you may call this:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001232 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001233 */
Grant Likelybd69f732013-02-10 22:57:21 +00001234static int __of_parse_phandle_with_args(const struct device_node *np,
1235 const char *list_name,
1236 const char *cells_name, int index,
1237 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001238{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001239 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001240 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001241 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001242 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001243 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001244
Grant Likely15c9a0a2011-12-12 09:25:57 -07001245 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001246 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001247 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001248 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001249 list_end = list + size / sizeof(*list);
1250
Grant Likely15c9a0a2011-12-12 09:25:57 -07001251 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001252 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001253 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001254 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001255
Grant Likely15c9a0a2011-12-12 09:25:57 -07001256 /*
1257 * If phandle is 0, then it is an empty entry with no
1258 * arguments. Skip forward to the next entry.
1259 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001260 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001261 if (phandle) {
1262 /*
1263 * Find the provider node and parse the #*-cells
1264 * property to determine the argument length
1265 */
1266 node = of_find_node_by_phandle(phandle);
1267 if (!node) {
1268 pr_err("%s: could not find phandle\n",
1269 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001270 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001271 }
1272 if (of_property_read_u32(node, cells_name, &count)) {
1273 pr_err("%s: could not get %s for %s\n",
1274 np->full_name, cells_name,
1275 node->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001276 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001277 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001278
Grant Likely15c9a0a2011-12-12 09:25:57 -07001279 /*
1280 * Make sure that the arguments actually fit in the
1281 * remaining property data length
1282 */
1283 if (list + count > list_end) {
1284 pr_err("%s: arguments longer than property\n",
1285 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001286 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001287 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001288 }
1289
Grant Likely15c9a0a2011-12-12 09:25:57 -07001290 /*
1291 * All of the error cases above bail out of the loop, so at
1292 * this point, the parsing is successful. If the requested
1293 * index matches, then fill the out_args structure and return,
1294 * or return -ENOENT for an empty entry.
1295 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001296 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001297 if (cur_index == index) {
1298 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001299 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001300
Grant Likely15c9a0a2011-12-12 09:25:57 -07001301 if (out_args) {
1302 int i;
1303 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1304 count = MAX_PHANDLE_ARGS;
1305 out_args->np = node;
1306 out_args->args_count = count;
1307 for (i = 0; i < count; i++)
1308 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001309 } else {
1310 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001311 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001312
1313 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001314 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001315 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001316
1317 of_node_put(node);
1318 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001319 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001320 cur_index++;
1321 }
1322
Grant Likely23ce04c2013-02-12 21:21:49 +00001323 /*
1324 * Unlock node before returning result; will be one of:
1325 * -ENOENT : index is for empty phandle
1326 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001327 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001328 */
Grant Likelybd69f732013-02-10 22:57:21 +00001329 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001330 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001331 if (node)
1332 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001333 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001334}
Grant Likelybd69f732013-02-10 22:57:21 +00001335
1336int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1337 const char *cells_name, int index,
1338 struct of_phandle_args *out_args)
1339{
1340 if (index < 0)
1341 return -EINVAL;
1342 return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
1343}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001344EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001345
Grant Likelybd69f732013-02-10 22:57:21 +00001346/**
1347 * of_count_phandle_with_args() - Find the number of phandles references in a property
1348 * @np: pointer to a device tree node containing a list
1349 * @list_name: property name that contains a list
1350 * @cells_name: property name that specifies phandles' arguments count
1351 *
1352 * Returns the number of phandle + argument tuples within a property. It
1353 * is a typical pattern to encode a list of phandle and variable
1354 * arguments into a single property. The number of arguments is encoded
1355 * by a property in the phandle-target node. For example, a gpios
1356 * property would contain a list of GPIO specifies consisting of a
1357 * phandle and 1 or more arguments. The number of arguments are
1358 * determined by the #gpio-cells property in the node pointed to by the
1359 * phandle.
1360 */
1361int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1362 const char *cells_name)
1363{
1364 return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
1365}
1366EXPORT_SYMBOL(of_count_phandle_with_args);
1367
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001368#if defined(CONFIG_OF_DYNAMIC)
1369static int of_property_notify(int action, struct device_node *np,
1370 struct property *prop)
1371{
1372 struct of_prop_reconfig pr;
1373
1374 pr.dn = np;
1375 pr.prop = prop;
1376 return of_reconfig_notify(action, &pr);
1377}
1378#else
1379static int of_property_notify(int action, struct device_node *np,
1380 struct property *prop)
1381{
1382 return 0;
1383}
1384#endif
1385
Grant Likely02af11b2009-11-23 20:16:45 -07001386/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001387 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001388 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001389int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001390{
1391 struct property **next;
1392 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001393 int rc;
1394
1395 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1396 if (rc)
1397 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001398
1399 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001400 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001401 next = &np->properties;
1402 while (*next) {
1403 if (strcmp(prop->name, (*next)->name) == 0) {
1404 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001405 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001406 return -1;
1407 }
1408 next = &(*next)->next;
1409 }
1410 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001411 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001412
1413#ifdef CONFIG_PROC_DEVICETREE
1414 /* try to add to proc as well if it was initialized */
1415 if (np->pde)
1416 proc_device_tree_add_prop(np->pde, prop);
1417#endif /* CONFIG_PROC_DEVICETREE */
1418
1419 return 0;
1420}
1421
1422/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001423 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001424 *
1425 * Note that we don't actually remove it, since we have given out
1426 * who-knows-how-many pointers to the data using get-property.
1427 * Instead we just move the property to the "dead properties"
1428 * list, so it won't be found any more.
1429 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001430int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001431{
1432 struct property **next;
1433 unsigned long flags;
1434 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001435 int rc;
1436
1437 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1438 if (rc)
1439 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001440
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001441 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001442 next = &np->properties;
1443 while (*next) {
1444 if (*next == prop) {
1445 /* found the node */
1446 *next = prop->next;
1447 prop->next = np->deadprops;
1448 np->deadprops = prop;
1449 found = 1;
1450 break;
1451 }
1452 next = &(*next)->next;
1453 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001454 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001455
1456 if (!found)
1457 return -ENODEV;
1458
1459#ifdef CONFIG_PROC_DEVICETREE
1460 /* try to remove the proc node as well */
1461 if (np->pde)
1462 proc_device_tree_remove_prop(np->pde, prop);
1463#endif /* CONFIG_PROC_DEVICETREE */
1464
1465 return 0;
1466}
1467
1468/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001469 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001470 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001471 *
1472 * Note that we don't actually remove it, since we have given out
1473 * who-knows-how-many pointers to the data using get-property.
1474 * Instead we just move the property to the "dead properties" list,
1475 * and add the new property to the property list
1476 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001477int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001478{
Dong Aisheng475d0092012-07-11 15:16:37 +10001479 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001480 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001481 int rc, found = 0;
1482
1483 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1484 if (rc)
1485 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001486
Dong Aisheng475d0092012-07-11 15:16:37 +10001487 if (!newprop->name)
1488 return -EINVAL;
1489
1490 oldprop = of_find_property(np, newprop->name, NULL);
1491 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001492 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001493
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001494 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001495 next = &np->properties;
1496 while (*next) {
1497 if (*next == oldprop) {
1498 /* found the node */
1499 newprop->next = oldprop->next;
1500 *next = newprop;
1501 oldprop->next = np->deadprops;
1502 np->deadprops = oldprop;
1503 found = 1;
1504 break;
1505 }
1506 next = &(*next)->next;
1507 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001508 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001509
1510 if (!found)
1511 return -ENODEV;
1512
1513#ifdef CONFIG_PROC_DEVICETREE
1514 /* try to add to proc as well if it was initialized */
1515 if (np->pde)
1516 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1517#endif /* CONFIG_PROC_DEVICETREE */
1518
1519 return 0;
1520}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001521
1522#if defined(CONFIG_OF_DYNAMIC)
1523/*
1524 * Support for dynamic device trees.
1525 *
1526 * On some platforms, the device tree can be manipulated at runtime.
1527 * The routines in this section support adding, removing and changing
1528 * device tree nodes.
1529 */
1530
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001531static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1532
1533int of_reconfig_notifier_register(struct notifier_block *nb)
1534{
1535 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1536}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001537EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001538
1539int of_reconfig_notifier_unregister(struct notifier_block *nb)
1540{
1541 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1542}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001543EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001544
1545int of_reconfig_notify(unsigned long action, void *p)
1546{
1547 int rc;
1548
1549 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1550 return notifier_to_errno(rc);
1551}
1552
Nathan Fontenote81b3292012-10-02 16:55:01 +00001553#ifdef CONFIG_PROC_DEVICETREE
1554static void of_add_proc_dt_entry(struct device_node *dn)
1555{
1556 struct proc_dir_entry *ent;
1557
1558 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1559 if (ent)
1560 proc_device_tree_add_node(dn, ent);
1561}
1562#else
1563static void of_add_proc_dt_entry(struct device_node *dn)
1564{
1565 return;
1566}
1567#endif
1568
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001569/**
1570 * of_attach_node - Plug a device node into the tree and global list.
1571 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001572int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001573{
1574 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001575 int rc;
1576
1577 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1578 if (rc)
1579 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001580
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001581 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001582 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001583 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001584 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001585 of_allnodes = np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001586 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001587
1588 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001589 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001590}
1591
Nathan Fontenote81b3292012-10-02 16:55:01 +00001592#ifdef CONFIG_PROC_DEVICETREE
1593static void of_remove_proc_dt_entry(struct device_node *dn)
1594{
David Howellsa8ca16e2013-04-12 17:27:28 +01001595 proc_remove(dn->pde);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001596}
1597#else
1598static void of_remove_proc_dt_entry(struct device_node *dn)
1599{
1600 return;
1601}
1602#endif
1603
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001604/**
1605 * of_detach_node - "Unplug" a node from the device tree.
1606 *
1607 * The caller must hold a reference to the node. The memory associated with
1608 * the node is not freed until its refcount goes to zero.
1609 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001610int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001611{
1612 struct device_node *parent;
1613 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001614 int rc = 0;
1615
1616 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1617 if (rc)
1618 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001619
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001620 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001621
Nathan Fontenote81b3292012-10-02 16:55:01 +00001622 if (of_node_check_flag(np, OF_DETACHED)) {
1623 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001624 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001625 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001626 }
1627
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001628 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001629 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001630 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001631 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001632 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001633
Randy Dunlap465aac62012-11-30 10:01:51 +00001634 if (of_allnodes == np)
1635 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001636 else {
1637 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001638 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001639 prev->allnext != np;
1640 prev = prev->allnext)
1641 ;
1642 prev->allnext = np->allnext;
1643 }
1644
1645 if (parent->child == np)
1646 parent->child = np->sibling;
1647 else {
1648 struct device_node *prevsib;
1649 for (prevsib = np->parent->child;
1650 prevsib->sibling != np;
1651 prevsib = prevsib->sibling)
1652 ;
1653 prevsib->sibling = np->sibling;
1654 }
1655
1656 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001657 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001658
1659 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001660 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001661}
1662#endif /* defined(CONFIG_OF_DYNAMIC) */
1663
Shawn Guo611cad72011-08-15 15:28:14 +08001664static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1665 int id, const char *stem, int stem_len)
1666{
1667 ap->np = np;
1668 ap->id = id;
1669 strncpy(ap->stem, stem, stem_len);
1670 ap->stem[stem_len] = 0;
1671 list_add_tail(&ap->link, &aliases_lookup);
1672 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001673 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001674}
1675
1676/**
1677 * of_alias_scan - Scan all properties of 'aliases' node
1678 *
1679 * The function scans all the properties of 'aliases' node and populate
1680 * the the global lookup table with the properties. It returns the
1681 * number of alias_prop found, or error code in error case.
1682 *
1683 * @dt_alloc: An allocator that provides a virtual address to memory
1684 * for the resulting tree
1685 */
1686void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1687{
1688 struct property *pp;
1689
1690 of_chosen = of_find_node_by_path("/chosen");
1691 if (of_chosen == NULL)
1692 of_chosen = of_find_node_by_path("/chosen@0");
1693 of_aliases = of_find_node_by_path("/aliases");
1694 if (!of_aliases)
1695 return;
1696
Dong Aisheng8af0da92011-12-22 20:19:24 +08001697 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001698 const char *start = pp->name;
1699 const char *end = start + strlen(start);
1700 struct device_node *np;
1701 struct alias_prop *ap;
1702 int id, len;
1703
1704 /* Skip those we do not want to proceed */
1705 if (!strcmp(pp->name, "name") ||
1706 !strcmp(pp->name, "phandle") ||
1707 !strcmp(pp->name, "linux,phandle"))
1708 continue;
1709
1710 np = of_find_node_by_path(pp->value);
1711 if (!np)
1712 continue;
1713
1714 /* walk the alias backwards to extract the id and work out
1715 * the 'stem' string */
1716 while (isdigit(*(end-1)) && end > start)
1717 end--;
1718 len = end - start;
1719
1720 if (kstrtoint(end, 10, &id) < 0)
1721 continue;
1722
1723 /* Allocate an alias_prop with enough space for the stem */
1724 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1725 if (!ap)
1726 continue;
1727 ap->alias = start;
1728 of_alias_add(ap, np, id, start, len);
1729 }
1730}
1731
1732/**
1733 * of_alias_get_id - Get alias id for the given device_node
1734 * @np: Pointer to the given device_node
1735 * @stem: Alias stem of the given device_node
1736 *
1737 * The function travels the lookup table to get alias id for the given
1738 * device_node and alias stem. It returns the alias id if find it.
1739 */
1740int of_alias_get_id(struct device_node *np, const char *stem)
1741{
1742 struct alias_prop *app;
1743 int id = -ENODEV;
1744
1745 mutex_lock(&of_aliases_mutex);
1746 list_for_each_entry(app, &aliases_lookup, link) {
1747 if (strcmp(app->stem, stem) != 0)
1748 continue;
1749
1750 if (np == app->np) {
1751 id = app->id;
1752 break;
1753 }
1754 }
1755 mutex_unlock(&of_aliases_mutex);
1756
1757 return id;
1758}
1759EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001760
1761const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1762 u32 *pu)
1763{
1764 const void *curv = cur;
1765
1766 if (!prop)
1767 return NULL;
1768
1769 if (!cur) {
1770 curv = prop->value;
1771 goto out_val;
1772 }
1773
1774 curv += sizeof(*cur);
1775 if (curv >= prop->value + prop->length)
1776 return NULL;
1777
1778out_val:
1779 *pu = be32_to_cpup(curv);
1780 return curv;
1781}
1782EXPORT_SYMBOL_GPL(of_prop_next_u32);
1783
1784const char *of_prop_next_string(struct property *prop, const char *cur)
1785{
1786 const void *curv = cur;
1787
1788 if (!prop)
1789 return NULL;
1790
1791 if (!cur)
1792 return prop->value;
1793
1794 curv += strlen(cur) + 1;
1795 if (curv >= prop->value + prop->length)
1796 return NULL;
1797
1798 return curv;
1799}
1800EXPORT_SYMBOL_GPL(of_prop_next_string);