blob: b6ff6ecf519d95fd1f18c8e446b1c5f9af1d3991 [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. Wysocki0140d8b2011-12-01 00:02:17 +010036#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
37({ \
38 ktime_t __start = ktime_get(); \
39 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
40 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
41 struct generic_pm_domain_data *__gpd_data = dev_gpd_data(dev); \
42 if (__elapsed > __gpd_data->td.field) { \
43 __gpd_data->td.field = __elapsed; \
44 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
45 __elapsed); \
46 } \
47 __retval; \
48})
49
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +020050static LIST_HEAD(gpd_list);
51static DEFINE_MUTEX(gpd_list_lock);
52
Rafael J. Wysocki52480512011-07-01 22:13:10 +020053#ifdef CONFIG_PM
54
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010055struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020056{
57 if (IS_ERR_OR_NULL(dev->pm_domain))
58 return ERR_PTR(-EINVAL);
59
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020060 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020061}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020062
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010063static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
64{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010065 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
66 stop_latency_ns, "stop");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010067}
68
69static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
70{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010071 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
72 start_latency_ns, "start");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010073}
74
Rafael J. Wysockiecf00472011-11-27 13:11:44 +010075static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
76{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010077 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
78 save_state_latency_ns, "state save");
Rafael J. Wysockiecf00472011-11-27 13:11:44 +010079}
80
81static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
82{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010083 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
84 restore_state_latency_ns,
85 "state restore");
Rafael J. Wysockiecf00472011-11-27 13:11:44 +010086}
87
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020088static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020089{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020090 bool ret = false;
91
92 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
93 ret = !!atomic_dec_and_test(&genpd->sd_count);
94
95 return ret;
96}
97
98static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
99{
100 atomic_inc(&genpd->sd_count);
101 smp_mb__after_atomic_inc();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200102}
103
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200104static void genpd_acquire_lock(struct generic_pm_domain *genpd)
105{
106 DEFINE_WAIT(wait);
107
108 mutex_lock(&genpd->lock);
109 /*
110 * Wait for the domain to transition into either the active,
111 * or the power off state.
112 */
113 for (;;) {
114 prepare_to_wait(&genpd->status_wait_queue, &wait,
115 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200116 if (genpd->status == GPD_STATE_ACTIVE
117 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200118 break;
119 mutex_unlock(&genpd->lock);
120
121 schedule();
122
123 mutex_lock(&genpd->lock);
124 }
125 finish_wait(&genpd->status_wait_queue, &wait);
126}
127
128static void genpd_release_lock(struct generic_pm_domain *genpd)
129{
130 mutex_unlock(&genpd->lock);
131}
132
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200133static void genpd_set_active(struct generic_pm_domain *genpd)
134{
135 if (genpd->resume_count == 0)
136 genpd->status = GPD_STATE_ACTIVE;
137}
138
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200139/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200140 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200141 * @genpd: PM domain to power up.
142 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200143 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200144 * resume a device belonging to it.
145 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200146int __pm_genpd_poweron(struct generic_pm_domain *genpd)
147 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200148{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200149 struct gpd_link *link;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200150 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200151 int ret = 0;
152
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200153 /* If the domain's master is being waited for, we have to wait too. */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200154 for (;;) {
155 prepare_to_wait(&genpd->status_wait_queue, &wait,
156 TASK_UNINTERRUPTIBLE);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200157 if (genpd->status != GPD_STATE_WAIT_MASTER)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200158 break;
159 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200160
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200161 schedule();
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200162
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200163 mutex_lock(&genpd->lock);
164 }
165 finish_wait(&genpd->status_wait_queue, &wait);
166
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200167 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200168 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200169 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200170
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200171 if (genpd->status != GPD_STATE_POWER_OFF) {
172 genpd_set_active(genpd);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200173 return 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200174 }
175
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200176 /*
177 * The list is guaranteed not to change while the loop below is being
178 * executed, unless one of the masters' .power_on() callbacks fiddles
179 * with it.
180 */
181 list_for_each_entry(link, &genpd->slave_links, slave_node) {
182 genpd_sd_counter_inc(link->master);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200183 genpd->status = GPD_STATE_WAIT_MASTER;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200184
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200185 mutex_unlock(&genpd->lock);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200186
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200187 ret = pm_genpd_poweron(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200188
189 mutex_lock(&genpd->lock);
190
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200191 /*
192 * The "wait for parent" status is guaranteed not to change
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200193 * while the master is powering on.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200194 */
195 genpd->status = GPD_STATE_POWER_OFF;
196 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200197 if (ret) {
198 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200199 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200200 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200201 }
202
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200203 if (genpd->power_on) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100204 ktime_t time_start = ktime_get();
205 s64 elapsed_ns;
206
Rafael J. Wysockife202fd2011-08-05 21:45:11 +0200207 ret = genpd->power_on(genpd);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200208 if (ret)
209 goto err;
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100210
211 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100212 if (elapsed_ns > genpd->power_on_latency_ns) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100213 genpd->power_on_latency_ns = elapsed_ns;
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100214 if (genpd->name)
215 pr_warning("%s: Power-on latency exceeded, "
216 "new value %lld ns\n", genpd->name,
217 elapsed_ns);
218 }
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200219 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200220
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200221 genpd_set_active(genpd);
222
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200223 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200224
225 err:
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200226 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
227 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200228
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200229 return ret;
230}
231
232/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200233 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200234 * @genpd: PM domain to power up.
235 */
236int pm_genpd_poweron(struct generic_pm_domain *genpd)
237{
238 int ret;
239
240 mutex_lock(&genpd->lock);
241 ret = __pm_genpd_poweron(genpd);
242 mutex_unlock(&genpd->lock);
243 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200244}
245
246#endif /* CONFIG_PM */
247
248#ifdef CONFIG_PM_RUNTIME
249
250/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200251 * __pm_genpd_save_device - Save the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200252 * @pdd: Domain data of the device to save the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200253 * @genpd: PM domain the device belongs to.
254 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200255static int __pm_genpd_save_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200256 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200257 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200258{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200259 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200260 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200261 int ret = 0;
262
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200263 if (gpd_data->need_restore)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200264 return 0;
265
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200266 mutex_unlock(&genpd->lock);
267
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100268 genpd_start_dev(genpd, dev);
269 ret = genpd_save_dev(genpd, dev);
270 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200271
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200272 mutex_lock(&genpd->lock);
273
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200274 if (!ret)
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200275 gpd_data->need_restore = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200276
277 return ret;
278}
279
280/**
281 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200282 * @pdd: Domain data of the device to restore the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200283 * @genpd: PM domain the device belongs to.
284 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200285static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200286 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200287 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200288{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200289 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200290 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200291
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200292 if (!gpd_data->need_restore)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200293 return;
294
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200295 mutex_unlock(&genpd->lock);
296
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100297 genpd_start_dev(genpd, dev);
298 genpd_restore_dev(genpd, dev);
299 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200300
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200301 mutex_lock(&genpd->lock);
302
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200303 gpd_data->need_restore = false;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200304}
305
306/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200307 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
308 * @genpd: PM domain to check.
309 *
310 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
311 * a "power off" operation, which means that a "power on" has occured in the
312 * meantime, or if its resume_count field is different from zero, which means
313 * that one of its devices has been resumed in the meantime.
314 */
315static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
316{
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200317 return genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200318 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200319}
320
321/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200322 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
323 * @genpd: PM domait to power off.
324 *
325 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
326 * before.
327 */
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200328void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200329{
330 if (!work_pending(&genpd->power_off_work))
331 queue_work(pm_wq, &genpd->power_off_work);
332}
333
334/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200335 * pm_genpd_poweroff - Remove power from a given PM domain.
336 * @genpd: PM domain to power down.
337 *
338 * If all of the @genpd's devices have been suspended and all of its subdomains
339 * have been powered down, run the runtime suspend callbacks provided by all of
340 * the @genpd's devices' drivers and remove power from @genpd.
341 */
342static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200343 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200344{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200345 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200346 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200347 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200348 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200349
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200350 start:
351 /*
352 * Do not try to power off the domain in the following situations:
353 * (1) The domain is already in the "power off" state.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200354 * (2) The domain is waiting for its master to power up.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200355 * (3) One of the domain's devices is being resumed right now.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200356 * (4) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200357 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200358 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200359 || genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200360 || genpd->resume_count > 0 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200361 return 0;
362
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200363 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200364 return -EBUSY;
365
366 not_suspended = 0;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200367 list_for_each_entry(pdd, &genpd->dev_list, list_node)
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200368 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100369 || pdd->dev->power.irq_safe || to_gpd_data(pdd)->always_on))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200370 not_suspended++;
371
372 if (not_suspended > genpd->in_progress)
373 return -EBUSY;
374
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200375 if (genpd->poweroff_task) {
376 /*
377 * Another instance of pm_genpd_poweroff() is executing
378 * callbacks, so tell it to start over and return.
379 */
380 genpd->status = GPD_STATE_REPEAT;
381 return 0;
382 }
383
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200384 if (genpd->gov && genpd->gov->power_down_ok) {
385 if (!genpd->gov->power_down_ok(&genpd->domain))
386 return -EAGAIN;
387 }
388
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200389 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200390 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200391
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200392 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200393 ret = atomic_read(&genpd->sd_count) == 0 ?
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200394 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200395
396 if (genpd_abort_poweroff(genpd))
397 goto out;
398
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200399 if (ret) {
400 genpd_set_active(genpd);
401 goto out;
402 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200403
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200404 if (genpd->status == GPD_STATE_REPEAT) {
405 genpd->poweroff_task = NULL;
406 goto start;
407 }
408 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200409
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200410 if (genpd->power_off) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100411 ktime_t time_start;
412 s64 elapsed_ns;
413
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200414 if (atomic_read(&genpd->sd_count) > 0) {
415 ret = -EBUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200416 goto out;
417 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200418
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100419 time_start = ktime_get();
420
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200421 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200422 * If sd_count > 0 at this point, one of the subdomains hasn't
423 * managed to call pm_genpd_poweron() for the master yet after
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200424 * incrementing it. In that case pm_genpd_poweron() will wait
425 * for us to drop the lock, so we can call .power_off() and let
426 * the pm_genpd_poweron() restore power for us (this shouldn't
427 * happen very often).
428 */
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200429 ret = genpd->power_off(genpd);
430 if (ret == -EBUSY) {
431 genpd_set_active(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200432 goto out;
433 }
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100434
435 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100436 if (elapsed_ns > genpd->power_off_latency_ns) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100437 genpd->power_off_latency_ns = elapsed_ns;
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100438 if (genpd->name)
439 pr_warning("%s: Power-off latency exceeded, "
440 "new value %lld ns\n", genpd->name,
441 elapsed_ns);
442 }
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200443 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200444
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200445 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100446 genpd->power_off_time = ktime_get();
447
448 /* Update PM QoS information for devices in the domain. */
449 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
450 struct gpd_timing_data *td = &to_gpd_data(pdd)->td;
451
452 pm_runtime_update_max_time_suspended(pdd->dev,
453 td->start_latency_ns +
454 td->restore_state_latency_ns +
455 genpd->power_on_latency_ns);
456 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200457
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200458 list_for_each_entry(link, &genpd->slave_links, slave_node) {
459 genpd_sd_counter_dec(link->master);
460 genpd_queue_power_off_work(link->master);
461 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200462
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200463 out:
464 genpd->poweroff_task = NULL;
465 wake_up_all(&genpd->status_wait_queue);
466 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200467}
468
469/**
470 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
471 * @work: Work structure used for scheduling the execution of this function.
472 */
473static void genpd_power_off_work_fn(struct work_struct *work)
474{
475 struct generic_pm_domain *genpd;
476
477 genpd = container_of(work, struct generic_pm_domain, power_off_work);
478
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200479 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200480 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200481 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200482}
483
484/**
485 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
486 * @dev: Device to suspend.
487 *
488 * Carry out a runtime suspend of a device under the assumption that its
489 * pm_domain field points to the domain member of an object of type
490 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
491 */
492static int pm_genpd_runtime_suspend(struct device *dev)
493{
494 struct generic_pm_domain *genpd;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100495 bool (*stop_ok)(struct device *__dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100496 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200497
498 dev_dbg(dev, "%s()\n", __func__);
499
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200500 genpd = dev_to_genpd(dev);
501 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200502 return -EINVAL;
503
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200504 might_sleep_if(!genpd->dev_irq_safe);
505
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100506 if (dev_gpd_data(dev)->always_on)
507 return -EBUSY;
508
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100509 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
510 if (stop_ok && !stop_ok(dev))
511 return -EBUSY;
512
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100513 ret = genpd_stop_dev(genpd, dev);
514 if (ret)
515 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200516
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100517 pm_runtime_update_max_time_suspended(dev,
518 dev_gpd_data(dev)->td.start_latency_ns);
519
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200520 /*
521 * If power.irq_safe is set, this routine will be run with interrupts
522 * off, so it can't use mutexes.
523 */
524 if (dev->power.irq_safe)
525 return 0;
526
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200527 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200528 genpd->in_progress++;
529 pm_genpd_poweroff(genpd);
530 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200531 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200532
533 return 0;
534}
535
536/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200537 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
538 * @dev: Device to resume.
539 *
540 * Carry out a runtime resume of a device under the assumption that its
541 * pm_domain field points to the domain member of an object of type
542 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
543 */
544static int pm_genpd_runtime_resume(struct device *dev)
545{
546 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200547 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200548 int ret;
549
550 dev_dbg(dev, "%s()\n", __func__);
551
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200552 genpd = dev_to_genpd(dev);
553 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200554 return -EINVAL;
555
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200556 might_sleep_if(!genpd->dev_irq_safe);
557
558 /* If power.irq_safe, the PM domain is never powered off. */
559 if (dev->power.irq_safe)
560 goto out;
561
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200562 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200563 ret = __pm_genpd_poweron(genpd);
564 if (ret) {
565 mutex_unlock(&genpd->lock);
566 return ret;
567 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200568 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200569 genpd->resume_count++;
570 for (;;) {
571 prepare_to_wait(&genpd->status_wait_queue, &wait,
572 TASK_UNINTERRUPTIBLE);
573 /*
574 * If current is the powering off task, we have been called
575 * reentrantly from one of the device callbacks, so we should
576 * not wait.
577 */
578 if (!genpd->poweroff_task || genpd->poweroff_task == current)
579 break;
580 mutex_unlock(&genpd->lock);
581
582 schedule();
583
584 mutex_lock(&genpd->lock);
585 }
586 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200587 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200588 genpd->resume_count--;
589 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200590 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200591 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200592
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200593 out:
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100594 genpd_start_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200595
596 return 0;
597}
598
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200599/**
600 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
601 */
602void pm_genpd_poweroff_unused(void)
603{
604 struct generic_pm_domain *genpd;
605
606 mutex_lock(&gpd_list_lock);
607
608 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
609 genpd_queue_power_off_work(genpd);
610
611 mutex_unlock(&gpd_list_lock);
612}
613
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200614#else
615
616static inline void genpd_power_off_work_fn(struct work_struct *work) {}
617
618#define pm_genpd_runtime_suspend NULL
619#define pm_genpd_runtime_resume NULL
620
621#endif /* CONFIG_PM_RUNTIME */
622
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200623#ifdef CONFIG_PM_SLEEP
624
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100625static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
626 struct device *dev)
627{
628 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
629}
630
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100631static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
632{
633 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
634}
635
636static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
637{
638 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
639}
640
641static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
642{
643 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
644}
645
646static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
647{
648 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
649}
650
651static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
652{
653 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
654}
655
656static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
657{
658 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
659}
660
661static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
662{
663 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
664}
665
666static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
667{
668 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
669}
670
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200671/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200672 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200673 * @genpd: PM domain to power off, if possible.
674 *
675 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200676 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200677 *
678 * This function is only called in "noirq" stages of system power transitions,
679 * so it need not acquire locks (all of the "noirq" callbacks are executed
680 * sequentially, so it is guaranteed that it will never run twice in parallel).
681 */
682static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
683{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200684 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200685
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200686 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200687 return;
688
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200689 if (genpd->suspended_count != genpd->device_count
690 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200691 return;
692
693 if (genpd->power_off)
694 genpd->power_off(genpd);
695
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200696 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200697
698 list_for_each_entry(link, &genpd->slave_links, slave_node) {
699 genpd_sd_counter_dec(link->master);
700 pm_genpd_sync_poweroff(link->master);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200701 }
702}
703
704/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200705 * resume_needed - Check whether to resume a device before system suspend.
706 * @dev: Device to check.
707 * @genpd: PM domain the device belongs to.
708 *
709 * There are two cases in which a device that can wake up the system from sleep
710 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
711 * to wake up the system and it has to remain active for this purpose while the
712 * system is in the sleep state and (2) if the device is not enabled to wake up
713 * the system from sleep states and it generally doesn't generate wakeup signals
714 * by itself (those signals are generated on its behalf by other parts of the
715 * system). In the latter case it may be necessary to reconfigure the device's
716 * wakeup settings during system suspend, because it may have been set up to
717 * signal remote wakeup from the system's working state as needed by runtime PM.
718 * Return 'true' in either of the above cases.
719 */
720static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
721{
722 bool active_wakeup;
723
724 if (!device_can_wakeup(dev))
725 return false;
726
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100727 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200728 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
729}
730
731/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200732 * pm_genpd_prepare - Start power transition of a device in a PM domain.
733 * @dev: Device to start the transition of.
734 *
735 * Start a power transition of a device (during a system-wide power transition)
736 * under the assumption that its pm_domain field points to the domain member of
737 * an object of type struct generic_pm_domain representing a PM domain
738 * consisting of I/O devices.
739 */
740static int pm_genpd_prepare(struct device *dev)
741{
742 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200743 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200744
745 dev_dbg(dev, "%s()\n", __func__);
746
747 genpd = dev_to_genpd(dev);
748 if (IS_ERR(genpd))
749 return -EINVAL;
750
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200751 /*
752 * If a wakeup request is pending for the device, it should be woken up
753 * at this point and a system wakeup event should be reported if it's
754 * set up to wake up the system from sleep states.
755 */
756 pm_runtime_get_noresume(dev);
757 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
758 pm_wakeup_event(dev, 0);
759
760 if (pm_wakeup_pending()) {
761 pm_runtime_put_sync(dev);
762 return -EBUSY;
763 }
764
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200765 if (resume_needed(dev, genpd))
766 pm_runtime_resume(dev);
767
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200768 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200769
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100770 if (genpd->prepared_count++ == 0) {
771 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200772 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100773 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200774
775 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200776
777 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200778 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200779 return 0;
780 }
781
782 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200783 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
784 * so pm_genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100785 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200786 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200787 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200788 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200789 __pm_runtime_disable(dev, false);
790
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200791 ret = pm_generic_prepare(dev);
792 if (ret) {
793 mutex_lock(&genpd->lock);
794
795 if (--genpd->prepared_count == 0)
796 genpd->suspend_power_off = false;
797
798 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200799 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200800 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200801
802 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200803 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200804}
805
806/**
807 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
808 * @dev: Device to suspend.
809 *
810 * Suspend a device under the assumption that its pm_domain field points to the
811 * domain member of an object of type struct generic_pm_domain representing
812 * a PM domain consisting of I/O devices.
813 */
814static int pm_genpd_suspend(struct device *dev)
815{
816 struct generic_pm_domain *genpd;
817
818 dev_dbg(dev, "%s()\n", __func__);
819
820 genpd = dev_to_genpd(dev);
821 if (IS_ERR(genpd))
822 return -EINVAL;
823
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100824 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200825}
826
827/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100828 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200829 * @dev: Device to suspend.
830 *
831 * Carry out a late suspend of a device under the assumption that its
832 * pm_domain field points to the domain member of an object of type
833 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
834 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100835static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200836{
837 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200838
839 dev_dbg(dev, "%s()\n", __func__);
840
841 genpd = dev_to_genpd(dev);
842 if (IS_ERR(genpd))
843 return -EINVAL;
844
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100845 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
846}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200847
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100848/**
849 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
850 * @dev: Device to suspend.
851 *
852 * Stop the device and remove power from the domain if all devices in it have
853 * been stopped.
854 */
855static int pm_genpd_suspend_noirq(struct device *dev)
856{
857 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200858
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100859 dev_dbg(dev, "%s()\n", __func__);
860
861 genpd = dev_to_genpd(dev);
862 if (IS_ERR(genpd))
863 return -EINVAL;
864
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100865 if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100866 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200867 return 0;
868
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100869 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200870
871 /*
872 * Since all of the "noirq" callbacks are executed sequentially, it is
873 * guaranteed that this function will never run twice in parallel for
874 * the same PM domain, so it is not necessary to use locking here.
875 */
876 genpd->suspended_count++;
877 pm_genpd_sync_poweroff(genpd);
878
879 return 0;
880}
881
882/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100883 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200884 * @dev: Device to resume.
885 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100886 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200887 */
888static int pm_genpd_resume_noirq(struct device *dev)
889{
890 struct generic_pm_domain *genpd;
891
892 dev_dbg(dev, "%s()\n", __func__);
893
894 genpd = dev_to_genpd(dev);
895 if (IS_ERR(genpd))
896 return -EINVAL;
897
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100898 if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
Rafael J. Wysockicc85b202012-03-13 22:39:31 +0100899 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200900 return 0;
901
902 /*
903 * Since all of the "noirq" callbacks are executed sequentially, it is
904 * guaranteed that this function will never run twice in parallel for
905 * the same PM domain, so it is not necessary to use locking here.
906 */
907 pm_genpd_poweron(genpd);
908 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200909
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100910 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200911}
912
913/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100914 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
915 * @dev: Device to resume.
916 *
917 * Carry out an early resume of a device under the assumption that its
918 * pm_domain field points to the domain member of an object of type
919 * struct generic_pm_domain representing a power domain consisting of I/O
920 * devices.
921 */
922static int pm_genpd_resume_early(struct device *dev)
923{
924 struct generic_pm_domain *genpd;
925
926 dev_dbg(dev, "%s()\n", __func__);
927
928 genpd = dev_to_genpd(dev);
929 if (IS_ERR(genpd))
930 return -EINVAL;
931
932 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
933}
934
935/**
936 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200937 * @dev: Device to resume.
938 *
939 * Resume a device under the assumption that its pm_domain field points to the
940 * domain member of an object of type struct generic_pm_domain representing
941 * a power domain consisting of I/O devices.
942 */
943static int pm_genpd_resume(struct device *dev)
944{
945 struct generic_pm_domain *genpd;
946
947 dev_dbg(dev, "%s()\n", __func__);
948
949 genpd = dev_to_genpd(dev);
950 if (IS_ERR(genpd))
951 return -EINVAL;
952
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100953 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200954}
955
956/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100957 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200958 * @dev: Device to freeze.
959 *
960 * Freeze a device under the assumption that its pm_domain field points to the
961 * domain member of an object of type struct generic_pm_domain representing
962 * a power domain consisting of I/O devices.
963 */
964static int pm_genpd_freeze(struct device *dev)
965{
966 struct generic_pm_domain *genpd;
967
968 dev_dbg(dev, "%s()\n", __func__);
969
970 genpd = dev_to_genpd(dev);
971 if (IS_ERR(genpd))
972 return -EINVAL;
973
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100974 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200975}
976
977/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100978 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
979 * @dev: Device to freeze.
980 *
981 * Carry out a late freeze of a device under the assumption that its
982 * pm_domain field points to the domain member of an object of type
983 * struct generic_pm_domain representing a power domain consisting of I/O
984 * devices.
985 */
986static int pm_genpd_freeze_late(struct device *dev)
987{
988 struct generic_pm_domain *genpd;
989
990 dev_dbg(dev, "%s()\n", __func__);
991
992 genpd = dev_to_genpd(dev);
993 if (IS_ERR(genpd))
994 return -EINVAL;
995
996 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
997}
998
999/**
1000 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001001 * @dev: Device to freeze.
1002 *
1003 * Carry out a late freeze of a device under the assumption that its
1004 * pm_domain field points to the domain member of an object of type
1005 * struct generic_pm_domain representing a power domain consisting of I/O
1006 * devices.
1007 */
1008static int pm_genpd_freeze_noirq(struct device *dev)
1009{
1010 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001011
1012 dev_dbg(dev, "%s()\n", __func__);
1013
1014 genpd = dev_to_genpd(dev);
1015 if (IS_ERR(genpd))
1016 return -EINVAL;
1017
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001018 return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
1019 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001020}
1021
1022/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001023 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001024 * @dev: Device to thaw.
1025 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001026 * Start the device, unless power has been removed from the domain already
1027 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001028 */
1029static int pm_genpd_thaw_noirq(struct device *dev)
1030{
1031 struct generic_pm_domain *genpd;
1032
1033 dev_dbg(dev, "%s()\n", __func__);
1034
1035 genpd = dev_to_genpd(dev);
1036 if (IS_ERR(genpd))
1037 return -EINVAL;
1038
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001039 return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
1040 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001041}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001042
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001043/**
1044 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1045 * @dev: Device to thaw.
1046 *
1047 * Carry out an early thaw of a device under the assumption that its
1048 * pm_domain field points to the domain member of an object of type
1049 * struct generic_pm_domain representing a power domain consisting of I/O
1050 * devices.
1051 */
1052static int pm_genpd_thaw_early(struct device *dev)
1053{
1054 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001055
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001056 dev_dbg(dev, "%s()\n", __func__);
1057
1058 genpd = dev_to_genpd(dev);
1059 if (IS_ERR(genpd))
1060 return -EINVAL;
1061
1062 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001063}
1064
1065/**
1066 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1067 * @dev: Device to thaw.
1068 *
1069 * Thaw a device under the assumption that its pm_domain field points to the
1070 * domain member of an object of type struct generic_pm_domain representing
1071 * a power domain consisting of I/O devices.
1072 */
1073static int pm_genpd_thaw(struct device *dev)
1074{
1075 struct generic_pm_domain *genpd;
1076
1077 dev_dbg(dev, "%s()\n", __func__);
1078
1079 genpd = dev_to_genpd(dev);
1080 if (IS_ERR(genpd))
1081 return -EINVAL;
1082
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001083 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001084}
1085
1086/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001087 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001088 * @dev: Device to resume.
1089 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001090 * Make sure the domain will be in the same power state as before the
1091 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001092 */
1093static int pm_genpd_restore_noirq(struct device *dev)
1094{
1095 struct generic_pm_domain *genpd;
1096
1097 dev_dbg(dev, "%s()\n", __func__);
1098
1099 genpd = dev_to_genpd(dev);
1100 if (IS_ERR(genpd))
1101 return -EINVAL;
1102
1103 /*
1104 * Since all of the "noirq" callbacks are executed sequentially, it is
1105 * guaranteed that this function will never run twice in parallel for
1106 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001107 *
1108 * At this point suspended_count == 0 means we are being run for the
1109 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001110 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001111 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001112 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001113 * The boot kernel might put the domain into arbitrary state,
1114 * so make it appear as powered off to pm_genpd_poweron(), so
1115 * that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001116 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001117 genpd->status = GPD_STATE_POWER_OFF;
1118 if (genpd->suspend_power_off) {
1119 /*
1120 * If the domain was off before the hibernation, make
1121 * sure it will be off going forward.
1122 */
1123 if (genpd->power_off)
1124 genpd->power_off(genpd);
1125
1126 return 0;
1127 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001128 }
1129
1130 pm_genpd_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001131
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001132 return dev_gpd_data(dev)->always_on ? 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001133}
1134
1135/**
1136 * pm_genpd_complete - Complete power transition of a device in a power domain.
1137 * @dev: Device to complete the transition of.
1138 *
1139 * Complete a power transition of a device (during a system-wide power
1140 * transition) under the assumption that its pm_domain field points to the
1141 * domain member of an object of type struct generic_pm_domain representing
1142 * a power domain consisting of I/O devices.
1143 */
1144static void pm_genpd_complete(struct device *dev)
1145{
1146 struct generic_pm_domain *genpd;
1147 bool run_complete;
1148
1149 dev_dbg(dev, "%s()\n", __func__);
1150
1151 genpd = dev_to_genpd(dev);
1152 if (IS_ERR(genpd))
1153 return;
1154
1155 mutex_lock(&genpd->lock);
1156
1157 run_complete = !genpd->suspend_power_off;
1158 if (--genpd->prepared_count == 0)
1159 genpd->suspend_power_off = false;
1160
1161 mutex_unlock(&genpd->lock);
1162
1163 if (run_complete) {
1164 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001165 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001166 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001167 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001168 }
1169}
1170
1171#else
1172
1173#define pm_genpd_prepare NULL
1174#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001175#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001176#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001177#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001178#define pm_genpd_resume_noirq NULL
1179#define pm_genpd_resume NULL
1180#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001181#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001182#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001183#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001184#define pm_genpd_thaw_noirq NULL
1185#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001186#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001187#define pm_genpd_complete NULL
1188
1189#endif /* CONFIG_PM_SLEEP */
1190
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001191/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001192 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001193 * @genpd: PM domain to add the device to.
1194 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001195 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001196 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001197int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1198 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001199{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001200 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001201 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001202 int ret = 0;
1203
1204 dev_dbg(dev, "%s()\n", __func__);
1205
1206 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1207 return -EINVAL;
1208
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001209 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001210
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001211 if (genpd->status == GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001212 ret = -EINVAL;
1213 goto out;
1214 }
1215
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001216 if (genpd->prepared_count > 0) {
1217 ret = -EAGAIN;
1218 goto out;
1219 }
1220
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001221 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1222 if (pdd->dev == dev) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001223 ret = -EINVAL;
1224 goto out;
1225 }
1226
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001227 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1228 if (!gpd_data) {
1229 ret = -ENOMEM;
1230 goto out;
1231 }
1232
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001233 genpd->device_count++;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001234
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001235 dev->pm_domain = &genpd->domain;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001236 dev_pm_get_subsys_data(dev);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001237 dev->power.subsys_data->domain_data = &gpd_data->base;
1238 gpd_data->base.dev = dev;
1239 gpd_data->need_restore = false;
1240 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001241 if (td)
1242 gpd_data->td = *td;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001243
1244 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001245 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001246
1247 return ret;
1248}
1249
1250/**
Thomas Abrahamc8aa1302012-01-27 15:22:07 +09001251 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1252 * @genpd_node: Device tree node pointer representing a PM domain to which the
1253 * the device is added to.
1254 * @dev: Device to be added.
1255 * @td: Set of PM QoS timing parameters to attach to the device.
1256 */
1257int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1258 struct gpd_timing_data *td)
1259{
1260 struct generic_pm_domain *genpd = NULL, *gpd;
1261
1262 dev_dbg(dev, "%s()\n", __func__);
1263
1264 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1265 return -EINVAL;
1266
1267 mutex_lock(&gpd_list_lock);
1268 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1269 if (gpd->of_node == genpd_node) {
1270 genpd = gpd;
1271 break;
1272 }
1273 }
1274 mutex_unlock(&gpd_list_lock);
1275
1276 if (!genpd)
1277 return -EINVAL;
1278
1279 return __pm_genpd_add_device(genpd, dev, td);
1280}
1281
1282/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001283 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1284 * @genpd: PM domain to remove the device from.
1285 * @dev: Device to be removed.
1286 */
1287int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1288 struct device *dev)
1289{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001290 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001291 int ret = -EINVAL;
1292
1293 dev_dbg(dev, "%s()\n", __func__);
1294
1295 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1296 return -EINVAL;
1297
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001298 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001299
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001300 if (genpd->prepared_count > 0) {
1301 ret = -EAGAIN;
1302 goto out;
1303 }
1304
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001305 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
1306 if (pdd->dev != dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001307 continue;
1308
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001309 list_del_init(&pdd->list_node);
1310 pdd->dev = NULL;
1311 dev_pm_put_subsys_data(dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001312 dev->pm_domain = NULL;
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001313 kfree(to_gpd_data(pdd));
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001314
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001315 genpd->device_count--;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001316
1317 ret = 0;
1318 break;
1319 }
1320
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001321 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001322 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001323
1324 return ret;
1325}
1326
1327/**
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001328 * pm_genpd_dev_always_on - Set/unset the "always on" flag for a given device.
1329 * @dev: Device to set/unset the flag for.
1330 * @val: The new value of the device's "always on" flag.
1331 */
1332void pm_genpd_dev_always_on(struct device *dev, bool val)
1333{
1334 struct pm_subsys_data *psd;
1335 unsigned long flags;
1336
1337 spin_lock_irqsave(&dev->power.lock, flags);
1338
1339 psd = dev_to_psd(dev);
1340 if (psd && psd->domain_data)
1341 to_gpd_data(psd->domain_data)->always_on = val;
1342
1343 spin_unlock_irqrestore(&dev->power.lock, flags);
1344}
1345EXPORT_SYMBOL_GPL(pm_genpd_dev_always_on);
1346
1347/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001348 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1349 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001350 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001351 */
1352int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001353 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001354{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001355 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001356 int ret = 0;
1357
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001358 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001359 return -EINVAL;
1360
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001361 start:
1362 genpd_acquire_lock(genpd);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001363 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001364
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001365 if (subdomain->status != GPD_STATE_POWER_OFF
1366 && subdomain->status != GPD_STATE_ACTIVE) {
1367 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001368 genpd_release_lock(genpd);
1369 goto start;
1370 }
1371
1372 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001373 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001374 ret = -EINVAL;
1375 goto out;
1376 }
1377
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001378 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001379 if (link->slave == subdomain && link->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001380 ret = -EINVAL;
1381 goto out;
1382 }
1383 }
1384
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001385 link = kzalloc(sizeof(*link), GFP_KERNEL);
1386 if (!link) {
1387 ret = -ENOMEM;
1388 goto out;
1389 }
1390 link->master = genpd;
1391 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001392 link->slave = subdomain;
1393 list_add_tail(&link->slave_node, &subdomain->slave_links);
1394 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001395 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001396
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001397 out:
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001398 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001399 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001400
1401 return ret;
1402}
1403
1404/**
1405 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1406 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001407 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001408 */
1409int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001410 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001411{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001412 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001413 int ret = -EINVAL;
1414
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001415 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001416 return -EINVAL;
1417
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001418 start:
1419 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001420
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001421 list_for_each_entry(link, &genpd->master_links, master_node) {
1422 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001423 continue;
1424
1425 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1426
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001427 if (subdomain->status != GPD_STATE_POWER_OFF
1428 && subdomain->status != GPD_STATE_ACTIVE) {
1429 mutex_unlock(&subdomain->lock);
1430 genpd_release_lock(genpd);
1431 goto start;
1432 }
1433
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001434 list_del(&link->master_node);
1435 list_del(&link->slave_node);
1436 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001437 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001438 genpd_sd_counter_dec(genpd);
1439
1440 mutex_unlock(&subdomain->lock);
1441
1442 ret = 0;
1443 break;
1444 }
1445
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001446 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001447
1448 return ret;
1449}
1450
1451/**
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001452 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1453 * @dev: Device to add the callbacks to.
1454 * @ops: Set of callbacks to add.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001455 * @td: Timing data to add to the device along with the callbacks (optional).
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001456 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001457int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1458 struct gpd_timing_data *td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001459{
1460 struct pm_domain_data *pdd;
1461 int ret = 0;
1462
1463 if (!(dev && dev->power.subsys_data && ops))
1464 return -EINVAL;
1465
1466 pm_runtime_disable(dev);
1467 device_pm_lock();
1468
1469 pdd = dev->power.subsys_data->domain_data;
1470 if (pdd) {
1471 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1472
1473 gpd_data->ops = *ops;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001474 if (td)
1475 gpd_data->td = *td;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001476 } else {
1477 ret = -EINVAL;
1478 }
1479
1480 device_pm_unlock();
1481 pm_runtime_enable(dev);
1482
1483 return ret;
1484}
1485EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1486
1487/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001488 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001489 * @dev: Device to remove the callbacks from.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001490 * @clear_td: If set, clear the device's timing data too.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001491 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001492int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001493{
1494 struct pm_domain_data *pdd;
1495 int ret = 0;
1496
1497 if (!(dev && dev->power.subsys_data))
1498 return -EINVAL;
1499
1500 pm_runtime_disable(dev);
1501 device_pm_lock();
1502
1503 pdd = dev->power.subsys_data->domain_data;
1504 if (pdd) {
1505 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1506
1507 gpd_data->ops = (struct gpd_dev_ops){ 0 };
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001508 if (clear_td)
1509 gpd_data->td = (struct gpd_timing_data){ 0 };
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001510 } else {
1511 ret = -EINVAL;
1512 }
1513
1514 device_pm_unlock();
1515 pm_runtime_enable(dev);
1516
1517 return ret;
1518}
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001519EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001520
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001521/* Default device callbacks for generic PM domains. */
1522
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001523/**
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001524 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1525 * @dev: Device to handle.
1526 */
1527static int pm_genpd_default_save_state(struct device *dev)
1528{
1529 int (*cb)(struct device *__dev);
1530 struct device_driver *drv = dev->driver;
1531
1532 cb = dev_gpd_data(dev)->ops.save_state;
1533 if (cb)
1534 return cb(dev);
1535
1536 if (drv && drv->pm && drv->pm->runtime_suspend)
1537 return drv->pm->runtime_suspend(dev);
1538
1539 return 0;
1540}
1541
1542/**
1543 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1544 * @dev: Device to handle.
1545 */
1546static int pm_genpd_default_restore_state(struct device *dev)
1547{
1548 int (*cb)(struct device *__dev);
1549 struct device_driver *drv = dev->driver;
1550
1551 cb = dev_gpd_data(dev)->ops.restore_state;
1552 if (cb)
1553 return cb(dev);
1554
1555 if (drv && drv->pm && drv->pm->runtime_resume)
1556 return drv->pm->runtime_resume(dev);
1557
1558 return 0;
1559}
1560
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01001561#ifdef CONFIG_PM_SLEEP
1562
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001563/**
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001564 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
1565 * @dev: Device to handle.
1566 */
1567static int pm_genpd_default_suspend(struct device *dev)
1568{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001569 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001570
1571 return cb ? cb(dev) : pm_generic_suspend(dev);
1572}
1573
1574/**
1575 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
1576 * @dev: Device to handle.
1577 */
1578static int pm_genpd_default_suspend_late(struct device *dev)
1579{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001580 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001581
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001582 return cb ? cb(dev) : pm_generic_suspend_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001583}
1584
1585/**
1586 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
1587 * @dev: Device to handle.
1588 */
1589static int pm_genpd_default_resume_early(struct device *dev)
1590{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001591 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001592
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001593 return cb ? cb(dev) : pm_generic_resume_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001594}
1595
1596/**
1597 * pm_genpd_default_resume - Default "device resume" for PM domians.
1598 * @dev: Device to handle.
1599 */
1600static int pm_genpd_default_resume(struct device *dev)
1601{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001602 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001603
1604 return cb ? cb(dev) : pm_generic_resume(dev);
1605}
1606
1607/**
1608 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
1609 * @dev: Device to handle.
1610 */
1611static int pm_genpd_default_freeze(struct device *dev)
1612{
1613 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
1614
1615 return cb ? cb(dev) : pm_generic_freeze(dev);
1616}
1617
1618/**
1619 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
1620 * @dev: Device to handle.
1621 */
1622static int pm_genpd_default_freeze_late(struct device *dev)
1623{
1624 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
1625
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001626 return cb ? cb(dev) : pm_generic_freeze_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001627}
1628
1629/**
1630 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
1631 * @dev: Device to handle.
1632 */
1633static int pm_genpd_default_thaw_early(struct device *dev)
1634{
1635 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
1636
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001637 return cb ? cb(dev) : pm_generic_thaw_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001638}
1639
1640/**
1641 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
1642 * @dev: Device to handle.
1643 */
1644static int pm_genpd_default_thaw(struct device *dev)
1645{
1646 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
1647
1648 return cb ? cb(dev) : pm_generic_thaw(dev);
1649}
1650
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01001651#else /* !CONFIG_PM_SLEEP */
1652
1653#define pm_genpd_default_suspend NULL
1654#define pm_genpd_default_suspend_late NULL
1655#define pm_genpd_default_resume_early NULL
1656#define pm_genpd_default_resume NULL
1657#define pm_genpd_default_freeze NULL
1658#define pm_genpd_default_freeze_late NULL
1659#define pm_genpd_default_thaw_early NULL
1660#define pm_genpd_default_thaw NULL
1661
1662#endif /* !CONFIG_PM_SLEEP */
1663
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001664/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001665 * pm_genpd_init - Initialize a generic I/O PM domain object.
1666 * @genpd: PM domain object to initialize.
1667 * @gov: PM domain governor to associate with the domain (may be NULL).
1668 * @is_off: Initial value of the domain's power_is_off field.
1669 */
1670void pm_genpd_init(struct generic_pm_domain *genpd,
1671 struct dev_power_governor *gov, bool is_off)
1672{
1673 if (IS_ERR_OR_NULL(genpd))
1674 return;
1675
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001676 INIT_LIST_HEAD(&genpd->master_links);
1677 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001678 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001679 mutex_init(&genpd->lock);
1680 genpd->gov = gov;
1681 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1682 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001683 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001684 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1685 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001686 genpd->poweroff_task = NULL;
1687 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001688 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001689 genpd->max_off_time_ns = -1;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001690 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1691 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1692 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001693 genpd->domain.ops.prepare = pm_genpd_prepare;
1694 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001695 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001696 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1697 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001698 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001699 genpd->domain.ops.resume = pm_genpd_resume;
1700 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001701 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001702 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1703 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001704 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001705 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001706 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001707 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001708 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001709 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001710 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001711 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001712 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001713 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1714 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001715 genpd->dev_ops.suspend = pm_genpd_default_suspend;
1716 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
1717 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
1718 genpd->dev_ops.resume = pm_genpd_default_resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001719 genpd->dev_ops.freeze = pm_genpd_default_freeze;
1720 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
1721 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
1722 genpd->dev_ops.thaw = pm_genpd_default_thaw;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001723 mutex_lock(&gpd_list_lock);
1724 list_add(&genpd->gpd_list_node, &gpd_list);
1725 mutex_unlock(&gpd_list_lock);
1726}