1 /* Linux kernel driver for the ST LIS302D 3-axis accelerometer
3 * Copyright (C) 2007-2008 by Openmoko, Inc.
4 * Author: Harald Welte <laforge@openmoko.org>
5 * converted to private bitbang by:
6 * Andy Green <andy@openmoko.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * * statistics for overflow events
26 * * configuration interface (sysfs) for
27 * * enable/disable x/y/z axis data ready
28 * * enable/disable resume from freee fall / click
29 * * free fall / click parameters
30 * * high pass filter parameters
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/module.h>
35 #include <linux/device.h>
36 #include <linux/platform_device.h>
37 #include <linux/delay.h>
38 #include <linux/irq.h>
39 #include <linux/interrupt.h>
40 #include <linux/sysfs.h>
42 #include <linux/lis302dl.h>
46 static void __reg_set_bit_mask(struct lis302dl_info
*lis
, u8 reg
, u8 mask
,
53 tmp
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, reg
);
56 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, reg
, tmp
);
59 /* interrupt handling related */
61 enum lis302dl_intmode
{
62 LIS302DL_INTMODE_GND
= 0x00,
63 LIS302DL_INTMODE_FF_WU_1
= 0x01,
64 LIS302DL_INTMODE_FF_WU_2
= 0x02,
65 LIS302DL_INTMODE_FF_WU_12
= 0x03,
66 LIS302DL_INTMODE_DATA_READY
= 0x04,
67 LIS302DL_INTMODE_CLICK
= 0x07,
70 static void __lis302dl_int_mode(struct device
*dev
, int int_pin
,
71 enum lis302dl_intmode mode
)
73 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
77 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL3
, 0x07, mode
);
80 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL3
, 0x38, mode
<< 3);
87 static void _report_btn_single(struct input_dev
*inp
, int btn
)
89 input_report_key(inp
, btn
, 1);
91 input_report_key(inp
, btn
, 0);
94 static void _report_btn_double(struct input_dev
*inp
, int btn
)
96 input_report_key(inp
, btn
, 1);
98 input_report_key(inp
, btn
, 0);
100 input_report_key(inp
, btn
, 1);
102 input_report_key(inp
, btn
, 0);
107 static irqreturn_t
lis302dl_interrupt(int irq
, void *_lis
)
109 struct lis302dl_info
*lis
= _lis
;
111 (lis
->pdata
->lis302dl_bitbang_read_sample
)(lis
);
117 static ssize_t
show_rate(struct device
*dev
, struct device_attribute
*attr
,
120 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
124 local_irq_save(flags
);
125 ctrl1
= (lis
->pdata
->lis302dl_bitbang_reg_read
)
126 (lis
, LIS302DL_REG_CTRL1
);
127 local_irq_restore(flags
);
129 return sprintf(buf
, "%d\n", ctrl1
& LIS302DL_CTRL1_DR
? 400 : 100);
132 static ssize_t
set_rate(struct device
*dev
, struct device_attribute
*attr
,
133 const char *buf
, size_t count
)
135 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
138 local_irq_save(flags
);
140 if (!strcmp(buf
, "400\n"))
141 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_DR
,
144 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_DR
,
146 local_irq_restore(flags
);
151 static DEVICE_ATTR(sample_rate
, S_IRUGO
| S_IWUSR
, show_rate
, set_rate
);
153 static ssize_t
show_scale(struct device
*dev
, struct device_attribute
*attr
,
156 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
160 local_irq_save(flags
);
161 ctrl1
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
,
163 local_irq_restore(flags
);
165 return sprintf(buf
, "%s\n", ctrl1
& LIS302DL_CTRL1_FS
? "9.2" : "2.3");
168 static ssize_t
set_scale(struct device
*dev
, struct device_attribute
*attr
,
169 const char *buf
, size_t count
)
171 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
174 local_irq_save(flags
);
176 if (!strcmp(buf
, "9.2\n"))
177 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_FS
,
180 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_FS
,
183 local_irq_restore(flags
);
188 static DEVICE_ATTR(full_scale
, S_IRUGO
| S_IWUSR
, show_scale
, set_scale
);
190 static ssize_t
lis302dl_dump(struct device
*dev
, struct device_attribute
*attr
,
193 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
199 local_irq_save(flags
);
201 for (n
= 0; n
< sizeof(reg
); n
++)
202 reg
[n
] = (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, n
);
204 local_irq_restore(flags
);
206 for (n
= 0; n
< sizeof(reg
); n
+= 16) {
207 hex_dump_to_buffer(reg
+ n
, 16, 16, 1, end
, 128, 0);
215 static DEVICE_ATTR(dump
, S_IRUGO
, lis302dl_dump
, NULL
);
217 static int __freefall_ms_to_duration(struct lis302dl_info
*lis
, int ms
)
219 u8 r
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_CTRL1
);
221 /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
222 * on 100 ms the stepping is 10ms */
223 if (r
& LIS302DL_CTRL1_DR
) {
228 return (ms
* 10) / 25;
231 /* Too large value */
237 static int __freefall_duration_to_ms(struct lis302dl_info
*lis
, int duration
)
239 u8 r
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_CTRL1
);
241 if (r
& LIS302DL_CTRL1_DR
)
242 return (duration
* 25) / 10;
244 return duration
* 10;
247 /* Configure freefall/wakeup interrupts */
248 static ssize_t
set_freefall_common(int which
, struct device
*dev
,
249 struct device_attribute
*attr
, const char *buf
, size_t count
)
251 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
252 u_int8_t x_lo
, y_lo
, z_lo
;
253 u_int8_t x_hi
, y_hi
, z_hi
;
257 int r_ths
= LIS302DL_REG_FF_WU_THS_1
; /* registers, assume first pin */
258 int r_duration
= LIS302DL_REG_FF_WU_DURATION_1
;
259 int r_cfg
= LIS302DL_REG_FF_WU_CFG_1
;
260 int flag_mask
= LIS302DL_F_WUP_FF_1
;
261 int intmode
= LIS302DL_INTMODE_FF_WU_1
;
266 /* Configure for second freefall/wakeup pin */
268 r_ths
= LIS302DL_REG_FF_WU_THS_2
;
269 r_duration
= LIS302DL_REG_FF_WU_DURATION_2
;
270 r_cfg
= LIS302DL_REG_FF_WU_CFG_2
;
271 flag_mask
= LIS302DL_F_WUP_FF_2
;
272 intmode
= LIS302DL_INTMODE_FF_WU_2
;
275 "Configuring second freefall / wakeup interrupt\n");
278 /* Parse the input */
279 if (strcmp(buf
, "0\n") == 0) {
280 /* Turn off the interrupt */
281 local_irq_save(flags
);
282 if (lis
->flags
& LIS302DL_F_IRQ_WAKE
)
283 disable_irq_wake(lis
->pdata
->interrupt
);
284 __lis302dl_int_mode(lis
->dev
, which
,
285 LIS302DL_INTMODE_DATA_READY
);
286 lis
->flags
&= ~(flag_mask
| LIS302DL_F_IRQ_WAKE
);
288 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, r_cfg
, 0);
289 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, r_ths
, 0);
290 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, r_duration
, 0);
292 /* Power off unless the input subsystem is using the device */
293 if (!(lis
->flags
& LIS302DL_F_INPUT_OPEN
))
294 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
,
295 LIS302DL_CTRL1_PD
, 0);
297 local_irq_restore(flags
);
302 if (sscanf(buf
, "%d %d %d %d %d %d", &x
, &y
, &z
, &threshold
, &ms
,
306 local_irq_save(flags
);
307 duration
= __freefall_ms_to_duration(lis
, ms
);
308 local_irq_save(flags
);
314 if (threshold
< 0 || threshold
> 127)
317 /* Interrupt flags */
318 x_lo
= x
< 0 ? LIS302DL_FFWUCFG_XLIE
: 0;
319 y_lo
= y
< 0 ? LIS302DL_FFWUCFG_YLIE
: 0;
320 z_lo
= z
< 0 ? LIS302DL_FFWUCFG_ZLIE
: 0;
321 x_hi
= x
> 0 ? LIS302DL_FFWUCFG_XHIE
: 0;
322 y_hi
= y
> 0 ? LIS302DL_FFWUCFG_YHIE
: 0;
323 z_hi
= z
> 0 ? LIS302DL_FFWUCFG_ZHIE
: 0;
325 /* Setup the configuration registers */
326 local_irq_save(flags
);
327 /* First zero to get to a known state */
328 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, r_cfg
, 0);
329 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, r_cfg
,
330 (and_events
? LIS302DL_FFWUCFG_AOI
: 0) |
331 x_lo
| x_hi
| y_lo
| y_hi
| z_lo
| z_hi
);
332 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, r_ths
,
333 threshold
& ~LIS302DL_FFWUTHS_DCRM
);
334 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, r_duration
, duration
);
336 /* Route the interrupt for wakeup */
337 __lis302dl_int_mode(lis
->dev
, which
, intmode
);
339 /* Power up the device and note that we want to wake up from
341 if (!(lis
->flags
& LIS302DL_F_IRQ_WAKE
))
342 enable_irq_wake(lis
->pdata
->interrupt
);
344 lis
->flags
|= flag_mask
| LIS302DL_F_IRQ_WAKE
;
345 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_PD
,
347 local_irq_restore(flags
);
352 static ssize_t
set_freefall_1(struct device
*dev
, struct device_attribute
*attr
,
353 const char *buf
, size_t count
)
355 return set_freefall_common(1, dev
, attr
, buf
, count
);
357 static ssize_t
set_freefall_2(struct device
*dev
, struct device_attribute
*attr
,
358 const char *buf
, size_t count
)
360 return set_freefall_common(2, dev
, attr
, buf
, count
);
364 static ssize_t
show_freefall_common(int which
, struct device
*dev
,
365 struct device_attribute
*attr
, char *buf
)
367 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
373 int r_ths
= LIS302DL_REG_FF_WU_THS_1
; /* registers, assume first pin */
374 int r_duration
= LIS302DL_REG_FF_WU_DURATION_1
;
375 int r_cfg
= LIS302DL_REG_FF_WU_CFG_1
;
376 int r_src
= LIS302DL_REG_FF_WU_SRC_1
;
380 /* Configure second freefall/wakeup pin */
382 r_ths
= LIS302DL_REG_FF_WU_THS_2
;
383 r_duration
= LIS302DL_REG_FF_WU_DURATION_2
;
384 r_cfg
= LIS302DL_REG_FF_WU_CFG_2
;
385 r_src
= LIS302DL_REG_FF_WU_SRC_2
;
388 local_irq_save(flags
);
389 config
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, r_cfg
);
390 threshold
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, r_ths
);
391 duration
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, r_duration
);
392 r4
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, r_src
);
393 r5
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_CTRL3
);
394 ms
= __freefall_duration_to_ms(lis
, duration
);
395 local_irq_restore(flags
);
397 /* All events off? */
398 if ((config
& (LIS302DL_FFWUCFG_XLIE
| LIS302DL_FFWUCFG_XHIE
|
399 LIS302DL_FFWUCFG_YLIE
| LIS302DL_FFWUCFG_YHIE
|
400 LIS302DL_FFWUCFG_ZLIE
| LIS302DL_FFWUCFG_ZHIE
)) == 0)
401 return sprintf(buf
, "off\n");
405 "%s events, %s interrupt, duration %d, threshold %d, "
406 "enabled: %s %s %s %s %s %s\n",
407 (config
& LIS302DL_FFWUCFG_AOI
) == 0 ? "or" : "and",
408 (config
& LIS302DL_FFWUCFG_LIR
) == 0 ?
409 "don't latch" : "latch",
411 (config
& LIS302DL_FFWUCFG_XLIE
) == 0 ? "---" : "xlo",
412 (config
& LIS302DL_FFWUCFG_XHIE
) == 0 ? "---" : "xhi",
413 (config
& LIS302DL_FFWUCFG_YLIE
) == 0 ? "---" : "ylo",
414 (config
& LIS302DL_FFWUCFG_YHIE
) == 0 ? "---" : "yhi",
415 (config
& LIS302DL_FFWUCFG_ZLIE
) == 0 ? "---" : "zlo",
416 (config
& LIS302DL_FFWUCFG_ZHIE
) == 0 ? "---" : "zhi");
419 static ssize_t
show_freefall_1(struct device
*dev
,
420 struct device_attribute
*attr
, char *buf
)
422 return show_freefall_common(1, dev
, attr
, buf
);
425 static ssize_t
show_freefall_2(struct device
*dev
,
426 struct device_attribute
*attr
, char *buf
)
428 return show_freefall_common(2, dev
, attr
, buf
);
431 static DEVICE_ATTR(freefall_wakeup_1
, S_IRUGO
| S_IWUSR
, show_freefall_1
,
433 static DEVICE_ATTR(freefall_wakeup_2
, S_IRUGO
| S_IWUSR
, show_freefall_2
,
436 static struct attribute
*lis302dl_sysfs_entries
[] = {
437 &dev_attr_sample_rate
.attr
,
438 &dev_attr_full_scale
.attr
,
440 &dev_attr_freefall_wakeup_1
.attr
,
441 &dev_attr_freefall_wakeup_2
.attr
,
445 static struct attribute_group lis302dl_attr_group
= {
447 .attrs
= lis302dl_sysfs_entries
,
450 /* input device handling and driver core interaction */
452 static int lis302dl_input_open(struct input_dev
*inp
)
454 struct lis302dl_info
*lis
= inp
->private;
455 u_int8_t ctrl1
= LIS302DL_CTRL1_PD
| LIS302DL_CTRL1_Xen
|
456 LIS302DL_CTRL1_Yen
| LIS302DL_CTRL1_Zen
;
459 local_irq_save(flags
);
460 /* make sure we're powered up and generate data ready */
461 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, ctrl1
, ctrl1
);
463 lis
->flags
|= LIS302DL_F_INPUT_OPEN
;
465 /* kick it off -- since we are edge triggered, if we missed the edge
466 * permanent low interrupt is death for us */
467 (lis
->pdata
->lis302dl_bitbang_read_sample
)(lis
);
469 local_irq_restore(flags
);
474 static void lis302dl_input_close(struct input_dev
*inp
)
476 struct lis302dl_info
*lis
= inp
->private;
477 u_int8_t ctrl1
= LIS302DL_CTRL1_Xen
| LIS302DL_CTRL1_Yen
|
481 local_irq_save(flags
);
483 /* since the input core already serializes access and makes sure we
484 * only see close() for the close of the last user, we can safely
485 * disable the data ready events */
486 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, ctrl1
, 0x00);
487 lis
->flags
&= ~LIS302DL_F_INPUT_OPEN
;
489 /* however, don't power down the whole device if still needed */
490 if (!(lis
->flags
& LIS302DL_F_WUP_FF
||
491 lis
->flags
& LIS302DL_F_WUP_CLICK
)) {
492 __reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_PD
,
495 local_irq_restore(flags
);
498 /* get the device to reload its coefficients from EEPROM and wait for it
502 static int __lis302dl_reset_device(struct lis302dl_info
*lis
)
506 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL2
,
507 LIS302DL_CTRL2_BOOT
| LIS302DL_CTRL2_FDS
);
509 while (((lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_CTRL2
)
510 & LIS302DL_CTRL2_BOOT
) && (timeout
--))
513 return !!(timeout
< 0);
516 static int __devinit
lis302dl_probe(struct platform_device
*pdev
)
519 struct lis302dl_info
*lis
;
522 struct lis302dl_platform_data
*pdata
= pdev
->dev
.platform_data
;
524 lis
= kzalloc(sizeof(*lis
), GFP_KERNEL
);
528 local_irq_save(flags
);
530 lis
->dev
= &pdev
->dev
;
532 dev_set_drvdata(lis
->dev
, lis
);
536 /* Configure our IO */
537 (lis
->pdata
->lis302dl_suspend_io
)(lis
, 1);
539 wai
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
,
540 LIS302DL_REG_WHO_AM_I
);
541 if (wai
!= LIS302DL_WHO_AM_I_MAGIC
) {
542 dev_err(lis
->dev
, "unknown who_am_i signature 0x%02x\n", wai
);
543 dev_set_drvdata(lis
->dev
, NULL
);
548 rc
= sysfs_create_group(&lis
->dev
->kobj
, &lis302dl_attr_group
);
550 dev_err(lis
->dev
, "error creating sysfs group\n");
554 /* initialize input layer details */
555 lis
->input_dev
= input_allocate_device();
556 if (!lis
->input_dev
) {
557 dev_err(lis
->dev
, "Unable to allocate input device\n");
561 set_bit(EV_REL
, lis
->input_dev
->evbit
);
562 set_bit(REL_X
, lis
->input_dev
->relbit
);
563 set_bit(REL_Y
, lis
->input_dev
->relbit
);
564 set_bit(REL_Z
, lis
->input_dev
->relbit
);
565 /* set_bit(EV_KEY, lis->input_dev->evbit);
566 set_bit(BTN_X, lis->input_dev->keybit);
567 set_bit(BTN_Y, lis->input_dev->keybit);
568 set_bit(BTN_Z, lis->input_dev->keybit);
570 lis
->input_dev
->private = lis
;
571 lis
->input_dev
->name
= pdata
->name
;
572 /* SPI Bus not defined as a valid bus for input subsystem*/
573 lis
->input_dev
->id
.bustype
= BUS_I2C
; /* lie about it */
574 lis
->input_dev
->open
= lis302dl_input_open
;
575 lis
->input_dev
->close
= lis302dl_input_close
;
577 rc
= input_register_device(lis
->input_dev
);
579 dev_err(lis
->dev
, "error %d registering input device\n", rc
);
583 if (__lis302dl_reset_device(lis
))
584 dev_err(lis
->dev
, "device BOOT reload failed\n");
586 /* force us powered */
587 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL1
,
594 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL2
, 0);
595 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL3
,
596 LIS302DL_CTRL3_PP_OD
| LIS302DL_CTRL3_IHL
);
597 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
,
598 LIS302DL_REG_FF_WU_THS_1
, 0x0);
599 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
,
600 LIS302DL_REG_FF_WU_DURATION_1
, 0x00);
601 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
,
602 LIS302DL_REG_FF_WU_CFG_1
, 0x0);
604 /* start off in powered down mode; we power up when someone opens us */
605 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL1
,
610 if (pdata
->open_drain
)
611 /* switch interrupt to open collector, active-low */
612 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
,
613 LIS302DL_REG_CTRL3
, LIS302DL_CTRL3_PP_OD
|
616 /* push-pull, active-low */
617 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
,
618 LIS302DL_REG_CTRL3
, LIS302DL_CTRL3_IHL
);
620 __lis302dl_int_mode(lis
->dev
, 1, LIS302DL_INTMODE_DATA_READY
);
621 __lis302dl_int_mode(lis
->dev
, 2, LIS302DL_INTMODE_DATA_READY
);
623 (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_STATUS
);
624 (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_FF_WU_SRC_1
);
625 (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_FF_WU_SRC_2
);
626 (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_CLICK_SRC
);
628 dev_info(lis
->dev
, "Found %s\n", pdata
->name
);
632 rc
= request_irq(pdata
->interrupt
, lis302dl_interrupt
,
633 IRQF_TRIGGER_FALLING
, "lis302dl", lis
);
635 dev_err(lis
->dev
, "error requesting IRQ %d\n",
636 lis
->pdata
->interrupt
);
639 local_irq_restore(flags
);
643 input_unregister_device(lis
->input_dev
);
645 input_free_device(lis
->input_dev
);
647 sysfs_remove_group(&lis
->dev
->kobj
, &lis302dl_attr_group
);
650 local_irq_restore(flags
);
654 static int __devexit
lis302dl_remove(struct platform_device
*pdev
)
656 struct lis302dl_info
*lis
= dev_get_drvdata(&pdev
->dev
);
659 /* Reset and power down the device */
660 local_irq_save(flags
);
661 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL3
, 0x00);
662 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL2
, 0x00);
663 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL1
, 0x00);
664 local_irq_restore(flags
);
666 /* Cleanup resources */
667 free_irq(lis
->pdata
->interrupt
, lis
);
668 sysfs_remove_group(&pdev
->dev
.kobj
, &lis302dl_attr_group
);
669 input_unregister_device(lis
->input_dev
);
671 input_free_device(lis
->input_dev
);
672 dev_set_drvdata(lis
->dev
, NULL
);
680 static u8 regs_to_save
[] = {
684 LIS302DL_REG_FF_WU_CFG_1
,
685 LIS302DL_REG_FF_WU_THS_1
,
686 LIS302DL_REG_FF_WU_DURATION_1
,
687 LIS302DL_REG_FF_WU_CFG_2
,
688 LIS302DL_REG_FF_WU_THS_2
,
689 LIS302DL_REG_FF_WU_DURATION_2
,
690 LIS302DL_REG_CLICK_CFG
,
691 LIS302DL_REG_CLICK_THSY_X
,
692 LIS302DL_REG_CLICK_THSZ
,
693 LIS302DL_REG_CLICK_TIME_LIMIT
,
694 LIS302DL_REG_CLICK_LATENCY
,
695 LIS302DL_REG_CLICK_WINDOW
,
699 static int lis302dl_suspend(struct platform_device
*pdev
, pm_message_t state
)
701 struct lis302dl_info
*lis
= dev_get_drvdata(&pdev
->dev
);
706 /* determine if we want to wake up from the accel. */
707 if (lis
->flags
& LIS302DL_F_WUP_FF
||
708 lis
->flags
& LIS302DL_F_WUP_CLICK
)
711 disable_irq(lis
->pdata
->interrupt
);
712 local_irq_save(flags
);
715 * When we share SPI over multiple sensors, there is a race here
716 * that one or more sensors will lose. In that case, the shared
717 * SPI bus GPIO will be in sleep mode and partially pulled down. So
718 * we explicitly put our IO into "wake" mode here before the final
719 * traffic to the sensor.
721 (lis
->pdata
->lis302dl_suspend_io
)(lis
, 1);
724 for (n
= 0; n
< ARRAY_SIZE(regs_to_save
); n
++)
725 lis
->regs
[regs_to_save
[n
]] =
726 (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
,
730 tmp
= (lis
->pdata
->lis302dl_bitbang_reg_read
)(lis
, LIS302DL_REG_CTRL1
);
731 tmp
&= ~LIS302DL_CTRL1_PD
;
732 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL1
, tmp
);
734 /* place our IO to the device in sleep-compatible states */
735 (lis
->pdata
->lis302dl_suspend_io
)(lis
, 0);
737 local_irq_restore(flags
);
742 static int lis302dl_resume(struct platform_device
*pdev
)
744 struct lis302dl_info
*lis
= dev_get_drvdata(&pdev
->dev
);
748 if (lis
->flags
& LIS302DL_F_WUP_FF
||
749 lis
->flags
& LIS302DL_F_WUP_CLICK
)
752 local_irq_save(flags
);
754 /* get our IO to the device back in operational states */
755 (lis
->pdata
->lis302dl_suspend_io
)(lis
, 1);
757 /* resume from powerdown first! */
758 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
, LIS302DL_REG_CTRL1
,
765 if (__lis302dl_reset_device(lis
))
766 dev_err(&pdev
->dev
, "device BOOT reload failed\n");
768 lis
->regs
[LIS302DL_REG_CTRL1
] |= LIS302DL_CTRL1_PD
|
773 /* restore registers after resume */
774 for (n
= 0; n
< ARRAY_SIZE(regs_to_save
); n
++)
775 (lis
->pdata
->lis302dl_bitbang_reg_write
)(lis
,
776 regs_to_save
[n
], lis
->regs
[regs_to_save
[n
]]);
778 local_irq_restore(flags
);
779 enable_irq(lis
->pdata
->interrupt
);
784 #define lis302dl_suspend NULL
785 #define lis302dl_resume NULL
788 static struct platform_driver lis302dl_driver
= {
791 .owner
= THIS_MODULE
,
794 .probe
= lis302dl_probe
,
795 .remove
= __devexit_p(lis302dl_remove
),
796 .suspend
= lis302dl_suspend
,
797 .resume
= lis302dl_resume
,
800 static int __devinit
lis302dl_init(void)
802 return platform_driver_register(&lis302dl_driver
);
805 static void __exit
lis302dl_exit(void)
807 platform_driver_unregister(&lis302dl_driver
);
810 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
811 MODULE_LICENSE("GPL");
813 module_init(lis302dl_init
);
814 module_exit(lis302dl_exit
);