blob: c2c537de22b69b07926ae446acf101d34a0e0b1d [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. Wysockif7218892011-07-01 22:12:45 +020018
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +020019static LIST_HEAD(gpd_list);
20static DEFINE_MUTEX(gpd_list_lock);
21
Rafael J. Wysocki52480512011-07-01 22:13:10 +020022#ifdef CONFIG_PM
23
24static struct generic_pm_domain *dev_to_genpd(struct device *dev)
25{
26 if (IS_ERR_OR_NULL(dev->pm_domain))
27 return ERR_PTR(-EINVAL);
28
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020029 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020030}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020031
32static void genpd_sd_counter_dec(struct generic_pm_domain *genpd)
33{
34 if (!WARN_ON(genpd->sd_count == 0))
35 genpd->sd_count--;
36}
37
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020038static void genpd_acquire_lock(struct generic_pm_domain *genpd)
39{
40 DEFINE_WAIT(wait);
41
42 mutex_lock(&genpd->lock);
43 /*
44 * Wait for the domain to transition into either the active,
45 * or the power off state.
46 */
47 for (;;) {
48 prepare_to_wait(&genpd->status_wait_queue, &wait,
49 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020050 if (genpd->status == GPD_STATE_ACTIVE
51 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020052 break;
53 mutex_unlock(&genpd->lock);
54
55 schedule();
56
57 mutex_lock(&genpd->lock);
58 }
59 finish_wait(&genpd->status_wait_queue, &wait);
60}
61
62static void genpd_release_lock(struct generic_pm_domain *genpd)
63{
64 mutex_unlock(&genpd->lock);
65}
66
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020067static void genpd_set_active(struct generic_pm_domain *genpd)
68{
69 if (genpd->resume_count == 0)
70 genpd->status = GPD_STATE_ACTIVE;
71}
72
Rafael J. Wysockif7218892011-07-01 22:12:45 +020073/**
Rafael J. Wysocki52480512011-07-01 22:13:10 +020074 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
75 * @genpd: PM domain to power up.
76 *
77 * Restore power to @genpd and all of its parents so that it is possible to
78 * resume a device belonging to it.
79 */
Magnus Damm18b4f3f2011-07-10 10:39:14 +020080int pm_genpd_poweron(struct generic_pm_domain *genpd)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020081{
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020082 struct generic_pm_domain *parent = genpd->parent;
83 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020084 int ret = 0;
85
86 start:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020087 if (parent) {
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020088 genpd_acquire_lock(parent);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020089 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
90 } else {
91 mutex_lock(&genpd->lock);
92 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020093
94 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020095 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki52480512011-07-01 22:13:10 +020096 goto out;
97
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020098 if (genpd->status != GPD_STATE_POWER_OFF) {
99 genpd_set_active(genpd);
100 goto out;
101 }
102
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200103 if (parent && parent->status != GPD_STATE_ACTIVE) {
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200104 mutex_unlock(&genpd->lock);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200105 genpd_release_lock(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200106
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200107 ret = pm_genpd_poweron(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200108 if (ret)
109 return ret;
110
111 goto start;
112 }
113
114 if (genpd->power_on) {
115 int ret = genpd->power_on(genpd);
116 if (ret)
117 goto out;
118 }
119
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200120 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200121 if (parent)
122 parent->sd_count++;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200123
124 out:
125 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200126 if (parent)
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200127 genpd_release_lock(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200128
129 return ret;
130}
131
132#endif /* CONFIG_PM */
133
134#ifdef CONFIG_PM_RUNTIME
135
136/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200137 * __pm_genpd_save_device - Save the pre-suspend state of a device.
138 * @dle: Device list entry of the device to save the state of.
139 * @genpd: PM domain the device belongs to.
140 */
141static int __pm_genpd_save_device(struct dev_list_entry *dle,
142 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200143 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200144{
145 struct device *dev = dle->dev;
146 struct device_driver *drv = dev->driver;
147 int ret = 0;
148
149 if (dle->need_restore)
150 return 0;
151
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200152 mutex_unlock(&genpd->lock);
153
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200154 if (drv && drv->pm && drv->pm->runtime_suspend) {
155 if (genpd->start_device)
156 genpd->start_device(dev);
157
158 ret = drv->pm->runtime_suspend(dev);
159
160 if (genpd->stop_device)
161 genpd->stop_device(dev);
162 }
163
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200164 mutex_lock(&genpd->lock);
165
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200166 if (!ret)
167 dle->need_restore = true;
168
169 return ret;
170}
171
172/**
173 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
174 * @dle: Device list entry of the device to restore the state of.
175 * @genpd: PM domain the device belongs to.
176 */
177static void __pm_genpd_restore_device(struct dev_list_entry *dle,
178 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200179 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200180{
181 struct device *dev = dle->dev;
182 struct device_driver *drv = dev->driver;
183
184 if (!dle->need_restore)
185 return;
186
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200187 mutex_unlock(&genpd->lock);
188
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200189 if (drv && drv->pm && drv->pm->runtime_resume) {
190 if (genpd->start_device)
191 genpd->start_device(dev);
192
193 drv->pm->runtime_resume(dev);
194
195 if (genpd->stop_device)
196 genpd->stop_device(dev);
197 }
198
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200199 mutex_lock(&genpd->lock);
200
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200201 dle->need_restore = false;
202}
203
204/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200205 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
206 * @genpd: PM domain to check.
207 *
208 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
209 * a "power off" operation, which means that a "power on" has occured in the
210 * meantime, or if its resume_count field is different from zero, which means
211 * that one of its devices has been resumed in the meantime.
212 */
213static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
214{
215 return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
216}
217
218/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200219 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
220 * @genpd: PM domait to power off.
221 *
222 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
223 * before.
224 */
225static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
226{
227 if (!work_pending(&genpd->power_off_work))
228 queue_work(pm_wq, &genpd->power_off_work);
229}
230
231/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200232 * pm_genpd_poweroff - Remove power from a given PM domain.
233 * @genpd: PM domain to power down.
234 *
235 * If all of the @genpd's devices have been suspended and all of its subdomains
236 * have been powered down, run the runtime suspend callbacks provided by all of
237 * the @genpd's devices' drivers and remove power from @genpd.
238 */
239static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200240 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200241{
242 struct generic_pm_domain *parent;
243 struct dev_list_entry *dle;
244 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200245 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200246
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200247 start:
248 /*
249 * Do not try to power off the domain in the following situations:
250 * (1) The domain is already in the "power off" state.
251 * (2) System suspend is in progress.
252 * (3) One of the domain's devices is being resumed right now.
253 */
254 if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0
255 || genpd->resume_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200256 return 0;
257
258 if (genpd->sd_count > 0)
259 return -EBUSY;
260
261 not_suspended = 0;
262 list_for_each_entry(dle, &genpd->dev_list, node)
263 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
264 not_suspended++;
265
266 if (not_suspended > genpd->in_progress)
267 return -EBUSY;
268
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200269 if (genpd->poweroff_task) {
270 /*
271 * Another instance of pm_genpd_poweroff() is executing
272 * callbacks, so tell it to start over and return.
273 */
274 genpd->status = GPD_STATE_REPEAT;
275 return 0;
276 }
277
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200278 if (genpd->gov && genpd->gov->power_down_ok) {
279 if (!genpd->gov->power_down_ok(&genpd->domain))
280 return -EAGAIN;
281 }
282
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200283 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200284 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200285
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200286 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
287 ret = __pm_genpd_save_device(dle, genpd);
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200288 if (ret) {
289 genpd_set_active(genpd);
290 goto out;
291 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200292
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200293 if (genpd_abort_poweroff(genpd))
294 goto out;
295
296 if (genpd->status == GPD_STATE_REPEAT) {
297 genpd->poweroff_task = NULL;
298 goto start;
299 }
300 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200301
302 parent = genpd->parent;
303 if (parent) {
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200304 mutex_unlock(&genpd->lock);
305
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200306 genpd_acquire_lock(parent);
307 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200308
309 if (genpd_abort_poweroff(genpd)) {
310 genpd_release_lock(parent);
311 goto out;
312 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200313 }
314
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200315 if (genpd->power_off)
316 genpd->power_off(genpd);
317
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200318 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200319
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200320 if (parent) {
321 genpd_sd_counter_dec(parent);
322 if (parent->sd_count == 0)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200323 genpd_queue_power_off_work(parent);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200324
325 genpd_release_lock(parent);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200326 }
327
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200328 out:
329 genpd->poweroff_task = NULL;
330 wake_up_all(&genpd->status_wait_queue);
331 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200332}
333
334/**
335 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
336 * @work: Work structure used for scheduling the execution of this function.
337 */
338static void genpd_power_off_work_fn(struct work_struct *work)
339{
340 struct generic_pm_domain *genpd;
341
342 genpd = container_of(work, struct generic_pm_domain, power_off_work);
343
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200344 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200345 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200346 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200347}
348
349/**
350 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
351 * @dev: Device to suspend.
352 *
353 * Carry out a runtime suspend of a device under the assumption that its
354 * pm_domain field points to the domain member of an object of type
355 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
356 */
357static int pm_genpd_runtime_suspend(struct device *dev)
358{
359 struct generic_pm_domain *genpd;
360
361 dev_dbg(dev, "%s()\n", __func__);
362
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200363 genpd = dev_to_genpd(dev);
364 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200365 return -EINVAL;
366
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200367 if (genpd->stop_device) {
368 int ret = genpd->stop_device(dev);
369 if (ret)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200370 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200371 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200372
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200373 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200374 genpd->in_progress++;
375 pm_genpd_poweroff(genpd);
376 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200377 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200378
379 return 0;
380}
381
382/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200383 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
384 * @dev: Device to resume.
385 * @genpd: PM domain the device belongs to.
386 */
387static void __pm_genpd_runtime_resume(struct device *dev,
388 struct generic_pm_domain *genpd)
389{
390 struct dev_list_entry *dle;
391
392 list_for_each_entry(dle, &genpd->dev_list, node) {
393 if (dle->dev == dev) {
394 __pm_genpd_restore_device(dle, genpd);
395 break;
396 }
397 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200398}
399
400/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200401 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
402 * @dev: Device to resume.
403 *
404 * Carry out a runtime resume of a device under the assumption that its
405 * pm_domain field points to the domain member of an object of type
406 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
407 */
408static int pm_genpd_runtime_resume(struct device *dev)
409{
410 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200411 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200412 int ret;
413
414 dev_dbg(dev, "%s()\n", __func__);
415
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200416 genpd = dev_to_genpd(dev);
417 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200418 return -EINVAL;
419
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200420 ret = pm_genpd_poweron(genpd);
421 if (ret)
422 return ret;
423
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200424 mutex_lock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200425 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200426 genpd->resume_count++;
427 for (;;) {
428 prepare_to_wait(&genpd->status_wait_queue, &wait,
429 TASK_UNINTERRUPTIBLE);
430 /*
431 * If current is the powering off task, we have been called
432 * reentrantly from one of the device callbacks, so we should
433 * not wait.
434 */
435 if (!genpd->poweroff_task || genpd->poweroff_task == current)
436 break;
437 mutex_unlock(&genpd->lock);
438
439 schedule();
440
441 mutex_lock(&genpd->lock);
442 }
443 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200444 __pm_genpd_runtime_resume(dev, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200445 genpd->resume_count--;
446 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200447 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200448 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200449
450 if (genpd->start_device)
451 genpd->start_device(dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200452
453 return 0;
454}
455
456#else
457
458static inline void genpd_power_off_work_fn(struct work_struct *work) {}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200459static inline void __pm_genpd_runtime_resume(struct device *dev,
460 struct generic_pm_domain *genpd) {}
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200461
462#define pm_genpd_runtime_suspend NULL
463#define pm_genpd_runtime_resume NULL
464
465#endif /* CONFIG_PM_RUNTIME */
466
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200467#ifdef CONFIG_PM_SLEEP
468
469/**
470 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
471 * @genpd: PM domain to power off, if possible.
472 *
473 * Check if the given PM domain can be powered off (during system suspend or
474 * hibernation) and do that if so. Also, in that case propagate to its parent.
475 *
476 * This function is only called in "noirq" stages of system power transitions,
477 * so it need not acquire locks (all of the "noirq" callbacks are executed
478 * sequentially, so it is guaranteed that it will never run twice in parallel).
479 */
480static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
481{
482 struct generic_pm_domain *parent = genpd->parent;
483
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200484 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200485 return;
486
487 if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0)
488 return;
489
490 if (genpd->power_off)
491 genpd->power_off(genpd);
492
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200493 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200494 if (parent) {
495 genpd_sd_counter_dec(parent);
496 pm_genpd_sync_poweroff(parent);
497 }
498}
499
500/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200501 * resume_needed - Check whether to resume a device before system suspend.
502 * @dev: Device to check.
503 * @genpd: PM domain the device belongs to.
504 *
505 * There are two cases in which a device that can wake up the system from sleep
506 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
507 * to wake up the system and it has to remain active for this purpose while the
508 * system is in the sleep state and (2) if the device is not enabled to wake up
509 * the system from sleep states and it generally doesn't generate wakeup signals
510 * by itself (those signals are generated on its behalf by other parts of the
511 * system). In the latter case it may be necessary to reconfigure the device's
512 * wakeup settings during system suspend, because it may have been set up to
513 * signal remote wakeup from the system's working state as needed by runtime PM.
514 * Return 'true' in either of the above cases.
515 */
516static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
517{
518 bool active_wakeup;
519
520 if (!device_can_wakeup(dev))
521 return false;
522
523 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
524 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
525}
526
527/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200528 * pm_genpd_prepare - Start power transition of a device in a PM domain.
529 * @dev: Device to start the transition of.
530 *
531 * Start a power transition of a device (during a system-wide power transition)
532 * under the assumption that its pm_domain field points to the domain member of
533 * an object of type struct generic_pm_domain representing a PM domain
534 * consisting of I/O devices.
535 */
536static int pm_genpd_prepare(struct device *dev)
537{
538 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200539 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200540
541 dev_dbg(dev, "%s()\n", __func__);
542
543 genpd = dev_to_genpd(dev);
544 if (IS_ERR(genpd))
545 return -EINVAL;
546
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200547 /*
548 * If a wakeup request is pending for the device, it should be woken up
549 * at this point and a system wakeup event should be reported if it's
550 * set up to wake up the system from sleep states.
551 */
552 pm_runtime_get_noresume(dev);
553 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
554 pm_wakeup_event(dev, 0);
555
556 if (pm_wakeup_pending()) {
557 pm_runtime_put_sync(dev);
558 return -EBUSY;
559 }
560
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200561 if (resume_needed(dev, genpd))
562 pm_runtime_resume(dev);
563
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200564 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200565
566 if (genpd->prepared_count++ == 0)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200567 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
568
569 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200570
571 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200572 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200573 return 0;
574 }
575
576 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200577 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
578 * so pm_genpd_poweron() will return immediately, but if the device
579 * is suspended (e.g. it's been stopped by .stop_device()), we need
580 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200581 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200582 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200583 __pm_runtime_disable(dev, false);
584
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200585 ret = pm_generic_prepare(dev);
586 if (ret) {
587 mutex_lock(&genpd->lock);
588
589 if (--genpd->prepared_count == 0)
590 genpd->suspend_power_off = false;
591
592 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200593 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200594 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200595
596 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200597 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200598}
599
600/**
601 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
602 * @dev: Device to suspend.
603 *
604 * Suspend a device under the assumption that its pm_domain field points to the
605 * domain member of an object of type struct generic_pm_domain representing
606 * a PM domain consisting of I/O devices.
607 */
608static int pm_genpd_suspend(struct device *dev)
609{
610 struct generic_pm_domain *genpd;
611
612 dev_dbg(dev, "%s()\n", __func__);
613
614 genpd = dev_to_genpd(dev);
615 if (IS_ERR(genpd))
616 return -EINVAL;
617
618 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
619}
620
621/**
622 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
623 * @dev: Device to suspend.
624 *
625 * Carry out a late suspend of a device under the assumption that its
626 * pm_domain field points to the domain member of an object of type
627 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
628 */
629static int pm_genpd_suspend_noirq(struct device *dev)
630{
631 struct generic_pm_domain *genpd;
632 int ret;
633
634 dev_dbg(dev, "%s()\n", __func__);
635
636 genpd = dev_to_genpd(dev);
637 if (IS_ERR(genpd))
638 return -EINVAL;
639
640 if (genpd->suspend_power_off)
641 return 0;
642
643 ret = pm_generic_suspend_noirq(dev);
644 if (ret)
645 return ret;
646
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200647 if (device_may_wakeup(dev)
648 && genpd->active_wakeup && genpd->active_wakeup(dev))
649 return 0;
650
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200651 if (genpd->stop_device)
652 genpd->stop_device(dev);
653
654 /*
655 * Since all of the "noirq" callbacks are executed sequentially, it is
656 * guaranteed that this function will never run twice in parallel for
657 * the same PM domain, so it is not necessary to use locking here.
658 */
659 genpd->suspended_count++;
660 pm_genpd_sync_poweroff(genpd);
661
662 return 0;
663}
664
665/**
666 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
667 * @dev: Device to resume.
668 *
669 * Carry out an early resume of a device under the assumption that its
670 * pm_domain field points to the domain member of an object of type
671 * struct generic_pm_domain representing a power domain consisting of I/O
672 * devices.
673 */
674static int pm_genpd_resume_noirq(struct device *dev)
675{
676 struct generic_pm_domain *genpd;
677
678 dev_dbg(dev, "%s()\n", __func__);
679
680 genpd = dev_to_genpd(dev);
681 if (IS_ERR(genpd))
682 return -EINVAL;
683
684 if (genpd->suspend_power_off)
685 return 0;
686
687 /*
688 * Since all of the "noirq" callbacks are executed sequentially, it is
689 * guaranteed that this function will never run twice in parallel for
690 * the same PM domain, so it is not necessary to use locking here.
691 */
692 pm_genpd_poweron(genpd);
693 genpd->suspended_count--;
694 if (genpd->start_device)
695 genpd->start_device(dev);
696
697 return pm_generic_resume_noirq(dev);
698}
699
700/**
701 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
702 * @dev: Device to resume.
703 *
704 * Resume a device under the assumption that its pm_domain field points to the
705 * domain member of an object of type struct generic_pm_domain representing
706 * a power domain consisting of I/O devices.
707 */
708static int pm_genpd_resume(struct device *dev)
709{
710 struct generic_pm_domain *genpd;
711
712 dev_dbg(dev, "%s()\n", __func__);
713
714 genpd = dev_to_genpd(dev);
715 if (IS_ERR(genpd))
716 return -EINVAL;
717
718 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
719}
720
721/**
722 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
723 * @dev: Device to freeze.
724 *
725 * Freeze a device under the assumption that its pm_domain field points to the
726 * domain member of an object of type struct generic_pm_domain representing
727 * a power domain consisting of I/O devices.
728 */
729static int pm_genpd_freeze(struct device *dev)
730{
731 struct generic_pm_domain *genpd;
732
733 dev_dbg(dev, "%s()\n", __func__);
734
735 genpd = dev_to_genpd(dev);
736 if (IS_ERR(genpd))
737 return -EINVAL;
738
739 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
740}
741
742/**
743 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
744 * @dev: Device to freeze.
745 *
746 * Carry out a late freeze of a device under the assumption that its
747 * pm_domain field points to the domain member of an object of type
748 * struct generic_pm_domain representing a power domain consisting of I/O
749 * devices.
750 */
751static int pm_genpd_freeze_noirq(struct device *dev)
752{
753 struct generic_pm_domain *genpd;
754 int ret;
755
756 dev_dbg(dev, "%s()\n", __func__);
757
758 genpd = dev_to_genpd(dev);
759 if (IS_ERR(genpd))
760 return -EINVAL;
761
762 if (genpd->suspend_power_off)
763 return 0;
764
765 ret = pm_generic_freeze_noirq(dev);
766 if (ret)
767 return ret;
768
769 if (genpd->stop_device)
770 genpd->stop_device(dev);
771
772 return 0;
773}
774
775/**
776 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
777 * @dev: Device to thaw.
778 *
779 * Carry out an early thaw of a device under the assumption that its
780 * pm_domain field points to the domain member of an object of type
781 * struct generic_pm_domain representing a power domain consisting of I/O
782 * devices.
783 */
784static int pm_genpd_thaw_noirq(struct device *dev)
785{
786 struct generic_pm_domain *genpd;
787
788 dev_dbg(dev, "%s()\n", __func__);
789
790 genpd = dev_to_genpd(dev);
791 if (IS_ERR(genpd))
792 return -EINVAL;
793
794 if (genpd->suspend_power_off)
795 return 0;
796
797 if (genpd->start_device)
798 genpd->start_device(dev);
799
800 return pm_generic_thaw_noirq(dev);
801}
802
803/**
804 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
805 * @dev: Device to thaw.
806 *
807 * Thaw a device under the assumption that its pm_domain field points to the
808 * domain member of an object of type struct generic_pm_domain representing
809 * a power domain consisting of I/O devices.
810 */
811static int pm_genpd_thaw(struct device *dev)
812{
813 struct generic_pm_domain *genpd;
814
815 dev_dbg(dev, "%s()\n", __func__);
816
817 genpd = dev_to_genpd(dev);
818 if (IS_ERR(genpd))
819 return -EINVAL;
820
821 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
822}
823
824/**
825 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
826 * @dev: Device to suspend.
827 *
828 * Power off a device under the assumption that its pm_domain field points to
829 * the domain member of an object of type struct generic_pm_domain representing
830 * a PM domain consisting of I/O devices.
831 */
832static int pm_genpd_dev_poweroff(struct device *dev)
833{
834 struct generic_pm_domain *genpd;
835
836 dev_dbg(dev, "%s()\n", __func__);
837
838 genpd = dev_to_genpd(dev);
839 if (IS_ERR(genpd))
840 return -EINVAL;
841
842 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
843}
844
845/**
846 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
847 * @dev: Device to suspend.
848 *
849 * Carry out a late powering off of a device under the assumption that its
850 * pm_domain field points to the domain member of an object of type
851 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
852 */
853static int pm_genpd_dev_poweroff_noirq(struct device *dev)
854{
855 struct generic_pm_domain *genpd;
856 int ret;
857
858 dev_dbg(dev, "%s()\n", __func__);
859
860 genpd = dev_to_genpd(dev);
861 if (IS_ERR(genpd))
862 return -EINVAL;
863
864 if (genpd->suspend_power_off)
865 return 0;
866
867 ret = pm_generic_poweroff_noirq(dev);
868 if (ret)
869 return ret;
870
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200871 if (device_may_wakeup(dev)
872 && genpd->active_wakeup && genpd->active_wakeup(dev))
873 return 0;
874
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200875 if (genpd->stop_device)
876 genpd->stop_device(dev);
877
878 /*
879 * Since all of the "noirq" callbacks are executed sequentially, it is
880 * guaranteed that this function will never run twice in parallel for
881 * the same PM domain, so it is not necessary to use locking here.
882 */
883 genpd->suspended_count++;
884 pm_genpd_sync_poweroff(genpd);
885
886 return 0;
887}
888
889/**
890 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
891 * @dev: Device to resume.
892 *
893 * Carry out an early restore of a device under the assumption that its
894 * pm_domain field points to the domain member of an object of type
895 * struct generic_pm_domain representing a power domain consisting of I/O
896 * devices.
897 */
898static int pm_genpd_restore_noirq(struct device *dev)
899{
900 struct generic_pm_domain *genpd;
901
902 dev_dbg(dev, "%s()\n", __func__);
903
904 genpd = dev_to_genpd(dev);
905 if (IS_ERR(genpd))
906 return -EINVAL;
907
908 /*
909 * Since all of the "noirq" callbacks are executed sequentially, it is
910 * guaranteed that this function will never run twice in parallel for
911 * the same PM domain, so it is not necessary to use locking here.
912 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200913 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200914 if (genpd->suspend_power_off) {
915 /*
916 * The boot kernel might put the domain into the power on state,
917 * so make sure it really is powered off.
918 */
919 if (genpd->power_off)
920 genpd->power_off(genpd);
921 return 0;
922 }
923
924 pm_genpd_poweron(genpd);
925 genpd->suspended_count--;
926 if (genpd->start_device)
927 genpd->start_device(dev);
928
929 return pm_generic_restore_noirq(dev);
930}
931
932/**
933 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
934 * @dev: Device to resume.
935 *
936 * Restore a device under the assumption that its pm_domain field points to the
937 * domain member of an object of type struct generic_pm_domain representing
938 * a power domain consisting of I/O devices.
939 */
940static int pm_genpd_restore(struct device *dev)
941{
942 struct generic_pm_domain *genpd;
943
944 dev_dbg(dev, "%s()\n", __func__);
945
946 genpd = dev_to_genpd(dev);
947 if (IS_ERR(genpd))
948 return -EINVAL;
949
950 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
951}
952
953/**
954 * pm_genpd_complete - Complete power transition of a device in a power domain.
955 * @dev: Device to complete the transition of.
956 *
957 * Complete a power transition of a device (during a system-wide power
958 * transition) under the assumption that its pm_domain field points to the
959 * domain member of an object of type struct generic_pm_domain representing
960 * a power domain consisting of I/O devices.
961 */
962static void pm_genpd_complete(struct device *dev)
963{
964 struct generic_pm_domain *genpd;
965 bool run_complete;
966
967 dev_dbg(dev, "%s()\n", __func__);
968
969 genpd = dev_to_genpd(dev);
970 if (IS_ERR(genpd))
971 return;
972
973 mutex_lock(&genpd->lock);
974
975 run_complete = !genpd->suspend_power_off;
976 if (--genpd->prepared_count == 0)
977 genpd->suspend_power_off = false;
978
979 mutex_unlock(&genpd->lock);
980
981 if (run_complete) {
982 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +0200983 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200984 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +0200985 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200986 }
987}
988
989#else
990
991#define pm_genpd_prepare NULL
992#define pm_genpd_suspend NULL
993#define pm_genpd_suspend_noirq NULL
994#define pm_genpd_resume_noirq NULL
995#define pm_genpd_resume NULL
996#define pm_genpd_freeze NULL
997#define pm_genpd_freeze_noirq NULL
998#define pm_genpd_thaw_noirq NULL
999#define pm_genpd_thaw NULL
1000#define pm_genpd_dev_poweroff_noirq NULL
1001#define pm_genpd_dev_poweroff NULL
1002#define pm_genpd_restore_noirq NULL
1003#define pm_genpd_restore NULL
1004#define pm_genpd_complete NULL
1005
1006#endif /* CONFIG_PM_SLEEP */
1007
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001008/**
1009 * pm_genpd_add_device - Add a device to an I/O PM domain.
1010 * @genpd: PM domain to add the device to.
1011 * @dev: Device to be added.
1012 */
1013int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1014{
1015 struct dev_list_entry *dle;
1016 int ret = 0;
1017
1018 dev_dbg(dev, "%s()\n", __func__);
1019
1020 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1021 return -EINVAL;
1022
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001023 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001024
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001025 if (genpd->status == GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001026 ret = -EINVAL;
1027 goto out;
1028 }
1029
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001030 if (genpd->prepared_count > 0) {
1031 ret = -EAGAIN;
1032 goto out;
1033 }
1034
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001035 list_for_each_entry(dle, &genpd->dev_list, node)
1036 if (dle->dev == dev) {
1037 ret = -EINVAL;
1038 goto out;
1039 }
1040
1041 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1042 if (!dle) {
1043 ret = -ENOMEM;
1044 goto out;
1045 }
1046
1047 dle->dev = dev;
1048 dle->need_restore = false;
1049 list_add_tail(&dle->node, &genpd->dev_list);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001050 genpd->device_count++;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001051
1052 spin_lock_irq(&dev->power.lock);
1053 dev->pm_domain = &genpd->domain;
1054 spin_unlock_irq(&dev->power.lock);
1055
1056 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001057 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001058
1059 return ret;
1060}
1061
1062/**
1063 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1064 * @genpd: PM domain to remove the device from.
1065 * @dev: Device to be removed.
1066 */
1067int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1068 struct device *dev)
1069{
1070 struct dev_list_entry *dle;
1071 int ret = -EINVAL;
1072
1073 dev_dbg(dev, "%s()\n", __func__);
1074
1075 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1076 return -EINVAL;
1077
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001078 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001079
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001080 if (genpd->prepared_count > 0) {
1081 ret = -EAGAIN;
1082 goto out;
1083 }
1084
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001085 list_for_each_entry(dle, &genpd->dev_list, node) {
1086 if (dle->dev != dev)
1087 continue;
1088
1089 spin_lock_irq(&dev->power.lock);
1090 dev->pm_domain = NULL;
1091 spin_unlock_irq(&dev->power.lock);
1092
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001093 genpd->device_count--;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001094 list_del(&dle->node);
1095 kfree(dle);
1096
1097 ret = 0;
1098 break;
1099 }
1100
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001101 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001102 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001103
1104 return ret;
1105}
1106
1107/**
1108 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1109 * @genpd: Master PM domain to add the subdomain to.
1110 * @new_subdomain: Subdomain to be added.
1111 */
1112int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1113 struct generic_pm_domain *new_subdomain)
1114{
1115 struct generic_pm_domain *subdomain;
1116 int ret = 0;
1117
1118 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1119 return -EINVAL;
1120
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001121 start:
1122 genpd_acquire_lock(genpd);
1123 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001124
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001125 if (new_subdomain->status != GPD_STATE_POWER_OFF
1126 && new_subdomain->status != GPD_STATE_ACTIVE) {
1127 mutex_unlock(&new_subdomain->lock);
1128 genpd_release_lock(genpd);
1129 goto start;
1130 }
1131
1132 if (genpd->status == GPD_STATE_POWER_OFF
1133 && new_subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001134 ret = -EINVAL;
1135 goto out;
1136 }
1137
1138 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1139 if (subdomain == new_subdomain) {
1140 ret = -EINVAL;
1141 goto out;
1142 }
1143 }
1144
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001145 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1146 new_subdomain->parent = genpd;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001147 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001148 genpd->sd_count++;
1149
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001150 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001151 mutex_unlock(&new_subdomain->lock);
1152 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001153
1154 return ret;
1155}
1156
1157/**
1158 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1159 * @genpd: Master PM domain to remove the subdomain from.
1160 * @target: Subdomain to be removed.
1161 */
1162int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1163 struct generic_pm_domain *target)
1164{
1165 struct generic_pm_domain *subdomain;
1166 int ret = -EINVAL;
1167
1168 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1169 return -EINVAL;
1170
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001171 start:
1172 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001173
1174 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1175 if (subdomain != target)
1176 continue;
1177
1178 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1179
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001180 if (subdomain->status != GPD_STATE_POWER_OFF
1181 && subdomain->status != GPD_STATE_ACTIVE) {
1182 mutex_unlock(&subdomain->lock);
1183 genpd_release_lock(genpd);
1184 goto start;
1185 }
1186
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001187 list_del(&subdomain->sd_node);
1188 subdomain->parent = NULL;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001189 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001190 genpd_sd_counter_dec(genpd);
1191
1192 mutex_unlock(&subdomain->lock);
1193
1194 ret = 0;
1195 break;
1196 }
1197
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001198 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001199
1200 return ret;
1201}
1202
1203/**
1204 * pm_genpd_init - Initialize a generic I/O PM domain object.
1205 * @genpd: PM domain object to initialize.
1206 * @gov: PM domain governor to associate with the domain (may be NULL).
1207 * @is_off: Initial value of the domain's power_is_off field.
1208 */
1209void pm_genpd_init(struct generic_pm_domain *genpd,
1210 struct dev_power_governor *gov, bool is_off)
1211{
1212 if (IS_ERR_OR_NULL(genpd))
1213 return;
1214
1215 INIT_LIST_HEAD(&genpd->sd_node);
1216 genpd->parent = NULL;
1217 INIT_LIST_HEAD(&genpd->dev_list);
1218 INIT_LIST_HEAD(&genpd->sd_list);
1219 mutex_init(&genpd->lock);
1220 genpd->gov = gov;
1221 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1222 genpd->in_progress = 0;
1223 genpd->sd_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001224 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1225 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001226 genpd->poweroff_task = NULL;
1227 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001228 genpd->device_count = 0;
1229 genpd->suspended_count = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001230 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1231 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1232 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001233 genpd->domain.ops.prepare = pm_genpd_prepare;
1234 genpd->domain.ops.suspend = pm_genpd_suspend;
1235 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1236 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1237 genpd->domain.ops.resume = pm_genpd_resume;
1238 genpd->domain.ops.freeze = pm_genpd_freeze;
1239 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1240 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1241 genpd->domain.ops.thaw = pm_genpd_thaw;
1242 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1243 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1244 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1245 genpd->domain.ops.restore = pm_genpd_restore;
1246 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001247 mutex_lock(&gpd_list_lock);
1248 list_add(&genpd->gpd_list_node, &gpd_list);
1249 mutex_unlock(&gpd_list_lock);
1250}
1251
1252/**
1253 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
1254 */
1255void pm_genpd_poweroff_unused(void)
1256{
1257 struct generic_pm_domain *genpd;
1258
1259 mutex_lock(&gpd_list_lock);
1260
1261 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
1262 genpd_queue_power_off_work(genpd);
1263
1264 mutex_unlock(&gpd_list_lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001265}