blob: 260d33c0f26c9b290ec2c7f2d03237ebbad7da02 [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>
Sudeep Holla5fa23532017-01-16 10:40:43 +000028#include <linux/of_device.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010029#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100030#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000032#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070033#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100034
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080035#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080036
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080037LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080038
Grant Likely5063e252014-10-03 16:28:27 +010039struct device_node *of_root;
40EXPORT_SYMBOL(of_root);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070041struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080042struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -070043struct device_node *of_stdout;
Leif Lindholm7914a7c2014-11-27 17:56:07 +000044static const char *of_stdout_options;
Shawn Guo611cad72011-08-15 15:28:14 +080045
Grant Likely8a2b22a22014-07-23 17:05:06 -060046struct kset *of_kset;
Grant Likely75b57ec2014-02-20 18:02:11 +000047
48/*
Grant Likely8a2b22a22014-07-23 17:05:06 -060049 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
50 * This mutex must be held whenever modifications are being made to the
51 * device tree. The of_{attach,detach}_node() and
52 * of_{add,remove,update}_property() helpers make sure this happens.
Grant Likely75b57ec2014-02-20 18:02:11 +000053 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030054DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100055
Grant Likely5063e252014-10-03 16:28:27 +010056/* use when traversing tree through the child, sibling,
Stephen Rothwell581b6052007-04-24 16:46:53 +100057 * or parent members of struct device_node.
58 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050059DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100060
61int of_n_addr_cells(struct device_node *np)
62{
Sergei Shtylyov88329632017-07-23 19:55:47 +030063 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100064
65 do {
66 if (np->parent)
67 np = np->parent;
Sergei Shtylyov88329632017-07-23 19:55:47 +030068 if (!of_property_read_u32(np, "#address-cells", &cells))
69 return cells;
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{
Sergei Shtylyov88329632017-07-23 19:55:47 +030078 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100079
80 do {
81 if (np->parent)
82 np = np->parent;
Sergei Shtylyov88329632017-07-23 19:55:47 +030083 if (!of_property_read_u32(np, "#size-cells", &cells))
84 return cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100085 } while (np->parent);
86 /* No #size-cells property for the root node */
87 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
88}
89EXPORT_SYMBOL(of_n_size_cells);
90
Rob Herring0c3f0612013-09-17 10:42:50 -050091#ifdef CONFIG_NUMA
92int __weak of_node_to_nid(struct device_node *np)
93{
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +030094 return NUMA_NO_NODE;
Rob Herring0c3f0612013-09-17 10:42:50 -050095}
96#endif
97
Grant Likely6afc0dc2014-06-26 15:40:48 +010098#ifndef CONFIG_OF_DYNAMIC
Grant Likely75b57ec2014-02-20 18:02:11 +000099static void of_node_release(struct kobject *kobj)
100{
101 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
102}
Grant Likely0f22dd32012-02-15 20:38:40 -0700103#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700104
Grant Likely75b57ec2014-02-20 18:02:11 +0000105struct kobj_type of_node_ktype = {
106 .release = of_node_release,
107};
108
109static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
110 struct bin_attribute *bin_attr, char *buf,
111 loff_t offset, size_t count)
112{
113 struct property *pp = container_of(bin_attr, struct property, attr);
114 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
115}
116
Frank Rowandd9fc8802016-06-16 10:51:46 -0700117/* always return newly allocated name, caller must free after use */
Grant Likely75b57ec2014-02-20 18:02:11 +0000118static const char *safe_name(struct kobject *kobj, const char *orig_name)
119{
120 const char *name = orig_name;
121 struct kernfs_node *kn;
122 int i = 0;
123
124 /* don't be a hero. After 16 tries give up */
125 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
126 sysfs_put(kn);
127 if (name != orig_name)
128 kfree(name);
129 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
130 }
131
Frank Rowandd9fc8802016-06-16 10:51:46 -0700132 if (name == orig_name) {
133 name = kstrdup(orig_name, GFP_KERNEL);
134 } else {
Rob Herring606ad422016-06-15 08:32:18 -0500135 pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
Grant Likely75b57ec2014-02-20 18:02:11 +0000136 kobject_name(kobj), name);
Frank Rowandd9fc8802016-06-16 10:51:46 -0700137 }
Grant Likely75b57ec2014-02-20 18:02:11 +0000138 return name;
139}
140
Grant Likely8a2b22a22014-07-23 17:05:06 -0600141int __of_add_property_sysfs(struct device_node *np, struct property *pp)
Grant Likely75b57ec2014-02-20 18:02:11 +0000142{
143 int rc;
144
145 /* Important: Don't leak passwords */
146 bool secure = strncmp(pp->name, "security-", 9) == 0;
147
Gaurav Minochaef69d742014-09-05 09:56:13 -0700148 if (!IS_ENABLED(CONFIG_SYSFS))
149 return 0;
150
Grant Likely8a2b22a22014-07-23 17:05:06 -0600151 if (!of_kset || !of_node_is_attached(np))
152 return 0;
153
Grant Likely75b57ec2014-02-20 18:02:11 +0000154 sysfs_bin_attr_init(&pp->attr);
155 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
Frank Rowand7ace5f42017-06-21 12:20:03 -0700156 pp->attr.attr.mode = secure ? 0400 : 0444;
Grant Likely75b57ec2014-02-20 18:02:11 +0000157 pp->attr.size = secure ? 0 : pp->length;
158 pp->attr.read = of_node_property_read;
159
160 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
Rob Herring0d638a02017-06-01 15:50:55 -0500161 WARN(rc, "error adding attribute %s to node %pOF\n", pp->name, np);
Grant Likely75b57ec2014-02-20 18:02:11 +0000162 return rc;
163}
164
Grant Likely8a2b22a22014-07-23 17:05:06 -0600165int __of_attach_node_sysfs(struct device_node *np)
Grant Likely75b57ec2014-02-20 18:02:11 +0000166{
167 const char *name;
Frank Rowandd9fc8802016-06-16 10:51:46 -0700168 struct kobject *parent;
Grant Likely75b57ec2014-02-20 18:02:11 +0000169 struct property *pp;
170 int rc;
171
Gaurav Minochaef69d742014-09-05 09:56:13 -0700172 if (!IS_ENABLED(CONFIG_SYSFS))
173 return 0;
174
Grant Likely8a2b22a22014-07-23 17:05:06 -0600175 if (!of_kset)
176 return 0;
177
Grant Likely75b57ec2014-02-20 18:02:11 +0000178 np->kobj.kset = of_kset;
179 if (!np->parent) {
180 /* Nodes without parents are new top level trees */
Frank Rowandd9fc8802016-06-16 10:51:46 -0700181 name = safe_name(&of_kset->kobj, "base");
182 parent = NULL;
Grant Likely75b57ec2014-02-20 18:02:11 +0000183 } else {
184 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
Frank Rowandd9fc8802016-06-16 10:51:46 -0700185 parent = &np->parent->kobj;
Grant Likely75b57ec2014-02-20 18:02:11 +0000186 }
Frank Rowandd9fc8802016-06-16 10:51:46 -0700187 if (!name)
188 return -ENOMEM;
189 rc = kobject_add(&np->kobj, parent, "%s", name);
190 kfree(name);
Grant Likely75b57ec2014-02-20 18:02:11 +0000191 if (rc)
192 return rc;
193
194 for_each_property_of_node(np, pp)
195 __of_add_property_sysfs(np, pp);
196
197 return 0;
198}
199
Sudeep Holla194ec932015-05-14 15:28:24 +0100200void __init of_core_init(void)
Grant Likely75b57ec2014-02-20 18:02:11 +0000201{
202 struct device_node *np;
203
204 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300205 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000206 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
207 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300208 mutex_unlock(&of_mutex);
Rob Herring606ad422016-06-15 08:32:18 -0500209 pr_err("failed to register existing nodes\n");
Sudeep Holla194ec932015-05-14 15:28:24 +0100210 return;
Grant Likely75b57ec2014-02-20 18:02:11 +0000211 }
212 for_each_of_allnodes(np)
Grant Likely8a2b22a22014-07-23 17:05:06 -0600213 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300214 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000215
Grant Likely83570412012-11-06 21:03:27 +0000216 /* Symlink in /proc as required by userspace ABI */
Grant Likely5063e252014-10-03 16:28:27 +0100217 if (of_root)
Grant Likely75b57ec2014-02-20 18:02:11 +0000218 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000219}
Grant Likely75b57ec2014-02-20 18:02:11 +0000220
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500221static struct property *__of_find_property(const struct device_node *np,
222 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000223{
224 struct property *pp;
225
Timur Tabi64e45662008-05-08 05:19:59 +1000226 if (!np)
227 return NULL;
228
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530229 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000230 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530231 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000232 *lenp = pp->length;
233 break;
234 }
235 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500236
237 return pp;
238}
239
240struct property *of_find_property(const struct device_node *np,
241 const char *name,
242 int *lenp)
243{
244 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500245 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500246
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500247 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500248 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500249 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000250
251 return pp;
252}
253EXPORT_SYMBOL(of_find_property);
254
Grant Likely5063e252014-10-03 16:28:27 +0100255struct device_node *__of_find_all_nodes(struct device_node *prev)
256{
257 struct device_node *np;
258 if (!prev) {
259 np = of_root;
260 } else if (prev->child) {
261 np = prev->child;
262 } else {
263 /* Walk back up looking for a sibling, or the end of the structure */
264 np = prev;
265 while (np->parent && !np->sibling)
266 np = np->parent;
267 np = np->sibling; /* Might be null at the end of the tree */
268 }
269 return np;
270}
271
Grant Likelye91edcf2009-10-15 10:58:09 -0600272/**
273 * of_find_all_nodes - Get next node in global list
274 * @prev: Previous node or NULL to start iteration
275 * of_node_put() will be called on it
276 *
277 * Returns a node pointer with refcount incremented, use
278 * of_node_put() on it when done.
279 */
280struct device_node *of_find_all_nodes(struct device_node *prev)
281{
282 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000283 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600284
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000285 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100286 np = __of_find_all_nodes(prev);
287 of_node_get(np);
Grant Likelye91edcf2009-10-15 10:58:09 -0600288 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000289 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600290 return np;
291}
292EXPORT_SYMBOL(of_find_all_nodes);
293
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000294/*
295 * Find a property with a given name for a given node
296 * and return the value.
297 */
Grant Likelya25095d2014-07-15 23:25:43 -0600298const void *__of_get_property(const struct device_node *np,
299 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500300{
301 struct property *pp = __of_find_property(np, name, lenp);
302
303 return pp ? pp->value : NULL;
304}
305
306/*
307 * Find a property with a given name for a given node
308 * and return the value.
309 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000310const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500311 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000312{
313 struct property *pp = of_find_property(np, name, lenp);
314
315 return pp ? pp->value : NULL;
316}
317EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000318
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100319/*
320 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
321 *
322 * @cpu: logical cpu index of a core/thread
323 * @phys_id: physical identifier of a core/thread
324 *
325 * CPU logical to physical index mapping is architecture specific.
326 * However this __weak function provides a default match of physical
327 * id to logical cpu index. phys_id provided here is usually values read
328 * from the device tree which must match the hardware internal registers.
329 *
330 * Returns true if the physical identifier and the logical cpu index
331 * correspond to the same core/thread, false otherwise.
332 */
333bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
334{
335 return (u32)phys_id == cpu;
336}
337
338/**
339 * Checks if the given "prop_name" property holds the physical id of the
340 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
341 * NULL, local thread number within the core is returned in it.
342 */
343static bool __of_find_n_match_cpu_property(struct device_node *cpun,
344 const char *prop_name, int cpu, unsigned int *thread)
345{
346 const __be32 *cell;
347 int ac, prop_len, tid;
348 u64 hwid;
349
350 ac = of_n_addr_cells(cpun);
351 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100352 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100353 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100354 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100355 for (tid = 0; tid < prop_len; tid++) {
356 hwid = of_read_number(cell, ac);
357 if (arch_match_cpu_phys_id(cpu, hwid)) {
358 if (thread)
359 *thread = tid;
360 return true;
361 }
362 cell += ac;
363 }
364 return false;
365}
366
David Millerd1cb9d12013-10-03 17:24:51 -0400367/*
368 * arch_find_n_match_cpu_physical_id - See if the given device node is
369 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
370 * else false. If 'thread' is non-NULL, the local thread number within the
371 * core is returned in it.
372 */
373bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
374 int cpu, unsigned int *thread)
375{
376 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
377 * for thread ids on PowerPC. If it doesn't exist fallback to
378 * standard "reg" property.
379 */
380 if (IS_ENABLED(CONFIG_PPC) &&
381 __of_find_n_match_cpu_property(cpun,
382 "ibm,ppc-interrupt-server#s",
383 cpu, thread))
384 return true;
385
Masahiro Yamada510bd062015-10-28 12:05:27 +0900386 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
David Millerd1cb9d12013-10-03 17:24:51 -0400387}
388
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100389/**
390 * of_get_cpu_node - Get device node associated with the given logical CPU
391 *
392 * @cpu: CPU number(logical index) for which device node is required
393 * @thread: if not NULL, local thread number within the physical core is
394 * returned
395 *
396 * The main purpose of this function is to retrieve the device node for the
397 * given logical CPU index. It should be used to initialize the of_node in
398 * cpu device. Once of_node in cpu device is populated, all the further
399 * references can use that instead.
400 *
401 * CPU logical to physical index mapping is architecture specific and is built
402 * before booting secondary cores. This function uses arch_match_cpu_phys_id
403 * which can be overridden by architecture specific implementation.
404 *
Masahiro Yamada1c986e32016-04-20 10:18:46 +0900405 * Returns a node pointer for the logical cpu with refcount incremented, use
406 * of_node_put() on it when done. Returns NULL if not found.
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100407 */
408struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
409{
David Millerd1cb9d12013-10-03 17:24:51 -0400410 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100411
David Millerd1cb9d12013-10-03 17:24:51 -0400412 for_each_node_by_type(cpun, "cpu") {
413 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100414 return cpun;
415 }
416 return NULL;
417}
418EXPORT_SYMBOL(of_get_cpu_node);
419
Kevin Hao215a14c2014-02-19 16:15:45 +0800420/**
421 * __of_device_is_compatible() - Check if the node matches given constraints
422 * @device: pointer to node
423 * @compat: required compatible string, NULL or "" for any match
424 * @type: required device_type value, NULL or "" for any match
425 * @name: required node name, NULL or "" for any match
426 *
427 * Checks if the given @compat, @type and @name strings match the
428 * properties of the given @device. A constraints can be skipped by
429 * passing NULL or an empty string as the constraint.
430 *
431 * Returns 0 for no match, and a positive integer on match. The return
432 * value is a relative score with larger values indicating better
433 * matches. The score is weighted for the most specific compatible value
434 * to get the highest score. Matching type is next, followed by matching
435 * name. Practically speaking, this results in the following priority
436 * order for matches:
437 *
438 * 1. specific compatible && type && name
439 * 2. specific compatible && type
440 * 3. specific compatible && name
441 * 4. specific compatible
442 * 5. general compatible && type && name
443 * 6. general compatible && type
444 * 7. general compatible && name
445 * 8. general compatible
446 * 9. type && name
447 * 10. type
448 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000449 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500450static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800451 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000452{
Kevin Hao215a14c2014-02-19 16:15:45 +0800453 struct property *prop;
454 const char *cp;
455 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000456
Kevin Hao215a14c2014-02-19 16:15:45 +0800457 /* Compatible match has highest priority */
458 if (compat && compat[0]) {
459 prop = __of_find_property(device, "compatible", NULL);
460 for (cp = of_prop_next_string(prop, NULL); cp;
461 cp = of_prop_next_string(prop, cp), index++) {
462 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
463 score = INT_MAX/2 - (index << 2);
464 break;
465 }
466 }
467 if (!score)
468 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000469 }
470
Kevin Hao215a14c2014-02-19 16:15:45 +0800471 /* Matching type is better than matching name */
472 if (type && type[0]) {
473 if (!device->type || of_node_cmp(type, device->type))
474 return 0;
475 score += 2;
476 }
477
478 /* Matching name is a bit better than not */
479 if (name && name[0]) {
480 if (!device->name || of_node_cmp(name, device->name))
481 return 0;
482 score++;
483 }
484
485 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000486}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500487
488/** Checks if the given "compat" string matches one of the strings in
489 * the device's "compatible" property
490 */
491int of_device_is_compatible(const struct device_node *device,
492 const char *compat)
493{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500494 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500495 int res;
496
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500497 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800498 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500499 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500500 return res;
501}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000502EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000503
Benjamin Herrenschmidtb9c13fe32016-07-08 08:35:59 +1000504/** Checks if the device is compatible with any of the entries in
505 * a NULL terminated array of strings. Returns the best match
506 * score or 0.
507 */
508int of_device_compatible_match(struct device_node *device,
509 const char *const *compat)
510{
511 unsigned int tmp, score = 0;
512
513 if (!compat)
514 return 0;
515
516 while (*compat) {
517 tmp = of_device_is_compatible(device, *compat);
518 if (tmp > score)
519 score = tmp;
520 compat++;
521 }
522
523 return score;
524}
525
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000526/**
Grant Likely71a157e2010-02-01 21:34:14 -0700527 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700528 * @compat: compatible string to look for in root node's compatible property.
529 *
Kevin Cernekee25c7a1d2014-11-12 12:54:00 -0800530 * Returns a positive integer if the root node has the given value in its
Grant Likely1f43cfb2010-01-28 13:47:25 -0700531 * compatible property.
532 */
Grant Likely71a157e2010-02-01 21:34:14 -0700533int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700534{
535 struct device_node *root;
536 int rc = 0;
537
538 root = of_find_node_by_path("/");
539 if (root) {
540 rc = of_device_is_compatible(root, compat);
541 of_node_put(root);
542 }
543 return rc;
544}
Grant Likely71a157e2010-02-01 21:34:14 -0700545EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700546
547/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700548 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100549 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700550 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100551 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800552 * Returns true if the status property is absent or set to "okay" or "ok",
553 * false otherwise
Josh Boyer834d97d2008-03-27 00:33:14 +1100554 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800555static bool __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100556{
557 const char *status;
558 int statlen;
559
Xiubo Li42ccd782014-01-13 11:07:28 +0800560 if (!device)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800561 return false;
Xiubo Li42ccd782014-01-13 11:07:28 +0800562
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700563 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100564 if (status == NULL)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800565 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100566
567 if (statlen > 0) {
568 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800569 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100570 }
571
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800572 return false;
Josh Boyer834d97d2008-03-27 00:33:14 +1100573}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700574
575/**
576 * of_device_is_available - check if a device is available for use
577 *
578 * @device: Node to check for availability
579 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800580 * Returns true if the status property is absent or set to "okay" or "ok",
581 * false otherwise
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700582 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800583bool of_device_is_available(const struct device_node *device)
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700584{
585 unsigned long flags;
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800586 bool res;
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700587
588 raw_spin_lock_irqsave(&devtree_lock, flags);
589 res = __of_device_is_available(device);
590 raw_spin_unlock_irqrestore(&devtree_lock, flags);
591 return res;
592
593}
Josh Boyer834d97d2008-03-27 00:33:14 +1100594EXPORT_SYMBOL(of_device_is_available);
595
596/**
Kevin Cernekee37786c72015-04-09 13:05:14 -0700597 * of_device_is_big_endian - check if a device has BE registers
598 *
599 * @device: Node to check for endianness
600 *
601 * Returns true if the device has a "big-endian" property, or if the kernel
602 * was compiled for BE *and* the device has a "native-endian" property.
603 * Returns false otherwise.
604 *
605 * Callers would nominally use ioread32be/iowrite32be if
606 * of_device_is_big_endian() == true, or readl/writel otherwise.
607 */
608bool of_device_is_big_endian(const struct device_node *device)
609{
610 if (of_property_read_bool(device, "big-endian"))
611 return true;
612 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
613 of_property_read_bool(device, "native-endian"))
614 return true;
615 return false;
616}
617EXPORT_SYMBOL(of_device_is_big_endian);
618
619/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000620 * of_get_parent - Get a node's parent if any
621 * @node: Node to get parent
622 *
623 * Returns a node pointer with refcount incremented, use
624 * of_node_put() on it when done.
625 */
626struct device_node *of_get_parent(const struct device_node *node)
627{
628 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500629 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000630
631 if (!node)
632 return NULL;
633
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500634 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000635 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500636 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000637 return np;
638}
639EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000640
641/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000642 * of_get_next_parent - Iterate to a node's parent
643 * @node: Node to get parent of
644 *
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +0200645 * This is like of_get_parent() except that it drops the
646 * refcount on the passed node, making it suitable for iterating
647 * through a node's parents.
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000648 *
649 * Returns a node pointer with refcount incremented, use
650 * of_node_put() on it when done.
651 */
652struct device_node *of_get_next_parent(struct device_node *node)
653{
654 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500655 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000656
657 if (!node)
658 return NULL;
659
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500660 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000661 parent = of_node_get(node->parent);
662 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500663 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000664 return parent;
665}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300666EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000667
Grant Likely0d0e02d2014-05-22 01:04:17 +0900668static struct device_node *__of_get_next_child(const struct device_node *node,
669 struct device_node *prev)
670{
671 struct device_node *next;
672
Florian Fainelli43cb4362014-05-28 10:39:02 -0700673 if (!node)
674 return NULL;
675
Grant Likely0d0e02d2014-05-22 01:04:17 +0900676 next = prev ? prev->sibling : node->child;
677 for (; next; next = next->sibling)
678 if (of_node_get(next))
679 break;
680 of_node_put(prev);
681 return next;
682}
683#define __for_each_child_of_node(parent, child) \
684 for (child = __of_get_next_child(parent, NULL); child != NULL; \
685 child = __of_get_next_child(parent, child))
686
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000687/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000688 * of_get_next_child - Iterate a node childs
689 * @node: parent node
690 * @prev: previous child of the parent node, or NULL to get first
691 *
Baruch Siach64808272015-03-19 14:03:41 +0200692 * Returns a node pointer with refcount incremented, use of_node_put() on
693 * it when done. Returns NULL when prev is the last child. Decrements the
694 * refcount of prev.
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000695 */
696struct device_node *of_get_next_child(const struct device_node *node,
697 struct device_node *prev)
698{
699 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500700 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000701
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500702 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900703 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500704 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000705 return next;
706}
707EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000708
709/**
Timur Tabi32961932012-08-14 13:20:23 +0000710 * of_get_next_available_child - Find the next available child node
711 * @node: parent node
712 * @prev: previous child of the parent node, or NULL to get first
713 *
714 * This function is like of_get_next_child(), except that it
715 * automatically skips any disabled nodes (i.e. status = "disabled").
716 */
717struct device_node *of_get_next_available_child(const struct device_node *node,
718 struct device_node *prev)
719{
720 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000721 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000722
Florian Fainelli43cb4362014-05-28 10:39:02 -0700723 if (!node)
724 return NULL;
725
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000726 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000727 next = prev ? prev->sibling : node->child;
728 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700729 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000730 continue;
731 if (of_node_get(next))
732 break;
733 }
734 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000735 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000736 return next;
737}
738EXPORT_SYMBOL(of_get_next_available_child);
739
740/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100741 * of_get_child_by_name - Find the child node by name for a given parent
742 * @node: parent node
743 * @name: child name to look for.
744 *
745 * This function looks for child node for given matching name
746 *
747 * Returns a node pointer if found, with refcount incremented, use
748 * of_node_put() on it when done.
749 * Returns NULL if node is not found.
750 */
751struct device_node *of_get_child_by_name(const struct device_node *node,
752 const char *name)
753{
754 struct device_node *child;
755
756 for_each_child_of_node(node, child)
757 if (child->name && (of_node_cmp(child->name, name) == 0))
758 break;
759 return child;
760}
761EXPORT_SYMBOL(of_get_child_by_name);
762
Grant Likelyc22e6502014-03-14 17:07:12 +0000763static struct device_node *__of_find_node_by_path(struct device_node *parent,
764 const char *path)
765{
766 struct device_node *child;
Leif Lindholm106937e2015-03-06 16:52:53 +0000767 int len;
Grant Likelyc22e6502014-03-14 17:07:12 +0000768
Brian Norris721a09e2015-03-17 12:30:31 -0700769 len = strcspn(path, "/:");
Grant Likelyc22e6502014-03-14 17:07:12 +0000770 if (!len)
771 return NULL;
772
773 __for_each_child_of_node(parent, child) {
Rob Herring95e6b1f2017-06-01 18:00:00 -0500774 const char *name = kbasename(child->full_name);
Grant Likelyc22e6502014-03-14 17:07:12 +0000775 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
776 return child;
777 }
778 return NULL;
779}
780
Rob Herring27497e12017-06-02 12:43:18 -0500781struct device_node *__of_find_node_by_full_path(struct device_node *node,
782 const char *path)
783{
784 const char *separator = strchr(path, ':');
785
786 while (node && *path == '/') {
787 struct device_node *tmp = node;
788
789 path++; /* Increment past '/' delimiter */
790 node = __of_find_node_by_path(node, path);
791 of_node_put(tmp);
792 path = strchrnul(path, '/');
793 if (separator && separator < path)
794 break;
795 }
796 return node;
797}
798
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100799/**
Leif Lindholm75c28c02014-11-28 11:34:28 +0000800 * of_find_node_opts_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000801 * @path: Either the full path to match, or if the path does not
802 * start with '/', the name of a property of the /aliases
803 * node (an alias). In the case of an alias, the node
804 * matching the alias' value will be returned.
Leif Lindholm75c28c02014-11-28 11:34:28 +0000805 * @opts: Address of a pointer into which to store the start of
806 * an options string appended to the end of the path with
807 * a ':' separator.
Grant Likelyc22e6502014-03-14 17:07:12 +0000808 *
809 * Valid paths:
810 * /foo/bar Full path
811 * foo Valid alias
812 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000813 *
814 * Returns a node pointer with refcount incremented, use
815 * of_node_put() on it when done.
816 */
Leif Lindholm75c28c02014-11-28 11:34:28 +0000817struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000818{
Grant Likelyc22e6502014-03-14 17:07:12 +0000819 struct device_node *np = NULL;
820 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500821 unsigned long flags;
Leif Lindholm75c28c02014-11-28 11:34:28 +0000822 const char *separator = strchr(path, ':');
823
824 if (opts)
825 *opts = separator ? separator + 1 : NULL;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000826
Grant Likelyc22e6502014-03-14 17:07:12 +0000827 if (strcmp(path, "/") == 0)
Grant Likely5063e252014-10-03 16:28:27 +0100828 return of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000829
830 /* The path could begin with an alias */
831 if (*path != '/') {
Leif Lindholm106937e2015-03-06 16:52:53 +0000832 int len;
833 const char *p = separator;
834
835 if (!p)
836 p = strchrnul(path, '/');
837 len = p - path;
Grant Likelyc22e6502014-03-14 17:07:12 +0000838
839 /* of_aliases must not be NULL */
840 if (!of_aliases)
841 return NULL;
842
843 for_each_property_of_node(of_aliases, pp) {
844 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
845 np = of_find_node_by_path(pp->value);
846 break;
847 }
848 }
849 if (!np)
850 return NULL;
851 path = p;
852 }
853
854 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500855 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000856 if (!np)
Grant Likely5063e252014-10-03 16:28:27 +0100857 np = of_node_get(of_root);
Rob Herring27497e12017-06-02 12:43:18 -0500858 np = __of_find_node_by_full_path(np, path);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500859 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000860 return np;
861}
Leif Lindholm75c28c02014-11-28 11:34:28 +0000862EXPORT_SYMBOL(of_find_node_opts_by_path);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000863
864/**
865 * of_find_node_by_name - Find a node by its "name" property
866 * @from: The node to start searching from or NULL, the node
867 * you pass will not be searched, only the next one
868 * will; typically, you pass what the previous call
869 * returned. of_node_put() will be called on it
870 * @name: The name string to match against
871 *
872 * Returns a node pointer with refcount incremented, use
873 * of_node_put() on it when done.
874 */
875struct device_node *of_find_node_by_name(struct device_node *from,
876 const char *name)
877{
878 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500879 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000880
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500881 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100882 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000883 if (np->name && (of_node_cmp(np->name, name) == 0)
884 && of_node_get(np))
885 break;
886 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500887 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000888 return np;
889}
890EXPORT_SYMBOL(of_find_node_by_name);
891
892/**
893 * of_find_node_by_type - Find a node by its "device_type" property
894 * @from: The node to start searching from, or NULL to start searching
895 * the entire device tree. The node you pass will not be
896 * searched, only the next one will; typically, you pass
897 * what the previous call returned. of_node_put() will be
898 * called on from for you.
899 * @type: The type string to match against
900 *
901 * Returns a node pointer with refcount incremented, use
902 * of_node_put() on it when done.
903 */
904struct device_node *of_find_node_by_type(struct device_node *from,
905 const char *type)
906{
907 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500908 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000909
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500910 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100911 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000912 if (np->type && (of_node_cmp(np->type, type) == 0)
913 && of_node_get(np))
914 break;
915 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500916 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000917 return np;
918}
919EXPORT_SYMBOL(of_find_node_by_type);
920
921/**
922 * of_find_compatible_node - Find a node based on type and one of the
923 * tokens in its "compatible" property
924 * @from: The node to start searching from or NULL, the node
925 * you pass will not be searched, only the next one
926 * will; typically, you pass what the previous call
927 * returned. of_node_put() will be called on it
928 * @type: The type string to match "device_type" or NULL to ignore
929 * @compatible: The string to match to one of the tokens in the device
930 * "compatible" list.
931 *
932 * Returns a node pointer with refcount incremented, use
933 * of_node_put() on it when done.
934 */
935struct device_node *of_find_compatible_node(struct device_node *from,
936 const char *type, const char *compatible)
937{
938 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500939 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000940
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500941 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100942 for_each_of_allnodes_from(from, np)
Kevin Hao215a14c2014-02-19 16:15:45 +0800943 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500944 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000945 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000946 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500947 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000948 return np;
949}
950EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100951
952/**
Michael Ellerman1e291b142008-11-12 18:54:42 +0000953 * of_find_node_with_property - Find a node which has a property with
954 * the given name.
955 * @from: The node to start searching from or NULL, the node
956 * you pass will not be searched, only the next one
957 * will; typically, you pass what the previous call
958 * returned. of_node_put() will be called on it
959 * @prop_name: The name of the property to look for.
960 *
961 * Returns a node pointer with refcount incremented, use
962 * of_node_put() on it when done.
963 */
964struct device_node *of_find_node_with_property(struct device_node *from,
965 const char *prop_name)
966{
967 struct device_node *np;
968 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500969 unsigned long flags;
Michael Ellerman1e291b142008-11-12 18:54:42 +0000970
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500971 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100972 for_each_of_allnodes_from(from, np) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530973 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b142008-11-12 18:54:42 +0000974 if (of_prop_cmp(pp->name, prop_name) == 0) {
975 of_node_get(np);
976 goto out;
977 }
978 }
979 }
980out:
981 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500982 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000983 return np;
984}
985EXPORT_SYMBOL(of_find_node_with_property);
986
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500987static
988const struct of_device_id *__of_match_node(const struct of_device_id *matches,
989 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100990{
Kevin Hao215a14c2014-02-19 16:15:45 +0800991 const struct of_device_id *best_match = NULL;
992 int score, best_score = 0;
993
Grant Likelya52f07e2011-03-18 10:21:29 -0600994 if (!matches)
995 return NULL;
996
Kevin Hao215a14c2014-02-19 16:15:45 +0800997 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
998 score = __of_device_is_compatible(node, matches->compatible,
999 matches->type, matches->name);
1000 if (score > best_score) {
1001 best_match = matches;
1002 best_score = score;
1003 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +08001004 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001005
1006 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001007}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001008
1009/**
Geert Uytterhoevenc50949d2014-10-22 11:44:54 +02001010 * of_match_node - Tell if a device_node has a matching of_match structure
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001011 * @matches: array of of device match structures to search in
1012 * @node: the of device structure to match against
1013 *
Kevin Hao71c54982014-02-18 15:57:29 +08001014 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001015 */
1016const struct of_device_id *of_match_node(const struct of_device_id *matches,
1017 const struct device_node *node)
1018{
1019 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001020 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001021
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001022 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001023 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001024 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001025 return match;
1026}
Grant Likely283029d2008-01-09 06:20:40 +11001027EXPORT_SYMBOL(of_match_node);
1028
1029/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001030 * of_find_matching_node_and_match - Find a node based on an of_device_id
1031 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001032 * @from: The node to start searching from or NULL, the node
1033 * you pass will not be searched, only the next one
1034 * will; typically, you pass what the previous call
1035 * returned. of_node_put() will be called on it
1036 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001037 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001038 *
1039 * Returns a node pointer with refcount incremented, use
1040 * of_node_put() on it when done.
1041 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001042struct device_node *of_find_matching_node_and_match(struct device_node *from,
1043 const struct of_device_id *matches,
1044 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001045{
1046 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001047 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001048 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001049
Stephen Warren50c8af42012-11-20 16:12:20 -07001050 if (match)
1051 *match = NULL;
1052
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001053 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001054 for_each_of_allnodes_from(from, np) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001055 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001056 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001057 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001058 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001059 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001060 }
Grant Likely283029d2008-01-09 06:20:40 +11001061 }
1062 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001063 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001064 return np;
1065}
Grant Likely80c20222012-12-19 10:45:36 +00001066EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001067
1068/**
Grant Likely3f07af42008-07-25 22:25:13 -04001069 * of_modalias_node - Lookup appropriate modalias for a device node
1070 * @node: pointer to a device tree node
1071 * @modalias: Pointer to buffer that modalias value will be copied into
1072 * @len: Length of modalias value
1073 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001074 * Based on the value of the compatible property, this routine will attempt
1075 * to choose an appropriate modalias value for a particular device tree node.
1076 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1077 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001078 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001079 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001080 */
1081int of_modalias_node(struct device_node *node, char *modalias, int len)
1082{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001083 const char *compatible, *p;
1084 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001085
1086 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001087 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001088 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001089 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001090 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001091 return 0;
1092}
1093EXPORT_SYMBOL_GPL(of_modalias_node);
1094
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001095/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001096 * of_find_node_by_phandle - Find a node given a phandle
1097 * @handle: phandle of the node to find
1098 *
1099 * Returns a node pointer with refcount incremented, use
1100 * of_node_put() on it when done.
1101 */
1102struct device_node *of_find_node_by_phandle(phandle handle)
1103{
1104 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001105 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001106
Grant Likelyfc59b442014-10-02 13:08:02 +01001107 if (!handle)
1108 return NULL;
1109
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001110 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001111 for_each_of_allnodes(np)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001112 if (np->phandle == handle)
1113 break;
1114 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001115 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001116 return np;
1117}
1118EXPORT_SYMBOL(of_find_node_by_phandle);
1119
Grant Likely624cfca2013-10-11 22:05:10 +01001120void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1121{
1122 int i;
Rob Herring0d638a02017-06-01 15:50:55 -05001123 printk("%s %pOF", msg, args->np);
Marcin Nowakowski4aa66342016-12-01 09:00:55 +01001124 for (i = 0; i < args->args_count; i++) {
1125 const char delim = i ? ',' : ':';
1126
1127 pr_cont("%c%08x", delim, args->args[i]);
1128 }
1129 pr_cont("\n");
Grant Likely624cfca2013-10-11 22:05:10 +01001130}
1131
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001132int of_phandle_iterator_init(struct of_phandle_iterator *it,
1133 const struct device_node *np,
1134 const char *list_name,
1135 const char *cells_name,
1136 int cell_count)
1137{
1138 const __be32 *list;
1139 int size;
1140
1141 memset(it, 0, sizeof(*it));
1142
1143 list = of_get_property(np, list_name, &size);
1144 if (!list)
1145 return -ENOENT;
1146
1147 it->cells_name = cells_name;
1148 it->cell_count = cell_count;
1149 it->parent = np;
1150 it->list_end = list + size / sizeof(*list);
1151 it->phandle_end = list;
1152 it->cur = list;
1153
1154 return 0;
1155}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001156EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001157
Joerg Roedelcd209b42016-04-04 17:49:18 +02001158int of_phandle_iterator_next(struct of_phandle_iterator *it)
1159{
1160 uint32_t count = 0;
1161
1162 if (it->node) {
1163 of_node_put(it->node);
1164 it->node = NULL;
1165 }
1166
1167 if (!it->cur || it->phandle_end >= it->list_end)
1168 return -ENOENT;
1169
1170 it->cur = it->phandle_end;
1171
1172 /* If phandle is 0, then it is an empty entry with no arguments. */
1173 it->phandle = be32_to_cpup(it->cur++);
1174
1175 if (it->phandle) {
1176
1177 /*
1178 * Find the provider node and parse the #*-cells property to
1179 * determine the argument length.
1180 */
1181 it->node = of_find_node_by_phandle(it->phandle);
1182
1183 if (it->cells_name) {
1184 if (!it->node) {
Rob Herring0d638a02017-06-01 15:50:55 -05001185 pr_err("%pOF: could not find phandle\n",
1186 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001187 goto err;
1188 }
1189
1190 if (of_property_read_u32(it->node, it->cells_name,
1191 &count)) {
Rob Herring0d638a02017-06-01 15:50:55 -05001192 pr_err("%pOF: could not get %s for %pOF\n",
1193 it->parent,
Joerg Roedelcd209b42016-04-04 17:49:18 +02001194 it->cells_name,
Rob Herring0d638a02017-06-01 15:50:55 -05001195 it->node);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001196 goto err;
1197 }
1198 } else {
1199 count = it->cell_count;
1200 }
1201
1202 /*
1203 * Make sure that the arguments actually fit in the remaining
1204 * property data length
1205 */
1206 if (it->cur + count > it->list_end) {
Rob Herring0d638a02017-06-01 15:50:55 -05001207 pr_err("%pOF: arguments longer than property\n",
1208 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001209 goto err;
1210 }
1211 }
1212
1213 it->phandle_end = it->cur + count;
1214 it->cur_count = count;
1215
1216 return 0;
1217
1218err:
1219 if (it->node) {
1220 of_node_put(it->node);
1221 it->node = NULL;
1222 }
1223
1224 return -EINVAL;
1225}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001226EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001227
Joerg Roedelabdaa772016-04-04 17:49:21 +02001228int of_phandle_iterator_args(struct of_phandle_iterator *it,
1229 uint32_t *args,
1230 int size)
1231{
1232 int i, count;
1233
1234 count = it->cur_count;
1235
1236 if (WARN_ON(size < count))
1237 count = size;
1238
1239 for (i = 0; i < count; i++)
1240 args[i] = be32_to_cpup(it->cur++);
1241
1242 return count;
1243}
1244
Grant Likelybd69f732013-02-10 22:57:21 +00001245static int __of_parse_phandle_with_args(const struct device_node *np,
1246 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001247 const char *cells_name,
1248 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001249 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001250{
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001251 struct of_phandle_iterator it;
1252 int rc, cur_index = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001253
Grant Likely15c9a0a2011-12-12 09:25:57 -07001254 /* Loop over the phandles until all the requested entry is found */
Joerg Roedelf623ce92016-04-04 17:49:20 +02001255 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001256 /*
Joerg Roedelcd209b42016-04-04 17:49:18 +02001257 * All of the error cases bail out of the loop, so at
Grant Likely15c9a0a2011-12-12 09:25:57 -07001258 * this point, the parsing is successful. If the requested
1259 * index matches, then fill the out_args structure and return,
1260 * or return -ENOENT for an empty entry.
1261 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001262 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001263 if (cur_index == index) {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001264 if (!it.phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001265 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001266
Grant Likely15c9a0a2011-12-12 09:25:57 -07001267 if (out_args) {
Joerg Roedelabdaa772016-04-04 17:49:21 +02001268 int c;
1269
1270 c = of_phandle_iterator_args(&it,
1271 out_args->args,
1272 MAX_PHANDLE_ARGS);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001273 out_args->np = it.node;
Joerg Roedelabdaa772016-04-04 17:49:21 +02001274 out_args->args_count = c;
Tang Yuantianb855f162013-04-10 11:36:39 +08001275 } else {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001276 of_node_put(it.node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001277 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001278
1279 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001280 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001281 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001282
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001283 cur_index++;
1284 }
1285
Grant Likely23ce04c2013-02-12 21:21:49 +00001286 /*
1287 * Unlock node before returning result; will be one of:
1288 * -ENOENT : index is for empty phandle
1289 * -EINVAL : parsing error on data
1290 */
Joerg Roedelcd209b42016-04-04 17:49:18 +02001291
Grant Likely23ce04c2013-02-12 21:21:49 +00001292 err:
Markus Elfringbeab47d2016-07-19 22:42:22 +02001293 of_node_put(it.node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001294 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001295}
Grant Likelybd69f732013-02-10 22:57:21 +00001296
Stephen Warreneded9dd2013-08-14 15:27:08 -06001297/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001298 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1299 * @np: Pointer to device node holding phandle property
1300 * @phandle_name: Name of property holding a phandle value
1301 * @index: For properties holding a table of phandles, this is the index into
1302 * the table
1303 *
1304 * Returns the device_node pointer with refcount incremented. Use
1305 * of_node_put() on it when done.
1306 */
1307struct device_node *of_parse_phandle(const struct device_node *np,
1308 const char *phandle_name, int index)
1309{
Stephen Warren91d99422013-08-14 15:27:11 -06001310 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001311
Stephen Warren91d99422013-08-14 15:27:11 -06001312 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001313 return NULL;
1314
Stephen Warren91d99422013-08-14 15:27:11 -06001315 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1316 index, &args))
1317 return NULL;
1318
1319 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001320}
1321EXPORT_SYMBOL(of_parse_phandle);
1322
1323/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001324 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1325 * @np: pointer to a device tree node containing a list
1326 * @list_name: property name that contains a list
1327 * @cells_name: property name that specifies phandles' arguments count
1328 * @index: index of a phandle to parse out
1329 * @out_args: optional pointer to output arguments structure (will be filled)
1330 *
1331 * This function is useful to parse lists of phandles and their arguments.
1332 * Returns 0 on success and fills out_args, on error returns appropriate
1333 * errno value.
1334 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001335 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warreneded9dd2013-08-14 15:27:08 -06001336 * pointer.
1337 *
1338 * Example:
1339 *
1340 * phandle1: node1 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001341 * #list-cells = <2>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001342 * }
1343 *
1344 * phandle2: node2 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001345 * #list-cells = <1>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001346 * }
1347 *
1348 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001349 * list = <&phandle1 1 2 &phandle2 3>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001350 * }
1351 *
1352 * To get a device_node of the `node2' node you may call this:
1353 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1354 */
Grant Likelybd69f732013-02-10 22:57:21 +00001355int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1356 const char *cells_name, int index,
1357 struct of_phandle_args *out_args)
1358{
1359 if (index < 0)
1360 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001361 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1362 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001363}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001364EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001365
Grant Likelybd69f732013-02-10 22:57:21 +00001366/**
Stephen Warren035fd942013-08-14 15:27:10 -06001367 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1368 * @np: pointer to a device tree node containing a list
1369 * @list_name: property name that contains a list
1370 * @cell_count: number of argument cells following the phandle
1371 * @index: index of a phandle to parse out
1372 * @out_args: optional pointer to output arguments structure (will be filled)
1373 *
1374 * This function is useful to parse lists of phandles and their arguments.
1375 * Returns 0 on success and fills out_args, on error returns appropriate
1376 * errno value.
1377 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001378 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warren035fd942013-08-14 15:27:10 -06001379 * pointer.
1380 *
1381 * Example:
1382 *
1383 * phandle1: node1 {
1384 * }
1385 *
1386 * phandle2: node2 {
1387 * }
1388 *
1389 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001390 * list = <&phandle1 0 2 &phandle2 2 3>;
Stephen Warren035fd942013-08-14 15:27:10 -06001391 * }
1392 *
1393 * To get a device_node of the `node2' node you may call this:
1394 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1395 */
1396int of_parse_phandle_with_fixed_args(const struct device_node *np,
1397 const char *list_name, int cell_count,
1398 int index, struct of_phandle_args *out_args)
1399{
1400 if (index < 0)
1401 return -EINVAL;
1402 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1403 index, out_args);
1404}
1405EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1406
1407/**
Grant Likelybd69f732013-02-10 22:57:21 +00001408 * of_count_phandle_with_args() - Find the number of phandles references in a property
1409 * @np: pointer to a device tree node containing a list
1410 * @list_name: property name that contains a list
1411 * @cells_name: property name that specifies phandles' arguments count
1412 *
1413 * Returns the number of phandle + argument tuples within a property. It
1414 * is a typical pattern to encode a list of phandle and variable
1415 * arguments into a single property. The number of arguments is encoded
1416 * by a property in the phandle-target node. For example, a gpios
1417 * property would contain a list of GPIO specifies consisting of a
1418 * phandle and 1 or more arguments. The number of arguments are
1419 * determined by the #gpio-cells property in the node pointed to by the
1420 * phandle.
1421 */
1422int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1423 const char *cells_name)
1424{
Joerg Roedel2021bd02016-04-04 17:49:19 +02001425 struct of_phandle_iterator it;
1426 int rc, cur_index = 0;
1427
1428 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1429 if (rc)
1430 return rc;
1431
1432 while ((rc = of_phandle_iterator_next(&it)) == 0)
1433 cur_index += 1;
1434
1435 if (rc != -ENOENT)
1436 return rc;
1437
1438 return cur_index;
Grant Likelybd69f732013-02-10 22:57:21 +00001439}
1440EXPORT_SYMBOL(of_count_phandle_with_args);
1441
Grant Likely02af11b2009-11-23 20:16:45 -07001442/**
Xiubo Li62664f62014-01-22 13:57:39 +08001443 * __of_add_property - Add a property to a node without lock operations
1444 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001445int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001446{
1447 struct property **next;
1448
1449 prop->next = NULL;
1450 next = &np->properties;
1451 while (*next) {
1452 if (strcmp(prop->name, (*next)->name) == 0)
1453 /* duplicate ! don't insert it */
1454 return -EEXIST;
1455
1456 next = &(*next)->next;
1457 }
1458 *next = prop;
1459
1460 return 0;
1461}
1462
1463/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001464 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001465 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001466int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001467{
Grant Likely02af11b2009-11-23 20:16:45 -07001468 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001469 int rc;
1470
Grant Likely8a2b22a22014-07-23 17:05:06 -06001471 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001472
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001473 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001474 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001475 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001476
Grant Likely8a2b22a22014-07-23 17:05:06 -06001477 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001478 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001479
Grant Likely8a2b22a22014-07-23 17:05:06 -06001480 mutex_unlock(&of_mutex);
1481
Grant Likely259092a2014-07-16 12:48:23 -06001482 if (!rc)
1483 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1484
Xiubo Li62664f62014-01-22 13:57:39 +08001485 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001486}
1487
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001488int __of_remove_property(struct device_node *np, struct property *prop)
1489{
1490 struct property **next;
1491
1492 for (next = &np->properties; *next; next = &(*next)->next) {
1493 if (*next == prop)
1494 break;
1495 }
1496 if (*next == NULL)
1497 return -ENODEV;
1498
1499 /* found the node */
1500 *next = prop->next;
1501 prop->next = np->deadprops;
1502 np->deadprops = prop;
1503
1504 return 0;
1505}
1506
Frank Rowandd9fc8802016-06-16 10:51:46 -07001507void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
1508{
1509 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1510 kfree(prop->attr.attr.name);
1511}
1512
Grant Likely8a2b22a22014-07-23 17:05:06 -06001513void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1514{
Gaurav Minochaef69d742014-09-05 09:56:13 -07001515 if (!IS_ENABLED(CONFIG_SYSFS))
1516 return;
1517
Grant Likely8a2b22a22014-07-23 17:05:06 -06001518 /* at early boot, bail here and defer setup to of_init() */
1519 if (of_kset && of_node_is_attached(np))
Frank Rowandd9fc8802016-06-16 10:51:46 -07001520 __of_sysfs_remove_bin_file(np, prop);
Grant Likely8a2b22a22014-07-23 17:05:06 -06001521}
1522
Grant Likely02af11b2009-11-23 20:16:45 -07001523/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001524 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001525 *
1526 * Note that we don't actually remove it, since we have given out
1527 * who-knows-how-many pointers to the data using get-property.
1528 * Instead we just move the property to the "dead properties"
1529 * list, so it won't be found any more.
1530 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001531int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001532{
Grant Likely02af11b2009-11-23 20:16:45 -07001533 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001534 int rc;
1535
Suraj Jitindar Singh201b3fe2016-04-28 15:34:54 +10001536 if (!prop)
1537 return -ENODEV;
1538
Grant Likely8a2b22a22014-07-23 17:05:06 -06001539 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001540
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001541 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001542 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001543 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001544
Grant Likely8a2b22a22014-07-23 17:05:06 -06001545 if (!rc)
1546 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001547
Grant Likely8a2b22a22014-07-23 17:05:06 -06001548 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00001549
Grant Likely259092a2014-07-16 12:48:23 -06001550 if (!rc)
1551 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1552
Grant Likely8a2b22a22014-07-23 17:05:06 -06001553 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001554}
1555
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001556int __of_update_property(struct device_node *np, struct property *newprop,
1557 struct property **oldpropp)
1558{
1559 struct property **next, *oldprop;
1560
1561 for (next = &np->properties; *next; next = &(*next)->next) {
1562 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1563 break;
1564 }
1565 *oldpropp = oldprop = *next;
1566
1567 if (oldprop) {
1568 /* replace the node */
1569 newprop->next = oldprop->next;
1570 *next = newprop;
1571 oldprop->next = np->deadprops;
1572 np->deadprops = oldprop;
1573 } else {
1574 /* new node */
1575 newprop->next = NULL;
1576 *next = newprop;
1577 }
Grant Likely02af11b2009-11-23 20:16:45 -07001578
1579 return 0;
1580}
1581
Grant Likely8a2b22a22014-07-23 17:05:06 -06001582void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1583 struct property *oldprop)
1584{
Gaurav Minochaef69d742014-09-05 09:56:13 -07001585 if (!IS_ENABLED(CONFIG_SYSFS))
1586 return;
1587
Grant Likely8a2b22a22014-07-23 17:05:06 -06001588 /* At early boot, bail out and defer setup to of_init() */
1589 if (!of_kset)
1590 return;
1591
1592 if (oldprop)
Frank Rowandd9fc8802016-06-16 10:51:46 -07001593 __of_sysfs_remove_bin_file(np, oldprop);
Grant Likely8a2b22a22014-07-23 17:05:06 -06001594 __of_add_property_sysfs(np, newprop);
1595}
1596
Grant Likely02af11b2009-11-23 20:16:45 -07001597/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001598 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001599 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001600 *
1601 * Note that we don't actually remove it, since we have given out
1602 * who-knows-how-many pointers to the data using get-property.
1603 * Instead we just move the property to the "dead properties" list,
1604 * and add the new property to the property list
1605 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001606int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001607{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001608 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001609 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001610 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001611
Dong Aisheng475d0092012-07-11 15:16:37 +10001612 if (!newprop->name)
1613 return -EINVAL;
1614
Grant Likely8a2b22a22014-07-23 17:05:06 -06001615 mutex_lock(&of_mutex);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001616
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001617 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001618 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001619 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001620
Grant Likely8a2b22a22014-07-23 17:05:06 -06001621 if (!rc)
1622 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001623
Grant Likely8a2b22a22014-07-23 17:05:06 -06001624 mutex_unlock(&of_mutex);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001625
Grant Likely259092a2014-07-16 12:48:23 -06001626 if (!rc)
1627 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001628
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001629 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001630}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001631
Shawn Guo611cad72011-08-15 15:28:14 +08001632static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1633 int id, const char *stem, int stem_len)
1634{
1635 ap->np = np;
1636 ap->id = id;
1637 strncpy(ap->stem, stem, stem_len);
1638 ap->stem[stem_len] = 0;
1639 list_add_tail(&ap->link, &aliases_lookup);
Rob Herring0d638a02017-06-01 15:50:55 -05001640 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1641 ap->alias, ap->stem, ap->id, np);
Shawn Guo611cad72011-08-15 15:28:14 +08001642}
1643
1644/**
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001645 * of_alias_scan - Scan all properties of the 'aliases' node
Shawn Guo611cad72011-08-15 15:28:14 +08001646 *
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001647 * The function scans all the properties of the 'aliases' node and populates
1648 * the global lookup table with the properties. It returns the
1649 * number of alias properties found, or an error code in case of failure.
Shawn Guo611cad72011-08-15 15:28:14 +08001650 *
1651 * @dt_alloc: An allocator that provides a virtual address to memory
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001652 * for storing the resulting tree
Shawn Guo611cad72011-08-15 15:28:14 +08001653 */
1654void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1655{
1656 struct property *pp;
1657
Laurentiu Tudor7dbe5842014-08-27 17:09:39 +03001658 of_aliases = of_find_node_by_path("/aliases");
Shawn Guo611cad72011-08-15 15:28:14 +08001659 of_chosen = of_find_node_by_path("/chosen");
1660 if (of_chosen == NULL)
1661 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001662
1663 if (of_chosen) {
Grant Likelya752ee52014-03-28 08:12:18 -07001664 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001665 const char *name = NULL;
1666
1667 if (of_property_read_string(of_chosen, "stdout-path", &name))
1668 of_property_read_string(of_chosen, "linux,stdout-path",
1669 &name);
Grant Likelya752ee52014-03-28 08:12:18 -07001670 if (IS_ENABLED(CONFIG_PPC) && !name)
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001671 of_property_read_string(of_aliases, "stdout", &name);
Peter Hurleyf64255b2015-03-17 16:46:33 -04001672 if (name)
Leif Lindholm7914a7c2014-11-27 17:56:07 +00001673 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001674 }
1675
Shawn Guo611cad72011-08-15 15:28:14 +08001676 if (!of_aliases)
1677 return;
1678
Dong Aisheng8af0da92011-12-22 20:19:24 +08001679 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001680 const char *start = pp->name;
1681 const char *end = start + strlen(start);
1682 struct device_node *np;
1683 struct alias_prop *ap;
1684 int id, len;
1685
1686 /* Skip those we do not want to proceed */
1687 if (!strcmp(pp->name, "name") ||
1688 !strcmp(pp->name, "phandle") ||
1689 !strcmp(pp->name, "linux,phandle"))
1690 continue;
1691
1692 np = of_find_node_by_path(pp->value);
1693 if (!np)
1694 continue;
1695
1696 /* walk the alias backwards to extract the id and work out
1697 * the 'stem' string */
1698 while (isdigit(*(end-1)) && end > start)
1699 end--;
1700 len = end - start;
1701
1702 if (kstrtoint(end, 10, &id) < 0)
1703 continue;
1704
1705 /* Allocate an alias_prop with enough space for the stem */
Paul Burtonde96ec22016-09-23 16:38:27 +01001706 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
Shawn Guo611cad72011-08-15 15:28:14 +08001707 if (!ap)
1708 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001709 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001710 ap->alias = start;
1711 of_alias_add(ap, np, id, start, len);
1712 }
1713}
1714
1715/**
1716 * of_alias_get_id - Get alias id for the given device_node
1717 * @np: Pointer to the given device_node
1718 * @stem: Alias stem of the given device_node
1719 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01001720 * The function travels the lookup table to get the alias id for the given
1721 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08001722 */
1723int of_alias_get_id(struct device_node *np, const char *stem)
1724{
1725 struct alias_prop *app;
1726 int id = -ENODEV;
1727
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001728 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001729 list_for_each_entry(app, &aliases_lookup, link) {
1730 if (strcmp(app->stem, stem) != 0)
1731 continue;
1732
1733 if (np == app->np) {
1734 id = app->id;
1735 break;
1736 }
1737 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001738 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001739
1740 return id;
1741}
1742EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001743
Wolfram Sang351d2242015-03-12 17:17:58 +01001744/**
1745 * of_alias_get_highest_id - Get highest alias id for the given stem
1746 * @stem: Alias stem to be examined
1747 *
1748 * The function travels the lookup table to get the highest alias id for the
1749 * given alias stem. It returns the alias id if found.
1750 */
1751int of_alias_get_highest_id(const char *stem)
1752{
1753 struct alias_prop *app;
1754 int id = -ENODEV;
1755
1756 mutex_lock(&of_mutex);
1757 list_for_each_entry(app, &aliases_lookup, link) {
1758 if (strcmp(app->stem, stem) != 0)
1759 continue;
1760
1761 if (app->id > id)
1762 id = app->id;
1763 }
1764 mutex_unlock(&of_mutex);
1765
1766 return id;
1767}
1768EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
1769
Sascha Hauer5c19e952013-08-05 14:40:44 +02001770/**
Grant Likely3482f2c2014-03-27 17:18:55 -07001771 * of_console_check() - Test and setup console for DT setup
1772 * @dn - Pointer to device node
1773 * @name - Name to use for preferred console without index. ex. "ttyS"
1774 * @index - Index to use for preferred console.
Sascha Hauer5c19e952013-08-05 14:40:44 +02001775 *
Grant Likely3482f2c2014-03-27 17:18:55 -07001776 * Check if the given device node matches the stdout-path property in the
1777 * /chosen node. If it does then register it as the preferred console and return
1778 * TRUE. Otherwise return FALSE.
Sascha Hauer5c19e952013-08-05 14:40:44 +02001779 */
Grant Likely3482f2c2014-03-27 17:18:55 -07001780bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02001781{
Grant Likely3482f2c2014-03-27 17:18:55 -07001782 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02001783 return false;
Leif Lindholm7914a7c2014-11-27 17:56:07 +00001784 return !add_preferred_console(name, index,
1785 kstrdup(of_stdout_options, GFP_KERNEL));
Sascha Hauer5c19e952013-08-05 14:40:44 +02001786}
Grant Likely3482f2c2014-03-27 17:18:55 -07001787EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001788
1789/**
1790 * of_find_next_cache_node - Find a node's subsidiary cache
1791 * @np: node of type "cpu" or "cache"
1792 *
1793 * Returns a node pointer with refcount incremented, use
1794 * of_node_put() on it when done. Caller should hold a reference
1795 * to np.
1796 */
1797struct device_node *of_find_next_cache_node(const struct device_node *np)
1798{
Rob Herring91d96742017-05-04 12:30:07 -05001799 struct device_node *child, *cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001800
Rob Herring91d96742017-05-04 12:30:07 -05001801 cache_node = of_parse_phandle(np, "l2-cache", 0);
1802 if (!cache_node)
1803 cache_node = of_parse_phandle(np, "next-level-cache", 0);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001804
Rob Herring91d96742017-05-04 12:30:07 -05001805 if (cache_node)
1806 return cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001807
1808 /* OF on pmac has nodes instead of properties named "l2-cache"
1809 * beneath CPU nodes.
1810 */
1811 if (!strcmp(np->type, "cpu"))
1812 for_each_child_of_node(np, child)
1813 if (!strcmp(child->type, "cache"))
1814 return child;
1815
1816 return NULL;
1817}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01001818
1819/**
Sudeep Holla5fa23532017-01-16 10:40:43 +00001820 * of_find_last_cache_level - Find the level at which the last cache is
1821 * present for the given logical cpu
1822 *
1823 * @cpu: cpu number(logical index) for which the last cache level is needed
1824 *
1825 * Returns the the level at which the last cache is present. It is exactly
1826 * same as the total number of cache levels for the given logical cpu.
1827 */
1828int of_find_last_cache_level(unsigned int cpu)
1829{
1830 u32 cache_level = 0;
1831 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
1832
1833 while (np) {
1834 prev = np;
1835 of_node_put(np);
1836 np = of_find_next_cache_node(np);
1837 }
1838
1839 of_property_read_u32(prev, "cache-level", &cache_level);
1840
1841 return cache_level;
1842}