blob: 431a051295db0a9f2ee78239fea8a64e7c79590e [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 */
Rob Herring606ad422016-06-15 08:32:18 -050020
21#define pr_fmt(fmt) "OF: " fmt
22
Grant Likely3482f2c2014-03-27 17:18:55 -070023#include <linux/console.h>
Shawn Guo611cad72011-08-15 15:28:14 +080024#include <linux/ctype.h>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010025#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100026#include <linux/module.h>
27#include <linux/of.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010028#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100029#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000031#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070032#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100033
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080034#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080035
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080036LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080037
Grant Likely5063e252014-10-03 16:28:27 +010038struct device_node *of_root;
39EXPORT_SYMBOL(of_root);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070040struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080041struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -070042struct device_node *of_stdout;
Leif Lindholm7914a7c2014-11-27 17:56:07 +000043static const char *of_stdout_options;
Shawn Guo611cad72011-08-15 15:28:14 +080044
Grant Likely8a2b22a2014-07-23 17:05:06 -060045struct kset *of_kset;
Grant Likely75b57ec2014-02-20 18:02:11 +000046
47/*
Grant Likely8a2b22a2014-07-23 17:05:06 -060048 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
49 * This mutex must be held whenever modifications are being made to the
50 * device tree. The of_{attach,detach}_node() and
51 * of_{add,remove,update}_property() helpers make sure this happens.
Grant Likely75b57ec2014-02-20 18:02:11 +000052 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030053DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100054
Grant Likely5063e252014-10-03 16:28:27 +010055/* use when traversing tree through the child, sibling,
Stephen Rothwell581b6052007-04-24 16:46:53 +100056 * or parent members of struct device_node.
57 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050058DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100059
60int of_n_addr_cells(struct device_node *np)
61{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060062 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100063
64 do {
65 if (np->parent)
66 np = np->parent;
67 ip = of_get_property(np, "#address-cells", NULL);
68 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070069 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100070 } while (np->parent);
71 /* No #address-cells property for the root node */
72 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
73}
74EXPORT_SYMBOL(of_n_addr_cells);
75
76int of_n_size_cells(struct device_node *np)
77{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060078 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100079
80 do {
81 if (np->parent)
82 np = np->parent;
83 ip = of_get_property(np, "#size-cells", NULL);
84 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070085 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100086 } while (np->parent);
87 /* No #size-cells property for the root node */
88 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
89}
90EXPORT_SYMBOL(of_n_size_cells);
91
Rob Herring0c3f0612013-09-17 10:42:50 -050092#ifdef CONFIG_NUMA
93int __weak of_node_to_nid(struct device_node *np)
94{
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +030095 return NUMA_NO_NODE;
Rob Herring0c3f0612013-09-17 10:42:50 -050096}
97#endif
98
Grant Likely6afc0dc2014-06-26 15:40:48 +010099#ifndef CONFIG_OF_DYNAMIC
Grant Likely75b57ec2014-02-20 18:02:11 +0000100static void of_node_release(struct kobject *kobj)
101{
102 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
103}
Grant Likely0f22dd32012-02-15 20:38:40 -0700104#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700105
Grant Likely75b57ec2014-02-20 18:02:11 +0000106struct kobj_type of_node_ktype = {
107 .release = of_node_release,
108};
109
110static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
111 struct bin_attribute *bin_attr, char *buf,
112 loff_t offset, size_t count)
113{
114 struct property *pp = container_of(bin_attr, struct property, attr);
115 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
116}
117
Frank Rowandd9fc8802016-06-16 10:51:46 -0700118/* always return newly allocated name, caller must free after use */
Grant Likely75b57ec2014-02-20 18:02:11 +0000119static const char *safe_name(struct kobject *kobj, const char *orig_name)
120{
121 const char *name = orig_name;
122 struct kernfs_node *kn;
123 int i = 0;
124
125 /* don't be a hero. After 16 tries give up */
126 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
127 sysfs_put(kn);
128 if (name != orig_name)
129 kfree(name);
130 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
131 }
132
Frank Rowandd9fc8802016-06-16 10:51:46 -0700133 if (name == orig_name) {
134 name = kstrdup(orig_name, GFP_KERNEL);
135 } else {
Rob Herring606ad422016-06-15 08:32:18 -0500136 pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
Grant Likely75b57ec2014-02-20 18:02:11 +0000137 kobject_name(kobj), name);
Frank Rowandd9fc8802016-06-16 10:51:46 -0700138 }
Grant Likely75b57ec2014-02-20 18:02:11 +0000139 return name;
140}
141
Grant Likely8a2b22a2014-07-23 17:05:06 -0600142int __of_add_property_sysfs(struct device_node *np, struct property *pp)
Grant Likely75b57ec2014-02-20 18:02:11 +0000143{
144 int rc;
145
146 /* Important: Don't leak passwords */
147 bool secure = strncmp(pp->name, "security-", 9) == 0;
148
Gaurav Minochaef69d742014-09-05 09:56:13 -0700149 if (!IS_ENABLED(CONFIG_SYSFS))
150 return 0;
151
Grant Likely8a2b22a2014-07-23 17:05:06 -0600152 if (!of_kset || !of_node_is_attached(np))
153 return 0;
154
Grant Likely75b57ec2014-02-20 18:02:11 +0000155 sysfs_bin_attr_init(&pp->attr);
156 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
157 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
158 pp->attr.size = secure ? 0 : pp->length;
159 pp->attr.read = of_node_property_read;
160
161 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
162 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
163 return rc;
164}
165
Grant Likely8a2b22a2014-07-23 17:05:06 -0600166int __of_attach_node_sysfs(struct device_node *np)
Grant Likely75b57ec2014-02-20 18:02:11 +0000167{
168 const char *name;
Frank Rowandd9fc8802016-06-16 10:51:46 -0700169 struct kobject *parent;
Grant Likely75b57ec2014-02-20 18:02:11 +0000170 struct property *pp;
171 int rc;
172
Gaurav Minochaef69d742014-09-05 09:56:13 -0700173 if (!IS_ENABLED(CONFIG_SYSFS))
174 return 0;
175
Grant Likely8a2b22a2014-07-23 17:05:06 -0600176 if (!of_kset)
177 return 0;
178
Grant Likely75b57ec2014-02-20 18:02:11 +0000179 np->kobj.kset = of_kset;
180 if (!np->parent) {
181 /* Nodes without parents are new top level trees */
Frank Rowandd9fc8802016-06-16 10:51:46 -0700182 name = safe_name(&of_kset->kobj, "base");
183 parent = NULL;
Grant Likely75b57ec2014-02-20 18:02:11 +0000184 } else {
185 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
Frank Rowandd9fc8802016-06-16 10:51:46 -0700186 parent = &np->parent->kobj;
Grant Likely75b57ec2014-02-20 18:02:11 +0000187 }
Frank Rowandd9fc8802016-06-16 10:51:46 -0700188 if (!name)
189 return -ENOMEM;
190 rc = kobject_add(&np->kobj, parent, "%s", name);
191 kfree(name);
Grant Likely75b57ec2014-02-20 18:02:11 +0000192 if (rc)
193 return rc;
194
195 for_each_property_of_node(np, pp)
196 __of_add_property_sysfs(np, pp);
197
198 return 0;
199}
200
Frank Rowandf64054b2018-02-23 14:23:10 +0530201static struct device_node **phandle_cache;
202static u32 phandle_cache_mask;
203
204/*
205 * Assumptions behind phandle_cache implementation:
206 * - phandle property values are in a contiguous range of 1..n
207 *
208 * If the assumptions do not hold, then
209 * - the phandle lookup overhead reduction provided by the cache
210 * will likely be less
211 */
212static void of_populate_phandle_cache(void)
213{
214 unsigned long flags;
215 u32 cache_entries;
216 struct device_node *np;
217 u32 phandles = 0;
218
219 raw_spin_lock_irqsave(&devtree_lock, flags);
220
221 kfree(phandle_cache);
222 phandle_cache = NULL;
223
224 for_each_of_allnodes(np)
225 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
226 phandles++;
227
228 cache_entries = roundup_pow_of_two(phandles);
229 phandle_cache_mask = cache_entries - 1;
230
231 phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache),
232 GFP_ATOMIC);
233
234 if (phandle_cache)
235 for_each_of_allnodes(np)
236 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
237 phandle_cache[np->phandle & phandle_cache_mask] = np;
238
239 raw_spin_unlock_irqrestore(&devtree_lock, flags);
240}
241
242#ifndef CONFIG_MODULES
243static int __init of_free_phandle_cache(void)
244{
245 unsigned long flags;
246
247 raw_spin_lock_irqsave(&devtree_lock, flags);
248
249 kfree(phandle_cache);
250 phandle_cache = NULL;
251
252 raw_spin_unlock_irqrestore(&devtree_lock, flags);
253
254 return 0;
255}
256late_initcall_sync(of_free_phandle_cache);
257#endif
258
Sudeep Holla194ec932015-05-14 15:28:24 +0100259void __init of_core_init(void)
Grant Likely75b57ec2014-02-20 18:02:11 +0000260{
261 struct device_node *np;
262
Frank Rowandf64054b2018-02-23 14:23:10 +0530263 of_populate_phandle_cache();
264
Grant Likely75b57ec2014-02-20 18:02:11 +0000265 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300266 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000267 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
268 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300269 mutex_unlock(&of_mutex);
Rob Herring606ad422016-06-15 08:32:18 -0500270 pr_err("failed to register existing nodes\n");
Sudeep Holla194ec932015-05-14 15:28:24 +0100271 return;
Grant Likely75b57ec2014-02-20 18:02:11 +0000272 }
273 for_each_of_allnodes(np)
Grant Likely8a2b22a2014-07-23 17:05:06 -0600274 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300275 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000276
Grant Likely83570412012-11-06 21:03:27 +0000277 /* Symlink in /proc as required by userspace ABI */
Grant Likely5063e252014-10-03 16:28:27 +0100278 if (of_root)
Grant Likely75b57ec2014-02-20 18:02:11 +0000279 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000280}
Grant Likely75b57ec2014-02-20 18:02:11 +0000281
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500282static struct property *__of_find_property(const struct device_node *np,
283 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000284{
285 struct property *pp;
286
Timur Tabi64e45662008-05-08 05:19:59 +1000287 if (!np)
288 return NULL;
289
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530290 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000291 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530292 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000293 *lenp = pp->length;
294 break;
295 }
296 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500297
298 return pp;
299}
300
301struct property *of_find_property(const struct device_node *np,
302 const char *name,
303 int *lenp)
304{
305 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500306 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500307
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500308 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500309 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500310 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000311
312 return pp;
313}
314EXPORT_SYMBOL(of_find_property);
315
Grant Likely5063e252014-10-03 16:28:27 +0100316struct device_node *__of_find_all_nodes(struct device_node *prev)
317{
318 struct device_node *np;
319 if (!prev) {
320 np = of_root;
321 } else if (prev->child) {
322 np = prev->child;
323 } else {
324 /* Walk back up looking for a sibling, or the end of the structure */
325 np = prev;
326 while (np->parent && !np->sibling)
327 np = np->parent;
328 np = np->sibling; /* Might be null at the end of the tree */
329 }
330 return np;
331}
332
Grant Likelye91edcf2009-10-15 10:58:09 -0600333/**
334 * of_find_all_nodes - Get next node in global list
335 * @prev: Previous node or NULL to start iteration
336 * of_node_put() will be called on it
337 *
338 * Returns a node pointer with refcount incremented, use
339 * of_node_put() on it when done.
340 */
341struct device_node *of_find_all_nodes(struct device_node *prev)
342{
343 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000344 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600345
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000346 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100347 np = __of_find_all_nodes(prev);
348 of_node_get(np);
Grant Likelye91edcf2009-10-15 10:58:09 -0600349 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000350 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600351 return np;
352}
353EXPORT_SYMBOL(of_find_all_nodes);
354
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000355/*
356 * Find a property with a given name for a given node
357 * and return the value.
358 */
Grant Likelya25095d2014-07-15 23:25:43 -0600359const void *__of_get_property(const struct device_node *np,
360 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500361{
362 struct property *pp = __of_find_property(np, name, lenp);
363
364 return pp ? pp->value : NULL;
365}
366
367/*
368 * Find a property with a given name for a given node
369 * and return the value.
370 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000371const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500372 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000373{
374 struct property *pp = of_find_property(np, name, lenp);
375
376 return pp ? pp->value : NULL;
377}
378EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000379
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100380/*
381 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
382 *
383 * @cpu: logical cpu index of a core/thread
384 * @phys_id: physical identifier of a core/thread
385 *
386 * CPU logical to physical index mapping is architecture specific.
387 * However this __weak function provides a default match of physical
388 * id to logical cpu index. phys_id provided here is usually values read
389 * from the device tree which must match the hardware internal registers.
390 *
391 * Returns true if the physical identifier and the logical cpu index
392 * correspond to the same core/thread, false otherwise.
393 */
394bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
395{
396 return (u32)phys_id == cpu;
397}
398
399/**
400 * Checks if the given "prop_name" property holds the physical id of the
401 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
402 * NULL, local thread number within the core is returned in it.
403 */
404static bool __of_find_n_match_cpu_property(struct device_node *cpun,
405 const char *prop_name, int cpu, unsigned int *thread)
406{
407 const __be32 *cell;
408 int ac, prop_len, tid;
409 u64 hwid;
410
411 ac = of_n_addr_cells(cpun);
412 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100413 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100414 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100415 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100416 for (tid = 0; tid < prop_len; tid++) {
417 hwid = of_read_number(cell, ac);
418 if (arch_match_cpu_phys_id(cpu, hwid)) {
419 if (thread)
420 *thread = tid;
421 return true;
422 }
423 cell += ac;
424 }
425 return false;
426}
427
David Millerd1cb9d12013-10-03 17:24:51 -0400428/*
429 * arch_find_n_match_cpu_physical_id - See if the given device node is
430 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
431 * else false. If 'thread' is non-NULL, the local thread number within the
432 * core is returned in it.
433 */
434bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
435 int cpu, unsigned int *thread)
436{
437 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
438 * for thread ids on PowerPC. If it doesn't exist fallback to
439 * standard "reg" property.
440 */
441 if (IS_ENABLED(CONFIG_PPC) &&
442 __of_find_n_match_cpu_property(cpun,
443 "ibm,ppc-interrupt-server#s",
444 cpu, thread))
445 return true;
446
Masahiro Yamada510bd062015-10-28 12:05:27 +0900447 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
David Millerd1cb9d12013-10-03 17:24:51 -0400448}
449
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100450/**
451 * of_get_cpu_node - Get device node associated with the given logical CPU
452 *
453 * @cpu: CPU number(logical index) for which device node is required
454 * @thread: if not NULL, local thread number within the physical core is
455 * returned
456 *
457 * The main purpose of this function is to retrieve the device node for the
458 * given logical CPU index. It should be used to initialize the of_node in
459 * cpu device. Once of_node in cpu device is populated, all the further
460 * references can use that instead.
461 *
462 * CPU logical to physical index mapping is architecture specific and is built
463 * before booting secondary cores. This function uses arch_match_cpu_phys_id
464 * which can be overridden by architecture specific implementation.
465 *
Masahiro Yamada1c986e32016-04-20 10:18:46 +0900466 * Returns a node pointer for the logical cpu with refcount incremented, use
467 * of_node_put() on it when done. Returns NULL if not found.
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100468 */
469struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
470{
David Millerd1cb9d12013-10-03 17:24:51 -0400471 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100472
David Millerd1cb9d12013-10-03 17:24:51 -0400473 for_each_node_by_type(cpun, "cpu") {
474 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100475 return cpun;
476 }
477 return NULL;
478}
479EXPORT_SYMBOL(of_get_cpu_node);
480
Kevin Hao215a14c2014-02-19 16:15:45 +0800481/**
482 * __of_device_is_compatible() - Check if the node matches given constraints
483 * @device: pointer to node
484 * @compat: required compatible string, NULL or "" for any match
485 * @type: required device_type value, NULL or "" for any match
486 * @name: required node name, NULL or "" for any match
487 *
488 * Checks if the given @compat, @type and @name strings match the
489 * properties of the given @device. A constraints can be skipped by
490 * passing NULL or an empty string as the constraint.
491 *
492 * Returns 0 for no match, and a positive integer on match. The return
493 * value is a relative score with larger values indicating better
494 * matches. The score is weighted for the most specific compatible value
495 * to get the highest score. Matching type is next, followed by matching
496 * name. Practically speaking, this results in the following priority
497 * order for matches:
498 *
499 * 1. specific compatible && type && name
500 * 2. specific compatible && type
501 * 3. specific compatible && name
502 * 4. specific compatible
503 * 5. general compatible && type && name
504 * 6. general compatible && type
505 * 7. general compatible && name
506 * 8. general compatible
507 * 9. type && name
508 * 10. type
509 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000510 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500511static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800512 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000513{
Kevin Hao215a14c2014-02-19 16:15:45 +0800514 struct property *prop;
515 const char *cp;
516 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000517
Kevin Hao215a14c2014-02-19 16:15:45 +0800518 /* Compatible match has highest priority */
519 if (compat && compat[0]) {
520 prop = __of_find_property(device, "compatible", NULL);
521 for (cp = of_prop_next_string(prop, NULL); cp;
522 cp = of_prop_next_string(prop, cp), index++) {
523 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
524 score = INT_MAX/2 - (index << 2);
525 break;
526 }
527 }
528 if (!score)
529 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000530 }
531
Kevin Hao215a14c2014-02-19 16:15:45 +0800532 /* Matching type is better than matching name */
533 if (type && type[0]) {
534 if (!device->type || of_node_cmp(type, device->type))
535 return 0;
536 score += 2;
537 }
538
539 /* Matching name is a bit better than not */
540 if (name && name[0]) {
541 if (!device->name || of_node_cmp(name, device->name))
542 return 0;
543 score++;
544 }
545
546 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000547}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500548
549/** Checks if the given "compat" string matches one of the strings in
550 * the device's "compatible" property
551 */
552int of_device_is_compatible(const struct device_node *device,
553 const char *compat)
554{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500555 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500556 int res;
557
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500558 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800559 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500560 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500561 return res;
562}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000563EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000564
Benjamin Herrenschmidtb9c13fe2016-07-08 08:35:59 +1000565/** Checks if the device is compatible with any of the entries in
566 * a NULL terminated array of strings. Returns the best match
567 * score or 0.
568 */
569int of_device_compatible_match(struct device_node *device,
570 const char *const *compat)
571{
572 unsigned int tmp, score = 0;
573
574 if (!compat)
575 return 0;
576
577 while (*compat) {
578 tmp = of_device_is_compatible(device, *compat);
579 if (tmp > score)
580 score = tmp;
581 compat++;
582 }
583
584 return score;
585}
586
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000587/**
Grant Likely71a157e2010-02-01 21:34:14 -0700588 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700589 * @compat: compatible string to look for in root node's compatible property.
590 *
Kevin Cernekee25c7a1d2014-11-12 12:54:00 -0800591 * Returns a positive integer if the root node has the given value in its
Grant Likely1f43cfb2010-01-28 13:47:25 -0700592 * compatible property.
593 */
Grant Likely71a157e2010-02-01 21:34:14 -0700594int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700595{
596 struct device_node *root;
597 int rc = 0;
598
599 root = of_find_node_by_path("/");
600 if (root) {
601 rc = of_device_is_compatible(root, compat);
602 of_node_put(root);
603 }
604 return rc;
605}
Grant Likely71a157e2010-02-01 21:34:14 -0700606EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700607
608/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700609 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100610 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700611 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100612 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800613 * Returns true if the status property is absent or set to "okay" or "ok",
614 * false otherwise
Josh Boyer834d97d2008-03-27 00:33:14 +1100615 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800616static bool __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100617{
618 const char *status;
619 int statlen;
620
Xiubo Li42ccd782014-01-13 11:07:28 +0800621 if (!device)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800622 return false;
Xiubo Li42ccd782014-01-13 11:07:28 +0800623
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700624 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100625 if (status == NULL)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800626 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100627
628 if (statlen > 0) {
629 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800630 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100631 }
632
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800633 return false;
Josh Boyer834d97d2008-03-27 00:33:14 +1100634}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700635
636/**
637 * of_device_is_available - check if a device is available for use
638 *
639 * @device: Node to check for availability
640 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800641 * Returns true if the status property is absent or set to "okay" or "ok",
642 * false otherwise
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700643 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800644bool of_device_is_available(const struct device_node *device)
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700645{
646 unsigned long flags;
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800647 bool res;
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700648
649 raw_spin_lock_irqsave(&devtree_lock, flags);
650 res = __of_device_is_available(device);
651 raw_spin_unlock_irqrestore(&devtree_lock, flags);
652 return res;
653
654}
Josh Boyer834d97d2008-03-27 00:33:14 +1100655EXPORT_SYMBOL(of_device_is_available);
656
657/**
Kevin Cernekee37786c72015-04-09 13:05:14 -0700658 * of_device_is_big_endian - check if a device has BE registers
659 *
660 * @device: Node to check for endianness
661 *
662 * Returns true if the device has a "big-endian" property, or if the kernel
663 * was compiled for BE *and* the device has a "native-endian" property.
664 * Returns false otherwise.
665 *
666 * Callers would nominally use ioread32be/iowrite32be if
667 * of_device_is_big_endian() == true, or readl/writel otherwise.
668 */
669bool of_device_is_big_endian(const struct device_node *device)
670{
671 if (of_property_read_bool(device, "big-endian"))
672 return true;
673 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
674 of_property_read_bool(device, "native-endian"))
675 return true;
676 return false;
677}
678EXPORT_SYMBOL(of_device_is_big_endian);
679
680/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000681 * of_get_parent - Get a node's parent if any
682 * @node: Node to get parent
683 *
684 * Returns a node pointer with refcount incremented, use
685 * of_node_put() on it when done.
686 */
687struct device_node *of_get_parent(const struct device_node *node)
688{
689 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500690 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000691
692 if (!node)
693 return NULL;
694
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500695 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000696 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500697 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000698 return np;
699}
700EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000701
702/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000703 * of_get_next_parent - Iterate to a node's parent
704 * @node: Node to get parent of
705 *
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +0200706 * This is like of_get_parent() except that it drops the
707 * refcount on the passed node, making it suitable for iterating
708 * through a node's parents.
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000709 *
710 * Returns a node pointer with refcount incremented, use
711 * of_node_put() on it when done.
712 */
713struct device_node *of_get_next_parent(struct device_node *node)
714{
715 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500716 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000717
718 if (!node)
719 return NULL;
720
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500721 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000722 parent = of_node_get(node->parent);
723 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500724 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000725 return parent;
726}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300727EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000728
Grant Likely0d0e02d2014-05-22 01:04:17 +0900729static struct device_node *__of_get_next_child(const struct device_node *node,
730 struct device_node *prev)
731{
732 struct device_node *next;
733
Florian Fainelli43cb4362014-05-28 10:39:02 -0700734 if (!node)
735 return NULL;
736
Grant Likely0d0e02d2014-05-22 01:04:17 +0900737 next = prev ? prev->sibling : node->child;
738 for (; next; next = next->sibling)
739 if (of_node_get(next))
740 break;
741 of_node_put(prev);
742 return next;
743}
744#define __for_each_child_of_node(parent, child) \
745 for (child = __of_get_next_child(parent, NULL); child != NULL; \
746 child = __of_get_next_child(parent, child))
747
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000748/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000749 * of_get_next_child - Iterate a node childs
750 * @node: parent node
751 * @prev: previous child of the parent node, or NULL to get first
752 *
Baruch Siach64808272015-03-19 14:03:41 +0200753 * Returns a node pointer with refcount incremented, use of_node_put() on
754 * it when done. Returns NULL when prev is the last child. Decrements the
755 * refcount of prev.
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000756 */
757struct device_node *of_get_next_child(const struct device_node *node,
758 struct device_node *prev)
759{
760 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500761 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000762
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500763 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900764 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500765 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000766 return next;
767}
768EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000769
770/**
Timur Tabi32961932012-08-14 13:20:23 +0000771 * of_get_next_available_child - Find the next available child node
772 * @node: parent node
773 * @prev: previous child of the parent node, or NULL to get first
774 *
775 * This function is like of_get_next_child(), except that it
776 * automatically skips any disabled nodes (i.e. status = "disabled").
777 */
778struct device_node *of_get_next_available_child(const struct device_node *node,
779 struct device_node *prev)
780{
781 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000782 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000783
Florian Fainelli43cb4362014-05-28 10:39:02 -0700784 if (!node)
785 return NULL;
786
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000787 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000788 next = prev ? prev->sibling : node->child;
789 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700790 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000791 continue;
792 if (of_node_get(next))
793 break;
794 }
795 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000796 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000797 return next;
798}
799EXPORT_SYMBOL(of_get_next_available_child);
800
801/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100802 * of_get_child_by_name - Find the child node by name for a given parent
803 * @node: parent node
804 * @name: child name to look for.
805 *
806 * This function looks for child node for given matching name
807 *
808 * Returns a node pointer if found, with refcount incremented, use
809 * of_node_put() on it when done.
810 * Returns NULL if node is not found.
811 */
812struct device_node *of_get_child_by_name(const struct device_node *node,
813 const char *name)
814{
815 struct device_node *child;
816
817 for_each_child_of_node(node, child)
818 if (child->name && (of_node_cmp(child->name, name) == 0))
819 break;
820 return child;
821}
822EXPORT_SYMBOL(of_get_child_by_name);
823
Grant Likelyc22e6502014-03-14 17:07:12 +0000824static struct device_node *__of_find_node_by_path(struct device_node *parent,
825 const char *path)
826{
827 struct device_node *child;
Leif Lindholm106937e2015-03-06 16:52:53 +0000828 int len;
Grant Likelyc22e6502014-03-14 17:07:12 +0000829
Brian Norris721a09e2015-03-17 12:30:31 -0700830 len = strcspn(path, "/:");
Grant Likelyc22e6502014-03-14 17:07:12 +0000831 if (!len)
832 return NULL;
833
834 __for_each_child_of_node(parent, child) {
835 const char *name = strrchr(child->full_name, '/');
836 if (WARN(!name, "malformed device_node %s\n", child->full_name))
837 continue;
838 name++;
839 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
840 return child;
841 }
842 return NULL;
843}
844
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100845/**
Leif Lindholm75c28c02014-11-28 11:34:28 +0000846 * of_find_node_opts_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000847 * @path: Either the full path to match, or if the path does not
848 * start with '/', the name of a property of the /aliases
849 * node (an alias). In the case of an alias, the node
850 * matching the alias' value will be returned.
Leif Lindholm75c28c02014-11-28 11:34:28 +0000851 * @opts: Address of a pointer into which to store the start of
852 * an options string appended to the end of the path with
853 * a ':' separator.
Grant Likelyc22e6502014-03-14 17:07:12 +0000854 *
855 * Valid paths:
856 * /foo/bar Full path
857 * foo Valid alias
858 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000859 *
860 * Returns a node pointer with refcount incremented, use
861 * of_node_put() on it when done.
862 */
Leif Lindholm75c28c02014-11-28 11:34:28 +0000863struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000864{
Grant Likelyc22e6502014-03-14 17:07:12 +0000865 struct device_node *np = NULL;
866 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500867 unsigned long flags;
Leif Lindholm75c28c02014-11-28 11:34:28 +0000868 const char *separator = strchr(path, ':');
869
870 if (opts)
871 *opts = separator ? separator + 1 : NULL;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000872
Grant Likelyc22e6502014-03-14 17:07:12 +0000873 if (strcmp(path, "/") == 0)
Grant Likely5063e252014-10-03 16:28:27 +0100874 return of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000875
876 /* The path could begin with an alias */
877 if (*path != '/') {
Leif Lindholm106937e2015-03-06 16:52:53 +0000878 int len;
879 const char *p = separator;
880
881 if (!p)
882 p = strchrnul(path, '/');
883 len = p - path;
Grant Likelyc22e6502014-03-14 17:07:12 +0000884
885 /* of_aliases must not be NULL */
886 if (!of_aliases)
887 return NULL;
888
889 for_each_property_of_node(of_aliases, pp) {
890 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
891 np = of_find_node_by_path(pp->value);
892 break;
893 }
894 }
895 if (!np)
896 return NULL;
897 path = p;
898 }
899
900 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500901 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000902 if (!np)
Grant Likely5063e252014-10-03 16:28:27 +0100903 np = of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000904 while (np && *path == '/') {
905 path++; /* Increment past '/' delimiter */
906 np = __of_find_node_by_path(np, path);
907 path = strchrnul(path, '/');
Leif Lindholm106937e2015-03-06 16:52:53 +0000908 if (separator && separator < path)
909 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000910 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500911 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000912 return np;
913}
Leif Lindholm75c28c02014-11-28 11:34:28 +0000914EXPORT_SYMBOL(of_find_node_opts_by_path);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000915
916/**
917 * of_find_node_by_name - Find a node by its "name" property
918 * @from: The node to start searching from or NULL, the node
919 * you pass will not be searched, only the next one
920 * will; typically, you pass what the previous call
921 * returned. of_node_put() will be called on it
922 * @name: The name string to match against
923 *
924 * Returns a node pointer with refcount incremented, use
925 * of_node_put() on it when done.
926 */
927struct device_node *of_find_node_by_name(struct device_node *from,
928 const char *name)
929{
930 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500931 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000932
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500933 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100934 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000935 if (np->name && (of_node_cmp(np->name, name) == 0)
936 && of_node_get(np))
937 break;
938 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500939 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000940 return np;
941}
942EXPORT_SYMBOL(of_find_node_by_name);
943
944/**
945 * of_find_node_by_type - Find a node by its "device_type" property
946 * @from: The node to start searching from, or NULL to start searching
947 * the entire device tree. The node you pass will not be
948 * searched, only the next one will; typically, you pass
949 * what the previous call returned. of_node_put() will be
950 * called on from for you.
951 * @type: The type string to match against
952 *
953 * Returns a node pointer with refcount incremented, use
954 * of_node_put() on it when done.
955 */
956struct device_node *of_find_node_by_type(struct device_node *from,
957 const char *type)
958{
959 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500960 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000961
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500962 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100963 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000964 if (np->type && (of_node_cmp(np->type, type) == 0)
965 && of_node_get(np))
966 break;
967 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500968 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000969 return np;
970}
971EXPORT_SYMBOL(of_find_node_by_type);
972
973/**
974 * of_find_compatible_node - Find a node based on type and one of the
975 * tokens in its "compatible" property
976 * @from: The node to start searching from or NULL, the node
977 * you pass will not be searched, only the next one
978 * will; typically, you pass what the previous call
979 * returned. of_node_put() will be called on it
980 * @type: The type string to match "device_type" or NULL to ignore
981 * @compatible: The string to match to one of the tokens in the device
982 * "compatible" list.
983 *
984 * Returns a node pointer with refcount incremented, use
985 * of_node_put() on it when done.
986 */
987struct device_node *of_find_compatible_node(struct device_node *from,
988 const char *type, const char *compatible)
989{
990 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500991 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000992
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500993 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100994 for_each_of_allnodes_from(from, np)
Kevin Hao215a14c2014-02-19 16:15:45 +0800995 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500996 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000997 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000998 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500999 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001000 return np;
1001}
1002EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +11001003
1004/**
Michael Ellerman1e291b12008-11-12 18:54:42 +00001005 * of_find_node_with_property - Find a node which has a property with
1006 * the given name.
1007 * @from: The node to start searching from or NULL, the node
1008 * you pass will not be searched, only the next one
1009 * will; typically, you pass what the previous call
1010 * returned. of_node_put() will be called on it
1011 * @prop_name: The name of the property to look for.
1012 *
1013 * Returns a node pointer with refcount incremented, use
1014 * of_node_put() on it when done.
1015 */
1016struct device_node *of_find_node_with_property(struct device_node *from,
1017 const char *prop_name)
1018{
1019 struct device_node *np;
1020 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001021 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +00001022
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001023 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001024 for_each_of_allnodes_from(from, np) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +05301025 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +00001026 if (of_prop_cmp(pp->name, prop_name) == 0) {
1027 of_node_get(np);
1028 goto out;
1029 }
1030 }
1031 }
1032out:
1033 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001034 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +00001035 return np;
1036}
1037EXPORT_SYMBOL(of_find_node_with_property);
1038
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001039static
1040const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1041 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +11001042{
Kevin Hao215a14c2014-02-19 16:15:45 +08001043 const struct of_device_id *best_match = NULL;
1044 int score, best_score = 0;
1045
Grant Likelya52f07e2011-03-18 10:21:29 -06001046 if (!matches)
1047 return NULL;
1048
Kevin Hao215a14c2014-02-19 16:15:45 +08001049 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1050 score = __of_device_is_compatible(node, matches->compatible,
1051 matches->type, matches->name);
1052 if (score > best_score) {
1053 best_match = matches;
1054 best_score = score;
1055 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +08001056 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001057
1058 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001059}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001060
1061/**
Geert Uytterhoevenc50949d2014-10-22 11:44:54 +02001062 * of_match_node - Tell if a device_node has a matching of_match structure
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001063 * @matches: array of of device match structures to search in
1064 * @node: the of device structure to match against
1065 *
Kevin Hao71c54982014-02-18 15:57:29 +08001066 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001067 */
1068const struct of_device_id *of_match_node(const struct of_device_id *matches,
1069 const struct device_node *node)
1070{
1071 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001072 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001073
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001074 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001075 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001076 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001077 return match;
1078}
Grant Likely283029d2008-01-09 06:20:40 +11001079EXPORT_SYMBOL(of_match_node);
1080
1081/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001082 * of_find_matching_node_and_match - Find a node based on an of_device_id
1083 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001084 * @from: The node to start searching from or NULL, the node
1085 * you pass will not be searched, only the next one
1086 * will; typically, you pass what the previous call
1087 * returned. of_node_put() will be called on it
1088 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001089 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001090 *
1091 * Returns a node pointer with refcount incremented, use
1092 * of_node_put() on it when done.
1093 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001094struct device_node *of_find_matching_node_and_match(struct device_node *from,
1095 const struct of_device_id *matches,
1096 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001097{
1098 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001099 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001100 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001101
Stephen Warren50c8af42012-11-20 16:12:20 -07001102 if (match)
1103 *match = NULL;
1104
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001105 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001106 for_each_of_allnodes_from(from, np) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001107 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001108 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001109 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001110 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001111 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001112 }
Grant Likely283029d2008-01-09 06:20:40 +11001113 }
1114 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001115 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001116 return np;
1117}
Grant Likely80c20222012-12-19 10:45:36 +00001118EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001119
1120/**
Grant Likely3f07af42008-07-25 22:25:13 -04001121 * of_modalias_node - Lookup appropriate modalias for a device node
1122 * @node: pointer to a device tree node
1123 * @modalias: Pointer to buffer that modalias value will be copied into
1124 * @len: Length of modalias value
1125 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001126 * Based on the value of the compatible property, this routine will attempt
1127 * to choose an appropriate modalias value for a particular device tree node.
1128 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1129 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001130 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001131 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001132 */
1133int of_modalias_node(struct device_node *node, char *modalias, int len)
1134{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001135 const char *compatible, *p;
1136 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001137
1138 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001139 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001140 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001141 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001142 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001143 return 0;
1144}
1145EXPORT_SYMBOL_GPL(of_modalias_node);
1146
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001147/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001148 * of_find_node_by_phandle - Find a node given a phandle
1149 * @handle: phandle of the node to find
1150 *
1151 * Returns a node pointer with refcount incremented, use
1152 * of_node_put() on it when done.
1153 */
1154struct device_node *of_find_node_by_phandle(phandle handle)
1155{
Frank Rowandf64054b2018-02-23 14:23:10 +05301156 struct device_node *np = NULL;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001157 unsigned long flags;
Frank Rowandf64054b2018-02-23 14:23:10 +05301158 phandle masked_handle;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001159
Grant Likelyfc59b442014-10-02 13:08:02 +01001160 if (!handle)
1161 return NULL;
1162
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001163 raw_spin_lock_irqsave(&devtree_lock, flags);
Frank Rowandf64054b2018-02-23 14:23:10 +05301164
1165 masked_handle = handle & phandle_cache_mask;
1166
1167 if (phandle_cache) {
1168 if (phandle_cache[masked_handle] &&
1169 handle == phandle_cache[masked_handle]->phandle)
1170 np = phandle_cache[masked_handle];
1171 }
1172
1173 if (!np) {
1174 for_each_of_allnodes(np)
1175 if (np->phandle == handle) {
1176 if (phandle_cache)
1177 phandle_cache[masked_handle] = np;
1178 break;
1179 }
1180 }
1181
Jeremy Kerr89751a72010-02-01 21:34:11 -07001182 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001183 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001184 return np;
1185}
1186EXPORT_SYMBOL(of_find_node_by_phandle);
1187
1188/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +01001189 * of_property_count_elems_of_size - Count the number of elements in a property
1190 *
1191 * @np: device node from which the property value is to be read.
1192 * @propname: name of the property to be searched.
1193 * @elem_size: size of the individual element
1194 *
1195 * Search for a property in a device node and count the number of elements of
1196 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1197 * property does not exist or its length does not match a multiple of elem_size
1198 * and -ENODATA if the property does not have a value.
1199 */
1200int of_property_count_elems_of_size(const struct device_node *np,
1201 const char *propname, int elem_size)
1202{
1203 struct property *prop = of_find_property(np, propname, NULL);
1204
1205 if (!prop)
1206 return -EINVAL;
1207 if (!prop->value)
1208 return -ENODATA;
1209
1210 if (prop->length % elem_size != 0) {
1211 pr_err("size of %s in node %s is not a multiple of %d\n",
1212 propname, np->full_name, elem_size);
1213 return -EINVAL;
1214 }
1215
1216 return prop->length / elem_size;
1217}
1218EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1219
1220/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001221 * of_find_property_value_of_size
1222 *
1223 * @np: device node from which the property value is to be read.
1224 * @propname: name of the property to be searched.
Richard Fitzgerald79ac5d32016-09-12 14:01:28 +01001225 * @min: minimum allowed length of property value
1226 * @max: maximum allowed length of property value (0 means unlimited)
1227 * @len: if !=NULL, actual length is written to here
Tony Priskdaeec1f2013-04-03 17:57:11 +13001228 *
1229 * Search for a property in a device node and valid the requested size.
1230 * Returns the property value on success, -EINVAL if the property does not
1231 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
Richard Fitzgerald79ac5d32016-09-12 14:01:28 +01001232 * property data is too small or too large.
Tony Priskdaeec1f2013-04-03 17:57:11 +13001233 *
1234 */
1235static void *of_find_property_value_of_size(const struct device_node *np,
Richard Fitzgerald79ac5d32016-09-12 14:01:28 +01001236 const char *propname, u32 min, u32 max, size_t *len)
Tony Priskdaeec1f2013-04-03 17:57:11 +13001237{
1238 struct property *prop = of_find_property(np, propname, NULL);
1239
1240 if (!prop)
1241 return ERR_PTR(-EINVAL);
1242 if (!prop->value)
1243 return ERR_PTR(-ENODATA);
Richard Fitzgerald79ac5d32016-09-12 14:01:28 +01001244 if (prop->length < min)
Tony Priskdaeec1f2013-04-03 17:57:11 +13001245 return ERR_PTR(-EOVERFLOW);
Richard Fitzgerald79ac5d32016-09-12 14:01:28 +01001246 if (max && prop->length > max)
1247 return ERR_PTR(-EOVERFLOW);
1248
1249 if (len)
1250 *len = prop->length;
Tony Priskdaeec1f2013-04-03 17:57:11 +13001251
1252 return prop->value;
1253}
1254
1255/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001256 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1257 *
1258 * @np: device node from which the property value is to be read.
1259 * @propname: name of the property to be searched.
1260 * @index: index of the u32 in the list of values
1261 * @out_value: pointer to return value, modified only if no error.
1262 *
1263 * Search for a property in a device node and read nth 32-bit value from
1264 * it. Returns 0 on success, -EINVAL if the property does not exist,
1265 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1266 * property data isn't large enough.
1267 *
1268 * The out_value is modified only if a valid u32 value can be decoded.
1269 */
1270int of_property_read_u32_index(const struct device_node *np,
1271 const char *propname,
1272 u32 index, u32 *out_value)
1273{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001274 const u32 *val = of_find_property_value_of_size(np, propname,
Richard Fitzgerald79ac5d32016-09-12 14:01:28 +01001275 ((index + 1) * sizeof(*out_value)),
1276 0,
1277 NULL);
Tony Prisk3daf3722013-03-23 17:02:15 +13001278
Tony Priskdaeec1f2013-04-03 17:57:11 +13001279 if (IS_ERR(val))
1280 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001281
Tony Priskdaeec1f2013-04-03 17:57:11 +13001282 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001283 return 0;
1284}
1285EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1286
1287/**
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001288 * of_property_read_variable_u8_array - Find and read an array of u8 from a
1289 * property, with bounds on the minimum and maximum array size.
Viresh Kumarbe193242012-11-20 10:15:19 +05301290 *
1291 * @np: device node from which the property value is to be read.
1292 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301293 * @out_values: pointer to return value, modified only if return value is 0.
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001294 * @sz_min: minimum number of array elements to read
1295 * @sz_max: maximum number of array elements to read, if zero there is no
1296 * upper limit on the number of elements in the dts entry but only
1297 * sz_min will be read.
Viresh Kumarbe193242012-11-20 10:15:19 +05301298 *
1299 * Search for a property in a device node and read 8-bit value(s) from
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001300 * it. Returns number of elements read on success, -EINVAL if the property
1301 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1302 * if the property data is smaller than sz_min or longer than sz_max.
Viresh Kumarbe193242012-11-20 10:15:19 +05301303 *
1304 * dts entry of array should be like:
1305 * property = /bits/ 8 <0x50 0x60 0x70>;
1306 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301307 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301308 */
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001309int of_property_read_variable_u8_array(const struct device_node *np,
1310 const char *propname, u8 *out_values,
1311 size_t sz_min, size_t sz_max)
Viresh Kumarbe193242012-11-20 10:15:19 +05301312{
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001313 size_t sz, count;
Tony Priskdaeec1f2013-04-03 17:57:11 +13001314 const u8 *val = of_find_property_value_of_size(np, propname,
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001315 (sz_min * sizeof(*out_values)),
1316 (sz_max * sizeof(*out_values)),
1317 &sz);
Viresh Kumarbe193242012-11-20 10:15:19 +05301318
Tony Priskdaeec1f2013-04-03 17:57:11 +13001319 if (IS_ERR(val))
1320 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301321
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001322 if (!sz_max)
1323 sz = sz_min;
1324 else
1325 sz /= sizeof(*out_values);
1326
1327 count = sz;
1328 while (count--)
Viresh Kumarbe193242012-11-20 10:15:19 +05301329 *out_values++ = *val++;
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001330
1331 return sz;
Viresh Kumarbe193242012-11-20 10:15:19 +05301332}
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001333EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
Viresh Kumarbe193242012-11-20 10:15:19 +05301334
1335/**
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001336 * of_property_read_variable_u16_array - Find and read an array of u16 from a
1337 * property, with bounds on the minimum and maximum array size.
Viresh Kumarbe193242012-11-20 10:15:19 +05301338 *
1339 * @np: device node from which the property value is to be read.
1340 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301341 * @out_values: pointer to return value, modified only if return value is 0.
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001342 * @sz_min: minimum number of array elements to read
1343 * @sz_max: maximum number of array elements to read, if zero there is no
1344 * upper limit on the number of elements in the dts entry but only
1345 * sz_min will be read.
Viresh Kumarbe193242012-11-20 10:15:19 +05301346 *
1347 * Search for a property in a device node and read 16-bit value(s) from
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001348 * it. Returns number of elements read on success, -EINVAL if the property
1349 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1350 * if the property data is smaller than sz_min or longer than sz_max.
Viresh Kumarbe193242012-11-20 10:15:19 +05301351 *
1352 * dts entry of array should be like:
1353 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1354 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301355 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301356 */
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001357int of_property_read_variable_u16_array(const struct device_node *np,
1358 const char *propname, u16 *out_values,
1359 size_t sz_min, size_t sz_max)
Viresh Kumarbe193242012-11-20 10:15:19 +05301360{
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001361 size_t sz, count;
Tony Priskdaeec1f2013-04-03 17:57:11 +13001362 const __be16 *val = of_find_property_value_of_size(np, propname,
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001363 (sz_min * sizeof(*out_values)),
1364 (sz_max * sizeof(*out_values)),
1365 &sz);
Viresh Kumarbe193242012-11-20 10:15:19 +05301366
Tony Priskdaeec1f2013-04-03 17:57:11 +13001367 if (IS_ERR(val))
1368 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301369
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001370 if (!sz_max)
1371 sz = sz_min;
1372 else
1373 sz /= sizeof(*out_values);
1374
1375 count = sz;
1376 while (count--)
Viresh Kumarbe193242012-11-20 10:15:19 +05301377 *out_values++ = be16_to_cpup(val++);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001378
1379 return sz;
Viresh Kumarbe193242012-11-20 10:15:19 +05301380}
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001381EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
Viresh Kumarbe193242012-11-20 10:15:19 +05301382
1383/**
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001384 * of_property_read_variable_u32_array - Find and read an array of 32 bit
1385 * integers from a property, with bounds on the minimum and maximum array size.
Rob Herring0e373632011-07-06 15:42:58 -05001386 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301387 * @np: device node from which the property value is to be read.
1388 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301389 * @out_values: pointer to return value, modified only if return value is 0.
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001390 * @sz_min: minimum number of array elements to read
1391 * @sz_max: maximum number of array elements to read, if zero there is no
1392 * upper limit on the number of elements in the dts entry but only
1393 * sz_min will be read.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301394 *
Rob Herring0e373632011-07-06 15:42:58 -05001395 * Search for a property in a device node and read 32-bit value(s) from
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001396 * it. Returns number of elements read on success, -EINVAL if the property
1397 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1398 * if the property data is smaller than sz_min or longer than sz_max.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301399 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301400 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301401 */
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001402int of_property_read_variable_u32_array(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +01001403 const char *propname, u32 *out_values,
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001404 size_t sz_min, size_t sz_max)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301405{
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001406 size_t sz, count;
Tony Priskdaeec1f2013-04-03 17:57:11 +13001407 const __be32 *val = of_find_property_value_of_size(np, propname,
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001408 (sz_min * sizeof(*out_values)),
1409 (sz_max * sizeof(*out_values)),
1410 &sz);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301411
Tony Priskdaeec1f2013-04-03 17:57:11 +13001412 if (IS_ERR(val))
1413 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001414
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001415 if (!sz_max)
1416 sz = sz_min;
1417 else
1418 sz /= sizeof(*out_values);
1419
1420 count = sz;
1421 while (count--)
Rob Herring0e373632011-07-06 15:42:58 -05001422 *out_values++ = be32_to_cpup(val++);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001423
1424 return sz;
Thomas Abrahama3b85362011-06-30 21:26:10 +05301425}
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001426EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301427
1428/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001429 * of_property_read_u64 - Find and read a 64 bit integer from a property
1430 * @np: device node from which the property value is to be read.
1431 * @propname: name of the property to be searched.
1432 * @out_value: pointer to return value, modified only if return value is 0.
1433 *
1434 * Search for a property in a device node and read a 64-bit value from
1435 * it. Returns 0 on success, -EINVAL if the property does not exist,
1436 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1437 * property data isn't large enough.
1438 *
1439 * The out_value is modified only if a valid u64 value can be decoded.
1440 */
1441int of_property_read_u64(const struct device_node *np, const char *propname,
1442 u64 *out_value)
1443{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001444 const __be32 *val = of_find_property_value_of_size(np, propname,
Richard Fitzgerald79ac5d32016-09-12 14:01:28 +01001445 sizeof(*out_value),
1446 0,
1447 NULL);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001448
Tony Priskdaeec1f2013-04-03 17:57:11 +13001449 if (IS_ERR(val))
1450 return PTR_ERR(val);
1451
1452 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001453 return 0;
1454}
1455EXPORT_SYMBOL_GPL(of_property_read_u64);
1456
1457/**
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001458 * of_property_read_variable_u64_array - Find and read an array of 64 bit
1459 * integers from a property, with bounds on the minimum and maximum array size.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001460 *
1461 * @np: device node from which the property value is to be read.
1462 * @propname: name of the property to be searched.
1463 * @out_values: pointer to return value, modified only if return value is 0.
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001464 * @sz_min: minimum number of array elements to read
1465 * @sz_max: maximum number of array elements to read, if zero there is no
1466 * upper limit on the number of elements in the dts entry but only
1467 * sz_min will be read.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001468 *
1469 * Search for a property in a device node and read 64-bit value(s) from
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001470 * it. Returns number of elements read on success, -EINVAL if the property
1471 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1472 * if the property data is smaller than sz_min or longer than sz_max.
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001473 *
1474 * The out_values is modified only if a valid u64 value can be decoded.
1475 */
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001476int of_property_read_variable_u64_array(const struct device_node *np,
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001477 const char *propname, u64 *out_values,
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001478 size_t sz_min, size_t sz_max)
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001479{
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001480 size_t sz, count;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001481 const __be32 *val = of_find_property_value_of_size(np, propname,
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001482 (sz_min * sizeof(*out_values)),
1483 (sz_max * sizeof(*out_values)),
1484 &sz);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001485
1486 if (IS_ERR(val))
1487 return PTR_ERR(val);
1488
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001489 if (!sz_max)
1490 sz = sz_min;
1491 else
1492 sz /= sizeof(*out_values);
1493
1494 count = sz;
1495 while (count--) {
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001496 *out_values++ = of_read_number(val, 2);
1497 val += 2;
1498 }
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001499
1500 return sz;
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001501}
Richard Fitzgeralda67e9472016-09-12 14:01:29 +01001502EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001503
1504/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301505 * of_property_read_string - Find and read a string from a property
1506 * @np: device node from which the property value is to be read.
1507 * @propname: name of the property to be searched.
1508 * @out_string: pointer to null terminated return string, modified only if
1509 * return value is 0.
1510 *
1511 * Search for a property in a device tree node and retrieve a null
1512 * terminated string value (pointer to data, not a copy). Returns 0 on
1513 * success, -EINVAL if the property does not exist, -ENODATA if property
1514 * does not have a value, and -EILSEQ if the string is not null-terminated
1515 * within the length of the property data.
1516 *
1517 * The out_string pointer is modified only if a valid string can be decoded.
1518 */
David Rivshinfe99c702016-03-02 16:35:51 -05001519int of_property_read_string(const struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001520 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301521{
David Rivshinfe99c702016-03-02 16:35:51 -05001522 const struct property *prop = of_find_property(np, propname, NULL);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301523 if (!prop)
1524 return -EINVAL;
1525 if (!prop->value)
1526 return -ENODATA;
1527 if (strnlen(prop->value, prop->length) >= prop->length)
1528 return -EILSEQ;
1529 *out_string = prop->value;
1530 return 0;
1531}
1532EXPORT_SYMBOL_GPL(of_property_read_string);
1533
1534/**
Grant Likely7aff0fe2011-12-12 09:25:58 -07001535 * of_property_match_string() - Find string in a list and return index
1536 * @np: pointer to node containing string list property
1537 * @propname: string list property name
1538 * @string: pointer to string to search for in string list
1539 *
1540 * This function searches a string list property and returns the index
1541 * of a specific string value.
1542 */
David Rivshinfe99c702016-03-02 16:35:51 -05001543int of_property_match_string(const struct device_node *np, const char *propname,
Grant Likely7aff0fe2011-12-12 09:25:58 -07001544 const char *string)
1545{
David Rivshinfe99c702016-03-02 16:35:51 -05001546 const struct property *prop = of_find_property(np, propname, NULL);
Grant Likely7aff0fe2011-12-12 09:25:58 -07001547 size_t l;
1548 int i;
1549 const char *p, *end;
1550
1551 if (!prop)
1552 return -EINVAL;
1553 if (!prop->value)
1554 return -ENODATA;
1555
1556 p = prop->value;
1557 end = p + prop->length;
1558
1559 for (i = 0; p < end; i++, p += l) {
Grant Likelya87fa1d2014-11-03 15:15:35 +00001560 l = strnlen(p, end - p) + 1;
Grant Likely7aff0fe2011-12-12 09:25:58 -07001561 if (p + l > end)
1562 return -EILSEQ;
1563 pr_debug("comparing %s with %s\n", string, p);
1564 if (strcmp(string, p) == 0)
1565 return i; /* Found it; return index */
1566 }
1567 return -ENODATA;
1568}
1569EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001570
1571/**
Jiri Slabye99010e2014-11-21 13:23:09 +01001572 * of_property_read_string_helper() - Utility helper for parsing string properties
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001573 * @np: device node from which the property value is to be read.
1574 * @propname: name of the property to be searched.
Grant Likelya87fa1d2014-11-03 15:15:35 +00001575 * @out_strs: output array of string pointers.
1576 * @sz: number of array elements to read.
1577 * @skip: Number of strings to skip over at beginning of list.
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001578 *
Grant Likelya87fa1d2014-11-03 15:15:35 +00001579 * Don't call this function directly. It is a utility helper for the
1580 * of_property_read_string*() family of functions.
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001581 */
David Rivshinfe99c702016-03-02 16:35:51 -05001582int of_property_read_string_helper(const struct device_node *np,
1583 const char *propname, const char **out_strs,
1584 size_t sz, int skip)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001585{
David Rivshinfe99c702016-03-02 16:35:51 -05001586 const struct property *prop = of_find_property(np, propname, NULL);
Grant Likelya87fa1d2014-11-03 15:15:35 +00001587 int l = 0, i = 0;
1588 const char *p, *end;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001589
1590 if (!prop)
1591 return -EINVAL;
1592 if (!prop->value)
1593 return -ENODATA;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001594 p = prop->value;
Grant Likelya87fa1d2014-11-03 15:15:35 +00001595 end = p + prop->length;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001596
Grant Likelya87fa1d2014-11-03 15:15:35 +00001597 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1598 l = strnlen(p, end - p) + 1;
1599 if (p + l > end)
1600 return -EILSEQ;
1601 if (out_strs && i >= skip)
1602 *out_strs++ = p;
1603 }
1604 i -= skip;
1605 return i <= 0 ? -ENODATA : i;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001606}
Grant Likelya87fa1d2014-11-03 15:15:35 +00001607EXPORT_SYMBOL_GPL(of_property_read_string_helper);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001608
Grant Likely624cfca2013-10-11 22:05:10 +01001609void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1610{
1611 int i;
1612 printk("%s %s", msg, of_node_full_name(args->np));
1613 for (i = 0; i < args->args_count; i++)
1614 printk(i ? ",%08x" : ":%08x", args->args[i]);
1615 printk("\n");
1616}
1617
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001618int of_phandle_iterator_init(struct of_phandle_iterator *it,
1619 const struct device_node *np,
1620 const char *list_name,
1621 const char *cells_name,
1622 int cell_count)
1623{
1624 const __be32 *list;
1625 int size;
1626
1627 memset(it, 0, sizeof(*it));
1628
1629 list = of_get_property(np, list_name, &size);
1630 if (!list)
1631 return -ENOENT;
1632
1633 it->cells_name = cells_name;
1634 it->cell_count = cell_count;
1635 it->parent = np;
1636 it->list_end = list + size / sizeof(*list);
1637 it->phandle_end = list;
1638 it->cur = list;
1639
1640 return 0;
1641}
1642
Joerg Roedelcd209b42016-04-04 17:49:18 +02001643int of_phandle_iterator_next(struct of_phandle_iterator *it)
1644{
1645 uint32_t count = 0;
1646
1647 if (it->node) {
1648 of_node_put(it->node);
1649 it->node = NULL;
1650 }
1651
1652 if (!it->cur || it->phandle_end >= it->list_end)
1653 return -ENOENT;
1654
1655 it->cur = it->phandle_end;
1656
1657 /* If phandle is 0, then it is an empty entry with no arguments. */
1658 it->phandle = be32_to_cpup(it->cur++);
1659
1660 if (it->phandle) {
1661
1662 /*
1663 * Find the provider node and parse the #*-cells property to
1664 * determine the argument length.
1665 */
1666 it->node = of_find_node_by_phandle(it->phandle);
1667
1668 if (it->cells_name) {
1669 if (!it->node) {
1670 pr_err("%s: could not find phandle\n",
1671 it->parent->full_name);
1672 goto err;
1673 }
1674
1675 if (of_property_read_u32(it->node, it->cells_name,
1676 &count)) {
1677 pr_err("%s: could not get %s for %s\n",
1678 it->parent->full_name,
1679 it->cells_name,
1680 it->node->full_name);
1681 goto err;
1682 }
1683 } else {
1684 count = it->cell_count;
1685 }
1686
1687 /*
1688 * Make sure that the arguments actually fit in the remaining
1689 * property data length
1690 */
1691 if (it->cur + count > it->list_end) {
1692 pr_err("%s: arguments longer than property\n",
1693 it->parent->full_name);
1694 goto err;
1695 }
1696 }
1697
1698 it->phandle_end = it->cur + count;
1699 it->cur_count = count;
1700
1701 return 0;
1702
1703err:
1704 if (it->node) {
1705 of_node_put(it->node);
1706 it->node = NULL;
1707 }
1708
1709 return -EINVAL;
1710}
1711
Joerg Roedelabdaa772016-04-04 17:49:21 +02001712int of_phandle_iterator_args(struct of_phandle_iterator *it,
1713 uint32_t *args,
1714 int size)
1715{
1716 int i, count;
1717
1718 count = it->cur_count;
1719
1720 if (WARN_ON(size < count))
1721 count = size;
1722
1723 for (i = 0; i < count; i++)
1724 args[i] = be32_to_cpup(it->cur++);
1725
1726 return count;
1727}
1728
Grant Likelybd69f732013-02-10 22:57:21 +00001729static int __of_parse_phandle_with_args(const struct device_node *np,
1730 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001731 const char *cells_name,
1732 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001733 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001734{
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001735 struct of_phandle_iterator it;
1736 int rc, cur_index = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001737
Grant Likely15c9a0a2011-12-12 09:25:57 -07001738 /* Loop over the phandles until all the requested entry is found */
Joerg Roedelf623ce92016-04-04 17:49:20 +02001739 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001740 /*
Joerg Roedelcd209b42016-04-04 17:49:18 +02001741 * All of the error cases bail out of the loop, so at
Grant Likely15c9a0a2011-12-12 09:25:57 -07001742 * this point, the parsing is successful. If the requested
1743 * index matches, then fill the out_args structure and return,
1744 * or return -ENOENT for an empty entry.
1745 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001746 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001747 if (cur_index == index) {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001748 if (!it.phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001749 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001750
Grant Likely15c9a0a2011-12-12 09:25:57 -07001751 if (out_args) {
Joerg Roedelabdaa772016-04-04 17:49:21 +02001752 int c;
1753
1754 c = of_phandle_iterator_args(&it,
1755 out_args->args,
1756 MAX_PHANDLE_ARGS);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001757 out_args->np = it.node;
Joerg Roedelabdaa772016-04-04 17:49:21 +02001758 out_args->args_count = c;
Tang Yuantianb855f162013-04-10 11:36:39 +08001759 } else {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001760 of_node_put(it.node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001761 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001762
1763 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001764 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001765 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001766
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001767 cur_index++;
1768 }
1769
Grant Likely23ce04c2013-02-12 21:21:49 +00001770 /*
1771 * Unlock node before returning result; will be one of:
1772 * -ENOENT : index is for empty phandle
1773 * -EINVAL : parsing error on data
1774 */
Joerg Roedelcd209b42016-04-04 17:49:18 +02001775
Grant Likely23ce04c2013-02-12 21:21:49 +00001776 err:
Markus Elfringbeab47d2016-07-19 22:42:22 +02001777 of_node_put(it.node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001778 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001779}
Grant Likelybd69f732013-02-10 22:57:21 +00001780
Stephen Warreneded9dd2013-08-14 15:27:08 -06001781/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001782 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1783 * @np: Pointer to device node holding phandle property
1784 * @phandle_name: Name of property holding a phandle value
1785 * @index: For properties holding a table of phandles, this is the index into
1786 * the table
1787 *
1788 * Returns the device_node pointer with refcount incremented. Use
1789 * of_node_put() on it when done.
1790 */
1791struct device_node *of_parse_phandle(const struct device_node *np,
1792 const char *phandle_name, int index)
1793{
Stephen Warren91d99422013-08-14 15:27:11 -06001794 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001795
Stephen Warren91d99422013-08-14 15:27:11 -06001796 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001797 return NULL;
1798
Stephen Warren91d99422013-08-14 15:27:11 -06001799 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1800 index, &args))
1801 return NULL;
1802
1803 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001804}
1805EXPORT_SYMBOL(of_parse_phandle);
1806
1807/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001808 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1809 * @np: pointer to a device tree node containing a list
1810 * @list_name: property name that contains a list
1811 * @cells_name: property name that specifies phandles' arguments count
1812 * @index: index of a phandle to parse out
1813 * @out_args: optional pointer to output arguments structure (will be filled)
1814 *
1815 * This function is useful to parse lists of phandles and their arguments.
1816 * Returns 0 on success and fills out_args, on error returns appropriate
1817 * errno value.
1818 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001819 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warreneded9dd2013-08-14 15:27:08 -06001820 * pointer.
1821 *
1822 * Example:
1823 *
1824 * phandle1: node1 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001825 * #list-cells = <2>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001826 * }
1827 *
1828 * phandle2: node2 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001829 * #list-cells = <1>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001830 * }
1831 *
1832 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001833 * list = <&phandle1 1 2 &phandle2 3>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001834 * }
1835 *
1836 * To get a device_node of the `node2' node you may call this:
1837 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1838 */
Grant Likelybd69f732013-02-10 22:57:21 +00001839int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1840 const char *cells_name, int index,
1841 struct of_phandle_args *out_args)
1842{
1843 if (index < 0)
1844 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001845 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1846 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001847}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001848EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001849
Grant Likelybd69f732013-02-10 22:57:21 +00001850/**
Stephen Warren035fd942013-08-14 15:27:10 -06001851 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1852 * @np: pointer to a device tree node containing a list
1853 * @list_name: property name that contains a list
1854 * @cell_count: number of argument cells following the phandle
1855 * @index: index of a phandle to parse out
1856 * @out_args: optional pointer to output arguments structure (will be filled)
1857 *
1858 * This function is useful to parse lists of phandles and their arguments.
1859 * Returns 0 on success and fills out_args, on error returns appropriate
1860 * errno value.
1861 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001862 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warren035fd942013-08-14 15:27:10 -06001863 * pointer.
1864 *
1865 * Example:
1866 *
1867 * phandle1: node1 {
1868 * }
1869 *
1870 * phandle2: node2 {
1871 * }
1872 *
1873 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001874 * list = <&phandle1 0 2 &phandle2 2 3>;
Stephen Warren035fd942013-08-14 15:27:10 -06001875 * }
1876 *
1877 * To get a device_node of the `node2' node you may call this:
1878 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1879 */
1880int of_parse_phandle_with_fixed_args(const struct device_node *np,
1881 const char *list_name, int cell_count,
1882 int index, struct of_phandle_args *out_args)
1883{
1884 if (index < 0)
1885 return -EINVAL;
1886 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1887 index, out_args);
1888}
1889EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1890
1891/**
Grant Likelybd69f732013-02-10 22:57:21 +00001892 * of_count_phandle_with_args() - Find the number of phandles references in a property
1893 * @np: pointer to a device tree node containing a list
1894 * @list_name: property name that contains a list
1895 * @cells_name: property name that specifies phandles' arguments count
1896 *
1897 * Returns the number of phandle + argument tuples within a property. It
1898 * is a typical pattern to encode a list of phandle and variable
1899 * arguments into a single property. The number of arguments is encoded
1900 * by a property in the phandle-target node. For example, a gpios
1901 * property would contain a list of GPIO specifies consisting of a
1902 * phandle and 1 or more arguments. The number of arguments are
1903 * determined by the #gpio-cells property in the node pointed to by the
1904 * phandle.
1905 */
1906int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1907 const char *cells_name)
1908{
Joerg Roedel2021bd02016-04-04 17:49:19 +02001909 struct of_phandle_iterator it;
1910 int rc, cur_index = 0;
1911
1912 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1913 if (rc)
1914 return rc;
1915
1916 while ((rc = of_phandle_iterator_next(&it)) == 0)
1917 cur_index += 1;
1918
1919 if (rc != -ENOENT)
1920 return rc;
1921
1922 return cur_index;
Grant Likelybd69f732013-02-10 22:57:21 +00001923}
1924EXPORT_SYMBOL(of_count_phandle_with_args);
1925
Grant Likely02af11b2009-11-23 20:16:45 -07001926/**
Xiubo Li62664f62014-01-22 13:57:39 +08001927 * __of_add_property - Add a property to a node without lock operations
1928 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001929int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001930{
1931 struct property **next;
1932
1933 prop->next = NULL;
1934 next = &np->properties;
1935 while (*next) {
1936 if (strcmp(prop->name, (*next)->name) == 0)
1937 /* duplicate ! don't insert it */
1938 return -EEXIST;
1939
1940 next = &(*next)->next;
1941 }
1942 *next = prop;
1943
1944 return 0;
1945}
1946
1947/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001948 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001949 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001950int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001951{
Grant Likely02af11b2009-11-23 20:16:45 -07001952 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001953 int rc;
1954
Grant Likely8a2b22a2014-07-23 17:05:06 -06001955 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001956
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001957 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001958 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001959 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001960
Grant Likely8a2b22a2014-07-23 17:05:06 -06001961 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001962 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001963
Grant Likely8a2b22a2014-07-23 17:05:06 -06001964 mutex_unlock(&of_mutex);
1965
Grant Likely259092a2014-07-16 12:48:23 -06001966 if (!rc)
1967 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1968
Xiubo Li62664f62014-01-22 13:57:39 +08001969 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001970}
1971
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001972int __of_remove_property(struct device_node *np, struct property *prop)
1973{
1974 struct property **next;
1975
1976 for (next = &np->properties; *next; next = &(*next)->next) {
1977 if (*next == prop)
1978 break;
1979 }
1980 if (*next == NULL)
1981 return -ENODEV;
1982
1983 /* found the node */
1984 *next = prop->next;
1985 prop->next = np->deadprops;
1986 np->deadprops = prop;
1987
1988 return 0;
1989}
1990
Frank Rowandd9fc8802016-06-16 10:51:46 -07001991void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
1992{
1993 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1994 kfree(prop->attr.attr.name);
1995}
1996
Grant Likely8a2b22a2014-07-23 17:05:06 -06001997void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1998{
Gaurav Minochaef69d742014-09-05 09:56:13 -07001999 if (!IS_ENABLED(CONFIG_SYSFS))
2000 return;
2001
Grant Likely8a2b22a2014-07-23 17:05:06 -06002002 /* at early boot, bail here and defer setup to of_init() */
2003 if (of_kset && of_node_is_attached(np))
Frank Rowandd9fc8802016-06-16 10:51:46 -07002004 __of_sysfs_remove_bin_file(np, prop);
Grant Likely8a2b22a2014-07-23 17:05:06 -06002005}
2006
Grant Likely02af11b2009-11-23 20:16:45 -07002007/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00002008 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07002009 *
2010 * Note that we don't actually remove it, since we have given out
2011 * who-knows-how-many pointers to the data using get-property.
2012 * Instead we just move the property to the "dead properties"
2013 * list, so it won't be found any more.
2014 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00002015int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07002016{
Grant Likely02af11b2009-11-23 20:16:45 -07002017 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00002018 int rc;
2019
Suraj Jitindar Singh201b3fe2016-04-28 15:34:54 +10002020 if (!prop)
2021 return -ENODEV;
2022
Grant Likely8a2b22a2014-07-23 17:05:06 -06002023 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07002024
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05002025 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03002026 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05002027 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07002028
Grant Likely8a2b22a2014-07-23 17:05:06 -06002029 if (!rc)
2030 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07002031
Grant Likely8a2b22a2014-07-23 17:05:06 -06002032 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00002033
Grant Likely259092a2014-07-16 12:48:23 -06002034 if (!rc)
2035 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
2036
Grant Likely8a2b22a2014-07-23 17:05:06 -06002037 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07002038}
2039
Pantelis Antonioud8c50082014-07-04 19:58:46 +03002040int __of_update_property(struct device_node *np, struct property *newprop,
2041 struct property **oldpropp)
2042{
2043 struct property **next, *oldprop;
2044
2045 for (next = &np->properties; *next; next = &(*next)->next) {
2046 if (of_prop_cmp((*next)->name, newprop->name) == 0)
2047 break;
2048 }
2049 *oldpropp = oldprop = *next;
2050
2051 if (oldprop) {
2052 /* replace the node */
2053 newprop->next = oldprop->next;
2054 *next = newprop;
2055 oldprop->next = np->deadprops;
2056 np->deadprops = oldprop;
2057 } else {
2058 /* new node */
2059 newprop->next = NULL;
2060 *next = newprop;
2061 }
Grant Likely02af11b2009-11-23 20:16:45 -07002062
2063 return 0;
2064}
2065
Grant Likely8a2b22a2014-07-23 17:05:06 -06002066void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
2067 struct property *oldprop)
2068{
Gaurav Minochaef69d742014-09-05 09:56:13 -07002069 if (!IS_ENABLED(CONFIG_SYSFS))
2070 return;
2071
Grant Likely8a2b22a2014-07-23 17:05:06 -06002072 /* At early boot, bail out and defer setup to of_init() */
2073 if (!of_kset)
2074 return;
2075
2076 if (oldprop)
Frank Rowandd9fc8802016-06-16 10:51:46 -07002077 __of_sysfs_remove_bin_file(np, oldprop);
Grant Likely8a2b22a2014-07-23 17:05:06 -06002078 __of_add_property_sysfs(np, newprop);
2079}
2080
Grant Likely02af11b2009-11-23 20:16:45 -07002081/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00002082 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10002083 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07002084 *
2085 * Note that we don't actually remove it, since we have given out
2086 * who-knows-how-many pointers to the data using get-property.
2087 * Instead we just move the property to the "dead properties" list,
2088 * and add the new property to the property list
2089 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00002090int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07002091{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03002092 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07002093 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08002094 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00002095
Dong Aisheng475d0092012-07-11 15:16:37 +10002096 if (!newprop->name)
2097 return -EINVAL;
2098
Grant Likely8a2b22a2014-07-23 17:05:06 -06002099 mutex_lock(&of_mutex);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002100
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05002101 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03002102 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05002103 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00002104
Grant Likely8a2b22a2014-07-23 17:05:06 -06002105 if (!rc)
2106 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002107
Grant Likely8a2b22a2014-07-23 17:05:06 -06002108 mutex_unlock(&of_mutex);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00002109
Grant Likely259092a2014-07-16 12:48:23 -06002110 if (!rc)
2111 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002112
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00002113 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002114}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07002115
Shawn Guo611cad72011-08-15 15:28:14 +08002116static void of_alias_add(struct alias_prop *ap, struct device_node *np,
2117 int id, const char *stem, int stem_len)
2118{
2119 ap->np = np;
2120 ap->id = id;
2121 strncpy(ap->stem, stem, stem_len);
2122 ap->stem[stem_len] = 0;
2123 list_add_tail(&ap->link, &aliases_lookup);
2124 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06002125 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08002126}
2127
2128/**
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02002129 * of_alias_scan - Scan all properties of the 'aliases' node
Shawn Guo611cad72011-08-15 15:28:14 +08002130 *
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02002131 * The function scans all the properties of the 'aliases' node and populates
2132 * the global lookup table with the properties. It returns the
2133 * number of alias properties found, or an error code in case of failure.
Shawn Guo611cad72011-08-15 15:28:14 +08002134 *
2135 * @dt_alloc: An allocator that provides a virtual address to memory
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02002136 * for storing the resulting tree
Shawn Guo611cad72011-08-15 15:28:14 +08002137 */
2138void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
2139{
2140 struct property *pp;
2141
Laurentiu Tudor7dbe5842014-08-27 17:09:39 +03002142 of_aliases = of_find_node_by_path("/aliases");
Shawn Guo611cad72011-08-15 15:28:14 +08002143 of_chosen = of_find_node_by_path("/chosen");
2144 if (of_chosen == NULL)
2145 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02002146
2147 if (of_chosen) {
Grant Likelya752ee52014-03-28 08:12:18 -07002148 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
Grant Likely676e1b22014-03-27 17:11:23 -07002149 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
2150 if (!name)
2151 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Grant Likelya752ee52014-03-28 08:12:18 -07002152 if (IS_ENABLED(CONFIG_PPC) && !name)
2153 name = of_get_property(of_aliases, "stdout", NULL);
Peter Hurleyf64255b2015-03-17 16:46:33 -04002154 if (name)
Leif Lindholm7914a7c2014-11-27 17:56:07 +00002155 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002156 }
2157
Shawn Guo611cad72011-08-15 15:28:14 +08002158 if (!of_aliases)
2159 return;
2160
Dong Aisheng8af0da92011-12-22 20:19:24 +08002161 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08002162 const char *start = pp->name;
2163 const char *end = start + strlen(start);
2164 struct device_node *np;
2165 struct alias_prop *ap;
2166 int id, len;
2167
2168 /* Skip those we do not want to proceed */
2169 if (!strcmp(pp->name, "name") ||
2170 !strcmp(pp->name, "phandle") ||
2171 !strcmp(pp->name, "linux,phandle"))
2172 continue;
2173
2174 np = of_find_node_by_path(pp->value);
2175 if (!np)
2176 continue;
2177
2178 /* walk the alias backwards to extract the id and work out
2179 * the 'stem' string */
2180 while (isdigit(*(end-1)) && end > start)
2181 end--;
2182 len = end - start;
2183
2184 if (kstrtoint(end, 10, &id) < 0)
2185 continue;
2186
2187 /* Allocate an alias_prop with enough space for the stem */
2188 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
2189 if (!ap)
2190 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01002191 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08002192 ap->alias = start;
2193 of_alias_add(ap, np, id, start, len);
2194 }
2195}
2196
2197/**
2198 * of_alias_get_id - Get alias id for the given device_node
2199 * @np: Pointer to the given device_node
2200 * @stem: Alias stem of the given device_node
2201 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01002202 * The function travels the lookup table to get the alias id for the given
2203 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08002204 */
2205int of_alias_get_id(struct device_node *np, const char *stem)
2206{
2207 struct alias_prop *app;
2208 int id = -ENODEV;
2209
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03002210 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08002211 list_for_each_entry(app, &aliases_lookup, link) {
2212 if (strcmp(app->stem, stem) != 0)
2213 continue;
2214
2215 if (np == app->np) {
2216 id = app->id;
2217 break;
2218 }
2219 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03002220 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08002221
2222 return id;
2223}
2224EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06002225
Wolfram Sang351d2242015-03-12 17:17:58 +01002226/**
2227 * of_alias_get_highest_id - Get highest alias id for the given stem
2228 * @stem: Alias stem to be examined
2229 *
2230 * The function travels the lookup table to get the highest alias id for the
2231 * given alias stem. It returns the alias id if found.
2232 */
2233int of_alias_get_highest_id(const char *stem)
2234{
2235 struct alias_prop *app;
2236 int id = -ENODEV;
2237
2238 mutex_lock(&of_mutex);
2239 list_for_each_entry(app, &aliases_lookup, link) {
2240 if (strcmp(app->stem, stem) != 0)
2241 continue;
2242
2243 if (app->id > id)
2244 id = app->id;
2245 }
2246 mutex_unlock(&of_mutex);
2247
2248 return id;
2249}
2250EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
2251
Stephen Warrenc541adc2012-04-04 09:27:46 -06002252const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2253 u32 *pu)
2254{
2255 const void *curv = cur;
2256
2257 if (!prop)
2258 return NULL;
2259
2260 if (!cur) {
2261 curv = prop->value;
2262 goto out_val;
2263 }
2264
2265 curv += sizeof(*cur);
2266 if (curv >= prop->value + prop->length)
2267 return NULL;
2268
2269out_val:
2270 *pu = be32_to_cpup(curv);
2271 return curv;
2272}
2273EXPORT_SYMBOL_GPL(of_prop_next_u32);
2274
2275const char *of_prop_next_string(struct property *prop, const char *cur)
2276{
2277 const void *curv = cur;
2278
2279 if (!prop)
2280 return NULL;
2281
2282 if (!cur)
2283 return prop->value;
2284
2285 curv += strlen(cur) + 1;
2286 if (curv >= prop->value + prop->length)
2287 return NULL;
2288
2289 return curv;
2290}
2291EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002292
2293/**
Grant Likely3482f2c2014-03-27 17:18:55 -07002294 * of_console_check() - Test and setup console for DT setup
2295 * @dn - Pointer to device node
2296 * @name - Name to use for preferred console without index. ex. "ttyS"
2297 * @index - Index to use for preferred console.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002298 *
Grant Likely3482f2c2014-03-27 17:18:55 -07002299 * Check if the given device node matches the stdout-path property in the
2300 * /chosen node. If it does then register it as the preferred console and return
2301 * TRUE. Otherwise return FALSE.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002302 */
Grant Likely3482f2c2014-03-27 17:18:55 -07002303bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002304{
Grant Likely3482f2c2014-03-27 17:18:55 -07002305 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002306 return false;
Leif Lindholm7914a7c2014-11-27 17:56:07 +00002307 return !add_preferred_console(name, index,
2308 kstrdup(of_stdout_options, GFP_KERNEL));
Sascha Hauer5c19e952013-08-05 14:40:44 +02002309}
Grant Likely3482f2c2014-03-27 17:18:55 -07002310EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002311
2312/**
2313 * of_find_next_cache_node - Find a node's subsidiary cache
2314 * @np: node of type "cpu" or "cache"
2315 *
2316 * Returns a node pointer with refcount incremented, use
2317 * of_node_put() on it when done. Caller should hold a reference
2318 * to np.
2319 */
2320struct device_node *of_find_next_cache_node(const struct device_node *np)
2321{
2322 struct device_node *child;
2323 const phandle *handle;
2324
2325 handle = of_get_property(np, "l2-cache", NULL);
2326 if (!handle)
2327 handle = of_get_property(np, "next-level-cache", NULL);
2328
2329 if (handle)
2330 return of_find_node_by_phandle(be32_to_cpup(handle));
2331
2332 /* OF on pmac has nodes instead of properties named "l2-cache"
2333 * beneath CPU nodes.
2334 */
2335 if (!strcmp(np->type, "cpu"))
2336 for_each_child_of_node(np, child)
2337 if (!strcmp(child->type, "cache"))
2338 return child;
2339
2340 return NULL;
2341}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002342
2343/**
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002344 * of_graph_parse_endpoint() - parse common endpoint node properties
2345 * @node: pointer to endpoint device_node
2346 * @endpoint: pointer to the OF endpoint data structure
2347 *
2348 * The caller should hold a reference to @node.
2349 */
2350int of_graph_parse_endpoint(const struct device_node *node,
2351 struct of_endpoint *endpoint)
2352{
2353 struct device_node *port_node = of_get_parent(node);
2354
Philipp Zabeld4847002014-03-04 12:31:24 +01002355 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2356 __func__, node->full_name);
2357
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002358 memset(endpoint, 0, sizeof(*endpoint));
2359
2360 endpoint->local_node = node;
2361 /*
2362 * It doesn't matter whether the two calls below succeed.
2363 * If they don't then the default value 0 is used.
2364 */
2365 of_property_read_u32(port_node, "reg", &endpoint->port);
2366 of_property_read_u32(node, "reg", &endpoint->id);
2367
2368 of_node_put(port_node);
2369
2370 return 0;
2371}
2372EXPORT_SYMBOL(of_graph_parse_endpoint);
2373
2374/**
Philipp Zabelbfe446e2014-03-11 11:21:11 +01002375 * of_graph_get_port_by_id() - get the port matching a given id
2376 * @parent: pointer to the parent device node
2377 * @id: id of the port
2378 *
2379 * Return: A 'port' node pointer with refcount incremented. The caller
2380 * has to use of_node_put() on it when done.
2381 */
2382struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
2383{
2384 struct device_node *node, *port;
2385
2386 node = of_get_child_by_name(parent, "ports");
2387 if (node)
2388 parent = node;
2389
2390 for_each_child_of_node(parent, port) {
2391 u32 port_id = 0;
2392
2393 if (of_node_cmp(port->name, "port") != 0)
2394 continue;
2395 of_property_read_u32(port, "reg", &port_id);
2396 if (id == port_id)
2397 break;
2398 }
2399
2400 of_node_put(node);
2401
2402 return port;
2403}
2404EXPORT_SYMBOL(of_graph_get_port_by_id);
2405
2406/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002407 * of_graph_get_next_endpoint() - get next endpoint node
2408 * @parent: pointer to the parent device node
2409 * @prev: previous endpoint node, or NULL to get first
2410 *
2411 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
Philipp Zabelf033c0b2014-12-01 13:32:32 +01002412 * of the passed @prev node is decremented.
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002413 */
2414struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2415 struct device_node *prev)
2416{
2417 struct device_node *endpoint;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002418 struct device_node *port;
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002419
2420 if (!parent)
2421 return NULL;
2422
Linus Torvalds3c83e612014-04-04 09:50:07 -07002423 /*
2424 * Start by locating the port node. If no previous endpoint is specified
2425 * search for the first port node, otherwise get the previous endpoint
2426 * parent port node.
2427 */
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002428 if (!prev) {
2429 struct device_node *node;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002430
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002431 node = of_get_child_by_name(parent, "ports");
2432 if (node)
2433 parent = node;
2434
2435 port = of_get_child_by_name(parent, "port");
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002436 of_node_put(node);
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002437
Linus Torvalds3c83e612014-04-04 09:50:07 -07002438 if (!port) {
Rob Herring606ad422016-06-15 08:32:18 -05002439 pr_err("graph: no port node found in %s\n",
2440 parent->full_name);
Philipp Zabel4329b932014-02-26 20:43:42 +01002441 return NULL;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002442 }
2443 } else {
2444 port = of_get_parent(prev);
2445 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2446 __func__, prev->full_name))
2447 return NULL;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002448 }
Philipp Zabel4329b932014-02-26 20:43:42 +01002449
Linus Torvalds3c83e612014-04-04 09:50:07 -07002450 while (1) {
2451 /*
2452 * Now that we have a port node, get the next endpoint by
2453 * getting the next child. If the previous endpoint is NULL this
2454 * will return the first child.
2455 */
2456 endpoint = of_get_next_child(port, prev);
2457 if (endpoint) {
2458 of_node_put(port);
2459 return endpoint;
2460 }
2461
2462 /* No more endpoints under this port, try the next one. */
2463 prev = NULL;
2464
2465 do {
2466 port = of_get_next_child(parent, port);
2467 if (!port)
2468 return NULL;
2469 } while (of_node_cmp(port->name, "port"));
2470 }
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002471}
2472EXPORT_SYMBOL(of_graph_get_next_endpoint);
2473
2474/**
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002475 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
2476 * @parent: pointer to the parent device node
2477 * @port_reg: identifier (value of reg property) of the parent port node
2478 * @reg: identifier (value of reg property) of the endpoint node
2479 *
2480 * Return: An 'endpoint' node pointer which is identified by reg and at the same
2481 * is the child of a port node identified by port_reg. reg and port_reg are
2482 * ignored when they are -1.
2483 */
2484struct device_node *of_graph_get_endpoint_by_regs(
2485 const struct device_node *parent, int port_reg, int reg)
2486{
2487 struct of_endpoint endpoint;
Lucas Stach34276bb2016-08-15 14:58:43 +02002488 struct device_node *node = NULL;
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002489
Lucas Stach34276bb2016-08-15 14:58:43 +02002490 for_each_endpoint_of_node(parent, node) {
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002491 of_graph_parse_endpoint(node, &endpoint);
2492 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
2493 ((reg == -1) || (endpoint.id == reg)))
2494 return node;
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002495 }
2496
2497 return NULL;
2498}
Dave Airlie8ffaa902015-06-23 10:19:10 +10002499EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002500
2501/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002502 * of_graph_get_remote_port_parent() - get remote port's parent node
2503 * @node: pointer to a local endpoint device_node
2504 *
2505 * Return: Remote device node associated with remote endpoint node linked
2506 * to @node. Use of_node_put() on it when done.
2507 */
2508struct device_node *of_graph_get_remote_port_parent(
2509 const struct device_node *node)
2510{
2511 struct device_node *np;
2512 unsigned int depth;
2513
2514 /* Get remote endpoint node. */
2515 np = of_parse_phandle(node, "remote-endpoint", 0);
2516
2517 /* Walk 3 levels up only if there is 'ports' node. */
2518 for (depth = 3; depth && np; depth--) {
2519 np = of_get_next_parent(np);
2520 if (depth == 2 && of_node_cmp(np->name, "ports"))
2521 break;
2522 }
2523 return np;
2524}
2525EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2526
2527/**
2528 * of_graph_get_remote_port() - get remote port node
2529 * @node: pointer to a local endpoint device_node
2530 *
2531 * Return: Remote port node associated with remote endpoint node linked
2532 * to @node. Use of_node_put() on it when done.
2533 */
2534struct device_node *of_graph_get_remote_port(const struct device_node *node)
2535{
2536 struct device_node *np;
2537
2538 /* Get remote endpoint node. */
2539 np = of_parse_phandle(node, "remote-endpoint", 0);
2540 if (!np)
2541 return NULL;
2542 return of_get_next_parent(np);
2543}
2544EXPORT_SYMBOL(of_graph_get_remote_port);