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> |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 23 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Jeremy Kerr | a9f2f63 | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 25 | #include <linux/proc_fs.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 26 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 27 | #include "of_private.h" |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 28 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 29 | LIST_HEAD(aliases_lookup); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 30 | |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 31 | struct device_node *of_allnodes; |
| 32 | EXPORT_SYMBOL(of_allnodes); |
Grant Likely | fc0bdae | 2010-02-14 07:13:55 -0700 | [diff] [blame] | 33 | struct device_node *of_chosen; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 34 | struct device_node *of_aliases; |
| 35 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 36 | DEFINE_MUTEX(of_aliases_mutex); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 37 | |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 38 | /* use when traversing tree through the allnext, child, sibling, |
| 39 | * or parent members of struct device_node. |
| 40 | */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 41 | DEFINE_RAW_SPINLOCK(devtree_lock); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 42 | |
| 43 | int of_n_addr_cells(struct device_node *np) |
| 44 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 45 | const __be32 *ip; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 46 | |
| 47 | do { |
| 48 | if (np->parent) |
| 49 | np = np->parent; |
| 50 | ip = of_get_property(np, "#address-cells", NULL); |
| 51 | if (ip) |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 52 | return be32_to_cpup(ip); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 53 | } while (np->parent); |
| 54 | /* No #address-cells property for the root node */ |
| 55 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 56 | } |
| 57 | EXPORT_SYMBOL(of_n_addr_cells); |
| 58 | |
| 59 | int of_n_size_cells(struct device_node *np) |
| 60 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 61 | const __be32 *ip; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 62 | |
| 63 | do { |
| 64 | if (np->parent) |
| 65 | np = np->parent; |
| 66 | ip = of_get_property(np, "#size-cells", NULL); |
| 67 | if (ip) |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 68 | return be32_to_cpup(ip); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 69 | } while (np->parent); |
| 70 | /* No #size-cells property for the root node */ |
| 71 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 72 | } |
| 73 | EXPORT_SYMBOL(of_n_size_cells); |
| 74 | |
Grant Likely | 0f22dd3 | 2012-02-15 20:38:40 -0700 | [diff] [blame] | 75 | #if defined(CONFIG_OF_DYNAMIC) |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 76 | /** |
| 77 | * of_node_get - Increment refcount of a node |
| 78 | * @node: Node to inc refcount, NULL is supported to |
| 79 | * simplify writing of callers |
| 80 | * |
| 81 | * Returns node. |
| 82 | */ |
| 83 | struct device_node *of_node_get(struct device_node *node) |
| 84 | { |
| 85 | if (node) |
| 86 | kref_get(&node->kref); |
| 87 | return node; |
| 88 | } |
| 89 | EXPORT_SYMBOL(of_node_get); |
| 90 | |
| 91 | static inline struct device_node *kref_to_device_node(struct kref *kref) |
| 92 | { |
| 93 | return container_of(kref, struct device_node, kref); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * of_node_release - release a dynamically allocated node |
| 98 | * @kref: kref element of the node to be released |
| 99 | * |
| 100 | * In of_node_put() this function is passed to kref_put() |
| 101 | * as the destructor. |
| 102 | */ |
| 103 | static void of_node_release(struct kref *kref) |
| 104 | { |
| 105 | struct device_node *node = kref_to_device_node(kref); |
| 106 | struct property *prop = node->properties; |
| 107 | |
| 108 | /* We should never be releasing nodes that haven't been detached. */ |
| 109 | if (!of_node_check_flag(node, OF_DETACHED)) { |
| 110 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); |
| 111 | dump_stack(); |
| 112 | kref_init(&node->kref); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | if (!of_node_check_flag(node, OF_DYNAMIC)) |
| 117 | return; |
| 118 | |
| 119 | while (prop) { |
| 120 | struct property *next = prop->next; |
| 121 | kfree(prop->name); |
| 122 | kfree(prop->value); |
| 123 | kfree(prop); |
| 124 | prop = next; |
| 125 | |
| 126 | if (!prop) { |
| 127 | prop = node->deadprops; |
| 128 | node->deadprops = NULL; |
| 129 | } |
| 130 | } |
| 131 | kfree(node->full_name); |
| 132 | kfree(node->data); |
| 133 | kfree(node); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * of_node_put - Decrement refcount of a node |
| 138 | * @node: Node to dec refcount, NULL is supported to |
| 139 | * simplify writing of callers |
| 140 | * |
| 141 | */ |
| 142 | void of_node_put(struct device_node *node) |
| 143 | { |
| 144 | if (node) |
| 145 | kref_put(&node->kref, of_node_release); |
| 146 | } |
| 147 | EXPORT_SYMBOL(of_node_put); |
Grant Likely | 0f22dd3 | 2012-02-15 20:38:40 -0700 | [diff] [blame] | 148 | #endif /* CONFIG_OF_DYNAMIC */ |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 149 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 150 | static struct property *__of_find_property(const struct device_node *np, |
| 151 | const char *name, int *lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 152 | { |
| 153 | struct property *pp; |
| 154 | |
Timur Tabi | 64e4566 | 2008-05-08 05:19:59 +1000 | [diff] [blame] | 155 | if (!np) |
| 156 | return NULL; |
| 157 | |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 158 | for (pp = np->properties; pp; pp = pp->next) { |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 159 | if (of_prop_cmp(pp->name, name) == 0) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 160 | if (lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 161 | *lenp = pp->length; |
| 162 | break; |
| 163 | } |
| 164 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 165 | |
| 166 | return pp; |
| 167 | } |
| 168 | |
| 169 | struct property *of_find_property(const struct device_node *np, |
| 170 | const char *name, |
| 171 | int *lenp) |
| 172 | { |
| 173 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 174 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 175 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 176 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 177 | pp = __of_find_property(np, name, lenp); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 178 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 179 | |
| 180 | return pp; |
| 181 | } |
| 182 | EXPORT_SYMBOL(of_find_property); |
| 183 | |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 184 | /** |
| 185 | * of_find_all_nodes - Get next node in global list |
| 186 | * @prev: Previous node or NULL to start iteration |
| 187 | * of_node_put() will be called on it |
| 188 | * |
| 189 | * Returns a node pointer with refcount incremented, use |
| 190 | * of_node_put() on it when done. |
| 191 | */ |
| 192 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 193 | { |
| 194 | struct device_node *np; |
| 195 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 196 | raw_spin_lock(&devtree_lock); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 197 | np = prev ? prev->allnext : of_allnodes; |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 198 | for (; np != NULL; np = np->allnext) |
| 199 | if (of_node_get(np)) |
| 200 | break; |
| 201 | of_node_put(prev); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 202 | raw_spin_unlock(&devtree_lock); |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 203 | return np; |
| 204 | } |
| 205 | EXPORT_SYMBOL(of_find_all_nodes); |
| 206 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 207 | /* |
| 208 | * Find a property with a given name for a given node |
| 209 | * and return the value. |
| 210 | */ |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 211 | static const void *__of_get_property(const struct device_node *np, |
| 212 | const char *name, int *lenp) |
| 213 | { |
| 214 | struct property *pp = __of_find_property(np, name, lenp); |
| 215 | |
| 216 | return pp ? pp->value : NULL; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Find a property with a given name for a given node |
| 221 | * and return the value. |
| 222 | */ |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 223 | 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] | 224 | int *lenp) |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 225 | { |
| 226 | struct property *pp = of_find_property(np, name, lenp); |
| 227 | |
| 228 | return pp ? pp->value : NULL; |
| 229 | } |
| 230 | EXPORT_SYMBOL(of_get_property); |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 231 | |
| 232 | /** Checks if the given "compat" string matches one of the strings in |
| 233 | * the device's "compatible" property |
| 234 | */ |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 235 | static int __of_device_is_compatible(const struct device_node *device, |
| 236 | const char *compat) |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 237 | { |
| 238 | const char* cp; |
| 239 | int cplen, l; |
| 240 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 241 | cp = __of_get_property(device, "compatible", &cplen); |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 242 | if (cp == NULL) |
| 243 | return 0; |
| 244 | while (cplen > 0) { |
| 245 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) |
| 246 | return 1; |
| 247 | l = strlen(cp) + 1; |
| 248 | cp += l; |
| 249 | cplen -= l; |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 254 | |
| 255 | /** Checks if the given "compat" string matches one of the strings in |
| 256 | * the device's "compatible" property |
| 257 | */ |
| 258 | int of_device_is_compatible(const struct device_node *device, |
| 259 | const char *compat) |
| 260 | { |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 261 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 262 | int res; |
| 263 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 264 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 265 | res = __of_device_is_compatible(device, compat); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 266 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 267 | return res; |
| 268 | } |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 269 | EXPORT_SYMBOL(of_device_is_compatible); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 270 | |
| 271 | /** |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 272 | * 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] | 273 | * @compat: compatible string to look for in root node's compatible property. |
| 274 | * |
| 275 | * Returns true if the root node has the given value in its |
| 276 | * compatible property. |
| 277 | */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 278 | int of_machine_is_compatible(const char *compat) |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 279 | { |
| 280 | struct device_node *root; |
| 281 | int rc = 0; |
| 282 | |
| 283 | root = of_find_node_by_path("/"); |
| 284 | if (root) { |
| 285 | rc = of_device_is_compatible(root, compat); |
| 286 | of_node_put(root); |
| 287 | } |
| 288 | return rc; |
| 289 | } |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 290 | EXPORT_SYMBOL(of_machine_is_compatible); |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 291 | |
| 292 | /** |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 293 | * __of_device_is_available - check if a device is available for use |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 294 | * |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 295 | * @device: Node to check for availability, with locks already held |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 296 | * |
| 297 | * Returns 1 if the status property is absent or set to "okay" or "ok", |
| 298 | * 0 otherwise |
| 299 | */ |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 300 | static int __of_device_is_available(const struct device_node *device) |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 301 | { |
| 302 | const char *status; |
| 303 | int statlen; |
| 304 | |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 305 | status = __of_get_property(device, "status", &statlen); |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 306 | if (status == NULL) |
| 307 | return 1; |
| 308 | |
| 309 | if (statlen > 0) { |
| 310 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * of_device_is_available - check if a device is available for use |
| 319 | * |
| 320 | * @device: Node to check for availability |
| 321 | * |
| 322 | * Returns 1 if the status property is absent or set to "okay" or "ok", |
| 323 | * 0 otherwise |
| 324 | */ |
| 325 | int of_device_is_available(const struct device_node *device) |
| 326 | { |
| 327 | unsigned long flags; |
| 328 | int res; |
| 329 | |
| 330 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 331 | res = __of_device_is_available(device); |
| 332 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 333 | return res; |
| 334 | |
| 335 | } |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 336 | EXPORT_SYMBOL(of_device_is_available); |
| 337 | |
| 338 | /** |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 339 | * of_get_parent - Get a node's parent if any |
| 340 | * @node: Node to get parent |
| 341 | * |
| 342 | * Returns a node pointer with refcount incremented, use |
| 343 | * of_node_put() on it when done. |
| 344 | */ |
| 345 | struct device_node *of_get_parent(const struct device_node *node) |
| 346 | { |
| 347 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 348 | unsigned long flags; |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 349 | |
| 350 | if (!node) |
| 351 | return NULL; |
| 352 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 353 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 354 | np = of_node_get(node->parent); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 355 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 356 | return np; |
| 357 | } |
| 358 | EXPORT_SYMBOL(of_get_parent); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 359 | |
| 360 | /** |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 361 | * of_get_next_parent - Iterate to a node's parent |
| 362 | * @node: Node to get parent of |
| 363 | * |
| 364 | * This is like of_get_parent() except that it drops the |
| 365 | * refcount on the passed node, making it suitable for iterating |
| 366 | * through a node's parents. |
| 367 | * |
| 368 | * Returns a node pointer with refcount incremented, use |
| 369 | * of_node_put() on it when done. |
| 370 | */ |
| 371 | struct device_node *of_get_next_parent(struct device_node *node) |
| 372 | { |
| 373 | struct device_node *parent; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 374 | unsigned long flags; |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 375 | |
| 376 | if (!node) |
| 377 | return NULL; |
| 378 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 379 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 380 | parent = of_node_get(node->parent); |
| 381 | of_node_put(node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 382 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 383 | return parent; |
| 384 | } |
Guennadi Liakhovetski | 6695be6 | 2013-04-02 12:28:11 -0300 | [diff] [blame] | 385 | EXPORT_SYMBOL(of_get_next_parent); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 386 | |
| 387 | /** |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 388 | * of_get_next_child - Iterate a node childs |
| 389 | * @node: parent node |
| 390 | * @prev: previous child of the parent node, or NULL to get first |
| 391 | * |
| 392 | * Returns a node pointer with refcount incremented, use |
| 393 | * of_node_put() on it when done. |
| 394 | */ |
| 395 | struct device_node *of_get_next_child(const struct device_node *node, |
| 396 | struct device_node *prev) |
| 397 | { |
| 398 | struct device_node *next; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 399 | unsigned long flags; |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 400 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 401 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 402 | next = prev ? prev->sibling : node->child; |
| 403 | for (; next; next = next->sibling) |
| 404 | if (of_node_get(next)) |
| 405 | break; |
| 406 | of_node_put(prev); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 407 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 408 | return next; |
| 409 | } |
| 410 | EXPORT_SYMBOL(of_get_next_child); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 411 | |
| 412 | /** |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 413 | * of_get_next_available_child - Find the next available child node |
| 414 | * @node: parent node |
| 415 | * @prev: previous child of the parent node, or NULL to get first |
| 416 | * |
| 417 | * This function is like of_get_next_child(), except that it |
| 418 | * automatically skips any disabled nodes (i.e. status = "disabled"). |
| 419 | */ |
| 420 | struct device_node *of_get_next_available_child(const struct device_node *node, |
| 421 | struct device_node *prev) |
| 422 | { |
| 423 | struct device_node *next; |
| 424 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 425 | raw_spin_lock(&devtree_lock); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 426 | next = prev ? prev->sibling : node->child; |
| 427 | for (; next; next = next->sibling) { |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 428 | if (!__of_device_is_available(next)) |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 429 | continue; |
| 430 | if (of_node_get(next)) |
| 431 | break; |
| 432 | } |
| 433 | of_node_put(prev); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 434 | raw_spin_unlock(&devtree_lock); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 435 | return next; |
| 436 | } |
| 437 | EXPORT_SYMBOL(of_get_next_available_child); |
| 438 | |
| 439 | /** |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 440 | * of_get_child_by_name - Find the child node by name for a given parent |
| 441 | * @node: parent node |
| 442 | * @name: child name to look for. |
| 443 | * |
| 444 | * This function looks for child node for given matching name |
| 445 | * |
| 446 | * Returns a node pointer if found, with refcount incremented, use |
| 447 | * of_node_put() on it when done. |
| 448 | * Returns NULL if node is not found. |
| 449 | */ |
| 450 | struct device_node *of_get_child_by_name(const struct device_node *node, |
| 451 | const char *name) |
| 452 | { |
| 453 | struct device_node *child; |
| 454 | |
| 455 | for_each_child_of_node(node, child) |
| 456 | if (child->name && (of_node_cmp(child->name, name) == 0)) |
| 457 | break; |
| 458 | return child; |
| 459 | } |
| 460 | EXPORT_SYMBOL(of_get_child_by_name); |
| 461 | |
| 462 | /** |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 463 | * of_find_node_by_path - Find a node matching a full OF path |
| 464 | * @path: The full path to match |
| 465 | * |
| 466 | * Returns a node pointer with refcount incremented, use |
| 467 | * of_node_put() on it when done. |
| 468 | */ |
| 469 | struct device_node *of_find_node_by_path(const char *path) |
| 470 | { |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 471 | struct device_node *np = of_allnodes; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 472 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 473 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 474 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 475 | for (; np; np = np->allnext) { |
| 476 | if (np->full_name && (of_node_cmp(np->full_name, path) == 0) |
| 477 | && of_node_get(np)) |
| 478 | break; |
| 479 | } |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 480 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 481 | return np; |
| 482 | } |
| 483 | EXPORT_SYMBOL(of_find_node_by_path); |
| 484 | |
| 485 | /** |
| 486 | * of_find_node_by_name - Find a node by its "name" property |
| 487 | * @from: The node to start searching from or NULL, the node |
| 488 | * you pass will not be searched, only the next one |
| 489 | * will; typically, you pass what the previous call |
| 490 | * returned. of_node_put() will be called on it |
| 491 | * @name: The name string to match against |
| 492 | * |
| 493 | * Returns a node pointer with refcount incremented, use |
| 494 | * of_node_put() on it when done. |
| 495 | */ |
| 496 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 497 | const char *name) |
| 498 | { |
| 499 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 500 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 501 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 502 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 503 | np = from ? from->allnext : of_allnodes; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 504 | for (; np; np = np->allnext) |
| 505 | if (np->name && (of_node_cmp(np->name, name) == 0) |
| 506 | && of_node_get(np)) |
| 507 | break; |
| 508 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 509 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 510 | return np; |
| 511 | } |
| 512 | EXPORT_SYMBOL(of_find_node_by_name); |
| 513 | |
| 514 | /** |
| 515 | * of_find_node_by_type - Find a node by its "device_type" property |
| 516 | * @from: The node to start searching from, or NULL to start searching |
| 517 | * the entire device tree. The node you pass will not be |
| 518 | * searched, only the next one will; typically, you pass |
| 519 | * what the previous call returned. of_node_put() will be |
| 520 | * called on from for you. |
| 521 | * @type: The type string to match against |
| 522 | * |
| 523 | * Returns a node pointer with refcount incremented, use |
| 524 | * of_node_put() on it when done. |
| 525 | */ |
| 526 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 527 | const char *type) |
| 528 | { |
| 529 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 530 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 531 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 532 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 533 | np = from ? from->allnext : of_allnodes; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 534 | for (; np; np = np->allnext) |
| 535 | if (np->type && (of_node_cmp(np->type, type) == 0) |
| 536 | && of_node_get(np)) |
| 537 | break; |
| 538 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 539 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 540 | return np; |
| 541 | } |
| 542 | EXPORT_SYMBOL(of_find_node_by_type); |
| 543 | |
| 544 | /** |
| 545 | * of_find_compatible_node - Find a node based on type and one of the |
| 546 | * tokens in its "compatible" property |
| 547 | * @from: The node to start searching from or NULL, the node |
| 548 | * you pass will not be searched, only the next one |
| 549 | * will; typically, you pass what the previous call |
| 550 | * returned. of_node_put() will be called on it |
| 551 | * @type: The type string to match "device_type" or NULL to ignore |
| 552 | * @compatible: The string to match to one of the tokens in the device |
| 553 | * "compatible" list. |
| 554 | * |
| 555 | * Returns a node pointer with refcount incremented, use |
| 556 | * of_node_put() on it when done. |
| 557 | */ |
| 558 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 559 | const char *type, const char *compatible) |
| 560 | { |
| 561 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 562 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 563 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 564 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 565 | np = from ? from->allnext : of_allnodes; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 566 | for (; np; np = np->allnext) { |
| 567 | if (type |
| 568 | && !(np->type && (of_node_cmp(np->type, type) == 0))) |
| 569 | continue; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 570 | if (__of_device_is_compatible(np, compatible) && |
| 571 | of_node_get(np)) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 572 | break; |
| 573 | } |
| 574 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 575 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 576 | return np; |
| 577 | } |
| 578 | EXPORT_SYMBOL(of_find_compatible_node); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 579 | |
| 580 | /** |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 581 | * of_find_node_with_property - Find a node which has a property with |
| 582 | * the given name. |
| 583 | * @from: The node to start searching from or NULL, the node |
| 584 | * you pass will not be searched, only the next one |
| 585 | * will; typically, you pass what the previous call |
| 586 | * returned. of_node_put() will be called on it |
| 587 | * @prop_name: The name of the property to look for. |
| 588 | * |
| 589 | * Returns a node pointer with refcount incremented, use |
| 590 | * of_node_put() on it when done. |
| 591 | */ |
| 592 | struct device_node *of_find_node_with_property(struct device_node *from, |
| 593 | const char *prop_name) |
| 594 | { |
| 595 | struct device_node *np; |
| 596 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 597 | unsigned long flags; |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 598 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 599 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 600 | np = from ? from->allnext : of_allnodes; |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 601 | for (; np; np = np->allnext) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 602 | for (pp = np->properties; pp; pp = pp->next) { |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 603 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
| 604 | of_node_get(np); |
| 605 | goto out; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | out: |
| 610 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 611 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 612 | return np; |
| 613 | } |
| 614 | EXPORT_SYMBOL(of_find_node_with_property); |
| 615 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 616 | static |
| 617 | const struct of_device_id *__of_match_node(const struct of_device_id *matches, |
| 618 | const struct device_node *node) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 619 | { |
Grant Likely | a52f07e | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 620 | if (!matches) |
| 621 | return NULL; |
| 622 | |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 623 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { |
| 624 | int match = 1; |
| 625 | if (matches->name[0]) |
| 626 | match &= node->name |
| 627 | && !strcmp(matches->name, node->name); |
| 628 | if (matches->type[0]) |
| 629 | match &= node->type |
| 630 | && !strcmp(matches->type, node->type); |
Linus Torvalds | bc51b0c | 2012-07-10 12:49:32 -0700 | [diff] [blame] | 631 | if (matches->compatible[0]) |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 632 | match &= __of_device_is_compatible(node, |
| 633 | matches->compatible); |
Linus Torvalds | bc51b0c | 2012-07-10 12:49:32 -0700 | [diff] [blame] | 634 | if (match) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 635 | return matches; |
| 636 | matches++; |
| 637 | } |
| 638 | return NULL; |
| 639 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 640 | |
| 641 | /** |
| 642 | * of_match_node - Tell if an device_node has a matching of_match structure |
| 643 | * @matches: array of of device match structures to search in |
| 644 | * @node: the of device structure to match against |
| 645 | * |
| 646 | * Low level utility function used by device matching. |
| 647 | */ |
| 648 | const struct of_device_id *of_match_node(const struct of_device_id *matches, |
| 649 | const struct device_node *node) |
| 650 | { |
| 651 | const struct of_device_id *match; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 652 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 653 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 654 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 655 | match = __of_match_node(matches, node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 656 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 657 | return match; |
| 658 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 659 | EXPORT_SYMBOL(of_match_node); |
| 660 | |
| 661 | /** |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 662 | * of_find_matching_node_and_match - Find a node based on an of_device_id |
| 663 | * match table. |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 664 | * @from: The node to start searching from or NULL, the node |
| 665 | * you pass will not be searched, only the next one |
| 666 | * will; typically, you pass what the previous call |
| 667 | * returned. of_node_put() will be called on it |
| 668 | * @matches: array of of device match structures to search in |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 669 | * @match Updated to point at the matches entry which matched |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 670 | * |
| 671 | * Returns a node pointer with refcount incremented, use |
| 672 | * of_node_put() on it when done. |
| 673 | */ |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 674 | struct device_node *of_find_matching_node_and_match(struct device_node *from, |
| 675 | const struct of_device_id *matches, |
| 676 | const struct of_device_id **match) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 677 | { |
| 678 | struct device_node *np; |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 679 | const struct of_device_id *m; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 680 | unsigned long flags; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 681 | |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 682 | if (match) |
| 683 | *match = NULL; |
| 684 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 685 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 686 | np = from ? from->allnext : of_allnodes; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 687 | for (; np; np = np->allnext) { |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 688 | m = __of_match_node(matches, np); |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 689 | if (m && of_node_get(np)) { |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 690 | if (match) |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 691 | *match = m; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 692 | break; |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 693 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 694 | } |
| 695 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 696 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 697 | return np; |
| 698 | } |
Grant Likely | 80c2022 | 2012-12-19 10:45:36 +0000 | [diff] [blame] | 699 | EXPORT_SYMBOL(of_find_matching_node_and_match); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 700 | |
| 701 | /** |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 702 | * of_modalias_node - Lookup appropriate modalias for a device node |
| 703 | * @node: pointer to a device tree node |
| 704 | * @modalias: Pointer to buffer that modalias value will be copied into |
| 705 | * @len: Length of modalias value |
| 706 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 707 | * Based on the value of the compatible property, this routine will attempt |
| 708 | * to choose an appropriate modalias value for a particular device tree node. |
| 709 | * It does this by stripping the manufacturer prefix (as delimited by a ',') |
| 710 | * from the first entry in the compatible list property. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 711 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 712 | * This routine returns 0 on success, <0 on failure. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 713 | */ |
| 714 | int of_modalias_node(struct device_node *node, char *modalias, int len) |
| 715 | { |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 716 | const char *compatible, *p; |
| 717 | int cplen; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 718 | |
| 719 | compatible = of_get_property(node, "compatible", &cplen); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 720 | if (!compatible || strlen(compatible) > cplen) |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 721 | return -ENODEV; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 722 | p = strchr(compatible, ','); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 723 | strlcpy(modalias, p ? p + 1 : compatible, len); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 727 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 728 | /** |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 729 | * of_find_node_by_phandle - Find a node given a phandle |
| 730 | * @handle: phandle of the node to find |
| 731 | * |
| 732 | * Returns a node pointer with refcount incremented, use |
| 733 | * of_node_put() on it when done. |
| 734 | */ |
| 735 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 736 | { |
| 737 | struct device_node *np; |
| 738 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 739 | raw_spin_lock(&devtree_lock); |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 740 | for (np = of_allnodes; np; np = np->allnext) |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 741 | if (np->phandle == handle) |
| 742 | break; |
| 743 | of_node_get(np); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 744 | raw_spin_unlock(&devtree_lock); |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 745 | return np; |
| 746 | } |
| 747 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 748 | |
| 749 | /** |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 750 | * of_find_property_value_of_size |
| 751 | * |
| 752 | * @np: device node from which the property value is to be read. |
| 753 | * @propname: name of the property to be searched. |
| 754 | * @len: requested length of property value |
| 755 | * |
| 756 | * Search for a property in a device node and valid the requested size. |
| 757 | * Returns the property value on success, -EINVAL if the property does not |
| 758 | * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 759 | * property data isn't large enough. |
| 760 | * |
| 761 | */ |
| 762 | static void *of_find_property_value_of_size(const struct device_node *np, |
| 763 | const char *propname, u32 len) |
| 764 | { |
| 765 | struct property *prop = of_find_property(np, propname, NULL); |
| 766 | |
| 767 | if (!prop) |
| 768 | return ERR_PTR(-EINVAL); |
| 769 | if (!prop->value) |
| 770 | return ERR_PTR(-ENODATA); |
| 771 | if (len > prop->length) |
| 772 | return ERR_PTR(-EOVERFLOW); |
| 773 | |
| 774 | return prop->value; |
| 775 | } |
| 776 | |
| 777 | /** |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 778 | * of_property_read_u32_index - Find and read a u32 from a multi-value property. |
| 779 | * |
| 780 | * @np: device node from which the property value is to be read. |
| 781 | * @propname: name of the property to be searched. |
| 782 | * @index: index of the u32 in the list of values |
| 783 | * @out_value: pointer to return value, modified only if no error. |
| 784 | * |
| 785 | * Search for a property in a device node and read nth 32-bit value from |
| 786 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 787 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 788 | * property data isn't large enough. |
| 789 | * |
| 790 | * The out_value is modified only if a valid u32 value can be decoded. |
| 791 | */ |
| 792 | int of_property_read_u32_index(const struct device_node *np, |
| 793 | const char *propname, |
| 794 | u32 index, u32 *out_value) |
| 795 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 796 | const u32 *val = of_find_property_value_of_size(np, propname, |
| 797 | ((index + 1) * sizeof(*out_value))); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 798 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 799 | if (IS_ERR(val)) |
| 800 | return PTR_ERR(val); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 801 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 802 | *out_value = be32_to_cpup(((__be32 *)val) + index); |
Tony Prisk | 3daf372 | 2013-03-23 17:02:15 +1300 | [diff] [blame] | 803 | return 0; |
| 804 | } |
| 805 | EXPORT_SYMBOL_GPL(of_property_read_u32_index); |
| 806 | |
| 807 | /** |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 808 | * of_property_read_u8_array - Find and read an array of u8 from a property. |
| 809 | * |
| 810 | * @np: device node from which the property value is to be read. |
| 811 | * @propname: name of the property to be searched. |
| 812 | * @out_value: pointer to return value, modified only if return value is 0. |
| 813 | * @sz: number of array elements to read |
| 814 | * |
| 815 | * Search for a property in a device node and read 8-bit value(s) from |
| 816 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 817 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 818 | * property data isn't large enough. |
| 819 | * |
| 820 | * dts entry of array should be like: |
| 821 | * property = /bits/ 8 <0x50 0x60 0x70>; |
| 822 | * |
| 823 | * The out_value is modified only if a valid u8 value can be decoded. |
| 824 | */ |
| 825 | int of_property_read_u8_array(const struct device_node *np, |
| 826 | const char *propname, u8 *out_values, size_t sz) |
| 827 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 828 | const u8 *val = of_find_property_value_of_size(np, propname, |
| 829 | (sz * sizeof(*out_values))); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 830 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 831 | if (IS_ERR(val)) |
| 832 | return PTR_ERR(val); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 833 | |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 834 | while (sz--) |
| 835 | *out_values++ = *val++; |
| 836 | return 0; |
| 837 | } |
| 838 | EXPORT_SYMBOL_GPL(of_property_read_u8_array); |
| 839 | |
| 840 | /** |
| 841 | * of_property_read_u16_array - Find and read an array of u16 from a property. |
| 842 | * |
| 843 | * @np: device node from which the property value is to be read. |
| 844 | * @propname: name of the property to be searched. |
| 845 | * @out_value: pointer to return value, modified only if return value is 0. |
| 846 | * @sz: number of array elements to read |
| 847 | * |
| 848 | * Search for a property in a device node and read 16-bit value(s) from |
| 849 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 850 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 851 | * property data isn't large enough. |
| 852 | * |
| 853 | * dts entry of array should be like: |
| 854 | * property = /bits/ 16 <0x5000 0x6000 0x7000>; |
| 855 | * |
| 856 | * The out_value is modified only if a valid u16 value can be decoded. |
| 857 | */ |
| 858 | int of_property_read_u16_array(const struct device_node *np, |
| 859 | const char *propname, u16 *out_values, size_t sz) |
| 860 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 861 | const __be16 *val = of_find_property_value_of_size(np, propname, |
| 862 | (sz * sizeof(*out_values))); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 863 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 864 | if (IS_ERR(val)) |
| 865 | return PTR_ERR(val); |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 866 | |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 867 | while (sz--) |
| 868 | *out_values++ = be16_to_cpup(val++); |
| 869 | return 0; |
| 870 | } |
| 871 | EXPORT_SYMBOL_GPL(of_property_read_u16_array); |
| 872 | |
| 873 | /** |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 874 | * of_property_read_u32_array - Find and read an array of 32 bit integers |
| 875 | * from a property. |
| 876 | * |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 877 | * @np: device node from which the property value is to be read. |
| 878 | * @propname: name of the property to be searched. |
| 879 | * @out_value: pointer to return value, modified only if return value is 0. |
Viresh Kumar | be19324 | 2012-11-20 10:15:19 +0530 | [diff] [blame] | 880 | * @sz: number of array elements to read |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 881 | * |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 882 | * 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] | 883 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 884 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 885 | * property data isn't large enough. |
| 886 | * |
| 887 | * The out_value is modified only if a valid u32 value can be decoded. |
| 888 | */ |
Jamie Iles | aac285c | 2011-08-02 15:45:07 +0100 | [diff] [blame] | 889 | int of_property_read_u32_array(const struct device_node *np, |
| 890 | const char *propname, u32 *out_values, |
| 891 | size_t sz) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 892 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 893 | const __be32 *val = of_find_property_value_of_size(np, propname, |
| 894 | (sz * sizeof(*out_values))); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 895 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 896 | if (IS_ERR(val)) |
| 897 | return PTR_ERR(val); |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 898 | |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 899 | while (sz--) |
| 900 | *out_values++ = be32_to_cpup(val++); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 901 | return 0; |
| 902 | } |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 903 | EXPORT_SYMBOL_GPL(of_property_read_u32_array); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 904 | |
| 905 | /** |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 906 | * of_property_read_u64 - Find and read a 64 bit integer from a property |
| 907 | * @np: device node from which the property value is to be read. |
| 908 | * @propname: name of the property to be searched. |
| 909 | * @out_value: pointer to return value, modified only if return value is 0. |
| 910 | * |
| 911 | * Search for a property in a device node and read a 64-bit value from |
| 912 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 913 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 914 | * property data isn't large enough. |
| 915 | * |
| 916 | * The out_value is modified only if a valid u64 value can be decoded. |
| 917 | */ |
| 918 | int of_property_read_u64(const struct device_node *np, const char *propname, |
| 919 | u64 *out_value) |
| 920 | { |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 921 | const __be32 *val = of_find_property_value_of_size(np, propname, |
| 922 | sizeof(*out_value)); |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 923 | |
Tony Prisk | daeec1f | 2013-04-03 17:57:11 +1300 | [diff] [blame] | 924 | if (IS_ERR(val)) |
| 925 | return PTR_ERR(val); |
| 926 | |
| 927 | *out_value = of_read_number(val, 2); |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | EXPORT_SYMBOL_GPL(of_property_read_u64); |
| 931 | |
| 932 | /** |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 933 | * of_property_read_string - Find and read a string from a property |
| 934 | * @np: device node from which the property value is to be read. |
| 935 | * @propname: name of the property to be searched. |
| 936 | * @out_string: pointer to null terminated return string, modified only if |
| 937 | * return value is 0. |
| 938 | * |
| 939 | * Search for a property in a device tree node and retrieve a null |
| 940 | * terminated string value (pointer to data, not a copy). Returns 0 on |
| 941 | * success, -EINVAL if the property does not exist, -ENODATA if property |
| 942 | * does not have a value, and -EILSEQ if the string is not null-terminated |
| 943 | * within the length of the property data. |
| 944 | * |
| 945 | * The out_string pointer is modified only if a valid string can be decoded. |
| 946 | */ |
Jamie Iles | aac285c | 2011-08-02 15:45:07 +0100 | [diff] [blame] | 947 | int of_property_read_string(struct device_node *np, const char *propname, |
Shawn Guo | f09bc83 | 2011-07-04 09:01:18 +0800 | [diff] [blame] | 948 | const char **out_string) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 949 | { |
| 950 | struct property *prop = of_find_property(np, propname, NULL); |
| 951 | if (!prop) |
| 952 | return -EINVAL; |
| 953 | if (!prop->value) |
| 954 | return -ENODATA; |
| 955 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 956 | return -EILSEQ; |
| 957 | *out_string = prop->value; |
| 958 | return 0; |
| 959 | } |
| 960 | EXPORT_SYMBOL_GPL(of_property_read_string); |
| 961 | |
| 962 | /** |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 963 | * of_property_read_string_index - Find and read a string from a multiple |
| 964 | * strings property. |
| 965 | * @np: device node from which the property value is to be read. |
| 966 | * @propname: name of the property to be searched. |
| 967 | * @index: index of the string in the list of strings |
| 968 | * @out_string: pointer to null terminated return string, modified only if |
| 969 | * return value is 0. |
| 970 | * |
| 971 | * Search for a property in a device tree node and retrieve a null |
| 972 | * terminated string value (pointer to data, not a copy) in the list of strings |
| 973 | * contained in that property. |
| 974 | * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if |
| 975 | * property does not have a value, and -EILSEQ if the string is not |
| 976 | * null-terminated within the length of the property data. |
| 977 | * |
| 978 | * The out_string pointer is modified only if a valid string can be decoded. |
| 979 | */ |
| 980 | int of_property_read_string_index(struct device_node *np, const char *propname, |
| 981 | int index, const char **output) |
| 982 | { |
| 983 | struct property *prop = of_find_property(np, propname, NULL); |
| 984 | int i = 0; |
| 985 | size_t l = 0, total = 0; |
| 986 | const char *p; |
| 987 | |
| 988 | if (!prop) |
| 989 | return -EINVAL; |
| 990 | if (!prop->value) |
| 991 | return -ENODATA; |
| 992 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 993 | return -EILSEQ; |
| 994 | |
| 995 | p = prop->value; |
| 996 | |
| 997 | for (i = 0; total < prop->length; total += l, p += l) { |
| 998 | l = strlen(p) + 1; |
Benoit Cousson | 88af7f5 | 2011-12-05 15:23:54 +0100 | [diff] [blame] | 999 | if (i++ == index) { |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1000 | *output = p; |
| 1001 | return 0; |
| 1002 | } |
| 1003 | } |
| 1004 | return -ENODATA; |
| 1005 | } |
| 1006 | EXPORT_SYMBOL_GPL(of_property_read_string_index); |
| 1007 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 1008 | /** |
| 1009 | * of_property_match_string() - Find string in a list and return index |
| 1010 | * @np: pointer to node containing string list property |
| 1011 | * @propname: string list property name |
| 1012 | * @string: pointer to string to search for in string list |
| 1013 | * |
| 1014 | * This function searches a string list property and returns the index |
| 1015 | * of a specific string value. |
| 1016 | */ |
| 1017 | int of_property_match_string(struct device_node *np, const char *propname, |
| 1018 | const char *string) |
| 1019 | { |
| 1020 | struct property *prop = of_find_property(np, propname, NULL); |
| 1021 | size_t l; |
| 1022 | int i; |
| 1023 | const char *p, *end; |
| 1024 | |
| 1025 | if (!prop) |
| 1026 | return -EINVAL; |
| 1027 | if (!prop->value) |
| 1028 | return -ENODATA; |
| 1029 | |
| 1030 | p = prop->value; |
| 1031 | end = p + prop->length; |
| 1032 | |
| 1033 | for (i = 0; p < end; i++, p += l) { |
| 1034 | l = strlen(p) + 1; |
| 1035 | if (p + l > end) |
| 1036 | return -EILSEQ; |
| 1037 | pr_debug("comparing %s with %s\n", string, p); |
| 1038 | if (strcmp(string, p) == 0) |
| 1039 | return i; /* Found it; return index */ |
| 1040 | } |
| 1041 | return -ENODATA; |
| 1042 | } |
| 1043 | EXPORT_SYMBOL_GPL(of_property_match_string); |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1044 | |
| 1045 | /** |
| 1046 | * of_property_count_strings - Find and return the number of strings from a |
| 1047 | * multiple strings property. |
| 1048 | * @np: device node from which the property value is to be read. |
| 1049 | * @propname: name of the property to be searched. |
| 1050 | * |
| 1051 | * Search for a property in a device tree node and retrieve the number of null |
| 1052 | * terminated string contain in it. Returns the number of strings on |
| 1053 | * success, -EINVAL if the property does not exist, -ENODATA if property |
| 1054 | * does not have a value, and -EILSEQ if the string is not null-terminated |
| 1055 | * within the length of the property data. |
| 1056 | */ |
| 1057 | int of_property_count_strings(struct device_node *np, const char *propname) |
| 1058 | { |
| 1059 | struct property *prop = of_find_property(np, propname, NULL); |
| 1060 | int i = 0; |
| 1061 | size_t l = 0, total = 0; |
| 1062 | const char *p; |
| 1063 | |
| 1064 | if (!prop) |
| 1065 | return -EINVAL; |
| 1066 | if (!prop->value) |
| 1067 | return -ENODATA; |
| 1068 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 1069 | return -EILSEQ; |
| 1070 | |
| 1071 | p = prop->value; |
| 1072 | |
Benoit Cousson | 88af7f5 | 2011-12-05 15:23:54 +0100 | [diff] [blame] | 1073 | for (i = 0; total < prop->length; total += l, p += l, i++) |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1074 | l = strlen(p) + 1; |
Benoit Cousson | 88af7f5 | 2011-12-05 15:23:54 +0100 | [diff] [blame] | 1075 | |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 1076 | return i; |
| 1077 | } |
| 1078 | EXPORT_SYMBOL_GPL(of_property_count_strings); |
| 1079 | |
| 1080 | /** |
Grant Likely | 739649c | 2009-04-25 12:52:40 +0000 | [diff] [blame] | 1081 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
| 1082 | * @np: Pointer to device node holding phandle property |
| 1083 | * @phandle_name: Name of property holding a phandle value |
| 1084 | * @index: For properties holding a table of phandles, this is the index into |
| 1085 | * the table |
| 1086 | * |
| 1087 | * Returns the device_node pointer with refcount incremented. Use |
| 1088 | * of_node_put() on it when done. |
| 1089 | */ |
Steffen Trumtrar | b8fbdc4 | 2012-11-22 12:16:43 +0100 | [diff] [blame] | 1090 | struct device_node *of_parse_phandle(const struct device_node *np, |
| 1091 | const char *phandle_name, int index) |
Grant Likely | 739649c | 2009-04-25 12:52:40 +0000 | [diff] [blame] | 1092 | { |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 1093 | const __be32 *phandle; |
Grant Likely | 739649c | 2009-04-25 12:52:40 +0000 | [diff] [blame] | 1094 | int size; |
| 1095 | |
| 1096 | phandle = of_get_property(np, phandle_name, &size); |
| 1097 | if ((!phandle) || (size < sizeof(*phandle) * (index + 1))) |
| 1098 | return NULL; |
| 1099 | |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 1100 | return of_find_node_by_phandle(be32_to_cpup(phandle + index)); |
Grant Likely | 739649c | 2009-04-25 12:52:40 +0000 | [diff] [blame] | 1101 | } |
| 1102 | EXPORT_SYMBOL(of_parse_phandle); |
| 1103 | |
| 1104 | /** |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1105 | * of_parse_phandle_with_args() - Find a node pointed by phandle in a list |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1106 | * @np: pointer to a device tree node containing a list |
| 1107 | * @list_name: property name that contains a list |
| 1108 | * @cells_name: property name that specifies phandles' arguments count |
| 1109 | * @index: index of a phandle to parse out |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1110 | * @out_args: optional pointer to output arguments structure (will be filled) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1111 | * |
| 1112 | * This function is useful to parse lists of phandles and their arguments. |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1113 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 1114 | * errno value. |
| 1115 | * |
| 1116 | * Caller is responsible to call of_node_put() on the returned out_args->node |
| 1117 | * pointer. |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1118 | * |
| 1119 | * Example: |
| 1120 | * |
| 1121 | * phandle1: node1 { |
| 1122 | * #list-cells = <2>; |
| 1123 | * } |
| 1124 | * |
| 1125 | * phandle2: node2 { |
| 1126 | * #list-cells = <1>; |
| 1127 | * } |
| 1128 | * |
| 1129 | * node3 { |
| 1130 | * list = <&phandle1 1 2 &phandle2 3>; |
| 1131 | * } |
| 1132 | * |
| 1133 | * To get a device_node of the `node2' node you may call this: |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1134 | * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1135 | */ |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1136 | static int __of_parse_phandle_with_args(const struct device_node *np, |
| 1137 | const char *list_name, |
| 1138 | const char *cells_name, int index, |
| 1139 | struct of_phandle_args *out_args) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1140 | { |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1141 | const __be32 *list, *list_end; |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1142 | int rc = 0, size, cur_index = 0; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1143 | uint32_t count = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1144 | struct device_node *node = NULL; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1145 | phandle phandle; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1146 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1147 | /* Retrieve the phandle list property */ |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1148 | list = of_get_property(np, list_name, &size); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1149 | if (!list) |
Alexandre Courbot | 1af4c7f | 2012-06-29 13:57:58 +0900 | [diff] [blame] | 1150 | return -ENOENT; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1151 | list_end = list + size / sizeof(*list); |
| 1152 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1153 | /* Loop over the phandles until all the requested entry is found */ |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1154 | while (list < list_end) { |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1155 | rc = -EINVAL; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1156 | count = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1157 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1158 | /* |
| 1159 | * If phandle is 0, then it is an empty entry with no |
| 1160 | * arguments. Skip forward to the next entry. |
| 1161 | */ |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 1162 | phandle = be32_to_cpup(list++); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1163 | if (phandle) { |
| 1164 | /* |
| 1165 | * Find the provider node and parse the #*-cells |
| 1166 | * property to determine the argument length |
| 1167 | */ |
| 1168 | node = of_find_node_by_phandle(phandle); |
| 1169 | if (!node) { |
| 1170 | pr_err("%s: could not find phandle\n", |
| 1171 | np->full_name); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1172 | goto err; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1173 | } |
| 1174 | if (of_property_read_u32(node, cells_name, &count)) { |
| 1175 | pr_err("%s: could not get %s for %s\n", |
| 1176 | np->full_name, cells_name, |
| 1177 | node->full_name); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1178 | goto err; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1179 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1180 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1181 | /* |
| 1182 | * Make sure that the arguments actually fit in the |
| 1183 | * remaining property data length |
| 1184 | */ |
| 1185 | if (list + count > list_end) { |
| 1186 | pr_err("%s: arguments longer than property\n", |
| 1187 | np->full_name); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1188 | goto err; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1189 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1192 | /* |
| 1193 | * All of the error cases above bail out of the loop, so at |
| 1194 | * this point, the parsing is successful. If the requested |
| 1195 | * index matches, then fill the out_args structure and return, |
| 1196 | * or return -ENOENT for an empty entry. |
| 1197 | */ |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1198 | rc = -ENOENT; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1199 | if (cur_index == index) { |
| 1200 | if (!phandle) |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1201 | goto err; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1202 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1203 | if (out_args) { |
| 1204 | int i; |
| 1205 | if (WARN_ON(count > MAX_PHANDLE_ARGS)) |
| 1206 | count = MAX_PHANDLE_ARGS; |
| 1207 | out_args->np = node; |
| 1208 | out_args->args_count = count; |
| 1209 | for (i = 0; i < count; i++) |
| 1210 | out_args->args[i] = be32_to_cpup(list++); |
| 1211 | } |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1212 | |
| 1213 | /* Found it! return success */ |
| 1214 | if (node) |
| 1215 | of_node_put(node); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1216 | return 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1217 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1218 | |
| 1219 | of_node_put(node); |
| 1220 | node = NULL; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1221 | list += count; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1222 | cur_index++; |
| 1223 | } |
| 1224 | |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1225 | /* |
| 1226 | * Unlock node before returning result; will be one of: |
| 1227 | * -ENOENT : index is for empty phandle |
| 1228 | * -EINVAL : parsing error on data |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1229 | * [1..n] : Number of phandle (count mode; when index = -1) |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1230 | */ |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1231 | rc = index < 0 ? cur_index : -ENOENT; |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1232 | err: |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1233 | if (node) |
| 1234 | of_node_put(node); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1235 | return rc; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1236 | } |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1237 | |
| 1238 | int of_parse_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1239 | const char *cells_name, int index, |
| 1240 | struct of_phandle_args *out_args) |
| 1241 | { |
| 1242 | if (index < 0) |
| 1243 | return -EINVAL; |
| 1244 | return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args); |
| 1245 | } |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1246 | EXPORT_SYMBOL(of_parse_phandle_with_args); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1247 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1248 | /** |
| 1249 | * of_count_phandle_with_args() - Find the number of phandles references in a property |
| 1250 | * @np: pointer to a device tree node containing a list |
| 1251 | * @list_name: property name that contains a list |
| 1252 | * @cells_name: property name that specifies phandles' arguments count |
| 1253 | * |
| 1254 | * Returns the number of phandle + argument tuples within a property. It |
| 1255 | * is a typical pattern to encode a list of phandle and variable |
| 1256 | * arguments into a single property. The number of arguments is encoded |
| 1257 | * by a property in the phandle-target node. For example, a gpios |
| 1258 | * property would contain a list of GPIO specifies consisting of a |
| 1259 | * phandle and 1 or more arguments. The number of arguments are |
| 1260 | * determined by the #gpio-cells property in the node pointed to by the |
| 1261 | * phandle. |
| 1262 | */ |
| 1263 | int of_count_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1264 | const char *cells_name) |
| 1265 | { |
| 1266 | return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL); |
| 1267 | } |
| 1268 | EXPORT_SYMBOL(of_count_phandle_with_args); |
| 1269 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1270 | #if defined(CONFIG_OF_DYNAMIC) |
| 1271 | static int of_property_notify(int action, struct device_node *np, |
| 1272 | struct property *prop) |
| 1273 | { |
| 1274 | struct of_prop_reconfig pr; |
| 1275 | |
| 1276 | pr.dn = np; |
| 1277 | pr.prop = prop; |
| 1278 | return of_reconfig_notify(action, &pr); |
| 1279 | } |
| 1280 | #else |
| 1281 | static int of_property_notify(int action, struct device_node *np, |
| 1282 | struct property *prop) |
| 1283 | { |
| 1284 | return 0; |
| 1285 | } |
| 1286 | #endif |
| 1287 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1288 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1289 | * of_add_property - Add a property to a node |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1290 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1291 | int of_add_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1292 | { |
| 1293 | struct property **next; |
| 1294 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1295 | int rc; |
| 1296 | |
| 1297 | rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop); |
| 1298 | if (rc) |
| 1299 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1300 | |
| 1301 | prop->next = NULL; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1302 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1303 | next = &np->properties; |
| 1304 | while (*next) { |
| 1305 | if (strcmp(prop->name, (*next)->name) == 0) { |
| 1306 | /* duplicate ! don't insert it */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1307 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1308 | return -1; |
| 1309 | } |
| 1310 | next = &(*next)->next; |
| 1311 | } |
| 1312 | *next = prop; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1313 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1314 | |
| 1315 | #ifdef CONFIG_PROC_DEVICETREE |
| 1316 | /* try to add to proc as well if it was initialized */ |
| 1317 | if (np->pde) |
| 1318 | proc_device_tree_add_prop(np->pde, prop); |
| 1319 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1320 | |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1325 | * of_remove_property - Remove a property from a node. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1326 | * |
| 1327 | * Note that we don't actually remove it, since we have given out |
| 1328 | * who-knows-how-many pointers to the data using get-property. |
| 1329 | * Instead we just move the property to the "dead properties" |
| 1330 | * list, so it won't be found any more. |
| 1331 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1332 | int of_remove_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1333 | { |
| 1334 | struct property **next; |
| 1335 | unsigned long flags; |
| 1336 | int found = 0; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1337 | int rc; |
| 1338 | |
| 1339 | rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop); |
| 1340 | if (rc) |
| 1341 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1342 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1343 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1344 | next = &np->properties; |
| 1345 | while (*next) { |
| 1346 | if (*next == prop) { |
| 1347 | /* found the node */ |
| 1348 | *next = prop->next; |
| 1349 | prop->next = np->deadprops; |
| 1350 | np->deadprops = prop; |
| 1351 | found = 1; |
| 1352 | break; |
| 1353 | } |
| 1354 | next = &(*next)->next; |
| 1355 | } |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1356 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1357 | |
| 1358 | if (!found) |
| 1359 | return -ENODEV; |
| 1360 | |
| 1361 | #ifdef CONFIG_PROC_DEVICETREE |
| 1362 | /* try to remove the proc node as well */ |
| 1363 | if (np->pde) |
| 1364 | proc_device_tree_remove_prop(np->pde, prop); |
| 1365 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1366 | |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1371 | * 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] | 1372 | * not exist, add it. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1373 | * |
| 1374 | * Note that we don't actually remove it, since we have given out |
| 1375 | * who-knows-how-many pointers to the data using get-property. |
| 1376 | * Instead we just move the property to the "dead properties" list, |
| 1377 | * and add the new property to the property list |
| 1378 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1379 | int of_update_property(struct device_node *np, struct property *newprop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1380 | { |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1381 | struct property **next, *oldprop; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1382 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1383 | int rc, found = 0; |
| 1384 | |
| 1385 | rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop); |
| 1386 | if (rc) |
| 1387 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1388 | |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1389 | if (!newprop->name) |
| 1390 | return -EINVAL; |
| 1391 | |
| 1392 | oldprop = of_find_property(np, newprop->name, NULL); |
| 1393 | if (!oldprop) |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1394 | return of_add_property(np, newprop); |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1395 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1396 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1397 | next = &np->properties; |
| 1398 | while (*next) { |
| 1399 | if (*next == oldprop) { |
| 1400 | /* found the node */ |
| 1401 | newprop->next = oldprop->next; |
| 1402 | *next = newprop; |
| 1403 | oldprop->next = np->deadprops; |
| 1404 | np->deadprops = oldprop; |
| 1405 | found = 1; |
| 1406 | break; |
| 1407 | } |
| 1408 | next = &(*next)->next; |
| 1409 | } |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1410 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1411 | |
| 1412 | if (!found) |
| 1413 | return -ENODEV; |
| 1414 | |
| 1415 | #ifdef CONFIG_PROC_DEVICETREE |
| 1416 | /* try to add to proc as well if it was initialized */ |
| 1417 | if (np->pde) |
| 1418 | proc_device_tree_update_prop(np->pde, newprop, oldprop); |
| 1419 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1420 | |
| 1421 | return 0; |
| 1422 | } |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1423 | |
| 1424 | #if defined(CONFIG_OF_DYNAMIC) |
| 1425 | /* |
| 1426 | * Support for dynamic device trees. |
| 1427 | * |
| 1428 | * On some platforms, the device tree can be manipulated at runtime. |
| 1429 | * The routines in this section support adding, removing and changing |
| 1430 | * device tree nodes. |
| 1431 | */ |
| 1432 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1433 | static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); |
| 1434 | |
| 1435 | int of_reconfig_notifier_register(struct notifier_block *nb) |
| 1436 | { |
| 1437 | return blocking_notifier_chain_register(&of_reconfig_chain, nb); |
| 1438 | } |
Nathan Fontenot | 1a9bd45 | 2012-11-28 09:42:26 +0000 | [diff] [blame] | 1439 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1440 | |
| 1441 | int of_reconfig_notifier_unregister(struct notifier_block *nb) |
| 1442 | { |
| 1443 | return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); |
| 1444 | } |
Nathan Fontenot | 1a9bd45 | 2012-11-28 09:42:26 +0000 | [diff] [blame] | 1445 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1446 | |
| 1447 | int of_reconfig_notify(unsigned long action, void *p) |
| 1448 | { |
| 1449 | int rc; |
| 1450 | |
| 1451 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); |
| 1452 | return notifier_to_errno(rc); |
| 1453 | } |
| 1454 | |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1455 | #ifdef CONFIG_PROC_DEVICETREE |
| 1456 | static void of_add_proc_dt_entry(struct device_node *dn) |
| 1457 | { |
| 1458 | struct proc_dir_entry *ent; |
| 1459 | |
| 1460 | ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde); |
| 1461 | if (ent) |
| 1462 | proc_device_tree_add_node(dn, ent); |
| 1463 | } |
| 1464 | #else |
| 1465 | static void of_add_proc_dt_entry(struct device_node *dn) |
| 1466 | { |
| 1467 | return; |
| 1468 | } |
| 1469 | #endif |
| 1470 | |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1471 | /** |
| 1472 | * of_attach_node - Plug a device node into the tree and global list. |
| 1473 | */ |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1474 | int of_attach_node(struct device_node *np) |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1475 | { |
| 1476 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1477 | int rc; |
| 1478 | |
| 1479 | rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); |
| 1480 | if (rc) |
| 1481 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1482 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1483 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1484 | np->sibling = np->parent->child; |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1485 | np->allnext = of_allnodes; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1486 | np->parent->child = np; |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1487 | of_allnodes = np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1488 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1489 | |
| 1490 | of_add_proc_dt_entry(np); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1491 | return 0; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1494 | #ifdef CONFIG_PROC_DEVICETREE |
| 1495 | static void of_remove_proc_dt_entry(struct device_node *dn) |
| 1496 | { |
David Howells | a8ca16e | 2013-04-12 17:27:28 +0100 | [diff] [blame] | 1497 | proc_remove(dn->pde); |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1498 | } |
| 1499 | #else |
| 1500 | static void of_remove_proc_dt_entry(struct device_node *dn) |
| 1501 | { |
| 1502 | return; |
| 1503 | } |
| 1504 | #endif |
| 1505 | |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1506 | /** |
| 1507 | * of_detach_node - "Unplug" a node from the device tree. |
| 1508 | * |
| 1509 | * The caller must hold a reference to the node. The memory associated with |
| 1510 | * the node is not freed until its refcount goes to zero. |
| 1511 | */ |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1512 | int of_detach_node(struct device_node *np) |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1513 | { |
| 1514 | struct device_node *parent; |
| 1515 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1516 | int rc = 0; |
| 1517 | |
| 1518 | rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); |
| 1519 | if (rc) |
| 1520 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1521 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1522 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1523 | |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1524 | if (of_node_check_flag(np, OF_DETACHED)) { |
| 1525 | /* someone already detached it */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1526 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1527 | return rc; |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1530 | parent = np->parent; |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1531 | if (!parent) { |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1532 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1533 | return rc; |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1534 | } |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1535 | |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1536 | if (of_allnodes == np) |
| 1537 | of_allnodes = np->allnext; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1538 | else { |
| 1539 | struct device_node *prev; |
Randy Dunlap | 465aac6 | 2012-11-30 10:01:51 +0000 | [diff] [blame] | 1540 | for (prev = of_allnodes; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1541 | prev->allnext != np; |
| 1542 | prev = prev->allnext) |
| 1543 | ; |
| 1544 | prev->allnext = np->allnext; |
| 1545 | } |
| 1546 | |
| 1547 | if (parent->child == np) |
| 1548 | parent->child = np->sibling; |
| 1549 | else { |
| 1550 | struct device_node *prevsib; |
| 1551 | for (prevsib = np->parent->child; |
| 1552 | prevsib->sibling != np; |
| 1553 | prevsib = prevsib->sibling) |
| 1554 | ; |
| 1555 | prevsib->sibling = np->sibling; |
| 1556 | } |
| 1557 | |
| 1558 | of_node_set_flag(np, OF_DETACHED); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1559 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1560 | |
| 1561 | of_remove_proc_dt_entry(np); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1562 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1563 | } |
| 1564 | #endif /* defined(CONFIG_OF_DYNAMIC) */ |
| 1565 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1566 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
| 1567 | int id, const char *stem, int stem_len) |
| 1568 | { |
| 1569 | ap->np = np; |
| 1570 | ap->id = id; |
| 1571 | strncpy(ap->stem, stem, stem_len); |
| 1572 | ap->stem[stem_len] = 0; |
| 1573 | list_add_tail(&ap->link, &aliases_lookup); |
| 1574 | 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] | 1575 | ap->alias, ap->stem, ap->id, of_node_full_name(np)); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * of_alias_scan - Scan all properties of 'aliases' node |
| 1580 | * |
| 1581 | * The function scans all the properties of 'aliases' node and populate |
| 1582 | * the the global lookup table with the properties. It returns the |
| 1583 | * number of alias_prop found, or error code in error case. |
| 1584 | * |
| 1585 | * @dt_alloc: An allocator that provides a virtual address to memory |
| 1586 | * for the resulting tree |
| 1587 | */ |
| 1588 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) |
| 1589 | { |
| 1590 | struct property *pp; |
| 1591 | |
| 1592 | of_chosen = of_find_node_by_path("/chosen"); |
| 1593 | if (of_chosen == NULL) |
| 1594 | of_chosen = of_find_node_by_path("/chosen@0"); |
| 1595 | of_aliases = of_find_node_by_path("/aliases"); |
| 1596 | if (!of_aliases) |
| 1597 | return; |
| 1598 | |
Dong Aisheng | 8af0da9 | 2011-12-22 20:19:24 +0800 | [diff] [blame] | 1599 | for_each_property_of_node(of_aliases, pp) { |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1600 | const char *start = pp->name; |
| 1601 | const char *end = start + strlen(start); |
| 1602 | struct device_node *np; |
| 1603 | struct alias_prop *ap; |
| 1604 | int id, len; |
| 1605 | |
| 1606 | /* Skip those we do not want to proceed */ |
| 1607 | if (!strcmp(pp->name, "name") || |
| 1608 | !strcmp(pp->name, "phandle") || |
| 1609 | !strcmp(pp->name, "linux,phandle")) |
| 1610 | continue; |
| 1611 | |
| 1612 | np = of_find_node_by_path(pp->value); |
| 1613 | if (!np) |
| 1614 | continue; |
| 1615 | |
| 1616 | /* walk the alias backwards to extract the id and work out |
| 1617 | * the 'stem' string */ |
| 1618 | while (isdigit(*(end-1)) && end > start) |
| 1619 | end--; |
| 1620 | len = end - start; |
| 1621 | |
| 1622 | if (kstrtoint(end, 10, &id) < 0) |
| 1623 | continue; |
| 1624 | |
| 1625 | /* Allocate an alias_prop with enough space for the stem */ |
| 1626 | ap = dt_alloc(sizeof(*ap) + len + 1, 4); |
| 1627 | if (!ap) |
| 1628 | continue; |
| 1629 | ap->alias = start; |
| 1630 | of_alias_add(ap, np, id, start, len); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | /** |
| 1635 | * of_alias_get_id - Get alias id for the given device_node |
| 1636 | * @np: Pointer to the given device_node |
| 1637 | * @stem: Alias stem of the given device_node |
| 1638 | * |
| 1639 | * The function travels the lookup table to get alias id for the given |
| 1640 | * device_node and alias stem. It returns the alias id if find it. |
| 1641 | */ |
| 1642 | int of_alias_get_id(struct device_node *np, const char *stem) |
| 1643 | { |
| 1644 | struct alias_prop *app; |
| 1645 | int id = -ENODEV; |
| 1646 | |
| 1647 | mutex_lock(&of_aliases_mutex); |
| 1648 | list_for_each_entry(app, &aliases_lookup, link) { |
| 1649 | if (strcmp(app->stem, stem) != 0) |
| 1650 | continue; |
| 1651 | |
| 1652 | if (np == app->np) { |
| 1653 | id = app->id; |
| 1654 | break; |
| 1655 | } |
| 1656 | } |
| 1657 | mutex_unlock(&of_aliases_mutex); |
| 1658 | |
| 1659 | return id; |
| 1660 | } |
| 1661 | EXPORT_SYMBOL_GPL(of_alias_get_id); |
Stephen Warren | c541adc | 2012-04-04 09:27:46 -0600 | [diff] [blame] | 1662 | |
| 1663 | const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur, |
| 1664 | u32 *pu) |
| 1665 | { |
| 1666 | const void *curv = cur; |
| 1667 | |
| 1668 | if (!prop) |
| 1669 | return NULL; |
| 1670 | |
| 1671 | if (!cur) { |
| 1672 | curv = prop->value; |
| 1673 | goto out_val; |
| 1674 | } |
| 1675 | |
| 1676 | curv += sizeof(*cur); |
| 1677 | if (curv >= prop->value + prop->length) |
| 1678 | return NULL; |
| 1679 | |
| 1680 | out_val: |
| 1681 | *pu = be32_to_cpup(curv); |
| 1682 | return curv; |
| 1683 | } |
| 1684 | EXPORT_SYMBOL_GPL(of_prop_next_u32); |
| 1685 | |
| 1686 | const char *of_prop_next_string(struct property *prop, const char *cur) |
| 1687 | { |
| 1688 | const void *curv = cur; |
| 1689 | |
| 1690 | if (!prop) |
| 1691 | return NULL; |
| 1692 | |
| 1693 | if (!cur) |
| 1694 | return prop->value; |
| 1695 | |
| 1696 | curv += strlen(cur) + 1; |
| 1697 | if (curv >= prop->value + prop->length) |
| 1698 | return NULL; |
| 1699 | |
| 1700 | return curv; |
| 1701 | } |
| 1702 | EXPORT_SYMBOL_GPL(of_prop_next_string); |