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