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 genpd_poweroff().
185 * @genpd: PM domait to power off.
187 * Queue up the execution of 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);
195 static int genpd_poweron(struct generic_pm_domain *genpd);
198 * __genpd_poweron - Restore power to a given PM domain and its masters.
199 * @genpd: PM domain to power up.
201 * Restore power to @genpd and all of its masters so that it is possible to
202 * resume a device belonging to it.
204 static int __genpd_poweron(struct generic_pm_domain *genpd)
206 struct gpd_link *link;
209 if (genpd->status == GPD_STATE_ACTIVE
210 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
214 * The list is guaranteed not to change while the loop below is being
215 * executed, unless one of the masters' .power_on() callbacks fiddles
218 list_for_each_entry(link, &genpd->slave_links, slave_node) {
219 genpd_sd_counter_inc(link->master);
221 ret = genpd_poweron(link->master);
223 genpd_sd_counter_dec(link->master);
228 ret = genpd_power_on(genpd, true);
232 genpd->status = GPD_STATE_ACTIVE;
236 list_for_each_entry_continue_reverse(link,
239 genpd_sd_counter_dec(link->master);
240 genpd_queue_power_off_work(link->master);
247 * genpd_poweron - Restore power to a given PM domain and its masters.
248 * @genpd: PM domain to power up.
250 static int genpd_poweron(struct generic_pm_domain *genpd)
254 mutex_lock(&genpd->lock);
255 ret = __genpd_poweron(genpd);
256 mutex_unlock(&genpd->lock);
260 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
262 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
263 save_state_latency_ns, "state save");
266 static int genpd_restore_dev(struct generic_pm_domain *genpd,
267 struct device *dev, bool timed)
270 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
272 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
273 restore_state_latency_ns,
277 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
278 unsigned long val, void *ptr)
280 struct generic_pm_domain_data *gpd_data;
283 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
284 dev = gpd_data->base.dev;
287 struct generic_pm_domain *genpd;
288 struct pm_domain_data *pdd;
290 spin_lock_irq(&dev->power.lock);
292 pdd = dev->power.subsys_data ?
293 dev->power.subsys_data->domain_data : NULL;
294 if (pdd && pdd->dev) {
295 to_gpd_data(pdd)->td.constraint_changed = true;
296 genpd = dev_to_genpd(dev);
298 genpd = ERR_PTR(-ENODATA);
301 spin_unlock_irq(&dev->power.lock);
303 if (!IS_ERR(genpd)) {
304 mutex_lock(&genpd->lock);
305 genpd->max_off_time_changed = true;
306 mutex_unlock(&genpd->lock);
310 if (!dev || dev->power.ignore_children)
318 * genpd_poweroff - Remove power from a given PM domain.
319 * @genpd: PM domain to power down.
320 * @is_async: PM domain is powered down from a scheduled work
322 * If all of the @genpd's devices have been suspended and all of its subdomains
323 * have been powered down, remove power from @genpd.
325 static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
327 struct pm_domain_data *pdd;
328 struct gpd_link *link;
329 unsigned int not_suspended = 0;
332 * Do not try to power off the domain in the following situations:
333 * (1) The domain is already in the "power off" state.
334 * (2) System suspend is in progress.
336 if (genpd->status == GPD_STATE_POWER_OFF
337 || genpd->prepared_count > 0)
340 if (atomic_read(&genpd->sd_count) > 0)
343 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
344 enum pm_qos_flags_status stat;
346 stat = dev_pm_qos_flags(pdd->dev,
347 PM_QOS_FLAG_NO_POWER_OFF
348 | PM_QOS_FLAG_REMOTE_WAKEUP);
349 if (stat > PM_QOS_FLAGS_NONE)
352 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
353 || pdd->dev->power.irq_safe))
357 if (not_suspended > 1 || (not_suspended == 1 && is_async))
360 if (genpd->gov && genpd->gov->power_down_ok) {
361 if (!genpd->gov->power_down_ok(&genpd->domain))
365 if (genpd->power_off) {
368 if (atomic_read(&genpd->sd_count) > 0)
372 * If sd_count > 0 at this point, one of the subdomains hasn't
373 * managed to call genpd_poweron() for the master yet after
374 * incrementing it. In that case genpd_poweron() will wait
375 * for us to drop the lock, so we can call .power_off() and let
376 * the genpd_poweron() restore power for us (this shouldn't
377 * happen very often).
379 ret = genpd_power_off(genpd, true);
384 genpd->status = GPD_STATE_POWER_OFF;
386 list_for_each_entry(link, &genpd->slave_links, slave_node) {
387 genpd_sd_counter_dec(link->master);
388 genpd_queue_power_off_work(link->master);
395 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
396 * @work: Work structure used for scheduling the execution of this function.
398 static void genpd_power_off_work_fn(struct work_struct *work)
400 struct generic_pm_domain *genpd;
402 genpd = container_of(work, struct generic_pm_domain, power_off_work);
404 mutex_lock(&genpd->lock);
405 genpd_poweroff(genpd, true);
406 mutex_unlock(&genpd->lock);
410 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
411 * @dev: Device to suspend.
413 * Carry out a runtime suspend of a device under the assumption that its
414 * pm_domain field points to the domain member of an object of type
415 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
417 static int pm_genpd_runtime_suspend(struct device *dev)
419 struct generic_pm_domain *genpd;
420 bool (*stop_ok)(struct device *__dev);
423 dev_dbg(dev, "%s()\n", __func__);
425 genpd = dev_to_genpd(dev);
429 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
430 if (stop_ok && !stop_ok(dev))
433 ret = genpd_save_dev(genpd, dev);
437 ret = genpd_stop_dev(genpd, dev);
439 genpd_restore_dev(genpd, dev, true);
444 * If power.irq_safe is set, this routine will be run with interrupts
445 * off, so it can't use mutexes.
447 if (dev->power.irq_safe)
450 mutex_lock(&genpd->lock);
451 genpd_poweroff(genpd, false);
452 mutex_unlock(&genpd->lock);
458 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
459 * @dev: Device to resume.
461 * Carry out a runtime resume of a device under the assumption that its
462 * pm_domain field points to the domain member of an object of type
463 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
465 static int pm_genpd_runtime_resume(struct device *dev)
467 struct generic_pm_domain *genpd;
471 dev_dbg(dev, "%s()\n", __func__);
473 genpd = dev_to_genpd(dev);
477 /* If power.irq_safe, the PM domain is never powered off. */
478 if (dev->power.irq_safe) {
483 mutex_lock(&genpd->lock);
484 ret = __genpd_poweron(genpd);
485 mutex_unlock(&genpd->lock);
491 genpd_start_dev(genpd, dev, timed);
492 genpd_restore_dev(genpd, dev, timed);
497 static bool pd_ignore_unused;
498 static int __init pd_ignore_unused_setup(char *__unused)
500 pd_ignore_unused = true;
503 __setup("pd_ignore_unused", pd_ignore_unused_setup);
506 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
508 static int __init genpd_poweroff_unused(void)
510 struct generic_pm_domain *genpd;
512 if (pd_ignore_unused) {
513 pr_warn("genpd: Not disabling unused power domains\n");
517 mutex_lock(&gpd_list_lock);
519 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
520 genpd_queue_power_off_work(genpd);
522 mutex_unlock(&gpd_list_lock);
526 late_initcall(genpd_poweroff_unused);
528 #ifdef CONFIG_PM_SLEEP
531 * pm_genpd_present - Check if the given PM domain has been initialized.
532 * @genpd: PM domain to check.
534 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
536 const struct generic_pm_domain *gpd;
538 if (IS_ERR_OR_NULL(genpd))
541 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
548 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
551 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
555 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
556 * @genpd: PM domain to power off, if possible.
557 * @timed: True if latency measurements are allowed.
559 * Check if the given PM domain can be powered off (during system suspend or
560 * hibernation) and do that if so. Also, in that case propagate to its masters.
562 * This function is only called in "noirq" and "syscore" stages of system power
563 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
564 * executed sequentially, so it is guaranteed that it will never run twice in
567 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
570 struct gpd_link *link;
572 if (genpd->status == GPD_STATE_POWER_OFF)
575 if (genpd->suspended_count != genpd->device_count
576 || atomic_read(&genpd->sd_count) > 0)
579 genpd_power_off(genpd, timed);
581 genpd->status = GPD_STATE_POWER_OFF;
583 list_for_each_entry(link, &genpd->slave_links, slave_node) {
584 genpd_sd_counter_dec(link->master);
585 pm_genpd_sync_poweroff(link->master, timed);
590 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
591 * @genpd: PM domain to power on.
592 * @timed: True if latency measurements are allowed.
594 * This function is only called in "noirq" and "syscore" stages of system power
595 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
596 * executed sequentially, so it is guaranteed that it will never run twice in
599 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
602 struct gpd_link *link;
604 if (genpd->status == GPD_STATE_ACTIVE)
607 list_for_each_entry(link, &genpd->slave_links, slave_node) {
608 pm_genpd_sync_poweron(link->master, timed);
609 genpd_sd_counter_inc(link->master);
612 genpd_power_on(genpd, timed);
614 genpd->status = GPD_STATE_ACTIVE;
618 * resume_needed - Check whether to resume a device before system suspend.
619 * @dev: Device to check.
620 * @genpd: PM domain the device belongs to.
622 * There are two cases in which a device that can wake up the system from sleep
623 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
624 * to wake up the system and it has to remain active for this purpose while the
625 * system is in the sleep state and (2) if the device is not enabled to wake up
626 * the system from sleep states and it generally doesn't generate wakeup signals
627 * by itself (those signals are generated on its behalf by other parts of the
628 * system). In the latter case it may be necessary to reconfigure the device's
629 * wakeup settings during system suspend, because it may have been set up to
630 * signal remote wakeup from the system's working state as needed by runtime PM.
631 * Return 'true' in either of the above cases.
633 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
637 if (!device_can_wakeup(dev))
640 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
641 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
645 * pm_genpd_prepare - Start power transition of a device in a PM domain.
646 * @dev: Device to start the transition of.
648 * Start a power transition of a device (during a system-wide power transition)
649 * under the assumption that its pm_domain field points to the domain member of
650 * an object of type struct generic_pm_domain representing a PM domain
651 * consisting of I/O devices.
653 static int pm_genpd_prepare(struct device *dev)
655 struct generic_pm_domain *genpd;
658 dev_dbg(dev, "%s()\n", __func__);
660 genpd = dev_to_genpd(dev);
665 * If a wakeup request is pending for the device, it should be woken up
666 * at this point and a system wakeup event should be reported if it's
667 * set up to wake up the system from sleep states.
669 pm_runtime_get_noresume(dev);
670 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
671 pm_wakeup_event(dev, 0);
673 if (pm_wakeup_pending()) {
678 if (resume_needed(dev, genpd))
679 pm_runtime_resume(dev);
681 mutex_lock(&genpd->lock);
683 if (genpd->prepared_count++ == 0) {
684 genpd->suspended_count = 0;
685 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
688 mutex_unlock(&genpd->lock);
690 if (genpd->suspend_power_off) {
691 pm_runtime_put_noidle(dev);
696 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
697 * so genpd_poweron() will return immediately, but if the device
698 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
699 * to make it operational.
701 pm_runtime_resume(dev);
702 __pm_runtime_disable(dev, false);
704 ret = pm_generic_prepare(dev);
706 mutex_lock(&genpd->lock);
708 if (--genpd->prepared_count == 0)
709 genpd->suspend_power_off = false;
711 mutex_unlock(&genpd->lock);
712 pm_runtime_enable(dev);
720 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
721 * @dev: Device to suspend.
723 * Suspend a device under the assumption that its pm_domain field points to the
724 * domain member of an object of type struct generic_pm_domain representing
725 * a PM domain consisting of I/O devices.
727 static int pm_genpd_suspend(struct device *dev)
729 struct generic_pm_domain *genpd;
731 dev_dbg(dev, "%s()\n", __func__);
733 genpd = dev_to_genpd(dev);
737 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
741 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
742 * @dev: Device to suspend.
744 * Carry out a late suspend of a device under the assumption that its
745 * pm_domain field points to the domain member of an object of type
746 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
748 static int pm_genpd_suspend_late(struct device *dev)
750 struct generic_pm_domain *genpd;
752 dev_dbg(dev, "%s()\n", __func__);
754 genpd = dev_to_genpd(dev);
758 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
762 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
763 * @dev: Device to suspend.
765 * Stop the device and remove power from the domain if all devices in it have
768 static int pm_genpd_suspend_noirq(struct device *dev)
770 struct generic_pm_domain *genpd;
772 dev_dbg(dev, "%s()\n", __func__);
774 genpd = dev_to_genpd(dev);
778 if (genpd->suspend_power_off
779 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
782 genpd_stop_dev(genpd, dev);
785 * Since all of the "noirq" callbacks are executed sequentially, it is
786 * guaranteed that this function will never run twice in parallel for
787 * the same PM domain, so it is not necessary to use locking here.
789 genpd->suspended_count++;
790 pm_genpd_sync_poweroff(genpd, true);
796 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
797 * @dev: Device to resume.
799 * Restore power to the device's PM domain, if necessary, and start the device.
801 static int pm_genpd_resume_noirq(struct device *dev)
803 struct generic_pm_domain *genpd;
805 dev_dbg(dev, "%s()\n", __func__);
807 genpd = dev_to_genpd(dev);
811 if (genpd->suspend_power_off
812 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
816 * Since all of the "noirq" callbacks are executed sequentially, it is
817 * guaranteed that this function will never run twice in parallel for
818 * the same PM domain, so it is not necessary to use locking here.
820 pm_genpd_sync_poweron(genpd, true);
821 genpd->suspended_count--;
823 return genpd_start_dev(genpd, dev, true);
827 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
828 * @dev: Device to resume.
830 * Carry out an early resume of a device under the assumption that its
831 * pm_domain field points to the domain member of an object of type
832 * struct generic_pm_domain representing a power domain consisting of I/O
835 static int pm_genpd_resume_early(struct device *dev)
837 struct generic_pm_domain *genpd;
839 dev_dbg(dev, "%s()\n", __func__);
841 genpd = dev_to_genpd(dev);
845 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
849 * pm_genpd_resume - Resume of device in an I/O PM domain.
850 * @dev: Device to resume.
852 * Resume a device under the assumption that its pm_domain field points to the
853 * domain member of an object of type struct generic_pm_domain representing
854 * a power domain consisting of I/O devices.
856 static int pm_genpd_resume(struct device *dev)
858 struct generic_pm_domain *genpd;
860 dev_dbg(dev, "%s()\n", __func__);
862 genpd = dev_to_genpd(dev);
866 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
870 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
871 * @dev: Device to freeze.
873 * Freeze a device under the assumption that its pm_domain field points to the
874 * domain member of an object of type struct generic_pm_domain representing
875 * a power domain consisting of I/O devices.
877 static int pm_genpd_freeze(struct device *dev)
879 struct generic_pm_domain *genpd;
881 dev_dbg(dev, "%s()\n", __func__);
883 genpd = dev_to_genpd(dev);
887 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
891 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
892 * @dev: Device to freeze.
894 * Carry out a late freeze of a device under the assumption that its
895 * pm_domain field points to the domain member of an object of type
896 * struct generic_pm_domain representing a power domain consisting of I/O
899 static int pm_genpd_freeze_late(struct device *dev)
901 struct generic_pm_domain *genpd;
903 dev_dbg(dev, "%s()\n", __func__);
905 genpd = dev_to_genpd(dev);
909 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
913 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
914 * @dev: Device to freeze.
916 * Carry out a late freeze of a device under the assumption that its
917 * pm_domain field points to the domain member of an object of type
918 * struct generic_pm_domain representing a power domain consisting of I/O
921 static int pm_genpd_freeze_noirq(struct device *dev)
923 struct generic_pm_domain *genpd;
925 dev_dbg(dev, "%s()\n", __func__);
927 genpd = dev_to_genpd(dev);
931 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
935 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
936 * @dev: Device to thaw.
938 * Start the device, unless power has been removed from the domain already
939 * before the system transition.
941 static int pm_genpd_thaw_noirq(struct device *dev)
943 struct generic_pm_domain *genpd;
945 dev_dbg(dev, "%s()\n", __func__);
947 genpd = dev_to_genpd(dev);
951 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev, true);
955 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
956 * @dev: Device to thaw.
958 * Carry out an early thaw of a device under the assumption that its
959 * pm_domain field points to the domain member of an object of type
960 * struct generic_pm_domain representing a power domain consisting of I/O
963 static int pm_genpd_thaw_early(struct device *dev)
965 struct generic_pm_domain *genpd;
967 dev_dbg(dev, "%s()\n", __func__);
969 genpd = dev_to_genpd(dev);
973 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
977 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
978 * @dev: Device to thaw.
980 * Thaw a device under the assumption that its pm_domain field points to the
981 * domain member of an object of type struct generic_pm_domain representing
982 * a power domain consisting of I/O devices.
984 static int pm_genpd_thaw(struct device *dev)
986 struct generic_pm_domain *genpd;
988 dev_dbg(dev, "%s()\n", __func__);
990 genpd = dev_to_genpd(dev);
994 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
998 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
999 * @dev: Device to resume.
1001 * Make sure the domain will be in the same power state as before the
1002 * hibernation the system is resuming from and start the device if necessary.
1004 static int pm_genpd_restore_noirq(struct device *dev)
1006 struct generic_pm_domain *genpd;
1008 dev_dbg(dev, "%s()\n", __func__);
1010 genpd = dev_to_genpd(dev);
1015 * Since all of the "noirq" callbacks are executed sequentially, it is
1016 * guaranteed that this function will never run twice in parallel for
1017 * the same PM domain, so it is not necessary to use locking here.
1019 * At this point suspended_count == 0 means we are being run for the
1020 * first time for the given domain in the present cycle.
1022 if (genpd->suspended_count++ == 0) {
1024 * The boot kernel might put the domain into arbitrary state,
1025 * so make it appear as powered off to pm_genpd_sync_poweron(),
1026 * so that it tries to power it on in case it was really off.
1028 genpd->status = GPD_STATE_POWER_OFF;
1029 if (genpd->suspend_power_off) {
1031 * If the domain was off before the hibernation, make
1032 * sure it will be off going forward.
1034 genpd_power_off(genpd, true);
1040 if (genpd->suspend_power_off)
1043 pm_genpd_sync_poweron(genpd, true);
1045 return genpd_start_dev(genpd, dev, true);
1049 * pm_genpd_complete - Complete power transition of a device in a power domain.
1050 * @dev: Device to complete the transition of.
1052 * Complete a power transition of a device (during a system-wide power
1053 * transition) under the assumption that its pm_domain field points to the
1054 * domain member of an object of type struct generic_pm_domain representing
1055 * a power domain consisting of I/O devices.
1057 static void pm_genpd_complete(struct device *dev)
1059 struct generic_pm_domain *genpd;
1062 dev_dbg(dev, "%s()\n", __func__);
1064 genpd = dev_to_genpd(dev);
1068 mutex_lock(&genpd->lock);
1070 run_complete = !genpd->suspend_power_off;
1071 if (--genpd->prepared_count == 0)
1072 genpd->suspend_power_off = false;
1074 mutex_unlock(&genpd->lock);
1077 pm_generic_complete(dev);
1078 pm_runtime_set_active(dev);
1079 pm_runtime_enable(dev);
1080 pm_request_idle(dev);
1085 * genpd_syscore_switch - Switch power during system core suspend or resume.
1086 * @dev: Device that normally is marked as "always on" to switch power for.
1088 * This routine may only be called during the system core (syscore) suspend or
1089 * resume phase for devices whose "always on" flags are set.
1091 static void genpd_syscore_switch(struct device *dev, bool suspend)
1093 struct generic_pm_domain *genpd;
1095 genpd = dev_to_genpd(dev);
1096 if (!pm_genpd_present(genpd))
1100 genpd->suspended_count++;
1101 pm_genpd_sync_poweroff(genpd, false);
1103 pm_genpd_sync_poweron(genpd, false);
1104 genpd->suspended_count--;
1108 void pm_genpd_syscore_poweroff(struct device *dev)
1110 genpd_syscore_switch(dev, true);
1112 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1114 void pm_genpd_syscore_poweron(struct device *dev)
1116 genpd_syscore_switch(dev, false);
1118 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1120 #else /* !CONFIG_PM_SLEEP */
1122 #define pm_genpd_prepare NULL
1123 #define pm_genpd_suspend NULL
1124 #define pm_genpd_suspend_late NULL
1125 #define pm_genpd_suspend_noirq NULL
1126 #define pm_genpd_resume_early NULL
1127 #define pm_genpd_resume_noirq NULL
1128 #define pm_genpd_resume NULL
1129 #define pm_genpd_freeze NULL
1130 #define pm_genpd_freeze_late NULL
1131 #define pm_genpd_freeze_noirq NULL
1132 #define pm_genpd_thaw_early NULL
1133 #define pm_genpd_thaw_noirq NULL
1134 #define pm_genpd_thaw NULL
1135 #define pm_genpd_restore_noirq NULL
1136 #define pm_genpd_complete NULL
1138 #endif /* CONFIG_PM_SLEEP */
1140 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1141 struct generic_pm_domain *genpd,
1142 struct gpd_timing_data *td)
1144 struct generic_pm_domain_data *gpd_data;
1147 ret = dev_pm_get_subsys_data(dev);
1149 return ERR_PTR(ret);
1151 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1160 gpd_data->base.dev = dev;
1161 gpd_data->td.constraint_changed = true;
1162 gpd_data->td.effective_constraint_ns = -1;
1163 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1165 spin_lock_irq(&dev->power.lock);
1167 if (dev->power.subsys_data->domain_data) {
1172 dev->power.subsys_data->domain_data = &gpd_data->base;
1173 dev->pm_domain = &genpd->domain;
1175 spin_unlock_irq(&dev->power.lock);
1180 spin_unlock_irq(&dev->power.lock);
1183 dev_pm_put_subsys_data(dev);
1184 return ERR_PTR(ret);
1187 static void genpd_free_dev_data(struct device *dev,
1188 struct generic_pm_domain_data *gpd_data)
1190 spin_lock_irq(&dev->power.lock);
1192 dev->pm_domain = NULL;
1193 dev->power.subsys_data->domain_data = NULL;
1195 spin_unlock_irq(&dev->power.lock);
1198 dev_pm_put_subsys_data(dev);
1202 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1203 * @genpd: PM domain to add the device to.
1204 * @dev: Device to be added.
1205 * @td: Set of PM QoS timing parameters to attach to the device.
1207 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1208 struct gpd_timing_data *td)
1210 struct generic_pm_domain_data *gpd_data;
1213 dev_dbg(dev, "%s()\n", __func__);
1215 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1218 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1219 if (IS_ERR(gpd_data))
1220 return PTR_ERR(gpd_data);
1222 mutex_lock(&genpd->lock);
1224 if (genpd->prepared_count > 0) {
1229 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1233 genpd->device_count++;
1234 genpd->max_off_time_changed = true;
1236 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1239 mutex_unlock(&genpd->lock);
1242 genpd_free_dev_data(dev, gpd_data);
1244 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1250 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1251 * @genpd: PM domain to remove the device from.
1252 * @dev: Device to be removed.
1254 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1257 struct generic_pm_domain_data *gpd_data;
1258 struct pm_domain_data *pdd;
1261 dev_dbg(dev, "%s()\n", __func__);
1263 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1266 /* The above validation also means we have existing domain_data. */
1267 pdd = dev->power.subsys_data->domain_data;
1268 gpd_data = to_gpd_data(pdd);
1269 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1271 mutex_lock(&genpd->lock);
1273 if (genpd->prepared_count > 0) {
1278 genpd->device_count--;
1279 genpd->max_off_time_changed = true;
1281 if (genpd->detach_dev)
1282 genpd->detach_dev(genpd, dev);
1284 list_del_init(&pdd->list_node);
1286 mutex_unlock(&genpd->lock);
1288 genpd_free_dev_data(dev, gpd_data);
1293 mutex_unlock(&genpd->lock);
1294 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1300 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1301 * @genpd: Master PM domain to add the subdomain to.
1302 * @subdomain: Subdomain to be added.
1304 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1305 struct generic_pm_domain *subdomain)
1307 struct gpd_link *link;
1310 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1311 || genpd == subdomain)
1314 mutex_lock(&genpd->lock);
1315 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1317 if (genpd->status == GPD_STATE_POWER_OFF
1318 && subdomain->status != GPD_STATE_POWER_OFF) {
1323 list_for_each_entry(link, &genpd->master_links, master_node) {
1324 if (link->slave == subdomain && link->master == genpd) {
1330 link = kzalloc(sizeof(*link), GFP_KERNEL);
1335 link->master = genpd;
1336 list_add_tail(&link->master_node, &genpd->master_links);
1337 link->slave = subdomain;
1338 list_add_tail(&link->slave_node, &subdomain->slave_links);
1339 if (subdomain->status != GPD_STATE_POWER_OFF)
1340 genpd_sd_counter_inc(genpd);
1343 mutex_unlock(&subdomain->lock);
1344 mutex_unlock(&genpd->lock);
1350 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1351 * @genpd: Master PM domain to remove the subdomain from.
1352 * @subdomain: Subdomain to be removed.
1354 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1355 struct generic_pm_domain *subdomain)
1357 struct gpd_link *link;
1360 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1363 mutex_lock(&genpd->lock);
1365 if (!list_empty(&subdomain->slave_links) || subdomain->device_count) {
1366 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1372 list_for_each_entry(link, &genpd->master_links, master_node) {
1373 if (link->slave != subdomain)
1376 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1378 list_del(&link->master_node);
1379 list_del(&link->slave_node);
1381 if (subdomain->status != GPD_STATE_POWER_OFF)
1382 genpd_sd_counter_dec(genpd);
1384 mutex_unlock(&subdomain->lock);
1391 mutex_unlock(&genpd->lock);
1396 /* Default device callbacks for generic PM domains. */
1399 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1400 * @dev: Device to handle.
1402 static int pm_genpd_default_save_state(struct device *dev)
1404 int (*cb)(struct device *__dev);
1406 if (dev->type && dev->type->pm)
1407 cb = dev->type->pm->runtime_suspend;
1408 else if (dev->class && dev->class->pm)
1409 cb = dev->class->pm->runtime_suspend;
1410 else if (dev->bus && dev->bus->pm)
1411 cb = dev->bus->pm->runtime_suspend;
1415 if (!cb && dev->driver && dev->driver->pm)
1416 cb = dev->driver->pm->runtime_suspend;
1418 return cb ? cb(dev) : 0;
1422 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1423 * @dev: Device to handle.
1425 static int pm_genpd_default_restore_state(struct device *dev)
1427 int (*cb)(struct device *__dev);
1429 if (dev->type && dev->type->pm)
1430 cb = dev->type->pm->runtime_resume;
1431 else if (dev->class && dev->class->pm)
1432 cb = dev->class->pm->runtime_resume;
1433 else if (dev->bus && dev->bus->pm)
1434 cb = dev->bus->pm->runtime_resume;
1438 if (!cb && dev->driver && dev->driver->pm)
1439 cb = dev->driver->pm->runtime_resume;
1441 return cb ? cb(dev) : 0;
1445 * pm_genpd_init - Initialize a generic I/O PM domain object.
1446 * @genpd: PM domain object to initialize.
1447 * @gov: PM domain governor to associate with the domain (may be NULL).
1448 * @is_off: Initial value of the domain's power_is_off field.
1450 void pm_genpd_init(struct generic_pm_domain *genpd,
1451 struct dev_power_governor *gov, bool is_off)
1453 if (IS_ERR_OR_NULL(genpd))
1456 INIT_LIST_HEAD(&genpd->master_links);
1457 INIT_LIST_HEAD(&genpd->slave_links);
1458 INIT_LIST_HEAD(&genpd->dev_list);
1459 mutex_init(&genpd->lock);
1461 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1462 atomic_set(&genpd->sd_count, 0);
1463 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1464 genpd->device_count = 0;
1465 genpd->max_off_time_ns = -1;
1466 genpd->max_off_time_changed = true;
1467 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1468 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1469 genpd->domain.ops.prepare = pm_genpd_prepare;
1470 genpd->domain.ops.suspend = pm_genpd_suspend;
1471 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1472 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1473 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1474 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1475 genpd->domain.ops.resume = pm_genpd_resume;
1476 genpd->domain.ops.freeze = pm_genpd_freeze;
1477 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1478 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1479 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1480 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1481 genpd->domain.ops.thaw = pm_genpd_thaw;
1482 genpd->domain.ops.poweroff = pm_genpd_suspend;
1483 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1484 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1485 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1486 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1487 genpd->domain.ops.restore = pm_genpd_resume;
1488 genpd->domain.ops.complete = pm_genpd_complete;
1489 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1490 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1492 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1493 genpd->dev_ops.stop = pm_clk_suspend;
1494 genpd->dev_ops.start = pm_clk_resume;
1497 mutex_lock(&gpd_list_lock);
1498 list_add(&genpd->gpd_list_node, &gpd_list);
1499 mutex_unlock(&gpd_list_lock);
1501 EXPORT_SYMBOL_GPL(pm_genpd_init);
1503 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1505 * Device Tree based PM domain providers.
1507 * The code below implements generic device tree based PM domain providers that
1508 * bind device tree nodes with generic PM domains registered in the system.
1510 * Any driver that registers generic PM domains and needs to support binding of
1511 * devices to these domains is supposed to register a PM domain provider, which
1512 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1514 * Two simple mapping functions have been provided for convenience:
1515 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1516 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1521 * struct of_genpd_provider - PM domain provider registration structure
1522 * @link: Entry in global list of PM domain providers
1523 * @node: Pointer to device tree node of PM domain provider
1524 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1526 * @data: context pointer to be passed into @xlate callback
1528 struct of_genpd_provider {
1529 struct list_head link;
1530 struct device_node *node;
1531 genpd_xlate_t xlate;
1535 /* List of registered PM domain providers. */
1536 static LIST_HEAD(of_genpd_providers);
1537 /* Mutex to protect the list above. */
1538 static DEFINE_MUTEX(of_genpd_mutex);
1541 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1542 * @genpdspec: OF phandle args to map into a PM domain
1543 * @data: xlate function private data - pointer to struct generic_pm_domain
1545 * This is a generic xlate function that can be used to model PM domains that
1546 * have their own device tree nodes. The private data of xlate function needs
1547 * to be a valid pointer to struct generic_pm_domain.
1549 struct generic_pm_domain *__of_genpd_xlate_simple(
1550 struct of_phandle_args *genpdspec,
1553 if (genpdspec->args_count != 0)
1554 return ERR_PTR(-EINVAL);
1557 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1560 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1561 * @genpdspec: OF phandle args to map into a PM domain
1562 * @data: xlate function private data - pointer to struct genpd_onecell_data
1564 * This is a generic xlate function that can be used to model simple PM domain
1565 * controllers that have one device tree node and provide multiple PM domains.
1566 * A single cell is used as an index into an array of PM domains specified in
1567 * the genpd_onecell_data struct when registering the provider.
1569 struct generic_pm_domain *__of_genpd_xlate_onecell(
1570 struct of_phandle_args *genpdspec,
1573 struct genpd_onecell_data *genpd_data = data;
1574 unsigned int idx = genpdspec->args[0];
1576 if (genpdspec->args_count != 1)
1577 return ERR_PTR(-EINVAL);
1579 if (idx >= genpd_data->num_domains) {
1580 pr_err("%s: invalid domain index %u\n", __func__, idx);
1581 return ERR_PTR(-EINVAL);
1584 if (!genpd_data->domains[idx])
1585 return ERR_PTR(-ENOENT);
1587 return genpd_data->domains[idx];
1589 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1592 * __of_genpd_add_provider() - Register a PM domain provider for a node
1593 * @np: Device node pointer associated with the PM domain provider.
1594 * @xlate: Callback for decoding PM domain from phandle arguments.
1595 * @data: Context pointer for @xlate callback.
1597 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1600 struct of_genpd_provider *cp;
1602 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1606 cp->node = of_node_get(np);
1610 mutex_lock(&of_genpd_mutex);
1611 list_add(&cp->link, &of_genpd_providers);
1612 mutex_unlock(&of_genpd_mutex);
1613 pr_debug("Added domain provider from %s\n", np->full_name);
1617 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1620 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1621 * @np: Device node pointer associated with the PM domain provider
1623 void of_genpd_del_provider(struct device_node *np)
1625 struct of_genpd_provider *cp;
1627 mutex_lock(&of_genpd_mutex);
1628 list_for_each_entry(cp, &of_genpd_providers, link) {
1629 if (cp->node == np) {
1630 list_del(&cp->link);
1631 of_node_put(cp->node);
1636 mutex_unlock(&of_genpd_mutex);
1638 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1641 * of_genpd_get_from_provider() - Look-up PM domain
1642 * @genpdspec: OF phandle args to use for look-up
1644 * Looks for a PM domain provider under the node specified by @genpdspec and if
1645 * found, uses xlate function of the provider to map phandle args to a PM
1648 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1651 struct generic_pm_domain *of_genpd_get_from_provider(
1652 struct of_phandle_args *genpdspec)
1654 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1655 struct of_genpd_provider *provider;
1657 mutex_lock(&of_genpd_mutex);
1659 /* Check if we have such a provider in our array */
1660 list_for_each_entry(provider, &of_genpd_providers, link) {
1661 if (provider->node == genpdspec->np)
1662 genpd = provider->xlate(genpdspec, provider->data);
1667 mutex_unlock(&of_genpd_mutex);
1671 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1674 * genpd_dev_pm_detach - Detach a device from its PM domain.
1675 * @dev: Device to detach.
1676 * @power_off: Currently not used
1678 * Try to locate a corresponding generic PM domain, which the device was
1679 * attached to previously. If such is found, the device is detached from it.
1681 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1683 struct generic_pm_domain *pd;
1687 pd = pm_genpd_lookup_dev(dev);
1691 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1693 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1694 ret = pm_genpd_remove_device(pd, dev);
1703 dev_err(dev, "failed to remove from PM domain %s: %d",
1708 /* Check if PM domain can be powered off after removing this device. */
1709 genpd_queue_power_off_work(pd);
1712 static void genpd_dev_pm_sync(struct device *dev)
1714 struct generic_pm_domain *pd;
1716 pd = dev_to_genpd(dev);
1720 genpd_queue_power_off_work(pd);
1724 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1725 * @dev: Device to attach.
1727 * Parse device's OF node to find a PM domain specifier. If such is found,
1728 * attaches the device to retrieved pm_domain ops.
1730 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1731 * backwards compatibility with existing DTBs.
1733 * Returns 0 on successfully attached PM domain or negative error code. Note
1734 * that if a power-domain exists for the device, but it cannot be found or
1735 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1736 * probed and to re-try again later.
1738 int genpd_dev_pm_attach(struct device *dev)
1740 struct of_phandle_args pd_args;
1741 struct generic_pm_domain *pd;
1751 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1752 "#power-domain-cells", 0, &pd_args);
1758 * Try legacy Samsung-specific bindings
1759 * (for backwards compatibility of DT ABI)
1761 pd_args.args_count = 0;
1762 pd_args.np = of_parse_phandle(dev->of_node,
1763 "samsung,power-domain", 0);
1768 pd = of_genpd_get_from_provider(&pd_args);
1770 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1771 __func__, PTR_ERR(pd));
1772 of_node_put(dev->of_node);
1773 return -EPROBE_DEFER;
1776 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1778 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1779 ret = pm_genpd_add_device(pd, dev);
1788 dev_err(dev, "failed to add to PM domain %s: %d",
1790 of_node_put(dev->of_node);
1794 dev->pm_domain->detach = genpd_dev_pm_detach;
1795 dev->pm_domain->sync = genpd_dev_pm_sync;
1796 ret = genpd_poweron(pd);
1799 return ret ? -EPROBE_DEFER : 0;
1801 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1802 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1805 /*** debugfs support ***/
1807 #ifdef CONFIG_PM_ADVANCED_DEBUG
1808 #include <linux/pm.h>
1809 #include <linux/device.h>
1810 #include <linux/debugfs.h>
1811 #include <linux/seq_file.h>
1812 #include <linux/init.h>
1813 #include <linux/kobject.h>
1814 static struct dentry *pm_genpd_debugfs_dir;
1817 * TODO: This function is a slightly modified version of rtpm_status_show
1818 * from sysfs.c, so generalize it.
1820 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1822 static const char * const status_lookup[] = {
1823 [RPM_ACTIVE] = "active",
1824 [RPM_RESUMING] = "resuming",
1825 [RPM_SUSPENDED] = "suspended",
1826 [RPM_SUSPENDING] = "suspending"
1830 if (dev->power.runtime_error)
1832 else if (dev->power.disable_depth)
1834 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1835 p = status_lookup[dev->power.runtime_status];
1842 static int pm_genpd_summary_one(struct seq_file *s,
1843 struct generic_pm_domain *genpd)
1845 static const char * const status_lookup[] = {
1846 [GPD_STATE_ACTIVE] = "on",
1847 [GPD_STATE_POWER_OFF] = "off"
1849 struct pm_domain_data *pm_data;
1850 const char *kobj_path;
1851 struct gpd_link *link;
1854 ret = mutex_lock_interruptible(&genpd->lock);
1856 return -ERESTARTSYS;
1858 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1860 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
1863 * Modifications on the list require holding locks on both
1864 * master and slave, so we are safe.
1865 * Also genpd->name is immutable.
1867 list_for_each_entry(link, &genpd->master_links, master_node) {
1868 seq_printf(s, "%s", link->slave->name);
1869 if (!list_is_last(&link->master_node, &genpd->master_links))
1873 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1874 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1875 if (kobj_path == NULL)
1878 seq_printf(s, "\n %-50s ", kobj_path);
1879 rtpm_status_str(s, pm_data->dev);
1885 mutex_unlock(&genpd->lock);
1890 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1892 struct generic_pm_domain *genpd;
1895 seq_puts(s, "domain status slaves\n");
1896 seq_puts(s, " /device runtime status\n");
1897 seq_puts(s, "----------------------------------------------------------------------\n");
1899 ret = mutex_lock_interruptible(&gpd_list_lock);
1901 return -ERESTARTSYS;
1903 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1904 ret = pm_genpd_summary_one(s, genpd);
1908 mutex_unlock(&gpd_list_lock);
1913 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1915 return single_open(file, pm_genpd_summary_show, NULL);
1918 static const struct file_operations pm_genpd_summary_fops = {
1919 .open = pm_genpd_summary_open,
1921 .llseek = seq_lseek,
1922 .release = single_release,
1925 static int __init pm_genpd_debug_init(void)
1929 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1931 if (!pm_genpd_debugfs_dir)
1934 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1935 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1941 late_initcall(pm_genpd_debug_init);
1943 static void __exit pm_genpd_debug_exit(void)
1945 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1947 __exitcall(pm_genpd_debug_exit);
1948 #endif /* CONFIG_PM_ADVANCED_DEBUG */