blob: 07c3c4a9522d4e6bec1f0f540bd38f8c1ce6ed00 [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. Wysocki58a1fbb2015-10-07 00:50:24 +020012#include <linux/suspend.h>
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010013
Ulf Hansson717e5d42013-12-10 14:37:41 +010014#ifdef CONFIG_PM
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010015/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010016 * pm_generic_runtime_suspend - Generic runtime suspend callback for subsystems.
17 * @dev: Device to suspend.
18 *
19 * If PM operations are defined for the @dev's driver and they include
20 * ->runtime_suspend(), execute it and return its error code. Otherwise,
Aaro Koskinen6675bc02010-11-25 23:41:19 +010021 * return 0.
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010022 */
23int pm_generic_runtime_suspend(struct device *dev)
24{
25 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
26 int ret;
27
Ohad Ben-Cohen05aa55d2010-09-09 00:46:16 +020028 ret = pm && pm->runtime_suspend ? pm->runtime_suspend(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010029
30 return ret;
31}
32EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend);
33
34/**
35 * pm_generic_runtime_resume - Generic runtime resume callback for subsystems.
36 * @dev: Device to resume.
37 *
38 * If PM operations are defined for the @dev's driver and they include
39 * ->runtime_resume(), execute it and return its error code. Otherwise,
Aaro Koskinen6675bc02010-11-25 23:41:19 +010040 * return 0.
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010041 */
42int pm_generic_runtime_resume(struct device *dev)
43{
44 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
45 int ret;
46
Ohad Ben-Cohen05aa55d2010-09-09 00:46:16 +020047 ret = pm && pm->runtime_resume ? pm->runtime_resume(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010048
49 return ret;
50}
51EXPORT_SYMBOL_GPL(pm_generic_runtime_resume);
Ulf Hansson717e5d42013-12-10 14:37:41 +010052#endif /* CONFIG_PM */
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010053
54#ifdef CONFIG_PM_SLEEP
55/**
Rafael J. Wysocki6538df82011-05-17 23:26:21 +020056 * pm_generic_prepare - Generic routine preparing a device for power transition.
57 * @dev: Device to prepare.
58 *
59 * Prepare a device for a system-wide power transition.
60 */
61int pm_generic_prepare(struct device *dev)
62{
63 struct device_driver *drv = dev->driver;
64 int ret = 0;
65
66 if (drv && drv->pm && drv->pm->prepare)
67 ret = drv->pm->prepare(dev);
68
69 return ret;
70}
71
72/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +020073 * pm_generic_suspend_noirq - Generic suspend_noirq callback for subsystems.
74 * @dev: Device to suspend.
75 */
76int pm_generic_suspend_noirq(struct device *dev)
77{
Rafael J. Wysockie470d062012-01-29 20:38:41 +010078 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
79
80 return pm && pm->suspend_noirq ? pm->suspend_noirq(dev) : 0;
Rafael J. Wysockie5291922011-07-01 22:12:59 +020081}
82EXPORT_SYMBOL_GPL(pm_generic_suspend_noirq);
83
84/**
Rafael J. Wysockie470d062012-01-29 20:38:41 +010085 * pm_generic_suspend_late - Generic suspend_late callback for subsystems.
86 * @dev: Device to suspend.
87 */
88int pm_generic_suspend_late(struct device *dev)
89{
90 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
91
92 return pm && pm->suspend_late ? pm->suspend_late(dev) : 0;
93}
94EXPORT_SYMBOL_GPL(pm_generic_suspend_late);
95
96/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +010097 * pm_generic_suspend - Generic suspend callback for subsystems.
98 * @dev: Device to suspend.
99 */
100int pm_generic_suspend(struct device *dev)
101{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100102 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
103
104 return pm && pm->suspend ? pm->suspend(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100105}
106EXPORT_SYMBOL_GPL(pm_generic_suspend);
107
108/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200109 * pm_generic_freeze_noirq - Generic freeze_noirq callback for subsystems.
110 * @dev: Device to freeze.
111 */
112int pm_generic_freeze_noirq(struct device *dev)
113{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100114 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
115
116 return pm && pm->freeze_noirq ? pm->freeze_noirq(dev) : 0;
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200117}
118EXPORT_SYMBOL_GPL(pm_generic_freeze_noirq);
119
120/**
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100121 * pm_generic_freeze_late - Generic freeze_late callback for subsystems.
122 * @dev: Device to freeze.
123 */
124int pm_generic_freeze_late(struct device *dev)
125{
126 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
127
128 return pm && pm->freeze_late ? pm->freeze_late(dev) : 0;
129}
130EXPORT_SYMBOL_GPL(pm_generic_freeze_late);
131
132/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100133 * pm_generic_freeze - Generic freeze callback for subsystems.
134 * @dev: Device to freeze.
135 */
136int pm_generic_freeze(struct device *dev)
137{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100138 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
139
140 return pm && pm->freeze ? pm->freeze(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100141}
142EXPORT_SYMBOL_GPL(pm_generic_freeze);
143
144/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200145 * pm_generic_poweroff_noirq - Generic poweroff_noirq callback for subsystems.
146 * @dev: Device to handle.
147 */
148int pm_generic_poweroff_noirq(struct device *dev)
149{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100150 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
151
152 return pm && pm->poweroff_noirq ? pm->poweroff_noirq(dev) : 0;
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200153}
154EXPORT_SYMBOL_GPL(pm_generic_poweroff_noirq);
155
156/**
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100157 * pm_generic_poweroff_late - Generic poweroff_late callback for subsystems.
158 * @dev: Device to handle.
159 */
160int pm_generic_poweroff_late(struct device *dev)
161{
162 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
163
164 return pm && pm->poweroff_late ? pm->poweroff_late(dev) : 0;
165}
166EXPORT_SYMBOL_GPL(pm_generic_poweroff_late);
167
168/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100169 * pm_generic_poweroff - Generic poweroff callback for subsystems.
170 * @dev: Device to handle.
171 */
172int pm_generic_poweroff(struct device *dev)
173{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100174 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
175
176 return pm && pm->poweroff ? pm->poweroff(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100177}
178EXPORT_SYMBOL_GPL(pm_generic_poweroff);
179
180/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200181 * pm_generic_thaw_noirq - Generic thaw_noirq callback for subsystems.
182 * @dev: Device to thaw.
183 */
184int pm_generic_thaw_noirq(struct device *dev)
185{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100186 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
187
188 return pm && pm->thaw_noirq ? pm->thaw_noirq(dev) : 0;
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200189}
190EXPORT_SYMBOL_GPL(pm_generic_thaw_noirq);
191
192/**
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100193 * pm_generic_thaw_early - Generic thaw_early callback for subsystems.
194 * @dev: Device to thaw.
195 */
196int pm_generic_thaw_early(struct device *dev)
197{
198 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
199
200 return pm && pm->thaw_early ? pm->thaw_early(dev) : 0;
201}
202EXPORT_SYMBOL_GPL(pm_generic_thaw_early);
203
204/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100205 * pm_generic_thaw - Generic thaw callback for subsystems.
206 * @dev: Device to thaw.
207 */
208int pm_generic_thaw(struct device *dev)
209{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100210 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
211
212 return pm && pm->thaw ? pm->thaw(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100213}
214EXPORT_SYMBOL_GPL(pm_generic_thaw);
215
216/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200217 * pm_generic_resume_noirq - Generic resume_noirq callback for subsystems.
218 * @dev: Device to resume.
219 */
220int pm_generic_resume_noirq(struct device *dev)
221{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100222 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
223
224 return pm && pm->resume_noirq ? pm->resume_noirq(dev) : 0;
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200225}
226EXPORT_SYMBOL_GPL(pm_generic_resume_noirq);
227
228/**
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100229 * pm_generic_resume_early - Generic resume_early callback for subsystems.
230 * @dev: Device to resume.
231 */
232int pm_generic_resume_early(struct device *dev)
233{
234 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
235
236 return pm && pm->resume_early ? pm->resume_early(dev) : 0;
237}
238EXPORT_SYMBOL_GPL(pm_generic_resume_early);
239
240/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100241 * pm_generic_resume - Generic resume callback for subsystems.
242 * @dev: Device to resume.
243 */
244int pm_generic_resume(struct device *dev)
245{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100246 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
247
248 return pm && pm->resume ? pm->resume(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100249}
250EXPORT_SYMBOL_GPL(pm_generic_resume);
251
252/**
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200253 * pm_generic_restore_noirq - Generic restore_noirq callback for subsystems.
254 * @dev: Device to restore.
255 */
256int pm_generic_restore_noirq(struct device *dev)
257{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100258 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
259
260 return pm && pm->restore_noirq ? pm->restore_noirq(dev) : 0;
Rafael J. Wysockie5291922011-07-01 22:12:59 +0200261}
262EXPORT_SYMBOL_GPL(pm_generic_restore_noirq);
263
264/**
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100265 * pm_generic_restore_early - Generic restore_early callback for subsystems.
266 * @dev: Device to resume.
267 */
268int pm_generic_restore_early(struct device *dev)
269{
270 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
271
272 return pm && pm->restore_early ? pm->restore_early(dev) : 0;
273}
274EXPORT_SYMBOL_GPL(pm_generic_restore_early);
275
276/**
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100277 * pm_generic_restore - Generic restore callback for subsystems.
278 * @dev: Device to restore.
279 */
280int pm_generic_restore(struct device *dev)
281{
Rafael J. Wysockie470d062012-01-29 20:38:41 +0100282 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
283
284 return pm && pm->restore ? pm->restore(dev) : 0;
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100285}
286EXPORT_SYMBOL_GPL(pm_generic_restore);
Rafael J. Wysocki6538df82011-05-17 23:26:21 +0200287
288/**
Geert Uytterhoeven9accff52014-03-24 21:37:03 +0100289 * pm_generic_complete - Generic routine completing a device power transition.
Rafael J. Wysocki6538df82011-05-17 23:26:21 +0200290 * @dev: Device to handle.
291 *
292 * Complete a device power transition during a system-wide power transition.
293 */
294void pm_generic_complete(struct device *dev)
295{
296 struct device_driver *drv = dev->driver;
297
298 if (drv && drv->pm && drv->pm->complete)
299 drv->pm->complete(dev);
Rafael J. Wysocki6538df82011-05-17 23:26:21 +0200300}
Rafael J. Wysocki58a1fbb2015-10-07 00:50:24 +0200301
302/**
303 * pm_complete_with_resume_check - Complete a device power transition.
304 * @dev: Device to handle.
305 *
306 * Complete a device power transition during a system-wide power transition and
307 * optionally schedule a runtime resume of the device if the system resume in
308 * progress has been initated by the platform firmware and the device had its
309 * power.direct_complete flag set.
310 */
311void pm_complete_with_resume_check(struct device *dev)
312{
313 pm_generic_complete(dev);
314 /*
315 * If the device had been runtime-suspended before the system went into
316 * the sleep state it is going out of and it has never been resumed till
317 * now, resume it in case the firmware powered it up.
318 */
319 if (dev->power.direct_complete && pm_resume_via_firmware())
320 pm_request_resume(dev);
321}
322EXPORT_SYMBOL_GPL(pm_complete_with_resume_check);
Rafael J. Wysockid690b2c2010-03-06 21:28:37 +0100323#endif /* CONFIG_PM_SLEEP */