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 * 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
))
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
;
123 callback
= noirq
? pm
->thaw_noirq
: pm
->thaw
;
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
*);
233 case PM_EVENT_RESUME
:
234 callback
= noirq
? pm
->resume_noirq
: pm
->resume
;
236 case PM_EVENT_RESTORE
:
237 callback
= noirq
? pm
->restore_noirq
: pm
->restore
;
248 if (!ret
&& !noirq
&& pm_runtime_enabled(dev
)) {
249 pm_runtime_disable(dev
);
250 pm_runtime_set_active(dev
);
251 pm_runtime_enable(dev
);
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
,
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
,
341 EXPORT_SYMBOL_GPL(generic_subsys_pm_ops
);