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 | */ |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
| 23 | |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 24 | struct device_node *allnodes; |
| 25 | |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 26 | /* use when traversing tree through the allnext, child, sibling, |
| 27 | * or parent members of struct device_node. |
| 28 | */ |
| 29 | DEFINE_RWLOCK(devtree_lock); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 30 | |
| 31 | int of_n_addr_cells(struct device_node *np) |
| 32 | { |
| 33 | const int *ip; |
| 34 | |
| 35 | do { |
| 36 | if (np->parent) |
| 37 | np = np->parent; |
| 38 | ip = of_get_property(np, "#address-cells", NULL); |
| 39 | if (ip) |
| 40 | return *ip; |
| 41 | } while (np->parent); |
| 42 | /* No #address-cells property for the root node */ |
| 43 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 44 | } |
| 45 | EXPORT_SYMBOL(of_n_addr_cells); |
| 46 | |
| 47 | int of_n_size_cells(struct device_node *np) |
| 48 | { |
| 49 | const int *ip; |
| 50 | |
| 51 | do { |
| 52 | if (np->parent) |
| 53 | np = np->parent; |
| 54 | ip = of_get_property(np, "#size-cells", NULL); |
| 55 | if (ip) |
| 56 | return *ip; |
| 57 | } while (np->parent); |
| 58 | /* No #size-cells property for the root node */ |
| 59 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 60 | } |
| 61 | EXPORT_SYMBOL(of_n_size_cells); |
| 62 | |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 63 | struct property *of_find_property(const struct device_node *np, |
| 64 | const char *name, |
| 65 | int *lenp) |
| 66 | { |
| 67 | struct property *pp; |
| 68 | |
Timur Tabi | 64e4566 | 2008-05-08 05:19:59 +1000 | [diff] [blame] | 69 | if (!np) |
| 70 | return NULL; |
| 71 | |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 72 | read_lock(&devtree_lock); |
| 73 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 74 | if (of_prop_cmp(pp->name, name) == 0) { |
| 75 | if (lenp != 0) |
| 76 | *lenp = pp->length; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | read_unlock(&devtree_lock); |
| 81 | |
| 82 | return pp; |
| 83 | } |
| 84 | EXPORT_SYMBOL(of_find_property); |
| 85 | |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 86 | /** |
| 87 | * of_find_all_nodes - Get next node in global list |
| 88 | * @prev: Previous node or NULL to start iteration |
| 89 | * of_node_put() will be called on it |
| 90 | * |
| 91 | * Returns a node pointer with refcount incremented, use |
| 92 | * of_node_put() on it when done. |
| 93 | */ |
| 94 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 95 | { |
| 96 | struct device_node *np; |
| 97 | |
| 98 | read_lock(&devtree_lock); |
| 99 | np = prev ? prev->allnext : allnodes; |
| 100 | for (; np != NULL; np = np->allnext) |
| 101 | if (of_node_get(np)) |
| 102 | break; |
| 103 | of_node_put(prev); |
| 104 | read_unlock(&devtree_lock); |
| 105 | return np; |
| 106 | } |
| 107 | EXPORT_SYMBOL(of_find_all_nodes); |
| 108 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 109 | /* |
| 110 | * Find a property with a given name for a given node |
| 111 | * and return the value. |
| 112 | */ |
| 113 | const void *of_get_property(const struct device_node *np, const char *name, |
| 114 | int *lenp) |
| 115 | { |
| 116 | struct property *pp = of_find_property(np, name, lenp); |
| 117 | |
| 118 | return pp ? pp->value : NULL; |
| 119 | } |
| 120 | EXPORT_SYMBOL(of_get_property); |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 121 | |
| 122 | /** Checks if the given "compat" string matches one of the strings in |
| 123 | * the device's "compatible" property |
| 124 | */ |
| 125 | int of_device_is_compatible(const struct device_node *device, |
| 126 | const char *compat) |
| 127 | { |
| 128 | const char* cp; |
| 129 | int cplen, l; |
| 130 | |
| 131 | cp = of_get_property(device, "compatible", &cplen); |
| 132 | if (cp == NULL) |
| 133 | return 0; |
| 134 | while (cplen > 0) { |
| 135 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) |
| 136 | return 1; |
| 137 | l = strlen(cp) + 1; |
| 138 | cp += l; |
| 139 | cplen -= l; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | EXPORT_SYMBOL(of_device_is_compatible); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 145 | |
| 146 | /** |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame^] | 147 | * machine_is_compatible - Test root of device tree for a given compatible value |
| 148 | * @compat: compatible string to look for in root node's compatible property. |
| 149 | * |
| 150 | * Returns true if the root node has the given value in its |
| 151 | * compatible property. |
| 152 | */ |
| 153 | int machine_is_compatible(const char *compat) |
| 154 | { |
| 155 | struct device_node *root; |
| 156 | int rc = 0; |
| 157 | |
| 158 | root = of_find_node_by_path("/"); |
| 159 | if (root) { |
| 160 | rc = of_device_is_compatible(root, compat); |
| 161 | of_node_put(root); |
| 162 | } |
| 163 | return rc; |
| 164 | } |
| 165 | EXPORT_SYMBOL(machine_is_compatible); |
| 166 | |
| 167 | /** |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 168 | * of_device_is_available - check if a device is available for use |
| 169 | * |
| 170 | * @device: Node to check for availability |
| 171 | * |
| 172 | * Returns 1 if the status property is absent or set to "okay" or "ok", |
| 173 | * 0 otherwise |
| 174 | */ |
| 175 | int of_device_is_available(const struct device_node *device) |
| 176 | { |
| 177 | const char *status; |
| 178 | int statlen; |
| 179 | |
| 180 | status = of_get_property(device, "status", &statlen); |
| 181 | if (status == NULL) |
| 182 | return 1; |
| 183 | |
| 184 | if (statlen > 0) { |
| 185 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | EXPORT_SYMBOL(of_device_is_available); |
| 192 | |
| 193 | /** |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 194 | * of_get_parent - Get a node's parent if any |
| 195 | * @node: Node to get parent |
| 196 | * |
| 197 | * Returns a node pointer with refcount incremented, use |
| 198 | * of_node_put() on it when done. |
| 199 | */ |
| 200 | struct device_node *of_get_parent(const struct device_node *node) |
| 201 | { |
| 202 | struct device_node *np; |
| 203 | |
| 204 | if (!node) |
| 205 | return NULL; |
| 206 | |
| 207 | read_lock(&devtree_lock); |
| 208 | np = of_node_get(node->parent); |
| 209 | read_unlock(&devtree_lock); |
| 210 | return np; |
| 211 | } |
| 212 | EXPORT_SYMBOL(of_get_parent); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 213 | |
| 214 | /** |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 215 | * of_get_next_parent - Iterate to a node's parent |
| 216 | * @node: Node to get parent of |
| 217 | * |
| 218 | * This is like of_get_parent() except that it drops the |
| 219 | * refcount on the passed node, making it suitable for iterating |
| 220 | * through a node's parents. |
| 221 | * |
| 222 | * Returns a node pointer with refcount incremented, use |
| 223 | * of_node_put() on it when done. |
| 224 | */ |
| 225 | struct device_node *of_get_next_parent(struct device_node *node) |
| 226 | { |
| 227 | struct device_node *parent; |
| 228 | |
| 229 | if (!node) |
| 230 | return NULL; |
| 231 | |
| 232 | read_lock(&devtree_lock); |
| 233 | parent = of_node_get(node->parent); |
| 234 | of_node_put(node); |
| 235 | read_unlock(&devtree_lock); |
| 236 | return parent; |
| 237 | } |
| 238 | |
| 239 | /** |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 240 | * of_get_next_child - Iterate a node childs |
| 241 | * @node: parent node |
| 242 | * @prev: previous child of the parent node, or NULL to get first |
| 243 | * |
| 244 | * Returns a node pointer with refcount incremented, use |
| 245 | * of_node_put() on it when done. |
| 246 | */ |
| 247 | struct device_node *of_get_next_child(const struct device_node *node, |
| 248 | struct device_node *prev) |
| 249 | { |
| 250 | struct device_node *next; |
| 251 | |
| 252 | read_lock(&devtree_lock); |
| 253 | next = prev ? prev->sibling : node->child; |
| 254 | for (; next; next = next->sibling) |
| 255 | if (of_node_get(next)) |
| 256 | break; |
| 257 | of_node_put(prev); |
| 258 | read_unlock(&devtree_lock); |
| 259 | return next; |
| 260 | } |
| 261 | EXPORT_SYMBOL(of_get_next_child); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * of_find_node_by_path - Find a node matching a full OF path |
| 265 | * @path: The full path to match |
| 266 | * |
| 267 | * Returns a node pointer with refcount incremented, use |
| 268 | * of_node_put() on it when done. |
| 269 | */ |
| 270 | struct device_node *of_find_node_by_path(const char *path) |
| 271 | { |
| 272 | struct device_node *np = allnodes; |
| 273 | |
| 274 | read_lock(&devtree_lock); |
| 275 | for (; np; np = np->allnext) { |
| 276 | if (np->full_name && (of_node_cmp(np->full_name, path) == 0) |
| 277 | && of_node_get(np)) |
| 278 | break; |
| 279 | } |
| 280 | read_unlock(&devtree_lock); |
| 281 | return np; |
| 282 | } |
| 283 | EXPORT_SYMBOL(of_find_node_by_path); |
| 284 | |
| 285 | /** |
| 286 | * of_find_node_by_name - Find a node by its "name" property |
| 287 | * @from: The node to start searching from or NULL, the node |
| 288 | * you pass will not be searched, only the next one |
| 289 | * will; typically, you pass what the previous call |
| 290 | * returned. of_node_put() will be called on it |
| 291 | * @name: The name string to match against |
| 292 | * |
| 293 | * Returns a node pointer with refcount incremented, use |
| 294 | * of_node_put() on it when done. |
| 295 | */ |
| 296 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 297 | const char *name) |
| 298 | { |
| 299 | struct device_node *np; |
| 300 | |
| 301 | read_lock(&devtree_lock); |
| 302 | np = from ? from->allnext : allnodes; |
| 303 | for (; np; np = np->allnext) |
| 304 | if (np->name && (of_node_cmp(np->name, name) == 0) |
| 305 | && of_node_get(np)) |
| 306 | break; |
| 307 | of_node_put(from); |
| 308 | read_unlock(&devtree_lock); |
| 309 | return np; |
| 310 | } |
| 311 | EXPORT_SYMBOL(of_find_node_by_name); |
| 312 | |
| 313 | /** |
| 314 | * of_find_node_by_type - Find a node by its "device_type" property |
| 315 | * @from: The node to start searching from, or NULL to start searching |
| 316 | * the entire device tree. The node you pass will not be |
| 317 | * searched, only the next one will; typically, you pass |
| 318 | * what the previous call returned. of_node_put() will be |
| 319 | * called on from for you. |
| 320 | * @type: The type string to match against |
| 321 | * |
| 322 | * Returns a node pointer with refcount incremented, use |
| 323 | * of_node_put() on it when done. |
| 324 | */ |
| 325 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 326 | const char *type) |
| 327 | { |
| 328 | struct device_node *np; |
| 329 | |
| 330 | read_lock(&devtree_lock); |
| 331 | np = from ? from->allnext : allnodes; |
| 332 | for (; np; np = np->allnext) |
| 333 | if (np->type && (of_node_cmp(np->type, type) == 0) |
| 334 | && of_node_get(np)) |
| 335 | break; |
| 336 | of_node_put(from); |
| 337 | read_unlock(&devtree_lock); |
| 338 | return np; |
| 339 | } |
| 340 | EXPORT_SYMBOL(of_find_node_by_type); |
| 341 | |
| 342 | /** |
| 343 | * of_find_compatible_node - Find a node based on type and one of the |
| 344 | * tokens in its "compatible" property |
| 345 | * @from: The node to start searching from or NULL, the node |
| 346 | * you pass will not be searched, only the next one |
| 347 | * will; typically, you pass what the previous call |
| 348 | * returned. of_node_put() will be called on it |
| 349 | * @type: The type string to match "device_type" or NULL to ignore |
| 350 | * @compatible: The string to match to one of the tokens in the device |
| 351 | * "compatible" list. |
| 352 | * |
| 353 | * Returns a node pointer with refcount incremented, use |
| 354 | * of_node_put() on it when done. |
| 355 | */ |
| 356 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 357 | const char *type, const char *compatible) |
| 358 | { |
| 359 | struct device_node *np; |
| 360 | |
| 361 | read_lock(&devtree_lock); |
| 362 | np = from ? from->allnext : allnodes; |
| 363 | for (; np; np = np->allnext) { |
| 364 | if (type |
| 365 | && !(np->type && (of_node_cmp(np->type, type) == 0))) |
| 366 | continue; |
| 367 | if (of_device_is_compatible(np, compatible) && of_node_get(np)) |
| 368 | break; |
| 369 | } |
| 370 | of_node_put(from); |
| 371 | read_unlock(&devtree_lock); |
| 372 | return np; |
| 373 | } |
| 374 | EXPORT_SYMBOL(of_find_compatible_node); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 375 | |
| 376 | /** |
Michael Ellerman | 1e291b1 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 377 | * of_find_node_with_property - Find a node which has a property with |
| 378 | * the given name. |
| 379 | * @from: The node to start searching from or NULL, the node |
| 380 | * you pass will not be searched, only the next one |
| 381 | * will; typically, you pass what the previous call |
| 382 | * returned. of_node_put() will be called on it |
| 383 | * @prop_name: The name of the property to look for. |
| 384 | * |
| 385 | * Returns a node pointer with refcount incremented, use |
| 386 | * of_node_put() on it when done. |
| 387 | */ |
| 388 | struct device_node *of_find_node_with_property(struct device_node *from, |
| 389 | const char *prop_name) |
| 390 | { |
| 391 | struct device_node *np; |
| 392 | struct property *pp; |
| 393 | |
| 394 | read_lock(&devtree_lock); |
| 395 | np = from ? from->allnext : allnodes; |
| 396 | for (; np; np = np->allnext) { |
| 397 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 398 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
| 399 | of_node_get(np); |
| 400 | goto out; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | out: |
| 405 | of_node_put(from); |
| 406 | read_unlock(&devtree_lock); |
| 407 | return np; |
| 408 | } |
| 409 | EXPORT_SYMBOL(of_find_node_with_property); |
| 410 | |
| 411 | /** |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 412 | * of_match_node - Tell if an device_node has a matching of_match structure |
| 413 | * @matches: array of of device match structures to search in |
| 414 | * @node: the of device structure to match against |
| 415 | * |
| 416 | * Low level utility function used by device matching. |
| 417 | */ |
| 418 | const struct of_device_id *of_match_node(const struct of_device_id *matches, |
| 419 | const struct device_node *node) |
| 420 | { |
| 421 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { |
| 422 | int match = 1; |
| 423 | if (matches->name[0]) |
| 424 | match &= node->name |
| 425 | && !strcmp(matches->name, node->name); |
| 426 | if (matches->type[0]) |
| 427 | match &= node->type |
| 428 | && !strcmp(matches->type, node->type); |
| 429 | if (matches->compatible[0]) |
| 430 | match &= of_device_is_compatible(node, |
| 431 | matches->compatible); |
| 432 | if (match) |
| 433 | return matches; |
| 434 | matches++; |
| 435 | } |
| 436 | return NULL; |
| 437 | } |
| 438 | EXPORT_SYMBOL(of_match_node); |
| 439 | |
| 440 | /** |
| 441 | * of_find_matching_node - Find a node based on an of_device_id match |
| 442 | * table. |
| 443 | * @from: The node to start searching from or NULL, the node |
| 444 | * you pass will not be searched, only the next one |
| 445 | * will; typically, you pass what the previous call |
| 446 | * returned. of_node_put() will be called on it |
| 447 | * @matches: array of of device match structures to search in |
| 448 | * |
| 449 | * Returns a node pointer with refcount incremented, use |
| 450 | * of_node_put() on it when done. |
| 451 | */ |
| 452 | struct device_node *of_find_matching_node(struct device_node *from, |
| 453 | const struct of_device_id *matches) |
| 454 | { |
| 455 | struct device_node *np; |
| 456 | |
| 457 | read_lock(&devtree_lock); |
| 458 | np = from ? from->allnext : allnodes; |
| 459 | for (; np; np = np->allnext) { |
| 460 | if (of_match_node(matches, np) && of_node_get(np)) |
| 461 | break; |
| 462 | } |
| 463 | of_node_put(from); |
| 464 | read_unlock(&devtree_lock); |
| 465 | return np; |
| 466 | } |
| 467 | EXPORT_SYMBOL(of_find_matching_node); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 468 | |
| 469 | /** |
| 470 | * of_modalias_table: Table of explicit compatible ==> modalias mappings |
| 471 | * |
| 472 | * This table allows particulare compatible property values to be mapped |
| 473 | * to modalias strings. This is useful for busses which do not directly |
| 474 | * understand the OF device tree but are populated based on data contained |
| 475 | * within the device tree. SPI and I2C are the two current users of this |
| 476 | * table. |
| 477 | * |
| 478 | * In most cases, devices do not need to be listed in this table because |
| 479 | * the modalias value can be derived directly from the compatible table. |
| 480 | * However, if for any reason a value cannot be derived, then this table |
| 481 | * provides a method to override the implicit derivation. |
| 482 | * |
| 483 | * At the moment, a single table is used for all bus types because it is |
| 484 | * assumed that the data size is small and that the compatible values |
| 485 | * should already be distinct enough to differentiate between SPI, I2C |
| 486 | * and other devices. |
| 487 | */ |
| 488 | struct of_modalias_table { |
| 489 | char *of_device; |
| 490 | char *modalias; |
| 491 | }; |
| 492 | static struct of_modalias_table of_modalias_table[] = { |
Anton Vorontsov | 4c3ed7d | 2008-09-23 14:12:19 +0400 | [diff] [blame] | 493 | { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" }, |
Anton Vorontsov | 3f1c6eb | 2009-03-31 15:24:38 -0700 | [diff] [blame] | 494 | { "mmc-spi-slot", "mmc_spi" }, |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | /** |
| 498 | * of_modalias_node - Lookup appropriate modalias for a device node |
| 499 | * @node: pointer to a device tree node |
| 500 | * @modalias: Pointer to buffer that modalias value will be copied into |
| 501 | * @len: Length of modalias value |
| 502 | * |
| 503 | * Based on the value of the compatible property, this routine will determine |
Grant Likely | 58f467c | 2008-10-08 05:05:29 +0000 | [diff] [blame] | 504 | * an appropriate modalias value for a particular device tree node. Two |
| 505 | * separate methods are attempted to derive a modalias value. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 506 | * |
| 507 | * First method is to lookup the compatible value in of_modalias_table. |
Grant Likely | 58f467c | 2008-10-08 05:05:29 +0000 | [diff] [blame] | 508 | * Second is to strip off the manufacturer prefix from the first |
| 509 | * compatible entry and use the remainder as modalias |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 510 | * |
| 511 | * This routine returns 0 on success |
| 512 | */ |
| 513 | int of_modalias_node(struct device_node *node, char *modalias, int len) |
| 514 | { |
| 515 | int i, cplen; |
| 516 | const char *compatible; |
| 517 | const char *p; |
| 518 | |
| 519 | /* 1. search for exception list entry */ |
| 520 | for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) { |
| 521 | compatible = of_modalias_table[i].of_device; |
| 522 | if (!of_device_is_compatible(node, compatible)) |
| 523 | continue; |
| 524 | strlcpy(modalias, of_modalias_table[i].modalias, len); |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | compatible = of_get_property(node, "compatible", &cplen); |
| 529 | if (!compatible) |
| 530 | return -ENODEV; |
| 531 | |
Grant Likely | 58f467c | 2008-10-08 05:05:29 +0000 | [diff] [blame] | 532 | /* 2. take first compatible entry and strip manufacturer */ |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 533 | p = strchr(compatible, ','); |
| 534 | if (!p) |
| 535 | return -ENODEV; |
| 536 | p++; |
| 537 | strlcpy(modalias, p, len); |
| 538 | return 0; |
| 539 | } |
| 540 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 541 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 542 | /** |
Grant Likely | 739649c | 2009-04-25 12:52:40 +0000 | [diff] [blame] | 543 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
| 544 | * @np: Pointer to device node holding phandle property |
| 545 | * @phandle_name: Name of property holding a phandle value |
| 546 | * @index: For properties holding a table of phandles, this is the index into |
| 547 | * the table |
| 548 | * |
| 549 | * Returns the device_node pointer with refcount incremented. Use |
| 550 | * of_node_put() on it when done. |
| 551 | */ |
| 552 | struct device_node * |
| 553 | of_parse_phandle(struct device_node *np, const char *phandle_name, int index) |
| 554 | { |
| 555 | const phandle *phandle; |
| 556 | int size; |
| 557 | |
| 558 | phandle = of_get_property(np, phandle_name, &size); |
| 559 | if ((!phandle) || (size < sizeof(*phandle) * (index + 1))) |
| 560 | return NULL; |
| 561 | |
| 562 | return of_find_node_by_phandle(phandle[index]); |
| 563 | } |
| 564 | EXPORT_SYMBOL(of_parse_phandle); |
| 565 | |
| 566 | /** |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 567 | * of_parse_phandles_with_args - Find a node pointed by phandle in a list |
| 568 | * @np: pointer to a device tree node containing a list |
| 569 | * @list_name: property name that contains a list |
| 570 | * @cells_name: property name that specifies phandles' arguments count |
| 571 | * @index: index of a phandle to parse out |
Anton Vorontsov | 7736a3d | 2008-12-05 08:15:46 +0000 | [diff] [blame] | 572 | * @out_node: optional pointer to device_node struct pointer (will be filled) |
| 573 | * @out_args: optional pointer to arguments pointer (will be filled) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 574 | * |
| 575 | * This function is useful to parse lists of phandles and their arguments. |
| 576 | * Returns 0 on success and fills out_node and out_args, on error returns |
| 577 | * appropriate errno value. |
| 578 | * |
| 579 | * Example: |
| 580 | * |
| 581 | * phandle1: node1 { |
| 582 | * #list-cells = <2>; |
| 583 | * } |
| 584 | * |
| 585 | * phandle2: node2 { |
| 586 | * #list-cells = <1>; |
| 587 | * } |
| 588 | * |
| 589 | * node3 { |
| 590 | * list = <&phandle1 1 2 &phandle2 3>; |
| 591 | * } |
| 592 | * |
| 593 | * To get a device_node of the `node2' node you may call this: |
| 594 | * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args); |
| 595 | */ |
| 596 | int of_parse_phandles_with_args(struct device_node *np, const char *list_name, |
| 597 | const char *cells_name, int index, |
| 598 | struct device_node **out_node, |
| 599 | const void **out_args) |
| 600 | { |
| 601 | int ret = -EINVAL; |
| 602 | const u32 *list; |
| 603 | const u32 *list_end; |
| 604 | int size; |
| 605 | int cur_index = 0; |
| 606 | struct device_node *node = NULL; |
Anton Vorontsov | 7736a3d | 2008-12-05 08:15:46 +0000 | [diff] [blame] | 607 | const void *args = NULL; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 608 | |
| 609 | list = of_get_property(np, list_name, &size); |
| 610 | if (!list) { |
| 611 | ret = -ENOENT; |
| 612 | goto err0; |
| 613 | } |
| 614 | list_end = list + size / sizeof(*list); |
| 615 | |
| 616 | while (list < list_end) { |
| 617 | const u32 *cells; |
| 618 | const phandle *phandle; |
| 619 | |
Anton Vorontsov | c1bb7c6 | 2008-12-05 08:15:39 +0000 | [diff] [blame] | 620 | phandle = list++; |
| 621 | args = list; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 622 | |
| 623 | /* one cell hole in the list = <>; */ |
Anton Vorontsov | c1bb7c6 | 2008-12-05 08:15:39 +0000 | [diff] [blame] | 624 | if (!*phandle) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 625 | goto next; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 626 | |
| 627 | node = of_find_node_by_phandle(*phandle); |
| 628 | if (!node) { |
| 629 | pr_debug("%s: could not find phandle\n", |
| 630 | np->full_name); |
| 631 | goto err0; |
| 632 | } |
| 633 | |
| 634 | cells = of_get_property(node, cells_name, &size); |
| 635 | if (!cells || size != sizeof(*cells)) { |
| 636 | pr_debug("%s: could not get %s for %s\n", |
| 637 | np->full_name, cells_name, node->full_name); |
| 638 | goto err1; |
| 639 | } |
| 640 | |
Anton Vorontsov | c1bb7c6 | 2008-12-05 08:15:39 +0000 | [diff] [blame] | 641 | list += *cells; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 642 | if (list > list_end) { |
| 643 | pr_debug("%s: insufficient arguments length\n", |
| 644 | np->full_name); |
| 645 | goto err1; |
| 646 | } |
| 647 | next: |
| 648 | if (cur_index == index) |
| 649 | break; |
| 650 | |
| 651 | of_node_put(node); |
| 652 | node = NULL; |
Anton Vorontsov | 7736a3d | 2008-12-05 08:15:46 +0000 | [diff] [blame] | 653 | args = NULL; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 654 | cur_index++; |
| 655 | } |
| 656 | |
| 657 | if (!node) { |
Anton Vorontsov | 7736a3d | 2008-12-05 08:15:46 +0000 | [diff] [blame] | 658 | /* |
| 659 | * args w/o node indicates that the loop above has stopped at |
| 660 | * the 'hole' cell. Report this differently. |
| 661 | */ |
| 662 | if (args) |
| 663 | ret = -EEXIST; |
| 664 | else |
| 665 | ret = -ENOENT; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 666 | goto err0; |
| 667 | } |
| 668 | |
Anton Vorontsov | 7736a3d | 2008-12-05 08:15:46 +0000 | [diff] [blame] | 669 | if (out_node) |
| 670 | *out_node = node; |
| 671 | if (out_args) |
| 672 | *out_args = args; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | err1: |
| 676 | of_node_put(node); |
| 677 | err0: |
| 678 | pr_debug("%s failed with status %d\n", __func__, ret); |
| 679 | return ret; |
| 680 | } |
| 681 | EXPORT_SYMBOL(of_parse_phandles_with_args); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 682 | |
| 683 | /** |
| 684 | * prom_add_property - Add a property to a node |
| 685 | */ |
| 686 | int prom_add_property(struct device_node *np, struct property *prop) |
| 687 | { |
| 688 | struct property **next; |
| 689 | unsigned long flags; |
| 690 | |
| 691 | prop->next = NULL; |
| 692 | write_lock_irqsave(&devtree_lock, flags); |
| 693 | next = &np->properties; |
| 694 | while (*next) { |
| 695 | if (strcmp(prop->name, (*next)->name) == 0) { |
| 696 | /* duplicate ! don't insert it */ |
| 697 | write_unlock_irqrestore(&devtree_lock, flags); |
| 698 | return -1; |
| 699 | } |
| 700 | next = &(*next)->next; |
| 701 | } |
| 702 | *next = prop; |
| 703 | write_unlock_irqrestore(&devtree_lock, flags); |
| 704 | |
| 705 | #ifdef CONFIG_PROC_DEVICETREE |
| 706 | /* try to add to proc as well if it was initialized */ |
| 707 | if (np->pde) |
| 708 | proc_device_tree_add_prop(np->pde, prop); |
| 709 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * prom_remove_property - Remove a property from a node. |
| 716 | * |
| 717 | * Note that we don't actually remove it, since we have given out |
| 718 | * who-knows-how-many pointers to the data using get-property. |
| 719 | * Instead we just move the property to the "dead properties" |
| 720 | * list, so it won't be found any more. |
| 721 | */ |
| 722 | int prom_remove_property(struct device_node *np, struct property *prop) |
| 723 | { |
| 724 | struct property **next; |
| 725 | unsigned long flags; |
| 726 | int found = 0; |
| 727 | |
| 728 | write_lock_irqsave(&devtree_lock, flags); |
| 729 | next = &np->properties; |
| 730 | while (*next) { |
| 731 | if (*next == prop) { |
| 732 | /* found the node */ |
| 733 | *next = prop->next; |
| 734 | prop->next = np->deadprops; |
| 735 | np->deadprops = prop; |
| 736 | found = 1; |
| 737 | break; |
| 738 | } |
| 739 | next = &(*next)->next; |
| 740 | } |
| 741 | write_unlock_irqrestore(&devtree_lock, flags); |
| 742 | |
| 743 | if (!found) |
| 744 | return -ENODEV; |
| 745 | |
| 746 | #ifdef CONFIG_PROC_DEVICETREE |
| 747 | /* try to remove the proc node as well */ |
| 748 | if (np->pde) |
| 749 | proc_device_tree_remove_prop(np->pde, prop); |
| 750 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * prom_update_property - Update a property in a node. |
| 757 | * |
| 758 | * Note that we don't actually remove it, since we have given out |
| 759 | * who-knows-how-many pointers to the data using get-property. |
| 760 | * Instead we just move the property to the "dead properties" list, |
| 761 | * and add the new property to the property list |
| 762 | */ |
| 763 | int prom_update_property(struct device_node *np, |
| 764 | struct property *newprop, |
| 765 | struct property *oldprop) |
| 766 | { |
| 767 | struct property **next; |
| 768 | unsigned long flags; |
| 769 | int found = 0; |
| 770 | |
| 771 | write_lock_irqsave(&devtree_lock, flags); |
| 772 | next = &np->properties; |
| 773 | while (*next) { |
| 774 | if (*next == oldprop) { |
| 775 | /* found the node */ |
| 776 | newprop->next = oldprop->next; |
| 777 | *next = newprop; |
| 778 | oldprop->next = np->deadprops; |
| 779 | np->deadprops = oldprop; |
| 780 | found = 1; |
| 781 | break; |
| 782 | } |
| 783 | next = &(*next)->next; |
| 784 | } |
| 785 | write_unlock_irqrestore(&devtree_lock, flags); |
| 786 | |
| 787 | if (!found) |
| 788 | return -ENODEV; |
| 789 | |
| 790 | #ifdef CONFIG_PROC_DEVICETREE |
| 791 | /* try to add to proc as well if it was initialized */ |
| 792 | if (np->pde) |
| 793 | proc_device_tree_update_prop(np->pde, newprop, oldprop); |
| 794 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 795 | |
| 796 | return 0; |
| 797 | } |