blob: b403f9d984612ad5388555639273c3702b64c14b [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/*
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030043 * Used to protect the of_aliases, to hold off addition of nodes to sysfs
Grant Likely75b57ec2014-02-20 18:02:11 +000044 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030045DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100046
Stephen Rothwell581b6052007-04-24 16:46:53 +100047/* use when traversing tree through the allnext, child, sibling,
48 * or parent members of struct device_node.
49 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050050DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100051
52int of_n_addr_cells(struct device_node *np)
53{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060054 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100055
56 do {
57 if (np->parent)
58 np = np->parent;
59 ip = of_get_property(np, "#address-cells", NULL);
60 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070061 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100062 } while (np->parent);
63 /* No #address-cells property for the root node */
64 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
65}
66EXPORT_SYMBOL(of_n_addr_cells);
67
68int of_n_size_cells(struct device_node *np)
69{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060070 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100071
72 do {
73 if (np->parent)
74 np = np->parent;
75 ip = of_get_property(np, "#size-cells", NULL);
76 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070077 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100078 } while (np->parent);
79 /* No #size-cells property for the root node */
80 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
81}
82EXPORT_SYMBOL(of_n_size_cells);
83
Rob Herring0c3f0612013-09-17 10:42:50 -050084#ifdef CONFIG_NUMA
85int __weak of_node_to_nid(struct device_node *np)
86{
87 return numa_node_id();
88}
89#endif
90
Grant Likely6afc0dc2014-06-26 15:40:48 +010091#ifndef CONFIG_OF_DYNAMIC
Grant Likely75b57ec2014-02-20 18:02:11 +000092static void of_node_release(struct kobject *kobj)
93{
94 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
95}
Grant Likely0f22dd32012-02-15 20:38:40 -070096#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -070097
Grant Likely75b57ec2014-02-20 18:02:11 +000098struct kobj_type of_node_ktype = {
99 .release = of_node_release,
100};
101
102static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
103 struct bin_attribute *bin_attr, char *buf,
104 loff_t offset, size_t count)
105{
106 struct property *pp = container_of(bin_attr, struct property, attr);
107 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
108}
109
110static const char *safe_name(struct kobject *kobj, const char *orig_name)
111{
112 const char *name = orig_name;
113 struct kernfs_node *kn;
114 int i = 0;
115
116 /* don't be a hero. After 16 tries give up */
117 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
118 sysfs_put(kn);
119 if (name != orig_name)
120 kfree(name);
121 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
122 }
123
124 if (name != orig_name)
125 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
126 kobject_name(kobj), name);
127 return name;
128}
129
130static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
131{
132 int rc;
133
134 /* Important: Don't leak passwords */
135 bool secure = strncmp(pp->name, "security-", 9) == 0;
136
137 sysfs_bin_attr_init(&pp->attr);
138 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
139 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
140 pp->attr.size = secure ? 0 : pp->length;
141 pp->attr.read = of_node_property_read;
142
143 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
144 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
145 return rc;
146}
147
148static int __of_node_add(struct device_node *np)
149{
150 const char *name;
151 struct property *pp;
152 int rc;
153
154 np->kobj.kset = of_kset;
155 if (!np->parent) {
156 /* Nodes without parents are new top level trees */
Kees Cook28d3ee42014-06-10 09:57:00 -0700157 rc = kobject_add(&np->kobj, NULL, "%s",
158 safe_name(&of_kset->kobj, "base"));
Grant Likely75b57ec2014-02-20 18:02:11 +0000159 } else {
160 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
161 if (!name || !name[0])
162 return -EINVAL;
163
164 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
165 }
166 if (rc)
167 return rc;
168
169 for_each_property_of_node(np, pp)
170 __of_add_property_sysfs(np, pp);
171
172 return 0;
173}
174
175int of_node_add(struct device_node *np)
176{
177 int rc = 0;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200178
179 BUG_ON(!of_node_is_initialized(np));
180
181 /*
182 * Grab the mutex here so that in a race condition between of_init() and
183 * of_node_add(), node addition will still be consistent.
184 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300185 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000186 if (of_kset)
187 rc = __of_node_add(np);
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200188 else
189 /* This scenario may be perfectly valid, but report it anyway */
190 pr_info("of_node_add(%s) before of_init()\n", np->full_name);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300191 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000192 return rc;
193}
194
Grant Likely75b57ec2014-02-20 18:02:11 +0000195static int __init of_init(void)
196{
197 struct device_node *np;
198
199 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300200 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000201 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
202 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300203 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000204 return -ENOMEM;
205 }
206 for_each_of_allnodes(np)
207 __of_node_add(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300208 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000209
Grant Likely83570412012-11-06 21:03:27 +0000210 /* Symlink in /proc as required by userspace ABI */
Grant Likely75b57ec2014-02-20 18:02:11 +0000211 if (of_allnodes)
212 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000213
214 return 0;
215}
216core_initcall(of_init);
217
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500218static struct property *__of_find_property(const struct device_node *np,
219 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000220{
221 struct property *pp;
222
Timur Tabi64e45662008-05-08 05:19:59 +1000223 if (!np)
224 return NULL;
225
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530226 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000227 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530228 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000229 *lenp = pp->length;
230 break;
231 }
232 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500233
234 return pp;
235}
236
237struct property *of_find_property(const struct device_node *np,
238 const char *name,
239 int *lenp)
240{
241 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500242 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500243
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500244 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500245 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500246 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000247
248 return pp;
249}
250EXPORT_SYMBOL(of_find_property);
251
Grant Likelye91edcf2009-10-15 10:58:09 -0600252/**
253 * of_find_all_nodes - Get next node in global list
254 * @prev: Previous node or NULL to start iteration
255 * of_node_put() will be called on it
256 *
257 * Returns a node pointer with refcount incremented, use
258 * of_node_put() on it when done.
259 */
260struct device_node *of_find_all_nodes(struct device_node *prev)
261{
262 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000263 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600264
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000265 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000266 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600267 for (; np != NULL; np = np->allnext)
268 if (of_node_get(np))
269 break;
270 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000271 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600272 return np;
273}
274EXPORT_SYMBOL(of_find_all_nodes);
275
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000276/*
277 * Find a property with a given name for a given node
278 * and return the value.
279 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500280static const void *__of_get_property(const struct device_node *np,
281 const char *name, int *lenp)
282{
283 struct property *pp = __of_find_property(np, name, lenp);
284
285 return pp ? pp->value : NULL;
286}
287
288/*
289 * Find a property with a given name for a given node
290 * and return the value.
291 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000292const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500293 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000294{
295 struct property *pp = of_find_property(np, name, lenp);
296
297 return pp ? pp->value : NULL;
298}
299EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000300
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100301/*
302 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
303 *
304 * @cpu: logical cpu index of a core/thread
305 * @phys_id: physical identifier of a core/thread
306 *
307 * CPU logical to physical index mapping is architecture specific.
308 * However this __weak function provides a default match of physical
309 * id to logical cpu index. phys_id provided here is usually values read
310 * from the device tree which must match the hardware internal registers.
311 *
312 * Returns true if the physical identifier and the logical cpu index
313 * correspond to the same core/thread, false otherwise.
314 */
315bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
316{
317 return (u32)phys_id == cpu;
318}
319
320/**
321 * Checks if the given "prop_name" property holds the physical id of the
322 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
323 * NULL, local thread number within the core is returned in it.
324 */
325static bool __of_find_n_match_cpu_property(struct device_node *cpun,
326 const char *prop_name, int cpu, unsigned int *thread)
327{
328 const __be32 *cell;
329 int ac, prop_len, tid;
330 u64 hwid;
331
332 ac = of_n_addr_cells(cpun);
333 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100334 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100335 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100336 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100337 for (tid = 0; tid < prop_len; tid++) {
338 hwid = of_read_number(cell, ac);
339 if (arch_match_cpu_phys_id(cpu, hwid)) {
340 if (thread)
341 *thread = tid;
342 return true;
343 }
344 cell += ac;
345 }
346 return false;
347}
348
David Millerd1cb9d12013-10-03 17:24:51 -0400349/*
350 * arch_find_n_match_cpu_physical_id - See if the given device node is
351 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
352 * else false. If 'thread' is non-NULL, the local thread number within the
353 * core is returned in it.
354 */
355bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
356 int cpu, unsigned int *thread)
357{
358 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
359 * for thread ids on PowerPC. If it doesn't exist fallback to
360 * standard "reg" property.
361 */
362 if (IS_ENABLED(CONFIG_PPC) &&
363 __of_find_n_match_cpu_property(cpun,
364 "ibm,ppc-interrupt-server#s",
365 cpu, thread))
366 return true;
367
368 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
369 return true;
370
371 return false;
372}
373
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100374/**
375 * of_get_cpu_node - Get device node associated with the given logical CPU
376 *
377 * @cpu: CPU number(logical index) for which device node is required
378 * @thread: if not NULL, local thread number within the physical core is
379 * returned
380 *
381 * The main purpose of this function is to retrieve the device node for the
382 * given logical CPU index. It should be used to initialize the of_node in
383 * cpu device. Once of_node in cpu device is populated, all the further
384 * references can use that instead.
385 *
386 * CPU logical to physical index mapping is architecture specific and is built
387 * before booting secondary cores. This function uses arch_match_cpu_phys_id
388 * which can be overridden by architecture specific implementation.
389 *
390 * Returns a node pointer for the logical cpu if found, else NULL.
391 */
392struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
393{
David Millerd1cb9d12013-10-03 17:24:51 -0400394 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100395
David Millerd1cb9d12013-10-03 17:24:51 -0400396 for_each_node_by_type(cpun, "cpu") {
397 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100398 return cpun;
399 }
400 return NULL;
401}
402EXPORT_SYMBOL(of_get_cpu_node);
403
Kevin Hao215a14c2014-02-19 16:15:45 +0800404/**
405 * __of_device_is_compatible() - Check if the node matches given constraints
406 * @device: pointer to node
407 * @compat: required compatible string, NULL or "" for any match
408 * @type: required device_type value, NULL or "" for any match
409 * @name: required node name, NULL or "" for any match
410 *
411 * Checks if the given @compat, @type and @name strings match the
412 * properties of the given @device. A constraints can be skipped by
413 * passing NULL or an empty string as the constraint.
414 *
415 * Returns 0 for no match, and a positive integer on match. The return
416 * value is a relative score with larger values indicating better
417 * matches. The score is weighted for the most specific compatible value
418 * to get the highest score. Matching type is next, followed by matching
419 * name. Practically speaking, this results in the following priority
420 * order for matches:
421 *
422 * 1. specific compatible && type && name
423 * 2. specific compatible && type
424 * 3. specific compatible && name
425 * 4. specific compatible
426 * 5. general compatible && type && name
427 * 6. general compatible && type
428 * 7. general compatible && name
429 * 8. general compatible
430 * 9. type && name
431 * 10. type
432 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000433 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500434static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800435 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000436{
Kevin Hao215a14c2014-02-19 16:15:45 +0800437 struct property *prop;
438 const char *cp;
439 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000440
Kevin Hao215a14c2014-02-19 16:15:45 +0800441 /* Compatible match has highest priority */
442 if (compat && compat[0]) {
443 prop = __of_find_property(device, "compatible", NULL);
444 for (cp = of_prop_next_string(prop, NULL); cp;
445 cp = of_prop_next_string(prop, cp), index++) {
446 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
447 score = INT_MAX/2 - (index << 2);
448 break;
449 }
450 }
451 if (!score)
452 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000453 }
454
Kevin Hao215a14c2014-02-19 16:15:45 +0800455 /* Matching type is better than matching name */
456 if (type && type[0]) {
457 if (!device->type || of_node_cmp(type, device->type))
458 return 0;
459 score += 2;
460 }
461
462 /* Matching name is a bit better than not */
463 if (name && name[0]) {
464 if (!device->name || of_node_cmp(name, device->name))
465 return 0;
466 score++;
467 }
468
469 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000470}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500471
472/** Checks if the given "compat" string matches one of the strings in
473 * the device's "compatible" property
474 */
475int of_device_is_compatible(const struct device_node *device,
476 const char *compat)
477{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500478 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500479 int res;
480
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500481 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800482 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500483 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500484 return res;
485}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000486EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000487
488/**
Grant Likely71a157e2010-02-01 21:34:14 -0700489 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700490 * @compat: compatible string to look for in root node's compatible property.
491 *
492 * Returns true if the root node has the given value in its
493 * compatible property.
494 */
Grant Likely71a157e2010-02-01 21:34:14 -0700495int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700496{
497 struct device_node *root;
498 int rc = 0;
499
500 root = of_find_node_by_path("/");
501 if (root) {
502 rc = of_device_is_compatible(root, compat);
503 of_node_put(root);
504 }
505 return rc;
506}
Grant Likely71a157e2010-02-01 21:34:14 -0700507EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700508
509/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700510 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100511 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700512 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100513 *
514 * Returns 1 if the status property is absent or set to "okay" or "ok",
515 * 0 otherwise
516 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700517static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100518{
519 const char *status;
520 int statlen;
521
Xiubo Li42ccd782014-01-13 11:07:28 +0800522 if (!device)
523 return 0;
524
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700525 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100526 if (status == NULL)
527 return 1;
528
529 if (statlen > 0) {
530 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
531 return 1;
532 }
533
534 return 0;
535}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700536
537/**
538 * of_device_is_available - check if a device is available for use
539 *
540 * @device: Node to check for availability
541 *
542 * Returns 1 if the status property is absent or set to "okay" or "ok",
543 * 0 otherwise
544 */
545int of_device_is_available(const struct device_node *device)
546{
547 unsigned long flags;
548 int res;
549
550 raw_spin_lock_irqsave(&devtree_lock, flags);
551 res = __of_device_is_available(device);
552 raw_spin_unlock_irqrestore(&devtree_lock, flags);
553 return res;
554
555}
Josh Boyer834d97d2008-03-27 00:33:14 +1100556EXPORT_SYMBOL(of_device_is_available);
557
558/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000559 * of_get_parent - Get a node's parent if any
560 * @node: Node to get parent
561 *
562 * Returns a node pointer with refcount incremented, use
563 * of_node_put() on it when done.
564 */
565struct device_node *of_get_parent(const struct device_node *node)
566{
567 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500568 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000569
570 if (!node)
571 return NULL;
572
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500573 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000574 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500575 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000576 return np;
577}
578EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000579
580/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000581 * of_get_next_parent - Iterate to a node's parent
582 * @node: Node to get parent of
583 *
584 * This is like of_get_parent() except that it drops the
585 * refcount on the passed node, making it suitable for iterating
586 * through a node's parents.
587 *
588 * Returns a node pointer with refcount incremented, use
589 * of_node_put() on it when done.
590 */
591struct device_node *of_get_next_parent(struct device_node *node)
592{
593 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500594 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000595
596 if (!node)
597 return NULL;
598
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500599 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000600 parent = of_node_get(node->parent);
601 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500602 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000603 return parent;
604}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300605EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000606
Grant Likely0d0e02d2014-05-22 01:04:17 +0900607static struct device_node *__of_get_next_child(const struct device_node *node,
608 struct device_node *prev)
609{
610 struct device_node *next;
611
Florian Fainelli43cb4362014-05-28 10:39:02 -0700612 if (!node)
613 return NULL;
614
Grant Likely0d0e02d2014-05-22 01:04:17 +0900615 next = prev ? prev->sibling : node->child;
616 for (; next; next = next->sibling)
617 if (of_node_get(next))
618 break;
619 of_node_put(prev);
620 return next;
621}
622#define __for_each_child_of_node(parent, child) \
623 for (child = __of_get_next_child(parent, NULL); child != NULL; \
624 child = __of_get_next_child(parent, child))
625
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000626/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000627 * of_get_next_child - Iterate a node childs
628 * @node: parent node
629 * @prev: previous child of the parent node, or NULL to get first
630 *
631 * Returns a node pointer with refcount incremented, use
632 * of_node_put() on it when done.
633 */
634struct device_node *of_get_next_child(const struct device_node *node,
635 struct device_node *prev)
636{
637 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500638 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000639
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500640 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900641 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500642 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000643 return next;
644}
645EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000646
647/**
Timur Tabi32961932012-08-14 13:20:23 +0000648 * of_get_next_available_child - Find the next available child node
649 * @node: parent node
650 * @prev: previous child of the parent node, or NULL to get first
651 *
652 * This function is like of_get_next_child(), except that it
653 * automatically skips any disabled nodes (i.e. status = "disabled").
654 */
655struct device_node *of_get_next_available_child(const struct device_node *node,
656 struct device_node *prev)
657{
658 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000659 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000660
Florian Fainelli43cb4362014-05-28 10:39:02 -0700661 if (!node)
662 return NULL;
663
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000664 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000665 next = prev ? prev->sibling : node->child;
666 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700667 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000668 continue;
669 if (of_node_get(next))
670 break;
671 }
672 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000673 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000674 return next;
675}
676EXPORT_SYMBOL(of_get_next_available_child);
677
678/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100679 * of_get_child_by_name - Find the child node by name for a given parent
680 * @node: parent node
681 * @name: child name to look for.
682 *
683 * This function looks for child node for given matching name
684 *
685 * Returns a node pointer if found, with refcount incremented, use
686 * of_node_put() on it when done.
687 * Returns NULL if node is not found.
688 */
689struct device_node *of_get_child_by_name(const struct device_node *node,
690 const char *name)
691{
692 struct device_node *child;
693
694 for_each_child_of_node(node, child)
695 if (child->name && (of_node_cmp(child->name, name) == 0))
696 break;
697 return child;
698}
699EXPORT_SYMBOL(of_get_child_by_name);
700
Grant Likelyc22e6502014-03-14 17:07:12 +0000701static struct device_node *__of_find_node_by_path(struct device_node *parent,
702 const char *path)
703{
704 struct device_node *child;
705 int len = strchrnul(path, '/') - path;
706
707 if (!len)
708 return NULL;
709
710 __for_each_child_of_node(parent, child) {
711 const char *name = strrchr(child->full_name, '/');
712 if (WARN(!name, "malformed device_node %s\n", child->full_name))
713 continue;
714 name++;
715 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
716 return child;
717 }
718 return NULL;
719}
720
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100721/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000722 * of_find_node_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000723 * @path: Either the full path to match, or if the path does not
724 * start with '/', the name of a property of the /aliases
725 * node (an alias). In the case of an alias, the node
726 * matching the alias' value will be returned.
727 *
728 * Valid paths:
729 * /foo/bar Full path
730 * foo Valid alias
731 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000732 *
733 * Returns a node pointer with refcount incremented, use
734 * of_node_put() on it when done.
735 */
736struct device_node *of_find_node_by_path(const char *path)
737{
Grant Likelyc22e6502014-03-14 17:07:12 +0000738 struct device_node *np = NULL;
739 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500740 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000741
Grant Likelyc22e6502014-03-14 17:07:12 +0000742 if (strcmp(path, "/") == 0)
743 return of_node_get(of_allnodes);
744
745 /* The path could begin with an alias */
746 if (*path != '/') {
747 char *p = strchrnul(path, '/');
748 int len = p - path;
749
750 /* of_aliases must not be NULL */
751 if (!of_aliases)
752 return NULL;
753
754 for_each_property_of_node(of_aliases, pp) {
755 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
756 np = of_find_node_by_path(pp->value);
757 break;
758 }
759 }
760 if (!np)
761 return NULL;
762 path = p;
763 }
764
765 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500766 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000767 if (!np)
768 np = of_node_get(of_allnodes);
769 while (np && *path == '/') {
770 path++; /* Increment past '/' delimiter */
771 np = __of_find_node_by_path(np, path);
772 path = strchrnul(path, '/');
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000773 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500774 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000775 return np;
776}
777EXPORT_SYMBOL(of_find_node_by_path);
778
779/**
780 * of_find_node_by_name - Find a node by its "name" property
781 * @from: The node to start searching from or NULL, the node
782 * you pass will not be searched, only the next one
783 * will; typically, you pass what the previous call
784 * returned. of_node_put() will be called on it
785 * @name: The name string to match against
786 *
787 * Returns a node pointer with refcount incremented, use
788 * of_node_put() on it when done.
789 */
790struct device_node *of_find_node_by_name(struct device_node *from,
791 const char *name)
792{
793 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500794 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000795
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500796 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000797 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000798 for (; np; np = np->allnext)
799 if (np->name && (of_node_cmp(np->name, name) == 0)
800 && of_node_get(np))
801 break;
802 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500803 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000804 return np;
805}
806EXPORT_SYMBOL(of_find_node_by_name);
807
808/**
809 * of_find_node_by_type - Find a node by its "device_type" property
810 * @from: The node to start searching from, or NULL to start searching
811 * the entire device tree. The node you pass will not be
812 * searched, only the next one will; typically, you pass
813 * what the previous call returned. of_node_put() will be
814 * called on from for you.
815 * @type: The type string to match against
816 *
817 * Returns a node pointer with refcount incremented, use
818 * of_node_put() on it when done.
819 */
820struct device_node *of_find_node_by_type(struct device_node *from,
821 const char *type)
822{
823 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500824 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000825
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500826 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000827 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000828 for (; np; np = np->allnext)
829 if (np->type && (of_node_cmp(np->type, type) == 0)
830 && of_node_get(np))
831 break;
832 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500833 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000834 return np;
835}
836EXPORT_SYMBOL(of_find_node_by_type);
837
838/**
839 * of_find_compatible_node - Find a node based on type and one of the
840 * tokens in its "compatible" property
841 * @from: The node to start searching from or NULL, the node
842 * you pass will not be searched, only the next one
843 * will; typically, you pass what the previous call
844 * returned. of_node_put() will be called on it
845 * @type: The type string to match "device_type" or NULL to ignore
846 * @compatible: The string to match to one of the tokens in the device
847 * "compatible" list.
848 *
849 * Returns a node pointer with refcount incremented, use
850 * of_node_put() on it when done.
851 */
852struct device_node *of_find_compatible_node(struct device_node *from,
853 const char *type, const char *compatible)
854{
855 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500856 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000857
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500858 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000859 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000860 for (; np; np = np->allnext) {
Kevin Hao215a14c2014-02-19 16:15:45 +0800861 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500862 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000863 break;
864 }
865 of_node_put(from);
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_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100870
871/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000872 * of_find_node_with_property - Find a node which has a property with
873 * the given name.
874 * @from: The node to start searching from or NULL, the node
875 * you pass will not be searched, only the next one
876 * will; typically, you pass what the previous call
877 * returned. of_node_put() will be called on it
878 * @prop_name: The name of the property to look for.
879 *
880 * Returns a node pointer with refcount incremented, use
881 * of_node_put() on it when done.
882 */
883struct device_node *of_find_node_with_property(struct device_node *from,
884 const char *prop_name)
885{
886 struct device_node *np;
887 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500888 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000889
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500890 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000891 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000892 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530893 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000894 if (of_prop_cmp(pp->name, prop_name) == 0) {
895 of_node_get(np);
896 goto out;
897 }
898 }
899 }
900out:
901 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500902 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000903 return np;
904}
905EXPORT_SYMBOL(of_find_node_with_property);
906
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500907static
908const struct of_device_id *__of_match_node(const struct of_device_id *matches,
909 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100910{
Kevin Hao215a14c2014-02-19 16:15:45 +0800911 const struct of_device_id *best_match = NULL;
912 int score, best_score = 0;
913
Grant Likelya52f07e2011-03-18 10:21:29 -0600914 if (!matches)
915 return NULL;
916
Kevin Hao215a14c2014-02-19 16:15:45 +0800917 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
918 score = __of_device_is_compatible(node, matches->compatible,
919 matches->type, matches->name);
920 if (score > best_score) {
921 best_match = matches;
922 best_score = score;
923 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +0800924 }
Kevin Hao215a14c2014-02-19 16:15:45 +0800925
926 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +1100927}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500928
929/**
930 * of_match_node - Tell if an device_node has a matching of_match structure
931 * @matches: array of of device match structures to search in
932 * @node: the of device structure to match against
933 *
Kevin Hao71c54982014-02-18 15:57:29 +0800934 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500935 */
936const struct of_device_id *of_match_node(const struct of_device_id *matches,
937 const struct device_node *node)
938{
939 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500940 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500941
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500942 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500943 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500944 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500945 return match;
946}
Grant Likely283029d2008-01-09 06:20:40 +1100947EXPORT_SYMBOL(of_match_node);
948
949/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700950 * of_find_matching_node_and_match - Find a node based on an of_device_id
951 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100952 * @from: The node to start searching from or NULL, the node
953 * you pass will not be searched, only the next one
954 * will; typically, you pass what the previous call
955 * returned. of_node_put() will be called on it
956 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700957 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100958 *
959 * Returns a node pointer with refcount incremented, use
960 * of_node_put() on it when done.
961 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700962struct device_node *of_find_matching_node_and_match(struct device_node *from,
963 const struct of_device_id *matches,
964 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100965{
966 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800967 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500968 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100969
Stephen Warren50c8af42012-11-20 16:12:20 -0700970 if (match)
971 *match = NULL;
972
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500973 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000974 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100975 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500976 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800977 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700978 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800979 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100980 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700981 }
Grant Likely283029d2008-01-09 06:20:40 +1100982 }
983 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500984 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100985 return np;
986}
Grant Likely80c20222012-12-19 10:45:36 +0000987EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400988
989/**
Grant Likely3f07af42008-07-25 22:25:13 -0400990 * of_modalias_node - Lookup appropriate modalias for a device node
991 * @node: pointer to a device tree node
992 * @modalias: Pointer to buffer that modalias value will be copied into
993 * @len: Length of modalias value
994 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600995 * Based on the value of the compatible property, this routine will attempt
996 * to choose an appropriate modalias value for a particular device tree node.
997 * It does this by stripping the manufacturer prefix (as delimited by a ',')
998 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400999 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001000 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001001 */
1002int of_modalias_node(struct device_node *node, char *modalias, int len)
1003{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001004 const char *compatible, *p;
1005 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001006
1007 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001008 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001009 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001010 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001011 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001012 return 0;
1013}
1014EXPORT_SYMBOL_GPL(of_modalias_node);
1015
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001016/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001017 * of_find_node_by_phandle - Find a node given a phandle
1018 * @handle: phandle of the node to find
1019 *
1020 * Returns a node pointer with refcount incremented, use
1021 * of_node_put() on it when done.
1022 */
1023struct device_node *of_find_node_by_phandle(phandle handle)
1024{
1025 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001026 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001027
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001028 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001029 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001030 if (np->phandle == handle)
1031 break;
1032 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001033 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001034 return np;
1035}
1036EXPORT_SYMBOL(of_find_node_by_phandle);
1037
1038/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +01001039 * of_property_count_elems_of_size - Count the number of elements in a property
1040 *
1041 * @np: device node from which the property value is to be read.
1042 * @propname: name of the property to be searched.
1043 * @elem_size: size of the individual element
1044 *
1045 * Search for a property in a device node and count the number of elements of
1046 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1047 * property does not exist or its length does not match a multiple of elem_size
1048 * and -ENODATA if the property does not have a value.
1049 */
1050int of_property_count_elems_of_size(const struct device_node *np,
1051 const char *propname, int elem_size)
1052{
1053 struct property *prop = of_find_property(np, propname, NULL);
1054
1055 if (!prop)
1056 return -EINVAL;
1057 if (!prop->value)
1058 return -ENODATA;
1059
1060 if (prop->length % elem_size != 0) {
1061 pr_err("size of %s in node %s is not a multiple of %d\n",
1062 propname, np->full_name, elem_size);
1063 return -EINVAL;
1064 }
1065
1066 return prop->length / elem_size;
1067}
1068EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1069
1070/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001071 * of_find_property_value_of_size
1072 *
1073 * @np: device node from which the property value is to be read.
1074 * @propname: name of the property to be searched.
1075 * @len: requested length of property value
1076 *
1077 * Search for a property in a device node and valid the requested size.
1078 * Returns the property value on success, -EINVAL if the property does not
1079 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1080 * property data isn't large enough.
1081 *
1082 */
1083static void *of_find_property_value_of_size(const struct device_node *np,
1084 const char *propname, u32 len)
1085{
1086 struct property *prop = of_find_property(np, propname, NULL);
1087
1088 if (!prop)
1089 return ERR_PTR(-EINVAL);
1090 if (!prop->value)
1091 return ERR_PTR(-ENODATA);
1092 if (len > prop->length)
1093 return ERR_PTR(-EOVERFLOW);
1094
1095 return prop->value;
1096}
1097
1098/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001099 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1100 *
1101 * @np: device node from which the property value is to be read.
1102 * @propname: name of the property to be searched.
1103 * @index: index of the u32 in the list of values
1104 * @out_value: pointer to return value, modified only if no error.
1105 *
1106 * Search for a property in a device node and read nth 32-bit value from
1107 * it. Returns 0 on success, -EINVAL if the property does not exist,
1108 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1109 * property data isn't large enough.
1110 *
1111 * The out_value is modified only if a valid u32 value can be decoded.
1112 */
1113int of_property_read_u32_index(const struct device_node *np,
1114 const char *propname,
1115 u32 index, u32 *out_value)
1116{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001117 const u32 *val = of_find_property_value_of_size(np, propname,
1118 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +13001119
Tony Priskdaeec1f2013-04-03 17:57:11 +13001120 if (IS_ERR(val))
1121 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001122
Tony Priskdaeec1f2013-04-03 17:57:11 +13001123 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001124 return 0;
1125}
1126EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1127
1128/**
Viresh Kumarbe193242012-11-20 10:15:19 +05301129 * of_property_read_u8_array - Find and read an array of u8 from a property.
1130 *
1131 * @np: device node from which the property value is to be read.
1132 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301133 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301134 * @sz: number of array elements to read
1135 *
1136 * Search for a property in a device node and read 8-bit value(s) from
1137 * it. Returns 0 on success, -EINVAL if the property does not exist,
1138 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1139 * property data isn't large enough.
1140 *
1141 * dts entry of array should be like:
1142 * property = /bits/ 8 <0x50 0x60 0x70>;
1143 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301144 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301145 */
1146int of_property_read_u8_array(const struct device_node *np,
1147 const char *propname, u8 *out_values, size_t sz)
1148{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001149 const u8 *val = of_find_property_value_of_size(np, propname,
1150 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301151
Tony Priskdaeec1f2013-04-03 17:57:11 +13001152 if (IS_ERR(val))
1153 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301154
Viresh Kumarbe193242012-11-20 10:15:19 +05301155 while (sz--)
1156 *out_values++ = *val++;
1157 return 0;
1158}
1159EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1160
1161/**
1162 * of_property_read_u16_array - Find and read an array of u16 from a property.
1163 *
1164 * @np: device node from which the property value is to be read.
1165 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301166 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301167 * @sz: number of array elements to read
1168 *
1169 * Search for a property in a device node and read 16-bit value(s) from
1170 * it. Returns 0 on success, -EINVAL if the property does not exist,
1171 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1172 * property data isn't large enough.
1173 *
1174 * dts entry of array should be like:
1175 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1176 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301177 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301178 */
1179int of_property_read_u16_array(const struct device_node *np,
1180 const char *propname, u16 *out_values, size_t sz)
1181{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001182 const __be16 *val = of_find_property_value_of_size(np, propname,
1183 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301184
Tony Priskdaeec1f2013-04-03 17:57:11 +13001185 if (IS_ERR(val))
1186 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301187
Viresh Kumarbe193242012-11-20 10:15:19 +05301188 while (sz--)
1189 *out_values++ = be16_to_cpup(val++);
1190 return 0;
1191}
1192EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1193
1194/**
Rob Herring0e373632011-07-06 15:42:58 -05001195 * of_property_read_u32_array - Find and read an array of 32 bit integers
1196 * from a property.
1197 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301198 * @np: device node from which the property value is to be read.
1199 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301200 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301201 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301202 *
Rob Herring0e373632011-07-06 15:42:58 -05001203 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301204 * it. Returns 0 on success, -EINVAL if the property does not exist,
1205 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1206 * property data isn't large enough.
1207 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301208 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301209 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001210int of_property_read_u32_array(const struct device_node *np,
1211 const char *propname, u32 *out_values,
1212 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301213{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001214 const __be32 *val = of_find_property_value_of_size(np, propname,
1215 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301216
Tony Priskdaeec1f2013-04-03 17:57:11 +13001217 if (IS_ERR(val))
1218 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001219
Rob Herring0e373632011-07-06 15:42:58 -05001220 while (sz--)
1221 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301222 return 0;
1223}
Rob Herring0e373632011-07-06 15:42:58 -05001224EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301225
1226/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001227 * of_property_read_u64 - Find and read a 64 bit integer from a property
1228 * @np: device node from which the property value is to be read.
1229 * @propname: name of the property to be searched.
1230 * @out_value: pointer to return value, modified only if return value is 0.
1231 *
1232 * Search for a property in a device node and read a 64-bit value from
1233 * it. Returns 0 on success, -EINVAL if the property does not exist,
1234 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1235 * property data isn't large enough.
1236 *
1237 * The out_value is modified only if a valid u64 value can be decoded.
1238 */
1239int of_property_read_u64(const struct device_node *np, const char *propname,
1240 u64 *out_value)
1241{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001242 const __be32 *val = of_find_property_value_of_size(np, propname,
1243 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001244
Tony Priskdaeec1f2013-04-03 17:57:11 +13001245 if (IS_ERR(val))
1246 return PTR_ERR(val);
1247
1248 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001249 return 0;
1250}
1251EXPORT_SYMBOL_GPL(of_property_read_u64);
1252
1253/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301254 * of_property_read_string - Find and read a string from a property
1255 * @np: device node from which the property value is to be read.
1256 * @propname: name of the property to be searched.
1257 * @out_string: pointer to null terminated return string, modified only if
1258 * return value is 0.
1259 *
1260 * Search for a property in a device tree node and retrieve a null
1261 * terminated string value (pointer to data, not a copy). Returns 0 on
1262 * success, -EINVAL if the property does not exist, -ENODATA if property
1263 * does not have a value, and -EILSEQ if the string is not null-terminated
1264 * within the length of the property data.
1265 *
1266 * The out_string pointer is modified only if a valid string can be decoded.
1267 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001268int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001269 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301270{
1271 struct property *prop = of_find_property(np, propname, NULL);
1272 if (!prop)
1273 return -EINVAL;
1274 if (!prop->value)
1275 return -ENODATA;
1276 if (strnlen(prop->value, prop->length) >= prop->length)
1277 return -EILSEQ;
1278 *out_string = prop->value;
1279 return 0;
1280}
1281EXPORT_SYMBOL_GPL(of_property_read_string);
1282
1283/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001284 * of_property_read_string_index - Find and read a string from a multiple
1285 * strings property.
1286 * @np: device node from which the property value is to be read.
1287 * @propname: name of the property to be searched.
1288 * @index: index of the string in the list of strings
1289 * @out_string: pointer to null terminated return string, modified only if
1290 * return value is 0.
1291 *
1292 * Search for a property in a device tree node and retrieve a null
1293 * terminated string value (pointer to data, not a copy) in the list of strings
1294 * contained in that property.
1295 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1296 * property does not have a value, and -EILSEQ if the string is not
1297 * null-terminated within the length of the property data.
1298 *
1299 * The out_string pointer is modified only if a valid string can be decoded.
1300 */
1301int of_property_read_string_index(struct device_node *np, const char *propname,
1302 int index, const char **output)
1303{
1304 struct property *prop = of_find_property(np, propname, NULL);
1305 int i = 0;
1306 size_t l = 0, total = 0;
1307 const char *p;
1308
1309 if (!prop)
1310 return -EINVAL;
1311 if (!prop->value)
1312 return -ENODATA;
1313 if (strnlen(prop->value, prop->length) >= prop->length)
1314 return -EILSEQ;
1315
1316 p = prop->value;
1317
1318 for (i = 0; total < prop->length; total += l, p += l) {
1319 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001320 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001321 *output = p;
1322 return 0;
1323 }
1324 }
1325 return -ENODATA;
1326}
1327EXPORT_SYMBOL_GPL(of_property_read_string_index);
1328
Grant Likely7aff0fe2011-12-12 09:25:58 -07001329/**
1330 * of_property_match_string() - Find string in a list and return index
1331 * @np: pointer to node containing string list property
1332 * @propname: string list property name
1333 * @string: pointer to string to search for in string list
1334 *
1335 * This function searches a string list property and returns the index
1336 * of a specific string value.
1337 */
1338int of_property_match_string(struct device_node *np, const char *propname,
1339 const char *string)
1340{
1341 struct property *prop = of_find_property(np, propname, NULL);
1342 size_t l;
1343 int i;
1344 const char *p, *end;
1345
1346 if (!prop)
1347 return -EINVAL;
1348 if (!prop->value)
1349 return -ENODATA;
1350
1351 p = prop->value;
1352 end = p + prop->length;
1353
1354 for (i = 0; p < end; i++, p += l) {
1355 l = strlen(p) + 1;
1356 if (p + l > end)
1357 return -EILSEQ;
1358 pr_debug("comparing %s with %s\n", string, p);
1359 if (strcmp(string, p) == 0)
1360 return i; /* Found it; return index */
1361 }
1362 return -ENODATA;
1363}
1364EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001365
1366/**
1367 * of_property_count_strings - Find and return the number of strings from a
1368 * multiple strings property.
1369 * @np: device node from which the property value is to be read.
1370 * @propname: name of the property to be searched.
1371 *
1372 * Search for a property in a device tree node and retrieve the number of null
1373 * terminated string contain in it. Returns the number of strings on
1374 * success, -EINVAL if the property does not exist, -ENODATA if property
1375 * does not have a value, and -EILSEQ if the string is not null-terminated
1376 * within the length of the property data.
1377 */
1378int of_property_count_strings(struct device_node *np, const char *propname)
1379{
1380 struct property *prop = of_find_property(np, propname, NULL);
1381 int i = 0;
1382 size_t l = 0, total = 0;
1383 const char *p;
1384
1385 if (!prop)
1386 return -EINVAL;
1387 if (!prop->value)
1388 return -ENODATA;
1389 if (strnlen(prop->value, prop->length) >= prop->length)
1390 return -EILSEQ;
1391
1392 p = prop->value;
1393
Benoit Cousson88af7f52011-12-05 15:23:54 +01001394 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001395 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001396
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001397 return i;
1398}
1399EXPORT_SYMBOL_GPL(of_property_count_strings);
1400
Grant Likely624cfca2013-10-11 22:05:10 +01001401void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1402{
1403 int i;
1404 printk("%s %s", msg, of_node_full_name(args->np));
1405 for (i = 0; i < args->args_count; i++)
1406 printk(i ? ",%08x" : ":%08x", args->args[i]);
1407 printk("\n");
1408}
1409
Grant Likelybd69f732013-02-10 22:57:21 +00001410static int __of_parse_phandle_with_args(const struct device_node *np,
1411 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001412 const char *cells_name,
1413 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001414 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001415{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001416 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001417 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001418 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001419 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001420 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001421
Grant Likely15c9a0a2011-12-12 09:25:57 -07001422 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001423 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001424 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001425 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001426 list_end = list + size / sizeof(*list);
1427
Grant Likely15c9a0a2011-12-12 09:25:57 -07001428 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001429 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001430 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001431 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001432
Grant Likely15c9a0a2011-12-12 09:25:57 -07001433 /*
1434 * If phandle is 0, then it is an empty entry with no
1435 * arguments. Skip forward to the next entry.
1436 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001437 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001438 if (phandle) {
1439 /*
1440 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001441 * property to determine the argument length.
1442 *
1443 * This is not needed if the cell count is hard-coded
1444 * (i.e. cells_name not set, but cell_count is set),
1445 * except when we're going to return the found node
1446 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001447 */
Stephen Warren91d99422013-08-14 15:27:11 -06001448 if (cells_name || cur_index == index) {
1449 node = of_find_node_by_phandle(phandle);
1450 if (!node) {
1451 pr_err("%s: could not find phandle\n",
1452 np->full_name);
1453 goto err;
1454 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001455 }
Stephen Warren035fd942013-08-14 15:27:10 -06001456
1457 if (cells_name) {
1458 if (of_property_read_u32(node, cells_name,
1459 &count)) {
1460 pr_err("%s: could not get %s for %s\n",
1461 np->full_name, cells_name,
1462 node->full_name);
1463 goto err;
1464 }
1465 } else {
1466 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001467 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001468
Grant Likely15c9a0a2011-12-12 09:25:57 -07001469 /*
1470 * Make sure that the arguments actually fit in the
1471 * remaining property data length
1472 */
1473 if (list + count > list_end) {
1474 pr_err("%s: arguments longer than property\n",
1475 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001476 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001477 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001478 }
1479
Grant Likely15c9a0a2011-12-12 09:25:57 -07001480 /*
1481 * All of the error cases above bail out of the loop, so at
1482 * this point, the parsing is successful. If the requested
1483 * index matches, then fill the out_args structure and return,
1484 * or return -ENOENT for an empty entry.
1485 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001486 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001487 if (cur_index == index) {
1488 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001489 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001490
Grant Likely15c9a0a2011-12-12 09:25:57 -07001491 if (out_args) {
1492 int i;
1493 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1494 count = MAX_PHANDLE_ARGS;
1495 out_args->np = node;
1496 out_args->args_count = count;
1497 for (i = 0; i < count; i++)
1498 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001499 } else {
1500 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001501 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001502
1503 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001504 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001505 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001506
1507 of_node_put(node);
1508 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001509 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001510 cur_index++;
1511 }
1512
Grant Likely23ce04c2013-02-12 21:21:49 +00001513 /*
1514 * Unlock node before returning result; will be one of:
1515 * -ENOENT : index is for empty phandle
1516 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001517 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001518 */
Grant Likelybd69f732013-02-10 22:57:21 +00001519 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001520 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001521 if (node)
1522 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001523 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001524}
Grant Likelybd69f732013-02-10 22:57:21 +00001525
Stephen Warreneded9dd2013-08-14 15:27:08 -06001526/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001527 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1528 * @np: Pointer to device node holding phandle property
1529 * @phandle_name: Name of property holding a phandle value
1530 * @index: For properties holding a table of phandles, this is the index into
1531 * the table
1532 *
1533 * Returns the device_node pointer with refcount incremented. Use
1534 * of_node_put() on it when done.
1535 */
1536struct device_node *of_parse_phandle(const struct device_node *np,
1537 const char *phandle_name, int index)
1538{
Stephen Warren91d99422013-08-14 15:27:11 -06001539 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001540
Stephen Warren91d99422013-08-14 15:27:11 -06001541 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001542 return NULL;
1543
Stephen Warren91d99422013-08-14 15:27:11 -06001544 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1545 index, &args))
1546 return NULL;
1547
1548 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001549}
1550EXPORT_SYMBOL(of_parse_phandle);
1551
1552/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001553 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1554 * @np: pointer to a device tree node containing a list
1555 * @list_name: property name that contains a list
1556 * @cells_name: property name that specifies phandles' arguments count
1557 * @index: index of a phandle to parse out
1558 * @out_args: optional pointer to output arguments structure (will be filled)
1559 *
1560 * This function is useful to parse lists of phandles and their arguments.
1561 * Returns 0 on success and fills out_args, on error returns appropriate
1562 * errno value.
1563 *
1564 * Caller is responsible to call of_node_put() on the returned out_args->node
1565 * pointer.
1566 *
1567 * Example:
1568 *
1569 * phandle1: node1 {
1570 * #list-cells = <2>;
1571 * }
1572 *
1573 * phandle2: node2 {
1574 * #list-cells = <1>;
1575 * }
1576 *
1577 * node3 {
1578 * list = <&phandle1 1 2 &phandle2 3>;
1579 * }
1580 *
1581 * To get a device_node of the `node2' node you may call this:
1582 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1583 */
Grant Likelybd69f732013-02-10 22:57:21 +00001584int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1585 const char *cells_name, int index,
1586 struct of_phandle_args *out_args)
1587{
1588 if (index < 0)
1589 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001590 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1591 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001592}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001593EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001594
Grant Likelybd69f732013-02-10 22:57:21 +00001595/**
Stephen Warren035fd942013-08-14 15:27:10 -06001596 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1597 * @np: pointer to a device tree node containing a list
1598 * @list_name: property name that contains a list
1599 * @cell_count: number of argument cells following the phandle
1600 * @index: index of a phandle to parse out
1601 * @out_args: optional pointer to output arguments structure (will be filled)
1602 *
1603 * This function is useful to parse lists of phandles and their arguments.
1604 * Returns 0 on success and fills out_args, on error returns appropriate
1605 * errno value.
1606 *
1607 * Caller is responsible to call of_node_put() on the returned out_args->node
1608 * pointer.
1609 *
1610 * Example:
1611 *
1612 * phandle1: node1 {
1613 * }
1614 *
1615 * phandle2: node2 {
1616 * }
1617 *
1618 * node3 {
1619 * list = <&phandle1 0 2 &phandle2 2 3>;
1620 * }
1621 *
1622 * To get a device_node of the `node2' node you may call this:
1623 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1624 */
1625int of_parse_phandle_with_fixed_args(const struct device_node *np,
1626 const char *list_name, int cell_count,
1627 int index, struct of_phandle_args *out_args)
1628{
1629 if (index < 0)
1630 return -EINVAL;
1631 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1632 index, out_args);
1633}
1634EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1635
1636/**
Grant Likelybd69f732013-02-10 22:57:21 +00001637 * of_count_phandle_with_args() - Find the number of phandles references in a property
1638 * @np: pointer to a device tree node containing a list
1639 * @list_name: property name that contains a list
1640 * @cells_name: property name that specifies phandles' arguments count
1641 *
1642 * Returns the number of phandle + argument tuples within a property. It
1643 * is a typical pattern to encode a list of phandle and variable
1644 * arguments into a single property. The number of arguments is encoded
1645 * by a property in the phandle-target node. For example, a gpios
1646 * property would contain a list of GPIO specifies consisting of a
1647 * phandle and 1 or more arguments. The number of arguments are
1648 * determined by the #gpio-cells property in the node pointed to by the
1649 * phandle.
1650 */
1651int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1652 const char *cells_name)
1653{
Stephen Warren035fd942013-08-14 15:27:10 -06001654 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1655 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001656}
1657EXPORT_SYMBOL(of_count_phandle_with_args);
1658
Grant Likely02af11b2009-11-23 20:16:45 -07001659/**
Xiubo Li62664f62014-01-22 13:57:39 +08001660 * __of_add_property - Add a property to a node without lock operations
1661 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001662int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001663{
1664 struct property **next;
1665
1666 prop->next = NULL;
1667 next = &np->properties;
1668 while (*next) {
1669 if (strcmp(prop->name, (*next)->name) == 0)
1670 /* duplicate ! don't insert it */
1671 return -EEXIST;
1672
1673 next = &(*next)->next;
1674 }
1675 *next = prop;
1676
1677 return 0;
1678}
1679
1680/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001681 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001682 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001683int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001684{
Grant Likely02af11b2009-11-23 20:16:45 -07001685 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001686 int rc;
1687
1688 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1689 if (rc)
1690 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001691
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001692 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001693 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001694 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely75b57ec2014-02-20 18:02:11 +00001695 if (rc)
1696 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001697
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001698 if (of_node_is_attached(np))
1699 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001700
Xiubo Li62664f62014-01-22 13:57:39 +08001701 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001702}
1703
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001704int __of_remove_property(struct device_node *np, struct property *prop)
1705{
1706 struct property **next;
1707
1708 for (next = &np->properties; *next; next = &(*next)->next) {
1709 if (*next == prop)
1710 break;
1711 }
1712 if (*next == NULL)
1713 return -ENODEV;
1714
1715 /* found the node */
1716 *next = prop->next;
1717 prop->next = np->deadprops;
1718 np->deadprops = prop;
1719
1720 return 0;
1721}
1722
Grant Likely02af11b2009-11-23 20:16:45 -07001723/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001724 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001725 *
1726 * Note that we don't actually remove it, since we have given out
1727 * who-knows-how-many pointers to the data using get-property.
1728 * Instead we just move the property to the "dead properties"
1729 * list, so it won't be found any more.
1730 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001731int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001732{
Grant Likely02af11b2009-11-23 20:16:45 -07001733 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001734 int rc;
1735
1736 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1737 if (rc)
1738 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001739
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001740 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001741 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001742 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001743
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001744 if (rc)
1745 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001746
Grant Likely75b57ec2014-02-20 18:02:11 +00001747 /* at early boot, bail hear and defer setup to of_init() */
1748 if (!of_kset)
1749 return 0;
1750
1751 sysfs_remove_bin_file(&np->kobj, &prop->attr);
Grant Likely02af11b2009-11-23 20:16:45 -07001752
1753 return 0;
1754}
1755
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001756int __of_update_property(struct device_node *np, struct property *newprop,
1757 struct property **oldpropp)
1758{
1759 struct property **next, *oldprop;
1760
1761 for (next = &np->properties; *next; next = &(*next)->next) {
1762 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1763 break;
1764 }
1765 *oldpropp = oldprop = *next;
1766
1767 if (oldprop) {
1768 /* replace the node */
1769 newprop->next = oldprop->next;
1770 *next = newprop;
1771 oldprop->next = np->deadprops;
1772 np->deadprops = oldprop;
1773 } else {
1774 /* new node */
1775 newprop->next = NULL;
1776 *next = newprop;
1777 }
1778
1779 return 0;
1780}
1781
Grant Likely02af11b2009-11-23 20:16:45 -07001782/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001783 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001784 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001785 *
1786 * Note that we don't actually remove it, since we have given out
1787 * who-knows-how-many pointers to the data using get-property.
1788 * Instead we just move the property to the "dead properties" list,
1789 * and add the new property to the property list
1790 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001791int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001792{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001793 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001794 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001795 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001796
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001797 if (!newprop->name)
1798 return -EINVAL;
1799
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001800 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1801 if (rc)
1802 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001803
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001804 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001805 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001806 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Xiubo Li947fdaad02014-04-17 15:48:29 +08001807 if (rc)
1808 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001809
Thomas Petazzoni582da652014-05-14 13:36:36 +02001810 /* At early boot, bail out and defer setup to of_init() */
1811 if (!of_kset)
Xiubo Li947fdaad02014-04-17 15:48:29 +08001812 return 0;
Thomas Petazzoni582da652014-05-14 13:36:36 +02001813
Guenter Roecke7a62df2014-04-15 08:38:17 -07001814 /* Update the sysfs attribute */
Xiubo Li947fdaad02014-04-17 15:48:29 +08001815 if (oldprop)
1816 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
Guenter Roecke7a62df2014-04-15 08:38:17 -07001817 __of_add_property_sysfs(np, newprop);
1818
Grant Likely02af11b2009-11-23 20:16:45 -07001819 return 0;
1820}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001821
Shawn Guo611cad72011-08-15 15:28:14 +08001822static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1823 int id, const char *stem, int stem_len)
1824{
1825 ap->np = np;
1826 ap->id = id;
1827 strncpy(ap->stem, stem, stem_len);
1828 ap->stem[stem_len] = 0;
1829 list_add_tail(&ap->link, &aliases_lookup);
1830 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001831 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001832}
1833
1834/**
1835 * of_alias_scan - Scan all properties of 'aliases' node
1836 *
1837 * The function scans all the properties of 'aliases' node and populate
1838 * the the global lookup table with the properties. It returns the
1839 * number of alias_prop found, or error code in error case.
1840 *
1841 * @dt_alloc: An allocator that provides a virtual address to memory
1842 * for the resulting tree
1843 */
1844void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1845{
1846 struct property *pp;
1847
1848 of_chosen = of_find_node_by_path("/chosen");
1849 if (of_chosen == NULL)
1850 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001851
1852 if (of_chosen) {
Grant Likely676e1b22014-03-27 17:11:23 -07001853 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
1854 if (!name)
1855 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001856 if (name)
1857 of_stdout = of_find_node_by_path(name);
1858 }
1859
Shawn Guo611cad72011-08-15 15:28:14 +08001860 of_aliases = of_find_node_by_path("/aliases");
1861 if (!of_aliases)
1862 return;
1863
Dong Aisheng8af0da92011-12-22 20:19:24 +08001864 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001865 const char *start = pp->name;
1866 const char *end = start + strlen(start);
1867 struct device_node *np;
1868 struct alias_prop *ap;
1869 int id, len;
1870
1871 /* Skip those we do not want to proceed */
1872 if (!strcmp(pp->name, "name") ||
1873 !strcmp(pp->name, "phandle") ||
1874 !strcmp(pp->name, "linux,phandle"))
1875 continue;
1876
1877 np = of_find_node_by_path(pp->value);
1878 if (!np)
1879 continue;
1880
1881 /* walk the alias backwards to extract the id and work out
1882 * the 'stem' string */
1883 while (isdigit(*(end-1)) && end > start)
1884 end--;
1885 len = end - start;
1886
1887 if (kstrtoint(end, 10, &id) < 0)
1888 continue;
1889
1890 /* Allocate an alias_prop with enough space for the stem */
1891 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1892 if (!ap)
1893 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001894 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001895 ap->alias = start;
1896 of_alias_add(ap, np, id, start, len);
1897 }
1898}
1899
1900/**
1901 * of_alias_get_id - Get alias id for the given device_node
1902 * @np: Pointer to the given device_node
1903 * @stem: Alias stem of the given device_node
1904 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01001905 * The function travels the lookup table to get the alias id for the given
1906 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08001907 */
1908int of_alias_get_id(struct device_node *np, const char *stem)
1909{
1910 struct alias_prop *app;
1911 int id = -ENODEV;
1912
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001913 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001914 list_for_each_entry(app, &aliases_lookup, link) {
1915 if (strcmp(app->stem, stem) != 0)
1916 continue;
1917
1918 if (np == app->np) {
1919 id = app->id;
1920 break;
1921 }
1922 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001923 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001924
1925 return id;
1926}
1927EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001928
1929const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1930 u32 *pu)
1931{
1932 const void *curv = cur;
1933
1934 if (!prop)
1935 return NULL;
1936
1937 if (!cur) {
1938 curv = prop->value;
1939 goto out_val;
1940 }
1941
1942 curv += sizeof(*cur);
1943 if (curv >= prop->value + prop->length)
1944 return NULL;
1945
1946out_val:
1947 *pu = be32_to_cpup(curv);
1948 return curv;
1949}
1950EXPORT_SYMBOL_GPL(of_prop_next_u32);
1951
1952const char *of_prop_next_string(struct property *prop, const char *cur)
1953{
1954 const void *curv = cur;
1955
1956 if (!prop)
1957 return NULL;
1958
1959 if (!cur)
1960 return prop->value;
1961
1962 curv += strlen(cur) + 1;
1963 if (curv >= prop->value + prop->length)
1964 return NULL;
1965
1966 return curv;
1967}
1968EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001969
1970/**
1971 * of_device_is_stdout_path - check if a device node matches the
1972 * linux,stdout-path property
1973 *
1974 * Check if this device node matches the linux,stdout-path property
1975 * in the chosen node. return true if yes, false otherwise.
1976 */
1977int of_device_is_stdout_path(struct device_node *dn)
1978{
1979 if (!of_stdout)
1980 return false;
1981
1982 return of_stdout == dn;
1983}
1984EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001985
1986/**
1987 * of_find_next_cache_node - Find a node's subsidiary cache
1988 * @np: node of type "cpu" or "cache"
1989 *
1990 * Returns a node pointer with refcount incremented, use
1991 * of_node_put() on it when done. Caller should hold a reference
1992 * to np.
1993 */
1994struct device_node *of_find_next_cache_node(const struct device_node *np)
1995{
1996 struct device_node *child;
1997 const phandle *handle;
1998
1999 handle = of_get_property(np, "l2-cache", NULL);
2000 if (!handle)
2001 handle = of_get_property(np, "next-level-cache", NULL);
2002
2003 if (handle)
2004 return of_find_node_by_phandle(be32_to_cpup(handle));
2005
2006 /* OF on pmac has nodes instead of properties named "l2-cache"
2007 * beneath CPU nodes.
2008 */
2009 if (!strcmp(np->type, "cpu"))
2010 for_each_child_of_node(np, child)
2011 if (!strcmp(child->type, "cache"))
2012 return child;
2013
2014 return NULL;
2015}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002016
2017/**
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002018 * of_graph_parse_endpoint() - parse common endpoint node properties
2019 * @node: pointer to endpoint device_node
2020 * @endpoint: pointer to the OF endpoint data structure
2021 *
2022 * The caller should hold a reference to @node.
2023 */
2024int of_graph_parse_endpoint(const struct device_node *node,
2025 struct of_endpoint *endpoint)
2026{
2027 struct device_node *port_node = of_get_parent(node);
2028
Philipp Zabeld4847002014-03-04 12:31:24 +01002029 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2030 __func__, node->full_name);
2031
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002032 memset(endpoint, 0, sizeof(*endpoint));
2033
2034 endpoint->local_node = node;
2035 /*
2036 * It doesn't matter whether the two calls below succeed.
2037 * If they don't then the default value 0 is used.
2038 */
2039 of_property_read_u32(port_node, "reg", &endpoint->port);
2040 of_property_read_u32(node, "reg", &endpoint->id);
2041
2042 of_node_put(port_node);
2043
2044 return 0;
2045}
2046EXPORT_SYMBOL(of_graph_parse_endpoint);
2047
2048/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002049 * of_graph_get_next_endpoint() - get next endpoint node
2050 * @parent: pointer to the parent device node
2051 * @prev: previous endpoint node, or NULL to get first
2052 *
2053 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2054 * of the passed @prev node is not decremented, the caller have to use
2055 * of_node_put() on it when done.
2056 */
2057struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2058 struct device_node *prev)
2059{
2060 struct device_node *endpoint;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002061 struct device_node *port;
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002062
2063 if (!parent)
2064 return NULL;
2065
Linus Torvalds3c83e612014-04-04 09:50:07 -07002066 /*
2067 * Start by locating the port node. If no previous endpoint is specified
2068 * search for the first port node, otherwise get the previous endpoint
2069 * parent port node.
2070 */
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002071 if (!prev) {
2072 struct device_node *node;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002073
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002074 node = of_get_child_by_name(parent, "ports");
2075 if (node)
2076 parent = node;
2077
2078 port = of_get_child_by_name(parent, "port");
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002079 of_node_put(node);
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002080
Linus Torvalds3c83e612014-04-04 09:50:07 -07002081 if (!port) {
2082 pr_err("%s(): no port node found in %s\n",
2083 __func__, parent->full_name);
Philipp Zabel4329b932014-02-26 20:43:42 +01002084 return NULL;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002085 }
2086 } else {
2087 port = of_get_parent(prev);
2088 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2089 __func__, prev->full_name))
2090 return NULL;
Philipp Zabel4329b932014-02-26 20:43:42 +01002091
Linus Torvalds3c83e612014-04-04 09:50:07 -07002092 /*
2093 * Avoid dropping prev node refcount to 0 when getting the next
2094 * child below.
2095 */
2096 of_node_get(prev);
2097 }
Philipp Zabel4329b932014-02-26 20:43:42 +01002098
Linus Torvalds3c83e612014-04-04 09:50:07 -07002099 while (1) {
2100 /*
2101 * Now that we have a port node, get the next endpoint by
2102 * getting the next child. If the previous endpoint is NULL this
2103 * will return the first child.
2104 */
2105 endpoint = of_get_next_child(port, prev);
2106 if (endpoint) {
2107 of_node_put(port);
2108 return endpoint;
2109 }
2110
2111 /* No more endpoints under this port, try the next one. */
2112 prev = NULL;
2113
2114 do {
2115 port = of_get_next_child(parent, port);
2116 if (!port)
2117 return NULL;
2118 } while (of_node_cmp(port->name, "port"));
2119 }
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002120}
2121EXPORT_SYMBOL(of_graph_get_next_endpoint);
2122
2123/**
2124 * of_graph_get_remote_port_parent() - get remote port's parent node
2125 * @node: pointer to a local endpoint device_node
2126 *
2127 * Return: Remote device node associated with remote endpoint node linked
2128 * to @node. Use of_node_put() on it when done.
2129 */
2130struct device_node *of_graph_get_remote_port_parent(
2131 const struct device_node *node)
2132{
2133 struct device_node *np;
2134 unsigned int depth;
2135
2136 /* Get remote endpoint node. */
2137 np = of_parse_phandle(node, "remote-endpoint", 0);
2138
2139 /* Walk 3 levels up only if there is 'ports' node. */
2140 for (depth = 3; depth && np; depth--) {
2141 np = of_get_next_parent(np);
2142 if (depth == 2 && of_node_cmp(np->name, "ports"))
2143 break;
2144 }
2145 return np;
2146}
2147EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2148
2149/**
2150 * of_graph_get_remote_port() - get remote port node
2151 * @node: pointer to a local endpoint device_node
2152 *
2153 * Return: Remote port node associated with remote endpoint node linked
2154 * to @node. Use of_node_put() on it when done.
2155 */
2156struct device_node *of_graph_get_remote_port(const struct device_node *node)
2157{
2158 struct device_node *np;
2159
2160 /* Get remote endpoint node. */
2161 np = of_parse_phandle(node, "remote-endpoint", 0);
2162 if (!np)
2163 return NULL;
2164 return of_get_next_parent(np);
2165}
2166EXPORT_SYMBOL(of_graph_get_remote_port);