blob: 715144af3a83b2592cde314aae0148376ae361cd [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>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070027#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100028
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080029#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080030
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080031LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080032
Randy Dunlap465aac62012-11-30 10:01:51 +000033struct device_node *of_allnodes;
34EXPORT_SYMBOL(of_allnodes);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070035struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080036struct device_node *of_aliases;
Sascha Hauer5c19e952013-08-05 14:40:44 +020037static struct device_node *of_stdout;
Shawn Guo611cad72011-08-15 15:28:14 +080038
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080039DEFINE_MUTEX(of_aliases_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100040
Stephen Rothwell581b6052007-04-24 16:46:53 +100041/* use when traversing tree through the allnext, child, sibling,
42 * or parent members of struct device_node.
43 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050044DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100045
46int of_n_addr_cells(struct device_node *np)
47{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060048 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100049
50 do {
51 if (np->parent)
52 np = np->parent;
53 ip = of_get_property(np, "#address-cells", NULL);
54 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070055 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100056 } while (np->parent);
57 /* No #address-cells property for the root node */
58 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
59}
60EXPORT_SYMBOL(of_n_addr_cells);
61
62int of_n_size_cells(struct device_node *np)
63{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060064 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100065
66 do {
67 if (np->parent)
68 np = np->parent;
69 ip = of_get_property(np, "#size-cells", NULL);
70 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070071 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100072 } while (np->parent);
73 /* No #size-cells property for the root node */
74 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
75}
76EXPORT_SYMBOL(of_n_size_cells);
77
Rob Herring0c3f0612013-09-17 10:42:50 -050078#ifdef CONFIG_NUMA
79int __weak of_node_to_nid(struct device_node *np)
80{
81 return numa_node_id();
82}
83#endif
84
Grant Likely0f22dd32012-02-15 20:38:40 -070085#if defined(CONFIG_OF_DYNAMIC)
Grant Likely923f7e32010-01-28 13:52:53 -070086/**
87 * of_node_get - Increment refcount of a node
88 * @node: Node to inc refcount, NULL is supported to
89 * simplify writing of callers
90 *
91 * Returns node.
92 */
93struct device_node *of_node_get(struct device_node *node)
94{
95 if (node)
96 kref_get(&node->kref);
97 return node;
98}
99EXPORT_SYMBOL(of_node_get);
100
101static inline struct device_node *kref_to_device_node(struct kref *kref)
102{
103 return container_of(kref, struct device_node, kref);
104}
105
106/**
107 * of_node_release - release a dynamically allocated node
108 * @kref: kref element of the node to be released
109 *
110 * In of_node_put() this function is passed to kref_put()
111 * as the destructor.
112 */
113static void of_node_release(struct kref *kref)
114{
115 struct device_node *node = kref_to_device_node(kref);
116 struct property *prop = node->properties;
117
118 /* We should never be releasing nodes that haven't been detached. */
119 if (!of_node_check_flag(node, OF_DETACHED)) {
120 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
121 dump_stack();
122 kref_init(&node->kref);
123 return;
124 }
125
126 if (!of_node_check_flag(node, OF_DYNAMIC))
127 return;
128
129 while (prop) {
130 struct property *next = prop->next;
131 kfree(prop->name);
132 kfree(prop->value);
133 kfree(prop);
134 prop = next;
135
136 if (!prop) {
137 prop = node->deadprops;
138 node->deadprops = NULL;
139 }
140 }
141 kfree(node->full_name);
142 kfree(node->data);
143 kfree(node);
144}
145
146/**
147 * of_node_put - Decrement refcount of a node
148 * @node: Node to dec refcount, NULL is supported to
149 * simplify writing of callers
150 *
151 */
152void of_node_put(struct device_node *node)
153{
154 if (node)
155 kref_put(&node->kref, of_node_release);
156}
157EXPORT_SYMBOL(of_node_put);
Grant Likely0f22dd32012-02-15 20:38:40 -0700158#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700159
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500160static struct property *__of_find_property(const struct device_node *np,
161 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000162{
163 struct property *pp;
164
Timur Tabi64e45662008-05-08 05:19:59 +1000165 if (!np)
166 return NULL;
167
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530168 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000169 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530170 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000171 *lenp = pp->length;
172 break;
173 }
174 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500175
176 return pp;
177}
178
179struct property *of_find_property(const struct device_node *np,
180 const char *name,
181 int *lenp)
182{
183 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500184 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500185
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500186 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500187 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500188 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000189
190 return pp;
191}
192EXPORT_SYMBOL(of_find_property);
193
Grant Likelye91edcf2009-10-15 10:58:09 -0600194/**
195 * of_find_all_nodes - Get next node in global list
196 * @prev: Previous node or NULL to start iteration
197 * of_node_put() will be called on it
198 *
199 * Returns a node pointer with refcount incremented, use
200 * of_node_put() on it when done.
201 */
202struct device_node *of_find_all_nodes(struct device_node *prev)
203{
204 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000205 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600206
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000207 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000208 np = prev ? prev->allnext : of_allnodes;
Grant Likelye91edcf2009-10-15 10:58:09 -0600209 for (; np != NULL; np = np->allnext)
210 if (of_node_get(np))
211 break;
212 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000213 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600214 return np;
215}
216EXPORT_SYMBOL(of_find_all_nodes);
217
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000218/*
219 * Find a property with a given name for a given node
220 * and return the value.
221 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500222static const void *__of_get_property(const struct device_node *np,
223 const char *name, int *lenp)
224{
225 struct property *pp = __of_find_property(np, name, lenp);
226
227 return pp ? pp->value : NULL;
228}
229
230/*
231 * Find a property with a given name for a given node
232 * and return the value.
233 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000234const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500235 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000236{
237 struct property *pp = of_find_property(np, name, lenp);
238
239 return pp ? pp->value : NULL;
240}
241EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000242
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100243/*
244 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
245 *
246 * @cpu: logical cpu index of a core/thread
247 * @phys_id: physical identifier of a core/thread
248 *
249 * CPU logical to physical index mapping is architecture specific.
250 * However this __weak function provides a default match of physical
251 * id to logical cpu index. phys_id provided here is usually values read
252 * from the device tree which must match the hardware internal registers.
253 *
254 * Returns true if the physical identifier and the logical cpu index
255 * correspond to the same core/thread, false otherwise.
256 */
257bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
258{
259 return (u32)phys_id == cpu;
260}
261
262/**
263 * Checks if the given "prop_name" property holds the physical id of the
264 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
265 * NULL, local thread number within the core is returned in it.
266 */
267static bool __of_find_n_match_cpu_property(struct device_node *cpun,
268 const char *prop_name, int cpu, unsigned int *thread)
269{
270 const __be32 *cell;
271 int ac, prop_len, tid;
272 u64 hwid;
273
274 ac = of_n_addr_cells(cpun);
275 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100276 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100277 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100278 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100279 for (tid = 0; tid < prop_len; tid++) {
280 hwid = of_read_number(cell, ac);
281 if (arch_match_cpu_phys_id(cpu, hwid)) {
282 if (thread)
283 *thread = tid;
284 return true;
285 }
286 cell += ac;
287 }
288 return false;
289}
290
David Millerd1cb9d12013-10-03 17:24:51 -0400291/*
292 * arch_find_n_match_cpu_physical_id - See if the given device node is
293 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
294 * else false. If 'thread' is non-NULL, the local thread number within the
295 * core is returned in it.
296 */
297bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
298 int cpu, unsigned int *thread)
299{
300 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
301 * for thread ids on PowerPC. If it doesn't exist fallback to
302 * standard "reg" property.
303 */
304 if (IS_ENABLED(CONFIG_PPC) &&
305 __of_find_n_match_cpu_property(cpun,
306 "ibm,ppc-interrupt-server#s",
307 cpu, thread))
308 return true;
309
310 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
311 return true;
312
313 return false;
314}
315
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100316/**
317 * of_get_cpu_node - Get device node associated with the given logical CPU
318 *
319 * @cpu: CPU number(logical index) for which device node is required
320 * @thread: if not NULL, local thread number within the physical core is
321 * returned
322 *
323 * The main purpose of this function is to retrieve the device node for the
324 * given logical CPU index. It should be used to initialize the of_node in
325 * cpu device. Once of_node in cpu device is populated, all the further
326 * references can use that instead.
327 *
328 * CPU logical to physical index mapping is architecture specific and is built
329 * before booting secondary cores. This function uses arch_match_cpu_phys_id
330 * which can be overridden by architecture specific implementation.
331 *
332 * Returns a node pointer for the logical cpu if found, else NULL.
333 */
334struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
335{
David Millerd1cb9d12013-10-03 17:24:51 -0400336 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100337
David Millerd1cb9d12013-10-03 17:24:51 -0400338 for_each_node_by_type(cpun, "cpu") {
339 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100340 return cpun;
341 }
342 return NULL;
343}
344EXPORT_SYMBOL(of_get_cpu_node);
345
Kevin Hao215a14c2014-02-19 16:15:45 +0800346/**
347 * __of_device_is_compatible() - Check if the node matches given constraints
348 * @device: pointer to node
349 * @compat: required compatible string, NULL or "" for any match
350 * @type: required device_type value, NULL or "" for any match
351 * @name: required node name, NULL or "" for any match
352 *
353 * Checks if the given @compat, @type and @name strings match the
354 * properties of the given @device. A constraints can be skipped by
355 * passing NULL or an empty string as the constraint.
356 *
357 * Returns 0 for no match, and a positive integer on match. The return
358 * value is a relative score with larger values indicating better
359 * matches. The score is weighted for the most specific compatible value
360 * to get the highest score. Matching type is next, followed by matching
361 * name. Practically speaking, this results in the following priority
362 * order for matches:
363 *
364 * 1. specific compatible && type && name
365 * 2. specific compatible && type
366 * 3. specific compatible && name
367 * 4. specific compatible
368 * 5. general compatible && type && name
369 * 6. general compatible && type
370 * 7. general compatible && name
371 * 8. general compatible
372 * 9. type && name
373 * 10. type
374 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000375 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500376static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800377 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000378{
Kevin Hao215a14c2014-02-19 16:15:45 +0800379 struct property *prop;
380 const char *cp;
381 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000382
Kevin Hao215a14c2014-02-19 16:15:45 +0800383 /* Compatible match has highest priority */
384 if (compat && compat[0]) {
385 prop = __of_find_property(device, "compatible", NULL);
386 for (cp = of_prop_next_string(prop, NULL); cp;
387 cp = of_prop_next_string(prop, cp), index++) {
388 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
389 score = INT_MAX/2 - (index << 2);
390 break;
391 }
392 }
393 if (!score)
394 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000395 }
396
Kevin Hao215a14c2014-02-19 16:15:45 +0800397 /* Matching type is better than matching name */
398 if (type && type[0]) {
399 if (!device->type || of_node_cmp(type, device->type))
400 return 0;
401 score += 2;
402 }
403
404 /* Matching name is a bit better than not */
405 if (name && name[0]) {
406 if (!device->name || of_node_cmp(name, device->name))
407 return 0;
408 score++;
409 }
410
411 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000412}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500413
414/** Checks if the given "compat" string matches one of the strings in
415 * the device's "compatible" property
416 */
417int of_device_is_compatible(const struct device_node *device,
418 const char *compat)
419{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500420 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500421 int res;
422
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500423 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800424 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500425 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500426 return res;
427}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000428EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000429
430/**
Grant Likely71a157e2010-02-01 21:34:14 -0700431 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700432 * @compat: compatible string to look for in root node's compatible property.
433 *
434 * Returns true if the root node has the given value in its
435 * compatible property.
436 */
Grant Likely71a157e2010-02-01 21:34:14 -0700437int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700438{
439 struct device_node *root;
440 int rc = 0;
441
442 root = of_find_node_by_path("/");
443 if (root) {
444 rc = of_device_is_compatible(root, compat);
445 of_node_put(root);
446 }
447 return rc;
448}
Grant Likely71a157e2010-02-01 21:34:14 -0700449EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700450
451/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700452 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100453 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700454 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100455 *
456 * Returns 1 if the status property is absent or set to "okay" or "ok",
457 * 0 otherwise
458 */
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700459static int __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100460{
461 const char *status;
462 int statlen;
463
Xiubo Li42ccd782014-01-13 11:07:28 +0800464 if (!device)
465 return 0;
466
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700467 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100468 if (status == NULL)
469 return 1;
470
471 if (statlen > 0) {
472 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
473 return 1;
474 }
475
476 return 0;
477}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700478
479/**
480 * of_device_is_available - check if a device is available for use
481 *
482 * @device: Node to check for availability
483 *
484 * Returns 1 if the status property is absent or set to "okay" or "ok",
485 * 0 otherwise
486 */
487int of_device_is_available(const struct device_node *device)
488{
489 unsigned long flags;
490 int res;
491
492 raw_spin_lock_irqsave(&devtree_lock, flags);
493 res = __of_device_is_available(device);
494 raw_spin_unlock_irqrestore(&devtree_lock, flags);
495 return res;
496
497}
Josh Boyer834d97d2008-03-27 00:33:14 +1100498EXPORT_SYMBOL(of_device_is_available);
499
500/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000501 * of_get_parent - Get a node's parent if any
502 * @node: Node to get parent
503 *
504 * Returns a node pointer with refcount incremented, use
505 * of_node_put() on it when done.
506 */
507struct device_node *of_get_parent(const struct device_node *node)
508{
509 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500510 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000511
512 if (!node)
513 return NULL;
514
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500515 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000516 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500517 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000518 return np;
519}
520EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000521
522/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000523 * of_get_next_parent - Iterate to a node's parent
524 * @node: Node to get parent of
525 *
526 * This is like of_get_parent() except that it drops the
527 * refcount on the passed node, making it suitable for iterating
528 * through a node's parents.
529 *
530 * Returns a node pointer with refcount incremented, use
531 * of_node_put() on it when done.
532 */
533struct device_node *of_get_next_parent(struct device_node *node)
534{
535 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500536 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000537
538 if (!node)
539 return NULL;
540
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500541 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000542 parent = of_node_get(node->parent);
543 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500544 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000545 return parent;
546}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300547EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000548
549/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000550 * of_get_next_child - Iterate a node childs
551 * @node: parent node
552 * @prev: previous child of the parent node, or NULL to get first
553 *
554 * Returns a node pointer with refcount incremented, use
555 * of_node_put() on it when done.
556 */
557struct device_node *of_get_next_child(const struct device_node *node,
558 struct device_node *prev)
559{
560 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500561 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000562
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500563 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000564 next = prev ? prev->sibling : node->child;
565 for (; next; next = next->sibling)
566 if (of_node_get(next))
567 break;
568 of_node_put(prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500569 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000570 return next;
571}
572EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000573
574/**
Timur Tabi32961932012-08-14 13:20:23 +0000575 * of_get_next_available_child - Find the next available child node
576 * @node: parent node
577 * @prev: previous child of the parent node, or NULL to get first
578 *
579 * This function is like of_get_next_child(), except that it
580 * automatically skips any disabled nodes (i.e. status = "disabled").
581 */
582struct device_node *of_get_next_available_child(const struct device_node *node,
583 struct device_node *prev)
584{
585 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000586 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000587
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000588 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000589 next = prev ? prev->sibling : node->child;
590 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700591 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000592 continue;
593 if (of_node_get(next))
594 break;
595 }
596 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000597 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000598 return next;
599}
600EXPORT_SYMBOL(of_get_next_available_child);
601
602/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100603 * of_get_child_by_name - Find the child node by name for a given parent
604 * @node: parent node
605 * @name: child name to look for.
606 *
607 * This function looks for child node for given matching name
608 *
609 * Returns a node pointer if found, with refcount incremented, use
610 * of_node_put() on it when done.
611 * Returns NULL if node is not found.
612 */
613struct device_node *of_get_child_by_name(const struct device_node *node,
614 const char *name)
615{
616 struct device_node *child;
617
618 for_each_child_of_node(node, child)
619 if (child->name && (of_node_cmp(child->name, name) == 0))
620 break;
621 return child;
622}
623EXPORT_SYMBOL(of_get_child_by_name);
624
625/**
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000626 * of_find_node_by_path - Find a node matching a full OF path
627 * @path: The full path to match
628 *
629 * Returns a node pointer with refcount incremented, use
630 * of_node_put() on it when done.
631 */
632struct device_node *of_find_node_by_path(const char *path)
633{
Randy Dunlap465aac62012-11-30 10:01:51 +0000634 struct device_node *np = of_allnodes;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500635 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000636
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500637 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000638 for (; np; np = np->allnext) {
639 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
640 && of_node_get(np))
641 break;
642 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500643 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000644 return np;
645}
646EXPORT_SYMBOL(of_find_node_by_path);
647
648/**
649 * of_find_node_by_name - Find a node by its "name" property
650 * @from: The node to start searching from or NULL, the node
651 * you pass will not be searched, only the next one
652 * will; typically, you pass what the previous call
653 * returned. of_node_put() will be called on it
654 * @name: The name string to match against
655 *
656 * Returns a node pointer with refcount incremented, use
657 * of_node_put() on it when done.
658 */
659struct device_node *of_find_node_by_name(struct device_node *from,
660 const char *name)
661{
662 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500663 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000664
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500665 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000666 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000667 for (; np; np = np->allnext)
668 if (np->name && (of_node_cmp(np->name, name) == 0)
669 && of_node_get(np))
670 break;
671 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500672 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000673 return np;
674}
675EXPORT_SYMBOL(of_find_node_by_name);
676
677/**
678 * of_find_node_by_type - Find a node by its "device_type" property
679 * @from: The node to start searching from, or NULL to start searching
680 * the entire device tree. The node you pass will not be
681 * searched, only the next one will; typically, you pass
682 * what the previous call returned. of_node_put() will be
683 * called on from for you.
684 * @type: The type string to match against
685 *
686 * Returns a node pointer with refcount incremented, use
687 * of_node_put() on it when done.
688 */
689struct device_node *of_find_node_by_type(struct device_node *from,
690 const char *type)
691{
692 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500693 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000694
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500695 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000696 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000697 for (; np; np = np->allnext)
698 if (np->type && (of_node_cmp(np->type, type) == 0)
699 && of_node_get(np))
700 break;
701 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500702 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000703 return np;
704}
705EXPORT_SYMBOL(of_find_node_by_type);
706
707/**
708 * of_find_compatible_node - Find a node based on type and one of the
709 * tokens in its "compatible" property
710 * @from: The node to start searching from or NULL, the node
711 * you pass will not be searched, only the next one
712 * will; typically, you pass what the previous call
713 * returned. of_node_put() will be called on it
714 * @type: The type string to match "device_type" or NULL to ignore
715 * @compatible: The string to match to one of the tokens in the device
716 * "compatible" list.
717 *
718 * Returns a node pointer with refcount incremented, use
719 * of_node_put() on it when done.
720 */
721struct device_node *of_find_compatible_node(struct device_node *from,
722 const char *type, const char *compatible)
723{
724 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500725 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000726
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500727 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000728 np = from ? from->allnext : of_allnodes;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000729 for (; np; np = np->allnext) {
Kevin Hao215a14c2014-02-19 16:15:45 +0800730 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500731 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000732 break;
733 }
734 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500735 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000736 return np;
737}
738EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100739
740/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000741 * of_find_node_with_property - Find a node which has a property with
742 * the given name.
743 * @from: The node to start searching from or NULL, the node
744 * you pass will not be searched, only the next one
745 * will; typically, you pass what the previous call
746 * returned. of_node_put() will be called on it
747 * @prop_name: The name of the property to look for.
748 *
749 * Returns a node pointer with refcount incremented, use
750 * of_node_put() on it when done.
751 */
752struct device_node *of_find_node_with_property(struct device_node *from,
753 const char *prop_name)
754{
755 struct device_node *np;
756 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500757 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000758
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500759 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000760 np = from ? from->allnext : of_allnodes;
Michael Ellerman1e291b12008-11-12 18:54:42 +0000761 for (; np; np = np->allnext) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530762 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +0000763 if (of_prop_cmp(pp->name, prop_name) == 0) {
764 of_node_get(np);
765 goto out;
766 }
767 }
768 }
769out:
770 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500771 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000772 return np;
773}
774EXPORT_SYMBOL(of_find_node_with_property);
775
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500776static
777const struct of_device_id *__of_match_node(const struct of_device_id *matches,
778 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100779{
Kevin Hao215a14c2014-02-19 16:15:45 +0800780 const struct of_device_id *best_match = NULL;
781 int score, best_score = 0;
782
Grant Likelya52f07e2011-03-18 10:21:29 -0600783 if (!matches)
784 return NULL;
785
Kevin Hao215a14c2014-02-19 16:15:45 +0800786 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
787 score = __of_device_is_compatible(node, matches->compatible,
788 matches->type, matches->name);
789 if (score > best_score) {
790 best_match = matches;
791 best_score = score;
792 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +0800793 }
Kevin Hao215a14c2014-02-19 16:15:45 +0800794
795 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +1100796}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500797
798/**
799 * of_match_node - Tell if an device_node has a matching of_match structure
800 * @matches: array of of device match structures to search in
801 * @node: the of device structure to match against
802 *
Kevin Hao71c54982014-02-18 15:57:29 +0800803 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500804 */
805const struct of_device_id *of_match_node(const struct of_device_id *matches,
806 const struct device_node *node)
807{
808 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500809 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500810
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500811 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500812 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500813 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500814 return match;
815}
Grant Likely283029d2008-01-09 06:20:40 +1100816EXPORT_SYMBOL(of_match_node);
817
818/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700819 * of_find_matching_node_and_match - Find a node based on an of_device_id
820 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100821 * @from: The node to start searching from or NULL, the node
822 * you pass will not be searched, only the next one
823 * will; typically, you pass what the previous call
824 * returned. of_node_put() will be called on it
825 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -0700826 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +1100827 *
828 * Returns a node pointer with refcount incremented, use
829 * of_node_put() on it when done.
830 */
Stephen Warren50c8af42012-11-20 16:12:20 -0700831struct device_node *of_find_matching_node_and_match(struct device_node *from,
832 const struct of_device_id *matches,
833 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +1100834{
835 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800836 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500837 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +1100838
Stephen Warren50c8af42012-11-20 16:12:20 -0700839 if (match)
840 *match = NULL;
841
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500842 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000843 np = from ? from->allnext : of_allnodes;
Grant Likely283029d2008-01-09 06:20:40 +1100844 for (; np; np = np->allnext) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500845 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800846 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -0700847 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -0800848 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +1100849 break;
Stephen Warren50c8af42012-11-20 16:12:20 -0700850 }
Grant Likely283029d2008-01-09 06:20:40 +1100851 }
852 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500853 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +1100854 return np;
855}
Grant Likely80c20222012-12-19 10:45:36 +0000856EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -0400857
858/**
Grant Likely3f07af42008-07-25 22:25:13 -0400859 * of_modalias_node - Lookup appropriate modalias for a device node
860 * @node: pointer to a device tree node
861 * @modalias: Pointer to buffer that modalias value will be copied into
862 * @len: Length of modalias value
863 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600864 * Based on the value of the compatible property, this routine will attempt
865 * to choose an appropriate modalias value for a particular device tree node.
866 * It does this by stripping the manufacturer prefix (as delimited by a ',')
867 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400868 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600869 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400870 */
871int of_modalias_node(struct device_node *node, char *modalias, int len)
872{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600873 const char *compatible, *p;
874 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400875
876 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600877 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400878 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400879 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600880 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400881 return 0;
882}
883EXPORT_SYMBOL_GPL(of_modalias_node);
884
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000885/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700886 * of_find_node_by_phandle - Find a node given a phandle
887 * @handle: phandle of the node to find
888 *
889 * Returns a node pointer with refcount incremented, use
890 * of_node_put() on it when done.
891 */
892struct device_node *of_find_node_by_phandle(phandle handle)
893{
894 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000895 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -0700896
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000897 raw_spin_lock_irqsave(&devtree_lock, flags);
Randy Dunlap465aac62012-11-30 10:01:51 +0000898 for (np = of_allnodes; np; np = np->allnext)
Jeremy Kerr89751a72010-02-01 21:34:11 -0700899 if (np->phandle == handle)
900 break;
901 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000902 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -0700903 return np;
904}
905EXPORT_SYMBOL(of_find_node_by_phandle);
906
907/**
Tony Priskdaeec1f2013-04-03 17:57:11 +1300908 * of_find_property_value_of_size
909 *
910 * @np: device node from which the property value is to be read.
911 * @propname: name of the property to be searched.
912 * @len: requested length of property value
913 *
914 * Search for a property in a device node and valid the requested size.
915 * Returns the property value on success, -EINVAL if the property does not
916 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
917 * property data isn't large enough.
918 *
919 */
920static void *of_find_property_value_of_size(const struct device_node *np,
921 const char *propname, u32 len)
922{
923 struct property *prop = of_find_property(np, propname, NULL);
924
925 if (!prop)
926 return ERR_PTR(-EINVAL);
927 if (!prop->value)
928 return ERR_PTR(-ENODATA);
929 if (len > prop->length)
930 return ERR_PTR(-EOVERFLOW);
931
932 return prop->value;
933}
934
935/**
Tony Prisk3daf3722013-03-23 17:02:15 +1300936 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
937 *
938 * @np: device node from which the property value is to be read.
939 * @propname: name of the property to be searched.
940 * @index: index of the u32 in the list of values
941 * @out_value: pointer to return value, modified only if no error.
942 *
943 * Search for a property in a device node and read nth 32-bit value from
944 * it. Returns 0 on success, -EINVAL if the property does not exist,
945 * -ENODATA if property does not have a value, and -EOVERFLOW if the
946 * property data isn't large enough.
947 *
948 * The out_value is modified only if a valid u32 value can be decoded.
949 */
950int of_property_read_u32_index(const struct device_node *np,
951 const char *propname,
952 u32 index, u32 *out_value)
953{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300954 const u32 *val = of_find_property_value_of_size(np, propname,
955 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +1300956
Tony Priskdaeec1f2013-04-03 17:57:11 +1300957 if (IS_ERR(val))
958 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +1300959
Tony Priskdaeec1f2013-04-03 17:57:11 +1300960 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +1300961 return 0;
962}
963EXPORT_SYMBOL_GPL(of_property_read_u32_index);
964
965/**
Viresh Kumarbe193242012-11-20 10:15:19 +0530966 * of_property_read_u8_array - Find and read an array of u8 from a property.
967 *
968 * @np: device node from which the property value is to be read.
969 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530970 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +0530971 * @sz: number of array elements to read
972 *
973 * Search for a property in a device node and read 8-bit value(s) from
974 * it. Returns 0 on success, -EINVAL if the property does not exist,
975 * -ENODATA if property does not have a value, and -EOVERFLOW if the
976 * property data isn't large enough.
977 *
978 * dts entry of array should be like:
979 * property = /bits/ 8 <0x50 0x60 0x70>;
980 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +0530981 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +0530982 */
983int of_property_read_u8_array(const struct device_node *np,
984 const char *propname, u8 *out_values, size_t sz)
985{
Tony Priskdaeec1f2013-04-03 17:57:11 +1300986 const u8 *val = of_find_property_value_of_size(np, propname,
987 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +0530988
Tony Priskdaeec1f2013-04-03 17:57:11 +1300989 if (IS_ERR(val))
990 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +0530991
Viresh Kumarbe193242012-11-20 10:15:19 +0530992 while (sz--)
993 *out_values++ = *val++;
994 return 0;
995}
996EXPORT_SYMBOL_GPL(of_property_read_u8_array);
997
998/**
999 * of_property_read_u16_array - Find and read an array of u16 from a property.
1000 *
1001 * @np: device node from which the property value is to be read.
1002 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301003 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301004 * @sz: number of array elements to read
1005 *
1006 * Search for a property in a device node and read 16-bit value(s) from
1007 * it. Returns 0 on success, -EINVAL if the property does not exist,
1008 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1009 * property data isn't large enough.
1010 *
1011 * dts entry of array should be like:
1012 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1013 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301014 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301015 */
1016int of_property_read_u16_array(const struct device_node *np,
1017 const char *propname, u16 *out_values, size_t sz)
1018{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001019 const __be16 *val = of_find_property_value_of_size(np, propname,
1020 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301021
Tony Priskdaeec1f2013-04-03 17:57:11 +13001022 if (IS_ERR(val))
1023 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301024
Viresh Kumarbe193242012-11-20 10:15:19 +05301025 while (sz--)
1026 *out_values++ = be16_to_cpup(val++);
1027 return 0;
1028}
1029EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1030
1031/**
Rob Herring0e373632011-07-06 15:42:58 -05001032 * of_property_read_u32_array - Find and read an array of 32 bit integers
1033 * from a property.
1034 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301035 * @np: device node from which the property value is to be read.
1036 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301037 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301038 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301039 *
Rob Herring0e373632011-07-06 15:42:58 -05001040 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301041 * it. Returns 0 on success, -EINVAL if the property does not exist,
1042 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1043 * property data isn't large enough.
1044 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301045 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301046 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001047int of_property_read_u32_array(const struct device_node *np,
1048 const char *propname, u32 *out_values,
1049 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301050{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001051 const __be32 *val = of_find_property_value_of_size(np, propname,
1052 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301053
Tony Priskdaeec1f2013-04-03 17:57:11 +13001054 if (IS_ERR(val))
1055 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001056
Rob Herring0e373632011-07-06 15:42:58 -05001057 while (sz--)
1058 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301059 return 0;
1060}
Rob Herring0e373632011-07-06 15:42:58 -05001061EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301062
1063/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001064 * of_property_read_u64 - Find and read a 64 bit integer from a property
1065 * @np: device node from which the property value is to be read.
1066 * @propname: name of the property to be searched.
1067 * @out_value: pointer to return value, modified only if return value is 0.
1068 *
1069 * Search for a property in a device node and read a 64-bit value from
1070 * it. Returns 0 on success, -EINVAL if the property does not exist,
1071 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1072 * property data isn't large enough.
1073 *
1074 * The out_value is modified only if a valid u64 value can be decoded.
1075 */
1076int of_property_read_u64(const struct device_node *np, const char *propname,
1077 u64 *out_value)
1078{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001079 const __be32 *val = of_find_property_value_of_size(np, propname,
1080 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001081
Tony Priskdaeec1f2013-04-03 17:57:11 +13001082 if (IS_ERR(val))
1083 return PTR_ERR(val);
1084
1085 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001086 return 0;
1087}
1088EXPORT_SYMBOL_GPL(of_property_read_u64);
1089
1090/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301091 * of_property_read_string - Find and read a string from a property
1092 * @np: device node from which the property value is to be read.
1093 * @propname: name of the property to be searched.
1094 * @out_string: pointer to null terminated return string, modified only if
1095 * return value is 0.
1096 *
1097 * Search for a property in a device tree node and retrieve a null
1098 * terminated string value (pointer to data, not a copy). Returns 0 on
1099 * success, -EINVAL if the property does not exist, -ENODATA if property
1100 * does not have a value, and -EILSEQ if the string is not null-terminated
1101 * within the length of the property data.
1102 *
1103 * The out_string pointer is modified only if a valid string can be decoded.
1104 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001105int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001106 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301107{
1108 struct property *prop = of_find_property(np, propname, NULL);
1109 if (!prop)
1110 return -EINVAL;
1111 if (!prop->value)
1112 return -ENODATA;
1113 if (strnlen(prop->value, prop->length) >= prop->length)
1114 return -EILSEQ;
1115 *out_string = prop->value;
1116 return 0;
1117}
1118EXPORT_SYMBOL_GPL(of_property_read_string);
1119
1120/**
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001121 * of_property_read_string_index - Find and read a string from a multiple
1122 * strings property.
1123 * @np: device node from which the property value is to be read.
1124 * @propname: name of the property to be searched.
1125 * @index: index of the string in the list of strings
1126 * @out_string: pointer to null terminated return string, modified only if
1127 * return value is 0.
1128 *
1129 * Search for a property in a device tree node and retrieve a null
1130 * terminated string value (pointer to data, not a copy) in the list of strings
1131 * contained in that property.
1132 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1133 * property does not have a value, and -EILSEQ if the string is not
1134 * null-terminated within the length of the property data.
1135 *
1136 * The out_string pointer is modified only if a valid string can be decoded.
1137 */
1138int of_property_read_string_index(struct device_node *np, const char *propname,
1139 int index, const char **output)
1140{
1141 struct property *prop = of_find_property(np, propname, NULL);
1142 int i = 0;
1143 size_t l = 0, total = 0;
1144 const char *p;
1145
1146 if (!prop)
1147 return -EINVAL;
1148 if (!prop->value)
1149 return -ENODATA;
1150 if (strnlen(prop->value, prop->length) >= prop->length)
1151 return -EILSEQ;
1152
1153 p = prop->value;
1154
1155 for (i = 0; total < prop->length; total += l, p += l) {
1156 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001157 if (i++ == index) {
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001158 *output = p;
1159 return 0;
1160 }
1161 }
1162 return -ENODATA;
1163}
1164EXPORT_SYMBOL_GPL(of_property_read_string_index);
1165
Grant Likely7aff0fe2011-12-12 09:25:58 -07001166/**
1167 * of_property_match_string() - Find string in a list and return index
1168 * @np: pointer to node containing string list property
1169 * @propname: string list property name
1170 * @string: pointer to string to search for in string list
1171 *
1172 * This function searches a string list property and returns the index
1173 * of a specific string value.
1174 */
1175int of_property_match_string(struct device_node *np, const char *propname,
1176 const char *string)
1177{
1178 struct property *prop = of_find_property(np, propname, NULL);
1179 size_t l;
1180 int i;
1181 const char *p, *end;
1182
1183 if (!prop)
1184 return -EINVAL;
1185 if (!prop->value)
1186 return -ENODATA;
1187
1188 p = prop->value;
1189 end = p + prop->length;
1190
1191 for (i = 0; p < end; i++, p += l) {
1192 l = strlen(p) + 1;
1193 if (p + l > end)
1194 return -EILSEQ;
1195 pr_debug("comparing %s with %s\n", string, p);
1196 if (strcmp(string, p) == 0)
1197 return i; /* Found it; return index */
1198 }
1199 return -ENODATA;
1200}
1201EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001202
1203/**
1204 * of_property_count_strings - Find and return the number of strings from a
1205 * multiple strings property.
1206 * @np: device node from which the property value is to be read.
1207 * @propname: name of the property to be searched.
1208 *
1209 * Search for a property in a device tree node and retrieve the number of null
1210 * terminated string contain in it. Returns the number of strings on
1211 * success, -EINVAL if the property does not exist, -ENODATA if property
1212 * does not have a value, and -EILSEQ if the string is not null-terminated
1213 * within the length of the property data.
1214 */
1215int of_property_count_strings(struct device_node *np, const char *propname)
1216{
1217 struct property *prop = of_find_property(np, propname, NULL);
1218 int i = 0;
1219 size_t l = 0, total = 0;
1220 const char *p;
1221
1222 if (!prop)
1223 return -EINVAL;
1224 if (!prop->value)
1225 return -ENODATA;
1226 if (strnlen(prop->value, prop->length) >= prop->length)
1227 return -EILSEQ;
1228
1229 p = prop->value;
1230
Benoit Cousson88af7f52011-12-05 15:23:54 +01001231 for (i = 0; total < prop->length; total += l, p += l, i++)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001232 l = strlen(p) + 1;
Benoit Cousson88af7f52011-12-05 15:23:54 +01001233
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001234 return i;
1235}
1236EXPORT_SYMBOL_GPL(of_property_count_strings);
1237
Grant Likely624cfca2013-10-11 22:05:10 +01001238void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1239{
1240 int i;
1241 printk("%s %s", msg, of_node_full_name(args->np));
1242 for (i = 0; i < args->args_count; i++)
1243 printk(i ? ",%08x" : ":%08x", args->args[i]);
1244 printk("\n");
1245}
1246
Grant Likelybd69f732013-02-10 22:57:21 +00001247static int __of_parse_phandle_with_args(const struct device_node *np,
1248 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001249 const char *cells_name,
1250 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001251 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001252{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001253 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001254 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001255 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001256 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001257 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001258
Grant Likely15c9a0a2011-12-12 09:25:57 -07001259 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001260 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001261 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001262 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001263 list_end = list + size / sizeof(*list);
1264
Grant Likely15c9a0a2011-12-12 09:25:57 -07001265 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001266 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001267 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001268 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001269
Grant Likely15c9a0a2011-12-12 09:25:57 -07001270 /*
1271 * If phandle is 0, then it is an empty entry with no
1272 * arguments. Skip forward to the next entry.
1273 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001274 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001275 if (phandle) {
1276 /*
1277 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001278 * property to determine the argument length.
1279 *
1280 * This is not needed if the cell count is hard-coded
1281 * (i.e. cells_name not set, but cell_count is set),
1282 * except when we're going to return the found node
1283 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001284 */
Stephen Warren91d99422013-08-14 15:27:11 -06001285 if (cells_name || cur_index == index) {
1286 node = of_find_node_by_phandle(phandle);
1287 if (!node) {
1288 pr_err("%s: could not find phandle\n",
1289 np->full_name);
1290 goto err;
1291 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001292 }
Stephen Warren035fd942013-08-14 15:27:10 -06001293
1294 if (cells_name) {
1295 if (of_property_read_u32(node, cells_name,
1296 &count)) {
1297 pr_err("%s: could not get %s for %s\n",
1298 np->full_name, cells_name,
1299 node->full_name);
1300 goto err;
1301 }
1302 } else {
1303 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001304 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001305
Grant Likely15c9a0a2011-12-12 09:25:57 -07001306 /*
1307 * Make sure that the arguments actually fit in the
1308 * remaining property data length
1309 */
1310 if (list + count > list_end) {
1311 pr_err("%s: arguments longer than property\n",
1312 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001313 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001314 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001315 }
1316
Grant Likely15c9a0a2011-12-12 09:25:57 -07001317 /*
1318 * All of the error cases above bail out of the loop, so at
1319 * this point, the parsing is successful. If the requested
1320 * index matches, then fill the out_args structure and return,
1321 * or return -ENOENT for an empty entry.
1322 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001323 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001324 if (cur_index == index) {
1325 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001326 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001327
Grant Likely15c9a0a2011-12-12 09:25:57 -07001328 if (out_args) {
1329 int i;
1330 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1331 count = MAX_PHANDLE_ARGS;
1332 out_args->np = node;
1333 out_args->args_count = count;
1334 for (i = 0; i < count; i++)
1335 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001336 } else {
1337 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001338 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001339
1340 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001341 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001342 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001343
1344 of_node_put(node);
1345 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001346 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001347 cur_index++;
1348 }
1349
Grant Likely23ce04c2013-02-12 21:21:49 +00001350 /*
1351 * Unlock node before returning result; will be one of:
1352 * -ENOENT : index is for empty phandle
1353 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001354 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001355 */
Grant Likelybd69f732013-02-10 22:57:21 +00001356 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001357 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001358 if (node)
1359 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001360 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001361}
Grant Likelybd69f732013-02-10 22:57:21 +00001362
Stephen Warreneded9dd2013-08-14 15:27:08 -06001363/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001364 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1365 * @np: Pointer to device node holding phandle property
1366 * @phandle_name: Name of property holding a phandle value
1367 * @index: For properties holding a table of phandles, this is the index into
1368 * the table
1369 *
1370 * Returns the device_node pointer with refcount incremented. Use
1371 * of_node_put() on it when done.
1372 */
1373struct device_node *of_parse_phandle(const struct device_node *np,
1374 const char *phandle_name, int index)
1375{
Stephen Warren91d99422013-08-14 15:27:11 -06001376 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001377
Stephen Warren91d99422013-08-14 15:27:11 -06001378 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001379 return NULL;
1380
Stephen Warren91d99422013-08-14 15:27:11 -06001381 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1382 index, &args))
1383 return NULL;
1384
1385 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001386}
1387EXPORT_SYMBOL(of_parse_phandle);
1388
1389/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001390 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1391 * @np: pointer to a device tree node containing a list
1392 * @list_name: property name that contains a list
1393 * @cells_name: property name that specifies phandles' arguments count
1394 * @index: index of a phandle to parse out
1395 * @out_args: optional pointer to output arguments structure (will be filled)
1396 *
1397 * This function is useful to parse lists of phandles and their arguments.
1398 * Returns 0 on success and fills out_args, on error returns appropriate
1399 * errno value.
1400 *
1401 * Caller is responsible to call of_node_put() on the returned out_args->node
1402 * pointer.
1403 *
1404 * Example:
1405 *
1406 * phandle1: node1 {
1407 * #list-cells = <2>;
1408 * }
1409 *
1410 * phandle2: node2 {
1411 * #list-cells = <1>;
1412 * }
1413 *
1414 * node3 {
1415 * list = <&phandle1 1 2 &phandle2 3>;
1416 * }
1417 *
1418 * To get a device_node of the `node2' node you may call this:
1419 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1420 */
Grant Likelybd69f732013-02-10 22:57:21 +00001421int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1422 const char *cells_name, int index,
1423 struct of_phandle_args *out_args)
1424{
1425 if (index < 0)
1426 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001427 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1428 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001429}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001430EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001431
Grant Likelybd69f732013-02-10 22:57:21 +00001432/**
Stephen Warren035fd942013-08-14 15:27:10 -06001433 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1434 * @np: pointer to a device tree node containing a list
1435 * @list_name: property name that contains a list
1436 * @cell_count: number of argument cells following the phandle
1437 * @index: index of a phandle to parse out
1438 * @out_args: optional pointer to output arguments structure (will be filled)
1439 *
1440 * This function is useful to parse lists of phandles and their arguments.
1441 * Returns 0 on success and fills out_args, on error returns appropriate
1442 * errno value.
1443 *
1444 * Caller is responsible to call of_node_put() on the returned out_args->node
1445 * pointer.
1446 *
1447 * Example:
1448 *
1449 * phandle1: node1 {
1450 * }
1451 *
1452 * phandle2: node2 {
1453 * }
1454 *
1455 * node3 {
1456 * list = <&phandle1 0 2 &phandle2 2 3>;
1457 * }
1458 *
1459 * To get a device_node of the `node2' node you may call this:
1460 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1461 */
1462int of_parse_phandle_with_fixed_args(const struct device_node *np,
1463 const char *list_name, int cell_count,
1464 int index, struct of_phandle_args *out_args)
1465{
1466 if (index < 0)
1467 return -EINVAL;
1468 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1469 index, out_args);
1470}
1471EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1472
1473/**
Grant Likelybd69f732013-02-10 22:57:21 +00001474 * of_count_phandle_with_args() - Find the number of phandles references in a property
1475 * @np: pointer to a device tree node containing a list
1476 * @list_name: property name that contains a list
1477 * @cells_name: property name that specifies phandles' arguments count
1478 *
1479 * Returns the number of phandle + argument tuples within a property. It
1480 * is a typical pattern to encode a list of phandle and variable
1481 * arguments into a single property. The number of arguments is encoded
1482 * by a property in the phandle-target node. For example, a gpios
1483 * property would contain a list of GPIO specifies consisting of a
1484 * phandle and 1 or more arguments. The number of arguments are
1485 * determined by the #gpio-cells property in the node pointed to by the
1486 * phandle.
1487 */
1488int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1489 const char *cells_name)
1490{
Stephen Warren035fd942013-08-14 15:27:10 -06001491 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1492 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001493}
1494EXPORT_SYMBOL(of_count_phandle_with_args);
1495
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001496#if defined(CONFIG_OF_DYNAMIC)
1497static int of_property_notify(int action, struct device_node *np,
1498 struct property *prop)
1499{
1500 struct of_prop_reconfig pr;
1501
1502 pr.dn = np;
1503 pr.prop = prop;
1504 return of_reconfig_notify(action, &pr);
1505}
1506#else
1507static int of_property_notify(int action, struct device_node *np,
1508 struct property *prop)
1509{
1510 return 0;
1511}
1512#endif
1513
Grant Likely02af11b2009-11-23 20:16:45 -07001514/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001515 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001516 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001517int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001518{
1519 struct property **next;
1520 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001521 int rc;
1522
1523 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1524 if (rc)
1525 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001526
1527 prop->next = NULL;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001528 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001529 next = &np->properties;
1530 while (*next) {
1531 if (strcmp(prop->name, (*next)->name) == 0) {
1532 /* duplicate ! don't insert it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001533 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001534 return -1;
1535 }
1536 next = &(*next)->next;
1537 }
1538 *next = prop;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001539 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001540
1541#ifdef CONFIG_PROC_DEVICETREE
1542 /* try to add to proc as well if it was initialized */
1543 if (np->pde)
1544 proc_device_tree_add_prop(np->pde, prop);
1545#endif /* CONFIG_PROC_DEVICETREE */
1546
1547 return 0;
1548}
1549
1550/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001551 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001552 *
1553 * Note that we don't actually remove it, since we have given out
1554 * who-knows-how-many pointers to the data using get-property.
1555 * Instead we just move the property to the "dead properties"
1556 * list, so it won't be found any more.
1557 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001558int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001559{
1560 struct property **next;
1561 unsigned long flags;
1562 int found = 0;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001563 int rc;
1564
1565 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1566 if (rc)
1567 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001568
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001569 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001570 next = &np->properties;
1571 while (*next) {
1572 if (*next == prop) {
1573 /* found the node */
1574 *next = prop->next;
1575 prop->next = np->deadprops;
1576 np->deadprops = prop;
1577 found = 1;
1578 break;
1579 }
1580 next = &(*next)->next;
1581 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001582 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001583
1584 if (!found)
1585 return -ENODEV;
1586
1587#ifdef CONFIG_PROC_DEVICETREE
1588 /* try to remove the proc node as well */
1589 if (np->pde)
1590 proc_device_tree_remove_prop(np->pde, prop);
1591#endif /* CONFIG_PROC_DEVICETREE */
1592
1593 return 0;
1594}
1595
1596/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001597 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001598 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001599 *
1600 * Note that we don't actually remove it, since we have given out
1601 * who-knows-how-many pointers to the data using get-property.
1602 * Instead we just move the property to the "dead properties" list,
1603 * and add the new property to the property list
1604 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001605int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001606{
Dong Aisheng475d0092012-07-11 15:16:37 +10001607 struct property **next, *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001608 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001609 int rc, found = 0;
1610
1611 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1612 if (rc)
1613 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001614
Dong Aisheng475d0092012-07-11 15:16:37 +10001615 if (!newprop->name)
1616 return -EINVAL;
1617
1618 oldprop = of_find_property(np, newprop->name, NULL);
1619 if (!oldprop)
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001620 return of_add_property(np, newprop);
Dong Aisheng475d0092012-07-11 15:16:37 +10001621
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001622 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001623 next = &np->properties;
1624 while (*next) {
1625 if (*next == oldprop) {
1626 /* found the node */
1627 newprop->next = oldprop->next;
1628 *next = newprop;
1629 oldprop->next = np->deadprops;
1630 np->deadprops = oldprop;
1631 found = 1;
1632 break;
1633 }
1634 next = &(*next)->next;
1635 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001636 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001637
1638 if (!found)
1639 return -ENODEV;
1640
1641#ifdef CONFIG_PROC_DEVICETREE
1642 /* try to add to proc as well if it was initialized */
1643 if (np->pde)
1644 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1645#endif /* CONFIG_PROC_DEVICETREE */
1646
1647 return 0;
1648}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001649
1650#if defined(CONFIG_OF_DYNAMIC)
1651/*
1652 * Support for dynamic device trees.
1653 *
1654 * On some platforms, the device tree can be manipulated at runtime.
1655 * The routines in this section support adding, removing and changing
1656 * device tree nodes.
1657 */
1658
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001659static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1660
1661int of_reconfig_notifier_register(struct notifier_block *nb)
1662{
1663 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1664}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001665EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001666
1667int of_reconfig_notifier_unregister(struct notifier_block *nb)
1668{
1669 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1670}
Nathan Fontenot1a9bd452012-11-28 09:42:26 +00001671EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001672
1673int of_reconfig_notify(unsigned long action, void *p)
1674{
1675 int rc;
1676
1677 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1678 return notifier_to_errno(rc);
1679}
1680
Nathan Fontenote81b3292012-10-02 16:55:01 +00001681#ifdef CONFIG_PROC_DEVICETREE
1682static void of_add_proc_dt_entry(struct device_node *dn)
1683{
1684 struct proc_dir_entry *ent;
1685
1686 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1687 if (ent)
1688 proc_device_tree_add_node(dn, ent);
1689}
1690#else
1691static void of_add_proc_dt_entry(struct device_node *dn)
1692{
1693 return;
1694}
1695#endif
1696
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001697/**
1698 * of_attach_node - Plug a device node into the tree and global list.
1699 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001700int of_attach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001701{
1702 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001703 int rc;
1704
1705 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1706 if (rc)
1707 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001708
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001709 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001710 np->sibling = np->parent->child;
Randy Dunlap465aac62012-11-30 10:01:51 +00001711 np->allnext = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001712 np->parent->child = np;
Randy Dunlap465aac62012-11-30 10:01:51 +00001713 of_allnodes = np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001714 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001715
1716 of_add_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001717 return 0;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001718}
1719
Nathan Fontenote81b3292012-10-02 16:55:01 +00001720#ifdef CONFIG_PROC_DEVICETREE
1721static void of_remove_proc_dt_entry(struct device_node *dn)
1722{
David Howellsa8ca16e2013-04-12 17:27:28 +01001723 proc_remove(dn->pde);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001724}
1725#else
1726static void of_remove_proc_dt_entry(struct device_node *dn)
1727{
1728 return;
1729}
1730#endif
1731
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001732/**
1733 * of_detach_node - "Unplug" a node from the device tree.
1734 *
1735 * The caller must hold a reference to the node. The memory associated with
1736 * the node is not freed until its refcount goes to zero.
1737 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001738int of_detach_node(struct device_node *np)
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001739{
1740 struct device_node *parent;
1741 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001742 int rc = 0;
1743
1744 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1745 if (rc)
1746 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001747
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001748 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001749
Nathan Fontenote81b3292012-10-02 16:55:01 +00001750 if (of_node_check_flag(np, OF_DETACHED)) {
1751 /* someone already detached it */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001752 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001753 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001754 }
1755
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001756 parent = np->parent;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001757 if (!parent) {
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001758 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001759 return rc;
Nathan Fontenote81b3292012-10-02 16:55:01 +00001760 }
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001761
Randy Dunlap465aac62012-11-30 10:01:51 +00001762 if (of_allnodes == np)
1763 of_allnodes = np->allnext;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001764 else {
1765 struct device_node *prev;
Randy Dunlap465aac62012-11-30 10:01:51 +00001766 for (prev = of_allnodes;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001767 prev->allnext != np;
1768 prev = prev->allnext)
1769 ;
1770 prev->allnext = np->allnext;
1771 }
1772
1773 if (parent->child == np)
1774 parent->child = np->sibling;
1775 else {
1776 struct device_node *prevsib;
1777 for (prevsib = np->parent->child;
1778 prevsib->sibling != np;
1779 prevsib = prevsib->sibling)
1780 ;
1781 prevsib->sibling = np->sibling;
1782 }
1783
1784 of_node_set_flag(np, OF_DETACHED);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001785 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001786
1787 of_remove_proc_dt_entry(np);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001788 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001789}
1790#endif /* defined(CONFIG_OF_DYNAMIC) */
1791
Shawn Guo611cad72011-08-15 15:28:14 +08001792static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1793 int id, const char *stem, int stem_len)
1794{
1795 ap->np = np;
1796 ap->id = id;
1797 strncpy(ap->stem, stem, stem_len);
1798 ap->stem[stem_len] = 0;
1799 list_add_tail(&ap->link, &aliases_lookup);
1800 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001801 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001802}
1803
1804/**
1805 * of_alias_scan - Scan all properties of 'aliases' node
1806 *
1807 * The function scans all the properties of 'aliases' node and populate
1808 * the the global lookup table with the properties. It returns the
1809 * number of alias_prop found, or error code in error case.
1810 *
1811 * @dt_alloc: An allocator that provides a virtual address to memory
1812 * for the resulting tree
1813 */
1814void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1815{
1816 struct property *pp;
1817
1818 of_chosen = of_find_node_by_path("/chosen");
1819 if (of_chosen == NULL)
1820 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001821
1822 if (of_chosen) {
1823 const char *name;
1824
1825 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1826 if (name)
1827 of_stdout = of_find_node_by_path(name);
1828 }
1829
Shawn Guo611cad72011-08-15 15:28:14 +08001830 of_aliases = of_find_node_by_path("/aliases");
1831 if (!of_aliases)
1832 return;
1833
Dong Aisheng8af0da92011-12-22 20:19:24 +08001834 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001835 const char *start = pp->name;
1836 const char *end = start + strlen(start);
1837 struct device_node *np;
1838 struct alias_prop *ap;
1839 int id, len;
1840
1841 /* Skip those we do not want to proceed */
1842 if (!strcmp(pp->name, "name") ||
1843 !strcmp(pp->name, "phandle") ||
1844 !strcmp(pp->name, "linux,phandle"))
1845 continue;
1846
1847 np = of_find_node_by_path(pp->value);
1848 if (!np)
1849 continue;
1850
1851 /* walk the alias backwards to extract the id and work out
1852 * the 'stem' string */
1853 while (isdigit(*(end-1)) && end > start)
1854 end--;
1855 len = end - start;
1856
1857 if (kstrtoint(end, 10, &id) < 0)
1858 continue;
1859
1860 /* Allocate an alias_prop with enough space for the stem */
1861 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1862 if (!ap)
1863 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001864 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001865 ap->alias = start;
1866 of_alias_add(ap, np, id, start, len);
1867 }
1868}
1869
1870/**
1871 * of_alias_get_id - Get alias id for the given device_node
1872 * @np: Pointer to the given device_node
1873 * @stem: Alias stem of the given device_node
1874 *
1875 * The function travels the lookup table to get alias id for the given
1876 * device_node and alias stem. It returns the alias id if find it.
1877 */
1878int of_alias_get_id(struct device_node *np, const char *stem)
1879{
1880 struct alias_prop *app;
1881 int id = -ENODEV;
1882
1883 mutex_lock(&of_aliases_mutex);
1884 list_for_each_entry(app, &aliases_lookup, link) {
1885 if (strcmp(app->stem, stem) != 0)
1886 continue;
1887
1888 if (np == app->np) {
1889 id = app->id;
1890 break;
1891 }
1892 }
1893 mutex_unlock(&of_aliases_mutex);
1894
1895 return id;
1896}
1897EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001898
1899const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1900 u32 *pu)
1901{
1902 const void *curv = cur;
1903
1904 if (!prop)
1905 return NULL;
1906
1907 if (!cur) {
1908 curv = prop->value;
1909 goto out_val;
1910 }
1911
1912 curv += sizeof(*cur);
1913 if (curv >= prop->value + prop->length)
1914 return NULL;
1915
1916out_val:
1917 *pu = be32_to_cpup(curv);
1918 return curv;
1919}
1920EXPORT_SYMBOL_GPL(of_prop_next_u32);
1921
1922const char *of_prop_next_string(struct property *prop, const char *cur)
1923{
1924 const void *curv = cur;
1925
1926 if (!prop)
1927 return NULL;
1928
1929 if (!cur)
1930 return prop->value;
1931
1932 curv += strlen(cur) + 1;
1933 if (curv >= prop->value + prop->length)
1934 return NULL;
1935
1936 return curv;
1937}
1938EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001939
1940/**
1941 * of_device_is_stdout_path - check if a device node matches the
1942 * linux,stdout-path property
1943 *
1944 * Check if this device node matches the linux,stdout-path property
1945 * in the chosen node. return true if yes, false otherwise.
1946 */
1947int of_device_is_stdout_path(struct device_node *dn)
1948{
1949 if (!of_stdout)
1950 return false;
1951
1952 return of_stdout == dn;
1953}
1954EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001955
1956/**
1957 * of_find_next_cache_node - Find a node's subsidiary cache
1958 * @np: node of type "cpu" or "cache"
1959 *
1960 * Returns a node pointer with refcount incremented, use
1961 * of_node_put() on it when done. Caller should hold a reference
1962 * to np.
1963 */
1964struct device_node *of_find_next_cache_node(const struct device_node *np)
1965{
1966 struct device_node *child;
1967 const phandle *handle;
1968
1969 handle = of_get_property(np, "l2-cache", NULL);
1970 if (!handle)
1971 handle = of_get_property(np, "next-level-cache", NULL);
1972
1973 if (handle)
1974 return of_find_node_by_phandle(be32_to_cpup(handle));
1975
1976 /* OF on pmac has nodes instead of properties named "l2-cache"
1977 * beneath CPU nodes.
1978 */
1979 if (!strcmp(np->type, "cpu"))
1980 for_each_child_of_node(np, child)
1981 if (!strcmp(child->type, "cache"))
1982 return child;
1983
1984 return NULL;
1985}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01001986
1987/**
Philipp Zabelf2a575f2014-02-14 11:53:56 +01001988 * of_graph_parse_endpoint() - parse common endpoint node properties
1989 * @node: pointer to endpoint device_node
1990 * @endpoint: pointer to the OF endpoint data structure
1991 *
1992 * The caller should hold a reference to @node.
1993 */
1994int of_graph_parse_endpoint(const struct device_node *node,
1995 struct of_endpoint *endpoint)
1996{
1997 struct device_node *port_node = of_get_parent(node);
1998
1999 memset(endpoint, 0, sizeof(*endpoint));
2000
2001 endpoint->local_node = node;
2002 /*
2003 * It doesn't matter whether the two calls below succeed.
2004 * If they don't then the default value 0 is used.
2005 */
2006 of_property_read_u32(port_node, "reg", &endpoint->port);
2007 of_property_read_u32(node, "reg", &endpoint->id);
2008
2009 of_node_put(port_node);
2010
2011 return 0;
2012}
2013EXPORT_SYMBOL(of_graph_parse_endpoint);
2014
2015/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002016 * of_graph_get_next_endpoint() - get next endpoint node
2017 * @parent: pointer to the parent device node
2018 * @prev: previous endpoint node, or NULL to get first
2019 *
2020 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2021 * of the passed @prev node is not decremented, the caller have to use
2022 * of_node_put() on it when done.
2023 */
2024struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2025 struct device_node *prev)
2026{
2027 struct device_node *endpoint;
2028 struct device_node *port = NULL;
2029
2030 if (!parent)
2031 return NULL;
2032
2033 if (!prev) {
2034 struct device_node *node;
2035 /*
2036 * It's the first call, we have to find a port subnode
2037 * within this node or within an optional 'ports' node.
2038 */
2039 node = of_get_child_by_name(parent, "ports");
2040 if (node)
2041 parent = node;
2042
2043 port = of_get_child_by_name(parent, "port");
2044
2045 if (port) {
2046 /* Found a port, get an endpoint. */
2047 endpoint = of_get_next_child(port, NULL);
2048 of_node_put(port);
2049 } else {
2050 endpoint = NULL;
2051 }
2052
2053 if (!endpoint)
2054 pr_err("%s(): no endpoint nodes specified for %s\n",
2055 __func__, parent->full_name);
2056 of_node_put(node);
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002057
Philipp Zabel4329b932014-02-26 20:43:42 +01002058 return endpoint;
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002059 }
2060
Philipp Zabel4329b932014-02-26 20:43:42 +01002061 port = of_get_parent(prev);
2062 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2063 __func__, prev->full_name))
2064 return NULL;
2065
2066 /* Avoid dropping prev node refcount to 0. */
2067 of_node_get(prev);
2068 endpoint = of_get_next_child(port, prev);
2069 if (endpoint) {
2070 of_node_put(port);
2071 return endpoint;
2072 }
2073
2074 /* No more endpoints under this port, try the next one. */
2075 do {
2076 port = of_get_next_child(parent, port);
2077 if (!port)
2078 return NULL;
2079 } while (of_node_cmp(port->name, "port"));
2080
2081 /* Pick up the first endpoint in this port. */
2082 endpoint = of_get_next_child(port, NULL);
2083 of_node_put(port);
2084
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002085 return endpoint;
2086}
2087EXPORT_SYMBOL(of_graph_get_next_endpoint);
2088
2089/**
2090 * of_graph_get_remote_port_parent() - get remote port's parent node
2091 * @node: pointer to a local endpoint device_node
2092 *
2093 * Return: Remote device node associated with remote endpoint node linked
2094 * to @node. Use of_node_put() on it when done.
2095 */
2096struct device_node *of_graph_get_remote_port_parent(
2097 const struct device_node *node)
2098{
2099 struct device_node *np;
2100 unsigned int depth;
2101
2102 /* Get remote endpoint node. */
2103 np = of_parse_phandle(node, "remote-endpoint", 0);
2104
2105 /* Walk 3 levels up only if there is 'ports' node. */
2106 for (depth = 3; depth && np; depth--) {
2107 np = of_get_next_parent(np);
2108 if (depth == 2 && of_node_cmp(np->name, "ports"))
2109 break;
2110 }
2111 return np;
2112}
2113EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2114
2115/**
2116 * of_graph_get_remote_port() - get remote port node
2117 * @node: pointer to a local endpoint device_node
2118 *
2119 * Return: Remote port node associated with remote endpoint node linked
2120 * to @node. Use of_node_put() on it when done.
2121 */
2122struct device_node *of_graph_get_remote_port(const struct device_node *node)
2123{
2124 struct device_node *np;
2125
2126 /* Get remote endpoint node. */
2127 np = of_parse_phandle(node, "remote-endpoint", 0);
2128 if (!np)
2129 return NULL;
2130 return of_get_next_parent(np);
2131}
2132EXPORT_SYMBOL(of_graph_get_remote_port);