blob: 31341290cd9136ab0b2830c67422eb334ed32f4e [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 */
Grant Likely3482f2c2014-03-27 17:18:55 -070020#include <linux/console.h>
Shawn Guo611cad72011-08-15 15:28:14 +080021#include <linux/ctype.h>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010022#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100023#include <linux/module.h>
24#include <linux/of.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010025#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100026#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000028#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070029#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100030
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080031#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080032
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080033LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080034
Grant Likely5063e252014-10-03 16:28:27 +010035struct device_node *of_root;
36EXPORT_SYMBOL(of_root);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070037struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080038struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -070039struct device_node *of_stdout;
Leif Lindholm7914a7c2014-11-27 17:56:07 +000040static const char *of_stdout_options;
Shawn Guo611cad72011-08-15 15:28:14 +080041
Grant Likely8a2b22a22014-07-23 17:05:06 -060042struct kset *of_kset;
Grant Likely75b57ec2014-02-20 18:02:11 +000043
44/*
Grant Likely8a2b22a22014-07-23 17:05:06 -060045 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
46 * This mutex must be held whenever modifications are being made to the
47 * device tree. The of_{attach,detach}_node() and
48 * of_{add,remove,update}_property() helpers make sure this happens.
Grant Likely75b57ec2014-02-20 18:02:11 +000049 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030050DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100051
Grant Likely5063e252014-10-03 16:28:27 +010052/* use when traversing tree through the child, sibling,
Stephen Rothwell581b6052007-04-24 16:46:53 +100053 * or parent members of struct device_node.
54 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050055DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100056
57int of_n_addr_cells(struct device_node *np)
58{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060059 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100060
61 do {
62 if (np->parent)
63 np = np->parent;
64 ip = of_get_property(np, "#address-cells", NULL);
65 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070066 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100067 } while (np->parent);
68 /* No #address-cells property for the root node */
69 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
70}
71EXPORT_SYMBOL(of_n_addr_cells);
72
73int of_n_size_cells(struct device_node *np)
74{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060075 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100076
77 do {
78 if (np->parent)
79 np = np->parent;
80 ip = of_get_property(np, "#size-cells", NULL);
81 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070082 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100083 } while (np->parent);
84 /* No #size-cells property for the root node */
85 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
86}
87EXPORT_SYMBOL(of_n_size_cells);
88
Rob Herring0c3f0612013-09-17 10:42:50 -050089#ifdef CONFIG_NUMA
90int __weak of_node_to_nid(struct device_node *np)
91{
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +030092 return NUMA_NO_NODE;
Rob Herring0c3f0612013-09-17 10:42:50 -050093}
94#endif
95
Grant Likely6afc0dc2014-06-26 15:40:48 +010096#ifndef CONFIG_OF_DYNAMIC
Grant Likely75b57ec2014-02-20 18:02:11 +000097static void of_node_release(struct kobject *kobj)
98{
99 /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
100}
Grant Likely0f22dd32012-02-15 20:38:40 -0700101#endif /* CONFIG_OF_DYNAMIC */
Grant Likely923f7e32010-01-28 13:52:53 -0700102
Grant Likely75b57ec2014-02-20 18:02:11 +0000103struct kobj_type of_node_ktype = {
104 .release = of_node_release,
105};
106
107static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
108 struct bin_attribute *bin_attr, char *buf,
109 loff_t offset, size_t count)
110{
111 struct property *pp = container_of(bin_attr, struct property, attr);
112 return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
113}
114
Frank Rowand2c00c212016-06-16 10:51:46 -0700115/* always return newly allocated name, caller must free after use */
Grant Likely75b57ec2014-02-20 18:02:11 +0000116static const char *safe_name(struct kobject *kobj, const char *orig_name)
117{
118 const char *name = orig_name;
119 struct kernfs_node *kn;
120 int i = 0;
121
122 /* don't be a hero. After 16 tries give up */
123 while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
124 sysfs_put(kn);
125 if (name != orig_name)
126 kfree(name);
127 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
128 }
129
Frank Rowand2c00c212016-06-16 10:51:46 -0700130 if (name == orig_name) {
131 name = kstrdup(orig_name, GFP_KERNEL);
132 } else {
Grant Likely75b57ec2014-02-20 18:02:11 +0000133 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
134 kobject_name(kobj), name);
Frank Rowand2c00c212016-06-16 10:51:46 -0700135 }
Grant Likely75b57ec2014-02-20 18:02:11 +0000136 return name;
137}
138
Grant Likely8a2b22a22014-07-23 17:05:06 -0600139int __of_add_property_sysfs(struct device_node *np, struct property *pp)
Grant Likely75b57ec2014-02-20 18:02:11 +0000140{
141 int rc;
142
143 /* Important: Don't leak passwords */
144 bool secure = strncmp(pp->name, "security-", 9) == 0;
145
Gaurav Minochaef69d742014-09-05 09:56:13 -0700146 if (!IS_ENABLED(CONFIG_SYSFS))
147 return 0;
148
Grant Likely8a2b22a22014-07-23 17:05:06 -0600149 if (!of_kset || !of_node_is_attached(np))
150 return 0;
151
Grant Likely75b57ec2014-02-20 18:02:11 +0000152 sysfs_bin_attr_init(&pp->attr);
153 pp->attr.attr.name = safe_name(&np->kobj, pp->name);
154 pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
155 pp->attr.size = secure ? 0 : pp->length;
156 pp->attr.read = of_node_property_read;
157
158 rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
159 WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
160 return rc;
161}
162
Grant Likely8a2b22a22014-07-23 17:05:06 -0600163int __of_attach_node_sysfs(struct device_node *np)
Grant Likely75b57ec2014-02-20 18:02:11 +0000164{
165 const char *name;
Frank Rowand2c00c212016-06-16 10:51:46 -0700166 struct kobject *parent;
Grant Likely75b57ec2014-02-20 18:02:11 +0000167 struct property *pp;
168 int rc;
169
Gaurav Minochaef69d742014-09-05 09:56:13 -0700170 if (!IS_ENABLED(CONFIG_SYSFS))
171 return 0;
172
Grant Likely8a2b22a22014-07-23 17:05:06 -0600173 if (!of_kset)
174 return 0;
175
Grant Likely75b57ec2014-02-20 18:02:11 +0000176 np->kobj.kset = of_kset;
177 if (!np->parent) {
178 /* Nodes without parents are new top level trees */
Frank Rowand2c00c212016-06-16 10:51:46 -0700179 name = safe_name(&of_kset->kobj, "base");
180 parent = NULL;
Grant Likely75b57ec2014-02-20 18:02:11 +0000181 } else {
182 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
Frank Rowand2c00c212016-06-16 10:51:46 -0700183 parent = &np->parent->kobj;
Grant Likely75b57ec2014-02-20 18:02:11 +0000184 }
Frank Rowand2c00c212016-06-16 10:51:46 -0700185 if (!name)
186 return -ENOMEM;
187 rc = kobject_add(&np->kobj, parent, "%s", name);
188 kfree(name);
Grant Likely75b57ec2014-02-20 18:02:11 +0000189 if (rc)
190 return rc;
191
192 for_each_property_of_node(np, pp)
193 __of_add_property_sysfs(np, pp);
194
195 return 0;
196}
197
Sudeep Holla194ec932015-05-14 15:28:24 +0100198void __init of_core_init(void)
Grant Likely75b57ec2014-02-20 18:02:11 +0000199{
200 struct device_node *np;
201
202 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300203 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000204 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
205 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300206 mutex_unlock(&of_mutex);
Sudeep Holla194ec932015-05-14 15:28:24 +0100207 pr_err("devicetree: failed to register existing nodes\n");
208 return;
Grant Likely75b57ec2014-02-20 18:02:11 +0000209 }
210 for_each_of_allnodes(np)
Grant Likely8a2b22a22014-07-23 17:05:06 -0600211 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300212 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000213
Grant Likely83570412012-11-06 21:03:27 +0000214 /* Symlink in /proc as required by userspace ABI */
Grant Likely5063e252014-10-03 16:28:27 +0100215 if (of_root)
Grant Likely75b57ec2014-02-20 18:02:11 +0000216 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000217}
Grant Likely75b57ec2014-02-20 18:02:11 +0000218
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500219static struct property *__of_find_property(const struct device_node *np,
220 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000221{
222 struct property *pp;
223
Timur Tabi64e45662008-05-08 05:19:59 +1000224 if (!np)
225 return NULL;
226
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530227 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000228 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530229 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000230 *lenp = pp->length;
231 break;
232 }
233 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500234
235 return pp;
236}
237
238struct property *of_find_property(const struct device_node *np,
239 const char *name,
240 int *lenp)
241{
242 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500243 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500244
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500245 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500246 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500247 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000248
249 return pp;
250}
251EXPORT_SYMBOL(of_find_property);
252
Grant Likely5063e252014-10-03 16:28:27 +0100253struct device_node *__of_find_all_nodes(struct device_node *prev)
254{
255 struct device_node *np;
256 if (!prev) {
257 np = of_root;
258 } else if (prev->child) {
259 np = prev->child;
260 } else {
261 /* Walk back up looking for a sibling, or the end of the structure */
262 np = prev;
263 while (np->parent && !np->sibling)
264 np = np->parent;
265 np = np->sibling; /* Might be null at the end of the tree */
266 }
267 return np;
268}
269
Grant Likelye91edcf2009-10-15 10:58:09 -0600270/**
271 * of_find_all_nodes - Get next node in global list
272 * @prev: Previous node or NULL to start iteration
273 * of_node_put() will be called on it
274 *
275 * Returns a node pointer with refcount incremented, use
276 * of_node_put() on it when done.
277 */
278struct device_node *of_find_all_nodes(struct device_node *prev)
279{
280 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000281 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600282
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000283 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100284 np = __of_find_all_nodes(prev);
285 of_node_get(np);
Grant Likelye91edcf2009-10-15 10:58:09 -0600286 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000287 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600288 return np;
289}
290EXPORT_SYMBOL(of_find_all_nodes);
291
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000292/*
293 * Find a property with a given name for a given node
294 * and return the value.
295 */
Grant Likelya25095d2014-07-15 23:25:43 -0600296const void *__of_get_property(const struct device_node *np,
297 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500298{
299 struct property *pp = __of_find_property(np, name, lenp);
300
301 return pp ? pp->value : NULL;
302}
303
304/*
305 * Find a property with a given name for a given node
306 * and return the value.
307 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000308const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500309 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000310{
311 struct property *pp = of_find_property(np, name, lenp);
312
313 return pp ? pp->value : NULL;
314}
315EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000316
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100317/*
318 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
319 *
320 * @cpu: logical cpu index of a core/thread
321 * @phys_id: physical identifier of a core/thread
322 *
323 * CPU logical to physical index mapping is architecture specific.
324 * However this __weak function provides a default match of physical
325 * id to logical cpu index. phys_id provided here is usually values read
326 * from the device tree which must match the hardware internal registers.
327 *
328 * Returns true if the physical identifier and the logical cpu index
329 * correspond to the same core/thread, false otherwise.
330 */
331bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
332{
333 return (u32)phys_id == cpu;
334}
335
336/**
337 * Checks if the given "prop_name" property holds the physical id of the
338 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
339 * NULL, local thread number within the core is returned in it.
340 */
341static bool __of_find_n_match_cpu_property(struct device_node *cpun,
342 const char *prop_name, int cpu, unsigned int *thread)
343{
344 const __be32 *cell;
345 int ac, prop_len, tid;
346 u64 hwid;
347
348 ac = of_n_addr_cells(cpun);
349 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100350 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100351 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100352 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100353 for (tid = 0; tid < prop_len; tid++) {
354 hwid = of_read_number(cell, ac);
355 if (arch_match_cpu_phys_id(cpu, hwid)) {
356 if (thread)
357 *thread = tid;
358 return true;
359 }
360 cell += ac;
361 }
362 return false;
363}
364
David Millerd1cb9d12013-10-03 17:24:51 -0400365/*
366 * arch_find_n_match_cpu_physical_id - See if the given device node is
367 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
368 * else false. If 'thread' is non-NULL, the local thread number within the
369 * core is returned in it.
370 */
371bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
372 int cpu, unsigned int *thread)
373{
374 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
375 * for thread ids on PowerPC. If it doesn't exist fallback to
376 * standard "reg" property.
377 */
378 if (IS_ENABLED(CONFIG_PPC) &&
379 __of_find_n_match_cpu_property(cpun,
380 "ibm,ppc-interrupt-server#s",
381 cpu, thread))
382 return true;
383
Masahiro Yamada510bd062015-10-28 12:05:27 +0900384 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
David Millerd1cb9d12013-10-03 17:24:51 -0400385}
386
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100387/**
388 * of_get_cpu_node - Get device node associated with the given logical CPU
389 *
390 * @cpu: CPU number(logical index) for which device node is required
391 * @thread: if not NULL, local thread number within the physical core is
392 * returned
393 *
394 * The main purpose of this function is to retrieve the device node for the
395 * given logical CPU index. It should be used to initialize the of_node in
396 * cpu device. Once of_node in cpu device is populated, all the further
397 * references can use that instead.
398 *
399 * CPU logical to physical index mapping is architecture specific and is built
400 * before booting secondary cores. This function uses arch_match_cpu_phys_id
401 * which can be overridden by architecture specific implementation.
402 *
403 * Returns a node pointer for the logical cpu if found, else NULL.
404 */
405struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
406{
David Millerd1cb9d12013-10-03 17:24:51 -0400407 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100408
David Millerd1cb9d12013-10-03 17:24:51 -0400409 for_each_node_by_type(cpun, "cpu") {
410 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100411 return cpun;
412 }
413 return NULL;
414}
415EXPORT_SYMBOL(of_get_cpu_node);
416
Kevin Hao215a14c2014-02-19 16:15:45 +0800417/**
418 * __of_device_is_compatible() - Check if the node matches given constraints
419 * @device: pointer to node
420 * @compat: required compatible string, NULL or "" for any match
421 * @type: required device_type value, NULL or "" for any match
422 * @name: required node name, NULL or "" for any match
423 *
424 * Checks if the given @compat, @type and @name strings match the
425 * properties of the given @device. A constraints can be skipped by
426 * passing NULL or an empty string as the constraint.
427 *
428 * Returns 0 for no match, and a positive integer on match. The return
429 * value is a relative score with larger values indicating better
430 * matches. The score is weighted for the most specific compatible value
431 * to get the highest score. Matching type is next, followed by matching
432 * name. Practically speaking, this results in the following priority
433 * order for matches:
434 *
435 * 1. specific compatible && type && name
436 * 2. specific compatible && type
437 * 3. specific compatible && name
438 * 4. specific compatible
439 * 5. general compatible && type && name
440 * 6. general compatible && type
441 * 7. general compatible && name
442 * 8. general compatible
443 * 9. type && name
444 * 10. type
445 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000446 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500447static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800448 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000449{
Kevin Hao215a14c2014-02-19 16:15:45 +0800450 struct property *prop;
451 const char *cp;
452 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000453
Kevin Hao215a14c2014-02-19 16:15:45 +0800454 /* Compatible match has highest priority */
455 if (compat && compat[0]) {
456 prop = __of_find_property(device, "compatible", NULL);
457 for (cp = of_prop_next_string(prop, NULL); cp;
458 cp = of_prop_next_string(prop, cp), index++) {
459 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
460 score = INT_MAX/2 - (index << 2);
461 break;
462 }
463 }
464 if (!score)
465 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000466 }
467
Kevin Hao215a14c2014-02-19 16:15:45 +0800468 /* Matching type is better than matching name */
469 if (type && type[0]) {
470 if (!device->type || of_node_cmp(type, device->type))
471 return 0;
472 score += 2;
473 }
474
475 /* Matching name is a bit better than not */
476 if (name && name[0]) {
477 if (!device->name || of_node_cmp(name, device->name))
478 return 0;
479 score++;
480 }
481
482 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000483}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500484
485/** Checks if the given "compat" string matches one of the strings in
486 * the device's "compatible" property
487 */
488int of_device_is_compatible(const struct device_node *device,
489 const char *compat)
490{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500491 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500492 int res;
493
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500494 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800495 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500496 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500497 return res;
498}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000499EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000500
501/**
Grant Likely71a157e2010-02-01 21:34:14 -0700502 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700503 * @compat: compatible string to look for in root node's compatible property.
504 *
Kevin Cernekee25c7a1d2014-11-12 12:54:00 -0800505 * Returns a positive integer if the root node has the given value in its
Grant Likely1f43cfb2010-01-28 13:47:25 -0700506 * compatible property.
507 */
Grant Likely71a157e2010-02-01 21:34:14 -0700508int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700509{
510 struct device_node *root;
511 int rc = 0;
512
513 root = of_find_node_by_path("/");
514 if (root) {
515 rc = of_device_is_compatible(root, compat);
516 of_node_put(root);
517 }
518 return rc;
519}
Grant Likely71a157e2010-02-01 21:34:14 -0700520EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700521
522/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700523 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100524 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700525 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100526 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800527 * Returns true if the status property is absent or set to "okay" or "ok",
528 * false otherwise
Josh Boyer834d97d2008-03-27 00:33:14 +1100529 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800530static bool __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100531{
532 const char *status;
533 int statlen;
534
Xiubo Li42ccd782014-01-13 11:07:28 +0800535 if (!device)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800536 return false;
Xiubo Li42ccd782014-01-13 11:07:28 +0800537
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700538 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100539 if (status == NULL)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800540 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100541
542 if (statlen > 0) {
543 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800544 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100545 }
546
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800547 return false;
Josh Boyer834d97d2008-03-27 00:33:14 +1100548}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700549
550/**
551 * of_device_is_available - check if a device is available for use
552 *
553 * @device: Node to check for availability
554 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800555 * Returns true if the status property is absent or set to "okay" or "ok",
556 * false otherwise
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700557 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800558bool of_device_is_available(const struct device_node *device)
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700559{
560 unsigned long flags;
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800561 bool res;
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700562
563 raw_spin_lock_irqsave(&devtree_lock, flags);
564 res = __of_device_is_available(device);
565 raw_spin_unlock_irqrestore(&devtree_lock, flags);
566 return res;
567
568}
Josh Boyer834d97d2008-03-27 00:33:14 +1100569EXPORT_SYMBOL(of_device_is_available);
570
571/**
Kevin Cernekee37786c72015-04-09 13:05:14 -0700572 * of_device_is_big_endian - check if a device has BE registers
573 *
574 * @device: Node to check for endianness
575 *
576 * Returns true if the device has a "big-endian" property, or if the kernel
577 * was compiled for BE *and* the device has a "native-endian" property.
578 * Returns false otherwise.
579 *
580 * Callers would nominally use ioread32be/iowrite32be if
581 * of_device_is_big_endian() == true, or readl/writel otherwise.
582 */
583bool of_device_is_big_endian(const struct device_node *device)
584{
585 if (of_property_read_bool(device, "big-endian"))
586 return true;
587 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
588 of_property_read_bool(device, "native-endian"))
589 return true;
590 return false;
591}
592EXPORT_SYMBOL(of_device_is_big_endian);
593
594/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000595 * of_get_parent - Get a node's parent if any
596 * @node: Node to get parent
597 *
598 * Returns a node pointer with refcount incremented, use
599 * of_node_put() on it when done.
600 */
601struct device_node *of_get_parent(const struct device_node *node)
602{
603 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500604 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000605
606 if (!node)
607 return NULL;
608
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500609 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000610 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500611 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000612 return np;
613}
614EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd355a2007-04-24 17:21:29 +1000615
616/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000617 * of_get_next_parent - Iterate to a node's parent
618 * @node: Node to get parent of
619 *
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +0200620 * This is like of_get_parent() except that it drops the
621 * refcount on the passed node, making it suitable for iterating
622 * through a node's parents.
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000623 *
624 * Returns a node pointer with refcount incremented, use
625 * of_node_put() on it when done.
626 */
627struct device_node *of_get_next_parent(struct device_node *node)
628{
629 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500630 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000631
632 if (!node)
633 return NULL;
634
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500635 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000636 parent = of_node_get(node->parent);
637 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500638 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000639 return parent;
640}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300641EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000642
Grant Likely0d0e02d2014-05-22 01:04:17 +0900643static struct device_node *__of_get_next_child(const struct device_node *node,
644 struct device_node *prev)
645{
646 struct device_node *next;
647
Florian Fainelli43cb4362014-05-28 10:39:02 -0700648 if (!node)
649 return NULL;
650
Grant Likely0d0e02d2014-05-22 01:04:17 +0900651 next = prev ? prev->sibling : node->child;
652 for (; next; next = next->sibling)
653 if (of_node_get(next))
654 break;
655 of_node_put(prev);
656 return next;
657}
658#define __for_each_child_of_node(parent, child) \
659 for (child = __of_get_next_child(parent, NULL); child != NULL; \
660 child = __of_get_next_child(parent, child))
661
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000662/**
Stephen Rothwelld1cd355a2007-04-24 17:21:29 +1000663 * of_get_next_child - Iterate a node childs
664 * @node: parent node
665 * @prev: previous child of the parent node, or NULL to get first
666 *
Baruch Siach64808272015-03-19 14:03:41 +0200667 * Returns a node pointer with refcount incremented, use of_node_put() on
668 * it when done. Returns NULL when prev is the last child. Decrements the
669 * refcount of prev.
Stephen Rothwelld1cd355a2007-04-24 17:21:29 +1000670 */
671struct device_node *of_get_next_child(const struct device_node *node,
672 struct device_node *prev)
673{
674 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500675 unsigned long flags;
Stephen Rothwelld1cd355a2007-04-24 17:21:29 +1000676
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500677 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900678 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500679 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd355a2007-04-24 17:21:29 +1000680 return next;
681}
682EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000683
684/**
Timur Tabi32961932012-08-14 13:20:23 +0000685 * of_get_next_available_child - Find the next available child node
686 * @node: parent node
687 * @prev: previous child of the parent node, or NULL to get first
688 *
689 * This function is like of_get_next_child(), except that it
690 * automatically skips any disabled nodes (i.e. status = "disabled").
691 */
692struct device_node *of_get_next_available_child(const struct device_node *node,
693 struct device_node *prev)
694{
695 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000696 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000697
Florian Fainelli43cb4362014-05-28 10:39:02 -0700698 if (!node)
699 return NULL;
700
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000701 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000702 next = prev ? prev->sibling : node->child;
703 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700704 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000705 continue;
706 if (of_node_get(next))
707 break;
708 }
709 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000710 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000711 return next;
712}
713EXPORT_SYMBOL(of_get_next_available_child);
714
715/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100716 * of_get_child_by_name - Find the child node by name for a given parent
717 * @node: parent node
718 * @name: child name to look for.
719 *
720 * This function looks for child node for given matching name
721 *
722 * Returns a node pointer if found, with refcount incremented, use
723 * of_node_put() on it when done.
724 * Returns NULL if node is not found.
725 */
726struct device_node *of_get_child_by_name(const struct device_node *node,
727 const char *name)
728{
729 struct device_node *child;
730
731 for_each_child_of_node(node, child)
732 if (child->name && (of_node_cmp(child->name, name) == 0))
733 break;
734 return child;
735}
736EXPORT_SYMBOL(of_get_child_by_name);
737
Grant Likelyc22e6502014-03-14 17:07:12 +0000738static struct device_node *__of_find_node_by_path(struct device_node *parent,
739 const char *path)
740{
741 struct device_node *child;
Leif Lindholm106937e2015-03-06 16:52:53 +0000742 int len;
Grant Likelyc22e6502014-03-14 17:07:12 +0000743
Brian Norris721a09e2015-03-17 12:30:31 -0700744 len = strcspn(path, "/:");
Grant Likelyc22e6502014-03-14 17:07:12 +0000745 if (!len)
746 return NULL;
747
748 __for_each_child_of_node(parent, child) {
749 const char *name = strrchr(child->full_name, '/');
750 if (WARN(!name, "malformed device_node %s\n", child->full_name))
751 continue;
752 name++;
753 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
754 return child;
755 }
756 return NULL;
757}
758
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100759/**
Leif Lindholm75c28c02014-11-28 11:34:28 +0000760 * of_find_node_opts_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000761 * @path: Either the full path to match, or if the path does not
762 * start with '/', the name of a property of the /aliases
763 * node (an alias). In the case of an alias, the node
764 * matching the alias' value will be returned.
Leif Lindholm75c28c02014-11-28 11:34:28 +0000765 * @opts: Address of a pointer into which to store the start of
766 * an options string appended to the end of the path with
767 * a ':' separator.
Grant Likelyc22e6502014-03-14 17:07:12 +0000768 *
769 * Valid paths:
770 * /foo/bar Full path
771 * foo Valid alias
772 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000773 *
774 * Returns a node pointer with refcount incremented, use
775 * of_node_put() on it when done.
776 */
Leif Lindholm75c28c02014-11-28 11:34:28 +0000777struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000778{
Grant Likelyc22e6502014-03-14 17:07:12 +0000779 struct device_node *np = NULL;
780 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500781 unsigned long flags;
Leif Lindholm75c28c02014-11-28 11:34:28 +0000782 const char *separator = strchr(path, ':');
783
784 if (opts)
785 *opts = separator ? separator + 1 : NULL;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000786
Grant Likelyc22e6502014-03-14 17:07:12 +0000787 if (strcmp(path, "/") == 0)
Grant Likely5063e252014-10-03 16:28:27 +0100788 return of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000789
790 /* The path could begin with an alias */
791 if (*path != '/') {
Leif Lindholm106937e2015-03-06 16:52:53 +0000792 int len;
793 const char *p = separator;
794
795 if (!p)
796 p = strchrnul(path, '/');
797 len = p - path;
Grant Likelyc22e6502014-03-14 17:07:12 +0000798
799 /* of_aliases must not be NULL */
800 if (!of_aliases)
801 return NULL;
802
803 for_each_property_of_node(of_aliases, pp) {
804 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
805 np = of_find_node_by_path(pp->value);
806 break;
807 }
808 }
809 if (!np)
810 return NULL;
811 path = p;
812 }
813
814 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500815 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000816 if (!np)
Grant Likely5063e252014-10-03 16:28:27 +0100817 np = of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000818 while (np && *path == '/') {
819 path++; /* Increment past '/' delimiter */
820 np = __of_find_node_by_path(np, path);
821 path = strchrnul(path, '/');
Leif Lindholm106937e2015-03-06 16:52:53 +0000822 if (separator && separator < path)
823 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000824 }
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500825 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000826 return np;
827}
Leif Lindholm75c28c02014-11-28 11:34:28 +0000828EXPORT_SYMBOL(of_find_node_opts_by_path);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000829
830/**
831 * of_find_node_by_name - Find a node by its "name" property
832 * @from: The node to start searching from or NULL, the node
833 * you pass will not be searched, only the next one
834 * will; typically, you pass what the previous call
835 * returned. of_node_put() will be called on it
836 * @name: The name string to match against
837 *
838 * Returns a node pointer with refcount incremented, use
839 * of_node_put() on it when done.
840 */
841struct device_node *of_find_node_by_name(struct device_node *from,
842 const char *name)
843{
844 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500845 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000846
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500847 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100848 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000849 if (np->name && (of_node_cmp(np->name, name) == 0)
850 && of_node_get(np))
851 break;
852 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500853 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000854 return np;
855}
856EXPORT_SYMBOL(of_find_node_by_name);
857
858/**
859 * of_find_node_by_type - Find a node by its "device_type" property
860 * @from: The node to start searching from, or NULL to start searching
861 * the entire device tree. The node you pass will not be
862 * searched, only the next one will; typically, you pass
863 * what the previous call returned. of_node_put() will be
864 * called on from for you.
865 * @type: The type string to match against
866 *
867 * Returns a node pointer with refcount incremented, use
868 * of_node_put() on it when done.
869 */
870struct device_node *of_find_node_by_type(struct device_node *from,
871 const char *type)
872{
873 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500874 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000875
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500876 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100877 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000878 if (np->type && (of_node_cmp(np->type, type) == 0)
879 && of_node_get(np))
880 break;
881 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500882 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000883 return np;
884}
885EXPORT_SYMBOL(of_find_node_by_type);
886
887/**
888 * of_find_compatible_node - Find a node based on type and one of the
889 * tokens in its "compatible" property
890 * @from: The node to start searching from or NULL, the node
891 * you pass will not be searched, only the next one
892 * will; typically, you pass what the previous call
893 * returned. of_node_put() will be called on it
894 * @type: The type string to match "device_type" or NULL to ignore
895 * @compatible: The string to match to one of the tokens in the device
896 * "compatible" list.
897 *
898 * Returns a node pointer with refcount incremented, use
899 * of_node_put() on it when done.
900 */
901struct device_node *of_find_compatible_node(struct device_node *from,
902 const char *type, const char *compatible)
903{
904 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500905 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000906
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500907 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100908 for_each_of_allnodes_from(from, np)
Kevin Hao215a14c2014-02-19 16:15:45 +0800909 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500910 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000911 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000912 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500913 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000914 return np;
915}
916EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100917
918/**
Michael Ellerman1e291b142008-11-12 18:54:42 +0000919 * of_find_node_with_property - Find a node which has a property with
920 * the given name.
921 * @from: The node to start searching from or NULL, the node
922 * you pass will not be searched, only the next one
923 * will; typically, you pass what the previous call
924 * returned. of_node_put() will be called on it
925 * @prop_name: The name of the property to look for.
926 *
927 * Returns a node pointer with refcount incremented, use
928 * of_node_put() on it when done.
929 */
930struct device_node *of_find_node_with_property(struct device_node *from,
931 const char *prop_name)
932{
933 struct device_node *np;
934 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500935 unsigned long flags;
Michael Ellerman1e291b142008-11-12 18:54:42 +0000936
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500937 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100938 for_each_of_allnodes_from(from, np) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530939 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b142008-11-12 18:54:42 +0000940 if (of_prop_cmp(pp->name, prop_name) == 0) {
941 of_node_get(np);
942 goto out;
943 }
944 }
945 }
946out:
947 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500948 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000949 return np;
950}
951EXPORT_SYMBOL(of_find_node_with_property);
952
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500953static
954const struct of_device_id *__of_match_node(const struct of_device_id *matches,
955 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +1100956{
Kevin Hao215a14c2014-02-19 16:15:45 +0800957 const struct of_device_id *best_match = NULL;
958 int score, best_score = 0;
959
Grant Likelya52f07e2011-03-18 10:21:29 -0600960 if (!matches)
961 return NULL;
962
Kevin Hao215a14c2014-02-19 16:15:45 +0800963 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
964 score = __of_device_is_compatible(node, matches->compatible,
965 matches->type, matches->name);
966 if (score > best_score) {
967 best_match = matches;
968 best_score = score;
969 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +0800970 }
Kevin Hao215a14c2014-02-19 16:15:45 +0800971
972 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +1100973}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500974
975/**
Geert Uytterhoevenc50949d2014-10-22 11:44:54 +0200976 * of_match_node - Tell if a device_node has a matching of_match structure
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500977 * @matches: array of of device match structures to search in
978 * @node: the of device structure to match against
979 *
Kevin Hao71c5498e2014-02-18 15:57:29 +0800980 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500981 */
982const struct of_device_id *of_match_node(const struct of_device_id *matches,
983 const struct device_node *node)
984{
985 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500986 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500987
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500988 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500989 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500990 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500991 return match;
992}
Grant Likely283029d2008-01-09 06:20:40 +1100993EXPORT_SYMBOL(of_match_node);
994
995/**
Stephen Warren50c8af42012-11-20 16:12:20 -0700996 * of_find_matching_node_and_match - Find a node based on an of_device_id
997 * match table.
Grant Likely283029d2008-01-09 06:20:40 +1100998 * @from: The node to start searching from or NULL, the node
999 * you pass will not be searched, only the next one
1000 * will; typically, you pass what the previous call
1001 * returned. of_node_put() will be called on it
1002 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001003 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001004 *
1005 * Returns a node pointer with refcount incremented, use
1006 * of_node_put() on it when done.
1007 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001008struct device_node *of_find_matching_node_and_match(struct device_node *from,
1009 const struct of_device_id *matches,
1010 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001011{
1012 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001013 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001014 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001015
Stephen Warren50c8af42012-11-20 16:12:20 -07001016 if (match)
1017 *match = NULL;
1018
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001019 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001020 for_each_of_allnodes_from(from, np) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001021 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001022 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001023 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001024 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001025 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001026 }
Grant Likely283029d2008-01-09 06:20:40 +11001027 }
1028 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001029 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001030 return np;
1031}
Grant Likely80c20222012-12-19 10:45:36 +00001032EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001033
1034/**
Grant Likely3f07af42008-07-25 22:25:13 -04001035 * of_modalias_node - Lookup appropriate modalias for a device node
1036 * @node: pointer to a device tree node
1037 * @modalias: Pointer to buffer that modalias value will be copied into
1038 * @len: Length of modalias value
1039 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001040 * Based on the value of the compatible property, this routine will attempt
1041 * to choose an appropriate modalias value for a particular device tree node.
1042 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1043 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001044 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001045 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001046 */
1047int of_modalias_node(struct device_node *node, char *modalias, int len)
1048{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001049 const char *compatible, *p;
1050 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001051
1052 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001053 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001054 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001055 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001056 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001057 return 0;
1058}
1059EXPORT_SYMBOL_GPL(of_modalias_node);
1060
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001061/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001062 * of_find_node_by_phandle - Find a node given a phandle
1063 * @handle: phandle of the node to find
1064 *
1065 * Returns a node pointer with refcount incremented, use
1066 * of_node_put() on it when done.
1067 */
1068struct device_node *of_find_node_by_phandle(phandle handle)
1069{
1070 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001071 unsigned long flags;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001072
Grant Likelyfc59b442014-10-02 13:08:02 +01001073 if (!handle)
1074 return NULL;
1075
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001076 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001077 for_each_of_allnodes(np)
Jeremy Kerr89751a72010-02-01 21:34:11 -07001078 if (np->phandle == handle)
1079 break;
1080 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001081 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001082 return np;
1083}
1084EXPORT_SYMBOL(of_find_node_by_phandle);
1085
1086/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +01001087 * of_property_count_elems_of_size - Count the number of elements in a property
1088 *
1089 * @np: device node from which the property value is to be read.
1090 * @propname: name of the property to be searched.
1091 * @elem_size: size of the individual element
1092 *
1093 * Search for a property in a device node and count the number of elements of
1094 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1095 * property does not exist or its length does not match a multiple of elem_size
1096 * and -ENODATA if the property does not have a value.
1097 */
1098int of_property_count_elems_of_size(const struct device_node *np,
1099 const char *propname, int elem_size)
1100{
1101 struct property *prop = of_find_property(np, propname, NULL);
1102
1103 if (!prop)
1104 return -EINVAL;
1105 if (!prop->value)
1106 return -ENODATA;
1107
1108 if (prop->length % elem_size != 0) {
1109 pr_err("size of %s in node %s is not a multiple of %d\n",
1110 propname, np->full_name, elem_size);
1111 return -EINVAL;
1112 }
1113
1114 return prop->length / elem_size;
1115}
1116EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1117
1118/**
Tony Priskdaeec1f2013-04-03 17:57:11 +13001119 * of_find_property_value_of_size
1120 *
1121 * @np: device node from which the property value is to be read.
1122 * @propname: name of the property to be searched.
1123 * @len: requested length of property value
1124 *
1125 * Search for a property in a device node and valid the requested size.
1126 * Returns the property value on success, -EINVAL if the property does not
1127 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1128 * property data isn't large enough.
1129 *
1130 */
1131static void *of_find_property_value_of_size(const struct device_node *np,
1132 const char *propname, u32 len)
1133{
1134 struct property *prop = of_find_property(np, propname, NULL);
1135
1136 if (!prop)
1137 return ERR_PTR(-EINVAL);
1138 if (!prop->value)
1139 return ERR_PTR(-ENODATA);
1140 if (len > prop->length)
1141 return ERR_PTR(-EOVERFLOW);
1142
1143 return prop->value;
1144}
1145
1146/**
Tony Prisk3daf3722013-03-23 17:02:15 +13001147 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1148 *
1149 * @np: device node from which the property value is to be read.
1150 * @propname: name of the property to be searched.
1151 * @index: index of the u32 in the list of values
1152 * @out_value: pointer to return value, modified only if no error.
1153 *
1154 * Search for a property in a device node and read nth 32-bit value from
1155 * it. Returns 0 on success, -EINVAL if the property does not exist,
1156 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1157 * property data isn't large enough.
1158 *
1159 * The out_value is modified only if a valid u32 value can be decoded.
1160 */
1161int of_property_read_u32_index(const struct device_node *np,
1162 const char *propname,
1163 u32 index, u32 *out_value)
1164{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001165 const u32 *val = of_find_property_value_of_size(np, propname,
1166 ((index + 1) * sizeof(*out_value)));
Tony Prisk3daf3722013-03-23 17:02:15 +13001167
Tony Priskdaeec1f2013-04-03 17:57:11 +13001168 if (IS_ERR(val))
1169 return PTR_ERR(val);
Tony Prisk3daf3722013-03-23 17:02:15 +13001170
Tony Priskdaeec1f2013-04-03 17:57:11 +13001171 *out_value = be32_to_cpup(((__be32 *)val) + index);
Tony Prisk3daf3722013-03-23 17:02:15 +13001172 return 0;
1173}
1174EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1175
1176/**
Viresh Kumarbe193242012-11-20 10:15:19 +05301177 * of_property_read_u8_array - Find and read an array of u8 from a property.
1178 *
1179 * @np: device node from which the property value is to be read.
1180 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301181 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301182 * @sz: number of array elements to read
1183 *
1184 * Search for a property in a device node and read 8-bit value(s) from
1185 * it. Returns 0 on success, -EINVAL if the property does not exist,
1186 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1187 * property data isn't large enough.
1188 *
1189 * dts entry of array should be like:
1190 * property = /bits/ 8 <0x50 0x60 0x70>;
1191 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301192 * The out_values is modified only if a valid u8 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301193 */
1194int of_property_read_u8_array(const struct device_node *np,
1195 const char *propname, u8 *out_values, size_t sz)
1196{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001197 const u8 *val = of_find_property_value_of_size(np, propname,
1198 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301199
Tony Priskdaeec1f2013-04-03 17:57:11 +13001200 if (IS_ERR(val))
1201 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301202
Viresh Kumarbe193242012-11-20 10:15:19 +05301203 while (sz--)
1204 *out_values++ = *val++;
1205 return 0;
1206}
1207EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1208
1209/**
1210 * of_property_read_u16_array - Find and read an array of u16 from a property.
1211 *
1212 * @np: device node from which the property value is to be read.
1213 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301214 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301215 * @sz: number of array elements to read
1216 *
1217 * Search for a property in a device node and read 16-bit value(s) from
1218 * it. Returns 0 on success, -EINVAL if the property does not exist,
1219 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1220 * property data isn't large enough.
1221 *
1222 * dts entry of array should be like:
1223 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1224 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301225 * The out_values is modified only if a valid u16 value can be decoded.
Viresh Kumarbe193242012-11-20 10:15:19 +05301226 */
1227int of_property_read_u16_array(const struct device_node *np,
1228 const char *propname, u16 *out_values, size_t sz)
1229{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001230 const __be16 *val = of_find_property_value_of_size(np, propname,
1231 (sz * sizeof(*out_values)));
Viresh Kumarbe193242012-11-20 10:15:19 +05301232
Tony Priskdaeec1f2013-04-03 17:57:11 +13001233 if (IS_ERR(val))
1234 return PTR_ERR(val);
Viresh Kumarbe193242012-11-20 10:15:19 +05301235
Viresh Kumarbe193242012-11-20 10:15:19 +05301236 while (sz--)
1237 *out_values++ = be16_to_cpup(val++);
1238 return 0;
1239}
1240EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1241
1242/**
Rob Herring0e373632011-07-06 15:42:58 -05001243 * of_property_read_u32_array - Find and read an array of 32 bit integers
1244 * from a property.
1245 *
Thomas Abrahama3b85362011-06-30 21:26:10 +05301246 * @np: device node from which the property value is to be read.
1247 * @propname: name of the property to be searched.
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301248 * @out_values: pointer to return value, modified only if return value is 0.
Viresh Kumarbe193242012-11-20 10:15:19 +05301249 * @sz: number of array elements to read
Thomas Abrahama3b85362011-06-30 21:26:10 +05301250 *
Rob Herring0e373632011-07-06 15:42:58 -05001251 * Search for a property in a device node and read 32-bit value(s) from
Thomas Abrahama3b85362011-06-30 21:26:10 +05301252 * it. Returns 0 on success, -EINVAL if the property does not exist,
1253 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1254 * property data isn't large enough.
1255 *
Lad, Prabhakar792efb82013-05-07 11:34:11 +05301256 * The out_values is modified only if a valid u32 value can be decoded.
Thomas Abrahama3b85362011-06-30 21:26:10 +05301257 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001258int of_property_read_u32_array(const struct device_node *np,
1259 const char *propname, u32 *out_values,
1260 size_t sz)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301261{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001262 const __be32 *val = of_find_property_value_of_size(np, propname,
1263 (sz * sizeof(*out_values)));
Thomas Abrahama3b85362011-06-30 21:26:10 +05301264
Tony Priskdaeec1f2013-04-03 17:57:11 +13001265 if (IS_ERR(val))
1266 return PTR_ERR(val);
Rob Herring0e373632011-07-06 15:42:58 -05001267
Rob Herring0e373632011-07-06 15:42:58 -05001268 while (sz--)
1269 *out_values++ = be32_to_cpup(val++);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301270 return 0;
1271}
Rob Herring0e373632011-07-06 15:42:58 -05001272EXPORT_SYMBOL_GPL(of_property_read_u32_array);
Thomas Abrahama3b85362011-06-30 21:26:10 +05301273
1274/**
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001275 * of_property_read_u64 - Find and read a 64 bit integer from a property
1276 * @np: device node from which the property value is to be read.
1277 * @propname: name of the property to be searched.
1278 * @out_value: pointer to return value, modified only if return value is 0.
1279 *
1280 * Search for a property in a device node and read a 64-bit value from
1281 * it. Returns 0 on success, -EINVAL if the property does not exist,
1282 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1283 * property data isn't large enough.
1284 *
1285 * The out_value is modified only if a valid u64 value can be decoded.
1286 */
1287int of_property_read_u64(const struct device_node *np, const char *propname,
1288 u64 *out_value)
1289{
Tony Priskdaeec1f2013-04-03 17:57:11 +13001290 const __be32 *val = of_find_property_value_of_size(np, propname,
1291 sizeof(*out_value));
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001292
Tony Priskdaeec1f2013-04-03 17:57:11 +13001293 if (IS_ERR(val))
1294 return PTR_ERR(val);
1295
1296 *out_value = of_read_number(val, 2);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +01001297 return 0;
1298}
1299EXPORT_SYMBOL_GPL(of_property_read_u64);
1300
1301/**
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001302 * of_property_read_u64_array - Find and read an array of 64 bit integers
1303 * from a property.
1304 *
1305 * @np: device node from which the property value is to be read.
1306 * @propname: name of the property to be searched.
1307 * @out_values: pointer to return value, modified only if return value is 0.
1308 * @sz: number of array elements to read
1309 *
1310 * Search for a property in a device node and read 64-bit value(s) from
1311 * it. Returns 0 on success, -EINVAL if the property does not exist,
1312 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1313 * property data isn't large enough.
1314 *
1315 * The out_values is modified only if a valid u64 value can be decoded.
1316 */
1317int of_property_read_u64_array(const struct device_node *np,
1318 const char *propname, u64 *out_values,
1319 size_t sz)
1320{
1321 const __be32 *val = of_find_property_value_of_size(np, propname,
1322 (sz * sizeof(*out_values)));
1323
1324 if (IS_ERR(val))
1325 return PTR_ERR(val);
1326
1327 while (sz--) {
1328 *out_values++ = of_read_number(val, 2);
1329 val += 2;
1330 }
1331 return 0;
1332}
Sakari Ailus2d4c0ae2015-01-27 12:26:35 +02001333EXPORT_SYMBOL_GPL(of_property_read_u64_array);
Rafael J. Wysockib31384f2014-11-04 01:28:56 +01001334
1335/**
Thomas Abrahama3b85362011-06-30 21:26:10 +05301336 * of_property_read_string - Find and read a string from a property
1337 * @np: device node from which the property value is to be read.
1338 * @propname: name of the property to be searched.
1339 * @out_string: pointer to null terminated return string, modified only if
1340 * return value is 0.
1341 *
1342 * Search for a property in a device tree node and retrieve a null
1343 * terminated string value (pointer to data, not a copy). Returns 0 on
1344 * success, -EINVAL if the property does not exist, -ENODATA if property
1345 * does not have a value, and -EILSEQ if the string is not null-terminated
1346 * within the length of the property data.
1347 *
1348 * The out_string pointer is modified only if a valid string can be decoded.
1349 */
Jamie Ilesaac285c2011-08-02 15:45:07 +01001350int of_property_read_string(struct device_node *np, const char *propname,
Shawn Guof09bc832011-07-04 09:01:18 +08001351 const char **out_string)
Thomas Abrahama3b85362011-06-30 21:26:10 +05301352{
1353 struct property *prop = of_find_property(np, propname, NULL);
1354 if (!prop)
1355 return -EINVAL;
1356 if (!prop->value)
1357 return -ENODATA;
1358 if (strnlen(prop->value, prop->length) >= prop->length)
1359 return -EILSEQ;
1360 *out_string = prop->value;
1361 return 0;
1362}
1363EXPORT_SYMBOL_GPL(of_property_read_string);
1364
1365/**
Grant Likely7aff0fe2011-12-12 09:25:58 -07001366 * of_property_match_string() - Find string in a list and return index
1367 * @np: pointer to node containing string list property
1368 * @propname: string list property name
1369 * @string: pointer to string to search for in string list
1370 *
1371 * This function searches a string list property and returns the index
1372 * of a specific string value.
1373 */
1374int of_property_match_string(struct device_node *np, const char *propname,
1375 const char *string)
1376{
1377 struct property *prop = of_find_property(np, propname, NULL);
1378 size_t l;
1379 int i;
1380 const char *p, *end;
1381
1382 if (!prop)
1383 return -EINVAL;
1384 if (!prop->value)
1385 return -ENODATA;
1386
1387 p = prop->value;
1388 end = p + prop->length;
1389
1390 for (i = 0; p < end; i++, p += l) {
Grant Likelya87fa1d2014-11-03 15:15:35 +00001391 l = strnlen(p, end - p) + 1;
Grant Likely7aff0fe2011-12-12 09:25:58 -07001392 if (p + l > end)
1393 return -EILSEQ;
1394 pr_debug("comparing %s with %s\n", string, p);
1395 if (strcmp(string, p) == 0)
1396 return i; /* Found it; return index */
1397 }
1398 return -ENODATA;
1399}
1400EXPORT_SYMBOL_GPL(of_property_match_string);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001401
1402/**
Jiri Slabye99010e2014-11-21 13:23:09 +01001403 * of_property_read_string_helper() - Utility helper for parsing string properties
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001404 * @np: device node from which the property value is to be read.
1405 * @propname: name of the property to be searched.
Grant Likelya87fa1d2014-11-03 15:15:35 +00001406 * @out_strs: output array of string pointers.
1407 * @sz: number of array elements to read.
1408 * @skip: Number of strings to skip over at beginning of list.
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001409 *
Grant Likelya87fa1d2014-11-03 15:15:35 +00001410 * Don't call this function directly. It is a utility helper for the
1411 * of_property_read_string*() family of functions.
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001412 */
Grant Likelya87fa1d2014-11-03 15:15:35 +00001413int of_property_read_string_helper(struct device_node *np, const char *propname,
1414 const char **out_strs, size_t sz, int skip)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001415{
1416 struct property *prop = of_find_property(np, propname, NULL);
Grant Likelya87fa1d2014-11-03 15:15:35 +00001417 int l = 0, i = 0;
1418 const char *p, *end;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001419
1420 if (!prop)
1421 return -EINVAL;
1422 if (!prop->value)
1423 return -ENODATA;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001424 p = prop->value;
Grant Likelya87fa1d2014-11-03 15:15:35 +00001425 end = p + prop->length;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001426
Grant Likelya87fa1d2014-11-03 15:15:35 +00001427 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1428 l = strnlen(p, end - p) + 1;
1429 if (p + l > end)
1430 return -EILSEQ;
1431 if (out_strs && i >= skip)
1432 *out_strs++ = p;
1433 }
1434 i -= skip;
1435 return i <= 0 ? -ENODATA : i;
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001436}
Grant Likelya87fa1d2014-11-03 15:15:35 +00001437EXPORT_SYMBOL_GPL(of_property_read_string_helper);
Benoit Cousson4fcd15a2011-09-27 17:45:43 +02001438
Grant Likely624cfca2013-10-11 22:05:10 +01001439void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1440{
1441 int i;
1442 printk("%s %s", msg, of_node_full_name(args->np));
1443 for (i = 0; i < args->args_count; i++)
1444 printk(i ? ",%08x" : ":%08x", args->args[i]);
1445 printk("\n");
1446}
1447
Grant Likelybd69f732013-02-10 22:57:21 +00001448static int __of_parse_phandle_with_args(const struct device_node *np,
1449 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001450 const char *cells_name,
1451 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001452 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001453{
Grant Likely15c9a0a2011-12-12 09:25:57 -07001454 const __be32 *list, *list_end;
Grant Likely23ce04c2013-02-12 21:21:49 +00001455 int rc = 0, size, cur_index = 0;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001456 uint32_t count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001457 struct device_node *node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001458 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001459
Grant Likely15c9a0a2011-12-12 09:25:57 -07001460 /* Retrieve the phandle list property */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001461 list = of_get_property(np, list_name, &size);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001462 if (!list)
Alexandre Courbot1af4c7f2012-06-29 13:57:58 +09001463 return -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001464 list_end = list + size / sizeof(*list);
1465
Grant Likely15c9a0a2011-12-12 09:25:57 -07001466 /* Loop over the phandles until all the requested entry is found */
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001467 while (list < list_end) {
Grant Likely23ce04c2013-02-12 21:21:49 +00001468 rc = -EINVAL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001469 count = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001470
Grant Likely15c9a0a2011-12-12 09:25:57 -07001471 /*
1472 * If phandle is 0, then it is an empty entry with no
1473 * arguments. Skip forward to the next entry.
1474 */
Grant Likely9a6b2e52010-07-23 01:48:25 -06001475 phandle = be32_to_cpup(list++);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001476 if (phandle) {
1477 /*
1478 * Find the provider node and parse the #*-cells
Stephen Warren91d99422013-08-14 15:27:11 -06001479 * property to determine the argument length.
1480 *
1481 * This is not needed if the cell count is hard-coded
1482 * (i.e. cells_name not set, but cell_count is set),
1483 * except when we're going to return the found node
1484 * below.
Grant Likely15c9a0a2011-12-12 09:25:57 -07001485 */
Stephen Warren91d99422013-08-14 15:27:11 -06001486 if (cells_name || cur_index == index) {
1487 node = of_find_node_by_phandle(phandle);
1488 if (!node) {
1489 pr_err("%s: could not find phandle\n",
1490 np->full_name);
1491 goto err;
1492 }
Grant Likely15c9a0a2011-12-12 09:25:57 -07001493 }
Stephen Warren035fd942013-08-14 15:27:10 -06001494
1495 if (cells_name) {
1496 if (of_property_read_u32(node, cells_name,
1497 &count)) {
1498 pr_err("%s: could not get %s for %s\n",
1499 np->full_name, cells_name,
1500 node->full_name);
1501 goto err;
1502 }
1503 } else {
1504 count = cell_count;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001505 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001506
Grant Likely15c9a0a2011-12-12 09:25:57 -07001507 /*
1508 * Make sure that the arguments actually fit in the
1509 * remaining property data length
1510 */
1511 if (list + count > list_end) {
1512 pr_err("%s: arguments longer than property\n",
1513 np->full_name);
Grant Likely23ce04c2013-02-12 21:21:49 +00001514 goto err;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001515 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001516 }
1517
Grant Likely15c9a0a2011-12-12 09:25:57 -07001518 /*
1519 * All of the error cases above bail out of the loop, so at
1520 * this point, the parsing is successful. If the requested
1521 * index matches, then fill the out_args structure and return,
1522 * or return -ENOENT for an empty entry.
1523 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001524 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001525 if (cur_index == index) {
1526 if (!phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001527 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001528
Grant Likely15c9a0a2011-12-12 09:25:57 -07001529 if (out_args) {
1530 int i;
1531 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1532 count = MAX_PHANDLE_ARGS;
1533 out_args->np = node;
1534 out_args->args_count = count;
1535 for (i = 0; i < count; i++)
1536 out_args->args[i] = be32_to_cpup(list++);
Tang Yuantianb855f162013-04-10 11:36:39 +08001537 } else {
1538 of_node_put(node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001539 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001540
1541 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001542 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001543 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001544
1545 of_node_put(node);
1546 node = NULL;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001547 list += count;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001548 cur_index++;
1549 }
1550
Grant Likely23ce04c2013-02-12 21:21:49 +00001551 /*
1552 * Unlock node before returning result; will be one of:
1553 * -ENOENT : index is for empty phandle
1554 * -EINVAL : parsing error on data
Grant Likelybd69f732013-02-10 22:57:21 +00001555 * [1..n] : Number of phandle (count mode; when index = -1)
Grant Likely23ce04c2013-02-12 21:21:49 +00001556 */
Grant Likelybd69f732013-02-10 22:57:21 +00001557 rc = index < 0 ? cur_index : -ENOENT;
Grant Likely23ce04c2013-02-12 21:21:49 +00001558 err:
Grant Likely15c9a0a2011-12-12 09:25:57 -07001559 if (node)
1560 of_node_put(node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001561 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001562}
Grant Likelybd69f732013-02-10 22:57:21 +00001563
Stephen Warreneded9dd42013-08-14 15:27:08 -06001564/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001565 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1566 * @np: Pointer to device node holding phandle property
1567 * @phandle_name: Name of property holding a phandle value
1568 * @index: For properties holding a table of phandles, this is the index into
1569 * the table
1570 *
1571 * Returns the device_node pointer with refcount incremented. Use
1572 * of_node_put() on it when done.
1573 */
1574struct device_node *of_parse_phandle(const struct device_node *np,
1575 const char *phandle_name, int index)
1576{
Stephen Warren91d99422013-08-14 15:27:11 -06001577 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001578
Stephen Warren91d99422013-08-14 15:27:11 -06001579 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001580 return NULL;
1581
Stephen Warren91d99422013-08-14 15:27:11 -06001582 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1583 index, &args))
1584 return NULL;
1585
1586 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001587}
1588EXPORT_SYMBOL(of_parse_phandle);
1589
1590/**
Stephen Warreneded9dd42013-08-14 15:27:08 -06001591 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1592 * @np: pointer to a device tree node containing a list
1593 * @list_name: property name that contains a list
1594 * @cells_name: property name that specifies phandles' arguments count
1595 * @index: index of a phandle to parse out
1596 * @out_args: optional pointer to output arguments structure (will be filled)
1597 *
1598 * This function is useful to parse lists of phandles and their arguments.
1599 * Returns 0 on success and fills out_args, on error returns appropriate
1600 * errno value.
1601 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001602 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warreneded9dd42013-08-14 15:27:08 -06001603 * pointer.
1604 *
1605 * Example:
1606 *
1607 * phandle1: node1 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001608 * #list-cells = <2>;
Stephen Warreneded9dd42013-08-14 15:27:08 -06001609 * }
1610 *
1611 * phandle2: node2 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001612 * #list-cells = <1>;
Stephen Warreneded9dd42013-08-14 15:27:08 -06001613 * }
1614 *
1615 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001616 * list = <&phandle1 1 2 &phandle2 3>;
Stephen Warreneded9dd42013-08-14 15:27:08 -06001617 * }
1618 *
1619 * To get a device_node of the `node2' node you may call this:
1620 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1621 */
Grant Likelybd69f732013-02-10 22:57:21 +00001622int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1623 const char *cells_name, int index,
1624 struct of_phandle_args *out_args)
1625{
1626 if (index < 0)
1627 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001628 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1629 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001630}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001631EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001632
Grant Likelybd69f732013-02-10 22:57:21 +00001633/**
Stephen Warren035fd942013-08-14 15:27:10 -06001634 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1635 * @np: pointer to a device tree node containing a list
1636 * @list_name: property name that contains a list
1637 * @cell_count: number of argument cells following the phandle
1638 * @index: index of a phandle to parse out
1639 * @out_args: optional pointer to output arguments structure (will be filled)
1640 *
1641 * This function is useful to parse lists of phandles and their arguments.
1642 * Returns 0 on success and fills out_args, on error returns appropriate
1643 * errno value.
1644 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001645 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warren035fd942013-08-14 15:27:10 -06001646 * pointer.
1647 *
1648 * Example:
1649 *
1650 * phandle1: node1 {
1651 * }
1652 *
1653 * phandle2: node2 {
1654 * }
1655 *
1656 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001657 * list = <&phandle1 0 2 &phandle2 2 3>;
Stephen Warren035fd942013-08-14 15:27:10 -06001658 * }
1659 *
1660 * To get a device_node of the `node2' node you may call this:
1661 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1662 */
1663int of_parse_phandle_with_fixed_args(const struct device_node *np,
1664 const char *list_name, int cell_count,
1665 int index, struct of_phandle_args *out_args)
1666{
1667 if (index < 0)
1668 return -EINVAL;
1669 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1670 index, out_args);
1671}
1672EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1673
1674/**
Grant Likelybd69f732013-02-10 22:57:21 +00001675 * of_count_phandle_with_args() - Find the number of phandles references in a property
1676 * @np: pointer to a device tree node containing a list
1677 * @list_name: property name that contains a list
1678 * @cells_name: property name that specifies phandles' arguments count
1679 *
1680 * Returns the number of phandle + argument tuples within a property. It
1681 * is a typical pattern to encode a list of phandle and variable
1682 * arguments into a single property. The number of arguments is encoded
1683 * by a property in the phandle-target node. For example, a gpios
1684 * property would contain a list of GPIO specifies consisting of a
1685 * phandle and 1 or more arguments. The number of arguments are
1686 * determined by the #gpio-cells property in the node pointed to by the
1687 * phandle.
1688 */
1689int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1690 const char *cells_name)
1691{
Stephen Warren035fd942013-08-14 15:27:10 -06001692 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1693 NULL);
Grant Likelybd69f732013-02-10 22:57:21 +00001694}
1695EXPORT_SYMBOL(of_count_phandle_with_args);
1696
Grant Likely02af11b2009-11-23 20:16:45 -07001697/**
Xiubo Li62664f62014-01-22 13:57:39 +08001698 * __of_add_property - Add a property to a node without lock operations
1699 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001700int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001701{
1702 struct property **next;
1703
1704 prop->next = NULL;
1705 next = &np->properties;
1706 while (*next) {
1707 if (strcmp(prop->name, (*next)->name) == 0)
1708 /* duplicate ! don't insert it */
1709 return -EEXIST;
1710
1711 next = &(*next)->next;
1712 }
1713 *next = prop;
1714
1715 return 0;
1716}
1717
1718/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001719 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001720 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001721int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001722{
Grant Likely02af11b2009-11-23 20:16:45 -07001723 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001724 int rc;
1725
Grant Likely8a2b22a22014-07-23 17:05:06 -06001726 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001727
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001728 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001729 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001730 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001731
Grant Likely8a2b22a22014-07-23 17:05:06 -06001732 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001733 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001734
Grant Likely8a2b22a22014-07-23 17:05:06 -06001735 mutex_unlock(&of_mutex);
1736
Grant Likely259092a2014-07-16 12:48:23 -06001737 if (!rc)
1738 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1739
Xiubo Li62664f62014-01-22 13:57:39 +08001740 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001741}
1742
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001743int __of_remove_property(struct device_node *np, struct property *prop)
1744{
1745 struct property **next;
1746
1747 for (next = &np->properties; *next; next = &(*next)->next) {
1748 if (*next == prop)
1749 break;
1750 }
1751 if (*next == NULL)
1752 return -ENODEV;
1753
1754 /* found the node */
1755 *next = prop->next;
1756 prop->next = np->deadprops;
1757 np->deadprops = prop;
1758
1759 return 0;
1760}
1761
Frank Rowand2c00c212016-06-16 10:51:46 -07001762void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
1763{
1764 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1765 kfree(prop->attr.attr.name);
1766}
1767
Grant Likely8a2b22a22014-07-23 17:05:06 -06001768void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1769{
Gaurav Minochaef69d742014-09-05 09:56:13 -07001770 if (!IS_ENABLED(CONFIG_SYSFS))
1771 return;
1772
Grant Likely8a2b22a22014-07-23 17:05:06 -06001773 /* at early boot, bail here and defer setup to of_init() */
1774 if (of_kset && of_node_is_attached(np))
Frank Rowand2c00c212016-06-16 10:51:46 -07001775 __of_sysfs_remove_bin_file(np, prop);
Grant Likely8a2b22a22014-07-23 17:05:06 -06001776}
1777
Grant Likely02af11b2009-11-23 20:16:45 -07001778/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001779 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001780 *
1781 * Note that we don't actually remove it, since we have given out
1782 * who-knows-how-many pointers to the data using get-property.
1783 * Instead we just move the property to the "dead properties"
1784 * list, so it won't be found any more.
1785 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001786int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001787{
Grant Likely02af11b2009-11-23 20:16:45 -07001788 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001789 int rc;
1790
Grant Likely8a2b22a22014-07-23 17:05:06 -06001791 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001792
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001793 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001794 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001795 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001796
Grant Likely8a2b22a22014-07-23 17:05:06 -06001797 if (!rc)
1798 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001799
Grant Likely8a2b22a22014-07-23 17:05:06 -06001800 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00001801
Grant Likely259092a2014-07-16 12:48:23 -06001802 if (!rc)
1803 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1804
Grant Likely8a2b22a22014-07-23 17:05:06 -06001805 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001806}
1807
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001808int __of_update_property(struct device_node *np, struct property *newprop,
1809 struct property **oldpropp)
1810{
1811 struct property **next, *oldprop;
1812
1813 for (next = &np->properties; *next; next = &(*next)->next) {
1814 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1815 break;
1816 }
1817 *oldpropp = oldprop = *next;
1818
1819 if (oldprop) {
1820 /* replace the node */
1821 newprop->next = oldprop->next;
1822 *next = newprop;
1823 oldprop->next = np->deadprops;
1824 np->deadprops = oldprop;
1825 } else {
1826 /* new node */
1827 newprop->next = NULL;
1828 *next = newprop;
1829 }
Grant Likely02af11b2009-11-23 20:16:45 -07001830
1831 return 0;
1832}
1833
Grant Likely8a2b22a22014-07-23 17:05:06 -06001834void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1835 struct property *oldprop)
1836{
Gaurav Minochaef69d742014-09-05 09:56:13 -07001837 if (!IS_ENABLED(CONFIG_SYSFS))
1838 return;
1839
Grant Likely8a2b22a22014-07-23 17:05:06 -06001840 /* At early boot, bail out and defer setup to of_init() */
1841 if (!of_kset)
1842 return;
1843
1844 if (oldprop)
Frank Rowand2c00c212016-06-16 10:51:46 -07001845 __of_sysfs_remove_bin_file(np, oldprop);
Grant Likely8a2b22a22014-07-23 17:05:06 -06001846 __of_add_property_sysfs(np, newprop);
1847}
1848
Grant Likely02af11b2009-11-23 20:16:45 -07001849/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001850 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001851 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001852 *
1853 * Note that we don't actually remove it, since we have given out
1854 * who-knows-how-many pointers to the data using get-property.
1855 * Instead we just move the property to the "dead properties" list,
1856 * and add the new property to the property list
1857 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001858int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001859{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001860 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001861 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001862 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001863
Dong Aisheng475d0092012-07-11 15:16:37 +10001864 if (!newprop->name)
1865 return -EINVAL;
1866
Grant Likely8a2b22a22014-07-23 17:05:06 -06001867 mutex_lock(&of_mutex);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001868
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001869 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001870 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001871 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001872
Grant Likely8a2b22a22014-07-23 17:05:06 -06001873 if (!rc)
1874 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001875
Grant Likely8a2b22a22014-07-23 17:05:06 -06001876 mutex_unlock(&of_mutex);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001877
Grant Likely259092a2014-07-16 12:48:23 -06001878 if (!rc)
1879 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001880
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001881 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001882}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001883
Shawn Guo611cad72011-08-15 15:28:14 +08001884static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1885 int id, const char *stem, int stem_len)
1886{
1887 ap->np = np;
1888 ap->id = id;
1889 strncpy(ap->stem, stem, stem_len);
1890 ap->stem[stem_len] = 0;
1891 list_add_tail(&ap->link, &aliases_lookup);
1892 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
Grant Likely74a7f082012-06-15 11:50:25 -06001893 ap->alias, ap->stem, ap->id, of_node_full_name(np));
Shawn Guo611cad72011-08-15 15:28:14 +08001894}
1895
1896/**
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001897 * of_alias_scan - Scan all properties of the 'aliases' node
Shawn Guo611cad72011-08-15 15:28:14 +08001898 *
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001899 * The function scans all the properties of the 'aliases' node and populates
1900 * the global lookup table with the properties. It returns the
1901 * number of alias properties found, or an error code in case of failure.
Shawn Guo611cad72011-08-15 15:28:14 +08001902 *
1903 * @dt_alloc: An allocator that provides a virtual address to memory
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001904 * for storing the resulting tree
Shawn Guo611cad72011-08-15 15:28:14 +08001905 */
1906void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1907{
1908 struct property *pp;
1909
Laurentiu Tudor7dbe5842014-08-27 17:09:39 +03001910 of_aliases = of_find_node_by_path("/aliases");
Shawn Guo611cad72011-08-15 15:28:14 +08001911 of_chosen = of_find_node_by_path("/chosen");
1912 if (of_chosen == NULL)
1913 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001914
1915 if (of_chosen) {
Grant Likelya752ee52014-03-28 08:12:18 -07001916 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
Grant Likely676e1b22014-03-27 17:11:23 -07001917 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
1918 if (!name)
1919 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Grant Likelya752ee52014-03-28 08:12:18 -07001920 if (IS_ENABLED(CONFIG_PPC) && !name)
1921 name = of_get_property(of_aliases, "stdout", NULL);
Peter Hurleyf64255b2015-03-17 16:46:33 -04001922 if (name)
Leif Lindholm7914a7c2014-11-27 17:56:07 +00001923 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001924 }
1925
Shawn Guo611cad72011-08-15 15:28:14 +08001926 if (!of_aliases)
1927 return;
1928
Dong Aisheng8af0da92011-12-22 20:19:24 +08001929 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001930 const char *start = pp->name;
1931 const char *end = start + strlen(start);
1932 struct device_node *np;
1933 struct alias_prop *ap;
1934 int id, len;
1935
1936 /* Skip those we do not want to proceed */
1937 if (!strcmp(pp->name, "name") ||
1938 !strcmp(pp->name, "phandle") ||
1939 !strcmp(pp->name, "linux,phandle"))
1940 continue;
1941
1942 np = of_find_node_by_path(pp->value);
1943 if (!np)
1944 continue;
1945
1946 /* walk the alias backwards to extract the id and work out
1947 * the 'stem' string */
1948 while (isdigit(*(end-1)) && end > start)
1949 end--;
1950 len = end - start;
1951
1952 if (kstrtoint(end, 10, &id) < 0)
1953 continue;
1954
1955 /* Allocate an alias_prop with enough space for the stem */
1956 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1957 if (!ap)
1958 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001959 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001960 ap->alias = start;
1961 of_alias_add(ap, np, id, start, len);
1962 }
1963}
1964
1965/**
1966 * of_alias_get_id - Get alias id for the given device_node
1967 * @np: Pointer to the given device_node
1968 * @stem: Alias stem of the given device_node
1969 *
Geert Uytterhoeven5a53a07f2014-03-11 11:23:38 +01001970 * The function travels the lookup table to get the alias id for the given
1971 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08001972 */
1973int of_alias_get_id(struct device_node *np, const char *stem)
1974{
1975 struct alias_prop *app;
1976 int id = -ENODEV;
1977
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001978 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001979 list_for_each_entry(app, &aliases_lookup, link) {
1980 if (strcmp(app->stem, stem) != 0)
1981 continue;
1982
1983 if (np == app->np) {
1984 id = app->id;
1985 break;
1986 }
1987 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001988 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001989
1990 return id;
1991}
1992EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001993
Wolfram Sang351d2242015-03-12 17:17:58 +01001994/**
1995 * of_alias_get_highest_id - Get highest alias id for the given stem
1996 * @stem: Alias stem to be examined
1997 *
1998 * The function travels the lookup table to get the highest alias id for the
1999 * given alias stem. It returns the alias id if found.
2000 */
2001int of_alias_get_highest_id(const char *stem)
2002{
2003 struct alias_prop *app;
2004 int id = -ENODEV;
2005
2006 mutex_lock(&of_mutex);
2007 list_for_each_entry(app, &aliases_lookup, link) {
2008 if (strcmp(app->stem, stem) != 0)
2009 continue;
2010
2011 if (app->id > id)
2012 id = app->id;
2013 }
2014 mutex_unlock(&of_mutex);
2015
2016 return id;
2017}
2018EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
2019
Stephen Warrenc541adc2012-04-04 09:27:46 -06002020const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2021 u32 *pu)
2022{
2023 const void *curv = cur;
2024
2025 if (!prop)
2026 return NULL;
2027
2028 if (!cur) {
2029 curv = prop->value;
2030 goto out_val;
2031 }
2032
2033 curv += sizeof(*cur);
2034 if (curv >= prop->value + prop->length)
2035 return NULL;
2036
2037out_val:
2038 *pu = be32_to_cpup(curv);
2039 return curv;
2040}
2041EXPORT_SYMBOL_GPL(of_prop_next_u32);
2042
2043const char *of_prop_next_string(struct property *prop, const char *cur)
2044{
2045 const void *curv = cur;
2046
2047 if (!prop)
2048 return NULL;
2049
2050 if (!cur)
2051 return prop->value;
2052
2053 curv += strlen(cur) + 1;
2054 if (curv >= prop->value + prop->length)
2055 return NULL;
2056
2057 return curv;
2058}
2059EXPORT_SYMBOL_GPL(of_prop_next_string);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002060
2061/**
Grant Likely3482f2c2014-03-27 17:18:55 -07002062 * of_console_check() - Test and setup console for DT setup
2063 * @dn - Pointer to device node
2064 * @name - Name to use for preferred console without index. ex. "ttyS"
2065 * @index - Index to use for preferred console.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002066 *
Grant Likely3482f2c2014-03-27 17:18:55 -07002067 * Check if the given device node matches the stdout-path property in the
2068 * /chosen node. If it does then register it as the preferred console and return
2069 * TRUE. Otherwise return FALSE.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002070 */
Grant Likely3482f2c2014-03-27 17:18:55 -07002071bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002072{
Grant Likely3482f2c2014-03-27 17:18:55 -07002073 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002074 return false;
Leif Lindholm7914a7c2014-11-27 17:56:07 +00002075 return !add_preferred_console(name, index,
2076 kstrdup(of_stdout_options, GFP_KERNEL));
Sascha Hauer5c19e952013-08-05 14:40:44 +02002077}
Grant Likely3482f2c2014-03-27 17:18:55 -07002078EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b452013-09-18 11:53:05 +01002079
2080/**
2081 * of_find_next_cache_node - Find a node's subsidiary cache
2082 * @np: node of type "cpu" or "cache"
2083 *
2084 * Returns a node pointer with refcount incremented, use
2085 * of_node_put() on it when done. Caller should hold a reference
2086 * to np.
2087 */
2088struct device_node *of_find_next_cache_node(const struct device_node *np)
2089{
2090 struct device_node *child;
2091 const phandle *handle;
2092
2093 handle = of_get_property(np, "l2-cache", NULL);
2094 if (!handle)
2095 handle = of_get_property(np, "next-level-cache", NULL);
2096
2097 if (handle)
2098 return of_find_node_by_phandle(be32_to_cpup(handle));
2099
2100 /* OF on pmac has nodes instead of properties named "l2-cache"
2101 * beneath CPU nodes.
2102 */
2103 if (!strcmp(np->type, "cpu"))
2104 for_each_child_of_node(np, child)
2105 if (!strcmp(child->type, "cache"))
2106 return child;
2107
2108 return NULL;
2109}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002110
2111/**
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002112 * of_graph_parse_endpoint() - parse common endpoint node properties
2113 * @node: pointer to endpoint device_node
2114 * @endpoint: pointer to the OF endpoint data structure
2115 *
2116 * The caller should hold a reference to @node.
2117 */
2118int of_graph_parse_endpoint(const struct device_node *node,
2119 struct of_endpoint *endpoint)
2120{
2121 struct device_node *port_node = of_get_parent(node);
2122
Philipp Zabeld4847002014-03-04 12:31:24 +01002123 WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2124 __func__, node->full_name);
2125
Philipp Zabelf2a575f2014-02-14 11:53:56 +01002126 memset(endpoint, 0, sizeof(*endpoint));
2127
2128 endpoint->local_node = node;
2129 /*
2130 * It doesn't matter whether the two calls below succeed.
2131 * If they don't then the default value 0 is used.
2132 */
2133 of_property_read_u32(port_node, "reg", &endpoint->port);
2134 of_property_read_u32(node, "reg", &endpoint->id);
2135
2136 of_node_put(port_node);
2137
2138 return 0;
2139}
2140EXPORT_SYMBOL(of_graph_parse_endpoint);
2141
2142/**
Philipp Zabelbfe446e2014-03-11 11:21:11 +01002143 * of_graph_get_port_by_id() - get the port matching a given id
2144 * @parent: pointer to the parent device node
2145 * @id: id of the port
2146 *
2147 * Return: A 'port' node pointer with refcount incremented. The caller
2148 * has to use of_node_put() on it when done.
2149 */
2150struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
2151{
2152 struct device_node *node, *port;
2153
2154 node = of_get_child_by_name(parent, "ports");
2155 if (node)
2156 parent = node;
2157
2158 for_each_child_of_node(parent, port) {
2159 u32 port_id = 0;
2160
2161 if (of_node_cmp(port->name, "port") != 0)
2162 continue;
2163 of_property_read_u32(port, "reg", &port_id);
2164 if (id == port_id)
2165 break;
2166 }
2167
2168 of_node_put(node);
2169
2170 return port;
2171}
2172EXPORT_SYMBOL(of_graph_get_port_by_id);
2173
2174/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002175 * of_graph_get_next_endpoint() - get next endpoint node
2176 * @parent: pointer to the parent device node
2177 * @prev: previous endpoint node, or NULL to get first
2178 *
2179 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
Philipp Zabelf033c0b2014-12-01 13:32:32 +01002180 * of the passed @prev node is decremented.
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002181 */
2182struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2183 struct device_node *prev)
2184{
2185 struct device_node *endpoint;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002186 struct device_node *port;
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002187
2188 if (!parent)
2189 return NULL;
2190
Linus Torvalds3c83e612014-04-04 09:50:07 -07002191 /*
2192 * Start by locating the port node. If no previous endpoint is specified
2193 * search for the first port node, otherwise get the previous endpoint
2194 * parent port node.
2195 */
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002196 if (!prev) {
2197 struct device_node *node;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002198
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002199 node = of_get_child_by_name(parent, "ports");
2200 if (node)
2201 parent = node;
2202
2203 port = of_get_child_by_name(parent, "port");
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002204 of_node_put(node);
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002205
Linus Torvalds3c83e612014-04-04 09:50:07 -07002206 if (!port) {
2207 pr_err("%s(): no port node found in %s\n",
2208 __func__, parent->full_name);
Philipp Zabel4329b932014-02-26 20:43:42 +01002209 return NULL;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002210 }
2211 } else {
2212 port = of_get_parent(prev);
2213 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2214 __func__, prev->full_name))
2215 return NULL;
Linus Torvalds3c83e612014-04-04 09:50:07 -07002216 }
Philipp Zabel4329b932014-02-26 20:43:42 +01002217
Linus Torvalds3c83e612014-04-04 09:50:07 -07002218 while (1) {
2219 /*
2220 * Now that we have a port node, get the next endpoint by
2221 * getting the next child. If the previous endpoint is NULL this
2222 * will return the first child.
2223 */
2224 endpoint = of_get_next_child(port, prev);
2225 if (endpoint) {
2226 of_node_put(port);
2227 return endpoint;
2228 }
2229
2230 /* No more endpoints under this port, try the next one. */
2231 prev = NULL;
2232
2233 do {
2234 port = of_get_next_child(parent, port);
2235 if (!port)
2236 return NULL;
2237 } while (of_node_cmp(port->name, "port"));
2238 }
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002239}
2240EXPORT_SYMBOL(of_graph_get_next_endpoint);
2241
2242/**
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002243 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
2244 * @parent: pointer to the parent device node
2245 * @port_reg: identifier (value of reg property) of the parent port node
2246 * @reg: identifier (value of reg property) of the endpoint node
2247 *
2248 * Return: An 'endpoint' node pointer which is identified by reg and at the same
2249 * is the child of a port node identified by port_reg. reg and port_reg are
2250 * ignored when they are -1.
2251 */
2252struct device_node *of_graph_get_endpoint_by_regs(
2253 const struct device_node *parent, int port_reg, int reg)
2254{
2255 struct of_endpoint endpoint;
Lucas Stach51ed10c2016-08-15 14:58:43 +02002256 struct device_node *node = NULL;
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002257
Lucas Stach51ed10c2016-08-15 14:58:43 +02002258 for_each_endpoint_of_node(parent, node) {
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002259 of_graph_parse_endpoint(node, &endpoint);
2260 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
2261 ((reg == -1) || (endpoint.id == reg)))
2262 return node;
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002263 }
2264
2265 return NULL;
2266}
Dave Airlie8ffaa902015-06-23 10:19:10 +10002267EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
Hyungwon Hwang8ccd0d02015-06-12 21:59:01 +09002268
2269/**
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002270 * of_graph_get_remote_port_parent() - get remote port's parent node
2271 * @node: pointer to a local endpoint device_node
2272 *
2273 * Return: Remote device node associated with remote endpoint node linked
2274 * to @node. Use of_node_put() on it when done.
2275 */
2276struct device_node *of_graph_get_remote_port_parent(
2277 const struct device_node *node)
2278{
2279 struct device_node *np;
2280 unsigned int depth;
2281
2282 /* Get remote endpoint node. */
2283 np = of_parse_phandle(node, "remote-endpoint", 0);
2284
2285 /* Walk 3 levels up only if there is 'ports' node. */
2286 for (depth = 3; depth && np; depth--) {
2287 np = of_get_next_parent(np);
2288 if (depth == 2 && of_node_cmp(np->name, "ports"))
2289 break;
2290 }
2291 return np;
2292}
2293EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2294
2295/**
2296 * of_graph_get_remote_port() - get remote port node
2297 * @node: pointer to a local endpoint device_node
2298 *
2299 * Return: Remote port node associated with remote endpoint node linked
2300 * to @node. Use of_node_put() on it when done.
2301 */
2302struct device_node *of_graph_get_remote_port(const struct device_node *node)
2303{
2304 struct device_node *np;
2305
2306 /* Get remote endpoint node. */
2307 np = of_parse_phandle(node, "remote-endpoint", 0);
2308 if (!np)
2309 return NULL;
2310 return of_get_next_parent(np);
2311}
2312EXPORT_SYMBOL(of_graph_get_remote_port);