Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/domain.c - Common code related to device power domains. |
| 3 | * |
| 4 | * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp. |
| 5 | * |
| 6 | * This file is released under the GPLv2. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/pm_runtime.h> |
| 13 | #include <linux/pm_domain.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/err.h> |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 16 | #include <linux/sched.h> |
| 17 | #include <linux/suspend.h> |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 18 | #include <linux/export.h> |
| 19 | |
| 20 | #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \ |
| 21 | ({ \ |
| 22 | type (*__routine)(struct device *__d); \ |
| 23 | type __ret = (type)0; \ |
| 24 | \ |
| 25 | __routine = genpd->dev_ops.callback; \ |
| 26 | if (__routine) { \ |
| 27 | __ret = __routine(dev); \ |
| 28 | } else { \ |
| 29 | __routine = dev_gpd_data(dev)->ops.callback; \ |
| 30 | if (__routine) \ |
| 31 | __ret = __routine(dev); \ |
| 32 | } \ |
| 33 | __ret; \ |
| 34 | }) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 35 | |
Rafael J. Wysocki | 5125bbf38 | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 36 | static LIST_HEAD(gpd_list); |
| 37 | static DEFINE_MUTEX(gpd_list_lock); |
| 38 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 39 | #ifdef CONFIG_PM |
| 40 | |
| 41 | static struct generic_pm_domain *dev_to_genpd(struct device *dev) |
| 42 | { |
| 43 | if (IS_ERR_OR_NULL(dev->pm_domain)) |
| 44 | return ERR_PTR(-EINVAL); |
| 45 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 46 | return pd_to_genpd(dev->pm_domain); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 47 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 48 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 49 | static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 50 | { |
| 51 | return GENPD_DEV_CALLBACK(genpd, int, stop, dev); |
| 52 | } |
| 53 | |
| 54 | static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 55 | { |
| 56 | return GENPD_DEV_CALLBACK(genpd, int, start, dev); |
| 57 | } |
| 58 | |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame^] | 59 | static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 60 | { |
| 61 | return GENPD_DEV_CALLBACK(genpd, int, save_state, dev); |
| 62 | } |
| 63 | |
| 64 | static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 65 | { |
| 66 | return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev); |
| 67 | } |
| 68 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 69 | static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 70 | { |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 71 | bool ret = false; |
| 72 | |
| 73 | if (!WARN_ON(atomic_read(&genpd->sd_count) == 0)) |
| 74 | ret = !!atomic_dec_and_test(&genpd->sd_count); |
| 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | static void genpd_sd_counter_inc(struct generic_pm_domain *genpd) |
| 80 | { |
| 81 | atomic_inc(&genpd->sd_count); |
| 82 | smp_mb__after_atomic_inc(); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 85 | static void genpd_acquire_lock(struct generic_pm_domain *genpd) |
| 86 | { |
| 87 | DEFINE_WAIT(wait); |
| 88 | |
| 89 | mutex_lock(&genpd->lock); |
| 90 | /* |
| 91 | * Wait for the domain to transition into either the active, |
| 92 | * or the power off state. |
| 93 | */ |
| 94 | for (;;) { |
| 95 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 96 | TASK_UNINTERRUPTIBLE); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 97 | if (genpd->status == GPD_STATE_ACTIVE |
| 98 | || genpd->status == GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 99 | break; |
| 100 | mutex_unlock(&genpd->lock); |
| 101 | |
| 102 | schedule(); |
| 103 | |
| 104 | mutex_lock(&genpd->lock); |
| 105 | } |
| 106 | finish_wait(&genpd->status_wait_queue, &wait); |
| 107 | } |
| 108 | |
| 109 | static void genpd_release_lock(struct generic_pm_domain *genpd) |
| 110 | { |
| 111 | mutex_unlock(&genpd->lock); |
| 112 | } |
| 113 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 114 | static void genpd_set_active(struct generic_pm_domain *genpd) |
| 115 | { |
| 116 | if (genpd->resume_count == 0) |
| 117 | genpd->status = GPD_STATE_ACTIVE; |
| 118 | } |
| 119 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 120 | /** |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 121 | * __pm_genpd_poweron - Restore power to a given PM domain and its masters. |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 122 | * @genpd: PM domain to power up. |
| 123 | * |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 124 | * Restore power to @genpd and all of its masters so that it is possible to |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 125 | * resume a device belonging to it. |
| 126 | */ |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 127 | int __pm_genpd_poweron(struct generic_pm_domain *genpd) |
| 128 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 129 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 130 | struct gpd_link *link; |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 131 | DEFINE_WAIT(wait); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 132 | int ret = 0; |
| 133 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 134 | /* If the domain's master is being waited for, we have to wait too. */ |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 135 | for (;;) { |
| 136 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 137 | TASK_UNINTERRUPTIBLE); |
Rafael J. Wysocki | 17877eb | 2011-08-08 23:43:50 +0200 | [diff] [blame] | 138 | if (genpd->status != GPD_STATE_WAIT_MASTER) |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 139 | break; |
| 140 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 141 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 142 | schedule(); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 143 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 144 | mutex_lock(&genpd->lock); |
| 145 | } |
| 146 | finish_wait(&genpd->status_wait_queue, &wait); |
| 147 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 148 | if (genpd->status == GPD_STATE_ACTIVE |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 149 | || (genpd->prepared_count > 0 && genpd->suspend_power_off)) |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 150 | return 0; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 151 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 152 | if (genpd->status != GPD_STATE_POWER_OFF) { |
| 153 | genpd_set_active(genpd); |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 154 | return 0; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 157 | /* |
| 158 | * The list is guaranteed not to change while the loop below is being |
| 159 | * executed, unless one of the masters' .power_on() callbacks fiddles |
| 160 | * with it. |
| 161 | */ |
| 162 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 163 | genpd_sd_counter_inc(link->master); |
Rafael J. Wysocki | 17877eb | 2011-08-08 23:43:50 +0200 | [diff] [blame] | 164 | genpd->status = GPD_STATE_WAIT_MASTER; |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 165 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 166 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 167 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 168 | ret = pm_genpd_poweron(link->master); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 169 | |
| 170 | mutex_lock(&genpd->lock); |
| 171 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 172 | /* |
| 173 | * The "wait for parent" status is guaranteed not to change |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 174 | * while the master is powering on. |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 175 | */ |
| 176 | genpd->status = GPD_STATE_POWER_OFF; |
| 177 | wake_up_all(&genpd->status_wait_queue); |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 178 | if (ret) { |
| 179 | genpd_sd_counter_dec(link->master); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 180 | goto err; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 181 | } |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 184 | if (genpd->power_on) { |
Rafael J. Wysocki | fe202fd | 2011-08-05 21:45:11 +0200 | [diff] [blame] | 185 | ret = genpd->power_on(genpd); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 186 | if (ret) |
| 187 | goto err; |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 188 | } |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 189 | |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 190 | genpd_set_active(genpd); |
| 191 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 192 | return 0; |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 193 | |
| 194 | err: |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 195 | list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node) |
| 196 | genpd_sd_counter_dec(link->master); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 197 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | /** |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 202 | * pm_genpd_poweron - Restore power to a given PM domain and its masters. |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 203 | * @genpd: PM domain to power up. |
| 204 | */ |
| 205 | int pm_genpd_poweron(struct generic_pm_domain *genpd) |
| 206 | { |
| 207 | int ret; |
| 208 | |
| 209 | mutex_lock(&genpd->lock); |
| 210 | ret = __pm_genpd_poweron(genpd); |
| 211 | mutex_unlock(&genpd->lock); |
| 212 | return ret; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | #endif /* CONFIG_PM */ |
| 216 | |
| 217 | #ifdef CONFIG_PM_RUNTIME |
| 218 | |
| 219 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 220 | * __pm_genpd_save_device - Save the pre-suspend state of a device. |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 221 | * @pdd: Domain data of the device to save the state of. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 222 | * @genpd: PM domain the device belongs to. |
| 223 | */ |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 224 | static int __pm_genpd_save_device(struct pm_domain_data *pdd, |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 225 | struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 226 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 227 | { |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 228 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 229 | struct device *dev = pdd->dev; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 230 | int ret = 0; |
| 231 | |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 232 | if (gpd_data->need_restore) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 233 | return 0; |
| 234 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 235 | mutex_unlock(&genpd->lock); |
| 236 | |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame^] | 237 | genpd_start_dev(genpd, dev); |
| 238 | ret = genpd_save_dev(genpd, dev); |
| 239 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 240 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 241 | mutex_lock(&genpd->lock); |
| 242 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 243 | if (!ret) |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 244 | gpd_data->need_restore = true; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * __pm_genpd_restore_device - Restore the pre-suspend state of a device. |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 251 | * @pdd: Domain data of the device to restore the state of. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 252 | * @genpd: PM domain the device belongs to. |
| 253 | */ |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 254 | static void __pm_genpd_restore_device(struct pm_domain_data *pdd, |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 255 | struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 256 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 257 | { |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 258 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 259 | struct device *dev = pdd->dev; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 260 | |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 261 | if (!gpd_data->need_restore) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 262 | return; |
| 263 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 264 | mutex_unlock(&genpd->lock); |
| 265 | |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame^] | 266 | genpd_start_dev(genpd, dev); |
| 267 | genpd_restore_dev(genpd, dev); |
| 268 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 269 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 270 | mutex_lock(&genpd->lock); |
| 271 | |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 272 | gpd_data->need_restore = false; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 276 | * genpd_abort_poweroff - Check if a PM domain power off should be aborted. |
| 277 | * @genpd: PM domain to check. |
| 278 | * |
| 279 | * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during |
| 280 | * a "power off" operation, which means that a "power on" has occured in the |
| 281 | * meantime, or if its resume_count field is different from zero, which means |
| 282 | * that one of its devices has been resumed in the meantime. |
| 283 | */ |
| 284 | static bool genpd_abort_poweroff(struct generic_pm_domain *genpd) |
| 285 | { |
Rafael J. Wysocki | 17877eb | 2011-08-08 23:43:50 +0200 | [diff] [blame] | 286 | return genpd->status == GPD_STATE_WAIT_MASTER |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 287 | || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
Rafael J. Wysocki | 56375fd | 2011-07-12 00:40:03 +0200 | [diff] [blame] | 291 | * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff(). |
| 292 | * @genpd: PM domait to power off. |
| 293 | * |
| 294 | * Queue up the execution of pm_genpd_poweroff() unless it's already been done |
| 295 | * before. |
| 296 | */ |
Rafael J. Wysocki | 0bc5b2d | 2011-07-14 20:59:07 +0200 | [diff] [blame] | 297 | void genpd_queue_power_off_work(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 56375fd | 2011-07-12 00:40:03 +0200 | [diff] [blame] | 298 | { |
| 299 | if (!work_pending(&genpd->power_off_work)) |
| 300 | queue_work(pm_wq, &genpd->power_off_work); |
| 301 | } |
| 302 | |
| 303 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 304 | * pm_genpd_poweroff - Remove power from a given PM domain. |
| 305 | * @genpd: PM domain to power down. |
| 306 | * |
| 307 | * If all of the @genpd's devices have been suspended and all of its subdomains |
| 308 | * have been powered down, run the runtime suspend callbacks provided by all of |
| 309 | * the @genpd's devices' drivers and remove power from @genpd. |
| 310 | */ |
| 311 | static int pm_genpd_poweroff(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 312 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 313 | { |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 314 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 315 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 316 | unsigned int not_suspended; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 317 | int ret = 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 318 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 319 | start: |
| 320 | /* |
| 321 | * Do not try to power off the domain in the following situations: |
| 322 | * (1) The domain is already in the "power off" state. |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 323 | * (2) The domain is waiting for its master to power up. |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 324 | * (3) One of the domain's devices is being resumed right now. |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 325 | * (4) System suspend is in progress. |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 326 | */ |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 327 | if (genpd->status == GPD_STATE_POWER_OFF |
Rafael J. Wysocki | 17877eb | 2011-08-08 23:43:50 +0200 | [diff] [blame] | 328 | || genpd->status == GPD_STATE_WAIT_MASTER |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 329 | || genpd->resume_count > 0 || genpd->prepared_count > 0) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 330 | return 0; |
| 331 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 332 | if (atomic_read(&genpd->sd_count) > 0) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 333 | return -EBUSY; |
| 334 | |
| 335 | not_suspended = 0; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 336 | list_for_each_entry(pdd, &genpd->dev_list, list_node) |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 337 | if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev) |
| 338 | || pdd->dev->power.irq_safe)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 339 | not_suspended++; |
| 340 | |
| 341 | if (not_suspended > genpd->in_progress) |
| 342 | return -EBUSY; |
| 343 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 344 | if (genpd->poweroff_task) { |
| 345 | /* |
| 346 | * Another instance of pm_genpd_poweroff() is executing |
| 347 | * callbacks, so tell it to start over and return. |
| 348 | */ |
| 349 | genpd->status = GPD_STATE_REPEAT; |
| 350 | return 0; |
| 351 | } |
| 352 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 353 | if (genpd->gov && genpd->gov->power_down_ok) { |
| 354 | if (!genpd->gov->power_down_ok(&genpd->domain)) |
| 355 | return -EAGAIN; |
| 356 | } |
| 357 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 358 | genpd->status = GPD_STATE_BUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 359 | genpd->poweroff_task = current; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 360 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 361 | list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) { |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 362 | ret = atomic_read(&genpd->sd_count) == 0 ? |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 363 | __pm_genpd_save_device(pdd, genpd) : -EBUSY; |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 364 | |
| 365 | if (genpd_abort_poweroff(genpd)) |
| 366 | goto out; |
| 367 | |
Rafael J. Wysocki | 697a7f3 | 2011-07-12 00:39:48 +0200 | [diff] [blame] | 368 | if (ret) { |
| 369 | genpd_set_active(genpd); |
| 370 | goto out; |
| 371 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 372 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 373 | if (genpd->status == GPD_STATE_REPEAT) { |
| 374 | genpd->poweroff_task = NULL; |
| 375 | goto start; |
| 376 | } |
| 377 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 378 | |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 379 | if (genpd->power_off) { |
| 380 | if (atomic_read(&genpd->sd_count) > 0) { |
| 381 | ret = -EBUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 382 | goto out; |
| 383 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 384 | |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 385 | /* |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 386 | * If sd_count > 0 at this point, one of the subdomains hasn't |
| 387 | * managed to call pm_genpd_poweron() for the master yet after |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 388 | * incrementing it. In that case pm_genpd_poweron() will wait |
| 389 | * for us to drop the lock, so we can call .power_off() and let |
| 390 | * the pm_genpd_poweron() restore power for us (this shouldn't |
| 391 | * happen very often). |
| 392 | */ |
Rafael J. Wysocki | d280540 | 2011-07-14 20:59:20 +0200 | [diff] [blame] | 393 | ret = genpd->power_off(genpd); |
| 394 | if (ret == -EBUSY) { |
| 395 | genpd_set_active(genpd); |
Rafael J. Wysocki | d280540 | 2011-07-14 20:59:20 +0200 | [diff] [blame] | 396 | goto out; |
| 397 | } |
| 398 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 399 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 400 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 401 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 402 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 403 | genpd_sd_counter_dec(link->master); |
| 404 | genpd_queue_power_off_work(link->master); |
| 405 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 406 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 407 | out: |
| 408 | genpd->poweroff_task = NULL; |
| 409 | wake_up_all(&genpd->status_wait_queue); |
| 410 | return ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** |
| 414 | * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0. |
| 415 | * @work: Work structure used for scheduling the execution of this function. |
| 416 | */ |
| 417 | static void genpd_power_off_work_fn(struct work_struct *work) |
| 418 | { |
| 419 | struct generic_pm_domain *genpd; |
| 420 | |
| 421 | genpd = container_of(work, struct generic_pm_domain, power_off_work); |
| 422 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 423 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 424 | pm_genpd_poweroff(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 425 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
| 429 | * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain. |
| 430 | * @dev: Device to suspend. |
| 431 | * |
| 432 | * Carry out a runtime suspend of a device under the assumption that its |
| 433 | * pm_domain field points to the domain member of an object of type |
| 434 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 435 | */ |
| 436 | static int pm_genpd_runtime_suspend(struct device *dev) |
| 437 | { |
| 438 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 439 | int ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 440 | |
| 441 | dev_dbg(dev, "%s()\n", __func__); |
| 442 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 443 | genpd = dev_to_genpd(dev); |
| 444 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 445 | return -EINVAL; |
| 446 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 447 | might_sleep_if(!genpd->dev_irq_safe); |
| 448 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 449 | ret = genpd_stop_dev(genpd, dev); |
| 450 | if (ret) |
| 451 | return ret; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 452 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 453 | /* |
| 454 | * If power.irq_safe is set, this routine will be run with interrupts |
| 455 | * off, so it can't use mutexes. |
| 456 | */ |
| 457 | if (dev->power.irq_safe) |
| 458 | return 0; |
| 459 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 460 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 461 | genpd->in_progress++; |
| 462 | pm_genpd_poweroff(genpd); |
| 463 | genpd->in_progress--; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 464 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 470 | * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain. |
| 471 | * @dev: Device to resume. |
| 472 | * |
| 473 | * Carry out a runtime resume of a device under the assumption that its |
| 474 | * pm_domain field points to the domain member of an object of type |
| 475 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 476 | */ |
| 477 | static int pm_genpd_runtime_resume(struct device *dev) |
| 478 | { |
| 479 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 480 | DEFINE_WAIT(wait); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 481 | int ret; |
| 482 | |
| 483 | dev_dbg(dev, "%s()\n", __func__); |
| 484 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 485 | genpd = dev_to_genpd(dev); |
| 486 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 487 | return -EINVAL; |
| 488 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 489 | might_sleep_if(!genpd->dev_irq_safe); |
| 490 | |
| 491 | /* If power.irq_safe, the PM domain is never powered off. */ |
| 492 | if (dev->power.irq_safe) |
| 493 | goto out; |
| 494 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 495 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 496 | ret = __pm_genpd_poweron(genpd); |
| 497 | if (ret) { |
| 498 | mutex_unlock(&genpd->lock); |
| 499 | return ret; |
| 500 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 501 | genpd->status = GPD_STATE_BUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 502 | genpd->resume_count++; |
| 503 | for (;;) { |
| 504 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 505 | TASK_UNINTERRUPTIBLE); |
| 506 | /* |
| 507 | * If current is the powering off task, we have been called |
| 508 | * reentrantly from one of the device callbacks, so we should |
| 509 | * not wait. |
| 510 | */ |
| 511 | if (!genpd->poweroff_task || genpd->poweroff_task == current) |
| 512 | break; |
| 513 | mutex_unlock(&genpd->lock); |
| 514 | |
| 515 | schedule(); |
| 516 | |
| 517 | mutex_lock(&genpd->lock); |
| 518 | } |
| 519 | finish_wait(&genpd->status_wait_queue, &wait); |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 520 | __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 521 | genpd->resume_count--; |
| 522 | genpd_set_active(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 523 | wake_up_all(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 524 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 525 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 526 | out: |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 527 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
Rafael J. Wysocki | 17f2ae7 | 2011-08-14 13:34:31 +0200 | [diff] [blame] | 532 | /** |
| 533 | * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use. |
| 534 | */ |
| 535 | void pm_genpd_poweroff_unused(void) |
| 536 | { |
| 537 | struct generic_pm_domain *genpd; |
| 538 | |
| 539 | mutex_lock(&gpd_list_lock); |
| 540 | |
| 541 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) |
| 542 | genpd_queue_power_off_work(genpd); |
| 543 | |
| 544 | mutex_unlock(&gpd_list_lock); |
| 545 | } |
| 546 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 547 | #else |
| 548 | |
| 549 | static inline void genpd_power_off_work_fn(struct work_struct *work) {} |
| 550 | |
| 551 | #define pm_genpd_runtime_suspend NULL |
| 552 | #define pm_genpd_runtime_resume NULL |
| 553 | |
| 554 | #endif /* CONFIG_PM_RUNTIME */ |
| 555 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 556 | #ifdef CONFIG_PM_SLEEP |
| 557 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 558 | static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd, |
| 559 | struct device *dev) |
| 560 | { |
| 561 | return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev); |
| 562 | } |
| 563 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 564 | /** |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 565 | * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 566 | * @genpd: PM domain to power off, if possible. |
| 567 | * |
| 568 | * Check if the given PM domain can be powered off (during system suspend or |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 569 | * hibernation) and do that if so. Also, in that case propagate to its masters. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 570 | * |
| 571 | * This function is only called in "noirq" stages of system power transitions, |
| 572 | * so it need not acquire locks (all of the "noirq" callbacks are executed |
| 573 | * sequentially, so it is guaranteed that it will never run twice in parallel). |
| 574 | */ |
| 575 | static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd) |
| 576 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 577 | struct gpd_link *link; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 578 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 579 | if (genpd->status == GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 580 | return; |
| 581 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 582 | if (genpd->suspended_count != genpd->device_count |
| 583 | || atomic_read(&genpd->sd_count) > 0) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 584 | return; |
| 585 | |
| 586 | if (genpd->power_off) |
| 587 | genpd->power_off(genpd); |
| 588 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 589 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 590 | |
| 591 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 592 | genpd_sd_counter_dec(link->master); |
| 593 | pm_genpd_sync_poweroff(link->master); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
| 597 | /** |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 598 | * resume_needed - Check whether to resume a device before system suspend. |
| 599 | * @dev: Device to check. |
| 600 | * @genpd: PM domain the device belongs to. |
| 601 | * |
| 602 | * There are two cases in which a device that can wake up the system from sleep |
| 603 | * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled |
| 604 | * to wake up the system and it has to remain active for this purpose while the |
| 605 | * system is in the sleep state and (2) if the device is not enabled to wake up |
| 606 | * the system from sleep states and it generally doesn't generate wakeup signals |
| 607 | * by itself (those signals are generated on its behalf by other parts of the |
| 608 | * system). In the latter case it may be necessary to reconfigure the device's |
| 609 | * wakeup settings during system suspend, because it may have been set up to |
| 610 | * signal remote wakeup from the system's working state as needed by runtime PM. |
| 611 | * Return 'true' in either of the above cases. |
| 612 | */ |
| 613 | static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd) |
| 614 | { |
| 615 | bool active_wakeup; |
| 616 | |
| 617 | if (!device_can_wakeup(dev)) |
| 618 | return false; |
| 619 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 620 | active_wakeup = genpd_dev_active_wakeup(genpd, dev); |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 621 | return device_may_wakeup(dev) ? active_wakeup : !active_wakeup; |
| 622 | } |
| 623 | |
| 624 | /** |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 625 | * pm_genpd_prepare - Start power transition of a device in a PM domain. |
| 626 | * @dev: Device to start the transition of. |
| 627 | * |
| 628 | * Start a power transition of a device (during a system-wide power transition) |
| 629 | * under the assumption that its pm_domain field points to the domain member of |
| 630 | * an object of type struct generic_pm_domain representing a PM domain |
| 631 | * consisting of I/O devices. |
| 632 | */ |
| 633 | static int pm_genpd_prepare(struct device *dev) |
| 634 | { |
| 635 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 636 | int ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 637 | |
| 638 | dev_dbg(dev, "%s()\n", __func__); |
| 639 | |
| 640 | genpd = dev_to_genpd(dev); |
| 641 | if (IS_ERR(genpd)) |
| 642 | return -EINVAL; |
| 643 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 644 | /* |
| 645 | * If a wakeup request is pending for the device, it should be woken up |
| 646 | * at this point and a system wakeup event should be reported if it's |
| 647 | * set up to wake up the system from sleep states. |
| 648 | */ |
| 649 | pm_runtime_get_noresume(dev); |
| 650 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) |
| 651 | pm_wakeup_event(dev, 0); |
| 652 | |
| 653 | if (pm_wakeup_pending()) { |
| 654 | pm_runtime_put_sync(dev); |
| 655 | return -EBUSY; |
| 656 | } |
| 657 | |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 658 | if (resume_needed(dev, genpd)) |
| 659 | pm_runtime_resume(dev); |
| 660 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 661 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 662 | |
| 663 | if (genpd->prepared_count++ == 0) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 664 | genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF; |
| 665 | |
| 666 | genpd_release_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 667 | |
| 668 | if (genpd->suspend_power_off) { |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 669 | pm_runtime_put_noidle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /* |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 674 | * The PM domain must be in the GPD_STATE_ACTIVE state at this point, |
| 675 | * so pm_genpd_poweron() will return immediately, but if the device |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 676 | * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 677 | * to make it operational. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 678 | */ |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 679 | pm_runtime_resume(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 680 | __pm_runtime_disable(dev, false); |
| 681 | |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 682 | ret = pm_generic_prepare(dev); |
| 683 | if (ret) { |
| 684 | mutex_lock(&genpd->lock); |
| 685 | |
| 686 | if (--genpd->prepared_count == 0) |
| 687 | genpd->suspend_power_off = false; |
| 688 | |
| 689 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 690 | pm_runtime_enable(dev); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 691 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 692 | |
| 693 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 694 | return ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /** |
| 698 | * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain. |
| 699 | * @dev: Device to suspend. |
| 700 | * |
| 701 | * Suspend a device under the assumption that its pm_domain field points to the |
| 702 | * domain member of an object of type struct generic_pm_domain representing |
| 703 | * a PM domain consisting of I/O devices. |
| 704 | */ |
| 705 | static int pm_genpd_suspend(struct device *dev) |
| 706 | { |
| 707 | struct generic_pm_domain *genpd; |
| 708 | |
| 709 | dev_dbg(dev, "%s()\n", __func__); |
| 710 | |
| 711 | genpd = dev_to_genpd(dev); |
| 712 | if (IS_ERR(genpd)) |
| 713 | return -EINVAL; |
| 714 | |
| 715 | return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain. |
| 720 | * @dev: Device to suspend. |
| 721 | * |
| 722 | * Carry out a late suspend of a device under the assumption that its |
| 723 | * pm_domain field points to the domain member of an object of type |
| 724 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 725 | */ |
| 726 | static int pm_genpd_suspend_noirq(struct device *dev) |
| 727 | { |
| 728 | struct generic_pm_domain *genpd; |
| 729 | int ret; |
| 730 | |
| 731 | dev_dbg(dev, "%s()\n", __func__); |
| 732 | |
| 733 | genpd = dev_to_genpd(dev); |
| 734 | if (IS_ERR(genpd)) |
| 735 | return -EINVAL; |
| 736 | |
| 737 | if (genpd->suspend_power_off) |
| 738 | return 0; |
| 739 | |
| 740 | ret = pm_generic_suspend_noirq(dev); |
| 741 | if (ret) |
| 742 | return ret; |
| 743 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 744 | if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)) |
Rafael J. Wysocki | d4f2d87 | 2011-07-01 22:13:29 +0200 | [diff] [blame] | 745 | return 0; |
| 746 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 747 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 751 | * guaranteed that this function will never run twice in parallel for |
| 752 | * the same PM domain, so it is not necessary to use locking here. |
| 753 | */ |
| 754 | genpd->suspended_count++; |
| 755 | pm_genpd_sync_poweroff(genpd); |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain. |
| 762 | * @dev: Device to resume. |
| 763 | * |
| 764 | * Carry out an early resume of a device under the assumption that its |
| 765 | * pm_domain field points to the domain member of an object of type |
| 766 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 767 | * devices. |
| 768 | */ |
| 769 | static int pm_genpd_resume_noirq(struct device *dev) |
| 770 | { |
| 771 | struct generic_pm_domain *genpd; |
| 772 | |
| 773 | dev_dbg(dev, "%s()\n", __func__); |
| 774 | |
| 775 | genpd = dev_to_genpd(dev); |
| 776 | if (IS_ERR(genpd)) |
| 777 | return -EINVAL; |
| 778 | |
| 779 | if (genpd->suspend_power_off) |
| 780 | return 0; |
| 781 | |
| 782 | /* |
| 783 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 784 | * guaranteed that this function will never run twice in parallel for |
| 785 | * the same PM domain, so it is not necessary to use locking here. |
| 786 | */ |
| 787 | pm_genpd_poweron(genpd); |
| 788 | genpd->suspended_count--; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 789 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 790 | |
| 791 | return pm_generic_resume_noirq(dev); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * pm_genpd_resume - Resume a device belonging to an I/O power domain. |
| 796 | * @dev: Device to resume. |
| 797 | * |
| 798 | * Resume a device under the assumption that its pm_domain field points to the |
| 799 | * domain member of an object of type struct generic_pm_domain representing |
| 800 | * a power domain consisting of I/O devices. |
| 801 | */ |
| 802 | static int pm_genpd_resume(struct device *dev) |
| 803 | { |
| 804 | struct generic_pm_domain *genpd; |
| 805 | |
| 806 | dev_dbg(dev, "%s()\n", __func__); |
| 807 | |
| 808 | genpd = dev_to_genpd(dev); |
| 809 | if (IS_ERR(genpd)) |
| 810 | return -EINVAL; |
| 811 | |
| 812 | return genpd->suspend_power_off ? 0 : pm_generic_resume(dev); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * pm_genpd_freeze - Freeze a device belonging to an I/O power domain. |
| 817 | * @dev: Device to freeze. |
| 818 | * |
| 819 | * Freeze a device under the assumption that its pm_domain field points to the |
| 820 | * domain member of an object of type struct generic_pm_domain representing |
| 821 | * a power domain consisting of I/O devices. |
| 822 | */ |
| 823 | static int pm_genpd_freeze(struct device *dev) |
| 824 | { |
| 825 | struct generic_pm_domain *genpd; |
| 826 | |
| 827 | dev_dbg(dev, "%s()\n", __func__); |
| 828 | |
| 829 | genpd = dev_to_genpd(dev); |
| 830 | if (IS_ERR(genpd)) |
| 831 | return -EINVAL; |
| 832 | |
| 833 | return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain. |
| 838 | * @dev: Device to freeze. |
| 839 | * |
| 840 | * Carry out a late freeze of a device under the assumption that its |
| 841 | * pm_domain field points to the domain member of an object of type |
| 842 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 843 | * devices. |
| 844 | */ |
| 845 | static int pm_genpd_freeze_noirq(struct device *dev) |
| 846 | { |
| 847 | struct generic_pm_domain *genpd; |
| 848 | int ret; |
| 849 | |
| 850 | dev_dbg(dev, "%s()\n", __func__); |
| 851 | |
| 852 | genpd = dev_to_genpd(dev); |
| 853 | if (IS_ERR(genpd)) |
| 854 | return -EINVAL; |
| 855 | |
| 856 | if (genpd->suspend_power_off) |
| 857 | return 0; |
| 858 | |
| 859 | ret = pm_generic_freeze_noirq(dev); |
| 860 | if (ret) |
| 861 | return ret; |
| 862 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 863 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain. |
| 870 | * @dev: Device to thaw. |
| 871 | * |
| 872 | * Carry out an early thaw of a device under the assumption that its |
| 873 | * pm_domain field points to the domain member of an object of type |
| 874 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 875 | * devices. |
| 876 | */ |
| 877 | static int pm_genpd_thaw_noirq(struct device *dev) |
| 878 | { |
| 879 | struct generic_pm_domain *genpd; |
| 880 | |
| 881 | dev_dbg(dev, "%s()\n", __func__); |
| 882 | |
| 883 | genpd = dev_to_genpd(dev); |
| 884 | if (IS_ERR(genpd)) |
| 885 | return -EINVAL; |
| 886 | |
| 887 | if (genpd->suspend_power_off) |
| 888 | return 0; |
| 889 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 890 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 891 | |
| 892 | return pm_generic_thaw_noirq(dev); |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * pm_genpd_thaw - Thaw a device belonging to an I/O power domain. |
| 897 | * @dev: Device to thaw. |
| 898 | * |
| 899 | * Thaw a device under the assumption that its pm_domain field points to the |
| 900 | * domain member of an object of type struct generic_pm_domain representing |
| 901 | * a power domain consisting of I/O devices. |
| 902 | */ |
| 903 | static int pm_genpd_thaw(struct device *dev) |
| 904 | { |
| 905 | struct generic_pm_domain *genpd; |
| 906 | |
| 907 | dev_dbg(dev, "%s()\n", __func__); |
| 908 | |
| 909 | genpd = dev_to_genpd(dev); |
| 910 | if (IS_ERR(genpd)) |
| 911 | return -EINVAL; |
| 912 | |
| 913 | return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev); |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain. |
| 918 | * @dev: Device to suspend. |
| 919 | * |
| 920 | * Power off a device under the assumption that its pm_domain field points to |
| 921 | * the domain member of an object of type struct generic_pm_domain representing |
| 922 | * a PM domain consisting of I/O devices. |
| 923 | */ |
| 924 | static int pm_genpd_dev_poweroff(struct device *dev) |
| 925 | { |
| 926 | struct generic_pm_domain *genpd; |
| 927 | |
| 928 | dev_dbg(dev, "%s()\n", __func__); |
| 929 | |
| 930 | genpd = dev_to_genpd(dev); |
| 931 | if (IS_ERR(genpd)) |
| 932 | return -EINVAL; |
| 933 | |
| 934 | return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev); |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain. |
| 939 | * @dev: Device to suspend. |
| 940 | * |
| 941 | * Carry out a late powering off of a device under the assumption that its |
| 942 | * pm_domain field points to the domain member of an object of type |
| 943 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 944 | */ |
| 945 | static int pm_genpd_dev_poweroff_noirq(struct device *dev) |
| 946 | { |
| 947 | struct generic_pm_domain *genpd; |
| 948 | int ret; |
| 949 | |
| 950 | dev_dbg(dev, "%s()\n", __func__); |
| 951 | |
| 952 | genpd = dev_to_genpd(dev); |
| 953 | if (IS_ERR(genpd)) |
| 954 | return -EINVAL; |
| 955 | |
| 956 | if (genpd->suspend_power_off) |
| 957 | return 0; |
| 958 | |
| 959 | ret = pm_generic_poweroff_noirq(dev); |
| 960 | if (ret) |
| 961 | return ret; |
| 962 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 963 | if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)) |
Rafael J. Wysocki | d4f2d87 | 2011-07-01 22:13:29 +0200 | [diff] [blame] | 964 | return 0; |
| 965 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 966 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 967 | |
| 968 | /* |
| 969 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 970 | * guaranteed that this function will never run twice in parallel for |
| 971 | * the same PM domain, so it is not necessary to use locking here. |
| 972 | */ |
| 973 | genpd->suspended_count++; |
| 974 | pm_genpd_sync_poweroff(genpd); |
| 975 | |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | /** |
| 980 | * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain. |
| 981 | * @dev: Device to resume. |
| 982 | * |
| 983 | * Carry out an early restore of a device under the assumption that its |
| 984 | * pm_domain field points to the domain member of an object of type |
| 985 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 986 | * devices. |
| 987 | */ |
| 988 | static int pm_genpd_restore_noirq(struct device *dev) |
| 989 | { |
| 990 | struct generic_pm_domain *genpd; |
| 991 | |
| 992 | dev_dbg(dev, "%s()\n", __func__); |
| 993 | |
| 994 | genpd = dev_to_genpd(dev); |
| 995 | if (IS_ERR(genpd)) |
| 996 | return -EINVAL; |
| 997 | |
| 998 | /* |
| 999 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 1000 | * guaranteed that this function will never run twice in parallel for |
| 1001 | * the same PM domain, so it is not necessary to use locking here. |
| 1002 | */ |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1003 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1004 | if (genpd->suspend_power_off) { |
| 1005 | /* |
| 1006 | * The boot kernel might put the domain into the power on state, |
| 1007 | * so make sure it really is powered off. |
| 1008 | */ |
| 1009 | if (genpd->power_off) |
| 1010 | genpd->power_off(genpd); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | pm_genpd_poweron(genpd); |
| 1015 | genpd->suspended_count--; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1016 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1017 | |
| 1018 | return pm_generic_restore_noirq(dev); |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * pm_genpd_restore - Restore a device belonging to an I/O power domain. |
| 1023 | * @dev: Device to resume. |
| 1024 | * |
| 1025 | * Restore a device under the assumption that its pm_domain field points to the |
| 1026 | * domain member of an object of type struct generic_pm_domain representing |
| 1027 | * a power domain consisting of I/O devices. |
| 1028 | */ |
| 1029 | static int pm_genpd_restore(struct device *dev) |
| 1030 | { |
| 1031 | struct generic_pm_domain *genpd; |
| 1032 | |
| 1033 | dev_dbg(dev, "%s()\n", __func__); |
| 1034 | |
| 1035 | genpd = dev_to_genpd(dev); |
| 1036 | if (IS_ERR(genpd)) |
| 1037 | return -EINVAL; |
| 1038 | |
| 1039 | return genpd->suspend_power_off ? 0 : pm_generic_restore(dev); |
| 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * pm_genpd_complete - Complete power transition of a device in a power domain. |
| 1044 | * @dev: Device to complete the transition of. |
| 1045 | * |
| 1046 | * Complete a power transition of a device (during a system-wide power |
| 1047 | * transition) under the assumption that its pm_domain field points to the |
| 1048 | * domain member of an object of type struct generic_pm_domain representing |
| 1049 | * a power domain consisting of I/O devices. |
| 1050 | */ |
| 1051 | static void pm_genpd_complete(struct device *dev) |
| 1052 | { |
| 1053 | struct generic_pm_domain *genpd; |
| 1054 | bool run_complete; |
| 1055 | |
| 1056 | dev_dbg(dev, "%s()\n", __func__); |
| 1057 | |
| 1058 | genpd = dev_to_genpd(dev); |
| 1059 | if (IS_ERR(genpd)) |
| 1060 | return; |
| 1061 | |
| 1062 | mutex_lock(&genpd->lock); |
| 1063 | |
| 1064 | run_complete = !genpd->suspend_power_off; |
| 1065 | if (--genpd->prepared_count == 0) |
| 1066 | genpd->suspend_power_off = false; |
| 1067 | |
| 1068 | mutex_unlock(&genpd->lock); |
| 1069 | |
| 1070 | if (run_complete) { |
| 1071 | pm_generic_complete(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 1072 | pm_runtime_set_active(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1073 | pm_runtime_enable(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 1074 | pm_runtime_idle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | #else |
| 1079 | |
| 1080 | #define pm_genpd_prepare NULL |
| 1081 | #define pm_genpd_suspend NULL |
| 1082 | #define pm_genpd_suspend_noirq NULL |
| 1083 | #define pm_genpd_resume_noirq NULL |
| 1084 | #define pm_genpd_resume NULL |
| 1085 | #define pm_genpd_freeze NULL |
| 1086 | #define pm_genpd_freeze_noirq NULL |
| 1087 | #define pm_genpd_thaw_noirq NULL |
| 1088 | #define pm_genpd_thaw NULL |
| 1089 | #define pm_genpd_dev_poweroff_noirq NULL |
| 1090 | #define pm_genpd_dev_poweroff NULL |
| 1091 | #define pm_genpd_restore_noirq NULL |
| 1092 | #define pm_genpd_restore NULL |
| 1093 | #define pm_genpd_complete NULL |
| 1094 | |
| 1095 | #endif /* CONFIG_PM_SLEEP */ |
| 1096 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1097 | /** |
| 1098 | * pm_genpd_add_device - Add a device to an I/O PM domain. |
| 1099 | * @genpd: PM domain to add the device to. |
| 1100 | * @dev: Device to be added. |
| 1101 | */ |
| 1102 | int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev) |
| 1103 | { |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1104 | struct generic_pm_domain_data *gpd_data; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1105 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1106 | int ret = 0; |
| 1107 | |
| 1108 | dev_dbg(dev, "%s()\n", __func__); |
| 1109 | |
| 1110 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1111 | return -EINVAL; |
| 1112 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1113 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1114 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1115 | if (genpd->status == GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1116 | ret = -EINVAL; |
| 1117 | goto out; |
| 1118 | } |
| 1119 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1120 | if (genpd->prepared_count > 0) { |
| 1121 | ret = -EAGAIN; |
| 1122 | goto out; |
| 1123 | } |
| 1124 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1125 | list_for_each_entry(pdd, &genpd->dev_list, list_node) |
| 1126 | if (pdd->dev == dev) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1127 | ret = -EINVAL; |
| 1128 | goto out; |
| 1129 | } |
| 1130 | |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1131 | gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL); |
| 1132 | if (!gpd_data) { |
| 1133 | ret = -ENOMEM; |
| 1134 | goto out; |
| 1135 | } |
| 1136 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1137 | genpd->device_count++; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1138 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1139 | dev->pm_domain = &genpd->domain; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1140 | dev_pm_get_subsys_data(dev); |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1141 | dev->power.subsys_data->domain_data = &gpd_data->base; |
| 1142 | gpd_data->base.dev = dev; |
| 1143 | gpd_data->need_restore = false; |
| 1144 | list_add_tail(&gpd_data->base.list_node, &genpd->dev_list); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1145 | |
| 1146 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1147 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1148 | |
| 1149 | return ret; |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * pm_genpd_remove_device - Remove a device from an I/O PM domain. |
| 1154 | * @genpd: PM domain to remove the device from. |
| 1155 | * @dev: Device to be removed. |
| 1156 | */ |
| 1157 | int pm_genpd_remove_device(struct generic_pm_domain *genpd, |
| 1158 | struct device *dev) |
| 1159 | { |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1160 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1161 | int ret = -EINVAL; |
| 1162 | |
| 1163 | dev_dbg(dev, "%s()\n", __func__); |
| 1164 | |
| 1165 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1166 | return -EINVAL; |
| 1167 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1168 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1169 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1170 | if (genpd->prepared_count > 0) { |
| 1171 | ret = -EAGAIN; |
| 1172 | goto out; |
| 1173 | } |
| 1174 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1175 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 1176 | if (pdd->dev != dev) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1177 | continue; |
| 1178 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1179 | list_del_init(&pdd->list_node); |
| 1180 | pdd->dev = NULL; |
| 1181 | dev_pm_put_subsys_data(dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1182 | dev->pm_domain = NULL; |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1183 | kfree(to_gpd_data(pdd)); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1184 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1185 | genpd->device_count--; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1186 | |
| 1187 | ret = 0; |
| 1188 | break; |
| 1189 | } |
| 1190 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1191 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1192 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1193 | |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | /** |
| 1198 | * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain. |
| 1199 | * @genpd: Master PM domain to add the subdomain to. |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1200 | * @subdomain: Subdomain to be added. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1201 | */ |
| 1202 | int pm_genpd_add_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1203 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1204 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1205 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1206 | int ret = 0; |
| 1207 | |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1208 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1209 | return -EINVAL; |
| 1210 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1211 | start: |
| 1212 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1213 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1214 | |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1215 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1216 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1217 | mutex_unlock(&subdomain->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1218 | genpd_release_lock(genpd); |
| 1219 | goto start; |
| 1220 | } |
| 1221 | |
| 1222 | if (genpd->status == GPD_STATE_POWER_OFF |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1223 | && subdomain->status != GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1224 | ret = -EINVAL; |
| 1225 | goto out; |
| 1226 | } |
| 1227 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1228 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1229 | if (link->slave == subdomain && link->master == genpd) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1230 | ret = -EINVAL; |
| 1231 | goto out; |
| 1232 | } |
| 1233 | } |
| 1234 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1235 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 1236 | if (!link) { |
| 1237 | ret = -ENOMEM; |
| 1238 | goto out; |
| 1239 | } |
| 1240 | link->master = genpd; |
| 1241 | list_add_tail(&link->master_node, &genpd->master_links); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1242 | link->slave = subdomain; |
| 1243 | list_add_tail(&link->slave_node, &subdomain->slave_links); |
| 1244 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1245 | genpd_sd_counter_inc(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1246 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1247 | out: |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1248 | mutex_unlock(&subdomain->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1249 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1250 | |
| 1251 | return ret; |
| 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain. |
| 1256 | * @genpd: Master PM domain to remove the subdomain from. |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1257 | * @subdomain: Subdomain to be removed. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1258 | */ |
| 1259 | int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1260 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1261 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1262 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1263 | int ret = -EINVAL; |
| 1264 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1265 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1266 | return -EINVAL; |
| 1267 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1268 | start: |
| 1269 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1270 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1271 | list_for_each_entry(link, &genpd->master_links, master_node) { |
| 1272 | if (link->slave != subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1273 | continue; |
| 1274 | |
| 1275 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
| 1276 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1277 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1278 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1279 | mutex_unlock(&subdomain->lock); |
| 1280 | genpd_release_lock(genpd); |
| 1281 | goto start; |
| 1282 | } |
| 1283 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1284 | list_del(&link->master_node); |
| 1285 | list_del(&link->slave_node); |
| 1286 | kfree(link); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1287 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1288 | genpd_sd_counter_dec(genpd); |
| 1289 | |
| 1290 | mutex_unlock(&subdomain->lock); |
| 1291 | |
| 1292 | ret = 0; |
| 1293 | break; |
| 1294 | } |
| 1295 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1296 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1297 | |
| 1298 | return ret; |
| 1299 | } |
| 1300 | |
| 1301 | /** |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1302 | * pm_genpd_add_callbacks - Add PM domain callbacks to a given device. |
| 1303 | * @dev: Device to add the callbacks to. |
| 1304 | * @ops: Set of callbacks to add. |
| 1305 | */ |
| 1306 | int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops) |
| 1307 | { |
| 1308 | struct pm_domain_data *pdd; |
| 1309 | int ret = 0; |
| 1310 | |
| 1311 | if (!(dev && dev->power.subsys_data && ops)) |
| 1312 | return -EINVAL; |
| 1313 | |
| 1314 | pm_runtime_disable(dev); |
| 1315 | device_pm_lock(); |
| 1316 | |
| 1317 | pdd = dev->power.subsys_data->domain_data; |
| 1318 | if (pdd) { |
| 1319 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
| 1320 | |
| 1321 | gpd_data->ops = *ops; |
| 1322 | } else { |
| 1323 | ret = -EINVAL; |
| 1324 | } |
| 1325 | |
| 1326 | device_pm_unlock(); |
| 1327 | pm_runtime_enable(dev); |
| 1328 | |
| 1329 | return ret; |
| 1330 | } |
| 1331 | EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks); |
| 1332 | |
| 1333 | /** |
| 1334 | * pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device. |
| 1335 | * @dev: Device to remove the callbacks from. |
| 1336 | */ |
| 1337 | int pm_genpd_remove_callbacks(struct device *dev) |
| 1338 | { |
| 1339 | struct pm_domain_data *pdd; |
| 1340 | int ret = 0; |
| 1341 | |
| 1342 | if (!(dev && dev->power.subsys_data)) |
| 1343 | return -EINVAL; |
| 1344 | |
| 1345 | pm_runtime_disable(dev); |
| 1346 | device_pm_lock(); |
| 1347 | |
| 1348 | pdd = dev->power.subsys_data->domain_data; |
| 1349 | if (pdd) { |
| 1350 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
| 1351 | |
| 1352 | gpd_data->ops = (struct gpd_dev_ops){ 0 }; |
| 1353 | } else { |
| 1354 | ret = -EINVAL; |
| 1355 | } |
| 1356 | |
| 1357 | device_pm_unlock(); |
| 1358 | pm_runtime_enable(dev); |
| 1359 | |
| 1360 | return ret; |
| 1361 | } |
| 1362 | EXPORT_SYMBOL_GPL(pm_genpd_remove_callbacks); |
| 1363 | |
| 1364 | /** |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame^] | 1365 | * pm_genpd_default_save_state - Default "save device state" for PM domians. |
| 1366 | * @dev: Device to handle. |
| 1367 | */ |
| 1368 | static int pm_genpd_default_save_state(struct device *dev) |
| 1369 | { |
| 1370 | int (*cb)(struct device *__dev); |
| 1371 | struct device_driver *drv = dev->driver; |
| 1372 | |
| 1373 | cb = dev_gpd_data(dev)->ops.save_state; |
| 1374 | if (cb) |
| 1375 | return cb(dev); |
| 1376 | |
| 1377 | if (drv && drv->pm && drv->pm->runtime_suspend) |
| 1378 | return drv->pm->runtime_suspend(dev); |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
| 1383 | /** |
| 1384 | * pm_genpd_default_restore_state - Default PM domians "restore device state". |
| 1385 | * @dev: Device to handle. |
| 1386 | */ |
| 1387 | static int pm_genpd_default_restore_state(struct device *dev) |
| 1388 | { |
| 1389 | int (*cb)(struct device *__dev); |
| 1390 | struct device_driver *drv = dev->driver; |
| 1391 | |
| 1392 | cb = dev_gpd_data(dev)->ops.restore_state; |
| 1393 | if (cb) |
| 1394 | return cb(dev); |
| 1395 | |
| 1396 | if (drv && drv->pm && drv->pm->runtime_resume) |
| 1397 | return drv->pm->runtime_resume(dev); |
| 1398 | |
| 1399 | return 0; |
| 1400 | } |
| 1401 | |
| 1402 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1403 | * pm_genpd_init - Initialize a generic I/O PM domain object. |
| 1404 | * @genpd: PM domain object to initialize. |
| 1405 | * @gov: PM domain governor to associate with the domain (may be NULL). |
| 1406 | * @is_off: Initial value of the domain's power_is_off field. |
| 1407 | */ |
| 1408 | void pm_genpd_init(struct generic_pm_domain *genpd, |
| 1409 | struct dev_power_governor *gov, bool is_off) |
| 1410 | { |
| 1411 | if (IS_ERR_OR_NULL(genpd)) |
| 1412 | return; |
| 1413 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1414 | INIT_LIST_HEAD(&genpd->master_links); |
| 1415 | INIT_LIST_HEAD(&genpd->slave_links); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1416 | INIT_LIST_HEAD(&genpd->dev_list); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1417 | mutex_init(&genpd->lock); |
| 1418 | genpd->gov = gov; |
| 1419 | INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn); |
| 1420 | genpd->in_progress = 0; |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1421 | atomic_set(&genpd->sd_count, 0); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1422 | genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE; |
| 1423 | init_waitqueue_head(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 1424 | genpd->poweroff_task = NULL; |
| 1425 | genpd->resume_count = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1426 | genpd->device_count = 0; |
| 1427 | genpd->suspended_count = 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1428 | genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend; |
| 1429 | genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume; |
| 1430 | genpd->domain.ops.runtime_idle = pm_generic_runtime_idle; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1431 | genpd->domain.ops.prepare = pm_genpd_prepare; |
| 1432 | genpd->domain.ops.suspend = pm_genpd_suspend; |
| 1433 | genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq; |
| 1434 | genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq; |
| 1435 | genpd->domain.ops.resume = pm_genpd_resume; |
| 1436 | genpd->domain.ops.freeze = pm_genpd_freeze; |
| 1437 | genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq; |
| 1438 | genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq; |
| 1439 | genpd->domain.ops.thaw = pm_genpd_thaw; |
| 1440 | genpd->domain.ops.poweroff = pm_genpd_dev_poweroff; |
| 1441 | genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq; |
| 1442 | genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq; |
| 1443 | genpd->domain.ops.restore = pm_genpd_restore; |
| 1444 | genpd->domain.ops.complete = pm_genpd_complete; |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame^] | 1445 | genpd->dev_ops.save_state = pm_genpd_default_save_state; |
| 1446 | genpd->dev_ops.restore_state = pm_genpd_default_restore_state; |
Rafael J. Wysocki | 5125bbf38 | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 1447 | mutex_lock(&gpd_list_lock); |
| 1448 | list_add(&genpd->gpd_list_node, &gpd_list); |
| 1449 | mutex_unlock(&gpd_list_lock); |
| 1450 | } |