blob: 3af9f5a71ad509d1c3c23bd5f6dbf7a5c505dedb [file] [log] [blame]
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001/*
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. Wysocki17b75ec2011-07-12 00:39:29 +020016#include <linux/sched.h>
17#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010018#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. Wysockif7218892011-07-01 22:12:45 +020035
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +020036static LIST_HEAD(gpd_list);
37static DEFINE_MUTEX(gpd_list_lock);
38
Rafael J. Wysocki52480512011-07-01 22:13:10 +020039#ifdef CONFIG_PM
40
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010041struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020042{
43 if (IS_ERR_OR_NULL(dev->pm_domain))
44 return ERR_PTR(-EINVAL);
45
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020046 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020047}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020048
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010049static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
50{
51 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
52}
53
54static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
55{
56 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
57}
58
Rafael J. Wysockiecf00472011-11-27 13:11:44 +010059static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
60{
61 return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
62}
63
64static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
65{
66 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
67}
68
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020069static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020070{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020071 bool ret = false;
72
73 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
74 ret = !!atomic_dec_and_test(&genpd->sd_count);
75
76 return ret;
77}
78
79static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
80{
81 atomic_inc(&genpd->sd_count);
82 smp_mb__after_atomic_inc();
Rafael J. Wysockif7218892011-07-01 22:12:45 +020083}
84
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020085static void genpd_acquire_lock(struct generic_pm_domain *genpd)
86{
87 DEFINE_WAIT(wait);
88
89 mutex_lock(&genpd->lock);
90 /*
91 * Wait for the domain to transition into either the active,
92 * or the power off state.
93 */
94 for (;;) {
95 prepare_to_wait(&genpd->status_wait_queue, &wait,
96 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020097 if (genpd->status == GPD_STATE_ACTIVE
98 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020099 break;
100 mutex_unlock(&genpd->lock);
101
102 schedule();
103
104 mutex_lock(&genpd->lock);
105 }
106 finish_wait(&genpd->status_wait_queue, &wait);
107}
108
109static void genpd_release_lock(struct generic_pm_domain *genpd)
110{
111 mutex_unlock(&genpd->lock);
112}
113
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200114static void genpd_set_active(struct generic_pm_domain *genpd)
115{
116 if (genpd->resume_count == 0)
117 genpd->status = GPD_STATE_ACTIVE;
118}
119
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200120/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200121 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200122 * @genpd: PM domain to power up.
123 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200124 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200125 * resume a device belonging to it.
126 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200127int __pm_genpd_poweron(struct generic_pm_domain *genpd)
128 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200129{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200130 struct gpd_link *link;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200131 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200132 int ret = 0;
133
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200134 /* If the domain's master is being waited for, we have to wait too. */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200135 for (;;) {
136 prepare_to_wait(&genpd->status_wait_queue, &wait,
137 TASK_UNINTERRUPTIBLE);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200138 if (genpd->status != GPD_STATE_WAIT_MASTER)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200139 break;
140 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200141
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200142 schedule();
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200143
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200144 mutex_lock(&genpd->lock);
145 }
146 finish_wait(&genpd->status_wait_queue, &wait);
147
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200148 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200149 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200150 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200151
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200152 if (genpd->status != GPD_STATE_POWER_OFF) {
153 genpd_set_active(genpd);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200154 return 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200155 }
156
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200157 /*
158 * The list is guaranteed not to change while the loop below is being
159 * executed, unless one of the masters' .power_on() callbacks fiddles
160 * with it.
161 */
162 list_for_each_entry(link, &genpd->slave_links, slave_node) {
163 genpd_sd_counter_inc(link->master);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200164 genpd->status = GPD_STATE_WAIT_MASTER;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200165
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200166 mutex_unlock(&genpd->lock);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200167
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200168 ret = pm_genpd_poweron(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200169
170 mutex_lock(&genpd->lock);
171
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200172 /*
173 * The "wait for parent" status is guaranteed not to change
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200174 * while the master is powering on.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200175 */
176 genpd->status = GPD_STATE_POWER_OFF;
177 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200178 if (ret) {
179 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200180 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200181 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200182 }
183
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200184 if (genpd->power_on) {
Rafael J. Wysockife202fd2011-08-05 21:45:11 +0200185 ret = genpd->power_on(genpd);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200186 if (ret)
187 goto err;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200188 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200189
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200190 genpd_set_active(genpd);
191
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200192 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200193
194 err:
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200195 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
196 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200197
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200198 return ret;
199}
200
201/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200202 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200203 * @genpd: PM domain to power up.
204 */
205int pm_genpd_poweron(struct generic_pm_domain *genpd)
206{
207 int ret;
208
209 mutex_lock(&genpd->lock);
210 ret = __pm_genpd_poweron(genpd);
211 mutex_unlock(&genpd->lock);
212 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200213}
214
215#endif /* CONFIG_PM */
216
217#ifdef CONFIG_PM_RUNTIME
218
219/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200220 * __pm_genpd_save_device - Save the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200221 * @pdd: Domain data of the device to save the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200222 * @genpd: PM domain the device belongs to.
223 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200224static int __pm_genpd_save_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200225 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200226 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200227{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200228 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200229 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200230 int ret = 0;
231
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200232 if (gpd_data->need_restore)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200233 return 0;
234
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200235 mutex_unlock(&genpd->lock);
236
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100237 genpd_start_dev(genpd, dev);
238 ret = genpd_save_dev(genpd, dev);
239 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200240
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200241 mutex_lock(&genpd->lock);
242
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200243 if (!ret)
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200244 gpd_data->need_restore = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200245
246 return ret;
247}
248
249/**
250 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200251 * @pdd: Domain data of the device to restore the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200252 * @genpd: PM domain the device belongs to.
253 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200254static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200255 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200256 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200257{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200258 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200259 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200260
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200261 if (!gpd_data->need_restore)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200262 return;
263
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200264 mutex_unlock(&genpd->lock);
265
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100266 genpd_start_dev(genpd, dev);
267 genpd_restore_dev(genpd, dev);
268 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200269
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200270 mutex_lock(&genpd->lock);
271
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200272 gpd_data->need_restore = false;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200273}
274
275/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200276 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
277 * @genpd: PM domain to check.
278 *
279 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
280 * a "power off" operation, which means that a "power on" has occured in the
281 * meantime, or if its resume_count field is different from zero, which means
282 * that one of its devices has been resumed in the meantime.
283 */
284static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
285{
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200286 return genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200287 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200288}
289
290/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200291 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
292 * @genpd: PM domait to power off.
293 *
294 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
295 * before.
296 */
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200297void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200298{
299 if (!work_pending(&genpd->power_off_work))
300 queue_work(pm_wq, &genpd->power_off_work);
301}
302
303/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200304 * pm_genpd_poweroff - Remove power from a given PM domain.
305 * @genpd: PM domain to power down.
306 *
307 * If all of the @genpd's devices have been suspended and all of its subdomains
308 * have been powered down, run the runtime suspend callbacks provided by all of
309 * the @genpd's devices' drivers and remove power from @genpd.
310 */
311static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200312 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200313{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200314 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200315 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200316 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200317 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200318
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200319 start:
320 /*
321 * Do not try to power off the domain in the following situations:
322 * (1) The domain is already in the "power off" state.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200323 * (2) The domain is waiting for its master to power up.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200324 * (3) One of the domain's devices is being resumed right now.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200325 * (4) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200326 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200327 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200328 || genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200329 || genpd->resume_count > 0 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200330 return 0;
331
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200332 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200333 return -EBUSY;
334
335 not_suspended = 0;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200336 list_for_each_entry(pdd, &genpd->dev_list, list_node)
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200337 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
338 || pdd->dev->power.irq_safe))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200339 not_suspended++;
340
341 if (not_suspended > genpd->in_progress)
342 return -EBUSY;
343
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200344 if (genpd->poweroff_task) {
345 /*
346 * Another instance of pm_genpd_poweroff() is executing
347 * callbacks, so tell it to start over and return.
348 */
349 genpd->status = GPD_STATE_REPEAT;
350 return 0;
351 }
352
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200353 if (genpd->gov && genpd->gov->power_down_ok) {
354 if (!genpd->gov->power_down_ok(&genpd->domain))
355 return -EAGAIN;
356 }
357
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200358 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200359 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200360
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200361 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200362 ret = atomic_read(&genpd->sd_count) == 0 ?
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200363 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200364
365 if (genpd_abort_poweroff(genpd))
366 goto out;
367
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200368 if (ret) {
369 genpd_set_active(genpd);
370 goto out;
371 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200372
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200373 if (genpd->status == GPD_STATE_REPEAT) {
374 genpd->poweroff_task = NULL;
375 goto start;
376 }
377 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200378
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200379 if (genpd->power_off) {
380 if (atomic_read(&genpd->sd_count) > 0) {
381 ret = -EBUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200382 goto out;
383 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200384
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200385 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200386 * If sd_count > 0 at this point, one of the subdomains hasn't
387 * managed to call pm_genpd_poweron() for the master yet after
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200388 * incrementing it. In that case pm_genpd_poweron() will wait
389 * for us to drop the lock, so we can call .power_off() and let
390 * the pm_genpd_poweron() restore power for us (this shouldn't
391 * happen very often).
392 */
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200393 ret = genpd->power_off(genpd);
394 if (ret == -EBUSY) {
395 genpd_set_active(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200396 goto out;
397 }
398 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200399
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200400 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200401
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200402 list_for_each_entry(link, &genpd->slave_links, slave_node) {
403 genpd_sd_counter_dec(link->master);
404 genpd_queue_power_off_work(link->master);
405 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200406
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200407 out:
408 genpd->poweroff_task = NULL;
409 wake_up_all(&genpd->status_wait_queue);
410 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200411}
412
413/**
414 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
415 * @work: Work structure used for scheduling the execution of this function.
416 */
417static void genpd_power_off_work_fn(struct work_struct *work)
418{
419 struct generic_pm_domain *genpd;
420
421 genpd = container_of(work, struct generic_pm_domain, power_off_work);
422
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200423 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200424 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200425 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200426}
427
428/**
429 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
430 * @dev: Device to suspend.
431 *
432 * Carry out a runtime suspend of a device under the assumption that its
433 * pm_domain field points to the domain member of an object of type
434 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
435 */
436static int pm_genpd_runtime_suspend(struct device *dev)
437{
438 struct generic_pm_domain *genpd;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100439 bool (*stop_ok)(struct device *__dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100440 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200441
442 dev_dbg(dev, "%s()\n", __func__);
443
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200444 genpd = dev_to_genpd(dev);
445 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200446 return -EINVAL;
447
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200448 might_sleep_if(!genpd->dev_irq_safe);
449
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100450 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
451 if (stop_ok && !stop_ok(dev))
452 return -EBUSY;
453
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100454 ret = genpd_stop_dev(genpd, dev);
455 if (ret)
456 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200457
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100458 pm_runtime_update_max_time_suspended(dev,
459 dev_gpd_data(dev)->td.start_latency_ns);
460
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200461 /*
462 * If power.irq_safe is set, this routine will be run with interrupts
463 * off, so it can't use mutexes.
464 */
465 if (dev->power.irq_safe)
466 return 0;
467
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200468 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200469 genpd->in_progress++;
470 pm_genpd_poweroff(genpd);
471 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200472 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200473
474 return 0;
475}
476
477/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200478 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
479 * @dev: Device to resume.
480 *
481 * Carry out a runtime resume of a device under the assumption that its
482 * pm_domain field points to the domain member of an object of type
483 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
484 */
485static int pm_genpd_runtime_resume(struct device *dev)
486{
487 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200488 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200489 int ret;
490
491 dev_dbg(dev, "%s()\n", __func__);
492
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200493 genpd = dev_to_genpd(dev);
494 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200495 return -EINVAL;
496
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200497 might_sleep_if(!genpd->dev_irq_safe);
498
499 /* If power.irq_safe, the PM domain is never powered off. */
500 if (dev->power.irq_safe)
501 goto out;
502
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200503 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200504 ret = __pm_genpd_poweron(genpd);
505 if (ret) {
506 mutex_unlock(&genpd->lock);
507 return ret;
508 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200509 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200510 genpd->resume_count++;
511 for (;;) {
512 prepare_to_wait(&genpd->status_wait_queue, &wait,
513 TASK_UNINTERRUPTIBLE);
514 /*
515 * If current is the powering off task, we have been called
516 * reentrantly from one of the device callbacks, so we should
517 * not wait.
518 */
519 if (!genpd->poweroff_task || genpd->poweroff_task == current)
520 break;
521 mutex_unlock(&genpd->lock);
522
523 schedule();
524
525 mutex_lock(&genpd->lock);
526 }
527 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200528 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200529 genpd->resume_count--;
530 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200531 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200532 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200533
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200534 out:
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100535 genpd_start_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200536
537 return 0;
538}
539
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200540/**
541 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
542 */
543void pm_genpd_poweroff_unused(void)
544{
545 struct generic_pm_domain *genpd;
546
547 mutex_lock(&gpd_list_lock);
548
549 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
550 genpd_queue_power_off_work(genpd);
551
552 mutex_unlock(&gpd_list_lock);
553}
554
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200555#else
556
557static inline void genpd_power_off_work_fn(struct work_struct *work) {}
558
559#define pm_genpd_runtime_suspend NULL
560#define pm_genpd_runtime_resume NULL
561
562#endif /* CONFIG_PM_RUNTIME */
563
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200564#ifdef CONFIG_PM_SLEEP
565
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100566static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
567 struct device *dev)
568{
569 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
570}
571
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100572static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
573{
574 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
575}
576
577static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
578{
579 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
580}
581
582static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
583{
584 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
585}
586
587static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
588{
589 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
590}
591
592static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
593{
594 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
595}
596
597static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
598{
599 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
600}
601
602static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
603{
604 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
605}
606
607static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
608{
609 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
610}
611
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200612/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200613 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200614 * @genpd: PM domain to power off, if possible.
615 *
616 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200617 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200618 *
619 * This function is only called in "noirq" stages of system power transitions,
620 * so it need not acquire locks (all of the "noirq" callbacks are executed
621 * sequentially, so it is guaranteed that it will never run twice in parallel).
622 */
623static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
624{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200625 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200626
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200627 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200628 return;
629
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200630 if (genpd->suspended_count != genpd->device_count
631 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200632 return;
633
634 if (genpd->power_off)
635 genpd->power_off(genpd);
636
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200637 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200638
639 list_for_each_entry(link, &genpd->slave_links, slave_node) {
640 genpd_sd_counter_dec(link->master);
641 pm_genpd_sync_poweroff(link->master);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200642 }
643}
644
645/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200646 * resume_needed - Check whether to resume a device before system suspend.
647 * @dev: Device to check.
648 * @genpd: PM domain the device belongs to.
649 *
650 * There are two cases in which a device that can wake up the system from sleep
651 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
652 * to wake up the system and it has to remain active for this purpose while the
653 * system is in the sleep state and (2) if the device is not enabled to wake up
654 * the system from sleep states and it generally doesn't generate wakeup signals
655 * by itself (those signals are generated on its behalf by other parts of the
656 * system). In the latter case it may be necessary to reconfigure the device's
657 * wakeup settings during system suspend, because it may have been set up to
658 * signal remote wakeup from the system's working state as needed by runtime PM.
659 * Return 'true' in either of the above cases.
660 */
661static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
662{
663 bool active_wakeup;
664
665 if (!device_can_wakeup(dev))
666 return false;
667
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100668 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200669 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
670}
671
672/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200673 * pm_genpd_prepare - Start power transition of a device in a PM domain.
674 * @dev: Device to start the transition of.
675 *
676 * Start a power transition of a device (during a system-wide power transition)
677 * under the assumption that its pm_domain field points to the domain member of
678 * an object of type struct generic_pm_domain representing a PM domain
679 * consisting of I/O devices.
680 */
681static int pm_genpd_prepare(struct device *dev)
682{
683 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200684 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200685
686 dev_dbg(dev, "%s()\n", __func__);
687
688 genpd = dev_to_genpd(dev);
689 if (IS_ERR(genpd))
690 return -EINVAL;
691
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200692 /*
693 * If a wakeup request is pending for the device, it should be woken up
694 * at this point and a system wakeup event should be reported if it's
695 * set up to wake up the system from sleep states.
696 */
697 pm_runtime_get_noresume(dev);
698 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
699 pm_wakeup_event(dev, 0);
700
701 if (pm_wakeup_pending()) {
702 pm_runtime_put_sync(dev);
703 return -EBUSY;
704 }
705
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200706 if (resume_needed(dev, genpd))
707 pm_runtime_resume(dev);
708
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200709 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200710
711 if (genpd->prepared_count++ == 0)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200712 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
713
714 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200715
716 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200717 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200718 return 0;
719 }
720
721 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200722 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
723 * so pm_genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100724 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200725 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200726 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200727 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200728 __pm_runtime_disable(dev, false);
729
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200730 ret = pm_generic_prepare(dev);
731 if (ret) {
732 mutex_lock(&genpd->lock);
733
734 if (--genpd->prepared_count == 0)
735 genpd->suspend_power_off = false;
736
737 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200738 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200739 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200740
741 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200742 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200743}
744
745/**
746 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
747 * @dev: Device to suspend.
748 *
749 * Suspend a device under the assumption that its pm_domain field points to the
750 * domain member of an object of type struct generic_pm_domain representing
751 * a PM domain consisting of I/O devices.
752 */
753static int pm_genpd_suspend(struct device *dev)
754{
755 struct generic_pm_domain *genpd;
756
757 dev_dbg(dev, "%s()\n", __func__);
758
759 genpd = dev_to_genpd(dev);
760 if (IS_ERR(genpd))
761 return -EINVAL;
762
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100763 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200764}
765
766/**
767 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
768 * @dev: Device to suspend.
769 *
770 * Carry out a late suspend of a device under the assumption that its
771 * pm_domain field points to the domain member of an object of type
772 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
773 */
774static int pm_genpd_suspend_noirq(struct device *dev)
775{
776 struct generic_pm_domain *genpd;
777 int ret;
778
779 dev_dbg(dev, "%s()\n", __func__);
780
781 genpd = dev_to_genpd(dev);
782 if (IS_ERR(genpd))
783 return -EINVAL;
784
785 if (genpd->suspend_power_off)
786 return 0;
787
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100788 ret = genpd_suspend_late(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200789 if (ret)
790 return ret;
791
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100792 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200793 return 0;
794
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100795 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200796
797 /*
798 * Since all of the "noirq" callbacks are executed sequentially, it is
799 * guaranteed that this function will never run twice in parallel for
800 * the same PM domain, so it is not necessary to use locking here.
801 */
802 genpd->suspended_count++;
803 pm_genpd_sync_poweroff(genpd);
804
805 return 0;
806}
807
808/**
809 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
810 * @dev: Device to resume.
811 *
812 * Carry out an early resume of a device under the assumption that its
813 * pm_domain field points to the domain member of an object of type
814 * struct generic_pm_domain representing a power domain consisting of I/O
815 * devices.
816 */
817static int pm_genpd_resume_noirq(struct device *dev)
818{
819 struct generic_pm_domain *genpd;
820
821 dev_dbg(dev, "%s()\n", __func__);
822
823 genpd = dev_to_genpd(dev);
824 if (IS_ERR(genpd))
825 return -EINVAL;
826
827 if (genpd->suspend_power_off)
828 return 0;
829
830 /*
831 * Since all of the "noirq" callbacks are executed sequentially, it is
832 * guaranteed that this function will never run twice in parallel for
833 * the same PM domain, so it is not necessary to use locking here.
834 */
835 pm_genpd_poweron(genpd);
836 genpd->suspended_count--;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100837 genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200838
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100839 return genpd_resume_early(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200840}
841
842/**
843 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
844 * @dev: Device to resume.
845 *
846 * Resume a device under the assumption that its pm_domain field points to the
847 * domain member of an object of type struct generic_pm_domain representing
848 * a power domain consisting of I/O devices.
849 */
850static int pm_genpd_resume(struct device *dev)
851{
852 struct generic_pm_domain *genpd;
853
854 dev_dbg(dev, "%s()\n", __func__);
855
856 genpd = dev_to_genpd(dev);
857 if (IS_ERR(genpd))
858 return -EINVAL;
859
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100860 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200861}
862
863/**
864 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
865 * @dev: Device to freeze.
866 *
867 * Freeze a device under the assumption that its pm_domain field points to the
868 * domain member of an object of type struct generic_pm_domain representing
869 * a power domain consisting of I/O devices.
870 */
871static int pm_genpd_freeze(struct device *dev)
872{
873 struct generic_pm_domain *genpd;
874
875 dev_dbg(dev, "%s()\n", __func__);
876
877 genpd = dev_to_genpd(dev);
878 if (IS_ERR(genpd))
879 return -EINVAL;
880
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100881 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200882}
883
884/**
885 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
886 * @dev: Device to freeze.
887 *
888 * Carry out a late freeze of a device under the assumption that its
889 * pm_domain field points to the domain member of an object of type
890 * struct generic_pm_domain representing a power domain consisting of I/O
891 * devices.
892 */
893static int pm_genpd_freeze_noirq(struct device *dev)
894{
895 struct generic_pm_domain *genpd;
896 int ret;
897
898 dev_dbg(dev, "%s()\n", __func__);
899
900 genpd = dev_to_genpd(dev);
901 if (IS_ERR(genpd))
902 return -EINVAL;
903
904 if (genpd->suspend_power_off)
905 return 0;
906
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100907 ret = genpd_freeze_late(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200908 if (ret)
909 return ret;
910
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100911 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200912
913 return 0;
914}
915
916/**
917 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
918 * @dev: Device to thaw.
919 *
920 * Carry out an early thaw of a device under the assumption that its
921 * pm_domain field points to the domain member of an object of type
922 * struct generic_pm_domain representing a power domain consisting of I/O
923 * devices.
924 */
925static int pm_genpd_thaw_noirq(struct device *dev)
926{
927 struct generic_pm_domain *genpd;
928
929 dev_dbg(dev, "%s()\n", __func__);
930
931 genpd = dev_to_genpd(dev);
932 if (IS_ERR(genpd))
933 return -EINVAL;
934
935 if (genpd->suspend_power_off)
936 return 0;
937
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100938 genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200939
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100940 return genpd_thaw_early(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200941}
942
943/**
944 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
945 * @dev: Device to thaw.
946 *
947 * Thaw a device under the assumption that its pm_domain field points to the
948 * domain member of an object of type struct generic_pm_domain representing
949 * a power domain consisting of I/O devices.
950 */
951static int pm_genpd_thaw(struct device *dev)
952{
953 struct generic_pm_domain *genpd;
954
955 dev_dbg(dev, "%s()\n", __func__);
956
957 genpd = dev_to_genpd(dev);
958 if (IS_ERR(genpd))
959 return -EINVAL;
960
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100961 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200962}
963
964/**
965 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
966 * @dev: Device to resume.
967 *
968 * Carry out an early restore of a device under the assumption that its
969 * pm_domain field points to the domain member of an object of type
970 * struct generic_pm_domain representing a power domain consisting of I/O
971 * devices.
972 */
973static int pm_genpd_restore_noirq(struct device *dev)
974{
975 struct generic_pm_domain *genpd;
976
977 dev_dbg(dev, "%s()\n", __func__);
978
979 genpd = dev_to_genpd(dev);
980 if (IS_ERR(genpd))
981 return -EINVAL;
982
983 /*
984 * Since all of the "noirq" callbacks are executed sequentially, it is
985 * guaranteed that this function will never run twice in parallel for
986 * the same PM domain, so it is not necessary to use locking here.
987 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200988 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200989 if (genpd->suspend_power_off) {
990 /*
991 * The boot kernel might put the domain into the power on state,
992 * so make sure it really is powered off.
993 */
994 if (genpd->power_off)
995 genpd->power_off(genpd);
996 return 0;
997 }
998
999 pm_genpd_poweron(genpd);
1000 genpd->suspended_count--;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001001 genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001002
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001003 return genpd_resume_early(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001004}
1005
1006/**
1007 * pm_genpd_complete - Complete power transition of a device in a power domain.
1008 * @dev: Device to complete the transition of.
1009 *
1010 * Complete a power transition of a device (during a system-wide power
1011 * transition) under the assumption that its pm_domain field points to the
1012 * domain member of an object of type struct generic_pm_domain representing
1013 * a power domain consisting of I/O devices.
1014 */
1015static void pm_genpd_complete(struct device *dev)
1016{
1017 struct generic_pm_domain *genpd;
1018 bool run_complete;
1019
1020 dev_dbg(dev, "%s()\n", __func__);
1021
1022 genpd = dev_to_genpd(dev);
1023 if (IS_ERR(genpd))
1024 return;
1025
1026 mutex_lock(&genpd->lock);
1027
1028 run_complete = !genpd->suspend_power_off;
1029 if (--genpd->prepared_count == 0)
1030 genpd->suspend_power_off = false;
1031
1032 mutex_unlock(&genpd->lock);
1033
1034 if (run_complete) {
1035 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001036 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001037 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001038 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001039 }
1040}
1041
1042#else
1043
1044#define pm_genpd_prepare NULL
1045#define pm_genpd_suspend NULL
1046#define pm_genpd_suspend_noirq NULL
1047#define pm_genpd_resume_noirq NULL
1048#define pm_genpd_resume NULL
1049#define pm_genpd_freeze NULL
1050#define pm_genpd_freeze_noirq NULL
1051#define pm_genpd_thaw_noirq NULL
1052#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001053#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001054#define pm_genpd_complete NULL
1055
1056#endif /* CONFIG_PM_SLEEP */
1057
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001058/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001059 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001060 * @genpd: PM domain to add the device to.
1061 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001062 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001063 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001064int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1065 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001066{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001067 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001068 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001069 int ret = 0;
1070
1071 dev_dbg(dev, "%s()\n", __func__);
1072
1073 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1074 return -EINVAL;
1075
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001076 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001077
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001078 if (genpd->status == GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001079 ret = -EINVAL;
1080 goto out;
1081 }
1082
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001083 if (genpd->prepared_count > 0) {
1084 ret = -EAGAIN;
1085 goto out;
1086 }
1087
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001088 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1089 if (pdd->dev == dev) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001090 ret = -EINVAL;
1091 goto out;
1092 }
1093
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001094 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1095 if (!gpd_data) {
1096 ret = -ENOMEM;
1097 goto out;
1098 }
1099
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001100 genpd->device_count++;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001101
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001102 dev->pm_domain = &genpd->domain;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001103 dev_pm_get_subsys_data(dev);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001104 dev->power.subsys_data->domain_data = &gpd_data->base;
1105 gpd_data->base.dev = dev;
1106 gpd_data->need_restore = false;
1107 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001108 if (td)
1109 gpd_data->td = *td;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001110
1111 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001112 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001113
1114 return ret;
1115}
1116
1117/**
1118 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1119 * @genpd: PM domain to remove the device from.
1120 * @dev: Device to be removed.
1121 */
1122int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1123 struct device *dev)
1124{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001125 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001126 int ret = -EINVAL;
1127
1128 dev_dbg(dev, "%s()\n", __func__);
1129
1130 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1131 return -EINVAL;
1132
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001133 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001134
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001135 if (genpd->prepared_count > 0) {
1136 ret = -EAGAIN;
1137 goto out;
1138 }
1139
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001140 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
1141 if (pdd->dev != dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001142 continue;
1143
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001144 list_del_init(&pdd->list_node);
1145 pdd->dev = NULL;
1146 dev_pm_put_subsys_data(dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001147 dev->pm_domain = NULL;
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001148 kfree(to_gpd_data(pdd));
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001149
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001150 genpd->device_count--;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001151
1152 ret = 0;
1153 break;
1154 }
1155
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001156 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001157 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001158
1159 return ret;
1160}
1161
1162/**
1163 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1164 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001165 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001166 */
1167int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001168 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001169{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001170 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001171 int ret = 0;
1172
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001173 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001174 return -EINVAL;
1175
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001176 start:
1177 genpd_acquire_lock(genpd);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001178 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001179
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001180 if (subdomain->status != GPD_STATE_POWER_OFF
1181 && subdomain->status != GPD_STATE_ACTIVE) {
1182 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001183 genpd_release_lock(genpd);
1184 goto start;
1185 }
1186
1187 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001188 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001189 ret = -EINVAL;
1190 goto out;
1191 }
1192
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001193 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001194 if (link->slave == subdomain && link->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001195 ret = -EINVAL;
1196 goto out;
1197 }
1198 }
1199
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001200 link = kzalloc(sizeof(*link), GFP_KERNEL);
1201 if (!link) {
1202 ret = -ENOMEM;
1203 goto out;
1204 }
1205 link->master = genpd;
1206 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001207 link->slave = subdomain;
1208 list_add_tail(&link->slave_node, &subdomain->slave_links);
1209 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001210 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001211
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001212 out:
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001213 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001214 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001215
1216 return ret;
1217}
1218
1219/**
1220 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1221 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001222 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001223 */
1224int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001225 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001226{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001227 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001228 int ret = -EINVAL;
1229
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001230 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001231 return -EINVAL;
1232
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001233 start:
1234 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001235
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001236 list_for_each_entry(link, &genpd->master_links, master_node) {
1237 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001238 continue;
1239
1240 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1241
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001242 if (subdomain->status != GPD_STATE_POWER_OFF
1243 && subdomain->status != GPD_STATE_ACTIVE) {
1244 mutex_unlock(&subdomain->lock);
1245 genpd_release_lock(genpd);
1246 goto start;
1247 }
1248
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001249 list_del(&link->master_node);
1250 list_del(&link->slave_node);
1251 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001252 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001253 genpd_sd_counter_dec(genpd);
1254
1255 mutex_unlock(&subdomain->lock);
1256
1257 ret = 0;
1258 break;
1259 }
1260
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001261 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001262
1263 return ret;
1264}
1265
1266/**
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001267 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1268 * @dev: Device to add the callbacks to.
1269 * @ops: Set of callbacks to add.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001270 * @td: Timing data to add to the device along with the callbacks (optional).
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001271 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001272int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1273 struct gpd_timing_data *td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001274{
1275 struct pm_domain_data *pdd;
1276 int ret = 0;
1277
1278 if (!(dev && dev->power.subsys_data && ops))
1279 return -EINVAL;
1280
1281 pm_runtime_disable(dev);
1282 device_pm_lock();
1283
1284 pdd = dev->power.subsys_data->domain_data;
1285 if (pdd) {
1286 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1287
1288 gpd_data->ops = *ops;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001289 if (td)
1290 gpd_data->td = *td;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001291 } else {
1292 ret = -EINVAL;
1293 }
1294
1295 device_pm_unlock();
1296 pm_runtime_enable(dev);
1297
1298 return ret;
1299}
1300EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1301
1302/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001303 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001304 * @dev: Device to remove the callbacks from.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001305 * @clear_td: If set, clear the device's timing data too.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001306 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001307int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001308{
1309 struct pm_domain_data *pdd;
1310 int ret = 0;
1311
1312 if (!(dev && dev->power.subsys_data))
1313 return -EINVAL;
1314
1315 pm_runtime_disable(dev);
1316 device_pm_lock();
1317
1318 pdd = dev->power.subsys_data->domain_data;
1319 if (pdd) {
1320 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1321
1322 gpd_data->ops = (struct gpd_dev_ops){ 0 };
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001323 if (clear_td)
1324 gpd_data->td = (struct gpd_timing_data){ 0 };
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001325 } else {
1326 ret = -EINVAL;
1327 }
1328
1329 device_pm_unlock();
1330 pm_runtime_enable(dev);
1331
1332 return ret;
1333}
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001334EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001335
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001336/* Default device callbacks for generic PM domains. */
1337
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001338/**
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001339 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1340 * @dev: Device to handle.
1341 */
1342static int pm_genpd_default_save_state(struct device *dev)
1343{
1344 int (*cb)(struct device *__dev);
1345 struct device_driver *drv = dev->driver;
1346
1347 cb = dev_gpd_data(dev)->ops.save_state;
1348 if (cb)
1349 return cb(dev);
1350
1351 if (drv && drv->pm && drv->pm->runtime_suspend)
1352 return drv->pm->runtime_suspend(dev);
1353
1354 return 0;
1355}
1356
1357/**
1358 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1359 * @dev: Device to handle.
1360 */
1361static int pm_genpd_default_restore_state(struct device *dev)
1362{
1363 int (*cb)(struct device *__dev);
1364 struct device_driver *drv = dev->driver;
1365
1366 cb = dev_gpd_data(dev)->ops.restore_state;
1367 if (cb)
1368 return cb(dev);
1369
1370 if (drv && drv->pm && drv->pm->runtime_resume)
1371 return drv->pm->runtime_resume(dev);
1372
1373 return 0;
1374}
1375
1376/**
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001377 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
1378 * @dev: Device to handle.
1379 */
1380static int pm_genpd_default_suspend(struct device *dev)
1381{
1382 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
1383
1384 return cb ? cb(dev) : pm_generic_suspend(dev);
1385}
1386
1387/**
1388 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
1389 * @dev: Device to handle.
1390 */
1391static int pm_genpd_default_suspend_late(struct device *dev)
1392{
1393 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
1394
1395 return cb ? cb(dev) : pm_generic_suspend_noirq(dev);
1396}
1397
1398/**
1399 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
1400 * @dev: Device to handle.
1401 */
1402static int pm_genpd_default_resume_early(struct device *dev)
1403{
1404 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
1405
1406 return cb ? cb(dev) : pm_generic_resume_noirq(dev);
1407}
1408
1409/**
1410 * pm_genpd_default_resume - Default "device resume" for PM domians.
1411 * @dev: Device to handle.
1412 */
1413static int pm_genpd_default_resume(struct device *dev)
1414{
1415 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
1416
1417 return cb ? cb(dev) : pm_generic_resume(dev);
1418}
1419
1420/**
1421 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
1422 * @dev: Device to handle.
1423 */
1424static int pm_genpd_default_freeze(struct device *dev)
1425{
1426 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
1427
1428 return cb ? cb(dev) : pm_generic_freeze(dev);
1429}
1430
1431/**
1432 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
1433 * @dev: Device to handle.
1434 */
1435static int pm_genpd_default_freeze_late(struct device *dev)
1436{
1437 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
1438
1439 return cb ? cb(dev) : pm_generic_freeze_noirq(dev);
1440}
1441
1442/**
1443 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
1444 * @dev: Device to handle.
1445 */
1446static int pm_genpd_default_thaw_early(struct device *dev)
1447{
1448 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
1449
1450 return cb ? cb(dev) : pm_generic_thaw_noirq(dev);
1451}
1452
1453/**
1454 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
1455 * @dev: Device to handle.
1456 */
1457static int pm_genpd_default_thaw(struct device *dev)
1458{
1459 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
1460
1461 return cb ? cb(dev) : pm_generic_thaw(dev);
1462}
1463
1464/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001465 * pm_genpd_init - Initialize a generic I/O PM domain object.
1466 * @genpd: PM domain object to initialize.
1467 * @gov: PM domain governor to associate with the domain (may be NULL).
1468 * @is_off: Initial value of the domain's power_is_off field.
1469 */
1470void pm_genpd_init(struct generic_pm_domain *genpd,
1471 struct dev_power_governor *gov, bool is_off)
1472{
1473 if (IS_ERR_OR_NULL(genpd))
1474 return;
1475
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001476 INIT_LIST_HEAD(&genpd->master_links);
1477 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001478 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001479 mutex_init(&genpd->lock);
1480 genpd->gov = gov;
1481 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1482 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001483 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001484 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1485 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001486 genpd->poweroff_task = NULL;
1487 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001488 genpd->device_count = 0;
1489 genpd->suspended_count = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001490 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1491 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1492 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001493 genpd->domain.ops.prepare = pm_genpd_prepare;
1494 genpd->domain.ops.suspend = pm_genpd_suspend;
1495 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1496 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1497 genpd->domain.ops.resume = pm_genpd_resume;
1498 genpd->domain.ops.freeze = pm_genpd_freeze;
1499 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1500 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1501 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001502 genpd->domain.ops.poweroff = pm_genpd_suspend;
1503 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001504 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001505 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001506 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001507 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1508 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001509 genpd->dev_ops.freeze = pm_genpd_default_suspend;
1510 genpd->dev_ops.freeze_late = pm_genpd_default_suspend_late;
1511 genpd->dev_ops.thaw_early = pm_genpd_default_resume_early;
1512 genpd->dev_ops.thaw = pm_genpd_default_resume;
1513 genpd->dev_ops.freeze = pm_genpd_default_freeze;
1514 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
1515 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
1516 genpd->dev_ops.thaw = pm_genpd_default_thaw;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001517 mutex_lock(&gpd_list_lock);
1518 list_add(&genpd->gpd_list_node, &gpd_list);
1519 mutex_unlock(&gpd_list_lock);
1520}