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_prepare - Generic routine preparing a device for power transition.
78 * @dev: Device to prepare.
80 * Prepare a device for a system-wide power transition.
82 int pm_generic_prepare(struct device
*dev
)
84 struct device_driver
*drv
= dev
->driver
;
87 if (drv
&& drv
->pm
&& drv
->pm
->prepare
)
88 ret
= drv
->pm
->prepare(dev
);
94 * __pm_generic_call - Generic suspend/freeze/poweroff/thaw subsystem callback.
95 * @dev: Device to handle.
96 * @event: PM transition of the system under way.
98 * If the device has not been suspended at run time, execute the
99 * suspend/freeze/poweroff/thaw callback provided by its driver, if defined, and
100 * return its error code. Otherwise, return zero.
102 static int __pm_generic_call(struct device
*dev
, int event
)
104 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
105 int (*callback
)(struct device
*);
107 if (!pm
|| pm_runtime_suspended(dev
))
111 case PM_EVENT_SUSPEND
:
112 callback
= pm
->suspend
;
114 case PM_EVENT_FREEZE
:
115 callback
= pm
->freeze
;
117 case PM_EVENT_HIBERNATE
:
118 callback
= pm
->poweroff
;
128 return callback
? callback(dev
) : 0;
132 * pm_generic_suspend - Generic suspend callback for subsystems.
133 * @dev: Device to suspend.
135 int pm_generic_suspend(struct device
*dev
)
137 return __pm_generic_call(dev
, PM_EVENT_SUSPEND
);
139 EXPORT_SYMBOL_GPL(pm_generic_suspend
);
142 * pm_generic_freeze - Generic freeze callback for subsystems.
143 * @dev: Device to freeze.
145 int pm_generic_freeze(struct device
*dev
)
147 return __pm_generic_call(dev
, PM_EVENT_FREEZE
);
149 EXPORT_SYMBOL_GPL(pm_generic_freeze
);
152 * pm_generic_poweroff - Generic poweroff callback for subsystems.
153 * @dev: Device to handle.
155 int pm_generic_poweroff(struct device
*dev
)
157 return __pm_generic_call(dev
, PM_EVENT_HIBERNATE
);
159 EXPORT_SYMBOL_GPL(pm_generic_poweroff
);
162 * pm_generic_thaw - Generic thaw callback for subsystems.
163 * @dev: Device to thaw.
165 int pm_generic_thaw(struct device
*dev
)
167 return __pm_generic_call(dev
, PM_EVENT_THAW
);
169 EXPORT_SYMBOL_GPL(pm_generic_thaw
);
172 * __pm_generic_resume - Generic resume/restore callback for subsystems.
173 * @dev: Device to handle.
174 * @event: PM transition of the system under way.
176 * Execute the resume/resotre callback provided by the @dev's driver, if
177 * defined. If it returns 0, change the device's runtime PM status to 'active'.
178 * Return the callback's error code.
180 static int __pm_generic_resume(struct device
*dev
, int event
)
182 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
183 int (*callback
)(struct device
*);
190 case PM_EVENT_RESUME
:
191 callback
= pm
->resume
;
193 case PM_EVENT_RESTORE
:
194 callback
= pm
->restore
;
205 if (!ret
&& pm_runtime_enabled(dev
)) {
206 pm_runtime_disable(dev
);
207 pm_runtime_set_active(dev
);
208 pm_runtime_enable(dev
);
215 * pm_generic_resume - Generic resume callback for subsystems.
216 * @dev: Device to resume.
218 int pm_generic_resume(struct device
*dev
)
220 return __pm_generic_resume(dev
, PM_EVENT_RESUME
);
222 EXPORT_SYMBOL_GPL(pm_generic_resume
);
225 * pm_generic_restore - Generic restore callback for subsystems.
226 * @dev: Device to restore.
228 int pm_generic_restore(struct device
*dev
)
230 return __pm_generic_resume(dev
, PM_EVENT_RESTORE
);
232 EXPORT_SYMBOL_GPL(pm_generic_restore
);
235 * pm_generic_complete - Generic routine competing a device power transition.
236 * @dev: Device to handle.
238 * Complete a device power transition during a system-wide power transition.
240 void pm_generic_complete(struct device
*dev
)
242 struct device_driver
*drv
= dev
->driver
;
244 if (drv
&& drv
->pm
&& drv
->pm
->complete
)
245 drv
->pm
->complete(dev
);
248 * Let runtime PM try to suspend devices that haven't been in use before
249 * going into the system-wide sleep state we're resuming from.
251 pm_runtime_idle(dev
);
253 #endif /* CONFIG_PM_SLEEP */
255 struct dev_pm_ops generic_subsys_pm_ops
= {
256 #ifdef CONFIG_PM_SLEEP
257 .prepare
= pm_generic_prepare
,
258 .suspend
= pm_generic_suspend
,
259 .resume
= pm_generic_resume
,
260 .freeze
= pm_generic_freeze
,
261 .thaw
= pm_generic_thaw
,
262 .poweroff
= pm_generic_poweroff
,
263 .restore
= pm_generic_restore
,
264 .complete
= pm_generic_complete
,
266 #ifdef CONFIG_PM_RUNTIME
267 .runtime_suspend
= pm_generic_runtime_suspend
,
268 .runtime_resume
= pm_generic_runtime_resume
,
269 .runtime_idle
= pm_generic_runtime_idle
,
272 EXPORT_SYMBOL_GPL(generic_subsys_pm_ops
);