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