blob: b9864806e9b811a0c3cc3b0e16404a19fad271eb [file] [log] [blame]
Stephen Rothwell97e873e2007-05-01 16:26:07 +10001/*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11 *
Grant Likelye91edcf2009-10-15 10:58:09 -060012 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
Stephen Rothwell97e873e2007-05-01 16:26:07 +100014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
Shawn Guo611cad72011-08-15 15:28:14 +080020#include <linux/ctype.h>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010021#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100022#include <linux/module.h>
23#include <linux/of.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010024#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100025#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000027#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070028#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100029
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080030#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080031
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080032LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080033
Randy Dunlap465aac62012-11-30 10:01:51 +000034struct device_node *of_allnodes;
35EXPORT_SYMBOL(of_allnodes);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070036struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080037struct device_node *of_aliases;
Sascha Hauer5c19e952013-08-05 14:40:44 +020038static struct device_node *of_stdout;
Shawn Guo611cad72011-08-15 15:28:14 +080039
Grant Likely75b57ec2014-02-20 18:02:11 +000040static struct kset *of_kset;
41
42/*
43 * Used to protect the of_aliases; but also overloaded to hold off addition of
44 * nodes to sysfs
45 */
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080046DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100047
Stephen Rothwell581b6052007-04-24 16:46:53 +100048/* use when traversing tree through the allnext, child, sibling,
49 * or parent members of struct device_node.
50 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050051DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100052
53int of_n_addr_cells(struct device_node *np)
54{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060055 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100056
57 do {
58 if (np->parent)
59 np = np->parent;
60 ip = of_get_property(np, "#address-cells", NULL);
61 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070062 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100063 } while (np->parent);
64 /* No #address-cells property for the root node */
65 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
66}
67EXPORT_SYMBOL(of_n_addr_cells);
68
69int of_n_size_cells(struct device_node *np)
70{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060071 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100072
73 do {
74 if (np->parent)
75 np = np->parent;
76 ip = of_get_property(np, "#size-cells", NULL);
77 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070078 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100079 } while (np->parent);
80 /* No #size-cells property for the root node */
81 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
82}
83EXPORT_SYMBOL(of_n_size_cells);
84
Rob Herring0c3f0612013-09-17 10:42:50 -050085#ifdef CONFIG_NUMA
86int __weak of_node_to_nid(struct device_node *np)
87{
88 return numa_node_id();
89}
90#endif
91
Grant Likely0f22dd32012-02-15 20:38:40 -070092#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070093/**
94 * of_node_get - Increment refcount of a node
95 * @node: Node to inc refcount, NULL is supported to
96 * simplify writing of callers
97 *
98 * Returns node.
99 */
100struct device_node *of_node_get(struct device_node *node)
101{
102 if (node)
Grant Likely75b57ec2014-02-20 18:02:11 +0000103 kobject_get(&node->kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700104 return node;
105}
106EXPORT_SYMBOL(of_node_get);
107
Grant Likely75b57ec2014-02-20 18:02:11 +0000108static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -0700109{
Grant Likely75b57ec2014-02-20 18:02:11 +0000110 return container_of(kobj, struct device_node, kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700111}
112
113/**
114 * of_node_release - release a dynamically allocated node
115 * @kref: kref element of the node to be released
116 *
117 * In of_node_put() this function is passed to kref_put()
118 * as the destructor.
119 */
Grant Likely75b57ec2014-02-20 18:02:11 +0000120static void of_node_release(struct kobject *kobj)
Grant Likely923f7e32010-01-28 13:52:53 -0700121{
Grant Likely75b57ec2014-02-20 18:02:11 +0000122 struct device_node *node = kobj_to_device_node(kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700123 struct property *prop = node->properties;
124
125 /* We should never be releasing nodes that haven't been detached. */
126 if (!of_node_check_flag(node, OF_DETACHED)) {
127 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
128 dump_stack();
Grant Likely923f7e32010-01-28 13:52:53 -0700129 return;
130 }
131
132 if (!of_node_check_flag(node, OF_DYNAMIC))
133 return;
134
135 while (prop) {
136 struct property *next = prop->next;
137 kfree(prop->name);
138 kfree(prop->value);
139 kfree(prop);
140 prop = next;
141
142 if (!prop) {
143 prop = node->deadprops;
144 node->deadprops = NULL;
145 }
146 }
147 kfree(node->full_name);
148 kfree(node->data);
149 kfree(node);
150}
151
152/**
153 * of_node_put - Decrement refcount of a node
154 * @node: Node to dec refcount, NULL is supported to
155 * simplify writing of callers
156 *
157 */
158void of_node_put(struct device_node *node)
159{
160 if (node)
Grant Likely75b57ec2014-02-20 18:02:11 +0000161 kobject_put(&node->kobj);
Grant Likely923f7e32010-01-28 13:52:53 -0700162}
163EXPORT_SYMBOL(of_node_put);
Grant Likely75b57ec2014-02-20 18:02:11 +0000164#else
165static void of_node_release(struct kobject *kobj)
166{
167 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
168}
Grant Likely0f22dd32012-02-15 20:38:40 -0700169#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700170
Grant Likely75b57ec2014-02-20 18:02:11 +0000171struct kobj_type of_node_ktype = {
172 .release = of_node_release,
173};
174
175static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
176 struct bin_attribute *bin_attr, char *buf,
177 loff_t offset, size_t count)
178{
179 struct property *pp = container_of(bin_attr, struct property, attr);
180 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
181}
182
183static const char *safe_name(struct kobject *kobj, const char *orig_name)
184{
185 const char *name = orig_name;
186 struct kernfs_node *kn;
187 int i = 0;
188
189 /* don't be a hero. After 16 tries give up */
190 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
191 sysfs_put(kn);
192 if (name != orig_name)
193 kfree(name);
194 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
195 }
196
197 if (name != orig_name)
198 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
199 kobject_name(kobj), name);
200 return name;
201}
202
203static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
204{
205 int rc;
206
207 /* Important: Don't leak passwords */
208 bool secure = strncmp(pp->name, "security-", 9) == 0;
209
210 sysfs_bin_attr_init(&pp->attr);
211 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
212 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
213 pp->attr.size = secure ? 0 : pp->length;
214 pp->attr.read = of_node_property_read;
215
216 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
217 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
218 return rc;
219}
220
221static int __of_node_add(struct device_node *np)
222{
223 const char *name;
224 struct property *pp;
225 int rc;
226
227 np->kobj.kset = of_kset;
228 if (!np->parent) {
229 /* Nodes without parents are new top level trees */
Kees Cook28d3ee42014-06-10 09:57:00 -0700230 rc = kobject_add(&np->kobj, NULL, "%s",
231 safe_name(&of_kset->kobj, "base"));
Grant Likely75b57ec2014-02-20 18:02:11 +0000232 } else {
233 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
234 if (!name || !name[0])
235 return -EINVAL;
236
237 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
238 }
239 if (rc)
240 return rc;
241
242 for_each_property_of_node(np, pp)
243 __of_add_property_sysfs(np, pp);
244
245 return 0;
246}
247
248int of_node_add(struct device_node *np)
249{
250 int rc = 0;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200251
252 BUG_ON(!of_node_is_initialized(np));
253
254 /*
255 * Grab the mutex here so that in a race condition between of_init() and
256 * of_node_add(), node addition will still be consistent.
257 */
Grant Likely75b57ec2014-02-20 18:02:11 +0000258 mutex_lock(&of_aliases_mutex);
259 if (of_kset)
260 rc = __of_node_add(np);
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200261 else
262 /* This scenario may be perfectly valid, but report it anyway */
263 pr_info("of_node_add(%s) before of_init()\n", np->full_name);
Grant Likely75b57ec2014-02-20 18:02:11 +0000264 mutex_unlock(&of_aliases_mutex);
265 return rc;
266}
267
268#if defined(CONFIG_OF_DYNAMIC)
269static void of_node_remove(struct device_node *np)
270{
271 struct property *pp;
272
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200273 BUG_ON(!of_node_is_initialized(np));
Grant Likely75b57ec2014-02-20 18:02:11 +0000274
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200275 /* only remove properties if on sysfs */
276 if (of_node_is_attached(np)) {
277 for_each_property_of_node(np, pp)
278 sysfs_remove_bin_file(&np->kobj, &pp->attr);
279 kobject_del(&np->kobj);
280 }
281
282 /* finally remove the kobj_init ref */
283 of_node_put(np);
Grant Likely75b57ec2014-02-20 18:02:11 +0000284}
285#endif
286
287static int __init of_init(void)
288{
289 struct device_node *np;
290
291 /* Create the kset, and register existing nodes */
292 mutex_lock(&of_aliases_mutex);
293 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
294 if (!of_kset) {
295 mutex_unlock(&of_aliases_mutex);
296 return -ENOMEM;
297 }
298 for_each_of_allnodes(np)
299 __of_node_add(np);
300 mutex_unlock(&of_aliases_mutex);
301
Grant Likely83570412012-11-06 21:03:27 +0000302 /* Symlink in /proc as required by userspace ABI */
Grant Likely75b57ec2014-02-20 18:02:11 +0000303 if (of_allnodes)
304 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000305
306 return 0;
307}
308core_initcall(of_init);
309
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500310static struct property *__of_find_property(const struct device_node *np,
311 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000312{
313 struct property *pp;
314
Timur Tabi64e45662008-05-08 05:19:59 +1000315 if (!np)
316 return NULL;
317
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530318 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000319 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530320 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000321 *lenp = pp->length;
322 break;
323 }
324 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500325
326 return pp;
327}
328
329struct property *of_find_property(const struct device_node *np,
330 const char *name,
331 int *lenp)
332{
333 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500334 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500335
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500336 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500337 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500338 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000339
340 return pp;
341}
342EXPORT_SYMBOL(of_find_property);
343
Grant Likelye91edcf2009-10-15 10:58:09 -0600344/**
345 * of_find_all_nodes - Get next node in global list
346 * @prev: Previous node or NULL to start iteration
347 * of_node_put() will be called on it
348 *
349 * Returns a node pointer with refcount incremented, use
350 * of_node_put() on it when done.
351 */
352struct device_node *of_find_all_nodes(struct device_node *prev)
353{
354 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000355 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600356
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000357 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000358 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600359 for (; np != NULL; np = np->allnext)
360 if (of_node_get(np))
361 break;
362 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000363 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600364 return np;
365}
366EXPORT_SYMBOL(of_find_all_nodes);
367
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000368/*
369 * Find a property with a given name for a given node
370 * and return the value.
371 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500372static const void *__of_get_property(const struct device_node *np,
373 const char *name, int *lenp)
374{
375 struct property *pp = __of_find_property(np, name, lenp);
376
377 return pp ? pp->value : NULL;
378}
379
380/*
381 * Find a property with a given name for a given node
382 * and return the value.
383 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000384const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500385 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000386{
387 struct property *pp = of_find_property(np, name, lenp);
388
389 return pp ? pp->value : NULL;
390}
391EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000392
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100393/*
394 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
395 *
396 * @cpu: logical cpu index of a core/thread
397 * @phys_id: physical identifier of a core/thread
398 *
399 * CPU logical to physical index mapping is architecture specific.
400 * However this __weak function provides a default match of physical
401 * id to logical cpu index. phys_id provided here is usually values read
402 * from the device tree which must match the hardware internal registers.
403 *
404 * Returns true if the physical identifier and the logical cpu index
405 * correspond to the same core/thread, false otherwise.
406 */
407bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
408{
409 return (u32)phys_id == cpu;
410}
411
412/**
413 * Checks if the given "prop_name" property holds the physical id of the
414 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
415 * NULL, local thread number within the core is returned in it.
416 */
417static bool __of_find_n_match_cpu_property(struct device_node *cpun,
418 const char *prop_name, int cpu, unsigned int *thread)
419{
420 const __be32 *cell;
421 int ac, prop_len, tid;
422 u64 hwid;
423
424 ac = of_n_addr_cells(cpun);
425 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100426 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100427 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100428 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100429 for (tid = 0; tid < prop_len; tid++) {
430 hwid = of_read_number(cell, ac);
431 if (arch_match_cpu_phys_id(cpu, hwid)) {
432 if (thread)
433 *thread = tid;
434 return true;
435 }
436 cell += ac;
437 }
438 return false;
439}
440
David Millerd1cb9d12013-10-03 17:24:51 -0400441/*
442 * arch_find_n_match_cpu_physical_id - See if the given device node is
443 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
444 * else false. If 'thread' is non-NULL, the local thread number within the
445 * core is returned in it.
446 */
447bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
448 int cpu, unsigned int *thread)
449{
450 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
451 * for thread ids on PowerPC. If it doesn't exist fallback to
452 * standard "reg" property.
453 */
454 if (IS_ENABLED(CONFIG_PPC) &&
455 __of_find_n_match_cpu_property(cpun,
456 "ibm,ppc-interrupt-server#s",
457 cpu, thread))
458 return true;
459
460 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
461 return true;
462
463 return false;
464}
465
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100466/**
467 * of_get_cpu_node - Get device node associated with the given logical CPU
468 *
469 * @cpu: CPU number(logical index) for which device node is required
470 * @thread: if not NULL, local thread number within the physical core is
471 * returned
472 *
473 * The main purpose of this function is to retrieve the device node for the
474 * given logical CPU index. It should be used to initialize the of_node in
475 * cpu device. Once of_node in cpu device is populated, all the further
476 * references can use that instead.
477 *
478 * CPU logical to physical index mapping is architecture specific and is built
479 * before booting secondary cores. This function uses arch_match_cpu_phys_id
480 * which can be overridden by architecture specific implementation.
481 *
482 * Returns a node pointer for the logical cpu if found, else NULL.
483 */
484struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
485{
David Millerd1cb9d12013-10-03 17:24:51 -0400486 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100487
David Millerd1cb9d12013-10-03 17:24:51 -0400488 for_each_node_by_type(cpun, "cpu") {
489 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100490 return cpun;
491 }
492 return NULL;
493}
494EXPORT_SYMBOL(of_get_cpu_node);
495
Kevin Hao215a14c2014-02-19 16:15:45 +0800496/**
497 * __of_device_is_compatible() - Check if the node matches given constraints
498 * @device: pointer to node
499 * @compat: required compatible string, NULL or "" for any match
500 * @type: required device_type value, NULL or "" for any match
501 * @name: required node name, NULL or "" for any match
502 *
503 * Checks if the given @compat, @type and @name strings match the
504 * properties of the given @device. A constraints can be skipped by
505 * passing NULL or an empty string as the constraint.
506 *
507 * Returns 0 for no match, and a positive integer on match. The return
508 * value is a relative score with larger values indicating better
509 * matches. The score is weighted for the most specific compatible value
510 * to get the highest score. Matching type is next, followed by matching
511 * name. Practically speaking, this results in the following priority
512 * order for matches:
513 *
514 * 1. specific compatible && type && name
515 * 2. specific compatible && type
516 * 3. specific compatible && name
517 * 4. specific compatible
518 * 5. general compatible && type && name
519 * 6. general compatible && type
520 * 7. general compatible && name
521 * 8. general compatible
522 * 9. type && name
523 * 10. type
524 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000525 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500526static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800527 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000528{
Kevin Hao215a14c2014-02-19 16:15:45 +0800529 struct property *prop;
530 const char *cp;
531 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000532
Kevin Hao215a14c2014-02-19 16:15:45 +0800533 /* Compatible match has highest priority */
534 if (compat && compat[0]) {
535 prop = __of_find_property(device, "compatible", NULL);
536 for (cp = of_prop_next_string(prop, NULL); cp;
537 cp = of_prop_next_string(prop, cp), index++) {
538 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
539 score = INT_MAX/2 - (index << 2);
540 break;
541 }
542 }
543 if (!score)
544 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000545 }
546
Kevin Hao215a14c2014-02-19 16:15:45 +0800547 /* Matching type is better than matching name */
548 if (type && type[0]) {
549 if (!device->type || of_node_cmp(type, device->type))
550 return 0;
551 score += 2;
552 }
553
554 /* Matching name is a bit better than not */
555 if (name && name[0]) {
556 if (!device->name || of_node_cmp(name, device->name))
557 return 0;
558 score++;
559 }
560
561 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000562}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500563
564/** Checks if the given "compat" string matches one of the strings in
565 * the device's "compatible" property
566 */
567int of_device_is_compatible(const struct device_node *device,
568 const char *compat)
569{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500570 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500571 int res;
572
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500573 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800574 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500575 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500576 return res;
577}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000578EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000579
580/**
Grant Likely71a157e2010-02-01 21:34:14 -0700581 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700582 * @compat: compatible string to look for in root node's compatible property.
583 *
584 * Returns true if the root node has the given value in its
585 * compatible property.
586 */
Grant Likely71a157e2010-02-01 21:34:14 -0700587int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700588{
589 struct device_node *root;
590 int rc = 0;
591
592 root = of_find_node_by_path("/");
593 if (root) {
594 rc = of_device_is_compatible(root, compat);
595 of_node_put(root);
596 }
597 return rc;
598}
Grant Likely71a157e2010-02-01 21:34:14 -0700599EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700600
601/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700602 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100603 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700604 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100605 *
606 * Returns 1 if the status property is absent or set to "okay" or "ok",
607 * 0 otherwise
608 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700609static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100610{
611 const char *status;
612 int statlen;
613
Xiubo Li42ccd782014-01-13 11:07:28 +0800614 if (!device)
615 return 0;
616
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700617 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100618 if (status == NULL)
619 return 1;
620
621 if (statlen > 0) {
622 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
623 return 1;
624 }
625
626 return 0;
627}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700628
629/**
630 * of_device_is_available - check if a device is available for use
631 *
632 * @device: Node to check for availability
633 *
634 * Returns 1 if the status property is absent or set to "okay" or "ok",
635 * 0 otherwise
636 */
637int of_device_is_available(const struct device_node *device)
638{
639 unsigned long flags;
640 int res;
641
642 raw_spin_lock_irqsave(&devtree_lock, flags);
643 res = __of_device_is_available(device);
644 raw_spin_unlock_irqrestore(&devtree_lock, flags);
645 return res;
646
647}
Josh Boyer834d97d2008-03-27 00:33:14 +1100648EXPORT_SYMBOL(of_device_is_available);
649
650/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000651 * of_get_parent - Get a node's parent if any
652 * @node: Node to get parent
653 *
654 * Returns a node pointer with refcount incremented, use
655 * of_node_put() on it when done.
656 */
657struct device_node *of_get_parent(const struct device_node *node)
658{
659 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500660 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000661
662 if (!node)
663 return NULL;
664
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500665 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000666 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500667 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000668 return np;
669}
670EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000671
672/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000673 * of_get_next_parent - Iterate to a node's parent
674 * @node: Node to get parent of
675 *
676 * This is like of_get_parent() except that it drops the
677 * refcount on the passed node, making it suitable for iterating
678 * through a node's parents.
679 *
680 * Returns a node pointer with refcount incremented, use
681 * of_node_put() on it when done.
682 */
683struct device_node *of_get_next_parent(struct device_node *node)
684{
685 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500686 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000687
688 if (!node)
689 return NULL;
690
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500691 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000692 parent = of_node_get(node->parent);
693 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500694 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000695 return parent;
696}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300697EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000698
Grant Likely0d0e02d2014-05-22 01:04:17 +0900699static struct device_node *__of_get_next_child(const struct device_node *node,
700 struct device_node *prev)
701{
702 struct device_node *next;
703
Florian Fainelli43cb4362014-05-28 10:39:02 -0700704 if (!node)
705 return NULL;
706
Grant Likely0d0e02d2014-05-22 01:04:17 +0900707 next = prev ? prev->sibling : node->child;
708 for (; next; next = next->sibling)
709 if (of_node_get(next))
710 break;
711 of_node_put(prev);
712 return next;
713}
714#define __for_each_child_of_node(parent, child) \
715 for (child = __of_get_next_child(parent, NULL); child != NULL; \
716 child = __of_get_next_child(parent, child))
717
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000718/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000719 * of_get_next_child - Iterate a node childs
720 * @node: parent node
721 * @prev: previous child of the parent node, or NULL to get first
722 *
723 * Returns a node pointer with refcount incremented, use
724 * of_node_put() on it when done.
725 */
726struct device_node *of_get_next_child(const struct device_node *node,
727 struct device_node *prev)
728{
729 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500730 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000731
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500732 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900733 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500734 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000735 return next;
736}
737EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000738
739/**
Timur Tabi32961932012-08-14 13:20:23 +0000740 * of_get_next_available_child - Find the next available child node
741 * @node: parent node
742 * @prev: previous child of the parent node, or NULL to get first
743 *
744 * This function is like of_get_next_child(), except that it
745 * automatically skips any disabled nodes (i.e. status = "disabled").
746 */
747struct device_node *of_get_next_available_child(const struct device_node *node,
748 struct device_node *prev)
749{
750 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000751 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000752
Florian Fainelli43cb4362014-05-28 10:39:02 -0700753 if (!node)
754 return NULL;
755
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000756 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000757 next = prev ? prev->sibling : node->child;
758 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700759 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000760 continue;
761 if (of_node_get(next))
762 break;
763 }
764 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000765 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000766 return next;
767}
768EXPORT_SYMBOL(of_get_next_available_child);
769
770/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100771 * of_get_child_by_name - Find the child node by name for a given parent
772 * @node: parent node
773 * @name: child name to look for.
774 *
775 * This function looks for child node for given matching name
776 *
777 * Returns a node pointer if found, with refcount incremented, use
778 * of_node_put() on it when done.
779 * Returns NULL if node is not found.
780 */
781struct device_node *of_get_child_by_name(const struct device_node *node,
782 const char *name)
783{
784 struct device_node *child;
785
786 for_each_child_of_node(node, child)
787 if (child->name && (of_node_cmp(child->name, name) == 0))
788 break;
789 return child;
790}
791EXPORT_SYMBOL(of_get_child_by_name);
792
Grant Likelyc22e6502014-03-14 17:07:12 +0000793static struct device_node *__of_find_node_by_path(struct device_node *parent,
794 const char *path)
795{
796 struct device_node *child;
797 int len = strchrnul(path, '/') - path;
798
799 if (!len)
800 return NULL;
801
802 __for_each_child_of_node(parent, child) {
803 const char *name = strrchr(child->full_name, '/');
804 if (WARN(!name, "malformed device_node %s\n", child->full_name))
805 continue;
806 name++;
807 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
808 return child;
809 }
810 return NULL;
811}
812
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100813/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000814 * of_find_node_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000815 * @path: Either the full path to match, or if the path does not
816 * start with '/', the name of a property of the /aliases
817 * node (an alias). In the case of an alias, the node
818 * matching the alias' value will be returned.
819 *
820 * Valid paths:
821 * /foo/bar Full path
822 * foo Valid alias
823 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000824 *
825 * Returns a node pointer with refcount incremented, use
826 * of_node_put() on it when done.
827 */
828struct device_node *of_find_node_by_path(const char *path)
829{
Grant Likelyc22e6502014-03-14 17:07:12 +0000830 struct device_node *np = NULL;
831 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500832 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000833
Grant Likelyc22e6502014-03-14 17:07:12 +0000834 if (strcmp(path, "/") == 0)
835 return of_node_get(of_allnodes);
836
837 /* The path could begin with an alias */
838 if (*path != '/') {
839 char *p = strchrnul(path, '/');
840 int len = p - path;
841
842 /* of_aliases must not be NULL */
843 if (!of_aliases)
844 return NULL;
845
846 for_each_property_of_node(of_aliases, pp) {
847 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
848 np = of_find_node_by_path(pp->value);
849 break;
850 }
851 }
852 if (!np)
853 return NULL;
854 path = p;
855 }
856
857 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500858 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000859 if (!np)
860 np = of_node_get(of_allnodes);
861 while (np && *path == '/') {
862 path++; /* Increment past '/' delimiter */
863 np = __of_find_node_by_path(np, path);
864 path = strchrnul(path, '/');
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000865 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500866 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000867 return np;
868}
869EXPORT_SYMBOL(of_find_node_by_path);
870
871/**
872 * of_find_node_by_name - Find a node by its "name" property
873 * @from: The node to start searching from or NULL, the node
874 * you pass will not be searched, only the next one
875 * will; typically, you pass what the previous call
876 * returned. of_node_put() will be called on it
877 * @name: The name string to match against
878 *
879 * Returns a node pointer with refcount incremented, use
880 * of_node_put() on it when done.
881 */
882struct device_node *of_find_node_by_name(struct device_node *from,
883 const char *name)
884{
885 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500886 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000887
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500888 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000889 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000890 for (; np; np = np->allnext)
891 if (np->name && (of_node_cmp(np->name, name) == 0)
892 && of_node_get(np))
893 break;
894 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500895 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000896 return np;
897}
898EXPORT_SYMBOL(of_find_node_by_name);
899
900/**
901 * of_find_node_by_type - Find a node by its "device_type" property
902 * @from: The node to start searching from, or NULL to start searching
903 * the entire device tree. The node you pass will not be
904 * searched, only the next one will; typically, you pass
905 * what the previous call returned. of_node_put() will be
906 * called on from for you.
907 * @type: The type string to match against
908 *
909 * Returns a node pointer with refcount incremented, use
910 * of_node_put() on it when done.
911 */
912struct device_node *of_find_node_by_type(struct device_node *from,
913 const char *type)
914{
915 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500916 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000917
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500918 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000919 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000920 for (; np; np = np->allnext)
921 if (np->type && (of_node_cmp(np->type, type) == 0)
922 && of_node_get(np))
923 break;
924 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500925 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000926 return np;
927}
928EXPORT_SYMBOL(of_find_node_by_type);
929
930/**
931 * of_find_compatible_node - Find a node based on type and one of the
932 * tokens in its "compatible" property
933 * @from: The node to start searching from or NULL, the node
934 * you pass will not be searched, only the next one
935 * will; typically, you pass what the previous call
936 * returned. of_node_put() will be called on it
937 * @type: The type string to match "device_type" or NULL to ignore
938 * @compatible: The string to match to one of the tokens in the device
939 * "compatible" list.
940 *
941 * Returns a node pointer with refcount incremented, use
942 * of_node_put() on it when done.
943 */
944struct device_node *of_find_compatible_node(struct device_node *from,
945 const char *type, const char *compatible)
946{
947 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500948 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000949
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500950 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000951 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000952 for (; np; np = np->allnext) {
Kevin Hao215a14c2014-02-19 16:15:45 +0800953 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500954 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000955 break;
956 }
957 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500958 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000959 return np;
960}
961EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100962
963/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000964 * of_find_node_with_property - Find a node which has a property with
965 * the given name.
966 * @from: The node to start searching from or NULL, the node
967 * you pass will not be searched, only the next one
968 * will; typically, you pass what the previous call
969 * returned. of_node_put() will be called on it
970 * @prop_name: The name of the property to look for.
971 *
972 * Returns a node pointer with refcount incremented, use
973 * of_node_put() on it when done.
974 */
975struct device_node *of_find_node_with_property(struct device_node *from,
976 const char *prop_name)
977{
978 struct device_node *np;
979 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500980 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000981
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500982 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000983 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000984 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530985 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000986 if (of_prop_cmp(pp->name, prop_name) == 0) {
987 of_node_get(np);
988 goto out;
989 }
990 }
991 }
992out:
993 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500994 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000995 return np;
996}
997EXPORT_SYMBOL(of_find_node_with_property);
998
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500999static
1000const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1001 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +11001002{
Kevin Hao215a14c2014-02-19 16:15:45 +08001003 const struct of_device_id *best_match = NULL;
1004 int score, best_score = 0;
1005
Grant Likelya52f07e2011-03-18 10:21:29 -06001006 if (!matches)
1007 return NULL;
1008
Kevin Hao215a14c2014-02-19 16:15:45 +08001009 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1010 score = __of_device_is_compatible(node, matches->compatible,
1011 matches->type, matches->name);
1012 if (score > best_score) {
1013 best_match = matches;
1014 best_score = score;
1015 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +08001016 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001017
1018 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001019}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001020
1021/**
1022 * of_match_node - Tell if an device_node has a matching of_match structure
1023 * @matches: array of of device match structures to search in
1024 * @node: the of device structure to match against
1025 *
Kevin Hao71c54982014-02-18 15:57:29 +08001026 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001027 */
1028const struct of_device_id *of_match_node(const struct of_device_id *matches,
1029 const struct device_node *node)
1030{
1031 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001032 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001033
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001034 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001035 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001036 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001037 return match;
1038}
Grant Likely283029d2008-01-09 06:20:40 +11001039EXPORT_SYMBOL(of_match_node);
1040
1041/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001042 * of_find_matching_node_and_match - Find a node based on an of_device_id
1043 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001044 * @from: The node to start searching from or NULL, the node
1045 * you pass will not be searched, only the next one
1046 * will; typically, you pass what the previous call
1047 * returned. of_node_put() will be called on it
1048 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001049 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001050 *
1051 * Returns a node pointer with refcount incremented, use
1052 * of_node_put() on it when done.
1053 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001054struct device_node *of_find_matching_node_and_match(struct device_node *from,
1055 const struct of_device_id *matches,
1056 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001057{
1058 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001059 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001060 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001061
Stephen Warren50c8af42012-11-20 16:12:20 -07001062 if (match)
1063 *match = NULL;
1064
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001065 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001066 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +11001067 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001068 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001069 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001070 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001071 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001072 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001073 }
Grant Likely283029d2008-01-09 06:20:40 +11001074 }
1075 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001076 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001077 return np;
1078}
Grant Likely80c20222012-12-19 10:45:36 +00001079EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001080
1081/**
Grant Likely3f07af42008-07-25 22:25:13 -04001082 * of_modalias_node - Lookup appropriate modalias for a device node
1083 * @node: pointer to a device tree node
1084 * @modalias: Pointer to buffer that modalias value will be copied into
1085 * @len: Length of modalias value
1086 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001087 * Based on the value of the compatible property, this routine will attempt
1088 * to choose an appropriate modalias value for a particular device tree node.
1089 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1090 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001091 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001092 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001093 */
1094int of_modalias_node(struct device_node *node, char *modalias, int len)
1095{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001096 const char *compatible, *p;
1097 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001098
1099 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001100 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001101 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001102 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001103 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001104 return 0;
1105}
1106EXPORT_SYMBOL_GPL(of_modalias_node);
1107
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001108/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001109 * of_find_node_by_phandle - Find a node given a phandle
1110 * @handle: phandle of the node to find
1111 *
1112 * Returns a node pointer with refcount incremented, use
1113 * of_node_put() on it when done.
1114 */
1115struct device_node *of_find_node_by_phandle(phandle handle)
1116{
1117 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001118 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001119
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001120 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001121 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001122 if (np->phandle == handle)
1123 break;
1124 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001125 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001126 return np;
1127}
1128EXPORT_SYMBOL(of_find_node_by_phandle);
1129
1130/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +01001131 * of_property_count_elems_of_size - Count the number of elements in a property
1132 *
1133 * @np: device node from which the property value is to be read.
1134 * @propname: name of the property to be searched.
1135 * @elem_size: size of the individual element
1136 *
1137 * Search for a property in a device node and count the number of elements of
1138 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1139 * property does not exist or its length does not match a multiple of elem_size
1140 * and -ENODATA if the property does not have a value.
1141 */
1142int of_property_count_elems_of_size(const struct device_node *np,
1143 const char *propname, int elem_size)
1144{
1145 struct property *prop = of_find_property(np, propname, NULL);
1146
1147 if (!prop)
1148 return -EINVAL;
1149 if (!prop->value)
1150 return -ENODATA;
1151
1152 if (prop->length % elem_size != 0) {
1153 pr_err("size of %s in node %s is not a multiple of %d\n",
1154 propname, np->full_name, elem_size);
1155 return -EINVAL;
1156 }
1157
1158 return prop->length / elem_size;
1159}
1160EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1161
1162/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001163 * of_find_property_value_of_size
1164 *
1165 * @np: device node from which the property value is to be read.
1166 * @propname: name of the property to be searched.
1167 * @len: requested length of property value
1168 *
1169 * Search for a property in a device node and valid the requested size.
1170 * Returns the property value on success, -EINVAL if the property does not
1171 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1172 * property data isn't large enough.
1173 *
1174 */
1175static void *of_find_property_value_of_size(const struct device_node *np,
1176 const char *propname, u32 len)
1177{
1178 struct property *prop = of_find_property(np, propname, NULL);
1179
1180 if (!prop)
1181 return ERR_PTR(-EINVAL);
1182 if (!prop->value)
1183 return ERR_PTR(-ENODATA);
1184 if (len > prop->length)
1185 return ERR_PTR(-EOVERFLOW);
1186
1187 return prop->value;
1188}
1189
1190/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001191 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1192 *
1193 * @np: device node from which the property value is to be read.
1194 * @propname: name of the property to be searched.
1195 * @index: index of the u32 in the list of values
1196 * @out_value: pointer to return value, modified only if no error.
1197 *
1198 * Search for a property in a device node and read nth 32-bit value from
1199 * it. Returns 0 on success, -EINVAL if the property does not exist,
1200 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1201 * property data isn't large enough.
1202 *
1203 * The out_value is modified only if a valid u32 value can be decoded.
1204 */
1205int of_property_read_u32_index(const struct device_node *np,
1206 const char *propname,
1207 u32 index, u32 *out_value)
1208{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001209 const u32 *val = of_find_property_value_of_size(np, propname,
1210 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +13001211
Tony Priskdaeec1f2013-04-03 17:57:11 +13001212 if (IS_ERR(val))
1213 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001214
Tony Priskdaeec1f2013-04-03 17:57:11 +13001215 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001216 return 0;
1217}
1218EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1219
1220/**
Viresh Kumarbe193242012-11-20 10:15:19 +05301221 * of_property_read_u8_array - Find and read an array of u8 from a property.
1222 *
1223 * @np: device node from which the property value is to be read.
1224 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301225 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301226 * @sz: number of array elements to read
1227 *
1228 * Search for a property in a device node and read 8-bit value(s) from
1229 * it. Returns 0 on success, -EINVAL if the property does not exist,
1230 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1231 * property data isn't large enough.
1232 *
1233 * dts entry of array should be like:
1234 * property = /bits/ 8 <0x50 0x60 0x70>;
1235 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301236 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301237 */
1238int of_property_read_u8_array(const struct device_node *np,
1239 const char *propname, u8 *out_values, size_t sz)
1240{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001241 const u8 *val = of_find_property_value_of_size(np, propname,
1242 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301243
Tony Priskdaeec1f2013-04-03 17:57:11 +13001244 if (IS_ERR(val))
1245 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301246
Viresh Kumarbe193242012-11-20 10:15:19 +05301247 while (sz--)
1248 *out_values++ = *val++;
1249 return 0;
1250}
1251EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1252
1253/**
1254 * of_property_read_u16_array - Find and read an array of u16 from a property.
1255 *
1256 * @np: device node from which the property value is to be read.
1257 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301258 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301259 * @sz: number of array elements to read
1260 *
1261 * Search for a property in a device node and read 16-bit value(s) from
1262 * it. Returns 0 on success, -EINVAL if the property does not exist,
1263 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1264 * property data isn't large enough.
1265 *
1266 * dts entry of array should be like:
1267 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1268 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301269 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301270 */
1271int of_property_read_u16_array(const struct device_node *np,
1272 const char *propname, u16 *out_values, size_t sz)
1273{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001274 const __be16 *val = of_find_property_value_of_size(np, propname,
1275 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301276
Tony Priskdaeec1f2013-04-03 17:57:11 +13001277 if (IS_ERR(val))
1278 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301279
Viresh Kumarbe193242012-11-20 10:15:19 +05301280 while (sz--)
1281 *out_values++ = be16_to_cpup(val++);
1282 return 0;
1283}
1284EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1285
1286/**
Rob Herring0e373632011-07-06 15:42:58 -05001287 * of_property_read_u32_array - Find and read an array of 32 bit integers
1288 * from a property.
1289 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301290 * @np: device node from which the property value is to be read.
1291 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301292 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301293 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301294 *
Rob Herring0e373632011-07-06 15:42:58 -05001295 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301296 * it. Returns 0 on success, -EINVAL if the property does not exist,
1297 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1298 * property data isn't large enough.
1299 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301300 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301301 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001302int of_property_read_u32_array(const struct device_node *np,
1303 const char *propname, u32 *out_values,
1304 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301305{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001306 const __be32 *val = of_find_property_value_of_size(np, propname,
1307 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301308
Tony Priskdaeec1f2013-04-03 17:57:11 +13001309 if (IS_ERR(val))
1310 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001311
Rob Herring0e373632011-07-06 15:42:58 -05001312 while (sz--)
1313 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301314 return 0;
1315}
Rob Herring0e373632011-07-06 15:42:58 -05001316EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301317
1318/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001319 * of_property_read_u64 - Find and read a 64 bit integer from a property
1320 * @np: device node from which the property value is to be read.
1321 * @propname: name of the property to be searched.
1322 * @out_value: pointer to return value, modified only if return value is 0.
1323 *
1324 * Search for a property in a device node and read a 64-bit value from
1325 * it. Returns 0 on success, -EINVAL if the property does not exist,
1326 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1327 * property data isn't large enough.
1328 *
1329 * The out_value is modified only if a valid u64 value can be decoded.
1330 */
1331int of_property_read_u64(const struct device_node *np, const char *propname,
1332 u64 *out_value)
1333{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001334 const __be32 *val = of_find_property_value_of_size(np, propname,
1335 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001336
Tony Priskdaeec1f2013-04-03 17:57:11 +13001337 if (IS_ERR(val))
1338 return PTR_ERR(val);
1339
1340 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001341 return 0;
1342}
1343EXPORT_SYMBOL_GPL(of_property_read_u64);
1344
1345/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301346 * of_property_read_string - Find and read a string from a property
1347 * @np: device node from which the property value is to be read.
1348 * @propname: name of the property to be searched.
1349 * @out_string: pointer to null terminated return string, modified only if
1350 * return value is 0.
1351 *
1352 * Search for a property in a device tree node and retrieve a null
1353 * terminated string value (pointer to data, not a copy). Returns 0 on
1354 * success, -EINVAL if the property does not exist, -ENODATA if property
1355 * does not have a value, and -EILSEQ if the string is not null-terminated
1356 * within the length of the property data.
1357 *
1358 * The out_string pointer is modified only if a valid string can be decoded.
1359 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001360int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001361 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301362{
1363 struct property *prop = of_find_property(np, propname, NULL);
1364 if (!prop)
1365 return -EINVAL;
1366 if (!prop->value)
1367 return -ENODATA;
1368 if (strnlen(prop->value, prop->length) >= prop->length)
1369 return -EILSEQ;
1370 *out_string = prop->value;
1371 return 0;
1372}
1373EXPORT_SYMBOL_GPL(of_property_read_string);
1374
1375/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001376 * of_property_read_string_index - Find and read a string from a multiple
1377 * strings property.
1378 * @np: device node from which the property value is to be read.
1379 * @propname: name of the property to be searched.
1380 * @index: index of the string in the list of strings
1381 * @out_string: pointer to null terminated return string, modified only if
1382 * return value is 0.
1383 *
1384 * Search for a property in a device tree node and retrieve a null
1385 * terminated string value (pointer to data, not a copy) in the list of strings
1386 * contained in that property.
1387 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1388 * property does not have a value, and -EILSEQ if the string is not
1389 * null-terminated within the length of the property data.
1390 *
1391 * The out_string pointer is modified only if a valid string can be decoded.
1392 */
1393int of_property_read_string_index(struct device_node *np, const char *propname,
1394 int index, const char **output)
1395{
1396 struct property *prop = of_find_property(np, propname, NULL);
1397 int i = 0;
1398 size_t l = 0, total = 0;
1399 const char *p;
1400
1401 if (!prop)
1402 return -EINVAL;
1403 if (!prop->value)
1404 return -ENODATA;
1405 if (strnlen(prop->value, prop->length) >= prop->length)
1406 return -EILSEQ;
1407
1408 p = prop->value;
1409
1410 for (i = 0; total < prop->length; total += l, p += l) {
1411 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001412 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001413 *output = p;
1414 return 0;
1415 }
1416 }
1417 return -ENODATA;
1418}
1419EXPORT_SYMBOL_GPL(of_property_read_string_index);
1420
Grant Likely7aff0fe2011-12-12 09:25:58 -07001421/**
1422 * of_property_match_string() - Find string in a list and return index
1423 * @np: pointer to node containing string list property
1424 * @propname: string list property name
1425 * @string: pointer to string to search for in string list
1426 *
1427 * This function searches a string list property and returns the index
1428 * of a specific string value.
1429 */
1430int of_property_match_string(struct device_node *np, const char *propname,
1431 const char *string)
1432{
1433 struct property *prop = of_find_property(np, propname, NULL);
1434 size_t l;
1435 int i;
1436 const char *p, *end;
1437
1438 if (!prop)
1439 return -EINVAL;
1440 if (!prop->value)
1441 return -ENODATA;
1442
1443 p = prop->value;
1444 end = p + prop->length;
1445
1446 for (i = 0; p < end; i++, p += l) {
1447 l = strlen(p) + 1;
1448 if (p + l > end)
1449 return -EILSEQ;
1450 pr_debug("comparing %s with %s\n", string, p);
1451 if (strcmp(string, p) == 0)
1452 return i; /* Found it; return index */
1453 }
1454 return -ENODATA;
1455}
1456EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001457
1458/**
1459 * of_property_count_strings - Find and return the number of strings from a
1460 * multiple strings property.
1461 * @np: device node from which the property value is to be read.
1462 * @propname: name of the property to be searched.
1463 *
1464 * Search for a property in a device tree node and retrieve the number of null
1465 * terminated string contain in it. Returns the number of strings on
1466 * success, -EINVAL if the property does not exist, -ENODATA if property
1467 * does not have a value, and -EILSEQ if the string is not null-terminated
1468 * within the length of the property data.
1469 */
1470int of_property_count_strings(struct device_node *np, const char *propname)
1471{
1472 struct property *prop = of_find_property(np, propname, NULL);
1473 int i = 0;
1474 size_t l = 0, total = 0;
1475 const char *p;
1476
1477 if (!prop)
1478 return -EINVAL;
1479 if (!prop->value)
1480 return -ENODATA;
1481 if (strnlen(prop->value, prop->length) >= prop->length)
1482 return -EILSEQ;
1483
1484 p = prop->value;
1485
Benoit Cousson88af7f52011-12-05 15:23:54 +01001486 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001487 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001488
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001489 return i;
1490}
1491EXPORT_SYMBOL_GPL(of_property_count_strings);
1492
Grant Likely624cfca2013-10-11 22:05:10 +01001493void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1494{
1495 int i;
1496 printk("%s %s", msg, of_node_full_name(args->np));
1497 for (i = 0; i < args->args_count; i++)
1498 printk(i ? ",%08x" : ":%08x", args->args[i]);
1499 printk("\n");
1500}
1501
Grant Likelybd69f732013-02-10 22:57:21 +00001502static int __of_parse_phandle_with_args(const struct device_node *np,
1503 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001504 const char *cells_name,
1505 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001506 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001507{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001508 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001509 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001510 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001511 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001512 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001513
Grant Likely15c9a0a2011-12-12 09:25:57 -07001514 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001515 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001516 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001517 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001518 list_end = list + size / sizeof(*list);
1519
Grant Likely15c9a0a2011-12-12 09:25:57 -07001520 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001521 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001522 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001523 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001524
Grant Likely15c9a0a2011-12-12 09:25:57 -07001525 /*
1526 * If phandle is 0, then it is an empty entry with no
1527 * arguments. Skip forward to the next entry.
1528 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001529 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001530 if (phandle) {
1531 /*
1532 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001533 * property to determine the argument length.
1534 *
1535 * This is not needed if the cell count is hard-coded
1536 * (i.e. cells_name not set, but cell_count is set),
1537 * except when we're going to return the found node
1538 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001539 */
Stephen Warren91d99422013-08-14 15:27:11 -06001540 if (cells_name || cur_index == index) {
1541 node = of_find_node_by_phandle(phandle);
1542 if (!node) {
1543 pr_err("%s: could not find phandle\n",
1544 np->full_name);
1545 goto err;
1546 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001547 }
Stephen Warren035fd942013-08-14 15:27:10 -06001548
1549 if (cells_name) {
1550 if (of_property_read_u32(node, cells_name,
1551 &count)) {
1552 pr_err("%s: could not get %s for %s\n",
1553 np->full_name, cells_name,
1554 node->full_name);
1555 goto err;
1556 }
1557 } else {
1558 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001559 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001560
Grant Likely15c9a0a2011-12-12 09:25:57 -07001561 /*
1562 * Make sure that the arguments actually fit in the
1563 * remaining property data length
1564 */
1565 if (list + count > list_end) {
1566 pr_err("%s: arguments longer than property\n",
1567 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001568 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001569 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001570 }
1571
Grant Likely15c9a0a2011-12-12 09:25:57 -07001572 /*
1573 * All of the error cases above bail out of the loop, so at
1574 * this point, the parsing is successful. If the requested
1575 * index matches, then fill the out_args structure and return,
1576 * or return -ENOENT for an empty entry.
1577 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001578 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001579 if (cur_index == index) {
1580 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001581 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001582
Grant Likely15c9a0a2011-12-12 09:25:57 -07001583 if (out_args) {
1584 int i;
1585 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1586 count = MAX_PHANDLE_ARGS;
1587 out_args->np = node;
1588 out_args->args_count = count;
1589 for (i = 0; i < count; i++)
1590 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001591 } else {
1592 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001593 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001594
1595 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001596 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001597 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001598
1599 of_node_put(node);
1600 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001601 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001602 cur_index++;
1603 }
1604
Grant Likely23ce04c2013-02-12 21:21:49 +00001605 /*
1606 * Unlock node before returning result; will be one of:
1607 * -ENOENT : index is for empty phandle
1608 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001609 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001610 */
Grant Likelybd69f732013-02-10 22:57:21 +00001611 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001612 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001613 if (node)
1614 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001615 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001616}
Grant Likelybd69f732013-02-10 22:57:21 +00001617
Stephen Warreneded9dd2013-08-14 15:27:08 -06001618/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001619 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1620 * @np: Pointer to device node holding phandle property
1621 * @phandle_name: Name of property holding a phandle value
1622 * @index: For properties holding a table of phandles, this is the index into
1623 * the table
1624 *
1625 * Returns the device_node pointer with refcount incremented. Use
1626 * of_node_put() on it when done.
1627 */
1628struct device_node *of_parse_phandle(const struct device_node *np,
1629 const char *phandle_name, int index)
1630{
Stephen Warren91d99422013-08-14 15:27:11 -06001631 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001632
Stephen Warren91d99422013-08-14 15:27:11 -06001633 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001634 return NULL;
1635
Stephen Warren91d99422013-08-14 15:27:11 -06001636 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1637 index, &args))
1638 return NULL;
1639
1640 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001641}
1642EXPORT_SYMBOL(of_parse_phandle);
1643
1644/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001645 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1646 * @np: pointer to a device tree node containing a list
1647 * @list_name: property name that contains a list
1648 * @cells_name: property name that specifies phandles' arguments count
1649 * @index: index of a phandle to parse out
1650 * @out_args: optional pointer to output arguments structure (will be filled)
1651 *
1652 * This function is useful to parse lists of phandles and their arguments.
1653 * Returns 0 on success and fills out_args, on error returns appropriate
1654 * errno value.
1655 *
1656 * Caller is responsible to call of_node_put() on the returned out_args->node
1657 * pointer.
1658 *
1659 * Example:
1660 *
1661 * phandle1: node1 {
1662 * #list-cells = <2>;
1663 * }
1664 *
1665 * phandle2: node2 {
1666 * #list-cells = <1>;
1667 * }
1668 *
1669 * node3 {
1670 * list = <&phandle1 1 2 &phandle2 3>;
1671 * }
1672 *
1673 * To get a device_node of the `node2' node you may call this:
1674 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1675 */
Grant Likelybd69f732013-02-10 22:57:21 +00001676int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1677 const char *cells_name, int index,
1678 struct of_phandle_args *out_args)
1679{
1680 if (index < 0)
1681 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001682 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1683 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001684}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001685EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001686
Grant Likelybd69f732013-02-10 22:57:21 +00001687/**
Stephen Warren035fd942013-08-14 15:27:10 -06001688 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1689 * @np: pointer to a device tree node containing a list
1690 * @list_name: property name that contains a list
1691 * @cell_count: number of argument cells following the phandle
1692 * @index: index of a phandle to parse out
1693 * @out_args: optional pointer to output arguments structure (will be filled)
1694 *
1695 * This function is useful to parse lists of phandles and their arguments.
1696 * Returns 0 on success and fills out_args, on error returns appropriate
1697 * errno value.
1698 *
1699 * Caller is responsible to call of_node_put() on the returned out_args->node
1700 * pointer.
1701 *
1702 * Example:
1703 *
1704 * phandle1: node1 {
1705 * }
1706 *
1707 * phandle2: node2 {
1708 * }
1709 *
1710 * node3 {
1711 * list = <&phandle1 0 2 &phandle2 2 3>;
1712 * }
1713 *
1714 * To get a device_node of the `node2' node you may call this:
1715 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1716 */
1717int of_parse_phandle_with_fixed_args(const struct device_node *np,
1718 const char *list_name, int cell_count,
1719 int index, struct of_phandle_args *out_args)
1720{
1721 if (index < 0)
1722 return -EINVAL;
1723 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1724 index, out_args);
1725}
1726EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1727
1728/**
Grant Likelybd69f732013-02-10 22:57:21 +00001729 * of_count_phandle_with_args() - Find the number of phandles references in a property
1730 * @np: pointer to a device tree node containing a list
1731 * @list_name: property name that contains a list
1732 * @cells_name: property name that specifies phandles' arguments count
1733 *
1734 * Returns the number of phandle + argument tuples within a property. It
1735 * is a typical pattern to encode a list of phandle and variable
1736 * arguments into a single property. The number of arguments is encoded
1737 * by a property in the phandle-target node. For example, a gpios
1738 * property would contain a list of GPIO specifies consisting of a
1739 * phandle and 1 or more arguments. The number of arguments are
1740 * determined by the #gpio-cells property in the node pointed to by the
1741 * phandle.
1742 */
1743int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1744 const char *cells_name)
1745{
Stephen Warren035fd942013-08-14 15:27:10 -06001746 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1747 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001748}
1749EXPORT_SYMBOL(of_count_phandle_with_args);
1750
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001751#if defined(CONFIG_OF_DYNAMIC)
1752static int of_property_notify(int action, struct device_node *np,
1753 struct property *prop)
1754{
1755 struct of_prop_reconfig pr;
1756
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001757 /* only call notifiers if the node is attached */
1758 if (!of_node_is_attached(np))
1759 return 0;
1760
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001761 pr.dn = np;
1762 pr.prop = prop;
1763 return of_reconfig_notify(action, &pr);
1764}
1765#else
1766static int of_property_notify(int action, struct device_node *np,
1767 struct property *prop)
1768{
1769 return 0;
1770}
1771#endif
1772
Grant Likely02af11b2009-11-23 20:16:45 -07001773/**
Xiubo Li62664f62014-01-22 13:57:39 +08001774 * __of_add_property - Add a property to a node without lock operations
1775 */
1776static int __of_add_property(struct device_node *np, struct property *prop)
1777{
1778 struct property **next;
1779
1780 prop->next = NULL;
1781 next = &np->properties;
1782 while (*next) {
1783 if (strcmp(prop->name, (*next)->name) == 0)
1784 /* duplicate ! don't insert it */
1785 return -EEXIST;
1786
1787 next = &(*next)->next;
1788 }
1789 *next = prop;
1790
1791 return 0;
1792}
1793
1794/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001795 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001796 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001797int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001798{
Grant Likely02af11b2009-11-23 20:16:45 -07001799 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001800 int rc;
1801
1802 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1803 if (rc)
1804 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001805
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001806 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001807 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001808 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001809 if (rc)
1810 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001811
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001812 if (of_node_is_attached(np))
1813 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001814
Xiubo Li62664f62014-01-22 13:57:39 +08001815 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001816}
1817
1818/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001819 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001820 *
1821 * Note that we don't actually remove it, since we have given out
1822 * who-knows-how-many pointers to the data using get-property.
1823 * Instead we just move the property to the "dead properties"
1824 * list, so it won't be found any more.
1825 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001826int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001827{
1828 struct property **next;
1829 unsigned long flags;
1830 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001831 int rc;
1832
1833 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1834 if (rc)
1835 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001836
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001837 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001838 next = &np->properties;
1839 while (*next) {
1840 if (*next == prop) {
1841 /* found the node */
1842 *next = prop->next;
1843 prop->next = np->deadprops;
1844 np->deadprops = prop;
1845 found = 1;
1846 break;
1847 }
1848 next = &(*next)->next;
1849 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001850 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001851
1852 if (!found)
1853 return -ENODEV;
1854
Grant Likely75b57ec2014-02-20 18:02:11 +00001855 /* at early boot, bail hear and defer setup to of_init() */
1856 if (!of_kset)
1857 return 0;
1858
1859 sysfs_remove_bin_file(&np->kobj, &prop->attr);
Grant Likely02af11b2009-11-23 20:16:45 -07001860
1861 return 0;
1862}
1863
1864/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001865 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001866 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001867 *
1868 * Note that we don't actually remove it, since we have given out
1869 * who-knows-how-many pointers to the data using get-property.
1870 * Instead we just move the property to the "dead properties" list,
1871 * and add the new property to the property list
1872 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001873int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001874{
Dong Aisheng475d0092012-07-11 15:16:37 +10001875 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001876 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001877 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001878
1879 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1880 if (rc)
1881 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001882
Dong Aisheng475d0092012-07-11 15:16:37 +10001883 if (!newprop->name)
1884 return -EINVAL;
1885
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001886 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001887 next = &np->properties;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001888 oldprop = __of_find_property(np, newprop->name, NULL);
1889 if (!oldprop) {
1890 /* add the new node */
1891 rc = __of_add_property(np, newprop);
1892 } else while (*next) {
1893 /* replace the node */
Grant Likely02af11b2009-11-23 20:16:45 -07001894 if (*next == oldprop) {
Grant Likely02af11b2009-11-23 20:16:45 -07001895 newprop->next = oldprop->next;
1896 *next = newprop;
1897 oldprop->next = np->deadprops;
1898 np->deadprops = oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001899 break;
1900 }
1901 next = &(*next)->next;
1902 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001903 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Xiubo Li947fdaad02014-04-17 15:48:29 +08001904 if (rc)
1905 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001906
Thomas Petazzoni582da652014-05-14 13:36:36 +02001907 /* At early boot, bail out and defer setup to of_init() */
1908 if (!of_kset)
Xiubo Li947fdaad02014-04-17 15:48:29 +08001909 return 0;
Thomas Petazzoni582da652014-05-14 13:36:36 +02001910
Guenter Roecke7a62df2014-04-15 08:38:17 -07001911 /* Update the sysfs attribute */
Xiubo Li947fdaad02014-04-17 15:48:29 +08001912 if (oldprop)
1913 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
Guenter Roecke7a62df2014-04-15 08:38:17 -07001914 __of_add_property_sysfs(np, newprop);
1915
Grant Likely02af11b2009-11-23 20:16:45 -07001916 return 0;
1917}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001918
1919#if defined(CONFIG_OF_DYNAMIC)
1920/*
1921 * Support for dynamic device trees.
1922 *
1923 * On some platforms, the device tree can be manipulated at runtime.
1924 * The routines in this section support adding, removing and changing
1925 * device tree nodes.
1926 */
1927
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001928static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1929
1930int of_reconfig_notifier_register(struct notifier_block *nb)
1931{
1932 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1933}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001934EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001935
1936int of_reconfig_notifier_unregister(struct notifier_block *nb)
1937{
1938 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1939}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001940EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001941
1942int of_reconfig_notify(unsigned long action, void *p)
1943{
1944 int rc;
1945
1946 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1947 return notifier_to_errno(rc);
1948}
1949
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001950/**
1951 * of_attach_node - Plug a device node into the tree and global list.
1952 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001953int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001954{
1955 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001956 int rc;
1957
1958 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1959 if (rc)
1960 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001961
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001962 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001963 np->sibling = np->parent->child;
Frank Rowand99de6492014-06-14 20:39:05 -07001964 np->allnext = np->parent->allnext;
1965 np->parent->allnext = np;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001966 np->parent->child = np;
Pantelis Antonioue3963fd2013-11-08 17:03:57 +02001967 of_node_clear_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001968 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001969
Grant Likely75b57ec2014-02-20 18:02:11 +00001970 of_node_add(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001971 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001972}
1973
1974/**
1975 * of_detach_node - "Unplug" a node from the device tree.
1976 *
1977 * The caller must hold a reference to the node. The memory associated with
1978 * the node is not freed until its refcount goes to zero.
1979 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001980int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001981{
1982 struct device_node *parent;
1983 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001984 int rc = 0;
1985
1986 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1987 if (rc)
1988 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001989
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001990 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001991
Nathan Fontenote81b3292012-10-02 16:55:01 +00001992 if (of_node_check_flag(np, OF_DETACHED)) {
1993 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001994 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001995 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001996 }
1997
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001998 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001999 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05002000 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00002001 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00002002 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002003
Randy Dunlap465aac62012-11-30 10:01:51 +00002004 if (of_allnodes == np)
2005 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002006 else {
2007 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00002008 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002009 prev->allnext != np;
2010 prev = prev->allnext)
2011 ;
2012 prev->allnext = np->allnext;
2013 }
2014
2015 if (parent->child == np)
2016 parent->child = np->sibling;
2017 else {
2018 struct device_node *prevsib;
2019 for (prevsib = np->parent->child;
2020 prevsib->sibling != np;
2021 prevsib = prevsib->sibling)
2022 ;
2023 prevsib->sibling = np->sibling;
2024 }
2025
2026 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05002027 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00002028
Grant Likely75b57ec2014-02-20 18:02:11 +00002029 of_node_remove(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00002030 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002031}
2032#endif /* defined(CONFIG_OF_DYNAMIC) */
2033
Shawn Guo611cad72011-08-15 15:28:14 +08002034static void of_alias_add(struct alias_prop *ap, struct device_node *np,
2035 int id, const char *stem, int stem_len)
2036{
2037 ap->np = np;
2038 ap->id = id;
2039 strncpy(ap->stem, stem, stem_len);
2040 ap->stem[stem_len] = 0;
2041 list_add_tail(&ap->link, &aliases_lookup);
2042 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06002043 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08002044}
2045
2046/**
2047 * of_alias_scan - Scan all properties of 'aliases' node
2048 *
2049 * The function scans all the properties of 'aliases' node and populate
2050 * the the global lookup table with the properties. It returns the
2051 * number of alias_prop found, or error code in error case.
2052 *
2053 * @dt_alloc: An allocator that provides a virtual address to memory
2054 * for the resulting tree
2055 */
2056void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
2057{
2058 struct property *pp;
2059
2060 of_chosen = of_find_node_by_path("/chosen");
2061 if (of_chosen == NULL)
2062 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02002063
2064 if (of_chosen) {
Grant Likely676e1b22014-03-27 17:11:23 -07002065 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
2066 if (!name)
2067 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002068 if (name)
2069 of_stdout = of_find_node_by_path(name);
2070 }
2071
Shawn Guo611cad72011-08-15 15:28:14 +08002072 of_aliases = of_find_node_by_path("/aliases");
2073 if (!of_aliases)
2074 return;
2075
Dong Aisheng8af0da92011-12-22 20:19:24 +08002076 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08002077 const char *start = pp->name;
2078 const char *end = start + strlen(start);
2079 struct device_node *np;
2080 struct alias_prop *ap;
2081 int id, len;
2082
2083 /* Skip those we do not want to proceed */
2084 if (!strcmp(pp->name, "name") ||
2085 !strcmp(pp->name, "phandle") ||
2086 !strcmp(pp->name, "linux,phandle"))
2087 continue;
2088
2089 np = of_find_node_by_path(pp->value);
2090 if (!np)
2091 continue;
2092
2093 /* walk the alias backwards to extract the id and work out
2094 * the 'stem' string */
2095 while (isdigit(*(end-1)) && end > start)
2096 end--;
2097 len = end - start;
2098
2099 if (kstrtoint(end, 10, &id) < 0)
2100 continue;
2101
2102 /* Allocate an alias_prop with enough space for the stem */
2103 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
2104 if (!ap)
2105 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01002106 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08002107 ap->alias = start;
2108 of_alias_add(ap, np, id, start, len);
2109 }
2110}
2111
2112/**
2113 * of_alias_get_id - Get alias id for the given device_node
2114 * @np: Pointer to the given device_node
2115 * @stem: Alias stem of the given device_node
2116 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01002117 * The function travels the lookup table to get the alias id for the given
2118 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08002119 */
2120int of_alias_get_id(struct device_node *np, const char *stem)
2121{
2122 struct alias_prop *app;
2123 int id = -ENODEV;
2124
2125 mutex_lock(&of_aliases_mutex);
2126 list_for_each_entry(app, &aliases_lookup, link) {
2127 if (strcmp(app->stem, stem) != 0)
2128 continue;
2129
2130 if (np == app->np) {
2131 id = app->id;
2132 break;
2133 }
2134 }
2135 mutex_unlock(&of_aliases_mutex);
2136
2137 return id;
2138}
2139EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06002140
2141const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2142 u32 *pu)
2143{
2144 const void *curv = cur;
2145
2146 if (!prop)
2147 return NULL;
2148
2149 if (!cur) {
2150 curv = prop->value;
2151 goto out_val;
2152 }
2153
2154 curv += sizeof(*cur);
2155 if (curv >= prop->value + prop->length)
2156 return NULL;
2157
2158out_val:
2159 *pu = be32_to_cpup(curv);
2160 return curv;
2161}
2162EXPORT_SYMBOL_GPL(of_prop_next_u32);
2163
2164const char *of_prop_next_string(struct property *prop, const char *cur)
2165{
2166 const void *curv = cur;
2167
2168 if (!prop)
2169 return NULL;
2170
2171 if (!cur)
2172 return prop->value;
2173
2174 curv += strlen(cur) + 1;
2175 if (curv >= prop->value + prop->length)
2176 return NULL;
2177
2178 return curv;
2179}
2180EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002181
2182/**
2183 * of_device_is_stdout_path - check if a device node matches the
2184 * linux,stdout-path property
2185 *
2186 * Check if this device node matches the linux,stdout-path property
2187 * in the chosen node. return true if yes, false otherwise.
2188 */
2189int of_device_is_stdout_path(struct device_node *dn)
2190{
2191 if (!of_stdout)
2192 return false;
2193
2194 return of_stdout == dn;
2195}
2196EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002197
2198/**
2199 * of_find_next_cache_node - Find a node's subsidiary cache
2200 * @np: node of type "cpu" or "cache"
2201 *
2202 * Returns a node pointer with refcount incremented, use
2203 * of_node_put() on it when done. Caller should hold a reference
2204 * to np.
2205 */
2206struct device_node *of_find_next_cache_node(const struct device_node *np)
2207{
2208 struct device_node *child;
2209 const phandle *handle;
2210
2211 handle = of_get_property(np, "l2-cache", NULL);
2212 if (!handle)
2213 handle = of_get_property(np, "next-level-cache", NULL);
2214
2215 if (handle)
2216 return of_find_node_by_phandle(be32_to_cpup(handle));
2217
2218 /* OF on pmac has nodes instead of properties named "l2-cache"
2219 * beneath CPU nodes.
2220 */
2221 if (!strcmp(np->type, "cpu"))
2222 for_each_child_of_node(np, child)
2223 if (!strcmp(child->type, "cache"))
2224 return child;
2225
2226 return NULL;
2227}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002228
2229/**
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002230 * of_graph_parse_endpoint() - parse common endpoint node properties
2231 * @node: pointer to endpoint device_node
2232 * @endpoint: pointer to the OF endpoint data structure
2233 *
2234 * The caller should hold a reference to @node.
2235 */
2236int of_graph_parse_endpoint(const struct device_node *node,
2237 struct of_endpoint *endpoint)
2238{
2239 struct device_node *port_node = of_get_parent(node);
2240
Philipp Zabeld4847002014-03-04 12:31:24 +01002241 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2242 __func__, node->full_name);
2243
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002244 memset(endpoint, 0, sizeof(*endpoint));
2245
2246 endpoint->local_node = node;
2247 /*
2248 * It doesn't matter whether the two calls below succeed.
2249 * If they don't then the default value 0 is used.
2250 */
2251 of_property_read_u32(port_node, "reg", &endpoint->port);
2252 of_property_read_u32(node, "reg", &endpoint->id);
2253
2254 of_node_put(port_node);
2255
2256 return 0;
2257}
2258EXPORT_SYMBOL(of_graph_parse_endpoint);
2259
2260/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002261 * of_graph_get_next_endpoint() - get next endpoint node
2262 * @parent: pointer to the parent device node
2263 * @prev: previous endpoint node, or NULL to get first
2264 *
2265 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2266 * of the passed @prev node is not decremented, the caller have to use
2267 * of_node_put() on it when done.
2268 */
2269struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2270 struct device_node *prev)
2271{
2272 struct device_node *endpoint;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002273 struct device_node *port;
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002274
2275 if (!parent)
2276 return NULL;
2277
Linus Torvalds3c83e612014-04-04 09:50:07 -07002278 /*
2279 * Start by locating the port node. If no previous endpoint is specified
2280 * search for the first port node, otherwise get the previous endpoint
2281 * parent port node.
2282 */
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002283 if (!prev) {
2284 struct device_node *node;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002285
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002286 node = of_get_child_by_name(parent, "ports");
2287 if (node)
2288 parent = node;
2289
2290 port = of_get_child_by_name(parent, "port");
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002291 of_node_put(node);
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002292
Linus Torvalds3c83e612014-04-04 09:50:07 -07002293 if (!port) {
2294 pr_err("%s(): no port node found in %s\n",
2295 __func__, parent->full_name);
Philipp Zabel4329b932014-02-26 20:43:42 +01002296 return NULL;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002297 }
2298 } else {
2299 port = of_get_parent(prev);
2300 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2301 __func__, prev->full_name))
2302 return NULL;
Philipp Zabel4329b932014-02-26 20:43:42 +01002303
Linus Torvalds3c83e612014-04-04 09:50:07 -07002304 /*
2305 * Avoid dropping prev node refcount to 0 when getting the next
2306 * child below.
2307 */
2308 of_node_get(prev);
2309 }
Philipp Zabel4329b932014-02-26 20:43:42 +01002310
Linus Torvalds3c83e612014-04-04 09:50:07 -07002311 while (1) {
2312 /*
2313 * Now that we have a port node, get the next endpoint by
2314 * getting the next child. If the previous endpoint is NULL this
2315 * will return the first child.
2316 */
2317 endpoint = of_get_next_child(port, prev);
2318 if (endpoint) {
2319 of_node_put(port);
2320 return endpoint;
2321 }
2322
2323 /* No more endpoints under this port, try the next one. */
2324 prev = NULL;
2325
2326 do {
2327 port = of_get_next_child(parent, port);
2328 if (!port)
2329 return NULL;
2330 } while (of_node_cmp(port->name, "port"));
2331 }
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002332}
2333EXPORT_SYMBOL(of_graph_get_next_endpoint);
2334
2335/**
2336 * of_graph_get_remote_port_parent() - get remote port's parent node
2337 * @node: pointer to a local endpoint device_node
2338 *
2339 * Return: Remote device node associated with remote endpoint node linked
2340 * to @node. Use of_node_put() on it when done.
2341 */
2342struct device_node *of_graph_get_remote_port_parent(
2343 const struct device_node *node)
2344{
2345 struct device_node *np;
2346 unsigned int depth;
2347
2348 /* Get remote endpoint node. */
2349 np = of_parse_phandle(node, "remote-endpoint", 0);
2350
2351 /* Walk 3 levels up only if there is 'ports' node. */
2352 for (depth = 3; depth && np; depth--) {
2353 np = of_get_next_parent(np);
2354 if (depth == 2 && of_node_cmp(np->name, "ports"))
2355 break;
2356 }
2357 return np;
2358}
2359EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2360
2361/**
2362 * of_graph_get_remote_port() - get remote port node
2363 * @node: pointer to a local endpoint device_node
2364 *
2365 * Return: Remote port node associated with remote endpoint node linked
2366 * to @node. Use of_node_put() on it when done.
2367 */
2368struct device_node *of_graph_get_remote_port(const struct device_node *node)
2369{
2370 struct device_node *np;
2371
2372 /* Get remote endpoint node. */
2373 np = of_parse_phandle(node, "remote-endpoint", 0);
2374 if (!np)
2375 return NULL;
2376 return of_get_next_parent(np);
2377}
2378EXPORT_SYMBOL(of_graph_get_remote_port);