[POWERPC] Souped-up of_platform_device support
[pandora-kernel.git] / arch / powerpc / kernel / of_platform.c
1 /*
2  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3  *                       <benh@kernel.crashing.org>
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU General Public License
7  *  as published by the Free Software Foundation; either version
8  *  2 of the License, or (at your option) any later version.
9  *
10  */
11
12 #undef DEBUG
13
14 #include <linux/string.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/slab.h>
20
21 #include <asm/errno.h>
22 #include <asm/dcr.h>
23 #include <asm/of_device.h>
24 #include <asm/of_platform.h>
25
26
27 /*
28  * The list of OF IDs below is used for matching bus types in the
29  * system whose devices are to be exposed as of_platform_devices.
30  *
31  * This is the default list valid for most platforms. This file provides
32  * functions who can take an explicit list if necessary though
33  *
34  * The search is always performed recursively looking for children of
35  * the provided device_node and recursively if such a children matches
36  * a bus type in the list
37  */
38
39 static struct of_device_id of_default_bus_ids[] = {
40         { .type = "soc", },
41         { .compatible = "soc", },
42         { .type = "spider", },
43         { .type = "axon", },
44         { .type = "plb5", },
45         { .type = "plb4", },
46         { .type = "opb", },
47         {},
48 };
49
50 /*
51  *
52  * OF platform device type definition & base infrastructure
53  *
54  */
55
56 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
57 {
58         struct of_device * of_dev = to_of_device(dev);
59         struct of_platform_driver * of_drv = to_of_platform_driver(drv);
60         const struct of_device_id * matches = of_drv->match_table;
61
62         if (!matches)
63                 return 0;
64
65         return of_match_device(matches, of_dev) != NULL;
66 }
67
68 static int of_platform_device_probe(struct device *dev)
69 {
70         int error = -ENODEV;
71         struct of_platform_driver *drv;
72         struct of_device *of_dev;
73         const struct of_device_id *match;
74
75         drv = to_of_platform_driver(dev->driver);
76         of_dev = to_of_device(dev);
77
78         if (!drv->probe)
79                 return error;
80
81         of_dev_get(of_dev);
82
83         match = of_match_device(drv->match_table, of_dev);
84         if (match)
85                 error = drv->probe(of_dev, match);
86         if (error)
87                 of_dev_put(of_dev);
88
89         return error;
90 }
91
92 static int of_platform_device_remove(struct device *dev)
93 {
94         struct of_device * of_dev = to_of_device(dev);
95         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
96
97         if (dev->driver && drv->remove)
98                 drv->remove(of_dev);
99         return 0;
100 }
101
102 static int of_platform_device_suspend(struct device *dev, pm_message_t state)
103 {
104         struct of_device * of_dev = to_of_device(dev);
105         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
106         int error = 0;
107
108         if (dev->driver && drv->suspend)
109                 error = drv->suspend(of_dev, state);
110         return error;
111 }
112
113 static int of_platform_device_resume(struct device * dev)
114 {
115         struct of_device * of_dev = to_of_device(dev);
116         struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
117         int error = 0;
118
119         if (dev->driver && drv->resume)
120                 error = drv->resume(of_dev);
121         return error;
122 }
123
124 struct bus_type of_platform_bus_type = {
125        .name    = "of_platform",
126        .match   = of_platform_bus_match,
127        .probe   = of_platform_device_probe,
128        .remove  = of_platform_device_remove,
129        .suspend = of_platform_device_suspend,
130        .resume  = of_platform_device_resume,
131 };
132 EXPORT_SYMBOL(of_platform_bus_type);
133
134 static int __init of_bus_driver_init(void)
135 {
136         return bus_register(&of_platform_bus_type);
137 }
138
139 postcore_initcall(of_bus_driver_init);
140
141 int of_register_platform_driver(struct of_platform_driver *drv)
142 {
143         /* initialize common driver fields */
144         drv->driver.name = drv->name;
145         drv->driver.bus = &of_platform_bus_type;
146
147         /* register with core */
148         return driver_register(&drv->driver);
149 }
150 EXPORT_SYMBOL(of_register_platform_driver);
151
152 void of_unregister_platform_driver(struct of_platform_driver *drv)
153 {
154         driver_unregister(&drv->driver);
155 }
156 EXPORT_SYMBOL(of_unregister_platform_driver);
157
158 static void of_platform_make_bus_id(struct of_device *dev)
159 {
160         struct device_node *node = dev->node;
161         char *name = dev->dev.bus_id;
162         const u32 *reg;
163         u64 addr;
164
165         /*
166          * If it's a DCR based device, use 'd' for native DCRs
167          * and 'D' for MMIO DCRs.
168          */
169 #ifdef CONFIG_PPC_DCR
170         reg = get_property(node, "dcr-reg", NULL);
171         if (reg) {
172 #ifdef CONFIG_PPC_DCR_NATIVE
173                 snprintf(name, BUS_ID_SIZE, "d%x.%s",
174                          *reg, node->name);
175 #else /* CONFIG_PPC_DCR_NATIVE */
176                 addr = of_translate_dcr_address(node, *reg, NULL);
177                 if (addr != OF_BAD_ADDR) {
178                         snprintf(name, BUS_ID_SIZE,
179                                  "D%llx.%s", (unsigned long long)addr,
180                                  node->name);
181                         return;
182                 }
183 #endif /* !CONFIG_PPC_DCR_NATIVE */
184         }
185 #endif /* CONFIG_PPC_DCR */
186
187         /*
188          * For MMIO, get the physical address
189          */
190         reg = get_property(node, "reg", NULL);
191         if (reg) {
192                 addr = of_translate_address(node, reg);
193                 if (addr != OF_BAD_ADDR) {
194                         snprintf(name, BUS_ID_SIZE,
195                                  "%llx.%s", (unsigned long long)addr,
196                                  node->name);
197                         return;
198                 }
199         }
200
201         /*
202          * No BusID, use the node name and pray
203          */
204         snprintf(name, BUS_ID_SIZE, "%s", node->name);
205 }
206
207 struct of_device* of_platform_device_create(struct device_node *np,
208                                             const char *bus_id,
209                                             struct device *parent)
210 {
211         struct of_device *dev;
212
213         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
214         if (!dev)
215                 return NULL;
216         memset(dev, 0, sizeof(*dev));
217
218         dev->node = of_node_get(np);
219         dev->dma_mask = 0xffffffffUL;
220         dev->dev.dma_mask = &dev->dma_mask;
221         dev->dev.parent = parent;
222         dev->dev.bus = &of_platform_bus_type;
223         dev->dev.release = of_release_dev;
224
225         if (bus_id)
226                 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
227         else
228                 of_platform_make_bus_id(dev);
229
230         if (of_device_register(dev) != 0) {
231                 kfree(dev);
232                 return NULL;
233         }
234
235         return dev;
236 }
237 EXPORT_SYMBOL(of_platform_device_create);
238
239
240
241 /**
242  * of_platform_bus_create - Create an OF device for a bus node and all its
243  * children. Optionally recursively instanciate matching busses.
244  * @bus: device node of the bus to instanciate
245  * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
246  * disallow recursive creation of child busses
247  */
248 static int of_platform_bus_create(struct device_node *bus,
249                                   struct of_device_id *matches,
250                                   struct device *parent)
251 {
252         struct device_node *child;
253         struct of_device *dev;
254         int rc = 0;
255
256         for (child = NULL; (child = of_get_next_child(bus, child)); ) {
257                 pr_debug("   create child: %s\n", child->full_name);
258                 dev = of_platform_device_create(child, NULL, parent);
259                 if (dev == NULL)
260                         rc = -ENOMEM;
261                 else if (!of_match_node(matches, child))
262                         continue;
263                 if (rc == 0) {
264                         pr_debug("   and sub busses\n");
265                         rc = of_platform_bus_create(child, matches, &dev->dev);
266                 } if (rc) {
267                         of_node_put(child);
268                         break;
269                 }
270         }
271         return rc;
272 }
273
274 /**
275  * of_platform_bus_probe - Probe the device-tree for platform busses
276  * @root: parent of the first level to probe or NULL for the root of the tree
277  * @matches: match table, NULL to use the default
278  * @parent: parent to hook devices from, NULL for toplevel
279  *
280  * Note that children of the provided root are not instanciated as devices
281  * unless the specified root itself matches the bus list and is not NULL.
282  */
283
284 int of_platform_bus_probe(struct device_node *root,
285                           struct of_device_id *matches,
286                           struct device *parent)
287 {
288         struct device_node *child;
289         struct of_device *dev;
290         int rc = 0;
291
292         if (matches == NULL)
293                 matches = of_default_bus_ids;
294         if (matches == OF_NO_DEEP_PROBE)
295                 return -EINVAL;
296         if (root == NULL)
297                 root = of_find_node_by_path("/");
298         else
299                 of_node_get(root);
300
301         pr_debug("of_platform_bus_probe()\n");
302         pr_debug(" starting at: %s\n", root->full_name);
303
304         /* Do a self check of bus type, if there's a match, create
305          * children
306          */
307         if (of_match_node(matches, root)) {
308                 pr_debug(" root match, create all sub devices\n");
309                 dev = of_platform_device_create(root, NULL, parent);
310                 if (dev == NULL) {
311                         rc = -ENOMEM;
312                         goto bail;
313                 }
314                 pr_debug(" create all sub busses\n");
315                 rc = of_platform_bus_create(root, matches, &dev->dev);
316                 goto bail;
317         }
318         for (child = NULL; (child = of_get_next_child(root, child)); ) {
319                 if (!of_match_node(matches, child))
320                         continue;
321
322                 pr_debug("  match: %s\n", child->full_name);
323                 dev = of_platform_device_create(child, NULL, parent);
324                 if (dev == NULL)
325                         rc = -ENOMEM;
326                 else
327                         rc = of_platform_bus_create(child, matches, &dev->dev);
328                 if (rc) {
329                         of_node_put(child);
330                         break;
331                 }
332         }
333  bail:
334         of_node_put(root);
335         return rc;
336 }
337 EXPORT_SYMBOL(of_platform_bus_probe);
338
339 static int of_dev_node_match(struct device *dev, void *data)
340 {
341         return to_of_device(dev)->node == data;
342 }
343
344 struct of_device *of_find_device_by_node(struct device_node *np)
345 {
346         struct device *dev;
347
348         dev = bus_find_device(&of_platform_bus_type,
349                               NULL, np, of_dev_node_match);
350         if (dev)
351                 return to_of_device(dev);
352         return NULL;
353 }
354 EXPORT_SYMBOL(of_find_device_by_node);
355
356 static int of_dev_phandle_match(struct device *dev, void *data)
357 {
358         phandle *ph = data;
359         return to_of_device(dev)->node->linux_phandle == *ph;
360 }
361
362 struct of_device *of_find_device_by_phandle(phandle ph)
363 {
364         struct device *dev;
365
366         dev = bus_find_device(&of_platform_bus_type,
367                               NULL, &ph, of_dev_phandle_match);
368         if (dev)
369                 return to_of_device(dev);
370         return NULL;
371 }
372 EXPORT_SYMBOL(of_find_device_by_phandle);