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
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.
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>
20 extern struct device_attribute of_platform_device_attrs[];
22 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
24 struct of_device *of_dev = to_of_device(dev);
25 const struct of_device_id *matches = drv->of_match_table;
30 return of_match_device(matches, of_dev) != NULL;
33 static int of_platform_device_probe(struct device *dev)
36 struct of_platform_driver *drv;
37 struct of_device *of_dev;
38 const struct of_device_id *match;
40 drv = to_of_platform_driver(dev->driver);
41 of_dev = to_of_device(dev);
48 match = of_match_device(drv->driver.of_match_table, of_dev);
50 error = drv->probe(of_dev, match);
57 static int of_platform_device_remove(struct device *dev)
59 struct of_device *of_dev = to_of_device(dev);
60 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
62 if (dev->driver && drv->remove)
67 static void of_platform_device_shutdown(struct device *dev)
69 struct of_device *of_dev = to_of_device(dev);
70 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
72 if (dev->driver && drv->shutdown)
73 drv->shutdown(of_dev);
76 #ifdef CONFIG_PM_SLEEP
78 static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
80 struct of_device *of_dev = to_of_device(dev);
81 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
84 if (dev->driver && drv->suspend)
85 ret = drv->suspend(of_dev, mesg);
89 static int of_platform_legacy_resume(struct device *dev)
91 struct of_device *of_dev = to_of_device(dev);
92 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
95 if (dev->driver && drv->resume)
96 ret = drv->resume(of_dev);
100 static int of_platform_pm_prepare(struct device *dev)
102 struct device_driver *drv = dev->driver;
105 if (drv && drv->pm && drv->pm->prepare)
106 ret = drv->pm->prepare(dev);
111 static void of_platform_pm_complete(struct device *dev)
113 struct device_driver *drv = dev->driver;
115 if (drv && drv->pm && drv->pm->complete)
116 drv->pm->complete(dev);
119 #ifdef CONFIG_SUSPEND
121 static int of_platform_pm_suspend(struct device *dev)
123 struct device_driver *drv = dev->driver;
130 if (drv->pm->suspend)
131 ret = drv->pm->suspend(dev);
133 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
139 static int of_platform_pm_suspend_noirq(struct device *dev)
141 struct device_driver *drv = dev->driver;
148 if (drv->pm->suspend_noirq)
149 ret = drv->pm->suspend_noirq(dev);
155 static int of_platform_pm_resume(struct device *dev)
157 struct device_driver *drv = dev->driver;
165 ret = drv->pm->resume(dev);
167 ret = of_platform_legacy_resume(dev);
173 static int of_platform_pm_resume_noirq(struct device *dev)
175 struct device_driver *drv = dev->driver;
182 if (drv->pm->resume_noirq)
183 ret = drv->pm->resume_noirq(dev);
189 #else /* !CONFIG_SUSPEND */
191 #define of_platform_pm_suspend NULL
192 #define of_platform_pm_resume NULL
193 #define of_platform_pm_suspend_noirq NULL
194 #define of_platform_pm_resume_noirq NULL
196 #endif /* !CONFIG_SUSPEND */
198 #ifdef CONFIG_HIBERNATION
200 static int of_platform_pm_freeze(struct device *dev)
202 struct device_driver *drv = dev->driver;
210 ret = drv->pm->freeze(dev);
212 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
218 static int of_platform_pm_freeze_noirq(struct device *dev)
220 struct device_driver *drv = dev->driver;
227 if (drv->pm->freeze_noirq)
228 ret = drv->pm->freeze_noirq(dev);
234 static int of_platform_pm_thaw(struct device *dev)
236 struct device_driver *drv = dev->driver;
244 ret = drv->pm->thaw(dev);
246 ret = of_platform_legacy_resume(dev);
252 static int of_platform_pm_thaw_noirq(struct device *dev)
254 struct device_driver *drv = dev->driver;
261 if (drv->pm->thaw_noirq)
262 ret = drv->pm->thaw_noirq(dev);
268 static int of_platform_pm_poweroff(struct device *dev)
270 struct device_driver *drv = dev->driver;
277 if (drv->pm->poweroff)
278 ret = drv->pm->poweroff(dev);
280 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
286 static int of_platform_pm_poweroff_noirq(struct device *dev)
288 struct device_driver *drv = dev->driver;
295 if (drv->pm->poweroff_noirq)
296 ret = drv->pm->poweroff_noirq(dev);
302 static int of_platform_pm_restore(struct device *dev)
304 struct device_driver *drv = dev->driver;
311 if (drv->pm->restore)
312 ret = drv->pm->restore(dev);
314 ret = of_platform_legacy_resume(dev);
320 static int of_platform_pm_restore_noirq(struct device *dev)
322 struct device_driver *drv = dev->driver;
329 if (drv->pm->restore_noirq)
330 ret = drv->pm->restore_noirq(dev);
336 #else /* !CONFIG_HIBERNATION */
338 #define of_platform_pm_freeze NULL
339 #define of_platform_pm_thaw NULL
340 #define of_platform_pm_poweroff NULL
341 #define of_platform_pm_restore NULL
342 #define of_platform_pm_freeze_noirq NULL
343 #define of_platform_pm_thaw_noirq NULL
344 #define of_platform_pm_poweroff_noirq NULL
345 #define of_platform_pm_restore_noirq NULL
347 #endif /* !CONFIG_HIBERNATION */
349 static struct dev_pm_ops of_platform_dev_pm_ops = {
350 .prepare = of_platform_pm_prepare,
351 .complete = of_platform_pm_complete,
352 .suspend = of_platform_pm_suspend,
353 .resume = of_platform_pm_resume,
354 .freeze = of_platform_pm_freeze,
355 .thaw = of_platform_pm_thaw,
356 .poweroff = of_platform_pm_poweroff,
357 .restore = of_platform_pm_restore,
358 .suspend_noirq = of_platform_pm_suspend_noirq,
359 .resume_noirq = of_platform_pm_resume_noirq,
360 .freeze_noirq = of_platform_pm_freeze_noirq,
361 .thaw_noirq = of_platform_pm_thaw_noirq,
362 .poweroff_noirq = of_platform_pm_poweroff_noirq,
363 .restore_noirq = of_platform_pm_restore_noirq,
366 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
368 #else /* !CONFIG_PM_SLEEP */
370 #define OF_PLATFORM_PM_OPS_PTR NULL
372 #endif /* !CONFIG_PM_SLEEP */
374 int of_bus_type_init(struct bus_type *bus, const char *name)
377 bus->match = of_platform_bus_match;
378 bus->probe = of_platform_device_probe;
379 bus->remove = of_platform_device_remove;
380 bus->shutdown = of_platform_device_shutdown;
381 bus->dev_attrs = of_platform_device_attrs;
382 bus->pm = OF_PLATFORM_PM_OPS_PTR;
383 return bus_register(bus);
386 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
388 drv->driver.bus = bus;
390 /* register with core */
391 return driver_register(&drv->driver);
393 EXPORT_SYMBOL(of_register_driver);
395 void of_unregister_driver(struct of_platform_driver *drv)
397 driver_unregister(&drv->driver);
399 EXPORT_SYMBOL(of_unregister_driver);