Lines Matching +full:power +full:- +full:source

1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/wakeup.c - System wakeup events framework
18 #include <trace/events/power.h>
20 #include "power.h"
46 #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
77 * wakeup_source_create - Create a struct wakeup_source object.
78 * @name: Name of the new wakeup source.
93 ws->name = ws_name; in wakeup_source_create()
98 ws->id = id; in wakeup_source_create()
103 kfree_const(ws->name); in wakeup_source_create()
120 if (ws->event_count) { in wakeup_source_record()
122 ktime_add(deleted_ws.total_time, ws->total_time); in wakeup_source_record()
125 ws->prevent_sleep_time); in wakeup_source_record()
127 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ? in wakeup_source_record()
128 deleted_ws.max_time : ws->max_time; in wakeup_source_record()
129 deleted_ws.event_count += ws->event_count; in wakeup_source_record()
130 deleted_ws.active_count += ws->active_count; in wakeup_source_record()
131 deleted_ws.relax_count += ws->relax_count; in wakeup_source_record()
132 deleted_ws.expire_count += ws->expire_count; in wakeup_source_record()
133 deleted_ws.wakeup_count += ws->wakeup_count; in wakeup_source_record()
141 ida_free(&wakeup_ida, ws->id); in wakeup_source_free()
142 kfree_const(ws->name); in wakeup_source_free()
147 * wakeup_source_destroy - Destroy a struct wakeup_source object.
148 * @ws: Wakeup source to destroy.
150 * Use only for wakeup source objects created with wakeup_source_create().
164 * wakeup_source_add - Add given object to the list of wakeup sources.
165 * @ws: Wakeup source object to add to the list.
174 spin_lock_init(&ws->lock); in wakeup_source_add()
175 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0); in wakeup_source_add()
176 ws->active = false; in wakeup_source_add()
179 list_add_rcu(&ws->entry, &wakeup_sources); in wakeup_source_add()
185 * wakeup_source_remove - Remove given object from the wakeup sources list.
186 * @ws: Wakeup source object to remove from the list.
196 list_del_rcu(&ws->entry); in wakeup_source_remove()
200 del_timer_sync(&ws->timer); in wakeup_source_remove()
203 * this wakeup source as not registered. in wakeup_source_remove()
205 ws->timer.function = NULL; in wakeup_source_remove()
210 * wakeup_source_register - Create wakeup source and add it to the list.
211 * @dev: Device this wakeup source is associated with (or NULL if virtual).
212 * @name: Name of the wakeup source to register.
236 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
237 * @ws: Wakeup source object to unregister.
243 if (ws->dev) in wakeup_source_unregister()
252 * wakeup_sources_read_lock - Lock wakeup source list for read.
264 * wakeup_sources_read_unlock - Unlock wakeup source list.
274 * wakeup_sources_walk_start - Begin a walk on wakeup source list
285 return list_entry_rcu(ws_head->next, struct wakeup_source, entry); in wakeup_sources_walk_start()
290 * wakeup_sources_walk_next - Get next wakeup source from the list
291 * @ws: Previous wakeup source object
300 return list_next_or_null_rcu(ws_head, &ws->entry, in wakeup_sources_walk_next()
306 * device_wakeup_attach - Attach a wakeup source object to a device object.
308 * @ws: Wakeup source object to attach to @dev.
314 spin_lock_irq(&dev->power.lock); in device_wakeup_attach()
315 if (dev->power.wakeup) { in device_wakeup_attach()
316 spin_unlock_irq(&dev->power.lock); in device_wakeup_attach()
317 return -EEXIST; in device_wakeup_attach()
319 dev->power.wakeup = ws; in device_wakeup_attach()
320 if (dev->power.wakeirq) in device_wakeup_attach()
321 device_wakeup_attach_irq(dev, dev->power.wakeirq); in device_wakeup_attach()
322 spin_unlock_irq(&dev->power.lock); in device_wakeup_attach()
327 * device_wakeup_enable - Enable given device to be a wakeup source.
330 * Create a wakeup source object, register it and attach it to @dev.
337 if (!dev || !dev->power.can_wakeup) in device_wakeup_enable()
338 return -EINVAL; in device_wakeup_enable()
345 return -ENOMEM; in device_wakeup_enable()
356 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
360 * Attach a device wakeirq to the wakeup source so the device
364 * Call under the device's power.lock lock.
371 ws = dev->power.wakeup; in device_wakeup_attach_irq()
375 if (ws->wakeirq) in device_wakeup_attach_irq()
378 ws->wakeirq = wakeirq; in device_wakeup_attach_irq()
382 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
385 * Removes a device wakeirq from the wakeup source.
387 * Call under the device's power.lock lock.
393 ws = dev->power.wakeup; in device_wakeup_detach_irq()
395 ws->wakeirq = NULL; in device_wakeup_detach_irq()
399 * device_wakeup_arm_wake_irqs -
410 dev_pm_arm_wake_irq(ws->wakeirq); in device_wakeup_arm_wake_irqs()
415 * device_wakeup_disarm_wake_irqs -
426 dev_pm_disarm_wake_irq(ws->wakeirq); in device_wakeup_disarm_wake_irqs()
431 * device_wakeup_detach - Detach a device's wakeup source object from it.
432 * @dev: Device to detach the wakeup source object from.
440 spin_lock_irq(&dev->power.lock); in device_wakeup_detach()
441 ws = dev->power.wakeup; in device_wakeup_detach()
442 dev->power.wakeup = NULL; in device_wakeup_detach()
443 spin_unlock_irq(&dev->power.lock); in device_wakeup_detach()
448 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
451 * Detach the @dev's wakeup source object from it, unregister this wakeup source
458 if (!dev || !dev->power.can_wakeup) in device_wakeup_disable()
467 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
471 * If @capable is set, set the @dev's power.can_wakeup flag and add its
472 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
473 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
480 if (!!dev->power.can_wakeup == !!capable) in device_set_wakeup_capable()
483 dev->power.can_wakeup = capable; in device_set_wakeup_capable()
484 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) { in device_set_wakeup_capable()
498 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
513 * wakeup_source_not_registered - validate the given wakeup source.
514 * @ws: Wakeup source to be validated.
519 * Use timer struct to check if the given source is initialized in wakeup_source_not_registered()
522 return ws->timer.function != pm_wakeup_timer_fn; in wakeup_source_not_registered()
554 * wakeup_source_activate - Mark given wakeup source as active.
555 * @ws: Wakeup source to handle.
566 "unregistered wakeup source\n")) in wakeup_source_activate()
569 ws->active = true; in wakeup_source_activate()
570 ws->active_count++; in wakeup_source_activate()
571 ws->last_time = ktime_get(); in wakeup_source_activate()
572 if (ws->autosleep_enabled) in wakeup_source_activate()
573 ws->start_prevent_time = ws->last_time; in wakeup_source_activate()
578 trace_wakeup_source_activate(ws->name, cec); in wakeup_source_activate()
582 * wakeup_source_report_event - Report wakeup event using the given source.
583 * @ws: Wakeup source to report the event for.
584 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
588 ws->event_count++; in wakeup_source_report_event()
591 ws->wakeup_count++; in wakeup_source_report_event()
593 if (!ws->active) in wakeup_source_report_event()
601 * __pm_stay_awake - Notify the PM core of a wakeup event.
602 * @ws: Wakeup source object associated with the source of the event.
613 spin_lock_irqsave(&ws->lock, flags); in __pm_stay_awake()
616 del_timer(&ws->timer); in __pm_stay_awake()
617 ws->timer_expires = 0; in __pm_stay_awake()
619 spin_unlock_irqrestore(&ws->lock, flags); in __pm_stay_awake()
624 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
628 * __pm_stay_awake for the @dev's wakeup source object.
641 spin_lock_irqsave(&dev->power.lock, flags); in pm_stay_awake()
642 __pm_stay_awake(dev->power.wakeup); in pm_stay_awake()
643 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_stay_awake()
650 ktime_t delta = ktime_sub(now, ws->start_prevent_time); in update_prevent_sleep_time()
651 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta); in update_prevent_sleep_time()
659 * wakeup_source_deactivate - Mark given wakeup source as inactive.
660 * @ws: Wakeup source to handle.
662 * Update the @ws' statistics and notify the PM core that the wakeup source has
672 ws->relax_count++; in wakeup_source_deactivate()
678 * will set ws->active. Then, ws->active may be cleared immediately in wakeup_source_deactivate()
680 * case ws->relax_count will be different from ws->active_count. in wakeup_source_deactivate()
682 if (ws->relax_count != ws->active_count) { in wakeup_source_deactivate()
683 ws->relax_count--; in wakeup_source_deactivate()
687 ws->active = false; in wakeup_source_deactivate()
690 duration = ktime_sub(now, ws->last_time); in wakeup_source_deactivate()
691 ws->total_time = ktime_add(ws->total_time, duration); in wakeup_source_deactivate()
692 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time)) in wakeup_source_deactivate()
693 ws->max_time = duration; in wakeup_source_deactivate()
695 ws->last_time = now; in wakeup_source_deactivate()
696 del_timer(&ws->timer); in wakeup_source_deactivate()
697 ws->timer_expires = 0; in wakeup_source_deactivate()
699 if (ws->autosleep_enabled) in wakeup_source_deactivate()
707 trace_wakeup_source_deactivate(ws->name, cec); in wakeup_source_deactivate()
715 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
716 * @ws: Wakeup source object associated with the source of the event.
730 spin_lock_irqsave(&ws->lock, flags); in __pm_relax()
731 if (ws->active) in __pm_relax()
733 spin_unlock_irqrestore(&ws->lock, flags); in __pm_relax()
738 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
741 * Execute __pm_relax() for the @dev's wakeup source object.
750 spin_lock_irqsave(&dev->power.lock, flags); in pm_relax()
751 __pm_relax(dev->power.wakeup); in pm_relax()
752 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_relax()
757 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
760 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
769 spin_lock_irqsave(&ws->lock, flags); in pm_wakeup_timer_fn()
771 if (ws->active && ws->timer_expires in pm_wakeup_timer_fn()
772 && time_after_eq(jiffies, ws->timer_expires)) { in pm_wakeup_timer_fn()
774 ws->expire_count++; in pm_wakeup_timer_fn()
777 spin_unlock_irqrestore(&ws->lock, flags); in pm_wakeup_timer_fn()
781 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
782 * @ws: Wakeup source object associated with the event source.
784 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
786 * Notify the PM core of a wakeup event whose source is @ws that will take
801 spin_lock_irqsave(&ws->lock, flags); in pm_wakeup_ws_event()
814 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) { in pm_wakeup_ws_event()
815 mod_timer(&ws->timer, expires); in pm_wakeup_ws_event()
816 ws->timer_expires = expires; in pm_wakeup_ws_event()
820 spin_unlock_irqrestore(&ws->lock, flags); in pm_wakeup_ws_event()
825 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
828 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
830 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
839 spin_lock_irqsave(&dev->power.lock, flags); in pm_wakeup_dev_event()
840 pm_wakeup_ws_event(dev->power.wakeup, msec, hard); in pm_wakeup_dev_event()
841 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_wakeup_dev_event()
853 if (ws->active) { in pm_print_active_wakeup_sources()
854 pm_pr_dbg("active wakeup source: %s\n", ws->name); in pm_print_active_wakeup_sources()
858 ktime_to_ns(ws->last_time) > in pm_print_active_wakeup_sources()
859 ktime_to_ns(last_activity_ws->last_time))) { in pm_print_active_wakeup_sources()
865 pm_pr_dbg("last active wakeup source: %s\n", in pm_print_active_wakeup_sources()
866 last_activity_ws->name); in pm_print_active_wakeup_sources()
872 * pm_wakeup_pending - Check if power transition in progress should be aborted.
959 * pm_get_wakeup_count - Read the number of registered wakeup events.
995 * pm_save_wakeup_count - Save the current number of registered wakeup events.
1022 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
1033 spin_lock_irq(&ws->lock); in pm_wakep_autosleep_enabled()
1034 if (ws->autosleep_enabled != set) { in pm_wakep_autosleep_enabled()
1035 ws->autosleep_enabled = set; in pm_wakep_autosleep_enabled()
1036 if (ws->active) { in pm_wakep_autosleep_enabled()
1038 ws->start_prevent_time = now; in pm_wakep_autosleep_enabled()
1043 spin_unlock_irq(&ws->lock); in pm_wakep_autosleep_enabled()
1050 * print_wakeup_source_stats - Print wakeup source statistics information.
1052 * @ws: Wakeup source object to print the statistics for.
1064 spin_lock_irqsave(&ws->lock, flags); in print_wakeup_source_stats()
1066 total_time = ws->total_time; in print_wakeup_source_stats()
1067 max_time = ws->max_time; in print_wakeup_source_stats()
1068 prevent_sleep_time = ws->prevent_sleep_time; in print_wakeup_source_stats()
1069 active_count = ws->active_count; in print_wakeup_source_stats()
1070 if (ws->active) { in print_wakeup_source_stats()
1073 active_time = ktime_sub(now, ws->last_time); in print_wakeup_source_stats()
1078 if (ws->autosleep_enabled) in print_wakeup_source_stats()
1080 ktime_sub(now, ws->start_prevent_time)); in print_wakeup_source_stats()
1085 seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n", in print_wakeup_source_stats()
1086 ws->name, active_count, ws->event_count, in print_wakeup_source_stats()
1087 ws->wakeup_count, ws->expire_count, in print_wakeup_source_stats()
1089 ktime_to_ms(max_time), ktime_to_ms(ws->last_time), in print_wakeup_source_stats()
1092 spin_unlock_irqrestore(&ws->lock, flags); in print_wakeup_source_stats()
1102 int *srcuidx = m->private; in wakeup_sources_stats_seq_start()
1112 if (n-- <= 0) in wakeup_sources_stats_seq_start()
1140 int *srcuidx = m->private; in wakeup_sources_stats_seq_stop()
1146 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.