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