blob: ad8ac1a8af28689acc7198b22fe95a888d62e6a6 [file] [log] [blame]
Stephen Rothwell97e873e2007-05-01 16:26:07 +10001/*
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>
Stephen Rothwell581b6052007-04-24 16:46:53 +100021#include <linux/spinlock.h>
22
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100023struct device_node *allnodes;
24
Stephen Rothwell581b6052007-04-24 16:46:53 +100025/* use when traversing tree through the allnext, child, sibling,
26 * or parent members of struct device_node.
27 */
28DEFINE_RWLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100029
30int 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}
44EXPORT_SYMBOL(of_n_addr_cells);
45
46int 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}
60EXPORT_SYMBOL(of_n_size_cells);
61
Stephen Rothwell581b6052007-04-24 16:46:53 +100062struct property *of_find_property(const struct device_node *np,
63 const char *name,
64 int *lenp)
65{
66 struct property *pp;
67
Timur Tabi64e45662008-05-08 05:19:59 +100068 if (!np)
69 return NULL;
70
Stephen Rothwell581b6052007-04-24 16:46:53 +100071 read_lock(&devtree_lock);
72 for (pp = np->properties; pp != 0; pp = pp->next) {
73 if (of_prop_cmp(pp->name, name) == 0) {
74 if (lenp != 0)
75 *lenp = pp->length;
76 break;
77 }
78 }
79 read_unlock(&devtree_lock);
80
81 return pp;
82}
83EXPORT_SYMBOL(of_find_property);
84
Stephen Rothwell97e873e2007-05-01 16:26:07 +100085/*
86 * Find a property with a given name for a given node
87 * and return the value.
88 */
89const void *of_get_property(const struct device_node *np, const char *name,
90 int *lenp)
91{
92 struct property *pp = of_find_property(np, name, lenp);
93
94 return pp ? pp->value : NULL;
95}
96EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +100097
98/** Checks if the given "compat" string matches one of the strings in
99 * the device's "compatible" property
100 */
101int of_device_is_compatible(const struct device_node *device,
102 const char *compat)
103{
104 const char* cp;
105 int cplen, l;
106
107 cp = of_get_property(device, "compatible", &cplen);
108 if (cp == NULL)
109 return 0;
110 while (cplen > 0) {
111 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
112 return 1;
113 l = strlen(cp) + 1;
114 cp += l;
115 cplen -= l;
116 }
117
118 return 0;
119}
120EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000121
122/**
Josh Boyer834d97d2008-03-27 00:33:14 +1100123 * of_device_is_available - check if a device is available for use
124 *
125 * @device: Node to check for availability
126 *
127 * Returns 1 if the status property is absent or set to "okay" or "ok",
128 * 0 otherwise
129 */
130int of_device_is_available(const struct device_node *device)
131{
132 const char *status;
133 int statlen;
134
135 status = of_get_property(device, "status", &statlen);
136 if (status == NULL)
137 return 1;
138
139 if (statlen > 0) {
140 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
141 return 1;
142 }
143
144 return 0;
145}
146EXPORT_SYMBOL(of_device_is_available);
147
148/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000149 * of_get_parent - Get a node's parent if any
150 * @node: Node to get parent
151 *
152 * Returns a node pointer with refcount incremented, use
153 * of_node_put() on it when done.
154 */
155struct device_node *of_get_parent(const struct device_node *node)
156{
157 struct device_node *np;
158
159 if (!node)
160 return NULL;
161
162 read_lock(&devtree_lock);
163 np = of_node_get(node->parent);
164 read_unlock(&devtree_lock);
165 return np;
166}
167EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000168
169/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000170 * of_get_next_parent - Iterate to a node's parent
171 * @node: Node to get parent of
172 *
173 * This is like of_get_parent() except that it drops the
174 * refcount on the passed node, making it suitable for iterating
175 * through a node's parents.
176 *
177 * Returns a node pointer with refcount incremented, use
178 * of_node_put() on it when done.
179 */
180struct device_node *of_get_next_parent(struct device_node *node)
181{
182 struct device_node *parent;
183
184 if (!node)
185 return NULL;
186
187 read_lock(&devtree_lock);
188 parent = of_node_get(node->parent);
189 of_node_put(node);
190 read_unlock(&devtree_lock);
191 return parent;
192}
193
194/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000195 * of_get_next_child - Iterate a node childs
196 * @node: parent node
197 * @prev: previous child of the parent node, or NULL to get first
198 *
199 * Returns a node pointer with refcount incremented, use
200 * of_node_put() on it when done.
201 */
202struct device_node *of_get_next_child(const struct device_node *node,
203 struct device_node *prev)
204{
205 struct device_node *next;
206
207 read_lock(&devtree_lock);
208 next = prev ? prev->sibling : node->child;
209 for (; next; next = next->sibling)
210 if (of_node_get(next))
211 break;
212 of_node_put(prev);
213 read_unlock(&devtree_lock);
214 return next;
215}
216EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000217
218/**
219 * of_find_node_by_path - Find a node matching a full OF path
220 * @path: The full path to match
221 *
222 * Returns a node pointer with refcount incremented, use
223 * of_node_put() on it when done.
224 */
225struct device_node *of_find_node_by_path(const char *path)
226{
227 struct device_node *np = allnodes;
228
229 read_lock(&devtree_lock);
230 for (; np; np = np->allnext) {
231 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
232 && of_node_get(np))
233 break;
234 }
235 read_unlock(&devtree_lock);
236 return np;
237}
238EXPORT_SYMBOL(of_find_node_by_path);
239
240/**
241 * of_find_node_by_name - Find a node by its "name" property
242 * @from: The node to start searching from or NULL, the node
243 * you pass will not be searched, only the next one
244 * will; typically, you pass what the previous call
245 * returned. of_node_put() will be called on it
246 * @name: The name string to match against
247 *
248 * Returns a node pointer with refcount incremented, use
249 * of_node_put() on it when done.
250 */
251struct device_node *of_find_node_by_name(struct device_node *from,
252 const char *name)
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->name && (of_node_cmp(np->name, name) == 0)
260 && of_node_get(np))
261 break;
262 of_node_put(from);
263 read_unlock(&devtree_lock);
264 return np;
265}
266EXPORT_SYMBOL(of_find_node_by_name);
267
268/**
269 * of_find_node_by_type - Find a node by its "device_type" property
270 * @from: The node to start searching from, or NULL to start searching
271 * the entire device tree. The node you pass will not be
272 * searched, only the next one will; typically, you pass
273 * what the previous call returned. of_node_put() will be
274 * called on from for you.
275 * @type: The type string to match against
276 *
277 * Returns a node pointer with refcount incremented, use
278 * of_node_put() on it when done.
279 */
280struct device_node *of_find_node_by_type(struct device_node *from,
281 const char *type)
282{
283 struct device_node *np;
284
285 read_lock(&devtree_lock);
286 np = from ? from->allnext : allnodes;
287 for (; np; np = np->allnext)
288 if (np->type && (of_node_cmp(np->type, type) == 0)
289 && of_node_get(np))
290 break;
291 of_node_put(from);
292 read_unlock(&devtree_lock);
293 return np;
294}
295EXPORT_SYMBOL(of_find_node_by_type);
296
297/**
298 * of_find_compatible_node - Find a node based on type and one of the
299 * tokens in its "compatible" property
300 * @from: The node to start searching from or NULL, the node
301 * you pass will not be searched, only the next one
302 * will; typically, you pass what the previous call
303 * returned. of_node_put() will be called on it
304 * @type: The type string to match "device_type" or NULL to ignore
305 * @compatible: The string to match to one of the tokens in the device
306 * "compatible" list.
307 *
308 * Returns a node pointer with refcount incremented, use
309 * of_node_put() on it when done.
310 */
311struct device_node *of_find_compatible_node(struct device_node *from,
312 const char *type, const char *compatible)
313{
314 struct device_node *np;
315
316 read_lock(&devtree_lock);
317 np = from ? from->allnext : allnodes;
318 for (; np; np = np->allnext) {
319 if (type
320 && !(np->type && (of_node_cmp(np->type, type) == 0)))
321 continue;
322 if (of_device_is_compatible(np, compatible) && of_node_get(np))
323 break;
324 }
325 of_node_put(from);
326 read_unlock(&devtree_lock);
327 return np;
328}
329EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100330
331/**
332 * of_match_node - Tell if an device_node has a matching of_match structure
333 * @matches: array of of device match structures to search in
334 * @node: the of device structure to match against
335 *
336 * Low level utility function used by device matching.
337 */
338const struct of_device_id *of_match_node(const struct of_device_id *matches,
339 const struct device_node *node)
340{
341 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
342 int match = 1;
343 if (matches->name[0])
344 match &= node->name
345 && !strcmp(matches->name, node->name);
346 if (matches->type[0])
347 match &= node->type
348 && !strcmp(matches->type, node->type);
349 if (matches->compatible[0])
350 match &= of_device_is_compatible(node,
351 matches->compatible);
352 if (match)
353 return matches;
354 matches++;
355 }
356 return NULL;
357}
358EXPORT_SYMBOL(of_match_node);
359
360/**
361 * of_find_matching_node - Find a node based on an of_device_id match
362 * table.
363 * @from: The node to start searching from or NULL, the node
364 * you pass will not be searched, only the next one
365 * will; typically, you pass what the previous call
366 * returned. of_node_put() will be called on it
367 * @matches: array of of device match structures to search in
368 *
369 * Returns a node pointer with refcount incremented, use
370 * of_node_put() on it when done.
371 */
372struct device_node *of_find_matching_node(struct device_node *from,
373 const struct of_device_id *matches)
374{
375 struct device_node *np;
376
377 read_lock(&devtree_lock);
378 np = from ? from->allnext : allnodes;
379 for (; np; np = np->allnext) {
380 if (of_match_node(matches, np) && of_node_get(np))
381 break;
382 }
383 of_node_put(from);
384 read_unlock(&devtree_lock);
385 return np;
386}
387EXPORT_SYMBOL(of_find_matching_node);
Grant Likely3f07af42008-07-25 22:25:13 -0400388
389/**
390 * of_modalias_table: Table of explicit compatible ==> modalias mappings
391 *
392 * This table allows particulare compatible property values to be mapped
393 * to modalias strings. This is useful for busses which do not directly
394 * understand the OF device tree but are populated based on data contained
395 * within the device tree. SPI and I2C are the two current users of this
396 * table.
397 *
398 * In most cases, devices do not need to be listed in this table because
399 * the modalias value can be derived directly from the compatible table.
400 * However, if for any reason a value cannot be derived, then this table
401 * provides a method to override the implicit derivation.
402 *
403 * At the moment, a single table is used for all bus types because it is
404 * assumed that the data size is small and that the compatible values
405 * should already be distinct enough to differentiate between SPI, I2C
406 * and other devices.
407 */
408struct of_modalias_table {
409 char *of_device;
410 char *modalias;
411};
412static struct of_modalias_table of_modalias_table[] = {
413 /* Empty for now; add entries as needed */
414};
415
416/**
417 * of_modalias_node - Lookup appropriate modalias for a device node
418 * @node: pointer to a device tree node
419 * @modalias: Pointer to buffer that modalias value will be copied into
420 * @len: Length of modalias value
421 *
422 * Based on the value of the compatible property, this routine will determine
423 * an appropriate modalias value for a particular device tree node. Three
424 * separate methods are used to derive a modalias value.
425 *
426 * First method is to lookup the compatible value in of_modalias_table.
427 * Second is to look for a "linux,<modalias>" entry in the compatible list
428 * and used that for modalias. Third is to strip off the manufacturer
429 * prefix from the first compatible entry and use the remainder as modalias
430 *
431 * This routine returns 0 on success
432 */
433int of_modalias_node(struct device_node *node, char *modalias, int len)
434{
435 int i, cplen;
436 const char *compatible;
437 const char *p;
438
439 /* 1. search for exception list entry */
440 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
441 compatible = of_modalias_table[i].of_device;
442 if (!of_device_is_compatible(node, compatible))
443 continue;
444 strlcpy(modalias, of_modalias_table[i].modalias, len);
445 return 0;
446 }
447
448 compatible = of_get_property(node, "compatible", &cplen);
449 if (!compatible)
450 return -ENODEV;
451
452 /* 2. search for linux,<modalias> entry */
453 p = compatible;
454 while (cplen > 0) {
455 if (!strncmp(p, "linux,", 6)) {
456 p += 6;
457 strlcpy(modalias, p, len);
458 return 0;
459 }
460
461 i = strlen(p) + 1;
462 p += i;
463 cplen -= i;
464 }
465
466 /* 3. take first compatible entry and strip manufacturer */
467 p = strchr(compatible, ',');
468 if (!p)
469 return -ENODEV;
470 p++;
471 strlcpy(modalias, p, len);
472 return 0;
473}
474EXPORT_SYMBOL_GPL(of_modalias_node);
475