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