Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / base / power / generic_ops.c
blob265a0ee3b49eac0ae9a179e158c2f612d3eedba8
1 /*
2 * drivers/base/power/generic_ops.c - Generic PM callbacks for subsystems
4 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
6 * This file is released under the GPLv2.
7 */
9 #include <linux/pm.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/export.h>
13 #ifdef CONFIG_PM_RUNTIME
14 /**
15 * pm_generic_runtime_idle - Generic runtime idle callback for subsystems.
16 * @dev: Device to handle.
18 * If PM operations are defined for the @dev's driver and they include
19 * ->runtime_idle(), execute it and return its error code, if nonzero.
20 * Otherwise, execute pm_runtime_suspend() for the device and return 0.
22 int pm_generic_runtime_idle(struct device *dev)
24 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
26 if (pm && pm->runtime_idle) {
27 int ret = pm->runtime_idle(dev);
28 if (ret)
29 return ret;
32 pm_runtime_suspend(dev);
33 return 0;
35 EXPORT_SYMBOL_GPL(pm_generic_runtime_idle);
37 /**
38 * pm_generic_runtime_suspend - Generic runtime suspend callback for subsystems.
39 * @dev: Device to suspend.
41 * If PM operations are defined for the @dev's driver and they include
42 * ->runtime_suspend(), execute it and return its error code. Otherwise,
43 * return 0.
45 int pm_generic_runtime_suspend(struct device *dev)
47 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
48 int ret;
50 ret = pm && pm->runtime_suspend ? pm->runtime_suspend(dev) : 0;
52 return ret;
54 EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend);
56 /**
57 * pm_generic_runtime_resume - Generic runtime resume callback for subsystems.
58 * @dev: Device to resume.
60 * If PM operations are defined for the @dev's driver and they include
61 * ->runtime_resume(), execute it and return its error code. Otherwise,
62 * return 0.
64 int pm_generic_runtime_resume(struct device *dev)
66 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
67 int ret;
69 ret = pm && pm->runtime_resume ? pm->runtime_resume(dev) : 0;
71 return ret;
73 EXPORT_SYMBOL_GPL(pm_generic_runtime_resume);
74 #endif /* CONFIG_PM_RUNTIME */
76 #ifdef CONFIG_PM_SLEEP
77 /**
78 * pm_generic_prepare - Generic routine preparing a device for power transition.
79 * @dev: Device to prepare.
81 * Prepare a device for a system-wide power transition.
83 int pm_generic_prepare(struct device *dev)
85 struct device_driver *drv = dev->driver;
86 int ret = 0;
88 if (drv && drv->pm && drv->pm->prepare)
89 ret = drv->pm->prepare(dev);
91 return ret;
94 /**
95 * __pm_generic_call - Generic suspend/freeze/poweroff/thaw subsystem callback.
96 * @dev: Device to handle.
97 * @event: PM transition of the system under way.
98 * @bool: Whether or not this is the "noirq" stage.
100 * If the device has not been suspended at run time, execute the
101 * suspend/freeze/poweroff/thaw callback provided by its driver, if defined, and
102 * return its error code. Otherwise, return zero.
104 static int __pm_generic_call(struct device *dev, int event, bool noirq)
106 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
107 int (*callback)(struct device *);
109 if (!pm || pm_runtime_suspended(dev))
110 return 0;
112 switch (event) {
113 case PM_EVENT_SUSPEND:
114 callback = noirq ? pm->suspend_noirq : pm->suspend;
115 break;
116 case PM_EVENT_FREEZE:
117 callback = noirq ? pm->freeze_noirq : pm->freeze;
118 break;
119 case PM_EVENT_HIBERNATE:
120 callback = noirq ? pm->poweroff_noirq : pm->poweroff;
121 break;
122 case PM_EVENT_THAW:
123 callback = noirq ? pm->thaw_noirq : pm->thaw;
124 break;
125 default:
126 callback = NULL;
127 break;
130 return callback ? callback(dev) : 0;
134 * pm_generic_suspend_noirq - Generic suspend_noirq callback for subsystems.
135 * @dev: Device to suspend.
137 int pm_generic_suspend_noirq(struct device *dev)
139 return __pm_generic_call(dev, PM_EVENT_SUSPEND, true);
141 EXPORT_SYMBOL_GPL(pm_generic_suspend_noirq);
144 * pm_generic_suspend - Generic suspend callback for subsystems.
145 * @dev: Device to suspend.
147 int pm_generic_suspend(struct device *dev)
149 return __pm_generic_call(dev, PM_EVENT_SUSPEND, false);
151 EXPORT_SYMBOL_GPL(pm_generic_suspend);
154 * pm_generic_freeze_noirq - Generic freeze_noirq callback for subsystems.
155 * @dev: Device to freeze.
157 int pm_generic_freeze_noirq(struct device *dev)
159 return __pm_generic_call(dev, PM_EVENT_FREEZE, true);
161 EXPORT_SYMBOL_GPL(pm_generic_freeze_noirq);
164 * pm_generic_freeze - Generic freeze callback for subsystems.
165 * @dev: Device to freeze.
167 int pm_generic_freeze(struct device *dev)
169 return __pm_generic_call(dev, PM_EVENT_FREEZE, false);
171 EXPORT_SYMBOL_GPL(pm_generic_freeze);
174 * pm_generic_poweroff_noirq - Generic poweroff_noirq callback for subsystems.
175 * @dev: Device to handle.
177 int pm_generic_poweroff_noirq(struct device *dev)
179 return __pm_generic_call(dev, PM_EVENT_HIBERNATE, true);
181 EXPORT_SYMBOL_GPL(pm_generic_poweroff_noirq);
184 * pm_generic_poweroff - Generic poweroff callback for subsystems.
185 * @dev: Device to handle.
187 int pm_generic_poweroff(struct device *dev)
189 return __pm_generic_call(dev, PM_EVENT_HIBERNATE, false);
191 EXPORT_SYMBOL_GPL(pm_generic_poweroff);
194 * pm_generic_thaw_noirq - Generic thaw_noirq callback for subsystems.
195 * @dev: Device to thaw.
197 int pm_generic_thaw_noirq(struct device *dev)
199 return __pm_generic_call(dev, PM_EVENT_THAW, true);
201 EXPORT_SYMBOL_GPL(pm_generic_thaw_noirq);
204 * pm_generic_thaw - Generic thaw callback for subsystems.
205 * @dev: Device to thaw.
207 int pm_generic_thaw(struct device *dev)
209 return __pm_generic_call(dev, PM_EVENT_THAW, false);
211 EXPORT_SYMBOL_GPL(pm_generic_thaw);
214 * __pm_generic_resume - Generic resume/restore callback for subsystems.
215 * @dev: Device to handle.
216 * @event: PM transition of the system under way.
217 * @bool: Whether or not this is the "noirq" stage.
219 * Execute the resume/resotre callback provided by the @dev's driver, if
220 * defined. If it returns 0, change the device's runtime PM status to 'active'.
221 * Return the callback's error code.
223 static int __pm_generic_resume(struct device *dev, int event, bool noirq)
225 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
226 int (*callback)(struct device *);
227 int ret;
229 if (!pm)
230 return 0;
232 switch (event) {
233 case PM_EVENT_RESUME:
234 callback = noirq ? pm->resume_noirq : pm->resume;
235 break;
236 case PM_EVENT_RESTORE:
237 callback = noirq ? pm->restore_noirq : pm->restore;
238 break;
239 default:
240 callback = NULL;
241 break;
244 if (!callback)
245 return 0;
247 ret = callback(dev);
248 if (!ret && !noirq && pm_runtime_enabled(dev)) {
249 pm_runtime_disable(dev);
250 pm_runtime_set_active(dev);
251 pm_runtime_enable(dev);
254 return ret;
258 * pm_generic_resume_noirq - Generic resume_noirq callback for subsystems.
259 * @dev: Device to resume.
261 int pm_generic_resume_noirq(struct device *dev)
263 return __pm_generic_resume(dev, PM_EVENT_RESUME, true);
265 EXPORT_SYMBOL_GPL(pm_generic_resume_noirq);
268 * pm_generic_resume - Generic resume callback for subsystems.
269 * @dev: Device to resume.
271 int pm_generic_resume(struct device *dev)
273 return __pm_generic_resume(dev, PM_EVENT_RESUME, false);
275 EXPORT_SYMBOL_GPL(pm_generic_resume);
278 * pm_generic_restore_noirq - Generic restore_noirq callback for subsystems.
279 * @dev: Device to restore.
281 int pm_generic_restore_noirq(struct device *dev)
283 return __pm_generic_resume(dev, PM_EVENT_RESTORE, true);
285 EXPORT_SYMBOL_GPL(pm_generic_restore_noirq);
288 * pm_generic_restore - Generic restore callback for subsystems.
289 * @dev: Device to restore.
291 int pm_generic_restore(struct device *dev)
293 return __pm_generic_resume(dev, PM_EVENT_RESTORE, false);
295 EXPORT_SYMBOL_GPL(pm_generic_restore);
298 * pm_generic_complete - Generic routine competing a device power transition.
299 * @dev: Device to handle.
301 * Complete a device power transition during a system-wide power transition.
303 void pm_generic_complete(struct device *dev)
305 struct device_driver *drv = dev->driver;
307 if (drv && drv->pm && drv->pm->complete)
308 drv->pm->complete(dev);
311 * Let runtime PM try to suspend devices that haven't been in use before
312 * going into the system-wide sleep state we're resuming from.
314 pm_runtime_idle(dev);
316 #endif /* CONFIG_PM_SLEEP */
318 struct dev_pm_ops generic_subsys_pm_ops = {
319 #ifdef CONFIG_PM_SLEEP
320 .prepare = pm_generic_prepare,
321 .suspend = pm_generic_suspend,
322 .suspend_noirq = pm_generic_suspend_noirq,
323 .resume = pm_generic_resume,
324 .resume_noirq = pm_generic_resume_noirq,
325 .freeze = pm_generic_freeze,
326 .freeze_noirq = pm_generic_freeze_noirq,
327 .thaw = pm_generic_thaw,
328 .thaw_noirq = pm_generic_thaw_noirq,
329 .poweroff = pm_generic_poweroff,
330 .poweroff_noirq = pm_generic_poweroff_noirq,
331 .restore = pm_generic_restore,
332 .restore_noirq = pm_generic_restore_noirq,
333 .complete = pm_generic_complete,
334 #endif
335 #ifdef CONFIG_PM_RUNTIME
336 .runtime_suspend = pm_generic_runtime_suspend,
337 .runtime_resume = pm_generic_runtime_resume,
338 .runtime_idle = pm_generic_runtime_idle,
339 #endif
341 EXPORT_SYMBOL_GPL(generic_subsys_pm_ops);