aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@woody.linux-foundation.org>2007-07-20 12:18:08 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-07-20 12:18:08 -0400
commit2cb7e714229681408e323852bed939989faf6991 (patch)
tree552b78fa5830a0337594f9fbab5f1dc0306e93cd /drivers
parentd638d4990bfb99998420e78e8fd4607bca5cf8d0 (diff)
parent3f23de10f283819bcdc0d2282e8b5b14c2e96d3b (diff)
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/sfr/ofcons
* 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/sfr/ofcons: Create drivers/of/platform.c Create linux/of_platorm.h [SPARC/64] Rename some functions like PowerPC Begin consolidation of of_device.h Begin to consolidate of_device.c Consolidate of_find_node_by routines Consolidate of_get_next_child Consolidate of_get_parent Consolidate of_find_property Consolidate of_device_is_compatible Start split out of common open firmware code Split out common parts of prom.h
Diffstat (limited to 'drivers')
-rw-r--r--drivers/Kconfig2
-rw-r--r--drivers/Makefile1
-rw-r--r--drivers/of/Kconfig3
-rw-r--r--drivers/of/Makefile2
-rw-r--r--drivers/of/base.c275
-rw-r--r--drivers/of/device.c131
-rw-r--r--drivers/of/platform.c96
7 files changed, 510 insertions, 0 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 707650ab77a7..3e1c442deff9 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -8,6 +8,8 @@ source "drivers/connector/Kconfig"
8 8
9source "drivers/mtd/Kconfig" 9source "drivers/mtd/Kconfig"
10 10
11source "drivers/of/Kconfig"
12
11source "drivers/parport/Kconfig" 13source "drivers/parport/Kconfig"
12 14
13source "drivers/pnp/Kconfig" 15source "drivers/pnp/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index 0ea8e3237c0d..a9e4c5f922a0 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -86,3 +86,4 @@ obj-$(CONFIG_GENERIC_TIME) += clocksource/
86obj-$(CONFIG_DMA_ENGINE) += dma/ 86obj-$(CONFIG_DMA_ENGINE) += dma/
87obj-$(CONFIG_HID) += hid/ 87obj-$(CONFIG_HID) += hid/
88obj-$(CONFIG_PPC_PS3) += ps3/ 88obj-$(CONFIG_PPC_PS3) += ps3/
89obj-$(CONFIG_OF) += of/
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
new file mode 100644
index 000000000000..c03072b12f42
--- /dev/null
+++ b/drivers/of/Kconfig
@@ -0,0 +1,3 @@
1config OF_DEVICE
2 def_bool y
3 depends on OF && (SPARC || PPC_OF)
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
new file mode 100644
index 000000000000..ab9be5d5255b
--- /dev/null
+++ b/drivers/of/Makefile
@@ -0,0 +1,2 @@
1obj-y = base.o
2obj-$(CONFIG_OF_DEVICE) += device.o platform.o
diff --git a/drivers/of/base.c b/drivers/of/base.c
new file mode 100644
index 000000000000..9377f3bc410a
--- /dev/null
+++ b/drivers/of/base.c
@@ -0,0 +1,275 @@
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>
21#include <linux/spinlock.h>
22
23struct device_node *allnodes;
24
25/* use when traversing tree through the allnext, child, sibling,
26 * or parent members of struct device_node.
27 */
28DEFINE_RWLOCK(devtree_lock);
29
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
62struct 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}
80EXPORT_SYMBOL(of_find_property);
81
82/*
83 * Find a property with a given name for a given node
84 * and return the value.
85 */
86const 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}
93EXPORT_SYMBOL(of_get_property);
94
95/** Checks if the given "compat" string matches one of the strings in
96 * the device's "compatible" property
97 */
98int 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}
117EXPORT_SYMBOL(of_device_is_compatible);
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 */
126struct 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}
138EXPORT_SYMBOL(of_get_parent);
139
140/**
141 * of_get_next_child - Iterate a node childs
142 * @node: parent node
143 * @prev: previous child of the parent node, or NULL to get first
144 *
145 * Returns a node pointer with refcount incremented, use
146 * of_node_put() on it when done.
147 */
148struct device_node *of_get_next_child(const struct device_node *node,
149 struct device_node *prev)
150{
151 struct device_node *next;
152
153 read_lock(&devtree_lock);
154 next = prev ? prev->sibling : node->child;
155 for (; next; next = next->sibling)
156 if (of_node_get(next))
157 break;
158 of_node_put(prev);
159 read_unlock(&devtree_lock);
160 return next;
161}
162EXPORT_SYMBOL(of_get_next_child);
163
164/**
165 * of_find_node_by_path - Find a node matching a full OF path
166 * @path: The full path to match
167 *
168 * Returns a node pointer with refcount incremented, use
169 * of_node_put() on it when done.
170 */
171struct device_node *of_find_node_by_path(const char *path)
172{
173 struct device_node *np = allnodes;
174
175 read_lock(&devtree_lock);
176 for (; np; np = np->allnext) {
177 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
178 && of_node_get(np))
179 break;
180 }
181 read_unlock(&devtree_lock);
182 return np;
183}
184EXPORT_SYMBOL(of_find_node_by_path);
185
186/**
187 * of_find_node_by_name - Find a node by its "name" property
188 * @from: The node to start searching from or NULL, the node
189 * you pass will not be searched, only the next one
190 * will; typically, you pass what the previous call
191 * returned. of_node_put() will be called on it
192 * @name: The name string to match against
193 *
194 * Returns a node pointer with refcount incremented, use
195 * of_node_put() on it when done.
196 */
197struct device_node *of_find_node_by_name(struct device_node *from,
198 const char *name)
199{
200 struct device_node *np;
201
202 read_lock(&devtree_lock);
203 np = from ? from->allnext : allnodes;
204 for (; np; np = np->allnext)
205 if (np->name && (of_node_cmp(np->name, name) == 0)
206 && of_node_get(np))
207 break;
208 of_node_put(from);
209 read_unlock(&devtree_lock);
210 return np;
211}
212EXPORT_SYMBOL(of_find_node_by_name);
213
214/**
215 * of_find_node_by_type - Find a node by its "device_type" property
216 * @from: The node to start searching from, or NULL to start searching
217 * the entire device tree. The node you pass will not be
218 * searched, only the next one will; typically, you pass
219 * what the previous call returned. of_node_put() will be
220 * called on from for you.
221 * @type: The type string to match against
222 *
223 * Returns a node pointer with refcount incremented, use
224 * of_node_put() on it when done.
225 */
226struct device_node *of_find_node_by_type(struct device_node *from,
227 const char *type)
228{
229 struct device_node *np;
230
231 read_lock(&devtree_lock);
232 np = from ? from->allnext : allnodes;
233 for (; np; np = np->allnext)
234 if (np->type && (of_node_cmp(np->type, type) == 0)
235 && of_node_get(np))
236 break;
237 of_node_put(from);
238 read_unlock(&devtree_lock);
239 return np;
240}
241EXPORT_SYMBOL(of_find_node_by_type);
242
243/**
244 * of_find_compatible_node - Find a node based on type and one of the
245 * tokens in its "compatible" property
246 * @from: The node to start searching from or NULL, the node
247 * you pass will not be searched, only the next one
248 * will; typically, you pass what the previous call
249 * returned. of_node_put() will be called on it
250 * @type: The type string to match "device_type" or NULL to ignore
251 * @compatible: The string to match to one of the tokens in the device
252 * "compatible" list.
253 *
254 * Returns a node pointer with refcount incremented, use
255 * of_node_put() on it when done.
256 */
257struct device_node *of_find_compatible_node(struct device_node *from,
258 const char *type, const char *compatible)
259{
260 struct device_node *np;
261
262 read_lock(&devtree_lock);
263 np = from ? from->allnext : allnodes;
264 for (; np; np = np->allnext) {
265 if (type
266 && !(np->type && (of_node_cmp(np->type, type) == 0)))
267 continue;
268 if (of_device_is_compatible(np, compatible) && of_node_get(np))
269 break;
270 }
271 of_node_put(from);
272 read_unlock(&devtree_lock);
273 return np;
274}
275EXPORT_SYMBOL(of_find_compatible_node);
diff --git a/drivers/of/device.c b/drivers/of/device.c
new file mode 100644
index 000000000000..6245f060fb77
--- /dev/null
+++ b/drivers/of/device.c
@@ -0,0 +1,131 @@
1#include <linux/string.h>
2#include <linux/kernel.h>
3#include <linux/of.h>
4#include <linux/of_device.h>
5#include <linux/init.h>
6#include <linux/module.h>
7#include <linux/mod_devicetable.h>
8#include <linux/slab.h>
9
10#include <asm/errno.h>
11
12/**
13 * of_match_node - Tell if an device_node has a matching of_match structure
14 * @ids: array of of device match structures to search in
15 * @node: the of device structure to match against
16 *
17 * Low level utility function used by device matching.
18 */
19const struct of_device_id *of_match_node(const struct of_device_id *matches,
20 const struct device_node *node)
21{
22 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
23 int match = 1;
24 if (matches->name[0])
25 match &= node->name
26 && !strcmp(matches->name, node->name);
27 if (matches->type[0])
28 match &= node->type
29 && !strcmp(matches->type, node->type);
30 if (matches->compatible[0])
31 match &= of_device_is_compatible(node,
32 matches->compatible);
33 if (match)
34 return matches;
35 matches++;
36 }
37 return NULL;
38}
39EXPORT_SYMBOL(of_match_node);
40
41/**
42 * of_match_device - Tell if an of_device structure has a matching
43 * of_match structure
44 * @ids: array of of device match structures to search in
45 * @dev: the of device structure to match against
46 *
47 * Used by a driver to check whether an of_device present in the
48 * system is in its list of supported devices.
49 */
50const struct of_device_id *of_match_device(const struct of_device_id *matches,
51 const struct of_device *dev)
52{
53 if (!dev->node)
54 return NULL;
55 return of_match_node(matches, dev->node);
56}
57EXPORT_SYMBOL(of_match_device);
58
59struct of_device *of_dev_get(struct of_device *dev)
60{
61 struct device *tmp;
62
63 if (!dev)
64 return NULL;
65 tmp = get_device(&dev->dev);
66 if (tmp)
67 return to_of_device(tmp);
68 else
69 return NULL;
70}
71EXPORT_SYMBOL(of_dev_get);
72
73void of_dev_put(struct of_device *dev)
74{
75 if (dev)
76 put_device(&dev->dev);
77}
78EXPORT_SYMBOL(of_dev_put);
79
80static ssize_t dev_show_devspec(struct device *dev,
81 struct device_attribute *attr, char *buf)
82{
83 struct of_device *ofdev;
84
85 ofdev = to_of_device(dev);
86 return sprintf(buf, "%s", ofdev->node->full_name);
87}
88
89static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
90
91/**
92 * of_release_dev - free an of device structure when all users of it are finished.
93 * @dev: device that's been disconnected
94 *
95 * Will be called only by the device core when all users of this of device are
96 * done.
97 */
98void of_release_dev(struct device *dev)
99{
100 struct of_device *ofdev;
101
102 ofdev = to_of_device(dev);
103 of_node_put(ofdev->node);
104 kfree(ofdev);
105}
106EXPORT_SYMBOL(of_release_dev);
107
108int of_device_register(struct of_device *ofdev)
109{
110 int rc;
111
112 BUG_ON(ofdev->node == NULL);
113
114 rc = device_register(&ofdev->dev);
115 if (rc)
116 return rc;
117
118 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
119 if (rc)
120 device_unregister(&ofdev->dev);
121
122 return rc;
123}
124EXPORT_SYMBOL(of_device_register);
125
126void of_device_unregister(struct of_device *ofdev)
127{
128 device_remove_file(&ofdev->dev, &dev_attr_devspec);
129 device_unregister(&ofdev->dev);
130}
131EXPORT_SYMBOL(of_device_unregister);
diff --git a/drivers/of/platform.c b/drivers/of/platform.c
new file mode 100644
index 000000000000..864f09fd9f86
--- /dev/null
+++ b/drivers/of/platform.c
@@ -0,0 +1,96 @@
1/*
2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 * and Arnd Bergmann, IBM Corp.
5 * Merged from powerpc/kernel/of_platform.c and
6 * sparc{,64}/kernel/of_device.c by Stephen Rothwell
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 */
14#include <linux/errno.h>
15#include <linux/device.h>
16#include <linux/of_device.h>
17#include <linux/of_platform.h>
18
19static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
20{
21 struct of_device *of_dev = to_of_device(dev);
22 struct of_platform_driver *of_drv = to_of_platform_driver(drv);
23 const struct of_device_id *matches = of_drv->match_table;
24
25 if (!matches)
26 return 0;
27
28 return of_match_device(matches, of_dev) != NULL;
29}
30
31static int of_platform_device_probe(struct device *dev)
32{
33 int error = -ENODEV;
34 struct of_platform_driver *drv;
35 struct of_device *of_dev;
36 const struct of_device_id *match;
37
38 drv = to_of_platform_driver(dev->driver);
39 of_dev = to_of_device(dev);
40
41 if (!drv->probe)
42 return error;
43
44 of_dev_get(of_dev);
45
46 match = of_match_device(drv->match_table, of_dev);
47 if (match)
48 error = drv->probe(of_dev, match);
49 if (error)
50 of_dev_put(of_dev);
51
52 return error;
53}
54
55static int of_platform_device_remove(struct device *dev)
56{
57 struct of_device *of_dev = to_of_device(dev);
58 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
59
60 if (dev->driver && drv->remove)
61 drv->remove(of_dev);
62 return 0;
63}
64
65static int of_platform_device_suspend(struct device *dev, pm_message_t state)
66{
67 struct of_device *of_dev = to_of_device(dev);
68 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
69 int error = 0;
70
71 if (dev->driver && drv->suspend)
72 error = drv->suspend(of_dev, state);
73 return error;
74}
75
76static int of_platform_device_resume(struct device * dev)
77{
78 struct of_device *of_dev = to_of_device(dev);
79 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
80 int error = 0;
81
82 if (dev->driver && drv->resume)
83 error = drv->resume(of_dev);
84 return error;
85}
86
87int of_bus_type_init(struct bus_type *bus, const char *name)
88{
89 bus->name = name;
90 bus->match = of_platform_bus_match;
91 bus->probe = of_platform_device_probe;
92 bus->remove = of_platform_device_remove;
93 bus->suspend = of_platform_device_suspend;
94 bus->resume = of_platform_device_resume;
95 return bus_register(bus);
96}