blob: df9b2bb7bb272afd86760a738a7c59d54761a3b1 [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 */
Grant Likely3482f2c2014-03-27 17:18:55 -070020#include <linux/console.h>
Shawn Guo611cad72011-08-15 15:28:14 +080021#include <linux/ctype.h>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010022#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100023#include <linux/module.h>
24#include <linux/of.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010025#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100026#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000028#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070029#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100030
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080031#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080032
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080033LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080034
Randy Dunlap465aac62012-11-30 10:01:51 +000035struct device_node *of_allnodes;
36EXPORT_SYMBOL(of_allnodes);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070037struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080038struct device_node *of_aliases;
Sascha Hauer5c19e952013-08-05 14:40:44 +020039static struct device_node *of_stdout;
Shawn Guo611cad72011-08-15 15:28:14 +080040
Grant Likely75b57ec2014-02-20 18:02:11 +000041static struct kset *of_kset;
42
43/*
44 * Used to protect the of_aliases; but also overloaded to hold off addition of
45 * nodes to sysfs
46 */
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080047DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100048
Stephen Rothwell581b6052007-04-24 16:46:53 +100049/* use when traversing tree through the allnext, child, sibling,
50 * or parent members of struct device_node.
51 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050052DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100053
54int of_n_addr_cells(struct device_node *np)
55{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060056 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100057
58 do {
59 if (np->parent)
60 np = np->parent;
61 ip = of_get_property(np, "#address-cells", NULL);
62 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070063 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100064 } while (np->parent);
65 /* No #address-cells property for the root node */
66 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
67}
68EXPORT_SYMBOL(of_n_addr_cells);
69
70int of_n_size_cells(struct device_node *np)
71{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060072 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100073
74 do {
75 if (np->parent)
76 np = np->parent;
77 ip = of_get_property(np, "#size-cells", NULL);
78 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070079 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100080 } while (np->parent);
81 /* No #size-cells property for the root node */
82 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
83}
84EXPORT_SYMBOL(of_n_size_cells);
85
Rob Herring0c3f0612013-09-17 10:42:50 -050086#ifdef CONFIG_NUMA
87int __weak of_node_to_nid(struct device_node *np)
88{
89 return numa_node_id();
90}
91#endif
92
Grant Likely0f22dd32012-02-15 20:38:40 -070093#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070094/**
95 * of_node_get - Increment refcount of a node
96 * @node: Node to inc refcount, NULL is supported to
97 * simplify writing of callers
98 *
99 * Returns node.
100 */
101struct device_node *of_node_get(struct device_node *node)
102{
103 if (node)
Grant Likely75b57ec2014-02-20 18:02:11 +0000104 kobject_get(&node->kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700105 return node;
106}
107EXPORT_SYMBOL(of_node_get);
108
Grant Likely75b57ec2014-02-20 18:02:11 +0000109static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -0700110{
Grant Likely75b57ec2014-02-20 18:02:11 +0000111 return container_of(kobj, struct device_node, kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700112}
113
114/**
115 * of_node_release - release a dynamically allocated node
116 * @kref: kref element of the node to be released
117 *
118 * In of_node_put() this function is passed to kref_put()
119 * as the destructor.
120 */
Grant Likely75b57ec2014-02-20 18:02:11 +0000121static void of_node_release(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -0700122{
Grant Likely75b57ec2014-02-20 18:02:11 +0000123 struct device_node *node = kobj_to_device_node(kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700124 struct property *prop = node->properties;
125
126 /* We should never be releasing nodes that haven't been detached. */
127 if (!of_node_check_flag(node, OF_DETACHED)) {
128 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
129 dump_stack();
Grant Likely923f7e32010-01-28 13:52:53 -0700130 return;
131 }
132
133 if (!of_node_check_flag(node, OF_DYNAMIC))
134 return;
135
136 while (prop) {
137 struct property *next = prop->next;
138 kfree(prop->name);
139 kfree(prop->value);
140 kfree(prop);
141 prop = next;
142
143 if (!prop) {
144 prop = node->deadprops;
145 node->deadprops = NULL;
146 }
147 }
148 kfree(node->full_name);
149 kfree(node->data);
150 kfree(node);
151}
152
153/**
154 * of_node_put - Decrement refcount of a node
155 * @node: Node to dec refcount, NULL is supported to
156 * simplify writing of callers
157 *
158 */
159void of_node_put(struct device_node *node)
160{
161 if (node)
Grant Likely75b57ec2014-02-20 18:02:11 +0000162 kobject_put(&node->kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700163}
164EXPORT_SYMBOL(of_node_put);
Grant Likely75b57ec2014-02-20 18:02:11 +0000165#else
166static void of_node_release(struct kobject *kobj)
167{
168 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
169}
Grant Likely0f22dd32012-02-15 20:38:40 -0700170#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700171
Grant Likely75b57ec2014-02-20 18:02:11 +0000172struct kobj_type of_node_ktype = {
173 .release = of_node_release,
174};
175
176static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
177 struct bin_attribute *bin_attr, char *buf,
178 loff_t offset, size_t count)
179{
180 struct property *pp = container_of(bin_attr, struct property, attr);
181 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
182}
183
184static const char *safe_name(struct kobject *kobj, const char *orig_name)
185{
186 const char *name = orig_name;
187 struct kernfs_node *kn;
188 int i = 0;
189
190 /* don't be a hero. After 16 tries give up */
191 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
192 sysfs_put(kn);
193 if (name != orig_name)
194 kfree(name);
195 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
196 }
197
198 if (name != orig_name)
199 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
200 kobject_name(kobj), name);
201 return name;
202}
203
204static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
205{
206 int rc;
207
208 /* Important: Don't leak passwords */
209 bool secure = strncmp(pp->name, "security-", 9) == 0;
210
211 sysfs_bin_attr_init(&pp->attr);
212 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
213 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
214 pp->attr.size = secure ? 0 : pp->length;
215 pp->attr.read = of_node_property_read;
216
217 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
218 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
219 return rc;
220}
221
222static int __of_node_add(struct device_node *np)
223{
224 const char *name;
225 struct property *pp;
226 int rc;
227
228 np->kobj.kset = of_kset;
229 if (!np->parent) {
230 /* Nodes without parents are new top level trees */
Kees Cook28d3ee42014-06-10 09:57:00 -0700231 rc = kobject_add(&np->kobj, NULL, "%s",
232 safe_name(&of_kset->kobj, "base"));
Grant Likely75b57ec2014-02-20 18:02:11 +0000233 } else {
234 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
235 if (!name || !name[0])
236 return -EINVAL;
237
238 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
239 }
240 if (rc)
241 return rc;
242
243 for_each_property_of_node(np, pp)
244 __of_add_property_sysfs(np, pp);
245
246 return 0;
247}
248
249int of_node_add(struct device_node *np)
250{
251 int rc = 0;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200252
253 BUG_ON(!of_node_is_initialized(np));
254
255 /*
256 * Grab the mutex here so that in a race condition between of_init() and
257 * of_node_add(), node addition will still be consistent.
258 */
Grant Likely75b57ec2014-02-20 18:02:11 +0000259 mutex_lock(&of_aliases_mutex);
260 if (of_kset)
261 rc = __of_node_add(np);
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200262 else
263 /* This scenario may be perfectly valid, but report it anyway */
264 pr_info("of_node_add(%s) before of_init()\n", np->full_name);
Grant Likely75b57ec2014-02-20 18:02:11 +0000265 mutex_unlock(&of_aliases_mutex);
266 return rc;
267}
268
269#if defined(CONFIG_OF_DYNAMIC)
270static void of_node_remove(struct device_node *np)
271{
272 struct property *pp;
273
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200274 BUG_ON(!of_node_is_initialized(np));
Grant Likely75b57ec2014-02-20 18:02:11 +0000275
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200276 /* only remove properties if on sysfs */
277 if (of_node_is_attached(np)) {
278 for_each_property_of_node(np, pp)
279 sysfs_remove_bin_file(&np->kobj, &pp->attr);
280 kobject_del(&np->kobj);
281 }
282
283 /* finally remove the kobj_init ref */
284 of_node_put(np);
Grant Likely75b57ec2014-02-20 18:02:11 +0000285}
286#endif
287
288static int __init of_init(void)
289{
290 struct device_node *np;
291
292 /* Create the kset, and register existing nodes */
293 mutex_lock(&of_aliases_mutex);
294 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
295 if (!of_kset) {
296 mutex_unlock(&of_aliases_mutex);
297 return -ENOMEM;
298 }
299 for_each_of_allnodes(np)
300 __of_node_add(np);
301 mutex_unlock(&of_aliases_mutex);
302
Grant Likely83570412012-11-06 21:03:27 +0000303 /* Symlink in /proc as required by userspace ABI */
Grant Likely75b57ec2014-02-20 18:02:11 +0000304 if (of_allnodes)
305 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000306
307 return 0;
308}
309core_initcall(of_init);
310
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500311static struct property *__of_find_property(const struct device_node *np,
312 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000313{
314 struct property *pp;
315
Timur Tabi64e45662008-05-08 05:19:59 +1000316 if (!np)
317 return NULL;
318
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530319 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000320 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530321 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000322 *lenp = pp->length;
323 break;
324 }
325 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500326
327 return pp;
328}
329
330struct property *of_find_property(const struct device_node *np,
331 const char *name,
332 int *lenp)
333{
334 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500335 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500336
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500337 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500338 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500339 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000340
341 return pp;
342}
343EXPORT_SYMBOL(of_find_property);
344
Grant Likelye91edcf2009-10-15 10:58:09 -0600345/**
346 * of_find_all_nodes - Get next node in global list
347 * @prev: Previous node or NULL to start iteration
348 * of_node_put() will be called on it
349 *
350 * Returns a node pointer with refcount incremented, use
351 * of_node_put() on it when done.
352 */
353struct device_node *of_find_all_nodes(struct device_node *prev)
354{
355 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000356 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600357
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000358 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000359 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600360 for (; np != NULL; np = np->allnext)
361 if (of_node_get(np))
362 break;
363 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000364 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600365 return np;
366}
367EXPORT_SYMBOL(of_find_all_nodes);
368
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000369/*
370 * Find a property with a given name for a given node
371 * and return the value.
372 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500373static const void *__of_get_property(const struct device_node *np,
374 const char *name, int *lenp)
375{
376 struct property *pp = __of_find_property(np, name, lenp);
377
378 return pp ? pp->value : NULL;
379}
380
381/*
382 * Find a property with a given name for a given node
383 * and return the value.
384 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000385const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500386 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000387{
388 struct property *pp = of_find_property(np, name, lenp);
389
390 return pp ? pp->value : NULL;
391}
392EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000393
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100394/*
395 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
396 *
397 * @cpu: logical cpu index of a core/thread
398 * @phys_id: physical identifier of a core/thread
399 *
400 * CPU logical to physical index mapping is architecture specific.
401 * However this __weak function provides a default match of physical
402 * id to logical cpu index. phys_id provided here is usually values read
403 * from the device tree which must match the hardware internal registers.
404 *
405 * Returns true if the physical identifier and the logical cpu index
406 * correspond to the same core/thread, false otherwise.
407 */
408bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
409{
410 return (u32)phys_id == cpu;
411}
412
413/**
414 * Checks if the given "prop_name" property holds the physical id of the
415 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
416 * NULL, local thread number within the core is returned in it.
417 */
418static bool __of_find_n_match_cpu_property(struct device_node *cpun,
419 const char *prop_name, int cpu, unsigned int *thread)
420{
421 const __be32 *cell;
422 int ac, prop_len, tid;
423 u64 hwid;
424
425 ac = of_n_addr_cells(cpun);
426 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100427 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100428 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100429 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100430 for (tid = 0; tid < prop_len; tid++) {
431 hwid = of_read_number(cell, ac);
432 if (arch_match_cpu_phys_id(cpu, hwid)) {
433 if (thread)
434 *thread = tid;
435 return true;
436 }
437 cell += ac;
438 }
439 return false;
440}
441
David Millerd1cb9d12013-10-03 17:24:51 -0400442/*
443 * arch_find_n_match_cpu_physical_id - See if the given device node is
444 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
445 * else false. If 'thread' is non-NULL, the local thread number within the
446 * core is returned in it.
447 */
448bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
449 int cpu, unsigned int *thread)
450{
451 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
452 * for thread ids on PowerPC. If it doesn't exist fallback to
453 * standard "reg" property.
454 */
455 if (IS_ENABLED(CONFIG_PPC) &&
456 __of_find_n_match_cpu_property(cpun,
457 "ibm,ppc-interrupt-server#s",
458 cpu, thread))
459 return true;
460
461 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
462 return true;
463
464 return false;
465}
466
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100467/**
468 * of_get_cpu_node - Get device node associated with the given logical CPU
469 *
470 * @cpu: CPU number(logical index) for which device node is required
471 * @thread: if not NULL, local thread number within the physical core is
472 * returned
473 *
474 * The main purpose of this function is to retrieve the device node for the
475 * given logical CPU index. It should be used to initialize the of_node in
476 * cpu device. Once of_node in cpu device is populated, all the further
477 * references can use that instead.
478 *
479 * CPU logical to physical index mapping is architecture specific and is built
480 * before booting secondary cores. This function uses arch_match_cpu_phys_id
481 * which can be overridden by architecture specific implementation.
482 *
483 * Returns a node pointer for the logical cpu if found, else NULL.
484 */
485struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
486{
David Millerd1cb9d12013-10-03 17:24:51 -0400487 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100488
David Millerd1cb9d12013-10-03 17:24:51 -0400489 for_each_node_by_type(cpun, "cpu") {
490 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100491 return cpun;
492 }
493 return NULL;
494}
495EXPORT_SYMBOL(of_get_cpu_node);
496
Kevin Hao215a14c2014-02-19 16:15:45 +0800497/**
498 * __of_device_is_compatible() - Check if the node matches given constraints
499 * @device: pointer to node
500 * @compat: required compatible string, NULL or "" for any match
501 * @type: required device_type value, NULL or "" for any match
502 * @name: required node name, NULL or "" for any match
503 *
504 * Checks if the given @compat, @type and @name strings match the
505 * properties of the given @device. A constraints can be skipped by
506 * passing NULL or an empty string as the constraint.
507 *
508 * Returns 0 for no match, and a positive integer on match. The return
509 * value is a relative score with larger values indicating better
510 * matches. The score is weighted for the most specific compatible value
511 * to get the highest score. Matching type is next, followed by matching
512 * name. Practically speaking, this results in the following priority
513 * order for matches:
514 *
515 * 1. specific compatible && type && name
516 * 2. specific compatible && type
517 * 3. specific compatible && name
518 * 4. specific compatible
519 * 5. general compatible && type && name
520 * 6. general compatible && type
521 * 7. general compatible && name
522 * 8. general compatible
523 * 9. type && name
524 * 10. type
525 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000526 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500527static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800528 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000529{
Kevin Hao215a14c2014-02-19 16:15:45 +0800530 struct property *prop;
531 const char *cp;
532 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000533
Kevin Hao215a14c2014-02-19 16:15:45 +0800534 /* Compatible match has highest priority */
535 if (compat && compat[0]) {
536 prop = __of_find_property(device, "compatible", NULL);
537 for (cp = of_prop_next_string(prop, NULL); cp;
538 cp = of_prop_next_string(prop, cp), index++) {
539 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
540 score = INT_MAX/2 - (index << 2);
541 break;
542 }
543 }
544 if (!score)
545 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000546 }
547
Kevin Hao215a14c2014-02-19 16:15:45 +0800548 /* Matching type is better than matching name */
549 if (type && type[0]) {
550 if (!device->type || of_node_cmp(type, device->type))
551 return 0;
552 score += 2;
553 }
554
555 /* Matching name is a bit better than not */
556 if (name && name[0]) {
557 if (!device->name || of_node_cmp(name, device->name))
558 return 0;
559 score++;
560 }
561
562 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000563}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500564
565/** Checks if the given "compat" string matches one of the strings in
566 * the device's "compatible" property
567 */
568int of_device_is_compatible(const struct device_node *device,
569 const char *compat)
570{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500571 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500572 int res;
573
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500574 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800575 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500576 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500577 return res;
578}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000579EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000580
581/**
Grant Likely71a157e2010-02-01 21:34:14 -0700582 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700583 * @compat: compatible string to look for in root node's compatible property.
584 *
585 * Returns true if the root node has the given value in its
586 * compatible property.
587 */
Grant Likely71a157e2010-02-01 21:34:14 -0700588int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700589{
590 struct device_node *root;
591 int rc = 0;
592
593 root = of_find_node_by_path("/");
594 if (root) {
595 rc = of_device_is_compatible(root, compat);
596 of_node_put(root);
597 }
598 return rc;
599}
Grant Likely71a157e2010-02-01 21:34:14 -0700600EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700601
602/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700603 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100604 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700605 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100606 *
607 * Returns 1 if the status property is absent or set to "okay" or "ok",
608 * 0 otherwise
609 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700610static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100611{
612 const char *status;
613 int statlen;
614
Xiubo Li42ccd782014-01-13 11:07:28 +0800615 if (!device)
616 return 0;
617
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700618 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100619 if (status == NULL)
620 return 1;
621
622 if (statlen > 0) {
623 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
624 return 1;
625 }
626
627 return 0;
628}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700629
630/**
631 * of_device_is_available - check if a device is available for use
632 *
633 * @device: Node to check for availability
634 *
635 * Returns 1 if the status property is absent or set to "okay" or "ok",
636 * 0 otherwise
637 */
638int of_device_is_available(const struct device_node *device)
639{
640 unsigned long flags;
641 int res;
642
643 raw_spin_lock_irqsave(&devtree_lock, flags);
644 res = __of_device_is_available(device);
645 raw_spin_unlock_irqrestore(&devtree_lock, flags);
646 return res;
647
648}
Josh Boyer834d97d2008-03-27 00:33:14 +1100649EXPORT_SYMBOL(of_device_is_available);
650
651/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000652 * of_get_parent - Get a node's parent if any
653 * @node: Node to get parent
654 *
655 * Returns a node pointer with refcount incremented, use
656 * of_node_put() on it when done.
657 */
658struct device_node *of_get_parent(const struct device_node *node)
659{
660 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500661 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000662
663 if (!node)
664 return NULL;
665
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500666 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000667 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500668 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000669 return np;
670}
671EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000672
673/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000674 * of_get_next_parent - Iterate to a node's parent
675 * @node: Node to get parent of
676 *
677 * This is like of_get_parent() except that it drops the
678 * refcount on the passed node, making it suitable for iterating
679 * through a node's parents.
680 *
681 * Returns a node pointer with refcount incremented, use
682 * of_node_put() on it when done.
683 */
684struct device_node *of_get_next_parent(struct device_node *node)
685{
686 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500687 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000688
689 if (!node)
690 return NULL;
691
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500692 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000693 parent = of_node_get(node->parent);
694 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500695 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000696 return parent;
697}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300698EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000699
Grant Likely0d0e02d2014-05-22 01:04:17 +0900700static struct device_node *__of_get_next_child(const struct device_node *node,
701 struct device_node *prev)
702{
703 struct device_node *next;
704
Florian Fainelli43cb4362014-05-28 10:39:02 -0700705 if (!node)
706 return NULL;
707
Grant Likely0d0e02d2014-05-22 01:04:17 +0900708 next = prev ? prev->sibling : node->child;
709 for (; next; next = next->sibling)
710 if (of_node_get(next))
711 break;
712 of_node_put(prev);
713 return next;
714}
715#define __for_each_child_of_node(parent, child) \
716 for (child = __of_get_next_child(parent, NULL); child != NULL; \
717 child = __of_get_next_child(parent, child))
718
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000719/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000720 * of_get_next_child - Iterate a node childs
721 * @node: parent node
722 * @prev: previous child of the parent node, or NULL to get first
723 *
724 * Returns a node pointer with refcount incremented, use
725 * of_node_put() on it when done.
726 */
727struct device_node *of_get_next_child(const struct device_node *node,
728 struct device_node *prev)
729{
730 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500731 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000732
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500733 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900734 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500735 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000736 return next;
737}
738EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000739
740/**
Timur Tabi32961932012-08-14 13:20:23 +0000741 * of_get_next_available_child - Find the next available child node
742 * @node: parent node
743 * @prev: previous child of the parent node, or NULL to get first
744 *
745 * This function is like of_get_next_child(), except that it
746 * automatically skips any disabled nodes (i.e. status = "disabled").
747 */
748struct device_node *of_get_next_available_child(const struct device_node *node,
749 struct device_node *prev)
750{
751 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000752 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000753
Florian Fainelli43cb4362014-05-28 10:39:02 -0700754 if (!node)
755 return NULL;
756
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000757 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000758 next = prev ? prev->sibling : node->child;
759 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700760 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000761 continue;
762 if (of_node_get(next))
763 break;
764 }
765 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000766 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000767 return next;
768}
769EXPORT_SYMBOL(of_get_next_available_child);
770
771/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100772 * of_get_child_by_name - Find the child node by name for a given parent
773 * @node: parent node
774 * @name: child name to look for.
775 *
776 * This function looks for child node for given matching name
777 *
778 * Returns a node pointer if found, with refcount incremented, use
779 * of_node_put() on it when done.
780 * Returns NULL if node is not found.
781 */
782struct device_node *of_get_child_by_name(const struct device_node *node,
783 const char *name)
784{
785 struct device_node *child;
786
787 for_each_child_of_node(node, child)
788 if (child->name && (of_node_cmp(child->name, name) == 0))
789 break;
790 return child;
791}
792EXPORT_SYMBOL(of_get_child_by_name);
793
Grant Likelyc22e6502014-03-14 17:07:12 +0000794static struct device_node *__of_find_node_by_path(struct device_node *parent,
795 const char *path)
796{
797 struct device_node *child;
798 int len = strchrnul(path, '/') - path;
799
800 if (!len)
801 return NULL;
802
803 __for_each_child_of_node(parent, child) {
804 const char *name = strrchr(child->full_name, '/');
805 if (WARN(!name, "malformed device_node %s\n", child->full_name))
806 continue;
807 name++;
808 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
809 return child;
810 }
811 return NULL;
812}
813
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100814/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000815 * of_find_node_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000816 * @path: Either the full path to match, or if the path does not
817 * start with '/', the name of a property of the /aliases
818 * node (an alias). In the case of an alias, the node
819 * matching the alias' value will be returned.
820 *
821 * Valid paths:
822 * /foo/bar Full path
823 * foo Valid alias
824 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000825 *
826 * Returns a node pointer with refcount incremented, use
827 * of_node_put() on it when done.
828 */
829struct device_node *of_find_node_by_path(const char *path)
830{
Grant Likelyc22e6502014-03-14 17:07:12 +0000831 struct device_node *np = NULL;
832 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500833 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000834
Grant Likelyc22e6502014-03-14 17:07:12 +0000835 if (strcmp(path, "/") == 0)
836 return of_node_get(of_allnodes);
837
838 /* The path could begin with an alias */
839 if (*path != '/') {
840 char *p = strchrnul(path, '/');
841 int len = p - path;
842
843 /* of_aliases must not be NULL */
844 if (!of_aliases)
845 return NULL;
846
847 for_each_property_of_node(of_aliases, pp) {
848 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
849 np = of_find_node_by_path(pp->value);
850 break;
851 }
852 }
853 if (!np)
854 return NULL;
855 path = p;
856 }
857
858 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500859 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000860 if (!np)
861 np = of_node_get(of_allnodes);
862 while (np && *path == '/') {
863 path++; /* Increment past '/' delimiter */
864 np = __of_find_node_by_path(np, path);
865 path = strchrnul(path, '/');
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000866 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500867 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000868 return np;
869}
870EXPORT_SYMBOL(of_find_node_by_path);
871
872/**
873 * of_find_node_by_name - Find a node by its "name" property
874 * @from: The node to start searching from or NULL, the node
875 * you pass will not be searched, only the next one
876 * will; typically, you pass what the previous call
877 * returned. of_node_put() will be called on it
878 * @name: The name string to match against
879 *
880 * Returns a node pointer with refcount incremented, use
881 * of_node_put() on it when done.
882 */
883struct device_node *of_find_node_by_name(struct device_node *from,
884 const char *name)
885{
886 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500887 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000888
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500889 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000890 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000891 for (; np; np = np->allnext)
892 if (np->name && (of_node_cmp(np->name, name) == 0)
893 && of_node_get(np))
894 break;
895 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500896 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000897 return np;
898}
899EXPORT_SYMBOL(of_find_node_by_name);
900
901/**
902 * of_find_node_by_type - Find a node by its "device_type" property
903 * @from: The node to start searching from, or NULL to start searching
904 * the entire device tree. The node you pass will not be
905 * searched, only the next one will; typically, you pass
906 * what the previous call returned. of_node_put() will be
907 * called on from for you.
908 * @type: The type string to match against
909 *
910 * Returns a node pointer with refcount incremented, use
911 * of_node_put() on it when done.
912 */
913struct device_node *of_find_node_by_type(struct device_node *from,
914 const char *type)
915{
916 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500917 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000918
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500919 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000920 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000921 for (; np; np = np->allnext)
922 if (np->type && (of_node_cmp(np->type, type) == 0)
923 && of_node_get(np))
924 break;
925 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500926 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000927 return np;
928}
929EXPORT_SYMBOL(of_find_node_by_type);
930
931/**
932 * of_find_compatible_node - Find a node based on type and one of the
933 * tokens in its "compatible" property
934 * @from: The node to start searching from or NULL, the node
935 * you pass will not be searched, only the next one
936 * will; typically, you pass what the previous call
937 * returned. of_node_put() will be called on it
938 * @type: The type string to match "device_type" or NULL to ignore
939 * @compatible: The string to match to one of the tokens in the device
940 * "compatible" list.
941 *
942 * Returns a node pointer with refcount incremented, use
943 * of_node_put() on it when done.
944 */
945struct device_node *of_find_compatible_node(struct device_node *from,
946 const char *type, const char *compatible)
947{
948 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500949 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000950
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500951 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000952 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000953 for (; np; np = np->allnext) {
Kevin Hao215a14c2014-02-19 16:15:45 +0800954 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500955 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000956 break;
957 }
958 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500959 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000960 return np;
961}
962EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100963
964/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000965 * of_find_node_with_property - Find a node which has a property with
966 * the given name.
967 * @from: The node to start searching from or NULL, the node
968 * you pass will not be searched, only the next one
969 * will; typically, you pass what the previous call
970 * returned. of_node_put() will be called on it
971 * @prop_name: The name of the property to look for.
972 *
973 * Returns a node pointer with refcount incremented, use
974 * of_node_put() on it when done.
975 */
976struct device_node *of_find_node_with_property(struct device_node *from,
977 const char *prop_name)
978{
979 struct device_node *np;
980 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500981 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000982
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500983 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000984 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000985 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530986 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000987 if (of_prop_cmp(pp->name, prop_name) == 0) {
988 of_node_get(np);
989 goto out;
990 }
991 }
992 }
993out:
994 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500995 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000996 return np;
997}
998EXPORT_SYMBOL(of_find_node_with_property);
999
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001000static
1001const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1002 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +11001003{
Kevin Hao215a14c2014-02-19 16:15:45 +08001004 const struct of_device_id *best_match = NULL;
1005 int score, best_score = 0;
1006
Grant Likelya52f07e2011-03-18 10:21:29 -06001007 if (!matches)
1008 return NULL;
1009
Kevin Hao215a14c2014-02-19 16:15:45 +08001010 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1011 score = __of_device_is_compatible(node, matches->compatible,
1012 matches->type, matches->name);
1013 if (score > best_score) {
1014 best_match = matches;
1015 best_score = score;
1016 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +08001017 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001018
1019 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001020}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001021
1022/**
1023 * of_match_node - Tell if an device_node has a matching of_match structure
1024 * @matches: array of of device match structures to search in
1025 * @node: the of device structure to match against
1026 *
Kevin Hao71c54982014-02-18 15:57:29 +08001027 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001028 */
1029const struct of_device_id *of_match_node(const struct of_device_id *matches,
1030 const struct device_node *node)
1031{
1032 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001033 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001034
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001035 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001036 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001037 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001038 return match;
1039}
Grant Likely283029d2008-01-09 06:20:40 +11001040EXPORT_SYMBOL(of_match_node);
1041
1042/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001043 * of_find_matching_node_and_match - Find a node based on an of_device_id
1044 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001045 * @from: The node to start searching from or NULL, the node
1046 * you pass will not be searched, only the next one
1047 * will; typically, you pass what the previous call
1048 * returned. of_node_put() will be called on it
1049 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001050 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001051 *
1052 * Returns a node pointer with refcount incremented, use
1053 * of_node_put() on it when done.
1054 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001055struct device_node *of_find_matching_node_and_match(struct device_node *from,
1056 const struct of_device_id *matches,
1057 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001058{
1059 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001060 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001061 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001062
Stephen Warren50c8af42012-11-20 16:12:20 -07001063 if (match)
1064 *match = NULL;
1065
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001066 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001067 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +11001068 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001069 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001070 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001071 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001072 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001073 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001074 }
Grant Likely283029d2008-01-09 06:20:40 +11001075 }
1076 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001077 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001078 return np;
1079}
Grant Likely80c20222012-12-19 10:45:36 +00001080EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001081
1082/**
Grant Likely3f07af42008-07-25 22:25:13 -04001083 * of_modalias_node - Lookup appropriate modalias for a device node
1084 * @node: pointer to a device tree node
1085 * @modalias: Pointer to buffer that modalias value will be copied into
1086 * @len: Length of modalias value
1087 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001088 * Based on the value of the compatible property, this routine will attempt
1089 * to choose an appropriate modalias value for a particular device tree node.
1090 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1091 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001092 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001093 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001094 */
1095int of_modalias_node(struct device_node *node, char *modalias, int len)
1096{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001097 const char *compatible, *p;
1098 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001099
1100 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001101 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001102 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001103 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001104 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001105 return 0;
1106}
1107EXPORT_SYMBOL_GPL(of_modalias_node);
1108
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001109/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001110 * of_find_node_by_phandle - Find a node given a phandle
1111 * @handle: phandle of the node to find
1112 *
1113 * Returns a node pointer with refcount incremented, use
1114 * of_node_put() on it when done.
1115 */
1116struct device_node *of_find_node_by_phandle(phandle handle)
1117{
1118 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001119 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001120
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001121 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001122 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001123 if (np->phandle == handle)
1124 break;
1125 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001126 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001127 return np;
1128}
1129EXPORT_SYMBOL(of_find_node_by_phandle);
1130
1131/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +01001132 * of_property_count_elems_of_size - Count the number of elements in a property
1133 *
1134 * @np: device node from which the property value is to be read.
1135 * @propname: name of the property to be searched.
1136 * @elem_size: size of the individual element
1137 *
1138 * Search for a property in a device node and count the number of elements of
1139 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1140 * property does not exist or its length does not match a multiple of elem_size
1141 * and -ENODATA if the property does not have a value.
1142 */
1143int of_property_count_elems_of_size(const struct device_node *np,
1144 const char *propname, int elem_size)
1145{
1146 struct property *prop = of_find_property(np, propname, NULL);
1147
1148 if (!prop)
1149 return -EINVAL;
1150 if (!prop->value)
1151 return -ENODATA;
1152
1153 if (prop->length % elem_size != 0) {
1154 pr_err("size of %s in node %s is not a multiple of %d\n",
1155 propname, np->full_name, elem_size);
1156 return -EINVAL;
1157 }
1158
1159 return prop->length / elem_size;
1160}
1161EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1162
1163/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001164 * of_find_property_value_of_size
1165 *
1166 * @np: device node from which the property value is to be read.
1167 * @propname: name of the property to be searched.
1168 * @len: requested length of property value
1169 *
1170 * Search for a property in a device node and valid the requested size.
1171 * Returns the property value on success, -EINVAL if the property does not
1172 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1173 * property data isn't large enough.
1174 *
1175 */
1176static void *of_find_property_value_of_size(const struct device_node *np,
1177 const char *propname, u32 len)
1178{
1179 struct property *prop = of_find_property(np, propname, NULL);
1180
1181 if (!prop)
1182 return ERR_PTR(-EINVAL);
1183 if (!prop->value)
1184 return ERR_PTR(-ENODATA);
1185 if (len > prop->length)
1186 return ERR_PTR(-EOVERFLOW);
1187
1188 return prop->value;
1189}
1190
1191/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001192 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1193 *
1194 * @np: device node from which the property value is to be read.
1195 * @propname: name of the property to be searched.
1196 * @index: index of the u32 in the list of values
1197 * @out_value: pointer to return value, modified only if no error.
1198 *
1199 * Search for a property in a device node and read nth 32-bit value from
1200 * it. Returns 0 on success, -EINVAL if the property does not exist,
1201 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1202 * property data isn't large enough.
1203 *
1204 * The out_value is modified only if a valid u32 value can be decoded.
1205 */
1206int of_property_read_u32_index(const struct device_node *np,
1207 const char *propname,
1208 u32 index, u32 *out_value)
1209{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001210 const u32 *val = of_find_property_value_of_size(np, propname,
1211 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +13001212
Tony Priskdaeec1f2013-04-03 17:57:11 +13001213 if (IS_ERR(val))
1214 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001215
Tony Priskdaeec1f2013-04-03 17:57:11 +13001216 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001217 return 0;
1218}
1219EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1220
1221/**
Viresh Kumarbe193242012-11-20 10:15:19 +05301222 * of_property_read_u8_array - Find and read an array of u8 from a property.
1223 *
1224 * @np: device node from which the property value is to be read.
1225 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301226 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301227 * @sz: number of array elements to read
1228 *
1229 * Search for a property in a device node and read 8-bit value(s) from
1230 * it. Returns 0 on success, -EINVAL if the property does not exist,
1231 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1232 * property data isn't large enough.
1233 *
1234 * dts entry of array should be like:
1235 * property = /bits/ 8 <0x50 0x60 0x70>;
1236 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301237 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301238 */
1239int of_property_read_u8_array(const struct device_node *np,
1240 const char *propname, u8 *out_values, size_t sz)
1241{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001242 const u8 *val = of_find_property_value_of_size(np, propname,
1243 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301244
Tony Priskdaeec1f2013-04-03 17:57:11 +13001245 if (IS_ERR(val))
1246 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301247
Viresh Kumarbe193242012-11-20 10:15:19 +05301248 while (sz--)
1249 *out_values++ = *val++;
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1253
1254/**
1255 * of_property_read_u16_array - Find and read an array of u16 from a property.
1256 *
1257 * @np: device node from which the property value is to be read.
1258 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301259 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301260 * @sz: number of array elements to read
1261 *
1262 * Search for a property in a device node and read 16-bit value(s) from
1263 * it. Returns 0 on success, -EINVAL if the property does not exist,
1264 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1265 * property data isn't large enough.
1266 *
1267 * dts entry of array should be like:
1268 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1269 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301270 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301271 */
1272int of_property_read_u16_array(const struct device_node *np,
1273 const char *propname, u16 *out_values, size_t sz)
1274{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001275 const __be16 *val = of_find_property_value_of_size(np, propname,
1276 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301277
Tony Priskdaeec1f2013-04-03 17:57:11 +13001278 if (IS_ERR(val))
1279 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301280
Viresh Kumarbe193242012-11-20 10:15:19 +05301281 while (sz--)
1282 *out_values++ = be16_to_cpup(val++);
1283 return 0;
1284}
1285EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1286
1287/**
Rob Herring0e373632011-07-06 15:42:58 -05001288 * of_property_read_u32_array - Find and read an array of 32 bit integers
1289 * from a property.
1290 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301291 * @np: device node from which the property value is to be read.
1292 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301293 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301294 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301295 *
Rob Herring0e373632011-07-06 15:42:58 -05001296 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301297 * it. Returns 0 on success, -EINVAL if the property does not exist,
1298 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1299 * property data isn't large enough.
1300 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301301 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301302 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001303int of_property_read_u32_array(const struct device_node *np,
1304 const char *propname, u32 *out_values,
1305 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301306{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001307 const __be32 *val = of_find_property_value_of_size(np, propname,
1308 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301309
Tony Priskdaeec1f2013-04-03 17:57:11 +13001310 if (IS_ERR(val))
1311 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001312
Rob Herring0e373632011-07-06 15:42:58 -05001313 while (sz--)
1314 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301315 return 0;
1316}
Rob Herring0e373632011-07-06 15:42:58 -05001317EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301318
1319/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001320 * of_property_read_u64 - Find and read a 64 bit integer from a property
1321 * @np: device node from which the property value is to be read.
1322 * @propname: name of the property to be searched.
1323 * @out_value: pointer to return value, modified only if return value is 0.
1324 *
1325 * Search for a property in a device node and read a 64-bit value from
1326 * it. Returns 0 on success, -EINVAL if the property does not exist,
1327 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1328 * property data isn't large enough.
1329 *
1330 * The out_value is modified only if a valid u64 value can be decoded.
1331 */
1332int of_property_read_u64(const struct device_node *np, const char *propname,
1333 u64 *out_value)
1334{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001335 const __be32 *val = of_find_property_value_of_size(np, propname,
1336 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001337
Tony Priskdaeec1f2013-04-03 17:57:11 +13001338 if (IS_ERR(val))
1339 return PTR_ERR(val);
1340
1341 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001342 return 0;
1343}
1344EXPORT_SYMBOL_GPL(of_property_read_u64);
1345
1346/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301347 * of_property_read_string - Find and read a string from a property
1348 * @np: device node from which the property value is to be read.
1349 * @propname: name of the property to be searched.
1350 * @out_string: pointer to null terminated return string, modified only if
1351 * return value is 0.
1352 *
1353 * Search for a property in a device tree node and retrieve a null
1354 * terminated string value (pointer to data, not a copy). Returns 0 on
1355 * success, -EINVAL if the property does not exist, -ENODATA if property
1356 * does not have a value, and -EILSEQ if the string is not null-terminated
1357 * within the length of the property data.
1358 *
1359 * The out_string pointer is modified only if a valid string can be decoded.
1360 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001361int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001362 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301363{
1364 struct property *prop = of_find_property(np, propname, NULL);
1365 if (!prop)
1366 return -EINVAL;
1367 if (!prop->value)
1368 return -ENODATA;
1369 if (strnlen(prop->value, prop->length) >= prop->length)
1370 return -EILSEQ;
1371 *out_string = prop->value;
1372 return 0;
1373}
1374EXPORT_SYMBOL_GPL(of_property_read_string);
1375
1376/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001377 * of_property_read_string_index - Find and read a string from a multiple
1378 * strings property.
1379 * @np: device node from which the property value is to be read.
1380 * @propname: name of the property to be searched.
1381 * @index: index of the string in the list of strings
1382 * @out_string: pointer to null terminated return string, modified only if
1383 * return value is 0.
1384 *
1385 * Search for a property in a device tree node and retrieve a null
1386 * terminated string value (pointer to data, not a copy) in the list of strings
1387 * contained in that property.
1388 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1389 * property does not have a value, and -EILSEQ if the string is not
1390 * null-terminated within the length of the property data.
1391 *
1392 * The out_string pointer is modified only if a valid string can be decoded.
1393 */
1394int of_property_read_string_index(struct device_node *np, const char *propname,
1395 int index, const char **output)
1396{
1397 struct property *prop = of_find_property(np, propname, NULL);
1398 int i = 0;
1399 size_t l = 0, total = 0;
1400 const char *p;
1401
1402 if (!prop)
1403 return -EINVAL;
1404 if (!prop->value)
1405 return -ENODATA;
1406 if (strnlen(prop->value, prop->length) >= prop->length)
1407 return -EILSEQ;
1408
1409 p = prop->value;
1410
1411 for (i = 0; total < prop->length; total += l, p += l) {
1412 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001413 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001414 *output = p;
1415 return 0;
1416 }
1417 }
1418 return -ENODATA;
1419}
1420EXPORT_SYMBOL_GPL(of_property_read_string_index);
1421
Grant Likely7aff0fe2011-12-12 09:25:58 -07001422/**
1423 * of_property_match_string() - Find string in a list and return index
1424 * @np: pointer to node containing string list property
1425 * @propname: string list property name
1426 * @string: pointer to string to search for in string list
1427 *
1428 * This function searches a string list property and returns the index
1429 * of a specific string value.
1430 */
1431int of_property_match_string(struct device_node *np, const char *propname,
1432 const char *string)
1433{
1434 struct property *prop = of_find_property(np, propname, NULL);
1435 size_t l;
1436 int i;
1437 const char *p, *end;
1438
1439 if (!prop)
1440 return -EINVAL;
1441 if (!prop->value)
1442 return -ENODATA;
1443
1444 p = prop->value;
1445 end = p + prop->length;
1446
1447 for (i = 0; p < end; i++, p += l) {
1448 l = strlen(p) + 1;
1449 if (p + l > end)
1450 return -EILSEQ;
1451 pr_debug("comparing %s with %s\n", string, p);
1452 if (strcmp(string, p) == 0)
1453 return i; /* Found it; return index */
1454 }
1455 return -ENODATA;
1456}
1457EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001458
1459/**
1460 * of_property_count_strings - Find and return the number of strings from a
1461 * multiple strings property.
1462 * @np: device node from which the property value is to be read.
1463 * @propname: name of the property to be searched.
1464 *
1465 * Search for a property in a device tree node and retrieve the number of null
1466 * terminated string contain in it. Returns the number of strings on
1467 * success, -EINVAL if the property does not exist, -ENODATA if property
1468 * does not have a value, and -EILSEQ if the string is not null-terminated
1469 * within the length of the property data.
1470 */
1471int of_property_count_strings(struct device_node *np, const char *propname)
1472{
1473 struct property *prop = of_find_property(np, propname, NULL);
1474 int i = 0;
1475 size_t l = 0, total = 0;
1476 const char *p;
1477
1478 if (!prop)
1479 return -EINVAL;
1480 if (!prop->value)
1481 return -ENODATA;
1482 if (strnlen(prop->value, prop->length) >= prop->length)
1483 return -EILSEQ;
1484
1485 p = prop->value;
1486
Benoit Cousson88af7f52011-12-05 15:23:54 +01001487 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001488 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001489
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001490 return i;
1491}
1492EXPORT_SYMBOL_GPL(of_property_count_strings);
1493
Grant Likely624cfca2013-10-11 22:05:10 +01001494void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1495{
1496 int i;
1497 printk("%s %s", msg, of_node_full_name(args->np));
1498 for (i = 0; i < args->args_count; i++)
1499 printk(i ? ",%08x" : ":%08x", args->args[i]);
1500 printk("\n");
1501}
1502
Grant Likelybd69f732013-02-10 22:57:21 +00001503static int __of_parse_phandle_with_args(const struct device_node *np,
1504 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001505 const char *cells_name,
1506 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001507 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001508{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001509 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001510 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001511 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001512 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001513 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001514
Grant Likely15c9a0a2011-12-12 09:25:57 -07001515 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001516 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001517 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001518 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001519 list_end = list + size / sizeof(*list);
1520
Grant Likely15c9a0a2011-12-12 09:25:57 -07001521 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001522 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001523 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001524 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001525
Grant Likely15c9a0a2011-12-12 09:25:57 -07001526 /*
1527 * If phandle is 0, then it is an empty entry with no
1528 * arguments. Skip forward to the next entry.
1529 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001530 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001531 if (phandle) {
1532 /*
1533 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001534 * property to determine the argument length.
1535 *
1536 * This is not needed if the cell count is hard-coded
1537 * (i.e. cells_name not set, but cell_count is set),
1538 * except when we're going to return the found node
1539 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001540 */
Stephen Warren91d99422013-08-14 15:27:11 -06001541 if (cells_name || cur_index == index) {
1542 node = of_find_node_by_phandle(phandle);
1543 if (!node) {
1544 pr_err("%s: could not find phandle\n",
1545 np->full_name);
1546 goto err;
1547 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001548 }
Stephen Warren035fd942013-08-14 15:27:10 -06001549
1550 if (cells_name) {
1551 if (of_property_read_u32(node, cells_name,
1552 &count)) {
1553 pr_err("%s: could not get %s for %s\n",
1554 np->full_name, cells_name,
1555 node->full_name);
1556 goto err;
1557 }
1558 } else {
1559 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001560 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001561
Grant Likely15c9a0a2011-12-12 09:25:57 -07001562 /*
1563 * Make sure that the arguments actually fit in the
1564 * remaining property data length
1565 */
1566 if (list + count > list_end) {
1567 pr_err("%s: arguments longer than property\n",
1568 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001569 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001570 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001571 }
1572
Grant Likely15c9a0a2011-12-12 09:25:57 -07001573 /*
1574 * All of the error cases above bail out of the loop, so at
1575 * this point, the parsing is successful. If the requested
1576 * index matches, then fill the out_args structure and return,
1577 * or return -ENOENT for an empty entry.
1578 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001579 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001580 if (cur_index == index) {
1581 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001582 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001583
Grant Likely15c9a0a2011-12-12 09:25:57 -07001584 if (out_args) {
1585 int i;
1586 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1587 count = MAX_PHANDLE_ARGS;
1588 out_args->np = node;
1589 out_args->args_count = count;
1590 for (i = 0; i < count; i++)
1591 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001592 } else {
1593 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001594 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001595
1596 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001597 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001598 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001599
1600 of_node_put(node);
1601 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001602 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001603 cur_index++;
1604 }
1605
Grant Likely23ce04c2013-02-12 21:21:49 +00001606 /*
1607 * Unlock node before returning result; will be one of:
1608 * -ENOENT : index is for empty phandle
1609 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001610 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001611 */
Grant Likelybd69f732013-02-10 22:57:21 +00001612 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001613 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001614 if (node)
1615 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001616 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001617}
Grant Likelybd69f732013-02-10 22:57:21 +00001618
Stephen Warreneded9dd2013-08-14 15:27:08 -06001619/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001620 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1621 * @np: Pointer to device node holding phandle property
1622 * @phandle_name: Name of property holding a phandle value
1623 * @index: For properties holding a table of phandles, this is the index into
1624 * the table
1625 *
1626 * Returns the device_node pointer with refcount incremented. Use
1627 * of_node_put() on it when done.
1628 */
1629struct device_node *of_parse_phandle(const struct device_node *np,
1630 const char *phandle_name, int index)
1631{
Stephen Warren91d99422013-08-14 15:27:11 -06001632 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001633
Stephen Warren91d99422013-08-14 15:27:11 -06001634 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001635 return NULL;
1636
Stephen Warren91d99422013-08-14 15:27:11 -06001637 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1638 index, &args))
1639 return NULL;
1640
1641 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001642}
1643EXPORT_SYMBOL(of_parse_phandle);
1644
1645/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001646 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1647 * @np: pointer to a device tree node containing a list
1648 * @list_name: property name that contains a list
1649 * @cells_name: property name that specifies phandles' arguments count
1650 * @index: index of a phandle to parse out
1651 * @out_args: optional pointer to output arguments structure (will be filled)
1652 *
1653 * This function is useful to parse lists of phandles and their arguments.
1654 * Returns 0 on success and fills out_args, on error returns appropriate
1655 * errno value.
1656 *
1657 * Caller is responsible to call of_node_put() on the returned out_args->node
1658 * pointer.
1659 *
1660 * Example:
1661 *
1662 * phandle1: node1 {
1663 * #list-cells = <2>;
1664 * }
1665 *
1666 * phandle2: node2 {
1667 * #list-cells = <1>;
1668 * }
1669 *
1670 * node3 {
1671 * list = <&phandle1 1 2 &phandle2 3>;
1672 * }
1673 *
1674 * To get a device_node of the `node2' node you may call this:
1675 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1676 */
Grant Likelybd69f732013-02-10 22:57:21 +00001677int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1678 const char *cells_name, int index,
1679 struct of_phandle_args *out_args)
1680{
1681 if (index < 0)
1682 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001683 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1684 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001685}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001686EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001687
Grant Likelybd69f732013-02-10 22:57:21 +00001688/**
Stephen Warren035fd942013-08-14 15:27:10 -06001689 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1690 * @np: pointer to a device tree node containing a list
1691 * @list_name: property name that contains a list
1692 * @cell_count: number of argument cells following the phandle
1693 * @index: index of a phandle to parse out
1694 * @out_args: optional pointer to output arguments structure (will be filled)
1695 *
1696 * This function is useful to parse lists of phandles and their arguments.
1697 * Returns 0 on success and fills out_args, on error returns appropriate
1698 * errno value.
1699 *
1700 * Caller is responsible to call of_node_put() on the returned out_args->node
1701 * pointer.
1702 *
1703 * Example:
1704 *
1705 * phandle1: node1 {
1706 * }
1707 *
1708 * phandle2: node2 {
1709 * }
1710 *
1711 * node3 {
1712 * list = <&phandle1 0 2 &phandle2 2 3>;
1713 * }
1714 *
1715 * To get a device_node of the `node2' node you may call this:
1716 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1717 */
1718int of_parse_phandle_with_fixed_args(const struct device_node *np,
1719 const char *list_name, int cell_count,
1720 int index, struct of_phandle_args *out_args)
1721{
1722 if (index < 0)
1723 return -EINVAL;
1724 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1725 index, out_args);
1726}
1727EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1728
1729/**
Grant Likelybd69f732013-02-10 22:57:21 +00001730 * of_count_phandle_with_args() - Find the number of phandles references in a property
1731 * @np: pointer to a device tree node containing a list
1732 * @list_name: property name that contains a list
1733 * @cells_name: property name that specifies phandles' arguments count
1734 *
1735 * Returns the number of phandle + argument tuples within a property. It
1736 * is a typical pattern to encode a list of phandle and variable
1737 * arguments into a single property. The number of arguments is encoded
1738 * by a property in the phandle-target node. For example, a gpios
1739 * property would contain a list of GPIO specifies consisting of a
1740 * phandle and 1 or more arguments. The number of arguments are
1741 * determined by the #gpio-cells property in the node pointed to by the
1742 * phandle.
1743 */
1744int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1745 const char *cells_name)
1746{
Stephen Warren035fd942013-08-14 15:27:10 -06001747 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1748 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001749}
1750EXPORT_SYMBOL(of_count_phandle_with_args);
1751
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001752#if defined(CONFIG_OF_DYNAMIC)
1753static int of_property_notify(int action, struct device_node *np,
1754 struct property *prop)
1755{
1756 struct of_prop_reconfig pr;
1757
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001758 /* only call notifiers if the node is attached */
1759 if (!of_node_is_attached(np))
1760 return 0;
1761
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001762 pr.dn = np;
1763 pr.prop = prop;
1764 return of_reconfig_notify(action, &pr);
1765}
1766#else
1767static int of_property_notify(int action, struct device_node *np,
1768 struct property *prop)
1769{
1770 return 0;
1771}
1772#endif
1773
Grant Likely02af11b2009-11-23 20:16:45 -07001774/**
Xiubo Li62664f62014-01-22 13:57:39 +08001775 * __of_add_property - Add a property to a node without lock operations
1776 */
1777static int __of_add_property(struct device_node *np, struct property *prop)
1778{
1779 struct property **next;
1780
1781 prop->next = NULL;
1782 next = &np->properties;
1783 while (*next) {
1784 if (strcmp(prop->name, (*next)->name) == 0)
1785 /* duplicate ! don't insert it */
1786 return -EEXIST;
1787
1788 next = &(*next)->next;
1789 }
1790 *next = prop;
1791
1792 return 0;
1793}
1794
1795/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001796 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001797 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001798int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001799{
Grant Likely02af11b2009-11-23 20:16:45 -07001800 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001801 int rc;
1802
1803 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1804 if (rc)
1805 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001806
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001807 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001808 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001809 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001810 if (rc)
1811 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001812
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001813 if (of_node_is_attached(np))
1814 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001815
Xiubo Li62664f62014-01-22 13:57:39 +08001816 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001817}
1818
1819/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001820 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001821 *
1822 * Note that we don't actually remove it, since we have given out
1823 * who-knows-how-many pointers to the data using get-property.
1824 * Instead we just move the property to the "dead properties"
1825 * list, so it won't be found any more.
1826 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001827int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001828{
1829 struct property **next;
1830 unsigned long flags;
1831 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001832 int rc;
1833
1834 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1835 if (rc)
1836 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001837
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001838 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001839 next = &np->properties;
1840 while (*next) {
1841 if (*next == prop) {
1842 /* found the node */
1843 *next = prop->next;
1844 prop->next = np->deadprops;
1845 np->deadprops = prop;
1846 found = 1;
1847 break;
1848 }
1849 next = &(*next)->next;
1850 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001851 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001852
1853 if (!found)
1854 return -ENODEV;
1855
Grant Likely75b57ec2014-02-20 18:02:11 +00001856 /* at early boot, bail hear and defer setup to of_init() */
1857 if (!of_kset)
1858 return 0;
1859
1860 sysfs_remove_bin_file(&np->kobj, &prop->attr);
Grant Likely02af11b2009-11-23 20:16:45 -07001861
1862 return 0;
1863}
1864
1865/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001866 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001867 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001868 *
1869 * Note that we don't actually remove it, since we have given out
1870 * who-knows-how-many pointers to the data using get-property.
1871 * Instead we just move the property to the "dead properties" list,
1872 * and add the new property to the property list
1873 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001874int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001875{
Dong Aisheng475d0092012-07-11 15:16:37 +10001876 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001877 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001878 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001879
1880 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1881 if (rc)
1882 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001883
Dong Aisheng475d0092012-07-11 15:16:37 +10001884 if (!newprop->name)
1885 return -EINVAL;
1886
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001887 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001888 next = &np->properties;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001889 oldprop = __of_find_property(np, newprop->name, NULL);
1890 if (!oldprop) {
1891 /* add the new node */
1892 rc = __of_add_property(np, newprop);
1893 } else while (*next) {
1894 /* replace the node */
Grant Likely02af11b2009-11-23 20:16:45 -07001895 if (*next == oldprop) {
Grant Likely02af11b2009-11-23 20:16:45 -07001896 newprop->next = oldprop->next;
1897 *next = newprop;
1898 oldprop->next = np->deadprops;
1899 np->deadprops = oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001900 break;
1901 }
1902 next = &(*next)->next;
1903 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001904 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Xiubo Li947fdaad02014-04-17 15:48:29 +08001905 if (rc)
1906 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001907
Thomas Petazzoni582da652014-05-14 13:36:36 +02001908 /* At early boot, bail out and defer setup to of_init() */
1909 if (!of_kset)
Xiubo Li947fdaad02014-04-17 15:48:29 +08001910 return 0;
Thomas Petazzoni582da652014-05-14 13:36:36 +02001911
Guenter Roecke7a62df2014-04-15 08:38:17 -07001912 /* Update the sysfs attribute */
Xiubo Li947fdaad02014-04-17 15:48:29 +08001913 if (oldprop)
1914 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
Guenter Roecke7a62df2014-04-15 08:38:17 -07001915 __of_add_property_sysfs(np, newprop);
1916
Grant Likely02af11b2009-11-23 20:16:45 -07001917 return 0;
1918}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001919
1920#if defined(CONFIG_OF_DYNAMIC)
1921/*
1922 * Support for dynamic device trees.
1923 *
1924 * On some platforms, the device tree can be manipulated at runtime.
1925 * The routines in this section support adding, removing and changing
1926 * device tree nodes.
1927 */
1928
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001929static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1930
1931int of_reconfig_notifier_register(struct notifier_block *nb)
1932{
1933 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1934}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001935EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001936
1937int of_reconfig_notifier_unregister(struct notifier_block *nb)
1938{
1939 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1940}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001941EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001942
1943int of_reconfig_notify(unsigned long action, void *p)
1944{
1945 int rc;
1946
1947 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1948 return notifier_to_errno(rc);
1949}
1950
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001951/**
1952 * of_attach_node - Plug a device node into the tree and global list.
1953 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001954int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001955{
1956 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001957 int rc;
1958
1959 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1960 if (rc)
1961 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001962
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001963 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001964 np->sibling = np->parent->child;
Frank Rowand99de6492014-06-14 20:39:05 -07001965 np->allnext = np->parent->allnext;
1966 np->parent->allnext = np;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001967 np->parent->child = np;
Pantelis Antonioue3963fd2013-11-08 17:03:57 +02001968 of_node_clear_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001969 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001970
Grant Likely75b57ec2014-02-20 18:02:11 +00001971 of_node_add(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001972 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001973}
1974
1975/**
1976 * of_detach_node - "Unplug" a node from the device tree.
1977 *
1978 * The caller must hold a reference to the node. The memory associated with
1979 * the node is not freed until its refcount goes to zero.
1980 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001981int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001982{
1983 struct device_node *parent;
1984 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001985 int rc = 0;
1986
1987 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1988 if (rc)
1989 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001990
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001991 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001992
Nathan Fontenote81b3292012-10-02 16:55:01 +00001993 if (of_node_check_flag(np, OF_DETACHED)) {
1994 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001995 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001996 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001997 }
1998
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001999 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00002000 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05002001 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00002002 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00002003 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002004
Randy Dunlap465aac62012-11-30 10:01:51 +00002005 if (of_allnodes == np)
2006 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002007 else {
2008 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00002009 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002010 prev->allnext != np;
2011 prev = prev->allnext)
2012 ;
2013 prev->allnext = np->allnext;
2014 }
2015
2016 if (parent->child == np)
2017 parent->child = np->sibling;
2018 else {
2019 struct device_node *prevsib;
2020 for (prevsib = np->parent->child;
2021 prevsib->sibling != np;
2022 prevsib = prevsib->sibling)
2023 ;
2024 prevsib->sibling = np->sibling;
2025 }
2026
2027 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05002028 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00002029
Grant Likely75b57ec2014-02-20 18:02:11 +00002030 of_node_remove(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00002031 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002032}
2033#endif /* defined(CONFIG_OF_DYNAMIC) */
2034
Shawn Guo611cad72011-08-15 15:28:14 +08002035static void of_alias_add(struct alias_prop *ap, struct device_node *np,
2036 int id, const char *stem, int stem_len)
2037{
2038 ap->np = np;
2039 ap->id = id;
2040 strncpy(ap->stem, stem, stem_len);
2041 ap->stem[stem_len] = 0;
2042 list_add_tail(&ap->link, &aliases_lookup);
2043 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06002044 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08002045}
2046
2047/**
2048 * of_alias_scan - Scan all properties of 'aliases' node
2049 *
2050 * The function scans all the properties of 'aliases' node and populate
2051 * the the global lookup table with the properties. It returns the
2052 * number of alias_prop found, or error code in error case.
2053 *
2054 * @dt_alloc: An allocator that provides a virtual address to memory
2055 * for the resulting tree
2056 */
2057void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
2058{
2059 struct property *pp;
2060
2061 of_chosen = of_find_node_by_path("/chosen");
2062 if (of_chosen == NULL)
2063 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02002064
2065 if (of_chosen) {
Grant Likely676e1b22014-03-27 17:11:23 -07002066 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
2067 if (!name)
2068 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002069 if (name)
2070 of_stdout = of_find_node_by_path(name);
2071 }
2072
Shawn Guo611cad72011-08-15 15:28:14 +08002073 of_aliases = of_find_node_by_path("/aliases");
2074 if (!of_aliases)
2075 return;
2076
Dong Aisheng8af0da92011-12-22 20:19:24 +08002077 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08002078 const char *start = pp->name;
2079 const char *end = start + strlen(start);
2080 struct device_node *np;
2081 struct alias_prop *ap;
2082 int id, len;
2083
2084 /* Skip those we do not want to proceed */
2085 if (!strcmp(pp->name, "name") ||
2086 !strcmp(pp->name, "phandle") ||
2087 !strcmp(pp->name, "linux,phandle"))
2088 continue;
2089
2090 np = of_find_node_by_path(pp->value);
2091 if (!np)
2092 continue;
2093
2094 /* walk the alias backwards to extract the id and work out
2095 * the 'stem' string */
2096 while (isdigit(*(end-1)) && end > start)
2097 end--;
2098 len = end - start;
2099
2100 if (kstrtoint(end, 10, &id) < 0)
2101 continue;
2102
2103 /* Allocate an alias_prop with enough space for the stem */
2104 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
2105 if (!ap)
2106 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01002107 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08002108 ap->alias = start;
2109 of_alias_add(ap, np, id, start, len);
2110 }
2111}
2112
2113/**
2114 * of_alias_get_id - Get alias id for the given device_node
2115 * @np: Pointer to the given device_node
2116 * @stem: Alias stem of the given device_node
2117 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01002118 * The function travels the lookup table to get the alias id for the given
2119 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08002120 */
2121int of_alias_get_id(struct device_node *np, const char *stem)
2122{
2123 struct alias_prop *app;
2124 int id = -ENODEV;
2125
2126 mutex_lock(&of_aliases_mutex);
2127 list_for_each_entry(app, &aliases_lookup, link) {
2128 if (strcmp(app->stem, stem) != 0)
2129 continue;
2130
2131 if (np == app->np) {
2132 id = app->id;
2133 break;
2134 }
2135 }
2136 mutex_unlock(&of_aliases_mutex);
2137
2138 return id;
2139}
2140EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06002141
2142const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2143 u32 *pu)
2144{
2145 const void *curv = cur;
2146
2147 if (!prop)
2148 return NULL;
2149
2150 if (!cur) {
2151 curv = prop->value;
2152 goto out_val;
2153 }
2154
2155 curv += sizeof(*cur);
2156 if (curv >= prop->value + prop->length)
2157 return NULL;
2158
2159out_val:
2160 *pu = be32_to_cpup(curv);
2161 return curv;
2162}
2163EXPORT_SYMBOL_GPL(of_prop_next_u32);
2164
2165const char *of_prop_next_string(struct property *prop, const char *cur)
2166{
2167 const void *curv = cur;
2168
2169 if (!prop)
2170 return NULL;
2171
2172 if (!cur)
2173 return prop->value;
2174
2175 curv += strlen(cur) + 1;
2176 if (curv >= prop->value + prop->length)
2177 return NULL;
2178
2179 return curv;
2180}
2181EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002182
2183/**
Grant Likely3482f2c2014-03-27 17:18:55 -07002184 * of_console_check() - Test and setup console for DT setup
2185 * @dn - Pointer to device node
2186 * @name - Name to use for preferred console without index. ex. "ttyS"
2187 * @index - Index to use for preferred console.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002188 *
Grant Likely3482f2c2014-03-27 17:18:55 -07002189 * Check if the given device node matches the stdout-path property in the
2190 * /chosen node. If it does then register it as the preferred console and return
2191 * TRUE. Otherwise return FALSE.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002192 */
Grant Likely3482f2c2014-03-27 17:18:55 -07002193bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002194{
Grant Likely3482f2c2014-03-27 17:18:55 -07002195 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002196 return false;
Grant Likely3482f2c2014-03-27 17:18:55 -07002197 return add_preferred_console(name, index, NULL);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002198}
Grant Likely3482f2c2014-03-27 17:18:55 -07002199EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002200
2201/**
2202 * of_find_next_cache_node - Find a node's subsidiary cache
2203 * @np: node of type "cpu" or "cache"
2204 *
2205 * Returns a node pointer with refcount incremented, use
2206 * of_node_put() on it when done. Caller should hold a reference
2207 * to np.
2208 */
2209struct device_node *of_find_next_cache_node(const struct device_node *np)
2210{
2211 struct device_node *child;
2212 const phandle *handle;
2213
2214 handle = of_get_property(np, "l2-cache", NULL);
2215 if (!handle)
2216 handle = of_get_property(np, "next-level-cache", NULL);
2217
2218 if (handle)
2219 return of_find_node_by_phandle(be32_to_cpup(handle));
2220
2221 /* OF on pmac has nodes instead of properties named "l2-cache"
2222 * beneath CPU nodes.
2223 */
2224 if (!strcmp(np->type, "cpu"))
2225 for_each_child_of_node(np, child)
2226 if (!strcmp(child->type, "cache"))
2227 return child;
2228
2229 return NULL;
2230}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002231
2232/**
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002233 * of_graph_parse_endpoint() - parse common endpoint node properties
2234 * @node: pointer to endpoint device_node
2235 * @endpoint: pointer to the OF endpoint data structure
2236 *
2237 * The caller should hold a reference to @node.
2238 */
2239int of_graph_parse_endpoint(const struct device_node *node,
2240 struct of_endpoint *endpoint)
2241{
2242 struct device_node *port_node = of_get_parent(node);
2243
Philipp Zabeld4847002014-03-04 12:31:24 +01002244 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2245 __func__, node->full_name);
2246
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002247 memset(endpoint, 0, sizeof(*endpoint));
2248
2249 endpoint->local_node = node;
2250 /*
2251 * It doesn't matter whether the two calls below succeed.
2252 * If they don't then the default value 0 is used.
2253 */
2254 of_property_read_u32(port_node, "reg", &endpoint->port);
2255 of_property_read_u32(node, "reg", &endpoint->id);
2256
2257 of_node_put(port_node);
2258
2259 return 0;
2260}
2261EXPORT_SYMBOL(of_graph_parse_endpoint);
2262
2263/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002264 * of_graph_get_next_endpoint() - get next endpoint node
2265 * @parent: pointer to the parent device node
2266 * @prev: previous endpoint node, or NULL to get first
2267 *
2268 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2269 * of the passed @prev node is not decremented, the caller have to use
2270 * of_node_put() on it when done.
2271 */
2272struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2273 struct device_node *prev)
2274{
2275 struct device_node *endpoint;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002276 struct device_node *port;
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002277
2278 if (!parent)
2279 return NULL;
2280
Linus Torvalds3c83e612014-04-04 09:50:07 -07002281 /*
2282 * Start by locating the port node. If no previous endpoint is specified
2283 * search for the first port node, otherwise get the previous endpoint
2284 * parent port node.
2285 */
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002286 if (!prev) {
2287 struct device_node *node;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002288
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002289 node = of_get_child_by_name(parent, "ports");
2290 if (node)
2291 parent = node;
2292
2293 port = of_get_child_by_name(parent, "port");
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002294 of_node_put(node);
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002295
Linus Torvalds3c83e612014-04-04 09:50:07 -07002296 if (!port) {
2297 pr_err("%s(): no port node found in %s\n",
2298 __func__, parent->full_name);
Philipp Zabel4329b932014-02-26 20:43:42 +01002299 return NULL;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002300 }
2301 } else {
2302 port = of_get_parent(prev);
2303 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2304 __func__, prev->full_name))
2305 return NULL;
Philipp Zabel4329b932014-02-26 20:43:42 +01002306
Linus Torvalds3c83e612014-04-04 09:50:07 -07002307 /*
2308 * Avoid dropping prev node refcount to 0 when getting the next
2309 * child below.
2310 */
2311 of_node_get(prev);
2312 }
Philipp Zabel4329b932014-02-26 20:43:42 +01002313
Linus Torvalds3c83e612014-04-04 09:50:07 -07002314 while (1) {
2315 /*
2316 * Now that we have a port node, get the next endpoint by
2317 * getting the next child. If the previous endpoint is NULL this
2318 * will return the first child.
2319 */
2320 endpoint = of_get_next_child(port, prev);
2321 if (endpoint) {
2322 of_node_put(port);
2323 return endpoint;
2324 }
2325
2326 /* No more endpoints under this port, try the next one. */
2327 prev = NULL;
2328
2329 do {
2330 port = of_get_next_child(parent, port);
2331 if (!port)
2332 return NULL;
2333 } while (of_node_cmp(port->name, "port"));
2334 }
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002335}
2336EXPORT_SYMBOL(of_graph_get_next_endpoint);
2337
2338/**
2339 * of_graph_get_remote_port_parent() - get remote port's parent node
2340 * @node: pointer to a local endpoint device_node
2341 *
2342 * Return: Remote device node associated with remote endpoint node linked
2343 * to @node. Use of_node_put() on it when done.
2344 */
2345struct device_node *of_graph_get_remote_port_parent(
2346 const struct device_node *node)
2347{
2348 struct device_node *np;
2349 unsigned int depth;
2350
2351 /* Get remote endpoint node. */
2352 np = of_parse_phandle(node, "remote-endpoint", 0);
2353
2354 /* Walk 3 levels up only if there is 'ports' node. */
2355 for (depth = 3; depth && np; depth--) {
2356 np = of_get_next_parent(np);
2357 if (depth == 2 && of_node_cmp(np->name, "ports"))
2358 break;
2359 }
2360 return np;
2361}
2362EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2363
2364/**
2365 * of_graph_get_remote_port() - get remote port node
2366 * @node: pointer to a local endpoint device_node
2367 *
2368 * Return: Remote port node associated with remote endpoint node linked
2369 * to @node. Use of_node_put() on it when done.
2370 */
2371struct device_node *of_graph_get_remote_port(const struct device_node *node)
2372{
2373 struct device_node *np;
2374
2375 /* Get remote endpoint node. */
2376 np = of_parse_phandle(node, "remote-endpoint", 0);
2377 if (!np)
2378 return NULL;
2379 return of_get_next_parent(np);
2380}
2381EXPORT_SYMBOL(of_graph_get_remote_port);