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>
25 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
27 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
32 __routine = genpd->dev_ops.callback; \
34 __ret = __routine(dev); \
39 static LIST_HEAD(gpd_list);
40 static DEFINE_MUTEX(gpd_list_lock);
43 * Get the generic PM domain for a particular struct device.
44 * This validates the struct device pointer, the PM domain pointer,
45 * and checks that the PM domain pointer is a real generic PM domain.
46 * Any failure results in NULL being returned.
48 struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
50 struct generic_pm_domain *genpd = NULL, *gpd;
52 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
55 mutex_lock(&gpd_list_lock);
56 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
57 if (&gpd->domain == dev->pm_domain) {
62 mutex_unlock(&gpd_list_lock);
68 * This should only be used where we are certain that the pm_domain
69 * attached to the device is a genpd domain.
71 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
73 if (IS_ERR_OR_NULL(dev->pm_domain))
74 return ERR_PTR(-EINVAL);
76 return pd_to_genpd(dev->pm_domain);
79 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
81 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
84 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
86 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
89 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
93 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
94 ret = !!atomic_dec_and_test(&genpd->sd_count);
99 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
101 atomic_inc(&genpd->sd_count);
102 smp_mb__after_atomic();
105 static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
111 if (!genpd->power_on)
115 return genpd->power_on(genpd);
117 time_start = ktime_get();
118 ret = genpd->power_on(genpd);
122 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
123 if (elapsed_ns <= genpd->power_on_latency_ns)
126 genpd->power_on_latency_ns = elapsed_ns;
127 genpd->max_off_time_changed = true;
128 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
129 genpd->name, "on", elapsed_ns);
134 static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
140 if (!genpd->power_off)
144 return genpd->power_off(genpd);
146 time_start = ktime_get();
147 ret = genpd->power_off(genpd);
151 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
152 if (elapsed_ns <= genpd->power_off_latency_ns)
155 genpd->power_off_latency_ns = elapsed_ns;
156 genpd->max_off_time_changed = true;
157 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
158 genpd->name, "off", elapsed_ns);
164 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
165 * @genpd: PM domain to power off.
167 * Queue up the execution of genpd_poweroff() unless it's already been done
170 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
172 queue_work(pm_wq, &genpd->power_off_work);
176 * genpd_poweron - Restore power to a given PM domain and its masters.
177 * @genpd: PM domain to power up.
178 * @depth: nesting count for lockdep.
180 * Restore power to @genpd and all of its masters so that it is possible to
181 * resume a device belonging to it.
183 static int genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
185 struct gpd_link *link;
188 if (genpd->status == GPD_STATE_ACTIVE
189 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
193 * The list is guaranteed not to change while the loop below is being
194 * executed, unless one of the masters' .power_on() callbacks fiddles
197 list_for_each_entry(link, &genpd->slave_links, slave_node) {
198 struct generic_pm_domain *master = link->master;
200 genpd_sd_counter_inc(master);
202 mutex_lock_nested(&master->lock, depth + 1);
203 ret = genpd_poweron(master, depth + 1);
204 mutex_unlock(&master->lock);
207 genpd_sd_counter_dec(master);
212 ret = genpd_power_on(genpd, true);
216 genpd->status = GPD_STATE_ACTIVE;
220 list_for_each_entry_continue_reverse(link,
223 genpd_sd_counter_dec(link->master);
224 genpd_queue_power_off_work(link->master);
230 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
232 return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
235 static int genpd_restore_dev(struct generic_pm_domain *genpd,
238 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
241 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
242 unsigned long val, void *ptr)
244 struct generic_pm_domain_data *gpd_data;
247 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
248 dev = gpd_data->base.dev;
251 struct generic_pm_domain *genpd;
252 struct pm_domain_data *pdd;
254 spin_lock_irq(&dev->power.lock);
256 pdd = dev->power.subsys_data ?
257 dev->power.subsys_data->domain_data : NULL;
258 if (pdd && pdd->dev) {
259 to_gpd_data(pdd)->td.constraint_changed = true;
260 genpd = dev_to_genpd(dev);
262 genpd = ERR_PTR(-ENODATA);
265 spin_unlock_irq(&dev->power.lock);
267 if (!IS_ERR(genpd)) {
268 mutex_lock(&genpd->lock);
269 genpd->max_off_time_changed = true;
270 mutex_unlock(&genpd->lock);
274 if (!dev || dev->power.ignore_children)
282 * genpd_poweroff - Remove power from a given PM domain.
283 * @genpd: PM domain to power down.
284 * @is_async: PM domain is powered down from a scheduled work
286 * If all of the @genpd's devices have been suspended and all of its subdomains
287 * have been powered down, remove power from @genpd.
289 static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
291 struct pm_domain_data *pdd;
292 struct gpd_link *link;
293 unsigned int not_suspended = 0;
296 * Do not try to power off the domain in the following situations:
297 * (1) The domain is already in the "power off" state.
298 * (2) System suspend is in progress.
300 if (genpd->status == GPD_STATE_POWER_OFF
301 || genpd->prepared_count > 0)
304 if (atomic_read(&genpd->sd_count) > 0)
307 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
308 enum pm_qos_flags_status stat;
310 stat = dev_pm_qos_flags(pdd->dev,
311 PM_QOS_FLAG_NO_POWER_OFF
312 | PM_QOS_FLAG_REMOTE_WAKEUP);
313 if (stat > PM_QOS_FLAGS_NONE)
316 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
320 if (not_suspended > 1 || (not_suspended == 1 && is_async))
323 if (genpd->gov && genpd->gov->power_down_ok) {
324 if (!genpd->gov->power_down_ok(&genpd->domain))
328 if (genpd->power_off) {
331 if (atomic_read(&genpd->sd_count) > 0)
335 * If sd_count > 0 at this point, one of the subdomains hasn't
336 * managed to call genpd_poweron() for the master yet after
337 * incrementing it. In that case genpd_poweron() will wait
338 * for us to drop the lock, so we can call .power_off() and let
339 * the genpd_poweron() restore power for us (this shouldn't
340 * happen very often).
342 ret = genpd_power_off(genpd, true);
347 genpd->status = GPD_STATE_POWER_OFF;
349 list_for_each_entry(link, &genpd->slave_links, slave_node) {
350 genpd_sd_counter_dec(link->master);
351 genpd_queue_power_off_work(link->master);
358 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
359 * @work: Work structure used for scheduling the execution of this function.
361 static void genpd_power_off_work_fn(struct work_struct *work)
363 struct generic_pm_domain *genpd;
365 genpd = container_of(work, struct generic_pm_domain, power_off_work);
367 mutex_lock(&genpd->lock);
368 genpd_poweroff(genpd, true);
369 mutex_unlock(&genpd->lock);
373 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
374 * @dev: Device to suspend.
376 * Carry out a runtime suspend of a device under the assumption that its
377 * pm_domain field points to the domain member of an object of type
378 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
380 static int pm_genpd_runtime_suspend(struct device *dev)
382 struct generic_pm_domain *genpd;
383 bool (*stop_ok)(struct device *__dev);
384 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
385 bool runtime_pm = pm_runtime_enabled(dev);
390 dev_dbg(dev, "%s()\n", __func__);
392 genpd = dev_to_genpd(dev);
397 * A runtime PM centric subsystem/driver may re-use the runtime PM
398 * callbacks for other purposes than runtime PM. In those scenarios
399 * runtime PM is disabled. Under these circumstances, we shall skip
400 * validating/measuring the PM QoS latency.
402 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
403 if (runtime_pm && stop_ok && !stop_ok(dev))
406 /* Measure suspend latency. */
408 time_start = ktime_get();
410 ret = genpd_save_dev(genpd, dev);
414 ret = genpd_stop_dev(genpd, dev);
416 genpd_restore_dev(genpd, dev);
420 /* Update suspend latency value if the measured time exceeds it. */
422 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
423 if (elapsed_ns > td->suspend_latency_ns) {
424 td->suspend_latency_ns = elapsed_ns;
425 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
427 genpd->max_off_time_changed = true;
428 td->constraint_changed = true;
433 * If power.irq_safe is set, this routine will be run with interrupts
434 * off, so it can't use mutexes.
436 if (dev->power.irq_safe)
439 mutex_lock(&genpd->lock);
440 genpd_poweroff(genpd, false);
441 mutex_unlock(&genpd->lock);
447 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
448 * @dev: Device to resume.
450 * Carry out a runtime resume of a device under the assumption that its
451 * pm_domain field points to the domain member of an object of type
452 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
454 static int pm_genpd_runtime_resume(struct device *dev)
456 struct generic_pm_domain *genpd;
457 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
458 bool runtime_pm = pm_runtime_enabled(dev);
464 dev_dbg(dev, "%s()\n", __func__);
466 genpd = dev_to_genpd(dev);
470 /* If power.irq_safe, the PM domain is never powered off. */
471 if (dev->power.irq_safe) {
476 mutex_lock(&genpd->lock);
477 ret = genpd_poweron(genpd, 0);
478 mutex_unlock(&genpd->lock);
484 /* Measure resume latency. */
485 if (timed && runtime_pm)
486 time_start = ktime_get();
488 genpd_start_dev(genpd, dev);
489 genpd_restore_dev(genpd, dev);
491 /* Update resume latency value if the measured time exceeds it. */
492 if (timed && runtime_pm) {
493 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
494 if (elapsed_ns > td->resume_latency_ns) {
495 td->resume_latency_ns = elapsed_ns;
496 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
498 genpd->max_off_time_changed = true;
499 td->constraint_changed = true;
506 static bool pd_ignore_unused;
507 static int __init pd_ignore_unused_setup(char *__unused)
509 pd_ignore_unused = true;
512 __setup("pd_ignore_unused", pd_ignore_unused_setup);
515 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
517 static int __init genpd_poweroff_unused(void)
519 struct generic_pm_domain *genpd;
521 if (pd_ignore_unused) {
522 pr_warn("genpd: Not disabling unused power domains\n");
526 mutex_lock(&gpd_list_lock);
528 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
529 genpd_queue_power_off_work(genpd);
531 mutex_unlock(&gpd_list_lock);
535 late_initcall(genpd_poweroff_unused);
537 #ifdef CONFIG_PM_SLEEP
540 * pm_genpd_present - Check if the given PM domain has been initialized.
541 * @genpd: PM domain to check.
543 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
545 const struct generic_pm_domain *gpd;
547 if (IS_ERR_OR_NULL(genpd))
550 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
557 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
560 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
564 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
565 * @genpd: PM domain to power off, if possible.
566 * @timed: True if latency measurements are allowed.
568 * Check if the given PM domain can be powered off (during system suspend or
569 * hibernation) and do that if so. Also, in that case propagate to its masters.
571 * This function is only called in "noirq" and "syscore" stages of system power
572 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
573 * executed sequentially, so it is guaranteed that it will never run twice in
576 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
579 struct gpd_link *link;
581 if (genpd->status == GPD_STATE_POWER_OFF)
584 if (genpd->suspended_count != genpd->device_count
585 || atomic_read(&genpd->sd_count) > 0)
588 genpd_power_off(genpd, timed);
590 genpd->status = GPD_STATE_POWER_OFF;
592 list_for_each_entry(link, &genpd->slave_links, slave_node) {
593 genpd_sd_counter_dec(link->master);
594 pm_genpd_sync_poweroff(link->master, timed);
599 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
600 * @genpd: PM domain to power on.
601 * @timed: True if latency measurements are allowed.
603 * This function is only called in "noirq" and "syscore" stages of system power
604 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
605 * executed sequentially, so it is guaranteed that it will never run twice in
608 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
611 struct gpd_link *link;
613 if (genpd->status == GPD_STATE_ACTIVE)
616 list_for_each_entry(link, &genpd->slave_links, slave_node) {
617 pm_genpd_sync_poweron(link->master, timed);
618 genpd_sd_counter_inc(link->master);
621 genpd_power_on(genpd, timed);
623 genpd->status = GPD_STATE_ACTIVE;
627 * resume_needed - Check whether to resume a device before system suspend.
628 * @dev: Device to check.
629 * @genpd: PM domain the device belongs to.
631 * There are two cases in which a device that can wake up the system from sleep
632 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
633 * to wake up the system and it has to remain active for this purpose while the
634 * system is in the sleep state and (2) if the device is not enabled to wake up
635 * the system from sleep states and it generally doesn't generate wakeup signals
636 * by itself (those signals are generated on its behalf by other parts of the
637 * system). In the latter case it may be necessary to reconfigure the device's
638 * wakeup settings during system suspend, because it may have been set up to
639 * signal remote wakeup from the system's working state as needed by runtime PM.
640 * Return 'true' in either of the above cases.
642 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
646 if (!device_can_wakeup(dev))
649 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
650 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
654 * pm_genpd_prepare - Start power transition of a device in a PM domain.
655 * @dev: Device to start the transition of.
657 * Start a power transition of a device (during a system-wide power transition)
658 * under the assumption that its pm_domain field points to the domain member of
659 * an object of type struct generic_pm_domain representing a PM domain
660 * consisting of I/O devices.
662 static int pm_genpd_prepare(struct device *dev)
664 struct generic_pm_domain *genpd;
667 dev_dbg(dev, "%s()\n", __func__);
669 genpd = dev_to_genpd(dev);
674 * If a wakeup request is pending for the device, it should be woken up
675 * at this point and a system wakeup event should be reported if it's
676 * set up to wake up the system from sleep states.
678 pm_runtime_get_noresume(dev);
679 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
680 pm_wakeup_event(dev, 0);
682 if (pm_wakeup_pending()) {
687 if (resume_needed(dev, genpd))
688 pm_runtime_resume(dev);
690 mutex_lock(&genpd->lock);
692 if (genpd->prepared_count++ == 0) {
693 genpd->suspended_count = 0;
694 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
697 mutex_unlock(&genpd->lock);
699 if (genpd->suspend_power_off) {
700 pm_runtime_put_noidle(dev);
705 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
706 * so genpd_poweron() will return immediately, but if the device
707 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
708 * to make it operational.
710 pm_runtime_resume(dev);
711 __pm_runtime_disable(dev, false);
713 ret = pm_generic_prepare(dev);
715 mutex_lock(&genpd->lock);
717 if (--genpd->prepared_count == 0)
718 genpd->suspend_power_off = false;
720 mutex_unlock(&genpd->lock);
721 pm_runtime_enable(dev);
729 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
730 * @dev: Device to suspend.
732 * Suspend a device under the assumption that its pm_domain field points to the
733 * domain member of an object of type struct generic_pm_domain representing
734 * a PM domain consisting of I/O devices.
736 static int pm_genpd_suspend(struct device *dev)
738 struct generic_pm_domain *genpd;
740 dev_dbg(dev, "%s()\n", __func__);
742 genpd = dev_to_genpd(dev);
746 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
750 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
751 * @dev: Device to suspend.
753 * Carry out a late suspend of a device under the assumption that its
754 * pm_domain field points to the domain member of an object of type
755 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
757 static int pm_genpd_suspend_late(struct device *dev)
759 struct generic_pm_domain *genpd;
761 dev_dbg(dev, "%s()\n", __func__);
763 genpd = dev_to_genpd(dev);
767 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
771 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
772 * @dev: Device to suspend.
774 * Stop the device and remove power from the domain if all devices in it have
777 static int pm_genpd_suspend_noirq(struct device *dev)
779 struct generic_pm_domain *genpd;
781 dev_dbg(dev, "%s()\n", __func__);
783 genpd = dev_to_genpd(dev);
787 if (genpd->suspend_power_off
788 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
791 genpd_stop_dev(genpd, dev);
794 * Since all of the "noirq" callbacks are executed sequentially, it is
795 * guaranteed that this function will never run twice in parallel for
796 * the same PM domain, so it is not necessary to use locking here.
798 genpd->suspended_count++;
799 pm_genpd_sync_poweroff(genpd, true);
805 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
806 * @dev: Device to resume.
808 * Restore power to the device's PM domain, if necessary, and start the device.
810 static int pm_genpd_resume_noirq(struct device *dev)
812 struct generic_pm_domain *genpd;
814 dev_dbg(dev, "%s()\n", __func__);
816 genpd = dev_to_genpd(dev);
820 if (genpd->suspend_power_off
821 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
825 * Since all of the "noirq" callbacks are executed sequentially, it is
826 * guaranteed that this function will never run twice in parallel for
827 * the same PM domain, so it is not necessary to use locking here.
829 pm_genpd_sync_poweron(genpd, true);
830 genpd->suspended_count--;
832 return genpd_start_dev(genpd, dev);
836 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
837 * @dev: Device to resume.
839 * Carry out an early resume of a device under the assumption that its
840 * pm_domain field points to the domain member of an object of type
841 * struct generic_pm_domain representing a power domain consisting of I/O
844 static int pm_genpd_resume_early(struct device *dev)
846 struct generic_pm_domain *genpd;
848 dev_dbg(dev, "%s()\n", __func__);
850 genpd = dev_to_genpd(dev);
854 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
858 * pm_genpd_resume - Resume of device in an I/O PM domain.
859 * @dev: Device to resume.
861 * Resume a device under the assumption that its pm_domain field points to the
862 * domain member of an object of type struct generic_pm_domain representing
863 * a power domain consisting of I/O devices.
865 static int pm_genpd_resume(struct device *dev)
867 struct generic_pm_domain *genpd;
869 dev_dbg(dev, "%s()\n", __func__);
871 genpd = dev_to_genpd(dev);
875 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
879 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
880 * @dev: Device to freeze.
882 * Freeze a device under the assumption that its pm_domain field points to the
883 * domain member of an object of type struct generic_pm_domain representing
884 * a power domain consisting of I/O devices.
886 static int pm_genpd_freeze(struct device *dev)
888 struct generic_pm_domain *genpd;
890 dev_dbg(dev, "%s()\n", __func__);
892 genpd = dev_to_genpd(dev);
896 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
900 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
901 * @dev: Device to freeze.
903 * Carry out a late freeze of a device under the assumption that its
904 * pm_domain field points to the domain member of an object of type
905 * struct generic_pm_domain representing a power domain consisting of I/O
908 static int pm_genpd_freeze_late(struct device *dev)
910 struct generic_pm_domain *genpd;
912 dev_dbg(dev, "%s()\n", __func__);
914 genpd = dev_to_genpd(dev);
918 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
922 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
923 * @dev: Device to freeze.
925 * Carry out a late freeze of a device under the assumption that its
926 * pm_domain field points to the domain member of an object of type
927 * struct generic_pm_domain representing a power domain consisting of I/O
930 static int pm_genpd_freeze_noirq(struct device *dev)
932 struct generic_pm_domain *genpd;
934 dev_dbg(dev, "%s()\n", __func__);
936 genpd = dev_to_genpd(dev);
940 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
944 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
945 * @dev: Device to thaw.
947 * Start the device, unless power has been removed from the domain already
948 * before the system transition.
950 static int pm_genpd_thaw_noirq(struct device *dev)
952 struct generic_pm_domain *genpd;
954 dev_dbg(dev, "%s()\n", __func__);
956 genpd = dev_to_genpd(dev);
960 return genpd->suspend_power_off ?
961 0 : genpd_start_dev(genpd, dev);
965 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
966 * @dev: Device to thaw.
968 * Carry out an early thaw of a device under the assumption that its
969 * pm_domain field points to the domain member of an object of type
970 * struct generic_pm_domain representing a power domain consisting of I/O
973 static int pm_genpd_thaw_early(struct device *dev)
975 struct generic_pm_domain *genpd;
977 dev_dbg(dev, "%s()\n", __func__);
979 genpd = dev_to_genpd(dev);
983 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
987 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
988 * @dev: Device to thaw.
990 * Thaw a device under the assumption that its pm_domain field points to the
991 * domain member of an object of type struct generic_pm_domain representing
992 * a power domain consisting of I/O devices.
994 static int pm_genpd_thaw(struct device *dev)
996 struct generic_pm_domain *genpd;
998 dev_dbg(dev, "%s()\n", __func__);
1000 genpd = dev_to_genpd(dev);
1004 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1008 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1009 * @dev: Device to resume.
1011 * Make sure the domain will be in the same power state as before the
1012 * hibernation the system is resuming from and start the device if necessary.
1014 static int pm_genpd_restore_noirq(struct device *dev)
1016 struct generic_pm_domain *genpd;
1018 dev_dbg(dev, "%s()\n", __func__);
1020 genpd = dev_to_genpd(dev);
1025 * Since all of the "noirq" callbacks are executed sequentially, it is
1026 * guaranteed that this function will never run twice in parallel for
1027 * the same PM domain, so it is not necessary to use locking here.
1029 * At this point suspended_count == 0 means we are being run for the
1030 * first time for the given domain in the present cycle.
1032 if (genpd->suspended_count++ == 0) {
1034 * The boot kernel might put the domain into arbitrary state,
1035 * so make it appear as powered off to pm_genpd_sync_poweron(),
1036 * so that it tries to power it on in case it was really off.
1038 genpd->status = GPD_STATE_POWER_OFF;
1039 if (genpd->suspend_power_off) {
1041 * If the domain was off before the hibernation, make
1042 * sure it will be off going forward.
1044 genpd_power_off(genpd, true);
1050 if (genpd->suspend_power_off)
1053 pm_genpd_sync_poweron(genpd, true);
1055 return genpd_start_dev(genpd, dev);
1059 * pm_genpd_complete - Complete power transition of a device in a power domain.
1060 * @dev: Device to complete the transition of.
1062 * Complete a power transition of a device (during a system-wide power
1063 * transition) under the assumption that its pm_domain field points to the
1064 * domain member of an object of type struct generic_pm_domain representing
1065 * a power domain consisting of I/O devices.
1067 static void pm_genpd_complete(struct device *dev)
1069 struct generic_pm_domain *genpd;
1072 dev_dbg(dev, "%s()\n", __func__);
1074 genpd = dev_to_genpd(dev);
1078 mutex_lock(&genpd->lock);
1080 run_complete = !genpd->suspend_power_off;
1081 if (--genpd->prepared_count == 0)
1082 genpd->suspend_power_off = false;
1084 mutex_unlock(&genpd->lock);
1087 pm_generic_complete(dev);
1088 pm_runtime_set_active(dev);
1089 pm_runtime_enable(dev);
1090 pm_request_idle(dev);
1095 * genpd_syscore_switch - Switch power during system core suspend or resume.
1096 * @dev: Device that normally is marked as "always on" to switch power for.
1098 * This routine may only be called during the system core (syscore) suspend or
1099 * resume phase for devices whose "always on" flags are set.
1101 static void genpd_syscore_switch(struct device *dev, bool suspend)
1103 struct generic_pm_domain *genpd;
1105 genpd = dev_to_genpd(dev);
1106 if (!pm_genpd_present(genpd))
1110 genpd->suspended_count++;
1111 pm_genpd_sync_poweroff(genpd, false);
1113 pm_genpd_sync_poweron(genpd, false);
1114 genpd->suspended_count--;
1118 void pm_genpd_syscore_poweroff(struct device *dev)
1120 genpd_syscore_switch(dev, true);
1122 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1124 void pm_genpd_syscore_poweron(struct device *dev)
1126 genpd_syscore_switch(dev, false);
1128 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1130 #else /* !CONFIG_PM_SLEEP */
1132 #define pm_genpd_prepare NULL
1133 #define pm_genpd_suspend NULL
1134 #define pm_genpd_suspend_late NULL
1135 #define pm_genpd_suspend_noirq NULL
1136 #define pm_genpd_resume_early NULL
1137 #define pm_genpd_resume_noirq NULL
1138 #define pm_genpd_resume NULL
1139 #define pm_genpd_freeze NULL
1140 #define pm_genpd_freeze_late NULL
1141 #define pm_genpd_freeze_noirq NULL
1142 #define pm_genpd_thaw_early NULL
1143 #define pm_genpd_thaw_noirq NULL
1144 #define pm_genpd_thaw NULL
1145 #define pm_genpd_restore_noirq NULL
1146 #define pm_genpd_complete NULL
1148 #endif /* CONFIG_PM_SLEEP */
1150 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1151 struct generic_pm_domain *genpd,
1152 struct gpd_timing_data *td)
1154 struct generic_pm_domain_data *gpd_data;
1157 ret = dev_pm_get_subsys_data(dev);
1159 return ERR_PTR(ret);
1161 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1170 gpd_data->base.dev = dev;
1171 gpd_data->td.constraint_changed = true;
1172 gpd_data->td.effective_constraint_ns = -1;
1173 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1175 spin_lock_irq(&dev->power.lock);
1177 if (dev->power.subsys_data->domain_data) {
1182 dev->power.subsys_data->domain_data = &gpd_data->base;
1184 spin_unlock_irq(&dev->power.lock);
1186 dev_pm_domain_set(dev, &genpd->domain);
1191 spin_unlock_irq(&dev->power.lock);
1194 dev_pm_put_subsys_data(dev);
1195 return ERR_PTR(ret);
1198 static void genpd_free_dev_data(struct device *dev,
1199 struct generic_pm_domain_data *gpd_data)
1201 dev_pm_domain_set(dev, NULL);
1203 spin_lock_irq(&dev->power.lock);
1205 dev->power.subsys_data->domain_data = NULL;
1207 spin_unlock_irq(&dev->power.lock);
1210 dev_pm_put_subsys_data(dev);
1214 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1215 * @genpd: PM domain to add the device to.
1216 * @dev: Device to be added.
1217 * @td: Set of PM QoS timing parameters to attach to the device.
1219 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1220 struct gpd_timing_data *td)
1222 struct generic_pm_domain_data *gpd_data;
1225 dev_dbg(dev, "%s()\n", __func__);
1227 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1230 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1231 if (IS_ERR(gpd_data))
1232 return PTR_ERR(gpd_data);
1234 mutex_lock(&genpd->lock);
1236 if (genpd->prepared_count > 0) {
1241 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1245 genpd->device_count++;
1246 genpd->max_off_time_changed = true;
1248 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1251 mutex_unlock(&genpd->lock);
1254 genpd_free_dev_data(dev, gpd_data);
1256 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1260 EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1263 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1264 * @genpd: PM domain to remove the device from.
1265 * @dev: Device to be removed.
1267 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1270 struct generic_pm_domain_data *gpd_data;
1271 struct pm_domain_data *pdd;
1274 dev_dbg(dev, "%s()\n", __func__);
1276 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1279 /* The above validation also means we have existing domain_data. */
1280 pdd = dev->power.subsys_data->domain_data;
1281 gpd_data = to_gpd_data(pdd);
1282 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1284 mutex_lock(&genpd->lock);
1286 if (genpd->prepared_count > 0) {
1291 genpd->device_count--;
1292 genpd->max_off_time_changed = true;
1294 if (genpd->detach_dev)
1295 genpd->detach_dev(genpd, dev);
1297 list_del_init(&pdd->list_node);
1299 mutex_unlock(&genpd->lock);
1301 genpd_free_dev_data(dev, gpd_data);
1306 mutex_unlock(&genpd->lock);
1307 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1311 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1314 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1315 * @genpd: Master PM domain to add the subdomain to.
1316 * @subdomain: Subdomain to be added.
1318 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1319 struct generic_pm_domain *subdomain)
1321 struct gpd_link *link, *itr;
1324 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1325 || genpd == subdomain)
1328 link = kzalloc(sizeof(*link), GFP_KERNEL);
1332 mutex_lock(&subdomain->lock);
1333 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1335 if (genpd->status == GPD_STATE_POWER_OFF
1336 && subdomain->status != GPD_STATE_POWER_OFF) {
1341 list_for_each_entry(itr, &genpd->master_links, master_node) {
1342 if (itr->slave == subdomain && itr->master == genpd) {
1348 link->master = genpd;
1349 list_add_tail(&link->master_node, &genpd->master_links);
1350 link->slave = subdomain;
1351 list_add_tail(&link->slave_node, &subdomain->slave_links);
1352 if (subdomain->status != GPD_STATE_POWER_OFF)
1353 genpd_sd_counter_inc(genpd);
1356 mutex_unlock(&genpd->lock);
1357 mutex_unlock(&subdomain->lock);
1362 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1365 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1366 * @genpd: Master PM domain to remove the subdomain from.
1367 * @subdomain: Subdomain to be removed.
1369 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1370 struct generic_pm_domain *subdomain)
1372 struct gpd_link *link;
1375 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1378 mutex_lock(&subdomain->lock);
1379 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1381 if (!list_empty(&subdomain->slave_links) || subdomain->device_count) {
1382 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1388 list_for_each_entry(link, &genpd->master_links, master_node) {
1389 if (link->slave != subdomain)
1392 list_del(&link->master_node);
1393 list_del(&link->slave_node);
1395 if (subdomain->status != GPD_STATE_POWER_OFF)
1396 genpd_sd_counter_dec(genpd);
1403 mutex_unlock(&genpd->lock);
1404 mutex_unlock(&subdomain->lock);
1408 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1410 /* Default device callbacks for generic PM domains. */
1413 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1414 * @dev: Device to handle.
1416 static int pm_genpd_default_save_state(struct device *dev)
1418 int (*cb)(struct device *__dev);
1420 if (dev->type && dev->type->pm)
1421 cb = dev->type->pm->runtime_suspend;
1422 else if (dev->class && dev->class->pm)
1423 cb = dev->class->pm->runtime_suspend;
1424 else if (dev->bus && dev->bus->pm)
1425 cb = dev->bus->pm->runtime_suspend;
1429 if (!cb && dev->driver && dev->driver->pm)
1430 cb = dev->driver->pm->runtime_suspend;
1432 return cb ? cb(dev) : 0;
1436 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1437 * @dev: Device to handle.
1439 static int pm_genpd_default_restore_state(struct device *dev)
1441 int (*cb)(struct device *__dev);
1443 if (dev->type && dev->type->pm)
1444 cb = dev->type->pm->runtime_resume;
1445 else if (dev->class && dev->class->pm)
1446 cb = dev->class->pm->runtime_resume;
1447 else if (dev->bus && dev->bus->pm)
1448 cb = dev->bus->pm->runtime_resume;
1452 if (!cb && dev->driver && dev->driver->pm)
1453 cb = dev->driver->pm->runtime_resume;
1455 return cb ? cb(dev) : 0;
1459 * pm_genpd_init - Initialize a generic I/O PM domain object.
1460 * @genpd: PM domain object to initialize.
1461 * @gov: PM domain governor to associate with the domain (may be NULL).
1462 * @is_off: Initial value of the domain's power_is_off field.
1464 void pm_genpd_init(struct generic_pm_domain *genpd,
1465 struct dev_power_governor *gov, bool is_off)
1467 if (IS_ERR_OR_NULL(genpd))
1470 INIT_LIST_HEAD(&genpd->master_links);
1471 INIT_LIST_HEAD(&genpd->slave_links);
1472 INIT_LIST_HEAD(&genpd->dev_list);
1473 mutex_init(&genpd->lock);
1475 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1476 atomic_set(&genpd->sd_count, 0);
1477 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1478 genpd->device_count = 0;
1479 genpd->max_off_time_ns = -1;
1480 genpd->max_off_time_changed = true;
1481 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1482 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1483 genpd->domain.ops.prepare = pm_genpd_prepare;
1484 genpd->domain.ops.suspend = pm_genpd_suspend;
1485 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1486 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1487 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1488 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1489 genpd->domain.ops.resume = pm_genpd_resume;
1490 genpd->domain.ops.freeze = pm_genpd_freeze;
1491 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1492 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1493 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1494 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1495 genpd->domain.ops.thaw = pm_genpd_thaw;
1496 genpd->domain.ops.poweroff = pm_genpd_suspend;
1497 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1498 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1499 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1500 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1501 genpd->domain.ops.restore = pm_genpd_resume;
1502 genpd->domain.ops.complete = pm_genpd_complete;
1503 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1504 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1506 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1507 genpd->dev_ops.stop = pm_clk_suspend;
1508 genpd->dev_ops.start = pm_clk_resume;
1511 mutex_lock(&gpd_list_lock);
1512 list_add(&genpd->gpd_list_node, &gpd_list);
1513 mutex_unlock(&gpd_list_lock);
1515 EXPORT_SYMBOL_GPL(pm_genpd_init);
1517 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1519 * Device Tree based PM domain providers.
1521 * The code below implements generic device tree based PM domain providers that
1522 * bind device tree nodes with generic PM domains registered in the system.
1524 * Any driver that registers generic PM domains and needs to support binding of
1525 * devices to these domains is supposed to register a PM domain provider, which
1526 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1528 * Two simple mapping functions have been provided for convenience:
1529 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1530 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1535 * struct of_genpd_provider - PM domain provider registration structure
1536 * @link: Entry in global list of PM domain providers
1537 * @node: Pointer to device tree node of PM domain provider
1538 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1540 * @data: context pointer to be passed into @xlate callback
1542 struct of_genpd_provider {
1543 struct list_head link;
1544 struct device_node *node;
1545 genpd_xlate_t xlate;
1549 /* List of registered PM domain providers. */
1550 static LIST_HEAD(of_genpd_providers);
1551 /* Mutex to protect the list above. */
1552 static DEFINE_MUTEX(of_genpd_mutex);
1555 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1556 * @genpdspec: OF phandle args to map into a PM domain
1557 * @data: xlate function private data - pointer to struct generic_pm_domain
1559 * This is a generic xlate function that can be used to model PM domains that
1560 * have their own device tree nodes. The private data of xlate function needs
1561 * to be a valid pointer to struct generic_pm_domain.
1563 struct generic_pm_domain *__of_genpd_xlate_simple(
1564 struct of_phandle_args *genpdspec,
1567 if (genpdspec->args_count != 0)
1568 return ERR_PTR(-EINVAL);
1571 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1574 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1575 * @genpdspec: OF phandle args to map into a PM domain
1576 * @data: xlate function private data - pointer to struct genpd_onecell_data
1578 * This is a generic xlate function that can be used to model simple PM domain
1579 * controllers that have one device tree node and provide multiple PM domains.
1580 * A single cell is used as an index into an array of PM domains specified in
1581 * the genpd_onecell_data struct when registering the provider.
1583 struct generic_pm_domain *__of_genpd_xlate_onecell(
1584 struct of_phandle_args *genpdspec,
1587 struct genpd_onecell_data *genpd_data = data;
1588 unsigned int idx = genpdspec->args[0];
1590 if (genpdspec->args_count != 1)
1591 return ERR_PTR(-EINVAL);
1593 if (idx >= genpd_data->num_domains) {
1594 pr_err("%s: invalid domain index %u\n", __func__, idx);
1595 return ERR_PTR(-EINVAL);
1598 if (!genpd_data->domains[idx])
1599 return ERR_PTR(-ENOENT);
1601 return genpd_data->domains[idx];
1603 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1606 * __of_genpd_add_provider() - Register a PM domain provider for a node
1607 * @np: Device node pointer associated with the PM domain provider.
1608 * @xlate: Callback for decoding PM domain from phandle arguments.
1609 * @data: Context pointer for @xlate callback.
1611 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1614 struct of_genpd_provider *cp;
1616 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1620 cp->node = of_node_get(np);
1624 mutex_lock(&of_genpd_mutex);
1625 list_add(&cp->link, &of_genpd_providers);
1626 mutex_unlock(&of_genpd_mutex);
1627 pr_debug("Added domain provider from %s\n", np->full_name);
1631 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1634 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1635 * @np: Device node pointer associated with the PM domain provider
1637 void of_genpd_del_provider(struct device_node *np)
1639 struct of_genpd_provider *cp;
1641 mutex_lock(&of_genpd_mutex);
1642 list_for_each_entry(cp, &of_genpd_providers, link) {
1643 if (cp->node == np) {
1644 list_del(&cp->link);
1645 of_node_put(cp->node);
1650 mutex_unlock(&of_genpd_mutex);
1652 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1655 * of_genpd_get_from_provider() - Look-up PM domain
1656 * @genpdspec: OF phandle args to use for look-up
1658 * Looks for a PM domain provider under the node specified by @genpdspec and if
1659 * found, uses xlate function of the provider to map phandle args to a PM
1662 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1665 struct generic_pm_domain *of_genpd_get_from_provider(
1666 struct of_phandle_args *genpdspec)
1668 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1669 struct of_genpd_provider *provider;
1671 mutex_lock(&of_genpd_mutex);
1673 /* Check if we have such a provider in our array */
1674 list_for_each_entry(provider, &of_genpd_providers, link) {
1675 if (provider->node == genpdspec->np)
1676 genpd = provider->xlate(genpdspec, provider->data);
1681 mutex_unlock(&of_genpd_mutex);
1685 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1688 * genpd_dev_pm_detach - Detach a device from its PM domain.
1689 * @dev: Device to detach.
1690 * @power_off: Currently not used
1692 * Try to locate a corresponding generic PM domain, which the device was
1693 * attached to previously. If such is found, the device is detached from it.
1695 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1697 struct generic_pm_domain *pd;
1701 pd = pm_genpd_lookup_dev(dev);
1705 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1707 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1708 ret = pm_genpd_remove_device(pd, dev);
1717 dev_err(dev, "failed to remove from PM domain %s: %d",
1722 /* Check if PM domain can be powered off after removing this device. */
1723 genpd_queue_power_off_work(pd);
1726 static void genpd_dev_pm_sync(struct device *dev)
1728 struct generic_pm_domain *pd;
1730 pd = dev_to_genpd(dev);
1734 genpd_queue_power_off_work(pd);
1738 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1739 * @dev: Device to attach.
1741 * Parse device's OF node to find a PM domain specifier. If such is found,
1742 * attaches the device to retrieved pm_domain ops.
1744 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1745 * backwards compatibility with existing DTBs.
1747 * Returns 0 on successfully attached PM domain or negative error code. Note
1748 * that if a power-domain exists for the device, but it cannot be found or
1749 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1750 * probed and to re-try again later.
1752 int genpd_dev_pm_attach(struct device *dev)
1754 struct of_phandle_args pd_args;
1755 struct generic_pm_domain *pd;
1765 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1766 "#power-domain-cells", 0, &pd_args);
1772 * Try legacy Samsung-specific bindings
1773 * (for backwards compatibility of DT ABI)
1775 pd_args.args_count = 0;
1776 pd_args.np = of_parse_phandle(dev->of_node,
1777 "samsung,power-domain", 0);
1782 pd = of_genpd_get_from_provider(&pd_args);
1783 of_node_put(pd_args.np);
1785 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1786 __func__, PTR_ERR(pd));
1787 return -EPROBE_DEFER;
1790 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1792 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1793 ret = pm_genpd_add_device(pd, dev);
1802 dev_err(dev, "failed to add to PM domain %s: %d",
1807 dev->pm_domain->detach = genpd_dev_pm_detach;
1808 dev->pm_domain->sync = genpd_dev_pm_sync;
1810 mutex_lock(&pd->lock);
1811 ret = genpd_poweron(pd, 0);
1812 mutex_unlock(&pd->lock);
1814 return ret ? -EPROBE_DEFER : 0;
1816 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1817 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1820 /*** debugfs support ***/
1822 #ifdef CONFIG_PM_ADVANCED_DEBUG
1823 #include <linux/pm.h>
1824 #include <linux/device.h>
1825 #include <linux/debugfs.h>
1826 #include <linux/seq_file.h>
1827 #include <linux/init.h>
1828 #include <linux/kobject.h>
1829 static struct dentry *pm_genpd_debugfs_dir;
1832 * TODO: This function is a slightly modified version of rtpm_status_show
1833 * from sysfs.c, so generalize it.
1835 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1837 static const char * const status_lookup[] = {
1838 [RPM_ACTIVE] = "active",
1839 [RPM_RESUMING] = "resuming",
1840 [RPM_SUSPENDED] = "suspended",
1841 [RPM_SUSPENDING] = "suspending"
1845 if (dev->power.runtime_error)
1847 else if (dev->power.disable_depth)
1849 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1850 p = status_lookup[dev->power.runtime_status];
1857 static int pm_genpd_summary_one(struct seq_file *s,
1858 struct generic_pm_domain *genpd)
1860 static const char * const status_lookup[] = {
1861 [GPD_STATE_ACTIVE] = "on",
1862 [GPD_STATE_POWER_OFF] = "off"
1864 struct pm_domain_data *pm_data;
1865 const char *kobj_path;
1866 struct gpd_link *link;
1869 ret = mutex_lock_interruptible(&genpd->lock);
1871 return -ERESTARTSYS;
1873 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1875 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
1878 * Modifications on the list require holding locks on both
1879 * master and slave, so we are safe.
1880 * Also genpd->name is immutable.
1882 list_for_each_entry(link, &genpd->master_links, master_node) {
1883 seq_printf(s, "%s", link->slave->name);
1884 if (!list_is_last(&link->master_node, &genpd->master_links))
1888 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1889 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1890 if (kobj_path == NULL)
1893 seq_printf(s, "\n %-50s ", kobj_path);
1894 rtpm_status_str(s, pm_data->dev);
1900 mutex_unlock(&genpd->lock);
1905 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1907 struct generic_pm_domain *genpd;
1910 seq_puts(s, "domain status slaves\n");
1911 seq_puts(s, " /device runtime status\n");
1912 seq_puts(s, "----------------------------------------------------------------------\n");
1914 ret = mutex_lock_interruptible(&gpd_list_lock);
1916 return -ERESTARTSYS;
1918 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1919 ret = pm_genpd_summary_one(s, genpd);
1923 mutex_unlock(&gpd_list_lock);
1928 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1930 return single_open(file, pm_genpd_summary_show, NULL);
1933 static const struct file_operations pm_genpd_summary_fops = {
1934 .open = pm_genpd_summary_open,
1936 .llseek = seq_lseek,
1937 .release = single_release,
1940 static int __init pm_genpd_debug_init(void)
1944 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1946 if (!pm_genpd_debugfs_dir)
1949 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1950 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1956 late_initcall(pm_genpd_debug_init);
1958 static void __exit pm_genpd_debug_exit(void)
1960 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1962 __exitcall(pm_genpd_debug_exit);
1963 #endif /* CONFIG_PM_ADVANCED_DEBUG */