[s3c24xx] bump to 2.6.30-rc6
[openwrt/svn-archive/archive.git] / target / linux / s3c24xx / files-2.6.30 / drivers / input / misc / lis302dl.c
1 /* Linux kernel driver for the ST LIS302D 3-axis accelerometer
2 *
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>
9 * All rights reserved.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 *
26 * TODO
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
33 */
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>
44
45 #include <linux/lis302dl.h>
46
47 /* Utility functions */
48 static u8 __reg_read(struct lis302dl_info *lis, u8 reg)
49 {
50 struct spi_message msg;
51 struct spi_transfer t;
52 u8 data[2] = {0xc0 | reg};
53 int rc;
54
55 spi_message_init(&msg);
56 memset(&t, 0, sizeof t);
57 t.len = 2;
58 spi_message_add_tail(&t, &msg);
59 t.tx_buf = &data[0];
60 t.rx_buf = &data[0];
61
62 /* Should complete without blocking */
63 rc = spi_non_blocking_transfer(lis->spi, &msg);
64 if (rc < 0) {
65 dev_err(lis->dev, "Error reading register\n");
66 return rc;
67 }
68
69 return data[1];
70 }
71
72 static void __reg_write(struct lis302dl_info *lis, u8 reg, u8 val)
73 {
74 struct spi_message msg;
75 struct spi_transfer t;
76 u8 data[2] = {reg, val};
77
78 spi_message_init(&msg);
79 memset(&t, 0, sizeof t);
80 t.len = 2;
81 spi_message_add_tail(&t, &msg);
82 t.tx_buf = &data[0];
83 t.rx_buf = &data[0];
84
85 /* Completes without blocking */
86 if (spi_non_blocking_transfer(lis->spi, &msg) < 0)
87 dev_err(lis->dev, "Error writing register\n");
88 }
89
90 static void __reg_set_bit_mask(struct lis302dl_info *lis, u8 reg, u8 mask,
91 u8 val)
92 {
93 u_int8_t tmp;
94
95 val &= mask;
96
97 tmp = __reg_read(lis, reg);
98 tmp &= ~mask;
99 tmp |= val;
100 __reg_write(lis, reg, tmp);
101 }
102
103 static int __ms_to_duration(struct lis302dl_info *lis, int ms)
104 {
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);
109
110 return min(ms / 10, 2550);
111 }
112
113 static int __duration_to_ms(struct lis302dl_info *lis, int duration)
114 {
115 if (lis->flags & LIS302DL_F_DR)
116 return (duration * 25) / 10;
117
118 return duration * 10;
119 }
120
121 static u8 __mg_to_threshold(struct lis302dl_info *lis, int mg)
122 {
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);
127
128 return min(mg / 18, 127);
129 }
130
131 static int __threshold_to_mg(struct lis302dl_info *lis, u8 threshold)
132 {
133 if (lis->flags & LIS302DL_F_FS)
134 return threshold * 71;
135
136 return threshold * 18;
137 }
138
139 /* interrupt handling related */
140
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,
148 };
149
150 static void __lis302dl_int_mode(struct device *dev, int int_pin,
151 enum lis302dl_intmode mode)
152 {
153 struct lis302dl_info *lis = dev_get_drvdata(dev);
154
155 switch (int_pin) {
156 case 1:
157 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x07, mode);
158 break;
159 case 2:
160 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x38, mode << 3);
161 break;
162 default:
163 BUG();
164 }
165 }
166
167 static void __enable_wakeup(struct lis302dl_info *lis)
168 {
169 __reg_write(lis, LIS302DL_REG_CTRL1, 0);
170
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));
179
180 /* Route the interrupt for wakeup */
181 __lis302dl_int_mode(lis->dev, 1,
182 LIS302DL_INTMODE_FF_WU_1);
183
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);
192 }
193
194 static void __enable_data_collection(struct lis302dl_info *lis)
195 {
196 u_int8_t ctrl1 = LIS302DL_CTRL1_PD | LIS302DL_CTRL1_Xen |
197 LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen;
198
199 /* make sure we're powered up and generate data ready */
200 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, ctrl1);
201
202 /* If the threshold is zero, let the device generated an interrupt
203 * on each datum */
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);
208 } else {
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));
215
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);
223 }
224 }
225
226 #if 0
227 static void _report_btn_single(struct input_dev *inp, int btn)
228 {
229 input_report_key(inp, btn, 1);
230 input_sync(inp);
231 input_report_key(inp, btn, 0);
232 }
233
234 static void _report_btn_double(struct input_dev *inp, int btn)
235 {
236 input_report_key(inp, btn, 1);
237 input_sync(inp);
238 input_report_key(inp, btn, 0);
239 input_sync(inp);
240 input_report_key(inp, btn, 1);
241 input_sync(inp);
242 input_report_key(inp, btn, 0);
243 }
244 #endif
245
246
247 static void lis302dl_bitbang_read_sample(struct lis302dl_info *lis)
248 {
249 u8 data[(LIS302DL_REG_OUT_Z - LIS302DL_REG_STATUS) + 2] = {0xC0 | LIS302DL_REG_STATUS};
250 u8 *read = data + 1;
251 unsigned long flags;
252 int mg_per_sample = __threshold_to_mg(lis, 1);
253 struct spi_message msg;
254 struct spi_transfer t;
255
256 spi_message_init(&msg);
257 memset(&t, 0, sizeof t);
258 t.len = sizeof(data);
259 spi_message_add_tail(&t, &msg);
260 t.tx_buf = &data[0];
261 t.rx_buf = &data[0];
262
263 /* grab the set of register containing status and XYZ data */
264
265 local_irq_save(flags);
266
267 /* Should complete without blocking */
268 if (spi_non_blocking_transfer(lis->spi, &msg) < 0)
269 dev_err(lis->dev, "Error reading registers\n");
270
271 local_irq_restore(flags);
272
273 /*
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.
278 */
279
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))
284 lis->overruns++;
285
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]);
294
295 input_sync(lis->input_dev);
296 }
297
298 if (lis->threshold)
299 /* acknowledge the wakeup source */
300 __reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
301 }
302
303 static irqreturn_t lis302dl_interrupt(int irq, void *_lis)
304 {
305 struct lis302dl_info *lis = _lis;
306
307 lis302dl_bitbang_read_sample(lis);
308 return IRQ_HANDLED;
309 }
310
311 /* sysfs */
312
313 static ssize_t show_overruns(struct device *dev, struct device_attribute *attr,
314 char *buf)
315 {
316 struct lis302dl_info *lis = dev_get_drvdata(dev);
317
318 return sprintf(buf, "%u\n", lis->overruns);
319 }
320
321 static DEVICE_ATTR(overruns, S_IRUGO, show_overruns, NULL);
322
323 static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
324 char *buf)
325 {
326 struct lis302dl_info *lis = dev_get_drvdata(dev);
327 u8 ctrl1;
328 unsigned long flags;
329
330 local_irq_save(flags);
331 ctrl1 = __reg_read(lis, LIS302DL_REG_CTRL1);
332 local_irq_restore(flags);
333
334 return sprintf(buf, "%d\n", ctrl1 & LIS302DL_CTRL1_DR ? 400 : 100);
335 }
336
337 static ssize_t set_rate(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
339 {
340 struct lis302dl_info *lis = dev_get_drvdata(dev);
341 unsigned long flags;
342
343 local_irq_save(flags);
344
345 if (!strcmp(buf, "400\n")) {
346 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
347 LIS302DL_CTRL1_DR);
348 lis->flags |= LIS302DL_F_DR;
349 } else {
350 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
351 0);
352 lis->flags &= ~LIS302DL_F_DR;
353 }
354 local_irq_restore(flags);
355
356 return count;
357 }
358
359 static DEVICE_ATTR(sample_rate, S_IRUGO | S_IWUSR, show_rate, set_rate);
360
361 static ssize_t show_scale(struct device *dev, struct device_attribute *attr,
362 char *buf)
363 {
364 struct lis302dl_info *lis = dev_get_drvdata(dev);
365 u_int8_t ctrl1;
366 unsigned long flags;
367
368 local_irq_save(flags);
369 ctrl1 = __reg_read(lis, LIS302DL_REG_CTRL1);
370 local_irq_restore(flags);
371
372 return sprintf(buf, "%s\n", ctrl1 & LIS302DL_CTRL1_FS ? "9.2" : "2.3");
373 }
374
375 static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
376 const char *buf, size_t count)
377 {
378 struct lis302dl_info *lis = dev_get_drvdata(dev);
379 unsigned long flags;
380
381 local_irq_save(flags);
382
383 if (!strcmp(buf, "9.2\n")) {
384 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
385 LIS302DL_CTRL1_FS);
386 lis->flags |= LIS302DL_F_FS;
387 } else {
388 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
389 0);
390 lis->flags &= ~LIS302DL_F_FS;
391 }
392
393 if (lis->flags & LIS302DL_F_INPUT_OPEN)
394 __enable_data_collection(lis);
395
396 local_irq_restore(flags);
397
398 return count;
399 }
400
401 static DEVICE_ATTR(full_scale, S_IRUGO | S_IWUSR, show_scale, set_scale);
402
403 static ssize_t show_threshold(struct device *dev, struct device_attribute *attr,
404 char *buf)
405 {
406 struct lis302dl_info *lis = dev_get_drvdata(dev);
407
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)));
411 }
412
413 static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
414 const char *buf, size_t count)
415 {
416 struct lis302dl_info *lis = dev_get_drvdata(dev);
417 unsigned int val;
418
419 if (sscanf(buf, "%u\n", &val) != 1)
420 return -EINVAL;
421 /* 8g is the maximum if FS is 1 */
422 if (val > 8000)
423 return -ERANGE;
424
425 /* Set the threshold and write it out if the device is used */
426 lis->threshold = val;
427
428 if (lis->flags & LIS302DL_F_INPUT_OPEN) {
429 unsigned long flags;
430
431 local_irq_save(flags);
432 __enable_data_collection(lis);
433 local_irq_restore(flags);
434 }
435
436 return count;
437 }
438
439 static DEVICE_ATTR(threshold, S_IRUGO | S_IWUSR, show_threshold, set_threshold);
440
441 static ssize_t show_duration(struct device *dev, struct device_attribute *attr,
442 char *buf)
443 {
444 struct lis302dl_info *lis = dev_get_drvdata(dev);
445
446 return sprintf(buf, "%d\n", __duration_to_ms(lis,
447 __ms_to_duration(lis, lis->duration)));
448 }
449
450 static ssize_t set_duration(struct device *dev, struct device_attribute *attr,
451 const char *buf, size_t count)
452 {
453 struct lis302dl_info *lis = dev_get_drvdata(dev);
454 unsigned int val;
455
456 if (sscanf(buf, "%u\n", &val) != 1)
457 return -EINVAL;
458 if (val > 2550)
459 return -ERANGE;
460
461 lis->duration = val;
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));
465
466 return count;
467 }
468
469 static DEVICE_ATTR(duration, S_IRUGO | S_IWUSR, show_duration, set_duration);
470
471 static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
472 char *buf)
473 {
474 struct lis302dl_info *lis = dev_get_drvdata(dev);
475 int n = 0;
476 u8 reg[0x40];
477 char *end = buf;
478 unsigned long flags;
479
480 local_irq_save(flags);
481
482 for (n = 0; n < sizeof(reg); n++)
483 reg[n] = __reg_read(lis, n);
484
485 local_irq_restore(flags);
486
487 for (n = 0; n < sizeof(reg); n += 16) {
488 hex_dump_to_buffer(reg + n, 16, 16, 1, end, 128, 0);
489 end += strlen(end);
490 *end++ = '\n';
491 *end++ = '\0';
492 }
493
494 return end - buf;
495 }
496 static DEVICE_ATTR(dump, S_IRUGO, lis302dl_dump, NULL);
497
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)
501 {
502 struct lis302dl_info *lis = dev_get_drvdata(dev);
503 unsigned int threshold;
504
505 if (sscanf(buf, "%u\n", &threshold) != 1)
506 return -EINVAL;
507
508 if (threshold > 8000)
509 return -ERANGE;
510
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;
516 }
517
518 return count;
519 }
520
521 lis->wakeup.threshold = threshold;
522
523 if (!(lis->flags & LIS302DL_F_IRQ_WAKE)) {
524 enable_irq_wake(lis->pdata->interrupt);
525 lis->flags |= LIS302DL_F_IRQ_WAKE;
526 }
527
528 return count;
529 }
530
531 static ssize_t show_wakeup_threshold(struct device *dev,
532 struct device_attribute *attr, char *buf)
533 {
534 struct lis302dl_info *lis = dev_get_drvdata(dev);
535
536 /* All events off? */
537 if (lis->wakeup.threshold == 0)
538 return sprintf(buf, "off\n");
539
540 return sprintf(buf, "%u\n", lis->wakeup.threshold);
541 }
542
543 static DEVICE_ATTR(wakeup_threshold, S_IRUGO | S_IWUSR, show_wakeup_threshold,
544 set_wakeup_threshold);
545
546 static ssize_t set_wakeup_duration(struct device *dev,
547 struct device_attribute *attr, const char *buf, size_t count)
548 {
549 struct lis302dl_info *lis = dev_get_drvdata(dev);
550 unsigned int duration;
551
552 if (sscanf(buf, "%u\n", &duration) != 1)
553 return -EINVAL;
554
555 if (duration > 2550)
556 return -ERANGE;
557
558 lis->wakeup.duration = duration;
559
560 return count;
561 }
562
563 static ssize_t show_wakeup_duration(struct device *dev,
564 struct device_attribute *attr, char *buf)
565 {
566 struct lis302dl_info *lis = dev_get_drvdata(dev);
567
568 return sprintf(buf, "%u\n", lis->wakeup.duration);
569 }
570
571 static DEVICE_ATTR(wakeup_duration, S_IRUGO | S_IWUSR, show_wakeup_duration,
572 set_wakeup_duration);
573
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,
579 &dev_attr_dump.attr,
580 &dev_attr_wakeup_threshold.attr,
581 &dev_attr_wakeup_duration.attr,
582 &dev_attr_overruns.attr,
583 NULL
584 };
585
586 static struct attribute_group lis302dl_attr_group = {
587 .name = NULL,
588 .attrs = lis302dl_sysfs_entries,
589 };
590
591 /* input device handling and driver core interaction */
592
593 static int lis302dl_input_open(struct input_dev *inp)
594 {
595 struct lis302dl_info *lis = input_get_drvdata(inp);
596 unsigned long flags;
597
598 local_irq_save(flags);
599
600 __enable_data_collection(lis);
601 lis->flags |= LIS302DL_F_INPUT_OPEN;
602
603 local_irq_restore(flags);
604
605 return 0;
606 }
607
608 static void lis302dl_input_close(struct input_dev *inp)
609 {
610 struct lis302dl_info *lis = input_get_drvdata(inp);
611 u_int8_t ctrl1 = LIS302DL_CTRL1_Xen | LIS302DL_CTRL1_Yen |
612 LIS302DL_CTRL1_Zen;
613 unsigned long flags;
614
615 local_irq_save(flags);
616
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;
622
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,
627 0x00);
628 }
629 local_irq_restore(flags);
630 }
631
632 /* get the device to reload its coefficients from EEPROM and wait for it
633 * to complete
634 */
635
636 static int __lis302dl_reset_device(struct lis302dl_info *lis)
637 {
638 int timeout = 10;
639
640 __reg_write(lis, LIS302DL_REG_CTRL2,
641 LIS302DL_CTRL2_BOOT | LIS302DL_CTRL2_FDS);
642
643 while ((__reg_read(lis, LIS302DL_REG_CTRL2)
644 & LIS302DL_CTRL2_BOOT) && (timeout--))
645 mdelay(1);
646
647 return !!(timeout < 0);
648 }
649
650 static int __devinit lis302dl_probe(struct spi_device *spi)
651 {
652 int rc;
653 struct lis302dl_info *lis;
654 u_int8_t wai;
655 unsigned long flags;
656 struct lis302dl_platform_data *pdata = spi->dev.platform_data;
657
658 spi->mode = SPI_MODE_3;
659 rc = spi_setup(spi);
660 if (rc < 0) {
661 dev_err(&spi->dev, "spi_setup failed\n");
662 return rc;
663 }
664
665 lis = kzalloc(sizeof(*lis), GFP_KERNEL);
666 if (!lis)
667 return -ENOMEM;
668
669 lis->dev = &spi->dev;
670 lis->spi = spi;
671
672 dev_set_drvdata(lis->dev, lis);
673
674 lis->pdata = pdata;
675
676 rc = sysfs_create_group(&lis->dev->kobj, &lis302dl_attr_group);
677 if (rc) {
678 dev_err(lis->dev, "error creating sysfs group\n");
679 goto bail_free_lis;
680 }
681
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");
686 goto bail_sysfs;
687 }
688
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;
695
696 rc = input_register_device(lis->input_dev);
697 if (rc) {
698 dev_err(lis->dev, "error %d registering input device\n", rc);
699 goto bail_inp_dev;
700 }
701
702 local_irq_save(flags);
703 /* Configure our IO */
704 (lis->pdata->lis302dl_suspend_io)(lis, 1);
705
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);
710 rc = -ENODEV;
711 local_irq_restore(flags);
712 goto bail_inp_reg;
713 }
714
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);
719
720 lis->threshold = 0;
721 lis->duration = 0;
722 memset(&lis->wakeup, 0, sizeof(lis->wakeup));
723
724 if (__lis302dl_reset_device(lis))
725 dev_err(lis->dev, "device BOOT reload failed\n");
726
727 /* force us powered */
728 __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD |
729 LIS302DL_CTRL1_Xen |
730 LIS302DL_CTRL1_Yen |
731 LIS302DL_CTRL1_Zen);
732 mdelay(1);
733
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);
740
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);
744
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);
749 else
750 /* push-pull, active-low */
751 __reg_write(lis, LIS302DL_REG_CTRL3, LIS302DL_CTRL3_IHL);
752
753 __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_GND);
754 __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_GND);
755
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);
761
762 dev_info(lis->dev, "Found %s\n", pdata->name);
763
764 lis->pdata = pdata;
765
766 set_irq_handler(lis->pdata->interrupt, handle_level_irq);
767
768 rc = request_irq(lis->pdata->interrupt, lis302dl_interrupt,
769 IRQF_TRIGGER_LOW, "lis302dl", lis);
770
771 if (rc < 0) {
772 dev_err(lis->dev, "error requesting IRQ %d\n",
773 lis->pdata->interrupt);
774 goto bail_inp_reg;
775 }
776 return 0;
777
778 bail_inp_reg:
779 input_unregister_device(lis->input_dev);
780 bail_inp_dev:
781 input_free_device(lis->input_dev);
782 bail_sysfs:
783 sysfs_remove_group(&lis->dev->kobj, &lis302dl_attr_group);
784 bail_free_lis:
785 kfree(lis);
786 return rc;
787 }
788
789 static int __devexit lis302dl_remove(struct spi_device *spi)
790 {
791 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
792 unsigned long flags;
793
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);
798
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);
805
806 /* Cleanup resources */
807 sysfs_remove_group(&spi->dev.kobj, &lis302dl_attr_group);
808 input_unregister_device(lis->input_dev);
809 if (lis->input_dev)
810 input_free_device(lis->input_dev);
811 dev_set_drvdata(lis->dev, NULL);
812 kfree(lis);
813
814 return 0;
815 }
816
817 #ifdef CONFIG_PM
818
819 static u8 regs_to_save[] = {
820 LIS302DL_REG_CTRL1,
821 LIS302DL_REG_CTRL2,
822 LIS302DL_REG_CTRL3,
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,
835
836 };
837
838 static int lis302dl_suspend(struct spi_device *spi, pm_message_t state)
839 {
840 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
841 unsigned long flags;
842 u_int8_t tmp;
843 int n;
844
845 /* determine if we want to wake up from the accel. */
846 if (lis->flags & LIS302DL_F_WUP_CLICK)
847 return 0;
848
849 disable_irq(lis->pdata->interrupt);
850 local_irq_save(flags);
851
852 /*
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.
858 */
859 (lis->pdata->lis302dl_suspend_io)(lis, 1);
860
861 /* save registers */
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]);
865
866 /* power down or enable wakeup */
867
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);
872 } else
873 __enable_wakeup(lis);
874
875 /* place our IO to the device in sleep-compatible states */
876 (lis->pdata->lis302dl_suspend_io)(lis, 0);
877
878 local_irq_restore(flags);
879
880 return 0;
881 }
882
883 static int lis302dl_resume(struct spi_device *spi)
884 {
885 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
886 unsigned long flags;
887 int n;
888
889 if (lis->flags & LIS302DL_F_WUP_CLICK)
890 return 0;
891
892 local_irq_save(flags);
893
894 /* get our IO to the device back in operational states */
895 (lis->pdata->lis302dl_suspend_io)(lis, 1);
896
897 /* resume from powerdown first! */
898 __reg_write(lis, LIS302DL_REG_CTRL1,
899 LIS302DL_CTRL1_PD |
900 LIS302DL_CTRL1_Xen |
901 LIS302DL_CTRL1_Yen |
902 LIS302DL_CTRL1_Zen);
903 mdelay(1);
904
905 if (__lis302dl_reset_device(lis))
906 dev_err(&spi->dev, "device BOOT reload failed\n");
907
908 lis->regs[LIS302DL_REG_CTRL1] |= LIS302DL_CTRL1_PD |
909 LIS302DL_CTRL1_Xen |
910 LIS302DL_CTRL1_Yen |
911 LIS302DL_CTRL1_Zen;
912
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]]);
916
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);
920
921 local_irq_restore(flags);
922 enable_irq(lis->pdata->interrupt);
923
924 return 0;
925 }
926 #else
927 #define lis302dl_suspend NULL
928 #define lis302dl_resume NULL
929 #endif
930
931 static struct spi_driver lis302dl_spi_driver = {
932 .driver = {
933 .name = "lis302dl",
934 .owner = THIS_MODULE,
935 },
936
937 .probe = lis302dl_probe,
938 .remove = __devexit_p(lis302dl_remove),
939 .suspend = lis302dl_suspend,
940 .resume = lis302dl_resume,
941 };
942
943 static int __devinit lis302dl_init(void)
944 {
945 return spi_register_driver(&lis302dl_spi_driver);
946 }
947
948 static void __exit lis302dl_exit(void)
949 {
950 spi_unregister_driver(&lis302dl_spi_driver);
951 }
952
953 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
954 MODULE_LICENSE("GPL");
955
956 module_init(lis302dl_init);
957 module_exit(lis302dl_exit);