enable CONFIG_STAGING on 2.6.31
[openwrt.git] / target / linux / s3c24xx / files-2.6.31 / 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 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 *
27 * TODO
28 * * statistics for overflow events
29 * * configuration interface (sysfs) for
30 * * enable/disable x/y/z axis data ready
31 * * enable/disable resume from freee fall / click
32 * * free fall / click parameters
33 * * high pass filter parameters
34 */
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/module.h>
38 #include <linux/device.h>
39 #include <linux/platform_device.h>
40 #include <linux/delay.h>
41 #include <linux/irq.h>
42 #include <linux/interrupt.h>
43 #include <linux/sysfs.h>
44 #include <linux/spi/spi.h>
45
46 #include <linux/lis302dl.h>
47
48 static uint8_t lis_reg_read(struct lis302dl_info *lis, uint8_t reg)
49 {
50 return spi_w8r8(lis->spi, 0xc0 | reg);
51 }
52
53 static void lis_reg_write(struct lis302dl_info *lis, uint8_t reg, uint8_t val)
54 {
55 uint8_t data[2] = {reg, val};
56
57 spi_write(lis->spi, data, sizeof(data));
58 }
59
60 static void lis_reg_set_bit_mask(struct lis302dl_info *lis, uint8_t reg, uint8_t mask,
61 uint8_t val)
62 {
63 uint8_t tmp;
64
65 val &= mask;
66
67 tmp = lis_reg_read(lis, reg);
68 tmp &= ~mask;
69 tmp |= val;
70 lis_reg_write(lis, reg, tmp);
71 }
72
73 static int __ms_to_duration(struct lis302dl_info *lis, int ms)
74 {
75 /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
76 * on 100 ms the stepping is 10ms */
77 if (lis->flags & LIS302DL_F_DR)
78 return min((ms * 10) / 25, 637);
79
80 return min(ms / 10, 2550);
81 }
82
83 static int __duration_to_ms(struct lis302dl_info *lis, int duration)
84 {
85 if (lis->flags & LIS302DL_F_DR)
86 return (duration * 25) / 10;
87
88 return duration * 10;
89 }
90
91 static uint8_t __mg_to_threshold(struct lis302dl_info *lis, int mg)
92 {
93 /* If FS is set each bit is 71mg, otherwise 18mg. The THS register
94 * has 7 bits for the threshold value */
95 if (lis->flags & LIS302DL_F_FS)
96 return min(mg / 71, 127);
97
98 return min(mg / 18, 127);
99 }
100
101 static int __threshold_to_mg(struct lis302dl_info *lis, uint8_t threshold)
102 {
103 if (lis->flags & LIS302DL_F_FS)
104 return threshold * 71;
105
106 return threshold * 18;
107 }
108
109 /* interrupt handling related */
110
111 enum lis302dl_intmode {
112 LIS302DL_INTMODE_GND = 0x00,
113 LIS302DL_INTMODE_FF_WU_1 = 0x01,
114 LIS302DL_INTMODE_FF_WU_2 = 0x02,
115 LIS302DL_INTMODE_FF_WU_12 = 0x03,
116 LIS302DL_INTMODE_DATA_READY = 0x04,
117 LIS302DL_INTMODE_CLICK = 0x07,
118 };
119
120 static void lis302dl_set_int_mode(struct device *dev, int int_pin,
121 enum lis302dl_intmode mode)
122 {
123 struct lis302dl_info *lis = dev_get_drvdata(dev);
124 switch (int_pin) {
125 case 1:
126 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x07, mode);
127 break;
128 case 2:
129 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL3, 0x38, mode << 3);
130 break;
131 default:
132 BUG();
133 }
134 }
135
136 static void __enable_wakeup(struct lis302dl_info *lis)
137 {
138 lis_reg_write(lis, LIS302DL_REG_CTRL1, 0);
139
140 /* First zero to get to a known state */
141 lis_reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, LIS302DL_FFWUCFG_XHIE |
142 LIS302DL_FFWUCFG_YHIE | LIS302DL_FFWUCFG_ZHIE |
143 LIS302DL_FFWUCFG_LIR);
144 lis_reg_write(lis, LIS302DL_REG_FF_WU_THS_1,
145 __mg_to_threshold(lis, lis->wakeup.threshold));
146 lis_reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1,
147 __ms_to_duration(lis, lis->wakeup.duration));
148
149 /* Route the interrupt for wakeup */
150 lis302dl_set_int_mode(lis->dev, 1,
151 LIS302DL_INTMODE_FF_WU_1);
152
153 lis_reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
154 lis_reg_read(lis, LIS302DL_REG_OUT_X);
155 lis_reg_read(lis, LIS302DL_REG_OUT_Y);
156 lis_reg_read(lis, LIS302DL_REG_OUT_Z);
157 lis_reg_read(lis, LIS302DL_REG_STATUS);
158 lis_reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
159 lis_reg_read(lis, LIS302DL_REG_FF_WU_SRC_2);
160 lis_reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD | 7);
161 }
162
163 static void __enable_data_collection(struct lis302dl_info *lis)
164 {
165 u_int8_t ctrl1 = LIS302DL_CTRL1_PD | LIS302DL_CTRL1_Xen |
166 LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen;
167
168 /* make sure we're powered up and generate data ready */
169 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, ctrl1);
170
171 /* If the threshold is zero, let the device generated an interrupt
172 * on each datum */
173 if (lis->threshold == 0) {
174 lis_reg_write(lis, LIS302DL_REG_CTRL2, 0);
175 lis302dl_set_int_mode(lis->dev, 1, LIS302DL_INTMODE_DATA_READY);
176 lis302dl_set_int_mode(lis->dev, 2, LIS302DL_INTMODE_DATA_READY);
177 } else {
178 lis_reg_write(lis, LIS302DL_REG_CTRL2,
179 LIS302DL_CTRL2_HPFF1);
180 lis_reg_write(lis, LIS302DL_REG_FF_WU_THS_1,
181 __mg_to_threshold(lis, lis->threshold));
182 lis_reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1,
183 __ms_to_duration(lis, lis->duration));
184
185 /* Clear the HP filter "starting point" */
186 lis_reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
187 lis_reg_write(lis, LIS302DL_REG_FF_WU_CFG_1,
188 LIS302DL_FFWUCFG_XHIE | LIS302DL_FFWUCFG_YHIE |
189 LIS302DL_FFWUCFG_ZHIE | LIS302DL_FFWUCFG_LIR);
190 lis302dl_set_int_mode(lis->dev, 1, LIS302DL_INTMODE_FF_WU_12);
191 lis302dl_set_int_mode(lis->dev, 2, LIS302DL_INTMODE_FF_WU_12);
192 }
193 }
194
195 #if 0
196 static void _report_btn_single(struct input_dev *inp, int btn)
197 {
198 input_report_key(inp, btn, 1);
199 input_sync(inp);
200 input_report_key(inp, btn, 0);
201 }
202
203 static void _report_btn_double(struct input_dev *inp, int btn)
204 {
205 input_report_key(inp, btn, 1);
206 input_sync(inp);
207 input_report_key(inp, btn, 0);
208 input_sync(inp);
209 input_report_key(inp, btn, 1);
210 input_sync(inp);
211 input_report_key(inp, btn, 0);
212 }
213 #endif
214
215
216 static void lis302dl_bitbang_read_sample(struct lis302dl_info *lis)
217 {
218 uint8_t data[(LIS302DL_REG_OUT_Z - LIS302DL_REG_STATUS) + 2] = {0xC0 | LIS302DL_REG_STATUS};
219 uint8_t *read = data + 1;
220 unsigned long flags;
221 int mg_per_sample = __threshold_to_mg(lis, 1);
222 struct spi_message msg;
223 struct spi_transfer t;
224
225 spi_message_init(&msg);
226 memset(&t, 0, sizeof t);
227 t.len = sizeof(data);
228 spi_message_add_tail(&t, &msg);
229 t.tx_buf = &data[0];
230 t.rx_buf = &data[0];
231
232 /* grab the set of register containing status and XYZ data */
233 local_irq_save(flags);
234 /* Should complete without blocking */
235 if (spi_sync(lis->spi, &msg) < 0)
236 dev_err(lis->dev, "Error reading registers\n");
237
238 local_irq_restore(flags);
239 /*
240 * at the minute the test below fails 50% of the time due to
241 * a problem with level interrupts causing ISRs to get called twice.
242 * This is a workaround for that, but actually this test is still
243 * valid and the information can be used for overrrun stats.
244 */
245
246 /* has any kind of overrun been observed by the lis302dl? */
247 if (read[0] & (LIS302DL_STATUS_XOR |
248 LIS302DL_STATUS_YOR |
249 LIS302DL_STATUS_ZOR))
250 lis->overruns++;
251
252 /* we have a valid sample set? */
253 if (read[0] & LIS302DL_STATUS_XYZDA) {
254 input_report_abs(lis->input_dev, ABS_X, mg_per_sample *
255 (s8)read[LIS302DL_REG_OUT_X - LIS302DL_REG_STATUS]);
256 input_report_abs(lis->input_dev, ABS_Y, mg_per_sample *
257 (s8)read[LIS302DL_REG_OUT_Y - LIS302DL_REG_STATUS]);
258 input_report_abs(lis->input_dev, ABS_Z, mg_per_sample *
259 (s8)read[LIS302DL_REG_OUT_Z - LIS302DL_REG_STATUS]);
260
261 input_sync(lis->input_dev);
262 } else {
263 printk("invalid sample\n");
264 }
265
266 if (lis->threshold)
267 /* acknowledge the wakeup source */
268 lis_reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
269 enable_irq(lis->pdata->interrupt);
270 }
271
272 static void lis302dl_irq_worker(struct work_struct *work) {
273 struct lis302dl_info *lis = container_of(work, struct lis302dl_info, work);
274 lis302dl_bitbang_read_sample(lis);
275 }
276
277 static irqreturn_t lis302dl_interrupt(int irq, void *_lis)
278 {
279 struct lis302dl_info *lis = _lis;
280 disable_irq_nosync(lis->pdata->interrupt);
281 schedule_work(&lis->work);
282 return IRQ_HANDLED;
283 }
284
285 /* sysfs */
286
287 static ssize_t show_overruns(struct device *dev, struct device_attribute *attr,
288 char *buf)
289 {
290 struct lis302dl_info *lis = dev_get_drvdata(dev);
291
292 return sprintf(buf, "%u\n", lis->overruns);
293 }
294
295 static DEVICE_ATTR(overruns, S_IRUGO, show_overruns, NULL);
296
297 static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
298 char *buf)
299 {
300 struct lis302dl_info *lis = dev_get_drvdata(dev);
301 uint8_t ctrl1;
302 unsigned long flags;
303
304 local_irq_save(flags);
305 ctrl1 = lis_reg_read(lis, LIS302DL_REG_CTRL1);
306 local_irq_restore(flags);
307
308 return sprintf(buf, "%d\n", ctrl1 & LIS302DL_CTRL1_DR ? 400 : 100);
309 }
310
311 static ssize_t set_rate(struct device *dev, struct device_attribute *attr,
312 const char *buf, size_t count)
313 {
314 struct lis302dl_info *lis = dev_get_drvdata(dev);
315 unsigned long flags;
316
317 local_irq_save(flags);
318
319 if (!strcmp(buf, "400\n")) {
320 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
321 LIS302DL_CTRL1_DR);
322 lis->flags |= LIS302DL_F_DR;
323 } else {
324 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
325 0);
326 lis->flags &= ~LIS302DL_F_DR;
327 }
328 local_irq_restore(flags);
329
330 return count;
331 }
332
333 static DEVICE_ATTR(sample_rate, S_IRUGO | S_IWUSR, show_rate, set_rate);
334
335 static ssize_t show_scale(struct device *dev, struct device_attribute *attr,
336 char *buf)
337 {
338 struct lis302dl_info *lis = dev_get_drvdata(dev);
339 u_int8_t ctrl1;
340 unsigned long flags;
341
342 local_irq_save(flags);
343 ctrl1 = lis_reg_read(lis, LIS302DL_REG_CTRL1);
344 local_irq_restore(flags);
345
346 return sprintf(buf, "%s\n", ctrl1 & LIS302DL_CTRL1_FS ? "9.2" : "2.3");
347 }
348
349 static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
350 const char *buf, size_t count)
351 {
352 struct lis302dl_info *lis = dev_get_drvdata(dev);
353 unsigned long flags;
354
355 local_irq_save(flags);
356
357 if (!strcmp(buf, "9.2\n")) {
358 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
359 LIS302DL_CTRL1_FS);
360 lis->flags |= LIS302DL_F_FS;
361 } else {
362 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
363 0);
364 lis->flags &= ~LIS302DL_F_FS;
365 }
366
367 if (lis->flags & LIS302DL_F_INPUT_OPEN)
368 __enable_data_collection(lis);
369
370 local_irq_restore(flags);
371
372 return count;
373 }
374
375 static DEVICE_ATTR(full_scale, S_IRUGO | S_IWUSR, show_scale, set_scale);
376
377 static ssize_t show_threshold(struct device *dev, struct device_attribute *attr,
378 char *buf)
379 {
380 struct lis302dl_info *lis = dev_get_drvdata(dev);
381
382 /* Display the device view of the threshold setting */
383 return sprintf(buf, "%d\n", __threshold_to_mg(lis,
384 __mg_to_threshold(lis, lis->threshold)));
385 }
386
387 static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
388 const char *buf, size_t count)
389 {
390 struct lis302dl_info *lis = dev_get_drvdata(dev);
391 unsigned int val;
392
393 if (sscanf(buf, "%u\n", &val) != 1)
394 return -EINVAL;
395 /* 8g is the maximum if FS is 1 */
396 if (val > 8000)
397 return -ERANGE;
398
399 /* Set the threshold and write it out if the device is used */
400 lis->threshold = val;
401
402 if (lis->flags & LIS302DL_F_INPUT_OPEN) {
403 unsigned long flags;
404
405 local_irq_save(flags);
406 __enable_data_collection(lis);
407 local_irq_restore(flags);
408 }
409
410 return count;
411 }
412
413 static DEVICE_ATTR(threshold, S_IRUGO | S_IWUSR, show_threshold, set_threshold);
414
415 static ssize_t show_duration(struct device *dev, struct device_attribute *attr,
416 char *buf)
417 {
418 struct lis302dl_info *lis = dev_get_drvdata(dev);
419
420 return sprintf(buf, "%d\n", __duration_to_ms(lis,
421 __ms_to_duration(lis, lis->duration)));
422 }
423
424 static ssize_t set_duration(struct device *dev, struct device_attribute *attr,
425 const char *buf, size_t count)
426 {
427 struct lis302dl_info *lis = dev_get_drvdata(dev);
428 unsigned int val;
429
430 if (sscanf(buf, "%u\n", &val) != 1)
431 return -EINVAL;
432 if (val > 2550)
433 return -ERANGE;
434
435 lis->duration = val;
436 if (lis->flags & LIS302DL_F_INPUT_OPEN)
437 lis_reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1,
438 __ms_to_duration(lis, lis->duration));
439
440 return count;
441 }
442
443 static DEVICE_ATTR(duration, S_IRUGO | S_IWUSR, show_duration, set_duration);
444
445 static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
446 char *buf)
447 {
448 struct lis302dl_info *lis = dev_get_drvdata(dev);
449 int n = 0;
450 uint8_t reg[0x40];
451 char *end = buf;
452 unsigned long flags;
453
454 local_irq_save(flags);
455
456 for (n = 0; n < sizeof(reg); n++)
457 reg[n] = lis_reg_read(lis, n);
458
459 local_irq_restore(flags);
460
461 for (n = 0; n < sizeof(reg); n += 16) {
462 hex_dump_to_buffer(reg + n, 16, 16, 1, end, 128, 0);
463 end += strlen(end);
464 *end++ = '\n';
465 *end++ = '\0';
466 }
467
468 return end - buf;
469 }
470 static DEVICE_ATTR(dump, S_IRUGO, lis302dl_dump, NULL);
471
472 /* Configure freefall/wakeup interrupts */
473 static ssize_t set_wakeup_threshold(struct device *dev,
474 struct device_attribute *attr, const char *buf, size_t count)
475 {
476 struct lis302dl_info *lis = dev_get_drvdata(dev);
477 unsigned int threshold;
478
479 if (sscanf(buf, "%u\n", &threshold) != 1)
480 return -EINVAL;
481
482 if (threshold > 8000)
483 return -ERANGE;
484
485 /* Zero turns the feature off */
486 if (threshold == 0) {
487 if (lis->flags & LIS302DL_F_IRQ_WAKE) {
488 disable_irq_wake(lis->pdata->interrupt);
489 lis->flags &= ~LIS302DL_F_IRQ_WAKE;
490 }
491
492 return count;
493 }
494
495 lis->wakeup.threshold = threshold;
496
497 if (!(lis->flags & LIS302DL_F_IRQ_WAKE)) {
498 enable_irq_wake(lis->pdata->interrupt);
499 lis->flags |= LIS302DL_F_IRQ_WAKE;
500 }
501
502 return count;
503 }
504
505 static ssize_t show_wakeup_threshold(struct device *dev,
506 struct device_attribute *attr, char *buf)
507 {
508 struct lis302dl_info *lis = dev_get_drvdata(dev);
509
510 /* All events off? */
511 if (lis->wakeup.threshold == 0)
512 return sprintf(buf, "off\n");
513
514 return sprintf(buf, "%u\n", lis->wakeup.threshold);
515 }
516
517 static DEVICE_ATTR(wakeup_threshold, S_IRUGO | S_IWUSR, show_wakeup_threshold,
518 set_wakeup_threshold);
519
520 static ssize_t set_wakeup_duration(struct device *dev,
521 struct device_attribute *attr, const char *buf, size_t count)
522 {
523 struct lis302dl_info *lis = dev_get_drvdata(dev);
524 unsigned int duration;
525
526 if (sscanf(buf, "%u\n", &duration) != 1)
527 return -EINVAL;
528
529 if (duration > 2550)
530 return -ERANGE;
531
532 lis->wakeup.duration = duration;
533
534 return count;
535 }
536
537 static ssize_t show_wakeup_duration(struct device *dev,
538 struct device_attribute *attr, char *buf)
539 {
540 struct lis302dl_info *lis = dev_get_drvdata(dev);
541
542 return sprintf(buf, "%u\n", lis->wakeup.duration);
543 }
544
545 static DEVICE_ATTR(wakeup_duration, S_IRUGO | S_IWUSR, show_wakeup_duration,
546 set_wakeup_duration);
547
548 static struct attribute *lis302dl_sysfs_entries[] = {
549 &dev_attr_sample_rate.attr,
550 &dev_attr_full_scale.attr,
551 &dev_attr_threshold.attr,
552 &dev_attr_duration.attr,
553 &dev_attr_dump.attr,
554 &dev_attr_wakeup_threshold.attr,
555 &dev_attr_wakeup_duration.attr,
556 &dev_attr_overruns.attr,
557 NULL
558 };
559
560 static struct attribute_group lis302dl_attr_group = {
561 .name = NULL,
562 .attrs = lis302dl_sysfs_entries,
563 };
564
565 /* input device handling and driver core interaction */
566
567 static int lis302dl_input_open(struct input_dev *inp)
568 {
569 struct lis302dl_info *lis = input_get_drvdata(inp);
570 unsigned long flags;
571
572 local_irq_save(flags);
573
574 __enable_data_collection(lis);
575 lis->flags |= LIS302DL_F_INPUT_OPEN;
576
577 local_irq_restore(flags);
578
579 return 0;
580 }
581
582 static void lis302dl_input_close(struct input_dev *inp)
583 {
584 struct lis302dl_info *lis = input_get_drvdata(inp);
585 u_int8_t ctrl1 = LIS302DL_CTRL1_Xen | LIS302DL_CTRL1_Yen |
586 LIS302DL_CTRL1_Zen;
587 unsigned long flags;
588
589 local_irq_save(flags);
590
591 /* since the input core already serializes access and makes sure we
592 * only see close() for the close of the last user, we can safely
593 * disable the data ready events */
594 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, 0x00);
595 lis->flags &= ~LIS302DL_F_INPUT_OPEN;
596
597 /* however, don't power down the whole device if still needed */
598 if (!(lis->flags & LIS302DL_F_WUP_FF ||
599 lis->flags & LIS302DL_F_WUP_CLICK)) {
600 lis_reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD,
601 0x00);
602 }
603 local_irq_restore(flags);
604 }
605
606 /* get the device to reload its coefficients from EEPROM and wait for it
607 * to complete
608 */
609
610 static int __lis302dl_reset_device(struct lis302dl_info *lis)
611 {
612 int timeout = 10;
613
614 lis_reg_write(lis, LIS302DL_REG_CTRL2,
615 LIS302DL_CTRL2_BOOT | LIS302DL_CTRL2_FDS);
616
617 while ((lis_reg_read(lis, LIS302DL_REG_CTRL2)
618 & LIS302DL_CTRL2_BOOT) && (timeout--))
619 mdelay(1);
620
621 return !!(timeout < 0);
622 }
623
624 static int __devinit lis302dl_probe(struct spi_device *spi)
625 {
626 int rc;
627 struct lis302dl_info *lis;
628 u_int8_t wai;
629 unsigned long flags;
630 struct lis302dl_platform_data *pdata = spi->dev.platform_data;
631
632 spi->mode = SPI_MODE_3;
633 rc = spi_setup(spi);
634 if (rc < 0) {
635 dev_err(&spi->dev, "spi_setup failed\n");
636 return rc;
637 }
638
639 lis = kzalloc(sizeof(*lis), GFP_KERNEL);
640 if (!lis)
641 return -ENOMEM;
642
643 lis->dev = &spi->dev;
644 lis->spi = spi;
645
646 dev_set_drvdata(lis->dev, lis);
647
648 lis->pdata = pdata;
649
650 rc = sysfs_create_group(&lis->dev->kobj, &lis302dl_attr_group);
651 if (rc) {
652 dev_err(lis->dev, "error creating sysfs group\n");
653 goto bail_free_lis;
654 }
655
656 /* initialize input layer details */
657 lis->input_dev = input_allocate_device();
658 if (!lis->input_dev) {
659 dev_err(lis->dev, "Unable to allocate input device\n");
660 goto bail_sysfs;
661 }
662
663 input_set_drvdata(lis->input_dev, lis);
664 lis->input_dev->name = pdata->name;
665 /* SPI Bus not defined as a valid bus for input subsystem*/
666 lis->input_dev->id.bustype = BUS_I2C; /* lie about it */
667 lis->input_dev->open = lis302dl_input_open;
668 lis->input_dev->close = lis302dl_input_close;
669
670 rc = input_register_device(lis->input_dev);
671 if (rc) {
672 dev_err(lis->dev, "error %d registering input device\n", rc);
673 goto bail_inp_dev;
674 }
675
676 local_irq_save(flags);
677 /* Configure our IO */
678 (lis->pdata->lis302dl_suspend_io)(lis, 1);
679
680 wai = lis_reg_read(lis, LIS302DL_REG_WHO_AM_I);
681 if (wai != LIS302DL_WHO_AM_I_MAGIC) {
682 dev_err(lis->dev, "unknown who_am_i signature 0x%02x\n", wai);
683 dev_set_drvdata(lis->dev, NULL);
684 rc = -ENODEV;
685 local_irq_restore(flags);
686 goto bail_inp_reg;
687 }
688
689 set_bit(EV_ABS, lis->input_dev->evbit);
690 input_set_abs_params(lis->input_dev, ABS_X, 0, 0, 0, 0);
691 input_set_abs_params(lis->input_dev, ABS_Y, 0, 0, 0, 0);
692 input_set_abs_params(lis->input_dev, ABS_Z, 0, 0, 0, 0);
693
694 lis->threshold = 0;
695 lis->duration = 0;
696 memset(&lis->wakeup, 0, sizeof(lis->wakeup));
697
698 if (__lis302dl_reset_device(lis))
699 dev_err(lis->dev, "device BOOT reload failed\n");
700
701 /* force us powered */
702 lis_reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD |
703 LIS302DL_CTRL1_Xen |
704 LIS302DL_CTRL1_Yen |
705 LIS302DL_CTRL1_Zen);
706 mdelay(1);
707
708 lis_reg_write(lis, LIS302DL_REG_CTRL2, 0);
709 lis_reg_write(lis, LIS302DL_REG_CTRL3,
710 LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
711 lis_reg_write(lis, LIS302DL_REG_FF_WU_THS_1, 0x0);
712 lis_reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, 0x00);
713 lis_reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, 0x0);
714
715 /* start off in powered down mode; we power up when someone opens us */
716 lis_reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_Xen |
717 LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen);
718
719 if (pdata->open_drain)
720 /* switch interrupt to open collector, active-low */
721 lis_reg_write(lis, LIS302DL_REG_CTRL3,
722 LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
723 else
724 /* push-pull, active-low */
725 lis_reg_write(lis, LIS302DL_REG_CTRL3, LIS302DL_CTRL3_IHL);
726
727 lis302dl_set_int_mode(lis->dev, 1, LIS302DL_INTMODE_GND);
728 lis302dl_set_int_mode(lis->dev, 2, LIS302DL_INTMODE_GND);
729
730 lis_reg_read(lis, LIS302DL_REG_STATUS);
731 lis_reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
732 lis_reg_read(lis, LIS302DL_REG_FF_WU_SRC_2);
733 lis_reg_read(lis, LIS302DL_REG_CLICK_SRC);
734 local_irq_restore(flags);
735
736 dev_info(lis->dev, "Found %s\n", pdata->name);
737
738 lis->pdata = pdata;
739
740 INIT_WORK(&lis->work, lis302dl_irq_worker);
741
742 set_irq_handler(lis->pdata->interrupt, handle_level_irq);
743 set_irq_type(lis->pdata->interrupt, IRQ_TYPE_LEVEL_LOW);
744 rc = request_irq(lis->pdata->interrupt, lis302dl_interrupt,
745 IRQF_TRIGGER_FALLING, "lis302dl", lis);
746
747 if (rc < 0) {
748 dev_err(lis->dev, "error requesting IRQ %d\n",
749 lis->pdata->interrupt);
750 goto bail_inp_reg;
751 }
752 return 0;
753
754 bail_inp_reg:
755 input_unregister_device(lis->input_dev);
756 bail_inp_dev:
757 input_free_device(lis->input_dev);
758 bail_sysfs:
759 sysfs_remove_group(&lis->dev->kobj, &lis302dl_attr_group);
760 bail_free_lis:
761 kfree(lis);
762 return rc;
763 }
764
765 static int __devexit lis302dl_remove(struct spi_device *spi)
766 {
767 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
768 unsigned long flags;
769
770 /* Disable interrupts */
771 if (lis->flags & LIS302DL_F_IRQ_WAKE)
772 disable_irq_wake(lis->pdata->interrupt);
773 free_irq(lis->pdata->interrupt, lis);
774
775 /* Reset and power down the device */
776 local_irq_save(flags);
777 lis_reg_write(lis, LIS302DL_REG_CTRL3, 0x00);
778 lis_reg_write(lis, LIS302DL_REG_CTRL2, 0x00);
779 lis_reg_write(lis, LIS302DL_REG_CTRL1, 0x00);
780 local_irq_restore(flags);
781
782 /* Cleanup resources */
783 sysfs_remove_group(&spi->dev.kobj, &lis302dl_attr_group);
784 input_unregister_device(lis->input_dev);
785 if (lis->input_dev)
786 input_free_device(lis->input_dev);
787 dev_set_drvdata(lis->dev, NULL);
788 kfree(lis);
789
790 return 0;
791 }
792
793 #ifdef CONFIG_PM
794
795 static uint8_t regs_to_save[] = {
796 LIS302DL_REG_CTRL1,
797 LIS302DL_REG_CTRL2,
798 LIS302DL_REG_CTRL3,
799 LIS302DL_REG_FF_WU_CFG_1,
800 LIS302DL_REG_FF_WU_THS_1,
801 LIS302DL_REG_FF_WU_DURATION_1,
802 LIS302DL_REG_FF_WU_CFG_2,
803 LIS302DL_REG_FF_WU_THS_2,
804 LIS302DL_REG_FF_WU_DURATION_2,
805 LIS302DL_REG_CLICK_CFG,
806 LIS302DL_REG_CLICK_THSY_X,
807 LIS302DL_REG_CLICK_THSZ,
808 LIS302DL_REG_CLICK_TIME_LIMIT,
809 LIS302DL_REG_CLICK_LATENCY,
810 LIS302DL_REG_CLICK_WINDOW,
811
812 };
813
814 static int lis302dl_suspend(struct spi_device *spi, pm_message_t state)
815 {
816 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
817 unsigned long flags;
818 u_int8_t tmp;
819 int n;
820
821 /* determine if we want to wake up from the accel. */
822 if (lis->flags & LIS302DL_F_WUP_CLICK)
823 return 0;
824
825 disable_irq(lis->pdata->interrupt);
826 local_irq_save(flags);
827
828 /*
829 * When we share SPI over multiple sensors, there is a race here
830 * that one or more sensors will lose. In that case, the shared
831 * SPI bus GPIO will be in sleep mode and partially pulled down. So
832 * we explicitly put our IO into "wake" mode here before the final
833 * traffic to the sensor.
834 */
835 (lis->pdata->lis302dl_suspend_io)(lis, 1);
836
837 /* save registers */
838 for (n = 0; n < ARRAY_SIZE(regs_to_save); n++)
839 lis->regs[regs_to_save[n]] =
840 lis_reg_read(lis, regs_to_save[n]);
841
842 /* power down or enable wakeup */
843
844 if (lis->wakeup.threshold == 0) {
845 tmp = lis_reg_read(lis, LIS302DL_REG_CTRL1);
846 tmp &= ~LIS302DL_CTRL1_PD;
847 lis_reg_write(lis, LIS302DL_REG_CTRL1, tmp);
848 } else
849 __enable_wakeup(lis);
850
851 /* place our IO to the device in sleep-compatible states */
852 (lis->pdata->lis302dl_suspend_io)(lis, 0);
853
854 local_irq_restore(flags);
855
856 return 0;
857 }
858
859 static int lis302dl_resume(struct spi_device *spi)
860 {
861 struct lis302dl_info *lis = dev_get_drvdata(&spi->dev);
862 unsigned long flags;
863 int n;
864
865 if (lis->flags & LIS302DL_F_WUP_CLICK)
866 return 0;
867
868 local_irq_save(flags);
869
870 /* get our IO to the device back in operational states */
871 (lis->pdata->lis302dl_suspend_io)(lis, 1);
872
873 /* resume from powerdown first! */
874 lis_reg_write(lis, LIS302DL_REG_CTRL1,
875 LIS302DL_CTRL1_PD |
876 LIS302DL_CTRL1_Xen |
877 LIS302DL_CTRL1_Yen |
878 LIS302DL_CTRL1_Zen);
879 mdelay(1);
880
881 if (__lis302dl_reset_device(lis))
882 dev_err(&spi->dev, "device BOOT reload failed\n");
883
884 lis->regs[LIS302DL_REG_CTRL1] |= LIS302DL_CTRL1_PD |
885 LIS302DL_CTRL1_Xen |
886 LIS302DL_CTRL1_Yen |
887 LIS302DL_CTRL1_Zen;
888
889 /* restore registers after resume */
890 for (n = 0; n < ARRAY_SIZE(regs_to_save); n++)
891 lis_reg_write(lis, regs_to_save[n], lis->regs[regs_to_save[n]]);
892
893 /* if someone had us open, reset the non-wake threshold stuff */
894 if (lis->flags & LIS302DL_F_INPUT_OPEN)
895 __enable_data_collection(lis);
896
897 local_irq_restore(flags);
898 enable_irq(lis->pdata->interrupt);
899
900 return 0;
901 }
902 #else
903 #define lis302dl_suspend NULL
904 #define lis302dl_resume NULL
905 #endif
906
907 static struct spi_driver lis302dl_spi_driver = {
908 .driver = {
909 .name = "lis302dl",
910 .owner = THIS_MODULE,
911 },
912
913 .probe = lis302dl_probe,
914 .remove = __devexit_p(lis302dl_remove),
915 .suspend = lis302dl_suspend,
916 .resume = lis302dl_resume,
917 };
918
919 static int __devinit lis302dl_init(void)
920 {
921 return spi_register_driver(&lis302dl_spi_driver);
922 }
923 module_init(lis302dl_init);
924
925 static void __exit lis302dl_exit(void)
926 {
927 spi_unregister_driver(&lis302dl_spi_driver);
928 }
929 module_exit(lis302dl_exit);
930
931 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
932 MODULE_LICENSE("GPL");
This page took 0.087028 seconds and 5 git commands to generate.