blob: 33d7c80316b8f3b43997ecf36299539d9444343a [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 Likely50788572014-06-26 15:40:48 +010083#ifndef CONFIG_OF_DYNAMIC
Grant Likelyf57a8e22014-02-20 18:02:11 +000084static void of_node_release(struct kobject *kobj)
85{
86 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
87}
Grant Likely0f22dd32012-02-15 20:38:40 -070088#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -070089
Grant Likelyf57a8e22014-02-20 18:02:11 +000090struct kobj_type of_node_ktype = {
91 .release = of_node_release,
92};
93
94static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
95 struct bin_attribute *bin_attr, char *buf,
96 loff_t offset, size_t count)
97{
98 struct property *pp = container_of(bin_attr, struct property, attr);
99 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
100}
101
102static const char *safe_name(struct kobject *kobj, const char *orig_name)
103{
104 const char *name = orig_name;
105 struct sysfs_dirent *kn;
106 int i = 0;
107
108 /* don't be a hero. After 16 tries give up */
109 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, NULL, name))) {
110 sysfs_put(kn);
111 if (name != orig_name)
112 kfree(name);
113 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
114 }
115
116 if (name != orig_name)
117 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
118 kobject_name(kobj), name);
119 return name;
120}
121
122static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
123{
124 int rc;
125
126 /* Important: Don't leak passwords */
127 bool secure = strncmp(pp->name, "security-", 9) == 0;
128
129 sysfs_bin_attr_init(&pp->attr);
130 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
131 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
132 pp->attr.size = secure ? 0 : pp->length;
133 pp->attr.read = of_node_property_read;
134
135 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
136 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
137 return rc;
138}
139
140static int __of_node_add(struct device_node *np)
141{
142 const char *name;
143 struct property *pp;
144 int rc;
145
146 np->kobj.kset = of_kset;
147 if (!np->parent) {
148 /* Nodes without parents are new top level trees */
149 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
150 } else {
151 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
152 if (!name || !name[0])
153 return -EINVAL;
154
155 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
156 }
157 if (rc)
158 return rc;
159
160 for_each_property_of_node(np, pp)
161 __of_add_property_sysfs(np, pp);
162
163 return 0;
164}
165
166int of_node_add(struct device_node *np)
167{
168 int rc = 0;
Pantelis Antonioub74e9e12013-12-13 20:08:59 +0200169
170 BUG_ON(!of_node_is_initialized(np));
171
172 /*
173 * Grab the mutex here so that in a race condition between of_init() and
174 * of_node_add(), node addition will still be consistent.
175 */
Grant Likelyf57a8e22014-02-20 18:02:11 +0000176 mutex_lock(&of_aliases_mutex);
177 if (of_kset)
178 rc = __of_node_add(np);
Pantelis Antonioub74e9e12013-12-13 20:08:59 +0200179 else
180 /* This scenario may be perfectly valid, but report it anyway */
181 pr_info("of_node_add(%s) before of_init()\n", np->full_name);
Grant Likelyf57a8e22014-02-20 18:02:11 +0000182 mutex_unlock(&of_aliases_mutex);
183 return rc;
184}
185
Grant Likelyf57a8e22014-02-20 18:02:11 +0000186static int __init of_init(void)
187{
188 struct device_node *np;
189
190 /* Create the kset, and register existing nodes */
191 mutex_lock(&of_aliases_mutex);
192 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
193 if (!of_kset) {
194 mutex_unlock(&of_aliases_mutex);
195 return -ENOMEM;
196 }
197 for_each_of_allnodes(np)
198 __of_node_add(np);
199 mutex_unlock(&of_aliases_mutex);
200
Grant Likely43bba282012-11-06 21:03:27 +0000201 /* Symlink in /proc as required by userspace ABI */
Grant Likelyf57a8e22014-02-20 18:02:11 +0000202 if (of_allnodes)
203 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likelyf57a8e22014-02-20 18:02:11 +0000204
205 return 0;
206}
207core_initcall(of_init);
208
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500209static struct property *__of_find_property(const struct device_node *np,
210 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000211{
212 struct property *pp;
213
Timur Tabi64e45662008-05-08 05:19:59 +1000214 if (!np)
215 return NULL;
216
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530217 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000218 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530219 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000220 *lenp = pp->length;
221 break;
222 }
223 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500224
225 return pp;
226}
227
228struct property *of_find_property(const struct device_node *np,
229 const char *name,
230 int *lenp)
231{
232 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500233 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500234
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500235 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500236 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500237 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000238
239 return pp;
240}
241EXPORT_SYMBOL(of_find_property);
242
Grant Likelye91edcf2009-10-15 10:58:09 -0600243/**
244 * of_find_all_nodes - Get next node in global list
245 * @prev: Previous node or NULL to start iteration
246 * of_node_put() will be called on it
247 *
248 * Returns a node pointer with refcount incremented, use
249 * of_node_put() on it when done.
250 */
251struct device_node *of_find_all_nodes(struct device_node *prev)
252{
253 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000254 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600255
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000256 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000257 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600258 for (; np != NULL; np = np->allnext)
259 if (of_node_get(np))
260 break;
261 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000262 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600263 return np;
264}
265EXPORT_SYMBOL(of_find_all_nodes);
266
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000267/*
268 * Find a property with a given name for a given node
269 * and return the value.
270 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500271static const void *__of_get_property(const struct device_node *np,
272 const char *name, int *lenp)
273{
274 struct property *pp = __of_find_property(np, name, lenp);
275
276 return pp ? pp->value : NULL;
277}
278
279/*
280 * Find a property with a given name for a given node
281 * and return the value.
282 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000283const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500284 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000285{
286 struct property *pp = of_find_property(np, name, lenp);
287
288 return pp ? pp->value : NULL;
289}
290EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000291
292/** Checks if the given "compat" string matches one of the strings in
293 * the device's "compatible" property
294 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500295static int __of_device_is_compatible(const struct device_node *device,
296 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000297{
298 const char* cp;
299 int cplen, l;
300
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500301 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000302 if (cp == NULL)
303 return 0;
304 while (cplen > 0) {
305 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
306 return 1;
307 l = strlen(cp) + 1;
308 cp += l;
309 cplen -= l;
310 }
311
312 return 0;
313}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500314
315/** Checks if the given "compat" string matches one of the strings in
316 * the device's "compatible" property
317 */
318int of_device_is_compatible(const struct device_node *device,
319 const char *compat)
320{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500321 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500322 int res;
323
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500324 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500325 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500326 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500327 return res;
328}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000329EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000330
331/**
Grant Likely71a157e2010-02-01 21:34:14 -0700332 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700333 * @compat: compatible string to look for in root node's compatible property.
334 *
335 * Returns true if the root node has the given value in its
336 * compatible property.
337 */
Grant Likely71a157e2010-02-01 21:34:14 -0700338int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700339{
340 struct device_node *root;
341 int rc = 0;
342
343 root = of_find_node_by_path("/");
344 if (root) {
345 rc = of_device_is_compatible(root, compat);
346 of_node_put(root);
347 }
348 return rc;
349}
Grant Likely71a157e2010-02-01 21:34:14 -0700350EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700351
352/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700353 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100354 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700355 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100356 *
357 * Returns 1 if the status property is absent or set to "okay" or "ok",
358 * 0 otherwise
359 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700360static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100361{
362 const char *status;
363 int statlen;
364
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700365 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100366 if (status == NULL)
367 return 1;
368
369 if (statlen > 0) {
370 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
371 return 1;
372 }
373
374 return 0;
375}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700376
377/**
378 * of_device_is_available - check if a device is available for use
379 *
380 * @device: Node to check for availability
381 *
382 * Returns 1 if the status property is absent or set to "okay" or "ok",
383 * 0 otherwise
384 */
385int of_device_is_available(const struct device_node *device)
386{
387 unsigned long flags;
388 int res;
389
390 raw_spin_lock_irqsave(&devtree_lock, flags);
391 res = __of_device_is_available(device);
392 raw_spin_unlock_irqrestore(&devtree_lock, flags);
393 return res;
394
395}
Josh Boyer834d97d2008-03-27 00:33:14 +1100396EXPORT_SYMBOL(of_device_is_available);
397
398/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000399 * of_get_parent - Get a node's parent if any
400 * @node: Node to get parent
401 *
402 * Returns a node pointer with refcount incremented, use
403 * of_node_put() on it when done.
404 */
405struct device_node *of_get_parent(const struct device_node *node)
406{
407 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500408 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000409
410 if (!node)
411 return NULL;
412
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500413 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000414 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500415 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000416 return np;
417}
418EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000419
420/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000421 * of_get_next_parent - Iterate to a node's parent
422 * @node: Node to get parent of
423 *
424 * This is like of_get_parent() except that it drops the
425 * refcount on the passed node, making it suitable for iterating
426 * through a node's parents.
427 *
428 * Returns a node pointer with refcount incremented, use
429 * of_node_put() on it when done.
430 */
431struct device_node *of_get_next_parent(struct device_node *node)
432{
433 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500434 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000435
436 if (!node)
437 return NULL;
438
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500439 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000440 parent = of_node_get(node->parent);
441 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500442 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000443 return parent;
444}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300445EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000446
447/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000448 * of_get_next_child - Iterate a node childs
449 * @node: parent node
450 * @prev: previous child of the parent node, or NULL to get first
451 *
452 * Returns a node pointer with refcount incremented, use
453 * of_node_put() on it when done.
454 */
455struct device_node *of_get_next_child(const struct device_node *node,
456 struct device_node *prev)
457{
458 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500459 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000460
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500461 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000462 next = prev ? prev->sibling : node->child;
463 for (; next; next = next->sibling)
464 if (of_node_get(next))
465 break;
466 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500467 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000468 return next;
469}
470EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000471
472/**
Timur Tabi32961932012-08-14 13:20:23 +0000473 * of_get_next_available_child - Find the next available child node
474 * @node: parent node
475 * @prev: previous child of the parent node, or NULL to get first
476 *
477 * This function is like of_get_next_child(), except that it
478 * automatically skips any disabled nodes (i.e. status = "disabled").
479 */
480struct device_node *of_get_next_available_child(const struct device_node *node,
481 struct device_node *prev)
482{
483 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000484 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000485
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000486 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000487 next = prev ? prev->sibling : node->child;
488 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700489 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000490 continue;
491 if (of_node_get(next))
492 break;
493 }
494 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000495 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000496 return next;
497}
498EXPORT_SYMBOL(of_get_next_available_child);
499
500/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100501 * of_get_child_by_name - Find the child node by name for a given parent
502 * @node: parent node
503 * @name: child name to look for.
504 *
505 * This function looks for child node for given matching name
506 *
507 * Returns a node pointer if found, with refcount incremented, use
508 * of_node_put() on it when done.
509 * Returns NULL if node is not found.
510 */
511struct device_node *of_get_child_by_name(const struct device_node *node,
512 const char *name)
513{
514 struct device_node *child;
515
516 for_each_child_of_node(node, child)
517 if (child->name && (of_node_cmp(child->name, name) == 0))
518 break;
519 return child;
520}
521EXPORT_SYMBOL(of_get_child_by_name);
522
523/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000524 * of_find_node_by_path - Find a node matching a full OF path
525 * @path: The full path to match
526 *
527 * Returns a node pointer with refcount incremented, use
528 * of_node_put() on it when done.
529 */
530struct device_node *of_find_node_by_path(const char *path)
531{
Randy Dunlap465aac62012-11-30 10:01:51 +0000532 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500533 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000534
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500535 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000536 for (; np; np = np->allnext) {
537 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
538 && of_node_get(np))
539 break;
540 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500541 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000542 return np;
543}
544EXPORT_SYMBOL(of_find_node_by_path);
545
546/**
547 * of_find_node_by_name - Find a node by its "name" property
548 * @from: The node to start searching from or NULL, the node
549 * you pass will not be searched, only the next one
550 * will; typically, you pass what the previous call
551 * returned. of_node_put() will be called on it
552 * @name: The name string to match against
553 *
554 * Returns a node pointer with refcount incremented, use
555 * of_node_put() on it when done.
556 */
557struct device_node *of_find_node_by_name(struct device_node *from,
558 const char *name)
559{
560 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500561 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000562
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500563 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000564 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000565 for (; np; np = np->allnext)
566 if (np->name && (of_node_cmp(np->name, name) == 0)
567 && of_node_get(np))
568 break;
569 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500570 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000571 return np;
572}
573EXPORT_SYMBOL(of_find_node_by_name);
574
575/**
576 * of_find_node_by_type - Find a node by its "device_type" property
577 * @from: The node to start searching from, or NULL to start searching
578 * the entire device tree. The node you pass will not be
579 * searched, only the next one will; typically, you pass
580 * what the previous call returned. of_node_put() will be
581 * called on from for you.
582 * @type: The type string to match against
583 *
584 * Returns a node pointer with refcount incremented, use
585 * of_node_put() on it when done.
586 */
587struct device_node *of_find_node_by_type(struct device_node *from,
588 const char *type)
589{
590 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500591 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000592
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500593 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000594 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000595 for (; np; np = np->allnext)
596 if (np->type && (of_node_cmp(np->type, type) == 0)
597 && of_node_get(np))
598 break;
599 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500600 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000601 return np;
602}
603EXPORT_SYMBOL(of_find_node_by_type);
604
605/**
606 * of_find_compatible_node - Find a node based on type and one of the
607 * tokens in its "compatible" property
608 * @from: The node to start searching from or NULL, the node
609 * you pass will not be searched, only the next one
610 * will; typically, you pass what the previous call
611 * returned. of_node_put() will be called on it
612 * @type: The type string to match "device_type" or NULL to ignore
613 * @compatible: The string to match to one of the tokens in the device
614 * "compatible" list.
615 *
616 * Returns a node pointer with refcount incremented, use
617 * of_node_put() on it when done.
618 */
619struct device_node *of_find_compatible_node(struct device_node *from,
620 const char *type, const char *compatible)
621{
622 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500623 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000624
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500625 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000626 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000627 for (; np; np = np->allnext) {
628 if (type
629 && !(np->type && (of_node_cmp(np->type, type) == 0)))
630 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500631 if (__of_device_is_compatible(np, compatible) &&
632 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000633 break;
634 }
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_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100640
641/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000642 * of_find_node_with_property - Find a node which has a property with
643 * the given name.
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 * @prop_name: The name of the property to look for.
649 *
650 * Returns a node pointer with refcount incremented, use
651 * of_node_put() on it when done.
652 */
653struct device_node *of_find_node_with_property(struct device_node *from,
654 const char *prop_name)
655{
656 struct device_node *np;
657 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500658 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000659
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500660 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000661 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000662 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530663 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000664 if (of_prop_cmp(pp->name, prop_name) == 0) {
665 of_node_get(np);
666 goto out;
667 }
668 }
669 }
670out:
671 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500672 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000673 return np;
674}
675EXPORT_SYMBOL(of_find_node_with_property);
676
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500677static
678const struct of_device_id *__of_match_node(const struct of_device_id *matches,
679 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100680{
Grant Likelya52f07e2011-03-18 10:21:29 -0600681 if (!matches)
682 return NULL;
683
Grant Likely283029d2008-01-09 06:20:40 +1100684 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
685 int match = 1;
686 if (matches->name[0])
687 match &= node->name
688 && !strcmp(matches->name, node->name);
689 if (matches->type[0])
690 match &= node->type
691 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700692 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500693 match &= __of_device_is_compatible(node,
694 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700695 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100696 return matches;
697 matches++;
698 }
699 return NULL;
700}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500701
702/**
703 * of_match_node - Tell if an device_node has a matching of_match structure
704 * @matches: array of of device match structures to search in
705 * @node: the of device structure to match against
706 *
707 * Low level utility function used by device matching.
708 */
709const struct of_device_id *of_match_node(const struct of_device_id *matches,
710 const struct device_node *node)
711{
712 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500713 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500714
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500715 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500716 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500717 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500718 return match;
719}
Grant Likely283029d2008-01-09 06:20:40 +1100720EXPORT_SYMBOL(of_match_node);
721
722/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700723 * of_find_matching_node_and_match - Find a node based on an of_device_id
724 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100725 * @from: The node to start searching from or NULL, the node
726 * you pass will not be searched, only the next one
727 * will; typically, you pass what the previous call
728 * returned. of_node_put() will be called on it
729 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700730 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100731 *
732 * Returns a node pointer with refcount incremented, use
733 * of_node_put() on it when done.
734 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700735struct device_node *of_find_matching_node_and_match(struct device_node *from,
736 const struct of_device_id *matches,
737 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100738{
739 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800740 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500741 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100742
Stephen Warren50c8af42012-11-20 16:12:20 -0700743 if (match)
744 *match = NULL;
745
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500746 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000747 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100748 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500749 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800750 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700751 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800752 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100753 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700754 }
Grant Likely283029d2008-01-09 06:20:40 +1100755 }
756 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500757 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100758 return np;
759}
Grant Likely80c20222012-12-19 10:45:36 +0000760EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400761
762/**
Grant Likely3f07af42008-07-25 22:25:13 -0400763 * of_modalias_node - Lookup appropriate modalias for a device node
764 * @node: pointer to a device tree node
765 * @modalias: Pointer to buffer that modalias value will be copied into
766 * @len: Length of modalias value
767 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600768 * Based on the value of the compatible property, this routine will attempt
769 * to choose an appropriate modalias value for a particular device tree node.
770 * It does this by stripping the manufacturer prefix (as delimited by a ',')
771 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400772 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600773 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400774 */
775int of_modalias_node(struct device_node *node, char *modalias, int len)
776{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600777 const char *compatible, *p;
778 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400779
780 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600781 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400782 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400783 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600784 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400785 return 0;
786}
787EXPORT_SYMBOL_GPL(of_modalias_node);
788
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000789/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700790 * of_find_node_by_phandle - Find a node given a phandle
791 * @handle: phandle of the node to find
792 *
793 * Returns a node pointer with refcount incremented, use
794 * of_node_put() on it when done.
795 */
796struct device_node *of_find_node_by_phandle(phandle handle)
797{
798 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000799 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -0700800
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000801 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000802 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700803 if (np->phandle == handle)
804 break;
805 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000806 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700807 return np;
808}
809EXPORT_SYMBOL(of_find_node_by_phandle);
810
811/**
Heiko Stuebner10408542014-02-12 01:00:34 +0100812 * of_property_count_elems_of_size - Count the number of elements in a property
813 *
814 * @np: device node from which the property value is to be read.
815 * @propname: name of the property to be searched.
816 * @elem_size: size of the individual element
817 *
818 * Search for a property in a device node and count the number of elements of
819 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
820 * property does not exist or its length does not match a multiple of elem_size
821 * and -ENODATA if the property does not have a value.
822 */
823int of_property_count_elems_of_size(const struct device_node *np,
824 const char *propname, int elem_size)
825{
826 struct property *prop = of_find_property(np, propname, NULL);
827
828 if (!prop)
829 return -EINVAL;
830 if (!prop->value)
831 return -ENODATA;
832
833 if (prop->length % elem_size != 0) {
834 pr_err("size of %s in node %s is not a multiple of %d\n",
835 propname, np->full_name, elem_size);
836 return -EINVAL;
837 }
838
839 return prop->length / elem_size;
840}
841EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
842
843/**
Tony Priskdaeec1f2013-04-03 17:57:11 +1300844 * of_find_property_value_of_size
845 *
846 * @np: device node from which the property value is to be read.
847 * @propname: name of the property to be searched.
848 * @len: requested length of property value
849 *
850 * Search for a property in a device node and valid the requested size.
851 * Returns the property value on success, -EINVAL if the property does not
852 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
853 * property data isn't large enough.
854 *
855 */
856static void *of_find_property_value_of_size(const struct device_node *np,
857 const char *propname, u32 len)
858{
859 struct property *prop = of_find_property(np, propname, NULL);
860
861 if (!prop)
862 return ERR_PTR(-EINVAL);
863 if (!prop->value)
864 return ERR_PTR(-ENODATA);
865 if (len > prop->length)
866 return ERR_PTR(-EOVERFLOW);
867
868 return prop->value;
869}
870
871/**
Tony Prisk3daf3722013-03-23 17:02:15 +1300872 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
873 *
874 * @np: device node from which the property value is to be read.
875 * @propname: name of the property to be searched.
876 * @index: index of the u32 in the list of values
877 * @out_value: pointer to return value, modified only if no error.
878 *
879 * Search for a property in a device node and read nth 32-bit value from
880 * it. Returns 0 on success, -EINVAL if the property does not exist,
881 * -ENODATA if property does not have a value, and -EOVERFLOW if the
882 * property data isn't large enough.
883 *
884 * The out_value is modified only if a valid u32 value can be decoded.
885 */
886int of_property_read_u32_index(const struct device_node *np,
887 const char *propname,
888 u32 index, u32 *out_value)
889{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300890 const u32 *val = of_find_property_value_of_size(np, propname,
891 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +1300892
Tony Priskdaeec1f2013-04-03 17:57:11 +1300893 if (IS_ERR(val))
894 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +1300895
Tony Priskdaeec1f2013-04-03 17:57:11 +1300896 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +1300897 return 0;
898}
899EXPORT_SYMBOL_GPL(of_property_read_u32_index);
900
901/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530902 * of_property_read_u8_array - Find and read an array of u8 from a property.
903 *
904 * @np: device node from which the property value is to be read.
905 * @propname: name of the property to be searched.
906 * @out_value: pointer to return value, modified only if return value is 0.
907 * @sz: number of array elements to read
908 *
909 * Search for a property in a device node and read 8-bit value(s) from
910 * it. Returns 0 on success, -EINVAL if the property does not exist,
911 * -ENODATA if property does not have a value, and -EOVERFLOW if the
912 * property data isn't large enough.
913 *
914 * dts entry of array should be like:
915 * property = /bits/ 8 <0x50 0x60 0x70>;
916 *
917 * The out_value is modified only if a valid u8 value can be decoded.
918 */
919int of_property_read_u8_array(const struct device_node *np,
920 const char *propname, u8 *out_values, size_t sz)
921{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300922 const u8 *val = of_find_property_value_of_size(np, propname,
923 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530924
Tony Priskdaeec1f2013-04-03 17:57:11 +1300925 if (IS_ERR(val))
926 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530927
Viresh Kumarbe193242012-11-20 10:15:19 +0530928 while (sz--)
929 *out_values++ = *val++;
930 return 0;
931}
932EXPORT_SYMBOL_GPL(of_property_read_u8_array);
933
934/**
935 * of_property_read_u16_array - Find and read an array of u16 from a property.
936 *
937 * @np: device node from which the property value is to be read.
938 * @propname: name of the property to be searched.
939 * @out_value: pointer to return value, modified only if return value is 0.
940 * @sz: number of array elements to read
941 *
942 * Search for a property in a device node and read 16-bit value(s) from
943 * it. Returns 0 on success, -EINVAL if the property does not exist,
944 * -ENODATA if property does not have a value, and -EOVERFLOW if the
945 * property data isn't large enough.
946 *
947 * dts entry of array should be like:
948 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
949 *
950 * The out_value is modified only if a valid u16 value can be decoded.
951 */
952int of_property_read_u16_array(const struct device_node *np,
953 const char *propname, u16 *out_values, size_t sz)
954{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300955 const __be16 *val = of_find_property_value_of_size(np, propname,
956 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530957
Tony Priskdaeec1f2013-04-03 17:57:11 +1300958 if (IS_ERR(val))
959 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530960
Viresh Kumarbe193242012-11-20 10:15:19 +0530961 while (sz--)
962 *out_values++ = be16_to_cpup(val++);
963 return 0;
964}
965EXPORT_SYMBOL_GPL(of_property_read_u16_array);
966
967/**
Rob Herring0e373632011-07-06 15:42:58 -0500968 * of_property_read_u32_array - Find and read an array of 32 bit integers
969 * from a property.
970 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530971 * @np: device node from which the property value is to be read.
972 * @propname: name of the property to be searched.
973 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530974 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530975 *
Rob Herring0e373632011-07-06 15:42:58 -0500976 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530977 * it. Returns 0 on success, -EINVAL if the property does not exist,
978 * -ENODATA if property does not have a value, and -EOVERFLOW if the
979 * property data isn't large enough.
980 *
981 * The out_value is modified only if a valid u32 value can be decoded.
982 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100983int of_property_read_u32_array(const struct device_node *np,
984 const char *propname, u32 *out_values,
985 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530986{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300987 const __be32 *val = of_find_property_value_of_size(np, propname,
988 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +0530989
Tony Priskdaeec1f2013-04-03 17:57:11 +1300990 if (IS_ERR(val))
991 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -0500992
Rob Herring0e373632011-07-06 15:42:58 -0500993 while (sz--)
994 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530995 return 0;
996}
Rob Herring0e373632011-07-06 15:42:58 -0500997EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530998
999/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001000 * of_property_read_u64 - Find and read a 64 bit integer from a property
1001 * @np: device node from which the property value is to be read.
1002 * @propname: name of the property to be searched.
1003 * @out_value: pointer to return value, modified only if return value is 0.
1004 *
1005 * Search for a property in a device node and read a 64-bit value from
1006 * it. Returns 0 on success, -EINVAL if the property does not exist,
1007 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1008 * property data isn't large enough.
1009 *
1010 * The out_value is modified only if a valid u64 value can be decoded.
1011 */
1012int of_property_read_u64(const struct device_node *np, const char *propname,
1013 u64 *out_value)
1014{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001015 const __be32 *val = of_find_property_value_of_size(np, propname,
1016 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001017
Tony Priskdaeec1f2013-04-03 17:57:11 +13001018 if (IS_ERR(val))
1019 return PTR_ERR(val);
1020
1021 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001022 return 0;
1023}
1024EXPORT_SYMBOL_GPL(of_property_read_u64);
1025
1026/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301027 * of_property_read_string - Find and read a string from a property
1028 * @np: device node from which the property value is to be read.
1029 * @propname: name of the property to be searched.
1030 * @out_string: pointer to null terminated return string, modified only if
1031 * return value is 0.
1032 *
1033 * Search for a property in a device tree node and retrieve a null
1034 * terminated string value (pointer to data, not a copy). Returns 0 on
1035 * success, -EINVAL if the property does not exist, -ENODATA if property
1036 * does not have a value, and -EILSEQ if the string is not null-terminated
1037 * within the length of the property data.
1038 *
1039 * The out_string pointer is modified only if a valid string can be decoded.
1040 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001041int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001042 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301043{
1044 struct property *prop = of_find_property(np, propname, NULL);
1045 if (!prop)
1046 return -EINVAL;
1047 if (!prop->value)
1048 return -ENODATA;
1049 if (strnlen(prop->value, prop->length) >= prop->length)
1050 return -EILSEQ;
1051 *out_string = prop->value;
1052 return 0;
1053}
1054EXPORT_SYMBOL_GPL(of_property_read_string);
1055
1056/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001057 * of_property_read_string_index - Find and read a string from a multiple
1058 * strings property.
1059 * @np: device node from which the property value is to be read.
1060 * @propname: name of the property to be searched.
1061 * @index: index of the string in the list of strings
1062 * @out_string: pointer to null terminated return string, modified only if
1063 * return value is 0.
1064 *
1065 * Search for a property in a device tree node and retrieve a null
1066 * terminated string value (pointer to data, not a copy) in the list of strings
1067 * contained in that property.
1068 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1069 * property does not have a value, and -EILSEQ if the string is not
1070 * null-terminated within the length of the property data.
1071 *
1072 * The out_string pointer is modified only if a valid string can be decoded.
1073 */
1074int of_property_read_string_index(struct device_node *np, const char *propname,
1075 int index, const char **output)
1076{
1077 struct property *prop = of_find_property(np, propname, NULL);
1078 int i = 0;
1079 size_t l = 0, total = 0;
1080 const char *p;
1081
1082 if (!prop)
1083 return -EINVAL;
1084 if (!prop->value)
1085 return -ENODATA;
1086 if (strnlen(prop->value, prop->length) >= prop->length)
1087 return -EILSEQ;
1088
1089 p = prop->value;
1090
1091 for (i = 0; total < prop->length; total += l, p += l) {
1092 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001093 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001094 *output = p;
1095 return 0;
1096 }
1097 }
1098 return -ENODATA;
1099}
1100EXPORT_SYMBOL_GPL(of_property_read_string_index);
1101
Grant Likely7aff0fe2011-12-12 09:25:58 -07001102/**
1103 * of_property_match_string() - Find string in a list and return index
1104 * @np: pointer to node containing string list property
1105 * @propname: string list property name
1106 * @string: pointer to string to search for in string list
1107 *
1108 * This function searches a string list property and returns the index
1109 * of a specific string value.
1110 */
1111int of_property_match_string(struct device_node *np, const char *propname,
1112 const char *string)
1113{
1114 struct property *prop = of_find_property(np, propname, NULL);
1115 size_t l;
1116 int i;
1117 const char *p, *end;
1118
1119 if (!prop)
1120 return -EINVAL;
1121 if (!prop->value)
1122 return -ENODATA;
1123
1124 p = prop->value;
1125 end = p + prop->length;
1126
1127 for (i = 0; p < end; i++, p += l) {
1128 l = strlen(p) + 1;
1129 if (p + l > end)
1130 return -EILSEQ;
1131 pr_debug("comparing %s with %s\n", string, p);
1132 if (strcmp(string, p) == 0)
1133 return i; /* Found it; return index */
1134 }
1135 return -ENODATA;
1136}
1137EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001138
1139/**
1140 * of_property_count_strings - Find and return the number of strings from a
1141 * multiple strings property.
1142 * @np: device node from which the property value is to be read.
1143 * @propname: name of the property to be searched.
1144 *
1145 * Search for a property in a device tree node and retrieve the number of null
1146 * terminated string contain in it. Returns the number of strings on
1147 * success, -EINVAL if the property does not exist, -ENODATA if property
1148 * does not have a value, and -EILSEQ if the string is not null-terminated
1149 * within the length of the property data.
1150 */
1151int of_property_count_strings(struct device_node *np, const char *propname)
1152{
1153 struct property *prop = of_find_property(np, propname, NULL);
1154 int i = 0;
1155 size_t l = 0, total = 0;
1156 const char *p;
1157
1158 if (!prop)
1159 return -EINVAL;
1160 if (!prop->value)
1161 return -ENODATA;
1162 if (strnlen(prop->value, prop->length) >= prop->length)
1163 return -EILSEQ;
1164
1165 p = prop->value;
1166
Benoit Cousson88af7f52011-12-05 15:23:54 +01001167 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001168 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001169
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001170 return i;
1171}
1172EXPORT_SYMBOL_GPL(of_property_count_strings);
1173
Grant Likelybd69f732013-02-10 22:57:21 +00001174static int __of_parse_phandle_with_args(const struct device_node *np,
1175 const char *list_name,
Stephen Warrenf35c0932013-08-14 15:27:10 -06001176 const char *cells_name,
1177 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001178 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001179{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001180 const __be32 *list, *list_end;
Grant Likely23ce04c02013-02-12 21:21:49 +00001181 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001182 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001183 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001184 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001185
Grant Likely15c9a0a2011-12-12 09:25:57 -07001186 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001187 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001188 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001189 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001190 list_end = list + size / sizeof(*list);
1191
Grant Likely15c9a0a2011-12-12 09:25:57 -07001192 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001193 while (list < list_end) {
Grant Likely23ce04c02013-02-12 21:21:49 +00001194 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001195 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001196
Grant Likely15c9a0a2011-12-12 09:25:57 -07001197 /*
1198 * If phandle is 0, then it is an empty entry with no
1199 * arguments. Skip forward to the next entry.
1200 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001201 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001202 if (phandle) {
1203 /*
1204 * Find the provider node and parse the #*-cells
Stephen Warren3a9560b2013-08-14 15:27:11 -06001205 * property to determine the argument length.
1206 *
1207 * This is not needed if the cell count is hard-coded
1208 * (i.e. cells_name not set, but cell_count is set),
1209 * except when we're going to return the found node
1210 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001211 */
Stephen Warren3a9560b2013-08-14 15:27:11 -06001212 if (cells_name || cur_index == index) {
1213 node = of_find_node_by_phandle(phandle);
1214 if (!node) {
1215 pr_err("%s: could not find phandle\n",
1216 np->full_name);
1217 goto err;
1218 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001219 }
Stephen Warrenf35c0932013-08-14 15:27:10 -06001220
1221 if (cells_name) {
1222 if (of_property_read_u32(node, cells_name,
1223 &count)) {
1224 pr_err("%s: could not get %s for %s\n",
1225 np->full_name, cells_name,
1226 node->full_name);
1227 goto err;
1228 }
1229 } else {
1230 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001231 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001232
Grant Likely15c9a0a2011-12-12 09:25:57 -07001233 /*
1234 * Make sure that the arguments actually fit in the
1235 * remaining property data length
1236 */
1237 if (list + count > list_end) {
1238 pr_err("%s: arguments longer than property\n",
1239 np->full_name);
Grant Likely23ce04c02013-02-12 21:21:49 +00001240 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001241 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001242 }
1243
Grant Likely15c9a0a2011-12-12 09:25:57 -07001244 /*
1245 * All of the error cases above bail out of the loop, so at
1246 * this point, the parsing is successful. If the requested
1247 * index matches, then fill the out_args structure and return,
1248 * or return -ENOENT for an empty entry.
1249 */
Grant Likely23ce04c02013-02-12 21:21:49 +00001250 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001251 if (cur_index == index) {
1252 if (!phandle)
Grant Likely23ce04c02013-02-12 21:21:49 +00001253 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001254
Grant Likely15c9a0a2011-12-12 09:25:57 -07001255 if (out_args) {
1256 int i;
1257 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1258 count = MAX_PHANDLE_ARGS;
1259 out_args->np = node;
1260 out_args->args_count = count;
1261 for (i = 0; i < count; i++)
1262 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001263 } else {
1264 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001265 }
Grant Likely23ce04c02013-02-12 21:21:49 +00001266
1267 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001268 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001269 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001270
1271 of_node_put(node);
1272 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001273 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001274 cur_index++;
1275 }
1276
Grant Likely23ce04c02013-02-12 21:21:49 +00001277 /*
1278 * Unlock node before returning result; will be one of:
1279 * -ENOENT : index is for empty phandle
1280 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001281 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c02013-02-12 21:21:49 +00001282 */
Grant Likelybd69f732013-02-10 22:57:21 +00001283 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c02013-02-12 21:21:49 +00001284 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001285 if (node)
1286 of_node_put(node);
Grant Likely23ce04c02013-02-12 21:21:49 +00001287 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001288}
Grant Likelybd69f732013-02-10 22:57:21 +00001289
Stephen Warrend2621342013-08-14 15:27:08 -06001290/**
Stephen Warren73ed9312013-08-14 15:27:09 -06001291 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1292 * @np: Pointer to device node holding phandle property
1293 * @phandle_name: Name of property holding a phandle value
1294 * @index: For properties holding a table of phandles, this is the index into
1295 * the table
1296 *
1297 * Returns the device_node pointer with refcount incremented. Use
1298 * of_node_put() on it when done.
1299 */
1300struct device_node *of_parse_phandle(const struct device_node *np,
1301 const char *phandle_name, int index)
1302{
Stephen Warren3a9560b2013-08-14 15:27:11 -06001303 struct of_phandle_args args;
Stephen Warren73ed9312013-08-14 15:27:09 -06001304
Stephen Warren3a9560b2013-08-14 15:27:11 -06001305 if (index < 0)
Stephen Warren73ed9312013-08-14 15:27:09 -06001306 return NULL;
1307
Stephen Warren3a9560b2013-08-14 15:27:11 -06001308 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1309 index, &args))
1310 return NULL;
1311
1312 return args.np;
Stephen Warren73ed9312013-08-14 15:27:09 -06001313}
1314EXPORT_SYMBOL(of_parse_phandle);
1315
1316/**
Stephen Warrend2621342013-08-14 15:27:08 -06001317 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1318 * @np: pointer to a device tree node containing a list
1319 * @list_name: property name that contains a list
1320 * @cells_name: property name that specifies phandles' arguments count
1321 * @index: index of a phandle to parse out
1322 * @out_args: optional pointer to output arguments structure (will be filled)
1323 *
1324 * This function is useful to parse lists of phandles and their arguments.
1325 * Returns 0 on success and fills out_args, on error returns appropriate
1326 * errno value.
1327 *
1328 * Caller is responsible to call of_node_put() on the returned out_args->node
1329 * pointer.
1330 *
1331 * Example:
1332 *
1333 * phandle1: node1 {
1334 * #list-cells = <2>;
1335 * }
1336 *
1337 * phandle2: node2 {
1338 * #list-cells = <1>;
1339 * }
1340 *
1341 * node3 {
1342 * list = <&phandle1 1 2 &phandle2 3>;
1343 * }
1344 *
1345 * To get a device_node of the `node2' node you may call this:
1346 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1347 */
Grant Likelybd69f732013-02-10 22:57:21 +00001348int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1349 const char *cells_name, int index,
1350 struct of_phandle_args *out_args)
1351{
1352 if (index < 0)
1353 return -EINVAL;
Stephen Warrenf35c0932013-08-14 15:27:10 -06001354 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1355 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001356}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001357EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001358
Grant Likelybd69f732013-02-10 22:57:21 +00001359/**
Stephen Warrenf35c0932013-08-14 15:27:10 -06001360 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1361 * @np: pointer to a device tree node containing a list
1362 * @list_name: property name that contains a list
1363 * @cell_count: number of argument cells following the phandle
1364 * @index: index of a phandle to parse out
1365 * @out_args: optional pointer to output arguments structure (will be filled)
1366 *
1367 * This function is useful to parse lists of phandles and their arguments.
1368 * Returns 0 on success and fills out_args, on error returns appropriate
1369 * errno value.
1370 *
1371 * Caller is responsible to call of_node_put() on the returned out_args->node
1372 * pointer.
1373 *
1374 * Example:
1375 *
1376 * phandle1: node1 {
1377 * }
1378 *
1379 * phandle2: node2 {
1380 * }
1381 *
1382 * node3 {
1383 * list = <&phandle1 0 2 &phandle2 2 3>;
1384 * }
1385 *
1386 * To get a device_node of the `node2' node you may call this:
1387 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1388 */
1389int of_parse_phandle_with_fixed_args(const struct device_node *np,
1390 const char *list_name, int cell_count,
1391 int index, struct of_phandle_args *out_args)
1392{
1393 if (index < 0)
1394 return -EINVAL;
1395 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1396 index, out_args);
1397}
1398EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1399
1400/**
Grant Likelybd69f732013-02-10 22:57:21 +00001401 * of_count_phandle_with_args() - Find the number of phandles references in a property
1402 * @np: pointer to a device tree node containing a list
1403 * @list_name: property name that contains a list
1404 * @cells_name: property name that specifies phandles' arguments count
1405 *
1406 * Returns the number of phandle + argument tuples within a property. It
1407 * is a typical pattern to encode a list of phandle and variable
1408 * arguments into a single property. The number of arguments is encoded
1409 * by a property in the phandle-target node. For example, a gpios
1410 * property would contain a list of GPIO specifies consisting of a
1411 * phandle and 1 or more arguments. The number of arguments are
1412 * determined by the #gpio-cells property in the node pointed to by the
1413 * phandle.
1414 */
1415int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1416 const char *cells_name)
1417{
Stephen Warrenf35c0932013-08-14 15:27:10 -06001418 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1419 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001420}
1421EXPORT_SYMBOL(of_count_phandle_with_args);
1422
Grant Likely02af11b2009-11-23 20:16:45 -07001423/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001424 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001425 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001426int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001427{
1428 struct property **next;
1429 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001430 int rc;
1431
1432 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1433 if (rc)
1434 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001435
1436 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001437 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001438 next = &np->properties;
1439 while (*next) {
1440 if (strcmp(prop->name, (*next)->name) == 0) {
1441 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001442 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001443 return -1;
1444 }
1445 next = &(*next)->next;
1446 }
1447 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001448 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelyf57a8e22014-02-20 18:02:11 +00001449 if (rc)
1450 return rc;
1451
Pantelis Antonioub74e9e12013-12-13 20:08:59 +02001452 if (of_node_is_attached(np))
1453 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001454
Grant Likely02af11b2009-11-23 20:16:45 -07001455 return 0;
1456}
1457
1458/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001459 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001460 *
1461 * Note that we don't actually remove it, since we have given out
1462 * who-knows-how-many pointers to the data using get-property.
1463 * Instead we just move the property to the "dead properties"
1464 * list, so it won't be found any more.
1465 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001466int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001467{
1468 struct property **next;
1469 unsigned long flags;
1470 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001471 int rc;
1472
1473 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1474 if (rc)
1475 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001476
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001477 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001478 next = &np->properties;
1479 while (*next) {
1480 if (*next == prop) {
1481 /* found the node */
1482 *next = prop->next;
1483 prop->next = np->deadprops;
1484 np->deadprops = prop;
1485 found = 1;
1486 break;
1487 }
1488 next = &(*next)->next;
1489 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001490 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001491
1492 if (!found)
1493 return -ENODEV;
1494
Grant Likelyf57a8e22014-02-20 18:02:11 +00001495 /* at early boot, bail hear and defer setup to of_init() */
1496 if (!of_kset)
1497 return 0;
1498
1499 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1500
Grant Likely02af11b2009-11-23 20:16:45 -07001501 return 0;
1502}
1503
1504/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001505 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001506 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001507 *
1508 * Note that we don't actually remove it, since we have given out
1509 * who-knows-how-many pointers to the data using get-property.
1510 * Instead we just move the property to the "dead properties" list,
1511 * and add the new property to the property list
1512 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001513int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001514{
Dong Aisheng475d0092012-07-11 15:16:37 +10001515 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001516 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001517 int rc, found = 0;
1518
1519 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1520 if (rc)
1521 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001522
Dong Aisheng475d0092012-07-11 15:16:37 +10001523 if (!newprop->name)
1524 return -EINVAL;
1525
1526 oldprop = of_find_property(np, newprop->name, NULL);
1527 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001528 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001529
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001530 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001531 next = &np->properties;
1532 while (*next) {
1533 if (*next == oldprop) {
1534 /* found the node */
1535 newprop->next = oldprop->next;
1536 *next = newprop;
1537 oldprop->next = np->deadprops;
1538 np->deadprops = oldprop;
1539 found = 1;
1540 break;
1541 }
1542 next = &(*next)->next;
1543 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001544 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelyf57a8e22014-02-20 18:02:11 +00001545 if (rc)
1546 return rc;
1547
1548 /* Update the sysfs attribute */
1549 if (oldprop)
1550 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1551 __of_add_property_sysfs(np, newprop);
Grant Likely02af11b2009-11-23 20:16:45 -07001552
1553 if (!found)
1554 return -ENODEV;
1555
Grant Likely02af11b2009-11-23 20:16:45 -07001556 return 0;
1557}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001558
Shawn Guo611cad72011-08-15 15:28:14 +08001559static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1560 int id, const char *stem, int stem_len)
1561{
1562 ap->np = np;
1563 ap->id = id;
1564 strncpy(ap->stem, stem, stem_len);
1565 ap->stem[stem_len] = 0;
1566 list_add_tail(&ap->link, &aliases_lookup);
1567 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001568 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001569}
1570
1571/**
1572 * of_alias_scan - Scan all properties of 'aliases' node
1573 *
1574 * The function scans all the properties of 'aliases' node and populate
1575 * the the global lookup table with the properties. It returns the
1576 * number of alias_prop found, or error code in error case.
1577 *
1578 * @dt_alloc: An allocator that provides a virtual address to memory
1579 * for the resulting tree
1580 */
1581void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1582{
1583 struct property *pp;
1584
1585 of_chosen = of_find_node_by_path("/chosen");
1586 if (of_chosen == NULL)
1587 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauerca257782013-08-05 14:40:44 +02001588
1589 if (of_chosen) {
1590 const char *name;
1591
1592 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1593 if (name)
1594 of_stdout = of_find_node_by_path(name);
1595 }
1596
Shawn Guo611cad72011-08-15 15:28:14 +08001597 of_aliases = of_find_node_by_path("/aliases");
1598 if (!of_aliases)
1599 return;
1600
Dong Aisheng8af0da92011-12-22 20:19:24 +08001601 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001602 const char *start = pp->name;
1603 const char *end = start + strlen(start);
1604 struct device_node *np;
1605 struct alias_prop *ap;
1606 int id, len;
1607
1608 /* Skip those we do not want to proceed */
1609 if (!strcmp(pp->name, "name") ||
1610 !strcmp(pp->name, "phandle") ||
1611 !strcmp(pp->name, "linux,phandle"))
1612 continue;
1613
1614 np = of_find_node_by_path(pp->value);
1615 if (!np)
1616 continue;
1617
1618 /* walk the alias backwards to extract the id and work out
1619 * the 'stem' string */
1620 while (isdigit(*(end-1)) && end > start)
1621 end--;
1622 len = end - start;
1623
1624 if (kstrtoint(end, 10, &id) < 0)
1625 continue;
1626
1627 /* Allocate an alias_prop with enough space for the stem */
1628 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1629 if (!ap)
1630 continue;
1631 ap->alias = start;
1632 of_alias_add(ap, np, id, start, len);
1633 }
1634}
1635
1636/**
1637 * of_alias_get_id - Get alias id for the given device_node
1638 * @np: Pointer to the given device_node
1639 * @stem: Alias stem of the given device_node
1640 *
1641 * The function travels the lookup table to get alias id for the given
1642 * device_node and alias stem. It returns the alias id if find it.
1643 */
1644int of_alias_get_id(struct device_node *np, const char *stem)
1645{
1646 struct alias_prop *app;
1647 int id = -ENODEV;
1648
1649 mutex_lock(&of_aliases_mutex);
1650 list_for_each_entry(app, &aliases_lookup, link) {
1651 if (strcmp(app->stem, stem) != 0)
1652 continue;
1653
1654 if (np == app->np) {
1655 id = app->id;
1656 break;
1657 }
1658 }
1659 mutex_unlock(&of_aliases_mutex);
1660
1661 return id;
1662}
1663EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001664
1665const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1666 u32 *pu)
1667{
1668 const void *curv = cur;
1669
1670 if (!prop)
1671 return NULL;
1672
1673 if (!cur) {
1674 curv = prop->value;
1675 goto out_val;
1676 }
1677
1678 curv += sizeof(*cur);
1679 if (curv >= prop->value + prop->length)
1680 return NULL;
1681
1682out_val:
1683 *pu = be32_to_cpup(curv);
1684 return curv;
1685}
1686EXPORT_SYMBOL_GPL(of_prop_next_u32);
1687
1688const char *of_prop_next_string(struct property *prop, const char *cur)
1689{
1690 const void *curv = cur;
1691
1692 if (!prop)
1693 return NULL;
1694
1695 if (!cur)
1696 return prop->value;
1697
1698 curv += strlen(cur) + 1;
1699 if (curv >= prop->value + prop->length)
1700 return NULL;
1701
1702 return curv;
1703}
1704EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauerca257782013-08-05 14:40:44 +02001705
1706/**
1707 * of_device_is_stdout_path - check if a device node matches the
1708 * linux,stdout-path property
1709 *
1710 * Check if this device node matches the linux,stdout-path property
1711 * in the chosen node. return true if yes, false otherwise.
1712 */
1713int of_device_is_stdout_path(struct device_node *dn)
1714{
1715 if (!of_stdout)
1716 return false;
1717
1718 return of_stdout == dn;
1719}
1720EXPORT_SYMBOL_GPL(of_device_is_stdout_path);