Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * kernel/power/autosleep.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Opportunistic sleep support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/pm_wakeup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "power.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static suspend_state_t autosleep_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static struct workqueue_struct *autosleep_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * Note: it is only safe to mutex_lock(&autosleep_lock) if a wakeup_source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * is active, otherwise a deadlock with try_to_suspend() is possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * Alternatively mutex_lock_interruptible() can be used.  This will then fail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * if an auto_sleep cycle tries to freeze processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static DEFINE_MUTEX(autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static struct wakeup_source *autosleep_ws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static void try_to_suspend(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	unsigned int initial_count, final_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (!pm_get_wakeup_count(&initial_count, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	mutex_lock(&autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (!pm_save_wakeup_count(initial_count) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		system_state != SYSTEM_RUNNING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		mutex_unlock(&autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (autosleep_state == PM_SUSPEND_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		mutex_unlock(&autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (autosleep_state >= PM_SUSPEND_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		hibernate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		pm_suspend(autosleep_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	mutex_unlock(&autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!pm_get_wakeup_count(&final_count, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 * If the wakeup occured for an unknown reason, wait to prevent the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * system from trying to suspend and waking up in a tight loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (final_count == initial_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		schedule_timeout_uninterruptible(HZ / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	queue_up_suspend_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static DECLARE_WORK(suspend_work, try_to_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) void queue_up_suspend_work(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (autosleep_state > PM_SUSPEND_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		queue_work(autosleep_wq, &suspend_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) suspend_state_t pm_autosleep_state(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return autosleep_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) int pm_autosleep_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return mutex_lock_interruptible(&autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) void pm_autosleep_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	mutex_unlock(&autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) int pm_autosleep_set_state(suspend_state_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) #ifndef CONFIG_HIBERNATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (state >= PM_SUSPEND_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	__pm_stay_awake(autosleep_ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	mutex_lock(&autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	autosleep_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	__pm_relax(autosleep_ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (state > PM_SUSPEND_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		pm_wakep_autosleep_enabled(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		queue_up_suspend_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		pm_wakep_autosleep_enabled(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	mutex_unlock(&autosleep_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int __init pm_autosleep_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	autosleep_ws = wakeup_source_register(NULL, "autosleep");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (!autosleep_ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	autosleep_wq = alloc_ordered_workqueue("autosleep", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (autosleep_wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	wakeup_source_unregister(autosleep_ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }