blob: 4a2cf4f70f4796de9928db02d841bccc7e497349 [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 Likely952f9f42014-07-23 17:05:06 -060038struct kset *of_kset;
Grant Likelyf57a8e22014-02-20 18:02:11 +000039
40/*
Grant Likely952f9f42014-07-23 17:05:06 -060041 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
42 * This mutex must be held whenever modifications are being made to the
43 * device tree. The of_{attach,detach}_node() and
44 * of_{add,remove,update}_property() helpers make sure this happens.
Grant Likelyf57a8e22014-02-20 18:02:11 +000045 */
Pantelis Antoniou59cf2512014-07-04 19:58:03 +030046DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100047
Stephen Rothwell581b6052007-04-24 16:46:53 +100048/* use when traversing tree through the allnext, child, sibling,
49 * or parent members of struct device_node.
50 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050051DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100052
53int of_n_addr_cells(struct device_node *np)
54{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060055 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100056
57 do {
58 if (np->parent)
59 np = np->parent;
60 ip = of_get_property(np, "#address-cells", NULL);
61 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070062 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100063 } while (np->parent);
64 /* No #address-cells property for the root node */
65 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
66}
67EXPORT_SYMBOL(of_n_addr_cells);
68
69int of_n_size_cells(struct device_node *np)
70{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060071 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100072
73 do {
74 if (np->parent)
75 np = np->parent;
76 ip = of_get_property(np, "#size-cells", NULL);
77 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070078 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100079 } while (np->parent);
80 /* No #size-cells property for the root node */
81 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
82}
83EXPORT_SYMBOL(of_n_size_cells);
84
Grant Likely50788572014-06-26 15:40:48 +010085#ifndef CONFIG_OF_DYNAMIC
Grant Likelyf57a8e22014-02-20 18:02:11 +000086static void of_node_release(struct kobject *kobj)
87{
88 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
89}
Grant Likely0f22dd32012-02-15 20:38:40 -070090#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -070091
Grant Likelyf57a8e22014-02-20 18:02:11 +000092struct kobj_type of_node_ktype = {
93 .release = of_node_release,
94};
95
96static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
97 struct bin_attribute *bin_attr, char *buf,
98 loff_t offset, size_t count)
99{
100 struct property *pp = container_of(bin_attr, struct property, attr);
101 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
102}
103
104static const char *safe_name(struct kobject *kobj, const char *orig_name)
105{
106 const char *name = orig_name;
107 struct sysfs_dirent *kn;
108 int i = 0;
109
110 /* don't be a hero. After 16 tries give up */
111 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, NULL, name))) {
112 sysfs_put(kn);
113 if (name != orig_name)
114 kfree(name);
115 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
116 }
117
118 if (name != orig_name)
119 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
120 kobject_name(kobj), name);
121 return name;
122}
123
Grant Likely952f9f42014-07-23 17:05:06 -0600124int __of_add_property_sysfs(struct device_node *np, struct property *pp)
Grant Likelyf57a8e22014-02-20 18:02:11 +0000125{
126 int rc;
127
128 /* Important: Don't leak passwords */
129 bool secure = strncmp(pp->name, "security-", 9) == 0;
130
Grant Likely952f9f42014-07-23 17:05:06 -0600131 if (!of_kset || !of_node_is_attached(np))
132 return 0;
133
Grant Likelyf57a8e22014-02-20 18:02:11 +0000134 sysfs_bin_attr_init(&pp->attr);
135 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
136 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
137 pp->attr.size = secure ? 0 : pp->length;
138 pp->attr.read = of_node_property_read;
139
140 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
141 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
142 return rc;
143}
144
Grant Likely952f9f42014-07-23 17:05:06 -0600145int __of_attach_node_sysfs(struct device_node *np)
Grant Likelyf57a8e22014-02-20 18:02:11 +0000146{
147 const char *name;
148 struct property *pp;
149 int rc;
150
Grant Likely952f9f42014-07-23 17:05:06 -0600151 if (!of_kset)
152 return 0;
153
Grant Likelyf57a8e22014-02-20 18:02:11 +0000154 np->kobj.kset = of_kset;
155 if (!np->parent) {
156 /* Nodes without parents are new top level trees */
157 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
158 } else {
159 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
160 if (!name || !name[0])
161 return -EINVAL;
162
163 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
164 }
165 if (rc)
166 return rc;
167
168 for_each_property_of_node(np, pp)
169 __of_add_property_sysfs(np, pp);
170
171 return 0;
172}
173
Grant Likelyf57a8e22014-02-20 18:02:11 +0000174static int __init of_init(void)
175{
176 struct device_node *np;
177
178 /* Create the kset, and register existing nodes */
Pantelis Antoniou59cf2512014-07-04 19:58:03 +0300179 mutex_lock(&of_mutex);
Grant Likelyf57a8e22014-02-20 18:02:11 +0000180 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
181 if (!of_kset) {
Pantelis Antoniou59cf2512014-07-04 19:58:03 +0300182 mutex_unlock(&of_mutex);
Grant Likelyf57a8e22014-02-20 18:02:11 +0000183 return -ENOMEM;
184 }
185 for_each_of_allnodes(np)
Grant Likely952f9f42014-07-23 17:05:06 -0600186 __of_attach_node_sysfs(np);
Pantelis Antoniou59cf2512014-07-04 19:58:03 +0300187 mutex_unlock(&of_mutex);
Grant Likelyf57a8e22014-02-20 18:02:11 +0000188
Grant Likely43bba282012-11-06 21:03:27 +0000189 /* Symlink in /proc as required by userspace ABI */
Grant Likelyf57a8e22014-02-20 18:02:11 +0000190 if (of_allnodes)
191 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likelyf57a8e22014-02-20 18:02:11 +0000192
193 return 0;
194}
195core_initcall(of_init);
196
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500197static struct property *__of_find_property(const struct device_node *np,
198 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000199{
200 struct property *pp;
201
Timur Tabi64e45662008-05-08 05:19:59 +1000202 if (!np)
203 return NULL;
204
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530205 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000206 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530207 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000208 *lenp = pp->length;
209 break;
210 }
211 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500212
213 return pp;
214}
215
216struct property *of_find_property(const struct device_node *np,
217 const char *name,
218 int *lenp)
219{
220 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500221 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500222
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500223 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500224 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500225 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000226
227 return pp;
228}
229EXPORT_SYMBOL(of_find_property);
230
Grant Likelye91edcf2009-10-15 10:58:09 -0600231/**
232 * of_find_all_nodes - Get next node in global list
233 * @prev: Previous node or NULL to start iteration
234 * of_node_put() will be called on it
235 *
236 * Returns a node pointer with refcount incremented, use
237 * of_node_put() on it when done.
238 */
239struct device_node *of_find_all_nodes(struct device_node *prev)
240{
241 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000242 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600243
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000244 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000245 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600246 for (; np != NULL; np = np->allnext)
247 if (of_node_get(np))
248 break;
249 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000250 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600251 return np;
252}
253EXPORT_SYMBOL(of_find_all_nodes);
254
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000255/*
256 * Find a property with a given name for a given node
257 * and return the value.
258 */
Grant Likely3a09bee2014-07-15 23:25:43 -0600259const void *__of_get_property(const struct device_node *np,
260 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500261{
262 struct property *pp = __of_find_property(np, name, lenp);
263
264 return pp ? pp->value : NULL;
265}
266
267/*
268 * Find a property with a given name for a given node
269 * and return the value.
270 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000271const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500272 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000273{
274 struct property *pp = of_find_property(np, name, lenp);
275
276 return pp ? pp->value : NULL;
277}
278EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000279
280/** Checks if the given "compat" string matches one of the strings in
281 * the device's "compatible" property
282 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500283static int __of_device_is_compatible(const struct device_node *device,
284 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000285{
286 const char* cp;
287 int cplen, l;
288
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500289 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000290 if (cp == NULL)
291 return 0;
292 while (cplen > 0) {
293 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
294 return 1;
295 l = strlen(cp) + 1;
296 cp += l;
297 cplen -= l;
298 }
299
300 return 0;
301}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500302
303/** Checks if the given "compat" string matches one of the strings in
304 * the device's "compatible" property
305 */
306int of_device_is_compatible(const struct device_node *device,
307 const char *compat)
308{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500309 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500310 int res;
311
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500312 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500313 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500314 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500315 return res;
316}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000317EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000318
319/**
Grant Likely71a157e2010-02-01 21:34:14 -0700320 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700321 * @compat: compatible string to look for in root node's compatible property.
322 *
323 * Returns true if the root node has the given value in its
324 * compatible property.
325 */
Grant Likely71a157e2010-02-01 21:34:14 -0700326int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700327{
328 struct device_node *root;
329 int rc = 0;
330
331 root = of_find_node_by_path("/");
332 if (root) {
333 rc = of_device_is_compatible(root, compat);
334 of_node_put(root);
335 }
336 return rc;
337}
Grant Likely71a157e2010-02-01 21:34:14 -0700338EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700339
340/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700341 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100342 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700343 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100344 *
345 * Returns 1 if the status property is absent or set to "okay" or "ok",
346 * 0 otherwise
347 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700348static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100349{
350 const char *status;
351 int statlen;
352
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700353 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100354 if (status == NULL)
355 return 1;
356
357 if (statlen > 0) {
358 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
359 return 1;
360 }
361
362 return 0;
363}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700364
365/**
366 * of_device_is_available - check if a device is available for use
367 *
368 * @device: Node to check for availability
369 *
370 * Returns 1 if the status property is absent or set to "okay" or "ok",
371 * 0 otherwise
372 */
373int of_device_is_available(const struct device_node *device)
374{
375 unsigned long flags;
376 int res;
377
378 raw_spin_lock_irqsave(&devtree_lock, flags);
379 res = __of_device_is_available(device);
380 raw_spin_unlock_irqrestore(&devtree_lock, flags);
381 return res;
382
383}
Josh Boyer834d97d2008-03-27 00:33:14 +1100384EXPORT_SYMBOL(of_device_is_available);
385
386/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000387 * of_get_parent - Get a node's parent if any
388 * @node: Node to get parent
389 *
390 * Returns a node pointer with refcount incremented, use
391 * of_node_put() on it when done.
392 */
393struct device_node *of_get_parent(const struct device_node *node)
394{
395 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500396 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000397
398 if (!node)
399 return NULL;
400
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500401 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000402 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500403 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000404 return np;
405}
406EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000407
408/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000409 * of_get_next_parent - Iterate to a node's parent
410 * @node: Node to get parent of
411 *
412 * This is like of_get_parent() except that it drops the
413 * refcount on the passed node, making it suitable for iterating
414 * through a node's parents.
415 *
416 * Returns a node pointer with refcount incremented, use
417 * of_node_put() on it when done.
418 */
419struct device_node *of_get_next_parent(struct device_node *node)
420{
421 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500422 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000423
424 if (!node)
425 return NULL;
426
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500427 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000428 parent = of_node_get(node->parent);
429 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500430 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000431 return parent;
432}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300433EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000434
435/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000436 * of_get_next_child - Iterate a node childs
437 * @node: parent node
438 * @prev: previous child of the parent node, or NULL to get first
439 *
440 * Returns a node pointer with refcount incremented, use
441 * of_node_put() on it when done.
442 */
443struct device_node *of_get_next_child(const struct device_node *node,
444 struct device_node *prev)
445{
446 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500447 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000448
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500449 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000450 next = prev ? prev->sibling : node->child;
451 for (; next; next = next->sibling)
452 if (of_node_get(next))
453 break;
454 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500455 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000456 return next;
457}
458EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000459
460/**
Timur Tabi32961932012-08-14 13:20:23 +0000461 * of_get_next_available_child - Find the next available child node
462 * @node: parent node
463 * @prev: previous child of the parent node, or NULL to get first
464 *
465 * This function is like of_get_next_child(), except that it
466 * automatically skips any disabled nodes (i.e. status = "disabled").
467 */
468struct device_node *of_get_next_available_child(const struct device_node *node,
469 struct device_node *prev)
470{
471 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000472 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000473
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000474 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000475 next = prev ? prev->sibling : node->child;
476 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700477 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000478 continue;
479 if (of_node_get(next))
480 break;
481 }
482 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000483 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000484 return next;
485}
486EXPORT_SYMBOL(of_get_next_available_child);
487
488/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100489 * of_get_child_by_name - Find the child node by name for a given parent
490 * @node: parent node
491 * @name: child name to look for.
492 *
493 * This function looks for child node for given matching name
494 *
495 * Returns a node pointer if found, with refcount incremented, use
496 * of_node_put() on it when done.
497 * Returns NULL if node is not found.
498 */
499struct device_node *of_get_child_by_name(const struct device_node *node,
500 const char *name)
501{
502 struct device_node *child;
503
504 for_each_child_of_node(node, child)
505 if (child->name && (of_node_cmp(child->name, name) == 0))
506 break;
507 return child;
508}
509EXPORT_SYMBOL(of_get_child_by_name);
510
511/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000512 * of_find_node_by_path - Find a node matching a full OF path
513 * @path: The full path to match
514 *
515 * Returns a node pointer with refcount incremented, use
516 * of_node_put() on it when done.
517 */
518struct device_node *of_find_node_by_path(const char *path)
519{
Randy Dunlap465aac62012-11-30 10:01:51 +0000520 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500521 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000522
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500523 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000524 for (; np; np = np->allnext) {
525 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
526 && of_node_get(np))
527 break;
528 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500529 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000530 return np;
531}
532EXPORT_SYMBOL(of_find_node_by_path);
533
534/**
535 * of_find_node_by_name - Find a node by its "name" property
536 * @from: The node to start searching from or NULL, the node
537 * you pass will not be searched, only the next one
538 * will; typically, you pass what the previous call
539 * returned. of_node_put() will be called on it
540 * @name: The name string to match against
541 *
542 * Returns a node pointer with refcount incremented, use
543 * of_node_put() on it when done.
544 */
545struct device_node *of_find_node_by_name(struct device_node *from,
546 const char *name)
547{
548 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500549 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000550
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500551 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000552 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000553 for (; np; np = np->allnext)
554 if (np->name && (of_node_cmp(np->name, name) == 0)
555 && of_node_get(np))
556 break;
557 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500558 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000559 return np;
560}
561EXPORT_SYMBOL(of_find_node_by_name);
562
563/**
564 * of_find_node_by_type - Find a node by its "device_type" property
565 * @from: The node to start searching from, or NULL to start searching
566 * the entire device tree. The node you pass will not be
567 * searched, only the next one will; typically, you pass
568 * what the previous call returned. of_node_put() will be
569 * called on from for you.
570 * @type: The type string to match against
571 *
572 * Returns a node pointer with refcount incremented, use
573 * of_node_put() on it when done.
574 */
575struct device_node *of_find_node_by_type(struct device_node *from,
576 const char *type)
577{
578 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500579 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000580
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500581 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000582 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000583 for (; np; np = np->allnext)
584 if (np->type && (of_node_cmp(np->type, type) == 0)
585 && of_node_get(np))
586 break;
587 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500588 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000589 return np;
590}
591EXPORT_SYMBOL(of_find_node_by_type);
592
593/**
594 * of_find_compatible_node - Find a node based on type and one of the
595 * tokens in its "compatible" property
596 * @from: The node to start searching from or NULL, the node
597 * you pass will not be searched, only the next one
598 * will; typically, you pass what the previous call
599 * returned. of_node_put() will be called on it
600 * @type: The type string to match "device_type" or NULL to ignore
601 * @compatible: The string to match to one of the tokens in the device
602 * "compatible" list.
603 *
604 * Returns a node pointer with refcount incremented, use
605 * of_node_put() on it when done.
606 */
607struct device_node *of_find_compatible_node(struct device_node *from,
608 const char *type, const char *compatible)
609{
610 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500611 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000612
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500613 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000614 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000615 for (; np; np = np->allnext) {
616 if (type
617 && !(np->type && (of_node_cmp(np->type, type) == 0)))
618 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500619 if (__of_device_is_compatible(np, compatible) &&
620 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000621 break;
622 }
623 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500624 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000625 return np;
626}
627EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100628
629/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000630 * of_find_node_with_property - Find a node which has a property with
631 * the given name.
632 * @from: The node to start searching from or NULL, the node
633 * you pass will not be searched, only the next one
634 * will; typically, you pass what the previous call
635 * returned. of_node_put() will be called on it
636 * @prop_name: The name of the property to look for.
637 *
638 * Returns a node pointer with refcount incremented, use
639 * of_node_put() on it when done.
640 */
641struct device_node *of_find_node_with_property(struct device_node *from,
642 const char *prop_name)
643{
644 struct device_node *np;
645 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500646 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000647
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500648 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000649 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000650 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530651 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000652 if (of_prop_cmp(pp->name, prop_name) == 0) {
653 of_node_get(np);
654 goto out;
655 }
656 }
657 }
658out:
659 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500660 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000661 return np;
662}
663EXPORT_SYMBOL(of_find_node_with_property);
664
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500665static
666const struct of_device_id *__of_match_node(const struct of_device_id *matches,
667 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100668{
Grant Likelya52f07e2011-03-18 10:21:29 -0600669 if (!matches)
670 return NULL;
671
Grant Likely283029d2008-01-09 06:20:40 +1100672 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
673 int match = 1;
674 if (matches->name[0])
675 match &= node->name
676 && !strcmp(matches->name, node->name);
677 if (matches->type[0])
678 match &= node->type
679 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700680 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500681 match &= __of_device_is_compatible(node,
682 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700683 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100684 return matches;
685 matches++;
686 }
687 return NULL;
688}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500689
690/**
691 * of_match_node - Tell if an device_node has a matching of_match structure
692 * @matches: array of of device match structures to search in
693 * @node: the of device structure to match against
694 *
695 * Low level utility function used by device matching.
696 */
697const struct of_device_id *of_match_node(const struct of_device_id *matches,
698 const struct device_node *node)
699{
700 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500701 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500702
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500703 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500704 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500705 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500706 return match;
707}
Grant Likely283029d2008-01-09 06:20:40 +1100708EXPORT_SYMBOL(of_match_node);
709
710/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700711 * of_find_matching_node_and_match - Find a node based on an of_device_id
712 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100713 * @from: The node to start searching from or NULL, the node
714 * you pass will not be searched, only the next one
715 * will; typically, you pass what the previous call
716 * returned. of_node_put() will be called on it
717 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700718 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100719 *
720 * Returns a node pointer with refcount incremented, use
721 * of_node_put() on it when done.
722 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700723struct device_node *of_find_matching_node_and_match(struct device_node *from,
724 const struct of_device_id *matches,
725 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100726{
727 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800728 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500729 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100730
Stephen Warren50c8af42012-11-20 16:12:20 -0700731 if (match)
732 *match = NULL;
733
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500734 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000735 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100736 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500737 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800738 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700739 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800740 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100741 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700742 }
Grant Likely283029d2008-01-09 06:20:40 +1100743 }
744 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500745 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100746 return np;
747}
Grant Likely80c20222012-12-19 10:45:36 +0000748EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400749
750/**
Grant Likely3f07af42008-07-25 22:25:13 -0400751 * of_modalias_node - Lookup appropriate modalias for a device node
752 * @node: pointer to a device tree node
753 * @modalias: Pointer to buffer that modalias value will be copied into
754 * @len: Length of modalias value
755 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600756 * Based on the value of the compatible property, this routine will attempt
757 * to choose an appropriate modalias value for a particular device tree node.
758 * It does this by stripping the manufacturer prefix (as delimited by a ',')
759 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400760 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600761 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400762 */
763int of_modalias_node(struct device_node *node, char *modalias, int len)
764{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600765 const char *compatible, *p;
766 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400767
768 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600769 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400770 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400771 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600772 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400773 return 0;
774}
775EXPORT_SYMBOL_GPL(of_modalias_node);
776
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000777/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700778 * of_find_node_by_phandle - Find a node given a phandle
779 * @handle: phandle of the node to find
780 *
781 * Returns a node pointer with refcount incremented, use
782 * of_node_put() on it when done.
783 */
784struct device_node *of_find_node_by_phandle(phandle handle)
785{
786 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000787 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -0700788
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000789 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000790 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700791 if (np->phandle == handle)
792 break;
793 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000794 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700795 return np;
796}
797EXPORT_SYMBOL(of_find_node_by_phandle);
798
799/**
Heiko Stuebner10408542014-02-12 01:00:34 +0100800 * of_property_count_elems_of_size - Count the number of elements in a property
801 *
802 * @np: device node from which the property value is to be read.
803 * @propname: name of the property to be searched.
804 * @elem_size: size of the individual element
805 *
806 * Search for a property in a device node and count the number of elements of
807 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
808 * property does not exist or its length does not match a multiple of elem_size
809 * and -ENODATA if the property does not have a value.
810 */
811int of_property_count_elems_of_size(const struct device_node *np,
812 const char *propname, int elem_size)
813{
814 struct property *prop = of_find_property(np, propname, NULL);
815
816 if (!prop)
817 return -EINVAL;
818 if (!prop->value)
819 return -ENODATA;
820
821 if (prop->length % elem_size != 0) {
822 pr_err("size of %s in node %s is not a multiple of %d\n",
823 propname, np->full_name, elem_size);
824 return -EINVAL;
825 }
826
827 return prop->length / elem_size;
828}
829EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
830
831/**
Tony Priskdaeec1f2013-04-03 17:57:11 +1300832 * of_find_property_value_of_size
833 *
834 * @np: device node from which the property value is to be read.
835 * @propname: name of the property to be searched.
836 * @len: requested length of property value
837 *
838 * Search for a property in a device node and valid the requested size.
839 * Returns the property value on success, -EINVAL if the property does not
840 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
841 * property data isn't large enough.
842 *
843 */
844static void *of_find_property_value_of_size(const struct device_node *np,
845 const char *propname, u32 len)
846{
847 struct property *prop = of_find_property(np, propname, NULL);
848
849 if (!prop)
850 return ERR_PTR(-EINVAL);
851 if (!prop->value)
852 return ERR_PTR(-ENODATA);
853 if (len > prop->length)
854 return ERR_PTR(-EOVERFLOW);
855
856 return prop->value;
857}
858
859/**
Tony Prisk3daf3722013-03-23 17:02:15 +1300860 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
861 *
862 * @np: device node from which the property value is to be read.
863 * @propname: name of the property to be searched.
864 * @index: index of the u32 in the list of values
865 * @out_value: pointer to return value, modified only if no error.
866 *
867 * Search for a property in a device node and read nth 32-bit value from
868 * it. Returns 0 on success, -EINVAL if the property does not exist,
869 * -ENODATA if property does not have a value, and -EOVERFLOW if the
870 * property data isn't large enough.
871 *
872 * The out_value is modified only if a valid u32 value can be decoded.
873 */
874int of_property_read_u32_index(const struct device_node *np,
875 const char *propname,
876 u32 index, u32 *out_value)
877{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300878 const u32 *val = of_find_property_value_of_size(np, propname,
879 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +1300880
Tony Priskdaeec1f2013-04-03 17:57:11 +1300881 if (IS_ERR(val))
882 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +1300883
Tony Priskdaeec1f2013-04-03 17:57:11 +1300884 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +1300885 return 0;
886}
887EXPORT_SYMBOL_GPL(of_property_read_u32_index);
888
889/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530890 * of_property_read_u8_array - Find and read an array of u8 from a property.
891 *
892 * @np: device node from which the property value is to be read.
893 * @propname: name of the property to be searched.
894 * @out_value: pointer to return value, modified only if return value is 0.
895 * @sz: number of array elements to read
896 *
897 * Search for a property in a device node and read 8-bit value(s) from
898 * it. Returns 0 on success, -EINVAL if the property does not exist,
899 * -ENODATA if property does not have a value, and -EOVERFLOW if the
900 * property data isn't large enough.
901 *
902 * dts entry of array should be like:
903 * property = /bits/ 8 <0x50 0x60 0x70>;
904 *
905 * The out_value is modified only if a valid u8 value can be decoded.
906 */
907int of_property_read_u8_array(const struct device_node *np,
908 const char *propname, u8 *out_values, size_t sz)
909{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300910 const u8 *val = of_find_property_value_of_size(np, propname,
911 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530912
Tony Priskdaeec1f2013-04-03 17:57:11 +1300913 if (IS_ERR(val))
914 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530915
Viresh Kumarbe193242012-11-20 10:15:19 +0530916 while (sz--)
917 *out_values++ = *val++;
918 return 0;
919}
920EXPORT_SYMBOL_GPL(of_property_read_u8_array);
921
922/**
923 * of_property_read_u16_array - Find and read an array of u16 from a property.
924 *
925 * @np: device node from which the property value is to be read.
926 * @propname: name of the property to be searched.
927 * @out_value: pointer to return value, modified only if return value is 0.
928 * @sz: number of array elements to read
929 *
930 * Search for a property in a device node and read 16-bit value(s) from
931 * it. Returns 0 on success, -EINVAL if the property does not exist,
932 * -ENODATA if property does not have a value, and -EOVERFLOW if the
933 * property data isn't large enough.
934 *
935 * dts entry of array should be like:
936 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
937 *
938 * The out_value is modified only if a valid u16 value can be decoded.
939 */
940int of_property_read_u16_array(const struct device_node *np,
941 const char *propname, u16 *out_values, size_t sz)
942{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300943 const __be16 *val = of_find_property_value_of_size(np, propname,
944 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530945
Tony Priskdaeec1f2013-04-03 17:57:11 +1300946 if (IS_ERR(val))
947 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530948
Viresh Kumarbe193242012-11-20 10:15:19 +0530949 while (sz--)
950 *out_values++ = be16_to_cpup(val++);
951 return 0;
952}
953EXPORT_SYMBOL_GPL(of_property_read_u16_array);
954
955/**
Rob Herring0e373632011-07-06 15:42:58 -0500956 * of_property_read_u32_array - Find and read an array of 32 bit integers
957 * from a property.
958 *
Thomas Abrahama3b85362011-06-30 21:26:10 +0530959 * @np: device node from which the property value is to be read.
960 * @propname: name of the property to be searched.
961 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530962 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +0530963 *
Rob Herring0e373632011-07-06 15:42:58 -0500964 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +0530965 * it. Returns 0 on success, -EINVAL if the property does not exist,
966 * -ENODATA if property does not have a value, and -EOVERFLOW if the
967 * property data isn't large enough.
968 *
969 * The out_value is modified only if a valid u32 value can be decoded.
970 */
Jamie Ilesaac285c2011-08-02 15:45:07 +0100971int of_property_read_u32_array(const struct device_node *np,
972 const char *propname, u32 *out_values,
973 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +0530974{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300975 const __be32 *val = of_find_property_value_of_size(np, propname,
976 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +0530977
Tony Priskdaeec1f2013-04-03 17:57:11 +1300978 if (IS_ERR(val))
979 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -0500980
Rob Herring0e373632011-07-06 15:42:58 -0500981 while (sz--)
982 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530983 return 0;
984}
Rob Herring0e373632011-07-06 15:42:58 -0500985EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +0530986
987/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100988 * of_property_read_u64 - Find and read a 64 bit integer from a property
989 * @np: device node from which the property value is to be read.
990 * @propname: name of the property to be searched.
991 * @out_value: pointer to return value, modified only if return value is 0.
992 *
993 * Search for a property in a device node and read a 64-bit value from
994 * it. Returns 0 on success, -EINVAL if the property does not exist,
995 * -ENODATA if property does not have a value, and -EOVERFLOW if the
996 * property data isn't large enough.
997 *
998 * The out_value is modified only if a valid u64 value can be decoded.
999 */
1000int of_property_read_u64(const struct device_node *np, const char *propname,
1001 u64 *out_value)
1002{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001003 const __be32 *val = of_find_property_value_of_size(np, propname,
1004 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001005
Tony Priskdaeec1f2013-04-03 17:57:11 +13001006 if (IS_ERR(val))
1007 return PTR_ERR(val);
1008
1009 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001010 return 0;
1011}
1012EXPORT_SYMBOL_GPL(of_property_read_u64);
1013
1014/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301015 * of_property_read_string - Find and read a string from a property
1016 * @np: device node from which the property value is to be read.
1017 * @propname: name of the property to be searched.
1018 * @out_string: pointer to null terminated return string, modified only if
1019 * return value is 0.
1020 *
1021 * Search for a property in a device tree node and retrieve a null
1022 * terminated string value (pointer to data, not a copy). Returns 0 on
1023 * success, -EINVAL if the property does not exist, -ENODATA if property
1024 * does not have a value, and -EILSEQ if the string is not null-terminated
1025 * within the length of the property data.
1026 *
1027 * The out_string pointer is modified only if a valid string can be decoded.
1028 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001029int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001030 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301031{
1032 struct property *prop = of_find_property(np, propname, NULL);
1033 if (!prop)
1034 return -EINVAL;
1035 if (!prop->value)
1036 return -ENODATA;
1037 if (strnlen(prop->value, prop->length) >= prop->length)
1038 return -EILSEQ;
1039 *out_string = prop->value;
1040 return 0;
1041}
1042EXPORT_SYMBOL_GPL(of_property_read_string);
1043
1044/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001045 * of_property_read_string_index - Find and read a string from a multiple
1046 * strings property.
1047 * @np: device node from which the property value is to be read.
1048 * @propname: name of the property to be searched.
1049 * @index: index of the string in the list of strings
1050 * @out_string: pointer to null terminated return string, modified only if
1051 * return value is 0.
1052 *
1053 * Search for a property in a device tree node and retrieve a null
1054 * terminated string value (pointer to data, not a copy) in the list of strings
1055 * contained in that property.
1056 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1057 * property does not have a value, and -EILSEQ if the string is not
1058 * null-terminated within the length of the property data.
1059 *
1060 * The out_string pointer is modified only if a valid string can be decoded.
1061 */
1062int of_property_read_string_index(struct device_node *np, const char *propname,
1063 int index, const char **output)
1064{
1065 struct property *prop = of_find_property(np, propname, NULL);
1066 int i = 0;
1067 size_t l = 0, total = 0;
1068 const char *p;
1069
1070 if (!prop)
1071 return -EINVAL;
1072 if (!prop->value)
1073 return -ENODATA;
1074 if (strnlen(prop->value, prop->length) >= prop->length)
1075 return -EILSEQ;
1076
1077 p = prop->value;
1078
1079 for (i = 0; total < prop->length; total += l, p += l) {
1080 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001081 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001082 *output = p;
1083 return 0;
1084 }
1085 }
1086 return -ENODATA;
1087}
1088EXPORT_SYMBOL_GPL(of_property_read_string_index);
1089
Grant Likely7aff0fe2011-12-12 09:25:58 -07001090/**
1091 * of_property_match_string() - Find string in a list and return index
1092 * @np: pointer to node containing string list property
1093 * @propname: string list property name
1094 * @string: pointer to string to search for in string list
1095 *
1096 * This function searches a string list property and returns the index
1097 * of a specific string value.
1098 */
1099int of_property_match_string(struct device_node *np, const char *propname,
1100 const char *string)
1101{
1102 struct property *prop = of_find_property(np, propname, NULL);
1103 size_t l;
1104 int i;
1105 const char *p, *end;
1106
1107 if (!prop)
1108 return -EINVAL;
1109 if (!prop->value)
1110 return -ENODATA;
1111
1112 p = prop->value;
1113 end = p + prop->length;
1114
1115 for (i = 0; p < end; i++, p += l) {
1116 l = strlen(p) + 1;
1117 if (p + l > end)
1118 return -EILSEQ;
1119 pr_debug("comparing %s with %s\n", string, p);
1120 if (strcmp(string, p) == 0)
1121 return i; /* Found it; return index */
1122 }
1123 return -ENODATA;
1124}
1125EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001126
1127/**
1128 * of_property_count_strings - Find and return the number of strings from a
1129 * multiple strings property.
1130 * @np: device node from which the property value is to be read.
1131 * @propname: name of the property to be searched.
1132 *
1133 * Search for a property in a device tree node and retrieve the number of null
1134 * terminated string contain in it. Returns the number of strings on
1135 * success, -EINVAL if the property does not exist, -ENODATA if property
1136 * does not have a value, and -EILSEQ if the string is not null-terminated
1137 * within the length of the property data.
1138 */
1139int of_property_count_strings(struct device_node *np, const char *propname)
1140{
1141 struct property *prop = of_find_property(np, propname, NULL);
1142 int i = 0;
1143 size_t l = 0, total = 0;
1144 const char *p;
1145
1146 if (!prop)
1147 return -EINVAL;
1148 if (!prop->value)
1149 return -ENODATA;
1150 if (strnlen(prop->value, prop->length) >= prop->length)
1151 return -EILSEQ;
1152
1153 p = prop->value;
1154
Benoit Cousson88af7f52011-12-05 15:23:54 +01001155 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001156 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001157
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001158 return i;
1159}
1160EXPORT_SYMBOL_GPL(of_property_count_strings);
1161
Grant Likelybd69f732013-02-10 22:57:21 +00001162static int __of_parse_phandle_with_args(const struct device_node *np,
1163 const char *list_name,
Stephen Warrenf35c0932013-08-14 15:27:10 -06001164 const char *cells_name,
1165 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001166 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001167{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001168 const __be32 *list, *list_end;
Grant Likely23ce04c02013-02-12 21:21:49 +00001169 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001170 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001171 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001172 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001173
Grant Likely15c9a0a2011-12-12 09:25:57 -07001174 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001175 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001176 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001177 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001178 list_end = list + size / sizeof(*list);
1179
Grant Likely15c9a0a2011-12-12 09:25:57 -07001180 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001181 while (list < list_end) {
Grant Likely23ce04c02013-02-12 21:21:49 +00001182 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001183 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001184
Grant Likely15c9a0a2011-12-12 09:25:57 -07001185 /*
1186 * If phandle is 0, then it is an empty entry with no
1187 * arguments. Skip forward to the next entry.
1188 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001189 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001190 if (phandle) {
1191 /*
1192 * Find the provider node and parse the #*-cells
Stephen Warren3a9560b2013-08-14 15:27:11 -06001193 * property to determine the argument length.
1194 *
1195 * This is not needed if the cell count is hard-coded
1196 * (i.e. cells_name not set, but cell_count is set),
1197 * except when we're going to return the found node
1198 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001199 */
Stephen Warren3a9560b2013-08-14 15:27:11 -06001200 if (cells_name || cur_index == index) {
1201 node = of_find_node_by_phandle(phandle);
1202 if (!node) {
1203 pr_err("%s: could not find phandle\n",
1204 np->full_name);
1205 goto err;
1206 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001207 }
Stephen Warrenf35c0932013-08-14 15:27:10 -06001208
1209 if (cells_name) {
1210 if (of_property_read_u32(node, cells_name,
1211 &count)) {
1212 pr_err("%s: could not get %s for %s\n",
1213 np->full_name, cells_name,
1214 node->full_name);
1215 goto err;
1216 }
1217 } else {
1218 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001219 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001220
Grant Likely15c9a0a2011-12-12 09:25:57 -07001221 /*
1222 * Make sure that the arguments actually fit in the
1223 * remaining property data length
1224 */
1225 if (list + count > list_end) {
1226 pr_err("%s: arguments longer than property\n",
1227 np->full_name);
Grant Likely23ce04c02013-02-12 21:21:49 +00001228 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001229 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001230 }
1231
Grant Likely15c9a0a2011-12-12 09:25:57 -07001232 /*
1233 * All of the error cases above bail out of the loop, so at
1234 * this point, the parsing is successful. If the requested
1235 * index matches, then fill the out_args structure and return,
1236 * or return -ENOENT for an empty entry.
1237 */
Grant Likely23ce04c02013-02-12 21:21:49 +00001238 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001239 if (cur_index == index) {
1240 if (!phandle)
Grant Likely23ce04c02013-02-12 21:21:49 +00001241 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001242
Grant Likely15c9a0a2011-12-12 09:25:57 -07001243 if (out_args) {
1244 int i;
1245 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1246 count = MAX_PHANDLE_ARGS;
1247 out_args->np = node;
1248 out_args->args_count = count;
1249 for (i = 0; i < count; i++)
1250 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001251 } else {
1252 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001253 }
Grant Likely23ce04c02013-02-12 21:21:49 +00001254
1255 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001256 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001257 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001258
1259 of_node_put(node);
1260 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001261 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001262 cur_index++;
1263 }
1264
Grant Likely23ce04c02013-02-12 21:21:49 +00001265 /*
1266 * Unlock node before returning result; will be one of:
1267 * -ENOENT : index is for empty phandle
1268 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001269 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c02013-02-12 21:21:49 +00001270 */
Grant Likelybd69f732013-02-10 22:57:21 +00001271 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c02013-02-12 21:21:49 +00001272 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001273 if (node)
1274 of_node_put(node);
Grant Likely23ce04c02013-02-12 21:21:49 +00001275 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001276}
Grant Likelybd69f732013-02-10 22:57:21 +00001277
Stephen Warrend2621342013-08-14 15:27:08 -06001278/**
Stephen Warren73ed9312013-08-14 15:27:09 -06001279 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1280 * @np: Pointer to device node holding phandle property
1281 * @phandle_name: Name of property holding a phandle value
1282 * @index: For properties holding a table of phandles, this is the index into
1283 * the table
1284 *
1285 * Returns the device_node pointer with refcount incremented. Use
1286 * of_node_put() on it when done.
1287 */
1288struct device_node *of_parse_phandle(const struct device_node *np,
1289 const char *phandle_name, int index)
1290{
Stephen Warren3a9560b2013-08-14 15:27:11 -06001291 struct of_phandle_args args;
Stephen Warren73ed9312013-08-14 15:27:09 -06001292
Stephen Warren3a9560b2013-08-14 15:27:11 -06001293 if (index < 0)
Stephen Warren73ed9312013-08-14 15:27:09 -06001294 return NULL;
1295
Stephen Warren3a9560b2013-08-14 15:27:11 -06001296 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1297 index, &args))
1298 return NULL;
1299
1300 return args.np;
Stephen Warren73ed9312013-08-14 15:27:09 -06001301}
1302EXPORT_SYMBOL(of_parse_phandle);
1303
1304/**
Stephen Warrend2621342013-08-14 15:27:08 -06001305 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1306 * @np: pointer to a device tree node containing a list
1307 * @list_name: property name that contains a list
1308 * @cells_name: property name that specifies phandles' arguments count
1309 * @index: index of a phandle to parse out
1310 * @out_args: optional pointer to output arguments structure (will be filled)
1311 *
1312 * This function is useful to parse lists of phandles and their arguments.
1313 * Returns 0 on success and fills out_args, on error returns appropriate
1314 * errno value.
1315 *
1316 * Caller is responsible to call of_node_put() on the returned out_args->node
1317 * pointer.
1318 *
1319 * Example:
1320 *
1321 * phandle1: node1 {
1322 * #list-cells = <2>;
1323 * }
1324 *
1325 * phandle2: node2 {
1326 * #list-cells = <1>;
1327 * }
1328 *
1329 * node3 {
1330 * list = <&phandle1 1 2 &phandle2 3>;
1331 * }
1332 *
1333 * To get a device_node of the `node2' node you may call this:
1334 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1335 */
Grant Likelybd69f732013-02-10 22:57:21 +00001336int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1337 const char *cells_name, int index,
1338 struct of_phandle_args *out_args)
1339{
1340 if (index < 0)
1341 return -EINVAL;
Stephen Warrenf35c0932013-08-14 15:27:10 -06001342 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1343 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001344}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001345EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001346
Grant Likelybd69f732013-02-10 22:57:21 +00001347/**
Stephen Warrenf35c0932013-08-14 15:27:10 -06001348 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1349 * @np: pointer to a device tree node containing a list
1350 * @list_name: property name that contains a list
1351 * @cell_count: number of argument cells following the phandle
1352 * @index: index of a phandle to parse out
1353 * @out_args: optional pointer to output arguments structure (will be filled)
1354 *
1355 * This function is useful to parse lists of phandles and their arguments.
1356 * Returns 0 on success and fills out_args, on error returns appropriate
1357 * errno value.
1358 *
1359 * Caller is responsible to call of_node_put() on the returned out_args->node
1360 * pointer.
1361 *
1362 * Example:
1363 *
1364 * phandle1: node1 {
1365 * }
1366 *
1367 * phandle2: node2 {
1368 * }
1369 *
1370 * node3 {
1371 * list = <&phandle1 0 2 &phandle2 2 3>;
1372 * }
1373 *
1374 * To get a device_node of the `node2' node you may call this:
1375 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1376 */
1377int of_parse_phandle_with_fixed_args(const struct device_node *np,
1378 const char *list_name, int cell_count,
1379 int index, struct of_phandle_args *out_args)
1380{
1381 if (index < 0)
1382 return -EINVAL;
1383 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1384 index, out_args);
1385}
1386EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1387
1388/**
Grant Likelybd69f732013-02-10 22:57:21 +00001389 * of_count_phandle_with_args() - Find the number of phandles references in a property
1390 * @np: pointer to a device tree node containing a list
1391 * @list_name: property name that contains a list
1392 * @cells_name: property name that specifies phandles' arguments count
1393 *
1394 * Returns the number of phandle + argument tuples within a property. It
1395 * is a typical pattern to encode a list of phandle and variable
1396 * arguments into a single property. The number of arguments is encoded
1397 * by a property in the phandle-target node. For example, a gpios
1398 * property would contain a list of GPIO specifies consisting of a
1399 * phandle and 1 or more arguments. The number of arguments are
1400 * determined by the #gpio-cells property in the node pointed to by the
1401 * phandle.
1402 */
1403int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1404 const char *cells_name)
1405{
Stephen Warrenf35c0932013-08-14 15:27:10 -06001406 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1407 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001408}
1409EXPORT_SYMBOL(of_count_phandle_with_args);
1410
Grant Likely02af11b2009-11-23 20:16:45 -07001411/**
Xiubo Li24648be2014-01-22 13:57:39 +08001412 * __of_add_property - Add a property to a node without lock operations
1413 */
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001414int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li24648be2014-01-22 13:57:39 +08001415{
1416 struct property **next;
1417
1418 prop->next = NULL;
1419 next = &np->properties;
1420 while (*next) {
1421 if (strcmp(prop->name, (*next)->name) == 0)
1422 /* duplicate ! don't insert it */
1423 return -EEXIST;
1424
1425 next = &(*next)->next;
1426 }
1427 *next = prop;
1428
1429 return 0;
1430}
1431
1432/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001433 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001434 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001435int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001436{
Grant Likely02af11b2009-11-23 20:16:45 -07001437 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001438 int rc;
1439
Grant Likely952f9f42014-07-23 17:05:06 -06001440 mutex_lock(&of_mutex);
1441
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001442 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li24648be2014-01-22 13:57:39 +08001443 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001444 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely952f9f42014-07-23 17:05:06 -06001445
Xiubo Li24648be2014-01-22 13:57:39 +08001446 if (!rc)
Pantelis Antonioub74e9e12013-12-13 20:08:59 +02001447 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001448
Grant Likely952f9f42014-07-23 17:05:06 -06001449 mutex_unlock(&of_mutex);
1450
Grant Likely3e262052014-07-16 12:48:23 -06001451 if (!rc)
1452 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1453
Xiubo Li24648be2014-01-22 13:57:39 +08001454 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001455}
1456
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001457int __of_remove_property(struct device_node *np, struct property *prop)
1458{
1459 struct property **next;
1460
1461 for (next = &np->properties; *next; next = &(*next)->next) {
1462 if (*next == prop)
1463 break;
1464 }
1465 if (*next == NULL)
1466 return -ENODEV;
1467
1468 /* found the node */
1469 *next = prop->next;
1470 prop->next = np->deadprops;
1471 np->deadprops = prop;
1472
1473 return 0;
1474}
1475
Grant Likely952f9f42014-07-23 17:05:06 -06001476void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1477{
1478 /* at early boot, bail here and defer setup to of_init() */
1479 if (of_kset && of_node_is_attached(np))
1480 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1481}
1482
Grant Likely02af11b2009-11-23 20:16:45 -07001483/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001484 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001485 *
1486 * Note that we don't actually remove it, since we have given out
1487 * who-knows-how-many pointers to the data using get-property.
1488 * Instead we just move the property to the "dead properties"
1489 * list, so it won't be found any more.
1490 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001491int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001492{
Grant Likely02af11b2009-11-23 20:16:45 -07001493 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001494 int rc;
1495
Grant Likely952f9f42014-07-23 17:05:06 -06001496 mutex_lock(&of_mutex);
1497
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001498 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001499 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001500 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001501
Grant Likely952f9f42014-07-23 17:05:06 -06001502 if (!rc)
1503 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001504
Grant Likely952f9f42014-07-23 17:05:06 -06001505 mutex_unlock(&of_mutex);
Grant Likelyf57a8e22014-02-20 18:02:11 +00001506
Grant Likely3e262052014-07-16 12:48:23 -06001507 if (!rc)
1508 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1509
Grant Likely952f9f42014-07-23 17:05:06 -06001510 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001511}
1512
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001513int __of_update_property(struct device_node *np, struct property *newprop,
1514 struct property **oldpropp)
1515{
1516 struct property **next, *oldprop;
1517
1518 for (next = &np->properties; *next; next = &(*next)->next) {
1519 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1520 break;
1521 }
1522 *oldpropp = oldprop = *next;
1523
1524 if (oldprop) {
1525 /* replace the node */
1526 newprop->next = oldprop->next;
1527 *next = newprop;
1528 oldprop->next = np->deadprops;
1529 np->deadprops = oldprop;
1530 } else {
1531 /* new node */
1532 newprop->next = NULL;
1533 *next = newprop;
1534 }
1535
1536 return 0;
1537}
1538
Grant Likely952f9f42014-07-23 17:05:06 -06001539void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1540 struct property *oldprop)
1541{
1542 /* At early boot, bail out and defer setup to of_init() */
1543 if (!of_kset)
1544 return;
1545
1546 if (oldprop)
1547 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1548 __of_add_property_sysfs(np, newprop);
1549}
1550
Grant Likely02af11b2009-11-23 20:16:45 -07001551/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001552 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001553 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001554 *
1555 * Note that we don't actually remove it, since we have given out
1556 * who-knows-how-many pointers to the data using get-property.
1557 * Instead we just move the property to the "dead properties" list,
1558 * and add the new property to the property list
1559 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001560int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001561{
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001562 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001563 unsigned long flags;
Grant Likely952f9f42014-07-23 17:05:06 -06001564 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001565
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001566 if (!newprop->name)
1567 return -EINVAL;
1568
Grant Likely952f9f42014-07-23 17:05:06 -06001569 mutex_lock(&of_mutex);
1570
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001571 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001572 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001573 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelyf57a8e22014-02-20 18:02:11 +00001574
Grant Likely952f9f42014-07-23 17:05:06 -06001575 if (!rc)
1576 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likely02af11b2009-11-23 20:16:45 -07001577
Grant Likely952f9f42014-07-23 17:05:06 -06001578 mutex_unlock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001579
Grant Likely3e262052014-07-16 12:48:23 -06001580 if (!rc)
1581 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1582
Grant Likely952f9f42014-07-23 17:05:06 -06001583 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001584}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001585
Shawn Guo611cad72011-08-15 15:28:14 +08001586static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1587 int id, const char *stem, int stem_len)
1588{
1589 ap->np = np;
1590 ap->id = id;
1591 strncpy(ap->stem, stem, stem_len);
1592 ap->stem[stem_len] = 0;
1593 list_add_tail(&ap->link, &aliases_lookup);
1594 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001595 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001596}
1597
1598/**
1599 * of_alias_scan - Scan all properties of 'aliases' node
1600 *
1601 * The function scans all the properties of 'aliases' node and populate
1602 * the the global lookup table with the properties. It returns the
1603 * number of alias_prop found, or error code in error case.
1604 *
1605 * @dt_alloc: An allocator that provides a virtual address to memory
1606 * for the resulting tree
1607 */
1608void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1609{
1610 struct property *pp;
1611
1612 of_chosen = of_find_node_by_path("/chosen");
1613 if (of_chosen == NULL)
1614 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauerca257782013-08-05 14:40:44 +02001615
1616 if (of_chosen) {
1617 const char *name;
1618
1619 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1620 if (name)
1621 of_stdout = of_find_node_by_path(name);
1622 }
1623
Shawn Guo611cad72011-08-15 15:28:14 +08001624 of_aliases = of_find_node_by_path("/aliases");
1625 if (!of_aliases)
1626 return;
1627
Dong Aisheng8af0da92011-12-22 20:19:24 +08001628 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001629 const char *start = pp->name;
1630 const char *end = start + strlen(start);
1631 struct device_node *np;
1632 struct alias_prop *ap;
1633 int id, len;
1634
1635 /* Skip those we do not want to proceed */
1636 if (!strcmp(pp->name, "name") ||
1637 !strcmp(pp->name, "phandle") ||
1638 !strcmp(pp->name, "linux,phandle"))
1639 continue;
1640
1641 np = of_find_node_by_path(pp->value);
1642 if (!np)
1643 continue;
1644
1645 /* walk the alias backwards to extract the id and work out
1646 * the 'stem' string */
1647 while (isdigit(*(end-1)) && end > start)
1648 end--;
1649 len = end - start;
1650
1651 if (kstrtoint(end, 10, &id) < 0)
1652 continue;
1653
1654 /* Allocate an alias_prop with enough space for the stem */
1655 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1656 if (!ap)
1657 continue;
1658 ap->alias = start;
1659 of_alias_add(ap, np, id, start, len);
1660 }
1661}
1662
1663/**
1664 * of_alias_get_id - Get alias id for the given device_node
1665 * @np: Pointer to the given device_node
1666 * @stem: Alias stem of the given device_node
1667 *
1668 * The function travels the lookup table to get alias id for the given
1669 * device_node and alias stem. It returns the alias id if find it.
1670 */
1671int of_alias_get_id(struct device_node *np, const char *stem)
1672{
1673 struct alias_prop *app;
1674 int id = -ENODEV;
1675
Pantelis Antoniou59cf2512014-07-04 19:58:03 +03001676 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001677 list_for_each_entry(app, &aliases_lookup, link) {
1678 if (strcmp(app->stem, stem) != 0)
1679 continue;
1680
1681 if (np == app->np) {
1682 id = app->id;
1683 break;
1684 }
1685 }
Pantelis Antoniou59cf2512014-07-04 19:58:03 +03001686 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001687
1688 return id;
1689}
1690EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001691
1692const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1693 u32 *pu)
1694{
1695 const void *curv = cur;
1696
1697 if (!prop)
1698 return NULL;
1699
1700 if (!cur) {
1701 curv = prop->value;
1702 goto out_val;
1703 }
1704
1705 curv += sizeof(*cur);
1706 if (curv >= prop->value + prop->length)
1707 return NULL;
1708
1709out_val:
1710 *pu = be32_to_cpup(curv);
1711 return curv;
1712}
1713EXPORT_SYMBOL_GPL(of_prop_next_u32);
1714
1715const char *of_prop_next_string(struct property *prop, const char *cur)
1716{
1717 const void *curv = cur;
1718
1719 if (!prop)
1720 return NULL;
1721
1722 if (!cur)
1723 return prop->value;
1724
1725 curv += strlen(cur) + 1;
1726 if (curv >= prop->value + prop->length)
1727 return NULL;
1728
1729 return curv;
1730}
1731EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauerca257782013-08-05 14:40:44 +02001732
1733/**
1734 * of_device_is_stdout_path - check if a device node matches the
1735 * linux,stdout-path property
1736 *
1737 * Check if this device node matches the linux,stdout-path property
1738 * in the chosen node. return true if yes, false otherwise.
1739 */
1740int of_device_is_stdout_path(struct device_node *dn)
1741{
1742 if (!of_stdout)
1743 return false;
1744
1745 return of_stdout == dn;
1746}
1747EXPORT_SYMBOL_GPL(of_device_is_stdout_path);