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 | |
| 1130 | pm_genpd_poweron(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1131 | |
Rafael J. Wysocki | 1e78a0c | 2012-03-13 22:39:48 +0100 | [diff] [blame^] | 1132 | 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] | 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * pm_genpd_complete - Complete power transition of a device in a power domain. |
| 1137 | * @dev: Device to complete the transition of. |
| 1138 | * |
| 1139 | * Complete a power transition of a device (during a system-wide power |
| 1140 | * transition) under the assumption that its pm_domain field points to the |
| 1141 | * domain member of an object of type struct generic_pm_domain representing |
| 1142 | * a power domain consisting of I/O devices. |
| 1143 | */ |
| 1144 | static void pm_genpd_complete(struct device *dev) |
| 1145 | { |
| 1146 | struct generic_pm_domain *genpd; |
| 1147 | bool run_complete; |
| 1148 | |
| 1149 | dev_dbg(dev, "%s()\n", __func__); |
| 1150 | |
| 1151 | genpd = dev_to_genpd(dev); |
| 1152 | if (IS_ERR(genpd)) |
| 1153 | return; |
| 1154 | |
| 1155 | mutex_lock(&genpd->lock); |
| 1156 | |
| 1157 | run_complete = !genpd->suspend_power_off; |
| 1158 | if (--genpd->prepared_count == 0) |
| 1159 | genpd->suspend_power_off = false; |
| 1160 | |
| 1161 | mutex_unlock(&genpd->lock); |
| 1162 | |
| 1163 | if (run_complete) { |
| 1164 | pm_generic_complete(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 1165 | pm_runtime_set_active(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1166 | pm_runtime_enable(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 1167 | pm_runtime_idle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | #else |
| 1172 | |
| 1173 | #define pm_genpd_prepare NULL |
| 1174 | #define pm_genpd_suspend NULL |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1175 | #define pm_genpd_suspend_late NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1176 | #define pm_genpd_suspend_noirq NULL |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1177 | #define pm_genpd_resume_early NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1178 | #define pm_genpd_resume_noirq NULL |
| 1179 | #define pm_genpd_resume NULL |
| 1180 | #define pm_genpd_freeze NULL |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1181 | #define pm_genpd_freeze_late NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1182 | #define pm_genpd_freeze_noirq NULL |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1183 | #define pm_genpd_thaw_early NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1184 | #define pm_genpd_thaw_noirq NULL |
| 1185 | #define pm_genpd_thaw NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1186 | #define pm_genpd_restore_noirq NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1187 | #define pm_genpd_complete NULL |
| 1188 | |
| 1189 | #endif /* CONFIG_PM_SLEEP */ |
| 1190 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1191 | /** |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1192 | * __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] | 1193 | * @genpd: PM domain to add the device to. |
| 1194 | * @dev: Device to be added. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1195 | * @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] | 1196 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1197 | int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev, |
| 1198 | struct gpd_timing_data *td) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1199 | { |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1200 | struct generic_pm_domain_data *gpd_data; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1201 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1202 | int ret = 0; |
| 1203 | |
| 1204 | dev_dbg(dev, "%s()\n", __func__); |
| 1205 | |
| 1206 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1207 | return -EINVAL; |
| 1208 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1209 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1210 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1211 | if (genpd->status == GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1212 | ret = -EINVAL; |
| 1213 | goto out; |
| 1214 | } |
| 1215 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1216 | if (genpd->prepared_count > 0) { |
| 1217 | ret = -EAGAIN; |
| 1218 | goto out; |
| 1219 | } |
| 1220 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1221 | list_for_each_entry(pdd, &genpd->dev_list, list_node) |
| 1222 | if (pdd->dev == dev) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1223 | ret = -EINVAL; |
| 1224 | goto out; |
| 1225 | } |
| 1226 | |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1227 | gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL); |
| 1228 | if (!gpd_data) { |
| 1229 | ret = -ENOMEM; |
| 1230 | goto out; |
| 1231 | } |
| 1232 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1233 | genpd->device_count++; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1234 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1235 | dev->pm_domain = &genpd->domain; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1236 | dev_pm_get_subsys_data(dev); |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1237 | dev->power.subsys_data->domain_data = &gpd_data->base; |
| 1238 | gpd_data->base.dev = dev; |
| 1239 | gpd_data->need_restore = false; |
| 1240 | list_add_tail(&gpd_data->base.list_node, &genpd->dev_list); |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1241 | if (td) |
| 1242 | gpd_data->td = *td; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1243 | |
| 1244 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1245 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1246 | |
| 1247 | return ret; |
| 1248 | } |
| 1249 | |
| 1250 | /** |
Thomas Abraham | c8aa130 | 2012-01-27 15:22:07 +0900 | [diff] [blame] | 1251 | * __pm_genpd_of_add_device - Add a device to an I/O PM domain. |
| 1252 | * @genpd_node: Device tree node pointer representing a PM domain to which the |
| 1253 | * the device is added to. |
| 1254 | * @dev: Device to be added. |
| 1255 | * @td: Set of PM QoS timing parameters to attach to the device. |
| 1256 | */ |
| 1257 | int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev, |
| 1258 | struct gpd_timing_data *td) |
| 1259 | { |
| 1260 | struct generic_pm_domain *genpd = NULL, *gpd; |
| 1261 | |
| 1262 | dev_dbg(dev, "%s()\n", __func__); |
| 1263 | |
| 1264 | if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev)) |
| 1265 | return -EINVAL; |
| 1266 | |
| 1267 | mutex_lock(&gpd_list_lock); |
| 1268 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) { |
| 1269 | if (gpd->of_node == genpd_node) { |
| 1270 | genpd = gpd; |
| 1271 | break; |
| 1272 | } |
| 1273 | } |
| 1274 | mutex_unlock(&gpd_list_lock); |
| 1275 | |
| 1276 | if (!genpd) |
| 1277 | return -EINVAL; |
| 1278 | |
| 1279 | return __pm_genpd_add_device(genpd, dev, td); |
| 1280 | } |
| 1281 | |
| 1282 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1283 | * pm_genpd_remove_device - Remove a device from an I/O PM domain. |
| 1284 | * @genpd: PM domain to remove the device from. |
| 1285 | * @dev: Device to be removed. |
| 1286 | */ |
| 1287 | int pm_genpd_remove_device(struct generic_pm_domain *genpd, |
| 1288 | struct device *dev) |
| 1289 | { |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1290 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1291 | int ret = -EINVAL; |
| 1292 | |
| 1293 | dev_dbg(dev, "%s()\n", __func__); |
| 1294 | |
| 1295 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1296 | return -EINVAL; |
| 1297 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1298 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1299 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1300 | if (genpd->prepared_count > 0) { |
| 1301 | ret = -EAGAIN; |
| 1302 | goto out; |
| 1303 | } |
| 1304 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1305 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 1306 | if (pdd->dev != dev) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1307 | continue; |
| 1308 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1309 | list_del_init(&pdd->list_node); |
| 1310 | pdd->dev = NULL; |
| 1311 | dev_pm_put_subsys_data(dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1312 | dev->pm_domain = NULL; |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 1313 | kfree(to_gpd_data(pdd)); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1314 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1315 | genpd->device_count--; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1316 | |
| 1317 | ret = 0; |
| 1318 | break; |
| 1319 | } |
| 1320 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1321 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1322 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1323 | |
| 1324 | return ret; |
| 1325 | } |
| 1326 | |
| 1327 | /** |
Rafael J. Wysocki | 1e78a0c | 2012-03-13 22:39:48 +0100 | [diff] [blame^] | 1328 | * pm_genpd_dev_always_on - Set/unset the "always on" flag for a given device. |
| 1329 | * @dev: Device to set/unset the flag for. |
| 1330 | * @val: The new value of the device's "always on" flag. |
| 1331 | */ |
| 1332 | void pm_genpd_dev_always_on(struct device *dev, bool val) |
| 1333 | { |
| 1334 | struct pm_subsys_data *psd; |
| 1335 | unsigned long flags; |
| 1336 | |
| 1337 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1338 | |
| 1339 | psd = dev_to_psd(dev); |
| 1340 | if (psd && psd->domain_data) |
| 1341 | to_gpd_data(psd->domain_data)->always_on = val; |
| 1342 | |
| 1343 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1344 | } |
| 1345 | EXPORT_SYMBOL_GPL(pm_genpd_dev_always_on); |
| 1346 | |
| 1347 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1348 | * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain. |
| 1349 | * @genpd: Master PM domain to add the subdomain to. |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1350 | * @subdomain: Subdomain to be added. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1351 | */ |
| 1352 | int pm_genpd_add_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1353 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1354 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1355 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1356 | int ret = 0; |
| 1357 | |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1358 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1359 | return -EINVAL; |
| 1360 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1361 | start: |
| 1362 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1363 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1364 | |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1365 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1366 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1367 | mutex_unlock(&subdomain->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1368 | genpd_release_lock(genpd); |
| 1369 | goto start; |
| 1370 | } |
| 1371 | |
| 1372 | if (genpd->status == GPD_STATE_POWER_OFF |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1373 | && subdomain->status != GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1374 | ret = -EINVAL; |
| 1375 | goto out; |
| 1376 | } |
| 1377 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1378 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1379 | if (link->slave == subdomain && link->master == genpd) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1380 | ret = -EINVAL; |
| 1381 | goto out; |
| 1382 | } |
| 1383 | } |
| 1384 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1385 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 1386 | if (!link) { |
| 1387 | ret = -ENOMEM; |
| 1388 | goto out; |
| 1389 | } |
| 1390 | link->master = genpd; |
| 1391 | list_add_tail(&link->master_node, &genpd->master_links); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1392 | link->slave = subdomain; |
| 1393 | list_add_tail(&link->slave_node, &subdomain->slave_links); |
| 1394 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1395 | genpd_sd_counter_inc(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1396 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1397 | out: |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1398 | mutex_unlock(&subdomain->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1399 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1400 | |
| 1401 | return ret; |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain. |
| 1406 | * @genpd: Master PM domain to remove the subdomain from. |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1407 | * @subdomain: Subdomain to be removed. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1408 | */ |
| 1409 | int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1410 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1411 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1412 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1413 | int ret = -EINVAL; |
| 1414 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1415 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1416 | return -EINVAL; |
| 1417 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1418 | start: |
| 1419 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1420 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1421 | list_for_each_entry(link, &genpd->master_links, master_node) { |
| 1422 | if (link->slave != subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1423 | continue; |
| 1424 | |
| 1425 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
| 1426 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1427 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1428 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1429 | mutex_unlock(&subdomain->lock); |
| 1430 | genpd_release_lock(genpd); |
| 1431 | goto start; |
| 1432 | } |
| 1433 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1434 | list_del(&link->master_node); |
| 1435 | list_del(&link->slave_node); |
| 1436 | kfree(link); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1437 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1438 | genpd_sd_counter_dec(genpd); |
| 1439 | |
| 1440 | mutex_unlock(&subdomain->lock); |
| 1441 | |
| 1442 | ret = 0; |
| 1443 | break; |
| 1444 | } |
| 1445 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1446 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1447 | |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
| 1451 | /** |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1452 | * pm_genpd_add_callbacks - Add PM domain callbacks to a given device. |
| 1453 | * @dev: Device to add the callbacks to. |
| 1454 | * @ops: Set of callbacks to add. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1455 | * @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] | 1456 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1457 | int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops, |
| 1458 | struct gpd_timing_data *td) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1459 | { |
| 1460 | struct pm_domain_data *pdd; |
| 1461 | int ret = 0; |
| 1462 | |
| 1463 | if (!(dev && dev->power.subsys_data && ops)) |
| 1464 | return -EINVAL; |
| 1465 | |
| 1466 | pm_runtime_disable(dev); |
| 1467 | device_pm_lock(); |
| 1468 | |
| 1469 | pdd = dev->power.subsys_data->domain_data; |
| 1470 | if (pdd) { |
| 1471 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
| 1472 | |
| 1473 | gpd_data->ops = *ops; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1474 | if (td) |
| 1475 | gpd_data->td = *td; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1476 | } else { |
| 1477 | ret = -EINVAL; |
| 1478 | } |
| 1479 | |
| 1480 | device_pm_unlock(); |
| 1481 | pm_runtime_enable(dev); |
| 1482 | |
| 1483 | return ret; |
| 1484 | } |
| 1485 | EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks); |
| 1486 | |
| 1487 | /** |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1488 | * __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] | 1489 | * @dev: Device to remove the callbacks from. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1490 | * @clear_td: If set, clear the device's timing data too. |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1491 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1492 | int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1493 | { |
| 1494 | struct pm_domain_data *pdd; |
| 1495 | int ret = 0; |
| 1496 | |
| 1497 | if (!(dev && dev->power.subsys_data)) |
| 1498 | return -EINVAL; |
| 1499 | |
| 1500 | pm_runtime_disable(dev); |
| 1501 | device_pm_lock(); |
| 1502 | |
| 1503 | pdd = dev->power.subsys_data->domain_data; |
| 1504 | if (pdd) { |
| 1505 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
| 1506 | |
| 1507 | gpd_data->ops = (struct gpd_dev_ops){ 0 }; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1508 | if (clear_td) |
| 1509 | gpd_data->td = (struct gpd_timing_data){ 0 }; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1510 | } else { |
| 1511 | ret = -EINVAL; |
| 1512 | } |
| 1513 | |
| 1514 | device_pm_unlock(); |
| 1515 | pm_runtime_enable(dev); |
| 1516 | |
| 1517 | return ret; |
| 1518 | } |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1519 | EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1520 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1521 | /* Default device callbacks for generic PM domains. */ |
| 1522 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1523 | /** |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1524 | * pm_genpd_default_save_state - Default "save device state" for PM domians. |
| 1525 | * @dev: Device to handle. |
| 1526 | */ |
| 1527 | static int pm_genpd_default_save_state(struct device *dev) |
| 1528 | { |
| 1529 | int (*cb)(struct device *__dev); |
| 1530 | struct device_driver *drv = dev->driver; |
| 1531 | |
| 1532 | cb = dev_gpd_data(dev)->ops.save_state; |
| 1533 | if (cb) |
| 1534 | return cb(dev); |
| 1535 | |
| 1536 | if (drv && drv->pm && drv->pm->runtime_suspend) |
| 1537 | return drv->pm->runtime_suspend(dev); |
| 1538 | |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | /** |
| 1543 | * pm_genpd_default_restore_state - Default PM domians "restore device state". |
| 1544 | * @dev: Device to handle. |
| 1545 | */ |
| 1546 | static int pm_genpd_default_restore_state(struct device *dev) |
| 1547 | { |
| 1548 | int (*cb)(struct device *__dev); |
| 1549 | struct device_driver *drv = dev->driver; |
| 1550 | |
| 1551 | cb = dev_gpd_data(dev)->ops.restore_state; |
| 1552 | if (cb) |
| 1553 | return cb(dev); |
| 1554 | |
| 1555 | if (drv && drv->pm && drv->pm->runtime_resume) |
| 1556 | return drv->pm->runtime_resume(dev); |
| 1557 | |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
Rafael J. Wysocki | 0f1d698 | 2012-01-14 00:39:25 +0100 | [diff] [blame] | 1561 | #ifdef CONFIG_PM_SLEEP |
| 1562 | |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1563 | /** |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1564 | * pm_genpd_default_suspend - Default "device suspend" for PM domians. |
| 1565 | * @dev: Device to handle. |
| 1566 | */ |
| 1567 | static int pm_genpd_default_suspend(struct device *dev) |
| 1568 | { |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1569 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1570 | |
| 1571 | return cb ? cb(dev) : pm_generic_suspend(dev); |
| 1572 | } |
| 1573 | |
| 1574 | /** |
| 1575 | * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians. |
| 1576 | * @dev: Device to handle. |
| 1577 | */ |
| 1578 | static int pm_genpd_default_suspend_late(struct device *dev) |
| 1579 | { |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1580 | 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] | 1581 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1582 | return cb ? cb(dev) : pm_generic_suspend_late(dev); |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | /** |
| 1586 | * pm_genpd_default_resume_early - Default "early device resume" for PM domians. |
| 1587 | * @dev: Device to handle. |
| 1588 | */ |
| 1589 | static int pm_genpd_default_resume_early(struct device *dev) |
| 1590 | { |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1591 | 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] | 1592 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1593 | return cb ? cb(dev) : pm_generic_resume_early(dev); |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /** |
| 1597 | * pm_genpd_default_resume - Default "device resume" for PM domians. |
| 1598 | * @dev: Device to handle. |
| 1599 | */ |
| 1600 | static int pm_genpd_default_resume(struct device *dev) |
| 1601 | { |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1602 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1603 | |
| 1604 | return cb ? cb(dev) : pm_generic_resume(dev); |
| 1605 | } |
| 1606 | |
| 1607 | /** |
| 1608 | * pm_genpd_default_freeze - Default "device freeze" for PM domians. |
| 1609 | * @dev: Device to handle. |
| 1610 | */ |
| 1611 | static int pm_genpd_default_freeze(struct device *dev) |
| 1612 | { |
| 1613 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze; |
| 1614 | |
| 1615 | return cb ? cb(dev) : pm_generic_freeze(dev); |
| 1616 | } |
| 1617 | |
| 1618 | /** |
| 1619 | * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians. |
| 1620 | * @dev: Device to handle. |
| 1621 | */ |
| 1622 | static int pm_genpd_default_freeze_late(struct device *dev) |
| 1623 | { |
| 1624 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late; |
| 1625 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1626 | return cb ? cb(dev) : pm_generic_freeze_late(dev); |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians. |
| 1631 | * @dev: Device to handle. |
| 1632 | */ |
| 1633 | static int pm_genpd_default_thaw_early(struct device *dev) |
| 1634 | { |
| 1635 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early; |
| 1636 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1637 | return cb ? cb(dev) : pm_generic_thaw_early(dev); |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | /** |
| 1641 | * pm_genpd_default_thaw - Default "device thaw" for PM domians. |
| 1642 | * @dev: Device to handle. |
| 1643 | */ |
| 1644 | static int pm_genpd_default_thaw(struct device *dev) |
| 1645 | { |
| 1646 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw; |
| 1647 | |
| 1648 | return cb ? cb(dev) : pm_generic_thaw(dev); |
| 1649 | } |
| 1650 | |
Rafael J. Wysocki | 0f1d698 | 2012-01-14 00:39:25 +0100 | [diff] [blame] | 1651 | #else /* !CONFIG_PM_SLEEP */ |
| 1652 | |
| 1653 | #define pm_genpd_default_suspend NULL |
| 1654 | #define pm_genpd_default_suspend_late NULL |
| 1655 | #define pm_genpd_default_resume_early NULL |
| 1656 | #define pm_genpd_default_resume NULL |
| 1657 | #define pm_genpd_default_freeze NULL |
| 1658 | #define pm_genpd_default_freeze_late NULL |
| 1659 | #define pm_genpd_default_thaw_early NULL |
| 1660 | #define pm_genpd_default_thaw NULL |
| 1661 | |
| 1662 | #endif /* !CONFIG_PM_SLEEP */ |
| 1663 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1664 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1665 | * pm_genpd_init - Initialize a generic I/O PM domain object. |
| 1666 | * @genpd: PM domain object to initialize. |
| 1667 | * @gov: PM domain governor to associate with the domain (may be NULL). |
| 1668 | * @is_off: Initial value of the domain's power_is_off field. |
| 1669 | */ |
| 1670 | void pm_genpd_init(struct generic_pm_domain *genpd, |
| 1671 | struct dev_power_governor *gov, bool is_off) |
| 1672 | { |
| 1673 | if (IS_ERR_OR_NULL(genpd)) |
| 1674 | return; |
| 1675 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1676 | INIT_LIST_HEAD(&genpd->master_links); |
| 1677 | INIT_LIST_HEAD(&genpd->slave_links); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1678 | INIT_LIST_HEAD(&genpd->dev_list); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1679 | mutex_init(&genpd->lock); |
| 1680 | genpd->gov = gov; |
| 1681 | INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn); |
| 1682 | genpd->in_progress = 0; |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1683 | atomic_set(&genpd->sd_count, 0); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1684 | genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE; |
| 1685 | init_waitqueue_head(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 1686 | genpd->poweroff_task = NULL; |
| 1687 | genpd->resume_count = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1688 | genpd->device_count = 0; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 1689 | genpd->max_off_time_ns = -1; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1690 | genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend; |
| 1691 | genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume; |
| 1692 | genpd->domain.ops.runtime_idle = pm_generic_runtime_idle; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1693 | genpd->domain.ops.prepare = pm_genpd_prepare; |
| 1694 | genpd->domain.ops.suspend = pm_genpd_suspend; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1695 | genpd->domain.ops.suspend_late = pm_genpd_suspend_late; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1696 | genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq; |
| 1697 | genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1698 | genpd->domain.ops.resume_early = pm_genpd_resume_early; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1699 | genpd->domain.ops.resume = pm_genpd_resume; |
| 1700 | genpd->domain.ops.freeze = pm_genpd_freeze; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1701 | genpd->domain.ops.freeze_late = pm_genpd_freeze_late; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1702 | genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq; |
| 1703 | genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1704 | genpd->domain.ops.thaw_early = pm_genpd_thaw_early; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1705 | genpd->domain.ops.thaw = pm_genpd_thaw; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1706 | genpd->domain.ops.poweroff = pm_genpd_suspend; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1707 | genpd->domain.ops.poweroff_late = pm_genpd_suspend_late; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1708 | genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1709 | genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1710 | genpd->domain.ops.restore_early = pm_genpd_resume_early; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1711 | genpd->domain.ops.restore = pm_genpd_resume; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1712 | genpd->domain.ops.complete = pm_genpd_complete; |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1713 | genpd->dev_ops.save_state = pm_genpd_default_save_state; |
| 1714 | genpd->dev_ops.restore_state = pm_genpd_default_restore_state; |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1715 | genpd->dev_ops.suspend = pm_genpd_default_suspend; |
| 1716 | genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late; |
| 1717 | genpd->dev_ops.resume_early = pm_genpd_default_resume_early; |
| 1718 | genpd->dev_ops.resume = pm_genpd_default_resume; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1719 | genpd->dev_ops.freeze = pm_genpd_default_freeze; |
| 1720 | genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late; |
| 1721 | genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early; |
| 1722 | genpd->dev_ops.thaw = pm_genpd_default_thaw; |
Rafael J. Wysocki | 5125bbf | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 1723 | mutex_lock(&gpd_list_lock); |
| 1724 | list_add(&genpd->gpd_list_node, &gpd_list); |
| 1725 | mutex_unlock(&gpd_list_lock); |
| 1726 | } |