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