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>
12 #ifdef CONFIG_PM_RUNTIME
14 * pm_generic_runtime_idle - Generic runtime idle callback for subsystems.
15 * @dev: Device to handle.
17 * If PM operations are defined for the @dev's driver and they include
18 * ->runtime_idle(), execute it and return its error code, if nonzero.
19 * Otherwise, execute pm_runtime_suspend() for the device and return 0.
21 int pm_generic_runtime_idle(struct device
*dev
)
23 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
25 if (pm
&& pm
->runtime_idle
) {
26 int ret
= pm
->runtime_idle(dev
);
31 pm_runtime_suspend(dev
);
34 EXPORT_SYMBOL_GPL(pm_generic_runtime_idle
);
37 * pm_generic_runtime_suspend - Generic runtime suspend callback for subsystems.
38 * @dev: Device to suspend.
40 * If PM operations are defined for the @dev's driver and they include
41 * ->runtime_suspend(), execute it and return its error code. Otherwise,
44 int pm_generic_runtime_suspend(struct device
*dev
)
46 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
49 ret
= pm
&& pm
->runtime_suspend
? pm
->runtime_suspend(dev
) : 0;
53 EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend
);
56 * pm_generic_runtime_resume - Generic runtime resume callback for subsystems.
57 * @dev: Device to resume.
59 * If PM operations are defined for the @dev's driver and they include
60 * ->runtime_resume(), execute it and return its error code. Otherwise,
63 int pm_generic_runtime_resume(struct device
*dev
)
65 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
68 ret
= pm
&& pm
->runtime_resume
? pm
->runtime_resume(dev
) : 0;
72 EXPORT_SYMBOL_GPL(pm_generic_runtime_resume
);
73 #endif /* CONFIG_PM_RUNTIME */
75 #ifdef CONFIG_PM_SLEEP
77 * __pm_generic_call - Generic suspend/freeze/poweroff/thaw subsystem callback.
78 * @dev: Device to handle.
79 * @event: PM transition of the system under way.
81 * If the device has not been suspended at run time, execute the
82 * suspend/freeze/poweroff/thaw callback provided by its driver, if defined, and
83 * return its error code. Otherwise, return zero.
85 static int __pm_generic_call(struct device
*dev
, int event
)
87 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
88 int (*callback
)(struct device
*);
90 if (!pm
|| pm_runtime_suspended(dev
))
94 case PM_EVENT_SUSPEND
:
95 callback
= pm
->suspend
;
98 callback
= pm
->freeze
;
100 case PM_EVENT_HIBERNATE
:
101 callback
= pm
->poweroff
;
111 return callback
? callback(dev
) : 0;
115 * pm_generic_suspend - Generic suspend callback for subsystems.
116 * @dev: Device to suspend.
118 int pm_generic_suspend(struct device
*dev
)
120 return __pm_generic_call(dev
, PM_EVENT_SUSPEND
);
122 EXPORT_SYMBOL_GPL(pm_generic_suspend
);
125 * pm_generic_freeze - Generic freeze callback for subsystems.
126 * @dev: Device to freeze.
128 int pm_generic_freeze(struct device
*dev
)
130 return __pm_generic_call(dev
, PM_EVENT_FREEZE
);
132 EXPORT_SYMBOL_GPL(pm_generic_freeze
);
135 * pm_generic_poweroff - Generic poweroff callback for subsystems.
136 * @dev: Device to handle.
138 int pm_generic_poweroff(struct device
*dev
)
140 return __pm_generic_call(dev
, PM_EVENT_HIBERNATE
);
142 EXPORT_SYMBOL_GPL(pm_generic_poweroff
);
145 * pm_generic_thaw - Generic thaw callback for subsystems.
146 * @dev: Device to thaw.
148 int pm_generic_thaw(struct device
*dev
)
150 return __pm_generic_call(dev
, PM_EVENT_THAW
);
152 EXPORT_SYMBOL_GPL(pm_generic_thaw
);
155 * __pm_generic_resume - Generic resume/restore callback for subsystems.
156 * @dev: Device to handle.
157 * @event: PM transition of the system under way.
159 * Execute the resume/resotre callback provided by the @dev's driver, if
160 * defined. If it returns 0, change the device's runtime PM status to 'active'.
161 * Return the callback's error code.
163 static int __pm_generic_resume(struct device
*dev
, int event
)
165 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
166 int (*callback
)(struct device
*);
173 case PM_EVENT_RESUME
:
174 callback
= pm
->resume
;
176 case PM_EVENT_RESTORE
:
177 callback
= pm
->restore
;
188 if (!ret
&& pm_runtime_enabled(dev
)) {
189 pm_runtime_disable(dev
);
190 pm_runtime_set_active(dev
);
191 pm_runtime_enable(dev
);
198 * pm_generic_resume - Generic resume callback for subsystems.
199 * @dev: Device to resume.
201 int pm_generic_resume(struct device
*dev
)
203 return __pm_generic_resume(dev
, PM_EVENT_RESUME
);
205 EXPORT_SYMBOL_GPL(pm_generic_resume
);
208 * pm_generic_restore - Generic restore callback for subsystems.
209 * @dev: Device to restore.
211 int pm_generic_restore(struct device
*dev
)
213 return __pm_generic_resume(dev
, PM_EVENT_RESTORE
);
215 EXPORT_SYMBOL_GPL(pm_generic_restore
);
216 #endif /* CONFIG_PM_SLEEP */
218 struct dev_pm_ops generic_subsys_pm_ops
= {
219 #ifdef CONFIG_PM_SLEEP
220 .suspend
= pm_generic_suspend
,
221 .resume
= pm_generic_resume
,
222 .freeze
= pm_generic_freeze
,
223 .thaw
= pm_generic_thaw
,
224 .poweroff
= pm_generic_poweroff
,
225 .restore
= pm_generic_restore
,
227 #ifdef CONFIG_PM_RUNTIME
228 .runtime_suspend
= pm_generic_runtime_suspend
,
229 .runtime_resume
= pm_generic_runtime_resume
,
230 .runtime_idle
= pm_generic_runtime_idle
,
233 EXPORT_SYMBOL_GPL(generic_subsys_pm_ops
);