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 | 5125bbf | 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 | |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 41 | struct generic_pm_domain *dev_to_genpd(struct device *dev) |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 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 | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 439 | bool (*stop_ok)(struct device *__dev); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 440 | int ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 441 | |
| 442 | dev_dbg(dev, "%s()\n", __func__); |
| 443 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 444 | genpd = dev_to_genpd(dev); |
| 445 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 446 | return -EINVAL; |
| 447 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 448 | might_sleep_if(!genpd->dev_irq_safe); |
| 449 | |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 450 | stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL; |
| 451 | if (stop_ok && !stop_ok(dev)) |
| 452 | return -EBUSY; |
| 453 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 454 | ret = genpd_stop_dev(genpd, dev); |
| 455 | if (ret) |
| 456 | return ret; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 457 | |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 458 | pm_runtime_update_max_time_suspended(dev, |
| 459 | dev_gpd_data(dev)->td.start_latency_ns); |
| 460 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 461 | /* |
| 462 | * If power.irq_safe is set, this routine will be run with interrupts |
| 463 | * off, so it can't use mutexes. |
| 464 | */ |
| 465 | if (dev->power.irq_safe) |
| 466 | return 0; |
| 467 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 468 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 469 | genpd->in_progress++; |
| 470 | pm_genpd_poweroff(genpd); |
| 471 | genpd->in_progress--; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 472 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 478 | * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain. |
| 479 | * @dev: Device to resume. |
| 480 | * |
| 481 | * Carry out a runtime resume of a device under the assumption that its |
| 482 | * pm_domain field points to the domain member of an object of type |
| 483 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 484 | */ |
| 485 | static int pm_genpd_runtime_resume(struct device *dev) |
| 486 | { |
| 487 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 488 | DEFINE_WAIT(wait); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 489 | int ret; |
| 490 | |
| 491 | dev_dbg(dev, "%s()\n", __func__); |
| 492 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 493 | genpd = dev_to_genpd(dev); |
| 494 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 495 | return -EINVAL; |
| 496 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 497 | might_sleep_if(!genpd->dev_irq_safe); |
| 498 | |
| 499 | /* If power.irq_safe, the PM domain is never powered off. */ |
| 500 | if (dev->power.irq_safe) |
| 501 | goto out; |
| 502 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 503 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 504 | ret = __pm_genpd_poweron(genpd); |
| 505 | if (ret) { |
| 506 | mutex_unlock(&genpd->lock); |
| 507 | return ret; |
| 508 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 509 | genpd->status = GPD_STATE_BUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 510 | genpd->resume_count++; |
| 511 | for (;;) { |
| 512 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 513 | TASK_UNINTERRUPTIBLE); |
| 514 | /* |
| 515 | * If current is the powering off task, we have been called |
| 516 | * reentrantly from one of the device callbacks, so we should |
| 517 | * not wait. |
| 518 | */ |
| 519 | if (!genpd->poweroff_task || genpd->poweroff_task == current) |
| 520 | break; |
| 521 | mutex_unlock(&genpd->lock); |
| 522 | |
| 523 | schedule(); |
| 524 | |
| 525 | mutex_lock(&genpd->lock); |
| 526 | } |
| 527 | finish_wait(&genpd->status_wait_queue, &wait); |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 528 | __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 529 | genpd->resume_count--; |
| 530 | genpd_set_active(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 531 | wake_up_all(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 532 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 533 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 534 | out: |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 535 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
Rafael J. Wysocki | 17f2ae7 | 2011-08-14 13:34:31 +0200 | [diff] [blame] | 540 | /** |
| 541 | * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use. |
| 542 | */ |
| 543 | void pm_genpd_poweroff_unused(void) |
| 544 | { |
| 545 | struct generic_pm_domain *genpd; |
| 546 | |
| 547 | mutex_lock(&gpd_list_lock); |
| 548 | |
| 549 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) |
| 550 | genpd_queue_power_off_work(genpd); |
| 551 | |
| 552 | mutex_unlock(&gpd_list_lock); |
| 553 | } |
| 554 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 555 | #else |
| 556 | |
| 557 | static inline void genpd_power_off_work_fn(struct work_struct *work) {} |
| 558 | |
| 559 | #define pm_genpd_runtime_suspend NULL |
| 560 | #define pm_genpd_runtime_resume NULL |
| 561 | |
| 562 | #endif /* CONFIG_PM_RUNTIME */ |
| 563 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 564 | #ifdef CONFIG_PM_SLEEP |
| 565 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 566 | static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd, |
| 567 | struct device *dev) |
| 568 | { |
| 569 | return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev); |
| 570 | } |
| 571 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 572 | static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 573 | { |
| 574 | return GENPD_DEV_CALLBACK(genpd, int, suspend, dev); |
| 575 | } |
| 576 | |
| 577 | static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev) |
| 578 | { |
| 579 | return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev); |
| 580 | } |
| 581 | |
| 582 | static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev) |
| 583 | { |
| 584 | return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev); |
| 585 | } |
| 586 | |
| 587 | static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 588 | { |
| 589 | return GENPD_DEV_CALLBACK(genpd, int, resume, dev); |
| 590 | } |
| 591 | |
| 592 | static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 593 | { |
| 594 | return GENPD_DEV_CALLBACK(genpd, int, freeze, dev); |
| 595 | } |
| 596 | |
| 597 | static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev) |
| 598 | { |
| 599 | return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev); |
| 600 | } |
| 601 | |
| 602 | static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev) |
| 603 | { |
| 604 | return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev); |
| 605 | } |
| 606 | |
| 607 | static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 608 | { |
| 609 | return GENPD_DEV_CALLBACK(genpd, int, thaw, dev); |
| 610 | } |
| 611 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 612 | /** |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 613 | * 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] | 614 | * @genpd: PM domain to power off, if possible. |
| 615 | * |
| 616 | * 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] | 617 | * 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] | 618 | * |
| 619 | * This function is only called in "noirq" stages of system power transitions, |
| 620 | * so it need not acquire locks (all of the "noirq" callbacks are executed |
| 621 | * sequentially, so it is guaranteed that it will never run twice in parallel). |
| 622 | */ |
| 623 | static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd) |
| 624 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 625 | struct gpd_link *link; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 626 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 627 | if (genpd->status == GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 628 | return; |
| 629 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 630 | if (genpd->suspended_count != genpd->device_count |
| 631 | || atomic_read(&genpd->sd_count) > 0) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 632 | return; |
| 633 | |
| 634 | if (genpd->power_off) |
| 635 | genpd->power_off(genpd); |
| 636 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 637 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 638 | |
| 639 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 640 | genpd_sd_counter_dec(link->master); |
| 641 | pm_genpd_sync_poweroff(link->master); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 646 | * resume_needed - Check whether to resume a device before system suspend. |
| 647 | * @dev: Device to check. |
| 648 | * @genpd: PM domain the device belongs to. |
| 649 | * |
| 650 | * There are two cases in which a device that can wake up the system from sleep |
| 651 | * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled |
| 652 | * to wake up the system and it has to remain active for this purpose while the |
| 653 | * system is in the sleep state and (2) if the device is not enabled to wake up |
| 654 | * the system from sleep states and it generally doesn't generate wakeup signals |
| 655 | * by itself (those signals are generated on its behalf by other parts of the |
| 656 | * system). In the latter case it may be necessary to reconfigure the device's |
| 657 | * wakeup settings during system suspend, because it may have been set up to |
| 658 | * signal remote wakeup from the system's working state as needed by runtime PM. |
| 659 | * Return 'true' in either of the above cases. |
| 660 | */ |
| 661 | static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd) |
| 662 | { |
| 663 | bool active_wakeup; |
| 664 | |
| 665 | if (!device_can_wakeup(dev)) |
| 666 | return false; |
| 667 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 668 | active_wakeup = genpd_dev_active_wakeup(genpd, dev); |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 669 | return device_may_wakeup(dev) ? active_wakeup : !active_wakeup; |
| 670 | } |
| 671 | |
| 672 | /** |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 673 | * pm_genpd_prepare - Start power transition of a device in a PM domain. |
| 674 | * @dev: Device to start the transition of. |
| 675 | * |
| 676 | * Start a power transition of a device (during a system-wide power transition) |
| 677 | * under the assumption that its pm_domain field points to the domain member of |
| 678 | * an object of type struct generic_pm_domain representing a PM domain |
| 679 | * consisting of I/O devices. |
| 680 | */ |
| 681 | static int pm_genpd_prepare(struct device *dev) |
| 682 | { |
| 683 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 684 | int ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 685 | |
| 686 | dev_dbg(dev, "%s()\n", __func__); |
| 687 | |
| 688 | genpd = dev_to_genpd(dev); |
| 689 | if (IS_ERR(genpd)) |
| 690 | return -EINVAL; |
| 691 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 692 | /* |
| 693 | * If a wakeup request is pending for the device, it should be woken up |
| 694 | * at this point and a system wakeup event should be reported if it's |
| 695 | * set up to wake up the system from sleep states. |
| 696 | */ |
| 697 | pm_runtime_get_noresume(dev); |
| 698 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) |
| 699 | pm_wakeup_event(dev, 0); |
| 700 | |
| 701 | if (pm_wakeup_pending()) { |
| 702 | pm_runtime_put_sync(dev); |
| 703 | return -EBUSY; |
| 704 | } |
| 705 | |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 706 | if (resume_needed(dev, genpd)) |
| 707 | pm_runtime_resume(dev); |
| 708 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 709 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 710 | |
| 711 | if (genpd->prepared_count++ == 0) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 712 | genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF; |
| 713 | |
| 714 | genpd_release_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 715 | |
| 716 | if (genpd->suspend_power_off) { |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 717 | pm_runtime_put_noidle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | /* |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 722 | * The PM domain must be in the GPD_STATE_ACTIVE state at this point, |
| 723 | * so pm_genpd_poweron() will return immediately, but if the device |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 724 | * 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] | 725 | * to make it operational. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 726 | */ |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 727 | pm_runtime_resume(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 728 | __pm_runtime_disable(dev, false); |
| 729 | |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 730 | ret = pm_generic_prepare(dev); |
| 731 | if (ret) { |
| 732 | mutex_lock(&genpd->lock); |
| 733 | |
| 734 | if (--genpd->prepared_count == 0) |
| 735 | genpd->suspend_power_off = false; |
| 736 | |
| 737 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 738 | pm_runtime_enable(dev); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 739 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 740 | |
| 741 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 742 | return ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /** |
| 746 | * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain. |
| 747 | * @dev: Device to suspend. |
| 748 | * |
| 749 | * Suspend a device under the assumption that its pm_domain field points to the |
| 750 | * domain member of an object of type struct generic_pm_domain representing |
| 751 | * a PM domain consisting of I/O devices. |
| 752 | */ |
| 753 | static int pm_genpd_suspend(struct device *dev) |
| 754 | { |
| 755 | struct generic_pm_domain *genpd; |
| 756 | |
| 757 | dev_dbg(dev, "%s()\n", __func__); |
| 758 | |
| 759 | genpd = dev_to_genpd(dev); |
| 760 | if (IS_ERR(genpd)) |
| 761 | return -EINVAL; |
| 762 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 763 | return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /** |
| 767 | * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain. |
| 768 | * @dev: Device to suspend. |
| 769 | * |
| 770 | * Carry out a late suspend of a device under the assumption that its |
| 771 | * pm_domain field points to the domain member of an object of type |
| 772 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 773 | */ |
| 774 | static int pm_genpd_suspend_noirq(struct device *dev) |
| 775 | { |
| 776 | struct generic_pm_domain *genpd; |
| 777 | int ret; |
| 778 | |
| 779 | dev_dbg(dev, "%s()\n", __func__); |
| 780 | |
| 781 | genpd = dev_to_genpd(dev); |
| 782 | if (IS_ERR(genpd)) |
| 783 | return -EINVAL; |
| 784 | |
| 785 | if (genpd->suspend_power_off) |
| 786 | return 0; |
| 787 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 788 | ret = genpd_suspend_late(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 789 | if (ret) |
| 790 | return ret; |
| 791 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 792 | if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)) |
Rafael J. Wysocki | d4f2d87 | 2011-07-01 22:13:29 +0200 | [diff] [blame] | 793 | return 0; |
| 794 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 795 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 799 | * guaranteed that this function will never run twice in parallel for |
| 800 | * the same PM domain, so it is not necessary to use locking here. |
| 801 | */ |
| 802 | genpd->suspended_count++; |
| 803 | pm_genpd_sync_poweroff(genpd); |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain. |
| 810 | * @dev: Device to resume. |
| 811 | * |
| 812 | * Carry out an early resume of a device under the assumption that its |
| 813 | * pm_domain field points to the domain member of an object of type |
| 814 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 815 | * devices. |
| 816 | */ |
| 817 | static int pm_genpd_resume_noirq(struct device *dev) |
| 818 | { |
| 819 | struct generic_pm_domain *genpd; |
| 820 | |
| 821 | dev_dbg(dev, "%s()\n", __func__); |
| 822 | |
| 823 | genpd = dev_to_genpd(dev); |
| 824 | if (IS_ERR(genpd)) |
| 825 | return -EINVAL; |
| 826 | |
| 827 | if (genpd->suspend_power_off) |
| 828 | return 0; |
| 829 | |
| 830 | /* |
| 831 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 832 | * guaranteed that this function will never run twice in parallel for |
| 833 | * the same PM domain, so it is not necessary to use locking here. |
| 834 | */ |
| 835 | pm_genpd_poweron(genpd); |
| 836 | genpd->suspended_count--; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 837 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 838 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 839 | return genpd_resume_early(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
| 843 | * pm_genpd_resume - Resume a device belonging to an I/O power domain. |
| 844 | * @dev: Device to resume. |
| 845 | * |
| 846 | * Resume a device under the assumption that its pm_domain field points to the |
| 847 | * domain member of an object of type struct generic_pm_domain representing |
| 848 | * a power domain consisting of I/O devices. |
| 849 | */ |
| 850 | static int pm_genpd_resume(struct device *dev) |
| 851 | { |
| 852 | struct generic_pm_domain *genpd; |
| 853 | |
| 854 | dev_dbg(dev, "%s()\n", __func__); |
| 855 | |
| 856 | genpd = dev_to_genpd(dev); |
| 857 | if (IS_ERR(genpd)) |
| 858 | return -EINVAL; |
| 859 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 860 | return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /** |
| 864 | * pm_genpd_freeze - Freeze a device belonging to an I/O power domain. |
| 865 | * @dev: Device to freeze. |
| 866 | * |
| 867 | * Freeze a device under the assumption that its pm_domain field points to the |
| 868 | * domain member of an object of type struct generic_pm_domain representing |
| 869 | * a power domain consisting of I/O devices. |
| 870 | */ |
| 871 | static int pm_genpd_freeze(struct device *dev) |
| 872 | { |
| 873 | struct generic_pm_domain *genpd; |
| 874 | |
| 875 | dev_dbg(dev, "%s()\n", __func__); |
| 876 | |
| 877 | genpd = dev_to_genpd(dev); |
| 878 | if (IS_ERR(genpd)) |
| 879 | return -EINVAL; |
| 880 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 881 | return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /** |
| 885 | * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain. |
| 886 | * @dev: Device to freeze. |
| 887 | * |
| 888 | * Carry out a late freeze of a device under the assumption that its |
| 889 | * pm_domain field points to the domain member of an object of type |
| 890 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 891 | * devices. |
| 892 | */ |
| 893 | static int pm_genpd_freeze_noirq(struct device *dev) |
| 894 | { |
| 895 | struct generic_pm_domain *genpd; |
| 896 | int ret; |
| 897 | |
| 898 | dev_dbg(dev, "%s()\n", __func__); |
| 899 | |
| 900 | genpd = dev_to_genpd(dev); |
| 901 | if (IS_ERR(genpd)) |
| 902 | return -EINVAL; |
| 903 | |
| 904 | if (genpd->suspend_power_off) |
| 905 | return 0; |
| 906 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 907 | ret = genpd_freeze_late(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 908 | if (ret) |
| 909 | return ret; |
| 910 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 911 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain. |
| 918 | * @dev: Device to thaw. |
| 919 | * |
| 920 | * Carry out an early thaw of a device under the assumption that its |
| 921 | * pm_domain field points to the domain member of an object of type |
| 922 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 923 | * devices. |
| 924 | */ |
| 925 | static int pm_genpd_thaw_noirq(struct device *dev) |
| 926 | { |
| 927 | struct generic_pm_domain *genpd; |
| 928 | |
| 929 | dev_dbg(dev, "%s()\n", __func__); |
| 930 | |
| 931 | genpd = dev_to_genpd(dev); |
| 932 | if (IS_ERR(genpd)) |
| 933 | return -EINVAL; |
| 934 | |
| 935 | if (genpd->suspend_power_off) |
| 936 | return 0; |
| 937 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 938 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 939 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 940 | return genpd_thaw_early(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /** |
| 944 | * pm_genpd_thaw - Thaw a device belonging to an I/O power domain. |
| 945 | * @dev: Device to thaw. |
| 946 | * |
| 947 | * Thaw a device under the assumption that its pm_domain field points to the |
| 948 | * domain member of an object of type struct generic_pm_domain representing |
| 949 | * a power domain consisting of I/O devices. |
| 950 | */ |
| 951 | static int pm_genpd_thaw(struct device *dev) |
| 952 | { |
| 953 | struct generic_pm_domain *genpd; |
| 954 | |
| 955 | dev_dbg(dev, "%s()\n", __func__); |
| 956 | |
| 957 | genpd = dev_to_genpd(dev); |
| 958 | if (IS_ERR(genpd)) |
| 959 | return -EINVAL; |
| 960 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 961 | return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /** |
| 965 | * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain. |
| 966 | * @dev: Device to resume. |
| 967 | * |
| 968 | * Carry out an early restore of a device under the assumption that its |
| 969 | * pm_domain field points to the domain member of an object of type |
| 970 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 971 | * devices. |
| 972 | */ |
| 973 | static int pm_genpd_restore_noirq(struct device *dev) |
| 974 | { |
| 975 | struct generic_pm_domain *genpd; |
| 976 | |
| 977 | dev_dbg(dev, "%s()\n", __func__); |
| 978 | |
| 979 | genpd = dev_to_genpd(dev); |
| 980 | if (IS_ERR(genpd)) |
| 981 | return -EINVAL; |
| 982 | |
| 983 | /* |
| 984 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 985 | * guaranteed that this function will never run twice in parallel for |
| 986 | * the same PM domain, so it is not necessary to use locking here. |
| 987 | */ |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 988 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 989 | if (genpd->suspend_power_off) { |
| 990 | /* |
| 991 | * The boot kernel might put the domain into the power on state, |
| 992 | * so make sure it really is powered off. |
| 993 | */ |
| 994 | if (genpd->power_off) |
| 995 | genpd->power_off(genpd); |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | pm_genpd_poweron(genpd); |
| 1000 | genpd->suspended_count--; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1001 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1002 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1003 | return genpd_resume_early(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * pm_genpd_complete - Complete power transition of a device in a power domain. |
| 1008 | * @dev: Device to complete the transition of. |
| 1009 | * |
| 1010 | * Complete a power transition of a device (during a system-wide power |
| 1011 | * transition) under the assumption that its pm_domain field points to the |
| 1012 | * domain member of an object of type struct generic_pm_domain representing |
| 1013 | * a power domain consisting of I/O devices. |
| 1014 | */ |
| 1015 | static void pm_genpd_complete(struct device *dev) |
| 1016 | { |
| 1017 | struct generic_pm_domain *genpd; |
| 1018 | bool run_complete; |
| 1019 | |
| 1020 | dev_dbg(dev, "%s()\n", __func__); |
| 1021 | |
| 1022 | genpd = dev_to_genpd(dev); |
| 1023 | if (IS_ERR(genpd)) |
| 1024 | return; |
| 1025 | |
| 1026 | mutex_lock(&genpd->lock); |
| 1027 | |
| 1028 | run_complete = !genpd->suspend_power_off; |
| 1029 | if (--genpd->prepared_count == 0) |
| 1030 | genpd->suspend_power_off = false; |
| 1031 | |
| 1032 | mutex_unlock(&genpd->lock); |
| 1033 | |
| 1034 | if (run_complete) { |
| 1035 | pm_generic_complete(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 1036 | pm_runtime_set_active(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1037 | pm_runtime_enable(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 1038 | pm_runtime_idle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | #else |
| 1043 | |
| 1044 | #define pm_genpd_prepare NULL |
| 1045 | #define pm_genpd_suspend NULL |
| 1046 | #define pm_genpd_suspend_noirq NULL |
| 1047 | #define pm_genpd_resume_noirq NULL |
| 1048 | #define pm_genpd_resume NULL |
| 1049 | #define pm_genpd_freeze NULL |
| 1050 | #define pm_genpd_freeze_noirq NULL |
| 1051 | #define pm_genpd_thaw_noirq NULL |
| 1052 | #define pm_genpd_thaw NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1053 | #define pm_genpd_restore_noirq NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1054 | #define pm_genpd_complete NULL |
| 1055 | |
| 1056 | #endif /* CONFIG_PM_SLEEP */ |
| 1057 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1058 | /** |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1059 | * __pm_genpd_add_device - Add a device to an I/O PM domain. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1060 | * @genpd: PM domain to add the device to. |
| 1061 | * @dev: Device to be added. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1062 | * @td: Set of PM QoS timing parameters to attach to the device. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1063 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1064 | int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev, |
| 1065 | struct gpd_timing_data *td) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1066 | { |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1067 | struct generic_pm_domain_data *gpd_data; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1068 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1069 | int ret = 0; |
| 1070 | |
| 1071 | dev_dbg(dev, "%s()\n", __func__); |
| 1072 | |
| 1073 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1074 | return -EINVAL; |
| 1075 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1076 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1077 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1078 | if (genpd->status == GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1079 | ret = -EINVAL; |
| 1080 | goto out; |
| 1081 | } |
| 1082 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1083 | if (genpd->prepared_count > 0) { |
| 1084 | ret = -EAGAIN; |
| 1085 | goto out; |
| 1086 | } |
| 1087 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1088 | list_for_each_entry(pdd, &genpd->dev_list, list_node) |
| 1089 | if (pdd->dev == dev) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1090 | ret = -EINVAL; |
| 1091 | goto out; |
| 1092 | } |
| 1093 | |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1094 | gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL); |
| 1095 | if (!gpd_data) { |
| 1096 | ret = -ENOMEM; |
| 1097 | goto out; |
| 1098 | } |
| 1099 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1100 | genpd->device_count++; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1101 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1102 | dev->pm_domain = &genpd->domain; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1103 | dev_pm_get_subsys_data(dev); |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1104 | dev->power.subsys_data->domain_data = &gpd_data->base; |
| 1105 | gpd_data->base.dev = dev; |
| 1106 | gpd_data->need_restore = false; |
| 1107 | list_add_tail(&gpd_data->base.list_node, &genpd->dev_list); |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1108 | if (td) |
| 1109 | gpd_data->td = *td; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1110 | |
| 1111 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1112 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1113 | |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * pm_genpd_remove_device - Remove a device from an I/O PM domain. |
| 1119 | * @genpd: PM domain to remove the device from. |
| 1120 | * @dev: Device to be removed. |
| 1121 | */ |
| 1122 | int pm_genpd_remove_device(struct generic_pm_domain *genpd, |
| 1123 | struct device *dev) |
| 1124 | { |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1125 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1126 | int ret = -EINVAL; |
| 1127 | |
| 1128 | dev_dbg(dev, "%s()\n", __func__); |
| 1129 | |
| 1130 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1131 | return -EINVAL; |
| 1132 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1133 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1134 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1135 | if (genpd->prepared_count > 0) { |
| 1136 | ret = -EAGAIN; |
| 1137 | goto out; |
| 1138 | } |
| 1139 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1140 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 1141 | if (pdd->dev != dev) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1142 | continue; |
| 1143 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1144 | list_del_init(&pdd->list_node); |
| 1145 | pdd->dev = NULL; |
| 1146 | dev_pm_put_subsys_data(dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1147 | dev->pm_domain = NULL; |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1148 | kfree(to_gpd_data(pdd)); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1149 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1150 | genpd->device_count--; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1151 | |
| 1152 | ret = 0; |
| 1153 | break; |
| 1154 | } |
| 1155 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1156 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1157 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1158 | |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain. |
| 1164 | * @genpd: Master PM domain to add the subdomain to. |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1165 | * @subdomain: Subdomain to be added. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1166 | */ |
| 1167 | int pm_genpd_add_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1168 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1169 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1170 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1171 | int ret = 0; |
| 1172 | |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1173 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1174 | return -EINVAL; |
| 1175 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1176 | start: |
| 1177 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1178 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1179 | |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1180 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1181 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1182 | mutex_unlock(&subdomain->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1183 | genpd_release_lock(genpd); |
| 1184 | goto start; |
| 1185 | } |
| 1186 | |
| 1187 | if (genpd->status == GPD_STATE_POWER_OFF |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1188 | && subdomain->status != GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1189 | ret = -EINVAL; |
| 1190 | goto out; |
| 1191 | } |
| 1192 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1193 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1194 | if (link->slave == subdomain && link->master == genpd) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1195 | ret = -EINVAL; |
| 1196 | goto out; |
| 1197 | } |
| 1198 | } |
| 1199 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1200 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 1201 | if (!link) { |
| 1202 | ret = -ENOMEM; |
| 1203 | goto out; |
| 1204 | } |
| 1205 | link->master = genpd; |
| 1206 | list_add_tail(&link->master_node, &genpd->master_links); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1207 | link->slave = subdomain; |
| 1208 | list_add_tail(&link->slave_node, &subdomain->slave_links); |
| 1209 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1210 | genpd_sd_counter_inc(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1211 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1212 | out: |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1213 | mutex_unlock(&subdomain->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1214 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1215 | |
| 1216 | return ret; |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain. |
| 1221 | * @genpd: Master PM domain to remove the subdomain from. |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1222 | * @subdomain: Subdomain to be removed. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1223 | */ |
| 1224 | int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1225 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1226 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1227 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1228 | int ret = -EINVAL; |
| 1229 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1230 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1231 | return -EINVAL; |
| 1232 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1233 | start: |
| 1234 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1235 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1236 | list_for_each_entry(link, &genpd->master_links, master_node) { |
| 1237 | if (link->slave != subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1238 | continue; |
| 1239 | |
| 1240 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
| 1241 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1242 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1243 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1244 | mutex_unlock(&subdomain->lock); |
| 1245 | genpd_release_lock(genpd); |
| 1246 | goto start; |
| 1247 | } |
| 1248 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1249 | list_del(&link->master_node); |
| 1250 | list_del(&link->slave_node); |
| 1251 | kfree(link); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1252 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1253 | genpd_sd_counter_dec(genpd); |
| 1254 | |
| 1255 | mutex_unlock(&subdomain->lock); |
| 1256 | |
| 1257 | ret = 0; |
| 1258 | break; |
| 1259 | } |
| 1260 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1261 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1262 | |
| 1263 | return ret; |
| 1264 | } |
| 1265 | |
| 1266 | /** |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1267 | * pm_genpd_add_callbacks - Add PM domain callbacks to a given device. |
| 1268 | * @dev: Device to add the callbacks to. |
| 1269 | * @ops: Set of callbacks to add. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1270 | * @td: Timing data to add to the device along with the callbacks (optional). |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1271 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1272 | int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops, |
| 1273 | struct gpd_timing_data *td) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1274 | { |
| 1275 | struct pm_domain_data *pdd; |
| 1276 | int ret = 0; |
| 1277 | |
| 1278 | if (!(dev && dev->power.subsys_data && ops)) |
| 1279 | return -EINVAL; |
| 1280 | |
| 1281 | pm_runtime_disable(dev); |
| 1282 | device_pm_lock(); |
| 1283 | |
| 1284 | pdd = dev->power.subsys_data->domain_data; |
| 1285 | if (pdd) { |
| 1286 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
| 1287 | |
| 1288 | gpd_data->ops = *ops; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1289 | if (td) |
| 1290 | gpd_data->td = *td; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1291 | } else { |
| 1292 | ret = -EINVAL; |
| 1293 | } |
| 1294 | |
| 1295 | device_pm_unlock(); |
| 1296 | pm_runtime_enable(dev); |
| 1297 | |
| 1298 | return ret; |
| 1299 | } |
| 1300 | EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks); |
| 1301 | |
| 1302 | /** |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1303 | * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device. |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1304 | * @dev: Device to remove the callbacks from. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1305 | * @clear_td: If set, clear the device's timing data too. |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1306 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1307 | int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1308 | { |
| 1309 | struct pm_domain_data *pdd; |
| 1310 | int ret = 0; |
| 1311 | |
| 1312 | if (!(dev && dev->power.subsys_data)) |
| 1313 | return -EINVAL; |
| 1314 | |
| 1315 | pm_runtime_disable(dev); |
| 1316 | device_pm_lock(); |
| 1317 | |
| 1318 | pdd = dev->power.subsys_data->domain_data; |
| 1319 | if (pdd) { |
| 1320 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
| 1321 | |
| 1322 | gpd_data->ops = (struct gpd_dev_ops){ 0 }; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1323 | if (clear_td) |
| 1324 | gpd_data->td = (struct gpd_timing_data){ 0 }; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1325 | } else { |
| 1326 | ret = -EINVAL; |
| 1327 | } |
| 1328 | |
| 1329 | device_pm_unlock(); |
| 1330 | pm_runtime_enable(dev); |
| 1331 | |
| 1332 | return ret; |
| 1333 | } |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame^] | 1334 | EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1335 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1336 | /* Default device callbacks for generic PM domains. */ |
| 1337 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1338 | /** |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1339 | * pm_genpd_default_save_state - Default "save device state" for PM domians. |
| 1340 | * @dev: Device to handle. |
| 1341 | */ |
| 1342 | static int pm_genpd_default_save_state(struct device *dev) |
| 1343 | { |
| 1344 | int (*cb)(struct device *__dev); |
| 1345 | struct device_driver *drv = dev->driver; |
| 1346 | |
| 1347 | cb = dev_gpd_data(dev)->ops.save_state; |
| 1348 | if (cb) |
| 1349 | return cb(dev); |
| 1350 | |
| 1351 | if (drv && drv->pm && drv->pm->runtime_suspend) |
| 1352 | return drv->pm->runtime_suspend(dev); |
| 1353 | |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | /** |
| 1358 | * pm_genpd_default_restore_state - Default PM domians "restore device state". |
| 1359 | * @dev: Device to handle. |
| 1360 | */ |
| 1361 | static int pm_genpd_default_restore_state(struct device *dev) |
| 1362 | { |
| 1363 | int (*cb)(struct device *__dev); |
| 1364 | struct device_driver *drv = dev->driver; |
| 1365 | |
| 1366 | cb = dev_gpd_data(dev)->ops.restore_state; |
| 1367 | if (cb) |
| 1368 | return cb(dev); |
| 1369 | |
| 1370 | if (drv && drv->pm && drv->pm->runtime_resume) |
| 1371 | return drv->pm->runtime_resume(dev); |
| 1372 | |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1377 | * pm_genpd_default_suspend - Default "device suspend" for PM domians. |
| 1378 | * @dev: Device to handle. |
| 1379 | */ |
| 1380 | static int pm_genpd_default_suspend(struct device *dev) |
| 1381 | { |
| 1382 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze; |
| 1383 | |
| 1384 | return cb ? cb(dev) : pm_generic_suspend(dev); |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians. |
| 1389 | * @dev: Device to handle. |
| 1390 | */ |
| 1391 | static int pm_genpd_default_suspend_late(struct device *dev) |
| 1392 | { |
| 1393 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late; |
| 1394 | |
| 1395 | return cb ? cb(dev) : pm_generic_suspend_noirq(dev); |
| 1396 | } |
| 1397 | |
| 1398 | /** |
| 1399 | * pm_genpd_default_resume_early - Default "early device resume" for PM domians. |
| 1400 | * @dev: Device to handle. |
| 1401 | */ |
| 1402 | static int pm_genpd_default_resume_early(struct device *dev) |
| 1403 | { |
| 1404 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early; |
| 1405 | |
| 1406 | return cb ? cb(dev) : pm_generic_resume_noirq(dev); |
| 1407 | } |
| 1408 | |
| 1409 | /** |
| 1410 | * pm_genpd_default_resume - Default "device resume" for PM domians. |
| 1411 | * @dev: Device to handle. |
| 1412 | */ |
| 1413 | static int pm_genpd_default_resume(struct device *dev) |
| 1414 | { |
| 1415 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw; |
| 1416 | |
| 1417 | return cb ? cb(dev) : pm_generic_resume(dev); |
| 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * pm_genpd_default_freeze - Default "device freeze" for PM domians. |
| 1422 | * @dev: Device to handle. |
| 1423 | */ |
| 1424 | static int pm_genpd_default_freeze(struct device *dev) |
| 1425 | { |
| 1426 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze; |
| 1427 | |
| 1428 | return cb ? cb(dev) : pm_generic_freeze(dev); |
| 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians. |
| 1433 | * @dev: Device to handle. |
| 1434 | */ |
| 1435 | static int pm_genpd_default_freeze_late(struct device *dev) |
| 1436 | { |
| 1437 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late; |
| 1438 | |
| 1439 | return cb ? cb(dev) : pm_generic_freeze_noirq(dev); |
| 1440 | } |
| 1441 | |
| 1442 | /** |
| 1443 | * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians. |
| 1444 | * @dev: Device to handle. |
| 1445 | */ |
| 1446 | static int pm_genpd_default_thaw_early(struct device *dev) |
| 1447 | { |
| 1448 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early; |
| 1449 | |
| 1450 | return cb ? cb(dev) : pm_generic_thaw_noirq(dev); |
| 1451 | } |
| 1452 | |
| 1453 | /** |
| 1454 | * pm_genpd_default_thaw - Default "device thaw" for PM domians. |
| 1455 | * @dev: Device to handle. |
| 1456 | */ |
| 1457 | static int pm_genpd_default_thaw(struct device *dev) |
| 1458 | { |
| 1459 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw; |
| 1460 | |
| 1461 | return cb ? cb(dev) : pm_generic_thaw(dev); |
| 1462 | } |
| 1463 | |
| 1464 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1465 | * pm_genpd_init - Initialize a generic I/O PM domain object. |
| 1466 | * @genpd: PM domain object to initialize. |
| 1467 | * @gov: PM domain governor to associate with the domain (may be NULL). |
| 1468 | * @is_off: Initial value of the domain's power_is_off field. |
| 1469 | */ |
| 1470 | void pm_genpd_init(struct generic_pm_domain *genpd, |
| 1471 | struct dev_power_governor *gov, bool is_off) |
| 1472 | { |
| 1473 | if (IS_ERR_OR_NULL(genpd)) |
| 1474 | return; |
| 1475 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1476 | INIT_LIST_HEAD(&genpd->master_links); |
| 1477 | INIT_LIST_HEAD(&genpd->slave_links); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1478 | INIT_LIST_HEAD(&genpd->dev_list); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1479 | mutex_init(&genpd->lock); |
| 1480 | genpd->gov = gov; |
| 1481 | INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn); |
| 1482 | genpd->in_progress = 0; |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1483 | atomic_set(&genpd->sd_count, 0); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1484 | genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE; |
| 1485 | init_waitqueue_head(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 1486 | genpd->poweroff_task = NULL; |
| 1487 | genpd->resume_count = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1488 | genpd->device_count = 0; |
| 1489 | genpd->suspended_count = 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1490 | genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend; |
| 1491 | genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume; |
| 1492 | genpd->domain.ops.runtime_idle = pm_generic_runtime_idle; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1493 | genpd->domain.ops.prepare = pm_genpd_prepare; |
| 1494 | genpd->domain.ops.suspend = pm_genpd_suspend; |
| 1495 | genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq; |
| 1496 | genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq; |
| 1497 | genpd->domain.ops.resume = pm_genpd_resume; |
| 1498 | genpd->domain.ops.freeze = pm_genpd_freeze; |
| 1499 | genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq; |
| 1500 | genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq; |
| 1501 | genpd->domain.ops.thaw = pm_genpd_thaw; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1502 | genpd->domain.ops.poweroff = pm_genpd_suspend; |
| 1503 | genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1504 | genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1505 | genpd->domain.ops.restore = pm_genpd_resume; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1506 | genpd->domain.ops.complete = pm_genpd_complete; |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1507 | genpd->dev_ops.save_state = pm_genpd_default_save_state; |
| 1508 | genpd->dev_ops.restore_state = pm_genpd_default_restore_state; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1509 | genpd->dev_ops.freeze = pm_genpd_default_suspend; |
| 1510 | genpd->dev_ops.freeze_late = pm_genpd_default_suspend_late; |
| 1511 | genpd->dev_ops.thaw_early = pm_genpd_default_resume_early; |
| 1512 | genpd->dev_ops.thaw = pm_genpd_default_resume; |
| 1513 | genpd->dev_ops.freeze = pm_genpd_default_freeze; |
| 1514 | genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late; |
| 1515 | genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early; |
| 1516 | genpd->dev_ops.thaw = pm_genpd_default_thaw; |
Rafael J. Wysocki | 5125bbf | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 1517 | mutex_lock(&gpd_list_lock); |
| 1518 | list_add(&genpd->gpd_list_node, &gpd_list); |
| 1519 | mutex_unlock(&gpd_list_lock); |
| 1520 | } |