]>
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 | * | |
12 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell. | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU General Public License | |
16 | * as published by the Free Software Foundation; either version | |
17 | * 2 of the License, or (at your option) any later version. | |
18 | */ | |
19 | #include <linux/module.h> | |
20 | #include <linux/of.h> | |
581b605a SR |
21 | #include <linux/spinlock.h> |
22 | ||
1ef4d424 SR |
23 | struct device_node *allnodes; |
24 | ||
581b605a SR |
25 | /* use when traversing tree through the allnext, child, sibling, |
26 | * or parent members of struct device_node. | |
27 | */ | |
28 | DEFINE_RWLOCK(devtree_lock); | |
97e873e5 SR |
29 | |
30 | int of_n_addr_cells(struct device_node *np) | |
31 | { | |
32 | const int *ip; | |
33 | ||
34 | do { | |
35 | if (np->parent) | |
36 | np = np->parent; | |
37 | ip = of_get_property(np, "#address-cells", NULL); | |
38 | if (ip) | |
39 | return *ip; | |
40 | } while (np->parent); | |
41 | /* No #address-cells property for the root node */ | |
42 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; | |
43 | } | |
44 | EXPORT_SYMBOL(of_n_addr_cells); | |
45 | ||
46 | int of_n_size_cells(struct device_node *np) | |
47 | { | |
48 | const int *ip; | |
49 | ||
50 | do { | |
51 | if (np->parent) | |
52 | np = np->parent; | |
53 | ip = of_get_property(np, "#size-cells", NULL); | |
54 | if (ip) | |
55 | return *ip; | |
56 | } while (np->parent); | |
57 | /* No #size-cells property for the root node */ | |
58 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; | |
59 | } | |
60 | EXPORT_SYMBOL(of_n_size_cells); | |
61 | ||
581b605a SR |
62 | struct property *of_find_property(const struct device_node *np, |
63 | const char *name, | |
64 | int *lenp) | |
65 | { | |
66 | struct property *pp; | |
67 | ||
68 | read_lock(&devtree_lock); | |
69 | for (pp = np->properties; pp != 0; pp = pp->next) { | |
70 | if (of_prop_cmp(pp->name, name) == 0) { | |
71 | if (lenp != 0) | |
72 | *lenp = pp->length; | |
73 | break; | |
74 | } | |
75 | } | |
76 | read_unlock(&devtree_lock); | |
77 | ||
78 | return pp; | |
79 | } | |
80 | EXPORT_SYMBOL(of_find_property); | |
81 | ||
97e873e5 SR |
82 | /* |
83 | * Find a property with a given name for a given node | |
84 | * and return the value. | |
85 | */ | |
86 | const void *of_get_property(const struct device_node *np, const char *name, | |
87 | int *lenp) | |
88 | { | |
89 | struct property *pp = of_find_property(np, name, lenp); | |
90 | ||
91 | return pp ? pp->value : NULL; | |
92 | } | |
93 | EXPORT_SYMBOL(of_get_property); | |
0081cbc3 SR |
94 | |
95 | /** Checks if the given "compat" string matches one of the strings in | |
96 | * the device's "compatible" property | |
97 | */ | |
98 | int of_device_is_compatible(const struct device_node *device, | |
99 | const char *compat) | |
100 | { | |
101 | const char* cp; | |
102 | int cplen, l; | |
103 | ||
104 | cp = of_get_property(device, "compatible", &cplen); | |
105 | if (cp == NULL) | |
106 | return 0; | |
107 | while (cplen > 0) { | |
108 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) | |
109 | return 1; | |
110 | l = strlen(cp) + 1; | |
111 | cp += l; | |
112 | cplen -= l; | |
113 | } | |
114 | ||
115 | return 0; | |
116 | } | |
117 | EXPORT_SYMBOL(of_device_is_compatible); | |
e679c5f4 SR |
118 | |
119 | /** | |
120 | * of_get_parent - Get a node's parent if any | |
121 | * @node: Node to get parent | |
122 | * | |
123 | * Returns a node pointer with refcount incremented, use | |
124 | * of_node_put() on it when done. | |
125 | */ | |
126 | struct device_node *of_get_parent(const struct device_node *node) | |
127 | { | |
128 | struct device_node *np; | |
129 | ||
130 | if (!node) | |
131 | return NULL; | |
132 | ||
133 | read_lock(&devtree_lock); | |
134 | np = of_node_get(node->parent); | |
135 | read_unlock(&devtree_lock); | |
136 | return np; | |
137 | } | |
138 | EXPORT_SYMBOL(of_get_parent); | |
d1cd355a | 139 | |
f4eb0107 ME |
140 | /** |
141 | * of_get_next_parent - Iterate to a node's parent | |
142 | * @node: Node to get parent of | |
143 | * | |
144 | * This is like of_get_parent() except that it drops the | |
145 | * refcount on the passed node, making it suitable for iterating | |
146 | * through a node's parents. | |
147 | * | |
148 | * Returns a node pointer with refcount incremented, use | |
149 | * of_node_put() on it when done. | |
150 | */ | |
151 | struct device_node *of_get_next_parent(struct device_node *node) | |
152 | { | |
153 | struct device_node *parent; | |
154 | ||
155 | if (!node) | |
156 | return NULL; | |
157 | ||
158 | read_lock(&devtree_lock); | |
159 | parent = of_node_get(node->parent); | |
160 | of_node_put(node); | |
161 | read_unlock(&devtree_lock); | |
162 | return parent; | |
163 | } | |
164 | ||
d1cd355a SR |
165 | /** |
166 | * of_get_next_child - Iterate a node childs | |
167 | * @node: parent node | |
168 | * @prev: previous child of the parent node, or NULL to get first | |
169 | * | |
170 | * Returns a node pointer with refcount incremented, use | |
171 | * of_node_put() on it when done. | |
172 | */ | |
173 | struct device_node *of_get_next_child(const struct device_node *node, | |
174 | struct device_node *prev) | |
175 | { | |
176 | struct device_node *next; | |
177 | ||
178 | read_lock(&devtree_lock); | |
179 | next = prev ? prev->sibling : node->child; | |
180 | for (; next; next = next->sibling) | |
181 | if (of_node_get(next)) | |
182 | break; | |
183 | of_node_put(prev); | |
184 | read_unlock(&devtree_lock); | |
185 | return next; | |
186 | } | |
187 | EXPORT_SYMBOL(of_get_next_child); | |
1ef4d424 SR |
188 | |
189 | /** | |
190 | * of_find_node_by_path - Find a node matching a full OF path | |
191 | * @path: The full path to match | |
192 | * | |
193 | * Returns a node pointer with refcount incremented, use | |
194 | * of_node_put() on it when done. | |
195 | */ | |
196 | struct device_node *of_find_node_by_path(const char *path) | |
197 | { | |
198 | struct device_node *np = allnodes; | |
199 | ||
200 | read_lock(&devtree_lock); | |
201 | for (; np; np = np->allnext) { | |
202 | if (np->full_name && (of_node_cmp(np->full_name, path) == 0) | |
203 | && of_node_get(np)) | |
204 | break; | |
205 | } | |
206 | read_unlock(&devtree_lock); | |
207 | return np; | |
208 | } | |
209 | EXPORT_SYMBOL(of_find_node_by_path); | |
210 | ||
211 | /** | |
212 | * of_find_node_by_name - Find a node by its "name" property | |
213 | * @from: The node to start searching from or NULL, the node | |
214 | * you pass will not be searched, only the next one | |
215 | * will; typically, you pass what the previous call | |
216 | * returned. of_node_put() will be called on it | |
217 | * @name: The name string to match against | |
218 | * | |
219 | * Returns a node pointer with refcount incremented, use | |
220 | * of_node_put() on it when done. | |
221 | */ | |
222 | struct device_node *of_find_node_by_name(struct device_node *from, | |
223 | const char *name) | |
224 | { | |
225 | struct device_node *np; | |
226 | ||
227 | read_lock(&devtree_lock); | |
228 | np = from ? from->allnext : allnodes; | |
229 | for (; np; np = np->allnext) | |
230 | if (np->name && (of_node_cmp(np->name, name) == 0) | |
231 | && of_node_get(np)) | |
232 | break; | |
233 | of_node_put(from); | |
234 | read_unlock(&devtree_lock); | |
235 | return np; | |
236 | } | |
237 | EXPORT_SYMBOL(of_find_node_by_name); | |
238 | ||
239 | /** | |
240 | * of_find_node_by_type - Find a node by its "device_type" property | |
241 | * @from: The node to start searching from, or NULL to start searching | |
242 | * the entire device tree. The node you pass will not be | |
243 | * searched, only the next one will; typically, you pass | |
244 | * what the previous call returned. of_node_put() will be | |
245 | * called on from for you. | |
246 | * @type: The type string to match against | |
247 | * | |
248 | * Returns a node pointer with refcount incremented, use | |
249 | * of_node_put() on it when done. | |
250 | */ | |
251 | struct device_node *of_find_node_by_type(struct device_node *from, | |
252 | const char *type) | |
253 | { | |
254 | struct device_node *np; | |
255 | ||
256 | read_lock(&devtree_lock); | |
257 | np = from ? from->allnext : allnodes; | |
258 | for (; np; np = np->allnext) | |
259 | if (np->type && (of_node_cmp(np->type, type) == 0) | |
260 | && of_node_get(np)) | |
261 | break; | |
262 | of_node_put(from); | |
263 | read_unlock(&devtree_lock); | |
264 | return np; | |
265 | } | |
266 | EXPORT_SYMBOL(of_find_node_by_type); | |
267 | ||
268 | /** | |
269 | * of_find_compatible_node - Find a node based on type and one of the | |
270 | * tokens in its "compatible" property | |
271 | * @from: The node to start searching from or NULL, the node | |
272 | * you pass will not be searched, only the next one | |
273 | * will; typically, you pass what the previous call | |
274 | * returned. of_node_put() will be called on it | |
275 | * @type: The type string to match "device_type" or NULL to ignore | |
276 | * @compatible: The string to match to one of the tokens in the device | |
277 | * "compatible" list. | |
278 | * | |
279 | * Returns a node pointer with refcount incremented, use | |
280 | * of_node_put() on it when done. | |
281 | */ | |
282 | struct device_node *of_find_compatible_node(struct device_node *from, | |
283 | const char *type, const char *compatible) | |
284 | { | |
285 | struct device_node *np; | |
286 | ||
287 | read_lock(&devtree_lock); | |
288 | np = from ? from->allnext : allnodes; | |
289 | for (; np; np = np->allnext) { | |
290 | if (type | |
291 | && !(np->type && (of_node_cmp(np->type, type) == 0))) | |
292 | continue; | |
293 | if (of_device_is_compatible(np, compatible) && of_node_get(np)) | |
294 | break; | |
295 | } | |
296 | of_node_put(from); | |
297 | read_unlock(&devtree_lock); | |
298 | return np; | |
299 | } | |
300 | EXPORT_SYMBOL(of_find_compatible_node); | |
283029d1 GL |
301 | |
302 | /** | |
303 | * of_match_node - Tell if an device_node has a matching of_match structure | |
304 | * @matches: array of of device match structures to search in | |
305 | * @node: the of device structure to match against | |
306 | * | |
307 | * Low level utility function used by device matching. | |
308 | */ | |
309 | const struct of_device_id *of_match_node(const struct of_device_id *matches, | |
310 | const struct device_node *node) | |
311 | { | |
312 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { | |
313 | int match = 1; | |
314 | if (matches->name[0]) | |
315 | match &= node->name | |
316 | && !strcmp(matches->name, node->name); | |
317 | if (matches->type[0]) | |
318 | match &= node->type | |
319 | && !strcmp(matches->type, node->type); | |
320 | if (matches->compatible[0]) | |
321 | match &= of_device_is_compatible(node, | |
322 | matches->compatible); | |
323 | if (match) | |
324 | return matches; | |
325 | matches++; | |
326 | } | |
327 | return NULL; | |
328 | } | |
329 | EXPORT_SYMBOL(of_match_node); | |
330 | ||
331 | /** | |
332 | * of_find_matching_node - Find a node based on an of_device_id match | |
333 | * table. | |
334 | * @from: The node to start searching from or NULL, the node | |
335 | * you pass will not be searched, only the next one | |
336 | * will; typically, you pass what the previous call | |
337 | * returned. of_node_put() will be called on it | |
338 | * @matches: array of of device match structures to search in | |
339 | * | |
340 | * Returns a node pointer with refcount incremented, use | |
341 | * of_node_put() on it when done. | |
342 | */ | |
343 | struct device_node *of_find_matching_node(struct device_node *from, | |
344 | const struct of_device_id *matches) | |
345 | { | |
346 | struct device_node *np; | |
347 | ||
348 | read_lock(&devtree_lock); | |
349 | np = from ? from->allnext : allnodes; | |
350 | for (; np; np = np->allnext) { | |
351 | if (of_match_node(matches, np) && of_node_get(np)) | |
352 | break; | |
353 | } | |
354 | of_node_put(from); | |
355 | read_unlock(&devtree_lock); | |
356 | return np; | |
357 | } | |
358 | EXPORT_SYMBOL(of_find_matching_node); |