Lines Matching +full:wakeup +full:- +full:source

1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/wakeup.c - System wakeup events framework
32 * if wakeup events are registered during or immediately before the transition.
36 /* First wakeup IRQ seen by the kernel in the last cycle. */
43 * Combined counters of registered wakeup events and wakeup events in progress.
50 #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
81 * wakeup_source_create - Create a struct wakeup_source object.
82 * @name: Name of the new wakeup source.
97 ws->name = ws_name; in wakeup_source_create()
102 ws->id = id; in wakeup_source_create()
107 kfree_const(ws->name); in wakeup_source_create()
124 if (ws->event_count) { in wakeup_source_record()
126 ktime_add(deleted_ws.total_time, ws->total_time); in wakeup_source_record()
129 ws->prevent_sleep_time); in wakeup_source_record()
131 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ? in wakeup_source_record()
132 deleted_ws.max_time : ws->max_time; in wakeup_source_record()
133 deleted_ws.event_count += ws->event_count; in wakeup_source_record()
134 deleted_ws.active_count += ws->active_count; in wakeup_source_record()
135 deleted_ws.relax_count += ws->relax_count; in wakeup_source_record()
136 deleted_ws.expire_count += ws->expire_count; in wakeup_source_record()
137 deleted_ws.wakeup_count += ws->wakeup_count; in wakeup_source_record()
145 ida_free(&wakeup_ida, ws->id); in wakeup_source_free()
146 kfree_const(ws->name); in wakeup_source_free()
151 * wakeup_source_destroy - Destroy a struct wakeup_source object.
152 * @ws: Wakeup source to destroy.
154 * Use only for wakeup source objects created with wakeup_source_create().
168 * wakeup_source_add - Add given object to the list of wakeup sources.
169 * @ws: Wakeup source object to add to the list.
178 spin_lock_init(&ws->lock); in wakeup_source_add()
179 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0); in wakeup_source_add()
180 ws->active = false; in wakeup_source_add()
183 list_add_rcu(&ws->entry, &wakeup_sources); in wakeup_source_add()
189 * wakeup_source_remove - Remove given object from the wakeup sources list.
190 * @ws: Wakeup source object to remove from the list.
200 list_del_rcu(&ws->entry); in wakeup_source_remove()
204 del_timer_sync(&ws->timer); in wakeup_source_remove()
207 * this wakeup source as not registered. in wakeup_source_remove()
209 ws->timer.function = NULL; in wakeup_source_remove()
214 * wakeup_source_register - Create wakeup source and add it to the list.
215 * @dev: Device this wakeup source is associated with (or NULL if virtual).
216 * @name: Name of the wakeup source to register.
240 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
241 * @ws: Wakeup source object to unregister.
247 if (ws->dev) in wakeup_source_unregister()
256 * wakeup_sources_read_lock - Lock wakeup source list for read.
268 * wakeup_sources_read_unlock - Unlock wakeup source list.
278 * wakeup_sources_walk_start - Begin a walk on wakeup source list
280 * Returns first object of the list of wakeup sources.
282 * Note that to be safe, wakeup sources list needs to be locked by calling
289 return list_entry_rcu(ws_head->next, struct wakeup_source, entry); in wakeup_sources_walk_start()
294 * wakeup_sources_walk_next - Get next wakeup source from the list
295 * @ws: Previous wakeup source object
297 * Note that to be safe, wakeup sources list needs to be locked by calling
304 return list_next_or_null_rcu(ws_head, &ws->entry, in wakeup_sources_walk_next()
310 * device_wakeup_attach - Attach a wakeup source object to a device object.
312 * @ws: Wakeup source object to attach to @dev.
314 * This causes @dev to be treated as a wakeup device.
318 spin_lock_irq(&dev->power.lock); in device_wakeup_attach()
319 if (dev->power.wakeup) { in device_wakeup_attach()
320 spin_unlock_irq(&dev->power.lock); in device_wakeup_attach()
321 return -EEXIST; in device_wakeup_attach()
323 dev->power.wakeup = ws; in device_wakeup_attach()
324 if (dev->power.wakeirq) in device_wakeup_attach()
325 device_wakeup_attach_irq(dev, dev->power.wakeirq); in device_wakeup_attach()
326 spin_unlock_irq(&dev->power.lock); in device_wakeup_attach()
331 * device_wakeup_enable - Enable given device to be a wakeup source.
334 * Create a wakeup source object, register it and attach it to @dev.
341 if (!dev || !dev->power.can_wakeup) in device_wakeup_enable()
342 return -EINVAL; in device_wakeup_enable()
349 return -ENOMEM; in device_wakeup_enable()
360 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
364 * Attach a device wakeirq to the wakeup source so the device
375 ws = dev->power.wakeup; in device_wakeup_attach_irq()
379 if (ws->wakeirq) in device_wakeup_attach_irq()
380 dev_err(dev, "Leftover wakeup IRQ found, overriding\n"); in device_wakeup_attach_irq()
382 ws->wakeirq = wakeirq; in device_wakeup_attach_irq()
386 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
389 * Removes a device wakeirq from the wakeup source.
397 ws = dev->power.wakeup; in device_wakeup_detach_irq()
399 ws->wakeirq = NULL; in device_wakeup_detach_irq()
414 dev_pm_arm_wake_irq(ws->wakeirq); in device_wakeup_arm_wake_irqs()
430 dev_pm_disarm_wake_irq(ws->wakeirq); in device_wakeup_disarm_wake_irqs()
435 * device_wakeup_detach - Detach a device's wakeup source object from it.
436 * @dev: Device to detach the wakeup source object from.
438 * After it returns, @dev will not be treated as a wakeup device any more.
444 spin_lock_irq(&dev->power.lock); in device_wakeup_detach()
445 ws = dev->power.wakeup; in device_wakeup_detach()
446 dev->power.wakeup = NULL; in device_wakeup_detach()
447 spin_unlock_irq(&dev->power.lock); in device_wakeup_detach()
452 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
455 * Detach the @dev's wakeup source object from it, unregister this wakeup source
462 if (!dev || !dev->power.can_wakeup) in device_wakeup_disable()
463 return -EINVAL; in device_wakeup_disable()
472 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
477 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
478 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
485 if (!!dev->power.can_wakeup == !!capable) in device_set_wakeup_capable()
488 dev->power.can_wakeup = capable; in device_set_wakeup_capable()
489 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) { in device_set_wakeup_capable()
494 dev_info(dev, "Wakeup sysfs attributes not added\n"); in device_set_wakeup_capable()
503 * device_init_wakeup - Device wakeup initialization.
505 * @enable: Whether or not to enable @dev as a wakeup device.
507 * By default, most devices should leave wakeup disabled. The exceptions are
508 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
510 * own wakeup requests but merely forward requests from one bus to another
511 * (like PCI bridges) should have wakeup enabled by default.
518 return -EINVAL; in device_init_wakeup()
533 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
543 * wakeup_source_not_registered - validate the given wakeup source.
544 * @ws: Wakeup source to be validated.
549 * Use timer struct to check if the given source is initialized in wakeup_source_not_registered()
552 return ws->timer.function != pm_wakeup_timer_fn; in wakeup_source_not_registered()
556 * The functions below use the observation that each wakeup event starts a
558 * will end depends on how the wakeup event is going to be processed after being
562 * First, a wakeup event may be detected by the same functional unit that will
570 * Second, a wakeup event may be detected by one functional unit and processed
584 * wakup_source_activate - Mark given wakeup source as active.
585 * @ws: Wakeup source to handle.
588 * core of the event by incrementing the counter of of wakeup events being
596 "unregistered wakeup source\n")) in wakeup_source_activate()
599 ws->active = true; in wakeup_source_activate()
600 ws->active_count++; in wakeup_source_activate()
601 ws->last_time = ktime_get(); in wakeup_source_activate()
602 if (ws->autosleep_enabled) in wakeup_source_activate()
603 ws->start_prevent_time = ws->last_time; in wakeup_source_activate()
608 trace_wakeup_source_activate(ws->name, cec); in wakeup_source_activate()
612 * wakeup_source_report_event - Report wakeup event using the given source.
613 * @ws: Wakeup source to report the event for.
614 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
618 ws->event_count++; in wakeup_source_report_event()
621 ws->wakeup_count++; in wakeup_source_report_event()
623 if (!ws->active) in wakeup_source_report_event()
631 * __pm_stay_awake - Notify the PM core of a wakeup event.
632 * @ws: Wakeup source object associated with the source of the event.
643 spin_lock_irqsave(&ws->lock, flags); in __pm_stay_awake()
646 del_timer(&ws->timer); in __pm_stay_awake()
647 ws->timer_expires = 0; in __pm_stay_awake()
649 spin_unlock_irqrestore(&ws->lock, flags); in __pm_stay_awake()
654 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
655 * @dev: Device the wakeup event is related to.
657 * Notify the PM core of a wakeup event (signaled by @dev) by calling
658 * __pm_stay_awake for the @dev's wakeup source object.
660 * Call this function after detecting of a wakeup event if pm_relax() is going
671 spin_lock_irqsave(&dev->power.lock, flags); in pm_stay_awake()
672 __pm_stay_awake(dev->power.wakeup); in pm_stay_awake()
673 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_stay_awake()
680 ktime_t delta = ktime_sub(now, ws->start_prevent_time); in update_prevent_sleep_time()
681 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta); in update_prevent_sleep_time()
689 * wakup_source_deactivate - Mark given wakeup source as inactive.
690 * @ws: Wakeup source to handle.
692 * Update the @ws' statistics and notify the PM core that the wakeup source has
693 * become inactive by decrementing the counter of wakeup events being processed
694 * and incrementing the counter of registered wakeup events.
702 ws->relax_count++; in wakeup_source_deactivate()
708 * will set ws->active. Then, ws->active may be cleared immediately in wakeup_source_deactivate()
710 * case ws->relax_count will be different from ws->active_count. in wakeup_source_deactivate()
712 if (ws->relax_count != ws->active_count) { in wakeup_source_deactivate()
713 ws->relax_count--; in wakeup_source_deactivate()
717 ws->active = false; in wakeup_source_deactivate()
720 duration = ktime_sub(now, ws->last_time); in wakeup_source_deactivate()
721 ws->total_time = ktime_add(ws->total_time, duration); in wakeup_source_deactivate()
722 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time)) in wakeup_source_deactivate()
723 ws->max_time = duration; in wakeup_source_deactivate()
725 ws->last_time = now; in wakeup_source_deactivate()
726 del_timer(&ws->timer); in wakeup_source_deactivate()
727 ws->timer_expires = 0; in wakeup_source_deactivate()
729 if (ws->autosleep_enabled) in wakeup_source_deactivate()
733 * Increment the counter of registered wakeup events and decrement the in wakeup_source_deactivate()
734 * couter of wakeup events in progress simultaneously. in wakeup_source_deactivate()
737 trace_wakeup_source_deactivate(ws->name, cec); in wakeup_source_deactivate()
745 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
746 * @ws: Wakeup source object associated with the source of the event.
748 * Call this function for wakeup events whose processing started with calling
760 spin_lock_irqsave(&ws->lock, flags); in __pm_relax()
761 if (ws->active) in __pm_relax()
763 spin_unlock_irqrestore(&ws->lock, flags); in __pm_relax()
768 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
771 * Execute __pm_relax() for the @dev's wakeup source object.
780 spin_lock_irqsave(&dev->power.lock, flags); in pm_relax()
781 __pm_relax(dev->power.wakeup); in pm_relax()
782 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_relax()
787 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
788 * @data: Address of the wakeup source object associated with the event source.
790 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
799 spin_lock_irqsave(&ws->lock, flags); in pm_wakeup_timer_fn()
801 if (ws->active && ws->timer_expires in pm_wakeup_timer_fn()
802 && time_after_eq(jiffies, ws->timer_expires)) { in pm_wakeup_timer_fn()
804 ws->expire_count++; in pm_wakeup_timer_fn()
807 spin_unlock_irqrestore(&ws->lock, flags); in pm_wakeup_timer_fn()
811 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
812 * @ws: Wakeup source object associated with the event source.
814 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
816 * Notify the PM core of a wakeup event whose source is @ws that will take
831 spin_lock_irqsave(&ws->lock, flags); in pm_wakeup_ws_event()
844 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) { in pm_wakeup_ws_event()
845 mod_timer(&ws->timer, expires); in pm_wakeup_ws_event()
846 ws->timer_expires = expires; in pm_wakeup_ws_event()
850 spin_unlock_irqrestore(&ws->lock, flags); in pm_wakeup_ws_event()
855 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
856 * @dev: Device the wakeup event is related to.
858 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
860 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
869 spin_lock_irqsave(&dev->power.lock, flags); in pm_wakeup_dev_event()
870 pm_wakeup_ws_event(dev->power.wakeup, msec, hard); in pm_wakeup_dev_event()
871 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_wakeup_dev_event()
883 if (ws->active) { in pm_print_active_wakeup_sources()
884 pm_pr_dbg("active wakeup source: %s\n", ws->name); in pm_print_active_wakeup_sources()
888 ktime_to_ns(ws->last_time) > in pm_print_active_wakeup_sources()
889 ktime_to_ns(last_activity_ws->last_time))) { in pm_print_active_wakeup_sources()
895 pm_pr_dbg("last active wakeup source: %s\n", in pm_print_active_wakeup_sources()
896 last_activity_ws->name); in pm_print_active_wakeup_sources()
902 * pm_wakeup_pending - Check if power transition in progress should be aborted.
904 * Compare the current number of registered wakeup events with its preserved
905 * value from the past and return true if new wakeup events have been registered
907 * wakeup events being processed is different from zero.
925 pm_pr_dbg("Wakeup pending, aborting suspend\n"); in pm_wakeup_pending()
960 * pm_get_wakeup_count - Read the number of registered wakeup events.
964 * Store the number of registered wakeup events at the address in @count. If
965 * @block is set, block until the current number of wakeup events being
968 * Return 'false' if the current number of wakeup events being processed is
996 * pm_save_wakeup_count - Save the current number of registered wakeup events.
997 * @count: Value to compare with the current number of registered wakeup events.
999 * If @count is equal to the current number of registered wakeup events and the
1000 * current number of wakeup events being processed is zero, store @count as the
1001 * old number of registered wakeup events for pm_check_wakeup_events(), enable
1002 * wakeup events detection and return 'true'. Otherwise disable wakeup events
1023 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
1034 spin_lock_irq(&ws->lock); in pm_wakep_autosleep_enabled()
1035 if (ws->autosleep_enabled != set) { in pm_wakep_autosleep_enabled()
1036 ws->autosleep_enabled = set; in pm_wakep_autosleep_enabled()
1037 if (ws->active) { in pm_wakep_autosleep_enabled()
1039 ws->start_prevent_time = now; in pm_wakep_autosleep_enabled()
1044 spin_unlock_irq(&ws->lock); in pm_wakep_autosleep_enabled()
1051 * print_wakeup_source_stats - Print wakeup source statistics information.
1053 * @ws: Wakeup source object to print the statistics for.
1065 spin_lock_irqsave(&ws->lock, flags); in print_wakeup_source_stats()
1067 total_time = ws->total_time; in print_wakeup_source_stats()
1068 max_time = ws->max_time; in print_wakeup_source_stats()
1069 prevent_sleep_time = ws->prevent_sleep_time; in print_wakeup_source_stats()
1070 active_count = ws->active_count; in print_wakeup_source_stats()
1071 if (ws->active) { in print_wakeup_source_stats()
1074 active_time = ktime_sub(now, ws->last_time); in print_wakeup_source_stats()
1079 if (ws->autosleep_enabled) in print_wakeup_source_stats()
1081 ktime_sub(now, ws->start_prevent_time)); in print_wakeup_source_stats()
1086 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()
1087 ws->name, active_count, ws->event_count, in print_wakeup_source_stats()
1088 ws->wakeup_count, ws->expire_count, in print_wakeup_source_stats()
1090 ktime_to_ms(max_time), ktime_to_ms(ws->last_time), in print_wakeup_source_stats()
1093 spin_unlock_irqrestore(&ws->lock, flags); in print_wakeup_source_stats()
1103 int *srcuidx = m->private; in wakeup_sources_stats_seq_start()
1113 if (n-- <= 0) in wakeup_sources_stats_seq_start()
1141 int *srcuidx = m->private; in wakeup_sources_stats_seq_stop()
1147 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.