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