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 domait 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);
173 static int genpd_poweron(struct generic_pm_domain *genpd);
176 * __genpd_poweron - Restore power to a given PM domain and its masters.
177 * @genpd: PM domain to power up.
179 * Restore power to @genpd and all of its masters so that it is possible to
180 * resume a device belonging to it.
182 static int __genpd_poweron(struct generic_pm_domain *genpd)
184 struct gpd_link *link;
187 if (genpd->status == GPD_STATE_ACTIVE
188 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
192 * The list is guaranteed not to change while the loop below is being
193 * executed, unless one of the masters' .power_on() callbacks fiddles
196 list_for_each_entry(link, &genpd->slave_links, slave_node) {
197 genpd_sd_counter_inc(link->master);
199 ret = genpd_poweron(link->master);
201 genpd_sd_counter_dec(link->master);
206 ret = genpd_power_on(genpd, true);
210 genpd->status = GPD_STATE_ACTIVE;
214 list_for_each_entry_continue_reverse(link,
217 genpd_sd_counter_dec(link->master);
218 genpd_queue_power_off_work(link->master);
225 * genpd_poweron - Restore power to a given PM domain and its masters.
226 * @genpd: PM domain to power up.
228 static int genpd_poweron(struct generic_pm_domain *genpd)
232 mutex_lock(&genpd->lock);
233 ret = __genpd_poweron(genpd);
234 mutex_unlock(&genpd->lock);
238 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
240 return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
243 static int genpd_restore_dev(struct generic_pm_domain *genpd,
246 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
249 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
250 unsigned long val, void *ptr)
252 struct generic_pm_domain_data *gpd_data;
255 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
256 dev = gpd_data->base.dev;
259 struct generic_pm_domain *genpd;
260 struct pm_domain_data *pdd;
262 spin_lock_irq(&dev->power.lock);
264 pdd = dev->power.subsys_data ?
265 dev->power.subsys_data->domain_data : NULL;
266 if (pdd && pdd->dev) {
267 to_gpd_data(pdd)->td.constraint_changed = true;
268 genpd = dev_to_genpd(dev);
270 genpd = ERR_PTR(-ENODATA);
273 spin_unlock_irq(&dev->power.lock);
275 if (!IS_ERR(genpd)) {
276 mutex_lock(&genpd->lock);
277 genpd->max_off_time_changed = true;
278 mutex_unlock(&genpd->lock);
282 if (!dev || dev->power.ignore_children)
290 * genpd_poweroff - Remove power from a given PM domain.
291 * @genpd: PM domain to power down.
292 * @is_async: PM domain is powered down from a scheduled work
294 * If all of the @genpd's devices have been suspended and all of its subdomains
295 * have been powered down, remove power from @genpd.
297 static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
299 struct pm_domain_data *pdd;
300 struct gpd_link *link;
301 unsigned int not_suspended = 0;
304 * Do not try to power off the domain in the following situations:
305 * (1) The domain is already in the "power off" state.
306 * (2) System suspend is in progress.
308 if (genpd->status == GPD_STATE_POWER_OFF
309 || genpd->prepared_count > 0)
312 if (atomic_read(&genpd->sd_count) > 0)
315 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
316 enum pm_qos_flags_status stat;
318 stat = dev_pm_qos_flags(pdd->dev,
319 PM_QOS_FLAG_NO_POWER_OFF
320 | PM_QOS_FLAG_REMOTE_WAKEUP);
321 if (stat > PM_QOS_FLAGS_NONE)
324 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
328 if (not_suspended > 1 || (not_suspended == 1 && is_async))
331 if (genpd->gov && genpd->gov->power_down_ok) {
332 if (!genpd->gov->power_down_ok(&genpd->domain))
336 if (genpd->power_off) {
339 if (atomic_read(&genpd->sd_count) > 0)
343 * If sd_count > 0 at this point, one of the subdomains hasn't
344 * managed to call genpd_poweron() for the master yet after
345 * incrementing it. In that case genpd_poweron() will wait
346 * for us to drop the lock, so we can call .power_off() and let
347 * the genpd_poweron() restore power for us (this shouldn't
348 * happen very often).
350 ret = genpd_power_off(genpd, true);
355 genpd->status = GPD_STATE_POWER_OFF;
357 list_for_each_entry(link, &genpd->slave_links, slave_node) {
358 genpd_sd_counter_dec(link->master);
359 genpd_queue_power_off_work(link->master);
366 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
367 * @work: Work structure used for scheduling the execution of this function.
369 static void genpd_power_off_work_fn(struct work_struct *work)
371 struct generic_pm_domain *genpd;
373 genpd = container_of(work, struct generic_pm_domain, power_off_work);
375 mutex_lock(&genpd->lock);
376 genpd_poweroff(genpd, true);
377 mutex_unlock(&genpd->lock);
381 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
382 * @dev: Device to suspend.
384 * Carry out a runtime suspend of a device under the assumption that its
385 * pm_domain field points to the domain member of an object of type
386 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
388 static int pm_genpd_runtime_suspend(struct device *dev)
390 struct generic_pm_domain *genpd;
391 bool (*stop_ok)(struct device *__dev);
392 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
397 dev_dbg(dev, "%s()\n", __func__);
399 genpd = dev_to_genpd(dev);
403 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
404 if (stop_ok && !stop_ok(dev))
407 /* 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. */
421 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
422 if (elapsed_ns > td->suspend_latency_ns) {
423 td->suspend_latency_ns = elapsed_ns;
424 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
426 genpd->max_off_time_changed = true;
427 td->constraint_changed = true;
431 * If power.irq_safe is set, this routine will be run with interrupts
432 * off, so it can't use mutexes.
434 if (dev->power.irq_safe)
437 mutex_lock(&genpd->lock);
438 genpd_poweroff(genpd, false);
439 mutex_unlock(&genpd->lock);
445 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
446 * @dev: Device to resume.
448 * Carry out a runtime resume of a device under the assumption that its
449 * pm_domain field points to the domain member of an object of type
450 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
452 static int pm_genpd_runtime_resume(struct device *dev)
454 struct generic_pm_domain *genpd;
455 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
461 dev_dbg(dev, "%s()\n", __func__);
463 genpd = dev_to_genpd(dev);
467 /* If power.irq_safe, the PM domain is never powered off. */
468 if (dev->power.irq_safe) {
473 mutex_lock(&genpd->lock);
474 ret = __genpd_poweron(genpd);
475 mutex_unlock(&genpd->lock);
481 /* Measure resume latency. */
483 time_start = ktime_get();
485 genpd_start_dev(genpd, dev);
486 genpd_restore_dev(genpd, dev);
488 /* Update resume latency value if the measured time exceeds it. */
490 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
491 if (elapsed_ns > td->resume_latency_ns) {
492 td->resume_latency_ns = elapsed_ns;
493 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
495 genpd->max_off_time_changed = true;
496 td->constraint_changed = true;
503 static bool pd_ignore_unused;
504 static int __init pd_ignore_unused_setup(char *__unused)
506 pd_ignore_unused = true;
509 __setup("pd_ignore_unused", pd_ignore_unused_setup);
512 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
514 static int __init genpd_poweroff_unused(void)
516 struct generic_pm_domain *genpd;
518 if (pd_ignore_unused) {
519 pr_warn("genpd: Not disabling unused power domains\n");
523 mutex_lock(&gpd_list_lock);
525 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
526 genpd_queue_power_off_work(genpd);
528 mutex_unlock(&gpd_list_lock);
532 late_initcall(genpd_poweroff_unused);
534 #ifdef CONFIG_PM_SLEEP
537 * pm_genpd_present - Check if the given PM domain has been initialized.
538 * @genpd: PM domain to check.
540 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
542 const struct generic_pm_domain *gpd;
544 if (IS_ERR_OR_NULL(genpd))
547 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
554 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
557 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
561 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
562 * @genpd: PM domain to power off, if possible.
563 * @timed: True if latency measurements are allowed.
565 * Check if the given PM domain can be powered off (during system suspend or
566 * hibernation) and do that if so. Also, in that case propagate to its masters.
568 * This function is only called in "noirq" and "syscore" stages of system power
569 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
570 * executed sequentially, so it is guaranteed that it will never run twice in
573 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
576 struct gpd_link *link;
578 if (genpd->status == GPD_STATE_POWER_OFF)
581 if (genpd->suspended_count != genpd->device_count
582 || atomic_read(&genpd->sd_count) > 0)
585 genpd_power_off(genpd, timed);
587 genpd->status = GPD_STATE_POWER_OFF;
589 list_for_each_entry(link, &genpd->slave_links, slave_node) {
590 genpd_sd_counter_dec(link->master);
591 pm_genpd_sync_poweroff(link->master, timed);
596 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
597 * @genpd: PM domain to power on.
598 * @timed: True if latency measurements are allowed.
600 * This function is only called in "noirq" and "syscore" stages of system power
601 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
602 * executed sequentially, so it is guaranteed that it will never run twice in
605 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
608 struct gpd_link *link;
610 if (genpd->status == GPD_STATE_ACTIVE)
613 list_for_each_entry(link, &genpd->slave_links, slave_node) {
614 pm_genpd_sync_poweron(link->master, timed);
615 genpd_sd_counter_inc(link->master);
618 genpd_power_on(genpd, timed);
620 genpd->status = GPD_STATE_ACTIVE;
624 * resume_needed - Check whether to resume a device before system suspend.
625 * @dev: Device to check.
626 * @genpd: PM domain the device belongs to.
628 * There are two cases in which a device that can wake up the system from sleep
629 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
630 * to wake up the system and it has to remain active for this purpose while the
631 * system is in the sleep state and (2) if the device is not enabled to wake up
632 * the system from sleep states and it generally doesn't generate wakeup signals
633 * by itself (those signals are generated on its behalf by other parts of the
634 * system). In the latter case it may be necessary to reconfigure the device's
635 * wakeup settings during system suspend, because it may have been set up to
636 * signal remote wakeup from the system's working state as needed by runtime PM.
637 * Return 'true' in either of the above cases.
639 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
643 if (!device_can_wakeup(dev))
646 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
647 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
651 * pm_genpd_prepare - Start power transition of a device in a PM domain.
652 * @dev: Device to start the transition of.
654 * Start a power transition of a device (during a system-wide power transition)
655 * under the assumption that its pm_domain field points to the domain member of
656 * an object of type struct generic_pm_domain representing a PM domain
657 * consisting of I/O devices.
659 static int pm_genpd_prepare(struct device *dev)
661 struct generic_pm_domain *genpd;
664 dev_dbg(dev, "%s()\n", __func__);
666 genpd = dev_to_genpd(dev);
671 * If a wakeup request is pending for the device, it should be woken up
672 * at this point and a system wakeup event should be reported if it's
673 * set up to wake up the system from sleep states.
675 pm_runtime_get_noresume(dev);
676 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
677 pm_wakeup_event(dev, 0);
679 if (pm_wakeup_pending()) {
684 if (resume_needed(dev, genpd))
685 pm_runtime_resume(dev);
687 mutex_lock(&genpd->lock);
689 if (genpd->prepared_count++ == 0) {
690 genpd->suspended_count = 0;
691 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
694 mutex_unlock(&genpd->lock);
696 if (genpd->suspend_power_off) {
697 pm_runtime_put_noidle(dev);
702 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
703 * so genpd_poweron() will return immediately, but if the device
704 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
705 * to make it operational.
707 pm_runtime_resume(dev);
708 __pm_runtime_disable(dev, false);
710 ret = pm_generic_prepare(dev);
712 mutex_lock(&genpd->lock);
714 if (--genpd->prepared_count == 0)
715 genpd->suspend_power_off = false;
717 mutex_unlock(&genpd->lock);
718 pm_runtime_enable(dev);
726 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
727 * @dev: Device to suspend.
729 * Suspend a device under the assumption that its pm_domain field points to the
730 * domain member of an object of type struct generic_pm_domain representing
731 * a PM domain consisting of I/O devices.
733 static int pm_genpd_suspend(struct device *dev)
735 struct generic_pm_domain *genpd;
737 dev_dbg(dev, "%s()\n", __func__);
739 genpd = dev_to_genpd(dev);
743 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
747 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
748 * @dev: Device to suspend.
750 * Carry out a late suspend of a device under the assumption that its
751 * pm_domain field points to the domain member of an object of type
752 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
754 static int pm_genpd_suspend_late(struct device *dev)
756 struct generic_pm_domain *genpd;
758 dev_dbg(dev, "%s()\n", __func__);
760 genpd = dev_to_genpd(dev);
764 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
768 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
769 * @dev: Device to suspend.
771 * Stop the device and remove power from the domain if all devices in it have
774 static int pm_genpd_suspend_noirq(struct device *dev)
776 struct generic_pm_domain *genpd;
778 dev_dbg(dev, "%s()\n", __func__);
780 genpd = dev_to_genpd(dev);
784 if (genpd->suspend_power_off
785 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
788 genpd_stop_dev(genpd, dev);
791 * Since all of the "noirq" callbacks are executed sequentially, it is
792 * guaranteed that this function will never run twice in parallel for
793 * the same PM domain, so it is not necessary to use locking here.
795 genpd->suspended_count++;
796 pm_genpd_sync_poweroff(genpd, true);
802 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
803 * @dev: Device to resume.
805 * Restore power to the device's PM domain, if necessary, and start the device.
807 static int pm_genpd_resume_noirq(struct device *dev)
809 struct generic_pm_domain *genpd;
811 dev_dbg(dev, "%s()\n", __func__);
813 genpd = dev_to_genpd(dev);
817 if (genpd->suspend_power_off
818 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
822 * Since all of the "noirq" callbacks are executed sequentially, it is
823 * guaranteed that this function will never run twice in parallel for
824 * the same PM domain, so it is not necessary to use locking here.
826 pm_genpd_sync_poweron(genpd, true);
827 genpd->suspended_count--;
829 return genpd_start_dev(genpd, dev);
833 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
834 * @dev: Device to resume.
836 * Carry out an early resume of a device under the assumption that its
837 * pm_domain field points to the domain member of an object of type
838 * struct generic_pm_domain representing a power domain consisting of I/O
841 static int pm_genpd_resume_early(struct device *dev)
843 struct generic_pm_domain *genpd;
845 dev_dbg(dev, "%s()\n", __func__);
847 genpd = dev_to_genpd(dev);
851 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
855 * pm_genpd_resume - Resume of device in an I/O PM domain.
856 * @dev: Device to resume.
858 * Resume a device under the assumption that its pm_domain field points to the
859 * domain member of an object of type struct generic_pm_domain representing
860 * a power domain consisting of I/O devices.
862 static int pm_genpd_resume(struct device *dev)
864 struct generic_pm_domain *genpd;
866 dev_dbg(dev, "%s()\n", __func__);
868 genpd = dev_to_genpd(dev);
872 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
876 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
877 * @dev: Device to freeze.
879 * Freeze a device under the assumption that its pm_domain field points to the
880 * domain member of an object of type struct generic_pm_domain representing
881 * a power domain consisting of I/O devices.
883 static int pm_genpd_freeze(struct device *dev)
885 struct generic_pm_domain *genpd;
887 dev_dbg(dev, "%s()\n", __func__);
889 genpd = dev_to_genpd(dev);
893 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
897 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
898 * @dev: Device to freeze.
900 * Carry out a late freeze of a device under the assumption that its
901 * pm_domain field points to the domain member of an object of type
902 * struct generic_pm_domain representing a power domain consisting of I/O
905 static int pm_genpd_freeze_late(struct device *dev)
907 struct generic_pm_domain *genpd;
909 dev_dbg(dev, "%s()\n", __func__);
911 genpd = dev_to_genpd(dev);
915 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
919 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
920 * @dev: Device to freeze.
922 * Carry out a late freeze of a device under the assumption that its
923 * pm_domain field points to the domain member of an object of type
924 * struct generic_pm_domain representing a power domain consisting of I/O
927 static int pm_genpd_freeze_noirq(struct device *dev)
929 struct generic_pm_domain *genpd;
931 dev_dbg(dev, "%s()\n", __func__);
933 genpd = dev_to_genpd(dev);
937 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
941 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
942 * @dev: Device to thaw.
944 * Start the device, unless power has been removed from the domain already
945 * before the system transition.
947 static int pm_genpd_thaw_noirq(struct device *dev)
949 struct generic_pm_domain *genpd;
951 dev_dbg(dev, "%s()\n", __func__);
953 genpd = dev_to_genpd(dev);
957 return genpd->suspend_power_off ?
958 0 : genpd_start_dev(genpd, dev);
962 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
963 * @dev: Device to thaw.
965 * Carry out an early thaw of a device under the assumption that its
966 * pm_domain field points to the domain member of an object of type
967 * struct generic_pm_domain representing a power domain consisting of I/O
970 static int pm_genpd_thaw_early(struct device *dev)
972 struct generic_pm_domain *genpd;
974 dev_dbg(dev, "%s()\n", __func__);
976 genpd = dev_to_genpd(dev);
980 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
984 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
985 * @dev: Device to thaw.
987 * Thaw a device under the assumption that its pm_domain field points to the
988 * domain member of an object of type struct generic_pm_domain representing
989 * a power domain consisting of I/O devices.
991 static int pm_genpd_thaw(struct device *dev)
993 struct generic_pm_domain *genpd;
995 dev_dbg(dev, "%s()\n", __func__);
997 genpd = dev_to_genpd(dev);
1001 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
1005 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1006 * @dev: Device to resume.
1008 * Make sure the domain will be in the same power state as before the
1009 * hibernation the system is resuming from and start the device if necessary.
1011 static int pm_genpd_restore_noirq(struct device *dev)
1013 struct generic_pm_domain *genpd;
1015 dev_dbg(dev, "%s()\n", __func__);
1017 genpd = dev_to_genpd(dev);
1022 * Since all of the "noirq" callbacks are executed sequentially, it is
1023 * guaranteed that this function will never run twice in parallel for
1024 * the same PM domain, so it is not necessary to use locking here.
1026 * At this point suspended_count == 0 means we are being run for the
1027 * first time for the given domain in the present cycle.
1029 if (genpd->suspended_count++ == 0) {
1031 * The boot kernel might put the domain into arbitrary state,
1032 * so make it appear as powered off to pm_genpd_sync_poweron(),
1033 * so that it tries to power it on in case it was really off.
1035 genpd->status = GPD_STATE_POWER_OFF;
1036 if (genpd->suspend_power_off) {
1038 * If the domain was off before the hibernation, make
1039 * sure it will be off going forward.
1041 genpd_power_off(genpd, true);
1047 if (genpd->suspend_power_off)
1050 pm_genpd_sync_poweron(genpd, true);
1052 return genpd_start_dev(genpd, dev);
1056 * pm_genpd_complete - Complete power transition of a device in a power domain.
1057 * @dev: Device to complete the transition of.
1059 * Complete a power transition of a device (during a system-wide power
1060 * transition) under the assumption that its pm_domain field points to the
1061 * domain member of an object of type struct generic_pm_domain representing
1062 * a power domain consisting of I/O devices.
1064 static void pm_genpd_complete(struct device *dev)
1066 struct generic_pm_domain *genpd;
1069 dev_dbg(dev, "%s()\n", __func__);
1071 genpd = dev_to_genpd(dev);
1075 mutex_lock(&genpd->lock);
1077 run_complete = !genpd->suspend_power_off;
1078 if (--genpd->prepared_count == 0)
1079 genpd->suspend_power_off = false;
1081 mutex_unlock(&genpd->lock);
1084 pm_generic_complete(dev);
1085 pm_runtime_set_active(dev);
1086 pm_runtime_enable(dev);
1087 pm_request_idle(dev);
1092 * genpd_syscore_switch - Switch power during system core suspend or resume.
1093 * @dev: Device that normally is marked as "always on" to switch power for.
1095 * This routine may only be called during the system core (syscore) suspend or
1096 * resume phase for devices whose "always on" flags are set.
1098 static void genpd_syscore_switch(struct device *dev, bool suspend)
1100 struct generic_pm_domain *genpd;
1102 genpd = dev_to_genpd(dev);
1103 if (!pm_genpd_present(genpd))
1107 genpd->suspended_count++;
1108 pm_genpd_sync_poweroff(genpd, false);
1110 pm_genpd_sync_poweron(genpd, false);
1111 genpd->suspended_count--;
1115 void pm_genpd_syscore_poweroff(struct device *dev)
1117 genpd_syscore_switch(dev, true);
1119 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1121 void pm_genpd_syscore_poweron(struct device *dev)
1123 genpd_syscore_switch(dev, false);
1125 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1127 #else /* !CONFIG_PM_SLEEP */
1129 #define pm_genpd_prepare NULL
1130 #define pm_genpd_suspend NULL
1131 #define pm_genpd_suspend_late NULL
1132 #define pm_genpd_suspend_noirq NULL
1133 #define pm_genpd_resume_early NULL
1134 #define pm_genpd_resume_noirq NULL
1135 #define pm_genpd_resume NULL
1136 #define pm_genpd_freeze NULL
1137 #define pm_genpd_freeze_late NULL
1138 #define pm_genpd_freeze_noirq NULL
1139 #define pm_genpd_thaw_early NULL
1140 #define pm_genpd_thaw_noirq NULL
1141 #define pm_genpd_thaw NULL
1142 #define pm_genpd_restore_noirq NULL
1143 #define pm_genpd_complete NULL
1145 #endif /* CONFIG_PM_SLEEP */
1147 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1148 struct generic_pm_domain *genpd,
1149 struct gpd_timing_data *td)
1151 struct generic_pm_domain_data *gpd_data;
1154 ret = dev_pm_get_subsys_data(dev);
1156 return ERR_PTR(ret);
1158 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1167 gpd_data->base.dev = dev;
1168 gpd_data->td.constraint_changed = true;
1169 gpd_data->td.effective_constraint_ns = -1;
1170 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1172 spin_lock_irq(&dev->power.lock);
1174 if (dev->power.subsys_data->domain_data) {
1179 dev->power.subsys_data->domain_data = &gpd_data->base;
1180 dev->pm_domain = &genpd->domain;
1182 spin_unlock_irq(&dev->power.lock);
1187 spin_unlock_irq(&dev->power.lock);
1190 dev_pm_put_subsys_data(dev);
1191 return ERR_PTR(ret);
1194 static void genpd_free_dev_data(struct device *dev,
1195 struct generic_pm_domain_data *gpd_data)
1197 spin_lock_irq(&dev->power.lock);
1199 dev->pm_domain = NULL;
1200 dev->power.subsys_data->domain_data = NULL;
1202 spin_unlock_irq(&dev->power.lock);
1205 dev_pm_put_subsys_data(dev);
1209 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1210 * @genpd: PM domain to add the device to.
1211 * @dev: Device to be added.
1212 * @td: Set of PM QoS timing parameters to attach to the device.
1214 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1215 struct gpd_timing_data *td)
1217 struct generic_pm_domain_data *gpd_data;
1220 dev_dbg(dev, "%s()\n", __func__);
1222 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1225 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1226 if (IS_ERR(gpd_data))
1227 return PTR_ERR(gpd_data);
1229 mutex_lock(&genpd->lock);
1231 if (genpd->prepared_count > 0) {
1236 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1240 genpd->device_count++;
1241 genpd->max_off_time_changed = true;
1243 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1246 mutex_unlock(&genpd->lock);
1249 genpd_free_dev_data(dev, gpd_data);
1251 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1257 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1258 * @genpd: PM domain to remove the device from.
1259 * @dev: Device to be removed.
1261 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1264 struct generic_pm_domain_data *gpd_data;
1265 struct pm_domain_data *pdd;
1268 dev_dbg(dev, "%s()\n", __func__);
1270 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1273 /* The above validation also means we have existing domain_data. */
1274 pdd = dev->power.subsys_data->domain_data;
1275 gpd_data = to_gpd_data(pdd);
1276 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1278 mutex_lock(&genpd->lock);
1280 if (genpd->prepared_count > 0) {
1285 genpd->device_count--;
1286 genpd->max_off_time_changed = true;
1288 if (genpd->detach_dev)
1289 genpd->detach_dev(genpd, dev);
1291 list_del_init(&pdd->list_node);
1293 mutex_unlock(&genpd->lock);
1295 genpd_free_dev_data(dev, gpd_data);
1300 mutex_unlock(&genpd->lock);
1301 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1307 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1308 * @genpd: Master PM domain to add the subdomain to.
1309 * @subdomain: Subdomain to be added.
1311 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1312 struct generic_pm_domain *subdomain)
1314 struct gpd_link *link, *itr;
1317 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1318 || genpd == subdomain)
1321 link = kzalloc(sizeof(*link), GFP_KERNEL);
1325 mutex_lock(&genpd->lock);
1326 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1328 if (genpd->status == GPD_STATE_POWER_OFF
1329 && subdomain->status != GPD_STATE_POWER_OFF) {
1334 list_for_each_entry(itr, &genpd->master_links, master_node) {
1335 if (itr->slave == subdomain && itr->master == genpd) {
1341 link->master = genpd;
1342 list_add_tail(&link->master_node, &genpd->master_links);
1343 link->slave = subdomain;
1344 list_add_tail(&link->slave_node, &subdomain->slave_links);
1345 if (subdomain->status != GPD_STATE_POWER_OFF)
1346 genpd_sd_counter_inc(genpd);
1349 mutex_unlock(&subdomain->lock);
1350 mutex_unlock(&genpd->lock);
1357 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1358 * @genpd: Master PM domain to remove the subdomain from.
1359 * @subdomain: Subdomain to be removed.
1361 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1362 struct generic_pm_domain *subdomain)
1364 struct gpd_link *link;
1367 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1370 mutex_lock(&genpd->lock);
1372 if (!list_empty(&subdomain->slave_links) || subdomain->device_count) {
1373 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1379 list_for_each_entry(link, &genpd->master_links, master_node) {
1380 if (link->slave != subdomain)
1383 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1385 list_del(&link->master_node);
1386 list_del(&link->slave_node);
1388 if (subdomain->status != GPD_STATE_POWER_OFF)
1389 genpd_sd_counter_dec(genpd);
1391 mutex_unlock(&subdomain->lock);
1398 mutex_unlock(&genpd->lock);
1403 /* Default device callbacks for generic PM domains. */
1406 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1407 * @dev: Device to handle.
1409 static int pm_genpd_default_save_state(struct device *dev)
1411 int (*cb)(struct device *__dev);
1413 if (dev->type && dev->type->pm)
1414 cb = dev->type->pm->runtime_suspend;
1415 else if (dev->class && dev->class->pm)
1416 cb = dev->class->pm->runtime_suspend;
1417 else if (dev->bus && dev->bus->pm)
1418 cb = dev->bus->pm->runtime_suspend;
1422 if (!cb && dev->driver && dev->driver->pm)
1423 cb = dev->driver->pm->runtime_suspend;
1425 return cb ? cb(dev) : 0;
1429 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1430 * @dev: Device to handle.
1432 static int pm_genpd_default_restore_state(struct device *dev)
1434 int (*cb)(struct device *__dev);
1436 if (dev->type && dev->type->pm)
1437 cb = dev->type->pm->runtime_resume;
1438 else if (dev->class && dev->class->pm)
1439 cb = dev->class->pm->runtime_resume;
1440 else if (dev->bus && dev->bus->pm)
1441 cb = dev->bus->pm->runtime_resume;
1445 if (!cb && dev->driver && dev->driver->pm)
1446 cb = dev->driver->pm->runtime_resume;
1448 return cb ? cb(dev) : 0;
1452 * pm_genpd_init - Initialize a generic I/O PM domain object.
1453 * @genpd: PM domain object to initialize.
1454 * @gov: PM domain governor to associate with the domain (may be NULL).
1455 * @is_off: Initial value of the domain's power_is_off field.
1457 void pm_genpd_init(struct generic_pm_domain *genpd,
1458 struct dev_power_governor *gov, bool is_off)
1460 if (IS_ERR_OR_NULL(genpd))
1463 INIT_LIST_HEAD(&genpd->master_links);
1464 INIT_LIST_HEAD(&genpd->slave_links);
1465 INIT_LIST_HEAD(&genpd->dev_list);
1466 mutex_init(&genpd->lock);
1468 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1469 atomic_set(&genpd->sd_count, 0);
1470 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1471 genpd->device_count = 0;
1472 genpd->max_off_time_ns = -1;
1473 genpd->max_off_time_changed = true;
1474 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1475 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1476 genpd->domain.ops.prepare = pm_genpd_prepare;
1477 genpd->domain.ops.suspend = pm_genpd_suspend;
1478 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1479 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1480 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1481 genpd->domain.ops.resume_early = pm_genpd_resume_early;
1482 genpd->domain.ops.resume = pm_genpd_resume;
1483 genpd->domain.ops.freeze = pm_genpd_freeze;
1484 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1485 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1486 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1487 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1488 genpd->domain.ops.thaw = pm_genpd_thaw;
1489 genpd->domain.ops.poweroff = pm_genpd_suspend;
1490 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1491 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1492 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1493 genpd->domain.ops.restore_early = pm_genpd_resume_early;
1494 genpd->domain.ops.restore = pm_genpd_resume;
1495 genpd->domain.ops.complete = pm_genpd_complete;
1496 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1497 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1499 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1500 genpd->dev_ops.stop = pm_clk_suspend;
1501 genpd->dev_ops.start = pm_clk_resume;
1504 mutex_lock(&gpd_list_lock);
1505 list_add(&genpd->gpd_list_node, &gpd_list);
1506 mutex_unlock(&gpd_list_lock);
1508 EXPORT_SYMBOL_GPL(pm_genpd_init);
1510 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1512 * Device Tree based PM domain providers.
1514 * The code below implements generic device tree based PM domain providers that
1515 * bind device tree nodes with generic PM domains registered in the system.
1517 * Any driver that registers generic PM domains and needs to support binding of
1518 * devices to these domains is supposed to register a PM domain provider, which
1519 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1521 * Two simple mapping functions have been provided for convenience:
1522 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1523 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1528 * struct of_genpd_provider - PM domain provider registration structure
1529 * @link: Entry in global list of PM domain providers
1530 * @node: Pointer to device tree node of PM domain provider
1531 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1533 * @data: context pointer to be passed into @xlate callback
1535 struct of_genpd_provider {
1536 struct list_head link;
1537 struct device_node *node;
1538 genpd_xlate_t xlate;
1542 /* List of registered PM domain providers. */
1543 static LIST_HEAD(of_genpd_providers);
1544 /* Mutex to protect the list above. */
1545 static DEFINE_MUTEX(of_genpd_mutex);
1548 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1549 * @genpdspec: OF phandle args to map into a PM domain
1550 * @data: xlate function private data - pointer to struct generic_pm_domain
1552 * This is a generic xlate function that can be used to model PM domains that
1553 * have their own device tree nodes. The private data of xlate function needs
1554 * to be a valid pointer to struct generic_pm_domain.
1556 struct generic_pm_domain *__of_genpd_xlate_simple(
1557 struct of_phandle_args *genpdspec,
1560 if (genpdspec->args_count != 0)
1561 return ERR_PTR(-EINVAL);
1564 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1567 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1568 * @genpdspec: OF phandle args to map into a PM domain
1569 * @data: xlate function private data - pointer to struct genpd_onecell_data
1571 * This is a generic xlate function that can be used to model simple PM domain
1572 * controllers that have one device tree node and provide multiple PM domains.
1573 * A single cell is used as an index into an array of PM domains specified in
1574 * the genpd_onecell_data struct when registering the provider.
1576 struct generic_pm_domain *__of_genpd_xlate_onecell(
1577 struct of_phandle_args *genpdspec,
1580 struct genpd_onecell_data *genpd_data = data;
1581 unsigned int idx = genpdspec->args[0];
1583 if (genpdspec->args_count != 1)
1584 return ERR_PTR(-EINVAL);
1586 if (idx >= genpd_data->num_domains) {
1587 pr_err("%s: invalid domain index %u\n", __func__, idx);
1588 return ERR_PTR(-EINVAL);
1591 if (!genpd_data->domains[idx])
1592 return ERR_PTR(-ENOENT);
1594 return genpd_data->domains[idx];
1596 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1599 * __of_genpd_add_provider() - Register a PM domain provider for a node
1600 * @np: Device node pointer associated with the PM domain provider.
1601 * @xlate: Callback for decoding PM domain from phandle arguments.
1602 * @data: Context pointer for @xlate callback.
1604 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1607 struct of_genpd_provider *cp;
1609 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1613 cp->node = of_node_get(np);
1617 mutex_lock(&of_genpd_mutex);
1618 list_add(&cp->link, &of_genpd_providers);
1619 mutex_unlock(&of_genpd_mutex);
1620 pr_debug("Added domain provider from %s\n", np->full_name);
1624 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1627 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1628 * @np: Device node pointer associated with the PM domain provider
1630 void of_genpd_del_provider(struct device_node *np)
1632 struct of_genpd_provider *cp;
1634 mutex_lock(&of_genpd_mutex);
1635 list_for_each_entry(cp, &of_genpd_providers, link) {
1636 if (cp->node == np) {
1637 list_del(&cp->link);
1638 of_node_put(cp->node);
1643 mutex_unlock(&of_genpd_mutex);
1645 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1648 * of_genpd_get_from_provider() - Look-up PM domain
1649 * @genpdspec: OF phandle args to use for look-up
1651 * Looks for a PM domain provider under the node specified by @genpdspec and if
1652 * found, uses xlate function of the provider to map phandle args to a PM
1655 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1658 struct generic_pm_domain *of_genpd_get_from_provider(
1659 struct of_phandle_args *genpdspec)
1661 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1662 struct of_genpd_provider *provider;
1664 mutex_lock(&of_genpd_mutex);
1666 /* Check if we have such a provider in our array */
1667 list_for_each_entry(provider, &of_genpd_providers, link) {
1668 if (provider->node == genpdspec->np)
1669 genpd = provider->xlate(genpdspec, provider->data);
1674 mutex_unlock(&of_genpd_mutex);
1678 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1681 * genpd_dev_pm_detach - Detach a device from its PM domain.
1682 * @dev: Device to detach.
1683 * @power_off: Currently not used
1685 * Try to locate a corresponding generic PM domain, which the device was
1686 * attached to previously. If such is found, the device is detached from it.
1688 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1690 struct generic_pm_domain *pd;
1694 pd = pm_genpd_lookup_dev(dev);
1698 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1700 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1701 ret = pm_genpd_remove_device(pd, dev);
1710 dev_err(dev, "failed to remove from PM domain %s: %d",
1715 /* Check if PM domain can be powered off after removing this device. */
1716 genpd_queue_power_off_work(pd);
1719 static void genpd_dev_pm_sync(struct device *dev)
1721 struct generic_pm_domain *pd;
1723 pd = dev_to_genpd(dev);
1727 genpd_queue_power_off_work(pd);
1731 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1732 * @dev: Device to attach.
1734 * Parse device's OF node to find a PM domain specifier. If such is found,
1735 * attaches the device to retrieved pm_domain ops.
1737 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1738 * backwards compatibility with existing DTBs.
1740 * Returns 0 on successfully attached PM domain or negative error code. Note
1741 * that if a power-domain exists for the device, but it cannot be found or
1742 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1743 * probed and to re-try again later.
1745 int genpd_dev_pm_attach(struct device *dev)
1747 struct of_phandle_args pd_args;
1748 struct generic_pm_domain *pd;
1758 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1759 "#power-domain-cells", 0, &pd_args);
1765 * Try legacy Samsung-specific bindings
1766 * (for backwards compatibility of DT ABI)
1768 pd_args.args_count = 0;
1769 pd_args.np = of_parse_phandle(dev->of_node,
1770 "samsung,power-domain", 0);
1775 pd = of_genpd_get_from_provider(&pd_args);
1777 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1778 __func__, PTR_ERR(pd));
1779 of_node_put(dev->of_node);
1780 return -EPROBE_DEFER;
1783 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1785 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1786 ret = pm_genpd_add_device(pd, dev);
1795 dev_err(dev, "failed to add to PM domain %s: %d",
1797 of_node_put(dev->of_node);
1801 dev->pm_domain->detach = genpd_dev_pm_detach;
1802 dev->pm_domain->sync = genpd_dev_pm_sync;
1803 ret = genpd_poweron(pd);
1806 return ret ? -EPROBE_DEFER : 0;
1808 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1809 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1812 /*** debugfs support ***/
1814 #ifdef CONFIG_PM_ADVANCED_DEBUG
1815 #include <linux/pm.h>
1816 #include <linux/device.h>
1817 #include <linux/debugfs.h>
1818 #include <linux/seq_file.h>
1819 #include <linux/init.h>
1820 #include <linux/kobject.h>
1821 static struct dentry *pm_genpd_debugfs_dir;
1824 * TODO: This function is a slightly modified version of rtpm_status_show
1825 * from sysfs.c, so generalize it.
1827 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1829 static const char * const status_lookup[] = {
1830 [RPM_ACTIVE] = "active",
1831 [RPM_RESUMING] = "resuming",
1832 [RPM_SUSPENDED] = "suspended",
1833 [RPM_SUSPENDING] = "suspending"
1837 if (dev->power.runtime_error)
1839 else if (dev->power.disable_depth)
1841 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1842 p = status_lookup[dev->power.runtime_status];
1849 static int pm_genpd_summary_one(struct seq_file *s,
1850 struct generic_pm_domain *genpd)
1852 static const char * const status_lookup[] = {
1853 [GPD_STATE_ACTIVE] = "on",
1854 [GPD_STATE_POWER_OFF] = "off"
1856 struct pm_domain_data *pm_data;
1857 const char *kobj_path;
1858 struct gpd_link *link;
1861 ret = mutex_lock_interruptible(&genpd->lock);
1863 return -ERESTARTSYS;
1865 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1867 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
1870 * Modifications on the list require holding locks on both
1871 * master and slave, so we are safe.
1872 * Also genpd->name is immutable.
1874 list_for_each_entry(link, &genpd->master_links, master_node) {
1875 seq_printf(s, "%s", link->slave->name);
1876 if (!list_is_last(&link->master_node, &genpd->master_links))
1880 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1881 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1882 if (kobj_path == NULL)
1885 seq_printf(s, "\n %-50s ", kobj_path);
1886 rtpm_status_str(s, pm_data->dev);
1892 mutex_unlock(&genpd->lock);
1897 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1899 struct generic_pm_domain *genpd;
1902 seq_puts(s, "domain status slaves\n");
1903 seq_puts(s, " /device runtime status\n");
1904 seq_puts(s, "----------------------------------------------------------------------\n");
1906 ret = mutex_lock_interruptible(&gpd_list_lock);
1908 return -ERESTARTSYS;
1910 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1911 ret = pm_genpd_summary_one(s, genpd);
1915 mutex_unlock(&gpd_list_lock);
1920 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1922 return single_open(file, pm_genpd_summary_show, NULL);
1925 static const struct file_operations pm_genpd_summary_fops = {
1926 .open = pm_genpd_summary_open,
1928 .llseek = seq_lseek,
1929 .release = single_release,
1932 static int __init pm_genpd_debug_init(void)
1936 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1938 if (!pm_genpd_debugfs_dir)
1941 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1942 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1948 late_initcall(pm_genpd_debug_init);
1950 static void __exit pm_genpd_debug_exit(void)
1952 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1954 __exitcall(pm_genpd_debug_exit);
1955 #endif /* CONFIG_PM_ADVANCED_DEBUG */