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