ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / base / power / domain.c
1 /*
2  * drivers/base/power/domain.c - Common code related to device power domains.
3  *
4  * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5  *
6  * This file is released under the GPLv2.
7  */
8
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
11 #include <linux/io.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/pm_domain.h>
15 #include <linux/pm_qos.h>
16 #include <linux/pm_clock.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/sched.h>
20 #include <linux/suspend.h>
21 #include <linux/export.h>
22
23 #define GENPD_RETRY_MAX_MS      250             /* Approximate */
24
25 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev)          \
26 ({                                                              \
27         type (*__routine)(struct device *__d);                  \
28         type __ret = (type)0;                                   \
29                                                                 \
30         __routine = genpd->dev_ops.callback;                    \
31         if (__routine) {                                        \
32                 __ret = __routine(dev);                         \
33         }                                                       \
34         __ret;                                                  \
35 })
36
37 static LIST_HEAD(gpd_list);
38 static DEFINE_MUTEX(gpd_list_lock);
39
40 /*
41  * Get the generic PM domain for a particular struct device.
42  * This validates the struct device pointer, the PM domain pointer,
43  * and checks that the PM domain pointer is a real generic PM domain.
44  * Any failure results in NULL being returned.
45  */
46 struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
47 {
48         struct generic_pm_domain *genpd = NULL, *gpd;
49
50         if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
51                 return NULL;
52
53         mutex_lock(&gpd_list_lock);
54         list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
55                 if (&gpd->domain == dev->pm_domain) {
56                         genpd = gpd;
57                         break;
58                 }
59         }
60         mutex_unlock(&gpd_list_lock);
61
62         return genpd;
63 }
64
65 /*
66  * This should only be used where we are certain that the pm_domain
67  * attached to the device is a genpd domain.
68  */
69 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
70 {
71         if (IS_ERR_OR_NULL(dev->pm_domain))
72                 return ERR_PTR(-EINVAL);
73
74         return pd_to_genpd(dev->pm_domain);
75 }
76
77 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
78 {
79         return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
80 }
81
82 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
83 {
84         return GENPD_DEV_CALLBACK(genpd, int, start, dev);
85 }
86
87 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
88 {
89         bool ret = false;
90
91         if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
92                 ret = !!atomic_dec_and_test(&genpd->sd_count);
93
94         return ret;
95 }
96
97 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
98 {
99         atomic_inc(&genpd->sd_count);
100         smp_mb__after_atomic();
101 }
102
103 static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
104 {
105         ktime_t time_start;
106         s64 elapsed_ns;
107         int ret;
108
109         if (!genpd->power_on)
110                 return 0;
111
112         if (!timed)
113                 return genpd->power_on(genpd);
114
115         time_start = ktime_get();
116         ret = genpd->power_on(genpd);
117         if (ret)
118                 return ret;
119
120         elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
121         if (elapsed_ns <= genpd->power_on_latency_ns)
122                 return ret;
123
124         genpd->power_on_latency_ns = elapsed_ns;
125         genpd->max_off_time_changed = true;
126         pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
127                  genpd->name, "on", elapsed_ns);
128
129         return ret;
130 }
131
132 static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
133 {
134         ktime_t time_start;
135         s64 elapsed_ns;
136         int ret;
137
138         if (!genpd->power_off)
139                 return 0;
140
141         if (!timed)
142                 return genpd->power_off(genpd);
143
144         time_start = ktime_get();
145         ret = genpd->power_off(genpd);
146         if (ret == -EBUSY)
147                 return ret;
148
149         elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
150         if (elapsed_ns <= genpd->power_off_latency_ns)
151                 return ret;
152
153         genpd->power_off_latency_ns = elapsed_ns;
154         genpd->max_off_time_changed = true;
155         pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
156                  genpd->name, "off", elapsed_ns);
157
158         return ret;
159 }
160
161 /**
162  * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
163  * @genpd: PM domait to power off.
164  *
165  * Queue up the execution of genpd_poweroff() unless it's already been done
166  * before.
167  */
168 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
169 {
170         queue_work(pm_wq, &genpd->power_off_work);
171 }
172
173 /**
174  * __genpd_poweron - Restore power to a given PM domain and its masters.
175  * @genpd: PM domain to power up.
176  * @depth: nesting count for lockdep.
177  *
178  * Restore power to @genpd and all of its masters so that it is possible to
179  * resume a device belonging to it.
180  */
181 static int __genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
182 {
183         struct gpd_link *link;
184         int ret = 0;
185
186         if (genpd->status == GPD_STATE_ACTIVE)
187                 return 0;
188
189         /*
190          * The list is guaranteed not to change while the loop below is being
191          * executed, unless one of the masters' .power_on() callbacks fiddles
192          * with it.
193          */
194         list_for_each_entry(link, &genpd->slave_links, slave_node) {
195                 struct generic_pm_domain *master = link->master;
196
197                 genpd_sd_counter_inc(master);
198
199                 mutex_lock_nested(&master->lock, depth + 1);
200                 ret = __genpd_poweron(master, depth + 1);
201                 mutex_unlock(&master->lock);
202
203                 if (ret) {
204                         genpd_sd_counter_dec(master);
205                         goto err;
206                 }
207         }
208
209         ret = genpd_power_on(genpd, true);
210         if (ret)
211                 goto err;
212
213         genpd->status = GPD_STATE_ACTIVE;
214         return 0;
215
216  err:
217         list_for_each_entry_continue_reverse(link,
218                                         &genpd->slave_links,
219                                         slave_node) {
220                 genpd_sd_counter_dec(link->master);
221                 genpd_queue_power_off_work(link->master);
222         }
223
224         return ret;
225 }
226
227 /**
228  * genpd_poweron - Restore power to a given PM domain and its masters.
229  * @genpd: PM domain to power up.
230  */
231 static int genpd_poweron(struct generic_pm_domain *genpd)
232 {
233         int ret;
234
235         mutex_lock(&genpd->lock);
236         ret = __genpd_poweron(genpd, 0);
237         mutex_unlock(&genpd->lock);
238         return ret;
239 }
240
241
242 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
243 {
244         return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
245 }
246
247 static int genpd_restore_dev(struct generic_pm_domain *genpd,
248                         struct device *dev)
249 {
250         return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
251 }
252
253 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
254                                      unsigned long val, void *ptr)
255 {
256         struct generic_pm_domain_data *gpd_data;
257         struct device *dev;
258
259         gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
260         dev = gpd_data->base.dev;
261
262         for (;;) {
263                 struct generic_pm_domain *genpd;
264                 struct pm_domain_data *pdd;
265
266                 spin_lock_irq(&dev->power.lock);
267
268                 pdd = dev->power.subsys_data ?
269                                 dev->power.subsys_data->domain_data : NULL;
270                 if (pdd && pdd->dev) {
271                         to_gpd_data(pdd)->td.constraint_changed = true;
272                         genpd = dev_to_genpd(dev);
273                 } else {
274                         genpd = ERR_PTR(-ENODATA);
275                 }
276
277                 spin_unlock_irq(&dev->power.lock);
278
279                 if (!IS_ERR(genpd)) {
280                         mutex_lock(&genpd->lock);
281                         genpd->max_off_time_changed = true;
282                         mutex_unlock(&genpd->lock);
283                 }
284
285                 dev = dev->parent;
286                 if (!dev || dev->power.ignore_children)
287                         break;
288         }
289
290         return NOTIFY_DONE;
291 }
292
293 /**
294  * genpd_poweroff - Remove power from a given PM domain.
295  * @genpd: PM domain to power down.
296  * @is_async: PM domain is powered down from a scheduled work
297  *
298  * If all of the @genpd's devices have been suspended and all of its subdomains
299  * have been powered down, remove power from @genpd.
300  */
301 static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
302 {
303         struct pm_domain_data *pdd;
304         struct gpd_link *link;
305         unsigned int not_suspended = 0;
306
307         /*
308          * Do not try to power off the domain in the following situations:
309          * (1) The domain is already in the "power off" state.
310          * (2) System suspend is in progress.
311          */
312         if (genpd->status == GPD_STATE_POWER_OFF
313             || genpd->prepared_count > 0)
314                 return 0;
315
316         if (atomic_read(&genpd->sd_count) > 0)
317                 return -EBUSY;
318
319         list_for_each_entry(pdd, &genpd->dev_list, list_node) {
320                 enum pm_qos_flags_status stat;
321
322                 stat = dev_pm_qos_flags(pdd->dev,
323                                         PM_QOS_FLAG_NO_POWER_OFF
324                                                 | PM_QOS_FLAG_REMOTE_WAKEUP);
325                 if (stat > PM_QOS_FLAGS_NONE)
326                         return -EBUSY;
327
328                 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
329                         not_suspended++;
330         }
331
332         if (not_suspended > 1 || (not_suspended == 1 && is_async))
333                 return -EBUSY;
334
335         if (genpd->gov && genpd->gov->power_down_ok) {
336                 if (!genpd->gov->power_down_ok(&genpd->domain))
337                         return -EAGAIN;
338         }
339
340         if (genpd->power_off) {
341                 int ret;
342
343                 if (atomic_read(&genpd->sd_count) > 0)
344                         return -EBUSY;
345
346                 /*
347                  * If sd_count > 0 at this point, one of the subdomains hasn't
348                  * managed to call genpd_poweron() for the master yet after
349                  * incrementing it.  In that case genpd_poweron() will wait
350                  * for us to drop the lock, so we can call .power_off() and let
351                  * the genpd_poweron() restore power for us (this shouldn't
352                  * happen very often).
353                  */
354                 ret = genpd_power_off(genpd, true);
355                 if (ret)
356                         return ret;
357         }
358
359         genpd->status = GPD_STATE_POWER_OFF;
360
361         list_for_each_entry(link, &genpd->slave_links, slave_node) {
362                 genpd_sd_counter_dec(link->master);
363                 genpd_queue_power_off_work(link->master);
364         }
365
366         return 0;
367 }
368
369 /**
370  * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
371  * @work: Work structure used for scheduling the execution of this function.
372  */
373 static void genpd_power_off_work_fn(struct work_struct *work)
374 {
375         struct generic_pm_domain *genpd;
376
377         genpd = container_of(work, struct generic_pm_domain, power_off_work);
378
379         mutex_lock(&genpd->lock);
380         genpd_poweroff(genpd, true);
381         mutex_unlock(&genpd->lock);
382 }
383
384 /**
385  * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
386  * @dev: Device to suspend.
387  *
388  * Carry out a runtime suspend of a device under the assumption that its
389  * pm_domain field points to the domain member of an object of type
390  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
391  */
392 static int pm_genpd_runtime_suspend(struct device *dev)
393 {
394         struct generic_pm_domain *genpd;
395         bool (*stop_ok)(struct device *__dev);
396         struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
397         bool runtime_pm = pm_runtime_enabled(dev);
398         ktime_t time_start;
399         s64 elapsed_ns;
400         int ret;
401
402         dev_dbg(dev, "%s()\n", __func__);
403
404         genpd = dev_to_genpd(dev);
405         if (IS_ERR(genpd))
406                 return -EINVAL;
407
408         /*
409          * A runtime PM centric subsystem/driver may re-use the runtime PM
410          * callbacks for other purposes than runtime PM. In those scenarios
411          * runtime PM is disabled. Under these circumstances, we shall skip
412          * validating/measuring the PM QoS latency.
413          */
414         stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
415         if (runtime_pm && stop_ok && !stop_ok(dev))
416                 return -EBUSY;
417
418         /* Measure suspend latency. */
419         if (runtime_pm)
420                 time_start = ktime_get();
421
422         ret = genpd_save_dev(genpd, dev);
423         if (ret)
424                 return ret;
425
426         ret = genpd_stop_dev(genpd, dev);
427         if (ret) {
428                 genpd_restore_dev(genpd, dev);
429                 return ret;
430         }
431
432         /* Update suspend latency value if the measured time exceeds it. */
433         if (runtime_pm) {
434                 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
435                 if (elapsed_ns > td->suspend_latency_ns) {
436                         td->suspend_latency_ns = elapsed_ns;
437                         dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
438                                 elapsed_ns);
439                         genpd->max_off_time_changed = true;
440                         td->constraint_changed = true;
441                 }
442         }
443
444         /*
445          * If power.irq_safe is set, this routine will be run with interrupts
446          * off, so it can't use mutexes.
447          */
448         if (dev->power.irq_safe)
449                 return 0;
450
451         mutex_lock(&genpd->lock);
452         genpd_poweroff(genpd, false);
453         mutex_unlock(&genpd->lock);
454
455         return 0;
456 }
457
458 /**
459  * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
460  * @dev: Device to resume.
461  *
462  * Carry out a runtime resume of a device under the assumption that its
463  * pm_domain field points to the domain member of an object of type
464  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
465  */
466 static int pm_genpd_runtime_resume(struct device *dev)
467 {
468         struct generic_pm_domain *genpd;
469         struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
470         bool runtime_pm = pm_runtime_enabled(dev);
471         ktime_t time_start;
472         s64 elapsed_ns;
473         int ret;
474         bool timed = true;
475
476         dev_dbg(dev, "%s()\n", __func__);
477
478         genpd = dev_to_genpd(dev);
479         if (IS_ERR(genpd))
480                 return -EINVAL;
481
482         /* If power.irq_safe, the PM domain is never powered off. */
483         if (dev->power.irq_safe) {
484                 timed = false;
485                 goto out;
486         }
487
488         mutex_lock(&genpd->lock);
489         ret = __genpd_poweron(genpd, 0);
490         mutex_unlock(&genpd->lock);
491
492         if (ret)
493                 return ret;
494
495  out:
496         /* Measure resume latency. */
497         if (timed && runtime_pm)
498                 time_start = ktime_get();
499
500         genpd_start_dev(genpd, dev);
501         genpd_restore_dev(genpd, dev);
502
503         /* Update resume latency value if the measured time exceeds it. */
504         if (timed && runtime_pm) {
505                 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
506                 if (elapsed_ns > td->resume_latency_ns) {
507                         td->resume_latency_ns = elapsed_ns;
508                         dev_dbg(dev, "resume latency exceeded, %lld ns\n",
509                                 elapsed_ns);
510                         genpd->max_off_time_changed = true;
511                         td->constraint_changed = true;
512                 }
513         }
514
515         return 0;
516 }
517
518 static bool pd_ignore_unused;
519 static int __init pd_ignore_unused_setup(char *__unused)
520 {
521         pd_ignore_unused = true;
522         return 1;
523 }
524 __setup("pd_ignore_unused", pd_ignore_unused_setup);
525
526 /**
527  * genpd_poweroff_unused - Power off all PM domains with no devices in use.
528  */
529 static int __init genpd_poweroff_unused(void)
530 {
531         struct generic_pm_domain *genpd;
532
533         if (pd_ignore_unused) {
534                 pr_warn("genpd: Not disabling unused power domains\n");
535                 return 0;
536         }
537
538         mutex_lock(&gpd_list_lock);
539
540         list_for_each_entry(genpd, &gpd_list, gpd_list_node)
541                 genpd_queue_power_off_work(genpd);
542
543         mutex_unlock(&gpd_list_lock);
544
545         return 0;
546 }
547 late_initcall(genpd_poweroff_unused);
548
549 #ifdef CONFIG_PM_SLEEP
550
551 /**
552  * pm_genpd_present - Check if the given PM domain has been initialized.
553  * @genpd: PM domain to check.
554  */
555 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
556 {
557         const struct generic_pm_domain *gpd;
558
559         if (IS_ERR_OR_NULL(genpd))
560                 return false;
561
562         list_for_each_entry(gpd, &gpd_list, gpd_list_node)
563                 if (gpd == genpd)
564                         return true;
565
566         return false;
567 }
568
569 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
570                                     struct device *dev)
571 {
572         return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
573 }
574
575 /**
576  * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
577  * @genpd: PM domain to power off, if possible.
578  * @timed: True if latency measurements are allowed.
579  *
580  * Check if the given PM domain can be powered off (during system suspend or
581  * hibernation) and do that if so.  Also, in that case propagate to its masters.
582  *
583  * This function is only called in "noirq" and "syscore" stages of system power
584  * transitions, so it need not acquire locks (all of the "noirq" callbacks are
585  * executed sequentially, so it is guaranteed that it will never run twice in
586  * parallel).
587  */
588 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
589                                    bool timed)
590 {
591         struct gpd_link *link;
592
593         if (genpd->status == GPD_STATE_POWER_OFF)
594                 return;
595
596         if (genpd->suspended_count != genpd->device_count
597             || atomic_read(&genpd->sd_count) > 0)
598                 return;
599
600         genpd_power_off(genpd, timed);
601
602         genpd->status = GPD_STATE_POWER_OFF;
603
604         list_for_each_entry(link, &genpd->slave_links, slave_node) {
605                 genpd_sd_counter_dec(link->master);
606                 pm_genpd_sync_poweroff(link->master, timed);
607         }
608 }
609
610 /**
611  * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
612  * @genpd: PM domain to power on.
613  * @timed: True if latency measurements are allowed.
614  *
615  * This function is only called in "noirq" and "syscore" stages of system power
616  * transitions, so it need not acquire locks (all of the "noirq" callbacks are
617  * executed sequentially, so it is guaranteed that it will never run twice in
618  * parallel).
619  */
620 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
621                                   bool timed)
622 {
623         struct gpd_link *link;
624
625         if (genpd->status == GPD_STATE_ACTIVE)
626                 return;
627
628         list_for_each_entry(link, &genpd->slave_links, slave_node) {
629                 pm_genpd_sync_poweron(link->master, timed);
630                 genpd_sd_counter_inc(link->master);
631         }
632
633         genpd_power_on(genpd, timed);
634
635         genpd->status = GPD_STATE_ACTIVE;
636 }
637
638 /**
639  * resume_needed - Check whether to resume a device before system suspend.
640  * @dev: Device to check.
641  * @genpd: PM domain the device belongs to.
642  *
643  * There are two cases in which a device that can wake up the system from sleep
644  * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
645  * to wake up the system and it has to remain active for this purpose while the
646  * system is in the sleep state and (2) if the device is not enabled to wake up
647  * the system from sleep states and it generally doesn't generate wakeup signals
648  * by itself (those signals are generated on its behalf by other parts of the
649  * system).  In the latter case it may be necessary to reconfigure the device's
650  * wakeup settings during system suspend, because it may have been set up to
651  * signal remote wakeup from the system's working state as needed by runtime PM.
652  * Return 'true' in either of the above cases.
653  */
654 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
655 {
656         bool active_wakeup;
657
658         if (!device_can_wakeup(dev))
659                 return false;
660
661         active_wakeup = genpd_dev_active_wakeup(genpd, dev);
662         return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
663 }
664
665 /**
666  * pm_genpd_prepare - Start power transition of a device in a PM domain.
667  * @dev: Device to start the transition of.
668  *
669  * Start a power transition of a device (during a system-wide power transition)
670  * under the assumption that its pm_domain field points to the domain member of
671  * an object of type struct generic_pm_domain representing a PM domain
672  * consisting of I/O devices.
673  */
674 static int pm_genpd_prepare(struct device *dev)
675 {
676         struct generic_pm_domain *genpd;
677         int ret;
678
679         dev_dbg(dev, "%s()\n", __func__);
680
681         genpd = dev_to_genpd(dev);
682         if (IS_ERR(genpd))
683                 return -EINVAL;
684
685         /*
686          * If a wakeup request is pending for the device, it should be woken up
687          * at this point and a system wakeup event should be reported if it's
688          * set up to wake up the system from sleep states.
689          */
690         if (resume_needed(dev, genpd))
691                 pm_runtime_resume(dev);
692
693         mutex_lock(&genpd->lock);
694
695         if (genpd->prepared_count++ == 0) {
696                 genpd->suspended_count = 0;
697                 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
698         }
699
700         mutex_unlock(&genpd->lock);
701
702         ret = pm_generic_prepare(dev);
703         if (ret) {
704                 mutex_lock(&genpd->lock);
705
706                 genpd->prepared_count--;
707
708                 mutex_unlock(&genpd->lock);
709         }
710
711         return ret;
712 }
713
714 /**
715  * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
716  * @dev: Device to suspend.
717  *
718  * Stop the device and remove power from the domain if all devices in it have
719  * been stopped.
720  */
721 static int pm_genpd_suspend_noirq(struct device *dev)
722 {
723         struct generic_pm_domain *genpd;
724
725         dev_dbg(dev, "%s()\n", __func__);
726
727         genpd = dev_to_genpd(dev);
728         if (IS_ERR(genpd))
729                 return -EINVAL;
730
731         if (genpd->suspend_power_off ||
732             (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
733                 return 0;
734
735         /*
736          * Since all of the "noirq" callbacks are executed sequentially, it is
737          * guaranteed that this function will never run twice in parallel for
738          * the same PM domain, so it is not necessary to use locking here.
739          */
740         genpd->suspended_count++;
741         pm_genpd_sync_poweroff(genpd, true);
742
743         return 0;
744 }
745
746 /**
747  * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
748  * @dev: Device to resume.
749  *
750  * Restore power to the device's PM domain, if necessary, and start the device.
751  */
752 static int pm_genpd_resume_noirq(struct device *dev)
753 {
754         struct generic_pm_domain *genpd;
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             (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
764                 return 0;
765
766         /*
767          * Since all of the "noirq" callbacks are executed sequentially, it is
768          * guaranteed that this function will never run twice in parallel for
769          * the same PM domain, so it is not necessary to use locking here.
770          */
771         pm_genpd_sync_poweron(genpd, true);
772         genpd->suspended_count--;
773
774         return 0;
775 }
776
777 /**
778  * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
779  * @dev: Device to freeze.
780  *
781  * Carry out a late freeze of a device under the assumption that its
782  * pm_domain field points to the domain member of an object of type
783  * struct generic_pm_domain representing a power domain consisting of I/O
784  * devices.
785  */
786 static int pm_genpd_freeze_noirq(struct device *dev)
787 {
788         struct generic_pm_domain *genpd;
789
790         dev_dbg(dev, "%s()\n", __func__);
791
792         genpd = dev_to_genpd(dev);
793         if (IS_ERR(genpd))
794                 return -EINVAL;
795
796         return 0;
797 }
798
799 /**
800  * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
801  * @dev: Device to thaw.
802  *
803  * Start the device, unless power has been removed from the domain already
804  * before the system transition.
805  */
806 static int pm_genpd_thaw_noirq(struct device *dev)
807 {
808         struct generic_pm_domain *genpd;
809
810         dev_dbg(dev, "%s()\n", __func__);
811
812         genpd = dev_to_genpd(dev);
813         if (IS_ERR(genpd))
814                 return -EINVAL;
815
816         return 0;
817 }
818
819 /**
820  * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
821  * @dev: Device to resume.
822  *
823  * Make sure the domain will be in the same power state as before the
824  * hibernation the system is resuming from and start the device if necessary.
825  */
826 static int pm_genpd_restore_noirq(struct device *dev)
827 {
828         struct generic_pm_domain *genpd;
829
830         dev_dbg(dev, "%s()\n", __func__);
831
832         genpd = dev_to_genpd(dev);
833         if (IS_ERR(genpd))
834                 return -EINVAL;
835
836         /*
837          * Since all of the "noirq" callbacks are executed sequentially, it is
838          * guaranteed that this function will never run twice in parallel for
839          * the same PM domain, so it is not necessary to use locking here.
840          *
841          * At this point suspended_count == 0 means we are being run for the
842          * first time for the given domain in the present cycle.
843          */
844         if (genpd->suspended_count++ == 0)
845                 /*
846                  * The boot kernel might put the domain into arbitrary state,
847                  * so make it appear as powered off to pm_genpd_sync_poweron(),
848                  * so that it tries to power it on in case it was really off.
849                  */
850                 genpd->status = GPD_STATE_POWER_OFF;
851
852         pm_genpd_sync_poweron(genpd, true);
853
854         return 0;
855 }
856
857 /**
858  * pm_genpd_complete - Complete power transition of a device in a power domain.
859  * @dev: Device to complete the transition of.
860  *
861  * Complete a power transition of a device (during a system-wide power
862  * transition) under the assumption that its pm_domain field points to the
863  * domain member of an object of type struct generic_pm_domain representing
864  * a power domain consisting of I/O devices.
865  */
866 static void pm_genpd_complete(struct device *dev)
867 {
868         struct generic_pm_domain *genpd;
869
870         dev_dbg(dev, "%s()\n", __func__);
871
872         genpd = dev_to_genpd(dev);
873         if (IS_ERR(genpd))
874                 return;
875
876         pm_generic_complete(dev);
877
878         mutex_lock(&genpd->lock);
879
880         genpd->prepared_count--;
881         if (!genpd->prepared_count)
882                 genpd_queue_power_off_work(genpd);
883
884         mutex_unlock(&genpd->lock);
885 }
886
887 /**
888  * genpd_syscore_switch - Switch power during system core suspend or resume.
889  * @dev: Device that normally is marked as "always on" to switch power for.
890  *
891  * This routine may only be called during the system core (syscore) suspend or
892  * resume phase for devices whose "always on" flags are set.
893  */
894 static void genpd_syscore_switch(struct device *dev, bool suspend)
895 {
896         struct generic_pm_domain *genpd;
897
898         genpd = dev_to_genpd(dev);
899         if (!pm_genpd_present(genpd))
900                 return;
901
902         if (suspend) {
903                 genpd->suspended_count++;
904                 pm_genpd_sync_poweroff(genpd, false);
905         } else {
906                 pm_genpd_sync_poweron(genpd, false);
907                 genpd->suspended_count--;
908         }
909 }
910
911 void pm_genpd_syscore_poweroff(struct device *dev)
912 {
913         genpd_syscore_switch(dev, true);
914 }
915 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
916
917 void pm_genpd_syscore_poweron(struct device *dev)
918 {
919         genpd_syscore_switch(dev, false);
920 }
921 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
922
923 #else /* !CONFIG_PM_SLEEP */
924
925 #define pm_genpd_prepare                NULL
926 #define pm_genpd_suspend_noirq          NULL
927 #define pm_genpd_resume_noirq           NULL
928 #define pm_genpd_freeze_noirq           NULL
929 #define pm_genpd_thaw_noirq             NULL
930 #define pm_genpd_restore_noirq          NULL
931 #define pm_genpd_complete               NULL
932
933 #endif /* CONFIG_PM_SLEEP */
934
935 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
936                                         struct generic_pm_domain *genpd,
937                                         struct gpd_timing_data *td)
938 {
939         struct generic_pm_domain_data *gpd_data;
940         int ret;
941
942         ret = dev_pm_get_subsys_data(dev);
943         if (ret)
944                 return ERR_PTR(ret);
945
946         gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
947         if (!gpd_data) {
948                 ret = -ENOMEM;
949                 goto err_put;
950         }
951
952         if (td)
953                 gpd_data->td = *td;
954
955         gpd_data->base.dev = dev;
956         gpd_data->td.constraint_changed = true;
957         gpd_data->td.effective_constraint_ns = -1;
958         gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
959
960         spin_lock_irq(&dev->power.lock);
961
962         if (dev->power.subsys_data->domain_data) {
963                 ret = -EINVAL;
964                 goto err_free;
965         }
966
967         dev->power.subsys_data->domain_data = &gpd_data->base;
968         dev->pm_domain = &genpd->domain;
969
970         spin_unlock_irq(&dev->power.lock);
971
972         return gpd_data;
973
974  err_free:
975         spin_unlock_irq(&dev->power.lock);
976         kfree(gpd_data);
977  err_put:
978         dev_pm_put_subsys_data(dev);
979         return ERR_PTR(ret);
980 }
981
982 static void genpd_free_dev_data(struct device *dev,
983                                 struct generic_pm_domain_data *gpd_data)
984 {
985         spin_lock_irq(&dev->power.lock);
986
987         dev->pm_domain = NULL;
988         dev->power.subsys_data->domain_data = NULL;
989
990         spin_unlock_irq(&dev->power.lock);
991
992         kfree(gpd_data);
993         dev_pm_put_subsys_data(dev);
994 }
995
996 /**
997  * __pm_genpd_add_device - Add a device to an I/O PM domain.
998  * @genpd: PM domain to add the device to.
999  * @dev: Device to be added.
1000  * @td: Set of PM QoS timing parameters to attach to the device.
1001  */
1002 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1003                           struct gpd_timing_data *td)
1004 {
1005         struct generic_pm_domain_data *gpd_data;
1006         int ret = 0;
1007
1008         dev_dbg(dev, "%s()\n", __func__);
1009
1010         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1011                 return -EINVAL;
1012
1013         gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1014         if (IS_ERR(gpd_data))
1015                 return PTR_ERR(gpd_data);
1016
1017         mutex_lock(&genpd->lock);
1018
1019         if (genpd->prepared_count > 0) {
1020                 ret = -EAGAIN;
1021                 goto out;
1022         }
1023
1024         ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1025         if (ret)
1026                 goto out;
1027
1028         genpd->device_count++;
1029         genpd->max_off_time_changed = true;
1030
1031         list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1032
1033  out:
1034         mutex_unlock(&genpd->lock);
1035
1036         if (ret)
1037                 genpd_free_dev_data(dev, gpd_data);
1038         else
1039                 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1040
1041         return ret;
1042 }
1043
1044 /**
1045  * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1046  * @genpd: PM domain to remove the device from.
1047  * @dev: Device to be removed.
1048  */
1049 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1050                            struct device *dev)
1051 {
1052         struct generic_pm_domain_data *gpd_data;
1053         struct pm_domain_data *pdd;
1054         int ret = 0;
1055
1056         dev_dbg(dev, "%s()\n", __func__);
1057
1058         if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1059                 return -EINVAL;
1060
1061         /* The above validation also means we have existing domain_data. */
1062         pdd = dev->power.subsys_data->domain_data;
1063         gpd_data = to_gpd_data(pdd);
1064         dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1065
1066         mutex_lock(&genpd->lock);
1067
1068         if (genpd->prepared_count > 0) {
1069                 ret = -EAGAIN;
1070                 goto out;
1071         }
1072
1073         genpd->device_count--;
1074         genpd->max_off_time_changed = true;
1075
1076         if (genpd->detach_dev)
1077                 genpd->detach_dev(genpd, dev);
1078
1079         list_del_init(&pdd->list_node);
1080
1081         mutex_unlock(&genpd->lock);
1082
1083         genpd_free_dev_data(dev, gpd_data);
1084
1085         return 0;
1086
1087  out:
1088         mutex_unlock(&genpd->lock);
1089         dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1090
1091         return ret;
1092 }
1093
1094 /**
1095  * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1096  * @genpd: Master PM domain to add the subdomain to.
1097  * @subdomain: Subdomain to be added.
1098  */
1099 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1100                            struct generic_pm_domain *subdomain)
1101 {
1102         struct gpd_link *link, *itr;
1103         int ret = 0;
1104
1105         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1106             || genpd == subdomain)
1107                 return -EINVAL;
1108
1109         link = kzalloc(sizeof(*link), GFP_KERNEL);
1110         if (!link)
1111                 return -ENOMEM;
1112
1113         mutex_lock(&subdomain->lock);
1114         mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1115
1116         if (genpd->status == GPD_STATE_POWER_OFF
1117             &&  subdomain->status != GPD_STATE_POWER_OFF) {
1118                 ret = -EINVAL;
1119                 goto out;
1120         }
1121
1122         list_for_each_entry(itr, &genpd->master_links, master_node) {
1123                 if (itr->slave == subdomain && itr->master == genpd) {
1124                         ret = -EINVAL;
1125                         goto out;
1126                 }
1127         }
1128
1129         link->master = genpd;
1130         list_add_tail(&link->master_node, &genpd->master_links);
1131         link->slave = subdomain;
1132         list_add_tail(&link->slave_node, &subdomain->slave_links);
1133         if (subdomain->status != GPD_STATE_POWER_OFF)
1134                 genpd_sd_counter_inc(genpd);
1135
1136  out:
1137         mutex_unlock(&genpd->lock);
1138         mutex_unlock(&subdomain->lock);
1139         if (ret)
1140                 kfree(link);
1141         return ret;
1142 }
1143 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1144
1145 /**
1146  * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1147  * @genpd: Master PM domain to remove the subdomain from.
1148  * @subdomain: Subdomain to be removed.
1149  */
1150 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1151                               struct generic_pm_domain *subdomain)
1152 {
1153         struct gpd_link *link;
1154         int ret = -EINVAL;
1155
1156         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1157                 return -EINVAL;
1158
1159         mutex_lock(&subdomain->lock);
1160         mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1161
1162         if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1163                 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1164                         subdomain->name);
1165                 ret = -EBUSY;
1166                 goto out;
1167         }
1168
1169         list_for_each_entry(link, &genpd->master_links, master_node) {
1170                 if (link->slave != subdomain)
1171                         continue;
1172
1173                 list_del(&link->master_node);
1174                 list_del(&link->slave_node);
1175                 kfree(link);
1176                 if (subdomain->status != GPD_STATE_POWER_OFF)
1177                         genpd_sd_counter_dec(genpd);
1178
1179                 ret = 0;
1180                 break;
1181         }
1182
1183 out:
1184         mutex_unlock(&genpd->lock);
1185         mutex_unlock(&subdomain->lock);
1186
1187         return ret;
1188 }
1189 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1190
1191 /* Default device callbacks for generic PM domains. */
1192
1193 /**
1194  * pm_genpd_default_save_state - Default "save device state" for PM domains.
1195  * @dev: Device to handle.
1196  */
1197 static int pm_genpd_default_save_state(struct device *dev)
1198 {
1199         int (*cb)(struct device *__dev);
1200
1201         if (dev->type && dev->type->pm)
1202                 cb = dev->type->pm->runtime_suspend;
1203         else if (dev->class && dev->class->pm)
1204                 cb = dev->class->pm->runtime_suspend;
1205         else if (dev->bus && dev->bus->pm)
1206                 cb = dev->bus->pm->runtime_suspend;
1207         else
1208                 cb = NULL;
1209
1210         if (!cb && dev->driver && dev->driver->pm)
1211                 cb = dev->driver->pm->runtime_suspend;
1212
1213         return cb ? cb(dev) : 0;
1214 }
1215
1216 /**
1217  * pm_genpd_default_restore_state - Default PM domains "restore device state".
1218  * @dev: Device to handle.
1219  */
1220 static int pm_genpd_default_restore_state(struct device *dev)
1221 {
1222         int (*cb)(struct device *__dev);
1223
1224         if (dev->type && dev->type->pm)
1225                 cb = dev->type->pm->runtime_resume;
1226         else if (dev->class && dev->class->pm)
1227                 cb = dev->class->pm->runtime_resume;
1228         else if (dev->bus && dev->bus->pm)
1229                 cb = dev->bus->pm->runtime_resume;
1230         else
1231                 cb = NULL;
1232
1233         if (!cb && dev->driver && dev->driver->pm)
1234                 cb = dev->driver->pm->runtime_resume;
1235
1236         return cb ? cb(dev) : 0;
1237 }
1238
1239 /**
1240  * pm_genpd_init - Initialize a generic I/O PM domain object.
1241  * @genpd: PM domain object to initialize.
1242  * @gov: PM domain governor to associate with the domain (may be NULL).
1243  * @is_off: Initial value of the domain's power_is_off field.
1244  */
1245 void pm_genpd_init(struct generic_pm_domain *genpd,
1246                    struct dev_power_governor *gov, bool is_off)
1247 {
1248         if (IS_ERR_OR_NULL(genpd))
1249                 return;
1250
1251         INIT_LIST_HEAD(&genpd->master_links);
1252         INIT_LIST_HEAD(&genpd->slave_links);
1253         INIT_LIST_HEAD(&genpd->dev_list);
1254         mutex_init(&genpd->lock);
1255         genpd->gov = gov;
1256         INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1257         atomic_set(&genpd->sd_count, 0);
1258         genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1259         genpd->device_count = 0;
1260         genpd->max_off_time_ns = -1;
1261         genpd->max_off_time_changed = true;
1262         genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1263         genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1264         genpd->domain.ops.prepare = pm_genpd_prepare;
1265         genpd->domain.ops.suspend = pm_generic_suspend;
1266         genpd->domain.ops.suspend_late = pm_generic_suspend_late;
1267         genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1268         genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1269         genpd->domain.ops.resume_early = pm_generic_resume_early;
1270         genpd->domain.ops.resume = pm_generic_resume;
1271         genpd->domain.ops.freeze = pm_generic_freeze;
1272         genpd->domain.ops.freeze_late = pm_generic_freeze_late;
1273         genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1274         genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1275         genpd->domain.ops.thaw_early = pm_generic_thaw_early;
1276         genpd->domain.ops.thaw = pm_generic_thaw;
1277         genpd->domain.ops.poweroff = pm_generic_poweroff;
1278         genpd->domain.ops.poweroff_late = pm_generic_poweroff_late;
1279         genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1280         genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1281         genpd->domain.ops.restore_early = pm_generic_restore_early;
1282         genpd->domain.ops.restore = pm_generic_restore;
1283         genpd->domain.ops.complete = pm_genpd_complete;
1284         genpd->dev_ops.save_state = pm_genpd_default_save_state;
1285         genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1286
1287         if (genpd->flags & GENPD_FLAG_PM_CLK) {
1288                 genpd->dev_ops.stop = pm_clk_suspend;
1289                 genpd->dev_ops.start = pm_clk_resume;
1290         }
1291
1292         mutex_lock(&gpd_list_lock);
1293         list_add(&genpd->gpd_list_node, &gpd_list);
1294         mutex_unlock(&gpd_list_lock);
1295 }
1296 EXPORT_SYMBOL_GPL(pm_genpd_init);
1297
1298 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1299 /*
1300  * Device Tree based PM domain providers.
1301  *
1302  * The code below implements generic device tree based PM domain providers that
1303  * bind device tree nodes with generic PM domains registered in the system.
1304  *
1305  * Any driver that registers generic PM domains and needs to support binding of
1306  * devices to these domains is supposed to register a PM domain provider, which
1307  * maps a PM domain specifier retrieved from the device tree to a PM domain.
1308  *
1309  * Two simple mapping functions have been provided for convenience:
1310  *  - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1311  *  - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1312  *    index.
1313  */
1314
1315 /**
1316  * struct of_genpd_provider - PM domain provider registration structure
1317  * @link: Entry in global list of PM domain providers
1318  * @node: Pointer to device tree node of PM domain provider
1319  * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1320  *         into a PM domain.
1321  * @data: context pointer to be passed into @xlate callback
1322  */
1323 struct of_genpd_provider {
1324         struct list_head link;
1325         struct device_node *node;
1326         genpd_xlate_t xlate;
1327         void *data;
1328 };
1329
1330 /* List of registered PM domain providers. */
1331 static LIST_HEAD(of_genpd_providers);
1332 /* Mutex to protect the list above. */
1333 static DEFINE_MUTEX(of_genpd_mutex);
1334
1335 /**
1336  * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1337  * @genpdspec: OF phandle args to map into a PM domain
1338  * @data: xlate function private data - pointer to struct generic_pm_domain
1339  *
1340  * This is a generic xlate function that can be used to model PM domains that
1341  * have their own device tree nodes. The private data of xlate function needs
1342  * to be a valid pointer to struct generic_pm_domain.
1343  */
1344 struct generic_pm_domain *__of_genpd_xlate_simple(
1345                                         struct of_phandle_args *genpdspec,
1346                                         void *data)
1347 {
1348         if (genpdspec->args_count != 0)
1349                 return ERR_PTR(-EINVAL);
1350         return data;
1351 }
1352 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1353
1354 /**
1355  * __of_genpd_xlate_onecell() - Xlate function using a single index.
1356  * @genpdspec: OF phandle args to map into a PM domain
1357  * @data: xlate function private data - pointer to struct genpd_onecell_data
1358  *
1359  * This is a generic xlate function that can be used to model simple PM domain
1360  * controllers that have one device tree node and provide multiple PM domains.
1361  * A single cell is used as an index into an array of PM domains specified in
1362  * the genpd_onecell_data struct when registering the provider.
1363  */
1364 struct generic_pm_domain *__of_genpd_xlate_onecell(
1365                                         struct of_phandle_args *genpdspec,
1366                                         void *data)
1367 {
1368         struct genpd_onecell_data *genpd_data = data;
1369         unsigned int idx = genpdspec->args[0];
1370
1371         if (genpdspec->args_count != 1)
1372                 return ERR_PTR(-EINVAL);
1373
1374         if (idx >= genpd_data->num_domains) {
1375                 pr_err("%s: invalid domain index %u\n", __func__, idx);
1376                 return ERR_PTR(-EINVAL);
1377         }
1378
1379         if (!genpd_data->domains[idx])
1380                 return ERR_PTR(-ENOENT);
1381
1382         return genpd_data->domains[idx];
1383 }
1384 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1385
1386 /**
1387  * __of_genpd_add_provider() - Register a PM domain provider for a node
1388  * @np: Device node pointer associated with the PM domain provider.
1389  * @xlate: Callback for decoding PM domain from phandle arguments.
1390  * @data: Context pointer for @xlate callback.
1391  */
1392 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1393                         void *data)
1394 {
1395         struct of_genpd_provider *cp;
1396
1397         cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1398         if (!cp)
1399                 return -ENOMEM;
1400
1401         cp->node = of_node_get(np);
1402         cp->data = data;
1403         cp->xlate = xlate;
1404
1405         mutex_lock(&of_genpd_mutex);
1406         list_add(&cp->link, &of_genpd_providers);
1407         mutex_unlock(&of_genpd_mutex);
1408         pr_debug("Added domain provider from %s\n", np->full_name);
1409
1410         return 0;
1411 }
1412 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1413
1414 /**
1415  * of_genpd_del_provider() - Remove a previously registered PM domain provider
1416  * @np: Device node pointer associated with the PM domain provider
1417  */
1418 void of_genpd_del_provider(struct device_node *np)
1419 {
1420         struct of_genpd_provider *cp;
1421
1422         mutex_lock(&of_genpd_mutex);
1423         list_for_each_entry(cp, &of_genpd_providers, link) {
1424                 if (cp->node == np) {
1425                         list_del(&cp->link);
1426                         of_node_put(cp->node);
1427                         kfree(cp);
1428                         break;
1429                 }
1430         }
1431         mutex_unlock(&of_genpd_mutex);
1432 }
1433 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1434
1435 /**
1436  * of_genpd_get_from_provider() - Look-up PM domain
1437  * @genpdspec: OF phandle args to use for look-up
1438  *
1439  * Looks for a PM domain provider under the node specified by @genpdspec and if
1440  * found, uses xlate function of the provider to map phandle args to a PM
1441  * domain.
1442  *
1443  * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1444  * on failure.
1445  */
1446 struct generic_pm_domain *of_genpd_get_from_provider(
1447                                         struct of_phandle_args *genpdspec)
1448 {
1449         struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1450         struct of_genpd_provider *provider;
1451
1452         mutex_lock(&of_genpd_mutex);
1453
1454         /* Check if we have such a provider in our array */
1455         list_for_each_entry(provider, &of_genpd_providers, link) {
1456                 if (provider->node == genpdspec->np)
1457                         genpd = provider->xlate(genpdspec, provider->data);
1458                 if (!IS_ERR(genpd))
1459                         break;
1460         }
1461
1462         mutex_unlock(&of_genpd_mutex);
1463
1464         return genpd;
1465 }
1466 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1467
1468 /**
1469  * genpd_dev_pm_detach - Detach a device from its PM domain.
1470  * @dev: Device to detach.
1471  * @power_off: Currently not used
1472  *
1473  * Try to locate a corresponding generic PM domain, which the device was
1474  * attached to previously. If such is found, the device is detached from it.
1475  */
1476 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1477 {
1478         struct generic_pm_domain *pd;
1479         unsigned int i;
1480         int ret = 0;
1481
1482         pd = pm_genpd_lookup_dev(dev);
1483         if (!pd)
1484                 return;
1485
1486         dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1487
1488         for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1489                 ret = pm_genpd_remove_device(pd, dev);
1490                 if (ret != -EAGAIN)
1491                         break;
1492
1493                 mdelay(i);
1494                 cond_resched();
1495         }
1496
1497         if (ret < 0) {
1498                 dev_err(dev, "failed to remove from PM domain %s: %d",
1499                         pd->name, ret);
1500                 return;
1501         }
1502
1503         /* Check if PM domain can be powered off after removing this device. */
1504         genpd_queue_power_off_work(pd);
1505 }
1506
1507 static void genpd_dev_pm_sync(struct device *dev)
1508 {
1509         struct generic_pm_domain *pd;
1510
1511         pd = dev_to_genpd(dev);
1512         if (IS_ERR(pd))
1513                 return;
1514
1515         genpd_queue_power_off_work(pd);
1516 }
1517
1518 /**
1519  * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1520  * @dev: Device to attach.
1521  *
1522  * Parse device's OF node to find a PM domain specifier. If such is found,
1523  * attaches the device to retrieved pm_domain ops.
1524  *
1525  * Both generic and legacy Samsung-specific DT bindings are supported to keep
1526  * backwards compatibility with existing DTBs.
1527  *
1528  * Returns 0 on successfully attached PM domain or negative error code. Note
1529  * that if a power-domain exists for the device, but it cannot be found or
1530  * turned on, then return -EPROBE_DEFER to ensure that the device is not
1531  * probed and to re-try again later.
1532  */
1533 int genpd_dev_pm_attach(struct device *dev)
1534 {
1535         struct of_phandle_args pd_args;
1536         struct generic_pm_domain *pd;
1537         unsigned int i;
1538         int ret;
1539
1540         if (!dev->of_node)
1541                 return -ENODEV;
1542
1543         if (dev->pm_domain)
1544                 return -EEXIST;
1545
1546         ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1547                                         "#power-domain-cells", 0, &pd_args);
1548         if (ret < 0) {
1549                 if (ret != -ENOENT)
1550                         return ret;
1551
1552                 /*
1553                  * Try legacy Samsung-specific bindings
1554                  * (for backwards compatibility of DT ABI)
1555                  */
1556                 pd_args.args_count = 0;
1557                 pd_args.np = of_parse_phandle(dev->of_node,
1558                                                 "samsung,power-domain", 0);
1559                 if (!pd_args.np)
1560                         return -ENOENT;
1561         }
1562
1563         pd = of_genpd_get_from_provider(&pd_args);
1564         of_node_put(pd_args.np);
1565         if (IS_ERR(pd)) {
1566                 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1567                         __func__, PTR_ERR(pd));
1568                 return -EPROBE_DEFER;
1569         }
1570
1571         dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1572
1573         for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1574                 ret = pm_genpd_add_device(pd, dev);
1575                 if (ret != -EAGAIN)
1576                         break;
1577
1578                 mdelay(i);
1579                 cond_resched();
1580         }
1581
1582         if (ret < 0) {
1583                 dev_err(dev, "failed to add to PM domain %s: %d",
1584                         pd->name, ret);
1585                 goto out;
1586         }
1587
1588         dev->pm_domain->detach = genpd_dev_pm_detach;
1589         dev->pm_domain->sync = genpd_dev_pm_sync;
1590         ret = genpd_poweron(pd);
1591
1592 out:
1593         return ret ? -EPROBE_DEFER : 0;
1594 }
1595 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1596 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1597
1598
1599 /***        debugfs support        ***/
1600
1601 #ifdef CONFIG_PM_ADVANCED_DEBUG
1602 #include <linux/pm.h>
1603 #include <linux/device.h>
1604 #include <linux/debugfs.h>
1605 #include <linux/seq_file.h>
1606 #include <linux/init.h>
1607 #include <linux/kobject.h>
1608 static struct dentry *pm_genpd_debugfs_dir;
1609
1610 /*
1611  * TODO: This function is a slightly modified version of rtpm_status_show
1612  * from sysfs.c, so generalize it.
1613  */
1614 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1615 {
1616         static const char * const status_lookup[] = {
1617                 [RPM_ACTIVE] = "active",
1618                 [RPM_RESUMING] = "resuming",
1619                 [RPM_SUSPENDED] = "suspended",
1620                 [RPM_SUSPENDING] = "suspending"
1621         };
1622         const char *p = "";
1623
1624         if (dev->power.runtime_error)
1625                 p = "error";
1626         else if (dev->power.disable_depth)
1627                 p = "unsupported";
1628         else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1629                 p = status_lookup[dev->power.runtime_status];
1630         else
1631                 WARN_ON(1);
1632
1633         seq_puts(s, p);
1634 }
1635
1636 static int pm_genpd_summary_one(struct seq_file *s,
1637                                 struct generic_pm_domain *genpd)
1638 {
1639         static const char * const status_lookup[] = {
1640                 [GPD_STATE_ACTIVE] = "on",
1641                 [GPD_STATE_POWER_OFF] = "off"
1642         };
1643         struct pm_domain_data *pm_data;
1644         const char *kobj_path;
1645         struct gpd_link *link;
1646         int ret;
1647
1648         ret = mutex_lock_interruptible(&genpd->lock);
1649         if (ret)
1650                 return -ERESTARTSYS;
1651
1652         if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1653                 goto exit;
1654         seq_printf(s, "%-30s  %-15s ", genpd->name, status_lookup[genpd->status]);
1655
1656         /*
1657          * Modifications on the list require holding locks on both
1658          * master and slave, so we are safe.
1659          * Also genpd->name is immutable.
1660          */
1661         list_for_each_entry(link, &genpd->master_links, master_node) {
1662                 seq_printf(s, "%s", link->slave->name);
1663                 if (!list_is_last(&link->master_node, &genpd->master_links))
1664                         seq_puts(s, ", ");
1665         }
1666
1667         list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1668                 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1669                 if (kobj_path == NULL)
1670                         continue;
1671
1672                 seq_printf(s, "\n    %-50s  ", kobj_path);
1673                 rtpm_status_str(s, pm_data->dev);
1674                 kfree(kobj_path);
1675         }
1676
1677         seq_puts(s, "\n");
1678 exit:
1679         mutex_unlock(&genpd->lock);
1680
1681         return 0;
1682 }
1683
1684 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1685 {
1686         struct generic_pm_domain *genpd;
1687         int ret = 0;
1688
1689         seq_puts(s, "domain                          status          slaves\n");
1690         seq_puts(s, "    /device                                             runtime status\n");
1691         seq_puts(s, "----------------------------------------------------------------------\n");
1692
1693         ret = mutex_lock_interruptible(&gpd_list_lock);
1694         if (ret)
1695                 return -ERESTARTSYS;
1696
1697         list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1698                 ret = pm_genpd_summary_one(s, genpd);
1699                 if (ret)
1700                         break;
1701         }
1702         mutex_unlock(&gpd_list_lock);
1703
1704         return ret;
1705 }
1706
1707 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1708 {
1709         return single_open(file, pm_genpd_summary_show, NULL);
1710 }
1711
1712 static const struct file_operations pm_genpd_summary_fops = {
1713         .open = pm_genpd_summary_open,
1714         .read = seq_read,
1715         .llseek = seq_lseek,
1716         .release = single_release,
1717 };
1718
1719 static int __init pm_genpd_debug_init(void)
1720 {
1721         struct dentry *d;
1722
1723         pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1724
1725         if (!pm_genpd_debugfs_dir)
1726                 return -ENOMEM;
1727
1728         d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1729                         pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1730         if (!d)
1731                 return -ENOMEM;
1732
1733         return 0;
1734 }
1735 late_initcall(pm_genpd_debug_init);
1736
1737 static void __exit pm_genpd_debug_exit(void)
1738 {
1739         debugfs_remove_recursive(pm_genpd_debugfs_dir);
1740 }
1741 __exitcall(pm_genpd_debug_exit);
1742 #endif /* CONFIG_PM_ADVANCED_DEBUG */