2 * drivers/base/power/domain.c - Common code related to device power domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/delay.h>
10 #include <linux/kernel.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>
23 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
25 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
27 type (*__routine)(struct device *__d); \
28 type __ret = (type)0; \
30 __routine = genpd->dev_ops.callback; \
32 __ret = __routine(dev); \
37 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
39 ktime_t __start = ktime_get(); \
40 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
41 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
42 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
43 if (!__retval && __elapsed > __td->field) { \
44 __td->field = __elapsed; \
45 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
47 genpd->max_off_time_changed = true; \
48 __td->constraint_changed = true; \
53 static LIST_HEAD(gpd_list);
54 static DEFINE_MUTEX(gpd_list_lock);
57 * Get the generic PM domain for a particular struct device.
58 * This validates the struct device pointer, the PM domain pointer,
59 * and checks that the PM domain pointer is a real generic PM domain.
60 * Any failure results in NULL being returned.
62 struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
64 struct generic_pm_domain *genpd = NULL, *gpd;
66 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
69 mutex_lock(&gpd_list_lock);
70 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
71 if (&gpd->domain == dev->pm_domain) {
76 mutex_unlock(&gpd_list_lock);
82 * This should only be used where we are certain that the pm_domain
83 * attached to the device is a genpd domain.
85 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
87 if (IS_ERR_OR_NULL(dev->pm_domain))
88 return ERR_PTR(-EINVAL);
90 return pd_to_genpd(dev->pm_domain);
93 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
95 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
96 stop_latency_ns, "stop");
99 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev,
103 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
105 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
106 start_latency_ns, "start");
109 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
113 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
114 ret = !!atomic_dec_and_test(&genpd->sd_count);
119 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
121 atomic_inc(&genpd->sd_count);
122 smp_mb__after_atomic();
125 static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
131 if (!genpd->power_on)
135 return genpd->power_on(genpd);
137 time_start = ktime_get();
138 ret = genpd->power_on(genpd);
142 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
143 if (elapsed_ns <= genpd->power_on_latency_ns)
146 genpd->power_on_latency_ns = elapsed_ns;
147 genpd->max_off_time_changed = true;
148 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
149 genpd->name, "on", elapsed_ns);
154 static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
160 if (!genpd->power_off)
164 return genpd->power_off(genpd);
166 time_start = ktime_get();
167 ret = genpd->power_off(genpd);
171 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
172 if (elapsed_ns <= genpd->power_off_latency_ns)
175 genpd->power_off_latency_ns = elapsed_ns;
176 genpd->max_off_time_changed = true;
177 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
178 genpd->name, "off", elapsed_ns);
184 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
185 * @genpd: PM domait to power off.
187 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
190 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
192 queue_work(pm_wq, &genpd->power_off_work);
196 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
197 * @genpd: PM domain to power up.
199 * Restore power to @genpd and all of its masters so that it is possible to
200 * resume a device belonging to it.
202 static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
204 struct gpd_link *link;
207 if (genpd->status == GPD_STATE_ACTIVE
208 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
212 * The list is guaranteed not to change while the loop below is being
213 * executed, unless one of the masters' .power_on() callbacks fiddles
216 list_for_each_entry(link, &genpd->slave_links, slave_node) {
217 genpd_sd_counter_inc(link->master);
219 ret = pm_genpd_poweron(link->master);
221 genpd_sd_counter_dec(link->master);
226 ret = genpd_power_on(genpd, true);
230 genpd->status = GPD_STATE_ACTIVE;
234 list_for_each_entry_continue_reverse(link,
237 genpd_sd_counter_dec(link->master);
238 genpd_queue_power_off_work(link->master);
245 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
246 * @genpd: PM domain to power up.
248 int pm_genpd_poweron(struct generic_pm_domain *genpd)
252 mutex_lock(&genpd->lock);
253 ret = __pm_genpd_poweron(genpd);
254 mutex_unlock(&genpd->lock);
258 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
260 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
261 save_state_latency_ns, "state save");
264 static int genpd_restore_dev(struct generic_pm_domain *genpd,
265 struct device *dev, bool timed)
268 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
270 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
271 restore_state_latency_ns,
275 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
276 unsigned long val, void *ptr)
278 struct generic_pm_domain_data *gpd_data;
281 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
282 dev = gpd_data->base.dev;
285 struct generic_pm_domain *genpd;
286 struct pm_domain_data *pdd;
288 spin_lock_irq(&dev->power.lock);
290 pdd = dev->power.subsys_data ?
291 dev->power.subsys_data->domain_data : NULL;
292 if (pdd && pdd->dev) {
293 to_gpd_data(pdd)->td.constraint_changed = true;
294 genpd = dev_to_genpd(dev);
296 genpd = ERR_PTR(-ENODATA);
299 spin_unlock_irq(&dev->power.lock);
301 if (!IS_ERR(genpd)) {
302 mutex_lock(&genpd->lock);
303 genpd->max_off_time_changed = true;
304 mutex_unlock(&genpd->lock);
308 if (!dev || dev->power.ignore_children)
316 * pm_genpd_poweroff - Remove power from a given PM domain.
317 * @genpd: PM domain to power down.
318 * @is_async: PM domain is powered down from a scheduled work
320 * If all of the @genpd's devices have been suspended and all of its subdomains
321 * have been powered down, remove power from @genpd.
323 static int pm_genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
325 struct pm_domain_data *pdd;
326 struct gpd_link *link;
327 unsigned int not_suspended = 0;
330 * Do not try to power off the domain in the following situations:
331 * (1) The domain is already in the "power off" state.
332 * (2) System suspend is in progress.
334 if (genpd->status == GPD_STATE_POWER_OFF
335 || genpd->prepared_count > 0)
338 if (atomic_read(&genpd->sd_count) > 0)
341 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
342 enum pm_qos_flags_status stat;
344 stat = dev_pm_qos_flags(pdd->dev,
345 PM_QOS_FLAG_NO_POWER_OFF
346 | PM_QOS_FLAG_REMOTE_WAKEUP);
347 if (stat > PM_QOS_FLAGS_NONE)
350 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
351 || pdd->dev->power.irq_safe))
355 if (not_suspended > 1 || (not_suspended == 1 && is_async))
358 if (genpd->gov && genpd->gov->power_down_ok) {
359 if (!genpd->gov->power_down_ok(&genpd->domain))
363 if (genpd->power_off) {
366 if (atomic_read(&genpd->sd_count) > 0)
370 * If sd_count > 0 at this point, one of the subdomains hasn't
371 * managed to call pm_genpd_poweron() for the master yet after
372 * incrementing it. In that case pm_genpd_poweron() will wait
373 * for us to drop the lock, so we can call .power_off() and let
374 * the pm_genpd_poweron() restore power for us (this shouldn't
375 * happen very often).
377 ret = genpd_power_off(genpd, true);
382 genpd->status = GPD_STATE_POWER_OFF;
384 list_for_each_entry(link, &genpd->slave_links, slave_node) {
385 genpd_sd_counter_dec(link->master);
386 genpd_queue_power_off_work(link->master);
393 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
394 * @work: Work structure used for scheduling the execution of this function.
396 static void genpd_power_off_work_fn(struct work_struct *work)
398 struct generic_pm_domain *genpd;
400 genpd = container_of(work, struct generic_pm_domain, power_off_work);
402 mutex_lock(&genpd->lock);
403 pm_genpd_poweroff(genpd, true);
404 mutex_unlock(&genpd->lock);
408 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
409 * @dev: Device to suspend.
411 * Carry out a runtime suspend of a device under the assumption that its
412 * pm_domain field points to the domain member of an object of type
413 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
415 static int pm_genpd_runtime_suspend(struct device *dev)
417 struct generic_pm_domain *genpd;
418 bool (*stop_ok)(struct device *__dev);
421 dev_dbg(dev, "%s()\n", __func__);
423 genpd = dev_to_genpd(dev);
427 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
428 if (stop_ok && !stop_ok(dev))
431 ret = genpd_save_dev(genpd, dev);
435 ret = genpd_stop_dev(genpd, dev);
437 genpd_restore_dev(genpd, dev, true);
442 * If power.irq_safe is set, this routine will be run with interrupts
443 * off, so it can't use mutexes.
445 if (dev->power.irq_safe)
448 mutex_lock(&genpd->lock);
449 pm_genpd_poweroff(genpd, false);
450 mutex_unlock(&genpd->lock);
456 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
457 * @dev: Device to resume.
459 * Carry out a runtime resume of a device under the assumption that its
460 * pm_domain field points to the domain member of an object of type
461 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
463 static int pm_genpd_runtime_resume(struct device *dev)
465 struct generic_pm_domain *genpd;
469 dev_dbg(dev, "%s()\n", __func__);
471 genpd = dev_to_genpd(dev);
475 /* If power.irq_safe, the PM domain is never powered off. */
476 if (dev->power.irq_safe) {
481 mutex_lock(&genpd->lock);
482 ret = __pm_genpd_poweron(genpd);
483 mutex_unlock(&genpd->lock);
489 genpd_start_dev(genpd, dev, timed);
490 genpd_restore_dev(genpd, dev, timed);
495 static bool pd_ignore_unused;
496 static int __init pd_ignore_unused_setup(char *__unused)
498 pd_ignore_unused = true;
501 __setup("pd_ignore_unused", pd_ignore_unused_setup);
504 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
506 static int __init genpd_poweroff_unused(void)
508 struct generic_pm_domain *genpd;
510 if (pd_ignore_unused) {
511 pr_warn("genpd: Not disabling unused power domains\n");
515 mutex_lock(&gpd_list_lock);
517 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
518 genpd_queue_power_off_work(genpd);
520 mutex_unlock(&gpd_list_lock);
524 late_initcall(genpd_poweroff_unused);
526 #ifdef CONFIG_PM_SLEEP
529 * pm_genpd_present - Check if the given PM domain has been initialized.
530 * @genpd: PM domain to check.
532 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
534 const struct generic_pm_domain *gpd;
536 if (IS_ERR_OR_NULL(genpd))
539 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
546 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
549 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
553 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
554 * @genpd: PM domain to power off, if possible.
555 * @timed: True if latency measurements are allowed.
557 * Check if the given PM domain can be powered off (during system suspend or
558 * hibernation) and do that if so. Also, in that case propagate to its masters.
560 * This function is only called in "noirq" and "syscore" stages of system power
561 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
562 * executed sequentially, so it is guaranteed that it will never run twice in
565 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
568 struct gpd_link *link;
570 if (genpd->status == GPD_STATE_POWER_OFF)
573 if (genpd->suspended_count != genpd->device_count
574 || atomic_read(&genpd->sd_count) > 0)
577 genpd_power_off(genpd, timed);
579 genpd->status = GPD_STATE_POWER_OFF;
581 list_for_each_entry(link, &genpd->slave_links, slave_node) {
582 genpd_sd_counter_dec(link->master);
583 pm_genpd_sync_poweroff(link->master, timed);
588 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
589 * @genpd: PM domain to power on.
590 * @timed: True if latency measurements are allowed.
592 * This function is only called in "noirq" and "syscore" stages of system power
593 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
594 * executed sequentially, so it is guaranteed that it will never run twice in
597 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
600 struct gpd_link *link;
602 if (genpd->status == GPD_STATE_ACTIVE)
605 list_for_each_entry(link, &genpd->slave_links, slave_node) {
606 pm_genpd_sync_poweron(link->master, timed);
607 genpd_sd_counter_inc(link->master);
610 genpd_power_on(genpd, timed);
612 genpd->status = GPD_STATE_ACTIVE;
616 * resume_needed - Check whether to resume a device before system suspend.
617 * @dev: Device to check.
618 * @genpd: PM domain the device belongs to.
620 * There are two cases in which a device that can wake up the system from sleep
621 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
622 * to wake up the system and it has to remain active for this purpose while the
623 * system is in the sleep state and (2) if the device is not enabled to wake up
624 * the system from sleep states and it generally doesn't generate wakeup signals
625 * by itself (those signals are generated on its behalf by other parts of the
626 * system). In the latter case it may be necessary to reconfigure the device's
627 * wakeup settings during system suspend, because it may have been set up to
628 * signal remote wakeup from the system's working state as needed by runtime PM.
629 * Return 'true' in either of the above cases.
631 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
635 if (!device_can_wakeup(dev))
638 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
639 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
643 * pm_genpd_prepare - Start power transition of a device in a PM domain.
644 * @dev: Device to start the transition of.
646 * Start a power transition of a device (during a system-wide power transition)
647 * under the assumption that its pm_domain field points to the domain member of
648 * an object of type struct generic_pm_domain representing a PM domain
649 * consisting of I/O devices.
651 static int pm_genpd_prepare(struct device *dev)
653 struct generic_pm_domain *genpd;
656 dev_dbg(dev, "%s()\n", __func__);
658 genpd = dev_to_genpd(dev);
663 * If a wakeup request is pending for the device, it should be woken up
664 * at this point and a system wakeup event should be reported if it's
665 * set up to wake up the system from sleep states.
667 pm_runtime_get_noresume(dev);
668 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
669 pm_wakeup_event(dev, 0);
671 if (pm_wakeup_pending()) {
676 if (resume_needed(dev, genpd))
677 pm_runtime_resume(dev);
679 mutex_lock(&genpd->lock);
681 if (genpd->prepared_count++ == 0) {
682 genpd->suspended_count = 0;
683 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
686 mutex_unlock(&genpd->lock);
688 if (genpd->suspend_power_off) {
689 pm_runtime_put_noidle(dev);
694 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
695 * so pm_genpd_poweron() will return immediately, but if the device
696 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
697 * to make it operational.
699 pm_runtime_resume(dev);
700 __pm_runtime_disable(dev, false);
702 ret = pm_generic_prepare(dev);
704 mutex_lock(&genpd->lock);
706 if (--genpd->prepared_count == 0)
707 genpd->suspend_power_off = false;
709 mutex_unlock(&genpd->lock);
710 pm_runtime_enable(dev);
718 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
719 * @dev: Device to suspend.
721 * Suspend a device under the assumption that its pm_domain field points to the
722 * domain member of an object of type struct generic_pm_domain representing
723 * a PM domain consisting of I/O devices.
725 static int pm_genpd_suspend(struct device *dev)
727 struct generic_pm_domain *genpd;
729 dev_dbg(dev, "%s()\n", __func__);
731 genpd = dev_to_genpd(dev);
735 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
739 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
740 * @dev: Device to suspend.
742 * Carry out a late suspend of a device under the assumption that its
743 * pm_domain field points to the domain member of an object of type
744 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
746 static int pm_genpd_suspend_late(struct device *dev)
748 struct generic_pm_domain *genpd;
750 dev_dbg(dev, "%s()\n", __func__);
752 genpd = dev_to_genpd(dev);
756 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
760 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
761 * @dev: Device to suspend.
763 * Stop the device and remove power from the domain if all devices in it have
766 static int pm_genpd_suspend_noirq(struct device *dev)
768 struct generic_pm_domain *genpd;
770 dev_dbg(dev, "%s()\n", __func__);
772 genpd = dev_to_genpd(dev);
776 if (genpd->suspend_power_off
777 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
780 genpd_stop_dev(genpd, dev);
783 * Since all of the "noirq" callbacks are executed sequentially, it is
784 * guaranteed that this function will never run twice in parallel for
785 * the same PM domain, so it is not necessary to use locking here.
787 genpd->suspended_count++;
788 pm_genpd_sync_poweroff(genpd, true);
794 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
795 * @dev: Device to resume.
797 * Restore power to the device's PM domain, if necessary, and start the device.
799 static int pm_genpd_resume_noirq(struct device *dev)
801 struct generic_pm_domain *genpd;
803 dev_dbg(dev, "%s()\n", __func__);
805 genpd = dev_to_genpd(dev);
809 if (genpd->suspend_power_off
810 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
814 * Since all of the "noirq" callbacks are executed sequentially, it is
815 * guaranteed that this function will never run twice in parallel for
816 * the same PM domain, so it is not necessary to use locking here.
818 pm_genpd_sync_poweron(genpd, true);
819 genpd->suspended_count--;
821 return genpd_start_dev(genpd, dev, true);
825 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
826 * @dev: Device to resume.
828 * Carry out an early resume of a device under the assumption that its
829 * pm_domain field points to the domain member of an object of type
830 * struct generic_pm_domain representing a power domain consisting of I/O
833 static int pm_genpd_resume_early(struct device *dev)
835 struct generic_pm_domain *genpd;
837 dev_dbg(dev, "%s()\n", __func__);
839 genpd = dev_to_genpd(dev);
843 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
847 * pm_genpd_resume - Resume of device in an I/O PM domain.
848 * @dev: Device to resume.
850 * Resume a device under the assumption that its pm_domain field points to the
851 * domain member of an object of type struct generic_pm_domain representing
852 * a power domain consisting of I/O devices.
854 static int pm_genpd_resume(struct device *dev)
856 struct generic_pm_domain *genpd;
858 dev_dbg(dev, "%s()\n", __func__);
860 genpd = dev_to_genpd(dev);
864 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
868 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
869 * @dev: Device to freeze.
871 * Freeze a device under the assumption that its pm_domain field points to the
872 * domain member of an object of type struct generic_pm_domain representing
873 * a power domain consisting of I/O devices.
875 static int pm_genpd_freeze(struct device *dev)
877 struct generic_pm_domain *genpd;
879 dev_dbg(dev, "%s()\n", __func__);
881 genpd = dev_to_genpd(dev);
885 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
889 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
890 * @dev: Device to freeze.
892 * Carry out a late freeze of a device under the assumption that its
893 * pm_domain field points to the domain member of an object of type
894 * struct generic_pm_domain representing a power domain consisting of I/O
897 static int pm_genpd_freeze_late(struct device *dev)
899 struct generic_pm_domain *genpd;
901 dev_dbg(dev, "%s()\n", __func__);
903 genpd = dev_to_genpd(dev);
907 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
911 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
912 * @dev: Device to freeze.
914 * Carry out a late freeze of a device under the assumption that its
915 * pm_domain field points to the domain member of an object of type
916 * struct generic_pm_domain representing a power domain consisting of I/O
919 static int pm_genpd_freeze_noirq(struct device *dev)
921 struct generic_pm_domain *genpd;
923 dev_dbg(dev, "%s()\n", __func__);
925 genpd = dev_to_genpd(dev);
929 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
933 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
934 * @dev: Device to thaw.
936 * Start the device, unless power has been removed from the domain already
937 * before the system transition.
939 static int pm_genpd_thaw_noirq(struct device *dev)
941 struct generic_pm_domain *genpd;
943 dev_dbg(dev, "%s()\n", __func__);
945 genpd = dev_to_genpd(dev);
949 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev, true);
953 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
954 * @dev: Device to thaw.
956 * Carry out an early thaw of a device under the assumption that its
957 * pm_domain field points to the domain member of an object of type
958 * struct generic_pm_domain representing a power domain consisting of I/O
961 static int pm_genpd_thaw_early(struct device *dev)
963 struct generic_pm_domain *genpd;
965 dev_dbg(dev, "%s()\n", __func__);
967 genpd = dev_to_genpd(dev);
971 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
975 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
976 * @dev: Device to thaw.
978 * Thaw a device under the assumption that its pm_domain field points to the
979 * domain member of an object of type struct generic_pm_domain representing
980 * a power domain consisting of I/O devices.
982 static int pm_genpd_thaw(struct device *dev)
984 struct generic_pm_domain *genpd;
986 dev_dbg(dev, "%s()\n", __func__);
988 genpd = dev_to_genpd(dev);
992 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
996 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
997 * @dev: Device to resume.
999 * Make sure the domain will be in the same power state as before the
1000 * hibernation the system is resuming from and start the device if necessary.
1002 static int pm_genpd_restore_noirq(struct device *dev)
1004 struct generic_pm_domain *genpd;
1006 dev_dbg(dev, "%s()\n", __func__);
1008 genpd = dev_to_genpd(dev);
1013 * Since all of the "noirq" callbacks are executed sequentially, it is
1014 * guaranteed that this function will never run twice in parallel for
1015 * the same PM domain, so it is not necessary to use locking here.
1017 * At this point suspended_count == 0 means we are being run for the
1018 * first time for the given domain in the present cycle.
1020 if (genpd->suspended_count++ == 0) {
1022 * The boot kernel might put the domain into arbitrary state,
1023 * so make it appear as powered off to pm_genpd_sync_poweron(),
1024 * so that it tries to power it on in case it was really off.
1026 genpd->status = GPD_STATE_POWER_OFF;
1027 if (genpd->suspend_power_off) {
1029 * If the domain was off before the hibernation, make
1030 * sure it will be off going forward.
1032 genpd_power_off(genpd, true);
1038 if (genpd->suspend_power_off)
1041 pm_genpd_sync_poweron(genpd, true);
1043 return genpd_start_dev(genpd, dev, true);
1047 * pm_genpd_complete - Complete power transition of a device in a power domain.
1048 * @dev: Device to complete the transition of.
1050 * Complete a power transition of a device (during a system-wide power
1051 * transition) under the assumption that its pm_domain field points to the
1052 * domain member of an object of type struct generic_pm_domain representing
1053 * a power domain consisting of I/O devices.
1055 static void pm_genpd_complete(struct device *dev)
1057 struct generic_pm_domain *genpd;
1060 dev_dbg(dev, "%s()\n", __func__);
1062 genpd = dev_to_genpd(dev);
1066 mutex_lock(&genpd->lock);
1068 run_complete = !genpd->suspend_power_off;
1069 if (--genpd->prepared_count == 0)
1070 genpd->suspend_power_off = false;
1072 mutex_unlock(&genpd->lock);
1075 pm_generic_complete(dev);
1076 pm_runtime_set_active(dev);
1077 pm_runtime_enable(dev);
1078 pm_request_idle(dev);
1083 * genpd_syscore_switch - Switch power during system core suspend or resume.
1084 * @dev: Device that normally is marked as "always on" to switch power for.
1086 * This routine may only be called during the system core (syscore) suspend or
1087 * resume phase for devices whose "always on" flags are set.
1089 static void genpd_syscore_switch(struct device *dev, bool suspend)
1091 struct generic_pm_domain *genpd;
1093 genpd = dev_to_genpd(dev);
1094 if (!pm_genpd_present(genpd))
1098 genpd->suspended_count++;
1099 pm_genpd_sync_poweroff(genpd, false);
1101 pm_genpd_sync_poweron(genpd, false);
1102 genpd->suspended_count--;
1106 void pm_genpd_syscore_poweroff(struct device *dev)
1108 genpd_syscore_switch(dev, true);
1110 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1112 void pm_genpd_syscore_poweron(struct device *dev)
1114 genpd_syscore_switch(dev, false);
1116 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1118 #else /* !CONFIG_PM_SLEEP */
1120 #define pm_genpd_prepare NULL
1121 #define pm_genpd_suspend NULL
1122 #define pm_genpd_suspend_late NULL
1123 #define pm_genpd_suspend_noirq NULL
1124 #define pm_genpd_resume_early NULL
1125 #define pm_genpd_resume_noirq NULL
1126 #define pm_genpd_resume NULL
1127 #define pm_genpd_freeze NULL
1128 #define pm_genpd_freeze_late NULL
1129 #define pm_genpd_freeze_noirq NULL
1130 #define pm_genpd_thaw_early NULL
1131 #define pm_genpd_thaw_noirq NULL
1132 #define pm_genpd_thaw NULL
1133 #define pm_genpd_restore_noirq NULL
1134 #define pm_genpd_complete NULL
1136 #endif /* CONFIG_PM_SLEEP */
1138 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1139 struct generic_pm_domain *genpd,
1140 struct gpd_timing_data *td)
1142 struct generic_pm_domain_data *gpd_data;
1145 ret = dev_pm_get_subsys_data(dev);
1147 return ERR_PTR(ret);
1149 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1158 gpd_data->base.dev = dev;
1159 gpd_data->td.constraint_changed = true;
1160 gpd_data->td.effective_constraint_ns = -1;
1161 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1163 spin_lock_irq(&dev->power.lock);
1165 if (dev->power.subsys_data->domain_data) {
1170 dev->power.subsys_data->domain_data = &gpd_data->base;
1171 dev->pm_domain = &genpd->domain;
1173 spin_unlock_irq(&dev->power.lock);
1178 spin_unlock_irq(&dev->power.lock);
1181 dev_pm_put_subsys_data(dev);
1182 return ERR_PTR(ret);
1185 static void genpd_free_dev_data(struct device *dev,
1186 struct generic_pm_domain_data *gpd_data)
1188 spin_lock_irq(&dev->power.lock);
1190 dev->pm_domain = NULL;
1191 dev->power.subsys_data->domain_data = NULL;
1193 spin_unlock_irq(&dev->power.lock);
1196 dev_pm_put_subsys_data(dev);
1200 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1201 * @genpd: PM domain to add the device to.
1202 * @dev: Device to be added.
1203 * @td: Set of PM QoS timing parameters to attach to the device.
1205 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1206 struct gpd_timing_data *td)
1208 struct generic_pm_domain_data *gpd_data;
1211 dev_dbg(dev, "%s()\n", __func__);
1213 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1216 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1217 if (IS_ERR(gpd_data))
1218 return PTR_ERR(gpd_data);
1220 mutex_lock(&genpd->lock);
1222 if (genpd->prepared_count > 0) {
1227 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1231 genpd->device_count++;
1232 genpd->max_off_time_changed = true;
1234 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1237 mutex_unlock(&genpd->lock);
1240 genpd_free_dev_data(dev, gpd_data);
1242 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1248 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1249 * @genpd: PM domain to remove the device from.
1250 * @dev: Device to be removed.
1252 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1255 struct generic_pm_domain_data *gpd_data;
1256 struct pm_domain_data *pdd;
1259 dev_dbg(dev, "%s()\n", __func__);
1261 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1264 /* The above validation also means we have existing domain_data. */
1265 pdd = dev->power.subsys_data->domain_data;
1266 gpd_data = to_gpd_data(pdd);
1267 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1269 mutex_lock(&genpd->lock);
1271 if (genpd->prepared_count > 0) {
1276 genpd->device_count--;
1277 genpd->max_off_time_changed = true;
1279 if (genpd->detach_dev)
1280 genpd->detach_dev(genpd, dev);
1282 list_del_init(&pdd->list_node);
1284 mutex_unlock(&genpd->lock);
1286 genpd_free_dev_data(dev, gpd_data);
1291 mutex_unlock(&genpd->lock);
1292 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1298 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1299 * @genpd: Master PM domain to add the subdomain to.
1300 * @subdomain: Subdomain to be added.
1302 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1303 struct generic_pm_domain *subdomain)
1305 struct gpd_link *link;
1308 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1309 || genpd == subdomain)
1312 mutex_lock(&genpd->lock);
1313 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1315 if (genpd->status == GPD_STATE_POWER_OFF
1316 && subdomain->status != GPD_STATE_POWER_OFF) {
1321 list_for_each_entry(link, &genpd->master_links, master_node) {
1322 if (link->slave == subdomain && link->master == genpd) {
1328 link = kzalloc(sizeof(*link), GFP_KERNEL);
1333 link->master = genpd;
1334 list_add_tail(&link->master_node, &genpd->master_links);
1335 link->slave = subdomain;
1336 list_add_tail(&link->slave_node, &subdomain->slave_links);
1337 if (subdomain->status != GPD_STATE_POWER_OFF)
1338 genpd_sd_counter_inc(genpd);
1341 mutex_unlock(&subdomain->lock);
1342 mutex_unlock(&genpd->lock);
1348 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1349 * @genpd: Master PM domain to remove the subdomain from.
1350 * @subdomain: Subdomain to be removed.
1352 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1353 struct generic_pm_domain *subdomain)
1355 struct gpd_link *link;
1358 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1361 mutex_lock(&genpd->lock);
1363 if (!list_empty(&subdomain->slave_links) || subdomain->device_count) {
1364 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1370 list_for_each_entry(link, &genpd->master_links, master_node) {
1371 if (link->slave != subdomain)
1374 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1376 list_del(&link->master_node);
1377 list_del(&link->slave_node);
1379 if (subdomain->status != GPD_STATE_POWER_OFF)
1380 genpd_sd_counter_dec(genpd);
1382 mutex_unlock(&subdomain->lock);
1389 mutex_unlock(&genpd->lock);
1394 /* Default device callbacks for generic PM domains. */
1397 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1398 * @dev: Device to handle.
1400 static int pm_genpd_default_save_state(struct device *dev)
1402 int (*cb)(struct device *__dev);
1404 if (dev->type && dev->type->pm)
1405 cb = dev->type->pm->runtime_suspend;
1406 else if (dev->class && dev->class->pm)
1407 cb = dev->class->pm->runtime_suspend;
1408 else if (dev->bus && dev->bus->pm)
1409 cb = dev->bus->pm->runtime_suspend;
1413 if (!cb && dev->driver && dev->driver->pm)
1414 cb = dev->driver->pm->runtime_suspend;
1416 return cb ? cb(dev) : 0;
1420 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1421 * @dev: Device to handle.
1423 static int pm_genpd_default_restore_state(struct device *dev)
1425 int (*cb)(struct device *__dev);
1427 if (dev->type && dev->type->pm)
1428 cb = dev->type->pm->runtime_resume;
1429 else if (dev->class && dev->class->pm)
1430 cb = dev->class->pm->runtime_resume;
1431 else if (dev->bus && dev->bus->pm)
1432 cb = dev->bus->pm->runtime_resume;
1436 if (!cb && dev->driver && dev->driver->pm)
1437 cb = dev->driver->pm->runtime_resume;
1439 return cb ? cb(dev) : 0;
1443 * pm_genpd_init - Initialize a generic I/O PM domain object.
1444 * @genpd: PM domain object to initialize.
1445 * @gov: PM domain governor to associate with the domain (may be NULL).
1446 * @is_off: Initial value of the domain's power_is_off field.
1448 void pm_genpd_init(struct generic_pm_domain *genpd,
1449 struct dev_power_governor *gov, bool is_off)
1451 if (IS_ERR_OR_NULL(genpd))
1454 INIT_LIST_HEAD(&genpd->master_links);
1455 INIT_LIST_HEAD(&genpd->slave_links);
1456 INIT_LIST_HEAD(&genpd->dev_list);
1457 mutex_init(&genpd->lock);
1459 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1460 atomic_set(&genpd->sd_count, 0);
1461 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1462 genpd->device_count = 0;
1463 genpd->max_off_time_ns = -1;
1464 genpd->max_off_time_changed = true;
1465 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1466 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1467 genpd->domain.ops.prepare = pm_genpd_prepare;
1468 genpd->domain.ops.suspend = pm_genpd_suspend;
1469 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1470 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1471 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1472 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1473 genpd->domain.ops.resume = pm_genpd_resume;
1474 genpd->domain.ops.freeze = pm_genpd_freeze;
1475 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1476 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1477 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1478 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1479 genpd->domain.ops.thaw = pm_genpd_thaw;
1480 genpd->domain.ops.poweroff = pm_genpd_suspend;
1481 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1482 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1483 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1484 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1485 genpd->domain.ops.restore = pm_genpd_resume;
1486 genpd->domain.ops.complete = pm_genpd_complete;
1487 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1488 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1490 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1491 genpd->dev_ops.stop = pm_clk_suspend;
1492 genpd->dev_ops.start = pm_clk_resume;
1495 mutex_lock(&gpd_list_lock);
1496 list_add(&genpd->gpd_list_node, &gpd_list);
1497 mutex_unlock(&gpd_list_lock);
1499 EXPORT_SYMBOL_GPL(pm_genpd_init);
1501 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1503 * Device Tree based PM domain providers.
1505 * The code below implements generic device tree based PM domain providers that
1506 * bind device tree nodes with generic PM domains registered in the system.
1508 * Any driver that registers generic PM domains and needs to support binding of
1509 * devices to these domains is supposed to register a PM domain provider, which
1510 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1512 * Two simple mapping functions have been provided for convenience:
1513 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1514 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1519 * struct of_genpd_provider - PM domain provider registration structure
1520 * @link: Entry in global list of PM domain providers
1521 * @node: Pointer to device tree node of PM domain provider
1522 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1524 * @data: context pointer to be passed into @xlate callback
1526 struct of_genpd_provider {
1527 struct list_head link;
1528 struct device_node *node;
1529 genpd_xlate_t xlate;
1533 /* List of registered PM domain providers. */
1534 static LIST_HEAD(of_genpd_providers);
1535 /* Mutex to protect the list above. */
1536 static DEFINE_MUTEX(of_genpd_mutex);
1539 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1540 * @genpdspec: OF phandle args to map into a PM domain
1541 * @data: xlate function private data - pointer to struct generic_pm_domain
1543 * This is a generic xlate function that can be used to model PM domains that
1544 * have their own device tree nodes. The private data of xlate function needs
1545 * to be a valid pointer to struct generic_pm_domain.
1547 struct generic_pm_domain *__of_genpd_xlate_simple(
1548 struct of_phandle_args *genpdspec,
1551 if (genpdspec->args_count != 0)
1552 return ERR_PTR(-EINVAL);
1555 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1558 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1559 * @genpdspec: OF phandle args to map into a PM domain
1560 * @data: xlate function private data - pointer to struct genpd_onecell_data
1562 * This is a generic xlate function that can be used to model simple PM domain
1563 * controllers that have one device tree node and provide multiple PM domains.
1564 * A single cell is used as an index into an array of PM domains specified in
1565 * the genpd_onecell_data struct when registering the provider.
1567 struct generic_pm_domain *__of_genpd_xlate_onecell(
1568 struct of_phandle_args *genpdspec,
1571 struct genpd_onecell_data *genpd_data = data;
1572 unsigned int idx = genpdspec->args[0];
1574 if (genpdspec->args_count != 1)
1575 return ERR_PTR(-EINVAL);
1577 if (idx >= genpd_data->num_domains) {
1578 pr_err("%s: invalid domain index %u\n", __func__, idx);
1579 return ERR_PTR(-EINVAL);
1582 if (!genpd_data->domains[idx])
1583 return ERR_PTR(-ENOENT);
1585 return genpd_data->domains[idx];
1587 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1590 * __of_genpd_add_provider() - Register a PM domain provider for a node
1591 * @np: Device node pointer associated with the PM domain provider.
1592 * @xlate: Callback for decoding PM domain from phandle arguments.
1593 * @data: Context pointer for @xlate callback.
1595 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1598 struct of_genpd_provider *cp;
1600 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1604 cp->node = of_node_get(np);
1608 mutex_lock(&of_genpd_mutex);
1609 list_add(&cp->link, &of_genpd_providers);
1610 mutex_unlock(&of_genpd_mutex);
1611 pr_debug("Added domain provider from %s\n", np->full_name);
1615 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1618 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1619 * @np: Device node pointer associated with the PM domain provider
1621 void of_genpd_del_provider(struct device_node *np)
1623 struct of_genpd_provider *cp;
1625 mutex_lock(&of_genpd_mutex);
1626 list_for_each_entry(cp, &of_genpd_providers, link) {
1627 if (cp->node == np) {
1628 list_del(&cp->link);
1629 of_node_put(cp->node);
1634 mutex_unlock(&of_genpd_mutex);
1636 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1639 * of_genpd_get_from_provider() - Look-up PM domain
1640 * @genpdspec: OF phandle args to use for look-up
1642 * Looks for a PM domain provider under the node specified by @genpdspec and if
1643 * found, uses xlate function of the provider to map phandle args to a PM
1646 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1649 struct generic_pm_domain *of_genpd_get_from_provider(
1650 struct of_phandle_args *genpdspec)
1652 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1653 struct of_genpd_provider *provider;
1655 mutex_lock(&of_genpd_mutex);
1657 /* Check if we have such a provider in our array */
1658 list_for_each_entry(provider, &of_genpd_providers, link) {
1659 if (provider->node == genpdspec->np)
1660 genpd = provider->xlate(genpdspec, provider->data);
1665 mutex_unlock(&of_genpd_mutex);
1669 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1672 * genpd_dev_pm_detach - Detach a device from its PM domain.
1673 * @dev: Device to detach.
1674 * @power_off: Currently not used
1676 * Try to locate a corresponding generic PM domain, which the device was
1677 * attached to previously. If such is found, the device is detached from it.
1679 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1681 struct generic_pm_domain *pd;
1685 pd = pm_genpd_lookup_dev(dev);
1689 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1691 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1692 ret = pm_genpd_remove_device(pd, dev);
1701 dev_err(dev, "failed to remove from PM domain %s: %d",
1706 /* Check if PM domain can be powered off after removing this device. */
1707 genpd_queue_power_off_work(pd);
1710 static void genpd_dev_pm_sync(struct device *dev)
1712 struct generic_pm_domain *pd;
1714 pd = dev_to_genpd(dev);
1718 genpd_queue_power_off_work(pd);
1722 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1723 * @dev: Device to attach.
1725 * Parse device's OF node to find a PM domain specifier. If such is found,
1726 * attaches the device to retrieved pm_domain ops.
1728 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1729 * backwards compatibility with existing DTBs.
1731 * Returns 0 on successfully attached PM domain or negative error code. Note
1732 * that if a power-domain exists for the device, but it cannot be found or
1733 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1734 * probed and to re-try again later.
1736 int genpd_dev_pm_attach(struct device *dev)
1738 struct of_phandle_args pd_args;
1739 struct generic_pm_domain *pd;
1749 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1750 "#power-domain-cells", 0, &pd_args);
1756 * Try legacy Samsung-specific bindings
1757 * (for backwards compatibility of DT ABI)
1759 pd_args.args_count = 0;
1760 pd_args.np = of_parse_phandle(dev->of_node,
1761 "samsung,power-domain", 0);
1766 pd = of_genpd_get_from_provider(&pd_args);
1768 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1769 __func__, PTR_ERR(pd));
1770 of_node_put(dev->of_node);
1771 return -EPROBE_DEFER;
1774 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1776 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1777 ret = pm_genpd_add_device(pd, dev);
1786 dev_err(dev, "failed to add to PM domain %s: %d",
1788 of_node_put(dev->of_node);
1792 dev->pm_domain->detach = genpd_dev_pm_detach;
1793 dev->pm_domain->sync = genpd_dev_pm_sync;
1794 ret = pm_genpd_poweron(pd);
1797 return ret ? -EPROBE_DEFER : 0;
1799 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1800 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1803 /*** debugfs support ***/
1805 #ifdef CONFIG_PM_ADVANCED_DEBUG
1806 #include <linux/pm.h>
1807 #include <linux/device.h>
1808 #include <linux/debugfs.h>
1809 #include <linux/seq_file.h>
1810 #include <linux/init.h>
1811 #include <linux/kobject.h>
1812 static struct dentry *pm_genpd_debugfs_dir;
1815 * TODO: This function is a slightly modified version of rtpm_status_show
1816 * from sysfs.c, so generalize it.
1818 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1820 static const char * const status_lookup[] = {
1821 [RPM_ACTIVE] = "active",
1822 [RPM_RESUMING] = "resuming",
1823 [RPM_SUSPENDED] = "suspended",
1824 [RPM_SUSPENDING] = "suspending"
1828 if (dev->power.runtime_error)
1830 else if (dev->power.disable_depth)
1832 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1833 p = status_lookup[dev->power.runtime_status];
1840 static int pm_genpd_summary_one(struct seq_file *s,
1841 struct generic_pm_domain *genpd)
1843 static const char * const status_lookup[] = {
1844 [GPD_STATE_ACTIVE] = "on",
1845 [GPD_STATE_POWER_OFF] = "off"
1847 struct pm_domain_data *pm_data;
1848 const char *kobj_path;
1849 struct gpd_link *link;
1852 ret = mutex_lock_interruptible(&genpd->lock);
1854 return -ERESTARTSYS;
1856 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1858 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
1861 * Modifications on the list require holding locks on both
1862 * master and slave, so we are safe.
1863 * Also genpd->name is immutable.
1865 list_for_each_entry(link, &genpd->master_links, master_node) {
1866 seq_printf(s, "%s", link->slave->name);
1867 if (!list_is_last(&link->master_node, &genpd->master_links))
1871 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1872 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1873 if (kobj_path == NULL)
1876 seq_printf(s, "\n %-50s ", kobj_path);
1877 rtpm_status_str(s, pm_data->dev);
1883 mutex_unlock(&genpd->lock);
1888 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1890 struct generic_pm_domain *genpd;
1893 seq_puts(s, "domain status slaves\n");
1894 seq_puts(s, " /device runtime status\n");
1895 seq_puts(s, "----------------------------------------------------------------------\n");
1897 ret = mutex_lock_interruptible(&gpd_list_lock);
1899 return -ERESTARTSYS;
1901 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1902 ret = pm_genpd_summary_one(s, genpd);
1906 mutex_unlock(&gpd_list_lock);
1911 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1913 return single_open(file, pm_genpd_summary_show, NULL);
1916 static const struct file_operations pm_genpd_summary_fops = {
1917 .open = pm_genpd_summary_open,
1919 .llseek = seq_lseek,
1920 .release = single_release,
1923 static int __init pm_genpd_debug_init(void)
1927 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1929 if (!pm_genpd_debugfs_dir)
1932 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1933 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1939 late_initcall(pm_genpd_debug_init);
1941 static void __exit pm_genpd_debug_exit(void)
1943 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1945 __exitcall(pm_genpd_debug_exit);
1946 #endif /* CONFIG_PM_ADVANCED_DEBUG */