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>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * * statistics for overflow events
28 * * configuration interface (sysfs) for
29 * * enable/disable x/y/z axis data ready
30 * * enable/disable resume from freee fall / click
31 * * free fall / click parameters
32 * * high pass filter parameters
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/module.h>
37 #include <linux/device.h>
38 #include <linux/platform_device.h>
39 #include <linux/delay.h>
40 #include <linux/irq.h>
41 #include <linux/interrupt.h>
42 #include <linux/sysfs.h>
43 #include <linux/spi/spi.h>
45 #include <linux/lis302dl.h>
47 /* Utility functions */
48 static u8 __reg_read(struct lis302dl_info *lis, u8 reg)
50 struct spi_message msg;
51 struct spi_transfer t;
52 u8 data[2] = {0xc0 | reg};
55 spi_message_init(&msg);
56 memset(&t, 0, sizeof t);
58 spi_message_add_tail(&t, &msg);
62 /* Should complete without blocking */
63 rc = spi_non_blocking_transfer(lis->spi, &msg);
65 dev_err(lis->dev, "Error reading register\n");
72 static void __reg_write(struct lis302dl_info *lis, u8 reg, u8 val)
74 struct spi_message msg;
75 struct spi_transfer t;
76 u8 data[2] = {reg, val};
78 spi_message_init(&msg);
79 memset(&t, 0, sizeof t);
81 spi_message_add_tail(&t, &msg);
85 /* Completes without blocking */
86 if (spi_non_blocking_transfer(lis->spi, &msg) < 0)
87 dev_err(lis->dev, "Error writing register\n");
90 static void __reg_set_bit_mask(struct lis302dl_info *lis, u8 reg, u8 mask,
97 tmp = __reg_read(lis, reg);
100 __reg_write(lis, reg, tmp);
103 static int __ms_to_duration(struct lis302dl_info *lis, int ms)
105 /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
106 * on 100 ms the stepping is 10ms */
107 if (lis->flags & LIS302DL_F_DR)
108 return min((ms * 10) / 25, 637);
110 return min(ms / 10, 2550);
113 static int __duration_to_ms(struct lis302dl_info *lis, int duration)
115 if (lis->flags & LIS302DL_F_DR)
116 return (duration * 25) / 10;
118 return duration * 10;
121 static u8 __mg_to_threshold(struct lis302dl_info *lis, int mg)
123 /* If FS is set each bit is 71mg, otherwise 18mg. The THS register
124 * has 7 bits for the threshold value */
125 if (lis->flags & LIS302DL_F_FS)
126 return min(mg / 71, 127);
128 return min(mg / 18, 127);
131 static int __threshold_to_mg(struct lis302dl_info *lis, u8 threshold)
133 if (lis->flags & LIS302DL_F_FS)
134 return threshold * 71;
136 return threshold * 18;
139 /* interrupt handling related */
141 enum lis302dl_intmode {
142 LIS302DL_INTMODE_GND = 0x00,
143 LIS302DL_INTMODE_FF_WU_1 = 0x01,
144 LIS302DL_INTMODE_FF_WU_2 = 0x02,
145 LIS302DL_INTMODE_FF_WU_12 = 0x03,
146 LIS302DL_INTMODE_DATA_READY = 0x04,
147 LIS302DL_INTMODE_CLICK = 0x07,
150 static void __lis302dl_int_mode(struct device *dev, int int_pin,
151 enum lis302dl_intmode mode)
153 struct lis302dl_info *lis = dev_get_drvdata(dev);
157 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x07, mode);
160 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x38, mode << 3);
167 static void __enable_wakeup(struct lis302dl_info *lis)
169 __reg_write(lis, LIS302DL_REG_CTRL1, 0);
171 /* First zero to get to a known state */
172 __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, LIS302DL_FFWUCFG_XHIE |
173 LIS302DL_FFWUCFG_YHIE | LIS302DL_FFWUCFG_ZHIE |
174 LIS302DL_FFWUCFG_LIR);
175 __reg_write(lis, LIS302DL_REG_FF_WU_THS_1,
176 __mg_to_threshold(lis, lis->wakeup.threshold));
177 __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1,
178 __ms_to_duration(lis, lis->wakeup.duration));
180 /* Route the interrupt for wakeup */
181 __lis302dl_int_mode(lis->dev, 1,
182 LIS302DL_INTMODE_FF_WU_1);
184 __reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
185 __reg_read(lis, LIS302DL_REG_OUT_X);
186 __reg_read(lis, LIS302DL_REG_OUT_Y);
187 __reg_read(lis, LIS302DL_REG_OUT_Z);
188 __reg_read(lis, LIS302DL_REG_STATUS);
189 __reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
190 __reg_read(lis, LIS302DL_REG_FF_WU_SRC_2);
191 __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD | 7);
194 static void __enable_data_collection(struct lis302dl_info *lis)
196 u_int8_t ctrl1 = LIS302DL_CTRL1_PD | LIS302DL_CTRL1_Xen |
197 LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen;
199 /* make sure we're powered up and generate data ready */
200 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, ctrl1);
202 /* If the threshold is zero, let the device generated an interrupt
204 if (lis->threshold == 0) {
205 __reg_write(lis, LIS302DL_REG_CTRL2, 0);
206 __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_DATA_READY);
207 __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_DATA_READY);
209 __reg_write(lis, LIS302DL_REG_CTRL2,
210 LIS302DL_CTRL2_HPFF1);
211 __reg_write(lis, LIS302DL_REG_FF_WU_THS_1,
212 __mg_to_threshold(lis, lis->threshold));
213 __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1,
214 __ms_to_duration(lis, lis->duration));
216 /* Clear the HP filter "starting point" */
217 __reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
218 __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1,
219 LIS302DL_FFWUCFG_XHIE | LIS302DL_FFWUCFG_YHIE |
220 LIS302DL_FFWUCFG_ZHIE | LIS302DL_FFWUCFG_LIR);
221 __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_FF_WU_12);
222 __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_FF_WU_12);
227 static void _report_btn_single(struct input_dev *inp, int btn)
229 input_report_key(inp, btn, 1);
231 input_report_key(inp, btn, 0);
234 static void _report_btn_double(struct input_dev *inp, int btn)
236 input_report_key(inp, btn, 1);
238 input_report_key(inp, btn, 0);
240 input_report_key(inp, btn, 1);
242 input_report_key(inp, btn, 0);
247 static void lis302dl_bitbang_read_sample(struct lis302dl_info *lis)
249 u8 data[(LIS302DL_REG_OUT_Z - LIS302DL_REG_STATUS) + 2] = {0xC0 | LIS302DL_REG_STATUS};
252 int mg_per_sample = __threshold_to_mg(lis, 1);
253 struct spi_message msg;
254 struct spi_transfer t;
256 spi_message_init(&msg);
257 memset(&t, 0, sizeof t);
258 t.len = sizeof(data);
259 spi_message_add_tail(&t, &msg);
263 /* grab the set of register containing status and XYZ data */
265 local_irq_save(flags);
267 /* Should complete without blocking */
268 if (spi_non_blocking_transfer(lis->spi, &msg) < 0)
269 dev_err(lis->dev, "Error reading registers\n");
271 local_irq_restore(flags);
274 * at the minute the test below fails 50% of the time due to
275 * a problem with level interrupts causing ISRs to get called twice.
276 * This is a workaround for that, but actually this test is still
277 * valid and the information can be used for overrrun stats.
280 /* has any kind of overrun been observed by the lis302dl? */
281 if (read[0] & (LIS302DL_STATUS_XOR |
282 LIS302DL_STATUS_YOR |
283 LIS302DL_STATUS_ZOR))
286 /* we have a valid sample set? */
287 if (read[0] & LIS302DL_STATUS_XYZDA) {
288 input_report_abs(lis->input_dev, ABS_X, mg_per_sample *
289 (s8)read[LIS302DL_REG_OUT_X - LIS302DL_REG_STATUS]);
290 input_report_abs(lis->input_dev, ABS_Y, mg_per_sample *
291 (s8)read[LIS302DL_REG_OUT_Y - LIS302DL_REG_STATUS]);
292 input_report_abs(lis->input_dev, ABS_Z, mg_per_sample *
293 (s8)read[LIS302DL_REG_OUT_Z - LIS302DL_REG_STATUS]);
295 input_sync(lis->input_dev);
299 /* acknowledge the wakeup source */
300 __reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
303 static irqreturn_t lis302dl_interrupt(int irq, void *_lis)
305 struct lis302dl_info *lis = _lis;
307 lis302dl_bitbang_read_sample(lis);
313 static ssize_t show_overruns(struct device *dev, struct device_attribute *attr,
316 struct lis302dl_info *lis = dev_get_drvdata(dev);
318 return sprintf(buf, "%u\n", lis->overruns);
321 static DEVICE_ATTR(overruns, S_IRUGO, show_overruns, NULL);
323 static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
326 struct lis302dl_info *lis = dev_get_drvdata(dev);
330 local_irq_save(flags);
331 ctrl1 = __reg_read(lis, LIS302DL_REG_CTRL1);
332 local_irq_restore(flags);
334 return sprintf(buf, "%d\n", ctrl1 & LIS302DL_CTRL1_DR ? 400 : 100);
337 static ssize_t set_rate(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
340 struct lis302dl_info *lis = dev_get_drvdata(dev);
343 local_irq_save(flags);
345 if (!strcmp(buf, "400\n")) {
346 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
348 lis->flags |= LIS302DL_F_DR;
350 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
352 lis->flags &= ~LIS302DL_F_DR;
354 local_irq_restore(flags);
359 static DEVICE_ATTR(sample_rate, S_IRUGO | S_IWUSR, show_rate, set_rate);
361 static ssize_t show_scale(struct device *dev, struct device_attribute *attr,
364 struct lis302dl_info *lis = dev_get_drvdata(dev);
368 local_irq_save(flags);
369 ctrl1 = __reg_read(lis, LIS302DL_REG_CTRL1);
370 local_irq_restore(flags);
372 return sprintf(buf, "%s\n", ctrl1 & LIS302DL_CTRL1_FS ? "9.2" : "2.3");
375 static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
376 const char *buf, size_t count)
378 struct lis302dl_info *lis = dev_get_drvdata(dev);
381 local_irq_save(flags);
383 if (!strcmp(buf, "9.2\n")) {
384 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
386 lis->flags |= LIS302DL_F_FS;
388 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
390 lis->flags &= ~LIS302DL_F_FS;
393 if (lis->flags & LIS302DL_F_INPUT_OPEN)
394 __enable_data_collection(lis);
396 local_irq_restore(flags);
401 static DEVICE_ATTR(full_scale, S_IRUGO | S_IWUSR, show_scale, set_scale);
403 static ssize_t show_threshold(struct device *dev, struct device_attribute *attr,
406 struct lis302dl_info *lis = dev_get_drvdata(dev);
408 /* Display the device view of the threshold setting */
409 return sprintf(buf, "%d\n", __threshold_to_mg(lis,
410 __mg_to_threshold(lis, lis->threshold)));
413 static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
414 const char *buf, size_t count)
416 struct lis302dl_info *lis = dev_get_drvdata(dev);
419 if (sscanf(buf, "%u\n", &val) != 1)
421 /* 8g is the maximum if FS is 1 */
425 /* Set the threshold and write it out if the device is used */
426 lis->threshold = val;
428 if (lis->flags & LIS302DL_F_INPUT_OPEN) {
431 local_irq_save(flags);
432 __enable_data_collection(lis);
433 local_irq_restore(flags);
439 static DEVICE_ATTR(threshold, S_IRUGO | S_IWUSR, show_threshold, set_threshold);
441 static ssize_t show_duration(struct device *dev, struct device_attribute *attr,
444 struct lis302dl_info *lis = dev_get_drvdata(dev);
446 return sprintf(buf, "%d\n", __duration_to_ms(lis,
447 __ms_to_duration(lis, lis->duration)));
450 static ssize_t set_duration(struct device *dev, struct device_attribute *attr,
451 const char *buf, size_t count)
453 struct lis302dl_info *lis = dev_get_drvdata(dev);
456 if (sscanf(buf, "%u\n", &val) != 1)
462 if (lis->flags & LIS302DL_F_INPUT_OPEN)
463 __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1,
464 __ms_to_duration(lis, lis->duration));
469 static DEVICE_ATTR(duration, S_IRUGO | S_IWUSR, show_duration, set_duration);
471 static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
474 struct lis302dl_info *lis = dev_get_drvdata(dev);
480 local_irq_save(flags);
482 for (n = 0; n < sizeof(reg); n++)
483 reg[n] = __reg_read(lis, n);
485 local_irq_restore(flags);
487 for (n = 0; n < sizeof(reg); n += 16) {
488 hex_dump_to_buffer(reg + n, 16, 16, 1, end, 128, 0);
496 static DEVICE_ATTR(dump, S_IRUGO, lis302dl_dump, NULL);
498 /* Configure freefall/wakeup interrupts */
499 static ssize_t set_wakeup_threshold(struct device *dev,
500 struct device_attribute *attr, const char *buf, size_t count)
502 struct lis302dl_info *lis = dev_get_drvdata(dev);
503 unsigned int threshold;
505 if (sscanf(buf, "%u\n", &threshold) != 1)
508 if (threshold > 8000)
511 /* Zero turns the feature off */
512 if (threshold == 0) {
513 if (lis->flags & LIS302DL_F_IRQ_WAKE) {
514 disable_irq_wake(lis->pdata->interrupt);
515 lis->flags &= ~LIS302DL_F_IRQ_WAKE;
521 lis->wakeup.threshold = threshold;
523 if (!(lis->flags & LIS302DL_F_IRQ_WAKE)) {
524 enable_irq_wake(lis->pdata->interrupt);
525 lis->flags |= LIS302DL_F_IRQ_WAKE;
531 static ssize_t show_wakeup_threshold(struct device *dev,
532 struct device_attribute *attr, char *buf)
534 struct lis302dl_info *lis = dev_get_drvdata(dev);
536 /* All events off? */
537 if (lis->wakeup.threshold == 0)
538 return sprintf(buf, "off\n");
540 return sprintf(buf, "%u\n", lis->wakeup.threshold);
543 static DEVICE_ATTR(wakeup_threshold, S_IRUGO | S_IWUSR, show_wakeup_threshold,
544 set_wakeup_threshold);
546 static ssize_t set_wakeup_duration(struct device *dev,
547 struct device_attribute *attr, const char *buf, size_t count)
549 struct lis302dl_info *lis = dev_get_drvdata(dev);
550 unsigned int duration;
552 if (sscanf(buf, "%u\n", &duration) != 1)
558 lis->wakeup.duration = duration;
563 static ssize_t show_wakeup_duration(struct device *dev,
564 struct device_attribute *attr, char *buf)
566 struct lis302dl_info *lis = dev_get_drvdata(dev);
568 return sprintf(buf, "%u\n", lis->wakeup.duration);
571 static DEVICE_ATTR(wakeup_duration, S_IRUGO | S_IWUSR, show_wakeup_duration,
572 set_wakeup_duration);
574 static struct attribute *lis302dl_sysfs_entries[] = {
575 &dev_attr_sample_rate.attr,
576 &dev_attr_full_scale.attr,
577 &dev_attr_threshold.attr,
578 &dev_attr_duration.attr,
580 &dev_attr_wakeup_threshold.attr,
581 &dev_attr_wakeup_duration.attr,
582 &dev_attr_overruns.attr,
586 static struct attribute_group lis302dl_attr_group = {
588 .attrs = lis302dl_sysfs_entries,
591 /* input device handling and driver core interaction */
593 static int lis302dl_input_open(struct input_dev *inp)
595 struct lis302dl_info *lis = input_get_drvdata(inp);
598 local_irq_save(flags);
600 __enable_data_collection(lis);
601 lis->flags |= LIS302DL_F_INPUT_OPEN;
603 local_irq_restore(flags);
608 static void lis302dl_input_close(struct input_dev *inp)
610 struct lis302dl_info *lis = input_get_drvdata(inp);
611 u_int8_t ctrl1 = LIS302DL_CTRL1_Xen | LIS302DL_CTRL1_Yen |
615 local_irq_save(flags);
617 /* since the input core already serializes access and makes sure we
618 * only see close() for the close of the last user, we can safely
619 * disable the data ready events */
620 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, 0x00);
621 lis->flags &= ~LIS302DL_F_INPUT_OPEN;
623 /* however, don't power down the whole device if still needed */
624 if (!(lis->flags & LIS302DL_F_WUP_FF ||
625 lis->flags & LIS302DL_F_WUP_CLICK)) {
626 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD,
629 local_irq_restore(flags);
632 /* get the device to reload its coefficients from EEPROM and wait for it
636 static int __lis302dl_reset_device(struct lis302dl_info *lis)
640 __reg_write(lis, LIS302DL_REG_CTRL2,
641 LIS302DL_CTRL2_BOOT | LIS302DL_CTRL2_FDS);
643 while ((__reg_read(lis, LIS302DL_REG_CTRL2)
644 & LIS302DL_CTRL2_BOOT) && (timeout--))
647 return !!(timeout < 0);
650 static int __devinit lis302dl_probe(struct spi_device *spi)
653 struct lis302dl_info *lis;
656 struct lis302dl_platform_data *pdata = spi->dev.platform_data;
658 spi->mode = SPI_MODE_3;
661 dev_err(&spi->dev, "spi_setup failed\n");
665 lis = kzalloc(sizeof(*lis), GFP_KERNEL);
669 lis->dev = &spi->dev;
672 dev_set_drvdata(lis->dev, lis);
676 rc = sysfs_create_group(&lis->dev->kobj, &lis302dl_attr_group);
678 dev_err(lis->dev, "error creating sysfs group\n");
682 /* initialize input layer details */
683 lis->input_dev = input_allocate_device();
684 if (!lis->input_dev) {
685 dev_err(lis->dev, "Unable to allocate input device\n");
689 input_set_drvdata(lis->input_dev, lis);
690 lis->input_dev->name = pdata->name;
691 /* SPI Bus not defined as a valid bus for input subsystem*/
692 lis->input_dev->id.bustype = BUS_I2C; /* lie about it */
693 lis->input_dev->open = lis302dl_input_open;
694 lis->input_dev->close = lis302dl_input_close;
696 rc = input_register_device(lis->input_dev);
698 dev_err(lis->dev, "error %d registering input device\n", rc);
702 local_irq_save(flags);
703 /* Configure our IO */
704 (lis->pdata->lis302dl_suspend_io)(lis, 1);
706 wai = __reg_read(lis, LIS302DL_REG_WHO_AM_I);
707 if (wai != LIS302DL_WHO_AM_I_MAGIC) {
708 dev_err(lis->dev, "unknown who_am_i signature 0x%02x\n", wai);
709 dev_set_drvdata(lis->dev, NULL);
711 local_irq_restore(flags);
715 set_bit(EV_ABS, lis->input_dev->evbit);
716 input_set_abs_params(lis->input_dev, ABS_X, 0, 0, 0, 0);
717 input_set_abs_params(lis->input_dev, ABS_Y, 0, 0, 0, 0);
718 input_set_abs_params(lis->input_dev, ABS_Z, 0, 0, 0, 0);
722 memset(&lis->wakeup, 0, sizeof(lis->wakeup));
724 if (__lis302dl_reset_device(lis))
725 dev_err(lis->dev, "device BOOT reload failed\n");
727 /* force us powered */
728 __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD |
734 __reg_write(lis, LIS302DL_REG_CTRL2, 0);
735 __reg_write(lis, LIS302DL_REG_CTRL3,
736 LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
737 __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, 0x0);
738 __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, 0x00);
739 __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, 0x0);
741 /* start off in powered down mode; we power up when someone opens us */
742 __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_Xen |
743 LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen);
745 if (pdata->open_drain)
746 /* switch interrupt to open collector, active-low */
747 __reg_write(lis, LIS302DL_REG_CTRL3,
748 LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
750 /* push-pull, active-low */
751 __reg_write(lis, LIS302DL_REG_CTRL3, LIS302DL_CTRL3_IHL);
753 __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_GND);
754 __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_GND);
756 __reg_read(lis, LIS302DL_REG_STATUS);
757 __reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
758 __reg_read(lis, LIS302DL_REG_FF_WU_SRC_2);
759 __reg_read(lis, LIS302DL_REG_CLICK_SRC);
760 local_irq_restore(flags);
762 dev_info(lis->dev, "Found %s\n", pdata->name);
766 set_irq_handler(lis->pdata->interrupt, handle_level_irq);
768 rc = request_irq(lis->pdata->interrupt, lis302dl_interrupt,
769 IRQF_TRIGGER_LOW, "lis302dl", lis);
772 dev_err(lis->dev, "error requesting IRQ %d\n",
773 lis->pdata->interrupt);
779 input_unregister_device(lis->input_dev);
781 input_free_device(lis->input_dev);
783 sysfs_remove_group(&lis->dev->kobj, &lis302dl_attr_group);
789 static int __devexit lis302dl_remove(struct spi_device *spi)
791 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
794 /* Disable interrupts */
795 if (lis->flags & LIS302DL_F_IRQ_WAKE)
796 disable_irq_wake(lis->pdata->interrupt);
797 free_irq(lis->pdata->interrupt, lis);
799 /* Reset and power down the device */
800 local_irq_save(flags);
801 __reg_write(lis, LIS302DL_REG_CTRL3, 0x00);
802 __reg_write(lis, LIS302DL_REG_CTRL2, 0x00);
803 __reg_write(lis, LIS302DL_REG_CTRL1, 0x00);
804 local_irq_restore(flags);
806 /* Cleanup resources */
807 sysfs_remove_group(&spi->dev.kobj, &lis302dl_attr_group);
808 input_unregister_device(lis->input_dev);
810 input_free_device(lis->input_dev);
811 dev_set_drvdata(lis->dev, NULL);
819 static u8 regs_to_save[] = {
823 LIS302DL_REG_FF_WU_CFG_1,
824 LIS302DL_REG_FF_WU_THS_1,
825 LIS302DL_REG_FF_WU_DURATION_1,
826 LIS302DL_REG_FF_WU_CFG_2,
827 LIS302DL_REG_FF_WU_THS_2,
828 LIS302DL_REG_FF_WU_DURATION_2,
829 LIS302DL_REG_CLICK_CFG,
830 LIS302DL_REG_CLICK_THSY_X,
831 LIS302DL_REG_CLICK_THSZ,
832 LIS302DL_REG_CLICK_TIME_LIMIT,
833 LIS302DL_REG_CLICK_LATENCY,
834 LIS302DL_REG_CLICK_WINDOW,
838 static int lis302dl_suspend(struct spi_device *spi, pm_message_t state)
840 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
845 /* determine if we want to wake up from the accel. */
846 if (lis->flags & LIS302DL_F_WUP_CLICK)
849 disable_irq(lis->pdata->interrupt);
850 local_irq_save(flags);
853 * When we share SPI over multiple sensors, there is a race here
854 * that one or more sensors will lose. In that case, the shared
855 * SPI bus GPIO will be in sleep mode and partially pulled down. So
856 * we explicitly put our IO into "wake" mode here before the final
857 * traffic to the sensor.
859 (lis->pdata->lis302dl_suspend_io)(lis, 1);
862 for (n = 0; n < ARRAY_SIZE(regs_to_save); n++)
863 lis->regs[regs_to_save[n]] =
864 __reg_read(lis, regs_to_save[n]);
866 /* power down or enable wakeup */
868 if (lis->wakeup.threshold == 0) {
869 tmp = __reg_read(lis, LIS302DL_REG_CTRL1);
870 tmp &= ~LIS302DL_CTRL1_PD;
871 __reg_write(lis, LIS302DL_REG_CTRL1, tmp);
873 __enable_wakeup(lis);
875 /* place our IO to the device in sleep-compatible states */
876 (lis->pdata->lis302dl_suspend_io)(lis, 0);
878 local_irq_restore(flags);
883 static int lis302dl_resume(struct spi_device *spi)
885 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
889 if (lis->flags & LIS302DL_F_WUP_CLICK)
892 local_irq_save(flags);
894 /* get our IO to the device back in operational states */
895 (lis->pdata->lis302dl_suspend_io)(lis, 1);
897 /* resume from powerdown first! */
898 __reg_write(lis, LIS302DL_REG_CTRL1,
905 if (__lis302dl_reset_device(lis))
906 dev_err(&spi->dev, "device BOOT reload failed\n");
908 lis->regs[LIS302DL_REG_CTRL1] |= LIS302DL_CTRL1_PD |
913 /* restore registers after resume */
914 for (n = 0; n < ARRAY_SIZE(regs_to_save); n++)
915 __reg_write(lis, regs_to_save[n], lis->regs[regs_to_save[n]]);
917 /* if someone had us open, reset the non-wake threshold stuff */
918 if (lis->flags & LIS302DL_F_INPUT_OPEN)
919 __enable_data_collection(lis);
921 local_irq_restore(flags);
922 enable_irq(lis->pdata->interrupt);
927 #define lis302dl_suspend NULL
928 #define lis302dl_resume NULL
931 static struct spi_driver lis302dl_spi_driver = {
934 .owner = THIS_MODULE,
937 .probe = lis302dl_probe,
938 .remove = __devexit_p(lis302dl_remove),
939 .suspend = lis302dl_suspend,
940 .resume = lis302dl_resume,
943 static int __devinit lis302dl_init(void)
945 return spi_register_driver(&lis302dl_spi_driver);
948 static void __exit lis302dl_exit(void)
950 spi_unregister_driver(&lis302dl_spi_driver);
953 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
954 MODULE_LICENSE("GPL");
956 module_init(lis302dl_init);
957 module_exit(lis302dl_exit);