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