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 | */ |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 20 | #include <linux/ctype.h> |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/of.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 26 | #include <linux/string.h> |
Jeremy Kerr | a9f2f63 | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 27 | #include <linux/proc_fs.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 28 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 29 | #include "of_private.h" |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 30 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 31 | LIST_HEAD(aliases_lookup); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 32 | |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 33 | struct device_node *of_allnodes; |
| 34 | EXPORT_SYMBOL(of_allnodes); |
Grant Likely | fc0bdae | 2010-02-14 07:13:55 -0700 | [diff] [blame] | 35 | struct device_node *of_chosen; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 36 | struct device_node *of_aliases; |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 37 | static struct device_node *of_stdout; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 38 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 39 | static struct kset *of_kset; |
| 40 | |
| 41 | /* |
| 42 | * Used to protect the of_aliases; but also overloaded to hold off addition of |
| 43 | * nodes to sysfs |
| 44 | */ |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 45 | DEFINE_MUTEX(of_aliases_mutex); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 46 | |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 47 | /* use when traversing tree through the allnext, child, sibling, |
| 48 | * or parent members of struct device_node. |
| 49 | */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 50 | DEFINE_RAW_SPINLOCK(devtree_lock); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 51 | |
| 52 | int of_n_addr_cells(struct device_node *np) |
| 53 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 54 | const __be32 *ip; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 55 | |
| 56 | do { |
| 57 | if (np->parent) |
| 58 | np = np->parent; |
| 59 | ip = of_get_property(np, "#address-cells", NULL); |
| 60 | if (ip) |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 61 | return be32_to_cpup(ip); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 62 | } while (np->parent); |
| 63 | /* No #address-cells property for the root node */ |
| 64 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 65 | } |
| 66 | EXPORT_SYMBOL(of_n_addr_cells); |
| 67 | |
| 68 | int of_n_size_cells(struct device_node *np) |
| 69 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 70 | const __be32 *ip; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 71 | |
| 72 | do { |
| 73 | if (np->parent) |
| 74 | np = np->parent; |
| 75 | ip = of_get_property(np, "#size-cells", NULL); |
| 76 | if (ip) |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 77 | return be32_to_cpup(ip); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 78 | } while (np->parent); |
| 79 | /* No #size-cells property for the root node */ |
| 80 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 81 | } |
| 82 | EXPORT_SYMBOL(of_n_size_cells); |
| 83 | |
Rob Herring | 0c3f061 | 2013-09-17 10:42:50 -0500 | [diff] [blame] | 84 | #ifdef CONFIG_NUMA |
| 85 | int __weak of_node_to_nid(struct device_node *np) |
| 86 | { |
| 87 | return numa_node_id(); |
| 88 | } |
| 89 | #endif |
| 90 | |
Grant Likely | 0f22dd3 | 2012-02-15 20:38:40 -0700 | [diff] [blame] | 91 | #if defined(CONFIG_OF_DYNAMIC) |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 92 | /** |
| 93 | * of_node_get - Increment refcount of a node |
| 94 | * @node: Node to inc refcount, NULL is supported to |
| 95 | * simplify writing of callers |
| 96 | * |
| 97 | * Returns node. |
| 98 | */ |
| 99 | struct device_node *of_node_get(struct device_node *node) |
| 100 | { |
| 101 | if (node) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 102 | kobject_get(&node->kobj); |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 103 | return node; |
| 104 | } |
| 105 | EXPORT_SYMBOL(of_node_get); |
| 106 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 107 | static inline struct device_node *kobj_to_device_node(struct kobject *kobj) |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 108 | { |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 109 | return container_of(kobj, struct device_node, kobj); |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * of_node_release - release a dynamically allocated node |
| 114 | * @kref: kref element of the node to be released |
| 115 | * |
| 116 | * In of_node_put() this function is passed to kref_put() |
| 117 | * as the destructor. |
| 118 | */ |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 119 | static void of_node_release(struct kobject *kobj) |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 120 | { |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 121 | struct device_node *node = kobj_to_device_node(kobj); |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 122 | struct property *prop = node->properties; |
| 123 | |
| 124 | /* We should never be releasing nodes that haven't been detached. */ |
| 125 | if (!of_node_check_flag(node, OF_DETACHED)) { |
| 126 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); |
| 127 | dump_stack(); |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (!of_node_check_flag(node, OF_DYNAMIC)) |
| 132 | return; |
| 133 | |
| 134 | while (prop) { |
| 135 | struct property *next = prop->next; |
| 136 | kfree(prop->name); |
| 137 | kfree(prop->value); |
| 138 | kfree(prop); |
| 139 | prop = next; |
| 140 | |
| 141 | if (!prop) { |
| 142 | prop = node->deadprops; |
| 143 | node->deadprops = NULL; |
| 144 | } |
| 145 | } |
| 146 | kfree(node->full_name); |
| 147 | kfree(node->data); |
| 148 | kfree(node); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * of_node_put - Decrement refcount of a node |
| 153 | * @node: Node to dec refcount, NULL is supported to |
| 154 | * simplify writing of callers |
| 155 | * |
| 156 | */ |
| 157 | void of_node_put(struct device_node *node) |
| 158 | { |
| 159 | if (node) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 160 | kobject_put(&node->kobj); |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 161 | } |
| 162 | EXPORT_SYMBOL(of_node_put); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 163 | #else |
| 164 | static void of_node_release(struct kobject *kobj) |
| 165 | { |
| 166 | /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */ |
| 167 | } |
Grant Likely | 0f22dd3 | 2012-02-15 20:38:40 -0700 | [diff] [blame] | 168 | #endif /* CONFIG_OF_DYNAMIC */ |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 169 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 170 | struct kobj_type of_node_ktype = { |
| 171 | .release = of_node_release, |
| 172 | }; |
| 173 | |
| 174 | static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj, |
| 175 | struct bin_attribute *bin_attr, char *buf, |
| 176 | loff_t offset, size_t count) |
| 177 | { |
| 178 | struct property *pp = container_of(bin_attr, struct property, attr); |
| 179 | return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length); |
| 180 | } |
| 181 | |
| 182 | static const char *safe_name(struct kobject *kobj, const char *orig_name) |
| 183 | { |
| 184 | const char *name = orig_name; |
| 185 | struct kernfs_node *kn; |
| 186 | int i = 0; |
| 187 | |
| 188 | /* don't be a hero. After 16 tries give up */ |
| 189 | while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) { |
| 190 | sysfs_put(kn); |
| 191 | if (name != orig_name) |
| 192 | kfree(name); |
| 193 | name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i); |
| 194 | } |
| 195 | |
| 196 | if (name != orig_name) |
| 197 | pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n", |
| 198 | kobject_name(kobj), name); |
| 199 | return name; |
| 200 | } |
| 201 | |
| 202 | static int __of_add_property_sysfs(struct device_node *np, struct property *pp) |
| 203 | { |
| 204 | int rc; |
| 205 | |
| 206 | /* Important: Don't leak passwords */ |
| 207 | bool secure = strncmp(pp->name, "security-", 9) == 0; |
| 208 | |
| 209 | sysfs_bin_attr_init(&pp->attr); |
| 210 | pp->attr.attr.name = safe_name(&np->kobj, pp->name); |
| 211 | pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO; |
| 212 | pp->attr.size = secure ? 0 : pp->length; |
| 213 | pp->attr.read = of_node_property_read; |
| 214 | |
| 215 | rc = sysfs_create_bin_file(&np->kobj, &pp->attr); |
| 216 | WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name); |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | static int __of_node_add(struct device_node *np) |
| 221 | { |
| 222 | const char *name; |
| 223 | struct property *pp; |
| 224 | int rc; |
| 225 | |
| 226 | np->kobj.kset = of_kset; |
| 227 | if (!np->parent) { |
| 228 | /* Nodes without parents are new top level trees */ |
| 229 | rc = kobject_add(&np->kobj, NULL, safe_name(&of_kset->kobj, "base")); |
| 230 | } else { |
| 231 | name = safe_name(&np->parent->kobj, kbasename(np->full_name)); |
| 232 | if (!name || !name[0]) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name); |
| 236 | } |
| 237 | if (rc) |
| 238 | return rc; |
| 239 | |
| 240 | for_each_property_of_node(np, pp) |
| 241 | __of_add_property_sysfs(np, pp); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | int of_node_add(struct device_node *np) |
| 247 | { |
| 248 | int rc = 0; |
| 249 | kobject_init(&np->kobj, &of_node_ktype); |
| 250 | mutex_lock(&of_aliases_mutex); |
| 251 | if (of_kset) |
| 252 | rc = __of_node_add(np); |
| 253 | mutex_unlock(&of_aliases_mutex); |
| 254 | return rc; |
| 255 | } |
| 256 | |
| 257 | #if defined(CONFIG_OF_DYNAMIC) |
| 258 | static void of_node_remove(struct device_node *np) |
| 259 | { |
| 260 | struct property *pp; |
| 261 | |
| 262 | for_each_property_of_node(np, pp) |
| 263 | sysfs_remove_bin_file(&np->kobj, &pp->attr); |
| 264 | |
| 265 | kobject_del(&np->kobj); |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | static int __init of_init(void) |
| 270 | { |
| 271 | struct device_node *np; |
| 272 | |
| 273 | /* Create the kset, and register existing nodes */ |
| 274 | mutex_lock(&of_aliases_mutex); |
| 275 | of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj); |
| 276 | if (!of_kset) { |
| 277 | mutex_unlock(&of_aliases_mutex); |
| 278 | return -ENOMEM; |
| 279 | } |
| 280 | for_each_of_allnodes(np) |
| 281 | __of_node_add(np); |
| 282 | mutex_unlock(&of_aliases_mutex); |
| 283 | |
Grant Likely | 8357041 | 2012-11-06 21:03:27 +0000 | [diff] [blame^] | 284 | /* Symlink in /proc as required by userspace ABI */ |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 285 | if (of_allnodes) |
| 286 | proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base"); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | core_initcall(of_init); |
| 291 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 292 | static struct property *__of_find_property(const struct device_node *np, |
| 293 | const char *name, int *lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 294 | { |
| 295 | struct property *pp; |
| 296 | |
Timur Tabi | 64e4566 | 2008-05-08 05:19:59 +1000 | [diff] [blame] | 297 | if (!np) |
| 298 | return NULL; |
| 299 | |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 300 | for (pp = np->properties; pp; pp = pp->next) { |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 301 | if (of_prop_cmp(pp->name, name) == 0) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 302 | if (lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 303 | *lenp = pp->length; |
| 304 | break; |
| 305 | } |
| 306 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 307 | |
| 308 | return pp; |
| 309 | } |
| 310 | |
| 311 | struct property *of_find_property(const struct device_node *np, |
| 312 | const char *name, |
| 313 | int *lenp) |
| 314 | { |
| 315 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 316 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 317 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 318 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 319 | pp = __of_find_property(np, name, lenp); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 320 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 321 | |
| 322 | return pp; |
| 323 | } |
| 324 | EXPORT_SYMBOL(of_find_property); |
| 325 | |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 326 | /** |
| 327 | * of_find_all_nodes - Get next node in global list |
| 328 | * @prev: Previous node or NULL to start iteration |
| 329 | * of_node_put() will be called on it |
| 330 | * |
| 331 | * Returns a node pointer with refcount incremented, use |
| 332 | * of_node_put() on it when done. |
| 333 | */ |
| 334 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 335 | { |
| 336 | struct device_node *np; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 337 | unsigned long flags; |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 338 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 339 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 340 | np = prev ? prev->allnext : of_allnodes; |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 341 | for (; np != NULL; np = np->allnext) |
| 342 | if (of_node_get(np)) |
| 343 | break; |
| 344 | of_node_put(prev); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 345 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 346 | return np; |
| 347 | } |
| 348 | EXPORT_SYMBOL(of_find_all_nodes); |
| 349 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 350 | /* |
| 351 | * Find a property with a given name for a given node |
| 352 | * and return the value. |
| 353 | */ |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 354 | static const void *__of_get_property(const struct device_node *np, |
| 355 | const char *name, int *lenp) |
| 356 | { |
| 357 | struct property *pp = __of_find_property(np, name, lenp); |
| 358 | |
| 359 | return pp ? pp->value : NULL; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Find a property with a given name for a given node |
| 364 | * and return the value. |
| 365 | */ |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 366 | 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] | 367 | int *lenp) |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 368 | { |
| 369 | struct property *pp = of_find_property(np, name, lenp); |
| 370 | |
| 371 | return pp ? pp->value : NULL; |
| 372 | } |
| 373 | EXPORT_SYMBOL(of_get_property); |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 374 | |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 375 | /* |
| 376 | * arch_match_cpu_phys_id - Match the given logical CPU and physical id |
| 377 | * |
| 378 | * @cpu: logical cpu index of a core/thread |
| 379 | * @phys_id: physical identifier of a core/thread |
| 380 | * |
| 381 | * CPU logical to physical index mapping is architecture specific. |
| 382 | * However this __weak function provides a default match of physical |
| 383 | * id to logical cpu index. phys_id provided here is usually values read |
| 384 | * from the device tree which must match the hardware internal registers. |
| 385 | * |
| 386 | * Returns true if the physical identifier and the logical cpu index |
| 387 | * correspond to the same core/thread, false otherwise. |
| 388 | */ |
| 389 | bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id) |
| 390 | { |
| 391 | return (u32)phys_id == cpu; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Checks if the given "prop_name" property holds the physical id of the |
| 396 | * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not |
| 397 | * NULL, local thread number within the core is returned in it. |
| 398 | */ |
| 399 | static bool __of_find_n_match_cpu_property(struct device_node *cpun, |
| 400 | const char *prop_name, int cpu, unsigned int *thread) |
| 401 | { |
| 402 | const __be32 *cell; |
| 403 | int ac, prop_len, tid; |
| 404 | u64 hwid; |
| 405 | |
| 406 | ac = of_n_addr_cells(cpun); |
| 407 | cell = of_get_property(cpun, prop_name, &prop_len); |
Grant Likely | f3cea45 | 2013-10-04 17:24:26 +0100 | [diff] [blame] | 408 | if (!cell || !ac) |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 409 | return false; |
Grant Likely | f3cea45 | 2013-10-04 17:24:26 +0100 | [diff] [blame] | 410 | prop_len /= sizeof(*cell) * ac; |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 411 | for (tid = 0; tid < prop_len; tid++) { |
| 412 | hwid = of_read_number(cell, ac); |
| 413 | if (arch_match_cpu_phys_id(cpu, hwid)) { |
| 414 | if (thread) |
| 415 | *thread = tid; |
| 416 | return true; |
| 417 | } |
| 418 | cell += ac; |
| 419 | } |
| 420 | return false; |
| 421 | } |
| 422 | |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 423 | /* |
| 424 | * arch_find_n_match_cpu_physical_id - See if the given device node is |
| 425 | * for the cpu corresponding to logical cpu 'cpu'. Return true if so, |
| 426 | * else false. If 'thread' is non-NULL, the local thread number within the |
| 427 | * core is returned in it. |
| 428 | */ |
| 429 | bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun, |
| 430 | int cpu, unsigned int *thread) |
| 431 | { |
| 432 | /* Check for non-standard "ibm,ppc-interrupt-server#s" property |
| 433 | * for thread ids on PowerPC. If it doesn't exist fallback to |
| 434 | * standard "reg" property. |
| 435 | */ |
| 436 | if (IS_ENABLED(CONFIG_PPC) && |
| 437 | __of_find_n_match_cpu_property(cpun, |
| 438 | "ibm,ppc-interrupt-server#s", |
| 439 | cpu, thread)) |
| 440 | return true; |
| 441 | |
| 442 | if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread)) |
| 443 | return true; |
| 444 | |
| 445 | return false; |
| 446 | } |
| 447 | |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 448 | /** |
| 449 | * of_get_cpu_node - Get device node associated with the given logical CPU |
| 450 | * |
| 451 | * @cpu: CPU number(logical index) for which device node is required |
| 452 | * @thread: if not NULL, local thread number within the physical core is |
| 453 | * returned |
| 454 | * |
| 455 | * The main purpose of this function is to retrieve the device node for the |
| 456 | * given logical CPU index. It should be used to initialize the of_node in |
| 457 | * cpu device. Once of_node in cpu device is populated, all the further |
| 458 | * references can use that instead. |
| 459 | * |
| 460 | * CPU logical to physical index mapping is architecture specific and is built |
| 461 | * before booting secondary cores. This function uses arch_match_cpu_phys_id |
| 462 | * which can be overridden by architecture specific implementation. |
| 463 | * |
| 464 | * Returns a node pointer for the logical cpu if found, else NULL. |
| 465 | */ |
| 466 | struct device_node *of_get_cpu_node(int cpu, unsigned int *thread) |
| 467 | { |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 468 | struct device_node *cpun; |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 469 | |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 470 | for_each_node_by_type(cpun, "cpu") { |
| 471 | if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread)) |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 472 | return cpun; |
| 473 | } |
| 474 | return NULL; |
| 475 | } |
| 476 | EXPORT_SYMBOL(of_get_cpu_node); |
| 477 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 478 | /** |
| 479 | * __of_device_is_compatible() - Check if the node matches given constraints |
| 480 | * @device: pointer to node |
| 481 | * @compat: required compatible string, NULL or "" for any match |
| 482 | * @type: required device_type value, NULL or "" for any match |
| 483 | * @name: required node name, NULL or "" for any match |
| 484 | * |
| 485 | * Checks if the given @compat, @type and @name strings match the |
| 486 | * properties of the given @device. A constraints can be skipped by |
| 487 | * passing NULL or an empty string as the constraint. |
| 488 | * |
| 489 | * Returns 0 for no match, and a positive integer on match. The return |
| 490 | * value is a relative score with larger values indicating better |
| 491 | * matches. The score is weighted for the most specific compatible value |
| 492 | * to get the highest score. Matching type is next, followed by matching |
| 493 | * name. Practically speaking, this results in the following priority |
| 494 | * order for matches: |
| 495 | * |
| 496 | * 1. specific compatible && type && name |
| 497 | * 2. specific compatible && type |
| 498 | * 3. specific compatible && name |
| 499 | * 4. specific compatible |
| 500 | * 5. general compatible && type && name |
| 501 | * 6. general compatible && type |
| 502 | * 7. general compatible && name |
| 503 | * 8. general compatible |
| 504 | * 9. type && name |
| 505 | * 10. type |
| 506 | * 11. name |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 507 | */ |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 508 | static int __of_device_is_compatible(const struct device_node *device, |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 509 | const char *compat, const char *type, const char *name) |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 510 | { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 511 | struct property *prop; |
| 512 | const char *cp; |
| 513 | int index = 0, score = 0; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 514 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 515 | /* Compatible match has highest priority */ |
| 516 | if (compat && compat[0]) { |
| 517 | prop = __of_find_property(device, "compatible", NULL); |
| 518 | for (cp = of_prop_next_string(prop, NULL); cp; |
| 519 | cp = of_prop_next_string(prop, cp), index++) { |
| 520 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) { |
| 521 | score = INT_MAX/2 - (index << 2); |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | if (!score) |
| 526 | return 0; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 527 | } |
| 528 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 529 | /* Matching type is better than matching name */ |
| 530 | if (type && type[0]) { |
| 531 | if (!device->type || of_node_cmp(type, device->type)) |
| 532 | return 0; |
| 533 | score += 2; |
| 534 | } |
| 535 | |
| 536 | /* Matching name is a bit better than not */ |
| 537 | if (name && name[0]) { |
| 538 | if (!device->name || of_node_cmp(name, device->name)) |
| 539 | return 0; |
| 540 | score++; |
| 541 | } |
| 542 | |
| 543 | return score; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 544 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 545 | |
| 546 | /** Checks if the given "compat" string matches one of the strings in |
| 547 | * the device's "compatible" property |
| 548 | */ |
| 549 | int of_device_is_compatible(const struct device_node *device, |
| 550 | const char *compat) |
| 551 | { |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 552 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 553 | int res; |
| 554 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 555 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 556 | res = __of_device_is_compatible(device, compat, NULL, NULL); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 557 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 558 | return res; |
| 559 | } |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 560 | EXPORT_SYMBOL(of_device_is_compatible); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 561 | |
| 562 | /** |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 563 | * 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] | 564 | * @compat: compatible string to look for in root node's compatible property. |
| 565 | * |
| 566 | * Returns true if the root node has the given value in its |
| 567 | * compatible property. |
| 568 | */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 569 | int of_machine_is_compatible(const char *compat) |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 570 | { |
| 571 | struct device_node *root; |
| 572 | int rc = 0; |
| 573 | |
| 574 | root = of_find_node_by_path("/"); |
| 575 | if (root) { |
| 576 | rc = of_device_is_compatible(root, compat); |
| 577 | of_node_put(root); |
| 578 | } |
| 579 | return rc; |
| 580 | } |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 581 | EXPORT_SYMBOL(of_machine_is_compatible); |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 582 | |
| 583 | /** |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 584 | * __of_device_is_available - check if a device is available for use |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 585 | * |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 586 | * @device: Node to check for availability, with locks already held |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 587 | * |
| 588 | * Returns 1 if the status property is absent or set to "okay" or "ok", |
| 589 | * 0 otherwise |
| 590 | */ |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 591 | static int __of_device_is_available(const struct device_node *device) |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 592 | { |
| 593 | const char *status; |
| 594 | int statlen; |
| 595 | |
Xiubo Li | 42ccd78 | 2014-01-13 11:07:28 +0800 | [diff] [blame] | 596 | if (!device) |
| 597 | return 0; |
| 598 | |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 599 | status = __of_get_property(device, "status", &statlen); |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 600 | if (status == NULL) |
| 601 | return 1; |
| 602 | |
| 603 | if (statlen > 0) { |
| 604 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) |
| 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 610 | |
| 611 | /** |
| 612 | * of_device_is_available - check if a device is available for use |
| 613 | * |
| 614 | * @device: Node to check for availability |
| 615 | * |
| 616 | * Returns 1 if the status property is absent or set to "okay" or "ok", |
| 617 | * 0 otherwise |
| 618 | */ |
| 619 | int of_device_is_available(const struct device_node *device) |
| 620 | { |
| 621 | unsigned long flags; |
| 622 | int res; |
| 623 | |
| 624 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 625 | res = __of_device_is_available(device); |
| 626 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 627 | return res; |
| 628 | |
| 629 | } |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 630 | EXPORT_SYMBOL(of_device_is_available); |
| 631 | |
| 632 | /** |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 633 | * of_get_parent - Get a node's parent if any |
| 634 | * @node: Node to get parent |
| 635 | * |
| 636 | * Returns a node pointer with refcount incremented, use |
| 637 | * of_node_put() on it when done. |
| 638 | */ |
| 639 | struct device_node *of_get_parent(const struct device_node *node) |
| 640 | { |
| 641 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 642 | unsigned long flags; |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 643 | |
| 644 | if (!node) |
| 645 | return NULL; |
| 646 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 647 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 648 | np = of_node_get(node->parent); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 649 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 650 | return np; |
| 651 | } |
| 652 | EXPORT_SYMBOL(of_get_parent); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 653 | |
| 654 | /** |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 655 | * of_get_next_parent - Iterate to a node's parent |
| 656 | * @node: Node to get parent of |
| 657 | * |
| 658 | * This is like of_get_parent() except that it drops the |
| 659 | * refcount on the passed node, making it suitable for iterating |
| 660 | * through a node's parents. |
| 661 | * |
| 662 | * Returns a node pointer with refcount incremented, use |
| 663 | * of_node_put() on it when done. |
| 664 | */ |
| 665 | struct device_node *of_get_next_parent(struct device_node *node) |
| 666 | { |
| 667 | struct device_node *parent; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 668 | unsigned long flags; |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 669 | |
| 670 | if (!node) |
| 671 | return NULL; |
| 672 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 673 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 674 | parent = of_node_get(node->parent); |
| 675 | of_node_put(node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 676 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 677 | return parent; |
| 678 | } |
Guennadi Liakhovetski | 6695be6 | 2013-04-02 12:28:11 -0300 | [diff] [blame] | 679 | EXPORT_SYMBOL(of_get_next_parent); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 680 | |
| 681 | /** |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 682 | * of_get_next_child - Iterate a node childs |
| 683 | * @node: parent node |
| 684 | * @prev: previous child of the parent node, or NULL to get first |
| 685 | * |
| 686 | * Returns a node pointer with refcount incremented, use |
| 687 | * of_node_put() on it when done. |
| 688 | */ |
| 689 | struct device_node *of_get_next_child(const struct device_node *node, |
| 690 | struct device_node *prev) |
| 691 | { |
| 692 | struct device_node *next; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 693 | unsigned long flags; |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 694 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 695 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 696 | next = prev ? prev->sibling : node->child; |
| 697 | for (; next; next = next->sibling) |
| 698 | if (of_node_get(next)) |
| 699 | break; |
| 700 | of_node_put(prev); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 701 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 702 | return next; |
| 703 | } |
| 704 | EXPORT_SYMBOL(of_get_next_child); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 705 | |
| 706 | /** |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 707 | * of_get_next_available_child - Find the next available child node |
| 708 | * @node: parent node |
| 709 | * @prev: previous child of the parent node, or NULL to get first |
| 710 | * |
| 711 | * This function is like of_get_next_child(), except that it |
| 712 | * automatically skips any disabled nodes (i.e. status = "disabled"). |
| 713 | */ |
| 714 | struct device_node *of_get_next_available_child(const struct device_node *node, |
| 715 | struct device_node *prev) |
| 716 | { |
| 717 | struct device_node *next; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 718 | unsigned long flags; |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 719 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 720 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 721 | next = prev ? prev->sibling : node->child; |
| 722 | for (; next; next = next->sibling) { |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 723 | if (!__of_device_is_available(next)) |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 724 | continue; |
| 725 | if (of_node_get(next)) |
| 726 | break; |
| 727 | } |
| 728 | of_node_put(prev); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 729 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 730 | return next; |
| 731 | } |
| 732 | EXPORT_SYMBOL(of_get_next_available_child); |
| 733 | |
| 734 | /** |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 735 | * of_get_child_by_name - Find the child node by name for a given parent |
| 736 | * @node: parent node |
| 737 | * @name: child name to look for. |
| 738 | * |
| 739 | * This function looks for child node for given matching name |
| 740 | * |
| 741 | * Returns a node pointer if found, with refcount incremented, use |
| 742 | * of_node_put() on it when done. |
| 743 | * Returns NULL if node is not found. |
| 744 | */ |
| 745 | struct device_node *of_get_child_by_name(const struct device_node *node, |
| 746 | const char *name) |
| 747 | { |
| 748 | struct device_node *child; |
| 749 | |
| 750 | for_each_child_of_node(node, child) |
| 751 | if (child->name && (of_node_cmp(child->name, name) == 0)) |
| 752 | break; |
| 753 | return child; |
| 754 | } |
| 755 | EXPORT_SYMBOL(of_get_child_by_name); |
| 756 | |
| 757 | /** |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 758 | * of_find_node_by_path - Find a node matching a full OF path |
| 759 | * @path: The full path to match |
| 760 | * |
| 761 | * Returns a node pointer with refcount incremented, use |
| 762 | * of_node_put() on it when done. |
| 763 | */ |
| 764 | struct device_node *of_find_node_by_path(const char *path) |
| 765 | { |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 766 | struct device_node *np = of_allnodes; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 767 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 768 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 769 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 770 | for (; np; np = np->allnext) { |
| 771 | if (np->full_name && (of_node_cmp(np->full_name, path) == 0) |
| 772 | && of_node_get(np)) |
| 773 | break; |
| 774 | } |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 775 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 776 | return np; |
| 777 | } |
| 778 | EXPORT_SYMBOL(of_find_node_by_path); |
| 779 | |
| 780 | /** |
| 781 | * of_find_node_by_name - Find a node by its "name" property |
| 782 | * @from: The node to start searching from or NULL, the node |
| 783 | * you pass will not be searched, only the next one |
| 784 | * will; typically, you pass what the previous call |
| 785 | * returned. of_node_put() will be called on it |
| 786 | * @name: The name string to match against |
| 787 | * |
| 788 | * Returns a node pointer with refcount incremented, use |
| 789 | * of_node_put() on it when done. |
| 790 | */ |
| 791 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 792 | const char *name) |
| 793 | { |
| 794 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 795 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 796 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 797 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 798 | np = from ? from->allnext : of_allnodes; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 799 | for (; np; np = np->allnext) |
| 800 | if (np->name && (of_node_cmp(np->name, name) == 0) |
| 801 | && of_node_get(np)) |
| 802 | break; |
| 803 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 804 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 805 | return np; |
| 806 | } |
| 807 | EXPORT_SYMBOL(of_find_node_by_name); |
| 808 | |
| 809 | /** |
| 810 | * of_find_node_by_type - Find a node by its "device_type" property |
| 811 | * @from: The node to start searching from, or NULL to start searching |
| 812 | * the entire device tree. The node you pass will not be |
| 813 | * searched, only the next one will; typically, you pass |
| 814 | * what the previous call returned. of_node_put() will be |
| 815 | * called on from for you. |
| 816 | * @type: The type string to match against |
| 817 | * |
| 818 | * Returns a node pointer with refcount incremented, use |
| 819 | * of_node_put() on it when done. |
| 820 | */ |
| 821 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 822 | const char *type) |
| 823 | { |
| 824 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 825 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 826 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 827 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 828 | np = from ? from->allnext : of_allnodes; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 829 | for (; np; np = np->allnext) |
| 830 | if (np->type && (of_node_cmp(np->type, type) == 0) |
| 831 | && of_node_get(np)) |
| 832 | break; |
| 833 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 834 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 835 | return np; |
| 836 | } |
| 837 | EXPORT_SYMBOL(of_find_node_by_type); |
| 838 | |
| 839 | /** |
| 840 | * of_find_compatible_node - Find a node based on type and one of the |
| 841 | * tokens in its "compatible" property |
| 842 | * @from: The node to start searching from or NULL, the node |
| 843 | * you pass will not be searched, only the next one |
| 844 | * will; typically, you pass what the previous call |
| 845 | * returned. of_node_put() will be called on it |
| 846 | * @type: The type string to match "device_type" or NULL to ignore |
| 847 | * @compatible: The string to match to one of the tokens in the device |
| 848 | * "compatible" list. |
| 849 | * |
| 850 | * Returns a node pointer with refcount incremented, use |
| 851 | * of_node_put() on it when done. |
| 852 | */ |
| 853 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 854 | const char *type, const char *compatible) |
| 855 | { |
| 856 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 857 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 858 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 859 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 860 | np = from ? from->allnext : of_allnodes; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 861 | for (; np; np = np->allnext) { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 862 | if (__of_device_is_compatible(np, compatible, type, NULL) && |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 863 | of_node_get(np)) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 867 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 868 | return np; |
| 869 | } |
| 870 | EXPORT_SYMBOL(of_find_compatible_node); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 871 | |
| 872 | /** |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 873 | * of_find_node_with_property - Find a node which has a property with |
| 874 | * the given name. |
| 875 | * @from: The node to start searching from or NULL, the node |
| 876 | * you pass will not be searched, only the next one |
| 877 | * will; typically, you pass what the previous call |
| 878 | * returned. of_node_put() will be called on it |
| 879 | * @prop_name: The name of the property to look for. |
| 880 | * |
| 881 | * Returns a node pointer with refcount incremented, use |
| 882 | * of_node_put() on it when done. |
| 883 | */ |
| 884 | struct device_node *of_find_node_with_property(struct device_node *from, |
| 885 | const char *prop_name) |
| 886 | { |
| 887 | struct device_node *np; |
| 888 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 889 | unsigned long flags; |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 890 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 891 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 892 | np = from ? from->allnext : of_allnodes; |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 893 | for (; np; np = np->allnext) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 894 | for (pp = np->properties; pp; pp = pp->next) { |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 895 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
| 896 | of_node_get(np); |
| 897 | goto out; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | out: |
| 902 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 903 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 904 | return np; |
| 905 | } |
| 906 | EXPORT_SYMBOL(of_find_node_with_property); |
| 907 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 908 | static |
| 909 | const struct of_device_id *__of_match_node(const struct of_device_id *matches, |
| 910 | const struct device_node *node) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 911 | { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 912 | const struct of_device_id *best_match = NULL; |
| 913 | int score, best_score = 0; |
Sebastian Hesselbarth | 1053531 | 2013-12-03 14:52:00 +0100 | [diff] [blame] | 914 | |
Grant Likely | a52f07e | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 915 | if (!matches) |
| 916 | return NULL; |
| 917 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 918 | for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) { |
| 919 | score = __of_device_is_compatible(node, matches->compatible, |
| 920 | matches->type, matches->name); |
| 921 | if (score > best_score) { |
| 922 | best_match = matches; |
| 923 | best_score = score; |
Sebastian Hesselbarth | 1053531 | 2013-12-03 14:52:00 +0100 | [diff] [blame] | 924 | } |
Kevin Hao | 4e8ca6e | 2014-02-14 13:22:45 +0800 | [diff] [blame] | 925 | } |
Sebastian Hesselbarth | 1053531 | 2013-12-03 14:52:00 +0100 | [diff] [blame] | 926 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 927 | return best_match; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 928 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 929 | |
| 930 | /** |
| 931 | * of_match_node - Tell if an device_node has a matching of_match structure |
| 932 | * @matches: array of of device match structures to search in |
| 933 | * @node: the of device structure to match against |
| 934 | * |
Kevin Hao | 71c5498e | 2014-02-18 15:57:29 +0800 | [diff] [blame] | 935 | * Low level utility function used by device matching. |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 936 | */ |
| 937 | const struct of_device_id *of_match_node(const struct of_device_id *matches, |
| 938 | const struct device_node *node) |
| 939 | { |
| 940 | const struct of_device_id *match; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 941 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 942 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 943 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 944 | match = __of_match_node(matches, node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 945 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 946 | return match; |
| 947 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 948 | EXPORT_SYMBOL(of_match_node); |
| 949 | |
| 950 | /** |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 951 | * of_find_matching_node_and_match - Find a node based on an of_device_id |
| 952 | * match table. |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 953 | * @from: The node to start searching from or NULL, the node |
| 954 | * you pass will not be searched, only the next one |
| 955 | * will; typically, you pass what the previous call |
| 956 | * returned. of_node_put() will be called on it |
| 957 | * @matches: array of of device match structures to search in |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 958 | * @match Updated to point at the matches entry which matched |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 959 | * |
| 960 | * Returns a node pointer with refcount incremented, use |
| 961 | * of_node_put() on it when done. |
| 962 | */ |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 963 | struct device_node *of_find_matching_node_and_match(struct device_node *from, |
| 964 | const struct of_device_id *matches, |
| 965 | const struct of_device_id **match) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 966 | { |
| 967 | struct device_node *np; |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 968 | const struct of_device_id *m; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 969 | unsigned long flags; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 970 | |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 971 | if (match) |
| 972 | *match = NULL; |
| 973 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 974 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 975 | np = from ? from->allnext : of_allnodes; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 976 | for (; np; np = np->allnext) { |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 977 | m = __of_match_node(matches, np); |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 978 | if (m && of_node_get(np)) { |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 979 | if (match) |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 980 | *match = m; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 981 | break; |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 982 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 983 | } |
| 984 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 985 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 986 | return np; |
| 987 | } |
Grant Likely | 80c2022 | 2012-12-19 10:45:36 +0000 | [diff] [blame] | 988 | EXPORT_SYMBOL(of_find_matching_node_and_match); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 989 | |
| 990 | /** |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 991 | * of_modalias_node - Lookup appropriate modalias for a device node |
| 992 | * @node: pointer to a device tree node |
| 993 | * @modalias: Pointer to buffer that modalias value will be copied into |
| 994 | * @len: Length of modalias value |
| 995 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 996 | * Based on the value of the compatible property, this routine will attempt |
| 997 | * to choose an appropriate modalias value for a particular device tree node. |
| 998 | * It does this by stripping the manufacturer prefix (as delimited by a ',') |
| 999 | * from the first entry in the compatible list property. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1000 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1001 | * This routine returns 0 on success, <0 on failure. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1002 | */ |
| 1003 | int of_modalias_node(struct device_node *node, char *modalias, int len) |
| 1004 | { |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1005 | const char *compatible, *p; |
| 1006 | int cplen; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1007 | |
| 1008 | compatible = of_get_property(node, "compatible", &cplen); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1009 | if (!compatible || strlen(compatible) > cplen) |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1010 | return -ENODEV; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1011 | p = strchr(compatible, ','); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1012 | strlcpy(modalias, p ? p + 1 : compatible, len); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1013 | return 0; |
| 1014 | } |
| 1015 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 1016 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1017 | /** |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1018 | * of_find_node_by_phandle - Find a node given a phandle |
| 1019 | * @handle: phandle of the node to find |
| 1020 | * |
| 1021 | * Returns a node pointer with refcount incremented, use |
| 1022 | * of_node_put() on it when done. |
| 1023 | */ |
| 1024 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 1025 | { |
| 1026 | struct device_node *np; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1027 | unsigned long flags; |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1028 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1029 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1030 | for (np = of_allnodes; np; np = np->allnext) |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1031 | if (np->phandle == handle) |
| 1032 | break; |
| 1033 | of_node_get(np); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1034 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1035 | return np; |
| 1036 | } |
| 1037 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 1038 | |
| 1039 | /** |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1040 | * of_find_property_value_of_size |
| 1041 | * |
| 1042 | * @np: device node from which the property value is to be read. |
| 1043 | * @propname: name of the property to be searched. |
| 1044 | * @len: requested length of property value |
| 1045 | * |
| 1046 | * Search for a property in a device node and valid the requested size. |
| 1047 | * Returns the property value on success, -EINVAL if the property does not |
| 1048 | * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 1049 | * property data isn't large enough. |
| 1050 | * |
| 1051 | */ |
| 1052 | static void *of_find_property_value_of_size(const struct device_node *np, |
| 1053 | const char *propname, u32 len) |
| 1054 | { |
| 1055 | struct property *prop = of_find_property(np, propname, NULL); |
| 1056 | |
| 1057 | if (!prop) |
| 1058 | return ERR_PTR(-EINVAL); |
| 1059 | if (!prop->value) |
| 1060 | return ERR_PTR(-ENODATA); |
| 1061 | if (len > prop->length) |
| 1062 | return ERR_PTR(-EOVERFLOW); |
| 1063 | |
| 1064 | return prop->value; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 1068 | * of_property_read_u32_index - Find and read a u32 from a multi-value property. |
| 1069 | * |
| 1070 | * @np: device node from which the property value is to be read. |
| 1071 | * @propname: name of the property to be searched. |
| 1072 | * @index: index of the u32 in the list of values |
| 1073 | * @out_value: pointer to return value, modified only if no error. |
| 1074 | * |
| 1075 | * Search for a property in a device node and read nth 32-bit value from |
| 1076 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 1077 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 1078 | * property data isn't large enough. |
| 1079 | * |
| 1080 | * The out_value is modified only if a valid u32 value can be decoded. |
| 1081 | */ |
| 1082 | int of_property_read_u32_index(const struct device_node *np, |
| 1083 | const char *propname, |
| 1084 | u32 index, u32 *out_value) |
| 1085 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1086 | const u32 *val = of_find_property_value_of_size(np, propname, |
| 1087 | ((index + 1) * sizeof(*out_value))); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 1088 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1089 | if (IS_ERR(val)) |
| 1090 | return PTR_ERR(val); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 1091 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1092 | *out_value = be32_to_cpup(((__be32 *)val) + index); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | EXPORT_SYMBOL_GPL(of_property_read_u32_index); |
| 1096 | |
| 1097 | /** |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1098 | * of_property_read_u8_array - Find and read an array of u8 from a property. |
| 1099 | * |
| 1100 | * @np: device node from which the property value is to be read. |
| 1101 | * @propname: name of the property to be searched. |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1102 | * @out_values: pointer to return value, modified only if return value is 0. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1103 | * @sz: number of array elements to read |
| 1104 | * |
| 1105 | * Search for a property in a device node and read 8-bit value(s) from |
| 1106 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 1107 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 1108 | * property data isn't large enough. |
| 1109 | * |
| 1110 | * dts entry of array should be like: |
| 1111 | * property = /bits/ 8 <0x50 0x60 0x70>; |
| 1112 | * |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1113 | * 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] | 1114 | */ |
| 1115 | int of_property_read_u8_array(const struct device_node *np, |
| 1116 | const char *propname, u8 *out_values, size_t sz) |
| 1117 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1118 | const u8 *val = of_find_property_value_of_size(np, propname, |
| 1119 | (sz * sizeof(*out_values))); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1120 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1121 | if (IS_ERR(val)) |
| 1122 | return PTR_ERR(val); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1123 | |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1124 | while (sz--) |
| 1125 | *out_values++ = *val++; |
| 1126 | return 0; |
| 1127 | } |
| 1128 | EXPORT_SYMBOL_GPL(of_property_read_u8_array); |
| 1129 | |
| 1130 | /** |
| 1131 | * of_property_read_u16_array - Find and read an array of u16 from a property. |
| 1132 | * |
| 1133 | * @np: device node from which the property value is to be read. |
| 1134 | * @propname: name of the property to be searched. |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1135 | * @out_values: pointer to return value, modified only if return value is 0. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1136 | * @sz: number of array elements to read |
| 1137 | * |
| 1138 | * Search for a property in a device node and read 16-bit value(s) from |
| 1139 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 1140 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 1141 | * property data isn't large enough. |
| 1142 | * |
| 1143 | * dts entry of array should be like: |
| 1144 | * property = /bits/ 16 <0x5000 0x6000 0x7000>; |
| 1145 | * |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1146 | * 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] | 1147 | */ |
| 1148 | int of_property_read_u16_array(const struct device_node *np, |
| 1149 | const char *propname, u16 *out_values, size_t sz) |
| 1150 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1151 | const __be16 *val = of_find_property_value_of_size(np, propname, |
| 1152 | (sz * sizeof(*out_values))); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1153 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1154 | if (IS_ERR(val)) |
| 1155 | return PTR_ERR(val); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1156 | |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1157 | while (sz--) |
| 1158 | *out_values++ = be16_to_cpup(val++); |
| 1159 | return 0; |
| 1160 | } |
| 1161 | EXPORT_SYMBOL_GPL(of_property_read_u16_array); |
| 1162 | |
| 1163 | /** |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1164 | * of_property_read_u32_array - Find and read an array of 32 bit integers |
| 1165 | * from a property. |
| 1166 | * |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1167 | * @np: device node from which the property value is to be read. |
| 1168 | * @propname: name of the property to be searched. |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1169 | * @out_values: pointer to return value, modified only if return value is 0. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 1170 | * @sz: number of array elements to read |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1171 | * |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1172 | * Search for a property in a device node and read 32-bit value(s) from |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1173 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 1174 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 1175 | * property data isn't large enough. |
| 1176 | * |
Lad, Prabhakar | 792efb8 | 2013-05-07 11:34:11 +0530 | [diff] [blame] | 1177 | * 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] | 1178 | */ |
Jamie Iles | aac285c | 2011-08-02 15:45:07 +0100 | [diff] [blame] | 1179 | int of_property_read_u32_array(const struct device_node *np, |
| 1180 | const char *propname, u32 *out_values, |
| 1181 | size_t sz) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1182 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1183 | const __be32 *val = of_find_property_value_of_size(np, propname, |
| 1184 | (sz * sizeof(*out_values))); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1185 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1186 | if (IS_ERR(val)) |
| 1187 | return PTR_ERR(val); |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1188 | |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1189 | while (sz--) |
| 1190 | *out_values++ = be32_to_cpup(val++); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 1193 | EXPORT_SYMBOL_GPL(of_property_read_u32_array); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1194 | |
| 1195 | /** |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 1196 | * of_property_read_u64 - Find and read a 64 bit integer from a property |
| 1197 | * @np: device node from which the property value is to be read. |
| 1198 | * @propname: name of the property to be searched. |
| 1199 | * @out_value: pointer to return value, modified only if return value is 0. |
| 1200 | * |
| 1201 | * Search for a property in a device node and read a 64-bit value from |
| 1202 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 1203 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 1204 | * property data isn't large enough. |
| 1205 | * |
| 1206 | * The out_value is modified only if a valid u64 value can be decoded. |
| 1207 | */ |
| 1208 | int of_property_read_u64(const struct device_node *np, const char *propname, |
| 1209 | u64 *out_value) |
| 1210 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1211 | const __be32 *val = of_find_property_value_of_size(np, propname, |
| 1212 | sizeof(*out_value)); |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 1213 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 1214 | if (IS_ERR(val)) |
| 1215 | return PTR_ERR(val); |
| 1216 | |
| 1217 | *out_value = of_read_number(val, 2); |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 1218 | return 0; |
| 1219 | } |
| 1220 | EXPORT_SYMBOL_GPL(of_property_read_u64); |
| 1221 | |
| 1222 | /** |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1223 | * of_property_read_string - Find and read a string from a property |
| 1224 | * @np: device node from which the property value is to be read. |
| 1225 | * @propname: name of the property to be searched. |
| 1226 | * @out_string: pointer to null terminated return string, modified only if |
| 1227 | * return value is 0. |
| 1228 | * |
| 1229 | * Search for a property in a device tree node and retrieve a null |
| 1230 | * terminated string value (pointer to data, not a copy). Returns 0 on |
| 1231 | * success, -EINVAL if the property does not exist, -ENODATA if property |
| 1232 | * does not have a value, and -EILSEQ if the string is not null-terminated |
| 1233 | * within the length of the property data. |
| 1234 | * |
| 1235 | * The out_string pointer is modified only if a valid string can be decoded. |
| 1236 | */ |
Jamie Iles | aac285c | 2011-08-02 15:45:07 +0100 | [diff] [blame] | 1237 | int of_property_read_string(struct device_node *np, const char *propname, |
Shawn Guo | f09bc83 | 2011-07-04 09:01:18 +0800 | [diff] [blame] | 1238 | const char **out_string) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 1239 | { |
| 1240 | struct property *prop = of_find_property(np, propname, NULL); |
| 1241 | if (!prop) |
| 1242 | return -EINVAL; |
| 1243 | if (!prop->value) |
| 1244 | return -ENODATA; |
| 1245 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 1246 | return -EILSEQ; |
| 1247 | *out_string = prop->value; |
| 1248 | return 0; |
| 1249 | } |
| 1250 | EXPORT_SYMBOL_GPL(of_property_read_string); |
| 1251 | |
| 1252 | /** |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1253 | * of_property_read_string_index - Find and read a string from a multiple |
| 1254 | * strings property. |
| 1255 | * @np: device node from which the property value is to be read. |
| 1256 | * @propname: name of the property to be searched. |
| 1257 | * @index: index of the string in the list of strings |
| 1258 | * @out_string: pointer to null terminated return string, modified only if |
| 1259 | * return value is 0. |
| 1260 | * |
| 1261 | * Search for a property in a device tree node and retrieve a null |
| 1262 | * terminated string value (pointer to data, not a copy) in the list of strings |
| 1263 | * contained in that property. |
| 1264 | * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if |
| 1265 | * property does not have a value, and -EILSEQ if the string is not |
| 1266 | * null-terminated within the length of the property data. |
| 1267 | * |
| 1268 | * The out_string pointer is modified only if a valid string can be decoded. |
| 1269 | */ |
| 1270 | int of_property_read_string_index(struct device_node *np, const char *propname, |
| 1271 | int index, const char **output) |
| 1272 | { |
| 1273 | struct property *prop = of_find_property(np, propname, NULL); |
| 1274 | int i = 0; |
| 1275 | size_t l = 0, total = 0; |
| 1276 | const char *p; |
| 1277 | |
| 1278 | if (!prop) |
| 1279 | return -EINVAL; |
| 1280 | if (!prop->value) |
| 1281 | return -ENODATA; |
| 1282 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 1283 | return -EILSEQ; |
| 1284 | |
| 1285 | p = prop->value; |
| 1286 | |
| 1287 | for (i = 0; total < prop->length; total += l, p += l) { |
| 1288 | l = strlen(p) + 1; |
Benoit Cousson | 88af7f5 | 2011-12-05 15:23:54 +0100 | [diff] [blame] | 1289 | if (i++ == index) { |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1290 | *output = p; |
| 1291 | return 0; |
| 1292 | } |
| 1293 | } |
| 1294 | return -ENODATA; |
| 1295 | } |
| 1296 | EXPORT_SYMBOL_GPL(of_property_read_string_index); |
| 1297 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 1298 | /** |
| 1299 | * of_property_match_string() - Find string in a list and return index |
| 1300 | * @np: pointer to node containing string list property |
| 1301 | * @propname: string list property name |
| 1302 | * @string: pointer to string to search for in string list |
| 1303 | * |
| 1304 | * This function searches a string list property and returns the index |
| 1305 | * of a specific string value. |
| 1306 | */ |
| 1307 | int of_property_match_string(struct device_node *np, const char *propname, |
| 1308 | const char *string) |
| 1309 | { |
| 1310 | struct property *prop = of_find_property(np, propname, NULL); |
| 1311 | size_t l; |
| 1312 | int i; |
| 1313 | const char *p, *end; |
| 1314 | |
| 1315 | if (!prop) |
| 1316 | return -EINVAL; |
| 1317 | if (!prop->value) |
| 1318 | return -ENODATA; |
| 1319 | |
| 1320 | p = prop->value; |
| 1321 | end = p + prop->length; |
| 1322 | |
| 1323 | for (i = 0; p < end; i++, p += l) { |
| 1324 | l = strlen(p) + 1; |
| 1325 | if (p + l > end) |
| 1326 | return -EILSEQ; |
| 1327 | pr_debug("comparing %s with %s\n", string, p); |
| 1328 | if (strcmp(string, p) == 0) |
| 1329 | return i; /* Found it; return index */ |
| 1330 | } |
| 1331 | return -ENODATA; |
| 1332 | } |
| 1333 | EXPORT_SYMBOL_GPL(of_property_match_string); |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1334 | |
| 1335 | /** |
| 1336 | * of_property_count_strings - Find and return the number of strings from a |
| 1337 | * multiple strings property. |
| 1338 | * @np: device node from which the property value is to be read. |
| 1339 | * @propname: name of the property to be searched. |
| 1340 | * |
| 1341 | * Search for a property in a device tree node and retrieve the number of null |
| 1342 | * terminated string contain in it. Returns the number of strings on |
| 1343 | * success, -EINVAL if the property does not exist, -ENODATA if property |
| 1344 | * does not have a value, and -EILSEQ if the string is not null-terminated |
| 1345 | * within the length of the property data. |
| 1346 | */ |
| 1347 | int of_property_count_strings(struct device_node *np, const char *propname) |
| 1348 | { |
| 1349 | struct property *prop = of_find_property(np, propname, NULL); |
| 1350 | int i = 0; |
| 1351 | size_t l = 0, total = 0; |
| 1352 | const char *p; |
| 1353 | |
| 1354 | if (!prop) |
| 1355 | return -EINVAL; |
| 1356 | if (!prop->value) |
| 1357 | return -ENODATA; |
| 1358 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 1359 | return -EILSEQ; |
| 1360 | |
| 1361 | p = prop->value; |
| 1362 | |
Benoit Cousson | 88af7f5 | 2011-12-05 15:23:54 +0100 | [diff] [blame] | 1363 | for (i = 0; total < prop->length; total += l, p += l, i++) |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1364 | l = strlen(p) + 1; |
Benoit Cousson | 88af7f5 | 2011-12-05 15:23:54 +0100 | [diff] [blame] | 1365 | |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1366 | return i; |
| 1367 | } |
| 1368 | EXPORT_SYMBOL_GPL(of_property_count_strings); |
| 1369 | |
Grant Likely | 624cfca | 2013-10-11 22:05:10 +0100 | [diff] [blame] | 1370 | void of_print_phandle_args(const char *msg, const struct of_phandle_args *args) |
| 1371 | { |
| 1372 | int i; |
| 1373 | printk("%s %s", msg, of_node_full_name(args->np)); |
| 1374 | for (i = 0; i < args->args_count; i++) |
| 1375 | printk(i ? ",%08x" : ":%08x", args->args[i]); |
| 1376 | printk("\n"); |
| 1377 | } |
| 1378 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1379 | static int __of_parse_phandle_with_args(const struct device_node *np, |
| 1380 | const char *list_name, |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1381 | const char *cells_name, |
| 1382 | int cell_count, int index, |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1383 | struct of_phandle_args *out_args) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1384 | { |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1385 | const __be32 *list, *list_end; |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1386 | int rc = 0, size, cur_index = 0; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1387 | uint32_t count = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1388 | struct device_node *node = NULL; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1389 | phandle phandle; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1390 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1391 | /* Retrieve the phandle list property */ |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1392 | list = of_get_property(np, list_name, &size); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1393 | if (!list) |
Alexandre Courbot | 1af4c7f | 2012-06-29 13:57:58 +0900 | [diff] [blame] | 1394 | return -ENOENT; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1395 | list_end = list + size / sizeof(*list); |
| 1396 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1397 | /* Loop over the phandles until all the requested entry is found */ |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1398 | while (list < list_end) { |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1399 | rc = -EINVAL; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1400 | count = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1401 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1402 | /* |
| 1403 | * If phandle is 0, then it is an empty entry with no |
| 1404 | * arguments. Skip forward to the next entry. |
| 1405 | */ |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 1406 | phandle = be32_to_cpup(list++); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1407 | if (phandle) { |
| 1408 | /* |
| 1409 | * Find the provider node and parse the #*-cells |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1410 | * property to determine the argument length. |
| 1411 | * |
| 1412 | * This is not needed if the cell count is hard-coded |
| 1413 | * (i.e. cells_name not set, but cell_count is set), |
| 1414 | * except when we're going to return the found node |
| 1415 | * below. |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1416 | */ |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1417 | if (cells_name || cur_index == index) { |
| 1418 | node = of_find_node_by_phandle(phandle); |
| 1419 | if (!node) { |
| 1420 | pr_err("%s: could not find phandle\n", |
| 1421 | np->full_name); |
| 1422 | goto err; |
| 1423 | } |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1424 | } |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1425 | |
| 1426 | if (cells_name) { |
| 1427 | if (of_property_read_u32(node, cells_name, |
| 1428 | &count)) { |
| 1429 | pr_err("%s: could not get %s for %s\n", |
| 1430 | np->full_name, cells_name, |
| 1431 | node->full_name); |
| 1432 | goto err; |
| 1433 | } |
| 1434 | } else { |
| 1435 | count = cell_count; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1436 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1437 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1438 | /* |
| 1439 | * Make sure that the arguments actually fit in the |
| 1440 | * remaining property data length |
| 1441 | */ |
| 1442 | if (list + count > list_end) { |
| 1443 | pr_err("%s: arguments longer than property\n", |
| 1444 | np->full_name); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1445 | goto err; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1446 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1449 | /* |
| 1450 | * All of the error cases above bail out of the loop, so at |
| 1451 | * this point, the parsing is successful. If the requested |
| 1452 | * index matches, then fill the out_args structure and return, |
| 1453 | * or return -ENOENT for an empty entry. |
| 1454 | */ |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1455 | rc = -ENOENT; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1456 | if (cur_index == index) { |
| 1457 | if (!phandle) |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1458 | goto err; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1459 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1460 | if (out_args) { |
| 1461 | int i; |
| 1462 | if (WARN_ON(count > MAX_PHANDLE_ARGS)) |
| 1463 | count = MAX_PHANDLE_ARGS; |
| 1464 | out_args->np = node; |
| 1465 | out_args->args_count = count; |
| 1466 | for (i = 0; i < count; i++) |
| 1467 | out_args->args[i] = be32_to_cpup(list++); |
Tang Yuantian | b855f16 | 2013-04-10 11:36:39 +0800 | [diff] [blame] | 1468 | } else { |
| 1469 | of_node_put(node); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1470 | } |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1471 | |
| 1472 | /* Found it! return success */ |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1473 | return 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1474 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1475 | |
| 1476 | of_node_put(node); |
| 1477 | node = NULL; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1478 | list += count; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1479 | cur_index++; |
| 1480 | } |
| 1481 | |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1482 | /* |
| 1483 | * Unlock node before returning result; will be one of: |
| 1484 | * -ENOENT : index is for empty phandle |
| 1485 | * -EINVAL : parsing error on data |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1486 | * [1..n] : Number of phandle (count mode; when index = -1) |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1487 | */ |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1488 | rc = index < 0 ? cur_index : -ENOENT; |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1489 | err: |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1490 | if (node) |
| 1491 | of_node_put(node); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1492 | return rc; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1493 | } |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1494 | |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1495 | /** |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1496 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
| 1497 | * @np: Pointer to device node holding phandle property |
| 1498 | * @phandle_name: Name of property holding a phandle value |
| 1499 | * @index: For properties holding a table of phandles, this is the index into |
| 1500 | * the table |
| 1501 | * |
| 1502 | * Returns the device_node pointer with refcount incremented. Use |
| 1503 | * of_node_put() on it when done. |
| 1504 | */ |
| 1505 | struct device_node *of_parse_phandle(const struct device_node *np, |
| 1506 | const char *phandle_name, int index) |
| 1507 | { |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1508 | struct of_phandle_args args; |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1509 | |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1510 | if (index < 0) |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1511 | return NULL; |
| 1512 | |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1513 | if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, |
| 1514 | index, &args)) |
| 1515 | return NULL; |
| 1516 | |
| 1517 | return args.np; |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1518 | } |
| 1519 | EXPORT_SYMBOL(of_parse_phandle); |
| 1520 | |
| 1521 | /** |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1522 | * of_parse_phandle_with_args() - Find a node pointed by phandle in a list |
| 1523 | * @np: pointer to a device tree node containing a list |
| 1524 | * @list_name: property name that contains a list |
| 1525 | * @cells_name: property name that specifies phandles' arguments count |
| 1526 | * @index: index of a phandle to parse out |
| 1527 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 1528 | * |
| 1529 | * This function is useful to parse lists of phandles and their arguments. |
| 1530 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 1531 | * errno value. |
| 1532 | * |
| 1533 | * Caller is responsible to call of_node_put() on the returned out_args->node |
| 1534 | * pointer. |
| 1535 | * |
| 1536 | * Example: |
| 1537 | * |
| 1538 | * phandle1: node1 { |
| 1539 | * #list-cells = <2>; |
| 1540 | * } |
| 1541 | * |
| 1542 | * phandle2: node2 { |
| 1543 | * #list-cells = <1>; |
| 1544 | * } |
| 1545 | * |
| 1546 | * node3 { |
| 1547 | * list = <&phandle1 1 2 &phandle2 3>; |
| 1548 | * } |
| 1549 | * |
| 1550 | * To get a device_node of the `node2' node you may call this: |
| 1551 | * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); |
| 1552 | */ |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1553 | int of_parse_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1554 | const char *cells_name, int index, |
| 1555 | struct of_phandle_args *out_args) |
| 1556 | { |
| 1557 | if (index < 0) |
| 1558 | return -EINVAL; |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1559 | return __of_parse_phandle_with_args(np, list_name, cells_name, 0, |
| 1560 | index, out_args); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1561 | } |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1562 | EXPORT_SYMBOL(of_parse_phandle_with_args); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1563 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1564 | /** |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1565 | * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list |
| 1566 | * @np: pointer to a device tree node containing a list |
| 1567 | * @list_name: property name that contains a list |
| 1568 | * @cell_count: number of argument cells following the phandle |
| 1569 | * @index: index of a phandle to parse out |
| 1570 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 1571 | * |
| 1572 | * This function is useful to parse lists of phandles and their arguments. |
| 1573 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 1574 | * errno value. |
| 1575 | * |
| 1576 | * Caller is responsible to call of_node_put() on the returned out_args->node |
| 1577 | * pointer. |
| 1578 | * |
| 1579 | * Example: |
| 1580 | * |
| 1581 | * phandle1: node1 { |
| 1582 | * } |
| 1583 | * |
| 1584 | * phandle2: node2 { |
| 1585 | * } |
| 1586 | * |
| 1587 | * node3 { |
| 1588 | * list = <&phandle1 0 2 &phandle2 2 3>; |
| 1589 | * } |
| 1590 | * |
| 1591 | * To get a device_node of the `node2' node you may call this: |
| 1592 | * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args); |
| 1593 | */ |
| 1594 | int of_parse_phandle_with_fixed_args(const struct device_node *np, |
| 1595 | const char *list_name, int cell_count, |
| 1596 | int index, struct of_phandle_args *out_args) |
| 1597 | { |
| 1598 | if (index < 0) |
| 1599 | return -EINVAL; |
| 1600 | return __of_parse_phandle_with_args(np, list_name, NULL, cell_count, |
| 1601 | index, out_args); |
| 1602 | } |
| 1603 | EXPORT_SYMBOL(of_parse_phandle_with_fixed_args); |
| 1604 | |
| 1605 | /** |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1606 | * of_count_phandle_with_args() - Find the number of phandles references in a property |
| 1607 | * @np: pointer to a device tree node containing a list |
| 1608 | * @list_name: property name that contains a list |
| 1609 | * @cells_name: property name that specifies phandles' arguments count |
| 1610 | * |
| 1611 | * Returns the number of phandle + argument tuples within a property. It |
| 1612 | * is a typical pattern to encode a list of phandle and variable |
| 1613 | * arguments into a single property. The number of arguments is encoded |
| 1614 | * by a property in the phandle-target node. For example, a gpios |
| 1615 | * property would contain a list of GPIO specifies consisting of a |
| 1616 | * phandle and 1 or more arguments. The number of arguments are |
| 1617 | * determined by the #gpio-cells property in the node pointed to by the |
| 1618 | * phandle. |
| 1619 | */ |
| 1620 | int of_count_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1621 | const char *cells_name) |
| 1622 | { |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1623 | return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1, |
| 1624 | NULL); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1625 | } |
| 1626 | EXPORT_SYMBOL(of_count_phandle_with_args); |
| 1627 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1628 | #if defined(CONFIG_OF_DYNAMIC) |
| 1629 | static int of_property_notify(int action, struct device_node *np, |
| 1630 | struct property *prop) |
| 1631 | { |
| 1632 | struct of_prop_reconfig pr; |
| 1633 | |
| 1634 | pr.dn = np; |
| 1635 | pr.prop = prop; |
| 1636 | return of_reconfig_notify(action, &pr); |
| 1637 | } |
| 1638 | #else |
| 1639 | static int of_property_notify(int action, struct device_node *np, |
| 1640 | struct property *prop) |
| 1641 | { |
| 1642 | return 0; |
| 1643 | } |
| 1644 | #endif |
| 1645 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1646 | /** |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1647 | * __of_add_property - Add a property to a node without lock operations |
| 1648 | */ |
| 1649 | static int __of_add_property(struct device_node *np, struct property *prop) |
| 1650 | { |
| 1651 | struct property **next; |
| 1652 | |
| 1653 | prop->next = NULL; |
| 1654 | next = &np->properties; |
| 1655 | while (*next) { |
| 1656 | if (strcmp(prop->name, (*next)->name) == 0) |
| 1657 | /* duplicate ! don't insert it */ |
| 1658 | return -EEXIST; |
| 1659 | |
| 1660 | next = &(*next)->next; |
| 1661 | } |
| 1662 | *next = prop; |
| 1663 | |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1668 | * of_add_property - Add a property to a node |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1669 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1670 | int of_add_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1671 | { |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1672 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1673 | int rc; |
| 1674 | |
| 1675 | rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop); |
| 1676 | if (rc) |
| 1677 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1678 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1679 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1680 | rc = __of_add_property(np, prop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1681 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 1682 | if (rc) |
| 1683 | return rc; |
| 1684 | |
| 1685 | /* at early boot, bail hear and defer setup to of_init() */ |
| 1686 | if (!of_kset) |
| 1687 | return 0; |
| 1688 | |
| 1689 | __of_add_property_sysfs(np, prop); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1690 | |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1691 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1695 | * of_remove_property - Remove a property from a node. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1696 | * |
| 1697 | * Note that we don't actually remove it, since we have given out |
| 1698 | * who-knows-how-many pointers to the data using get-property. |
| 1699 | * Instead we just move the property to the "dead properties" |
| 1700 | * list, so it won't be found any more. |
| 1701 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1702 | int of_remove_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1703 | { |
| 1704 | struct property **next; |
| 1705 | unsigned long flags; |
| 1706 | int found = 0; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1707 | int rc; |
| 1708 | |
| 1709 | rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop); |
| 1710 | if (rc) |
| 1711 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1712 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1713 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1714 | next = &np->properties; |
| 1715 | while (*next) { |
| 1716 | if (*next == prop) { |
| 1717 | /* found the node */ |
| 1718 | *next = prop->next; |
| 1719 | prop->next = np->deadprops; |
| 1720 | np->deadprops = prop; |
| 1721 | found = 1; |
| 1722 | break; |
| 1723 | } |
| 1724 | next = &(*next)->next; |
| 1725 | } |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1726 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1727 | |
| 1728 | if (!found) |
| 1729 | return -ENODEV; |
| 1730 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 1731 | /* at early boot, bail hear and defer setup to of_init() */ |
| 1732 | if (!of_kset) |
| 1733 | return 0; |
| 1734 | |
| 1735 | sysfs_remove_bin_file(&np->kobj, &prop->attr); |
| 1736 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
| 1739 | |
| 1740 | /* |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1741 | * 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] | 1742 | * not exist, add it. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1743 | * |
| 1744 | * Note that we don't actually remove it, since we have given out |
| 1745 | * who-knows-how-many pointers to the data using get-property. |
| 1746 | * Instead we just move the property to the "dead properties" list, |
| 1747 | * and add the new property to the property list |
| 1748 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1749 | int of_update_property(struct device_node *np, struct property *newprop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1750 | { |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1751 | struct property **next, *oldprop; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1752 | unsigned long flags; |
Grant Likely | a3dbeb5 | 2014-03-04 16:07:17 +0800 | [diff] [blame] | 1753 | int rc, found = 0; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1754 | |
| 1755 | rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop); |
| 1756 | if (rc) |
| 1757 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1758 | |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1759 | if (!newprop->name) |
| 1760 | return -EINVAL; |
| 1761 | |
Grant Likely | a3dbeb5 | 2014-03-04 16:07:17 +0800 | [diff] [blame] | 1762 | oldprop = of_find_property(np, newprop->name, NULL); |
| 1763 | if (!oldprop) |
| 1764 | return of_add_property(np, newprop); |
| 1765 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1766 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | a3dbeb5 | 2014-03-04 16:07:17 +0800 | [diff] [blame] | 1767 | next = &np->properties; |
| 1768 | while (*next) { |
| 1769 | if (*next == oldprop) { |
| 1770 | /* found the node */ |
| 1771 | newprop->next = oldprop->next; |
| 1772 | *next = newprop; |
| 1773 | oldprop->next = np->deadprops; |
| 1774 | np->deadprops = oldprop; |
| 1775 | found = 1; |
| 1776 | break; |
| 1777 | } |
| 1778 | next = &(*next)->next; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1779 | } |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1780 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 1781 | if (rc) |
| 1782 | return rc; |
| 1783 | |
| 1784 | /* Update the sysfs attribute */ |
| 1785 | if (oldprop) |
| 1786 | sysfs_remove_bin_file(&np->kobj, &oldprop->attr); |
| 1787 | __of_add_property_sysfs(np, newprop); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1788 | |
Grant Likely | a3dbeb5 | 2014-03-04 16:07:17 +0800 | [diff] [blame] | 1789 | if (!found) |
| 1790 | return -ENODEV; |
| 1791 | |
Grant Likely | a3dbeb5 | 2014-03-04 16:07:17 +0800 | [diff] [blame] | 1792 | return 0; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1793 | } |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1794 | |
| 1795 | #if defined(CONFIG_OF_DYNAMIC) |
| 1796 | /* |
| 1797 | * Support for dynamic device trees. |
| 1798 | * |
| 1799 | * On some platforms, the device tree can be manipulated at runtime. |
| 1800 | * The routines in this section support adding, removing and changing |
| 1801 | * device tree nodes. |
| 1802 | */ |
| 1803 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1804 | static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); |
| 1805 | |
| 1806 | int of_reconfig_notifier_register(struct notifier_block *nb) |
| 1807 | { |
| 1808 | return blocking_notifier_chain_register(&of_reconfig_chain, nb); |
| 1809 | } |
Nathan Fontenot | 1a9bd45 | 2012-11-28 09:42:26 +0000 | [diff] [blame] | 1810 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1811 | |
| 1812 | int of_reconfig_notifier_unregister(struct notifier_block *nb) |
| 1813 | { |
| 1814 | return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); |
| 1815 | } |
Nathan Fontenot | 1a9bd45 | 2012-11-28 09:42:26 +0000 | [diff] [blame] | 1816 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1817 | |
| 1818 | int of_reconfig_notify(unsigned long action, void *p) |
| 1819 | { |
| 1820 | int rc; |
| 1821 | |
| 1822 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); |
| 1823 | return notifier_to_errno(rc); |
| 1824 | } |
| 1825 | |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1826 | /** |
| 1827 | * of_attach_node - Plug a device node into the tree and global list. |
| 1828 | */ |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1829 | int of_attach_node(struct device_node *np) |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1830 | { |
| 1831 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1832 | int rc; |
| 1833 | |
| 1834 | rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); |
| 1835 | if (rc) |
| 1836 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1837 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1838 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1839 | np->sibling = np->parent->child; |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1840 | np->allnext = of_allnodes; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1841 | np->parent->child = np; |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1842 | of_allnodes = np; |
Pantelis Antoniou | e3963fd | 2013-11-08 17:03:57 +0200 | [diff] [blame] | 1843 | of_node_clear_flag(np, OF_DETACHED); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1844 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1845 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 1846 | of_node_add(np); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1847 | return 0; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | /** |
| 1851 | * of_detach_node - "Unplug" a node from the device tree. |
| 1852 | * |
| 1853 | * The caller must hold a reference to the node. The memory associated with |
| 1854 | * the node is not freed until its refcount goes to zero. |
| 1855 | */ |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1856 | int of_detach_node(struct device_node *np) |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1857 | { |
| 1858 | struct device_node *parent; |
| 1859 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1860 | int rc = 0; |
| 1861 | |
| 1862 | rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); |
| 1863 | if (rc) |
| 1864 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1865 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1866 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1867 | |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1868 | if (of_node_check_flag(np, OF_DETACHED)) { |
| 1869 | /* someone already detached it */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1870 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1871 | return rc; |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1874 | parent = np->parent; |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1875 | if (!parent) { |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1876 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1877 | return rc; |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1878 | } |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1879 | |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1880 | if (of_allnodes == np) |
| 1881 | of_allnodes = np->allnext; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1882 | else { |
| 1883 | struct device_node *prev; |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1884 | for (prev = of_allnodes; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1885 | prev->allnext != np; |
| 1886 | prev = prev->allnext) |
| 1887 | ; |
| 1888 | prev->allnext = np->allnext; |
| 1889 | } |
| 1890 | |
| 1891 | if (parent->child == np) |
| 1892 | parent->child = np->sibling; |
| 1893 | else { |
| 1894 | struct device_node *prevsib; |
| 1895 | for (prevsib = np->parent->child; |
| 1896 | prevsib->sibling != np; |
| 1897 | prevsib = prevsib->sibling) |
| 1898 | ; |
| 1899 | prevsib->sibling = np->sibling; |
| 1900 | } |
| 1901 | |
| 1902 | of_node_set_flag(np, OF_DETACHED); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1903 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1904 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 1905 | of_node_remove(np); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1906 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1907 | } |
| 1908 | #endif /* defined(CONFIG_OF_DYNAMIC) */ |
| 1909 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1910 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
| 1911 | int id, const char *stem, int stem_len) |
| 1912 | { |
| 1913 | ap->np = np; |
| 1914 | ap->id = id; |
| 1915 | strncpy(ap->stem, stem, stem_len); |
| 1916 | ap->stem[stem_len] = 0; |
| 1917 | list_add_tail(&ap->link, &aliases_lookup); |
| 1918 | 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] | 1919 | ap->alias, ap->stem, ap->id, of_node_full_name(np)); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /** |
| 1923 | * of_alias_scan - Scan all properties of 'aliases' node |
| 1924 | * |
| 1925 | * The function scans all the properties of 'aliases' node and populate |
| 1926 | * the the global lookup table with the properties. It returns the |
| 1927 | * number of alias_prop found, or error code in error case. |
| 1928 | * |
| 1929 | * @dt_alloc: An allocator that provides a virtual address to memory |
| 1930 | * for the resulting tree |
| 1931 | */ |
| 1932 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) |
| 1933 | { |
| 1934 | struct property *pp; |
| 1935 | |
| 1936 | of_chosen = of_find_node_by_path("/chosen"); |
| 1937 | if (of_chosen == NULL) |
| 1938 | of_chosen = of_find_node_by_path("/chosen@0"); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 1939 | |
| 1940 | if (of_chosen) { |
| 1941 | const char *name; |
| 1942 | |
| 1943 | name = of_get_property(of_chosen, "linux,stdout-path", NULL); |
| 1944 | if (name) |
| 1945 | of_stdout = of_find_node_by_path(name); |
| 1946 | } |
| 1947 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1948 | of_aliases = of_find_node_by_path("/aliases"); |
| 1949 | if (!of_aliases) |
| 1950 | return; |
| 1951 | |
Dong Aisheng | 8af0da9 | 2011-12-22 20:19:24 +0800 | [diff] [blame] | 1952 | for_each_property_of_node(of_aliases, pp) { |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1953 | const char *start = pp->name; |
| 1954 | const char *end = start + strlen(start); |
| 1955 | struct device_node *np; |
| 1956 | struct alias_prop *ap; |
| 1957 | int id, len; |
| 1958 | |
| 1959 | /* Skip those we do not want to proceed */ |
| 1960 | if (!strcmp(pp->name, "name") || |
| 1961 | !strcmp(pp->name, "phandle") || |
| 1962 | !strcmp(pp->name, "linux,phandle")) |
| 1963 | continue; |
| 1964 | |
| 1965 | np = of_find_node_by_path(pp->value); |
| 1966 | if (!np) |
| 1967 | continue; |
| 1968 | |
| 1969 | /* walk the alias backwards to extract the id and work out |
| 1970 | * the 'stem' string */ |
| 1971 | while (isdigit(*(end-1)) && end > start) |
| 1972 | end--; |
| 1973 | len = end - start; |
| 1974 | |
| 1975 | if (kstrtoint(end, 10, &id) < 0) |
| 1976 | continue; |
| 1977 | |
| 1978 | /* Allocate an alias_prop with enough space for the stem */ |
| 1979 | ap = dt_alloc(sizeof(*ap) + len + 1, 4); |
| 1980 | if (!ap) |
| 1981 | continue; |
Grant Likely | 0640332e | 2013-08-28 21:24:17 +0100 | [diff] [blame] | 1982 | memset(ap, 0, sizeof(*ap) + len + 1); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1983 | ap->alias = start; |
| 1984 | of_alias_add(ap, np, id, start, len); |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | /** |
| 1989 | * of_alias_get_id - Get alias id for the given device_node |
| 1990 | * @np: Pointer to the given device_node |
| 1991 | * @stem: Alias stem of the given device_node |
| 1992 | * |
| 1993 | * The function travels the lookup table to get alias id for the given |
| 1994 | * device_node and alias stem. It returns the alias id if find it. |
| 1995 | */ |
| 1996 | int of_alias_get_id(struct device_node *np, const char *stem) |
| 1997 | { |
| 1998 | struct alias_prop *app; |
| 1999 | int id = -ENODEV; |
| 2000 | |
| 2001 | mutex_lock(&of_aliases_mutex); |
| 2002 | list_for_each_entry(app, &aliases_lookup, link) { |
| 2003 | if (strcmp(app->stem, stem) != 0) |
| 2004 | continue; |
| 2005 | |
| 2006 | if (np == app->np) { |
| 2007 | id = app->id; |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | mutex_unlock(&of_aliases_mutex); |
| 2012 | |
| 2013 | return id; |
| 2014 | } |
| 2015 | EXPORT_SYMBOL_GPL(of_alias_get_id); |
Stephen Warren | c541adc | 2012-04-04 09:27:46 -0600 | [diff] [blame] | 2016 | |
| 2017 | const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur, |
| 2018 | u32 *pu) |
| 2019 | { |
| 2020 | const void *curv = cur; |
| 2021 | |
| 2022 | if (!prop) |
| 2023 | return NULL; |
| 2024 | |
| 2025 | if (!cur) { |
| 2026 | curv = prop->value; |
| 2027 | goto out_val; |
| 2028 | } |
| 2029 | |
| 2030 | curv += sizeof(*cur); |
| 2031 | if (curv >= prop->value + prop->length) |
| 2032 | return NULL; |
| 2033 | |
| 2034 | out_val: |
| 2035 | *pu = be32_to_cpup(curv); |
| 2036 | return curv; |
| 2037 | } |
| 2038 | EXPORT_SYMBOL_GPL(of_prop_next_u32); |
| 2039 | |
| 2040 | const char *of_prop_next_string(struct property *prop, const char *cur) |
| 2041 | { |
| 2042 | const void *curv = cur; |
| 2043 | |
| 2044 | if (!prop) |
| 2045 | return NULL; |
| 2046 | |
| 2047 | if (!cur) |
| 2048 | return prop->value; |
| 2049 | |
| 2050 | curv += strlen(cur) + 1; |
| 2051 | if (curv >= prop->value + prop->length) |
| 2052 | return NULL; |
| 2053 | |
| 2054 | return curv; |
| 2055 | } |
| 2056 | EXPORT_SYMBOL_GPL(of_prop_next_string); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2057 | |
| 2058 | /** |
| 2059 | * of_device_is_stdout_path - check if a device node matches the |
| 2060 | * linux,stdout-path property |
| 2061 | * |
| 2062 | * Check if this device node matches the linux,stdout-path property |
| 2063 | * in the chosen node. return true if yes, false otherwise. |
| 2064 | */ |
| 2065 | int of_device_is_stdout_path(struct device_node *dn) |
| 2066 | { |
| 2067 | if (!of_stdout) |
| 2068 | return false; |
| 2069 | |
| 2070 | return of_stdout == dn; |
| 2071 | } |
| 2072 | EXPORT_SYMBOL_GPL(of_device_is_stdout_path); |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2073 | |
| 2074 | /** |
| 2075 | * of_find_next_cache_node - Find a node's subsidiary cache |
| 2076 | * @np: node of type "cpu" or "cache" |
| 2077 | * |
| 2078 | * Returns a node pointer with refcount incremented, use |
| 2079 | * of_node_put() on it when done. Caller should hold a reference |
| 2080 | * to np. |
| 2081 | */ |
| 2082 | struct device_node *of_find_next_cache_node(const struct device_node *np) |
| 2083 | { |
| 2084 | struct device_node *child; |
| 2085 | const phandle *handle; |
| 2086 | |
| 2087 | handle = of_get_property(np, "l2-cache", NULL); |
| 2088 | if (!handle) |
| 2089 | handle = of_get_property(np, "next-level-cache", NULL); |
| 2090 | |
| 2091 | if (handle) |
| 2092 | return of_find_node_by_phandle(be32_to_cpup(handle)); |
| 2093 | |
| 2094 | /* OF on pmac has nodes instead of properties named "l2-cache" |
| 2095 | * beneath CPU nodes. |
| 2096 | */ |
| 2097 | if (!strcmp(np->type, "cpu")) |
| 2098 | for_each_child_of_node(np, child) |
| 2099 | if (!strcmp(child->type, "cache")) |
| 2100 | return child; |
| 2101 | |
| 2102 | return NULL; |
| 2103 | } |