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. */
44 * Combined counters of registered wakeup events and wakeup events in progress.
51 #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
82 * wakeup_source_create - Create a struct wakeup_source object.
83 * @name: Name of the new wakeup source.
98 ws->name = ws_name; in wakeup_source_create()
103 ws->id = id; in wakeup_source_create()
108 kfree_const(ws->name); in wakeup_source_create()
125 if (ws->event_count) { in wakeup_source_record()
127 ktime_add(deleted_ws.total_time, ws->total_time); in wakeup_source_record()
130 ws->prevent_sleep_time); in wakeup_source_record()
132 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ? in wakeup_source_record()
133 deleted_ws.max_time : ws->max_time; in wakeup_source_record()
134 deleted_ws.event_count += ws->event_count; in wakeup_source_record()
135 deleted_ws.active_count += ws->active_count; in wakeup_source_record()
136 deleted_ws.relax_count += ws->relax_count; in wakeup_source_record()
137 deleted_ws.expire_count += ws->expire_count; in wakeup_source_record()
138 deleted_ws.wakeup_count += ws->wakeup_count; in wakeup_source_record()
146 ida_free(&wakeup_ida, ws->id); in wakeup_source_free()
147 kfree_const(ws->name); in wakeup_source_free()
152 * wakeup_source_destroy - Destroy a struct wakeup_source object.
153 * @ws: Wakeup source to destroy.
155 * Use only for wakeup source objects created with wakeup_source_create().
169 * wakeup_source_add - Add given object to the list of wakeup sources.
170 * @ws: Wakeup source object to add to the list.
179 spin_lock_init(&ws->lock); in wakeup_source_add()
180 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0); in wakeup_source_add()
181 ws->active = false; in wakeup_source_add()
184 list_add_rcu(&ws->entry, &wakeup_sources); in wakeup_source_add()
190 * wakeup_source_remove - Remove given object from the wakeup sources list.
191 * @ws: Wakeup source object to remove from the list.
201 list_del_rcu(&ws->entry); in wakeup_source_remove()
205 del_timer_sync(&ws->timer); in wakeup_source_remove()
208 * this wakeup source as not registered. in wakeup_source_remove()
210 ws->timer.function = NULL; in wakeup_source_remove()
215 * wakeup_source_register - Create wakeup source and add it to the list.
216 * @dev: Device this wakeup source is associated with (or NULL if virtual).
217 * @name: Name of the wakeup source to register.
241 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
242 * @ws: Wakeup source object to unregister.
248 if (ws->dev) in wakeup_source_unregister()
257 * wakeup_sources_read_lock - Lock wakeup source list for read.
269 * wakeup_sources_read_unlock - Unlock wakeup source list.
279 * wakeup_sources_walk_start - Begin a walk on wakeup source list
281 * Returns first object of the list of wakeup sources.
283 * Note that to be safe, wakeup sources list needs to be locked by calling
290 return list_entry_rcu(ws_head->next, struct wakeup_source, entry); in wakeup_sources_walk_start()
295 * wakeup_sources_walk_next - Get next wakeup source from the list
296 * @ws: Previous wakeup source object
298 * Note that to be safe, wakeup sources list needs to be locked by calling
305 return list_next_or_null_rcu(ws_head, &ws->entry, in wakeup_sources_walk_next()
311 * device_wakeup_attach - Attach a wakeup source object to a device object.
313 * @ws: Wakeup source object to attach to @dev.
315 * This causes @dev to be treated as a wakeup device.
319 spin_lock_irq(&dev->power.lock); in device_wakeup_attach()
320 if (dev->power.wakeup) { in device_wakeup_attach()
321 spin_unlock_irq(&dev->power.lock); in device_wakeup_attach()
322 return -EEXIST; in device_wakeup_attach()
324 dev->power.wakeup = ws; in device_wakeup_attach()
325 if (dev->power.wakeirq) in device_wakeup_attach()
326 device_wakeup_attach_irq(dev, dev->power.wakeirq); in device_wakeup_attach()
327 spin_unlock_irq(&dev->power.lock); in device_wakeup_attach()
332 * device_wakeup_enable - Enable given device to be a wakeup source.
335 * Create a wakeup source object, register it and attach it to @dev.
342 if (!dev || !dev->power.can_wakeup) in device_wakeup_enable()
343 return -EINVAL; in device_wakeup_enable()
350 return -ENOMEM; in device_wakeup_enable()
361 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
365 * Attach a device wakeirq to the wakeup source so the device
376 ws = dev->power.wakeup; in device_wakeup_attach_irq()
380 if (ws->wakeirq) in device_wakeup_attach_irq()
381 dev_err(dev, "Leftover wakeup IRQ found, overriding\n"); in device_wakeup_attach_irq()
383 ws->wakeirq = wakeirq; in device_wakeup_attach_irq()
387 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
390 * Removes a device wakeirq from the wakeup source.
398 ws = dev->power.wakeup; in device_wakeup_detach_irq()
400 ws->wakeirq = NULL; in device_wakeup_detach_irq()
404 * device_wakeup_arm_wake_irqs -
415 dev_pm_arm_wake_irq(ws->wakeirq); in device_wakeup_arm_wake_irqs()
420 * device_wakeup_disarm_wake_irqs -
431 dev_pm_disarm_wake_irq(ws->wakeirq); in device_wakeup_disarm_wake_irqs()
436 * device_wakeup_detach - Detach a device's wakeup source object from it.
437 * @dev: Device to detach the wakeup source object from.
439 * After it returns, @dev will not be treated as a wakeup device any more.
445 spin_lock_irq(&dev->power.lock); in device_wakeup_detach()
446 ws = dev->power.wakeup; in device_wakeup_detach()
447 dev->power.wakeup = NULL; in device_wakeup_detach()
448 spin_unlock_irq(&dev->power.lock); in device_wakeup_detach()
453 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
456 * Detach the @dev's wakeup source object from it, unregister this wakeup source
463 if (!dev || !dev->power.can_wakeup) in device_wakeup_disable()
464 return -EINVAL; in device_wakeup_disable()
473 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
478 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
479 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
486 if (!!dev->power.can_wakeup == !!capable) in device_set_wakeup_capable()
489 dev->power.can_wakeup = capable; in device_set_wakeup_capable()
490 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) { in device_set_wakeup_capable()
495 dev_info(dev, "Wakeup sysfs attributes not added\n"); in device_set_wakeup_capable()
504 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
515 * wakeup_source_not_registered - validate the given wakeup source.
516 * @ws: Wakeup source to be validated.
521 * Use timer struct to check if the given source is initialized in wakeup_source_not_registered()
524 return ws->timer.function != pm_wakeup_timer_fn; in wakeup_source_not_registered()
528 * The functions below use the observation that each wakeup event starts a
530 * will end depends on how the wakeup event is going to be processed after being
534 * First, a wakeup event may be detected by the same functional unit that will
542 * Second, a wakeup event may be detected by one functional unit and processed
556 * wakeup_source_activate - Mark given wakeup source as active.
557 * @ws: Wakeup source to handle.
560 * core of the event by incrementing the counter of the wakeup events being
568 "unregistered wakeup source\n")) in wakeup_source_activate()
571 ws->active = true; in wakeup_source_activate()
572 ws->active_count++; in wakeup_source_activate()
573 ws->last_time = ktime_get(); in wakeup_source_activate()
574 if (ws->autosleep_enabled) in wakeup_source_activate()
575 ws->start_prevent_time = ws->last_time; in wakeup_source_activate()
580 trace_wakeup_source_activate(ws->name, cec); in wakeup_source_activate()
584 * wakeup_source_report_event - Report wakeup event using the given source.
585 * @ws: Wakeup source to report the event for.
586 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
590 ws->event_count++; in wakeup_source_report_event()
593 ws->wakeup_count++; in wakeup_source_report_event()
595 if (!ws->active) in wakeup_source_report_event()
603 * __pm_stay_awake - Notify the PM core of a wakeup event.
604 * @ws: Wakeup source object associated with the source of the event.
615 spin_lock_irqsave(&ws->lock, flags); in __pm_stay_awake()
618 del_timer(&ws->timer); in __pm_stay_awake()
619 ws->timer_expires = 0; in __pm_stay_awake()
621 spin_unlock_irqrestore(&ws->lock, flags); in __pm_stay_awake()
626 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
627 * @dev: Device the wakeup event is related to.
629 * Notify the PM core of a wakeup event (signaled by @dev) by calling
630 * __pm_stay_awake for the @dev's wakeup source object.
632 * Call this function after detecting of a wakeup event if pm_relax() is going
643 spin_lock_irqsave(&dev->power.lock, flags); in pm_stay_awake()
644 __pm_stay_awake(dev->power.wakeup); in pm_stay_awake()
645 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_stay_awake()
652 ktime_t delta = ktime_sub(now, ws->start_prevent_time); in update_prevent_sleep_time()
653 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta); in update_prevent_sleep_time()
661 * wakeup_source_deactivate - Mark given wakeup source as inactive.
662 * @ws: Wakeup source to handle.
664 * Update the @ws' statistics and notify the PM core that the wakeup source has
665 * become inactive by decrementing the counter of wakeup events being processed
666 * and incrementing the counter of registered wakeup events.
674 ws->relax_count++; in wakeup_source_deactivate()
680 * will set ws->active. Then, ws->active may be cleared immediately in wakeup_source_deactivate()
682 * case ws->relax_count will be different from ws->active_count. in wakeup_source_deactivate()
684 if (ws->relax_count != ws->active_count) { in wakeup_source_deactivate()
685 ws->relax_count--; in wakeup_source_deactivate()
689 ws->active = false; in wakeup_source_deactivate()
692 duration = ktime_sub(now, ws->last_time); in wakeup_source_deactivate()
693 ws->total_time = ktime_add(ws->total_time, duration); in wakeup_source_deactivate()
694 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time)) in wakeup_source_deactivate()
695 ws->max_time = duration; in wakeup_source_deactivate()
697 ws->last_time = now; in wakeup_source_deactivate()
698 del_timer(&ws->timer); in wakeup_source_deactivate()
699 ws->timer_expires = 0; in wakeup_source_deactivate()
701 if (ws->autosleep_enabled) in wakeup_source_deactivate()
705 * Increment the counter of registered wakeup events and decrement the in wakeup_source_deactivate()
706 * counter of wakeup events in progress simultaneously. in wakeup_source_deactivate()
709 trace_wakeup_source_deactivate(ws->name, cec); in wakeup_source_deactivate()
717 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
718 * @ws: Wakeup source object associated with the source of the event.
720 * Call this function for wakeup events whose processing started with calling
732 spin_lock_irqsave(&ws->lock, flags); in __pm_relax()
733 if (ws->active) in __pm_relax()
735 spin_unlock_irqrestore(&ws->lock, flags); in __pm_relax()
740 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
743 * Execute __pm_relax() for the @dev's wakeup source object.
752 spin_lock_irqsave(&dev->power.lock, flags); in pm_relax()
753 __pm_relax(dev->power.wakeup); in pm_relax()
754 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_relax()
759 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
762 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
771 spin_lock_irqsave(&ws->lock, flags); in pm_wakeup_timer_fn()
773 if (ws->active && ws->timer_expires in pm_wakeup_timer_fn()
774 && time_after_eq(jiffies, ws->timer_expires)) { in pm_wakeup_timer_fn()
776 ws->expire_count++; in pm_wakeup_timer_fn()
779 spin_unlock_irqrestore(&ws->lock, flags); in pm_wakeup_timer_fn()
783 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
784 * @ws: Wakeup source object associated with the event source.
786 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
788 * Notify the PM core of a wakeup event whose source is @ws that will take
803 spin_lock_irqsave(&ws->lock, flags); in pm_wakeup_ws_event()
816 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) { in pm_wakeup_ws_event()
817 mod_timer(&ws->timer, expires); in pm_wakeup_ws_event()
818 ws->timer_expires = expires; in pm_wakeup_ws_event()
822 spin_unlock_irqrestore(&ws->lock, flags); in pm_wakeup_ws_event()
827 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
828 * @dev: Device the wakeup event is related to.
830 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
832 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
841 spin_lock_irqsave(&dev->power.lock, flags); in pm_wakeup_dev_event()
842 pm_wakeup_ws_event(dev->power.wakeup, msec, hard); in pm_wakeup_dev_event()
843 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_wakeup_dev_event()
855 if (ws->active) { in pm_print_active_wakeup_sources()
856 pm_pr_dbg("active wakeup source: %s\n", ws->name); in pm_print_active_wakeup_sources()
860 ktime_to_ns(ws->last_time) > in pm_print_active_wakeup_sources()
861 ktime_to_ns(last_activity_ws->last_time))) { in pm_print_active_wakeup_sources()
867 pm_pr_dbg("last active wakeup source: %s\n", in pm_print_active_wakeup_sources()
868 last_activity_ws->name); in pm_print_active_wakeup_sources()
874 * pm_wakeup_pending - Check if power transition in progress should be aborted.
876 * Compare the current number of registered wakeup events with its preserved
877 * value from the past and return true if new wakeup events have been registered
879 * wakeup events being processed is different from zero.
897 pm_pr_dbg("Wakeup pending, aborting suspend\n"); in pm_wakeup_pending()
947 pm_pr_dbg("Triggering wakeup from IRQ %d\n", irq_number); in pm_system_irq_wakeup()
961 * pm_get_wakeup_count - Read the number of registered wakeup events.
965 * Store the number of registered wakeup events at the address in @count. If
966 * @block is set, block until the current number of wakeup events being
969 * Return 'false' if the current number of wakeup events being processed is
997 * pm_save_wakeup_count - Save the current number of registered wakeup events.
998 * @count: Value to compare with the current number of registered wakeup events.
1000 * If @count is equal to the current number of registered wakeup events and the
1001 * current number of wakeup events being processed is zero, store @count as the
1002 * old number of registered wakeup events for pm_check_wakeup_events(), enable
1003 * wakeup events detection and return 'true'. Otherwise disable wakeup events
1024 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
1035 spin_lock_irq(&ws->lock); in pm_wakep_autosleep_enabled()
1036 if (ws->autosleep_enabled != set) { in pm_wakep_autosleep_enabled()
1037 ws->autosleep_enabled = set; in pm_wakep_autosleep_enabled()
1038 if (ws->active) { in pm_wakep_autosleep_enabled()
1040 ws->start_prevent_time = now; in pm_wakep_autosleep_enabled()
1045 spin_unlock_irq(&ws->lock); in pm_wakep_autosleep_enabled()
1052 * print_wakeup_source_stats - Print wakeup source statistics information.
1054 * @ws: Wakeup source object to print the statistics for.
1066 spin_lock_irqsave(&ws->lock, flags); in print_wakeup_source_stats()
1068 total_time = ws->total_time; in print_wakeup_source_stats()
1069 max_time = ws->max_time; in print_wakeup_source_stats()
1070 prevent_sleep_time = ws->prevent_sleep_time; in print_wakeup_source_stats()
1071 active_count = ws->active_count; in print_wakeup_source_stats()
1072 if (ws->active) { in print_wakeup_source_stats()
1075 active_time = ktime_sub(now, ws->last_time); in print_wakeup_source_stats()
1080 if (ws->autosleep_enabled) in print_wakeup_source_stats()
1082 ktime_sub(now, ws->start_prevent_time)); in print_wakeup_source_stats()
1087 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()
1088 ws->name, active_count, ws->event_count, in print_wakeup_source_stats()
1089 ws->wakeup_count, ws->expire_count, in print_wakeup_source_stats()
1091 ktime_to_ms(max_time), ktime_to_ms(ws->last_time), in print_wakeup_source_stats()
1094 spin_unlock_irqrestore(&ws->lock, flags); in print_wakeup_source_stats()
1104 int *srcuidx = m->private; in wakeup_sources_stats_seq_start()
1114 if (n-- <= 0) in wakeup_sources_stats_seq_start()
1142 int *srcuidx = m->private; in wakeup_sources_stats_seq_stop()
1148 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.