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 static LIST_HEAD(gpd_list);
38 static DEFINE_MUTEX(gpd_list_lock);
41 * Get the generic PM domain for a particular struct device.
42 * This validates the struct device pointer, the PM domain pointer,
43 * and checks that the PM domain pointer is a real generic PM domain.
44 * Any failure results in NULL being returned.
46 struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
48 struct generic_pm_domain *genpd = NULL, *gpd;
50 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
53 mutex_lock(&gpd_list_lock);
54 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
55 if (&gpd->domain == dev->pm_domain) {
60 mutex_unlock(&gpd_list_lock);
66 * This should only be used where we are certain that the pm_domain
67 * attached to the device is a genpd domain.
69 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
71 if (IS_ERR_OR_NULL(dev->pm_domain))
72 return ERR_PTR(-EINVAL);
74 return pd_to_genpd(dev->pm_domain);
77 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
79 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
82 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
84 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
87 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
91 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
92 ret = !!atomic_dec_and_test(&genpd->sd_count);
97 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
99 atomic_inc(&genpd->sd_count);
100 smp_mb__after_atomic();
103 static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
109 if (!genpd->power_on)
113 return genpd->power_on(genpd);
115 time_start = ktime_get();
116 ret = genpd->power_on(genpd);
120 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
121 if (elapsed_ns <= genpd->power_on_latency_ns)
124 genpd->power_on_latency_ns = elapsed_ns;
125 genpd->max_off_time_changed = true;
126 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
127 genpd->name, "on", elapsed_ns);
132 static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
138 if (!genpd->power_off)
142 return genpd->power_off(genpd);
144 time_start = ktime_get();
145 ret = genpd->power_off(genpd);
149 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
150 if (elapsed_ns <= genpd->power_off_latency_ns)
153 genpd->power_off_latency_ns = elapsed_ns;
154 genpd->max_off_time_changed = true;
155 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
156 genpd->name, "off", elapsed_ns);
162 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
163 * @genpd: PM domain to power off.
165 * Queue up the execution of genpd_poweroff() unless it's already been done
168 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
170 queue_work(pm_wq, &genpd->power_off_work);
174 * __genpd_poweron - Restore power to a given PM domain and its masters.
175 * @genpd: PM domain to power up.
176 * @depth: nesting count for lockdep.
178 * Restore power to @genpd and all of its masters so that it is possible to
179 * resume a device belonging to it.
181 static int __genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
183 struct gpd_link *link;
186 if (genpd->status == GPD_STATE_ACTIVE
187 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
191 * The list is guaranteed not to change while the loop below is being
192 * executed, unless one of the masters' .power_on() callbacks fiddles
195 list_for_each_entry(link, &genpd->slave_links, slave_node) {
196 struct generic_pm_domain *master = link->master;
198 genpd_sd_counter_inc(master);
200 mutex_lock_nested(&master->lock, depth + 1);
201 ret = __genpd_poweron(master, depth + 1);
202 mutex_unlock(&master->lock);
205 genpd_sd_counter_dec(master);
210 ret = genpd_power_on(genpd, true);
214 genpd->status = GPD_STATE_ACTIVE;
218 list_for_each_entry_continue_reverse(link,
221 genpd_sd_counter_dec(link->master);
222 genpd_queue_power_off_work(link->master);
229 * genpd_poweron - Restore power to a given PM domain and its masters.
230 * @genpd: PM domain to power up.
232 static int genpd_poweron(struct generic_pm_domain *genpd)
236 mutex_lock(&genpd->lock);
237 ret = __genpd_poweron(genpd, 0);
238 mutex_unlock(&genpd->lock);
243 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
245 return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
248 static int genpd_restore_dev(struct generic_pm_domain *genpd,
251 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
254 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
255 unsigned long val, void *ptr)
257 struct generic_pm_domain_data *gpd_data;
260 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
261 dev = gpd_data->base.dev;
264 struct generic_pm_domain *genpd;
265 struct pm_domain_data *pdd;
267 spin_lock_irq(&dev->power.lock);
269 pdd = dev->power.subsys_data ?
270 dev->power.subsys_data->domain_data : NULL;
271 if (pdd && pdd->dev) {
272 to_gpd_data(pdd)->td.constraint_changed = true;
273 genpd = dev_to_genpd(dev);
275 genpd = ERR_PTR(-ENODATA);
278 spin_unlock_irq(&dev->power.lock);
280 if (!IS_ERR(genpd)) {
281 mutex_lock(&genpd->lock);
282 genpd->max_off_time_changed = true;
283 mutex_unlock(&genpd->lock);
287 if (!dev || dev->power.ignore_children)
295 * genpd_poweroff - Remove power from a given PM domain.
296 * @genpd: PM domain to power down.
297 * @is_async: PM domain is powered down from a scheduled work
299 * If all of the @genpd's devices have been suspended and all of its subdomains
300 * have been powered down, remove power from @genpd.
302 static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
304 struct pm_domain_data *pdd;
305 struct gpd_link *link;
306 unsigned int not_suspended = 0;
309 * Do not try to power off the domain in the following situations:
310 * (1) The domain is already in the "power off" state.
311 * (2) System suspend is in progress.
313 if (genpd->status == GPD_STATE_POWER_OFF
314 || genpd->prepared_count > 0)
317 if (atomic_read(&genpd->sd_count) > 0)
320 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
321 enum pm_qos_flags_status stat;
323 stat = dev_pm_qos_flags(pdd->dev,
324 PM_QOS_FLAG_NO_POWER_OFF
325 | PM_QOS_FLAG_REMOTE_WAKEUP);
326 if (stat > PM_QOS_FLAGS_NONE)
329 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
333 if (not_suspended > 1 || (not_suspended == 1 && is_async))
336 if (genpd->gov && genpd->gov->power_down_ok) {
337 if (!genpd->gov->power_down_ok(&genpd->domain))
341 if (genpd->power_off) {
344 if (atomic_read(&genpd->sd_count) > 0)
348 * If sd_count > 0 at this point, one of the subdomains hasn't
349 * managed to call genpd_poweron() for the master yet after
350 * incrementing it. In that case genpd_poweron() will wait
351 * for us to drop the lock, so we can call .power_off() and let
352 * the genpd_poweron() restore power for us (this shouldn't
353 * happen very often).
355 ret = genpd_power_off(genpd, true);
360 genpd->status = GPD_STATE_POWER_OFF;
362 list_for_each_entry(link, &genpd->slave_links, slave_node) {
363 genpd_sd_counter_dec(link->master);
364 genpd_queue_power_off_work(link->master);
371 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
372 * @work: Work structure used for scheduling the execution of this function.
374 static void genpd_power_off_work_fn(struct work_struct *work)
376 struct generic_pm_domain *genpd;
378 genpd = container_of(work, struct generic_pm_domain, power_off_work);
380 mutex_lock(&genpd->lock);
381 genpd_poweroff(genpd, true);
382 mutex_unlock(&genpd->lock);
386 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
387 * @dev: Device to suspend.
389 * Carry out a runtime suspend of a device under the assumption that its
390 * pm_domain field points to the domain member of an object of type
391 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
393 static int pm_genpd_runtime_suspend(struct device *dev)
395 struct generic_pm_domain *genpd;
396 bool (*stop_ok)(struct device *__dev);
397 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
398 bool runtime_pm = pm_runtime_enabled(dev);
403 dev_dbg(dev, "%s()\n", __func__);
405 genpd = dev_to_genpd(dev);
410 * A runtime PM centric subsystem/driver may re-use the runtime PM
411 * callbacks for other purposes than runtime PM. In those scenarios
412 * runtime PM is disabled. Under these circumstances, we shall skip
413 * validating/measuring the PM QoS latency.
415 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
416 if (runtime_pm && stop_ok && !stop_ok(dev))
419 /* Measure suspend latency. */
421 time_start = ktime_get();
423 ret = genpd_save_dev(genpd, dev);
427 ret = genpd_stop_dev(genpd, dev);
429 genpd_restore_dev(genpd, dev);
433 /* Update suspend latency value if the measured time exceeds it. */
435 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
436 if (elapsed_ns > td->suspend_latency_ns) {
437 td->suspend_latency_ns = elapsed_ns;
438 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
440 genpd->max_off_time_changed = true;
441 td->constraint_changed = true;
446 * If power.irq_safe is set, this routine will be run with interrupts
447 * off, so it can't use mutexes.
449 if (dev->power.irq_safe)
452 mutex_lock(&genpd->lock);
453 genpd_poweroff(genpd, false);
454 mutex_unlock(&genpd->lock);
460 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
461 * @dev: Device to resume.
463 * Carry out a runtime resume of a device under the assumption that its
464 * pm_domain field points to the domain member of an object of type
465 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
467 static int pm_genpd_runtime_resume(struct device *dev)
469 struct generic_pm_domain *genpd;
470 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
471 bool runtime_pm = pm_runtime_enabled(dev);
477 dev_dbg(dev, "%s()\n", __func__);
479 genpd = dev_to_genpd(dev);
483 /* If power.irq_safe, the PM domain is never powered off. */
484 if (dev->power.irq_safe) {
489 mutex_lock(&genpd->lock);
490 ret = __genpd_poweron(genpd, 0);
491 mutex_unlock(&genpd->lock);
497 /* Measure resume latency. */
498 if (timed && runtime_pm)
499 time_start = ktime_get();
501 genpd_start_dev(genpd, dev);
502 genpd_restore_dev(genpd, dev);
504 /* Update resume latency value if the measured time exceeds it. */
505 if (timed && runtime_pm) {
506 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
507 if (elapsed_ns > td->resume_latency_ns) {
508 td->resume_latency_ns = elapsed_ns;
509 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
511 genpd->max_off_time_changed = true;
512 td->constraint_changed = true;
519 static bool pd_ignore_unused;
520 static int __init pd_ignore_unused_setup(char *__unused)
522 pd_ignore_unused = true;
525 __setup("pd_ignore_unused", pd_ignore_unused_setup);
528 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
530 static int __init genpd_poweroff_unused(void)
532 struct generic_pm_domain *genpd;
534 if (pd_ignore_unused) {
535 pr_warn("genpd: Not disabling unused power domains\n");
539 mutex_lock(&gpd_list_lock);
541 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
542 genpd_queue_power_off_work(genpd);
544 mutex_unlock(&gpd_list_lock);
548 late_initcall(genpd_poweroff_unused);
550 #ifdef CONFIG_PM_SLEEP
553 * pm_genpd_present - Check if the given PM domain has been initialized.
554 * @genpd: PM domain to check.
556 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
558 const struct generic_pm_domain *gpd;
560 if (IS_ERR_OR_NULL(genpd))
563 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
570 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
573 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
577 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
578 * @genpd: PM domain to power off, if possible.
579 * @timed: True if latency measurements are allowed.
581 * Check if the given PM domain can be powered off (during system suspend or
582 * hibernation) and do that if so. Also, in that case propagate to its masters.
584 * This function is only called in "noirq" and "syscore" stages of system power
585 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
586 * executed sequentially, so it is guaranteed that it will never run twice in
589 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
592 struct gpd_link *link;
594 if (genpd->status == GPD_STATE_POWER_OFF)
597 if (genpd->suspended_count != genpd->device_count
598 || atomic_read(&genpd->sd_count) > 0)
601 genpd_power_off(genpd, timed);
603 genpd->status = GPD_STATE_POWER_OFF;
605 list_for_each_entry(link, &genpd->slave_links, slave_node) {
606 genpd_sd_counter_dec(link->master);
607 pm_genpd_sync_poweroff(link->master, timed);
612 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
613 * @genpd: PM domain to power on.
614 * @timed: True if latency measurements are allowed.
616 * This function is only called in "noirq" and "syscore" stages of system power
617 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
618 * executed sequentially, so it is guaranteed that it will never run twice in
621 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
624 struct gpd_link *link;
626 if (genpd->status == GPD_STATE_ACTIVE)
629 list_for_each_entry(link, &genpd->slave_links, slave_node) {
630 pm_genpd_sync_poweron(link->master, timed);
631 genpd_sd_counter_inc(link->master);
634 genpd_power_on(genpd, timed);
636 genpd->status = GPD_STATE_ACTIVE;
640 * resume_needed - Check whether to resume a device before system suspend.
641 * @dev: Device to check.
642 * @genpd: PM domain the device belongs to.
644 * There are two cases in which a device that can wake up the system from sleep
645 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
646 * to wake up the system and it has to remain active for this purpose while the
647 * system is in the sleep state and (2) if the device is not enabled to wake up
648 * the system from sleep states and it generally doesn't generate wakeup signals
649 * by itself (those signals are generated on its behalf by other parts of the
650 * system). In the latter case it may be necessary to reconfigure the device's
651 * wakeup settings during system suspend, because it may have been set up to
652 * signal remote wakeup from the system's working state as needed by runtime PM.
653 * Return 'true' in either of the above cases.
655 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
659 if (!device_can_wakeup(dev))
662 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
663 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
667 * pm_genpd_prepare - Start power transition of a device in a PM domain.
668 * @dev: Device to start the transition of.
670 * Start a power transition of a device (during a system-wide power transition)
671 * under the assumption that its pm_domain field points to the domain member of
672 * an object of type struct generic_pm_domain representing a PM domain
673 * consisting of I/O devices.
675 static int pm_genpd_prepare(struct device *dev)
677 struct generic_pm_domain *genpd;
680 dev_dbg(dev, "%s()\n", __func__);
682 genpd = dev_to_genpd(dev);
687 * If a wakeup request is pending for the device, it should be woken up
688 * at this point and a system wakeup event should be reported if it's
689 * set up to wake up the system from sleep states.
691 pm_runtime_get_noresume(dev);
692 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
693 pm_wakeup_event(dev, 0);
695 if (pm_wakeup_pending()) {
700 if (resume_needed(dev, genpd))
701 pm_runtime_resume(dev);
703 mutex_lock(&genpd->lock);
705 if (genpd->prepared_count++ == 0) {
706 genpd->suspended_count = 0;
707 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
710 mutex_unlock(&genpd->lock);
712 if (genpd->suspend_power_off) {
713 pm_runtime_put_noidle(dev);
718 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
719 * so genpd_poweron() will return immediately, but if the device
720 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
721 * to make it operational.
723 pm_runtime_resume(dev);
724 __pm_runtime_disable(dev, false);
726 ret = pm_generic_prepare(dev);
728 mutex_lock(&genpd->lock);
730 if (--genpd->prepared_count == 0)
731 genpd->suspend_power_off = false;
733 mutex_unlock(&genpd->lock);
734 pm_runtime_enable(dev);
742 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
743 * @dev: Device to suspend.
745 * Suspend a device under the assumption that its pm_domain field points to the
746 * domain member of an object of type struct generic_pm_domain representing
747 * a PM domain consisting of I/O devices.
749 static int pm_genpd_suspend(struct device *dev)
751 struct generic_pm_domain *genpd;
753 dev_dbg(dev, "%s()\n", __func__);
755 genpd = dev_to_genpd(dev);
759 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
763 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
764 * @dev: Device to suspend.
766 * Carry out a late suspend of a device under the assumption that its
767 * pm_domain field points to the domain member of an object of type
768 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
770 static int pm_genpd_suspend_late(struct device *dev)
772 struct generic_pm_domain *genpd;
774 dev_dbg(dev, "%s()\n", __func__);
776 genpd = dev_to_genpd(dev);
780 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
784 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
785 * @dev: Device to suspend.
787 * Stop the device and remove power from the domain if all devices in it have
790 static int pm_genpd_suspend_noirq(struct device *dev)
792 struct generic_pm_domain *genpd;
794 dev_dbg(dev, "%s()\n", __func__);
796 genpd = dev_to_genpd(dev);
800 if (genpd->suspend_power_off
801 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
804 genpd_stop_dev(genpd, dev);
807 * Since all of the "noirq" callbacks are executed sequentially, it is
808 * guaranteed that this function will never run twice in parallel for
809 * the same PM domain, so it is not necessary to use locking here.
811 genpd->suspended_count++;
812 pm_genpd_sync_poweroff(genpd, true);
818 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
819 * @dev: Device to resume.
821 * Restore power to the device's PM domain, if necessary, and start the device.
823 static int pm_genpd_resume_noirq(struct device *dev)
825 struct generic_pm_domain *genpd;
827 dev_dbg(dev, "%s()\n", __func__);
829 genpd = dev_to_genpd(dev);
833 if (genpd->suspend_power_off
834 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
838 * Since all of the "noirq" callbacks are executed sequentially, it is
839 * guaranteed that this function will never run twice in parallel for
840 * the same PM domain, so it is not necessary to use locking here.
842 pm_genpd_sync_poweron(genpd, true);
843 genpd->suspended_count--;
845 return genpd_start_dev(genpd, dev);
849 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
850 * @dev: Device to resume.
852 * Carry out an early resume of a device under the assumption that its
853 * pm_domain field points to the domain member of an object of type
854 * struct generic_pm_domain representing a power domain consisting of I/O
857 static int pm_genpd_resume_early(struct device *dev)
859 struct generic_pm_domain *genpd;
861 dev_dbg(dev, "%s()\n", __func__);
863 genpd = dev_to_genpd(dev);
867 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
871 * pm_genpd_resume - Resume of device in an I/O PM domain.
872 * @dev: Device to resume.
874 * Resume a device under the assumption that its pm_domain field points to the
875 * domain member of an object of type struct generic_pm_domain representing
876 * a power domain consisting of I/O devices.
878 static int pm_genpd_resume(struct device *dev)
880 struct generic_pm_domain *genpd;
882 dev_dbg(dev, "%s()\n", __func__);
884 genpd = dev_to_genpd(dev);
888 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
892 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
893 * @dev: Device to freeze.
895 * Freeze a device under the assumption that its pm_domain field points to the
896 * domain member of an object of type struct generic_pm_domain representing
897 * a power domain consisting of I/O devices.
899 static int pm_genpd_freeze(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(dev);
913 * pm_genpd_freeze_late - Late freeze of 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_late(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 : pm_generic_freeze_late(dev);
935 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
936 * @dev: Device to freeze.
938 * Carry out a late freeze of a device under the assumption that its
939 * pm_domain field points to the domain member of an object of type
940 * struct generic_pm_domain representing a power domain consisting of I/O
943 static int pm_genpd_freeze_noirq(struct device *dev)
945 struct generic_pm_domain *genpd;
947 dev_dbg(dev, "%s()\n", __func__);
949 genpd = dev_to_genpd(dev);
953 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
957 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
958 * @dev: Device to thaw.
960 * Start the device, unless power has been removed from the domain already
961 * before the system transition.
963 static int pm_genpd_thaw_noirq(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 ?
974 0 : genpd_start_dev(genpd, dev);
978 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
979 * @dev: Device to thaw.
981 * Carry out an early thaw of a device under the assumption that its
982 * pm_domain field points to the domain member of an object of type
983 * struct generic_pm_domain representing a power domain consisting of I/O
986 static int pm_genpd_thaw_early(struct device *dev)
988 struct generic_pm_domain *genpd;
990 dev_dbg(dev, "%s()\n", __func__);
992 genpd = dev_to_genpd(dev);
996 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
1000 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1001 * @dev: Device to thaw.
1003 * Thaw a device under the assumption that its pm_domain field points to the
1004 * domain member of an object of type struct generic_pm_domain representing
1005 * a power domain consisting of I/O devices.
1007 static int pm_genpd_thaw(struct device *dev)
1009 struct generic_pm_domain *genpd;
1011 dev_dbg(dev, "%s()\n", __func__);
1013 genpd = dev_to_genpd(dev);
1017 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1021 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1022 * @dev: Device to resume.
1024 * Make sure the domain will be in the same power state as before the
1025 * hibernation the system is resuming from and start the device if necessary.
1027 static int pm_genpd_restore_noirq(struct device *dev)
1029 struct generic_pm_domain *genpd;
1031 dev_dbg(dev, "%s()\n", __func__);
1033 genpd = dev_to_genpd(dev);
1038 * Since all of the "noirq" callbacks are executed sequentially, it is
1039 * guaranteed that this function will never run twice in parallel for
1040 * the same PM domain, so it is not necessary to use locking here.
1042 * At this point suspended_count == 0 means we are being run for the
1043 * first time for the given domain in the present cycle.
1045 if (genpd->suspended_count++ == 0) {
1047 * The boot kernel might put the domain into arbitrary state,
1048 * so make it appear as powered off to pm_genpd_sync_poweron(),
1049 * so that it tries to power it on in case it was really off.
1051 genpd->status = GPD_STATE_POWER_OFF;
1052 if (genpd->suspend_power_off) {
1054 * If the domain was off before the hibernation, make
1055 * sure it will be off going forward.
1057 genpd_power_off(genpd, true);
1063 if (genpd->suspend_power_off)
1066 pm_genpd_sync_poweron(genpd, true);
1068 return genpd_start_dev(genpd, dev);
1072 * pm_genpd_complete - Complete power transition of a device in a power domain.
1073 * @dev: Device to complete the transition of.
1075 * Complete a power transition of a device (during a system-wide power
1076 * transition) under the assumption that its pm_domain field points to the
1077 * domain member of an object of type struct generic_pm_domain representing
1078 * a power domain consisting of I/O devices.
1080 static void pm_genpd_complete(struct device *dev)
1082 struct generic_pm_domain *genpd;
1085 dev_dbg(dev, "%s()\n", __func__);
1087 genpd = dev_to_genpd(dev);
1091 mutex_lock(&genpd->lock);
1093 run_complete = !genpd->suspend_power_off;
1094 if (--genpd->prepared_count == 0)
1095 genpd->suspend_power_off = false;
1097 mutex_unlock(&genpd->lock);
1100 pm_generic_complete(dev);
1101 pm_runtime_set_active(dev);
1102 pm_runtime_enable(dev);
1103 pm_request_idle(dev);
1108 * genpd_syscore_switch - Switch power during system core suspend or resume.
1109 * @dev: Device that normally is marked as "always on" to switch power for.
1111 * This routine may only be called during the system core (syscore) suspend or
1112 * resume phase for devices whose "always on" flags are set.
1114 static void genpd_syscore_switch(struct device *dev, bool suspend)
1116 struct generic_pm_domain *genpd;
1118 genpd = dev_to_genpd(dev);
1119 if (!pm_genpd_present(genpd))
1123 genpd->suspended_count++;
1124 pm_genpd_sync_poweroff(genpd, false);
1126 pm_genpd_sync_poweron(genpd, false);
1127 genpd->suspended_count--;
1131 void pm_genpd_syscore_poweroff(struct device *dev)
1133 genpd_syscore_switch(dev, true);
1135 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1137 void pm_genpd_syscore_poweron(struct device *dev)
1139 genpd_syscore_switch(dev, false);
1141 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1143 #else /* !CONFIG_PM_SLEEP */
1145 #define pm_genpd_prepare NULL
1146 #define pm_genpd_suspend NULL
1147 #define pm_genpd_suspend_late NULL
1148 #define pm_genpd_suspend_noirq NULL
1149 #define pm_genpd_resume_early NULL
1150 #define pm_genpd_resume_noirq NULL
1151 #define pm_genpd_resume NULL
1152 #define pm_genpd_freeze NULL
1153 #define pm_genpd_freeze_late NULL
1154 #define pm_genpd_freeze_noirq NULL
1155 #define pm_genpd_thaw_early NULL
1156 #define pm_genpd_thaw_noirq NULL
1157 #define pm_genpd_thaw NULL
1158 #define pm_genpd_restore_noirq NULL
1159 #define pm_genpd_complete NULL
1161 #endif /* CONFIG_PM_SLEEP */
1163 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1164 struct generic_pm_domain *genpd,
1165 struct gpd_timing_data *td)
1167 struct generic_pm_domain_data *gpd_data;
1170 ret = dev_pm_get_subsys_data(dev);
1172 return ERR_PTR(ret);
1174 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1183 gpd_data->base.dev = dev;
1184 gpd_data->td.constraint_changed = true;
1185 gpd_data->td.effective_constraint_ns = -1;
1186 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1188 spin_lock_irq(&dev->power.lock);
1190 if (dev->power.subsys_data->domain_data) {
1195 dev->power.subsys_data->domain_data = &gpd_data->base;
1196 dev->pm_domain = &genpd->domain;
1198 spin_unlock_irq(&dev->power.lock);
1203 spin_unlock_irq(&dev->power.lock);
1206 dev_pm_put_subsys_data(dev);
1207 return ERR_PTR(ret);
1210 static void genpd_free_dev_data(struct device *dev,
1211 struct generic_pm_domain_data *gpd_data)
1213 spin_lock_irq(&dev->power.lock);
1215 dev->pm_domain = NULL;
1216 dev->power.subsys_data->domain_data = NULL;
1218 spin_unlock_irq(&dev->power.lock);
1221 dev_pm_put_subsys_data(dev);
1225 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1226 * @genpd: PM domain to add the device to.
1227 * @dev: Device to be added.
1228 * @td: Set of PM QoS timing parameters to attach to the device.
1230 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1231 struct gpd_timing_data *td)
1233 struct generic_pm_domain_data *gpd_data;
1236 dev_dbg(dev, "%s()\n", __func__);
1238 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1241 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1242 if (IS_ERR(gpd_data))
1243 return PTR_ERR(gpd_data);
1245 mutex_lock(&genpd->lock);
1247 if (genpd->prepared_count > 0) {
1252 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1256 genpd->device_count++;
1257 genpd->max_off_time_changed = true;
1259 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1262 mutex_unlock(&genpd->lock);
1265 genpd_free_dev_data(dev, gpd_data);
1267 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1271 EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1274 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1275 * @genpd: PM domain to remove the device from.
1276 * @dev: Device to be removed.
1278 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1281 struct generic_pm_domain_data *gpd_data;
1282 struct pm_domain_data *pdd;
1285 dev_dbg(dev, "%s()\n", __func__);
1287 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1290 /* The above validation also means we have existing domain_data. */
1291 pdd = dev->power.subsys_data->domain_data;
1292 gpd_data = to_gpd_data(pdd);
1293 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1295 mutex_lock(&genpd->lock);
1297 if (genpd->prepared_count > 0) {
1302 genpd->device_count--;
1303 genpd->max_off_time_changed = true;
1305 if (genpd->detach_dev)
1306 genpd->detach_dev(genpd, dev);
1308 list_del_init(&pdd->list_node);
1310 mutex_unlock(&genpd->lock);
1312 genpd_free_dev_data(dev, gpd_data);
1317 mutex_unlock(&genpd->lock);
1318 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1322 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1325 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1326 * @genpd: Master PM domain to add the subdomain to.
1327 * @subdomain: Subdomain to be added.
1329 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1330 struct generic_pm_domain *subdomain)
1332 struct gpd_link *link, *itr;
1335 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1336 || genpd == subdomain)
1339 link = kzalloc(sizeof(*link), GFP_KERNEL);
1343 mutex_lock(&subdomain->lock);
1344 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1346 if (genpd->status == GPD_STATE_POWER_OFF
1347 && subdomain->status != GPD_STATE_POWER_OFF) {
1352 list_for_each_entry(itr, &genpd->master_links, master_node) {
1353 if (itr->slave == subdomain && itr->master == genpd) {
1359 link->master = genpd;
1360 list_add_tail(&link->master_node, &genpd->master_links);
1361 link->slave = subdomain;
1362 list_add_tail(&link->slave_node, &subdomain->slave_links);
1363 if (subdomain->status != GPD_STATE_POWER_OFF)
1364 genpd_sd_counter_inc(genpd);
1367 mutex_unlock(&genpd->lock);
1368 mutex_unlock(&subdomain->lock);
1373 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1376 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1377 * @genpd: Master PM domain to remove the subdomain from.
1378 * @subdomain: Subdomain to be removed.
1380 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1381 struct generic_pm_domain *subdomain)
1383 struct gpd_link *link;
1386 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1389 mutex_lock(&subdomain->lock);
1390 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1392 if (!list_empty(&subdomain->slave_links) || subdomain->device_count) {
1393 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1399 list_for_each_entry(link, &genpd->master_links, master_node) {
1400 if (link->slave != subdomain)
1403 list_del(&link->master_node);
1404 list_del(&link->slave_node);
1406 if (subdomain->status != GPD_STATE_POWER_OFF)
1407 genpd_sd_counter_dec(genpd);
1414 mutex_unlock(&genpd->lock);
1415 mutex_unlock(&subdomain->lock);
1419 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1421 /* Default device callbacks for generic PM domains. */
1424 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1425 * @dev: Device to handle.
1427 static int pm_genpd_default_save_state(struct device *dev)
1429 int (*cb)(struct device *__dev);
1431 if (dev->type && dev->type->pm)
1432 cb = dev->type->pm->runtime_suspend;
1433 else if (dev->class && dev->class->pm)
1434 cb = dev->class->pm->runtime_suspend;
1435 else if (dev->bus && dev->bus->pm)
1436 cb = dev->bus->pm->runtime_suspend;
1440 if (!cb && dev->driver && dev->driver->pm)
1441 cb = dev->driver->pm->runtime_suspend;
1443 return cb ? cb(dev) : 0;
1447 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1448 * @dev: Device to handle.
1450 static int pm_genpd_default_restore_state(struct device *dev)
1452 int (*cb)(struct device *__dev);
1454 if (dev->type && dev->type->pm)
1455 cb = dev->type->pm->runtime_resume;
1456 else if (dev->class && dev->class->pm)
1457 cb = dev->class->pm->runtime_resume;
1458 else if (dev->bus && dev->bus->pm)
1459 cb = dev->bus->pm->runtime_resume;
1463 if (!cb && dev->driver && dev->driver->pm)
1464 cb = dev->driver->pm->runtime_resume;
1466 return cb ? cb(dev) : 0;
1470 * pm_genpd_init - Initialize a generic I/O PM domain object.
1471 * @genpd: PM domain object to initialize.
1472 * @gov: PM domain governor to associate with the domain (may be NULL).
1473 * @is_off: Initial value of the domain's power_is_off field.
1475 void pm_genpd_init(struct generic_pm_domain *genpd,
1476 struct dev_power_governor *gov, bool is_off)
1478 if (IS_ERR_OR_NULL(genpd))
1481 INIT_LIST_HEAD(&genpd->master_links);
1482 INIT_LIST_HEAD(&genpd->slave_links);
1483 INIT_LIST_HEAD(&genpd->dev_list);
1484 mutex_init(&genpd->lock);
1486 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1487 atomic_set(&genpd->sd_count, 0);
1488 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1489 genpd->device_count = 0;
1490 genpd->max_off_time_ns = -1;
1491 genpd->max_off_time_changed = true;
1492 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1493 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1494 genpd->domain.ops.prepare = pm_genpd_prepare;
1495 genpd->domain.ops.suspend = pm_genpd_suspend;
1496 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1497 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1498 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1499 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1500 genpd->domain.ops.resume = pm_genpd_resume;
1501 genpd->domain.ops.freeze = pm_genpd_freeze;
1502 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1503 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1504 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1505 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1506 genpd->domain.ops.thaw = pm_genpd_thaw;
1507 genpd->domain.ops.poweroff = pm_genpd_suspend;
1508 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1509 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1510 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1511 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1512 genpd->domain.ops.restore = pm_genpd_resume;
1513 genpd->domain.ops.complete = pm_genpd_complete;
1514 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1515 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1517 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1518 genpd->dev_ops.stop = pm_clk_suspend;
1519 genpd->dev_ops.start = pm_clk_resume;
1522 mutex_lock(&gpd_list_lock);
1523 list_add(&genpd->gpd_list_node, &gpd_list);
1524 mutex_unlock(&gpd_list_lock);
1526 EXPORT_SYMBOL_GPL(pm_genpd_init);
1528 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1530 * Device Tree based PM domain providers.
1532 * The code below implements generic device tree based PM domain providers that
1533 * bind device tree nodes with generic PM domains registered in the system.
1535 * Any driver that registers generic PM domains and needs to support binding of
1536 * devices to these domains is supposed to register a PM domain provider, which
1537 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1539 * Two simple mapping functions have been provided for convenience:
1540 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1541 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1546 * struct of_genpd_provider - PM domain provider registration structure
1547 * @link: Entry in global list of PM domain providers
1548 * @node: Pointer to device tree node of PM domain provider
1549 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1551 * @data: context pointer to be passed into @xlate callback
1553 struct of_genpd_provider {
1554 struct list_head link;
1555 struct device_node *node;
1556 genpd_xlate_t xlate;
1560 /* List of registered PM domain providers. */
1561 static LIST_HEAD(of_genpd_providers);
1562 /* Mutex to protect the list above. */
1563 static DEFINE_MUTEX(of_genpd_mutex);
1566 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1567 * @genpdspec: OF phandle args to map into a PM domain
1568 * @data: xlate function private data - pointer to struct generic_pm_domain
1570 * This is a generic xlate function that can be used to model PM domains that
1571 * have their own device tree nodes. The private data of xlate function needs
1572 * to be a valid pointer to struct generic_pm_domain.
1574 struct generic_pm_domain *__of_genpd_xlate_simple(
1575 struct of_phandle_args *genpdspec,
1578 if (genpdspec->args_count != 0)
1579 return ERR_PTR(-EINVAL);
1582 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1585 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1586 * @genpdspec: OF phandle args to map into a PM domain
1587 * @data: xlate function private data - pointer to struct genpd_onecell_data
1589 * This is a generic xlate function that can be used to model simple PM domain
1590 * controllers that have one device tree node and provide multiple PM domains.
1591 * A single cell is used as an index into an array of PM domains specified in
1592 * the genpd_onecell_data struct when registering the provider.
1594 struct generic_pm_domain *__of_genpd_xlate_onecell(
1595 struct of_phandle_args *genpdspec,
1598 struct genpd_onecell_data *genpd_data = data;
1599 unsigned int idx = genpdspec->args[0];
1601 if (genpdspec->args_count != 1)
1602 return ERR_PTR(-EINVAL);
1604 if (idx >= genpd_data->num_domains) {
1605 pr_err("%s: invalid domain index %u\n", __func__, idx);
1606 return ERR_PTR(-EINVAL);
1609 if (!genpd_data->domains[idx])
1610 return ERR_PTR(-ENOENT);
1612 return genpd_data->domains[idx];
1614 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1617 * __of_genpd_add_provider() - Register a PM domain provider for a node
1618 * @np: Device node pointer associated with the PM domain provider.
1619 * @xlate: Callback for decoding PM domain from phandle arguments.
1620 * @data: Context pointer for @xlate callback.
1622 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1625 struct of_genpd_provider *cp;
1627 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1631 cp->node = of_node_get(np);
1635 mutex_lock(&of_genpd_mutex);
1636 list_add(&cp->link, &of_genpd_providers);
1637 mutex_unlock(&of_genpd_mutex);
1638 pr_debug("Added domain provider from %s\n", np->full_name);
1642 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1645 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1646 * @np: Device node pointer associated with the PM domain provider
1648 void of_genpd_del_provider(struct device_node *np)
1650 struct of_genpd_provider *cp;
1652 mutex_lock(&of_genpd_mutex);
1653 list_for_each_entry(cp, &of_genpd_providers, link) {
1654 if (cp->node == np) {
1655 list_del(&cp->link);
1656 of_node_put(cp->node);
1661 mutex_unlock(&of_genpd_mutex);
1663 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1666 * of_genpd_get_from_provider() - Look-up PM domain
1667 * @genpdspec: OF phandle args to use for look-up
1669 * Looks for a PM domain provider under the node specified by @genpdspec and if
1670 * found, uses xlate function of the provider to map phandle args to a PM
1673 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1676 struct generic_pm_domain *of_genpd_get_from_provider(
1677 struct of_phandle_args *genpdspec)
1679 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1680 struct of_genpd_provider *provider;
1682 mutex_lock(&of_genpd_mutex);
1684 /* Check if we have such a provider in our array */
1685 list_for_each_entry(provider, &of_genpd_providers, link) {
1686 if (provider->node == genpdspec->np)
1687 genpd = provider->xlate(genpdspec, provider->data);
1692 mutex_unlock(&of_genpd_mutex);
1696 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1699 * genpd_dev_pm_detach - Detach a device from its PM domain.
1700 * @dev: Device to detach.
1701 * @power_off: Currently not used
1703 * Try to locate a corresponding generic PM domain, which the device was
1704 * attached to previously. If such is found, the device is detached from it.
1706 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1708 struct generic_pm_domain *pd;
1712 pd = pm_genpd_lookup_dev(dev);
1716 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1718 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1719 ret = pm_genpd_remove_device(pd, dev);
1728 dev_err(dev, "failed to remove from PM domain %s: %d",
1733 /* Check if PM domain can be powered off after removing this device. */
1734 genpd_queue_power_off_work(pd);
1737 static void genpd_dev_pm_sync(struct device *dev)
1739 struct generic_pm_domain *pd;
1741 pd = dev_to_genpd(dev);
1745 genpd_queue_power_off_work(pd);
1749 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1750 * @dev: Device to attach.
1752 * Parse device's OF node to find a PM domain specifier. If such is found,
1753 * attaches the device to retrieved pm_domain ops.
1755 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1756 * backwards compatibility with existing DTBs.
1758 * Returns 0 on successfully attached PM domain or negative error code. Note
1759 * that if a power-domain exists for the device, but it cannot be found or
1760 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1761 * probed and to re-try again later.
1763 int genpd_dev_pm_attach(struct device *dev)
1765 struct of_phandle_args pd_args;
1766 struct generic_pm_domain *pd;
1776 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1777 "#power-domain-cells", 0, &pd_args);
1783 * Try legacy Samsung-specific bindings
1784 * (for backwards compatibility of DT ABI)
1786 pd_args.args_count = 0;
1787 pd_args.np = of_parse_phandle(dev->of_node,
1788 "samsung,power-domain", 0);
1793 pd = of_genpd_get_from_provider(&pd_args);
1794 of_node_put(pd_args.np);
1796 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1797 __func__, PTR_ERR(pd));
1798 return -EPROBE_DEFER;
1801 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1803 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1804 ret = pm_genpd_add_device(pd, dev);
1813 dev_err(dev, "failed to add to PM domain %s: %d",
1818 dev->pm_domain->detach = genpd_dev_pm_detach;
1819 dev->pm_domain->sync = genpd_dev_pm_sync;
1820 ret = genpd_poweron(pd);
1823 return ret ? -EPROBE_DEFER : 0;
1825 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1826 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1829 /*** debugfs support ***/
1831 #ifdef CONFIG_PM_ADVANCED_DEBUG
1832 #include <linux/pm.h>
1833 #include <linux/device.h>
1834 #include <linux/debugfs.h>
1835 #include <linux/seq_file.h>
1836 #include <linux/init.h>
1837 #include <linux/kobject.h>
1838 static struct dentry *pm_genpd_debugfs_dir;
1841 * TODO: This function is a slightly modified version of rtpm_status_show
1842 * from sysfs.c, so generalize it.
1844 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1846 static const char * const status_lookup[] = {
1847 [RPM_ACTIVE] = "active",
1848 [RPM_RESUMING] = "resuming",
1849 [RPM_SUSPENDED] = "suspended",
1850 [RPM_SUSPENDING] = "suspending"
1854 if (dev->power.runtime_error)
1856 else if (dev->power.disable_depth)
1858 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1859 p = status_lookup[dev->power.runtime_status];
1866 static int pm_genpd_summary_one(struct seq_file *s,
1867 struct generic_pm_domain *genpd)
1869 static const char * const status_lookup[] = {
1870 [GPD_STATE_ACTIVE] = "on",
1871 [GPD_STATE_POWER_OFF] = "off"
1873 struct pm_domain_data *pm_data;
1874 const char *kobj_path;
1875 struct gpd_link *link;
1878 ret = mutex_lock_interruptible(&genpd->lock);
1880 return -ERESTARTSYS;
1882 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1884 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
1887 * Modifications on the list require holding locks on both
1888 * master and slave, so we are safe.
1889 * Also genpd->name is immutable.
1891 list_for_each_entry(link, &genpd->master_links, master_node) {
1892 seq_printf(s, "%s", link->slave->name);
1893 if (!list_is_last(&link->master_node, &genpd->master_links))
1897 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1898 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1899 if (kobj_path == NULL)
1902 seq_printf(s, "\n %-50s ", kobj_path);
1903 rtpm_status_str(s, pm_data->dev);
1909 mutex_unlock(&genpd->lock);
1914 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1916 struct generic_pm_domain *genpd;
1919 seq_puts(s, "domain status slaves\n");
1920 seq_puts(s, " /device runtime status\n");
1921 seq_puts(s, "----------------------------------------------------------------------\n");
1923 ret = mutex_lock_interruptible(&gpd_list_lock);
1925 return -ERESTARTSYS;
1927 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1928 ret = pm_genpd_summary_one(s, genpd);
1932 mutex_unlock(&gpd_list_lock);
1937 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1939 return single_open(file, pm_genpd_summary_show, NULL);
1942 static const struct file_operations pm_genpd_summary_fops = {
1943 .open = pm_genpd_summary_open,
1945 .llseek = seq_lseek,
1946 .release = single_release,
1949 static int __init pm_genpd_debug_init(void)
1953 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1955 if (!pm_genpd_debugfs_dir)
1958 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1959 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1965 late_initcall(pm_genpd_debug_init);
1967 static void __exit pm_genpd_debug_exit(void)
1969 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1971 __exitcall(pm_genpd_debug_exit);
1972 #endif /* CONFIG_PM_ADVANCED_DEBUG */