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