blob: f0668356867bbbaceb514d4205597bcbdaf9be6d [file] [log] [blame]
Stephen Rothwell97e873e2007-05-01 16:26:07 +10001/*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11 *
Grant Likelye91edcf2009-10-15 10:58:09 -060012 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
Stephen Rothwell97e873e2007-05-01 16:26:07 +100014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
Shawn Guo611cad72011-08-15 15:28:14 +080020#include <linux/ctype.h>
Sudeep KarkadaNagesha6e2bb912013-08-15 14:01:40 +010021#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100022#include <linux/module.h>
23#include <linux/of.h>
Mathieu Poirierf7460932014-10-08 13:09:30 -060024#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100025#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Grant Likelyf57a8e22014-02-20 18:02:11 +000027#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070028#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100029
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080030#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080031
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080032LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080033
Randy Dunlap465aac62012-11-30 10:01:51 +000034struct device_node *of_allnodes;
35EXPORT_SYMBOL(of_allnodes);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070036struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080037struct device_node *of_aliases;
Sascha Hauer88a81042013-08-05 14:40:44 +020038static struct device_node *of_stdout;
Shawn Guo611cad72011-08-15 15:28:14 +080039
Grant Likely952f9f42014-07-23 17:05:06 -060040struct kset *of_kset;
Grant Likelyf57a8e22014-02-20 18:02:11 +000041
42/*
Grant Likely952f9f42014-07-23 17:05:06 -060043 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
44 * This mutex must be held whenever modifications are being made to the
45 * device tree. The of_{attach,detach}_node() and
46 * of_{add,remove,update}_property() helpers make sure this happens.
Grant Likelyf57a8e22014-02-20 18:02:11 +000047 */
Pantelis Antoniou59cf2512014-07-04 19:58:03 +030048DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100049
Stephen Rothwell581b6052007-04-24 16:46:53 +100050/* use when traversing tree through the allnext, child, sibling,
51 * or parent members of struct device_node.
52 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050053DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100054
55int of_n_addr_cells(struct device_node *np)
56{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060057 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100058
59 do {
60 if (np->parent)
61 np = np->parent;
62 ip = of_get_property(np, "#address-cells", NULL);
63 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070064 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100065 } while (np->parent);
66 /* No #address-cells property for the root node */
67 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
68}
69EXPORT_SYMBOL(of_n_addr_cells);
70
71int of_n_size_cells(struct device_node *np)
72{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060073 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100074
75 do {
76 if (np->parent)
77 np = np->parent;
78 ip = of_get_property(np, "#size-cells", NULL);
79 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070080 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100081 } while (np->parent);
82 /* No #size-cells property for the root node */
83 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
84}
85EXPORT_SYMBOL(of_n_size_cells);
86
Grant Likely50788572014-06-26 15:40:48 +010087#ifndef CONFIG_OF_DYNAMIC
Grant Likelyf57a8e22014-02-20 18:02:11 +000088static void of_node_release(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -070089{
Grant Likelyf57a8e22014-02-20 18:02:11 +000090 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
Grant Likely923f7e32010-01-28 13:52:53 -070091}
Grant Likely0f22dd32012-02-15 20:38:40 -070092#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -070093
Grant Likelyf57a8e22014-02-20 18:02:11 +000094struct kobj_type of_node_ktype = {
95 .release = of_node_release,
96};
97
98static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
99 struct bin_attribute *bin_attr, char *buf,
100 loff_t offset, size_t count)
101{
102 struct property *pp = container_of(bin_attr, struct property, attr);
103 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
104}
105
106static const char *safe_name(struct kobject *kobj, const char *orig_name)
107{
108 const char *name = orig_name;
109 struct sysfs_dirent *kn;
110 int i = 0;
111
112 /* don't be a hero. After 16 tries give up */
113 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, NULL, name))) {
114 sysfs_put(kn);
115 if (name != orig_name)
116 kfree(name);
117 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
118 }
119
120 if (name != orig_name)
121 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
122 kobject_name(kobj), name);
123 return name;
124}
125
Grant Likely952f9f42014-07-23 17:05:06 -0600126int __of_add_property_sysfs(struct device_node *np, struct property *pp)
Grant Likelyf57a8e22014-02-20 18:02:11 +0000127{
128 int rc;
129
130 /* Important: Don't leak passwords */
131 bool secure = strncmp(pp->name, "security-", 9) == 0;
132
Grant Likely952f9f42014-07-23 17:05:06 -0600133 if (!of_kset || !of_node_is_attached(np))
134 return 0;
135
Grant Likelyf57a8e22014-02-20 18:02:11 +0000136 sysfs_bin_attr_init(&pp->attr);
137 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
138 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
139 pp->attr.size = secure ? 0 : pp->length;
140 pp->attr.read = of_node_property_read;
141
142 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
143 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
144 return rc;
145}
146
Grant Likely952f9f42014-07-23 17:05:06 -0600147int __of_attach_node_sysfs(struct device_node *np)
Grant Likelyf57a8e22014-02-20 18:02:11 +0000148{
149 const char *name;
150 struct property *pp;
151 int rc;
152
Grant Likely952f9f42014-07-23 17:05:06 -0600153 if (!of_kset)
154 return 0;
155
Grant Likelyf57a8e22014-02-20 18:02:11 +0000156 np->kobj.kset = of_kset;
157 if (!np->parent) {
158 /* Nodes without parents are new top level trees */
159 rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base"));
160 } else {
161 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
162 if (!name || !name[0])
163 return -EINVAL;
164
165 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
166 }
167 if (rc)
168 return rc;
169
170 for_each_property_of_node(np, pp)
171 __of_add_property_sysfs(np, pp);
172
173 return 0;
174}
175
Grant Likelyf57a8e22014-02-20 18:02:11 +0000176static int __init of_init(void)
177{
178 struct device_node *np;
179
180 /* Create the kset, and register existing nodes */
Pantelis Antoniou59cf2512014-07-04 19:58:03 +0300181 mutex_lock(&of_mutex);
Grant Likelyf57a8e22014-02-20 18:02:11 +0000182 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
183 if (!of_kset) {
Pantelis Antoniou59cf2512014-07-04 19:58:03 +0300184 mutex_unlock(&of_mutex);
Grant Likelyf57a8e22014-02-20 18:02:11 +0000185 return -ENOMEM;
186 }
187 for_each_of_allnodes(np)
Grant Likely952f9f42014-07-23 17:05:06 -0600188 __of_attach_node_sysfs(np);
Pantelis Antoniou59cf2512014-07-04 19:58:03 +0300189 mutex_unlock(&of_mutex);
Grant Likelyf57a8e22014-02-20 18:02:11 +0000190
Grant Likely43bba282012-11-06 21:03:27 +0000191 /* Symlink in /proc as required by userspace ABI */
Grant Likelyf57a8e22014-02-20 18:02:11 +0000192 if (of_allnodes)
193 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likelyf57a8e22014-02-20 18:02:11 +0000194
195 return 0;
196}
197core_initcall(of_init);
198
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500199static struct property *__of_find_property(const struct device_node *np,
200 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000201{
202 struct property *pp;
203
Timur Tabi64e45662008-05-08 05:19:59 +1000204 if (!np)
205 return NULL;
206
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530207 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000208 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530209 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000210 *lenp = pp->length;
211 break;
212 }
213 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500214
215 return pp;
216}
217
218struct property *of_find_property(const struct device_node *np,
219 const char *name,
220 int *lenp)
221{
222 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500223 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500224
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500225 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500226 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500227 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000228
229 return pp;
230}
231EXPORT_SYMBOL(of_find_property);
232
Grant Likelye91edcf2009-10-15 10:58:09 -0600233/**
234 * of_find_all_nodes - Get next node in global list
235 * @prev: Previous node or NULL to start iteration
236 * of_node_put() will be called on it
237 *
238 * Returns a node pointer with refcount incremented, use
239 * of_node_put() on it when done.
240 */
241struct device_node *of_find_all_nodes(struct device_node *prev)
242{
243 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000244 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600245
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000246 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000247 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600248 for (; np != NULL; np = np->allnext)
249 if (of_node_get(np))
250 break;
251 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000252 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600253 return np;
254}
255EXPORT_SYMBOL(of_find_all_nodes);
256
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000257/*
258 * Find a property with a given name for a given node
259 * and return the value.
260 */
Grant Likely3a09bee2014-07-15 23:25:43 -0600261const void *__of_get_property(const struct device_node *np,
262 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500263{
264 struct property *pp = __of_find_property(np, name, lenp);
265
266 return pp ? pp->value : NULL;
267}
268
269/*
270 * Find a property with a given name for a given node
271 * and return the value.
272 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000273const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500274 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000275{
276 struct property *pp = of_find_property(np, name, lenp);
277
278 return pp ? pp->value : NULL;
279}
280EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000281
Sudeep KarkadaNagesha6e2bb912013-08-15 14:01:40 +0100282/*
283 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
284 *
285 * @cpu: logical cpu index of a core/thread
286 * @phys_id: physical identifier of a core/thread
287 *
288 * CPU logical to physical index mapping is architecture specific.
289 * However this __weak function provides a default match of physical
290 * id to logical cpu index. phys_id provided here is usually values read
291 * from the device tree which must match the hardware internal registers.
292 *
293 * Returns true if the physical identifier and the logical cpu index
294 * correspond to the same core/thread, false otherwise.
295 */
296bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
297{
298 return (u32)phys_id == cpu;
299}
300
301/**
302 * Checks if the given "prop_name" property holds the physical id of the
303 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
304 * NULL, local thread number within the core is returned in it.
305 */
306static bool __of_find_n_match_cpu_property(struct device_node *cpun,
307 const char *prop_name, int cpu, unsigned int *thread)
308{
309 const __be32 *cell;
310 int ac, prop_len, tid;
311 u64 hwid;
312
313 ac = of_n_addr_cells(cpun);
314 cell = of_get_property(cpun, prop_name, &prop_len);
315 if (!cell)
316 return false;
317 prop_len /= sizeof(*cell);
318 for (tid = 0; tid < prop_len; tid++) {
319 hwid = of_read_number(cell, ac);
320 if (arch_match_cpu_phys_id(cpu, hwid)) {
321 if (thread)
322 *thread = tid;
323 return true;
324 }
325 cell += ac;
326 }
327 return false;
328}
329
330/**
331 * of_get_cpu_node - Get device node associated with the given logical CPU
332 *
333 * @cpu: CPU number(logical index) for which device node is required
334 * @thread: if not NULL, local thread number within the physical core is
335 * returned
336 *
337 * The main purpose of this function is to retrieve the device node for the
338 * given logical CPU index. It should be used to initialize the of_node in
339 * cpu device. Once of_node in cpu device is populated, all the further
340 * references can use that instead.
341 *
342 * CPU logical to physical index mapping is architecture specific and is built
343 * before booting secondary cores. This function uses arch_match_cpu_phys_id
344 * which can be overridden by architecture specific implementation.
345 *
346 * Returns a node pointer for the logical cpu if found, else NULL.
347 */
348struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
349{
350 struct device_node *cpun, *cpus;
351
352 cpus = of_find_node_by_path("/cpus");
353 if (!cpus) {
354 pr_warn("Missing cpus node, bailing out\n");
355 return NULL;
356 }
357
358 for_each_child_of_node(cpus, cpun) {
359 if (of_node_cmp(cpun->type, "cpu"))
360 continue;
361 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
362 * for thread ids on PowerPC. If it doesn't exist fallback to
363 * standard "reg" property.
364 */
365 if (IS_ENABLED(CONFIG_PPC) &&
366 __of_find_n_match_cpu_property(cpun,
367 "ibm,ppc-interrupt-server#s", cpu, thread))
368 return cpun;
369 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
370 return cpun;
371 }
372 return NULL;
373}
374EXPORT_SYMBOL(of_get_cpu_node);
375
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000376/** Checks if the given "compat" string matches one of the strings in
377 * the device's "compatible" property
378 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500379static int __of_device_is_compatible(const struct device_node *device,
380 const char *compat)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000381{
382 const char* cp;
383 int cplen, l;
384
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500385 cp = __of_get_property(device, "compatible", &cplen);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000386 if (cp == NULL)
387 return 0;
388 while (cplen > 0) {
389 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
390 return 1;
391 l = strlen(cp) + 1;
392 cp += l;
393 cplen -= l;
394 }
395
396 return 0;
397}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500398
399/** Checks if the given "compat" string matches one of the strings in
400 * the device's "compatible" property
401 */
402int of_device_is_compatible(const struct device_node *device,
403 const char *compat)
404{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500405 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500406 int res;
407
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500408 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500409 res = __of_device_is_compatible(device, compat);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500410 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500411 return res;
412}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000413EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000414
415/**
Grant Likely71a157e2010-02-01 21:34:14 -0700416 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700417 * @compat: compatible string to look for in root node's compatible property.
418 *
419 * Returns true if the root node has the given value in its
420 * compatible property.
421 */
Grant Likely71a157e2010-02-01 21:34:14 -0700422int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700423{
424 struct device_node *root;
425 int rc = 0;
426
427 root = of_find_node_by_path("/");
428 if (root) {
429 rc = of_device_is_compatible(root, compat);
430 of_node_put(root);
431 }
432 return rc;
433}
Grant Likely71a157e2010-02-01 21:34:14 -0700434EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700435
436/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700437 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100438 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700439 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100440 *
441 * Returns 1 if the status property is absent or set to "okay" or "ok",
442 * 0 otherwise
443 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700444static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100445{
446 const char *status;
447 int statlen;
448
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700449 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100450 if (status == NULL)
451 return 1;
452
453 if (statlen > 0) {
454 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
455 return 1;
456 }
457
458 return 0;
459}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700460
461/**
462 * of_device_is_available - check if a device is available for use
463 *
464 * @device: Node to check for availability
465 *
466 * Returns 1 if the status property is absent or set to "okay" or "ok",
467 * 0 otherwise
468 */
469int of_device_is_available(const struct device_node *device)
470{
471 unsigned long flags;
472 int res;
473
474 raw_spin_lock_irqsave(&devtree_lock, flags);
475 res = __of_device_is_available(device);
476 raw_spin_unlock_irqrestore(&devtree_lock, flags);
477 return res;
478
479}
Josh Boyer834d97d2008-03-27 00:33:14 +1100480EXPORT_SYMBOL(of_device_is_available);
481
482/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000483 * of_get_parent - Get a node's parent if any
484 * @node: Node to get parent
485 *
486 * Returns a node pointer with refcount incremented, use
487 * of_node_put() on it when done.
488 */
489struct device_node *of_get_parent(const struct device_node *node)
490{
491 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500492 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000493
494 if (!node)
495 return NULL;
496
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500497 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000498 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500499 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000500 return np;
501}
502EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000503
504/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000505 * of_get_next_parent - Iterate to a node's parent
506 * @node: Node to get parent of
507 *
508 * This is like of_get_parent() except that it drops the
509 * refcount on the passed node, making it suitable for iterating
510 * through a node's parents.
511 *
512 * Returns a node pointer with refcount incremented, use
513 * of_node_put() on it when done.
514 */
515struct device_node *of_get_next_parent(struct device_node *node)
516{
517 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500518 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000519
520 if (!node)
521 return NULL;
522
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500523 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000524 parent = of_node_get(node->parent);
525 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500526 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000527 return parent;
528}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300529EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000530
531/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000532 * of_get_next_child - Iterate a node childs
533 * @node: parent node
534 * @prev: previous child of the parent node, or NULL to get first
535 *
536 * Returns a node pointer with refcount incremented, use
537 * of_node_put() on it when done.
538 */
539struct device_node *of_get_next_child(const struct device_node *node,
540 struct device_node *prev)
541{
542 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500543 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000544
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500545 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000546 next = prev ? prev->sibling : node->child;
547 for (; next; next = next->sibling)
548 if (of_node_get(next))
549 break;
550 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500551 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000552 return next;
553}
554EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000555
556/**
Timur Tabi32961932012-08-14 13:20:23 +0000557 * of_get_next_available_child - Find the next available child node
558 * @node: parent node
559 * @prev: previous child of the parent node, or NULL to get first
560 *
561 * This function is like of_get_next_child(), except that it
562 * automatically skips any disabled nodes (i.e. status = "disabled").
563 */
564struct device_node *of_get_next_available_child(const struct device_node *node,
565 struct device_node *prev)
566{
567 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000568 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000569
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000570 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000571 next = prev ? prev->sibling : node->child;
572 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700573 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000574 continue;
575 if (of_node_get(next))
576 break;
577 }
578 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000579 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000580 return next;
581}
582EXPORT_SYMBOL(of_get_next_available_child);
583
584/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100585 * of_get_child_by_name - Find the child node by name for a given parent
586 * @node: parent node
587 * @name: child name to look for.
588 *
589 * This function looks for child node for given matching name
590 *
591 * Returns a node pointer if found, with refcount incremented, use
592 * of_node_put() on it when done.
593 * Returns NULL if node is not found.
594 */
595struct device_node *of_get_child_by_name(const struct device_node *node,
596 const char *name)
597{
598 struct device_node *child;
599
600 for_each_child_of_node(node, child)
601 if (child->name && (of_node_cmp(child->name, name) == 0))
602 break;
603 return child;
604}
605EXPORT_SYMBOL(of_get_child_by_name);
606
607/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000608 * of_find_node_by_path - Find a node matching a full OF path
609 * @path: The full path to match
610 *
611 * Returns a node pointer with refcount incremented, use
612 * of_node_put() on it when done.
613 */
614struct device_node *of_find_node_by_path(const char *path)
615{
Randy Dunlap465aac62012-11-30 10:01:51 +0000616 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500617 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000618
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500619 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000620 for (; np; np = np->allnext) {
621 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
622 && of_node_get(np))
623 break;
624 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500625 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000626 return np;
627}
628EXPORT_SYMBOL(of_find_node_by_path);
629
630/**
631 * of_find_node_by_name - Find a node by its "name" property
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 * @name: The name string to match against
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_by_name(struct device_node *from,
642 const char *name)
643{
644 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500645 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000646
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500647 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000648 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000649 for (; np; np = np->allnext)
650 if (np->name && (of_node_cmp(np->name, name) == 0)
651 && of_node_get(np))
652 break;
653 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500654 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000655 return np;
656}
657EXPORT_SYMBOL(of_find_node_by_name);
658
659/**
660 * of_find_node_by_type - Find a node by its "device_type" property
661 * @from: The node to start searching from, or NULL to start searching
662 * the entire device tree. The node you pass will not be
663 * searched, only the next one will; typically, you pass
664 * what the previous call returned. of_node_put() will be
665 * called on from for you.
666 * @type: The type string to match against
667 *
668 * Returns a node pointer with refcount incremented, use
669 * of_node_put() on it when done.
670 */
671struct device_node *of_find_node_by_type(struct device_node *from,
672 const char *type)
673{
674 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500675 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000676
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500677 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000678 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000679 for (; np; np = np->allnext)
680 if (np->type && (of_node_cmp(np->type, type) == 0)
681 && of_node_get(np))
682 break;
683 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500684 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000685 return np;
686}
687EXPORT_SYMBOL(of_find_node_by_type);
688
689/**
690 * of_find_compatible_node - Find a node based on type and one of the
691 * tokens in its "compatible" property
692 * @from: The node to start searching from or NULL, the node
693 * you pass will not be searched, only the next one
694 * will; typically, you pass what the previous call
695 * returned. of_node_put() will be called on it
696 * @type: The type string to match "device_type" or NULL to ignore
697 * @compatible: The string to match to one of the tokens in the device
698 * "compatible" list.
699 *
700 * Returns a node pointer with refcount incremented, use
701 * of_node_put() on it when done.
702 */
703struct device_node *of_find_compatible_node(struct device_node *from,
704 const char *type, const char *compatible)
705{
706 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500707 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000708
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500709 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000710 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000711 for (; np; np = np->allnext) {
712 if (type
713 && !(np->type && (of_node_cmp(np->type, type) == 0)))
714 continue;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500715 if (__of_device_is_compatible(np, compatible) &&
716 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000717 break;
718 }
719 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500720 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000721 return np;
722}
723EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100724
725/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000726 * of_find_node_with_property - Find a node which has a property with
727 * the given name.
728 * @from: The node to start searching from or NULL, the node
729 * you pass will not be searched, only the next one
730 * will; typically, you pass what the previous call
731 * returned. of_node_put() will be called on it
732 * @prop_name: The name of the property to look for.
733 *
734 * Returns a node pointer with refcount incremented, use
735 * of_node_put() on it when done.
736 */
737struct device_node *of_find_node_with_property(struct device_node *from,
738 const char *prop_name)
739{
740 struct device_node *np;
741 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500742 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000743
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500744 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000745 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000746 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530747 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000748 if (of_prop_cmp(pp->name, prop_name) == 0) {
749 of_node_get(np);
750 goto out;
751 }
752 }
753 }
754out:
755 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500756 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000757 return np;
758}
759EXPORT_SYMBOL(of_find_node_with_property);
760
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500761static
762const struct of_device_id *__of_match_node(const struct of_device_id *matches,
763 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100764{
Grant Likelya52f07e2011-03-18 10:21:29 -0600765 if (!matches)
766 return NULL;
767
Grant Likely283029d2008-01-09 06:20:40 +1100768 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
769 int match = 1;
770 if (matches->name[0])
771 match &= node->name
772 && !strcmp(matches->name, node->name);
773 if (matches->type[0])
774 match &= node->type
775 && !strcmp(matches->type, node->type);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700776 if (matches->compatible[0])
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500777 match &= __of_device_is_compatible(node,
778 matches->compatible);
Linus Torvaldsbc51b0c2012-07-10 12:49:32 -0700779 if (match)
Grant Likely283029d2008-01-09 06:20:40 +1100780 return matches;
781 matches++;
782 }
783 return NULL;
784}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500785
786/**
787 * of_match_node - Tell if an device_node has a matching of_match structure
788 * @matches: array of of device match structures to search in
789 * @node: the of device structure to match against
790 *
791 * Low level utility function used by device matching.
792 */
793const struct of_device_id *of_match_node(const struct of_device_id *matches,
794 const struct device_node *node)
795{
796 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500797 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500798
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500799 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500800 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500801 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500802 return match;
803}
Grant Likely283029d2008-01-09 06:20:40 +1100804EXPORT_SYMBOL(of_match_node);
805
806/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700807 * of_find_matching_node_and_match - Find a node based on an of_device_id
808 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100809 * @from: The node to start searching from or NULL, the node
810 * you pass will not be searched, only the next one
811 * will; typically, you pass what the previous call
812 * returned. of_node_put() will be called on it
813 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700814 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100815 *
816 * Returns a node pointer with refcount incremented, use
817 * of_node_put() on it when done.
818 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700819struct device_node *of_find_matching_node_and_match(struct device_node *from,
820 const struct of_device_id *matches,
821 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100822{
823 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800824 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500825 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100826
Stephen Warren50c8af42012-11-20 16:12:20 -0700827 if (match)
828 *match = NULL;
829
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500830 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000831 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100832 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500833 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800834 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700835 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800836 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100837 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700838 }
Grant Likely283029d2008-01-09 06:20:40 +1100839 }
840 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500841 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100842 return np;
843}
Grant Likely80c20222012-12-19 10:45:36 +0000844EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400845
846/**
Grant Likely3f07af42008-07-25 22:25:13 -0400847 * of_modalias_node - Lookup appropriate modalias for a device node
848 * @node: pointer to a device tree node
849 * @modalias: Pointer to buffer that modalias value will be copied into
850 * @len: Length of modalias value
851 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600852 * Based on the value of the compatible property, this routine will attempt
853 * to choose an appropriate modalias value for a particular device tree node.
854 * It does this by stripping the manufacturer prefix (as delimited by a ',')
855 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400856 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600857 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400858 */
859int of_modalias_node(struct device_node *node, char *modalias, int len)
860{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600861 const char *compatible, *p;
862 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400863
864 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600865 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400866 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400867 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600868 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400869 return 0;
870}
871EXPORT_SYMBOL_GPL(of_modalias_node);
872
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000873/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700874 * of_find_node_by_phandle - Find a node given a phandle
875 * @handle: phandle of the node to find
876 *
877 * Returns a node pointer with refcount incremented, use
878 * of_node_put() on it when done.
879 */
880struct device_node *of_find_node_by_phandle(phandle handle)
881{
882 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000883 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -0700884
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000885 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000886 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700887 if (np->phandle == handle)
888 break;
889 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000890 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700891 return np;
892}
893EXPORT_SYMBOL(of_find_node_by_phandle);
894
895/**
Heiko Stuebner10408542014-02-12 01:00:34 +0100896 * of_property_count_elems_of_size - Count the number of elements in a property
897 *
898 * @np: device node from which the property value is to be read.
899 * @propname: name of the property to be searched.
900 * @elem_size: size of the individual element
901 *
902 * Search for a property in a device node and count the number of elements of
903 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
904 * property does not exist or its length does not match a multiple of elem_size
905 * and -ENODATA if the property does not have a value.
906 */
907int of_property_count_elems_of_size(const struct device_node *np,
908 const char *propname, int elem_size)
909{
910 struct property *prop = of_find_property(np, propname, NULL);
911
912 if (!prop)
913 return -EINVAL;
914 if (!prop->value)
915 return -ENODATA;
916
917 if (prop->length % elem_size != 0) {
918 pr_err("size of %s in node %s is not a multiple of %d\n",
919 propname, np->full_name, elem_size);
920 return -EINVAL;
921 }
922
923 return prop->length / elem_size;
924}
925EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
926
927/**
Tony Priskdaeec1f2013-04-03 17:57:11 +1300928 * of_find_property_value_of_size
929 *
930 * @np: device node from which the property value is to be read.
931 * @propname: name of the property to be searched.
932 * @len: requested length of property value
933 *
934 * Search for a property in a device node and valid the requested size.
935 * Returns the property value on success, -EINVAL if the property does not
936 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
937 * property data isn't large enough.
938 *
939 */
940static void *of_find_property_value_of_size(const struct device_node *np,
941 const char *propname, u32 len)
942{
943 struct property *prop = of_find_property(np, propname, NULL);
944
945 if (!prop)
946 return ERR_PTR(-EINVAL);
947 if (!prop->value)
948 return ERR_PTR(-ENODATA);
949 if (len > prop->length)
950 return ERR_PTR(-EOVERFLOW);
951
952 return prop->value;
953}
954
955/**
Tony Prisk3daf3722013-03-23 17:02:15 +1300956 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
957 *
958 * @np: device node from which the property value is to be read.
959 * @propname: name of the property to be searched.
960 * @index: index of the u32 in the list of values
961 * @out_value: pointer to return value, modified only if no error.
962 *
963 * Search for a property in a device node and read nth 32-bit value from
964 * it. Returns 0 on success, -EINVAL if the property does not exist,
965 * -ENODATA if property does not have a value, and -EOVERFLOW if the
966 * property data isn't large enough.
967 *
968 * The out_value is modified only if a valid u32 value can be decoded.
969 */
970int of_property_read_u32_index(const struct device_node *np,
971 const char *propname,
972 u32 index, u32 *out_value)
973{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300974 const u32 *val = of_find_property_value_of_size(np, propname,
975 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +1300976
Tony Priskdaeec1f2013-04-03 17:57:11 +1300977 if (IS_ERR(val))
978 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +1300979
Tony Priskdaeec1f2013-04-03 17:57:11 +1300980 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +1300981 return 0;
982}
983EXPORT_SYMBOL_GPL(of_property_read_u32_index);
984
985/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530986 * of_property_read_u8_array - Find and read an array of u8 from a property.
987 *
988 * @np: device node from which the property value is to be read.
989 * @propname: name of the property to be searched.
990 * @out_value: pointer to return value, modified only if return value is 0.
991 * @sz: number of array elements to read
992 *
993 * Search for a property in a device node and read 8-bit value(s) 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 * dts entry of array should be like:
999 * property = /bits/ 8 <0x50 0x60 0x70>;
1000 *
1001 * The out_value is modified only if a valid u8 value can be decoded.
1002 */
1003int of_property_read_u8_array(const struct device_node *np,
1004 const char *propname, u8 *out_values, size_t sz)
1005{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001006 const u8 *val = of_find_property_value_of_size(np, propname,
1007 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301008
Tony Priskdaeec1f2013-04-03 17:57:11 +13001009 if (IS_ERR(val))
1010 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301011
Viresh Kumarbe193242012-11-20 10:15:19 +05301012 while (sz--)
1013 *out_values++ = *val++;
1014 return 0;
1015}
1016EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1017
1018/**
1019 * of_property_read_u16_array - Find and read an array of u16 from a property.
1020 *
1021 * @np: device node from which the property value is to be read.
1022 * @propname: name of the property to be searched.
1023 * @out_value: pointer to return value, modified only if return value is 0.
1024 * @sz: number of array elements to read
1025 *
1026 * Search for a property in a device node and read 16-bit value(s) from
1027 * it. Returns 0 on success, -EINVAL if the property does not exist,
1028 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1029 * property data isn't large enough.
1030 *
1031 * dts entry of array should be like:
1032 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1033 *
1034 * The out_value is modified only if a valid u16 value can be decoded.
1035 */
1036int of_property_read_u16_array(const struct device_node *np,
1037 const char *propname, u16 *out_values, size_t sz)
1038{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001039 const __be16 *val = of_find_property_value_of_size(np, propname,
1040 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301041
Tony Priskdaeec1f2013-04-03 17:57:11 +13001042 if (IS_ERR(val))
1043 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301044
Viresh Kumarbe193242012-11-20 10:15:19 +05301045 while (sz--)
1046 *out_values++ = be16_to_cpup(val++);
1047 return 0;
1048}
1049EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1050
1051/**
Rob Herring0e373632011-07-06 15:42:58 -05001052 * of_property_read_u32_array - Find and read an array of 32 bit integers
1053 * from a property.
1054 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301055 * @np: device node from which the property value is to be read.
1056 * @propname: name of the property to be searched.
1057 * @out_value: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301058 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301059 *
Rob Herring0e373632011-07-06 15:42:58 -05001060 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301061 * it. Returns 0 on success, -EINVAL if the property does not exist,
1062 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1063 * property data isn't large enough.
1064 *
1065 * The out_value is modified only if a valid u32 value can be decoded.
1066 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001067int of_property_read_u32_array(const struct device_node *np,
1068 const char *propname, u32 *out_values,
1069 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301070{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001071 const __be32 *val = of_find_property_value_of_size(np, propname,
1072 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301073
Tony Priskdaeec1f2013-04-03 17:57:11 +13001074 if (IS_ERR(val))
1075 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001076
Rob Herring0e373632011-07-06 15:42:58 -05001077 while (sz--)
1078 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301079 return 0;
1080}
Rob Herring0e373632011-07-06 15:42:58 -05001081EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301082
1083/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001084 * of_property_read_u64 - Find and read a 64 bit integer from a property
1085 * @np: device node from which the property value is to be read.
1086 * @propname: name of the property to be searched.
1087 * @out_value: pointer to return value, modified only if return value is 0.
1088 *
1089 * Search for a property in a device node and read a 64-bit value from
1090 * it. Returns 0 on success, -EINVAL if the property does not exist,
1091 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1092 * property data isn't large enough.
1093 *
1094 * The out_value is modified only if a valid u64 value can be decoded.
1095 */
1096int of_property_read_u64(const struct device_node *np, const char *propname,
1097 u64 *out_value)
1098{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001099 const __be32 *val = of_find_property_value_of_size(np, propname,
1100 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001101
Tony Priskdaeec1f2013-04-03 17:57:11 +13001102 if (IS_ERR(val))
1103 return PTR_ERR(val);
1104
1105 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001106 return 0;
1107}
1108EXPORT_SYMBOL_GPL(of_property_read_u64);
1109
1110/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301111 * of_property_read_string - Find and read a string from a property
1112 * @np: device node from which the property value is to be read.
1113 * @propname: name of the property to be searched.
1114 * @out_string: pointer to null terminated return string, modified only if
1115 * return value is 0.
1116 *
1117 * Search for a property in a device tree node and retrieve a null
1118 * terminated string value (pointer to data, not a copy). Returns 0 on
1119 * success, -EINVAL if the property does not exist, -ENODATA if property
1120 * does not have a value, and -EILSEQ if the string is not null-terminated
1121 * within the length of the property data.
1122 *
1123 * The out_string pointer is modified only if a valid string can be decoded.
1124 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001125int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001126 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301127{
1128 struct property *prop = of_find_property(np, propname, NULL);
1129 if (!prop)
1130 return -EINVAL;
1131 if (!prop->value)
1132 return -ENODATA;
1133 if (strnlen(prop->value, prop->length) >= prop->length)
1134 return -EILSEQ;
1135 *out_string = prop->value;
1136 return 0;
1137}
1138EXPORT_SYMBOL_GPL(of_property_read_string);
1139
1140/**
Grant Likely7aff0fe2011-12-12 09:25:58 -07001141 * of_property_match_string() - Find string in a list and return index
1142 * @np: pointer to node containing string list property
1143 * @propname: string list property name
1144 * @string: pointer to string to search for in string list
1145 *
1146 * This function searches a string list property and returns the index
1147 * of a specific string value.
1148 */
1149int of_property_match_string(struct device_node *np, const char *propname,
1150 const char *string)
1151{
1152 struct property *prop = of_find_property(np, propname, NULL);
1153 size_t l;
1154 int i;
1155 const char *p, *end;
1156
1157 if (!prop)
1158 return -EINVAL;
1159 if (!prop->value)
1160 return -ENODATA;
1161
1162 p = prop->value;
1163 end = p + prop->length;
1164
1165 for (i = 0; p < end; i++, p += l) {
Grant Likely96db9732014-11-03 15:15:35 +00001166 l = strnlen(p, end - p) + 1;
Grant Likely7aff0fe2011-12-12 09:25:58 -07001167 if (p + l > end)
1168 return -EILSEQ;
1169 pr_debug("comparing %s with %s\n", string, p);
1170 if (strcmp(string, p) == 0)
1171 return i; /* Found it; return index */
1172 }
1173 return -ENODATA;
1174}
1175EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001176
1177/**
Grant Likely96db9732014-11-03 15:15:35 +00001178 * of_property_read_string_util() - Utility helper for parsing string properties
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001179 * @np: device node from which the property value is to be read.
1180 * @propname: name of the property to be searched.
Grant Likely96db9732014-11-03 15:15:35 +00001181 * @out_strs: output array of string pointers.
1182 * @sz: number of array elements to read.
1183 * @skip: Number of strings to skip over at beginning of list.
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001184 *
Grant Likely96db9732014-11-03 15:15:35 +00001185 * Don't call this function directly. It is a utility helper for the
1186 * of_property_read_string*() family of functions.
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001187 */
Grant Likely96db9732014-11-03 15:15:35 +00001188int of_property_read_string_helper(struct device_node *np, const char *propname,
1189 const char **out_strs, size_t sz, int skip)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001190{
1191 struct property *prop = of_find_property(np, propname, NULL);
Grant Likely96db9732014-11-03 15:15:35 +00001192 int l = 0, i = 0;
1193 const char *p, *end;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001194
1195 if (!prop)
1196 return -EINVAL;
1197 if (!prop->value)
1198 return -ENODATA;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001199 p = prop->value;
Grant Likely96db9732014-11-03 15:15:35 +00001200 end = p + prop->length;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001201
Grant Likely96db9732014-11-03 15:15:35 +00001202 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1203 l = strnlen(p, end - p) + 1;
1204 if (p + l > end)
1205 return -EILSEQ;
1206 if (out_strs && i >= skip)
1207 *out_strs++ = p;
1208 }
1209 i -= skip;
1210 return i <= 0 ? -ENODATA : i;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001211}
Grant Likely96db9732014-11-03 15:15:35 +00001212EXPORT_SYMBOL_GPL(of_property_read_string_helper);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001213
Grant Likelybd69f732013-02-10 22:57:21 +00001214static int __of_parse_phandle_with_args(const struct device_node *np,
1215 const char *list_name,
Stephen Warrenf35c0932013-08-14 15:27:10 -06001216 const char *cells_name,
1217 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001218 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001219{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001220 const __be32 *list, *list_end;
Grant Likely23ce04c02013-02-12 21:21:49 +00001221 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001222 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001223 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001224 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001225
Grant Likely15c9a0a2011-12-12 09:25:57 -07001226 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001227 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001228 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001229 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001230 list_end = list + size / sizeof(*list);
1231
Grant Likely15c9a0a2011-12-12 09:25:57 -07001232 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001233 while (list < list_end) {
Grant Likely23ce04c02013-02-12 21:21:49 +00001234 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001235 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001236
Grant Likely15c9a0a2011-12-12 09:25:57 -07001237 /*
1238 * If phandle is 0, then it is an empty entry with no
1239 * arguments. Skip forward to the next entry.
1240 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001241 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001242 if (phandle) {
1243 /*
1244 * Find the provider node and parse the #*-cells
Stephen Warren3a9560b2013-08-14 15:27:11 -06001245 * property to determine the argument length.
1246 *
1247 * This is not needed if the cell count is hard-coded
1248 * (i.e. cells_name not set, but cell_count is set),
1249 * except when we're going to return the found node
1250 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001251 */
Stephen Warren3a9560b2013-08-14 15:27:11 -06001252 if (cells_name || cur_index == index) {
1253 node = of_find_node_by_phandle(phandle);
1254 if (!node) {
1255 pr_err("%s: could not find phandle\n",
1256 np->full_name);
1257 goto err;
1258 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001259 }
Stephen Warrenf35c0932013-08-14 15:27:10 -06001260
1261 if (cells_name) {
1262 if (of_property_read_u32(node, cells_name,
1263 &count)) {
1264 pr_err("%s: could not get %s for %s\n",
1265 np->full_name, cells_name,
1266 node->full_name);
1267 goto err;
1268 }
1269 } else {
1270 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001271 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001272
Grant Likely15c9a0a2011-12-12 09:25:57 -07001273 /*
1274 * Make sure that the arguments actually fit in the
1275 * remaining property data length
1276 */
1277 if (list + count > list_end) {
1278 pr_err("%s: arguments longer than property\n",
1279 np->full_name);
Grant Likely23ce04c02013-02-12 21:21:49 +00001280 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001281 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001282 }
1283
Grant Likely15c9a0a2011-12-12 09:25:57 -07001284 /*
1285 * All of the error cases above bail out of the loop, so at
1286 * this point, the parsing is successful. If the requested
1287 * index matches, then fill the out_args structure and return,
1288 * or return -ENOENT for an empty entry.
1289 */
Grant Likely23ce04c02013-02-12 21:21:49 +00001290 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001291 if (cur_index == index) {
1292 if (!phandle)
Grant Likely23ce04c02013-02-12 21:21:49 +00001293 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001294
Grant Likely15c9a0a2011-12-12 09:25:57 -07001295 if (out_args) {
1296 int i;
1297 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1298 count = MAX_PHANDLE_ARGS;
1299 out_args->np = node;
1300 out_args->args_count = count;
1301 for (i = 0; i < count; i++)
1302 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001303 } else {
1304 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001305 }
Grant Likely23ce04c02013-02-12 21:21:49 +00001306
1307 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001308 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001309 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001310
1311 of_node_put(node);
1312 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001313 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001314 cur_index++;
1315 }
1316
Grant Likely23ce04c02013-02-12 21:21:49 +00001317 /*
1318 * Unlock node before returning result; will be one of:
1319 * -ENOENT : index is for empty phandle
1320 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001321 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c02013-02-12 21:21:49 +00001322 */
Grant Likelybd69f732013-02-10 22:57:21 +00001323 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c02013-02-12 21:21:49 +00001324 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001325 if (node)
1326 of_node_put(node);
Grant Likely23ce04c02013-02-12 21:21:49 +00001327 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001328}
Grant Likelybd69f732013-02-10 22:57:21 +00001329
Stephen Warrend2621342013-08-14 15:27:08 -06001330/**
Stephen Warren73ed9312013-08-14 15:27:09 -06001331 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1332 * @np: Pointer to device node holding phandle property
1333 * @phandle_name: Name of property holding a phandle value
1334 * @index: For properties holding a table of phandles, this is the index into
1335 * the table
1336 *
1337 * Returns the device_node pointer with refcount incremented. Use
1338 * of_node_put() on it when done.
1339 */
1340struct device_node *of_parse_phandle(const struct device_node *np,
1341 const char *phandle_name, int index)
1342{
Stephen Warren3a9560b2013-08-14 15:27:11 -06001343 struct of_phandle_args args;
Stephen Warren73ed9312013-08-14 15:27:09 -06001344
Stephen Warren3a9560b2013-08-14 15:27:11 -06001345 if (index < 0)
Stephen Warren73ed9312013-08-14 15:27:09 -06001346 return NULL;
1347
Stephen Warren3a9560b2013-08-14 15:27:11 -06001348 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1349 index, &args))
1350 return NULL;
1351
1352 return args.np;
Stephen Warren73ed9312013-08-14 15:27:09 -06001353}
1354EXPORT_SYMBOL(of_parse_phandle);
1355
1356/**
Stephen Warrend2621342013-08-14 15:27:08 -06001357 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1358 * @np: pointer to a device tree node containing a list
1359 * @list_name: property name that contains a list
1360 * @cells_name: property name that specifies phandles' arguments count
1361 * @index: index of a phandle to parse out
1362 * @out_args: optional pointer to output arguments structure (will be filled)
1363 *
1364 * This function is useful to parse lists of phandles and their arguments.
1365 * Returns 0 on success and fills out_args, on error returns appropriate
1366 * errno value.
1367 *
1368 * Caller is responsible to call of_node_put() on the returned out_args->node
1369 * pointer.
1370 *
1371 * Example:
1372 *
1373 * phandle1: node1 {
1374 * #list-cells = <2>;
1375 * }
1376 *
1377 * phandle2: node2 {
1378 * #list-cells = <1>;
1379 * }
1380 *
1381 * node3 {
1382 * list = <&phandle1 1 2 &phandle2 3>;
1383 * }
1384 *
1385 * To get a device_node of the `node2' node you may call this:
1386 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1387 */
Grant Likelybd69f732013-02-10 22:57:21 +00001388int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1389 const char *cells_name, int index,
1390 struct of_phandle_args *out_args)
1391{
1392 if (index < 0)
1393 return -EINVAL;
Stephen Warrenf35c0932013-08-14 15:27:10 -06001394 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1395 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001396}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001397EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001398
Grant Likelybd69f732013-02-10 22:57:21 +00001399/**
Stephen Warrenf35c0932013-08-14 15:27:10 -06001400 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1401 * @np: pointer to a device tree node containing a list
1402 * @list_name: property name that contains a list
1403 * @cell_count: number of argument cells following the phandle
1404 * @index: index of a phandle to parse out
1405 * @out_args: optional pointer to output arguments structure (will be filled)
1406 *
1407 * This function is useful to parse lists of phandles and their arguments.
1408 * Returns 0 on success and fills out_args, on error returns appropriate
1409 * errno value.
1410 *
1411 * Caller is responsible to call of_node_put() on the returned out_args->node
1412 * pointer.
1413 *
1414 * Example:
1415 *
1416 * phandle1: node1 {
1417 * }
1418 *
1419 * phandle2: node2 {
1420 * }
1421 *
1422 * node3 {
1423 * list = <&phandle1 0 2 &phandle2 2 3>;
1424 * }
1425 *
1426 * To get a device_node of the `node2' node you may call this:
1427 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1428 */
1429int of_parse_phandle_with_fixed_args(const struct device_node *np,
1430 const char *list_name, int cell_count,
1431 int index, struct of_phandle_args *out_args)
1432{
1433 if (index < 0)
1434 return -EINVAL;
1435 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1436 index, out_args);
1437}
1438EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1439
1440/**
Grant Likelybd69f732013-02-10 22:57:21 +00001441 * of_count_phandle_with_args() - Find the number of phandles references in a property
1442 * @np: pointer to a device tree node containing a list
1443 * @list_name: property name that contains a list
1444 * @cells_name: property name that specifies phandles' arguments count
1445 *
1446 * Returns the number of phandle + argument tuples within a property. It
1447 * is a typical pattern to encode a list of phandle and variable
1448 * arguments into a single property. The number of arguments is encoded
1449 * by a property in the phandle-target node. For example, a gpios
1450 * property would contain a list of GPIO specifies consisting of a
1451 * phandle and 1 or more arguments. The number of arguments are
1452 * determined by the #gpio-cells property in the node pointed to by the
1453 * phandle.
1454 */
1455int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1456 const char *cells_name)
1457{
Stephen Warrenf35c0932013-08-14 15:27:10 -06001458 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1459 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001460}
1461EXPORT_SYMBOL(of_count_phandle_with_args);
1462
Grant Likely02af11b2009-11-23 20:16:45 -07001463/**
Xiubo Li24648be2014-01-22 13:57:39 +08001464 * __of_add_property - Add a property to a node without lock operations
1465 */
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001466int __of_add_property(struct device_node *np, struct property *prop)
Stephen Rothwell97e873e2007-05-01 16:26:07 +10001467{
Xiubo Li24648be2014-01-22 13:57:39 +08001468 struct property **next;
Stephen Rothwell97e873e2007-05-01 16:26:07 +10001469
Xiubo Li24648be2014-01-22 13:57:39 +08001470 prop->next = NULL;
1471 next = &np->properties;
1472 while (*next) {
1473 if (strcmp(prop->name, (*next)->name) == 0)
1474 /* duplicate ! don't insert it */
1475 return -EEXIST;
1476
1477 next = &(*next)->next;
1478 }
1479 *next = prop;
1480
Grant Likely02af11b2009-11-23 20:16:45 -07001481 return 0;
1482}
Grant Likely02af11b2009-11-23 20:16:45 -07001483
1484/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001485 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001486 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001487int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001488{
Grant Likely02af11b2009-11-23 20:16:45 -07001489 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001490 int rc;
1491
Grant Likely952f9f42014-07-23 17:05:06 -06001492 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001493
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001494 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li24648be2014-01-22 13:57:39 +08001495 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001496 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001497
Xiubo Li24648be2014-01-22 13:57:39 +08001498 if (!rc)
Pantelis Antonioub74e9e12013-12-13 20:08:59 +02001499 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001500
Grant Likely952f9f42014-07-23 17:05:06 -06001501 mutex_unlock(&of_mutex);
1502
Grant Likely3e262052014-07-16 12:48:23 -06001503 if (!rc)
1504 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1505
Xiubo Li24648be2014-01-22 13:57:39 +08001506 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001507}
1508
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001509int __of_remove_property(struct device_node *np, struct property *prop)
1510{
1511 struct property **next;
1512
1513 for (next = &np->properties; *next; next = &(*next)->next) {
1514 if (*next == prop)
1515 break;
1516 }
1517 if (*next == NULL)
1518 return -ENODEV;
1519
1520 /* found the node */
1521 *next = prop->next;
1522 prop->next = np->deadprops;
1523 np->deadprops = prop;
Grant Likely02af11b2009-11-23 20:16:45 -07001524
1525 return 0;
1526}
1527
Grant Likely952f9f42014-07-23 17:05:06 -06001528void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1529{
1530 /* at early boot, bail here and defer setup to of_init() */
1531 if (of_kset && of_node_is_attached(np))
1532 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1533}
1534
Grant Likely02af11b2009-11-23 20:16:45 -07001535/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001536 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001537 *
1538 * Note that we don't actually remove it, since we have given out
1539 * who-knows-how-many pointers to the data using get-property.
1540 * Instead we just move the property to the "dead properties"
1541 * list, so it won't be found any more.
1542 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001543int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001544{
Grant Likely02af11b2009-11-23 20:16:45 -07001545 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001546 int rc;
1547
Grant Likely952f9f42014-07-23 17:05:06 -06001548 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001549
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001550 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001551 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001552 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001553
Grant Likely952f9f42014-07-23 17:05:06 -06001554 if (!rc)
1555 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001556
Grant Likely952f9f42014-07-23 17:05:06 -06001557 mutex_unlock(&of_mutex);
Grant Likelyf57a8e22014-02-20 18:02:11 +00001558
Grant Likely3e262052014-07-16 12:48:23 -06001559 if (!rc)
1560 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1561
Grant Likely952f9f42014-07-23 17:05:06 -06001562 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001563}
1564
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001565int __of_update_property(struct device_node *np, struct property *newprop,
1566 struct property **oldpropp)
1567{
1568 struct property **next, *oldprop;
1569
1570 for (next = &np->properties; *next; next = &(*next)->next) {
1571 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1572 break;
1573 }
1574 *oldpropp = oldprop = *next;
1575
1576 if (oldprop) {
1577 /* replace the node */
1578 newprop->next = oldprop->next;
1579 *next = newprop;
1580 oldprop->next = np->deadprops;
1581 np->deadprops = oldprop;
1582 } else {
1583 /* new node */
1584 newprop->next = NULL;
1585 *next = newprop;
1586 }
Grant Likely02af11b2009-11-23 20:16:45 -07001587
1588 return 0;
1589}
1590
Grant Likely952f9f42014-07-23 17:05:06 -06001591void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1592 struct property *oldprop)
1593{
1594 /* At early boot, bail out and defer setup to of_init() */
1595 if (!of_kset)
1596 return;
1597
1598 if (oldprop)
1599 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1600 __of_add_property_sysfs(np, newprop);
1601}
1602
Grant Likely02af11b2009-11-23 20:16:45 -07001603/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001604 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001605 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001606 *
1607 * Note that we don't actually remove it, since we have given out
1608 * who-knows-how-many pointers to the data using get-property.
1609 * Instead we just move the property to the "dead properties" list,
1610 * and add the new property to the property list
1611 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001612int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001613{
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001614 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001615 unsigned long flags;
Grant Likely952f9f42014-07-23 17:05:06 -06001616 int rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001617
Dong Aisheng475d0092012-07-11 15:16:37 +10001618 if (!newprop->name)
1619 return -EINVAL;
1620
Grant Likely952f9f42014-07-23 17:05:06 -06001621 mutex_lock(&of_mutex);
Dong Aisheng475d0092012-07-11 15:16:37 +10001622
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001623 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antoniou2189e1a2014-07-04 19:58:46 +03001624 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001625 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001626
Grant Likely952f9f42014-07-23 17:05:06 -06001627 if (!rc)
1628 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likely02af11b2009-11-23 20:16:45 -07001629
Grant Likely952f9f42014-07-23 17:05:06 -06001630 mutex_unlock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001631
Grant Likely3e262052014-07-16 12:48:23 -06001632 if (!rc)
1633 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001634
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001635 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001636}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001637
Shawn Guo611cad72011-08-15 15:28:14 +08001638static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1639 int id, const char *stem, int stem_len)
1640{
1641 ap->np = np;
1642 ap->id = id;
1643 strncpy(ap->stem, stem, stem_len);
1644 ap->stem[stem_len] = 0;
1645 list_add_tail(&ap->link, &aliases_lookup);
1646 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001647 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001648}
1649
1650/**
1651 * of_alias_scan - Scan all properties of 'aliases' node
1652 *
1653 * The function scans all the properties of 'aliases' node and populate
1654 * the the global lookup table with the properties. It returns the
1655 * number of alias_prop found, or error code in error case.
1656 *
1657 * @dt_alloc: An allocator that provides a virtual address to memory
1658 * for the resulting tree
1659 */
1660void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1661{
1662 struct property *pp;
1663
1664 of_chosen = of_find_node_by_path("/chosen");
1665 if (of_chosen == NULL)
1666 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer88a81042013-08-05 14:40:44 +02001667
1668 if (of_chosen) {
1669 const char *name;
1670
1671 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1672 if (name)
1673 of_stdout = of_find_node_by_path(name);
1674 }
1675
Shawn Guo611cad72011-08-15 15:28:14 +08001676 of_aliases = of_find_node_by_path("/aliases");
1677 if (!of_aliases)
1678 return;
1679
Dong Aisheng8af0da92011-12-22 20:19:24 +08001680 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001681 const char *start = pp->name;
1682 const char *end = start + strlen(start);
1683 struct device_node *np;
1684 struct alias_prop *ap;
1685 int id, len;
1686
1687 /* Skip those we do not want to proceed */
1688 if (!strcmp(pp->name, "name") ||
1689 !strcmp(pp->name, "phandle") ||
1690 !strcmp(pp->name, "linux,phandle"))
1691 continue;
1692
1693 np = of_find_node_by_path(pp->value);
1694 if (!np)
1695 continue;
1696
1697 /* walk the alias backwards to extract the id and work out
1698 * the 'stem' string */
1699 while (isdigit(*(end-1)) && end > start)
1700 end--;
1701 len = end - start;
1702
1703 if (kstrtoint(end, 10, &id) < 0)
1704 continue;
1705
1706 /* Allocate an alias_prop with enough space for the stem */
1707 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1708 if (!ap)
1709 continue;
Grant Likely6830e9a2013-08-28 21:24:17 +01001710 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001711 ap->alias = start;
1712 of_alias_add(ap, np, id, start, len);
1713 }
1714}
1715
1716/**
1717 * of_alias_get_id - Get alias id for the given device_node
1718 * @np: Pointer to the given device_node
1719 * @stem: Alias stem of the given device_node
1720 *
1721 * The function travels the lookup table to get alias id for the given
1722 * device_node and alias stem. It returns the alias id if find it.
1723 */
1724int of_alias_get_id(struct device_node *np, const char *stem)
1725{
1726 struct alias_prop *app;
1727 int id = -ENODEV;
1728
Pantelis Antoniou59cf2512014-07-04 19:58:03 +03001729 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001730 list_for_each_entry(app, &aliases_lookup, link) {
1731 if (strcmp(app->stem, stem) != 0)
1732 continue;
1733
1734 if (np == app->np) {
1735 id = app->id;
1736 break;
1737 }
1738 }
Pantelis Antoniou59cf2512014-07-04 19:58:03 +03001739 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001740
1741 return id;
1742}
1743EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001744
1745const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1746 u32 *pu)
1747{
1748 const void *curv = cur;
1749
1750 if (!prop)
1751 return NULL;
1752
1753 if (!cur) {
1754 curv = prop->value;
1755 goto out_val;
1756 }
1757
1758 curv += sizeof(*cur);
1759 if (curv >= prop->value + prop->length)
1760 return NULL;
1761
1762out_val:
1763 *pu = be32_to_cpup(curv);
1764 return curv;
1765}
1766EXPORT_SYMBOL_GPL(of_prop_next_u32);
1767
1768const char *of_prop_next_string(struct property *prop, const char *cur)
1769{
1770 const void *curv = cur;
1771
1772 if (!prop)
1773 return NULL;
1774
1775 if (!cur)
1776 return prop->value;
1777
1778 curv += strlen(cur) + 1;
1779 if (curv >= prop->value + prop->length)
1780 return NULL;
1781
1782 return curv;
1783}
1784EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer88a81042013-08-05 14:40:44 +02001785
1786/**
1787 * of_device_is_stdout_path - check if a device node matches the
1788 * linux,stdout-path property
1789 *
1790 * Check if this device node matches the linux,stdout-path property
1791 * in the chosen node. return true if yes, false otherwise.
1792 */
1793int of_device_is_stdout_path(struct device_node *dn)
1794{
1795 if (!of_stdout)
1796 return false;
1797
1798 return of_stdout == dn;
1799}
1800EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
Mark Brown2156eda2015-01-09 18:22:45 +00001801
1802/**
Mathieu Poirierf7460932014-10-08 13:09:30 -06001803 * of_graph_parse_endpoint() - parse common endpoint node properties
1804 * @node: pointer to endpoint device_node
1805 * @endpoint: pointer to the OF endpoint data structure
1806 *
1807 * The caller should hold a reference to @node.
1808 */
1809int of_graph_parse_endpoint(const struct device_node *node,
1810 struct of_endpoint *endpoint)
1811{
1812 struct device_node *port_node = of_get_parent(node);
1813
1814 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
1815 __func__, node->full_name);
1816
1817 memset(endpoint, 0, sizeof(*endpoint));
1818
1819 endpoint->local_node = node;
1820 /*
1821 * It doesn't matter whether the two calls below succeed.
1822 * If they don't then the default value 0 is used.
1823 */
1824 of_property_read_u32(port_node, "reg", &endpoint->port);
1825 of_property_read_u32(node, "reg", &endpoint->id);
1826
1827 of_node_put(port_node);
1828
1829 return 0;
1830}
1831EXPORT_SYMBOL(of_graph_parse_endpoint);
1832
1833/**
Mathieu Poirier2990c832014-10-08 12:02:38 -06001834 * of_graph_get_next_endpoint() - get next endpoint node
1835 * @parent: pointer to the parent device node
1836 * @prev: previous endpoint node, or NULL to get first
1837 *
1838 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
1839 * of the passed @prev node is not decremented, the caller have to use
1840 * of_node_put() on it when done.
1841 */
1842struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
1843 struct device_node *prev)
1844{
1845 struct device_node *endpoint;
1846 struct device_node *port;
1847
1848 if (!parent)
1849 return NULL;
1850
1851 /*
1852 * Start by locating the port node. If no previous endpoint is specified
1853 * search for the first port node, otherwise get the previous endpoint
1854 * parent port node.
1855 */
1856 if (!prev) {
1857 struct device_node *node;
1858
1859 node = of_get_child_by_name(parent, "ports");
1860 if (node)
1861 parent = node;
1862
1863 port = of_get_child_by_name(parent, "port");
1864 of_node_put(node);
1865
1866 if (!port) {
1867 pr_err("%s(): no port node found in %s\n",
1868 __func__, parent->full_name);
1869 return NULL;
1870 }
1871 } else {
1872 port = of_get_parent(prev);
1873 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
1874 __func__, prev->full_name))
1875 return NULL;
1876
1877 /*
1878 * Avoid dropping prev node refcount to 0 when getting the next
1879 * child below.
1880 */
1881 of_node_get(prev);
1882 }
1883
1884 while (1) {
1885 /*
1886 * Now that we have a port node, get the next endpoint by
1887 * getting the next child. If the previous endpoint is NULL this
1888 * will return the first child.
1889 */
1890 endpoint = of_get_next_child(port, prev);
1891 if (endpoint) {
1892 of_node_put(port);
1893 return endpoint;
1894 }
1895
1896 /* No more endpoints under this port, try the next one. */
1897 prev = NULL;
1898
1899 do {
1900 port = of_get_next_child(parent, port);
1901 if (!port)
1902 return NULL;
1903 } while (of_node_cmp(port->name, "port"));
1904 }
1905}
1906EXPORT_SYMBOL(of_graph_get_next_endpoint);
1907
1908/**
1909 * of_graph_get_remote_port_parent() - get remote port's parent node
1910 * @node: pointer to a local endpoint device_node
1911 *
1912 * Return: Remote device node associated with remote endpoint node linked
1913 * to @node. Use of_node_put() on it when done.
1914 */
1915struct device_node *of_graph_get_remote_port_parent(
1916 const struct device_node *node)
1917{
1918 struct device_node *np;
1919 unsigned int depth;
1920
1921 /* Get remote endpoint node. */
1922 np = of_parse_phandle(node, "remote-endpoint", 0);
1923
1924 /* Walk 3 levels up only if there is 'ports' node. */
1925 for (depth = 3; depth && np; depth--) {
1926 np = of_get_next_parent(np);
1927 if (depth == 2 && of_node_cmp(np->name, "ports"))
1928 break;
1929 }
1930 return np;
1931}
1932EXPORT_SYMBOL(of_graph_get_remote_port_parent);
1933
1934/**
1935 * of_graph_get_remote_port() - get remote port node
1936 * @node: pointer to a local endpoint device_node
1937 *
1938 * Return: Remote port node associated with remote endpoint node linked
1939 * to @node. Use of_node_put() on it when done.
1940 */
1941struct device_node *of_graph_get_remote_port(const struct device_node *node)
1942{
1943 struct device_node *np;
1944
1945 /* Get remote endpoint node. */
1946 np = of_parse_phandle(node, "remote-endpoint", 0);
1947 if (!np)
1948 return NULL;
1949 return of_get_next_parent(np);
1950}
1951EXPORT_SYMBOL(of_graph_get_remote_port);