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