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>
44 #include <linux/lis302dl.h>
46 /* Utility functions */
48 static u8 __reg_read(struct lis302dl_info *lis, u8 reg)
50 return (lis->pdata->lis302dl_bitbang_reg_read)(lis, reg);
53 static void __reg_write(struct lis302dl_info *lis, u8 reg, u8 val)
55 (lis->pdata->lis302dl_bitbang_reg_write)(lis, reg, val);
58 static void __reg_set_bit_mask(struct lis302dl_info *lis, u8 reg, u8 mask,
65 tmp = __reg_read(lis, reg);
68 __reg_write(lis, reg, tmp);
71 static int __ms_to_duration(struct lis302dl_info *lis, int ms)
73 /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
74 * on 100 ms the stepping is 10ms */
75 if (lis->flags & LIS302DL_F_DR)
76 return min((ms * 10) / 25, 637);
78 return min(ms / 10, 2550);
81 static int __duration_to_ms(struct lis302dl_info *lis, int duration)
83 if (lis->flags & LIS302DL_F_DR)
84 return (duration * 25) / 10;
89 static u8 __mg_to_threshold(struct lis302dl_info *lis, int mg)
91 /* If FS is set each bit is 71mg, otherwise 18mg. The THS register
92 * has 7 bits for the threshold value */
93 if (lis->flags & LIS302DL_F_FS)
94 return min(mg / 71, 127);
96 return min(mg / 18, 127);
99 static int __threshold_to_mg(struct lis302dl_info *lis, u8 threshold)
101 if (lis->flags & LIS302DL_F_FS)
102 return threshold * 71;
104 return threshold * 18;
107 /* interrupt handling related */
109 enum lis302dl_intmode {
110 LIS302DL_INTMODE_GND = 0x00,
111 LIS302DL_INTMODE_FF_WU_1 = 0x01,
112 LIS302DL_INTMODE_FF_WU_2 = 0x02,
113 LIS302DL_INTMODE_FF_WU_12 = 0x03,
114 LIS302DL_INTMODE_DATA_READY = 0x04,
115 LIS302DL_INTMODE_CLICK = 0x07,
119 static void __lis302dl_int_mode(struct device *dev, int int_pin,
120 enum lis302dl_intmode mode)
122 struct lis302dl_info *lis = dev_get_drvdata(dev);
126 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x07, mode);
129 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x38, mode << 3);
136 static void __enable_wakeup(struct lis302dl_info *lis)
138 /* First zero to get to a known state */
139 __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1,
141 __reg_write(lis, LIS302DL_REG_FF_WU_THS_1,
142 lis->wakeup.threshold);
143 __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1,
144 lis->wakeup.duration);
146 /* Route the interrupt for wakeup */
147 __lis302dl_int_mode(lis->dev, 1,
148 LIS302DL_INTMODE_FF_WU_1);
150 __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD);
153 static void __enable_data_collection(struct lis302dl_info *lis)
155 u_int8_t ctrl1 = LIS302DL_CTRL1_PD | LIS302DL_CTRL1_Xen |
156 LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen;
158 /* make sure we're powered up and generate data ready */
159 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, ctrl1);
161 /* If the threshold is zero, let the device generated an interrupt
163 if (lis->threshold == 0) {
164 __reg_write(lis, LIS302DL_REG_CTRL2, 0);
165 __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_DATA_READY);
166 __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_DATA_READY);
168 __reg_write(lis, LIS302DL_REG_CTRL2,
169 LIS302DL_CTRL2_HPFF1);
170 __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, lis->threshold);
171 __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, lis->duration);
173 /* Clear the HP filter "starting point" */
174 __reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
175 __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1,
176 LIS302DL_FFWUCFG_XHIE | LIS302DL_FFWUCFG_YHIE |
177 LIS302DL_FFWUCFG_ZHIE);
178 __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_FF_WU_12);
179 __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_FF_WU_12);
184 static void _report_btn_single(struct input_dev *inp, int btn)
186 input_report_key(inp, btn, 1);
188 input_report_key(inp, btn, 0);
191 static void _report_btn_double(struct input_dev *inp, int btn)
193 input_report_key(inp, btn, 1);
195 input_report_key(inp, btn, 0);
197 input_report_key(inp, btn, 1);
199 input_report_key(inp, btn, 0);
204 static void lis302dl_bitbang_read_sample(struct lis302dl_info *lis)
206 u8 data = 0xc0 | LIS302DL_REG_OUT_X; /* read, autoincrement */
211 local_irq_save(flags);
212 mg_per_sample = __threshold_to_mg(lis, 1);
214 (lis->pdata->lis302dl_bitbang)(lis, &data, 1, &read[0], 5);
216 local_irq_restore(flags);
218 input_report_rel(lis->input_dev, REL_X, mg_per_sample * (s8)read[0]);
219 input_report_rel(lis->input_dev, REL_Y, mg_per_sample * (s8)read[2]);
220 input_report_rel(lis->input_dev, REL_Z, mg_per_sample * (s8)read[4]);
222 input_sync(lis->input_dev);
224 /* Reset the HP filter */
225 __reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
228 static irqreturn_t lis302dl_interrupt(int irq, void *_lis)
230 struct lis302dl_info *lis = _lis;
232 lis302dl_bitbang_read_sample(lis);
238 static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
241 struct lis302dl_info *lis = dev_get_drvdata(dev);
245 local_irq_save(flags);
246 ctrl1 = __reg_read(lis, LIS302DL_REG_CTRL1);
247 local_irq_restore(flags);
249 return sprintf(buf, "%d\n", ctrl1 & LIS302DL_CTRL1_DR ? 400 : 100);
252 static ssize_t set_rate(struct device *dev, struct device_attribute *attr,
253 const char *buf, size_t count)
255 struct lis302dl_info *lis = dev_get_drvdata(dev);
257 int duration_ms = __duration_to_ms(lis, lis->duration);
259 local_irq_save(flags);
261 if (!strcmp(buf, "400\n")) {
262 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
264 lis->flags |= LIS302DL_F_DR;
266 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
268 lis->flags &= ~LIS302DL_F_DR;
270 lis->duration = __ms_to_duration(lis, duration_ms);
271 local_irq_restore(flags);
276 static DEVICE_ATTR(sample_rate, S_IRUGO | S_IWUSR, show_rate, set_rate);
278 static ssize_t show_scale(struct device *dev, struct device_attribute *attr,
281 struct lis302dl_info *lis = dev_get_drvdata(dev);
285 local_irq_save(flags);
286 ctrl1 = __reg_read(lis, LIS302DL_REG_CTRL1);
287 local_irq_restore(flags);
289 return sprintf(buf, "%s\n", ctrl1 & LIS302DL_CTRL1_FS ? "9.2" : "2.3");
292 static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count)
295 struct lis302dl_info *lis = dev_get_drvdata(dev);
297 int threshold_mg = __threshold_to_mg(lis, lis->threshold);
299 local_irq_save(flags);
301 if (!strcmp(buf, "9.2\n")) {
302 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
304 lis->flags |= LIS302DL_F_FS;
306 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
308 lis->flags &= ~LIS302DL_F_FS;
311 /* Adjust the threshold */
312 lis->threshold = __mg_to_threshold(lis, threshold_mg);
313 if (lis->flags & LIS302DL_F_INPUT_OPEN)
314 __enable_data_collection(lis);
316 local_irq_restore(flags);
321 static DEVICE_ATTR(full_scale, S_IRUGO | S_IWUSR, show_scale, set_scale);
323 static ssize_t show_threshold(struct device *dev, struct device_attribute *attr,
326 struct lis302dl_info *lis = dev_get_drvdata(dev);
328 return sprintf(buf, "%d\n", __threshold_to_mg(lis, lis->threshold));
331 static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
332 const char *buf, size_t count)
334 struct lis302dl_info *lis = dev_get_drvdata(dev);
337 if (sscanf(buf, "%d\n", &val) != 1)
339 /* 8g is the maximum if FS is 1 */
340 if (val < 0 || val > 8000)
343 /* Set the threshold and write it out if the device is used */
344 lis->threshold = __mg_to_threshold(lis, val);
346 if (lis->flags & LIS302DL_F_INPUT_OPEN) {
349 local_irq_save(flags);
350 __enable_data_collection(lis);
351 local_irq_restore(flags);
357 static DEVICE_ATTR(threshold, S_IRUGO | S_IWUSR, show_threshold, set_threshold);
359 static ssize_t show_duration(struct device *dev, struct device_attribute *attr,
362 struct lis302dl_info *lis = dev_get_drvdata(dev);
364 return sprintf(buf, "%d\n", __duration_to_ms(lis, lis->duration));
367 static ssize_t set_duration(struct device *dev, struct device_attribute *attr,
368 const char *buf, size_t count)
370 struct lis302dl_info *lis = dev_get_drvdata(dev);
373 if (sscanf(buf, "%d\n", &val) != 1)
375 if (val < 0 || val > 2550)
378 lis->duration = __ms_to_duration(lis, val);
379 if (lis->flags & LIS302DL_F_INPUT_OPEN)
380 __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, lis->duration);
385 static DEVICE_ATTR(duration, S_IRUGO | S_IWUSR, show_duration, set_duration);
387 static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
390 struct lis302dl_info *lis = dev_get_drvdata(dev);
396 local_irq_save(flags);
398 for (n = 0; n < sizeof(reg); n++)
399 reg[n] = __reg_read(lis, n);
401 local_irq_restore(flags);
403 for (n = 0; n < sizeof(reg); n += 16) {
404 hex_dump_to_buffer(reg + n, 16, 16, 1, end, 128, 0);
412 static DEVICE_ATTR(dump, S_IRUGO, lis302dl_dump, NULL);
414 /* Configure freefall/wakeup interrupts */
415 static ssize_t set_wakeup(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t count)
418 struct lis302dl_info *lis = dev_get_drvdata(dev);
419 u_int8_t x_lo, y_lo, z_lo;
420 u_int8_t x_hi, y_hi, z_hi;
421 int duration, threshold, and_events;
424 /* Zero turns the feature off */
425 if (strcmp(buf, "0\n") == 0) {
426 lis->wakeup.active = 0;
428 if (lis->flags & LIS302DL_F_IRQ_WAKE) {
429 disable_irq_wake(lis->pdata->interrupt);
430 lis->flags &= ~LIS302DL_F_IRQ_WAKE;
436 if (sscanf(buf, "%d %d %d %d %d %d", &x, &y, &z, &threshold, &duration,
440 if (duration < 0 || duration > 2550 ||
441 threshold < 0 || threshold > 8000)
444 /* Interrupt flags */
445 x_lo = x < 0 ? LIS302DL_FFWUCFG_XLIE : 0;
446 y_lo = y < 0 ? LIS302DL_FFWUCFG_YLIE : 0;
447 z_lo = z < 0 ? LIS302DL_FFWUCFG_ZLIE : 0;
448 x_hi = x > 0 ? LIS302DL_FFWUCFG_XHIE : 0;
449 y_hi = y > 0 ? LIS302DL_FFWUCFG_YHIE : 0;
450 z_hi = z > 0 ? LIS302DL_FFWUCFG_ZHIE : 0;
452 lis->wakeup.duration = __ms_to_duration(lis, duration);
453 lis->wakeup.threshold = __mg_to_threshold(lis, threshold);
454 lis->wakeup.cfg = (and_events ? LIS302DL_FFWUCFG_AOI : 0) |
455 x_lo | x_hi | y_lo | y_hi | z_lo | z_hi;
457 if (!(lis->flags & LIS302DL_F_IRQ_WAKE)) {
458 enable_irq_wake(lis->pdata->interrupt);
459 lis->flags |= LIS302DL_F_IRQ_WAKE;
461 lis->wakeup.active = 1;
466 static ssize_t show_wakeup(struct device *dev,
467 struct device_attribute *attr, char *buf)
469 struct lis302dl_info *lis = dev_get_drvdata(dev);
472 /* All events off? */
473 if (!lis->wakeup.active)
474 return sprintf(buf, "off\n");
476 config = lis->wakeup.cfg;
479 "%s events, duration %d, threshold %d, "
480 "enabled: %s %s %s %s %s %s\n",
481 (config & LIS302DL_FFWUCFG_AOI) == 0 ? "or" : "and",
482 __duration_to_ms(lis, lis->wakeup.duration),
483 __threshold_to_mg(lis, lis->wakeup.threshold),
484 (config & LIS302DL_FFWUCFG_XLIE) == 0 ? "---" : "xlo",
485 (config & LIS302DL_FFWUCFG_XHIE) == 0 ? "---" : "xhi",
486 (config & LIS302DL_FFWUCFG_YLIE) == 0 ? "---" : "ylo",
487 (config & LIS302DL_FFWUCFG_YHIE) == 0 ? "---" : "yhi",
488 (config & LIS302DL_FFWUCFG_ZLIE) == 0 ? "---" : "zlo",
489 (config & LIS302DL_FFWUCFG_ZHIE) == 0 ? "---" : "zhi");
492 static DEVICE_ATTR(wakeup, S_IRUGO | S_IWUSR, show_wakeup, set_wakeup);
494 static struct attribute *lis302dl_sysfs_entries[] = {
495 &dev_attr_sample_rate.attr,
496 &dev_attr_full_scale.attr,
497 &dev_attr_threshold.attr,
498 &dev_attr_duration.attr,
500 &dev_attr_wakeup.attr,
504 static struct attribute_group lis302dl_attr_group = {
506 .attrs = lis302dl_sysfs_entries,
509 /* input device handling and driver core interaction */
510 static int lis302dl_input_open(struct input_dev *inp)
512 struct lis302dl_info *lis = inp->private;
515 local_irq_save(flags);
517 __enable_data_collection(lis);
518 lis->flags |= LIS302DL_F_INPUT_OPEN;
520 /* kick it off -- since we are edge triggered, if we missed the edge
521 * permanent low interrupt is death for us */
522 lis302dl_bitbang_read_sample(lis);
524 local_irq_restore(flags);
529 static void lis302dl_input_close(struct input_dev *inp)
531 struct lis302dl_info *lis = inp->private;
532 u_int8_t ctrl1 = LIS302DL_CTRL1_Xen | LIS302DL_CTRL1_Yen |
536 local_irq_save(flags);
538 /* since the input core already serializes access and makes sure we
539 * only see close() for the close of the last user, we can safely
540 * disable the data ready events */
541 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, 0x00);
542 lis->flags &= ~LIS302DL_F_INPUT_OPEN;
544 /* however, don't power down the whole device if still needed */
545 if (!(lis->flags & LIS302DL_F_WUP_FF ||
546 lis->flags & LIS302DL_F_WUP_CLICK)) {
547 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD,
550 local_irq_restore(flags);
553 /* get the device to reload its coefficients from EEPROM and wait for it
557 static int __lis302dl_reset_device(struct lis302dl_info *lis)
561 __reg_write(lis, LIS302DL_REG_CTRL2,
562 LIS302DL_CTRL2_BOOT | LIS302DL_CTRL2_FDS);
564 while ((__reg_read(lis, LIS302DL_REG_CTRL2)
565 & LIS302DL_CTRL2_BOOT) && (timeout--))
568 return !!(timeout < 0);
571 static int __devinit lis302dl_probe(struct platform_device *pdev)
574 struct lis302dl_info *lis;
577 struct lis302dl_platform_data *pdata = pdev->dev.platform_data;
579 lis = kzalloc(sizeof(*lis), GFP_KERNEL);
583 local_irq_save(flags);
585 lis->dev = &pdev->dev;
587 dev_set_drvdata(lis->dev, lis);
591 /* Configure our IO */
592 (lis->pdata->lis302dl_suspend_io)(lis, 1);
594 wai = __reg_read(lis, LIS302DL_REG_WHO_AM_I);
595 if (wai != LIS302DL_WHO_AM_I_MAGIC) {
596 dev_err(lis->dev, "unknown who_am_i signature 0x%02x\n", wai);
597 dev_set_drvdata(lis->dev, NULL);
602 rc = sysfs_create_group(&lis->dev->kobj, &lis302dl_attr_group);
604 dev_err(lis->dev, "error creating sysfs group\n");
608 /* initialize input layer details */
609 lis->input_dev = input_allocate_device();
610 if (!lis->input_dev) {
611 dev_err(lis->dev, "Unable to allocate input device\n");
615 set_bit(EV_REL, lis->input_dev->evbit);
616 set_bit(REL_X, lis->input_dev->relbit);
617 set_bit(REL_Y, lis->input_dev->relbit);
618 set_bit(REL_Z, lis->input_dev->relbit);
619 /* set_bit(EV_KEY, lis->input_dev->evbit);
620 set_bit(BTN_X, lis->input_dev->keybit);
621 set_bit(BTN_Y, lis->input_dev->keybit);
622 set_bit(BTN_Z, lis->input_dev->keybit);
626 memset(&lis->wakeup, 0, sizeof(lis->wakeup));
628 lis->input_dev->private = lis;
629 lis->input_dev->name = pdata->name;
630 /* SPI Bus not defined as a valid bus for input subsystem*/
631 lis->input_dev->id.bustype = BUS_I2C; /* lie about it */
632 lis->input_dev->open = lis302dl_input_open;
633 lis->input_dev->close = lis302dl_input_close;
635 rc = input_register_device(lis->input_dev);
637 dev_err(lis->dev, "error %d registering input device\n", rc);
641 if (__lis302dl_reset_device(lis))
642 dev_err(lis->dev, "device BOOT reload failed\n");
644 /* force us powered */
645 __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD |
651 __reg_write(lis, LIS302DL_REG_CTRL2, 0);
652 __reg_write(lis, LIS302DL_REG_CTRL3,
653 LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
654 __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, 0x0);
655 __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, 0x00);
656 __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, 0x0);
658 /* start off in powered down mode; we power up when someone opens us */
659 __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_Xen |
660 LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen);
662 if (pdata->open_drain)
663 /* switch interrupt to open collector, active-low */
664 __reg_write(lis, LIS302DL_REG_CTRL3,
665 LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
667 /* push-pull, active-low */
668 __reg_write(lis, LIS302DL_REG_CTRL3, LIS302DL_CTRL3_IHL);
670 __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_GND);
671 __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_GND);
673 __reg_read(lis, LIS302DL_REG_STATUS);
674 __reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
675 __reg_read(lis, LIS302DL_REG_FF_WU_SRC_2);
676 __reg_read(lis, LIS302DL_REG_CLICK_SRC);
678 dev_info(lis->dev, "Found %s\n", pdata->name);
682 rc = request_irq(pdata->interrupt, lis302dl_interrupt,
683 IRQF_TRIGGER_FALLING, "lis302dl", lis);
685 dev_err(lis->dev, "error requesting IRQ %d\n",
686 lis->pdata->interrupt);
689 local_irq_restore(flags);
693 input_unregister_device(lis->input_dev);
695 input_free_device(lis->input_dev);
697 sysfs_remove_group(&lis->dev->kobj, &lis302dl_attr_group);
700 local_irq_restore(flags);
704 static int __devexit lis302dl_remove(struct platform_device *pdev)
706 struct lis302dl_info *lis = dev_get_drvdata(&pdev->dev);
709 /* Reset and power down the device */
710 local_irq_save(flags);
711 __reg_write(lis, LIS302DL_REG_CTRL3, 0x00);
712 __reg_write(lis, LIS302DL_REG_CTRL2, 0x00);
713 __reg_write(lis, LIS302DL_REG_CTRL1, 0x00);
714 local_irq_restore(flags);
716 /* Cleanup resources */
717 free_irq(lis->pdata->interrupt, lis);
718 sysfs_remove_group(&pdev->dev.kobj, &lis302dl_attr_group);
719 input_unregister_device(lis->input_dev);
721 input_free_device(lis->input_dev);
722 dev_set_drvdata(lis->dev, NULL);
730 static u8 regs_to_save[] = {
734 LIS302DL_REG_FF_WU_CFG_1,
735 LIS302DL_REG_FF_WU_THS_1,
736 LIS302DL_REG_FF_WU_DURATION_1,
737 LIS302DL_REG_FF_WU_CFG_2,
738 LIS302DL_REG_FF_WU_THS_2,
739 LIS302DL_REG_FF_WU_DURATION_2,
740 LIS302DL_REG_CLICK_CFG,
741 LIS302DL_REG_CLICK_THSY_X,
742 LIS302DL_REG_CLICK_THSZ,
743 LIS302DL_REG_CLICK_TIME_LIMIT,
744 LIS302DL_REG_CLICK_LATENCY,
745 LIS302DL_REG_CLICK_WINDOW,
749 static int lis302dl_suspend(struct platform_device *pdev, pm_message_t state)
751 struct lis302dl_info *lis = dev_get_drvdata(&pdev->dev);
756 /* determine if we want to wake up from the accel. */
757 if (lis->flags & LIS302DL_F_WUP_CLICK)
760 disable_irq(lis->pdata->interrupt);
761 local_irq_save(flags);
764 * When we share SPI over multiple sensors, there is a race here
765 * that one or more sensors will lose. In that case, the shared
766 * SPI bus GPIO will be in sleep mode and partially pulled down. So
767 * we explicitly put our IO into "wake" mode here before the final
768 * traffic to the sensor.
770 (lis->pdata->lis302dl_suspend_io)(lis, 1);
773 for (n = 0; n < ARRAY_SIZE(regs_to_save); n++)
774 lis->regs[regs_to_save[n]] =
775 __reg_read(lis, regs_to_save[n]);
777 /* power down or enable wakeup */
778 if (!lis->wakeup.active) {
779 tmp = __reg_read(lis, LIS302DL_REG_CTRL1);
780 tmp &= ~LIS302DL_CTRL1_PD;
781 __reg_write(lis, LIS302DL_REG_CTRL1, tmp);
783 __enable_wakeup(lis);
785 /* place our IO to the device in sleep-compatible states */
786 (lis->pdata->lis302dl_suspend_io)(lis, 0);
788 local_irq_restore(flags);
793 static int lis302dl_resume(struct platform_device *pdev)
795 struct lis302dl_info *lis = dev_get_drvdata(&pdev->dev);
799 if (lis->flags & LIS302DL_F_WUP_CLICK)
802 local_irq_save(flags);
804 /* get our IO to the device back in operational states */
805 (lis->pdata->lis302dl_suspend_io)(lis, 1);
807 /* resume from powerdown first! */
808 __reg_write(lis, LIS302DL_REG_CTRL1,
815 if (__lis302dl_reset_device(lis))
816 dev_err(&pdev->dev, "device BOOT reload failed\n");
818 lis->regs[LIS302DL_REG_CTRL1] |= LIS302DL_CTRL1_PD |
823 /* restore registers after resume */
824 for (n = 0; n < ARRAY_SIZE(regs_to_save); n++)
825 __reg_write(lis, regs_to_save[n], lis->regs[regs_to_save[n]]);
827 local_irq_restore(flags);
828 enable_irq(lis->pdata->interrupt);
833 #define lis302dl_suspend NULL
834 #define lis302dl_resume NULL
837 static struct platform_driver lis302dl_driver = {
840 .owner = THIS_MODULE,
843 .probe = lis302dl_probe,
844 .remove = __devexit_p(lis302dl_remove),
845 .suspend = lis302dl_suspend,
846 .resume = lis302dl_resume,
849 static int __devinit lis302dl_init(void)
851 return platform_driver_register(&lis302dl_driver);
854 static void __exit lis302dl_exit(void)
856 platform_driver_unregister(&lis302dl_driver);
859 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
860 MODULE_LICENSE("GPL");
862 module_init(lis302dl_init);
863 module_exit(lis302dl_exit);