summaryrefslogtreecommitdiff
path: root/arch/arm/mach-ux500/pm/suspend_dbg.c
blob: 68afa96e547270f54700cba00aeacc88636fa027 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) ST-Ericsson SA 2010-2011
 *
 * License Terms: GNU General Public License v2
 *
 * Author: Rickard Andersson <rickard.andersson@stericsson.com>,
 *	   Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
 *
 */
#include <linux/kernel.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/suspend.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <linux/mfd/dbx500-prcmu.h>
#include <linux/wakelock.h>

#include <mach/pm.h>
#include <mach/pm-timer.h>

/* To reach main_wake_lock */
#include "../../../../kernel/power/power.h"

#ifdef CONFIG_UX500_SUSPEND_STANDBY
static u32 sleep_enabled = 1;
#else
static u32 sleep_enabled;
#endif

#ifdef CONFIG_UX500_SUSPEND_MEM
static u32 deepsleep_enabled = 1;
#else
static u32 deepsleep_enabled;
#endif

static u32 suspend_enabled = 1;

static u32 deepsleeps_done;
static u32 deepsleeps_failed;
static u32 sleeps_done;
static u32 sleeps_failed;
static u32 suspend_count;

#ifdef CONFIG_UX500_SUSPEND_DBG_WAKE_ON_UART
void ux500_suspend_dbg_add_wake_on_uart(void)
{
	irq_set_irq_wake(GPIO_TO_IRQ(ux500_console_uart_gpio_pin), 1);
	irq_set_irq_type(GPIO_TO_IRQ(ux500_console_uart_gpio_pin),
			 IRQ_TYPE_EDGE_BOTH);
}

void ux500_suspend_dbg_remove_wake_on_uart(void)
{
	irq_set_irq_wake(GPIO_TO_IRQ(ux500_console_uart_gpio_pin), 0);
}
#endif

bool ux500_suspend_enabled(void)
{
	return suspend_enabled != 0;
}

bool ux500_suspend_sleep_enabled(void)
{
	return sleep_enabled != 0;
}

bool ux500_suspend_deepsleep_enabled(void)
{
	return deepsleep_enabled != 0;
}

void ux500_suspend_dbg_sleep_status(bool is_deepsleep)
{
	enum prcmu_power_status prcmu_status;

	prcmu_status = prcmu_get_power_state_result();

	if (is_deepsleep) {
		pr_info("Returning from ApDeepSleep. PRCMU ret: 0x%x - %s\n",
			prcmu_status,
			prcmu_status == PRCMU_DEEP_SLEEP_OK ?
			"Success" : "Fail!");
		if (prcmu_status == PRCMU_DEEP_SLEEP_OK)
			deepsleeps_done++;
		else
			deepsleeps_failed++;
	} else {
		pr_info("Returning from ApSleep. PRCMU ret: 0x%x - %s\n",
			prcmu_status,
			prcmu_status == PRCMU_SLEEP_OK ? "Success" : "Fail!");
		if (prcmu_status == PRCMU_SLEEP_OK)
			sleeps_done++;
		else
			sleeps_failed++;
	}
}

int ux500_suspend_dbg_begin(suspend_state_t state)
{
	suspend_count++;
	return 0;
}

/* The number of failed suspend attempts in a row before giving up */
#define TEST_FAILS 10

static int suspend_test_count;
static int suspend_test_current;
static int suspend_test_fail_count;

void ux500_suspend_dbg_test_set_wakeup(void)
{
	if (suspend_test_count == 0)
		return;

	ux500_rtcrtt_off();

	/* Make sure the rtc writes have been accepted */
	udelay(120);

	if (cpu_is_u9500())
		prcmu_enable_wakeups(PRCMU_WAKEUP(ABB) | PRCMU_WAKEUP(RTC) |
				     PRCMU_WAKEUP(HSI0));
	else
		prcmu_enable_wakeups(PRCMU_WAKEUP(ABB) | PRCMU_WAKEUP(RTC));

	/* Program RTC to generate an interrupt 1s later */
	ux500_rtcrtt_next(1000000);
}

void ux500_suspend_dbg_test_start(int num)
{
	suspend_test_count = num;
	suspend_test_current = deepsleeps_done;
	suspend_test_fail_count = 0;
}

bool ux500_suspend_test_success(bool *ongoing)
{
	(*ongoing) = ((suspend_test_fail_count < TEST_FAILS) &&
		      (suspend_test_count > 0));
	return suspend_test_fail_count < TEST_FAILS;
}

void ux500_suspend_dbg_end(void)
{
	static int attempts;

	if (suspend_test_count > 0) {
		attempts++;
		pr_info("Suspend test: %d done\n", attempts);
		suspend_test_count--;
		wake_lock(&main_wake_lock);

		if (suspend_test_current < deepsleeps_done) {
			suspend_test_current = deepsleeps_done;
			suspend_test_fail_count = 0;
		} else {
			suspend_test_fail_count++;
		}

		if (suspend_test_fail_count > TEST_FAILS) {
			suspend_test_count = 0;
			pr_err("suspend: Aborting after %d "
			       "failed suspend in a row\n",
			       TEST_FAILS);
		} else if (suspend_test_count > 0) {
			msleep(100);
			wake_unlock(&main_wake_lock);
		}

		if (suspend_test_count == 0)
			attempts = 0;
	}
}

void ux500_suspend_dbg_init(void)
{
	struct dentry *suspend_dir;
	struct dentry *file;

	suspend_dir = debugfs_create_dir("suspend", NULL);
	if (IS_ERR_OR_NULL(suspend_dir))
		return;

	file = debugfs_create_bool("sleep", S_IWUGO | S_IRUGO,
				   suspend_dir,
				   &sleep_enabled);
	if (IS_ERR_OR_NULL(file))
		goto error;

	file = debugfs_create_bool("deepsleep", S_IWUGO | S_IRUGO,
				   suspend_dir,
				   &deepsleep_enabled);
	if (IS_ERR_OR_NULL(file))
		goto error;

	file = debugfs_create_bool("enable", S_IWUGO | S_IRUGO,
				   suspend_dir,
				   &suspend_enabled);
	if (IS_ERR_OR_NULL(file))
		goto error;

	file = debugfs_create_u32("count", S_IRUGO,
				  suspend_dir,
				  &suspend_count);
	if (IS_ERR_OR_NULL(file))
		goto error;

	file = debugfs_create_u32("sleep_count", S_IRUGO,
				  suspend_dir,
				  &sleeps_done);
	if (IS_ERR_OR_NULL(file))
		goto error;

	file = debugfs_create_u32("deepsleep_count", S_IRUGO,
				  suspend_dir,
				  &deepsleeps_done);
	if (IS_ERR_OR_NULL(file))
		goto error;

	file = debugfs_create_u32("sleep_failed", S_IRUGO,
				  suspend_dir,
				  &sleeps_failed);
	if (IS_ERR_OR_NULL(file))
		goto error;

	file = debugfs_create_u32("deepsleep_failed", S_IRUGO,
				  suspend_dir,
				  &deepsleeps_failed);
	if (IS_ERR_OR_NULL(file))
		goto error;

	return;
error:
	if (!IS_ERR_OR_NULL(suspend_dir))
		debugfs_remove_recursive(suspend_dir);
}