aboutsummaryrefslogtreecommitdiff
path: root/kernel/power/autosleep.c
blob: 7923368342b01509bc0229c0ddba9dd4c5c8e4da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * kernel/power/autosleep.c
 *
 * Opportunistic sleep support.
 *
 * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
 */

#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/pm_wakeup.h>

#include "power.h"

//<20130327> <marc.huang> add autosleep dubug log
#define _TAG_AUTOSLEEP "AUTOSLEEP"
#define autosleep_log(fmt, ...)    pr_debug("[%s][%s]" fmt, _TAG_AUTOSLEEP, __func__, ##__VA_ARGS__)
#define autosleep_warn(fmt, ...)   pr_warn("[%s][%s]" fmt, _TAG_AUTOSLEEP, __func__, ##__VA_ARGS__)

#define HIB_AUTOSLEEP_DEBUG 1
#define _TAG_HIB_M "HIB/AUTOSLEEP"
#if (HIB_AUTOSLEEP_DEBUG)
#define hib_autoslp_log(fmt, ...)   pr_warn("[%s][%s]" fmt, _TAG_HIB_M, __func__, ##__VA_ARGS__);
#else
#define hib_autoslp_log(fmt, ...)
#endif
#define hib_autoslp_warn(fmt, ...)   pr_warn("[%s][%s]" fmt, _TAG_HIB_M, __func__, ##__VA_ARGS__);


static suspend_state_t autosleep_state;
static struct workqueue_struct *autosleep_wq;
/*
 * Note: it is only safe to mutex_lock(&autosleep_lock) if a wakeup_source
 * is active, otherwise a deadlock with try_to_suspend() is possible.
 * Alternatively mutex_lock_interruptible() can be used.  This will then fail
 * if an auto_sleep cycle tries to freeze processes.
 */
static DEFINE_MUTEX(autosleep_lock);
static struct wakeup_source *autosleep_ws;

#ifdef CONFIG_MTK_HIBERNATION
extern bool system_is_hibernating;
extern int mtk_hibernate_via_autosleep(suspend_state_t *autoslp_state);
#endif
static void try_to_suspend(struct work_struct *work)
{
	unsigned int initial_count, final_count;

	//<20130327> <marc.huang> add autosleep dubug log
	autosleep_log("pm_get_wakeup_count\n");
	if (!pm_get_wakeup_count(&initial_count, true))
		goto out;

	mutex_lock(&autosleep_lock);

	//<20130327> <marc.huang> add autosleep dubug log
	autosleep_log("pm_save_wakeup_count\n");
	if (!pm_save_wakeup_count(initial_count) ||
		system_state != SYSTEM_RUNNING) {
		mutex_unlock(&autosleep_lock);
		goto out;
	}

	if (autosleep_state == PM_SUSPEND_ON) {
#ifdef CONFIG_MTK_HIBERNATION
        system_is_hibernating = false;
#endif
		//<20130327> <marc.huang> add autosleep dubug log
		autosleep_warn("abort due to autosleep_state: %d\n", autosleep_state);
		mutex_unlock(&autosleep_lock);
		return;
	}
#ifdef CONFIG_MTK_HIBERNATION
    if (autosleep_state >= PM_SUSPEND_MAX) {
        mtk_hibernate_via_autosleep(&autosleep_state);
    }
    else {
        hib_autoslp_log("pm_suspend: state(%d)\n", autosleep_state);
        if (!system_is_hibernating) {
            hib_autoslp_warn("calling pm_suspend() state(%d)\n", autosleep_state);
            pm_suspend(autosleep_state);
        }
        else {
            hib_autoslp_warn("system is hibernating: so changing state(%d->%d)\n",  autosleep_state, PM_SUSPEND_MAX);
            autosleep_state = PM_SUSPEND_MAX;
        }
    }
#else // !CONFIG_MTK_HIBERNATION
	if (autosleep_state >= PM_SUSPEND_MAX)
		hibernate();
	else
        {
		//<20130327> <marc.huang> add autosleep dubug log
		autosleep_log("pm_suspend, autosleep_state: %d\n", autosleep_state);
		pm_suspend(autosleep_state);
        }
#endif // CONFIG_MTK_HIBERNATION
	mutex_unlock(&autosleep_lock);

	if (!pm_get_wakeup_count(&final_count, false))
		goto out;

	/*
	 * If the wakeup occured for an unknown reason, wait to prevent the
	 * system from trying to suspend and waking up in a tight loop.
	 */
	if (final_count == initial_count)
		schedule_timeout_uninterruptible(HZ / 2);

 out:
	//<20130327> <marc.huang> add autosleep dubug log
	autosleep_log("queue_up_suspend_work again\n");
	queue_up_suspend_work();
}

static DECLARE_WORK(suspend_work, try_to_suspend);

void queue_up_suspend_work(void)
{
	if (autosleep_state > PM_SUSPEND_ON)
        {
		//<20130327> <marc.huang> add autosleep dubug log
		autosleep_log("autosleep_state: %d\n", autosleep_state);
		queue_work(autosleep_wq, &suspend_work);
        }
}

suspend_state_t pm_autosleep_state(void)
{
	return autosleep_state;
}

int pm_autosleep_lock(void)
{
	return mutex_lock_interruptible(&autosleep_lock);
}

void pm_autosleep_unlock(void)
{
	mutex_unlock(&autosleep_lock);
}

int pm_autosleep_set_state(suspend_state_t state)
{

#ifndef CONFIG_HIBERNATION
	if (state >= PM_SUSPEND_MAX)
		return -EINVAL;
#endif

	__pm_stay_awake(autosleep_ws);

	mutex_lock(&autosleep_lock);

	autosleep_state = state;

	__pm_relax(autosleep_ws);

	if (state > PM_SUSPEND_ON) {
		//<20130327> <marc.huang> add autosleep dubug log
		autosleep_log("pm_wakep_autosleep_enabled(true)\n");
		pm_wakep_autosleep_enabled(true);
		queue_up_suspend_work();
	} else {
		//<20130327> <marc.huang> add autosleep dubug log
		autosleep_log("pm_wakep_autosleep_enabled(false)\n");
		pm_wakep_autosleep_enabled(false);
	}

	mutex_unlock(&autosleep_lock);
	return 0;
}

int __init pm_autosleep_init(void)
{
	autosleep_ws = wakeup_source_register("autosleep");
	if (!autosleep_ws)
		return -ENOMEM;

	autosleep_wq = alloc_ordered_workqueue("autosleep", 0);
	if (autosleep_wq)
		return 0;

	wakeup_source_unregister(autosleep_ws);
	return -ENOMEM;
}