blob: 11a3498502fc8dec3c3fd53575914ecd77c1a336 [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>
Grant Likelyf57a8e22014-02-20 18:02:11 +000025#include <linux/string.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;
Sascha Hauerca257782013-08-05 14:40:44 +020036static struct device_node *of_stdout;
Shawn Guo611cad72011-08-15 15:28:14 +080037
Grant Likelyf57a8e22014-02-20 18:02:11 +000038static struct kset *of_kset;
39
40/*
41 * Used to protect the of_aliases; but also overloaded to hold off addition of
42 * nodes to sysfs
43 */
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080044DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100045
Stephen Rothwell581b6052007-04-24 16:46:53 +100046/* use when traversing tree through the allnext, child, sibling,
47 * or parent members of struct device_node.
48 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050049DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100050
51int of_n_addr_cells(struct device_node *np)
52{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060053 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100054
55 do {
56 if (np->parent)
57 np = np->parent;
58 ip = of_get_property(np, "#address-cells", NULL);
59 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070060 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100061 } while (np->parent);
62 /* No #address-cells property for the root node */
63 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
64}
65EXPORT_SYMBOL(of_n_addr_cells);
66
67int of_n_size_cells(struct device_node *np)
68{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060069 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100070
71 do {
72 if (np->parent)
73 np = np->parent;
74 ip = of_get_property(np, "#size-cells", NULL);
75 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070076 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100077 } while (np->parent);
78 /* No #size-cells property for the root node */
79 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
80}
81EXPORT_SYMBOL(of_n_size_cells);
82
Grant Likely0f22dd32012-02-15 20:38:40 -070083#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070084/**
85 * of_node_get - Increment refcount of a node
86 * @node: Node to inc refcount, NULL is supported to
87 * simplify writing of callers
88 *
89 * Returns node.
90 */
91struct device_node *of_node_get(struct device_node *node)
92{
93 if (node)
Grant Likelyf57a8e22014-02-20 18:02:11 +000094 kobject_get(&node->kobj);
Grant Likely923f7e32010-01-28 13:52:53 -070095 return node;
96}
97EXPORT_SYMBOL(of_node_get);
98
Grant Likelyf57a8e22014-02-20 18:02:11 +000099static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -0700100{
Grant Likelyf57a8e22014-02-20 18:02:11 +0000101 return container_of(kobj, struct device_node, kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700102}
103
104/**
105 * of_node_release - release a dynamically allocated node
106 * @kref: kref element of the node to be released
107 *
108 * In of_node_put() this function is passed to kref_put()
109 * as the destructor.
110 */
Grant Likelyf57a8e22014-02-20 18:02:11 +0000111static void of_node_release(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -0700112{
Grant Likelyf57a8e22014-02-20 18:02:11 +0000113 struct device_node *node = kobj_to_device_node(kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700114 struct property *prop = node->properties;
115
116 /* We should never be releasing nodes that haven't been detached. */
117 if (!of_node_check_flag(node, OF_DETACHED)) {
118 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
119 dump_stack();
Grant Likely923f7e32010-01-28 13:52:53 -0700120 return;
121 }
122
123 if (!of_node_check_flag(node, OF_DYNAMIC))
124 return;
125
126 while (prop) {
127 struct property *next = prop->next;
128 kfree(prop->name);
129 kfree(prop->value);
130 kfree(prop);
131 prop = next;
132
133 if (!prop) {
134 prop = node->deadprops;
135 node->deadprops = NULL;
136 }
137 }
138 kfree(node->full_name);
139 kfree(node->data);
140 kfree(node);
141}
142
143/**
144 * of_node_put - Decrement refcount of a node
145 * @node: Node to dec refcount, NULL is supported to
146 * simplify writing of callers
147 *
148 */
149void of_node_put(struct device_node *node)
150{
151 if (node)
Grant Likelyf57a8e22014-02-20 18:02:11 +0000152 kobject_put(&node->kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700153}
154EXPORT_SYMBOL(of_node_put);
Grant Likelyf57a8e22014-02-20 18:02:11 +0000155#else
156static void of_node_release(struct kobject *kobj)
157{
158 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
159}
Grant Likely0f22dd32012-02-15 20:38:40 -0700160#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700161
Grant Likelyf57a8e22014-02-20 18:02:11 +0000162struct kobj_type of_node_ktype = {
163 .release = of_node_release,
164};
165
166static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
167 struct bin_attribute *bin_attr, char *buf,
168 loff_t offset, size_t count)
169{
170 struct property *pp = container_of(bin_attr, struct property, attr);
171 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
172}
173
174static const char *safe_name(struct kobject *kobj, const char *orig_name)
175{
176 const char *name = orig_name;
177 struct sysfs_dirent *kn;
178 int i = 0;
179
180 /* don't be a hero. After 16 tries give up */
181 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, NULL, name))) {
182 sysfs_put(kn);
183 if (name != orig_name)
184 kfree(name);
185 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
186 }
187
188 if (name != orig_name)
189 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
190 kobject_name(kobj), name);
191 return name;
192}
193
194static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
195{
196 int rc;
197
198 /* Important: Don't leak passwords */
199 bool secure = strncmp(pp->name, "security-", 9) == 0;
200
201 sysfs_bin_attr_init(&pp->attr);
202 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
203 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
204 pp->attr.size = secure ? 0 : pp->length;
205 pp->attr.read = of_node_property_read;
206
207 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
208 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
209 return rc;
210}
211
212static int __of_node_add(struct device_node *np)
213{
214 const char *name;
215 struct property *pp;
216 int rc;
217
218 np->kobj.kset = of_kset;
219 if (!np->parent) {
220 /* Nodes without parents are new top level trees */
221 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
222 } else {
223 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
224 if (!name || !name[0])
225 return -EINVAL;
226
227 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
228 }
229 if (rc)
230 return rc;
231
232 for_each_property_of_node(np, pp)
233 __of_add_property_sysfs(np, pp);
234
235 return 0;
236}
237
238int of_node_add(struct device_node *np)
239{
240 int rc = 0;
241 kobject_init(&np->kobj, &of_node_ktype);
242 mutex_lock(&of_aliases_mutex);
243 if (of_kset)
244 rc = __of_node_add(np);
245 mutex_unlock(&of_aliases_mutex);
246 return rc;
247}
248
249#if defined(CONFIG_OF_DYNAMIC)
250static void of_node_remove(struct device_node *np)
251{
252 struct property *pp;
253
254 for_each_property_of_node(np, pp)
255 sysfs_remove_bin_file(&np->kobj, &pp->attr);
256
257 kobject_del(&np->kobj);
258}
259#endif
260
261static int __init of_init(void)
262{
263 struct device_node *np;
264
265 /* Create the kset, and register existing nodes */
266 mutex_lock(&of_aliases_mutex);
267 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
268 if (!of_kset) {
269 mutex_unlock(&of_aliases_mutex);
270 return -ENOMEM;
271 }
272 for_each_of_allnodes(np)
273 __of_node_add(np);
274 mutex_unlock(&of_aliases_mutex);
275
276#if !defined(CONFIG_PROC_DEVICETREE)
277 /* Symlink to the new tree when PROC_DEVICETREE is disabled */
278 if (of_allnodes)
279 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
280#endif /* CONFIG_PROC_DEVICETREE */
281
282 return 0;
283}
284core_initcall(of_init);
285
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500286static struct property *__of_find_property(const struct device_node *np,
287 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000288{
289 struct property *pp;
290
Timur Tabi64e45662008-05-08 05:19:59 +1000291 if (!np)
292 return NULL;
293
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530294 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000295 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530296 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000297 *lenp = pp->length;
298 break;
299 }
300 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500301
302 return pp;
303}
304
305struct property *of_find_property(const struct device_node *np,
306 const char *name,
307 int *lenp)
308{
309 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500310 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500311
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500312 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500313 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500314 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000315
316 return pp;
317}
318EXPORT_SYMBOL(of_find_property);
319
Grant Likelye91edcf2009-10-15 10:58:09 -0600320/**
321 * of_find_all_nodes - Get next node in global list
322 * @prev: Previous node or NULL to start iteration
323 * of_node_put() will be called on it
324 *
325 * Returns a node pointer with refcount incremented, use
326 * of_node_put() on it when done.
327 */
328struct device_node *of_find_all_nodes(struct device_node *prev)
329{
330 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000331 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600332
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000333 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000334 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600335 for (; np != NULL; np = np->allnext)
336 if (of_node_get(np))
337 break;
338 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000339 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600340 return np;
341}
342EXPORT_SYMBOL(of_find_all_nodes);
343
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000344/*
345 * Find a property with a given name for a given node
346 * and return the value.
347 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500348static const void *__of_get_property(const struct device_node *np,
349 const char *name, int *lenp)
350{
351 struct property *pp = __of_find_property(np, name, lenp);
352
353 return pp ? pp->value : NULL;
354}
355
356/*
357 * Find a property with a given name for a given node
358 * and return the value.
359 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000360const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500361 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000362{
363 struct property *pp = of_find_property(np, name, lenp);
364
365 return pp ? pp->value : NULL;
366}
367EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000368
369/** Checks if the given "compat" string matches one of the strings in
370 * the device's "compatible" property
371 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500372static int __of_device_is_compatible(const struct device_node *device,
373 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000374{
375 const char* cp;
376 int cplen, l;
377
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500378 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000379 if (cp == NULL)
380 return 0;
381 while (cplen > 0) {
382 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
383 return 1;
384 l = strlen(cp) + 1;
385 cp += l;
386 cplen -= l;
387 }
388
389 return 0;
390}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500391
392/** Checks if the given "compat" string matches one of the strings in
393 * the device's "compatible" property
394 */
395int of_device_is_compatible(const struct device_node *device,
396 const char *compat)
397{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500398 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500399 int res;
400
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500401 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500402 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500403 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500404 return res;
405}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000406EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000407
408/**
Grant Likely71a157e2010-02-01 21:34:14 -0700409 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700410 * @compat: compatible string to look for in root node's compatible property.
411 *
412 * Returns true if the root node has the given value in its
413 * compatible property.
414 */
Grant Likely71a157e2010-02-01 21:34:14 -0700415int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700416{
417 struct device_node *root;
418 int rc = 0;
419
420 root = of_find_node_by_path("/");
421 if (root) {
422 rc = of_device_is_compatible(root, compat);
423 of_node_put(root);
424 }
425 return rc;
426}
Grant Likely71a157e2010-02-01 21:34:14 -0700427EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700428
429/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700430 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100431 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700432 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100433 *
434 * Returns 1 if the status property is absent or set to "okay" or "ok",
435 * 0 otherwise
436 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700437static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100438{
439 const char *status;
440 int statlen;
441
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700442 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100443 if (status == NULL)
444 return 1;
445
446 if (statlen > 0) {
447 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
448 return 1;
449 }
450
451 return 0;
452}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700453
454/**
455 * of_device_is_available - check if a device is available for use
456 *
457 * @device: Node to check for availability
458 *
459 * Returns 1 if the status property is absent or set to "okay" or "ok",
460 * 0 otherwise
461 */
462int of_device_is_available(const struct device_node *device)
463{
464 unsigned long flags;
465 int res;
466
467 raw_spin_lock_irqsave(&devtree_lock, flags);
468 res = __of_device_is_available(device);
469 raw_spin_unlock_irqrestore(&devtree_lock, flags);
470 return res;
471
472}
Josh Boyer834d97d2008-03-27 00:33:14 +1100473EXPORT_SYMBOL(of_device_is_available);
474
475/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000476 * of_get_parent - Get a node's parent if any
477 * @node: Node to get parent
478 *
479 * Returns a node pointer with refcount incremented, use
480 * of_node_put() on it when done.
481 */
482struct device_node *of_get_parent(const struct device_node *node)
483{
484 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500485 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000486
487 if (!node)
488 return NULL;
489
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500490 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000491 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500492 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000493 return np;
494}
495EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000496
497/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000498 * of_get_next_parent - Iterate to a node's parent
499 * @node: Node to get parent of
500 *
501 * This is like of_get_parent() except that it drops the
502 * refcount on the passed node, making it suitable for iterating
503 * through a node's parents.
504 *
505 * Returns a node pointer with refcount incremented, use
506 * of_node_put() on it when done.
507 */
508struct device_node *of_get_next_parent(struct device_node *node)
509{
510 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500511 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000512
513 if (!node)
514 return NULL;
515
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500516 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000517 parent = of_node_get(node->parent);
518 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500519 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000520 return parent;
521}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300522EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000523
524/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000525 * of_get_next_child - Iterate a node childs
526 * @node: parent node
527 * @prev: previous child of the parent node, or NULL to get first
528 *
529 * Returns a node pointer with refcount incremented, use
530 * of_node_put() on it when done.
531 */
532struct device_node *of_get_next_child(const struct device_node *node,
533 struct device_node *prev)
534{
535 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500536 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000537
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500538 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000539 next = prev ? prev->sibling : node->child;
540 for (; next; next = next->sibling)
541 if (of_node_get(next))
542 break;
543 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500544 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000545 return next;
546}
547EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000548
549/**
Timur Tabi32961932012-08-14 13:20:23 +0000550 * of_get_next_available_child - Find the next available child node
551 * @node: parent node
552 * @prev: previous child of the parent node, or NULL to get first
553 *
554 * This function is like of_get_next_child(), except that it
555 * automatically skips any disabled nodes (i.e. status = "disabled").
556 */
557struct device_node *of_get_next_available_child(const struct device_node *node,
558 struct device_node *prev)
559{
560 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000561 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000562
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000563 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000564 next = prev ? prev->sibling : node->child;
565 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700566 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000567 continue;
568 if (of_node_get(next))
569 break;
570 }
571 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000572 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000573 return next;
574}
575EXPORT_SYMBOL(of_get_next_available_child);
576
577/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100578 * of_get_child_by_name - Find the child node by name for a given parent
579 * @node: parent node
580 * @name: child name to look for.
581 *
582 * This function looks for child node for given matching name
583 *
584 * Returns a node pointer if found, with refcount incremented, use
585 * of_node_put() on it when done.
586 * Returns NULL if node is not found.
587 */
588struct device_node *of_get_child_by_name(const struct device_node *node,
589 const char *name)
590{
591 struct device_node *child;
592
593 for_each_child_of_node(node, child)
594 if (child->name && (of_node_cmp(child->name, name) == 0))
595 break;
596 return child;
597}
598EXPORT_SYMBOL(of_get_child_by_name);
599
600/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000601 * of_find_node_by_path - Find a node matching a full OF path
602 * @path: The full path to match
603 *
604 * Returns a node pointer with refcount incremented, use
605 * of_node_put() on it when done.
606 */
607struct device_node *of_find_node_by_path(const char *path)
608{
Randy Dunlap465aac62012-11-30 10:01:51 +0000609 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500610 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000611
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500612 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000613 for (; np; np = np->allnext) {
614 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
615 && of_node_get(np))
616 break;
617 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500618 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000619 return np;
620}
621EXPORT_SYMBOL(of_find_node_by_path);
622
623/**
624 * of_find_node_by_name - Find a node by its "name" property
625 * @from: The node to start searching from or NULL, the node
626 * you pass will not be searched, only the next one
627 * will; typically, you pass what the previous call
628 * returned. of_node_put() will be called on it
629 * @name: The name string to match against
630 *
631 * Returns a node pointer with refcount incremented, use
632 * of_node_put() on it when done.
633 */
634struct device_node *of_find_node_by_name(struct device_node *from,
635 const char *name)
636{
637 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500638 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000639
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500640 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000641 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000642 for (; np; np = np->allnext)
643 if (np->name && (of_node_cmp(np->name, name) == 0)
644 && of_node_get(np))
645 break;
646 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500647 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000648 return np;
649}
650EXPORT_SYMBOL(of_find_node_by_name);
651
652/**
653 * of_find_node_by_type - Find a node by its "device_type" property
654 * @from: The node to start searching from, or NULL to start searching
655 * the entire device tree. The node you pass will not be
656 * searched, only the next one will; typically, you pass
657 * what the previous call returned. of_node_put() will be
658 * called on from for you.
659 * @type: The type string to match against
660 *
661 * Returns a node pointer with refcount incremented, use
662 * of_node_put() on it when done.
663 */
664struct device_node *of_find_node_by_type(struct device_node *from,
665 const char *type)
666{
667 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500668 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000669
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500670 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000671 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000672 for (; np; np = np->allnext)
673 if (np->type && (of_node_cmp(np->type, type) == 0)
674 && of_node_get(np))
675 break;
676 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500677 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000678 return np;
679}
680EXPORT_SYMBOL(of_find_node_by_type);
681
682/**
683 * of_find_compatible_node - Find a node based on type and one of the
684 * tokens in its "compatible" property
685 * @from: The node to start searching from or NULL, the node
686 * you pass will not be searched, only the next one
687 * will; typically, you pass what the previous call
688 * returned. of_node_put() will be called on it
689 * @type: The type string to match "device_type" or NULL to ignore
690 * @compatible: The string to match to one of the tokens in the device
691 * "compatible" list.
692 *
693 * Returns a node pointer with refcount incremented, use
694 * of_node_put() on it when done.
695 */
696struct device_node *of_find_compatible_node(struct device_node *from,
697 const char *type, const char *compatible)
698{
699 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500700 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000701
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500702 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000703 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000704 for (; np; np = np->allnext) {
705 if (type
706 && !(np->type && (of_node_cmp(np->type, type) == 0)))
707 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500708 if (__of_device_is_compatible(np, compatible) &&
709 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000710 break;
711 }
712 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500713 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000714 return np;
715}
716EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100717
718/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000719 * of_find_node_with_property - Find a node which has a property with
720 * the given name.
721 * @from: The node to start searching from or NULL, the node
722 * you pass will not be searched, only the next one
723 * will; typically, you pass what the previous call
724 * returned. of_node_put() will be called on it
725 * @prop_name: The name of the property to look for.
726 *
727 * Returns a node pointer with refcount incremented, use
728 * of_node_put() on it when done.
729 */
730struct device_node *of_find_node_with_property(struct device_node *from,
731 const char *prop_name)
732{
733 struct device_node *np;
734 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500735 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000736
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500737 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000738 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000739 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530740 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000741 if (of_prop_cmp(pp->name, prop_name) == 0) {
742 of_node_get(np);
743 goto out;
744 }
745 }
746 }
747out:
748 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500749 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000750 return np;
751}
752EXPORT_SYMBOL(of_find_node_with_property);
753
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500754static
755const struct of_device_id *__of_match_node(const struct of_device_id *matches,
756 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100757{
Grant Likelya52f07e2011-03-18 10:21:29 -0600758 if (!matches)
759 return NULL;
760
Grant Likely283029d2008-01-09 06:20:40 +1100761 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
762 int match = 1;
763 if (matches->name[0])
764 match &= node->name
765 && !strcmp(matches->name, node->name);
766 if (matches->type[0])
767 match &= node->type
768 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700769 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500770 match &= __of_device_is_compatible(node,
771 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700772 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100773 return matches;
774 matches++;
775 }
776 return NULL;
777}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500778
779/**
780 * of_match_node - Tell if an device_node has a matching of_match structure
781 * @matches: array of of device match structures to search in
782 * @node: the of device structure to match against
783 *
784 * Low level utility function used by device matching.
785 */
786const struct of_device_id *of_match_node(const struct of_device_id *matches,
787 const struct device_node *node)
788{
789 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500790 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500791
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500792 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500793 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500794 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500795 return match;
796}
Grant Likely283029d2008-01-09 06:20:40 +1100797EXPORT_SYMBOL(of_match_node);
798
799/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700800 * of_find_matching_node_and_match - Find a node based on an of_device_id
801 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100802 * @from: The node to start searching from or NULL, the node
803 * you pass will not be searched, only the next one
804 * will; typically, you pass what the previous call
805 * returned. of_node_put() will be called on it
806 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700807 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100808 *
809 * Returns a node pointer with refcount incremented, use
810 * of_node_put() on it when done.
811 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700812struct device_node *of_find_matching_node_and_match(struct device_node *from,
813 const struct of_device_id *matches,
814 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100815{
816 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800817 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500818 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100819
Stephen Warren50c8af42012-11-20 16:12:20 -0700820 if (match)
821 *match = NULL;
822
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500823 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000824 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100825 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500826 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800827 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700828 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800829 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100830 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700831 }
Grant Likely283029d2008-01-09 06:20:40 +1100832 }
833 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500834 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100835 return np;
836}
Grant Likely80c20222012-12-19 10:45:36 +0000837EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400838
839/**
Grant Likely3f07af42008-07-25 22:25:13 -0400840 * of_modalias_node - Lookup appropriate modalias for a device node
841 * @node: pointer to a device tree node
842 * @modalias: Pointer to buffer that modalias value will be copied into
843 * @len: Length of modalias value
844 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600845 * Based on the value of the compatible property, this routine will attempt
846 * to choose an appropriate modalias value for a particular device tree node.
847 * It does this by stripping the manufacturer prefix (as delimited by a ',')
848 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400849 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600850 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400851 */
852int of_modalias_node(struct device_node *node, char *modalias, int len)
853{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600854 const char *compatible, *p;
855 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400856
857 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600858 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400859 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400860 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600861 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400862 return 0;
863}
864EXPORT_SYMBOL_GPL(of_modalias_node);
865
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000866/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700867 * of_find_node_by_phandle - Find a node given a phandle
868 * @handle: phandle of the node to find
869 *
870 * Returns a node pointer with refcount incremented, use
871 * of_node_put() on it when done.
872 */
873struct device_node *of_find_node_by_phandle(phandle handle)
874{
875 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000876 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -0700877
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000878 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000879 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700880 if (np->phandle == handle)
881 break;
882 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000883 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700884 return np;
885}
886EXPORT_SYMBOL(of_find_node_by_phandle);
887
888/**
Heiko Stuebner10408542014-02-12 01:00:34 +0100889 * of_property_count_elems_of_size - Count the number of elements in a property
890 *
891 * @np: device node from which the property value is to be read.
892 * @propname: name of the property to be searched.
893 * @elem_size: size of the individual element
894 *
895 * Search for a property in a device node and count the number of elements of
896 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
897 * property does not exist or its length does not match a multiple of elem_size
898 * and -ENODATA if the property does not have a value.
899 */
900int of_property_count_elems_of_size(const struct device_node *np,
901 const char *propname, int elem_size)
902{
903 struct property *prop = of_find_property(np, propname, NULL);
904
905 if (!prop)
906 return -EINVAL;
907 if (!prop->value)
908 return -ENODATA;
909
910 if (prop->length % elem_size != 0) {
911 pr_err("size of %s in node %s is not a multiple of %d\n",
912 propname, np->full_name, elem_size);
913 return -EINVAL;
914 }
915
916 return prop->length / elem_size;
917}
918EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
919
920/**
Tony Priskdaeec1f2013-04-03 17:57:11 +1300921 * of_find_property_value_of_size
922 *
923 * @np: device node from which the property value is to be read.
924 * @propname: name of the property to be searched.
925 * @len: requested length of property value
926 *
927 * Search for a property in a device node and valid the requested size.
928 * Returns the property value on success, -EINVAL if the property does not
929 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
930 * property data isn't large enough.
931 *
932 */
933static void *of_find_property_value_of_size(const struct device_node *np,
934 const char *propname, u32 len)
935{
936 struct property *prop = of_find_property(np, propname, NULL);
937
938 if (!prop)
939 return ERR_PTR(-EINVAL);
940 if (!prop->value)
941 return ERR_PTR(-ENODATA);
942 if (len > prop->length)
943 return ERR_PTR(-EOVERFLOW);
944
945 return prop->value;
946}
947
948/**
Tony Prisk3daf3722013-03-23 17:02:15 +1300949 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
950 *
951 * @np: device node from which the property value is to be read.
952 * @propname: name of the property to be searched.
953 * @index: index of the u32 in the list of values
954 * @out_value: pointer to return value, modified only if no error.
955 *
956 * Search for a property in a device node and read nth 32-bit value from
957 * it. Returns 0 on success, -EINVAL if the property does not exist,
958 * -ENODATA if property does not have a value, and -EOVERFLOW if the
959 * property data isn't large enough.
960 *
961 * The out_value is modified only if a valid u32 value can be decoded.
962 */
963int of_property_read_u32_index(const struct device_node *np,
964 const char *propname,
965 u32 index, u32 *out_value)
966{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300967 const u32 *val = of_find_property_value_of_size(np, propname,
968 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +1300969
Tony Priskdaeec1f2013-04-03 17:57:11 +1300970 if (IS_ERR(val))
971 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +1300972
Tony Priskdaeec1f2013-04-03 17:57:11 +1300973 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +1300974 return 0;
975}
976EXPORT_SYMBOL_GPL(of_property_read_u32_index);
977
978/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530979 * of_property_read_u8_array - Find and read an array of u8 from a property.
980 *
981 * @np: device node from which the property value is to be read.
982 * @propname: name of the property to be searched.
983 * @out_value: pointer to return value, modified only if return value is 0.
984 * @sz: number of array elements to read
985 *
986 * Search for a property in a device node and read 8-bit value(s) from
987 * it. Returns 0 on success, -EINVAL if the property does not exist,
988 * -ENODATA if property does not have a value, and -EOVERFLOW if the
989 * property data isn't large enough.
990 *
991 * dts entry of array should be like:
992 * property = /bits/ 8 <0x50 0x60 0x70>;
993 *
994 * The out_value is modified only if a valid u8 value can be decoded.
995 */
996int of_property_read_u8_array(const struct device_node *np,
997 const char *propname, u8 *out_values, size_t sz)
998{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300999 const u8 *val = of_find_property_value_of_size(np, propname,
1000 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301001
Tony Priskdaeec1f2013-04-03 17:57:11 +13001002 if (IS_ERR(val))
1003 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301004
Viresh Kumarbe193242012-11-20 10:15:19 +05301005 while (sz--)
1006 *out_values++ = *val++;
1007 return 0;
1008}
1009EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1010
1011/**
1012 * of_property_read_u16_array - Find and read an array of u16 from a property.
1013 *
1014 * @np: device node from which the property value is to be read.
1015 * @propname: name of the property to be searched.
1016 * @out_value: pointer to return value, modified only if return value is 0.
1017 * @sz: number of array elements to read
1018 *
1019 * Search for a property in a device node and read 16-bit value(s) from
1020 * it. Returns 0 on success, -EINVAL if the property does not exist,
1021 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1022 * property data isn't large enough.
1023 *
1024 * dts entry of array should be like:
1025 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1026 *
1027 * The out_value is modified only if a valid u16 value can be decoded.
1028 */
1029int of_property_read_u16_array(const struct device_node *np,
1030 const char *propname, u16 *out_values, size_t sz)
1031{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001032 const __be16 *val = of_find_property_value_of_size(np, propname,
1033 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301034
Tony Priskdaeec1f2013-04-03 17:57:11 +13001035 if (IS_ERR(val))
1036 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301037
Viresh Kumarbe193242012-11-20 10:15:19 +05301038 while (sz--)
1039 *out_values++ = be16_to_cpup(val++);
1040 return 0;
1041}
1042EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1043
1044/**
Rob Herring0e373632011-07-06 15:42:58 -05001045 * of_property_read_u32_array - Find and read an array of 32 bit integers
1046 * from a property.
1047 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301048 * @np: device node from which the property value is to be read.
1049 * @propname: name of the property to be searched.
1050 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301051 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301052 *
Rob Herring0e373632011-07-06 15:42:58 -05001053 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301054 * it. Returns 0 on success, -EINVAL if the property does not exist,
1055 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1056 * property data isn't large enough.
1057 *
1058 * The out_value is modified only if a valid u32 value can be decoded.
1059 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001060int of_property_read_u32_array(const struct device_node *np,
1061 const char *propname, u32 *out_values,
1062 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301063{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001064 const __be32 *val = of_find_property_value_of_size(np, propname,
1065 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301066
Tony Priskdaeec1f2013-04-03 17:57:11 +13001067 if (IS_ERR(val))
1068 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001069
Rob Herring0e373632011-07-06 15:42:58 -05001070 while (sz--)
1071 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301072 return 0;
1073}
Rob Herring0e373632011-07-06 15:42:58 -05001074EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301075
1076/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001077 * of_property_read_u64 - Find and read a 64 bit integer from a property
1078 * @np: device node from which the property value is to be read.
1079 * @propname: name of the property to be searched.
1080 * @out_value: pointer to return value, modified only if return value is 0.
1081 *
1082 * Search for a property in a device node and read a 64-bit value from
1083 * it. Returns 0 on success, -EINVAL if the property does not exist,
1084 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1085 * property data isn't large enough.
1086 *
1087 * The out_value is modified only if a valid u64 value can be decoded.
1088 */
1089int of_property_read_u64(const struct device_node *np, const char *propname,
1090 u64 *out_value)
1091{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001092 const __be32 *val = of_find_property_value_of_size(np, propname,
1093 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001094
Tony Priskdaeec1f2013-04-03 17:57:11 +13001095 if (IS_ERR(val))
1096 return PTR_ERR(val);
1097
1098 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001099 return 0;
1100}
1101EXPORT_SYMBOL_GPL(of_property_read_u64);
1102
1103/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301104 * of_property_read_string - Find and read a string from a property
1105 * @np: device node from which the property value is to be read.
1106 * @propname: name of the property to be searched.
1107 * @out_string: pointer to null terminated return string, modified only if
1108 * return value is 0.
1109 *
1110 * Search for a property in a device tree node and retrieve a null
1111 * terminated string value (pointer to data, not a copy). Returns 0 on
1112 * success, -EINVAL if the property does not exist, -ENODATA if property
1113 * does not have a value, and -EILSEQ if the string is not null-terminated
1114 * within the length of the property data.
1115 *
1116 * The out_string pointer is modified only if a valid string can be decoded.
1117 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001118int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001119 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301120{
1121 struct property *prop = of_find_property(np, propname, NULL);
1122 if (!prop)
1123 return -EINVAL;
1124 if (!prop->value)
1125 return -ENODATA;
1126 if (strnlen(prop->value, prop->length) >= prop->length)
1127 return -EILSEQ;
1128 *out_string = prop->value;
1129 return 0;
1130}
1131EXPORT_SYMBOL_GPL(of_property_read_string);
1132
1133/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001134 * of_property_read_string_index - Find and read a string from a multiple
1135 * strings property.
1136 * @np: device node from which the property value is to be read.
1137 * @propname: name of the property to be searched.
1138 * @index: index of the string in the list of strings
1139 * @out_string: pointer to null terminated return string, modified only if
1140 * return value is 0.
1141 *
1142 * Search for a property in a device tree node and retrieve a null
1143 * terminated string value (pointer to data, not a copy) in the list of strings
1144 * contained in that property.
1145 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1146 * property does not have a value, and -EILSEQ if the string is not
1147 * null-terminated within the length of the property data.
1148 *
1149 * The out_string pointer is modified only if a valid string can be decoded.
1150 */
1151int of_property_read_string_index(struct device_node *np, const char *propname,
1152 int index, const char **output)
1153{
1154 struct property *prop = of_find_property(np, propname, NULL);
1155 int i = 0;
1156 size_t l = 0, total = 0;
1157 const char *p;
1158
1159 if (!prop)
1160 return -EINVAL;
1161 if (!prop->value)
1162 return -ENODATA;
1163 if (strnlen(prop->value, prop->length) >= prop->length)
1164 return -EILSEQ;
1165
1166 p = prop->value;
1167
1168 for (i = 0; total < prop->length; total += l, p += l) {
1169 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001170 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001171 *output = p;
1172 return 0;
1173 }
1174 }
1175 return -ENODATA;
1176}
1177EXPORT_SYMBOL_GPL(of_property_read_string_index);
1178
Grant Likely7aff0fe2011-12-12 09:25:58 -07001179/**
1180 * of_property_match_string() - Find string in a list and return index
1181 * @np: pointer to node containing string list property
1182 * @propname: string list property name
1183 * @string: pointer to string to search for in string list
1184 *
1185 * This function searches a string list property and returns the index
1186 * of a specific string value.
1187 */
1188int of_property_match_string(struct device_node *np, const char *propname,
1189 const char *string)
1190{
1191 struct property *prop = of_find_property(np, propname, NULL);
1192 size_t l;
1193 int i;
1194 const char *p, *end;
1195
1196 if (!prop)
1197 return -EINVAL;
1198 if (!prop->value)
1199 return -ENODATA;
1200
1201 p = prop->value;
1202 end = p + prop->length;
1203
1204 for (i = 0; p < end; i++, p += l) {
1205 l = strlen(p) + 1;
1206 if (p + l > end)
1207 return -EILSEQ;
1208 pr_debug("comparing %s with %s\n", string, p);
1209 if (strcmp(string, p) == 0)
1210 return i; /* Found it; return index */
1211 }
1212 return -ENODATA;
1213}
1214EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001215
1216/**
1217 * of_property_count_strings - Find and return the number of strings from a
1218 * multiple strings property.
1219 * @np: device node from which the property value is to be read.
1220 * @propname: name of the property to be searched.
1221 *
1222 * Search for a property in a device tree node and retrieve the number of null
1223 * terminated string contain in it. Returns the number of strings on
1224 * success, -EINVAL if the property does not exist, -ENODATA if property
1225 * does not have a value, and -EILSEQ if the string is not null-terminated
1226 * within the length of the property data.
1227 */
1228int of_property_count_strings(struct device_node *np, const char *propname)
1229{
1230 struct property *prop = of_find_property(np, propname, NULL);
1231 int i = 0;
1232 size_t l = 0, total = 0;
1233 const char *p;
1234
1235 if (!prop)
1236 return -EINVAL;
1237 if (!prop->value)
1238 return -ENODATA;
1239 if (strnlen(prop->value, prop->length) >= prop->length)
1240 return -EILSEQ;
1241
1242 p = prop->value;
1243
Benoit Cousson88af7f52011-12-05 15:23:54 +01001244 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001245 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001246
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001247 return i;
1248}
1249EXPORT_SYMBOL_GPL(of_property_count_strings);
1250
Grant Likelybd69f732013-02-10 22:57:21 +00001251static int __of_parse_phandle_with_args(const struct device_node *np,
1252 const char *list_name,
Stephen Warrenf35c0932013-08-14 15:27:10 -06001253 const char *cells_name,
1254 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001255 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001256{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001257 const __be32 *list, *list_end;
Grant Likely23ce04c02013-02-12 21:21:49 +00001258 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001259 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001260 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001261 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001262
Grant Likely15c9a0a2011-12-12 09:25:57 -07001263 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001264 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001265 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001266 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001267 list_end = list + size / sizeof(*list);
1268
Grant Likely15c9a0a2011-12-12 09:25:57 -07001269 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001270 while (list < list_end) {
Grant Likely23ce04c02013-02-12 21:21:49 +00001271 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001272 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001273
Grant Likely15c9a0a2011-12-12 09:25:57 -07001274 /*
1275 * If phandle is 0, then it is an empty entry with no
1276 * arguments. Skip forward to the next entry.
1277 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001278 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001279 if (phandle) {
1280 /*
1281 * Find the provider node and parse the #*-cells
Stephen Warren3a9560b2013-08-14 15:27:11 -06001282 * property to determine the argument length.
1283 *
1284 * This is not needed if the cell count is hard-coded
1285 * (i.e. cells_name not set, but cell_count is set),
1286 * except when we're going to return the found node
1287 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001288 */
Stephen Warren3a9560b2013-08-14 15:27:11 -06001289 if (cells_name || cur_index == index) {
1290 node = of_find_node_by_phandle(phandle);
1291 if (!node) {
1292 pr_err("%s: could not find phandle\n",
1293 np->full_name);
1294 goto err;
1295 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001296 }
Stephen Warrenf35c0932013-08-14 15:27:10 -06001297
1298 if (cells_name) {
1299 if (of_property_read_u32(node, cells_name,
1300 &count)) {
1301 pr_err("%s: could not get %s for %s\n",
1302 np->full_name, cells_name,
1303 node->full_name);
1304 goto err;
1305 }
1306 } else {
1307 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001308 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001309
Grant Likely15c9a0a2011-12-12 09:25:57 -07001310 /*
1311 * Make sure that the arguments actually fit in the
1312 * remaining property data length
1313 */
1314 if (list + count > list_end) {
1315 pr_err("%s: arguments longer than property\n",
1316 np->full_name);
Grant Likely23ce04c02013-02-12 21:21:49 +00001317 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001318 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001319 }
1320
Grant Likely15c9a0a2011-12-12 09:25:57 -07001321 /*
1322 * All of the error cases above bail out of the loop, so at
1323 * this point, the parsing is successful. If the requested
1324 * index matches, then fill the out_args structure and return,
1325 * or return -ENOENT for an empty entry.
1326 */
Grant Likely23ce04c02013-02-12 21:21:49 +00001327 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001328 if (cur_index == index) {
1329 if (!phandle)
Grant Likely23ce04c02013-02-12 21:21:49 +00001330 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001331
Grant Likely15c9a0a2011-12-12 09:25:57 -07001332 if (out_args) {
1333 int i;
1334 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1335 count = MAX_PHANDLE_ARGS;
1336 out_args->np = node;
1337 out_args->args_count = count;
1338 for (i = 0; i < count; i++)
1339 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001340 } else {
1341 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001342 }
Grant Likely23ce04c02013-02-12 21:21:49 +00001343
1344 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001345 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001346 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001347
1348 of_node_put(node);
1349 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001350 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001351 cur_index++;
1352 }
1353
Grant Likely23ce04c02013-02-12 21:21:49 +00001354 /*
1355 * Unlock node before returning result; will be one of:
1356 * -ENOENT : index is for empty phandle
1357 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001358 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c02013-02-12 21:21:49 +00001359 */
Grant Likelybd69f732013-02-10 22:57:21 +00001360 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c02013-02-12 21:21:49 +00001361 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001362 if (node)
1363 of_node_put(node);
Grant Likely23ce04c02013-02-12 21:21:49 +00001364 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001365}
Grant Likelybd69f732013-02-10 22:57:21 +00001366
Stephen Warrend2621342013-08-14 15:27:08 -06001367/**
Stephen Warren73ed9312013-08-14 15:27:09 -06001368 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1369 * @np: Pointer to device node holding phandle property
1370 * @phandle_name: Name of property holding a phandle value
1371 * @index: For properties holding a table of phandles, this is the index into
1372 * the table
1373 *
1374 * Returns the device_node pointer with refcount incremented. Use
1375 * of_node_put() on it when done.
1376 */
1377struct device_node *of_parse_phandle(const struct device_node *np,
1378 const char *phandle_name, int index)
1379{
Stephen Warren3a9560b2013-08-14 15:27:11 -06001380 struct of_phandle_args args;
Stephen Warren73ed9312013-08-14 15:27:09 -06001381
Stephen Warren3a9560b2013-08-14 15:27:11 -06001382 if (index < 0)
Stephen Warren73ed9312013-08-14 15:27:09 -06001383 return NULL;
1384
Stephen Warren3a9560b2013-08-14 15:27:11 -06001385 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1386 index, &args))
1387 return NULL;
1388
1389 return args.np;
Stephen Warren73ed9312013-08-14 15:27:09 -06001390}
1391EXPORT_SYMBOL(of_parse_phandle);
1392
1393/**
Stephen Warrend2621342013-08-14 15:27:08 -06001394 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1395 * @np: pointer to a device tree node containing a list
1396 * @list_name: property name that contains a list
1397 * @cells_name: property name that specifies phandles' arguments count
1398 * @index: index of a phandle to parse out
1399 * @out_args: optional pointer to output arguments structure (will be filled)
1400 *
1401 * This function is useful to parse lists of phandles and their arguments.
1402 * Returns 0 on success and fills out_args, on error returns appropriate
1403 * errno value.
1404 *
1405 * Caller is responsible to call of_node_put() on the returned out_args->node
1406 * pointer.
1407 *
1408 * Example:
1409 *
1410 * phandle1: node1 {
1411 * #list-cells = <2>;
1412 * }
1413 *
1414 * phandle2: node2 {
1415 * #list-cells = <1>;
1416 * }
1417 *
1418 * node3 {
1419 * list = <&phandle1 1 2 &phandle2 3>;
1420 * }
1421 *
1422 * To get a device_node of the `node2' node you may call this:
1423 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1424 */
Grant Likelybd69f732013-02-10 22:57:21 +00001425int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1426 const char *cells_name, int index,
1427 struct of_phandle_args *out_args)
1428{
1429 if (index < 0)
1430 return -EINVAL;
Stephen Warrenf35c0932013-08-14 15:27:10 -06001431 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1432 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001433}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001434EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001435
Grant Likelybd69f732013-02-10 22:57:21 +00001436/**
Stephen Warrenf35c0932013-08-14 15:27:10 -06001437 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1438 * @np: pointer to a device tree node containing a list
1439 * @list_name: property name that contains a list
1440 * @cell_count: number of argument cells following the phandle
1441 * @index: index of a phandle to parse out
1442 * @out_args: optional pointer to output arguments structure (will be filled)
1443 *
1444 * This function is useful to parse lists of phandles and their arguments.
1445 * Returns 0 on success and fills out_args, on error returns appropriate
1446 * errno value.
1447 *
1448 * Caller is responsible to call of_node_put() on the returned out_args->node
1449 * pointer.
1450 *
1451 * Example:
1452 *
1453 * phandle1: node1 {
1454 * }
1455 *
1456 * phandle2: node2 {
1457 * }
1458 *
1459 * node3 {
1460 * list = <&phandle1 0 2 &phandle2 2 3>;
1461 * }
1462 *
1463 * To get a device_node of the `node2' node you may call this:
1464 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1465 */
1466int of_parse_phandle_with_fixed_args(const struct device_node *np,
1467 const char *list_name, int cell_count,
1468 int index, struct of_phandle_args *out_args)
1469{
1470 if (index < 0)
1471 return -EINVAL;
1472 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1473 index, out_args);
1474}
1475EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1476
1477/**
Grant Likelybd69f732013-02-10 22:57:21 +00001478 * of_count_phandle_with_args() - Find the number of phandles references in a property
1479 * @np: pointer to a device tree node containing a list
1480 * @list_name: property name that contains a list
1481 * @cells_name: property name that specifies phandles' arguments count
1482 *
1483 * Returns the number of phandle + argument tuples within a property. It
1484 * is a typical pattern to encode a list of phandle and variable
1485 * arguments into a single property. The number of arguments is encoded
1486 * by a property in the phandle-target node. For example, a gpios
1487 * property would contain a list of GPIO specifies consisting of a
1488 * phandle and 1 or more arguments. The number of arguments are
1489 * determined by the #gpio-cells property in the node pointed to by the
1490 * phandle.
1491 */
1492int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1493 const char *cells_name)
1494{
Stephen Warrenf35c0932013-08-14 15:27:10 -06001495 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1496 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001497}
1498EXPORT_SYMBOL(of_count_phandle_with_args);
1499
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001500#if defined(CONFIG_OF_DYNAMIC)
1501static int of_property_notify(int action, struct device_node *np,
1502 struct property *prop)
1503{
1504 struct of_prop_reconfig pr;
1505
1506 pr.dn = np;
1507 pr.prop = prop;
1508 return of_reconfig_notify(action, &pr);
1509}
1510#else
1511static int of_property_notify(int action, struct device_node *np,
1512 struct property *prop)
1513{
1514 return 0;
1515}
1516#endif
1517
Grant Likely02af11b2009-11-23 20:16:45 -07001518/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001519 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001520 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001521int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001522{
1523 struct property **next;
1524 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001525 int rc;
1526
1527 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1528 if (rc)
1529 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001530
1531 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001532 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001533 next = &np->properties;
1534 while (*next) {
1535 if (strcmp(prop->name, (*next)->name) == 0) {
1536 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001537 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001538 return -1;
1539 }
1540 next = &(*next)->next;
1541 }
1542 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001543 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelyf57a8e22014-02-20 18:02:11 +00001544 if (rc)
1545 return rc;
1546
1547 /* at early boot, bail hear and defer setup to of_init() */
1548 if (!of_kset)
1549 return 0;
1550
1551 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001552
1553#ifdef CONFIG_PROC_DEVICETREE
1554 /* try to add to proc as well if it was initialized */
1555 if (np->pde)
1556 proc_device_tree_add_prop(np->pde, prop);
1557#endif /* CONFIG_PROC_DEVICETREE */
1558
1559 return 0;
1560}
1561
1562/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001563 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001564 *
1565 * Note that we don't actually remove it, since we have given out
1566 * who-knows-how-many pointers to the data using get-property.
1567 * Instead we just move the property to the "dead properties"
1568 * list, so it won't be found any more.
1569 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001570int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001571{
1572 struct property **next;
1573 unsigned long flags;
1574 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001575 int rc;
1576
1577 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1578 if (rc)
1579 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001580
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001581 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001582 next = &np->properties;
1583 while (*next) {
1584 if (*next == prop) {
1585 /* found the node */
1586 *next = prop->next;
1587 prop->next = np->deadprops;
1588 np->deadprops = prop;
1589 found = 1;
1590 break;
1591 }
1592 next = &(*next)->next;
1593 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001594 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001595
1596 if (!found)
1597 return -ENODEV;
1598
Grant Likelyf57a8e22014-02-20 18:02:11 +00001599 /* at early boot, bail hear and defer setup to of_init() */
1600 if (!of_kset)
1601 return 0;
1602
1603 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1604
Grant Likely02af11b2009-11-23 20:16:45 -07001605#ifdef CONFIG_PROC_DEVICETREE
1606 /* try to remove the proc node as well */
1607 if (np->pde)
1608 proc_device_tree_remove_prop(np->pde, prop);
1609#endif /* CONFIG_PROC_DEVICETREE */
1610
1611 return 0;
1612}
1613
1614/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001615 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001616 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001617 *
1618 * Note that we don't actually remove it, since we have given out
1619 * who-knows-how-many pointers to the data using get-property.
1620 * Instead we just move the property to the "dead properties" list,
1621 * and add the new property to the property list
1622 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001623int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001624{
Dong Aisheng475d0092012-07-11 15:16:37 +10001625 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001626 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001627 int rc, found = 0;
1628
1629 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1630 if (rc)
1631 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001632
Dong Aisheng475d0092012-07-11 15:16:37 +10001633 if (!newprop->name)
1634 return -EINVAL;
1635
1636 oldprop = of_find_property(np, newprop->name, NULL);
1637 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001638 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001639
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001640 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001641 next = &np->properties;
1642 while (*next) {
1643 if (*next == oldprop) {
1644 /* found the node */
1645 newprop->next = oldprop->next;
1646 *next = newprop;
1647 oldprop->next = np->deadprops;
1648 np->deadprops = oldprop;
1649 found = 1;
1650 break;
1651 }
1652 next = &(*next)->next;
1653 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001654 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelyf57a8e22014-02-20 18:02:11 +00001655 if (rc)
1656 return rc;
1657
1658 /* Update the sysfs attribute */
1659 if (oldprop)
1660 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1661 __of_add_property_sysfs(np, newprop);
Grant Likely02af11b2009-11-23 20:16:45 -07001662
1663 if (!found)
1664 return -ENODEV;
1665
1666#ifdef CONFIG_PROC_DEVICETREE
1667 /* try to add to proc as well if it was initialized */
1668 if (np->pde)
1669 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1670#endif /* CONFIG_PROC_DEVICETREE */
1671
1672 return 0;
1673}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001674
1675#if defined(CONFIG_OF_DYNAMIC)
1676/*
1677 * Support for dynamic device trees.
1678 *
1679 * On some platforms, the device tree can be manipulated at runtime.
1680 * The routines in this section support adding, removing and changing
1681 * device tree nodes.
1682 */
1683
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001684static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1685
1686int of_reconfig_notifier_register(struct notifier_block *nb)
1687{
1688 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1689}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001690EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001691
1692int of_reconfig_notifier_unregister(struct notifier_block *nb)
1693{
1694 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1695}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001696EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001697
1698int of_reconfig_notify(unsigned long action, void *p)
1699{
1700 int rc;
1701
1702 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1703 return notifier_to_errno(rc);
1704}
1705
Nathan Fontenote81b3292012-10-02 16:55:01 +00001706#ifdef CONFIG_PROC_DEVICETREE
1707static void of_add_proc_dt_entry(struct device_node *dn)
1708{
1709 struct proc_dir_entry *ent;
1710
1711 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1712 if (ent)
1713 proc_device_tree_add_node(dn, ent);
1714}
1715#else
1716static void of_add_proc_dt_entry(struct device_node *dn)
1717{
1718 return;
1719}
1720#endif
1721
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001722/**
1723 * of_attach_node - Plug a device node into the tree and global list.
1724 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001725int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001726{
1727 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001728 int rc;
1729
1730 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1731 if (rc)
1732 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001733
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001734 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001735 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001736 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001737 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001738 of_allnodes = np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001739 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001740
Grant Likelyf57a8e22014-02-20 18:02:11 +00001741 of_node_add(np);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001742 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001743 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001744}
1745
Nathan Fontenote81b3292012-10-02 16:55:01 +00001746#ifdef CONFIG_PROC_DEVICETREE
1747static void of_remove_proc_dt_entry(struct device_node *dn)
1748{
David Howellsa8ca16e2013-04-12 17:27:28 +01001749 proc_remove(dn->pde);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001750}
1751#else
1752static void of_remove_proc_dt_entry(struct device_node *dn)
1753{
1754 return;
1755}
1756#endif
1757
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001758/**
1759 * of_detach_node - "Unplug" a node from the device tree.
1760 *
1761 * The caller must hold a reference to the node. The memory associated with
1762 * the node is not freed until its refcount goes to zero.
1763 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001764int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001765{
1766 struct device_node *parent;
1767 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001768 int rc = 0;
1769
1770 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1771 if (rc)
1772 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001773
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001774 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001775
Nathan Fontenote81b3292012-10-02 16:55:01 +00001776 if (of_node_check_flag(np, OF_DETACHED)) {
1777 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001778 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001779 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001780 }
1781
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001782 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001783 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001784 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001785 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001786 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001787
Randy Dunlap465aac62012-11-30 10:01:51 +00001788 if (of_allnodes == np)
1789 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001790 else {
1791 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001792 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001793 prev->allnext != np;
1794 prev = prev->allnext)
1795 ;
1796 prev->allnext = np->allnext;
1797 }
1798
1799 if (parent->child == np)
1800 parent->child = np->sibling;
1801 else {
1802 struct device_node *prevsib;
1803 for (prevsib = np->parent->child;
1804 prevsib->sibling != np;
1805 prevsib = prevsib->sibling)
1806 ;
1807 prevsib->sibling = np->sibling;
1808 }
1809
1810 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001811 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001812
1813 of_remove_proc_dt_entry(np);
Grant Likelyf57a8e22014-02-20 18:02:11 +00001814 of_node_remove(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001815 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001816}
1817#endif /* defined(CONFIG_OF_DYNAMIC) */
1818
Shawn Guo611cad72011-08-15 15:28:14 +08001819static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1820 int id, const char *stem, int stem_len)
1821{
1822 ap->np = np;
1823 ap->id = id;
1824 strncpy(ap->stem, stem, stem_len);
1825 ap->stem[stem_len] = 0;
1826 list_add_tail(&ap->link, &aliases_lookup);
1827 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001828 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001829}
1830
1831/**
1832 * of_alias_scan - Scan all properties of 'aliases' node
1833 *
1834 * The function scans all the properties of 'aliases' node and populate
1835 * the the global lookup table with the properties. It returns the
1836 * number of alias_prop found, or error code in error case.
1837 *
1838 * @dt_alloc: An allocator that provides a virtual address to memory
1839 * for the resulting tree
1840 */
1841void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1842{
1843 struct property *pp;
1844
1845 of_chosen = of_find_node_by_path("/chosen");
1846 if (of_chosen == NULL)
1847 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauerca257782013-08-05 14:40:44 +02001848
1849 if (of_chosen) {
1850 const char *name;
1851
1852 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1853 if (name)
1854 of_stdout = of_find_node_by_path(name);
1855 }
1856
Shawn Guo611cad72011-08-15 15:28:14 +08001857 of_aliases = of_find_node_by_path("/aliases");
1858 if (!of_aliases)
1859 return;
1860
Dong Aisheng8af0da92011-12-22 20:19:24 +08001861 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001862 const char *start = pp->name;
1863 const char *end = start + strlen(start);
1864 struct device_node *np;
1865 struct alias_prop *ap;
1866 int id, len;
1867
1868 /* Skip those we do not want to proceed */
1869 if (!strcmp(pp->name, "name") ||
1870 !strcmp(pp->name, "phandle") ||
1871 !strcmp(pp->name, "linux,phandle"))
1872 continue;
1873
1874 np = of_find_node_by_path(pp->value);
1875 if (!np)
1876 continue;
1877
1878 /* walk the alias backwards to extract the id and work out
1879 * the 'stem' string */
1880 while (isdigit(*(end-1)) && end > start)
1881 end--;
1882 len = end - start;
1883
1884 if (kstrtoint(end, 10, &id) < 0)
1885 continue;
1886
1887 /* Allocate an alias_prop with enough space for the stem */
1888 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1889 if (!ap)
1890 continue;
1891 ap->alias = start;
1892 of_alias_add(ap, np, id, start, len);
1893 }
1894}
1895
1896/**
1897 * of_alias_get_id - Get alias id for the given device_node
1898 * @np: Pointer to the given device_node
1899 * @stem: Alias stem of the given device_node
1900 *
1901 * The function travels the lookup table to get alias id for the given
1902 * device_node and alias stem. It returns the alias id if find it.
1903 */
1904int of_alias_get_id(struct device_node *np, const char *stem)
1905{
1906 struct alias_prop *app;
1907 int id = -ENODEV;
1908
1909 mutex_lock(&of_aliases_mutex);
1910 list_for_each_entry(app, &aliases_lookup, link) {
1911 if (strcmp(app->stem, stem) != 0)
1912 continue;
1913
1914 if (np == app->np) {
1915 id = app->id;
1916 break;
1917 }
1918 }
1919 mutex_unlock(&of_aliases_mutex);
1920
1921 return id;
1922}
1923EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001924
1925const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1926 u32 *pu)
1927{
1928 const void *curv = cur;
1929
1930 if (!prop)
1931 return NULL;
1932
1933 if (!cur) {
1934 curv = prop->value;
1935 goto out_val;
1936 }
1937
1938 curv += sizeof(*cur);
1939 if (curv >= prop->value + prop->length)
1940 return NULL;
1941
1942out_val:
1943 *pu = be32_to_cpup(curv);
1944 return curv;
1945}
1946EXPORT_SYMBOL_GPL(of_prop_next_u32);
1947
1948const char *of_prop_next_string(struct property *prop, const char *cur)
1949{
1950 const void *curv = cur;
1951
1952 if (!prop)
1953 return NULL;
1954
1955 if (!cur)
1956 return prop->value;
1957
1958 curv += strlen(cur) + 1;
1959 if (curv >= prop->value + prop->length)
1960 return NULL;
1961
1962 return curv;
1963}
1964EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauerca257782013-08-05 14:40:44 +02001965
1966/**
1967 * of_device_is_stdout_path - check if a device node matches the
1968 * linux,stdout-path property
1969 *
1970 * Check if this device node matches the linux,stdout-path property
1971 * in the chosen node. return true if yes, false otherwise.
1972 */
1973int of_device_is_stdout_path(struct device_node *dn)
1974{
1975 if (!of_stdout)
1976 return false;
1977
1978 return of_stdout == dn;
1979}
1980EXPORT_SYMBOL_GPL(of_device_is_stdout_path);