1
2
3
4
5
6
7
8
9#include <linux/init.h>
10#include <linux/rtc.h>
11
12#include "power.h"
13
14
15
16
17
18
19
20
21
22#define TEST_SUSPEND_SECONDS 10
23
24static unsigned long suspend_test_start_time;
25
26void suspend_test_start(void)
27{
28
29
30
31
32 suspend_test_start_time = jiffies;
33}
34
35void suspend_test_finish(const char *label)
36{
37 long nj = jiffies - suspend_test_start_time;
38 unsigned msec;
39
40 msec = jiffies_to_msecs(abs(nj));
41 pr_info("PM: %s took %d.%03d seconds\n", label,
42 msec / 1000, msec % 1000);
43
44
45
46
47
48
49
50
51
52 WARN(msec > (TEST_SUSPEND_SECONDS * 1000),
53 "Component: %s, time: %u\n", label, msec);
54}
55
56
57
58
59
60
61static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
62{
63 static char err_readtime[] __initdata =
64 KERN_ERR "PM: can't read %s time, err %d\n";
65 static char err_wakealarm [] __initdata =
66 KERN_ERR "PM: can't set %s wakealarm, err %d\n";
67 static char err_suspend[] __initdata =
68 KERN_ERR "PM: suspend test failed, error %d\n";
69 static char info_test[] __initdata =
70 KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
71
72 unsigned long now;
73 struct rtc_wkalrm alm;
74 int status;
75
76
77 status = rtc_read_time(rtc, &alm.time);
78 if (status < 0) {
79 printk(err_readtime, dev_name(&rtc->dev), status);
80 return;
81 }
82 rtc_tm_to_time(&alm.time, &now);
83
84 memset(&alm, 0, sizeof alm);
85 rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
86 alm.enabled = true;
87
88 status = rtc_set_alarm(rtc, &alm);
89 if (status < 0) {
90 printk(err_wakealarm, dev_name(&rtc->dev), status);
91 return;
92 }
93
94 if (state == PM_SUSPEND_MEM) {
95 printk(info_test, pm_states[state]);
96 status = pm_suspend(state);
97 if (status == -ENODEV)
98 state = PM_SUSPEND_STANDBY;
99 }
100 if (state == PM_SUSPEND_STANDBY) {
101 printk(info_test, pm_states[state]);
102 status = pm_suspend(state);
103 }
104 if (status < 0)
105 printk(err_suspend, status);
106
107
108
109
110
111 alm.enabled = false;
112 rtc_set_alarm(rtc, &alm);
113}
114
115static int __init has_wakealarm(struct device *dev, const void *data)
116{
117 struct rtc_device *candidate = to_rtc_device(dev);
118
119 if (!candidate->ops->set_alarm)
120 return 0;
121 if (!device_may_wakeup(candidate->dev.parent))
122 return 0;
123
124 return 1;
125}
126
127
128
129
130
131
132static suspend_state_t test_state __initdata = PM_SUSPEND_ON;
133
134static char warn_bad_state[] __initdata =
135 KERN_WARNING "PM: can't test '%s' suspend state\n";
136
137static int __init setup_test_suspend(char *value)
138{
139 unsigned i;
140
141
142 value++;
143 for (i = 0; i < PM_SUSPEND_MAX; i++) {
144 if (!pm_states[i])
145 continue;
146 if (strcmp(pm_states[i], value) != 0)
147 continue;
148 test_state = (__force suspend_state_t) i;
149 return 0;
150 }
151 printk(warn_bad_state, value);
152 return 0;
153}
154__setup("test_suspend", setup_test_suspend);
155
156static int __init test_suspend(void)
157{
158 static char warn_no_rtc[] __initdata =
159 KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
160
161 struct rtc_device *rtc = NULL;
162 struct device *dev;
163
164
165 if (test_state == PM_SUSPEND_ON)
166 goto done;
167 if (!valid_state(test_state)) {
168 printk(warn_bad_state, pm_states[test_state]);
169 goto done;
170 }
171
172
173 dev = class_find_device(rtc_class, NULL, NULL, has_wakealarm);
174 if (dev)
175 rtc = rtc_class_open(dev_name(dev));
176 if (!rtc) {
177 printk(warn_no_rtc);
178 goto done;
179 }
180
181
182 test_wakealarm(rtc, test_state);
183 rtc_class_close(rtc);
184done:
185 return 0;
186}
187late_initcall(test_suspend);
188