Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 1 | /* |
| 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 Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 12 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and |
| 13 | * Grant Likely. |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version |
| 18 | * 2 of the License, or (at your option) any later version. |
| 19 | */ |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 20 | |
| 21 | #define pr_fmt(fmt) "OF: " fmt |
| 22 | |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 23 | #include <linux/console.h> |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 24 | #include <linux/ctype.h> |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 25 | #include <linux/cpu.h> |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/of.h> |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 28 | #include <linux/of_graph.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 29 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 31 | #include <linux/string.h> |
Jeremy Kerr | a9f2f63 | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 32 | #include <linux/proc_fs.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 33 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 34 | #include "of_private.h" |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 35 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 36 | LIST_HEAD(aliases_lookup); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 37 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 38 | struct device_node *of_root; |
| 39 | EXPORT_SYMBOL(of_root); |
Grant Likely | fc0bdae | 2010-02-14 07:13:55 -0700 | [diff] [blame] | 40 | struct device_node *of_chosen; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 41 | struct device_node *of_aliases; |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 42 | struct device_node *of_stdout; |
Leif Lindholm | 7914a7c | 2014-11-27 17:56:07 +0000 | [diff] [blame] | 43 | static const char *of_stdout_options; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 44 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 45 | struct kset *of_kset; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 46 | |
| 47 | /* |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 48 | * Used to protect the of_aliases, to hold off addition of nodes to sysfs. |
| 49 | * This mutex must be held whenever modifications are being made to the |
| 50 | * device tree. The of_{attach,detach}_node() and |
| 51 | * of_{add,remove,update}_property() helpers make sure this happens. |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 52 | */ |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 53 | DEFINE_MUTEX(of_mutex); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 54 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 55 | /* use when traversing tree through the child, sibling, |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 56 | * or parent members of struct device_node. |
| 57 | */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 58 | DEFINE_RAW_SPINLOCK(devtree_lock); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 59 | |
| 60 | int of_n_addr_cells(struct device_node *np) |
| 61 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 62 | const __be32 *ip; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 63 | |
| 64 | do { |
| 65 | if (np->parent) |
| 66 | np = np->parent; |
| 67 | ip = of_get_property(np, "#address-cells", NULL); |
| 68 | if (ip) |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 69 | return be32_to_cpup(ip); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 70 | } while (np->parent); |
| 71 | /* No #address-cells property for the root node */ |
| 72 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 73 | } |
| 74 | EXPORT_SYMBOL(of_n_addr_cells); |
| 75 | |
| 76 | int of_n_size_cells(struct device_node *np) |
| 77 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 78 | const __be32 *ip; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 79 | |
| 80 | do { |
| 81 | if (np->parent) |
| 82 | np = np->parent; |
| 83 | ip = of_get_property(np, "#size-cells", NULL); |
| 84 | if (ip) |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 85 | return be32_to_cpup(ip); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 86 | } while (np->parent); |
| 87 | /* No #size-cells property for the root node */ |
| 88 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 89 | } |
| 90 | EXPORT_SYMBOL(of_n_size_cells); |
| 91 | |
Rob Herring | 0c3f061 | 2013-09-17 10:42:50 -0500 | [diff] [blame] | 92 | #ifdef CONFIG_NUMA |
| 93 | int __weak of_node_to_nid(struct device_node *np) |
| 94 | { |
Konstantin Khlebnikov | c8fff7b | 2015-04-08 19:59:20 +0300 | [diff] [blame] | 95 | return NUMA_NO_NODE; |
Rob Herring | 0c3f061 | 2013-09-17 10:42:50 -0500 | [diff] [blame] | 96 | } |
| 97 | #endif |
| 98 | |
Grant Likely | 6afc0dc | 2014-06-26 15:40:48 +0100 | [diff] [blame] | 99 | #ifndef CONFIG_OF_DYNAMIC |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 100 | static void of_node_release(struct kobject *kobj) |
| 101 | { |
| 102 | /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */ |
| 103 | } |
Grant Likely | 0f22dd3 | 2012-02-15 20:38:40 -0700 | [diff] [blame] | 104 | #endif /* CONFIG_OF_DYNAMIC */ |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 105 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 106 | struct kobj_type of_node_ktype = { |
| 107 | .release = of_node_release, |
| 108 | }; |
| 109 | |
| 110 | static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj, |
| 111 | struct bin_attribute *bin_attr, char *buf, |
| 112 | loff_t offset, size_t count) |
| 113 | { |
| 114 | struct property *pp = container_of(bin_attr, struct property, attr); |
| 115 | return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length); |
| 116 | } |
| 117 | |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 118 | /* always return newly allocated name, caller must free after use */ |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 119 | static const char *safe_name(struct kobject *kobj, const char *orig_name) |
| 120 | { |
| 121 | const char *name = orig_name; |
| 122 | struct kernfs_node *kn; |
| 123 | int i = 0; |
| 124 | |
| 125 | /* don't be a hero. After 16 tries give up */ |
| 126 | while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) { |
| 127 | sysfs_put(kn); |
| 128 | if (name != orig_name) |
| 129 | kfree(name); |
| 130 | name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i); |
| 131 | } |
| 132 | |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 133 | if (name == orig_name) { |
| 134 | name = kstrdup(orig_name, GFP_KERNEL); |
| 135 | } else { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 136 | pr_warn("Duplicate name in %s, renamed to \"%s\"\n", |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 137 | kobject_name(kobj), name); |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 138 | } |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 139 | return name; |
| 140 | } |
| 141 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 142 | int __of_add_property_sysfs(struct device_node *np, struct property *pp) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 143 | { |
| 144 | int rc; |
| 145 | |
| 146 | /* Important: Don't leak passwords */ |
| 147 | bool secure = strncmp(pp->name, "security-", 9) == 0; |
| 148 | |
Gaurav Minocha | ef69d74 | 2014-09-05 09:56:13 -0700 | [diff] [blame] | 149 | if (!IS_ENABLED(CONFIG_SYSFS)) |
| 150 | return 0; |
| 151 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 152 | if (!of_kset || !of_node_is_attached(np)) |
| 153 | return 0; |
| 154 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 155 | sysfs_bin_attr_init(&pp->attr); |
| 156 | pp->attr.attr.name = safe_name(&np->kobj, pp->name); |
| 157 | pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO; |
| 158 | pp->attr.size = secure ? 0 : pp->length; |
| 159 | pp->attr.read = of_node_property_read; |
| 160 | |
| 161 | rc = sysfs_create_bin_file(&np->kobj, &pp->attr); |
| 162 | WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name); |
| 163 | return rc; |
| 164 | } |
| 165 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 166 | int __of_attach_node_sysfs(struct device_node *np) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 167 | { |
| 168 | const char *name; |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 169 | struct kobject *parent; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 170 | struct property *pp; |
| 171 | int rc; |
| 172 | |
Gaurav Minocha | ef69d74 | 2014-09-05 09:56:13 -0700 | [diff] [blame] | 173 | if (!IS_ENABLED(CONFIG_SYSFS)) |
| 174 | return 0; |
| 175 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 176 | if (!of_kset) |
| 177 | return 0; |
| 178 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 179 | np->kobj.kset = of_kset; |
| 180 | if (!np->parent) { |
| 181 | /* Nodes without parents are new top level trees */ |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 182 | name = safe_name(&of_kset->kobj, "base"); |
| 183 | parent = NULL; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 184 | } else { |
| 185 | name = safe_name(&np->parent->kobj, kbasename(np->full_name)); |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 186 | parent = &np->parent->kobj; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 187 | } |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 188 | if (!name) |
| 189 | return -ENOMEM; |
| 190 | rc = kobject_add(&np->kobj, parent, "%s", name); |
| 191 | kfree(name); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 192 | if (rc) |
| 193 | return rc; |
| 194 | |
| 195 | for_each_property_of_node(np, pp) |
| 196 | __of_add_property_sysfs(np, pp); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Frank Rowand | f64054b | 2018-02-23 14:23:10 +0530 | [diff] [blame^] | 201 | static struct device_node **phandle_cache; |
| 202 | static u32 phandle_cache_mask; |
| 203 | |
| 204 | /* |
| 205 | * Assumptions behind phandle_cache implementation: |
| 206 | * - phandle property values are in a contiguous range of 1..n |
| 207 | * |
| 208 | * If the assumptions do not hold, then |
| 209 | * - the phandle lookup overhead reduction provided by the cache |
| 210 | * will likely be less |
| 211 | */ |
| 212 | static void of_populate_phandle_cache(void) |
| 213 | { |
| 214 | unsigned long flags; |
| 215 | u32 cache_entries; |
| 216 | struct device_node *np; |
| 217 | u32 phandles = 0; |
| 218 | |
| 219 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 220 | |
| 221 | kfree(phandle_cache); |
| 222 | phandle_cache = NULL; |
| 223 | |
| 224 | for_each_of_allnodes(np) |
| 225 | if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) |
| 226 | phandles++; |
| 227 | |
| 228 | cache_entries = roundup_pow_of_two(phandles); |
| 229 | phandle_cache_mask = cache_entries - 1; |
| 230 | |
| 231 | phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache), |
| 232 | GFP_ATOMIC); |
| 233 | |
| 234 | if (phandle_cache) |
| 235 | for_each_of_allnodes(np) |
| 236 | if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) |
| 237 | phandle_cache[np->phandle & phandle_cache_mask] = np; |
| 238 | |
| 239 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 240 | } |
| 241 | |
| 242 | #ifndef CONFIG_MODULES |
| 243 | static int __init of_free_phandle_cache(void) |
| 244 | { |
| 245 | unsigned long flags; |
| 246 | |
| 247 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 248 | |
| 249 | kfree(phandle_cache); |
| 250 | phandle_cache = NULL; |
| 251 | |
| 252 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | late_initcall_sync(of_free_phandle_cache); |
| 257 | #endif |
| 258 | |
Sudeep Holla | 194ec93 | 2015-05-14 15:28:24 +0100 | [diff] [blame] | 259 | void __init of_core_init(void) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 260 | { |
| 261 | struct device_node *np; |
| 262 | |
Frank Rowand | f64054b | 2018-02-23 14:23:10 +0530 | [diff] [blame^] | 263 | of_populate_phandle_cache(); |
| 264 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 265 | /* Create the kset, and register existing nodes */ |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 266 | mutex_lock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 267 | of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj); |
| 268 | if (!of_kset) { |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 269 | mutex_unlock(&of_mutex); |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 270 | pr_err("failed to register existing nodes\n"); |
Sudeep Holla | 194ec93 | 2015-05-14 15:28:24 +0100 | [diff] [blame] | 271 | return; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 272 | } |
| 273 | for_each_of_allnodes(np) |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 274 | __of_attach_node_sysfs(np); |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 275 | mutex_unlock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 276 | |
Grant Likely | 8357041 | 2012-11-06 21:03:27 +0000 | [diff] [blame] | 277 | /* Symlink in /proc as required by userspace ABI */ |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 278 | if (of_root) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 279 | proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base"); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 280 | } |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 281 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 282 | static struct property *__of_find_property(const struct device_node *np, |
| 283 | const char *name, int *lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 284 | { |
| 285 | struct property *pp; |
| 286 | |
Timur Tabi | 64e4566 | 2008-05-08 05:19:59 +1000 | [diff] [blame] | 287 | if (!np) |
| 288 | return NULL; |
| 289 | |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 290 | for (pp = np->properties; pp; pp = pp->next) { |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 291 | if (of_prop_cmp(pp->name, name) == 0) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 292 | if (lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 293 | *lenp = pp->length; |
| 294 | break; |
| 295 | } |
| 296 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 297 | |
| 298 | return pp; |
| 299 | } |
| 300 | |
| 301 | struct property *of_find_property(const struct device_node *np, |
| 302 | const char *name, |
| 303 | int *lenp) |
| 304 | { |
| 305 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 306 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 307 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 308 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 309 | pp = __of_find_property(np, name, lenp); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 310 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 311 | |
| 312 | return pp; |
| 313 | } |
| 314 | EXPORT_SYMBOL(of_find_property); |
| 315 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 316 | struct device_node *__of_find_all_nodes(struct device_node *prev) |
| 317 | { |
| 318 | struct device_node *np; |
| 319 | if (!prev) { |
| 320 | np = of_root; |
| 321 | } else if (prev->child) { |
| 322 | np = prev->child; |
| 323 | } else { |
| 324 | /* Walk back up looking for a sibling, or the end of the structure */ |
| 325 | np = prev; |
| 326 | while (np->parent && !np->sibling) |
| 327 | np = np->parent; |
| 328 | np = np->sibling; /* Might be null at the end of the tree */ |
| 329 | } |
| 330 | return np; |
| 331 | } |
| 332 | |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 333 | /** |
| 334 | * of_find_all_nodes - Get next node in global list |
| 335 | * @prev: Previous node or NULL to start iteration |
| 336 | * of_node_put() will be called on it |
| 337 | * |
| 338 | * Returns a node pointer with refcount incremented, use |
| 339 | * of_node_put() on it when done. |
| 340 | */ |
| 341 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 342 | { |
| 343 | struct device_node *np; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 344 | unsigned long flags; |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 345 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 346 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 347 | np = __of_find_all_nodes(prev); |
| 348 | of_node_get(np); |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 349 | of_node_put(prev); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 350 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 351 | return np; |
| 352 | } |
| 353 | EXPORT_SYMBOL(of_find_all_nodes); |
| 354 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 355 | /* |
| 356 | * Find a property with a given name for a given node |
| 357 | * and return the value. |
| 358 | */ |
Grant Likely | a25095d | 2014-07-15 23:25:43 -0600 | [diff] [blame] | 359 | const void *__of_get_property(const struct device_node *np, |
| 360 | const char *name, int *lenp) |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 361 | { |
| 362 | struct property *pp = __of_find_property(np, name, lenp); |
| 363 | |
| 364 | return pp ? pp->value : NULL; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Find a property with a given name for a given node |
| 369 | * and return the value. |
| 370 | */ |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 371 | const void *of_get_property(const struct device_node *np, const char *name, |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 372 | int *lenp) |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 373 | { |
| 374 | struct property *pp = of_find_property(np, name, lenp); |
| 375 | |
| 376 | return pp ? pp->value : NULL; |
| 377 | } |
| 378 | EXPORT_SYMBOL(of_get_property); |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 379 | |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 380 | /* |
| 381 | * arch_match_cpu_phys_id - Match the given logical CPU and physical id |
| 382 | * |
| 383 | * @cpu: logical cpu index of a core/thread |
| 384 | * @phys_id: physical identifier of a core/thread |
| 385 | * |
| 386 | * CPU logical to physical index mapping is architecture specific. |
| 387 | * However this __weak function provides a default match of physical |
| 388 | * id to logical cpu index. phys_id provided here is usually values read |
| 389 | * from the device tree which must match the hardware internal registers. |
| 390 | * |
| 391 | * Returns true if the physical identifier and the logical cpu index |
| 392 | * correspond to the same core/thread, false otherwise. |
| 393 | */ |
| 394 | bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id) |
| 395 | { |
| 396 | return (u32)phys_id == cpu; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Checks if the given "prop_name" property holds the physical id of the |
| 401 | * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not |
| 402 | * NULL, local thread number within the core is returned in it. |
| 403 | */ |
| 404 | static bool __of_find_n_match_cpu_property(struct device_node *cpun, |
| 405 | const char *prop_name, int cpu, unsigned int *thread) |
| 406 | { |
| 407 | const __be32 *cell; |
| 408 | int ac, prop_len, tid; |
| 409 | u64 hwid; |
| 410 | |
| 411 | ac = of_n_addr_cells(cpun); |
| 412 | cell = of_get_property(cpun, prop_name, &prop_len); |
Grant Likely | f3cea45 | 2013-10-04 17:24:26 +0100 | [diff] [blame] | 413 | if (!cell || !ac) |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 414 | return false; |
Grant Likely | f3cea45 | 2013-10-04 17:24:26 +0100 | [diff] [blame] | 415 | prop_len /= sizeof(*cell) * ac; |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 416 | for (tid = 0; tid < prop_len; tid++) { |
| 417 | hwid = of_read_number(cell, ac); |
| 418 | if (arch_match_cpu_phys_id(cpu, hwid)) { |
| 419 | if (thread) |
| 420 | *thread = tid; |
| 421 | return true; |
| 422 | } |
| 423 | cell += ac; |
| 424 | } |
| 425 | return false; |
| 426 | } |
| 427 | |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 428 | /* |
| 429 | * arch_find_n_match_cpu_physical_id - See if the given device node is |
| 430 | * for the cpu corresponding to logical cpu 'cpu'. Return true if so, |
| 431 | * else false. If 'thread' is non-NULL, the local thread number within the |
| 432 | * core is returned in it. |
| 433 | */ |
| 434 | bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun, |
| 435 | int cpu, unsigned int *thread) |
| 436 | { |
| 437 | /* Check for non-standard "ibm,ppc-interrupt-server#s" property |
| 438 | * for thread ids on PowerPC. If it doesn't exist fallback to |
| 439 | * standard "reg" property. |
| 440 | */ |
| 441 | if (IS_ENABLED(CONFIG_PPC) && |
| 442 | __of_find_n_match_cpu_property(cpun, |
| 443 | "ibm,ppc-interrupt-server#s", |
| 444 | cpu, thread)) |
| 445 | return true; |
| 446 | |
Masahiro Yamada | 510bd06 | 2015-10-28 12:05:27 +0900 | [diff] [blame] | 447 | return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread); |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 448 | } |
| 449 | |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 450 | /** |
| 451 | * of_get_cpu_node - Get device node associated with the given logical CPU |
| 452 | * |
| 453 | * @cpu: CPU number(logical index) for which device node is required |
| 454 | * @thread: if not NULL, local thread number within the physical core is |
| 455 | * returned |
| 456 | * |
| 457 | * The main purpose of this function is to retrieve the device node for the |
| 458 | * given logical CPU index. It should be used to initialize the of_node in |
| 459 | * cpu device. Once of_node in cpu device is populated, all the further |
| 460 | * references can use that instead. |
| 461 | * |
| 462 | * CPU logical to physical index mapping is architecture specific and is built |
| 463 | * before booting secondary cores. This function uses arch_match_cpu_phys_id |
| 464 | * which can be overridden by architecture specific implementation. |
| 465 | * |
Masahiro Yamada | 1c986e3 | 2016-04-20 10:18:46 +0900 | [diff] [blame] | 466 | * Returns a node pointer for the logical cpu with refcount incremented, use |
| 467 | * of_node_put() on it when done. Returns NULL if not found. |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 468 | */ |
| 469 | struct device_node *of_get_cpu_node(int cpu, unsigned int *thread) |
| 470 | { |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 471 | struct device_node *cpun; |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 472 | |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 473 | for_each_node_by_type(cpun, "cpu") { |
| 474 | if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread)) |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 475 | return cpun; |
| 476 | } |
| 477 | return NULL; |
| 478 | } |
| 479 | EXPORT_SYMBOL(of_get_cpu_node); |
| 480 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 481 | /** |
| 482 | * __of_device_is_compatible() - Check if the node matches given constraints |
| 483 | * @device: pointer to node |
| 484 | * @compat: required compatible string, NULL or "" for any match |
| 485 | * @type: required device_type value, NULL or "" for any match |
| 486 | * @name: required node name, NULL or "" for any match |
| 487 | * |
| 488 | * Checks if the given @compat, @type and @name strings match the |
| 489 | * properties of the given @device. A constraints can be skipped by |
| 490 | * passing NULL or an empty string as the constraint. |
| 491 | * |
| 492 | * Returns 0 for no match, and a positive integer on match. The return |
| 493 | * value is a relative score with larger values indicating better |
| 494 | * matches. The score is weighted for the most specific compatible value |
| 495 | * to get the highest score. Matching type is next, followed by matching |
| 496 | * name. Practically speaking, this results in the following priority |
| 497 | * order for matches: |
| 498 | * |
| 499 | * 1. specific compatible && type && name |
| 500 | * 2. specific compatible && type |
| 501 | * 3. specific compatible && name |
| 502 | * 4. specific compatible |
| 503 | * 5. general compatible && type && name |
| 504 | * 6. general compatible && type |
| 505 | * 7. general compatible && name |
| 506 | * 8. general compatible |
| 507 | * 9. type && name |
| 508 | * 10. type |
| 509 | * 11. name |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 510 | */ |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 511 | static int __of_device_is_compatible(const struct device_node *device, |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 512 | const char *compat, const char *type, const char *name) |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 513 | { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 514 | struct property *prop; |
| 515 | const char *cp; |
| 516 | int index = 0, score = 0; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 517 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 518 | /* Compatible match has highest priority */ |
| 519 | if (compat && compat[0]) { |
| 520 | prop = __of_find_property(device, "compatible", NULL); |
| 521 | for (cp = of_prop_next_string(prop, NULL); cp; |
| 522 | cp = of_prop_next_string(prop, cp), index++) { |
| 523 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) { |
| 524 | score = INT_MAX/2 - (index << 2); |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | if (!score) |
| 529 | return 0; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 530 | } |
| 531 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 532 | /* Matching type is better than matching name */ |
| 533 | if (type && type[0]) { |
| 534 | if (!device->type || of_node_cmp(type, device->type)) |
| 535 | return 0; |
| 536 | score += 2; |
| 537 | } |
| 538 | |
| 539 | /* Matching name is a bit better than not */ |
| 540 | if (name && name[0]) { |
| 541 | if (!device->name || of_node_cmp(name, device->name)) |
| 542 | return 0; |
| 543 | score++; |
| 544 | } |
| 545 | |
| 546 | return score; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 547 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 548 | |
| 549 | /** Checks if the given "compat" string matches one of the strings in |
| 550 | * the device's "compatible" property |
| 551 | */ |
| 552 | int of_device_is_compatible(const struct device_node *device, |
| 553 | const char *compat) |
| 554 | { |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 555 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 556 | int res; |
| 557 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 558 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 559 | res = __of_device_is_compatible(device, compat, NULL, NULL); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 560 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 561 | return res; |
| 562 | } |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 563 | EXPORT_SYMBOL(of_device_is_compatible); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 564 | |
Benjamin Herrenschmidt | b9c13fe | 2016-07-08 08:35:59 +1000 | [diff] [blame] | 565 | /** Checks if the device is compatible with any of the entries in |
| 566 | * a NULL terminated array of strings. Returns the best match |
| 567 | * score or 0. |
| 568 | */ |
| 569 | int of_device_compatible_match(struct device_node *device, |
| 570 | const char *const *compat) |
| 571 | { |
| 572 | unsigned int tmp, score = 0; |
| 573 | |
| 574 | if (!compat) |
| 575 | return 0; |
| 576 | |
| 577 | while (*compat) { |
| 578 | tmp = of_device_is_compatible(device, *compat); |
| 579 | if (tmp > score) |
| 580 | score = tmp; |
| 581 | compat++; |
| 582 | } |
| 583 | |
| 584 | return score; |
| 585 | } |
| 586 | |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 587 | /** |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 588 | * of_machine_is_compatible - Test root of device tree for a given compatible value |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 589 | * @compat: compatible string to look for in root node's compatible property. |
| 590 | * |
Kevin Cernekee | 25c7a1d | 2014-11-12 12:54:00 -0800 | [diff] [blame] | 591 | * Returns a positive integer if the root node has the given value in its |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 592 | * compatible property. |
| 593 | */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 594 | int of_machine_is_compatible(const char *compat) |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 595 | { |
| 596 | struct device_node *root; |
| 597 | int rc = 0; |
| 598 | |
| 599 | root = of_find_node_by_path("/"); |
| 600 | if (root) { |
| 601 | rc = of_device_is_compatible(root, compat); |
| 602 | of_node_put(root); |
| 603 | } |
| 604 | return rc; |
| 605 | } |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 606 | EXPORT_SYMBOL(of_machine_is_compatible); |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 607 | |
| 608 | /** |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 609 | * __of_device_is_available - check if a device is available for use |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 610 | * |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 611 | * @device: Node to check for availability, with locks already held |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 612 | * |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 613 | * Returns true if the status property is absent or set to "okay" or "ok", |
| 614 | * false otherwise |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 615 | */ |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 616 | static bool __of_device_is_available(const struct device_node *device) |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 617 | { |
| 618 | const char *status; |
| 619 | int statlen; |
| 620 | |
Xiubo Li | 42ccd78 | 2014-01-13 11:07:28 +0800 | [diff] [blame] | 621 | if (!device) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 622 | return false; |
Xiubo Li | 42ccd78 | 2014-01-13 11:07:28 +0800 | [diff] [blame] | 623 | |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 624 | status = __of_get_property(device, "status", &statlen); |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 625 | if (status == NULL) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 626 | return true; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 627 | |
| 628 | if (statlen > 0) { |
| 629 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 630 | return true; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 631 | } |
| 632 | |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 633 | return false; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 634 | } |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 635 | |
| 636 | /** |
| 637 | * of_device_is_available - check if a device is available for use |
| 638 | * |
| 639 | * @device: Node to check for availability |
| 640 | * |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 641 | * Returns true if the status property is absent or set to "okay" or "ok", |
| 642 | * false otherwise |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 643 | */ |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 644 | bool of_device_is_available(const struct device_node *device) |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 645 | { |
| 646 | unsigned long flags; |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 647 | bool res; |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 648 | |
| 649 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 650 | res = __of_device_is_available(device); |
| 651 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 652 | return res; |
| 653 | |
| 654 | } |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 655 | EXPORT_SYMBOL(of_device_is_available); |
| 656 | |
| 657 | /** |
Kevin Cernekee | 37786c7 | 2015-04-09 13:05:14 -0700 | [diff] [blame] | 658 | * of_device_is_big_endian - check if a device has BE registers |
| 659 | * |
| 660 | * @device: Node to check for endianness |
| 661 | * |
| 662 | * Returns true if the device has a "big-endian" property, or if the kernel |
| 663 | * was compiled for BE *and* the device has a "native-endian" property. |
| 664 | * Returns false otherwise. |
| 665 | * |
| 666 | * Callers would nominally use ioread32be/iowrite32be if |
| 667 | * of_device_is_big_endian() == true, or readl/writel otherwise. |
| 668 | */ |
| 669 | bool of_device_is_big_endian(const struct device_node *device) |
| 670 | { |
| 671 | if (of_property_read_bool(device, "big-endian")) |
| 672 | return true; |
| 673 | if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && |
| 674 | of_property_read_bool(device, "native-endian")) |
| 675 | return true; |
| 676 | return false; |
| 677 | } |
| 678 | EXPORT_SYMBOL(of_device_is_big_endian); |
| 679 | |
| 680 | /** |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 681 | * of_get_parent - Get a node's parent if any |
| 682 | * @node: Node to get parent |
| 683 | * |
| 684 | * Returns a node pointer with refcount incremented, use |
| 685 | * of_node_put() on it when done. |
| 686 | */ |
| 687 | struct device_node *of_get_parent(const struct device_node *node) |
| 688 | { |
| 689 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 690 | unsigned long flags; |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 691 | |
| 692 | if (!node) |
| 693 | return NULL; |
| 694 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 695 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 696 | np = of_node_get(node->parent); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 697 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 698 | return np; |
| 699 | } |
| 700 | EXPORT_SYMBOL(of_get_parent); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 701 | |
| 702 | /** |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 703 | * of_get_next_parent - Iterate to a node's parent |
| 704 | * @node: Node to get parent of |
| 705 | * |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 706 | * This is like of_get_parent() except that it drops the |
| 707 | * refcount on the passed node, making it suitable for iterating |
| 708 | * through a node's parents. |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 709 | * |
| 710 | * Returns a node pointer with refcount incremented, use |
| 711 | * of_node_put() on it when done. |
| 712 | */ |
| 713 | struct device_node *of_get_next_parent(struct device_node *node) |
| 714 | { |
| 715 | struct device_node *parent; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 716 | unsigned long flags; |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 717 | |
| 718 | if (!node) |
| 719 | return NULL; |
| 720 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 721 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 722 | parent = of_node_get(node->parent); |
| 723 | of_node_put(node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 724 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 725 | return parent; |
| 726 | } |
Guennadi Liakhovetski | 6695be6 | 2013-04-02 12:28:11 -0300 | [diff] [blame] | 727 | EXPORT_SYMBOL(of_get_next_parent); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 728 | |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 729 | static struct device_node *__of_get_next_child(const struct device_node *node, |
| 730 | struct device_node *prev) |
| 731 | { |
| 732 | struct device_node *next; |
| 733 | |
Florian Fainelli | 43cb436 | 2014-05-28 10:39:02 -0700 | [diff] [blame] | 734 | if (!node) |
| 735 | return NULL; |
| 736 | |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 737 | next = prev ? prev->sibling : node->child; |
| 738 | for (; next; next = next->sibling) |
| 739 | if (of_node_get(next)) |
| 740 | break; |
| 741 | of_node_put(prev); |
| 742 | return next; |
| 743 | } |
| 744 | #define __for_each_child_of_node(parent, child) \ |
| 745 | for (child = __of_get_next_child(parent, NULL); child != NULL; \ |
| 746 | child = __of_get_next_child(parent, child)) |
| 747 | |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 748 | /** |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 749 | * of_get_next_child - Iterate a node childs |
| 750 | * @node: parent node |
| 751 | * @prev: previous child of the parent node, or NULL to get first |
| 752 | * |
Baruch Siach | 6480827 | 2015-03-19 14:03:41 +0200 | [diff] [blame] | 753 | * Returns a node pointer with refcount incremented, use of_node_put() on |
| 754 | * it when done. Returns NULL when prev is the last child. Decrements the |
| 755 | * refcount of prev. |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 756 | */ |
| 757 | struct device_node *of_get_next_child(const struct device_node *node, |
| 758 | struct device_node *prev) |
| 759 | { |
| 760 | struct device_node *next; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 761 | unsigned long flags; |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 762 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 763 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 764 | next = __of_get_next_child(node, prev); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 765 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 766 | return next; |
| 767 | } |
| 768 | EXPORT_SYMBOL(of_get_next_child); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 769 | |
| 770 | /** |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 771 | * of_get_next_available_child - Find the next available child node |
| 772 | * @node: parent node |
| 773 | * @prev: previous child of the parent node, or NULL to get first |
| 774 | * |
| 775 | * This function is like of_get_next_child(), except that it |
| 776 | * automatically skips any disabled nodes (i.e. status = "disabled"). |
| 777 | */ |
| 778 | struct device_node *of_get_next_available_child(const struct device_node *node, |
| 779 | struct device_node *prev) |
| 780 | { |
| 781 | struct device_node *next; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 782 | unsigned long flags; |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 783 | |
Florian Fainelli | 43cb436 | 2014-05-28 10:39:02 -0700 | [diff] [blame] | 784 | if (!node) |
| 785 | return NULL; |
| 786 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 787 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 788 | next = prev ? prev->sibling : node->child; |
| 789 | for (; next; next = next->sibling) { |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 790 | if (!__of_device_is_available(next)) |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 791 | continue; |
| 792 | if (of_node_get(next)) |
| 793 | break; |
| 794 | } |
| 795 | of_node_put(prev); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 796 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 797 | return next; |
| 798 | } |
| 799 | EXPORT_SYMBOL(of_get_next_available_child); |
| 800 | |
| 801 | /** |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 802 | * of_get_child_by_name - Find the child node by name for a given parent |
| 803 | * @node: parent node |
| 804 | * @name: child name to look for. |
| 805 | * |
| 806 | * This function looks for child node for given matching name |
| 807 | * |
| 808 | * Returns a node pointer if found, with refcount incremented, use |
| 809 | * of_node_put() on it when done. |
| 810 | * Returns NULL if node is not found. |
| 811 | */ |
| 812 | struct device_node *of_get_child_by_name(const struct device_node *node, |
| 813 | const char *name) |
| 814 | { |
| 815 | struct device_node *child; |
| 816 | |
| 817 | for_each_child_of_node(node, child) |
| 818 | if (child->name && (of_node_cmp(child->name, name) == 0)) |
| 819 | break; |
| 820 | return child; |
| 821 | } |
| 822 | EXPORT_SYMBOL(of_get_child_by_name); |
| 823 | |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 824 | static struct device_node *__of_find_node_by_path(struct device_node *parent, |
| 825 | const char *path) |
| 826 | { |
| 827 | struct device_node *child; |
Leif Lindholm | 106937e | 2015-03-06 16:52:53 +0000 | [diff] [blame] | 828 | int len; |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 829 | |
Brian Norris | 721a09e | 2015-03-17 12:30:31 -0700 | [diff] [blame] | 830 | len = strcspn(path, "/:"); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 831 | if (!len) |
| 832 | return NULL; |
| 833 | |
| 834 | __for_each_child_of_node(parent, child) { |
| 835 | const char *name = strrchr(child->full_name, '/'); |
| 836 | if (WARN(!name, "malformed device_node %s\n", child->full_name)) |
| 837 | continue; |
| 838 | name++; |
| 839 | if (strncmp(path, name, len) == 0 && (strlen(name) == len)) |
| 840 | return child; |
| 841 | } |
| 842 | return NULL; |
| 843 | } |
| 844 | |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 845 | /** |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 846 | * of_find_node_opts_by_path - Find a node matching a full OF path |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 847 | * @path: Either the full path to match, or if the path does not |
| 848 | * start with '/', the name of a property of the /aliases |
| 849 | * node (an alias). In the case of an alias, the node |
| 850 | * matching the alias' value will be returned. |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 851 | * @opts: Address of a pointer into which to store the start of |
| 852 | * an options string appended to the end of the path with |
| 853 | * a ':' separator. |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 854 | * |
| 855 | * Valid paths: |
| 856 | * /foo/bar Full path |
| 857 | * foo Valid alias |
| 858 | * foo/bar Valid alias + relative path |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 859 | * |
| 860 | * Returns a node pointer with refcount incremented, use |
| 861 | * of_node_put() on it when done. |
| 862 | */ |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 863 | struct device_node *of_find_node_opts_by_path(const char *path, const char **opts) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 864 | { |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 865 | struct device_node *np = NULL; |
| 866 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 867 | unsigned long flags; |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 868 | const char *separator = strchr(path, ':'); |
| 869 | |
| 870 | if (opts) |
| 871 | *opts = separator ? separator + 1 : NULL; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 872 | |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 873 | if (strcmp(path, "/") == 0) |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 874 | return of_node_get(of_root); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 875 | |
| 876 | /* The path could begin with an alias */ |
| 877 | if (*path != '/') { |
Leif Lindholm | 106937e | 2015-03-06 16:52:53 +0000 | [diff] [blame] | 878 | int len; |
| 879 | const char *p = separator; |
| 880 | |
| 881 | if (!p) |
| 882 | p = strchrnul(path, '/'); |
| 883 | len = p - path; |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 884 | |
| 885 | /* of_aliases must not be NULL */ |
| 886 | if (!of_aliases) |
| 887 | return NULL; |
| 888 | |
| 889 | for_each_property_of_node(of_aliases, pp) { |
| 890 | if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) { |
| 891 | np = of_find_node_by_path(pp->value); |
| 892 | break; |
| 893 | } |
| 894 | } |
| 895 | if (!np) |
| 896 | return NULL; |
| 897 | path = p; |
| 898 | } |
| 899 | |
| 900 | /* Step down the tree matching path components */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 901 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 902 | if (!np) |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 903 | np = of_node_get(of_root); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 904 | while (np && *path == '/') { |
| 905 | path++; /* Increment past '/' delimiter */ |
| 906 | np = __of_find_node_by_path(np, path); |
| 907 | path = strchrnul(path, '/'); |
Leif Lindholm | 106937e | 2015-03-06 16:52:53 +0000 | [diff] [blame] | 908 | if (separator && separator < path) |
| 909 | break; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 910 | } |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 911 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 912 | return np; |
| 913 | } |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 914 | EXPORT_SYMBOL(of_find_node_opts_by_path); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 915 | |
| 916 | /** |
| 917 | * of_find_node_by_name - Find a node by its "name" property |
| 918 | * @from: The node to start searching from or NULL, the node |
| 919 | * you pass will not be searched, only the next one |
| 920 | * will; typically, you pass what the previous call |
| 921 | * returned. of_node_put() will be called on it |
| 922 | * @name: The name string to match against |
| 923 | * |
| 924 | * Returns a node pointer with refcount incremented, use |
| 925 | * of_node_put() on it when done. |
| 926 | */ |
| 927 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 928 | const char *name) |
| 929 | { |
| 930 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 931 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 932 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 933 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 934 | for_each_of_allnodes_from(from, np) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 935 | if (np->name && (of_node_cmp(np->name, name) == 0) |
| 936 | && of_node_get(np)) |
| 937 | break; |
| 938 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 939 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 940 | return np; |
| 941 | } |
| 942 | EXPORT_SYMBOL(of_find_node_by_name); |
| 943 | |
| 944 | /** |
| 945 | * of_find_node_by_type - Find a node by its "device_type" property |
| 946 | * @from: The node to start searching from, or NULL to start searching |
| 947 | * the entire device tree. The node you pass will not be |
| 948 | * searched, only the next one will; typically, you pass |
| 949 | * what the previous call returned. of_node_put() will be |
| 950 | * called on from for you. |
| 951 | * @type: The type string to match against |
| 952 | * |
| 953 | * Returns a node pointer with refcount incremented, use |
| 954 | * of_node_put() on it when done. |
| 955 | */ |
| 956 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 957 | const char *type) |
| 958 | { |
| 959 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 960 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 961 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 962 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 963 | for_each_of_allnodes_from(from, np) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 964 | if (np->type && (of_node_cmp(np->type, type) == 0) |
| 965 | && of_node_get(np)) |
| 966 | break; |
| 967 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 968 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 969 | return np; |
| 970 | } |
| 971 | EXPORT_SYMBOL(of_find_node_by_type); |
| 972 | |
| 973 | /** |
| 974 | * of_find_compatible_node - Find a node based on type and one of the |
| 975 | * tokens in its "compatible" property |
| 976 | * @from: The node to start searching from or NULL, the node |
| 977 | * you pass will not be searched, only the next one |
| 978 | * will; typically, you pass what the previous call |
| 979 | * returned. of_node_put() will be called on it |
| 980 | * @type: The type string to match "device_type" or NULL to ignore |
| 981 | * @compatible: The string to match to one of the tokens in the device |
| 982 | * "compatible" list. |
| 983 | * |
| 984 | * Returns a node pointer with refcount incremented, use |
| 985 | * of_node_put() on it when done. |
| 986 | */ |
| 987 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 988 | const char *type, const char *compatible) |
| 989 | { |
| 990 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 991 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 992 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 993 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 994 | for_each_of_allnodes_from(from, np) |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 995 | if (__of_device_is_compatible(np, compatible, type, NULL) && |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 996 | of_node_get(np)) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 997 | break; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 998 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 999 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1000 | return np; |
| 1001 | } |
| 1002 | EXPORT_SYMBOL(of_find_compatible_node); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1003 | |
| 1004 | /** |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1005 | * of_find_node_with_property - Find a node which has a property with |
| 1006 | * the given name. |
| 1007 | * @from: The node to start searching from or NULL, the node |
| 1008 | * you pass will not be searched, only the next one |
| 1009 | * will; typically, you pass what the previous call |
| 1010 | * returned. of_node_put() will be called on it |
| 1011 | * @prop_name: The name of the property to look for. |
| 1012 | * |
| 1013 | * Returns a node pointer with refcount incremented, use |
| 1014 | * of_node_put() on it when done. |
| 1015 | */ |
| 1016 | struct device_node *of_find_node_with_property(struct device_node *from, |
| 1017 | const char *prop_name) |
| 1018 | { |
| 1019 | struct device_node *np; |
| 1020 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1021 | unsigned long flags; |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1022 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1023 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1024 | for_each_of_allnodes_from(from, np) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 1025 | for (pp = np->properties; pp; pp = pp->next) { |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1026 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
| 1027 | of_node_get(np); |
| 1028 | goto out; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | out: |
| 1033 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1034 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1035 | return np; |
| 1036 | } |
| 1037 | EXPORT_SYMBOL(of_find_node_with_property); |
| 1038 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1039 | static |
| 1040 | const struct of_device_id *__of_match_node(const struct of_device_id *matches, |
| 1041 | const struct device_node *node) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1042 | { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1043 | const struct of_device_id *best_match = NULL; |
| 1044 | int score, best_score = 0; |
| 1045 | |
Grant Likely | a52f07e | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 1046 | if (!matches) |
| 1047 | return NULL; |
| 1048 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1049 | for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) { |
| 1050 | score = __of_device_is_compatible(node, matches->compatible, |
| 1051 | matches->type, matches->name); |
| 1052 | if (score > best_score) { |
| 1053 | best_match = matches; |
| 1054 | best_score = score; |
| 1055 | } |
Kevin Hao | 4e8ca6e | 2014-02-14 13:22:45 +0800 | [diff] [blame] | 1056 | } |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1057 | |
| 1058 | return best_match; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1059 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1060 | |
| 1061 | /** |
Geert Uytterhoeven | c50949d | 2014-10-22 11:44:54 +0200 | [diff] [blame] | 1062 | * of_match_node - Tell if a device_node has a matching of_match structure |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1063 | * @matches: array of of device match structures to search in |
| 1064 | * @node: the of device structure to match against |
| 1065 | * |
Kevin Hao | 71c5498 | 2014-02-18 15:57:29 +0800 | [diff] [blame] | 1066 | * Low level utility function used by device matching. |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1067 | */ |
| 1068 | const struct of_device_id *of_match_node(const struct of_device_id *matches, |
| 1069 | const struct device_node *node) |
| 1070 | { |
| 1071 | const struct of_device_id *match; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1072 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1073 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1074 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1075 | match = __of_match_node(matches, node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1076 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1077 | return match; |
| 1078 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1079 | EXPORT_SYMBOL(of_match_node); |
| 1080 | |
| 1081 | /** |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1082 | * of_find_matching_node_and_match - Find a node based on an of_device_id |
| 1083 | * match table. |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1084 | * @from: The node to start searching from or NULL, the node |
| 1085 | * you pass will not be searched, only the next one |
| 1086 | * will; typically, you pass what the previous call |
| 1087 | * returned. of_node_put() will be called on it |
| 1088 | * @matches: array of of device match structures to search in |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1089 | * @match Updated to point at the matches entry which matched |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1090 | * |
| 1091 | * Returns a node pointer with refcount incremented, use |
| 1092 | * of_node_put() on it when done. |
| 1093 | */ |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1094 | struct device_node *of_find_matching_node_and_match(struct device_node *from, |
| 1095 | const struct of_device_id *matches, |
| 1096 | const struct of_device_id **match) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1097 | { |
| 1098 | struct device_node *np; |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1099 | const struct of_device_id *m; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1100 | unsigned long flags; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1101 | |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1102 | if (match) |
| 1103 | *match = NULL; |
| 1104 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1105 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1106 | for_each_of_allnodes_from(from, np) { |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1107 | m = __of_match_node(matches, np); |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1108 | if (m && of_node_get(np)) { |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1109 | if (match) |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1110 | *match = m; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1111 | break; |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1112 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1113 | } |
| 1114 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1115 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1116 | return np; |
| 1117 | } |
Grant Likely | 80c2022 | 2012-12-19 10:45:36 +0000 | [diff] [blame] | 1118 | EXPORT_SYMBOL(of_find_matching_node_and_match); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1119 | |
| 1120 | /** |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1121 | * of_modalias_node - Lookup appropriate modalias for a device node |
| 1122 | * @node: pointer to a device tree node |
| 1123 | * @modalias: Pointer to buffer that modalias value will be copied into |
| 1124 | * @len: Length of modalias value |
| 1125 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1126 | * Based on the value of the compatible property, this routine will attempt |
| 1127 | * to choose an appropriate modalias value for a particular device tree node. |
| 1128 | * It does this by stripping the manufacturer prefix (as delimited by a ',') |
| 1129 | * from the first entry in the compatible list property. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1130 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1131 | * This routine returns 0 on success, <0 on failure. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1132 | */ |
| 1133 | int of_modalias_node(struct device_node *node, char *modalias, int len) |
| 1134 | { |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1135 | const char *compatible, *p; |
| 1136 | int cplen; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1137 | |
| 1138 | compatible = of_get_property(node, "compatible", &cplen); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1139 | if (!compatible || strlen(compatible) > cplen) |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1140 | return -ENODEV; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1141 | p = strchr(compatible, ','); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1142 | strlcpy(modalias, p ? p + 1 : compatible, len); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
| 1145 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 1146 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1147 | /** |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1148 | * of_find_node_by_phandle - Find a node given a phandle |
| 1149 | * @handle: phandle of the node to find |
| 1150 | * |
| 1151 | * Returns a node pointer with refcount incremented, use |
| 1152 | * of_node_put() on it when done. |
| 1153 | */ |
| 1154 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 1155 | { |
Frank Rowand | f64054b | 2018-02-23 14:23:10 +0530 | [diff] [blame^] | 1156 | struct device_node *np = NULL; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1157 | unsigned long flags; |
Frank Rowand | f64054b | 2018-02-23 14:23:10 +0530 | [diff] [blame^] | 1158 | phandle masked_handle; |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1159 | |
Grant Likely | fc59b44 | 2014-10-02 13:08:02 +0100 | [diff] [blame] | 1160 | if (!handle) |
| 1161 | return NULL; |
| 1162 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1163 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Frank Rowand | f64054b | 2018-02-23 14:23:10 +0530 | [diff] [blame^] | 1164 | |
| 1165 | masked_handle = handle & phandle_cache_mask; |
| 1166 | |
| 1167 | if (phandle_cache) { |
| 1168 | if (phandle_cache[masked_handle] && |
| 1169 | handle == phandle_cache[masked_handle]->phandle) |
| 1170 | np = phandle_cache[masked_handle]; |
| 1171 | } |
| 1172 | |
| 1173 | if (!np) { |
| 1174 | for_each_of_allnodes(np) |
| 1175 | if (np->phandle == handle) { |
| 1176 | if (phandle_cache) |
| 1177 | phandle_cache[masked_handle] = np; |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1182 | of_node_get(np); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1183 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1184 | return np; |
| 1185 | } |
| 1186 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 1187 | |
| 1188 | /** |
Heiko Stuebner | ad54a0c | 2014-02-12 01:00:34 +0100 | [diff] [blame] | 1189 | * of_property_count_elems_of_size - Count the number of elements in a property |
| 1190 | * |
| 1191 | * @np: device node from which the property value is to be read. |
| 1192 | * @propname: name of the property to be searched. |
| 1193 | * @elem_size: size of the individual element |
| 1194 | * |
| 1195 | * Search for a property in a device node and count the number of elements of |
| 1196 | * size elem_size in it. Returns number of elements on sucess, -EINVAL if the |
| 1197 | * property does not exist or its length does not match a multiple of elem_size |
| 1198 | * and -ENODATA if the property does not have a value. |
| 1199 | */ |
| 1200 | int of_property_count_elems_of_size(const struct device_node *np, |
| 1201 | const char *propname, int elem_size) |
| 1202 | { |
| 1203 | struct property *prop = of_find_property(np, propname, NULL); |
| 1204 | |
| 1205 | if (!prop) |
| 1206 | return -EINVAL; |
| 1207 | if (!prop->value) |
| 1208 | return -ENODATA; |
| 1209 | |
| 1210 | if (prop->length % elem_size != 0) { |
| 1211 | pr_err("size of %s in node %s is not a multiple of %d\n", |
| 1212 | propname, np->full_name, elem_size); |
| 1213 | return -EINVAL; |
| 1214 | } |
| 1215 | |
| 1216 | return prop->length / elem_size; |
| 1217 | } |
| 1218 | EXPORT_SYMBOL_GPL(of_property_count_elems_of_size); |
| 1219 | |
| 1220 | /** |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1221 | * of_find_property_value_of_size |
| 1222 | * |
| 1223 | * @np: device node from which the property value is to be read. |
| 1224 | * @propname: name of the property to be searched. |
Richard Fitzgerald | 79ac5d3 | 2016-09-12 14:01:28 +0100 | [diff] [blame] | 1225 | * @min: minimum allowed length of property value |
| 1226 | * @max: maximum allowed length of property value (0 means unlimited) |
| 1227 | * @len: if !=NULL, actual length is written to here |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1228 | * |
| 1229 | * Search for a property in a device node and valid the requested size. |
| 1230 | * Returns the property value on success, -EINVAL if the property does not |
| 1231 | * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the |
Richard Fitzgerald | 79ac5d3 | 2016-09-12 14:01:28 +0100 | [diff] [blame] | 1232 | * property data is too small or too large. |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1233 | * |
| 1234 | */ |
| 1235 | static void *of_find_property_value_of_size(const struct device_node *np, |
Richard Fitzgerald | 79ac5d3 | 2016-09-12 14:01:28 +0100 | [diff] [blame] | 1236 | const char *propname, u32 min, u32 max, size_t *len) |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1237 | { |
| 1238 | struct property *prop = of_find_property(np, propname, NULL); |
| 1239 | |
| 1240 | if (!prop) |
| 1241 | return ERR_PTR(-EINVAL); |
| 1242 | if (!prop->value) |
| 1243 | return ERR_PTR(-ENODATA); |
Richard Fitzgerald | 79ac5d3 | 2016-09-12 14:01:28 +0100 | [diff] [blame] | 1244 | if (prop->length < min) |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1245 | return ERR_PTR(-EOVERFLOW); |
Richard Fitzgerald | 79ac5d3 | 2016-09-12 14:01:28 +0100 | [diff] [blame] | 1246 | if (max && prop->length > max) |
| 1247 | return ERR_PTR(-EOVERFLOW); |
| 1248 | |
| 1249 | if (len) |
| 1250 | *len = prop->length; |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1251 | |
| 1252 | return prop->value; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 1256 | * of_property_read_u32_index - Find and read a u32 from a multi-value property. |
| 1257 | * |
| 1258 | * @np: device node from which the property value is to be read. |
| 1259 | * @propname: name of the property to be searched. |
| 1260 | * @index: index of the u32 in the list of values |
| 1261 | * @out_value: pointer to return value, modified only if no error. |
| 1262 | * |
| 1263 | * Search for a property in a device node and read nth 32-bit value from |
| 1264 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 1265 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 1266 | * property data isn't large enough. |
| 1267 | * |
| 1268 | * The out_value is modified only if a valid u32 value can be decoded. |
| 1269 | */ |
| 1270 | int of_property_read_u32_index(const struct device_node *np, |
| 1271 | const char *propname, |
| 1272 | u32 index, u32 *out_value) |
| 1273 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1274 | const u32 *val = of_find_property_value_of_size(np, propname, |
Richard Fitzgerald | 79ac5d3 | 2016-09-12 14:01:28 +0100 | [diff] [blame] | 1275 | ((index + 1) * sizeof(*out_value)), |
| 1276 | 0, |
| 1277 | NULL); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 1278 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1279 | if (IS_ERR(val)) |
| 1280 | return PTR_ERR(val); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 1281 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1282 | *out_value = be32_to_cpup(((__be32 *)val) + index); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 1283 | return 0; |
| 1284 | } |
| 1285 | EXPORT_SYMBOL_GPL(of_property_read_u32_index); |
| 1286 | |
| 1287 | /** |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1288 | * of_property_read_variable_u8_array - Find and read an array of u8 from a |
| 1289 | * property, with bounds on the minimum and maximum array size. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1290 | * |
| 1291 | * @np: device node from which the property value is to be read. |
| 1292 | * @propname: name of the property to be searched. |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1293 | * @out_values: pointer to return value, modified only if return value is 0. |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1294 | * @sz_min: minimum number of array elements to read |
| 1295 | * @sz_max: maximum number of array elements to read, if zero there is no |
| 1296 | * upper limit on the number of elements in the dts entry but only |
| 1297 | * sz_min will be read. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1298 | * |
| 1299 | * Search for a property in a device node and read 8-bit value(s) from |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1300 | * it. Returns number of elements read on success, -EINVAL if the property |
| 1301 | * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW |
| 1302 | * if the property data is smaller than sz_min or longer than sz_max. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1303 | * |
| 1304 | * dts entry of array should be like: |
| 1305 | * property = /bits/ 8 <0x50 0x60 0x70>; |
| 1306 | * |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1307 | * The out_values is modified only if a valid u8 value can be decoded. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1308 | */ |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1309 | int of_property_read_variable_u8_array(const struct device_node *np, |
| 1310 | const char *propname, u8 *out_values, |
| 1311 | size_t sz_min, size_t sz_max) |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1312 | { |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1313 | size_t sz, count; |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1314 | const u8 *val = of_find_property_value_of_size(np, propname, |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1315 | (sz_min * sizeof(*out_values)), |
| 1316 | (sz_max * sizeof(*out_values)), |
| 1317 | &sz); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1318 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1319 | if (IS_ERR(val)) |
| 1320 | return PTR_ERR(val); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1321 | |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1322 | if (!sz_max) |
| 1323 | sz = sz_min; |
| 1324 | else |
| 1325 | sz /= sizeof(*out_values); |
| 1326 | |
| 1327 | count = sz; |
| 1328 | while (count--) |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1329 | *out_values++ = *val++; |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1330 | |
| 1331 | return sz; |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1332 | } |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1333 | EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1334 | |
| 1335 | /** |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1336 | * of_property_read_variable_u16_array - Find and read an array of u16 from a |
| 1337 | * property, with bounds on the minimum and maximum array size. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1338 | * |
| 1339 | * @np: device node from which the property value is to be read. |
| 1340 | * @propname: name of the property to be searched. |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1341 | * @out_values: pointer to return value, modified only if return value is 0. |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1342 | * @sz_min: minimum number of array elements to read |
| 1343 | * @sz_max: maximum number of array elements to read, if zero there is no |
| 1344 | * upper limit on the number of elements in the dts entry but only |
| 1345 | * sz_min will be read. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1346 | * |
| 1347 | * Search for a property in a device node and read 16-bit value(s) from |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1348 | * it. Returns number of elements read on success, -EINVAL if the property |
| 1349 | * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW |
| 1350 | * if the property data is smaller than sz_min or longer than sz_max. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1351 | * |
| 1352 | * dts entry of array should be like: |
| 1353 | * property = /bits/ 16 <0x5000 0x6000 0x7000>; |
| 1354 | * |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1355 | * The out_values is modified only if a valid u16 value can be decoded. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1356 | */ |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1357 | int of_property_read_variable_u16_array(const struct device_node *np, |
| 1358 | const char *propname, u16 *out_values, |
| 1359 | size_t sz_min, size_t sz_max) |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1360 | { |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1361 | size_t sz, count; |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1362 | const __be16 *val = of_find_property_value_of_size(np, propname, |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1363 | (sz_min * sizeof(*out_values)), |
| 1364 | (sz_max * sizeof(*out_values)), |
| 1365 | &sz); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1366 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1367 | if (IS_ERR(val)) |
| 1368 | return PTR_ERR(val); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1369 | |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1370 | if (!sz_max) |
| 1371 | sz = sz_min; |
| 1372 | else |
| 1373 | sz /= sizeof(*out_values); |
| 1374 | |
| 1375 | count = sz; |
| 1376 | while (count--) |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1377 | *out_values++ = be16_to_cpup(val++); |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1378 | |
| 1379 | return sz; |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1380 | } |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1381 | EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1382 | |
| 1383 | /** |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1384 | * of_property_read_variable_u32_array - Find and read an array of 32 bit |
| 1385 | * integers from a property, with bounds on the minimum and maximum array size. |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1386 | * |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1387 | * @np: device node from which the property value is to be read. |
| 1388 | * @propname: name of the property to be searched. |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1389 | * @out_values: pointer to return value, modified only if return value is 0. |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1390 | * @sz_min: minimum number of array elements to read |
| 1391 | * @sz_max: maximum number of array elements to read, if zero there is no |
| 1392 | * upper limit on the number of elements in the dts entry but only |
| 1393 | * sz_min will be read. |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1394 | * |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1395 | * Search for a property in a device node and read 32-bit value(s) from |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1396 | * it. Returns number of elements read on success, -EINVAL if the property |
| 1397 | * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW |
| 1398 | * if the property data is smaller than sz_min or longer than sz_max. |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1399 | * |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1400 | * The out_values is modified only if a valid u32 value can be decoded. |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1401 | */ |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1402 | int of_property_read_variable_u32_array(const struct device_node *np, |
Jamie Iles | aac285c | 2011-08-02 15:45:07 +0100 | [diff] [blame] | 1403 | const char *propname, u32 *out_values, |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1404 | size_t sz_min, size_t sz_max) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1405 | { |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1406 | size_t sz, count; |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1407 | const __be32 *val = of_find_property_value_of_size(np, propname, |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1408 | (sz_min * sizeof(*out_values)), |
| 1409 | (sz_max * sizeof(*out_values)), |
| 1410 | &sz); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1411 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1412 | if (IS_ERR(val)) |
| 1413 | return PTR_ERR(val); |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1414 | |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1415 | if (!sz_max) |
| 1416 | sz = sz_min; |
| 1417 | else |
| 1418 | sz /= sizeof(*out_values); |
| 1419 | |
| 1420 | count = sz; |
| 1421 | while (count--) |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1422 | *out_values++ = be32_to_cpup(val++); |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1423 | |
| 1424 | return sz; |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1425 | } |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1426 | EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1427 | |
| 1428 | /** |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 1429 | * of_property_read_u64 - Find and read a 64 bit integer from a property |
| 1430 | * @np: device node from which the property value is to be read. |
| 1431 | * @propname: name of the property to be searched. |
| 1432 | * @out_value: pointer to return value, modified only if return value is 0. |
| 1433 | * |
| 1434 | * Search for a property in a device node and read a 64-bit value from |
| 1435 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 1436 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 1437 | * property data isn't large enough. |
| 1438 | * |
| 1439 | * The out_value is modified only if a valid u64 value can be decoded. |
| 1440 | */ |
| 1441 | int of_property_read_u64(const struct device_node *np, const char *propname, |
| 1442 | u64 *out_value) |
| 1443 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1444 | const __be32 *val = of_find_property_value_of_size(np, propname, |
Richard Fitzgerald | 79ac5d3 | 2016-09-12 14:01:28 +0100 | [diff] [blame] | 1445 | sizeof(*out_value), |
| 1446 | 0, |
| 1447 | NULL); |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 1448 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1449 | if (IS_ERR(val)) |
| 1450 | return PTR_ERR(val); |
| 1451 | |
| 1452 | *out_value = of_read_number(val, 2); |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | EXPORT_SYMBOL_GPL(of_property_read_u64); |
| 1456 | |
| 1457 | /** |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1458 | * of_property_read_variable_u64_array - Find and read an array of 64 bit |
| 1459 | * integers from a property, with bounds on the minimum and maximum array size. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1460 | * |
| 1461 | * @np: device node from which the property value is to be read. |
| 1462 | * @propname: name of the property to be searched. |
| 1463 | * @out_values: pointer to return value, modified only if return value is 0. |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1464 | * @sz_min: minimum number of array elements to read |
| 1465 | * @sz_max: maximum number of array elements to read, if zero there is no |
| 1466 | * upper limit on the number of elements in the dts entry but only |
| 1467 | * sz_min will be read. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1468 | * |
| 1469 | * Search for a property in a device node and read 64-bit value(s) from |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1470 | * it. Returns number of elements read on success, -EINVAL if the property |
| 1471 | * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW |
| 1472 | * if the property data is smaller than sz_min or longer than sz_max. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1473 | * |
| 1474 | * The out_values is modified only if a valid u64 value can be decoded. |
| 1475 | */ |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1476 | int of_property_read_variable_u64_array(const struct device_node *np, |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1477 | const char *propname, u64 *out_values, |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1478 | size_t sz_min, size_t sz_max) |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1479 | { |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1480 | size_t sz, count; |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1481 | const __be32 *val = of_find_property_value_of_size(np, propname, |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1482 | (sz_min * sizeof(*out_values)), |
| 1483 | (sz_max * sizeof(*out_values)), |
| 1484 | &sz); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1485 | |
| 1486 | if (IS_ERR(val)) |
| 1487 | return PTR_ERR(val); |
| 1488 | |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1489 | if (!sz_max) |
| 1490 | sz = sz_min; |
| 1491 | else |
| 1492 | sz /= sizeof(*out_values); |
| 1493 | |
| 1494 | count = sz; |
| 1495 | while (count--) { |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1496 | *out_values++ = of_read_number(val, 2); |
| 1497 | val += 2; |
| 1498 | } |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1499 | |
| 1500 | return sz; |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1501 | } |
Richard Fitzgerald | a67e947 | 2016-09-12 14:01:29 +0100 | [diff] [blame] | 1502 | EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1503 | |
| 1504 | /** |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1505 | * of_property_read_string - Find and read a string from a property |
| 1506 | * @np: device node from which the property value is to be read. |
| 1507 | * @propname: name of the property to be searched. |
| 1508 | * @out_string: pointer to null terminated return string, modified only if |
| 1509 | * return value is 0. |
| 1510 | * |
| 1511 | * Search for a property in a device tree node and retrieve a null |
| 1512 | * terminated string value (pointer to data, not a copy). Returns 0 on |
| 1513 | * success, -EINVAL if the property does not exist, -ENODATA if property |
| 1514 | * does not have a value, and -EILSEQ if the string is not null-terminated |
| 1515 | * within the length of the property data. |
| 1516 | * |
| 1517 | * The out_string pointer is modified only if a valid string can be decoded. |
| 1518 | */ |
David Rivshin | fe99c70 | 2016-03-02 16:35:51 -0500 | [diff] [blame] | 1519 | int of_property_read_string(const struct device_node *np, const char *propname, |
Shawn Guo | f09bc83 | 2011-07-04 09:01:18 +0800 | [diff] [blame] | 1520 | const char **out_string) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1521 | { |
David Rivshin | fe99c70 | 2016-03-02 16:35:51 -0500 | [diff] [blame] | 1522 | const struct property *prop = of_find_property(np, propname, NULL); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1523 | if (!prop) |
| 1524 | return -EINVAL; |
| 1525 | if (!prop->value) |
| 1526 | return -ENODATA; |
| 1527 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 1528 | return -EILSEQ; |
| 1529 | *out_string = prop->value; |
| 1530 | return 0; |
| 1531 | } |
| 1532 | EXPORT_SYMBOL_GPL(of_property_read_string); |
| 1533 | |
| 1534 | /** |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 1535 | * of_property_match_string() - Find string in a list and return index |
| 1536 | * @np: pointer to node containing string list property |
| 1537 | * @propname: string list property name |
| 1538 | * @string: pointer to string to search for in string list |
| 1539 | * |
| 1540 | * This function searches a string list property and returns the index |
| 1541 | * of a specific string value. |
| 1542 | */ |
David Rivshin | fe99c70 | 2016-03-02 16:35:51 -0500 | [diff] [blame] | 1543 | int of_property_match_string(const struct device_node *np, const char *propname, |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 1544 | const char *string) |
| 1545 | { |
David Rivshin | fe99c70 | 2016-03-02 16:35:51 -0500 | [diff] [blame] | 1546 | const struct property *prop = of_find_property(np, propname, NULL); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 1547 | size_t l; |
| 1548 | int i; |
| 1549 | const char *p, *end; |
| 1550 | |
| 1551 | if (!prop) |
| 1552 | return -EINVAL; |
| 1553 | if (!prop->value) |
| 1554 | return -ENODATA; |
| 1555 | |
| 1556 | p = prop->value; |
| 1557 | end = p + prop->length; |
| 1558 | |
| 1559 | for (i = 0; p < end; i++, p += l) { |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1560 | l = strnlen(p, end - p) + 1; |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 1561 | if (p + l > end) |
| 1562 | return -EILSEQ; |
| 1563 | pr_debug("comparing %s with %s\n", string, p); |
| 1564 | if (strcmp(string, p) == 0) |
| 1565 | return i; /* Found it; return index */ |
| 1566 | } |
| 1567 | return -ENODATA; |
| 1568 | } |
| 1569 | EXPORT_SYMBOL_GPL(of_property_match_string); |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1570 | |
| 1571 | /** |
Jiri Slaby | e99010e | 2014-11-21 13:23:09 +0100 | [diff] [blame] | 1572 | * of_property_read_string_helper() - Utility helper for parsing string properties |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1573 | * @np: device node from which the property value is to be read. |
| 1574 | * @propname: name of the property to be searched. |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1575 | * @out_strs: output array of string pointers. |
| 1576 | * @sz: number of array elements to read. |
| 1577 | * @skip: Number of strings to skip over at beginning of list. |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1578 | * |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1579 | * Don't call this function directly. It is a utility helper for the |
| 1580 | * of_property_read_string*() family of functions. |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1581 | */ |
David Rivshin | fe99c70 | 2016-03-02 16:35:51 -0500 | [diff] [blame] | 1582 | int of_property_read_string_helper(const struct device_node *np, |
| 1583 | const char *propname, const char **out_strs, |
| 1584 | size_t sz, int skip) |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1585 | { |
David Rivshin | fe99c70 | 2016-03-02 16:35:51 -0500 | [diff] [blame] | 1586 | const struct property *prop = of_find_property(np, propname, NULL); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1587 | int l = 0, i = 0; |
| 1588 | const char *p, *end; |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1589 | |
| 1590 | if (!prop) |
| 1591 | return -EINVAL; |
| 1592 | if (!prop->value) |
| 1593 | return -ENODATA; |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1594 | p = prop->value; |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1595 | end = p + prop->length; |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1596 | |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1597 | for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) { |
| 1598 | l = strnlen(p, end - p) + 1; |
| 1599 | if (p + l > end) |
| 1600 | return -EILSEQ; |
| 1601 | if (out_strs && i >= skip) |
| 1602 | *out_strs++ = p; |
| 1603 | } |
| 1604 | i -= skip; |
| 1605 | return i <= 0 ? -ENODATA : i; |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1606 | } |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1607 | EXPORT_SYMBOL_GPL(of_property_read_string_helper); |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1608 | |
Grant Likely | 624cfca | 2013-10-11 22:05:10 +0100 | [diff] [blame] | 1609 | void of_print_phandle_args(const char *msg, const struct of_phandle_args *args) |
| 1610 | { |
| 1611 | int i; |
| 1612 | printk("%s %s", msg, of_node_full_name(args->np)); |
| 1613 | for (i = 0; i < args->args_count; i++) |
| 1614 | printk(i ? ",%08x" : ":%08x", args->args[i]); |
| 1615 | printk("\n"); |
| 1616 | } |
| 1617 | |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1618 | int of_phandle_iterator_init(struct of_phandle_iterator *it, |
| 1619 | const struct device_node *np, |
| 1620 | const char *list_name, |
| 1621 | const char *cells_name, |
| 1622 | int cell_count) |
| 1623 | { |
| 1624 | const __be32 *list; |
| 1625 | int size; |
| 1626 | |
| 1627 | memset(it, 0, sizeof(*it)); |
| 1628 | |
| 1629 | list = of_get_property(np, list_name, &size); |
| 1630 | if (!list) |
| 1631 | return -ENOENT; |
| 1632 | |
| 1633 | it->cells_name = cells_name; |
| 1634 | it->cell_count = cell_count; |
| 1635 | it->parent = np; |
| 1636 | it->list_end = list + size / sizeof(*list); |
| 1637 | it->phandle_end = list; |
| 1638 | it->cur = list; |
| 1639 | |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1643 | int of_phandle_iterator_next(struct of_phandle_iterator *it) |
| 1644 | { |
| 1645 | uint32_t count = 0; |
| 1646 | |
| 1647 | if (it->node) { |
| 1648 | of_node_put(it->node); |
| 1649 | it->node = NULL; |
| 1650 | } |
| 1651 | |
| 1652 | if (!it->cur || it->phandle_end >= it->list_end) |
| 1653 | return -ENOENT; |
| 1654 | |
| 1655 | it->cur = it->phandle_end; |
| 1656 | |
| 1657 | /* If phandle is 0, then it is an empty entry with no arguments. */ |
| 1658 | it->phandle = be32_to_cpup(it->cur++); |
| 1659 | |
| 1660 | if (it->phandle) { |
| 1661 | |
| 1662 | /* |
| 1663 | * Find the provider node and parse the #*-cells property to |
| 1664 | * determine the argument length. |
| 1665 | */ |
| 1666 | it->node = of_find_node_by_phandle(it->phandle); |
| 1667 | |
| 1668 | if (it->cells_name) { |
| 1669 | if (!it->node) { |
| 1670 | pr_err("%s: could not find phandle\n", |
| 1671 | it->parent->full_name); |
| 1672 | goto err; |
| 1673 | } |
| 1674 | |
| 1675 | if (of_property_read_u32(it->node, it->cells_name, |
| 1676 | &count)) { |
| 1677 | pr_err("%s: could not get %s for %s\n", |
| 1678 | it->parent->full_name, |
| 1679 | it->cells_name, |
| 1680 | it->node->full_name); |
| 1681 | goto err; |
| 1682 | } |
| 1683 | } else { |
| 1684 | count = it->cell_count; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Make sure that the arguments actually fit in the remaining |
| 1689 | * property data length |
| 1690 | */ |
| 1691 | if (it->cur + count > it->list_end) { |
| 1692 | pr_err("%s: arguments longer than property\n", |
| 1693 | it->parent->full_name); |
| 1694 | goto err; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | it->phandle_end = it->cur + count; |
| 1699 | it->cur_count = count; |
| 1700 | |
| 1701 | return 0; |
| 1702 | |
| 1703 | err: |
| 1704 | if (it->node) { |
| 1705 | of_node_put(it->node); |
| 1706 | it->node = NULL; |
| 1707 | } |
| 1708 | |
| 1709 | return -EINVAL; |
| 1710 | } |
| 1711 | |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1712 | int of_phandle_iterator_args(struct of_phandle_iterator *it, |
| 1713 | uint32_t *args, |
| 1714 | int size) |
| 1715 | { |
| 1716 | int i, count; |
| 1717 | |
| 1718 | count = it->cur_count; |
| 1719 | |
| 1720 | if (WARN_ON(size < count)) |
| 1721 | count = size; |
| 1722 | |
| 1723 | for (i = 0; i < count; i++) |
| 1724 | args[i] = be32_to_cpup(it->cur++); |
| 1725 | |
| 1726 | return count; |
| 1727 | } |
| 1728 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1729 | static int __of_parse_phandle_with_args(const struct device_node *np, |
| 1730 | const char *list_name, |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1731 | const char *cells_name, |
| 1732 | int cell_count, int index, |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1733 | struct of_phandle_args *out_args) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1734 | { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1735 | struct of_phandle_iterator it; |
| 1736 | int rc, cur_index = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1737 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1738 | /* Loop over the phandles until all the requested entry is found */ |
Joerg Roedel | f623ce9 | 2016-04-04 17:49:20 +0200 | [diff] [blame] | 1739 | of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) { |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1740 | /* |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1741 | * All of the error cases bail out of the loop, so at |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1742 | * this point, the parsing is successful. If the requested |
| 1743 | * index matches, then fill the out_args structure and return, |
| 1744 | * or return -ENOENT for an empty entry. |
| 1745 | */ |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1746 | rc = -ENOENT; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1747 | if (cur_index == index) { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1748 | if (!it.phandle) |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1749 | goto err; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1750 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1751 | if (out_args) { |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1752 | int c; |
| 1753 | |
| 1754 | c = of_phandle_iterator_args(&it, |
| 1755 | out_args->args, |
| 1756 | MAX_PHANDLE_ARGS); |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1757 | out_args->np = it.node; |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1758 | out_args->args_count = c; |
Tang Yuantian | b855f16 | 2013-04-10 11:36:39 +0800 | [diff] [blame] | 1759 | } else { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1760 | of_node_put(it.node); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1761 | } |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1762 | |
| 1763 | /* Found it! return success */ |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1764 | return 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1765 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1766 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1767 | cur_index++; |
| 1768 | } |
| 1769 | |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1770 | /* |
| 1771 | * Unlock node before returning result; will be one of: |
| 1772 | * -ENOENT : index is for empty phandle |
| 1773 | * -EINVAL : parsing error on data |
| 1774 | */ |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1775 | |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1776 | err: |
Markus Elfring | beab47d | 2016-07-19 22:42:22 +0200 | [diff] [blame] | 1777 | of_node_put(it.node); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1778 | return rc; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1779 | } |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1780 | |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1781 | /** |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1782 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
| 1783 | * @np: Pointer to device node holding phandle property |
| 1784 | * @phandle_name: Name of property holding a phandle value |
| 1785 | * @index: For properties holding a table of phandles, this is the index into |
| 1786 | * the table |
| 1787 | * |
| 1788 | * Returns the device_node pointer with refcount incremented. Use |
| 1789 | * of_node_put() on it when done. |
| 1790 | */ |
| 1791 | struct device_node *of_parse_phandle(const struct device_node *np, |
| 1792 | const char *phandle_name, int index) |
| 1793 | { |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1794 | struct of_phandle_args args; |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1795 | |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1796 | if (index < 0) |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1797 | return NULL; |
| 1798 | |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1799 | if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, |
| 1800 | index, &args)) |
| 1801 | return NULL; |
| 1802 | |
| 1803 | return args.np; |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1804 | } |
| 1805 | EXPORT_SYMBOL(of_parse_phandle); |
| 1806 | |
| 1807 | /** |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1808 | * of_parse_phandle_with_args() - Find a node pointed by phandle in a list |
| 1809 | * @np: pointer to a device tree node containing a list |
| 1810 | * @list_name: property name that contains a list |
| 1811 | * @cells_name: property name that specifies phandles' arguments count |
| 1812 | * @index: index of a phandle to parse out |
| 1813 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 1814 | * |
| 1815 | * This function is useful to parse lists of phandles and their arguments. |
| 1816 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 1817 | * errno value. |
| 1818 | * |
Geert Uytterhoeven | d94a75c | 2014-10-22 11:44:52 +0200 | [diff] [blame] | 1819 | * Caller is responsible to call of_node_put() on the returned out_args->np |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1820 | * pointer. |
| 1821 | * |
| 1822 | * Example: |
| 1823 | * |
| 1824 | * phandle1: node1 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1825 | * #list-cells = <2>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1826 | * } |
| 1827 | * |
| 1828 | * phandle2: node2 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1829 | * #list-cells = <1>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1830 | * } |
| 1831 | * |
| 1832 | * node3 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1833 | * list = <&phandle1 1 2 &phandle2 3>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1834 | * } |
| 1835 | * |
| 1836 | * To get a device_node of the `node2' node you may call this: |
| 1837 | * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); |
| 1838 | */ |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1839 | int of_parse_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1840 | const char *cells_name, int index, |
| 1841 | struct of_phandle_args *out_args) |
| 1842 | { |
| 1843 | if (index < 0) |
| 1844 | return -EINVAL; |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1845 | return __of_parse_phandle_with_args(np, list_name, cells_name, 0, |
| 1846 | index, out_args); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1847 | } |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1848 | EXPORT_SYMBOL(of_parse_phandle_with_args); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1849 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1850 | /** |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1851 | * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list |
| 1852 | * @np: pointer to a device tree node containing a list |
| 1853 | * @list_name: property name that contains a list |
| 1854 | * @cell_count: number of argument cells following the phandle |
| 1855 | * @index: index of a phandle to parse out |
| 1856 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 1857 | * |
| 1858 | * This function is useful to parse lists of phandles and their arguments. |
| 1859 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 1860 | * errno value. |
| 1861 | * |
Geert Uytterhoeven | d94a75c | 2014-10-22 11:44:52 +0200 | [diff] [blame] | 1862 | * Caller is responsible to call of_node_put() on the returned out_args->np |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1863 | * pointer. |
| 1864 | * |
| 1865 | * Example: |
| 1866 | * |
| 1867 | * phandle1: node1 { |
| 1868 | * } |
| 1869 | * |
| 1870 | * phandle2: node2 { |
| 1871 | * } |
| 1872 | * |
| 1873 | * node3 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1874 | * list = <&phandle1 0 2 &phandle2 2 3>; |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1875 | * } |
| 1876 | * |
| 1877 | * To get a device_node of the `node2' node you may call this: |
| 1878 | * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args); |
| 1879 | */ |
| 1880 | int of_parse_phandle_with_fixed_args(const struct device_node *np, |
| 1881 | const char *list_name, int cell_count, |
| 1882 | int index, struct of_phandle_args *out_args) |
| 1883 | { |
| 1884 | if (index < 0) |
| 1885 | return -EINVAL; |
| 1886 | return __of_parse_phandle_with_args(np, list_name, NULL, cell_count, |
| 1887 | index, out_args); |
| 1888 | } |
| 1889 | EXPORT_SYMBOL(of_parse_phandle_with_fixed_args); |
| 1890 | |
| 1891 | /** |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1892 | * of_count_phandle_with_args() - Find the number of phandles references in a property |
| 1893 | * @np: pointer to a device tree node containing a list |
| 1894 | * @list_name: property name that contains a list |
| 1895 | * @cells_name: property name that specifies phandles' arguments count |
| 1896 | * |
| 1897 | * Returns the number of phandle + argument tuples within a property. It |
| 1898 | * is a typical pattern to encode a list of phandle and variable |
| 1899 | * arguments into a single property. The number of arguments is encoded |
| 1900 | * by a property in the phandle-target node. For example, a gpios |
| 1901 | * property would contain a list of GPIO specifies consisting of a |
| 1902 | * phandle and 1 or more arguments. The number of arguments are |
| 1903 | * determined by the #gpio-cells property in the node pointed to by the |
| 1904 | * phandle. |
| 1905 | */ |
| 1906 | int of_count_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1907 | const char *cells_name) |
| 1908 | { |
Joerg Roedel | 2021bd0 | 2016-04-04 17:49:19 +0200 | [diff] [blame] | 1909 | struct of_phandle_iterator it; |
| 1910 | int rc, cur_index = 0; |
| 1911 | |
| 1912 | rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0); |
| 1913 | if (rc) |
| 1914 | return rc; |
| 1915 | |
| 1916 | while ((rc = of_phandle_iterator_next(&it)) == 0) |
| 1917 | cur_index += 1; |
| 1918 | |
| 1919 | if (rc != -ENOENT) |
| 1920 | return rc; |
| 1921 | |
| 1922 | return cur_index; |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1923 | } |
| 1924 | EXPORT_SYMBOL(of_count_phandle_with_args); |
| 1925 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1926 | /** |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1927 | * __of_add_property - Add a property to a node without lock operations |
| 1928 | */ |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1929 | int __of_add_property(struct device_node *np, struct property *prop) |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1930 | { |
| 1931 | struct property **next; |
| 1932 | |
| 1933 | prop->next = NULL; |
| 1934 | next = &np->properties; |
| 1935 | while (*next) { |
| 1936 | if (strcmp(prop->name, (*next)->name) == 0) |
| 1937 | /* duplicate ! don't insert it */ |
| 1938 | return -EEXIST; |
| 1939 | |
| 1940 | next = &(*next)->next; |
| 1941 | } |
| 1942 | *next = prop; |
| 1943 | |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1948 | * of_add_property - Add a property to a node |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1949 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1950 | int of_add_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1951 | { |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1952 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1953 | int rc; |
| 1954 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1955 | mutex_lock(&of_mutex); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1956 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1957 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1958 | rc = __of_add_property(np, prop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1959 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1960 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1961 | if (!rc) |
Pantelis Antoniou | 0829f6d | 2013-12-13 20:08:59 +0200 | [diff] [blame] | 1962 | __of_add_property_sysfs(np, prop); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1963 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1964 | mutex_unlock(&of_mutex); |
| 1965 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 1966 | if (!rc) |
| 1967 | of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL); |
| 1968 | |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1969 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1972 | int __of_remove_property(struct device_node *np, struct property *prop) |
| 1973 | { |
| 1974 | struct property **next; |
| 1975 | |
| 1976 | for (next = &np->properties; *next; next = &(*next)->next) { |
| 1977 | if (*next == prop) |
| 1978 | break; |
| 1979 | } |
| 1980 | if (*next == NULL) |
| 1981 | return -ENODEV; |
| 1982 | |
| 1983 | /* found the node */ |
| 1984 | *next = prop->next; |
| 1985 | prop->next = np->deadprops; |
| 1986 | np->deadprops = prop; |
| 1987 | |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 1991 | void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop) |
| 1992 | { |
| 1993 | sysfs_remove_bin_file(&np->kobj, &prop->attr); |
| 1994 | kfree(prop->attr.attr.name); |
| 1995 | } |
| 1996 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1997 | void __of_remove_property_sysfs(struct device_node *np, struct property *prop) |
| 1998 | { |
Gaurav Minocha | ef69d74 | 2014-09-05 09:56:13 -0700 | [diff] [blame] | 1999 | if (!IS_ENABLED(CONFIG_SYSFS)) |
| 2000 | return; |
| 2001 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2002 | /* at early boot, bail here and defer setup to of_init() */ |
| 2003 | if (of_kset && of_node_is_attached(np)) |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 2004 | __of_sysfs_remove_bin_file(np, prop); |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2005 | } |
| 2006 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2007 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 2008 | * of_remove_property - Remove a property from a node. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2009 | * |
| 2010 | * Note that we don't actually remove it, since we have given out |
| 2011 | * who-knows-how-many pointers to the data using get-property. |
| 2012 | * Instead we just move the property to the "dead properties" |
| 2013 | * list, so it won't be found any more. |
| 2014 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 2015 | int of_remove_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2016 | { |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2017 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 2018 | int rc; |
| 2019 | |
Suraj Jitindar Singh | 201b3fe | 2016-04-28 15:34:54 +1000 | [diff] [blame] | 2020 | if (!prop) |
| 2021 | return -ENODEV; |
| 2022 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2023 | mutex_lock(&of_mutex); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2024 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 2025 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 2026 | rc = __of_remove_property(np, prop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 2027 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2028 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2029 | if (!rc) |
| 2030 | __of_remove_property_sysfs(np, prop); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2031 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2032 | mutex_unlock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 2033 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 2034 | if (!rc) |
| 2035 | of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL); |
| 2036 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2037 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 2040 | int __of_update_property(struct device_node *np, struct property *newprop, |
| 2041 | struct property **oldpropp) |
| 2042 | { |
| 2043 | struct property **next, *oldprop; |
| 2044 | |
| 2045 | for (next = &np->properties; *next; next = &(*next)->next) { |
| 2046 | if (of_prop_cmp((*next)->name, newprop->name) == 0) |
| 2047 | break; |
| 2048 | } |
| 2049 | *oldpropp = oldprop = *next; |
| 2050 | |
| 2051 | if (oldprop) { |
| 2052 | /* replace the node */ |
| 2053 | newprop->next = oldprop->next; |
| 2054 | *next = newprop; |
| 2055 | oldprop->next = np->deadprops; |
| 2056 | np->deadprops = oldprop; |
| 2057 | } else { |
| 2058 | /* new node */ |
| 2059 | newprop->next = NULL; |
| 2060 | *next = newprop; |
| 2061 | } |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2062 | |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2066 | void __of_update_property_sysfs(struct device_node *np, struct property *newprop, |
| 2067 | struct property *oldprop) |
| 2068 | { |
Gaurav Minocha | ef69d74 | 2014-09-05 09:56:13 -0700 | [diff] [blame] | 2069 | if (!IS_ENABLED(CONFIG_SYSFS)) |
| 2070 | return; |
| 2071 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2072 | /* At early boot, bail out and defer setup to of_init() */ |
| 2073 | if (!of_kset) |
| 2074 | return; |
| 2075 | |
| 2076 | if (oldprop) |
Frank Rowand | d9fc880 | 2016-06-16 10:51:46 -0700 | [diff] [blame] | 2077 | __of_sysfs_remove_bin_file(np, oldprop); |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2078 | __of_add_property_sysfs(np, newprop); |
| 2079 | } |
| 2080 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2081 | /* |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 2082 | * of_update_property - Update a property in a node, if the property does |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 2083 | * not exist, add it. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2084 | * |
| 2085 | * Note that we don't actually remove it, since we have given out |
| 2086 | * who-knows-how-many pointers to the data using get-property. |
| 2087 | * Instead we just move the property to the "dead properties" list, |
| 2088 | * and add the new property to the property list |
| 2089 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 2090 | int of_update_property(struct device_node *np, struct property *newprop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2091 | { |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 2092 | struct property *oldprop; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 2093 | unsigned long flags; |
Xiubo Li | 947fdaad0 | 2014-04-17 15:48:29 +0800 | [diff] [blame] | 2094 | int rc; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 2095 | |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 2096 | if (!newprop->name) |
| 2097 | return -EINVAL; |
| 2098 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2099 | mutex_lock(&of_mutex); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 2100 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 2101 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 2102 | rc = __of_update_property(np, newprop, &oldprop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 2103 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 2104 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2105 | if (!rc) |
| 2106 | __of_update_property_sysfs(np, newprop, oldprop); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 2107 | |
Grant Likely | 8a2b22a | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 2108 | mutex_unlock(&of_mutex); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 2109 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 2110 | if (!rc) |
| 2111 | of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 2112 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 2113 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 2114 | } |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 2115 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2116 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
| 2117 | int id, const char *stem, int stem_len) |
| 2118 | { |
| 2119 | ap->np = np; |
| 2120 | ap->id = id; |
| 2121 | strncpy(ap->stem, stem, stem_len); |
| 2122 | ap->stem[stem_len] = 0; |
| 2123 | list_add_tail(&ap->link, &aliases_lookup); |
| 2124 | pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n", |
Grant Likely | 74a7f08 | 2012-06-15 11:50:25 -0600 | [diff] [blame] | 2125 | ap->alias, ap->stem, ap->id, of_node_full_name(np)); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | /** |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 2129 | * of_alias_scan - Scan all properties of the 'aliases' node |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2130 | * |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 2131 | * The function scans all the properties of the 'aliases' node and populates |
| 2132 | * the global lookup table with the properties. It returns the |
| 2133 | * number of alias properties found, or an error code in case of failure. |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2134 | * |
| 2135 | * @dt_alloc: An allocator that provides a virtual address to memory |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 2136 | * for storing the resulting tree |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2137 | */ |
| 2138 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) |
| 2139 | { |
| 2140 | struct property *pp; |
| 2141 | |
Laurentiu Tudor | 7dbe584 | 2014-08-27 17:09:39 +0300 | [diff] [blame] | 2142 | of_aliases = of_find_node_by_path("/aliases"); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2143 | of_chosen = of_find_node_by_path("/chosen"); |
| 2144 | if (of_chosen == NULL) |
| 2145 | of_chosen = of_find_node_by_path("/chosen@0"); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2146 | |
| 2147 | if (of_chosen) { |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 2148 | /* linux,stdout-path and /aliases/stdout are for legacy compatibility */ |
Grant Likely | 676e1b2 | 2014-03-27 17:11:23 -0700 | [diff] [blame] | 2149 | const char *name = of_get_property(of_chosen, "stdout-path", NULL); |
| 2150 | if (!name) |
| 2151 | name = of_get_property(of_chosen, "linux,stdout-path", NULL); |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 2152 | if (IS_ENABLED(CONFIG_PPC) && !name) |
| 2153 | name = of_get_property(of_aliases, "stdout", NULL); |
Peter Hurley | f64255b | 2015-03-17 16:46:33 -0400 | [diff] [blame] | 2154 | if (name) |
Leif Lindholm | 7914a7c | 2014-11-27 17:56:07 +0000 | [diff] [blame] | 2155 | of_stdout = of_find_node_opts_by_path(name, &of_stdout_options); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2156 | } |
| 2157 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2158 | if (!of_aliases) |
| 2159 | return; |
| 2160 | |
Dong Aisheng | 8af0da9 | 2011-12-22 20:19:24 +0800 | [diff] [blame] | 2161 | for_each_property_of_node(of_aliases, pp) { |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2162 | const char *start = pp->name; |
| 2163 | const char *end = start + strlen(start); |
| 2164 | struct device_node *np; |
| 2165 | struct alias_prop *ap; |
| 2166 | int id, len; |
| 2167 | |
| 2168 | /* Skip those we do not want to proceed */ |
| 2169 | if (!strcmp(pp->name, "name") || |
| 2170 | !strcmp(pp->name, "phandle") || |
| 2171 | !strcmp(pp->name, "linux,phandle")) |
| 2172 | continue; |
| 2173 | |
| 2174 | np = of_find_node_by_path(pp->value); |
| 2175 | if (!np) |
| 2176 | continue; |
| 2177 | |
| 2178 | /* walk the alias backwards to extract the id and work out |
| 2179 | * the 'stem' string */ |
| 2180 | while (isdigit(*(end-1)) && end > start) |
| 2181 | end--; |
| 2182 | len = end - start; |
| 2183 | |
| 2184 | if (kstrtoint(end, 10, &id) < 0) |
| 2185 | continue; |
| 2186 | |
| 2187 | /* Allocate an alias_prop with enough space for the stem */ |
| 2188 | ap = dt_alloc(sizeof(*ap) + len + 1, 4); |
| 2189 | if (!ap) |
| 2190 | continue; |
Grant Likely | 0640332e | 2013-08-28 21:24:17 +0100 | [diff] [blame] | 2191 | memset(ap, 0, sizeof(*ap) + len + 1); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2192 | ap->alias = start; |
| 2193 | of_alias_add(ap, np, id, start, len); |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | /** |
| 2198 | * of_alias_get_id - Get alias id for the given device_node |
| 2199 | * @np: Pointer to the given device_node |
| 2200 | * @stem: Alias stem of the given device_node |
| 2201 | * |
Geert Uytterhoeven | 5a53a07 | 2014-03-11 11:23:38 +0100 | [diff] [blame] | 2202 | * The function travels the lookup table to get the alias id for the given |
| 2203 | * device_node and alias stem. It returns the alias id if found. |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2204 | */ |
| 2205 | int of_alias_get_id(struct device_node *np, const char *stem) |
| 2206 | { |
| 2207 | struct alias_prop *app; |
| 2208 | int id = -ENODEV; |
| 2209 | |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 2210 | mutex_lock(&of_mutex); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2211 | list_for_each_entry(app, &aliases_lookup, link) { |
| 2212 | if (strcmp(app->stem, stem) != 0) |
| 2213 | continue; |
| 2214 | |
| 2215 | if (np == app->np) { |
| 2216 | id = app->id; |
| 2217 | break; |
| 2218 | } |
| 2219 | } |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 2220 | mutex_unlock(&of_mutex); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2221 | |
| 2222 | return id; |
| 2223 | } |
| 2224 | EXPORT_SYMBOL_GPL(of_alias_get_id); |
Stephen Warren | c541adc | 2012-04-04 09:27:46 -0600 | [diff] [blame] | 2225 | |
Wolfram Sang | 351d224 | 2015-03-12 17:17:58 +0100 | [diff] [blame] | 2226 | /** |
| 2227 | * of_alias_get_highest_id - Get highest alias id for the given stem |
| 2228 | * @stem: Alias stem to be examined |
| 2229 | * |
| 2230 | * The function travels the lookup table to get the highest alias id for the |
| 2231 | * given alias stem. It returns the alias id if found. |
| 2232 | */ |
| 2233 | int of_alias_get_highest_id(const char *stem) |
| 2234 | { |
| 2235 | struct alias_prop *app; |
| 2236 | int id = -ENODEV; |
| 2237 | |
| 2238 | mutex_lock(&of_mutex); |
| 2239 | list_for_each_entry(app, &aliases_lookup, link) { |
| 2240 | if (strcmp(app->stem, stem) != 0) |
| 2241 | continue; |
| 2242 | |
| 2243 | if (app->id > id) |
| 2244 | id = app->id; |
| 2245 | } |
| 2246 | mutex_unlock(&of_mutex); |
| 2247 | |
| 2248 | return id; |
| 2249 | } |
| 2250 | EXPORT_SYMBOL_GPL(of_alias_get_highest_id); |
| 2251 | |
Stephen Warren | c541adc | 2012-04-04 09:27:46 -0600 | [diff] [blame] | 2252 | const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur, |
| 2253 | u32 *pu) |
| 2254 | { |
| 2255 | const void *curv = cur; |
| 2256 | |
| 2257 | if (!prop) |
| 2258 | return NULL; |
| 2259 | |
| 2260 | if (!cur) { |
| 2261 | curv = prop->value; |
| 2262 | goto out_val; |
| 2263 | } |
| 2264 | |
| 2265 | curv += sizeof(*cur); |
| 2266 | if (curv >= prop->value + prop->length) |
| 2267 | return NULL; |
| 2268 | |
| 2269 | out_val: |
| 2270 | *pu = be32_to_cpup(curv); |
| 2271 | return curv; |
| 2272 | } |
| 2273 | EXPORT_SYMBOL_GPL(of_prop_next_u32); |
| 2274 | |
| 2275 | const char *of_prop_next_string(struct property *prop, const char *cur) |
| 2276 | { |
| 2277 | const void *curv = cur; |
| 2278 | |
| 2279 | if (!prop) |
| 2280 | return NULL; |
| 2281 | |
| 2282 | if (!cur) |
| 2283 | return prop->value; |
| 2284 | |
| 2285 | curv += strlen(cur) + 1; |
| 2286 | if (curv >= prop->value + prop->length) |
| 2287 | return NULL; |
| 2288 | |
| 2289 | return curv; |
| 2290 | } |
| 2291 | EXPORT_SYMBOL_GPL(of_prop_next_string); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2292 | |
| 2293 | /** |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2294 | * of_console_check() - Test and setup console for DT setup |
| 2295 | * @dn - Pointer to device node |
| 2296 | * @name - Name to use for preferred console without index. ex. "ttyS" |
| 2297 | * @index - Index to use for preferred console. |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2298 | * |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2299 | * Check if the given device node matches the stdout-path property in the |
| 2300 | * /chosen node. If it does then register it as the preferred console and return |
| 2301 | * TRUE. Otherwise return FALSE. |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2302 | */ |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2303 | bool of_console_check(struct device_node *dn, char *name, int index) |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2304 | { |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2305 | if (!dn || dn != of_stdout || console_set_on_cmdline) |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2306 | return false; |
Leif Lindholm | 7914a7c | 2014-11-27 17:56:07 +0000 | [diff] [blame] | 2307 | return !add_preferred_console(name, index, |
| 2308 | kstrdup(of_stdout_options, GFP_KERNEL)); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2309 | } |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2310 | EXPORT_SYMBOL_GPL(of_console_check); |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2311 | |
| 2312 | /** |
| 2313 | * of_find_next_cache_node - Find a node's subsidiary cache |
| 2314 | * @np: node of type "cpu" or "cache" |
| 2315 | * |
| 2316 | * Returns a node pointer with refcount incremented, use |
| 2317 | * of_node_put() on it when done. Caller should hold a reference |
| 2318 | * to np. |
| 2319 | */ |
| 2320 | struct device_node *of_find_next_cache_node(const struct device_node *np) |
| 2321 | { |
| 2322 | struct device_node *child; |
| 2323 | const phandle *handle; |
| 2324 | |
| 2325 | handle = of_get_property(np, "l2-cache", NULL); |
| 2326 | if (!handle) |
| 2327 | handle = of_get_property(np, "next-level-cache", NULL); |
| 2328 | |
| 2329 | if (handle) |
| 2330 | return of_find_node_by_phandle(be32_to_cpup(handle)); |
| 2331 | |
| 2332 | /* OF on pmac has nodes instead of properties named "l2-cache" |
| 2333 | * beneath CPU nodes. |
| 2334 | */ |
| 2335 | if (!strcmp(np->type, "cpu")) |
| 2336 | for_each_child_of_node(np, child) |
| 2337 | if (!strcmp(child->type, "cache")) |
| 2338 | return child; |
| 2339 | |
| 2340 | return NULL; |
| 2341 | } |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2342 | |
| 2343 | /** |
Philipp Zabel | f2a575f | 2014-02-14 11:53:56 +0100 | [diff] [blame] | 2344 | * of_graph_parse_endpoint() - parse common endpoint node properties |
| 2345 | * @node: pointer to endpoint device_node |
| 2346 | * @endpoint: pointer to the OF endpoint data structure |
| 2347 | * |
| 2348 | * The caller should hold a reference to @node. |
| 2349 | */ |
| 2350 | int of_graph_parse_endpoint(const struct device_node *node, |
| 2351 | struct of_endpoint *endpoint) |
| 2352 | { |
| 2353 | struct device_node *port_node = of_get_parent(node); |
| 2354 | |
Philipp Zabel | d484700 | 2014-03-04 12:31:24 +0100 | [diff] [blame] | 2355 | WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n", |
| 2356 | __func__, node->full_name); |
| 2357 | |
Philipp Zabel | f2a575f | 2014-02-14 11:53:56 +0100 | [diff] [blame] | 2358 | memset(endpoint, 0, sizeof(*endpoint)); |
| 2359 | |
| 2360 | endpoint->local_node = node; |
| 2361 | /* |
| 2362 | * It doesn't matter whether the two calls below succeed. |
| 2363 | * If they don't then the default value 0 is used. |
| 2364 | */ |
| 2365 | of_property_read_u32(port_node, "reg", &endpoint->port); |
| 2366 | of_property_read_u32(node, "reg", &endpoint->id); |
| 2367 | |
| 2368 | of_node_put(port_node); |
| 2369 | |
| 2370 | return 0; |
| 2371 | } |
| 2372 | EXPORT_SYMBOL(of_graph_parse_endpoint); |
| 2373 | |
| 2374 | /** |
Philipp Zabel | bfe446e | 2014-03-11 11:21:11 +0100 | [diff] [blame] | 2375 | * of_graph_get_port_by_id() - get the port matching a given id |
| 2376 | * @parent: pointer to the parent device node |
| 2377 | * @id: id of the port |
| 2378 | * |
| 2379 | * Return: A 'port' node pointer with refcount incremented. The caller |
| 2380 | * has to use of_node_put() on it when done. |
| 2381 | */ |
| 2382 | struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id) |
| 2383 | { |
| 2384 | struct device_node *node, *port; |
| 2385 | |
| 2386 | node = of_get_child_by_name(parent, "ports"); |
| 2387 | if (node) |
| 2388 | parent = node; |
| 2389 | |
| 2390 | for_each_child_of_node(parent, port) { |
| 2391 | u32 port_id = 0; |
| 2392 | |
| 2393 | if (of_node_cmp(port->name, "port") != 0) |
| 2394 | continue; |
| 2395 | of_property_read_u32(port, "reg", &port_id); |
| 2396 | if (id == port_id) |
| 2397 | break; |
| 2398 | } |
| 2399 | |
| 2400 | of_node_put(node); |
| 2401 | |
| 2402 | return port; |
| 2403 | } |
| 2404 | EXPORT_SYMBOL(of_graph_get_port_by_id); |
| 2405 | |
| 2406 | /** |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2407 | * of_graph_get_next_endpoint() - get next endpoint node |
| 2408 | * @parent: pointer to the parent device node |
| 2409 | * @prev: previous endpoint node, or NULL to get first |
| 2410 | * |
| 2411 | * Return: An 'endpoint' node pointer with refcount incremented. Refcount |
Philipp Zabel | f033c0b | 2014-12-01 13:32:32 +0100 | [diff] [blame] | 2412 | * of the passed @prev node is decremented. |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2413 | */ |
| 2414 | struct device_node *of_graph_get_next_endpoint(const struct device_node *parent, |
| 2415 | struct device_node *prev) |
| 2416 | { |
| 2417 | struct device_node *endpoint; |
Linus Torvalds | 3c83e61 | 2014-04-04 09:50:07 -0700 | [diff] [blame] | 2418 | struct device_node *port; |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2419 | |
| 2420 | if (!parent) |
| 2421 | return NULL; |
| 2422 | |
Linus Torvalds | 3c83e61 | 2014-04-04 09:50:07 -0700 | [diff] [blame] | 2423 | /* |
| 2424 | * Start by locating the port node. If no previous endpoint is specified |
| 2425 | * search for the first port node, otherwise get the previous endpoint |
| 2426 | * parent port node. |
| 2427 | */ |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2428 | if (!prev) { |
| 2429 | struct device_node *node; |
Linus Torvalds | 3c83e61 | 2014-04-04 09:50:07 -0700 | [diff] [blame] | 2430 | |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2431 | node = of_get_child_by_name(parent, "ports"); |
| 2432 | if (node) |
| 2433 | parent = node; |
| 2434 | |
| 2435 | port = of_get_child_by_name(parent, "port"); |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2436 | of_node_put(node); |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2437 | |
Linus Torvalds | 3c83e61 | 2014-04-04 09:50:07 -0700 | [diff] [blame] | 2438 | if (!port) { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 2439 | pr_err("graph: no port node found in %s\n", |
| 2440 | parent->full_name); |
Philipp Zabel | 4329b93 | 2014-02-26 20:43:42 +0100 | [diff] [blame] | 2441 | return NULL; |
Linus Torvalds | 3c83e61 | 2014-04-04 09:50:07 -0700 | [diff] [blame] | 2442 | } |
| 2443 | } else { |
| 2444 | port = of_get_parent(prev); |
| 2445 | if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n", |
| 2446 | __func__, prev->full_name)) |
| 2447 | return NULL; |
Linus Torvalds | 3c83e61 | 2014-04-04 09:50:07 -0700 | [diff] [blame] | 2448 | } |
Philipp Zabel | 4329b93 | 2014-02-26 20:43:42 +0100 | [diff] [blame] | 2449 | |
Linus Torvalds | 3c83e61 | 2014-04-04 09:50:07 -0700 | [diff] [blame] | 2450 | while (1) { |
| 2451 | /* |
| 2452 | * Now that we have a port node, get the next endpoint by |
| 2453 | * getting the next child. If the previous endpoint is NULL this |
| 2454 | * will return the first child. |
| 2455 | */ |
| 2456 | endpoint = of_get_next_child(port, prev); |
| 2457 | if (endpoint) { |
| 2458 | of_node_put(port); |
| 2459 | return endpoint; |
| 2460 | } |
| 2461 | |
| 2462 | /* No more endpoints under this port, try the next one. */ |
| 2463 | prev = NULL; |
| 2464 | |
| 2465 | do { |
| 2466 | port = of_get_next_child(parent, port); |
| 2467 | if (!port) |
| 2468 | return NULL; |
| 2469 | } while (of_node_cmp(port->name, "port")); |
| 2470 | } |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2471 | } |
| 2472 | EXPORT_SYMBOL(of_graph_get_next_endpoint); |
| 2473 | |
| 2474 | /** |
Hyungwon Hwang | 8ccd0d0 | 2015-06-12 21:59:01 +0900 | [diff] [blame] | 2475 | * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers |
| 2476 | * @parent: pointer to the parent device node |
| 2477 | * @port_reg: identifier (value of reg property) of the parent port node |
| 2478 | * @reg: identifier (value of reg property) of the endpoint node |
| 2479 | * |
| 2480 | * Return: An 'endpoint' node pointer which is identified by reg and at the same |
| 2481 | * is the child of a port node identified by port_reg. reg and port_reg are |
| 2482 | * ignored when they are -1. |
| 2483 | */ |
| 2484 | struct device_node *of_graph_get_endpoint_by_regs( |
| 2485 | const struct device_node *parent, int port_reg, int reg) |
| 2486 | { |
| 2487 | struct of_endpoint endpoint; |
Lucas Stach | 34276bb | 2016-08-15 14:58:43 +0200 | [diff] [blame] | 2488 | struct device_node *node = NULL; |
Hyungwon Hwang | 8ccd0d0 | 2015-06-12 21:59:01 +0900 | [diff] [blame] | 2489 | |
Lucas Stach | 34276bb | 2016-08-15 14:58:43 +0200 | [diff] [blame] | 2490 | for_each_endpoint_of_node(parent, node) { |
Hyungwon Hwang | 8ccd0d0 | 2015-06-12 21:59:01 +0900 | [diff] [blame] | 2491 | of_graph_parse_endpoint(node, &endpoint); |
| 2492 | if (((port_reg == -1) || (endpoint.port == port_reg)) && |
| 2493 | ((reg == -1) || (endpoint.id == reg))) |
| 2494 | return node; |
Hyungwon Hwang | 8ccd0d0 | 2015-06-12 21:59:01 +0900 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | return NULL; |
| 2498 | } |
Dave Airlie | 8ffaa90 | 2015-06-23 10:19:10 +1000 | [diff] [blame] | 2499 | EXPORT_SYMBOL(of_graph_get_endpoint_by_regs); |
Hyungwon Hwang | 8ccd0d0 | 2015-06-12 21:59:01 +0900 | [diff] [blame] | 2500 | |
| 2501 | /** |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2502 | * of_graph_get_remote_port_parent() - get remote port's parent node |
| 2503 | * @node: pointer to a local endpoint device_node |
| 2504 | * |
| 2505 | * Return: Remote device node associated with remote endpoint node linked |
| 2506 | * to @node. Use of_node_put() on it when done. |
| 2507 | */ |
| 2508 | struct device_node *of_graph_get_remote_port_parent( |
| 2509 | const struct device_node *node) |
| 2510 | { |
| 2511 | struct device_node *np; |
| 2512 | unsigned int depth; |
| 2513 | |
| 2514 | /* Get remote endpoint node. */ |
| 2515 | np = of_parse_phandle(node, "remote-endpoint", 0); |
| 2516 | |
| 2517 | /* Walk 3 levels up only if there is 'ports' node. */ |
| 2518 | for (depth = 3; depth && np; depth--) { |
| 2519 | np = of_get_next_parent(np); |
| 2520 | if (depth == 2 && of_node_cmp(np->name, "ports")) |
| 2521 | break; |
| 2522 | } |
| 2523 | return np; |
| 2524 | } |
| 2525 | EXPORT_SYMBOL(of_graph_get_remote_port_parent); |
| 2526 | |
| 2527 | /** |
| 2528 | * of_graph_get_remote_port() - get remote port node |
| 2529 | * @node: pointer to a local endpoint device_node |
| 2530 | * |
| 2531 | * Return: Remote port node associated with remote endpoint node linked |
| 2532 | * to @node. Use of_node_put() on it when done. |
| 2533 | */ |
| 2534 | struct device_node *of_graph_get_remote_port(const struct device_node *node) |
| 2535 | { |
| 2536 | struct device_node *np; |
| 2537 | |
| 2538 | /* Get remote endpoint node. */ |
| 2539 | np = of_parse_phandle(node, "remote-endpoint", 0); |
| 2540 | if (!np) |
| 2541 | return NULL; |
| 2542 | return of_get_next_parent(np); |
| 2543 | } |
| 2544 | EXPORT_SYMBOL(of_graph_get_remote_port); |