1 From 933f18caaf8f96d1094f064312d416a2fe0b4337 Mon Sep 17 00:00:00 2001
2 From: Simon Kagstrom <simon.kagstrom@gmail.com>
3 Date: Thu, 16 Oct 2008 01:19:56 +0100
4 Subject: [PATCH] : lis302dl-wakeup-configuration.patch
6 Setup accelerometer interrupt to wake the device up
8 From: Simon Kagstrom <simon.kagstrom@gmail.com>
10 The threshold implementation recently broke this functionality. This
11 patch reinstates it again and simplifies the code a bit. It only
12 supports one of the two lis302dl interrupt sources now (which is the
13 only connected one on the openmoko). If you need to configure both, buy
19 Note that, as before, the device immediately wakes up when put to sleep
20 with the wakeup configured. The interface is therefore not currently
21 very useful, but to use it do e.g.,
23 echo 1 1 1 180 0 1 > wakeup
25 The value is of the form
27 X Y Z THRESHOLD DURATION SPEC
29 X, Y and Z are threshold values, given as a value > 0, < 0 or 0 to
30 specify if an interrupt should be generated for high or low thresholds
31 or neither (off). THRESHOLD specifies the threshold that must be
32 exceeded, in mg. DURATION specifies the time in milliseconds for which
33 the acceleration should be measured. SPEC is either '1' or '0' and
34 specifies if the thresholds should be taken all together or one at a
35 time ('and' or 'or' mode).
37 Echoing '0' to the file turns off the interrupts.
39 Signed-off-by: Simon Kagstrom <simon.kagstrom@gmail.com>
41 drivers/input/misc/lis302dl.c | 204 ++++++++++++-----------------------------
42 include/linux/lis302dl.h | 6 +
43 2 files changed, 63 insertions(+), 147 deletions(-)
45 diff --git a/drivers/input/misc/lis302dl.c b/drivers/input/misc/lis302dl.c
46 index e404a45..f743a24 100644
47 --- a/drivers/input/misc/lis302dl.c
48 +++ b/drivers/input/misc/lis302dl.c
49 @@ -133,6 +133,23 @@ static void __lis302dl_int_mode(struct device *dev, int int_pin,
53 +static void __enable_wakeup(struct lis302dl_info *lis)
55 + /* First zero to get to a known state */
56 + __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1,
58 + __reg_write(lis, LIS302DL_REG_FF_WU_THS_1,
59 + lis->wakeup.threshold);
60 + __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1,
61 + lis->wakeup.duration);
63 + /* Route the interrupt for wakeup */
64 + __lis302dl_int_mode(lis->dev, 1,
65 + LIS302DL_INTMODE_FF_WU_1);
67 + __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD);
70 static void __enable_data_collection(struct lis302dl_info *lis)
72 u_int8_t ctrl1 = LIS302DL_CTRL1_PD | LIS302DL_CTRL1_Xen |
73 @@ -395,73 +412,33 @@ static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
74 static DEVICE_ATTR(dump, S_IRUGO, lis302dl_dump, NULL);
76 /* Configure freefall/wakeup interrupts */
77 -static ssize_t set_freefall_common(int which, struct device *dev,
78 - struct device_attribute *attr, const char *buf, size_t count)
79 +static ssize_t set_wakeup(struct device *dev, struct device_attribute *attr,
80 + const char *buf, size_t count)
82 struct lis302dl_info *lis = dev_get_drvdata(dev);
83 u_int8_t x_lo, y_lo, z_lo;
84 u_int8_t x_hi, y_hi, z_hi;
88 - int r_ths = LIS302DL_REG_FF_WU_THS_1; /* registers, assume first pin */
89 - int r_duration = LIS302DL_REG_FF_WU_DURATION_1;
90 - int r_cfg = LIS302DL_REG_FF_WU_CFG_1;
91 - int flag_mask = LIS302DL_F_WUP_FF_1;
92 - int intmode = LIS302DL_INTMODE_FF_WU_1;
93 + int duration, threshold, and_events;
96 - unsigned long flags;
98 - /* Configure for second freefall/wakeup pin */
100 - r_ths = LIS302DL_REG_FF_WU_THS_2;
101 - r_duration = LIS302DL_REG_FF_WU_DURATION_2;
102 - r_cfg = LIS302DL_REG_FF_WU_CFG_2;
103 - flag_mask = LIS302DL_F_WUP_FF_2;
104 - intmode = LIS302DL_INTMODE_FF_WU_2;
106 - printk(KERN_WARNING
107 - "Configuring second freefall / wakeup interrupt\n");
110 - /* Parse the input */
111 + /* Zero turns the feature off */
112 if (strcmp(buf, "0\n") == 0) {
113 - /* Turn off the interrupt */
114 - local_irq_save(flags);
115 - if (lis->flags & LIS302DL_F_IRQ_WAKE)
116 - disable_irq_wake(lis->pdata->interrupt);
117 - __lis302dl_int_mode(lis->dev, which,
118 - LIS302DL_INTMODE_DATA_READY);
119 - lis->flags &= ~(flag_mask | LIS302DL_F_IRQ_WAKE);
121 - __reg_write(lis, r_cfg, 0);
122 - __reg_write(lis, r_ths, 0);
123 - __reg_write(lis, r_duration, 0);
124 + lis->wakeup.active = 0;
126 - /* Power off unless the input subsystem is using the device */
127 - if (!(lis->flags & LIS302DL_F_INPUT_OPEN))
128 - __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1,
129 - LIS302DL_CTRL1_PD, 0);
131 - local_irq_restore(flags);
132 + if (lis->flags & LIS302DL_F_IRQ_WAKE) {
133 + disable_irq_wake(lis->pdata->interrupt);
134 + lis->flags &= ~LIS302DL_F_IRQ_WAKE;
140 - if (sscanf(buf, "%d %d %d %d %d %d", &x, &y, &z, &threshold, &ms,
142 + if (sscanf(buf, "%d %d %d %d %d %d", &x, &y, &z, &threshold, &duration,
146 - local_irq_save(flags);
147 - duration = __ms_to_duration(lis, ms);
148 - local_irq_save(flags);
154 - if (threshold < 0 || threshold > 127)
155 + if (duration < 0 || duration > 2550 ||
156 + threshold < 0 || threshold > 8000)
159 /* Interrupt flags */
160 @@ -472,91 +449,38 @@ static ssize_t set_freefall_common(int which, struct device *dev,
161 y_hi = y > 0 ? LIS302DL_FFWUCFG_YHIE : 0;
162 z_hi = z > 0 ? LIS302DL_FFWUCFG_ZHIE : 0;
164 - /* Setup the configuration registers */
165 - local_irq_save(flags);
166 - /* First zero to get to a known state */
167 - __reg_write(lis, r_cfg, 0);
168 - __reg_write(lis, r_cfg,
169 - (and_events ? LIS302DL_FFWUCFG_AOI : 0) |
170 - x_lo | x_hi | y_lo | y_hi | z_lo | z_hi);
171 - __reg_write(lis, r_ths, threshold & ~LIS302DL_FFWUTHS_DCRM);
172 - __reg_write(lis, r_duration, duration);
173 + lis->wakeup.duration = __ms_to_duration(lis, duration);
174 + lis->wakeup.threshold = __mg_to_threshold(lis, threshold);
175 + lis->wakeup.cfg = (and_events ? LIS302DL_FFWUCFG_AOI : 0) |
176 + x_lo | x_hi | y_lo | y_hi | z_lo | z_hi;
178 - /* Route the interrupt for wakeup */
179 - __lis302dl_int_mode(lis->dev, which, intmode);
181 - /* Power up the device and note that we want to wake up from
182 - * this interrupt */
183 - if (!(lis->flags & LIS302DL_F_IRQ_WAKE))
184 + if (!(lis->flags & LIS302DL_F_IRQ_WAKE)) {
185 enable_irq_wake(lis->pdata->interrupt);
187 - lis->flags |= flag_mask | LIS302DL_F_IRQ_WAKE;
188 - __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD,
189 - LIS302DL_CTRL1_PD);
190 - local_irq_restore(flags);
191 + lis->flags |= LIS302DL_F_IRQ_WAKE;
193 + lis->wakeup.active = 1;
198 -static ssize_t set_freefall_1(struct device *dev, struct device_attribute *attr,
199 - const char *buf, size_t count)
201 - return set_freefall_common(1, dev, attr, buf, count);
203 -static ssize_t set_freefall_2(struct device *dev, struct device_attribute *attr,
204 - const char *buf, size_t count)
206 - return set_freefall_common(2, dev, attr, buf, count);
210 -static ssize_t show_freefall_common(int which, struct device *dev,
211 +static ssize_t show_wakeup(struct device *dev,
212 struct device_attribute *attr, char *buf)
214 struct lis302dl_info *lis = dev_get_drvdata(dev);
216 - u_int8_t threshold;
220 - int r_ths = LIS302DL_REG_FF_WU_THS_1; /* registers, assume first pin */
221 - int r_duration = LIS302DL_REG_FF_WU_DURATION_1;
222 - int r_cfg = LIS302DL_REG_FF_WU_CFG_1;
223 - int r_src = LIS302DL_REG_FF_WU_SRC_1;
224 - unsigned long flags;
227 - /* Configure second freefall/wakeup pin */
229 - r_ths = LIS302DL_REG_FF_WU_THS_2;
230 - r_duration = LIS302DL_REG_FF_WU_DURATION_2;
231 - r_cfg = LIS302DL_REG_FF_WU_CFG_2;
232 - r_src = LIS302DL_REG_FF_WU_SRC_2;
235 - local_irq_save(flags);
236 - config = __reg_read(lis, r_cfg);
237 - threshold = __reg_read(lis, r_ths);
238 - duration = __reg_read(lis, r_duration);
239 - r4 = __reg_read(lis, r_src);
240 - r5 = __reg_read(lis, LIS302DL_REG_CTRL3);
241 - ms = __duration_to_ms(lis, duration);
242 - local_irq_restore(flags);
245 /* All events off? */
246 - if ((config & (LIS302DL_FFWUCFG_XLIE | LIS302DL_FFWUCFG_XHIE |
247 - LIS302DL_FFWUCFG_YLIE | LIS302DL_FFWUCFG_YHIE |
248 - LIS302DL_FFWUCFG_ZLIE | LIS302DL_FFWUCFG_ZHIE)) == 0)
249 + if (!lis->wakeup.active)
250 return sprintf(buf, "off\n");
252 + config = lis->wakeup.cfg;
255 - "%s events, %s interrupt, duration %d, threshold %d, "
256 + "%s events, duration %d, threshold %d, "
257 "enabled: %s %s %s %s %s %s\n",
258 (config & LIS302DL_FFWUCFG_AOI) == 0 ? "or" : "and",
259 - (config & LIS302DL_FFWUCFG_LIR) == 0 ?
260 - "don't latch" : "latch",
262 + __duration_to_ms(lis, lis->wakeup.duration),
263 + __threshold_to_mg(lis, lis->wakeup.threshold),
264 (config & LIS302DL_FFWUCFG_XLIE) == 0 ? "---" : "xlo",
265 (config & LIS302DL_FFWUCFG_XHIE) == 0 ? "---" : "xhi",
266 (config & LIS302DL_FFWUCFG_YLIE) == 0 ? "---" : "ylo",
267 @@ -565,22 +489,7 @@ static ssize_t show_freefall_common(int which, struct device *dev,
268 (config & LIS302DL_FFWUCFG_ZHIE) == 0 ? "---" : "zhi");
271 -static ssize_t show_freefall_1(struct device *dev,
272 - struct device_attribute *attr, char *buf)
274 - return show_freefall_common(1, dev, attr, buf);
277 -static ssize_t show_freefall_2(struct device *dev,
278 - struct device_attribute *attr, char *buf)
280 - return show_freefall_common(2, dev, attr, buf);
283 -static DEVICE_ATTR(freefall_wakeup_1, S_IRUGO | S_IWUSR, show_freefall_1,
285 -static DEVICE_ATTR(freefall_wakeup_2, S_IRUGO | S_IWUSR, show_freefall_2,
287 +static DEVICE_ATTR(wakeup, S_IRUGO | S_IWUSR, show_wakeup, set_wakeup);
289 static struct attribute *lis302dl_sysfs_entries[] = {
290 &dev_attr_sample_rate.attr,
291 @@ -588,8 +497,7 @@ static struct attribute *lis302dl_sysfs_entries[] = {
292 &dev_attr_threshold.attr,
293 &dev_attr_duration.attr,
295 - &dev_attr_freefall_wakeup_1.attr,
296 - &dev_attr_freefall_wakeup_2.attr,
297 + &dev_attr_wakeup.attr,
301 @@ -715,6 +623,7 @@ static int __devinit lis302dl_probe(struct platform_device *pdev)
305 + memset(&lis->wakeup, 0, sizeof(lis->wakeup));
307 lis->input_dev->private = lis;
308 lis->input_dev->name = pdata->name;
309 @@ -845,8 +754,7 @@ static int lis302dl_suspend(struct platform_device *pdev, pm_message_t state)
312 /* determine if we want to wake up from the accel. */
313 - if (lis->flags & LIS302DL_F_WUP_FF ||
314 - lis->flags & LIS302DL_F_WUP_CLICK)
315 + if (lis->flags & LIS302DL_F_WUP_CLICK)
318 disable_irq(lis->pdata->interrupt);
319 @@ -866,10 +774,13 @@ static int lis302dl_suspend(struct platform_device *pdev, pm_message_t state)
320 lis->regs[regs_to_save[n]] =
321 __reg_read(lis, regs_to_save[n]);
324 - tmp = __reg_read(lis, LIS302DL_REG_CTRL1);
325 - tmp &= ~LIS302DL_CTRL1_PD;
326 - __reg_write(lis, LIS302DL_REG_CTRL1, tmp);
327 + /* power down or enable wakeup */
328 + if (!lis->wakeup.active) {
329 + tmp = __reg_read(lis, LIS302DL_REG_CTRL1);
330 + tmp &= ~LIS302DL_CTRL1_PD;
331 + __reg_write(lis, LIS302DL_REG_CTRL1, tmp);
333 + __enable_wakeup(lis);
335 /* place our IO to the device in sleep-compatible states */
336 (lis->pdata->lis302dl_suspend_io)(lis, 0);
337 @@ -885,8 +796,7 @@ static int lis302dl_resume(struct platform_device *pdev)
341 - if (lis->flags & LIS302DL_F_WUP_FF ||
342 - lis->flags & LIS302DL_F_WUP_CLICK)
343 + if (lis->flags & LIS302DL_F_WUP_CLICK)
346 local_irq_save(flags);
347 diff --git a/include/linux/lis302dl.h b/include/linux/lis302dl.h
348 index f7aa956..f4121d9 100644
349 --- a/include/linux/lis302dl.h
350 +++ b/include/linux/lis302dl.h
351 @@ -31,6 +31,12 @@ struct lis302dl_info {
353 unsigned int threshold;
354 unsigned int duration;