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 | 97d7b4d | 2012-12-03 19:19:38 -0800 | [diff] [blame] | 27 | #include "of_private.h" |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 28 | |
Stepan Moskovchenko | 97d7b4d | 2012-12-03 19:19:38 -0800 | [diff] [blame] | 29 | LIST_HEAD(aliases_lookup); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 30 | |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 31 | struct device_node *allnodes; |
Grant Likely | fc0bdae | 2010-02-14 07:13:55 -0700 | [diff] [blame] | 32 | struct device_node *of_chosen; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 33 | struct device_node *of_aliases; |
| 34 | |
Stepan Moskovchenko | 97d7b4d | 2012-12-03 19:19:38 -0800 | [diff] [blame] | 35 | DEFINE_MUTEX(of_aliases_mutex); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 36 | |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 37 | /* use when traversing tree through the allnext, child, sibling, |
| 38 | * or parent members of struct device_node. |
| 39 | */ |
| 40 | DEFINE_RWLOCK(devtree_lock); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 41 | |
| 42 | int of_n_addr_cells(struct device_node *np) |
| 43 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 44 | const __be32 *ip; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 45 | |
| 46 | do { |
| 47 | if (np->parent) |
| 48 | np = np->parent; |
| 49 | ip = of_get_property(np, "#address-cells", NULL); |
| 50 | if (ip) |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 51 | return be32_to_cpup(ip); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 52 | } while (np->parent); |
| 53 | /* No #address-cells property for the root node */ |
| 54 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 55 | } |
| 56 | EXPORT_SYMBOL(of_n_addr_cells); |
| 57 | |
| 58 | int of_n_size_cells(struct device_node *np) |
| 59 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 60 | const __be32 *ip; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 61 | |
| 62 | do { |
| 63 | if (np->parent) |
| 64 | np = np->parent; |
| 65 | ip = of_get_property(np, "#size-cells", NULL); |
| 66 | if (ip) |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 67 | return be32_to_cpup(ip); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 68 | } while (np->parent); |
| 69 | /* No #size-cells property for the root node */ |
| 70 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 71 | } |
| 72 | EXPORT_SYMBOL(of_n_size_cells); |
| 73 | |
Grant Likely | 0f22dd3 | 2012-02-15 20:38:40 -0700 | [diff] [blame] | 74 | #if defined(CONFIG_OF_DYNAMIC) |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 75 | /** |
| 76 | * of_node_get - Increment refcount of a node |
| 77 | * @node: Node to inc refcount, NULL is supported to |
| 78 | * simplify writing of callers |
| 79 | * |
| 80 | * Returns node. |
| 81 | */ |
| 82 | struct device_node *of_node_get(struct device_node *node) |
| 83 | { |
| 84 | if (node) |
| 85 | kref_get(&node->kref); |
| 86 | return node; |
| 87 | } |
| 88 | EXPORT_SYMBOL(of_node_get); |
| 89 | |
| 90 | static inline struct device_node *kref_to_device_node(struct kref *kref) |
| 91 | { |
| 92 | return container_of(kref, struct device_node, kref); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * of_node_release - release a dynamically allocated node |
| 97 | * @kref: kref element of the node to be released |
| 98 | * |
| 99 | * In of_node_put() this function is passed to kref_put() |
| 100 | * as the destructor. |
| 101 | */ |
| 102 | static void of_node_release(struct kref *kref) |
| 103 | { |
| 104 | struct device_node *node = kref_to_device_node(kref); |
| 105 | struct property *prop = node->properties; |
| 106 | |
| 107 | /* We should never be releasing nodes that haven't been detached. */ |
| 108 | if (!of_node_check_flag(node, OF_DETACHED)) { |
| 109 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); |
| 110 | dump_stack(); |
| 111 | kref_init(&node->kref); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if (!of_node_check_flag(node, OF_DYNAMIC)) |
| 116 | return; |
| 117 | |
| 118 | while (prop) { |
| 119 | struct property *next = prop->next; |
| 120 | kfree(prop->name); |
| 121 | kfree(prop->value); |
| 122 | kfree(prop); |
| 123 | prop = next; |
| 124 | |
| 125 | if (!prop) { |
| 126 | prop = node->deadprops; |
| 127 | node->deadprops = NULL; |
| 128 | } |
| 129 | } |
| 130 | kfree(node->full_name); |
| 131 | kfree(node->data); |
| 132 | kfree(node); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * of_node_put - Decrement refcount of a node |
| 137 | * @node: Node to dec refcount, NULL is supported to |
| 138 | * simplify writing of callers |
| 139 | * |
| 140 | */ |
| 141 | void of_node_put(struct device_node *node) |
| 142 | { |
| 143 | if (node) |
| 144 | kref_put(&node->kref, of_node_release); |
| 145 | } |
| 146 | EXPORT_SYMBOL(of_node_put); |
Grant Likely | 0f22dd3 | 2012-02-15 20:38:40 -0700 | [diff] [blame] | 147 | #endif /* CONFIG_OF_DYNAMIC */ |
Grant Likely | 923f7e3 | 2010-01-28 13:52:53 -0700 | [diff] [blame] | 148 | |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 149 | struct property *of_find_property(const struct device_node *np, |
| 150 | const char *name, |
| 151 | int *lenp) |
| 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 | |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 158 | read_lock(&devtree_lock); |
| 159 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 160 | if (of_prop_cmp(pp->name, name) == 0) { |
| 161 | if (lenp != 0) |
| 162 | *lenp = pp->length; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | read_unlock(&devtree_lock); |
| 167 | |
| 168 | return pp; |
| 169 | } |
| 170 | EXPORT_SYMBOL(of_find_property); |
| 171 | |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 172 | /** |
| 173 | * of_find_all_nodes - Get next node in global list |
| 174 | * @prev: Previous node or NULL to start iteration |
| 175 | * of_node_put() will be called on it |
| 176 | * |
| 177 | * Returns a node pointer with refcount incremented, use |
| 178 | * of_node_put() on it when done. |
| 179 | */ |
| 180 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 181 | { |
| 182 | struct device_node *np; |
| 183 | |
| 184 | read_lock(&devtree_lock); |
| 185 | np = prev ? prev->allnext : allnodes; |
| 186 | for (; np != NULL; np = np->allnext) |
| 187 | if (of_node_get(np)) |
| 188 | break; |
| 189 | of_node_put(prev); |
| 190 | read_unlock(&devtree_lock); |
| 191 | return np; |
| 192 | } |
| 193 | EXPORT_SYMBOL(of_find_all_nodes); |
| 194 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 195 | /* |
| 196 | * Find a property with a given name for a given node |
| 197 | * and return the value. |
| 198 | */ |
| 199 | const void *of_get_property(const struct device_node *np, const char *name, |
| 200 | int *lenp) |
| 201 | { |
| 202 | struct property *pp = of_find_property(np, name, lenp); |
| 203 | |
| 204 | return pp ? pp->value : NULL; |
| 205 | } |
| 206 | EXPORT_SYMBOL(of_get_property); |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 207 | |
| 208 | /** Checks if the given "compat" string matches one of the strings in |
| 209 | * the device's "compatible" property |
| 210 | */ |
| 211 | int of_device_is_compatible(const struct device_node *device, |
| 212 | const char *compat) |
| 213 | { |
| 214 | const char* cp; |
| 215 | int cplen, l; |
| 216 | |
| 217 | cp = of_get_property(device, "compatible", &cplen); |
| 218 | if (cp == NULL) |
| 219 | return 0; |
| 220 | while (cplen > 0) { |
| 221 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) |
| 222 | return 1; |
| 223 | l = strlen(cp) + 1; |
| 224 | cp += l; |
| 225 | cplen -= l; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | EXPORT_SYMBOL(of_device_is_compatible); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 231 | |
| 232 | /** |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 233 | * 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] | 234 | * @compat: compatible string to look for in root node's compatible property. |
| 235 | * |
| 236 | * Returns true if the root node has the given value in its |
| 237 | * compatible property. |
| 238 | */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 239 | int of_machine_is_compatible(const char *compat) |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 240 | { |
| 241 | struct device_node *root; |
| 242 | int rc = 0; |
| 243 | |
| 244 | root = of_find_node_by_path("/"); |
| 245 | if (root) { |
| 246 | rc = of_device_is_compatible(root, compat); |
| 247 | of_node_put(root); |
| 248 | } |
| 249 | return rc; |
| 250 | } |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 251 | EXPORT_SYMBOL(of_machine_is_compatible); |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 252 | |
| 253 | /** |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 254 | * of_device_is_available - check if a device is available for use |
| 255 | * |
| 256 | * @device: Node to check for availability |
| 257 | * |
| 258 | * Returns 1 if the status property is absent or set to "okay" or "ok", |
| 259 | * 0 otherwise |
| 260 | */ |
| 261 | int of_device_is_available(const struct device_node *device) |
| 262 | { |
| 263 | const char *status; |
| 264 | int statlen; |
| 265 | |
| 266 | status = of_get_property(device, "status", &statlen); |
| 267 | if (status == NULL) |
| 268 | return 1; |
| 269 | |
| 270 | if (statlen > 0) { |
| 271 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) |
| 272 | return 1; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | EXPORT_SYMBOL(of_device_is_available); |
| 278 | |
| 279 | /** |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 280 | * of_get_parent - Get a node's parent if any |
| 281 | * @node: Node to get parent |
| 282 | * |
| 283 | * Returns a node pointer with refcount incremented, use |
| 284 | * of_node_put() on it when done. |
| 285 | */ |
| 286 | struct device_node *of_get_parent(const struct device_node *node) |
| 287 | { |
| 288 | struct device_node *np; |
| 289 | |
| 290 | if (!node) |
| 291 | return NULL; |
| 292 | |
| 293 | read_lock(&devtree_lock); |
| 294 | np = of_node_get(node->parent); |
| 295 | read_unlock(&devtree_lock); |
| 296 | return np; |
| 297 | } |
| 298 | EXPORT_SYMBOL(of_get_parent); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 299 | |
| 300 | /** |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 301 | * of_get_next_parent - Iterate to a node's parent |
| 302 | * @node: Node to get parent of |
| 303 | * |
| 304 | * This is like of_get_parent() except that it drops the |
| 305 | * refcount on the passed node, making it suitable for iterating |
| 306 | * through a node's parents. |
| 307 | * |
| 308 | * Returns a node pointer with refcount incremented, use |
| 309 | * of_node_put() on it when done. |
| 310 | */ |
| 311 | struct device_node *of_get_next_parent(struct device_node *node) |
| 312 | { |
| 313 | struct device_node *parent; |
| 314 | |
| 315 | if (!node) |
| 316 | return NULL; |
| 317 | |
| 318 | read_lock(&devtree_lock); |
| 319 | parent = of_node_get(node->parent); |
| 320 | of_node_put(node); |
| 321 | read_unlock(&devtree_lock); |
| 322 | return parent; |
| 323 | } |
| 324 | |
| 325 | /** |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 326 | * of_get_next_child - Iterate a node childs |
| 327 | * @node: parent node |
| 328 | * @prev: previous child of the parent node, or NULL to get first |
| 329 | * |
| 330 | * Returns a node pointer with refcount incremented, use |
| 331 | * of_node_put() on it when done. |
| 332 | */ |
| 333 | struct device_node *of_get_next_child(const struct device_node *node, |
| 334 | struct device_node *prev) |
| 335 | { |
| 336 | struct device_node *next; |
| 337 | |
| 338 | read_lock(&devtree_lock); |
| 339 | next = prev ? prev->sibling : node->child; |
| 340 | for (; next; next = next->sibling) |
| 341 | if (of_node_get(next)) |
| 342 | break; |
| 343 | of_node_put(prev); |
| 344 | read_unlock(&devtree_lock); |
| 345 | return next; |
| 346 | } |
| 347 | EXPORT_SYMBOL(of_get_next_child); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 348 | |
| 349 | /** |
Timur Tabi | 770e0df | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 350 | * of_get_next_available_child - Find the next available child node |
| 351 | * @node: parent node |
| 352 | * @prev: previous child of the parent node, or NULL to get first |
| 353 | * |
| 354 | * This function is like of_get_next_child(), except that it |
| 355 | * automatically skips any disabled nodes (i.e. status = "disabled"). |
| 356 | */ |
| 357 | struct device_node *of_get_next_available_child(const struct device_node *node, |
| 358 | struct device_node *prev) |
| 359 | { |
| 360 | struct device_node *next; |
| 361 | |
| 362 | read_lock(&devtree_lock); |
| 363 | next = prev ? prev->sibling : node->child; |
| 364 | for (; next; next = next->sibling) { |
| 365 | if (!of_device_is_available(next)) |
| 366 | continue; |
| 367 | if (of_node_get(next)) |
| 368 | break; |
| 369 | } |
| 370 | of_node_put(prev); |
| 371 | read_unlock(&devtree_lock); |
| 372 | return next; |
| 373 | } |
| 374 | EXPORT_SYMBOL(of_get_next_available_child); |
| 375 | |
| 376 | /** |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 377 | * of_find_node_by_path - Find a node matching a full OF path |
| 378 | * @path: The full path to match |
| 379 | * |
| 380 | * Returns a node pointer with refcount incremented, use |
| 381 | * of_node_put() on it when done. |
| 382 | */ |
| 383 | struct device_node *of_find_node_by_path(const char *path) |
| 384 | { |
| 385 | struct device_node *np = allnodes; |
| 386 | |
| 387 | read_lock(&devtree_lock); |
| 388 | for (; np; np = np->allnext) { |
| 389 | if (np->full_name && (of_node_cmp(np->full_name, path) == 0) |
| 390 | && of_node_get(np)) |
| 391 | break; |
| 392 | } |
| 393 | read_unlock(&devtree_lock); |
| 394 | return np; |
| 395 | } |
| 396 | EXPORT_SYMBOL(of_find_node_by_path); |
| 397 | |
| 398 | /** |
| 399 | * of_find_node_by_name - Find a node by its "name" property |
| 400 | * @from: The node to start searching from or NULL, the node |
| 401 | * you pass will not be searched, only the next one |
| 402 | * will; typically, you pass what the previous call |
| 403 | * returned. of_node_put() will be called on it |
| 404 | * @name: The name string to match against |
| 405 | * |
| 406 | * Returns a node pointer with refcount incremented, use |
| 407 | * of_node_put() on it when done. |
| 408 | */ |
| 409 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 410 | const char *name) |
| 411 | { |
| 412 | struct device_node *np; |
| 413 | |
| 414 | read_lock(&devtree_lock); |
| 415 | np = from ? from->allnext : allnodes; |
| 416 | for (; np; np = np->allnext) |
| 417 | if (np->name && (of_node_cmp(np->name, name) == 0) |
| 418 | && of_node_get(np)) |
| 419 | break; |
| 420 | of_node_put(from); |
| 421 | read_unlock(&devtree_lock); |
| 422 | return np; |
| 423 | } |
| 424 | EXPORT_SYMBOL(of_find_node_by_name); |
| 425 | |
| 426 | /** |
| 427 | * of_find_node_by_type - Find a node by its "device_type" property |
| 428 | * @from: The node to start searching from, or NULL to start searching |
| 429 | * the entire device tree. The node you pass will not be |
| 430 | * searched, only the next one will; typically, you pass |
| 431 | * what the previous call returned. of_node_put() will be |
| 432 | * called on from for you. |
| 433 | * @type: The type string to match against |
| 434 | * |
| 435 | * Returns a node pointer with refcount incremented, use |
| 436 | * of_node_put() on it when done. |
| 437 | */ |
| 438 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 439 | const char *type) |
| 440 | { |
| 441 | struct device_node *np; |
| 442 | |
| 443 | read_lock(&devtree_lock); |
| 444 | np = from ? from->allnext : allnodes; |
| 445 | for (; np; np = np->allnext) |
| 446 | if (np->type && (of_node_cmp(np->type, type) == 0) |
| 447 | && of_node_get(np)) |
| 448 | break; |
| 449 | of_node_put(from); |
| 450 | read_unlock(&devtree_lock); |
| 451 | return np; |
| 452 | } |
| 453 | EXPORT_SYMBOL(of_find_node_by_type); |
| 454 | |
| 455 | /** |
| 456 | * of_find_compatible_node - Find a node based on type and one of the |
| 457 | * tokens in its "compatible" property |
| 458 | * @from: The node to start searching from or NULL, the node |
| 459 | * you pass will not be searched, only the next one |
| 460 | * will; typically, you pass what the previous call |
| 461 | * returned. of_node_put() will be called on it |
| 462 | * @type: The type string to match "device_type" or NULL to ignore |
| 463 | * @compatible: The string to match to one of the tokens in the device |
| 464 | * "compatible" list. |
| 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_compatible_node(struct device_node *from, |
| 470 | const char *type, const char *compatible) |
| 471 | { |
| 472 | struct device_node *np; |
| 473 | |
| 474 | read_lock(&devtree_lock); |
| 475 | np = from ? from->allnext : allnodes; |
| 476 | for (; np; np = np->allnext) { |
| 477 | if (type |
| 478 | && !(np->type && (of_node_cmp(np->type, type) == 0))) |
| 479 | continue; |
| 480 | if (of_device_is_compatible(np, compatible) && of_node_get(np)) |
| 481 | break; |
| 482 | } |
| 483 | of_node_put(from); |
| 484 | read_unlock(&devtree_lock); |
| 485 | return np; |
| 486 | } |
| 487 | EXPORT_SYMBOL(of_find_compatible_node); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 488 | |
| 489 | /** |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 490 | * of_find_node_with_property - Find a node which has a property with |
| 491 | * the given name. |
| 492 | * @from: The node to start searching from or NULL, the node |
| 493 | * you pass will not be searched, only the next one |
| 494 | * will; typically, you pass what the previous call |
| 495 | * returned. of_node_put() will be called on it |
| 496 | * @prop_name: The name of the property to look for. |
| 497 | * |
| 498 | * Returns a node pointer with refcount incremented, use |
| 499 | * of_node_put() on it when done. |
| 500 | */ |
| 501 | struct device_node *of_find_node_with_property(struct device_node *from, |
| 502 | const char *prop_name) |
| 503 | { |
| 504 | struct device_node *np; |
| 505 | struct property *pp; |
| 506 | |
| 507 | read_lock(&devtree_lock); |
| 508 | np = from ? from->allnext : allnodes; |
| 509 | for (; np; np = np->allnext) { |
| 510 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 511 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
| 512 | of_node_get(np); |
| 513 | goto out; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | out: |
| 518 | of_node_put(from); |
| 519 | read_unlock(&devtree_lock); |
| 520 | return np; |
| 521 | } |
| 522 | EXPORT_SYMBOL(of_find_node_with_property); |
| 523 | |
| 524 | /** |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 525 | * of_match_node - Tell if an device_node has a matching of_match structure |
| 526 | * @matches: array of of device match structures to search in |
| 527 | * @node: the of device structure to match against |
| 528 | * |
| 529 | * Low level utility function used by device matching. |
| 530 | */ |
| 531 | const struct of_device_id *of_match_node(const struct of_device_id *matches, |
| 532 | const struct device_node *node) |
| 533 | { |
Grant Likely | a52f07e | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 534 | if (!matches) |
| 535 | return NULL; |
| 536 | |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 537 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { |
| 538 | int match = 1; |
| 539 | if (matches->name[0]) |
| 540 | match &= node->name |
| 541 | && !strcmp(matches->name, node->name); |
| 542 | if (matches->type[0]) |
| 543 | match &= node->type |
| 544 | && !strcmp(matches->type, node->type); |
| 545 | if (matches->compatible[0]) |
| 546 | match &= of_device_is_compatible(node, |
| 547 | matches->compatible); |
| 548 | if (match) |
| 549 | return matches; |
| 550 | matches++; |
| 551 | } |
| 552 | return NULL; |
| 553 | } |
| 554 | EXPORT_SYMBOL(of_match_node); |
| 555 | |
| 556 | /** |
| 557 | * of_find_matching_node - Find a node based on an of_device_id match |
| 558 | * table. |
| 559 | * @from: The node to start searching from or NULL, the node |
| 560 | * you pass will not be searched, only the next one |
| 561 | * will; typically, you pass what the previous call |
| 562 | * returned. of_node_put() will be called on it |
| 563 | * @matches: array of of device match structures to search in |
| 564 | * |
| 565 | * Returns a node pointer with refcount incremented, use |
| 566 | * of_node_put() on it when done. |
| 567 | */ |
| 568 | struct device_node *of_find_matching_node(struct device_node *from, |
| 569 | const struct of_device_id *matches) |
| 570 | { |
| 571 | struct device_node *np; |
| 572 | |
| 573 | read_lock(&devtree_lock); |
| 574 | np = from ? from->allnext : allnodes; |
| 575 | for (; np; np = np->allnext) { |
| 576 | if (of_match_node(matches, np) && of_node_get(np)) |
| 577 | break; |
| 578 | } |
| 579 | of_node_put(from); |
| 580 | read_unlock(&devtree_lock); |
| 581 | return np; |
| 582 | } |
| 583 | EXPORT_SYMBOL(of_find_matching_node); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 584 | |
| 585 | /** |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 586 | * of_modalias_node - Lookup appropriate modalias for a device node |
| 587 | * @node: pointer to a device tree node |
| 588 | * @modalias: Pointer to buffer that modalias value will be copied into |
| 589 | * @len: Length of modalias value |
| 590 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 591 | * Based on the value of the compatible property, this routine will attempt |
| 592 | * to choose an appropriate modalias value for a particular device tree node. |
| 593 | * It does this by stripping the manufacturer prefix (as delimited by a ',') |
| 594 | * from the first entry in the compatible list property. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 595 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 596 | * This routine returns 0 on success, <0 on failure. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 597 | */ |
| 598 | int of_modalias_node(struct device_node *node, char *modalias, int len) |
| 599 | { |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 600 | const char *compatible, *p; |
| 601 | int cplen; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 602 | |
| 603 | compatible = of_get_property(node, "compatible", &cplen); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 604 | if (!compatible || strlen(compatible) > cplen) |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 605 | return -ENODEV; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 606 | p = strchr(compatible, ','); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 607 | strlcpy(modalias, p ? p + 1 : compatible, len); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 608 | return 0; |
| 609 | } |
| 610 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 611 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 612 | /** |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 613 | * of_find_node_by_phandle - Find a node given a phandle |
| 614 | * @handle: phandle of the node to find |
| 615 | * |
| 616 | * Returns a node pointer with refcount incremented, use |
| 617 | * of_node_put() on it when done. |
| 618 | */ |
| 619 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 620 | { |
| 621 | struct device_node *np; |
| 622 | |
| 623 | read_lock(&devtree_lock); |
| 624 | for (np = allnodes; np; np = np->allnext) |
| 625 | if (np->phandle == handle) |
| 626 | break; |
| 627 | of_node_get(np); |
| 628 | read_unlock(&devtree_lock); |
| 629 | return np; |
| 630 | } |
| 631 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 632 | |
| 633 | /** |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 634 | * of_property_read_u32_array - Find and read an array of 32 bit integers |
| 635 | * from a property. |
| 636 | * |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 637 | * @np: device node from which the property value is to be read. |
| 638 | * @propname: name of the property to be searched. |
| 639 | * @out_value: pointer to return value, modified only if return value is 0. |
| 640 | * |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 641 | * 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] | 642 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 643 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 644 | * property data isn't large enough. |
| 645 | * |
| 646 | * The out_value is modified only if a valid u32 value can be decoded. |
| 647 | */ |
Jamie Iles | aac285c | 2011-08-02 15:45:07 +0100 | [diff] [blame] | 648 | int of_property_read_u32_array(const struct device_node *np, |
| 649 | const char *propname, u32 *out_values, |
| 650 | size_t sz) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 651 | { |
| 652 | struct property *prop = of_find_property(np, propname, NULL); |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 653 | const __be32 *val; |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 654 | |
| 655 | if (!prop) |
| 656 | return -EINVAL; |
| 657 | if (!prop->value) |
| 658 | return -ENODATA; |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 659 | if ((sz * sizeof(*out_values)) > prop->length) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 660 | return -EOVERFLOW; |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 661 | |
| 662 | val = prop->value; |
| 663 | while (sz--) |
| 664 | *out_values++ = be32_to_cpup(val++); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 665 | return 0; |
| 666 | } |
Rob Herring | 0e37363 | 2011-07-06 15:42:58 -0500 | [diff] [blame] | 667 | EXPORT_SYMBOL_GPL(of_property_read_u32_array); |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 668 | |
| 669 | /** |
Jamie Iles | 4cd7f7a | 2011-09-14 20:49:59 +0100 | [diff] [blame] | 670 | * of_property_read_u64 - Find and read a 64 bit integer from a property |
| 671 | * @np: device node from which the property value is to be read. |
| 672 | * @propname: name of the property to be searched. |
| 673 | * @out_value: pointer to return value, modified only if return value is 0. |
| 674 | * |
| 675 | * Search for a property in a device node and read a 64-bit value from |
| 676 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 677 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 678 | * property data isn't large enough. |
| 679 | * |
| 680 | * The out_value is modified only if a valid u64 value can be decoded. |
| 681 | */ |
| 682 | int of_property_read_u64(const struct device_node *np, const char *propname, |
| 683 | u64 *out_value) |
| 684 | { |
| 685 | struct property *prop = of_find_property(np, propname, NULL); |
| 686 | |
| 687 | if (!prop) |
| 688 | return -EINVAL; |
| 689 | if (!prop->value) |
| 690 | return -ENODATA; |
| 691 | if (sizeof(*out_value) > prop->length) |
| 692 | return -EOVERFLOW; |
| 693 | *out_value = of_read_number(prop->value, 2); |
| 694 | return 0; |
| 695 | } |
| 696 | EXPORT_SYMBOL_GPL(of_property_read_u64); |
| 697 | |
| 698 | /** |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 699 | * of_property_read_string - Find and read a string from a property |
| 700 | * @np: device node from which the property value is to be read. |
| 701 | * @propname: name of the property to be searched. |
| 702 | * @out_string: pointer to null terminated return string, modified only if |
| 703 | * return value is 0. |
| 704 | * |
| 705 | * Search for a property in a device tree node and retrieve a null |
| 706 | * terminated string value (pointer to data, not a copy). Returns 0 on |
| 707 | * success, -EINVAL if the property does not exist, -ENODATA if property |
| 708 | * does not have a value, and -EILSEQ if the string is not null-terminated |
| 709 | * within the length of the property data. |
| 710 | * |
| 711 | * The out_string pointer is modified only if a valid string can be decoded. |
| 712 | */ |
Jamie Iles | aac285c | 2011-08-02 15:45:07 +0100 | [diff] [blame] | 713 | int of_property_read_string(struct device_node *np, const char *propname, |
Shawn Guo | f09bc83 | 2011-07-04 09:01:18 +0800 | [diff] [blame] | 714 | const char **out_string) |
Thomas Abraham | a3b8536 | 2011-06-30 21:26:10 +0530 | [diff] [blame] | 715 | { |
| 716 | struct property *prop = of_find_property(np, propname, NULL); |
| 717 | if (!prop) |
| 718 | return -EINVAL; |
| 719 | if (!prop->value) |
| 720 | return -ENODATA; |
| 721 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 722 | return -EILSEQ; |
| 723 | *out_string = prop->value; |
| 724 | return 0; |
| 725 | } |
| 726 | EXPORT_SYMBOL_GPL(of_property_read_string); |
| 727 | |
| 728 | /** |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 729 | * of_property_match_string() - Find string in a list and return index |
| 730 | * @np: pointer to node containing string list property |
| 731 | * @propname: string list property name |
| 732 | * @string: pointer to string to search for in string list |
| 733 | * |
| 734 | * This function searches a string list property and returns the index |
| 735 | * of a specific string value. |
| 736 | */ |
| 737 | int of_property_match_string(struct device_node *np, const char *propname, |
| 738 | const char *string) |
| 739 | { |
| 740 | struct property *prop = of_find_property(np, propname, NULL); |
| 741 | size_t l; |
| 742 | int i; |
| 743 | const char *p, *end; |
| 744 | |
| 745 | if (!prop) |
| 746 | return -EINVAL; |
| 747 | if (!prop->value) |
| 748 | return -ENODATA; |
| 749 | |
| 750 | p = prop->value; |
| 751 | end = p + prop->length; |
| 752 | |
| 753 | for (i = 0; p < end; i++, p += l) { |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 754 | l = strnlen(p, end - p) + 1; |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 755 | if (p + l > end) |
| 756 | return -EILSEQ; |
| 757 | pr_debug("comparing %s with %s\n", string, p); |
| 758 | if (strcmp(string, p) == 0) |
| 759 | return i; /* Found it; return index */ |
| 760 | } |
| 761 | return -ENODATA; |
| 762 | } |
| 763 | EXPORT_SYMBOL_GPL(of_property_match_string); |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 764 | |
| 765 | /** |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 766 | * of_property_read_string_util() - Utility helper for parsing string properties |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 767 | * @np: device node from which the property value is to be read. |
| 768 | * @propname: name of the property to be searched. |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 769 | * @out_strs: output array of string pointers. |
| 770 | * @sz: number of array elements to read. |
| 771 | * @skip: Number of strings to skip over at beginning of list. |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 772 | * |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 773 | * Don't call this function directly. It is a utility helper for the |
| 774 | * of_property_read_string*() family of functions. |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 775 | */ |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 776 | int of_property_read_string_helper(struct device_node *np, const char *propname, |
| 777 | const char **out_strs, size_t sz, int skip) |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 778 | { |
| 779 | struct property *prop = of_find_property(np, propname, NULL); |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 780 | int l = 0, i = 0; |
| 781 | const char *p, *end; |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 782 | |
| 783 | if (!prop) |
| 784 | return -EINVAL; |
| 785 | if (!prop->value) |
| 786 | return -ENODATA; |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 787 | p = prop->value; |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 788 | end = p + prop->length; |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 789 | |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 790 | for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) { |
| 791 | l = strnlen(p, end - p) + 1; |
| 792 | if (p + l > end) |
| 793 | return -EILSEQ; |
| 794 | if (out_strs && i >= skip) |
| 795 | *out_strs++ = p; |
| 796 | } |
| 797 | i -= skip; |
| 798 | return i <= 0 ? -ENODATA : i; |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 799 | } |
Grant Likely | cbf81c4 | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 800 | EXPORT_SYMBOL_GPL(of_property_read_string_helper); |
Benoit Cousson | 4fcd15a | 2011-09-27 17:45:43 +0200 | [diff] [blame] | 801 | |
| 802 | /** |
Grant Likely | 739649c | 2009-04-25 12:52:40 +0000 | [diff] [blame] | 803 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
| 804 | * @np: Pointer to device node holding phandle property |
| 805 | * @phandle_name: Name of property holding a phandle value |
| 806 | * @index: For properties holding a table of phandles, this is the index into |
| 807 | * the table |
| 808 | * |
| 809 | * Returns the device_node pointer with refcount incremented. Use |
| 810 | * of_node_put() on it when done. |
| 811 | */ |
| 812 | struct device_node * |
| 813 | of_parse_phandle(struct device_node *np, const char *phandle_name, int index) |
| 814 | { |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 815 | const __be32 *phandle; |
Grant Likely | 739649c | 2009-04-25 12:52:40 +0000 | [diff] [blame] | 816 | int size; |
| 817 | |
| 818 | phandle = of_get_property(np, phandle_name, &size); |
| 819 | if ((!phandle) || (size < sizeof(*phandle) * (index + 1))) |
| 820 | return NULL; |
| 821 | |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 822 | return of_find_node_by_phandle(be32_to_cpup(phandle + index)); |
Grant Likely | 739649c | 2009-04-25 12:52:40 +0000 | [diff] [blame] | 823 | } |
| 824 | EXPORT_SYMBOL(of_parse_phandle); |
| 825 | |
| 826 | /** |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 827 | * 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] | 828 | * @np: pointer to a device tree node containing a list |
| 829 | * @list_name: property name that contains a list |
| 830 | * @cells_name: property name that specifies phandles' arguments count |
| 831 | * @index: index of a phandle to parse out |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 832 | * @out_args: optional pointer to output arguments structure (will be filled) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 833 | * |
| 834 | * This function is useful to parse lists of phandles and their arguments. |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 835 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 836 | * errno value. |
| 837 | * |
| 838 | * Caller is responsible to call of_node_put() on the returned out_args->node |
| 839 | * pointer. |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 840 | * |
| 841 | * Example: |
| 842 | * |
| 843 | * phandle1: node1 { |
| 844 | * #list-cells = <2>; |
| 845 | * } |
| 846 | * |
| 847 | * phandle2: node2 { |
| 848 | * #list-cells = <1>; |
| 849 | * } |
| 850 | * |
| 851 | * node3 { |
| 852 | * list = <&phandle1 1 2 &phandle2 3>; |
| 853 | * } |
| 854 | * |
| 855 | * 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] | 856 | * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 857 | */ |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 858 | int of_parse_phandle_with_args(struct device_node *np, const char *list_name, |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 859 | const char *cells_name, int index, |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 860 | struct of_phandle_args *out_args) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 861 | { |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 862 | const __be32 *list, *list_end; |
| 863 | int size, cur_index = 0; |
| 864 | uint32_t count = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 865 | struct device_node *node = NULL; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 866 | phandle phandle; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 867 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 868 | /* Retrieve the phandle list property */ |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 869 | list = of_get_property(np, list_name, &size); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 870 | if (!list) |
| 871 | return -EINVAL; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 872 | list_end = list + size / sizeof(*list); |
| 873 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 874 | /* Loop over the phandles until all the requested entry is found */ |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 875 | while (list < list_end) { |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 876 | count = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 877 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 878 | /* |
| 879 | * If phandle is 0, then it is an empty entry with no |
| 880 | * arguments. Skip forward to the next entry. |
| 881 | */ |
Grant Likely | 9a6b2e5 | 2010-07-23 01:48:25 -0600 | [diff] [blame] | 882 | phandle = be32_to_cpup(list++); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 883 | if (phandle) { |
| 884 | /* |
| 885 | * Find the provider node and parse the #*-cells |
| 886 | * property to determine the argument length |
| 887 | */ |
| 888 | node = of_find_node_by_phandle(phandle); |
| 889 | if (!node) { |
| 890 | pr_err("%s: could not find phandle\n", |
| 891 | np->full_name); |
| 892 | break; |
| 893 | } |
| 894 | if (of_property_read_u32(node, cells_name, &count)) { |
| 895 | pr_err("%s: could not get %s for %s\n", |
| 896 | np->full_name, cells_name, |
| 897 | node->full_name); |
| 898 | break; |
| 899 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 900 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 901 | /* |
| 902 | * Make sure that the arguments actually fit in the |
| 903 | * remaining property data length |
| 904 | */ |
| 905 | if (list + count > list_end) { |
| 906 | pr_err("%s: arguments longer than property\n", |
| 907 | np->full_name); |
| 908 | break; |
| 909 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 912 | /* |
| 913 | * All of the error cases above bail out of the loop, so at |
| 914 | * this point, the parsing is successful. If the requested |
| 915 | * index matches, then fill the out_args structure and return, |
| 916 | * or return -ENOENT for an empty entry. |
| 917 | */ |
| 918 | if (cur_index == index) { |
| 919 | if (!phandle) |
| 920 | return -ENOENT; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 921 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 922 | if (out_args) { |
| 923 | int i; |
| 924 | if (WARN_ON(count > MAX_PHANDLE_ARGS)) |
| 925 | count = MAX_PHANDLE_ARGS; |
| 926 | out_args->np = node; |
| 927 | out_args->args_count = count; |
| 928 | for (i = 0; i < count; i++) |
| 929 | out_args->args[i] = be32_to_cpup(list++); |
| 930 | } |
| 931 | return 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 932 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 933 | |
| 934 | of_node_put(node); |
| 935 | node = NULL; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 936 | list += count; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 937 | cur_index++; |
| 938 | } |
| 939 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 940 | /* Loop exited without finding a valid entry; return an error */ |
| 941 | if (node) |
| 942 | of_node_put(node); |
| 943 | return -EINVAL; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 944 | } |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 945 | EXPORT_SYMBOL(of_parse_phandle_with_args); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 946 | |
| 947 | /** |
| 948 | * prom_add_property - Add a property to a node |
| 949 | */ |
| 950 | int prom_add_property(struct device_node *np, struct property *prop) |
| 951 | { |
| 952 | struct property **next; |
| 953 | unsigned long flags; |
| 954 | |
| 955 | prop->next = NULL; |
| 956 | write_lock_irqsave(&devtree_lock, flags); |
| 957 | next = &np->properties; |
| 958 | while (*next) { |
| 959 | if (strcmp(prop->name, (*next)->name) == 0) { |
| 960 | /* duplicate ! don't insert it */ |
| 961 | write_unlock_irqrestore(&devtree_lock, flags); |
| 962 | return -1; |
| 963 | } |
| 964 | next = &(*next)->next; |
| 965 | } |
| 966 | *next = prop; |
| 967 | write_unlock_irqrestore(&devtree_lock, flags); |
| 968 | |
| 969 | #ifdef CONFIG_PROC_DEVICETREE |
| 970 | /* try to add to proc as well if it was initialized */ |
| 971 | if (np->pde) |
| 972 | proc_device_tree_add_prop(np->pde, prop); |
| 973 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * prom_remove_property - Remove a property from a node. |
| 980 | * |
| 981 | * Note that we don't actually remove it, since we have given out |
| 982 | * who-knows-how-many pointers to the data using get-property. |
| 983 | * Instead we just move the property to the "dead properties" |
| 984 | * list, so it won't be found any more. |
| 985 | */ |
| 986 | int prom_remove_property(struct device_node *np, struct property *prop) |
| 987 | { |
| 988 | struct property **next; |
| 989 | unsigned long flags; |
| 990 | int found = 0; |
| 991 | |
| 992 | write_lock_irqsave(&devtree_lock, flags); |
| 993 | next = &np->properties; |
| 994 | while (*next) { |
| 995 | if (*next == prop) { |
| 996 | /* found the node */ |
| 997 | *next = prop->next; |
| 998 | prop->next = np->deadprops; |
| 999 | np->deadprops = prop; |
| 1000 | found = 1; |
| 1001 | break; |
| 1002 | } |
| 1003 | next = &(*next)->next; |
| 1004 | } |
| 1005 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1006 | |
| 1007 | if (!found) |
| 1008 | return -ENODEV; |
| 1009 | |
| 1010 | #ifdef CONFIG_PROC_DEVICETREE |
| 1011 | /* try to remove the proc node as well */ |
| 1012 | if (np->pde) |
| 1013 | proc_device_tree_remove_prop(np->pde, prop); |
| 1014 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * prom_update_property - Update a property in a node. |
| 1021 | * |
| 1022 | * Note that we don't actually remove it, since we have given out |
| 1023 | * who-knows-how-many pointers to the data using get-property. |
| 1024 | * Instead we just move the property to the "dead properties" list, |
| 1025 | * and add the new property to the property list |
| 1026 | */ |
| 1027 | int prom_update_property(struct device_node *np, |
| 1028 | struct property *newprop, |
| 1029 | struct property *oldprop) |
| 1030 | { |
| 1031 | struct property **next; |
| 1032 | unsigned long flags; |
| 1033 | int found = 0; |
| 1034 | |
| 1035 | write_lock_irqsave(&devtree_lock, flags); |
| 1036 | next = &np->properties; |
| 1037 | while (*next) { |
| 1038 | if (*next == oldprop) { |
| 1039 | /* found the node */ |
| 1040 | newprop->next = oldprop->next; |
| 1041 | *next = newprop; |
| 1042 | oldprop->next = np->deadprops; |
| 1043 | np->deadprops = oldprop; |
| 1044 | found = 1; |
| 1045 | break; |
| 1046 | } |
| 1047 | next = &(*next)->next; |
| 1048 | } |
| 1049 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1050 | |
| 1051 | if (!found) |
| 1052 | return -ENODEV; |
| 1053 | |
| 1054 | #ifdef CONFIG_PROC_DEVICETREE |
| 1055 | /* try to add to proc as well if it was initialized */ |
| 1056 | if (np->pde) |
| 1057 | proc_device_tree_update_prop(np->pde, newprop, oldprop); |
| 1058 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1059 | |
| 1060 | return 0; |
| 1061 | } |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1062 | |
| 1063 | #if defined(CONFIG_OF_DYNAMIC) |
| 1064 | /* |
| 1065 | * Support for dynamic device trees. |
| 1066 | * |
| 1067 | * On some platforms, the device tree can be manipulated at runtime. |
| 1068 | * The routines in this section support adding, removing and changing |
| 1069 | * device tree nodes. |
| 1070 | */ |
| 1071 | |
| 1072 | /** |
| 1073 | * of_attach_node - Plug a device node into the tree and global list. |
| 1074 | */ |
| 1075 | void of_attach_node(struct device_node *np) |
| 1076 | { |
| 1077 | unsigned long flags; |
| 1078 | |
| 1079 | write_lock_irqsave(&devtree_lock, flags); |
| 1080 | np->sibling = np->parent->child; |
| 1081 | np->allnext = allnodes; |
| 1082 | np->parent->child = np; |
| 1083 | allnodes = np; |
| 1084 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * of_detach_node - "Unplug" a node from the device tree. |
| 1089 | * |
| 1090 | * The caller must hold a reference to the node. The memory associated with |
| 1091 | * the node is not freed until its refcount goes to zero. |
| 1092 | */ |
| 1093 | void of_detach_node(struct device_node *np) |
| 1094 | { |
| 1095 | struct device_node *parent; |
| 1096 | unsigned long flags; |
| 1097 | |
| 1098 | write_lock_irqsave(&devtree_lock, flags); |
| 1099 | |
| 1100 | parent = np->parent; |
| 1101 | if (!parent) |
| 1102 | goto out_unlock; |
| 1103 | |
| 1104 | if (allnodes == np) |
| 1105 | allnodes = np->allnext; |
| 1106 | else { |
| 1107 | struct device_node *prev; |
| 1108 | for (prev = allnodes; |
| 1109 | prev->allnext != np; |
| 1110 | prev = prev->allnext) |
| 1111 | ; |
| 1112 | prev->allnext = np->allnext; |
| 1113 | } |
| 1114 | |
| 1115 | if (parent->child == np) |
| 1116 | parent->child = np->sibling; |
| 1117 | else { |
| 1118 | struct device_node *prevsib; |
| 1119 | for (prevsib = np->parent->child; |
| 1120 | prevsib->sibling != np; |
| 1121 | prevsib = prevsib->sibling) |
| 1122 | ; |
| 1123 | prevsib->sibling = np->sibling; |
| 1124 | } |
| 1125 | |
| 1126 | of_node_set_flag(np, OF_DETACHED); |
| 1127 | |
| 1128 | out_unlock: |
| 1129 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1130 | } |
| 1131 | #endif /* defined(CONFIG_OF_DYNAMIC) */ |
| 1132 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1133 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
| 1134 | int id, const char *stem, int stem_len) |
| 1135 | { |
| 1136 | ap->np = np; |
| 1137 | ap->id = id; |
| 1138 | strncpy(ap->stem, stem, stem_len); |
| 1139 | ap->stem[stem_len] = 0; |
| 1140 | list_add_tail(&ap->link, &aliases_lookup); |
| 1141 | pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n", |
| 1142 | ap->alias, ap->stem, ap->id, np ? np->full_name : NULL); |
| 1143 | } |
| 1144 | |
| 1145 | /** |
| 1146 | * of_alias_scan - Scan all properties of 'aliases' node |
| 1147 | * |
| 1148 | * The function scans all the properties of 'aliases' node and populate |
| 1149 | * the the global lookup table with the properties. It returns the |
| 1150 | * number of alias_prop found, or error code in error case. |
| 1151 | * |
| 1152 | * @dt_alloc: An allocator that provides a virtual address to memory |
| 1153 | * for the resulting tree |
| 1154 | */ |
| 1155 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) |
| 1156 | { |
| 1157 | struct property *pp; |
| 1158 | |
| 1159 | of_chosen = of_find_node_by_path("/chosen"); |
| 1160 | if (of_chosen == NULL) |
| 1161 | of_chosen = of_find_node_by_path("/chosen@0"); |
| 1162 | of_aliases = of_find_node_by_path("/aliases"); |
| 1163 | if (!of_aliases) |
| 1164 | return; |
| 1165 | |
Dong Aisheng | 8af0da9 | 2011-12-22 20:19:24 +0800 | [diff] [blame] | 1166 | for_each_property_of_node(of_aliases, pp) { |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1167 | const char *start = pp->name; |
| 1168 | const char *end = start + strlen(start); |
| 1169 | struct device_node *np; |
| 1170 | struct alias_prop *ap; |
| 1171 | int id, len; |
| 1172 | |
| 1173 | /* Skip those we do not want to proceed */ |
| 1174 | if (!strcmp(pp->name, "name") || |
| 1175 | !strcmp(pp->name, "phandle") || |
| 1176 | !strcmp(pp->name, "linux,phandle")) |
| 1177 | continue; |
| 1178 | |
| 1179 | np = of_find_node_by_path(pp->value); |
| 1180 | if (!np) |
| 1181 | continue; |
| 1182 | |
| 1183 | /* walk the alias backwards to extract the id and work out |
| 1184 | * the 'stem' string */ |
| 1185 | while (isdigit(*(end-1)) && end > start) |
| 1186 | end--; |
| 1187 | len = end - start; |
| 1188 | |
| 1189 | if (kstrtoint(end, 10, &id) < 0) |
| 1190 | continue; |
| 1191 | |
| 1192 | /* Allocate an alias_prop with enough space for the stem */ |
| 1193 | ap = dt_alloc(sizeof(*ap) + len + 1, 4); |
| 1194 | if (!ap) |
| 1195 | continue; |
Grant Likely | db7bd70 | 2013-08-28 21:24:17 +0100 | [diff] [blame] | 1196 | memset(ap, 0, sizeof(*ap) + len + 1); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1197 | ap->alias = start; |
| 1198 | of_alias_add(ap, np, id, start, len); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * of_alias_get_id - Get alias id for the given device_node |
| 1204 | * @np: Pointer to the given device_node |
| 1205 | * @stem: Alias stem of the given device_node |
| 1206 | * |
| 1207 | * The function travels the lookup table to get alias id for the given |
| 1208 | * device_node and alias stem. It returns the alias id if find it. |
| 1209 | */ |
| 1210 | int of_alias_get_id(struct device_node *np, const char *stem) |
| 1211 | { |
| 1212 | struct alias_prop *app; |
| 1213 | int id = -ENODEV; |
| 1214 | |
| 1215 | mutex_lock(&of_aliases_mutex); |
| 1216 | list_for_each_entry(app, &aliases_lookup, link) { |
| 1217 | if (strcmp(app->stem, stem) != 0) |
| 1218 | continue; |
| 1219 | |
| 1220 | if (np == app->np) { |
| 1221 | id = app->id; |
| 1222 | break; |
| 1223 | } |
| 1224 | } |
| 1225 | mutex_unlock(&of_aliases_mutex); |
| 1226 | |
| 1227 | return id; |
| 1228 | } |
| 1229 | EXPORT_SYMBOL_GPL(of_alias_get_id); |