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.
10 #include <linux/pm_runtime.h>
11 #include <linux/export.h>
13 #ifdef CONFIG_PM_RUNTIME
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
);
32 pm_runtime_suspend(dev
);
35 EXPORT_SYMBOL_GPL(pm_generic_runtime_idle
);
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,
45 int pm_generic_runtime_suspend(struct device
*dev
)
47 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
50 ret
= pm
&& pm
->runtime_suspend
? pm
->runtime_suspend(dev
) : 0;
54 EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend
);
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,
64 int pm_generic_runtime_resume(struct device
*dev
)
66 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
69 ret
= pm
&& pm
->runtime_resume
? pm
->runtime_resume(dev
) : 0;
73 EXPORT_SYMBOL_GPL(pm_generic_runtime_resume
);
74 #endif /* CONFIG_PM_RUNTIME */
76 #ifdef CONFIG_PM_SLEEP
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
;
88 if (drv
&& drv
->pm
&& drv
->pm
->prepare
)
89 ret
= drv
->pm
->prepare(dev
);
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 * Execute the PM callback corresponding to @event provided by the driver of
101 * @dev, if defined, and return its error code. Return 0 if the callback is
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
*);
113 case PM_EVENT_SUSPEND
:
114 callback
= noirq
? pm
->suspend_noirq
: pm
->suspend
;
116 case PM_EVENT_FREEZE
:
117 callback
= noirq
? pm
->freeze_noirq
: pm
->freeze
;
119 case PM_EVENT_HIBERNATE
:
120 callback
= noirq
? pm
->poweroff_noirq
: pm
->poweroff
;
122 case PM_EVENT_RESUME
:
123 callback
= noirq
? pm
->resume_noirq
: pm
->resume
;
126 callback
= noirq
? pm
->thaw_noirq
: pm
->thaw
;
128 case PM_EVENT_RESTORE
:
129 callback
= noirq
? pm
->restore_noirq
: pm
->restore
;
136 return callback
? callback(dev
) : 0;
140 * pm_generic_suspend_noirq - Generic suspend_noirq callback for subsystems.
141 * @dev: Device to suspend.
143 int pm_generic_suspend_noirq(struct device
*dev
)
145 return __pm_generic_call(dev
, PM_EVENT_SUSPEND
, true);
147 EXPORT_SYMBOL_GPL(pm_generic_suspend_noirq
);
150 * pm_generic_suspend - Generic suspend callback for subsystems.
151 * @dev: Device to suspend.
153 int pm_generic_suspend(struct device
*dev
)
155 return __pm_generic_call(dev
, PM_EVENT_SUSPEND
, false);
157 EXPORT_SYMBOL_GPL(pm_generic_suspend
);
160 * pm_generic_freeze_noirq - Generic freeze_noirq callback for subsystems.
161 * @dev: Device to freeze.
163 int pm_generic_freeze_noirq(struct device
*dev
)
165 return __pm_generic_call(dev
, PM_EVENT_FREEZE
, true);
167 EXPORT_SYMBOL_GPL(pm_generic_freeze_noirq
);
170 * pm_generic_freeze - Generic freeze callback for subsystems.
171 * @dev: Device to freeze.
173 int pm_generic_freeze(struct device
*dev
)
175 return __pm_generic_call(dev
, PM_EVENT_FREEZE
, false);
177 EXPORT_SYMBOL_GPL(pm_generic_freeze
);
180 * pm_generic_poweroff_noirq - Generic poweroff_noirq callback for subsystems.
181 * @dev: Device to handle.
183 int pm_generic_poweroff_noirq(struct device
*dev
)
185 return __pm_generic_call(dev
, PM_EVENT_HIBERNATE
, true);
187 EXPORT_SYMBOL_GPL(pm_generic_poweroff_noirq
);
190 * pm_generic_poweroff - Generic poweroff callback for subsystems.
191 * @dev: Device to handle.
193 int pm_generic_poweroff(struct device
*dev
)
195 return __pm_generic_call(dev
, PM_EVENT_HIBERNATE
, false);
197 EXPORT_SYMBOL_GPL(pm_generic_poweroff
);
200 * pm_generic_thaw_noirq - Generic thaw_noirq callback for subsystems.
201 * @dev: Device to thaw.
203 int pm_generic_thaw_noirq(struct device
*dev
)
205 return __pm_generic_call(dev
, PM_EVENT_THAW
, true);
207 EXPORT_SYMBOL_GPL(pm_generic_thaw_noirq
);
210 * pm_generic_thaw - Generic thaw callback for subsystems.
211 * @dev: Device to thaw.
213 int pm_generic_thaw(struct device
*dev
)
215 return __pm_generic_call(dev
, PM_EVENT_THAW
, false);
217 EXPORT_SYMBOL_GPL(pm_generic_thaw
);
220 * pm_generic_resume_noirq - Generic resume_noirq callback for subsystems.
221 * @dev: Device to resume.
223 int pm_generic_resume_noirq(struct device
*dev
)
225 return __pm_generic_call(dev
, PM_EVENT_RESUME
, true);
227 EXPORT_SYMBOL_GPL(pm_generic_resume_noirq
);
230 * pm_generic_resume - Generic resume callback for subsystems.
231 * @dev: Device to resume.
233 int pm_generic_resume(struct device
*dev
)
235 return __pm_generic_call(dev
, PM_EVENT_RESUME
, false);
237 EXPORT_SYMBOL_GPL(pm_generic_resume
);
240 * pm_generic_restore_noirq - Generic restore_noirq callback for subsystems.
241 * @dev: Device to restore.
243 int pm_generic_restore_noirq(struct device
*dev
)
245 return __pm_generic_call(dev
, PM_EVENT_RESTORE
, true);
247 EXPORT_SYMBOL_GPL(pm_generic_restore_noirq
);
250 * pm_generic_restore - Generic restore callback for subsystems.
251 * @dev: Device to restore.
253 int pm_generic_restore(struct device
*dev
)
255 return __pm_generic_call(dev
, PM_EVENT_RESTORE
, false);
257 EXPORT_SYMBOL_GPL(pm_generic_restore
);
260 * pm_generic_complete - Generic routine competing a device power transition.
261 * @dev: Device to handle.
263 * Complete a device power transition during a system-wide power transition.
265 void pm_generic_complete(struct device
*dev
)
267 struct device_driver
*drv
= dev
->driver
;
269 if (drv
&& drv
->pm
&& drv
->pm
->complete
)
270 drv
->pm
->complete(dev
);
273 * Let runtime PM try to suspend devices that haven't been in use before
274 * going into the system-wide sleep state we're resuming from.
276 pm_runtime_idle(dev
);
278 #endif /* CONFIG_PM_SLEEP */