blob: 265a0ee3b49eac0ae9a179e158c2f612d3eedba8 [file] [log] [blame]
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +01001/*
2 * drivers/base/power/generic_ops.c - Generic PM callbacks for subsystems
3 *
4 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5 *
6 * This file is released under the GPLv2.
7 */
8
9#include <linux/pm.h>
10#include <linux/pm_runtime.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040011#include <linux/export.h>
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010012
13#ifdef CONFIG_PM_RUNTIME
14/**
15 * pm_generic_runtime_idle - Generic runtime idle callback for subsystems.
16 * @dev: Device to handle.
17 *
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.
21 */
22int pm_generic_runtime_idle(struct device *dev)
23{
24 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
25
26 if (pm && pm->runtime_idle) {
27 int ret = pm->runtime_idle(dev);
28 if (ret)
29 return ret;
30 }
31
32 pm_runtime_suspend(dev);
33 return 0;
34}
35EXPORT_SYMBOL_GPL(pm_generic_runtime_idle);
36
37/**
38 * pm_generic_runtime_suspend - Generic runtime suspend callback for subsystems.
39 * @dev: Device to suspend.
40 *
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,
Aaro Koskinen6675bc02010-11-25 23:41:19 +010043 * return 0.
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010044 */
45int pm_generic_runtime_suspend(struct device *dev)
46{
47 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
48 int ret;
49
Ohad Ben-Cohen05aa55d2010-09-09 00:46:16 +020050 ret = pm && pm->runtime_suspend ? pm->runtime_suspend(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010051
52 return ret;
53}
54EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend);
55
56/**
57 * pm_generic_runtime_resume - Generic runtime resume callback for subsystems.
58 * @dev: Device to resume.
59 *
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,
Aaro Koskinen6675bc02010-11-25 23:41:19 +010062 * return 0.
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010063 */
64int pm_generic_runtime_resume(struct device *dev)
65{
66 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
67 int ret;
68
Ohad Ben-Cohen05aa55d2010-09-09 00:46:16 +020069 ret = pm && pm->runtime_resume ? pm->runtime_resume(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010070
71 return ret;
72}
73EXPORT_SYMBOL_GPL(pm_generic_runtime_resume);
74#endif /* CONFIG_PM_RUNTIME */
75
76#ifdef CONFIG_PM_SLEEP
77/**
Rafael J. Wysocki6538df82011-05-17 23:26:21 +020078 * pm_generic_prepare - Generic routine preparing a device for power transition.
79 * @dev: Device to prepare.
80 *
81 * Prepare a device for a system-wide power transition.
82 */
83int pm_generic_prepare(struct device *dev)
84{
85 struct device_driver *drv = dev->driver;
86 int ret = 0;
87
88 if (drv && drv->pm && drv->pm->prepare)
89 ret = drv->pm->prepare(dev);
90
91 return ret;
92}
93
94/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010095 * __pm_generic_call - Generic suspend/freeze/poweroff/thaw subsystem callback.
96 * @dev: Device to handle.
97 * @event: PM transition of the system under way.
Rafael J. Wysockie5291922011-07-01 22:12:59 +020098 * @bool: Whether or not this is the "noirq" stage.
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010099 *
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.
103 */
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200104static int __pm_generic_call(struct device *dev, int event, bool noirq)
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100105{
106 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
107 int (*callback)(struct device *);
108
109 if (!pm || pm_runtime_suspended(dev))
110 return 0;
111
112 switch (event) {
113 case PM_EVENT_SUSPEND:
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200114 callback = noirq ? pm->suspend_noirq : pm->suspend;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100115 break;
116 case PM_EVENT_FREEZE:
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200117 callback = noirq ? pm->freeze_noirq : pm->freeze;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100118 break;
119 case PM_EVENT_HIBERNATE:
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200120 callback = noirq ? pm->poweroff_noirq : pm->poweroff;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100121 break;
122 case PM_EVENT_THAW:
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200123 callback = noirq ? pm->thaw_noirq : pm->thaw;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100124 break;
125 default:
126 callback = NULL;
127 break;
128 }
129
130 return callback ? callback(dev) : 0;
131}
132
133/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200134 * pm_generic_suspend_noirq - Generic suspend_noirq callback for subsystems.
135 * @dev: Device to suspend.
136 */
137int pm_generic_suspend_noirq(struct device *dev)
138{
139 return __pm_generic_call(dev, PM_EVENT_SUSPEND, true);
140}
141EXPORT_SYMBOL_GPL(pm_generic_suspend_noirq);
142
143/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100144 * pm_generic_suspend - Generic suspend callback for subsystems.
145 * @dev: Device to suspend.
146 */
147int pm_generic_suspend(struct device *dev)
148{
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200149 return __pm_generic_call(dev, PM_EVENT_SUSPEND, false);
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100150}
151EXPORT_SYMBOL_GPL(pm_generic_suspend);
152
153/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200154 * pm_generic_freeze_noirq - Generic freeze_noirq callback for subsystems.
155 * @dev: Device to freeze.
156 */
157int pm_generic_freeze_noirq(struct device *dev)
158{
159 return __pm_generic_call(dev, PM_EVENT_FREEZE, true);
160}
161EXPORT_SYMBOL_GPL(pm_generic_freeze_noirq);
162
163/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100164 * pm_generic_freeze - Generic freeze callback for subsystems.
165 * @dev: Device to freeze.
166 */
167int pm_generic_freeze(struct device *dev)
168{
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200169 return __pm_generic_call(dev, PM_EVENT_FREEZE, false);
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100170}
171EXPORT_SYMBOL_GPL(pm_generic_freeze);
172
173/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200174 * pm_generic_poweroff_noirq - Generic poweroff_noirq callback for subsystems.
175 * @dev: Device to handle.
176 */
177int pm_generic_poweroff_noirq(struct device *dev)
178{
179 return __pm_generic_call(dev, PM_EVENT_HIBERNATE, true);
180}
181EXPORT_SYMBOL_GPL(pm_generic_poweroff_noirq);
182
183/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100184 * pm_generic_poweroff - Generic poweroff callback for subsystems.
185 * @dev: Device to handle.
186 */
187int pm_generic_poweroff(struct device *dev)
188{
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200189 return __pm_generic_call(dev, PM_EVENT_HIBERNATE, false);
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100190}
191EXPORT_SYMBOL_GPL(pm_generic_poweroff);
192
193/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200194 * pm_generic_thaw_noirq - Generic thaw_noirq callback for subsystems.
195 * @dev: Device to thaw.
196 */
197int pm_generic_thaw_noirq(struct device *dev)
198{
199 return __pm_generic_call(dev, PM_EVENT_THAW, true);
200}
201EXPORT_SYMBOL_GPL(pm_generic_thaw_noirq);
202
203/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100204 * pm_generic_thaw - Generic thaw callback for subsystems.
205 * @dev: Device to thaw.
206 */
207int pm_generic_thaw(struct device *dev)
208{
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200209 return __pm_generic_call(dev, PM_EVENT_THAW, false);
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100210}
211EXPORT_SYMBOL_GPL(pm_generic_thaw);
212
213/**
214 * __pm_generic_resume - Generic resume/restore callback for subsystems.
215 * @dev: Device to handle.
216 * @event: PM transition of the system under way.
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200217 * @bool: Whether or not this is the "noirq" stage.
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100218 *
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.
222 */
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200223static int __pm_generic_resume(struct device *dev, int event, bool noirq)
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100224{
225 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
226 int (*callback)(struct device *);
227 int ret;
228
229 if (!pm)
230 return 0;
231
232 switch (event) {
233 case PM_EVENT_RESUME:
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200234 callback = noirq ? pm->resume_noirq : pm->resume;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100235 break;
236 case PM_EVENT_RESTORE:
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200237 callback = noirq ? pm->restore_noirq : pm->restore;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100238 break;
239 default:
240 callback = NULL;
241 break;
242 }
243
244 if (!callback)
245 return 0;
246
247 ret = callback(dev);
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200248 if (!ret && !noirq && pm_runtime_enabled(dev)) {
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100249 pm_runtime_disable(dev);
250 pm_runtime_set_active(dev);
251 pm_runtime_enable(dev);
252 }
253
254 return ret;
255}
256
257/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200258 * pm_generic_resume_noirq - Generic resume_noirq callback for subsystems.
259 * @dev: Device to resume.
260 */
261int pm_generic_resume_noirq(struct device *dev)
262{
263 return __pm_generic_resume(dev, PM_EVENT_RESUME, true);
264}
265EXPORT_SYMBOL_GPL(pm_generic_resume_noirq);
266
267/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100268 * pm_generic_resume - Generic resume callback for subsystems.
269 * @dev: Device to resume.
270 */
271int pm_generic_resume(struct device *dev)
272{
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200273 return __pm_generic_resume(dev, PM_EVENT_RESUME, false);
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100274}
275EXPORT_SYMBOL_GPL(pm_generic_resume);
276
277/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200278 * pm_generic_restore_noirq - Generic restore_noirq callback for subsystems.
279 * @dev: Device to restore.
280 */
281int pm_generic_restore_noirq(struct device *dev)
282{
283 return __pm_generic_resume(dev, PM_EVENT_RESTORE, true);
284}
285EXPORT_SYMBOL_GPL(pm_generic_restore_noirq);
286
287/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100288 * pm_generic_restore - Generic restore callback for subsystems.
289 * @dev: Device to restore.
290 */
291int pm_generic_restore(struct device *dev)
292{
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200293 return __pm_generic_resume(dev, PM_EVENT_RESTORE, false);
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100294}
295EXPORT_SYMBOL_GPL(pm_generic_restore);
Rafael J. Wysocki6538df82011-05-17 23:26:21 +0200296
297/**
298 * pm_generic_complete - Generic routine competing a device power transition.
299 * @dev: Device to handle.
300 *
301 * Complete a device power transition during a system-wide power transition.
302 */
303void pm_generic_complete(struct device *dev)
304{
305 struct device_driver *drv = dev->driver;
306
307 if (drv && drv->pm && drv->pm->complete)
308 drv->pm->complete(dev);
309
310 /*
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.
313 */
314 pm_runtime_idle(dev);
315}
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100316#endif /* CONFIG_PM_SLEEP */
317
318struct dev_pm_ops generic_subsys_pm_ops = {
319#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki6538df82011-05-17 23:26:21 +0200320 .prepare = pm_generic_prepare,
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100321 .suspend = pm_generic_suspend,
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200322 .suspend_noirq = pm_generic_suspend_noirq,
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100323 .resume = pm_generic_resume,
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200324 .resume_noirq = pm_generic_resume_noirq,
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100325 .freeze = pm_generic_freeze,
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200326 .freeze_noirq = pm_generic_freeze_noirq,
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100327 .thaw = pm_generic_thaw,
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200328 .thaw_noirq = pm_generic_thaw_noirq,
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100329 .poweroff = pm_generic_poweroff,
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200330 .poweroff_noirq = pm_generic_poweroff_noirq,
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100331 .restore = pm_generic_restore,
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200332 .restore_noirq = pm_generic_restore_noirq,
Rafael J. Wysocki6538df82011-05-17 23:26:21 +0200333 .complete = pm_generic_complete,
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100334#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
340};
341EXPORT_SYMBOL_GPL(generic_subsys_pm_ops);