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