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>
7 * ability to set acceleration threshold added by:
8 * Simon Kagstrom <simon.kagstrom@gmail.com>
10 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * * statistics for overflow events
29 * * configuration interface (sysfs) for
30 * * enable/disable x/y/z axis data ready
31 * * enable/disable resume from freee fall / click
32 * * free fall / click parameters
33 * * high pass filter parameters
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/module.h>
38 #include <linux/device.h>
39 #include <linux/platform_device.h>
40 #include <linux/delay.h>
41 #include <linux/irq.h>
42 #include <linux/interrupt.h>
43 #include <linux/sysfs.h>
44 #include <linux/spi/spi.h>
46 #include <linux/lis302dl.h>
48 static uint8_t lis_reg_read(struct lis302dl_info
*lis
, uint8_t reg
)
50 return spi_w8r8(lis
->spi
, 0xc0 | reg
);
53 static void lis_reg_write(struct lis302dl_info
*lis
, uint8_t reg
, uint8_t val
)
55 uint8_t data
[2] = {reg
, val
};
57 spi_write(lis
->spi
, data
, sizeof(data
));
60 static void lis_reg_set_bit_mask(struct lis302dl_info
*lis
, uint8_t reg
, uint8_t mask
,
67 tmp
= lis_reg_read(lis
, reg
);
70 lis_reg_write(lis
, reg
, tmp
);
73 static int __ms_to_duration(struct lis302dl_info
*lis
, int ms
)
75 /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
76 * on 100 ms the stepping is 10ms */
77 if (lis
->flags
& LIS302DL_F_DR
)
78 return min((ms
* 10) / 25, 637);
80 return min(ms
/ 10, 2550);
83 static int __duration_to_ms(struct lis302dl_info
*lis
, int duration
)
85 if (lis
->flags
& LIS302DL_F_DR
)
86 return (duration
* 25) / 10;
91 static uint8_t __mg_to_threshold(struct lis302dl_info
*lis
, int mg
)
93 /* If FS is set each bit is 71mg, otherwise 18mg. The THS register
94 * has 7 bits for the threshold value */
95 if (lis
->flags
& LIS302DL_F_FS
)
96 return min(mg
/ 71, 127);
98 return min(mg
/ 18, 127);
101 static int __threshold_to_mg(struct lis302dl_info
*lis
, uint8_t threshold
)
103 if (lis
->flags
& LIS302DL_F_FS
)
104 return threshold
* 71;
106 return threshold
* 18;
109 /* interrupt handling related */
111 enum lis302dl_intmode
{
112 LIS302DL_INTMODE_GND
= 0x00,
113 LIS302DL_INTMODE_FF_WU_1
= 0x01,
114 LIS302DL_INTMODE_FF_WU_2
= 0x02,
115 LIS302DL_INTMODE_FF_WU_12
= 0x03,
116 LIS302DL_INTMODE_DATA_READY
= 0x04,
117 LIS302DL_INTMODE_CLICK
= 0x07,
120 static void lis302dl_set_int_mode(struct device
*dev
, int int_pin
,
121 enum lis302dl_intmode mode
)
123 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
126 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL3
, 0x07, mode
);
129 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL3
, 0x38, mode
<< 3);
136 static void __enable_wakeup(struct lis302dl_info
*lis
)
138 lis_reg_write(lis
, LIS302DL_REG_CTRL1
, 0);
140 /* First zero to get to a known state */
141 lis_reg_write(lis
, LIS302DL_REG_FF_WU_CFG_1
, LIS302DL_FFWUCFG_XHIE
|
142 LIS302DL_FFWUCFG_YHIE
| LIS302DL_FFWUCFG_ZHIE
|
143 LIS302DL_FFWUCFG_LIR
);
144 lis_reg_write(lis
, LIS302DL_REG_FF_WU_THS_1
,
145 __mg_to_threshold(lis
, lis
->wakeup
.threshold
));
146 lis_reg_write(lis
, LIS302DL_REG_FF_WU_DURATION_1
,
147 __ms_to_duration(lis
, lis
->wakeup
.duration
));
149 /* Route the interrupt for wakeup */
150 lis302dl_set_int_mode(lis
->dev
, 1,
151 LIS302DL_INTMODE_FF_WU_1
);
153 lis_reg_read(lis
, LIS302DL_REG_HP_FILTER_RESET
);
154 lis_reg_read(lis
, LIS302DL_REG_OUT_X
);
155 lis_reg_read(lis
, LIS302DL_REG_OUT_Y
);
156 lis_reg_read(lis
, LIS302DL_REG_OUT_Z
);
157 lis_reg_read(lis
, LIS302DL_REG_STATUS
);
158 lis_reg_read(lis
, LIS302DL_REG_FF_WU_SRC_1
);
159 lis_reg_read(lis
, LIS302DL_REG_FF_WU_SRC_2
);
160 lis_reg_write(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_PD
| 7);
163 static void __enable_data_collection(struct lis302dl_info
*lis
)
165 u_int8_t ctrl1
= LIS302DL_CTRL1_PD
| LIS302DL_CTRL1_Xen
|
166 LIS302DL_CTRL1_Yen
| LIS302DL_CTRL1_Zen
;
168 /* make sure we're powered up and generate data ready */
169 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, ctrl1
, ctrl1
);
171 /* If the threshold is zero, let the device generated an interrupt
173 if (lis
->threshold
== 0) {
174 lis_reg_write(lis
, LIS302DL_REG_CTRL2
, 0);
175 lis302dl_set_int_mode(lis
->dev
, 1, LIS302DL_INTMODE_DATA_READY
);
176 lis302dl_set_int_mode(lis
->dev
, 2, LIS302DL_INTMODE_DATA_READY
);
178 lis_reg_write(lis
, LIS302DL_REG_CTRL2
,
179 LIS302DL_CTRL2_HPFF1
);
180 lis_reg_write(lis
, LIS302DL_REG_FF_WU_THS_1
,
181 __mg_to_threshold(lis
, lis
->threshold
));
182 lis_reg_write(lis
, LIS302DL_REG_FF_WU_DURATION_1
,
183 __ms_to_duration(lis
, lis
->duration
));
185 /* Clear the HP filter "starting point" */
186 lis_reg_read(lis
, LIS302DL_REG_HP_FILTER_RESET
);
187 lis_reg_write(lis
, LIS302DL_REG_FF_WU_CFG_1
,
188 LIS302DL_FFWUCFG_XHIE
| LIS302DL_FFWUCFG_YHIE
|
189 LIS302DL_FFWUCFG_ZHIE
| LIS302DL_FFWUCFG_LIR
);
190 lis302dl_set_int_mode(lis
->dev
, 1, LIS302DL_INTMODE_FF_WU_12
);
191 lis302dl_set_int_mode(lis
->dev
, 2, LIS302DL_INTMODE_FF_WU_12
);
196 static void _report_btn_single(struct input_dev
*inp
, int btn
)
198 input_report_key(inp
, btn
, 1);
200 input_report_key(inp
, btn
, 0);
203 static void _report_btn_double(struct input_dev
*inp
, int btn
)
205 input_report_key(inp
, btn
, 1);
207 input_report_key(inp
, btn
, 0);
209 input_report_key(inp
, btn
, 1);
211 input_report_key(inp
, btn
, 0);
216 static void lis302dl_bitbang_read_sample(struct lis302dl_info
*lis
)
218 uint8_t data
[(LIS302DL_REG_OUT_Z
- LIS302DL_REG_STATUS
) + 2] = {0xC0 | LIS302DL_REG_STATUS
};
219 uint8_t *read
= data
+ 1;
221 int mg_per_sample
= __threshold_to_mg(lis
, 1);
222 struct spi_message msg
;
223 struct spi_transfer t
;
225 spi_message_init(&msg
);
226 memset(&t
, 0, sizeof t
);
227 t
.len
= sizeof(data
);
228 spi_message_add_tail(&t
, &msg
);
232 /* grab the set of register containing status and XYZ data */
233 local_irq_save(flags
);
234 /* Should complete without blocking */
235 if (spi_sync(lis
->spi
, &msg
) < 0)
236 dev_err(lis
->dev
, "Error reading registers\n");
238 local_irq_restore(flags
);
240 * at the minute the test below fails 50% of the time due to
241 * a problem with level interrupts causing ISRs to get called twice.
242 * This is a workaround for that, but actually this test is still
243 * valid and the information can be used for overrrun stats.
246 /* has any kind of overrun been observed by the lis302dl? */
247 if (read
[0] & (LIS302DL_STATUS_XOR
|
248 LIS302DL_STATUS_YOR
|
249 LIS302DL_STATUS_ZOR
))
252 /* we have a valid sample set? */
253 if (read
[0] & LIS302DL_STATUS_XYZDA
) {
254 input_report_abs(lis
->input_dev
, ABS_X
, mg_per_sample
*
255 (s8
)read
[LIS302DL_REG_OUT_X
- LIS302DL_REG_STATUS
]);
256 input_report_abs(lis
->input_dev
, ABS_Y
, mg_per_sample
*
257 (s8
)read
[LIS302DL_REG_OUT_Y
- LIS302DL_REG_STATUS
]);
258 input_report_abs(lis
->input_dev
, ABS_Z
, mg_per_sample
*
259 (s8
)read
[LIS302DL_REG_OUT_Z
- LIS302DL_REG_STATUS
]);
261 input_sync(lis
->input_dev
);
263 printk("invalid sample\n");
267 /* acknowledge the wakeup source */
268 lis_reg_read(lis
, LIS302DL_REG_FF_WU_SRC_1
);
269 enable_irq(lis
->pdata
->interrupt
);
272 static void lis302dl_irq_worker(struct work_struct
*work
) {
273 struct lis302dl_info
*lis
= container_of(work
, struct lis302dl_info
, work
);
274 lis302dl_bitbang_read_sample(lis
);
277 static irqreturn_t
lis302dl_interrupt(int irq
, void *_lis
)
279 struct lis302dl_info
*lis
= _lis
;
280 disable_irq_nosync(lis
->pdata
->interrupt
);
281 schedule_work(&lis
->work
);
287 static ssize_t
show_overruns(struct device
*dev
, struct device_attribute
*attr
,
290 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
292 return sprintf(buf
, "%u\n", lis
->overruns
);
295 static DEVICE_ATTR(overruns
, S_IRUGO
, show_overruns
, NULL
);
297 static ssize_t
show_rate(struct device
*dev
, struct device_attribute
*attr
,
300 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
304 local_irq_save(flags
);
305 ctrl1
= lis_reg_read(lis
, LIS302DL_REG_CTRL1
);
306 local_irq_restore(flags
);
308 return sprintf(buf
, "%d\n", ctrl1
& LIS302DL_CTRL1_DR
? 400 : 100);
311 static ssize_t
set_rate(struct device
*dev
, struct device_attribute
*attr
,
312 const char *buf
, size_t count
)
314 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
317 local_irq_save(flags
);
319 if (!strcmp(buf
, "400\n")) {
320 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_DR
,
322 lis
->flags
|= LIS302DL_F_DR
;
324 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_DR
,
326 lis
->flags
&= ~LIS302DL_F_DR
;
328 local_irq_restore(flags
);
333 static DEVICE_ATTR(sample_rate
, S_IRUGO
| S_IWUSR
, show_rate
, set_rate
);
335 static ssize_t
show_scale(struct device
*dev
, struct device_attribute
*attr
,
338 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
342 local_irq_save(flags
);
343 ctrl1
= lis_reg_read(lis
, LIS302DL_REG_CTRL1
);
344 local_irq_restore(flags
);
346 return sprintf(buf
, "%s\n", ctrl1
& LIS302DL_CTRL1_FS
? "9.2" : "2.3");
349 static ssize_t
set_scale(struct device
*dev
, struct device_attribute
*attr
,
350 const char *buf
, size_t count
)
352 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
355 local_irq_save(flags
);
357 if (!strcmp(buf
, "9.2\n")) {
358 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_FS
,
360 lis
->flags
|= LIS302DL_F_FS
;
362 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_FS
,
364 lis
->flags
&= ~LIS302DL_F_FS
;
367 if (lis
->flags
& LIS302DL_F_INPUT_OPEN
)
368 __enable_data_collection(lis
);
370 local_irq_restore(flags
);
375 static DEVICE_ATTR(full_scale
, S_IRUGO
| S_IWUSR
, show_scale
, set_scale
);
377 static ssize_t
show_threshold(struct device
*dev
, struct device_attribute
*attr
,
380 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
382 /* Display the device view of the threshold setting */
383 return sprintf(buf
, "%d\n", __threshold_to_mg(lis
,
384 __mg_to_threshold(lis
, lis
->threshold
)));
387 static ssize_t
set_threshold(struct device
*dev
, struct device_attribute
*attr
,
388 const char *buf
, size_t count
)
390 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
393 if (sscanf(buf
, "%u\n", &val
) != 1)
395 /* 8g is the maximum if FS is 1 */
399 /* Set the threshold and write it out if the device is used */
400 lis
->threshold
= val
;
402 if (lis
->flags
& LIS302DL_F_INPUT_OPEN
) {
405 local_irq_save(flags
);
406 __enable_data_collection(lis
);
407 local_irq_restore(flags
);
413 static DEVICE_ATTR(threshold
, S_IRUGO
| S_IWUSR
, show_threshold
, set_threshold
);
415 static ssize_t
show_duration(struct device
*dev
, struct device_attribute
*attr
,
418 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
420 return sprintf(buf
, "%d\n", __duration_to_ms(lis
,
421 __ms_to_duration(lis
, lis
->duration
)));
424 static ssize_t
set_duration(struct device
*dev
, struct device_attribute
*attr
,
425 const char *buf
, size_t count
)
427 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
430 if (sscanf(buf
, "%u\n", &val
) != 1)
436 if (lis
->flags
& LIS302DL_F_INPUT_OPEN
)
437 lis_reg_write(lis
, LIS302DL_REG_FF_WU_DURATION_1
,
438 __ms_to_duration(lis
, lis
->duration
));
443 static DEVICE_ATTR(duration
, S_IRUGO
| S_IWUSR
, show_duration
, set_duration
);
445 static ssize_t
lis302dl_dump(struct device
*dev
, struct device_attribute
*attr
,
448 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
454 local_irq_save(flags
);
456 for (n
= 0; n
< sizeof(reg
); n
++)
457 reg
[n
] = lis_reg_read(lis
, n
);
459 local_irq_restore(flags
);
461 for (n
= 0; n
< sizeof(reg
); n
+= 16) {
462 hex_dump_to_buffer(reg
+ n
, 16, 16, 1, end
, 128, 0);
470 static DEVICE_ATTR(dump
, S_IRUGO
, lis302dl_dump
, NULL
);
472 /* Configure freefall/wakeup interrupts */
473 static ssize_t
set_wakeup_threshold(struct device
*dev
,
474 struct device_attribute
*attr
, const char *buf
, size_t count
)
476 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
477 unsigned int threshold
;
479 if (sscanf(buf
, "%u\n", &threshold
) != 1)
482 if (threshold
> 8000)
485 /* Zero turns the feature off */
486 if (threshold
== 0) {
487 if (lis
->flags
& LIS302DL_F_IRQ_WAKE
) {
488 disable_irq_wake(lis
->pdata
->interrupt
);
489 lis
->flags
&= ~LIS302DL_F_IRQ_WAKE
;
495 lis
->wakeup
.threshold
= threshold
;
497 if (!(lis
->flags
& LIS302DL_F_IRQ_WAKE
)) {
498 enable_irq_wake(lis
->pdata
->interrupt
);
499 lis
->flags
|= LIS302DL_F_IRQ_WAKE
;
505 static ssize_t
show_wakeup_threshold(struct device
*dev
,
506 struct device_attribute
*attr
, char *buf
)
508 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
510 /* All events off? */
511 if (lis
->wakeup
.threshold
== 0)
512 return sprintf(buf
, "off\n");
514 return sprintf(buf
, "%u\n", lis
->wakeup
.threshold
);
517 static DEVICE_ATTR(wakeup_threshold
, S_IRUGO
| S_IWUSR
, show_wakeup_threshold
,
518 set_wakeup_threshold
);
520 static ssize_t
set_wakeup_duration(struct device
*dev
,
521 struct device_attribute
*attr
, const char *buf
, size_t count
)
523 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
524 unsigned int duration
;
526 if (sscanf(buf
, "%u\n", &duration
) != 1)
532 lis
->wakeup
.duration
= duration
;
537 static ssize_t
show_wakeup_duration(struct device
*dev
,
538 struct device_attribute
*attr
, char *buf
)
540 struct lis302dl_info
*lis
= dev_get_drvdata(dev
);
542 return sprintf(buf
, "%u\n", lis
->wakeup
.duration
);
545 static DEVICE_ATTR(wakeup_duration
, S_IRUGO
| S_IWUSR
, show_wakeup_duration
,
546 set_wakeup_duration
);
548 static struct attribute
*lis302dl_sysfs_entries
[] = {
549 &dev_attr_sample_rate
.attr
,
550 &dev_attr_full_scale
.attr
,
551 &dev_attr_threshold
.attr
,
552 &dev_attr_duration
.attr
,
554 &dev_attr_wakeup_threshold
.attr
,
555 &dev_attr_wakeup_duration
.attr
,
556 &dev_attr_overruns
.attr
,
560 static struct attribute_group lis302dl_attr_group
= {
562 .attrs
= lis302dl_sysfs_entries
,
565 /* input device handling and driver core interaction */
567 static int lis302dl_input_open(struct input_dev
*inp
)
569 struct lis302dl_info
*lis
= input_get_drvdata(inp
);
572 local_irq_save(flags
);
574 __enable_data_collection(lis
);
575 lis
->flags
|= LIS302DL_F_INPUT_OPEN
;
577 local_irq_restore(flags
);
582 static void lis302dl_input_close(struct input_dev
*inp
)
584 struct lis302dl_info
*lis
= input_get_drvdata(inp
);
585 u_int8_t ctrl1
= LIS302DL_CTRL1_Xen
| LIS302DL_CTRL1_Yen
|
589 local_irq_save(flags
);
591 /* since the input core already serializes access and makes sure we
592 * only see close() for the close of the last user, we can safely
593 * disable the data ready events */
594 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, ctrl1
, 0x00);
595 lis
->flags
&= ~LIS302DL_F_INPUT_OPEN
;
597 /* however, don't power down the whole device if still needed */
598 if (!(lis
->flags
& LIS302DL_F_WUP_FF
||
599 lis
->flags
& LIS302DL_F_WUP_CLICK
)) {
600 lis_reg_set_bit_mask(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_PD
,
603 local_irq_restore(flags
);
606 /* get the device to reload its coefficients from EEPROM and wait for it
610 static int __lis302dl_reset_device(struct lis302dl_info
*lis
)
614 lis_reg_write(lis
, LIS302DL_REG_CTRL2
,
615 LIS302DL_CTRL2_BOOT
| LIS302DL_CTRL2_FDS
);
617 while ((lis_reg_read(lis
, LIS302DL_REG_CTRL2
)
618 & LIS302DL_CTRL2_BOOT
) && (timeout
--))
621 return !!(timeout
< 0);
624 static int __devinit
lis302dl_probe(struct spi_device
*spi
)
627 struct lis302dl_info
*lis
;
630 struct lis302dl_platform_data
*pdata
= spi
->dev
.platform_data
;
632 spi
->mode
= SPI_MODE_3
;
635 dev_err(&spi
->dev
, "spi_setup failed\n");
639 lis
= kzalloc(sizeof(*lis
), GFP_KERNEL
);
643 lis
->dev
= &spi
->dev
;
646 dev_set_drvdata(lis
->dev
, lis
);
650 rc
= sysfs_create_group(&lis
->dev
->kobj
, &lis302dl_attr_group
);
652 dev_err(lis
->dev
, "error creating sysfs group\n");
656 /* initialize input layer details */
657 lis
->input_dev
= input_allocate_device();
658 if (!lis
->input_dev
) {
659 dev_err(lis
->dev
, "Unable to allocate input device\n");
663 input_set_drvdata(lis
->input_dev
, lis
);
664 lis
->input_dev
->name
= pdata
->name
;
665 /* SPI Bus not defined as a valid bus for input subsystem*/
666 lis
->input_dev
->id
.bustype
= BUS_I2C
; /* lie about it */
667 lis
->input_dev
->open
= lis302dl_input_open
;
668 lis
->input_dev
->close
= lis302dl_input_close
;
670 rc
= input_register_device(lis
->input_dev
);
672 dev_err(lis
->dev
, "error %d registering input device\n", rc
);
676 local_irq_save(flags
);
677 /* Configure our IO */
678 (lis
->pdata
->lis302dl_suspend_io
)(lis
, 1);
680 wai
= lis_reg_read(lis
, LIS302DL_REG_WHO_AM_I
);
681 if (wai
!= LIS302DL_WHO_AM_I_MAGIC
) {
682 dev_err(lis
->dev
, "unknown who_am_i signature 0x%02x\n", wai
);
683 dev_set_drvdata(lis
->dev
, NULL
);
685 local_irq_restore(flags
);
689 set_bit(EV_ABS
, lis
->input_dev
->evbit
);
690 input_set_abs_params(lis
->input_dev
, ABS_X
, 0, 0, 0, 0);
691 input_set_abs_params(lis
->input_dev
, ABS_Y
, 0, 0, 0, 0);
692 input_set_abs_params(lis
->input_dev
, ABS_Z
, 0, 0, 0, 0);
696 memset(&lis
->wakeup
, 0, sizeof(lis
->wakeup
));
698 if (__lis302dl_reset_device(lis
))
699 dev_err(lis
->dev
, "device BOOT reload failed\n");
701 /* force us powered */
702 lis_reg_write(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_PD
|
708 lis_reg_write(lis
, LIS302DL_REG_CTRL2
, 0);
709 lis_reg_write(lis
, LIS302DL_REG_CTRL3
,
710 LIS302DL_CTRL3_PP_OD
| LIS302DL_CTRL3_IHL
);
711 lis_reg_write(lis
, LIS302DL_REG_FF_WU_THS_1
, 0x0);
712 lis_reg_write(lis
, LIS302DL_REG_FF_WU_DURATION_1
, 0x00);
713 lis_reg_write(lis
, LIS302DL_REG_FF_WU_CFG_1
, 0x0);
715 /* start off in powered down mode; we power up when someone opens us */
716 lis_reg_write(lis
, LIS302DL_REG_CTRL1
, LIS302DL_CTRL1_Xen
|
717 LIS302DL_CTRL1_Yen
| LIS302DL_CTRL1_Zen
);
719 if (pdata
->open_drain
)
720 /* switch interrupt to open collector, active-low */
721 lis_reg_write(lis
, LIS302DL_REG_CTRL3
,
722 LIS302DL_CTRL3_PP_OD
| LIS302DL_CTRL3_IHL
);
724 /* push-pull, active-low */
725 lis_reg_write(lis
, LIS302DL_REG_CTRL3
, LIS302DL_CTRL3_IHL
);
727 lis302dl_set_int_mode(lis
->dev
, 1, LIS302DL_INTMODE_GND
);
728 lis302dl_set_int_mode(lis
->dev
, 2, LIS302DL_INTMODE_GND
);
730 lis_reg_read(lis
, LIS302DL_REG_STATUS
);
731 lis_reg_read(lis
, LIS302DL_REG_FF_WU_SRC_1
);
732 lis_reg_read(lis
, LIS302DL_REG_FF_WU_SRC_2
);
733 lis_reg_read(lis
, LIS302DL_REG_CLICK_SRC
);
734 local_irq_restore(flags
);
736 dev_info(lis
->dev
, "Found %s\n", pdata
->name
);
740 INIT_WORK(&lis
->work
, lis302dl_irq_worker
);
742 set_irq_handler(lis
->pdata
->interrupt
, handle_level_irq
);
743 set_irq_type(lis
->pdata
->interrupt
, IRQ_TYPE_LEVEL_LOW
);
744 rc
= request_irq(lis
->pdata
->interrupt
, lis302dl_interrupt
,
745 IRQF_TRIGGER_FALLING
, "lis302dl", lis
);
748 dev_err(lis
->dev
, "error requesting IRQ %d\n",
749 lis
->pdata
->interrupt
);
755 input_unregister_device(lis
->input_dev
);
757 input_free_device(lis
->input_dev
);
759 sysfs_remove_group(&lis
->dev
->kobj
, &lis302dl_attr_group
);
765 static int __devexit
lis302dl_remove(struct spi_device
*spi
)
767 struct lis302dl_info
*lis
= dev_get_drvdata(&spi
->dev
);
770 /* Disable interrupts */
771 if (lis
->flags
& LIS302DL_F_IRQ_WAKE
)
772 disable_irq_wake(lis
->pdata
->interrupt
);
773 free_irq(lis
->pdata
->interrupt
, lis
);
775 /* Reset and power down the device */
776 local_irq_save(flags
);
777 lis_reg_write(lis
, LIS302DL_REG_CTRL3
, 0x00);
778 lis_reg_write(lis
, LIS302DL_REG_CTRL2
, 0x00);
779 lis_reg_write(lis
, LIS302DL_REG_CTRL1
, 0x00);
780 local_irq_restore(flags
);
782 /* Cleanup resources */
783 sysfs_remove_group(&spi
->dev
.kobj
, &lis302dl_attr_group
);
784 input_unregister_device(lis
->input_dev
);
786 input_free_device(lis
->input_dev
);
787 dev_set_drvdata(lis
->dev
, NULL
);
795 static uint8_t regs_to_save
[] = {
799 LIS302DL_REG_FF_WU_CFG_1
,
800 LIS302DL_REG_FF_WU_THS_1
,
801 LIS302DL_REG_FF_WU_DURATION_1
,
802 LIS302DL_REG_FF_WU_CFG_2
,
803 LIS302DL_REG_FF_WU_THS_2
,
804 LIS302DL_REG_FF_WU_DURATION_2
,
805 LIS302DL_REG_CLICK_CFG
,
806 LIS302DL_REG_CLICK_THSY_X
,
807 LIS302DL_REG_CLICK_THSZ
,
808 LIS302DL_REG_CLICK_TIME_LIMIT
,
809 LIS302DL_REG_CLICK_LATENCY
,
810 LIS302DL_REG_CLICK_WINDOW
,
814 static int lis302dl_suspend(struct spi_device
*spi
, pm_message_t state
)
816 struct lis302dl_info
*lis
= dev_get_drvdata(&spi
->dev
);
821 /* determine if we want to wake up from the accel. */
822 if (lis
->flags
& LIS302DL_F_WUP_CLICK
)
825 disable_irq(lis
->pdata
->interrupt
);
826 local_irq_save(flags
);
829 * When we share SPI over multiple sensors, there is a race here
830 * that one or more sensors will lose. In that case, the shared
831 * SPI bus GPIO will be in sleep mode and partially pulled down. So
832 * we explicitly put our IO into "wake" mode here before the final
833 * traffic to the sensor.
835 (lis
->pdata
->lis302dl_suspend_io
)(lis
, 1);
838 for (n
= 0; n
< ARRAY_SIZE(regs_to_save
); n
++)
839 lis
->regs
[regs_to_save
[n
]] =
840 lis_reg_read(lis
, regs_to_save
[n
]);
842 /* power down or enable wakeup */
844 if (lis
->wakeup
.threshold
== 0) {
845 tmp
= lis_reg_read(lis
, LIS302DL_REG_CTRL1
);
846 tmp
&= ~LIS302DL_CTRL1_PD
;
847 lis_reg_write(lis
, LIS302DL_REG_CTRL1
, tmp
);
849 __enable_wakeup(lis
);
851 /* place our IO to the device in sleep-compatible states */
852 (lis
->pdata
->lis302dl_suspend_io
)(lis
, 0);
854 local_irq_restore(flags
);
859 static int lis302dl_resume(struct spi_device
*spi
)
861 struct lis302dl_info
*lis
= dev_get_drvdata(&spi
->dev
);
865 if (lis
->flags
& LIS302DL_F_WUP_CLICK
)
868 local_irq_save(flags
);
870 /* get our IO to the device back in operational states */
871 (lis
->pdata
->lis302dl_suspend_io
)(lis
, 1);
873 /* resume from powerdown first! */
874 lis_reg_write(lis
, LIS302DL_REG_CTRL1
,
881 if (__lis302dl_reset_device(lis
))
882 dev_err(&spi
->dev
, "device BOOT reload failed\n");
884 lis
->regs
[LIS302DL_REG_CTRL1
] |= LIS302DL_CTRL1_PD
|
889 /* restore registers after resume */
890 for (n
= 0; n
< ARRAY_SIZE(regs_to_save
); n
++)
891 lis_reg_write(lis
, regs_to_save
[n
], lis
->regs
[regs_to_save
[n
]]);
893 /* if someone had us open, reset the non-wake threshold stuff */
894 if (lis
->flags
& LIS302DL_F_INPUT_OPEN
)
895 __enable_data_collection(lis
);
897 local_irq_restore(flags
);
898 enable_irq(lis
->pdata
->interrupt
);
903 #define lis302dl_suspend NULL
904 #define lis302dl_resume NULL
907 static struct spi_driver lis302dl_spi_driver
= {
910 .owner
= THIS_MODULE
,
913 .probe
= lis302dl_probe
,
914 .remove
= __devexit_p(lis302dl_remove
),
915 .suspend
= lis302dl_suspend
,
916 .resume
= lis302dl_resume
,
919 static int __devinit
lis302dl_init(void)
921 return spi_register_driver(&lis302dl_spi_driver
);
923 module_init(lis302dl_init
);
925 static void __exit
lis302dl_exit(void)
927 spi_unregister_driver(&lis302dl_spi_driver
);
929 module_exit(lis302dl_exit
);
931 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
932 MODULE_LICENSE("GPL");