blob: ca09a63a64db7bdf66a03f0da0069518615e3d6f [file] [log] [blame]
Stephen Rothwell3f23de12007-05-03 02:38:57 +10001/*
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>
Stephen Rothwell5c457082007-10-17 21:17:42 -070015#include <linux/module.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100016#include <linux/device.h>
17#include <linux/of_device.h>
18#include <linux/of_platform.h>
19
20static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
21{
22 struct of_device *of_dev = to_of_device(dev);
23 struct of_platform_driver *of_drv = to_of_platform_driver(drv);
24 const struct of_device_id *matches = of_drv->match_table;
25
26 if (!matches)
27 return 0;
28
29 return of_match_device(matches, of_dev) != NULL;
30}
31
32static int of_platform_device_probe(struct device *dev)
33{
34 int error = -ENODEV;
35 struct of_platform_driver *drv;
36 struct of_device *of_dev;
37 const struct of_device_id *match;
38
39 drv = to_of_platform_driver(dev->driver);
40 of_dev = to_of_device(dev);
41
42 if (!drv->probe)
43 return error;
44
45 of_dev_get(of_dev);
46
47 match = of_match_device(drv->match_table, of_dev);
48 if (match)
49 error = drv->probe(of_dev, match);
50 if (error)
51 of_dev_put(of_dev);
52
53 return error;
54}
55
56static int of_platform_device_remove(struct device *dev)
57{
58 struct of_device *of_dev = to_of_device(dev);
59 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
60
61 if (dev->driver && drv->remove)
62 drv->remove(of_dev);
63 return 0;
64}
65
66static int of_platform_device_suspend(struct device *dev, pm_message_t state)
67{
68 struct of_device *of_dev = to_of_device(dev);
69 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
70 int error = 0;
71
72 if (dev->driver && drv->suspend)
73 error = drv->suspend(of_dev, state);
74 return error;
75}
76
77static int of_platform_device_resume(struct device * dev)
78{
79 struct of_device *of_dev = to_of_device(dev);
80 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
81 int error = 0;
82
83 if (dev->driver && drv->resume)
84 error = drv->resume(of_dev);
85 return error;
86}
87
Michael Ellermana09ad3c2008-01-25 16:59:13 +110088static void of_platform_device_shutdown(struct device *dev)
89{
90 struct of_device *of_dev = to_of_device(dev);
91 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
92
93 if (dev->driver && drv->shutdown)
94 drv->shutdown(of_dev);
95}
96
Stephen Rothwell3f23de12007-05-03 02:38:57 +100097int of_bus_type_init(struct bus_type *bus, const char *name)
98{
99 bus->name = name;
100 bus->match = of_platform_bus_match;
101 bus->probe = of_platform_device_probe;
102 bus->remove = of_platform_device_remove;
103 bus->suspend = of_platform_device_suspend;
104 bus->resume = of_platform_device_resume;
Michael Ellermana09ad3c2008-01-25 16:59:13 +1100105 bus->shutdown = of_platform_device_shutdown;
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000106 return bus_register(bus);
107}
Stephen Rothwell5c457082007-10-17 21:17:42 -0700108
109int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
110{
111 /* initialize common driver fields */
112 if (!drv->driver.name)
113 drv->driver.name = drv->name;
114 if (!drv->driver.owner)
115 drv->driver.owner = drv->owner;
116 drv->driver.bus = bus;
117
118 /* register with core */
119 return driver_register(&drv->driver);
120}
121EXPORT_SYMBOL(of_register_driver);
122
123void of_unregister_driver(struct of_platform_driver *drv)
124{
125 driver_unregister(&drv->driver);
126}
127EXPORT_SYMBOL(of_unregister_driver);