fix-lid302dl-bitbang-all-the-way-baby.patch
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / input / misc / lis302dl.c
blob1d5781d1f237d612c86e94f5f3307113fafbfedb
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 * All rights reserved.
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,
22 * MA 02111-1307 USA
24 * TODO
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,
47 u8 val)
49 u_int8_t tmp;
51 val &= mask;
53 tmp = (lis->pdata->lis302dl_bitbang_reg_read)(lis, reg);
54 tmp &= ~mask;
55 tmp |= val;
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);
75 switch (int_pin) {
76 case 1:
77 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x07, mode);
78 break;
79 case 2:
80 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x38, mode << 3);
81 break;
82 default:
83 BUG();
86 #if 0
87 static void _report_btn_single(struct input_dev *inp, int btn)
89 input_report_key(inp, btn, 1);
90 input_sync(inp);
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);
97 input_sync(inp);
98 input_report_key(inp, btn, 0);
99 input_sync(inp);
100 input_report_key(inp, btn, 1);
101 input_sync(inp);
102 input_report_key(inp, btn, 0);
104 #endif
107 static irqreturn_t lis302dl_interrupt(int irq, void *_lis)
109 struct lis302dl_info *lis = _lis;
111 (lis->pdata->lis302dl_bitbang_read_sample)(lis);
112 return IRQ_HANDLED;
115 /* sysfs */
117 static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
118 char *buf)
120 struct lis302dl_info *lis = dev_get_drvdata(dev);
121 u8 ctrl1;
122 unsigned long flags;
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);
136 unsigned long flags;
138 local_irq_save(flags);
140 if (!strcmp(buf, "400\n"))
141 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
142 LIS302DL_CTRL1_DR);
143 else
144 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
146 local_irq_restore(flags);
148 return count;
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,
154 char *buf)
156 struct lis302dl_info *lis = dev_get_drvdata(dev);
157 u_int8_t ctrl1;
158 unsigned long flags;
160 local_irq_save(flags);
161 ctrl1 = (lis->pdata->lis302dl_bitbang_reg_read)(lis,
162 LIS302DL_REG_CTRL1);
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);
172 unsigned long flags;
174 local_irq_save(flags);
176 if (!strcmp(buf, "9.2\n"))
177 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
178 LIS302DL_CTRL1_FS);
179 else
180 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
183 local_irq_restore(flags);
185 return count;
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,
191 char *buf)
193 struct lis302dl_info *lis = dev_get_drvdata(dev);
194 int n = 0;
195 u8 reg[0x40];
196 char *end = buf;
197 unsigned long flags;
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);
208 end += strlen(end);
209 *end++ = '\n';
210 *end++ = '\0';
213 return end - buf;
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) {
224 /* Too large */
225 if (ms > 637)
226 return -1;
228 return (ms * 10) / 25;
231 /* Too large value */
232 if (ms > 2550)
233 return -1;
234 return ms / 10;
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;
254 int duration;
255 int threshold;
256 int and_events;
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;
262 int x, y, z;
263 int ms;
264 unsigned long flags;
266 /* Configure for second freefall/wakeup pin */
267 if (which == 2) {
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;
274 printk(KERN_WARNING
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);
299 return count;
302 if (sscanf(buf, "%d %d %d %d %d %d", &x, &y, &z, &threshold, &ms,
303 &and_events) != 6)
304 return -EINVAL;
306 local_irq_save(flags);
307 duration = __freefall_ms_to_duration(lis, ms);
308 local_irq_save(flags);
310 if (duration < 0)
311 return -ERANGE;
313 /* 7 bits */
314 if (threshold < 0 || threshold > 127)
315 return -ERANGE;
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
340 * this interrupt */
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,
346 LIS302DL_CTRL1_PD);
347 local_irq_restore(flags);
349 return count;
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);
368 u_int8_t duration;
369 u_int8_t threshold;
370 u_int8_t config;
371 u_int8_t r4;
372 u_int8_t r5;
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;
377 unsigned long flags;
378 int ms;
380 /* Configure second freefall/wakeup pin */
381 if (which == 2) {
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");
404 return sprintf(buf,
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",
410 ms, threshold,
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,
432 set_freefall_1);
433 static DEVICE_ATTR(freefall_wakeup_2, S_IRUGO | S_IWUSR, show_freefall_2,
434 set_freefall_2);
436 static struct attribute *lis302dl_sysfs_entries[] = {
437 &dev_attr_sample_rate.attr,
438 &dev_attr_full_scale.attr,
439 &dev_attr_dump.attr,
440 &dev_attr_freefall_wakeup_1.attr,
441 &dev_attr_freefall_wakeup_2.attr,
442 NULL
445 static struct attribute_group lis302dl_attr_group = {
446 .name = NULL,
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;
457 unsigned long flags;
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);
471 return 0;
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 |
478 LIS302DL_CTRL1_Zen;
479 unsigned long flags;
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,
493 0x00);
495 local_irq_restore(flags);
498 /* get the device to reload its coefficients from EEPROM and wait for it
499 * to complete
502 static int __lis302dl_reset_device(struct lis302dl_info *lis)
504 int timeout = 10;
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--))
511 mdelay(1);
513 return !!(timeout < 0);
516 static int __devinit lis302dl_probe(struct platform_device *pdev)
518 int rc;
519 struct lis302dl_info *lis;
520 u_int8_t wai;
521 unsigned long flags;
522 struct lis302dl_platform_data *pdata = pdev->dev.platform_data;
524 lis = kzalloc(sizeof(*lis), GFP_KERNEL);
525 if (!lis)
526 return -ENOMEM;
528 local_irq_save(flags);
530 lis->dev = &pdev->dev;
532 dev_set_drvdata(lis->dev, lis);
534 lis->pdata = pdata;
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);
544 rc = -ENODEV;
545 goto bail_free_lis;
548 rc = sysfs_create_group(&lis->dev->kobj, &lis302dl_attr_group);
549 if (rc) {
550 dev_err(lis->dev, "error creating sysfs group\n");
551 goto bail_free_lis;
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");
558 goto bail_sysfs;
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);
578 if (rc) {
579 dev_err(lis->dev, "error %d registering input device\n", rc);
580 goto bail_inp_dev;
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,
588 LIS302DL_CTRL1_PD |
589 LIS302DL_CTRL1_Xen |
590 LIS302DL_CTRL1_Yen |
591 LIS302DL_CTRL1_Zen);
592 mdelay(1);
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,
606 LIS302DL_CTRL1_Xen |
607 LIS302DL_CTRL1_Yen |
608 LIS302DL_CTRL1_Zen);
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 |
614 LIS302DL_CTRL3_IHL);
615 else
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);
630 lis->pdata = pdata;
632 rc = request_irq(pdata->interrupt, lis302dl_interrupt,
633 IRQF_TRIGGER_FALLING, "lis302dl", lis);
634 if (rc < 0) {
635 dev_err(lis->dev, "error requesting IRQ %d\n",
636 lis->pdata->interrupt);
637 goto bail_inp_reg;
639 local_irq_restore(flags);
640 return 0;
642 bail_inp_reg:
643 input_unregister_device(lis->input_dev);
644 bail_inp_dev:
645 input_free_device(lis->input_dev);
646 bail_sysfs:
647 sysfs_remove_group(&lis->dev->kobj, &lis302dl_attr_group);
648 bail_free_lis:
649 kfree(lis);
650 local_irq_restore(flags);
651 return rc;
654 static int __devexit lis302dl_remove(struct platform_device *pdev)
656 struct lis302dl_info *lis = dev_get_drvdata(&pdev->dev);
657 unsigned long flags;
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);
670 if (lis->input_dev)
671 input_free_device(lis->input_dev);
672 dev_set_drvdata(lis->dev, NULL);
673 kfree(lis);
675 return 0;
678 #ifdef CONFIG_PM
680 static u8 regs_to_save[] = {
681 LIS302DL_REG_CTRL1,
682 LIS302DL_REG_CTRL2,
683 LIS302DL_REG_CTRL3,
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);
702 unsigned long flags;
703 u_int8_t tmp;
704 int n;
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)
709 return 0;
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);
723 /* save registers */
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,
727 regs_to_save[n]);
729 /* power down */
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);
739 return 0;
742 static int lis302dl_resume(struct platform_device *pdev)
744 struct lis302dl_info *lis = dev_get_drvdata(&pdev->dev);
745 unsigned long flags;
746 int n;
748 if (lis->flags & LIS302DL_F_WUP_FF ||
749 lis->flags & LIS302DL_F_WUP_CLICK)
750 return 0;
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,
759 LIS302DL_CTRL1_PD |
760 LIS302DL_CTRL1_Xen |
761 LIS302DL_CTRL1_Yen |
762 LIS302DL_CTRL1_Zen);
763 mdelay(1);
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 |
769 LIS302DL_CTRL1_Xen |
770 LIS302DL_CTRL1_Yen |
771 LIS302DL_CTRL1_Zen;
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);
781 return 0;
783 #else
784 #define lis302dl_suspend NULL
785 #define lis302dl_resume NULL
786 #endif
788 static struct platform_driver lis302dl_driver = {
789 .driver = {
790 .name = "lis302dl",
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);