Merge branch 'linus' into stackprotector
[linux-2.6] / drivers / of / platform.c
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/module.h>
16 #include <linux/device.h>
17 #include <linux/of_device.h>
18 #include <linux/of_platform.h>
19
20 extern struct device_attribute of_platform_device_attrs[];
21
22 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
23 {
24         struct of_device *of_dev = to_of_device(dev);
25         struct of_platform_driver *of_drv = to_of_platform_driver(drv);
26         const struct of_device_id *matches = of_drv->match_table;
27
28         if (!matches)
29                 return 0;
30
31         return of_match_device(matches, of_dev) != NULL;
32 }
33
34 static int of_platform_device_probe(struct device *dev)
35 {
36         int error = -ENODEV;
37         struct of_platform_driver *drv;
38         struct of_device *of_dev;
39         const struct of_device_id *match;
40
41         drv = to_of_platform_driver(dev->driver);
42         of_dev = to_of_device(dev);
43
44         if (!drv->probe)
45                 return error;
46
47         of_dev_get(of_dev);
48
49         match = of_match_device(drv->match_table, of_dev);
50         if (match)
51                 error = drv->probe(of_dev, match);
52         if (error)
53                 of_dev_put(of_dev);
54
55         return error;
56 }
57
58 static int of_platform_device_remove(struct device *dev)
59 {
60         struct of_device *of_dev = to_of_device(dev);
61         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
62
63         if (dev->driver && drv->remove)
64                 drv->remove(of_dev);
65         return 0;
66 }
67
68 static int of_platform_device_suspend(struct device *dev, pm_message_t state)
69 {
70         struct of_device *of_dev = to_of_device(dev);
71         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
72         int error = 0;
73
74         if (dev->driver && drv->suspend)
75                 error = drv->suspend(of_dev, state);
76         return error;
77 }
78
79 static int of_platform_device_resume(struct device * dev)
80 {
81         struct of_device *of_dev = to_of_device(dev);
82         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
83         int error = 0;
84
85         if (dev->driver && drv->resume)
86                 error = drv->resume(of_dev);
87         return error;
88 }
89
90 static void of_platform_device_shutdown(struct device *dev)
91 {
92         struct of_device *of_dev = to_of_device(dev);
93         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
94
95         if (dev->driver && drv->shutdown)
96                 drv->shutdown(of_dev);
97 }
98
99 int of_bus_type_init(struct bus_type *bus, const char *name)
100 {
101         bus->name = name;
102         bus->match = of_platform_bus_match;
103         bus->probe = of_platform_device_probe;
104         bus->remove = of_platform_device_remove;
105         bus->suspend = of_platform_device_suspend;
106         bus->resume = of_platform_device_resume;
107         bus->shutdown = of_platform_device_shutdown;
108         bus->dev_attrs = of_platform_device_attrs;
109         return bus_register(bus);
110 }
111
112 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
113 {
114         /* initialize common driver fields */
115         if (!drv->driver.name)
116                 drv->driver.name = drv->name;
117         if (!drv->driver.owner)
118                 drv->driver.owner = drv->owner;
119         drv->driver.bus = bus;
120
121         /* register with core */
122         return driver_register(&drv->driver);
123 }
124 EXPORT_SYMBOL(of_register_driver);
125
126 void of_unregister_driver(struct of_platform_driver *drv)
127 {
128         driver_unregister(&drv->driver);
129 }
130 EXPORT_SYMBOL(of_unregister_driver);