blob: d6dc5e74c27c213b814337826342605c58a748cf [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>
21
22int of_n_addr_cells(struct device_node *np)
23{
24 const int *ip;
25
26 do {
27 if (np->parent)
28 np = np->parent;
29 ip = of_get_property(np, "#address-cells", NULL);
30 if (ip)
31 return *ip;
32 } while (np->parent);
33 /* No #address-cells property for the root node */
34 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
35}
36EXPORT_SYMBOL(of_n_addr_cells);
37
38int of_n_size_cells(struct device_node *np)
39{
40 const int *ip;
41
42 do {
43 if (np->parent)
44 np = np->parent;
45 ip = of_get_property(np, "#size-cells", NULL);
46 if (ip)
47 return *ip;
48 } while (np->parent);
49 /* No #size-cells property for the root node */
50 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
51}
52EXPORT_SYMBOL(of_n_size_cells);
53
54/*
55 * Find a property with a given name for a given node
56 * and return the value.
57 */
58const void *of_get_property(const struct device_node *np, const char *name,
59 int *lenp)
60{
61 struct property *pp = of_find_property(np, name, lenp);
62
63 return pp ? pp->value : NULL;
64}
65EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +100066
67/** Checks if the given "compat" string matches one of the strings in
68 * the device's "compatible" property
69 */
70int of_device_is_compatible(const struct device_node *device,
71 const char *compat)
72{
73 const char* cp;
74 int cplen, l;
75
76 cp = of_get_property(device, "compatible", &cplen);
77 if (cp == NULL)
78 return 0;
79 while (cplen > 0) {
80 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
81 return 1;
82 l = strlen(cp) + 1;
83 cp += l;
84 cplen -= l;
85 }
86
87 return 0;
88}
89EXPORT_SYMBOL(of_device_is_compatible);