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