blob: ad4929cbd876fd49c5b1623e25b5e17d7ee150e8 [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 Likely8a2b22a2014-07-23 17:05:06 -060040struct kset *of_kset;
Grant Likely75b57ec2014-02-20 18:02:11 +000041
42/*
Grant Likely8a2b22a2014-07-23 17:05:06 -060043 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
44 * This mutex must be held whenever modifications are being made to the
45 * device tree. The of_{attach,detach}_node() and
46 * of_{add,remove,update}_property() helpers make sure this happens.
Grant Likely75b57ec2014-02-20 18:02:11 +000047 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030048DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100049
Stephen Rothwell581b6052007-04-24 16:46:53 +100050/* use when traversing tree through the allnext, child, sibling,
51 * or parent members of struct device_node.
52 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050053DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100054
55int of_n_addr_cells(struct device_node *np)
56{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060057 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100058
59 do {
60 if (np->parent)
61 np = np->parent;
62 ip = of_get_property(np, "#address-cells", NULL);
63 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070064 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100065 } while (np->parent);
66 /* No #address-cells property for the root node */
67 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
68}
69EXPORT_SYMBOL(of_n_addr_cells);
70
71int of_n_size_cells(struct device_node *np)
72{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060073 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100074
75 do {
76 if (np->parent)
77 np = np->parent;
78 ip = of_get_property(np, "#size-cells", NULL);
79 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070080 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100081 } while (np->parent);
82 /* No #size-cells property for the root node */
83 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
84}
85EXPORT_SYMBOL(of_n_size_cells);
86
Rob Herring0c3f0612013-09-17 10:42:50 -050087#ifdef CONFIG_NUMA
88int __weak of_node_to_nid(struct device_node *np)
89{
90 return numa_node_id();
91}
92#endif
93
Grant Likely6afc0dc2014-06-26 15:40:48 +010094#ifndef CONFIG_OF_DYNAMIC
Grant Likely75b57ec2014-02-20 18:02:11 +000095static void of_node_release(struct kobject *kobj)
96{
97 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
98}
Grant Likely0f22dd32012-02-15 20:38:40 -070099#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700100
Grant Likely75b57ec2014-02-20 18:02:11 +0000101struct kobj_type of_node_ktype = {
102 .release = of_node_release,
103};
104
105static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
106 struct bin_attribute *bin_attr, char *buf,
107 loff_t offset, size_t count)
108{
109 struct property *pp = container_of(bin_attr, struct property, attr);
110 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
111}
112
113static const char *safe_name(struct kobject *kobj, const char *orig_name)
114{
115 const char *name = orig_name;
116 struct kernfs_node *kn;
117 int i = 0;
118
119 /* don't be a hero. After 16 tries give up */
120 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
121 sysfs_put(kn);
122 if (name != orig_name)
123 kfree(name);
124 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
125 }
126
127 if (name != orig_name)
128 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
129 kobject_name(kobj), name);
130 return name;
131}
132
Grant Likely8a2b22a2014-07-23 17:05:06 -0600133int __of_add_property_sysfs(struct device_node *np, struct property *pp)
Grant Likely75b57ec2014-02-20 18:02:11 +0000134{
135 int rc;
136
137 /* Important: Don't leak passwords */
138 bool secure = strncmp(pp->name, "security-", 9) == 0;
139
Grant Likely8a2b22a2014-07-23 17:05:06 -0600140 if (!of_kset || !of_node_is_attached(np))
141 return 0;
142
Grant Likely75b57ec2014-02-20 18:02:11 +0000143 sysfs_bin_attr_init(&pp->attr);
144 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
145 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
146 pp->attr.size = secure ? 0 : pp->length;
147 pp->attr.read = of_node_property_read;
148
149 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
150 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
151 return rc;
152}
153
Grant Likely8a2b22a2014-07-23 17:05:06 -0600154int __of_attach_node_sysfs(struct device_node *np)
Grant Likely75b57ec2014-02-20 18:02:11 +0000155{
156 const char *name;
157 struct property *pp;
158 int rc;
159
Grant Likely8a2b22a2014-07-23 17:05:06 -0600160 if (!of_kset)
161 return 0;
162
Grant Likely75b57ec2014-02-20 18:02:11 +0000163 np->kobj.kset = of_kset;
164 if (!np->parent) {
165 /* Nodes without parents are new top level trees */
Kees Cook28d3ee42014-06-10 09:57:00 -0700166 rc = kobject_add(&np->kobj, NULL, "%s",
167 safe_name(&of_kset->kobj, "base"));
Grant Likely75b57ec2014-02-20 18:02:11 +0000168 } else {
169 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
170 if (!name || !name[0])
171 return -EINVAL;
172
173 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
174 }
175 if (rc)
176 return rc;
177
178 for_each_property_of_node(np, pp)
179 __of_add_property_sysfs(np, pp);
180
181 return 0;
182}
183
Grant Likely75b57ec2014-02-20 18:02:11 +0000184static int __init of_init(void)
185{
186 struct device_node *np;
187
188 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300189 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000190 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
191 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300192 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000193 return -ENOMEM;
194 }
195 for_each_of_allnodes(np)
Grant Likely8a2b22a2014-07-23 17:05:06 -0600196 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300197 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000198
Grant Likely83570412012-11-06 21:03:27 +0000199 /* Symlink in /proc as required by userspace ABI */
Grant Likely75b57ec2014-02-20 18:02:11 +0000200 if (of_allnodes)
201 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000202
203 return 0;
204}
205core_initcall(of_init);
206
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500207static struct property *__of_find_property(const struct device_node *np,
208 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000209{
210 struct property *pp;
211
Timur Tabi64e45662008-05-08 05:19:59 +1000212 if (!np)
213 return NULL;
214
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530215 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000216 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530217 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000218 *lenp = pp->length;
219 break;
220 }
221 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500222
223 return pp;
224}
225
226struct property *of_find_property(const struct device_node *np,
227 const char *name,
228 int *lenp)
229{
230 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500231 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500232
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500233 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500234 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500235 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000236
237 return pp;
238}
239EXPORT_SYMBOL(of_find_property);
240
Grant Likelye91edcf2009-10-15 10:58:09 -0600241/**
242 * of_find_all_nodes - Get next node in global list
243 * @prev: Previous node or NULL to start iteration
244 * of_node_put() will be called on it
245 *
246 * Returns a node pointer with refcount incremented, use
247 * of_node_put() on it when done.
248 */
249struct device_node *of_find_all_nodes(struct device_node *prev)
250{
251 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000252 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600253
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000254 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000255 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600256 for (; np != NULL; np = np->allnext)
257 if (of_node_get(np))
258 break;
259 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000260 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600261 return np;
262}
263EXPORT_SYMBOL(of_find_all_nodes);
264
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000265/*
266 * Find a property with a given name for a given node
267 * and return the value.
268 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500269static const void *__of_get_property(const struct device_node *np,
270 const char *name, int *lenp)
271{
272 struct property *pp = __of_find_property(np, name, lenp);
273
274 return pp ? pp->value : NULL;
275}
276
277/*
278 * Find a property with a given name for a given node
279 * and return the value.
280 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000281const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500282 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000283{
284 struct property *pp = of_find_property(np, name, lenp);
285
286 return pp ? pp->value : NULL;
287}
288EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000289
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100290/*
291 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
292 *
293 * @cpu: logical cpu index of a core/thread
294 * @phys_id: physical identifier of a core/thread
295 *
296 * CPU logical to physical index mapping is architecture specific.
297 * However this __weak function provides a default match of physical
298 * id to logical cpu index. phys_id provided here is usually values read
299 * from the device tree which must match the hardware internal registers.
300 *
301 * Returns true if the physical identifier and the logical cpu index
302 * correspond to the same core/thread, false otherwise.
303 */
304bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
305{
306 return (u32)phys_id == cpu;
307}
308
309/**
310 * Checks if the given "prop_name" property holds the physical id of the
311 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
312 * NULL, local thread number within the core is returned in it.
313 */
314static bool __of_find_n_match_cpu_property(struct device_node *cpun,
315 const char *prop_name, int cpu, unsigned int *thread)
316{
317 const __be32 *cell;
318 int ac, prop_len, tid;
319 u64 hwid;
320
321 ac = of_n_addr_cells(cpun);
322 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100323 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100324 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100325 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100326 for (tid = 0; tid < prop_len; tid++) {
327 hwid = of_read_number(cell, ac);
328 if (arch_match_cpu_phys_id(cpu, hwid)) {
329 if (thread)
330 *thread = tid;
331 return true;
332 }
333 cell += ac;
334 }
335 return false;
336}
337
David Millerd1cb9d12013-10-03 17:24:51 -0400338/*
339 * arch_find_n_match_cpu_physical_id - See if the given device node is
340 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
341 * else false. If 'thread' is non-NULL, the local thread number within the
342 * core is returned in it.
343 */
344bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
345 int cpu, unsigned int *thread)
346{
347 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
348 * for thread ids on PowerPC. If it doesn't exist fallback to
349 * standard "reg" property.
350 */
351 if (IS_ENABLED(CONFIG_PPC) &&
352 __of_find_n_match_cpu_property(cpun,
353 "ibm,ppc-interrupt-server#s",
354 cpu, thread))
355 return true;
356
357 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
358 return true;
359
360 return false;
361}
362
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100363/**
364 * of_get_cpu_node - Get device node associated with the given logical CPU
365 *
366 * @cpu: CPU number(logical index) for which device node is required
367 * @thread: if not NULL, local thread number within the physical core is
368 * returned
369 *
370 * The main purpose of this function is to retrieve the device node for the
371 * given logical CPU index. It should be used to initialize the of_node in
372 * cpu device. Once of_node in cpu device is populated, all the further
373 * references can use that instead.
374 *
375 * CPU logical to physical index mapping is architecture specific and is built
376 * before booting secondary cores. This function uses arch_match_cpu_phys_id
377 * which can be overridden by architecture specific implementation.
378 *
379 * Returns a node pointer for the logical cpu if found, else NULL.
380 */
381struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
382{
David Millerd1cb9d12013-10-03 17:24:51 -0400383 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100384
David Millerd1cb9d12013-10-03 17:24:51 -0400385 for_each_node_by_type(cpun, "cpu") {
386 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100387 return cpun;
388 }
389 return NULL;
390}
391EXPORT_SYMBOL(of_get_cpu_node);
392
Kevin Hao215a14c2014-02-19 16:15:45 +0800393/**
394 * __of_device_is_compatible() - Check if the node matches given constraints
395 * @device: pointer to node
396 * @compat: required compatible string, NULL or "" for any match
397 * @type: required device_type value, NULL or "" for any match
398 * @name: required node name, NULL or "" for any match
399 *
400 * Checks if the given @compat, @type and @name strings match the
401 * properties of the given @device. A constraints can be skipped by
402 * passing NULL or an empty string as the constraint.
403 *
404 * Returns 0 for no match, and a positive integer on match. The return
405 * value is a relative score with larger values indicating better
406 * matches. The score is weighted for the most specific compatible value
407 * to get the highest score. Matching type is next, followed by matching
408 * name. Practically speaking, this results in the following priority
409 * order for matches:
410 *
411 * 1. specific compatible && type && name
412 * 2. specific compatible && type
413 * 3. specific compatible && name
414 * 4. specific compatible
415 * 5. general compatible && type && name
416 * 6. general compatible && type
417 * 7. general compatible && name
418 * 8. general compatible
419 * 9. type && name
420 * 10. type
421 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000422 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500423static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800424 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000425{
Kevin Hao215a14c2014-02-19 16:15:45 +0800426 struct property *prop;
427 const char *cp;
428 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000429
Kevin Hao215a14c2014-02-19 16:15:45 +0800430 /* Compatible match has highest priority */
431 if (compat && compat[0]) {
432 prop = __of_find_property(device, "compatible", NULL);
433 for (cp = of_prop_next_string(prop, NULL); cp;
434 cp = of_prop_next_string(prop, cp), index++) {
435 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
436 score = INT_MAX/2 - (index << 2);
437 break;
438 }
439 }
440 if (!score)
441 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000442 }
443
Kevin Hao215a14c2014-02-19 16:15:45 +0800444 /* Matching type is better than matching name */
445 if (type && type[0]) {
446 if (!device->type || of_node_cmp(type, device->type))
447 return 0;
448 score += 2;
449 }
450
451 /* Matching name is a bit better than not */
452 if (name && name[0]) {
453 if (!device->name || of_node_cmp(name, device->name))
454 return 0;
455 score++;
456 }
457
458 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000459}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500460
461/** Checks if the given "compat" string matches one of the strings in
462 * the device's "compatible" property
463 */
464int of_device_is_compatible(const struct device_node *device,
465 const char *compat)
466{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500467 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500468 int res;
469
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500470 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800471 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500472 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500473 return res;
474}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000475EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000476
477/**
Grant Likely71a157e2010-02-01 21:34:14 -0700478 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700479 * @compat: compatible string to look for in root node's compatible property.
480 *
481 * Returns true if the root node has the given value in its
482 * compatible property.
483 */
Grant Likely71a157e2010-02-01 21:34:14 -0700484int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700485{
486 struct device_node *root;
487 int rc = 0;
488
489 root = of_find_node_by_path("/");
490 if (root) {
491 rc = of_device_is_compatible(root, compat);
492 of_node_put(root);
493 }
494 return rc;
495}
Grant Likely71a157e2010-02-01 21:34:14 -0700496EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700497
498/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700499 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100500 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700501 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100502 *
503 * Returns 1 if the status property is absent or set to "okay" or "ok",
504 * 0 otherwise
505 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700506static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100507{
508 const char *status;
509 int statlen;
510
Xiubo Li42ccd782014-01-13 11:07:28 +0800511 if (!device)
512 return 0;
513
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700514 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100515 if (status == NULL)
516 return 1;
517
518 if (statlen > 0) {
519 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
520 return 1;
521 }
522
523 return 0;
524}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700525
526/**
527 * of_device_is_available - check if a device is available for use
528 *
529 * @device: Node to check for availability
530 *
531 * Returns 1 if the status property is absent or set to "okay" or "ok",
532 * 0 otherwise
533 */
534int of_device_is_available(const struct device_node *device)
535{
536 unsigned long flags;
537 int res;
538
539 raw_spin_lock_irqsave(&devtree_lock, flags);
540 res = __of_device_is_available(device);
541 raw_spin_unlock_irqrestore(&devtree_lock, flags);
542 return res;
543
544}
Josh Boyer834d97d2008-03-27 00:33:14 +1100545EXPORT_SYMBOL(of_device_is_available);
546
547/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000548 * of_get_parent - Get a node's parent if any
549 * @node: Node to get parent
550 *
551 * Returns a node pointer with refcount incremented, use
552 * of_node_put() on it when done.
553 */
554struct device_node *of_get_parent(const struct device_node *node)
555{
556 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500557 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000558
559 if (!node)
560 return NULL;
561
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500562 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000563 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500564 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000565 return np;
566}
567EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000568
569/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000570 * of_get_next_parent - Iterate to a node's parent
571 * @node: Node to get parent of
572 *
573 * This is like of_get_parent() except that it drops the
574 * refcount on the passed node, making it suitable for iterating
575 * through a node's parents.
576 *
577 * Returns a node pointer with refcount incremented, use
578 * of_node_put() on it when done.
579 */
580struct device_node *of_get_next_parent(struct device_node *node)
581{
582 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500583 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000584
585 if (!node)
586 return NULL;
587
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500588 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000589 parent = of_node_get(node->parent);
590 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500591 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000592 return parent;
593}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300594EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000595
Grant Likely0d0e02d2014-05-22 01:04:17 +0900596static struct device_node *__of_get_next_child(const struct device_node *node,
597 struct device_node *prev)
598{
599 struct device_node *next;
600
Florian Fainelli43cb4362014-05-28 10:39:02 -0700601 if (!node)
602 return NULL;
603
Grant Likely0d0e02d2014-05-22 01:04:17 +0900604 next = prev ? prev->sibling : node->child;
605 for (; next; next = next->sibling)
606 if (of_node_get(next))
607 break;
608 of_node_put(prev);
609 return next;
610}
611#define __for_each_child_of_node(parent, child) \
612 for (child = __of_get_next_child(parent, NULL); child != NULL; \
613 child = __of_get_next_child(parent, child))
614
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000615/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000616 * of_get_next_child - Iterate a node childs
617 * @node: parent node
618 * @prev: previous child of the parent node, or NULL to get first
619 *
620 * Returns a node pointer with refcount incremented, use
621 * of_node_put() on it when done.
622 */
623struct device_node *of_get_next_child(const struct device_node *node,
624 struct device_node *prev)
625{
626 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500627 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000628
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500629 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900630 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500631 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000632 return next;
633}
634EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000635
636/**
Timur Tabi32961932012-08-14 13:20:23 +0000637 * of_get_next_available_child - Find the next available child node
638 * @node: parent node
639 * @prev: previous child of the parent node, or NULL to get first
640 *
641 * This function is like of_get_next_child(), except that it
642 * automatically skips any disabled nodes (i.e. status = "disabled").
643 */
644struct device_node *of_get_next_available_child(const struct device_node *node,
645 struct device_node *prev)
646{
647 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000648 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000649
Florian Fainelli43cb4362014-05-28 10:39:02 -0700650 if (!node)
651 return NULL;
652
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000653 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000654 next = prev ? prev->sibling : node->child;
655 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700656 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000657 continue;
658 if (of_node_get(next))
659 break;
660 }
661 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000662 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000663 return next;
664}
665EXPORT_SYMBOL(of_get_next_available_child);
666
667/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100668 * of_get_child_by_name - Find the child node by name for a given parent
669 * @node: parent node
670 * @name: child name to look for.
671 *
672 * This function looks for child node for given matching name
673 *
674 * Returns a node pointer if found, with refcount incremented, use
675 * of_node_put() on it when done.
676 * Returns NULL if node is not found.
677 */
678struct device_node *of_get_child_by_name(const struct device_node *node,
679 const char *name)
680{
681 struct device_node *child;
682
683 for_each_child_of_node(node, child)
684 if (child->name && (of_node_cmp(child->name, name) == 0))
685 break;
686 return child;
687}
688EXPORT_SYMBOL(of_get_child_by_name);
689
Grant Likelyc22e6502014-03-14 17:07:12 +0000690static struct device_node *__of_find_node_by_path(struct device_node *parent,
691 const char *path)
692{
693 struct device_node *child;
694 int len = strchrnul(path, '/') - path;
695
696 if (!len)
697 return NULL;
698
699 __for_each_child_of_node(parent, child) {
700 const char *name = strrchr(child->full_name, '/');
701 if (WARN(!name, "malformed device_node %s\n", child->full_name))
702 continue;
703 name++;
704 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
705 return child;
706 }
707 return NULL;
708}
709
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100710/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000711 * of_find_node_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000712 * @path: Either the full path to match, or if the path does not
713 * start with '/', the name of a property of the /aliases
714 * node (an alias). In the case of an alias, the node
715 * matching the alias' value will be returned.
716 *
717 * Valid paths:
718 * /foo/bar Full path
719 * foo Valid alias
720 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000721 *
722 * Returns a node pointer with refcount incremented, use
723 * of_node_put() on it when done.
724 */
725struct device_node *of_find_node_by_path(const char *path)
726{
Grant Likelyc22e6502014-03-14 17:07:12 +0000727 struct device_node *np = NULL;
728 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500729 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000730
Grant Likelyc22e6502014-03-14 17:07:12 +0000731 if (strcmp(path, "/") == 0)
732 return of_node_get(of_allnodes);
733
734 /* The path could begin with an alias */
735 if (*path != '/') {
736 char *p = strchrnul(path, '/');
737 int len = p - path;
738
739 /* of_aliases must not be NULL */
740 if (!of_aliases)
741 return NULL;
742
743 for_each_property_of_node(of_aliases, pp) {
744 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
745 np = of_find_node_by_path(pp->value);
746 break;
747 }
748 }
749 if (!np)
750 return NULL;
751 path = p;
752 }
753
754 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500755 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000756 if (!np)
757 np = of_node_get(of_allnodes);
758 while (np && *path == '/') {
759 path++; /* Increment past '/' delimiter */
760 np = __of_find_node_by_path(np, path);
761 path = strchrnul(path, '/');
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000762 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500763 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000764 return np;
765}
766EXPORT_SYMBOL(of_find_node_by_path);
767
768/**
769 * of_find_node_by_name - Find a node by its "name" property
770 * @from: The node to start searching from or NULL, the node
771 * you pass will not be searched, only the next one
772 * will; typically, you pass what the previous call
773 * returned. of_node_put() will be called on it
774 * @name: The name string to match against
775 *
776 * Returns a node pointer with refcount incremented, use
777 * of_node_put() on it when done.
778 */
779struct device_node *of_find_node_by_name(struct device_node *from,
780 const char *name)
781{
782 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500783 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000784
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500785 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000786 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000787 for (; np; np = np->allnext)
788 if (np->name && (of_node_cmp(np->name, name) == 0)
789 && of_node_get(np))
790 break;
791 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500792 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000793 return np;
794}
795EXPORT_SYMBOL(of_find_node_by_name);
796
797/**
798 * of_find_node_by_type - Find a node by its "device_type" property
799 * @from: The node to start searching from, or NULL to start searching
800 * the entire device tree. The node you pass will not be
801 * searched, only the next one will; typically, you pass
802 * what the previous call returned. of_node_put() will be
803 * called on from for you.
804 * @type: The type string to match against
805 *
806 * Returns a node pointer with refcount incremented, use
807 * of_node_put() on it when done.
808 */
809struct device_node *of_find_node_by_type(struct device_node *from,
810 const char *type)
811{
812 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500813 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000814
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500815 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000816 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000817 for (; np; np = np->allnext)
818 if (np->type && (of_node_cmp(np->type, type) == 0)
819 && of_node_get(np))
820 break;
821 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500822 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000823 return np;
824}
825EXPORT_SYMBOL(of_find_node_by_type);
826
827/**
828 * of_find_compatible_node - Find a node based on type and one of the
829 * tokens in its "compatible" property
830 * @from: The node to start searching from or NULL, the node
831 * you pass will not be searched, only the next one
832 * will; typically, you pass what the previous call
833 * returned. of_node_put() will be called on it
834 * @type: The type string to match "device_type" or NULL to ignore
835 * @compatible: The string to match to one of the tokens in the device
836 * "compatible" list.
837 *
838 * Returns a node pointer with refcount incremented, use
839 * of_node_put() on it when done.
840 */
841struct device_node *of_find_compatible_node(struct device_node *from,
842 const char *type, const char *compatible)
843{
844 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500845 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000846
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500847 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000848 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000849 for (; np; np = np->allnext) {
Kevin Hao215a14c2014-02-19 16:15:45 +0800850 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500851 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000852 break;
853 }
854 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500855 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000856 return np;
857}
858EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100859
860/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000861 * of_find_node_with_property - Find a node which has a property with
862 * the given name.
863 * @from: The node to start searching from or NULL, the node
864 * you pass will not be searched, only the next one
865 * will; typically, you pass what the previous call
866 * returned. of_node_put() will be called on it
867 * @prop_name: The name of the property to look for.
868 *
869 * Returns a node pointer with refcount incremented, use
870 * of_node_put() on it when done.
871 */
872struct device_node *of_find_node_with_property(struct device_node *from,
873 const char *prop_name)
874{
875 struct device_node *np;
876 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500877 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000878
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500879 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000880 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000881 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530882 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000883 if (of_prop_cmp(pp->name, prop_name) == 0) {
884 of_node_get(np);
885 goto out;
886 }
887 }
888 }
889out:
890 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500891 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000892 return np;
893}
894EXPORT_SYMBOL(of_find_node_with_property);
895
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500896static
897const struct of_device_id *__of_match_node(const struct of_device_id *matches,
898 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100899{
Kevin Hao215a14c2014-02-19 16:15:45 +0800900 const struct of_device_id *best_match = NULL;
901 int score, best_score = 0;
902
Grant Likelya52f07e2011-03-18 10:21:29 -0600903 if (!matches)
904 return NULL;
905
Kevin Hao215a14c2014-02-19 16:15:45 +0800906 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
907 score = __of_device_is_compatible(node, matches->compatible,
908 matches->type, matches->name);
909 if (score > best_score) {
910 best_match = matches;
911 best_score = score;
912 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +0800913 }
Kevin Hao215a14c2014-02-19 16:15:45 +0800914
915 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +1100916}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500917
918/**
919 * of_match_node - Tell if an device_node has a matching of_match structure
920 * @matches: array of of device match structures to search in
921 * @node: the of device structure to match against
922 *
Kevin Hao71c54982014-02-18 15:57:29 +0800923 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500924 */
925const struct of_device_id *of_match_node(const struct of_device_id *matches,
926 const struct device_node *node)
927{
928 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500929 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500930
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500931 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500932 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500933 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500934 return match;
935}
Grant Likely283029d2008-01-09 06:20:40 +1100936EXPORT_SYMBOL(of_match_node);
937
938/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700939 * of_find_matching_node_and_match - Find a node based on an of_device_id
940 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100941 * @from: The node to start searching from or NULL, the node
942 * you pass will not be searched, only the next one
943 * will; typically, you pass what the previous call
944 * returned. of_node_put() will be called on it
945 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700946 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100947 *
948 * Returns a node pointer with refcount incremented, use
949 * of_node_put() on it when done.
950 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700951struct device_node *of_find_matching_node_and_match(struct device_node *from,
952 const struct of_device_id *matches,
953 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100954{
955 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800956 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500957 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100958
Stephen Warren50c8af42012-11-20 16:12:20 -0700959 if (match)
960 *match = NULL;
961
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500962 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000963 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100964 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500965 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800966 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700967 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800968 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100969 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700970 }
Grant Likely283029d2008-01-09 06:20:40 +1100971 }
972 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500973 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100974 return np;
975}
Grant Likely80c20222012-12-19 10:45:36 +0000976EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400977
978/**
Grant Likely3f07af42008-07-25 22:25:13 -0400979 * of_modalias_node - Lookup appropriate modalias for a device node
980 * @node: pointer to a device tree node
981 * @modalias: Pointer to buffer that modalias value will be copied into
982 * @len: Length of modalias value
983 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600984 * Based on the value of the compatible property, this routine will attempt
985 * to choose an appropriate modalias value for a particular device tree node.
986 * It does this by stripping the manufacturer prefix (as delimited by a ',')
987 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400988 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600989 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400990 */
991int of_modalias_node(struct device_node *node, char *modalias, int len)
992{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600993 const char *compatible, *p;
994 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400995
996 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600997 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400998 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400999 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001000 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001001 return 0;
1002}
1003EXPORT_SYMBOL_GPL(of_modalias_node);
1004
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001005/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001006 * of_find_node_by_phandle - Find a node given a phandle
1007 * @handle: phandle of the node to find
1008 *
1009 * Returns a node pointer with refcount incremented, use
1010 * of_node_put() on it when done.
1011 */
1012struct device_node *of_find_node_by_phandle(phandle handle)
1013{
1014 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001015 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001016
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001017 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +00001018 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001019 if (np->phandle == handle)
1020 break;
1021 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001022 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001023 return np;
1024}
1025EXPORT_SYMBOL(of_find_node_by_phandle);
1026
1027/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +01001028 * of_property_count_elems_of_size - Count the number of elements in a property
1029 *
1030 * @np: device node from which the property value is to be read.
1031 * @propname: name of the property to be searched.
1032 * @elem_size: size of the individual element
1033 *
1034 * Search for a property in a device node and count the number of elements of
1035 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1036 * property does not exist or its length does not match a multiple of elem_size
1037 * and -ENODATA if the property does not have a value.
1038 */
1039int of_property_count_elems_of_size(const struct device_node *np,
1040 const char *propname, int elem_size)
1041{
1042 struct property *prop = of_find_property(np, propname, NULL);
1043
1044 if (!prop)
1045 return -EINVAL;
1046 if (!prop->value)
1047 return -ENODATA;
1048
1049 if (prop->length % elem_size != 0) {
1050 pr_err("size of %s in node %s is not a multiple of %d\n",
1051 propname, np->full_name, elem_size);
1052 return -EINVAL;
1053 }
1054
1055 return prop->length / elem_size;
1056}
1057EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1058
1059/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001060 * of_find_property_value_of_size
1061 *
1062 * @np: device node from which the property value is to be read.
1063 * @propname: name of the property to be searched.
1064 * @len: requested length of property value
1065 *
1066 * Search for a property in a device node and valid the requested size.
1067 * Returns the property value on success, -EINVAL if the property does not
1068 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1069 * property data isn't large enough.
1070 *
1071 */
1072static void *of_find_property_value_of_size(const struct device_node *np,
1073 const char *propname, u32 len)
1074{
1075 struct property *prop = of_find_property(np, propname, NULL);
1076
1077 if (!prop)
1078 return ERR_PTR(-EINVAL);
1079 if (!prop->value)
1080 return ERR_PTR(-ENODATA);
1081 if (len > prop->length)
1082 return ERR_PTR(-EOVERFLOW);
1083
1084 return prop->value;
1085}
1086
1087/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001088 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1089 *
1090 * @np: device node from which the property value is to be read.
1091 * @propname: name of the property to be searched.
1092 * @index: index of the u32 in the list of values
1093 * @out_value: pointer to return value, modified only if no error.
1094 *
1095 * Search for a property in a device node and read nth 32-bit value from
1096 * it. Returns 0 on success, -EINVAL if the property does not exist,
1097 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1098 * property data isn't large enough.
1099 *
1100 * The out_value is modified only if a valid u32 value can be decoded.
1101 */
1102int of_property_read_u32_index(const struct device_node *np,
1103 const char *propname,
1104 u32 index, u32 *out_value)
1105{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001106 const u32 *val = of_find_property_value_of_size(np, propname,
1107 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +13001108
Tony Priskdaeec1f2013-04-03 17:57:11 +13001109 if (IS_ERR(val))
1110 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001111
Tony Priskdaeec1f2013-04-03 17:57:11 +13001112 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001113 return 0;
1114}
1115EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1116
1117/**
Viresh Kumarbe193242012-11-20 10:15:19 +05301118 * of_property_read_u8_array - Find and read an array of u8 from a property.
1119 *
1120 * @np: device node from which the property value is to be read.
1121 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301122 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301123 * @sz: number of array elements to read
1124 *
1125 * Search for a property in a device node and read 8-bit value(s) from
1126 * it. Returns 0 on success, -EINVAL if the property does not exist,
1127 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1128 * property data isn't large enough.
1129 *
1130 * dts entry of array should be like:
1131 * property = /bits/ 8 <0x50 0x60 0x70>;
1132 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301133 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301134 */
1135int of_property_read_u8_array(const struct device_node *np,
1136 const char *propname, u8 *out_values, size_t sz)
1137{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001138 const u8 *val = of_find_property_value_of_size(np, propname,
1139 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301140
Tony Priskdaeec1f2013-04-03 17:57:11 +13001141 if (IS_ERR(val))
1142 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301143
Viresh Kumarbe193242012-11-20 10:15:19 +05301144 while (sz--)
1145 *out_values++ = *val++;
1146 return 0;
1147}
1148EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1149
1150/**
1151 * of_property_read_u16_array - Find and read an array of u16 from a property.
1152 *
1153 * @np: device node from which the property value is to be read.
1154 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301155 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301156 * @sz: number of array elements to read
1157 *
1158 * Search for a property in a device node and read 16-bit value(s) from
1159 * it. Returns 0 on success, -EINVAL if the property does not exist,
1160 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1161 * property data isn't large enough.
1162 *
1163 * dts entry of array should be like:
1164 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1165 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301166 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301167 */
1168int of_property_read_u16_array(const struct device_node *np,
1169 const char *propname, u16 *out_values, size_t sz)
1170{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001171 const __be16 *val = of_find_property_value_of_size(np, propname,
1172 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301173
Tony Priskdaeec1f2013-04-03 17:57:11 +13001174 if (IS_ERR(val))
1175 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301176
Viresh Kumarbe193242012-11-20 10:15:19 +05301177 while (sz--)
1178 *out_values++ = be16_to_cpup(val++);
1179 return 0;
1180}
1181EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1182
1183/**
Rob Herring0e373632011-07-06 15:42:58 -05001184 * of_property_read_u32_array - Find and read an array of 32 bit integers
1185 * from a property.
1186 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301187 * @np: device node from which the property value is to be read.
1188 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301189 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301190 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301191 *
Rob Herring0e373632011-07-06 15:42:58 -05001192 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301193 * it. Returns 0 on success, -EINVAL if the property does not exist,
1194 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1195 * property data isn't large enough.
1196 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301197 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301198 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001199int of_property_read_u32_array(const struct device_node *np,
1200 const char *propname, u32 *out_values,
1201 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301202{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001203 const __be32 *val = of_find_property_value_of_size(np, propname,
1204 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301205
Tony Priskdaeec1f2013-04-03 17:57:11 +13001206 if (IS_ERR(val))
1207 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001208
Rob Herring0e373632011-07-06 15:42:58 -05001209 while (sz--)
1210 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301211 return 0;
1212}
Rob Herring0e373632011-07-06 15:42:58 -05001213EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301214
1215/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001216 * of_property_read_u64 - Find and read a 64 bit integer from a property
1217 * @np: device node from which the property value is to be read.
1218 * @propname: name of the property to be searched.
1219 * @out_value: pointer to return value, modified only if return value is 0.
1220 *
1221 * Search for a property in a device node and read a 64-bit value from
1222 * it. Returns 0 on success, -EINVAL if the property does not exist,
1223 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1224 * property data isn't large enough.
1225 *
1226 * The out_value is modified only if a valid u64 value can be decoded.
1227 */
1228int of_property_read_u64(const struct device_node *np, const char *propname,
1229 u64 *out_value)
1230{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001231 const __be32 *val = of_find_property_value_of_size(np, propname,
1232 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001233
Tony Priskdaeec1f2013-04-03 17:57:11 +13001234 if (IS_ERR(val))
1235 return PTR_ERR(val);
1236
1237 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001238 return 0;
1239}
1240EXPORT_SYMBOL_GPL(of_property_read_u64);
1241
1242/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301243 * of_property_read_string - Find and read a string from a property
1244 * @np: device node from which the property value is to be read.
1245 * @propname: name of the property to be searched.
1246 * @out_string: pointer to null terminated return string, modified only if
1247 * return value is 0.
1248 *
1249 * Search for a property in a device tree node and retrieve a null
1250 * terminated string value (pointer to data, not a copy). Returns 0 on
1251 * success, -EINVAL if the property does not exist, -ENODATA if property
1252 * does not have a value, and -EILSEQ if the string is not null-terminated
1253 * within the length of the property data.
1254 *
1255 * The out_string pointer is modified only if a valid string can be decoded.
1256 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001257int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001258 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301259{
1260 struct property *prop = of_find_property(np, propname, NULL);
1261 if (!prop)
1262 return -EINVAL;
1263 if (!prop->value)
1264 return -ENODATA;
1265 if (strnlen(prop->value, prop->length) >= prop->length)
1266 return -EILSEQ;
1267 *out_string = prop->value;
1268 return 0;
1269}
1270EXPORT_SYMBOL_GPL(of_property_read_string);
1271
1272/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001273 * of_property_read_string_index - Find and read a string from a multiple
1274 * strings property.
1275 * @np: device node from which the property value is to be read.
1276 * @propname: name of the property to be searched.
1277 * @index: index of the string in the list of strings
1278 * @out_string: pointer to null terminated return string, modified only if
1279 * return value is 0.
1280 *
1281 * Search for a property in a device tree node and retrieve a null
1282 * terminated string value (pointer to data, not a copy) in the list of strings
1283 * contained in that property.
1284 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1285 * property does not have a value, and -EILSEQ if the string is not
1286 * null-terminated within the length of the property data.
1287 *
1288 * The out_string pointer is modified only if a valid string can be decoded.
1289 */
1290int of_property_read_string_index(struct device_node *np, const char *propname,
1291 int index, const char **output)
1292{
1293 struct property *prop = of_find_property(np, propname, NULL);
1294 int i = 0;
1295 size_t l = 0, total = 0;
1296 const char *p;
1297
1298 if (!prop)
1299 return -EINVAL;
1300 if (!prop->value)
1301 return -ENODATA;
1302 if (strnlen(prop->value, prop->length) >= prop->length)
1303 return -EILSEQ;
1304
1305 p = prop->value;
1306
1307 for (i = 0; total < prop->length; total += l, p += l) {
1308 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001309 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001310 *output = p;
1311 return 0;
1312 }
1313 }
1314 return -ENODATA;
1315}
1316EXPORT_SYMBOL_GPL(of_property_read_string_index);
1317
Grant Likely7aff0fe2011-12-12 09:25:58 -07001318/**
1319 * of_property_match_string() - Find string in a list and return index
1320 * @np: pointer to node containing string list property
1321 * @propname: string list property name
1322 * @string: pointer to string to search for in string list
1323 *
1324 * This function searches a string list property and returns the index
1325 * of a specific string value.
1326 */
1327int of_property_match_string(struct device_node *np, const char *propname,
1328 const char *string)
1329{
1330 struct property *prop = of_find_property(np, propname, NULL);
1331 size_t l;
1332 int i;
1333 const char *p, *end;
1334
1335 if (!prop)
1336 return -EINVAL;
1337 if (!prop->value)
1338 return -ENODATA;
1339
1340 p = prop->value;
1341 end = p + prop->length;
1342
1343 for (i = 0; p < end; i++, p += l) {
1344 l = strlen(p) + 1;
1345 if (p + l > end)
1346 return -EILSEQ;
1347 pr_debug("comparing %s with %s\n", string, p);
1348 if (strcmp(string, p) == 0)
1349 return i; /* Found it; return index */
1350 }
1351 return -ENODATA;
1352}
1353EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001354
1355/**
1356 * of_property_count_strings - Find and return the number of strings from a
1357 * multiple strings property.
1358 * @np: device node from which the property value is to be read.
1359 * @propname: name of the property to be searched.
1360 *
1361 * Search for a property in a device tree node and retrieve the number of null
1362 * terminated string contain in it. Returns the number of strings on
1363 * success, -EINVAL if the property does not exist, -ENODATA if property
1364 * does not have a value, and -EILSEQ if the string is not null-terminated
1365 * within the length of the property data.
1366 */
1367int of_property_count_strings(struct device_node *np, const char *propname)
1368{
1369 struct property *prop = of_find_property(np, propname, NULL);
1370 int i = 0;
1371 size_t l = 0, total = 0;
1372 const char *p;
1373
1374 if (!prop)
1375 return -EINVAL;
1376 if (!prop->value)
1377 return -ENODATA;
1378 if (strnlen(prop->value, prop->length) >= prop->length)
1379 return -EILSEQ;
1380
1381 p = prop->value;
1382
Benoit Cousson88af7f52011-12-05 15:23:54 +01001383 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001384 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001385
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001386 return i;
1387}
1388EXPORT_SYMBOL_GPL(of_property_count_strings);
1389
Grant Likely624cfca2013-10-11 22:05:10 +01001390void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1391{
1392 int i;
1393 printk("%s %s", msg, of_node_full_name(args->np));
1394 for (i = 0; i < args->args_count; i++)
1395 printk(i ? ",%08x" : ":%08x", args->args[i]);
1396 printk("\n");
1397}
1398
Grant Likelybd69f732013-02-10 22:57:21 +00001399static int __of_parse_phandle_with_args(const struct device_node *np,
1400 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001401 const char *cells_name,
1402 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001403 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001404{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001405 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001406 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001407 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001408 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001409 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001410
Grant Likely15c9a0a2011-12-12 09:25:57 -07001411 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001412 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001413 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001414 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001415 list_end = list + size / sizeof(*list);
1416
Grant Likely15c9a0a2011-12-12 09:25:57 -07001417 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001418 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001419 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001420 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001421
Grant Likely15c9a0a2011-12-12 09:25:57 -07001422 /*
1423 * If phandle is 0, then it is an empty entry with no
1424 * arguments. Skip forward to the next entry.
1425 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001426 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001427 if (phandle) {
1428 /*
1429 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001430 * property to determine the argument length.
1431 *
1432 * This is not needed if the cell count is hard-coded
1433 * (i.e. cells_name not set, but cell_count is set),
1434 * except when we're going to return the found node
1435 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001436 */
Stephen Warren91d99422013-08-14 15:27:11 -06001437 if (cells_name || cur_index == index) {
1438 node = of_find_node_by_phandle(phandle);
1439 if (!node) {
1440 pr_err("%s: could not find phandle\n",
1441 np->full_name);
1442 goto err;
1443 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001444 }
Stephen Warren035fd942013-08-14 15:27:10 -06001445
1446 if (cells_name) {
1447 if (of_property_read_u32(node, cells_name,
1448 &count)) {
1449 pr_err("%s: could not get %s for %s\n",
1450 np->full_name, cells_name,
1451 node->full_name);
1452 goto err;
1453 }
1454 } else {
1455 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001456 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001457
Grant Likely15c9a0a2011-12-12 09:25:57 -07001458 /*
1459 * Make sure that the arguments actually fit in the
1460 * remaining property data length
1461 */
1462 if (list + count > list_end) {
1463 pr_err("%s: arguments longer than property\n",
1464 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001465 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001466 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001467 }
1468
Grant Likely15c9a0a2011-12-12 09:25:57 -07001469 /*
1470 * All of the error cases above bail out of the loop, so at
1471 * this point, the parsing is successful. If the requested
1472 * index matches, then fill the out_args structure and return,
1473 * or return -ENOENT for an empty entry.
1474 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001475 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001476 if (cur_index == index) {
1477 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001478 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001479
Grant Likely15c9a0a2011-12-12 09:25:57 -07001480 if (out_args) {
1481 int i;
1482 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1483 count = MAX_PHANDLE_ARGS;
1484 out_args->np = node;
1485 out_args->args_count = count;
1486 for (i = 0; i < count; i++)
1487 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001488 } else {
1489 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001490 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001491
1492 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001493 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001494 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001495
1496 of_node_put(node);
1497 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001498 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001499 cur_index++;
1500 }
1501
Grant Likely23ce04c2013-02-12 21:21:49 +00001502 /*
1503 * Unlock node before returning result; will be one of:
1504 * -ENOENT : index is for empty phandle
1505 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001506 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001507 */
Grant Likelybd69f732013-02-10 22:57:21 +00001508 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001509 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001510 if (node)
1511 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001512 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001513}
Grant Likelybd69f732013-02-10 22:57:21 +00001514
Stephen Warreneded9dd2013-08-14 15:27:08 -06001515/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001516 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1517 * @np: Pointer to device node holding phandle property
1518 * @phandle_name: Name of property holding a phandle value
1519 * @index: For properties holding a table of phandles, this is the index into
1520 * the table
1521 *
1522 * Returns the device_node pointer with refcount incremented. Use
1523 * of_node_put() on it when done.
1524 */
1525struct device_node *of_parse_phandle(const struct device_node *np,
1526 const char *phandle_name, int index)
1527{
Stephen Warren91d99422013-08-14 15:27:11 -06001528 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001529
Stephen Warren91d99422013-08-14 15:27:11 -06001530 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001531 return NULL;
1532
Stephen Warren91d99422013-08-14 15:27:11 -06001533 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1534 index, &args))
1535 return NULL;
1536
1537 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001538}
1539EXPORT_SYMBOL(of_parse_phandle);
1540
1541/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001542 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1543 * @np: pointer to a device tree node containing a list
1544 * @list_name: property name that contains a list
1545 * @cells_name: property name that specifies phandles' arguments count
1546 * @index: index of a phandle to parse out
1547 * @out_args: optional pointer to output arguments structure (will be filled)
1548 *
1549 * This function is useful to parse lists of phandles and their arguments.
1550 * Returns 0 on success and fills out_args, on error returns appropriate
1551 * errno value.
1552 *
1553 * Caller is responsible to call of_node_put() on the returned out_args->node
1554 * pointer.
1555 *
1556 * Example:
1557 *
1558 * phandle1: node1 {
1559 * #list-cells = <2>;
1560 * }
1561 *
1562 * phandle2: node2 {
1563 * #list-cells = <1>;
1564 * }
1565 *
1566 * node3 {
1567 * list = <&phandle1 1 2 &phandle2 3>;
1568 * }
1569 *
1570 * To get a device_node of the `node2' node you may call this:
1571 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1572 */
Grant Likelybd69f732013-02-10 22:57:21 +00001573int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1574 const char *cells_name, int index,
1575 struct of_phandle_args *out_args)
1576{
1577 if (index < 0)
1578 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001579 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1580 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001581}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001582EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001583
Grant Likelybd69f732013-02-10 22:57:21 +00001584/**
Stephen Warren035fd942013-08-14 15:27:10 -06001585 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1586 * @np: pointer to a device tree node containing a list
1587 * @list_name: property name that contains a list
1588 * @cell_count: number of argument cells following the phandle
1589 * @index: index of a phandle to parse out
1590 * @out_args: optional pointer to output arguments structure (will be filled)
1591 *
1592 * This function is useful to parse lists of phandles and their arguments.
1593 * Returns 0 on success and fills out_args, on error returns appropriate
1594 * errno value.
1595 *
1596 * Caller is responsible to call of_node_put() on the returned out_args->node
1597 * pointer.
1598 *
1599 * Example:
1600 *
1601 * phandle1: node1 {
1602 * }
1603 *
1604 * phandle2: node2 {
1605 * }
1606 *
1607 * node3 {
1608 * list = <&phandle1 0 2 &phandle2 2 3>;
1609 * }
1610 *
1611 * To get a device_node of the `node2' node you may call this:
1612 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1613 */
1614int of_parse_phandle_with_fixed_args(const struct device_node *np,
1615 const char *list_name, int cell_count,
1616 int index, struct of_phandle_args *out_args)
1617{
1618 if (index < 0)
1619 return -EINVAL;
1620 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1621 index, out_args);
1622}
1623EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1624
1625/**
Grant Likelybd69f732013-02-10 22:57:21 +00001626 * of_count_phandle_with_args() - Find the number of phandles references in a property
1627 * @np: pointer to a device tree node containing a list
1628 * @list_name: property name that contains a list
1629 * @cells_name: property name that specifies phandles' arguments count
1630 *
1631 * Returns the number of phandle + argument tuples within a property. It
1632 * is a typical pattern to encode a list of phandle and variable
1633 * arguments into a single property. The number of arguments is encoded
1634 * by a property in the phandle-target node. For example, a gpios
1635 * property would contain a list of GPIO specifies consisting of a
1636 * phandle and 1 or more arguments. The number of arguments are
1637 * determined by the #gpio-cells property in the node pointed to by the
1638 * phandle.
1639 */
1640int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1641 const char *cells_name)
1642{
Stephen Warren035fd942013-08-14 15:27:10 -06001643 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1644 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001645}
1646EXPORT_SYMBOL(of_count_phandle_with_args);
1647
Grant Likely02af11b2009-11-23 20:16:45 -07001648/**
Xiubo Li62664f62014-01-22 13:57:39 +08001649 * __of_add_property - Add a property to a node without lock operations
1650 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001651int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001652{
1653 struct property **next;
1654
1655 prop->next = NULL;
1656 next = &np->properties;
1657 while (*next) {
1658 if (strcmp(prop->name, (*next)->name) == 0)
1659 /* duplicate ! don't insert it */
1660 return -EEXIST;
1661
1662 next = &(*next)->next;
1663 }
1664 *next = prop;
1665
1666 return 0;
1667}
1668
1669/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001670 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001671 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001672int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001673{
Grant Likely02af11b2009-11-23 20:16:45 -07001674 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001675 int rc;
1676
1677 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1678 if (rc)
1679 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001680
Grant Likely8a2b22a2014-07-23 17:05:06 -06001681 mutex_lock(&of_mutex);
1682
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001683 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001684 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001685 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001686
Grant Likely8a2b22a2014-07-23 17:05:06 -06001687 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001688 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001689
Grant Likely8a2b22a2014-07-23 17:05:06 -06001690 mutex_unlock(&of_mutex);
1691
Xiubo Li62664f62014-01-22 13:57:39 +08001692 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001693}
1694
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001695int __of_remove_property(struct device_node *np, struct property *prop)
1696{
1697 struct property **next;
1698
1699 for (next = &np->properties; *next; next = &(*next)->next) {
1700 if (*next == prop)
1701 break;
1702 }
1703 if (*next == NULL)
1704 return -ENODEV;
1705
1706 /* found the node */
1707 *next = prop->next;
1708 prop->next = np->deadprops;
1709 np->deadprops = prop;
1710
1711 return 0;
1712}
1713
Grant Likely8a2b22a2014-07-23 17:05:06 -06001714void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1715{
1716 /* at early boot, bail here and defer setup to of_init() */
1717 if (of_kset && of_node_is_attached(np))
1718 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1719}
1720
Grant Likely02af11b2009-11-23 20:16:45 -07001721/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001722 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001723 *
1724 * Note that we don't actually remove it, since we have given out
1725 * who-knows-how-many pointers to the data using get-property.
1726 * Instead we just move the property to the "dead properties"
1727 * list, so it won't be found any more.
1728 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001729int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001730{
Grant Likely02af11b2009-11-23 20:16:45 -07001731 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001732 int rc;
1733
1734 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1735 if (rc)
1736 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001737
Grant Likely8a2b22a2014-07-23 17:05:06 -06001738 mutex_lock(&of_mutex);
1739
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
Grant Likely8a2b22a2014-07-23 17:05:06 -06001744 if (!rc)
1745 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001746
Grant Likely8a2b22a2014-07-23 17:05:06 -06001747 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00001748
Grant Likely8a2b22a2014-07-23 17:05:06 -06001749 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001750}
1751
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001752int __of_update_property(struct device_node *np, struct property *newprop,
1753 struct property **oldpropp)
1754{
1755 struct property **next, *oldprop;
1756
1757 for (next = &np->properties; *next; next = &(*next)->next) {
1758 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1759 break;
1760 }
1761 *oldpropp = oldprop = *next;
1762
1763 if (oldprop) {
1764 /* replace the node */
1765 newprop->next = oldprop->next;
1766 *next = newprop;
1767 oldprop->next = np->deadprops;
1768 np->deadprops = oldprop;
1769 } else {
1770 /* new node */
1771 newprop->next = NULL;
1772 *next = newprop;
1773 }
1774
1775 return 0;
1776}
1777
Grant Likely8a2b22a2014-07-23 17:05:06 -06001778void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1779 struct property *oldprop)
1780{
1781 /* At early boot, bail out and defer setup to of_init() */
1782 if (!of_kset)
1783 return;
1784
1785 if (oldprop)
1786 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1787 __of_add_property_sysfs(np, newprop);
1788}
1789
Grant Likely02af11b2009-11-23 20:16:45 -07001790/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001791 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001792 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001793 *
1794 * Note that we don't actually remove it, since we have given out
1795 * who-knows-how-many pointers to the data using get-property.
1796 * Instead we just move the property to the "dead properties" list,
1797 * and add the new property to the property list
1798 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001799int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001800{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001801 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001802 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001803 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001804
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001805 if (!newprop->name)
1806 return -EINVAL;
1807
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001808 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1809 if (rc)
1810 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001811
Grant Likely8a2b22a2014-07-23 17:05:06 -06001812 mutex_lock(&of_mutex);
1813
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001814 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001815 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001816 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001817
Grant Likely8a2b22a2014-07-23 17:05:06 -06001818 if (!rc)
1819 __of_update_property_sysfs(np, newprop, oldprop);
Thomas Petazzoni582da652014-05-14 13:36:36 +02001820
Grant Likely8a2b22a2014-07-23 17:05:06 -06001821 mutex_unlock(&of_mutex);
Guenter Roecke7a62df2014-04-15 08:38:17 -07001822
Grant Likely8a2b22a2014-07-23 17:05:06 -06001823 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001824}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001825
Shawn Guo611cad72011-08-15 15:28:14 +08001826static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1827 int id, const char *stem, int stem_len)
1828{
1829 ap->np = np;
1830 ap->id = id;
1831 strncpy(ap->stem, stem, stem_len);
1832 ap->stem[stem_len] = 0;
1833 list_add_tail(&ap->link, &aliases_lookup);
1834 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001835 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001836}
1837
1838/**
1839 * of_alias_scan - Scan all properties of 'aliases' node
1840 *
1841 * The function scans all the properties of 'aliases' node and populate
1842 * the the global lookup table with the properties. It returns the
1843 * number of alias_prop found, or error code in error case.
1844 *
1845 * @dt_alloc: An allocator that provides a virtual address to memory
1846 * for the resulting tree
1847 */
1848void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1849{
1850 struct property *pp;
1851
1852 of_chosen = of_find_node_by_path("/chosen");
1853 if (of_chosen == NULL)
1854 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001855
1856 if (of_chosen) {
Grant Likely676e1b22014-03-27 17:11:23 -07001857 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
1858 if (!name)
1859 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001860 if (name)
1861 of_stdout = of_find_node_by_path(name);
1862 }
1863
Shawn Guo611cad72011-08-15 15:28:14 +08001864 of_aliases = of_find_node_by_path("/aliases");
1865 if (!of_aliases)
1866 return;
1867
Dong Aisheng8af0da92011-12-22 20:19:24 +08001868 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001869 const char *start = pp->name;
1870 const char *end = start + strlen(start);
1871 struct device_node *np;
1872 struct alias_prop *ap;
1873 int id, len;
1874
1875 /* Skip those we do not want to proceed */
1876 if (!strcmp(pp->name, "name") ||
1877 !strcmp(pp->name, "phandle") ||
1878 !strcmp(pp->name, "linux,phandle"))
1879 continue;
1880
1881 np = of_find_node_by_path(pp->value);
1882 if (!np)
1883 continue;
1884
1885 /* walk the alias backwards to extract the id and work out
1886 * the 'stem' string */
1887 while (isdigit(*(end-1)) && end > start)
1888 end--;
1889 len = end - start;
1890
1891 if (kstrtoint(end, 10, &id) < 0)
1892 continue;
1893
1894 /* Allocate an alias_prop with enough space for the stem */
1895 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1896 if (!ap)
1897 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001898 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001899 ap->alias = start;
1900 of_alias_add(ap, np, id, start, len);
1901 }
1902}
1903
1904/**
1905 * of_alias_get_id - Get alias id for the given device_node
1906 * @np: Pointer to the given device_node
1907 * @stem: Alias stem of the given device_node
1908 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01001909 * The function travels the lookup table to get the alias id for the given
1910 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08001911 */
1912int of_alias_get_id(struct device_node *np, const char *stem)
1913{
1914 struct alias_prop *app;
1915 int id = -ENODEV;
1916
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001917 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001918 list_for_each_entry(app, &aliases_lookup, link) {
1919 if (strcmp(app->stem, stem) != 0)
1920 continue;
1921
1922 if (np == app->np) {
1923 id = app->id;
1924 break;
1925 }
1926 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001927 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001928
1929 return id;
1930}
1931EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001932
1933const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1934 u32 *pu)
1935{
1936 const void *curv = cur;
1937
1938 if (!prop)
1939 return NULL;
1940
1941 if (!cur) {
1942 curv = prop->value;
1943 goto out_val;
1944 }
1945
1946 curv += sizeof(*cur);
1947 if (curv >= prop->value + prop->length)
1948 return NULL;
1949
1950out_val:
1951 *pu = be32_to_cpup(curv);
1952 return curv;
1953}
1954EXPORT_SYMBOL_GPL(of_prop_next_u32);
1955
1956const char *of_prop_next_string(struct property *prop, const char *cur)
1957{
1958 const void *curv = cur;
1959
1960 if (!prop)
1961 return NULL;
1962
1963 if (!cur)
1964 return prop->value;
1965
1966 curv += strlen(cur) + 1;
1967 if (curv >= prop->value + prop->length)
1968 return NULL;
1969
1970 return curv;
1971}
1972EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001973
1974/**
1975 * of_device_is_stdout_path - check if a device node matches the
1976 * linux,stdout-path property
1977 *
1978 * Check if this device node matches the linux,stdout-path property
1979 * in the chosen node. return true if yes, false otherwise.
1980 */
1981int of_device_is_stdout_path(struct device_node *dn)
1982{
1983 if (!of_stdout)
1984 return false;
1985
1986 return of_stdout == dn;
1987}
1988EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001989
1990/**
1991 * of_find_next_cache_node - Find a node's subsidiary cache
1992 * @np: node of type "cpu" or "cache"
1993 *
1994 * Returns a node pointer with refcount incremented, use
1995 * of_node_put() on it when done. Caller should hold a reference
1996 * to np.
1997 */
1998struct device_node *of_find_next_cache_node(const struct device_node *np)
1999{
2000 struct device_node *child;
2001 const phandle *handle;
2002
2003 handle = of_get_property(np, "l2-cache", NULL);
2004 if (!handle)
2005 handle = of_get_property(np, "next-level-cache", NULL);
2006
2007 if (handle)
2008 return of_find_node_by_phandle(be32_to_cpup(handle));
2009
2010 /* OF on pmac has nodes instead of properties named "l2-cache"
2011 * beneath CPU nodes.
2012 */
2013 if (!strcmp(np->type, "cpu"))
2014 for_each_child_of_node(np, child)
2015 if (!strcmp(child->type, "cache"))
2016 return child;
2017
2018 return NULL;
2019}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002020
2021/**
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002022 * of_graph_parse_endpoint() - parse common endpoint node properties
2023 * @node: pointer to endpoint device_node
2024 * @endpoint: pointer to the OF endpoint data structure
2025 *
2026 * The caller should hold a reference to @node.
2027 */
2028int of_graph_parse_endpoint(const struct device_node *node,
2029 struct of_endpoint *endpoint)
2030{
2031 struct device_node *port_node = of_get_parent(node);
2032
Philipp Zabeld4847002014-03-04 12:31:24 +01002033 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2034 __func__, node->full_name);
2035
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002036 memset(endpoint, 0, sizeof(*endpoint));
2037
2038 endpoint->local_node = node;
2039 /*
2040 * It doesn't matter whether the two calls below succeed.
2041 * If they don't then the default value 0 is used.
2042 */
2043 of_property_read_u32(port_node, "reg", &endpoint->port);
2044 of_property_read_u32(node, "reg", &endpoint->id);
2045
2046 of_node_put(port_node);
2047
2048 return 0;
2049}
2050EXPORT_SYMBOL(of_graph_parse_endpoint);
2051
2052/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002053 * of_graph_get_next_endpoint() - get next endpoint node
2054 * @parent: pointer to the parent device node
2055 * @prev: previous endpoint node, or NULL to get first
2056 *
2057 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2058 * of the passed @prev node is not decremented, the caller have to use
2059 * of_node_put() on it when done.
2060 */
2061struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2062 struct device_node *prev)
2063{
2064 struct device_node *endpoint;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002065 struct device_node *port;
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002066
2067 if (!parent)
2068 return NULL;
2069
Linus Torvalds3c83e612014-04-04 09:50:07 -07002070 /*
2071 * Start by locating the port node. If no previous endpoint is specified
2072 * search for the first port node, otherwise get the previous endpoint
2073 * parent port node.
2074 */
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002075 if (!prev) {
2076 struct device_node *node;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002077
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002078 node = of_get_child_by_name(parent, "ports");
2079 if (node)
2080 parent = node;
2081
2082 port = of_get_child_by_name(parent, "port");
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002083 of_node_put(node);
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002084
Linus Torvalds3c83e612014-04-04 09:50:07 -07002085 if (!port) {
2086 pr_err("%s(): no port node found in %s\n",
2087 __func__, parent->full_name);
Philipp Zabel4329b932014-02-26 20:43:42 +01002088 return NULL;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002089 }
2090 } else {
2091 port = of_get_parent(prev);
2092 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2093 __func__, prev->full_name))
2094 return NULL;
Philipp Zabel4329b932014-02-26 20:43:42 +01002095
Linus Torvalds3c83e612014-04-04 09:50:07 -07002096 /*
2097 * Avoid dropping prev node refcount to 0 when getting the next
2098 * child below.
2099 */
2100 of_node_get(prev);
2101 }
Philipp Zabel4329b932014-02-26 20:43:42 +01002102
Linus Torvalds3c83e612014-04-04 09:50:07 -07002103 while (1) {
2104 /*
2105 * Now that we have a port node, get the next endpoint by
2106 * getting the next child. If the previous endpoint is NULL this
2107 * will return the first child.
2108 */
2109 endpoint = of_get_next_child(port, prev);
2110 if (endpoint) {
2111 of_node_put(port);
2112 return endpoint;
2113 }
2114
2115 /* No more endpoints under this port, try the next one. */
2116 prev = NULL;
2117
2118 do {
2119 port = of_get_next_child(parent, port);
2120 if (!port)
2121 return NULL;
2122 } while (of_node_cmp(port->name, "port"));
2123 }
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002124}
2125EXPORT_SYMBOL(of_graph_get_next_endpoint);
2126
2127/**
2128 * of_graph_get_remote_port_parent() - get remote port's parent node
2129 * @node: pointer to a local endpoint device_node
2130 *
2131 * Return: Remote device node associated with remote endpoint node linked
2132 * to @node. Use of_node_put() on it when done.
2133 */
2134struct device_node *of_graph_get_remote_port_parent(
2135 const struct device_node *node)
2136{
2137 struct device_node *np;
2138 unsigned int depth;
2139
2140 /* Get remote endpoint node. */
2141 np = of_parse_phandle(node, "remote-endpoint", 0);
2142
2143 /* Walk 3 levels up only if there is 'ports' node. */
2144 for (depth = 3; depth && np; depth--) {
2145 np = of_get_next_parent(np);
2146 if (depth == 2 && of_node_cmp(np->name, "ports"))
2147 break;
2148 }
2149 return np;
2150}
2151EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2152
2153/**
2154 * of_graph_get_remote_port() - get remote port node
2155 * @node: pointer to a local endpoint device_node
2156 *
2157 * Return: Remote port node associated with remote endpoint node linked
2158 * to @node. Use of_node_put() on it when done.
2159 */
2160struct device_node *of_graph_get_remote_port(const struct device_node *node)
2161{
2162 struct device_node *np;
2163
2164 /* Get remote endpoint node. */
2165 np = of_parse_phandle(node, "remote-endpoint", 0);
2166 if (!np)
2167 return NULL;
2168 return of_get_next_parent(np);
2169}
2170EXPORT_SYMBOL(of_graph_get_remote_port);