1 From 69acbd477fb7024d2cfa3702036b92ecb6c8e7b5 Mon Sep 17 00:00:00 2001
2 From: Simon Kagstrom <simon.kagstrom@gmail.com>
3 Date: Fri, 15 Aug 2008 11:50:56 +0100
4 Subject: [PATCH] interface for configuring freefall/wakeup interrupts for the accelerometers
8 First: Unfortunately, the freerunner currently wakes up immediately on
9 suspend when the accelerometer IRQ is selected as a wakeup source. I'm
10 posting this for comments and if someone else wants to have a look at
13 The patch should be safe to apply even though the sleep portion doesn't
14 work - as long as it's configured it will not disturb anything.
18 lis302dl-configure-wakeup-interrupts.patch
20 From: simon.kagstrom <simon.kagstrom@gmail.com>
22 First: Unfortunately, the freerunner currently wakes up immediately on
23 suspend when the accelerometer IRQ is selected as a wakeup source.
26 Add configuration of wakeup/freefall interrupts through a sysfs
27 interface. Configuration is done through echoing a value of the
30 X Y Z THRESHOLD DURATION SPEC
32 to freefall_wakeup_1/2. X, Y and Z are threshold values, given as a
33 value > 0, < 0 or 0 to specify if an interrupt should be generated for
34 high or low thresholds or neither (off). THRESHOLD specifies the
35 threshold that must be exceeded. DURATION specifies the time in
36 milliseconds for which the acceleration should be measured. SPEC is
37 either '1' or '0' and specifies if the thresholds should be taken all
38 together or one at a time ('and' or 'or' mode).
40 Echoing '0' to the file turns off the interrupts.
44 echo "1 1 1 60 60 0" > freefall_wakeup_1 # Turn on x,y,z, 60ms/60 threshold, or-mode
45 echo "0" > freefall_wakeup_1 # Turn off interrupt
47 The hardware supports two simulataneous wakeup sources to be configured,
48 but the freerunner only connects one of the interrupt outputs. The patch
49 exports both. Similarly, only the "top" accelerometer can be used as a
50 wake-up source, and it's not possible to generate DATA_READY interrupts
51 while the wakeup interrupts are active.
53 Signed-off-by: Simon Kagstrom <simon.kagstrom@gmail.com>
55 drivers/input/misc/lis302dl.c | 232 +++++++++++++++++++++++++++++++++++++++--
56 include/linux/lis302dl.h | 8 +-
57 2 files changed, 227 insertions(+), 13 deletions(-)
59 diff --git a/drivers/input/misc/lis302dl.c b/drivers/input/misc/lis302dl.c
60 index b97cae7..3f6d0eb 100644
61 --- a/drivers/input/misc/lis302dl.c
62 +++ b/drivers/input/misc/lis302dl.c
63 @@ -248,10 +248,212 @@ static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
65 static DEVICE_ATTR(dump, S_IRUGO, lis302dl_dump, NULL);
67 +static int freefall_ms_to_duration(struct lis302dl_info *lis, int ms)
69 + u_int8_t r = reg_read(lis, LIS302DL_REG_CTRL1);
71 + /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
72 + * on 100 ms the stepping is 10ms */
73 + if ( r & LIS302DL_CTRL1_DR ) {
78 + return (ms * 10) / 25;
81 + /* Too large value */
87 +static int freefall_duration_to_ms(struct lis302dl_info *lis, int duration)
89 + u_int8_t r = reg_read(lis, LIS302DL_REG_CTRL1);
91 + if ( r & LIS302DL_CTRL1_DR )
92 + return (duration * 25) / 10;
94 + return duration * 10;
97 +/* Configure freefall/wakeup interrupts */
98 +static ssize_t set_freefall_common(int which, struct device *dev, struct device_attribute *attr,
99 + const char *buf, size_t count)
101 + struct lis302dl_info *lis = dev_get_drvdata(dev);
102 + u_int8_t x_lo, y_lo, z_lo;
103 + u_int8_t x_hi, y_hi, z_hi;
107 + int r_ths = LIS302DL_REG_FF_WU_THS_1; /* registers, assume first pin */
108 + int r_duration = LIS302DL_REG_FF_WU_DURATION_1;
109 + int r_cfg = LIS302DL_REG_FF_WU_CFG_1;
110 + int flag_mask = LIS302DL_F_WUP_FF_1;
111 + int intmode = LIS302DL_INTMODE_FF_WU_1;
114 + unsigned long flags;
116 + /* Configure for second freefall/wakeup pin */
118 + r_ths = LIS302DL_REG_FF_WU_THS_2;
119 + r_duration = LIS302DL_REG_FF_WU_DURATION_2;
120 + r_cfg = LIS302DL_REG_FF_WU_CFG_2;
121 + flag_mask = LIS302DL_F_WUP_FF_2;
122 + intmode = LIS302DL_INTMODE_FF_WU_2;
124 + printk(KERN_WARNING "Configuring second freefall / wakeup interrupt\n");
127 + /* Parse the input */
128 + if (strcmp(buf, "0\n") == 0)
130 + /* Turn off the interrupt */
131 + local_save_flags(flags);
132 + if ( (lis->flags & LIS302DL_F_IRQ_WAKE) != 0 )
133 + disable_irq_wake(lis->spi_dev->irq);
134 + lis302dl_int_mode(lis->spi_dev, which, LIS302DL_INTMODE_DATA_READY);
135 + lis->flags &= ~(flag_mask | LIS302DL_F_IRQ_WAKE);
137 + reg_write(lis, r_cfg, 0);
138 + reg_write(lis, r_ths, 0);
139 + reg_write(lis, r_duration, 0);
141 + /* Power off unless the input subsystem is using the device */
142 + if ( (lis->flags & LIS302DL_F_INPUT_OPEN) == 0 )
143 + reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD, 0);
145 + local_irq_restore(flags);
150 + if (sscanf(buf, "%d %d %d %d %d %d", &x, &y, &z, &threshold, &ms, &and_events) != 6)
153 + duration = freefall_ms_to_duration(lis, ms);
158 + if (threshold < 0 || threshold > 127)
161 + /* Interrupt flags */
162 + x_lo = x < 0 ? LIS302DL_FFWUCFG_XLIE : 0;
163 + y_lo = y < 0 ? LIS302DL_FFWUCFG_YLIE : 0;
164 + z_lo = z < 0 ? LIS302DL_FFWUCFG_ZLIE : 0;
165 + x_hi = x > 0 ? LIS302DL_FFWUCFG_XHIE : 0;
166 + y_hi = y > 0 ? LIS302DL_FFWUCFG_YHIE : 0;
167 + z_hi = z > 0 ? LIS302DL_FFWUCFG_ZHIE : 0;
169 + /* Setup the configuration registers */
170 + local_save_flags(flags);
171 + reg_write(lis, r_cfg, 0); /* First zero to get to a known state */
172 + reg_write(lis, r_cfg,
173 + (and_events ? LIS302DL_FFWUCFG_AOI : 0) |
174 + x_lo | x_hi | y_lo | y_hi | z_lo | z_hi);
175 + reg_write(lis, r_ths, threshold & ~LIS302DL_FFWUTHS_DCRM);
176 + reg_write(lis, r_duration, duration);
178 + /* Route the interrupt for wakeup */
179 + lis302dl_int_mode(lis->spi_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) == 0 )
184 + enable_irq_wake(lis->spi_dev->irq);
186 + lis->flags |= flag_mask | LIS302DL_F_IRQ_WAKE;
187 + reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD,
188 + LIS302DL_CTRL1_PD);
189 + local_irq_restore(flags);
194 +static ssize_t set_freefall_1(struct device *dev, struct device_attribute *attr,
195 + const char *buf, size_t count)
197 + return set_freefall_common(1, dev, attr, buf, count);
199 +static ssize_t set_freefall_2(struct device *dev, struct device_attribute *attr,
200 + const char *buf, size_t count)
202 + return set_freefall_common(2, dev, attr, buf, count);
206 +static ssize_t show_freefall_common(int which, struct device *dev,
207 + struct device_attribute *attr, char *buf)
209 + struct lis302dl_info *lis = dev_get_drvdata(dev);
211 + u_int8_t threshold;
215 + int r_ths = LIS302DL_REG_FF_WU_THS_1; /* registers, assume first pin */
216 + int r_duration = LIS302DL_REG_FF_WU_DURATION_1;
217 + int r_cfg = LIS302DL_REG_FF_WU_CFG_1;
218 + int r_src = LIS302DL_REG_FF_WU_SRC_1;
220 + /* Configure second freefall/wakeup pin */
222 + r_ths = LIS302DL_REG_FF_WU_THS_2;
223 + r_duration = LIS302DL_REG_FF_WU_DURATION_2;
224 + r_cfg = LIS302DL_REG_FF_WU_CFG_2;
225 + r_src = LIS302DL_REG_FF_WU_SRC_2;
227 + config = reg_read(lis, r_cfg);
228 + threshold = reg_read(lis, r_ths);
229 + duration = reg_read(lis, r_duration);
230 + r4 = reg_read(lis, r_src);
231 + r5 = reg_read(lis, LIS302DL_REG_CTRL3);
233 + /* All events off? */
234 + if ((config & (LIS302DL_FFWUCFG_XLIE | LIS302DL_FFWUCFG_XHIE |
235 + LIS302DL_FFWUCFG_YLIE | LIS302DL_FFWUCFG_YHIE |
236 + LIS302DL_FFWUCFG_ZLIE | LIS302DL_FFWUCFG_ZHIE)) == 0)
237 + return sprintf(buf, "off\n");
239 + return sprintf(buf,"%s events, %s interrupt, duration %d, threshold %d, "
240 + "enabled: %s %s %s %s %s %s\n",
241 + (config & LIS302DL_FFWUCFG_AOI) == 0 ? "or" : "and",
242 + (config & LIS302DL_FFWUCFG_LIR) == 0 ? "don't latch" : "latch",
243 + freefall_duration_to_ms(lis, duration), threshold,
244 + (config & LIS302DL_FFWUCFG_XLIE) == 0 ? "---" : "xlo",
245 + (config & LIS302DL_FFWUCFG_XHIE) == 0 ? "---" : "xhi",
246 + (config & LIS302DL_FFWUCFG_YLIE) == 0 ? "---" : "ylo",
247 + (config & LIS302DL_FFWUCFG_YHIE) == 0 ? "---" : "yhi",
248 + (config & LIS302DL_FFWUCFG_ZLIE) == 0 ? "---" : "zlo",
249 + (config & LIS302DL_FFWUCFG_ZHIE) == 0 ? "---" : "zhi");
252 +static ssize_t show_freefall_1(struct device *dev,
253 + struct device_attribute *attr, char *buf)
255 + return show_freefall_common(1, dev, attr, buf);
258 +static ssize_t show_freefall_2(struct device *dev,
259 + struct device_attribute *attr, char *buf)
261 + return show_freefall_common(2, dev, attr, buf);
264 +static DEVICE_ATTR(freefall_wakeup_1, S_IRUGO | S_IWUSR, show_freefall_1, set_freefall_1);
265 +static DEVICE_ATTR(freefall_wakeup_2, S_IRUGO | S_IWUSR, show_freefall_2, set_freefall_2);
267 static struct attribute *lis302dl_sysfs_entries[] = {
268 &dev_attr_sample_rate.attr,
269 &dev_attr_full_scale.attr,
271 + &dev_attr_freefall_wakeup_1.attr,
272 + &dev_attr_freefall_wakeup_2.attr,
276 @@ -274,6 +476,8 @@ static int lis302dl_input_open(struct input_dev *inp)
277 reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, ctrl1);
278 local_irq_restore(flags);
280 + lis->flags |= LIS302DL_F_INPUT_OPEN;
282 /* kick it off -- since we are edge triggered, if we missed the edge
283 * permanent low interrupt is death for us */
284 (lis->pdata->lis302dl_bitbang_read)(lis);
285 @@ -294,6 +498,7 @@ static void lis302dl_input_close(struct input_dev *inp)
286 * only see close() for the close of the last user, we can safely
287 * disable the data ready events */
288 reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, 0x00);
289 + lis->flags &= ~LIS302DL_F_INPUT_OPEN;
291 /* however, don't power down the whole device if still needed */
292 if (!(lis->flags & LIS302DL_F_WUP_FF ||
293 @@ -406,9 +611,9 @@ static int __devinit lis302dl_probe(struct spi_device *spi)
294 reg_write(lis, LIS302DL_REG_CTRL2, 0);
295 reg_write(lis, LIS302DL_REG_CTRL3, LIS302DL_CTRL3_PP_OD |
297 - reg_write(lis, LIS302DL_REG_FF_WU_THS_1, 0x14);
298 + reg_write(lis, LIS302DL_REG_FF_WU_THS_1, 0x0);
299 reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, 0x00);
300 - reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, 0x95);
301 + reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, 0x0);
303 /* start off in powered down mode; we power up when someone opens us */
304 reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_Xen |
305 @@ -486,6 +691,12 @@ static int lis302dl_suspend(struct spi_device *spi, pm_message_t state)
307 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
311 + /* determine if we want to wake up from the accel. */
312 + if (lis->flags & LIS302DL_F_WUP_FF ||
313 + lis->flags & LIS302DL_F_WUP_CLICK)
316 disable_irq(lis->spi_dev->irq);
317 local_save_flags(flags);
318 @@ -528,15 +739,10 @@ static int lis302dl_suspend(struct spi_device *spi, pm_message_t state)
319 lis->regs[LIS302DL_REG_CLICK_WINDOW] =
320 reg_read(lis, LIS302DL_REG_CLICK_WINDOW);
322 - /* determine if we want to wake up from the accel. */
323 - if (!(lis->flags & LIS302DL_F_WUP_FF ||
324 - lis->flags & LIS302DL_F_WUP_CLICK)) {
327 - tmp = reg_read(lis, LIS302DL_REG_CTRL1);
328 - tmp &= ~LIS302DL_CTRL1_PD;
329 - reg_write(lis, LIS302DL_REG_CTRL1, tmp);
332 + tmp = reg_read(lis, LIS302DL_REG_CTRL1);
333 + tmp &= ~LIS302DL_CTRL1_PD;
334 + reg_write(lis, LIS302DL_REG_CTRL1, tmp);
336 /* place our IO to the device in sleep-compatible states */
337 (lis->pdata->lis302dl_suspend_io)(lis, 0);
338 @@ -551,6 +757,10 @@ static int lis302dl_resume(struct spi_device *spi)
339 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
342 + if (lis->flags & LIS302DL_F_WUP_FF ||
343 + lis->flags & LIS302DL_F_WUP_CLICK)
346 local_save_flags(flags);
348 /* get our IO to the device back in operational states */
349 diff --git a/include/linux/lis302dl.h b/include/linux/lis302dl.h
350 index e4a44f5..7daa8b3 100644
351 --- a/include/linux/lis302dl.h
352 +++ b/include/linux/lis302dl.h
353 @@ -129,10 +129,14 @@ enum lis302dl_reg_cloik_src {
355 #define LIS302DL_WHO_AM_I_MAGIC 0x3b
357 -#define LIS302DL_F_WUP_FF 0x0001 /* wake up from free fall */
358 -#define LIS302DL_F_WUP_CLICK 0x0002
359 +#define LIS302DL_F_WUP_FF_1 0x0001 /* wake up from free fall */
360 +#define LIS302DL_F_WUP_FF_2 0x0002
361 +#define LIS302DL_F_WUP_FF 0x0003
362 +#define LIS302DL_F_WUP_CLICK 0x0004
363 #define LIS302DL_F_POWER 0x0010
364 #define LIS302DL_F_FS 0x0020 /* ADC full scale */
365 +#define LIS302DL_F_INPUT_OPEN 0x0040 /* Set if input device is opened */
366 +#define LIS302DL_F_IRQ_WAKE 0x0080 /* IRQ is setup in wake mode */
369 #endif /* _LINUX_LIS302DL_H */