Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 1 | /* |
| 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 Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame^] | 11 | #include <linux/export.h> |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 12 | |
| 13 | #ifdef CONFIG_PM_RUNTIME |
| 14 | /** |
| 15 | * pm_generic_runtime_idle - Generic runtime idle callback for subsystems. |
| 16 | * @dev: Device to handle. |
| 17 | * |
| 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. |
| 21 | */ |
| 22 | int pm_generic_runtime_idle(struct device *dev) |
| 23 | { |
| 24 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 25 | |
| 26 | if (pm && pm->runtime_idle) { |
| 27 | int ret = pm->runtime_idle(dev); |
| 28 | if (ret) |
| 29 | return ret; |
| 30 | } |
| 31 | |
| 32 | pm_runtime_suspend(dev); |
| 33 | return 0; |
| 34 | } |
| 35 | EXPORT_SYMBOL_GPL(pm_generic_runtime_idle); |
| 36 | |
| 37 | /** |
| 38 | * pm_generic_runtime_suspend - Generic runtime suspend callback for subsystems. |
| 39 | * @dev: Device to suspend. |
| 40 | * |
| 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, |
Aaro Koskinen | 6675bc0 | 2010-11-25 23:41:19 +0100 | [diff] [blame] | 43 | * return 0. |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 44 | */ |
| 45 | int pm_generic_runtime_suspend(struct device *dev) |
| 46 | { |
| 47 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 48 | int ret; |
| 49 | |
Ohad Ben-Cohen | 05aa55d | 2010-09-09 00:46:16 +0200 | [diff] [blame] | 50 | ret = pm && pm->runtime_suspend ? pm->runtime_suspend(dev) : 0; |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 51 | |
| 52 | return ret; |
| 53 | } |
| 54 | EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend); |
| 55 | |
| 56 | /** |
| 57 | * pm_generic_runtime_resume - Generic runtime resume callback for subsystems. |
| 58 | * @dev: Device to resume. |
| 59 | * |
| 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, |
Aaro Koskinen | 6675bc0 | 2010-11-25 23:41:19 +0100 | [diff] [blame] | 62 | * return 0. |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 63 | */ |
| 64 | int pm_generic_runtime_resume(struct device *dev) |
| 65 | { |
| 66 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 67 | int ret; |
| 68 | |
Ohad Ben-Cohen | 05aa55d | 2010-09-09 00:46:16 +0200 | [diff] [blame] | 69 | ret = pm && pm->runtime_resume ? pm->runtime_resume(dev) : 0; |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(pm_generic_runtime_resume); |
| 74 | #endif /* CONFIG_PM_RUNTIME */ |
| 75 | |
| 76 | #ifdef CONFIG_PM_SLEEP |
| 77 | /** |
Rafael J. Wysocki | 6538df8 | 2011-05-17 23:26:21 +0200 | [diff] [blame] | 78 | * pm_generic_prepare - Generic routine preparing a device for power transition. |
| 79 | * @dev: Device to prepare. |
| 80 | * |
| 81 | * Prepare a device for a system-wide power transition. |
| 82 | */ |
| 83 | int pm_generic_prepare(struct device *dev) |
| 84 | { |
| 85 | struct device_driver *drv = dev->driver; |
| 86 | int ret = 0; |
| 87 | |
| 88 | if (drv && drv->pm && drv->pm->prepare) |
| 89 | ret = drv->pm->prepare(dev); |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /** |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 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. |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 98 | * @bool: Whether or not this is the "noirq" stage. |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 99 | * |
| 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. |
| 103 | */ |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 104 | static int __pm_generic_call(struct device *dev, int event, bool noirq) |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 105 | { |
| 106 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 107 | int (*callback)(struct device *); |
| 108 | |
| 109 | if (!pm || pm_runtime_suspended(dev)) |
| 110 | return 0; |
| 111 | |
| 112 | switch (event) { |
| 113 | case PM_EVENT_SUSPEND: |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 114 | callback = noirq ? pm->suspend_noirq : pm->suspend; |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 115 | break; |
| 116 | case PM_EVENT_FREEZE: |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 117 | callback = noirq ? pm->freeze_noirq : pm->freeze; |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 118 | break; |
| 119 | case PM_EVENT_HIBERNATE: |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 120 | callback = noirq ? pm->poweroff_noirq : pm->poweroff; |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 121 | break; |
| 122 | case PM_EVENT_THAW: |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 123 | callback = noirq ? pm->thaw_noirq : pm->thaw; |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 124 | break; |
| 125 | default: |
| 126 | callback = NULL; |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | return callback ? callback(dev) : 0; |
| 131 | } |
| 132 | |
| 133 | /** |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 134 | * pm_generic_suspend_noirq - Generic suspend_noirq callback for subsystems. |
| 135 | * @dev: Device to suspend. |
| 136 | */ |
| 137 | int pm_generic_suspend_noirq(struct device *dev) |
| 138 | { |
| 139 | return __pm_generic_call(dev, PM_EVENT_SUSPEND, true); |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(pm_generic_suspend_noirq); |
| 142 | |
| 143 | /** |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 144 | * pm_generic_suspend - Generic suspend callback for subsystems. |
| 145 | * @dev: Device to suspend. |
| 146 | */ |
| 147 | int pm_generic_suspend(struct device *dev) |
| 148 | { |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 149 | return __pm_generic_call(dev, PM_EVENT_SUSPEND, false); |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 150 | } |
| 151 | EXPORT_SYMBOL_GPL(pm_generic_suspend); |
| 152 | |
| 153 | /** |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 154 | * pm_generic_freeze_noirq - Generic freeze_noirq callback for subsystems. |
| 155 | * @dev: Device to freeze. |
| 156 | */ |
| 157 | int pm_generic_freeze_noirq(struct device *dev) |
| 158 | { |
| 159 | return __pm_generic_call(dev, PM_EVENT_FREEZE, true); |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(pm_generic_freeze_noirq); |
| 162 | |
| 163 | /** |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 164 | * pm_generic_freeze - Generic freeze callback for subsystems. |
| 165 | * @dev: Device to freeze. |
| 166 | */ |
| 167 | int pm_generic_freeze(struct device *dev) |
| 168 | { |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 169 | return __pm_generic_call(dev, PM_EVENT_FREEZE, false); |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 170 | } |
| 171 | EXPORT_SYMBOL_GPL(pm_generic_freeze); |
| 172 | |
| 173 | /** |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 174 | * pm_generic_poweroff_noirq - Generic poweroff_noirq callback for subsystems. |
| 175 | * @dev: Device to handle. |
| 176 | */ |
| 177 | int pm_generic_poweroff_noirq(struct device *dev) |
| 178 | { |
| 179 | return __pm_generic_call(dev, PM_EVENT_HIBERNATE, true); |
| 180 | } |
| 181 | EXPORT_SYMBOL_GPL(pm_generic_poweroff_noirq); |
| 182 | |
| 183 | /** |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 184 | * pm_generic_poweroff - Generic poweroff callback for subsystems. |
| 185 | * @dev: Device to handle. |
| 186 | */ |
| 187 | int pm_generic_poweroff(struct device *dev) |
| 188 | { |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 189 | return __pm_generic_call(dev, PM_EVENT_HIBERNATE, false); |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 190 | } |
| 191 | EXPORT_SYMBOL_GPL(pm_generic_poweroff); |
| 192 | |
| 193 | /** |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 194 | * pm_generic_thaw_noirq - Generic thaw_noirq callback for subsystems. |
| 195 | * @dev: Device to thaw. |
| 196 | */ |
| 197 | int pm_generic_thaw_noirq(struct device *dev) |
| 198 | { |
| 199 | return __pm_generic_call(dev, PM_EVENT_THAW, true); |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(pm_generic_thaw_noirq); |
| 202 | |
| 203 | /** |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 204 | * pm_generic_thaw - Generic thaw callback for subsystems. |
| 205 | * @dev: Device to thaw. |
| 206 | */ |
| 207 | int pm_generic_thaw(struct device *dev) |
| 208 | { |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 209 | return __pm_generic_call(dev, PM_EVENT_THAW, false); |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 210 | } |
| 211 | EXPORT_SYMBOL_GPL(pm_generic_thaw); |
| 212 | |
| 213 | /** |
| 214 | * __pm_generic_resume - Generic resume/restore callback for subsystems. |
| 215 | * @dev: Device to handle. |
| 216 | * @event: PM transition of the system under way. |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 217 | * @bool: Whether or not this is the "noirq" stage. |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 218 | * |
| 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. |
| 222 | */ |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 223 | static int __pm_generic_resume(struct device *dev, int event, bool noirq) |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 224 | { |
| 225 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 226 | int (*callback)(struct device *); |
| 227 | int ret; |
| 228 | |
| 229 | if (!pm) |
| 230 | return 0; |
| 231 | |
| 232 | switch (event) { |
| 233 | case PM_EVENT_RESUME: |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 234 | callback = noirq ? pm->resume_noirq : pm->resume; |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 235 | break; |
| 236 | case PM_EVENT_RESTORE: |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 237 | callback = noirq ? pm->restore_noirq : pm->restore; |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 238 | break; |
| 239 | default: |
| 240 | callback = NULL; |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | if (!callback) |
| 245 | return 0; |
| 246 | |
| 247 | ret = callback(dev); |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 248 | if (!ret && !noirq && pm_runtime_enabled(dev)) { |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 249 | pm_runtime_disable(dev); |
| 250 | pm_runtime_set_active(dev); |
| 251 | pm_runtime_enable(dev); |
| 252 | } |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | /** |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 258 | * pm_generic_resume_noirq - Generic resume_noirq callback for subsystems. |
| 259 | * @dev: Device to resume. |
| 260 | */ |
| 261 | int pm_generic_resume_noirq(struct device *dev) |
| 262 | { |
| 263 | return __pm_generic_resume(dev, PM_EVENT_RESUME, true); |
| 264 | } |
| 265 | EXPORT_SYMBOL_GPL(pm_generic_resume_noirq); |
| 266 | |
| 267 | /** |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 268 | * pm_generic_resume - Generic resume callback for subsystems. |
| 269 | * @dev: Device to resume. |
| 270 | */ |
| 271 | int pm_generic_resume(struct device *dev) |
| 272 | { |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 273 | return __pm_generic_resume(dev, PM_EVENT_RESUME, false); |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 274 | } |
| 275 | EXPORT_SYMBOL_GPL(pm_generic_resume); |
| 276 | |
| 277 | /** |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 278 | * pm_generic_restore_noirq - Generic restore_noirq callback for subsystems. |
| 279 | * @dev: Device to restore. |
| 280 | */ |
| 281 | int pm_generic_restore_noirq(struct device *dev) |
| 282 | { |
| 283 | return __pm_generic_resume(dev, PM_EVENT_RESTORE, true); |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(pm_generic_restore_noirq); |
| 286 | |
| 287 | /** |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 288 | * pm_generic_restore - Generic restore callback for subsystems. |
| 289 | * @dev: Device to restore. |
| 290 | */ |
| 291 | int pm_generic_restore(struct device *dev) |
| 292 | { |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 293 | return __pm_generic_resume(dev, PM_EVENT_RESTORE, false); |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 294 | } |
| 295 | EXPORT_SYMBOL_GPL(pm_generic_restore); |
Rafael J. Wysocki | 6538df8 | 2011-05-17 23:26:21 +0200 | [diff] [blame] | 296 | |
| 297 | /** |
| 298 | * pm_generic_complete - Generic routine competing a device power transition. |
| 299 | * @dev: Device to handle. |
| 300 | * |
| 301 | * Complete a device power transition during a system-wide power transition. |
| 302 | */ |
| 303 | void pm_generic_complete(struct device *dev) |
| 304 | { |
| 305 | struct device_driver *drv = dev->driver; |
| 306 | |
| 307 | if (drv && drv->pm && drv->pm->complete) |
| 308 | drv->pm->complete(dev); |
| 309 | |
| 310 | /* |
| 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. |
| 313 | */ |
| 314 | pm_runtime_idle(dev); |
| 315 | } |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 316 | #endif /* CONFIG_PM_SLEEP */ |
| 317 | |
| 318 | struct dev_pm_ops generic_subsys_pm_ops = { |
| 319 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 6538df8 | 2011-05-17 23:26:21 +0200 | [diff] [blame] | 320 | .prepare = pm_generic_prepare, |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 321 | .suspend = pm_generic_suspend, |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 322 | .suspend_noirq = pm_generic_suspend_noirq, |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 323 | .resume = pm_generic_resume, |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 324 | .resume_noirq = pm_generic_resume_noirq, |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 325 | .freeze = pm_generic_freeze, |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 326 | .freeze_noirq = pm_generic_freeze_noirq, |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 327 | .thaw = pm_generic_thaw, |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 328 | .thaw_noirq = pm_generic_thaw_noirq, |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 329 | .poweroff = pm_generic_poweroff, |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 330 | .poweroff_noirq = pm_generic_poweroff_noirq, |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 331 | .restore = pm_generic_restore, |
Rafael J. Wysocki | e529192 | 2011-07-01 22:12:59 +0200 | [diff] [blame] | 332 | .restore_noirq = pm_generic_restore_noirq, |
Rafael J. Wysocki | 6538df8 | 2011-05-17 23:26:21 +0200 | [diff] [blame] | 333 | .complete = pm_generic_complete, |
Rafael J. Wysocki | d690b2c | 2010-03-06 21:28:37 +0100 | [diff] [blame] | 334 | #endif |
| 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, |
| 339 | #endif |
| 340 | }; |
| 341 | EXPORT_SYMBOL_GPL(generic_subsys_pm_ops); |