hwmon: (lis3) add axes module parameter for custom axis-mapping
[pandora-kernel.git] / drivers / hwmon / lis3lv02d.c
1 /*
2  *  lis3lv02d.c - ST LIS3LV02DL accelerometer driver
3  *
4  *  Copyright (C) 2007-2008 Yan Burman
5  *  Copyright (C) 2008 Eric Piel
6  *  Copyright (C) 2008-2009 Pavel Machek
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/dmi.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/platform_device.h>
29 #include <linux/interrupt.h>
30 #include <linux/input-polldev.h>
31 #include <linux/delay.h>
32 #include <linux/wait.h>
33 #include <linux/poll.h>
34 #include <linux/freezer.h>
35 #include <linux/uaccess.h>
36 #include <linux/miscdevice.h>
37 #include <asm/atomic.h>
38 #include "lis3lv02d.h"
39
40 #define DRIVER_NAME     "lis3lv02d"
41
42 /* joystick device poll interval in milliseconds */
43 #define MDPS_POLL_INTERVAL 50
44 #define MDPS_POLL_MIN      0
45 #define MDPS_POLL_MAX      2000
46 /*
47  * The sensor can also generate interrupts (DRDY) but it's pretty pointless
48  * because they are generated even if the data do not change. So it's better
49  * to keep the interrupt for the free-fall event. The values are updated at
50  * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
51  * some low processor, we poll the sensor only at 20Hz... enough for the
52  * joystick.
53  */
54
55 #define LIS3_PWRON_DELAY_WAI_12B        (5000)
56 #define LIS3_PWRON_DELAY_WAI_8B         (3000)
57
58 /*
59  * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
60  * LIS302D spec says: 18 mG / digit
61  * LIS3_ACCURACY is used to increase accuracy of the intermediate
62  * calculation results.
63  */
64 #define LIS3_ACCURACY                   1024
65 /* Sensitivity values for -2G +2G scale */
66 #define LIS3_SENSITIVITY_12B            ((LIS3_ACCURACY * 1000) / 1024)
67 #define LIS3_SENSITIVITY_8B             (18 * LIS3_ACCURACY)
68
69 #define LIS3_DEFAULT_FUZZ               3
70 #define LIS3_DEFAULT_FLAT               3
71
72 struct lis3lv02d lis3_dev = {
73         .misc_wait   = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
74 };
75
76 EXPORT_SYMBOL_GPL(lis3_dev);
77
78 /* just like param_set_int() but does sanity-check so that it won't point
79  * over the axis array size
80  */
81 static int param_set_axis(const char *val, const struct kernel_param *kp)
82 {
83         int ret = param_set_int(val, kp);
84         if (!ret) {
85                 int val = *(int *)kp->arg;
86                 if (val < 0)
87                         val = -val;
88                 if (!val || val > 3)
89                         return -EINVAL;
90         }
91         return ret;
92 }
93
94 static struct kernel_param_ops param_ops_axis = {
95         .set = param_set_axis,
96         .get = param_get_int,
97 };
98
99 module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
100 MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
101
102 static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
103 {
104         s8 lo;
105         if (lis3->read(lis3, reg, &lo) < 0)
106                 return 0;
107
108         return lo;
109 }
110
111 static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
112 {
113         u8 lo, hi;
114
115         lis3->read(lis3, reg - 1, &lo);
116         lis3->read(lis3, reg, &hi);
117         /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
118         return (s16)((hi << 8) | lo);
119 }
120
121 /**
122  * lis3lv02d_get_axis - For the given axis, give the value converted
123  * @axis:      1,2,3 - can also be negative
124  * @hw_values: raw values returned by the hardware
125  *
126  * Returns the converted value.
127  */
128 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
129 {
130         if (axis > 0)
131                 return hw_values[axis - 1];
132         else
133                 return -hw_values[-axis - 1];
134 }
135
136 /**
137  * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
138  * @lis3: pointer to the device struct
139  * @x:    where to store the X axis value
140  * @y:    where to store the Y axis value
141  * @z:    where to store the Z axis value
142  *
143  * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
144  */
145 static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
146 {
147         int position[3];
148         int i;
149
150         position[0] = lis3->read_data(lis3, OUTX);
151         position[1] = lis3->read_data(lis3, OUTY);
152         position[2] = lis3->read_data(lis3, OUTZ);
153
154         for (i = 0; i < 3; i++)
155                 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
156
157         *x = lis3lv02d_get_axis(lis3->ac.x, position);
158         *y = lis3lv02d_get_axis(lis3->ac.y, position);
159         *z = lis3lv02d_get_axis(lis3->ac.z, position);
160 }
161
162 /* conversion btw sampling rate and the register values */
163 static int lis3_12_rates[4] = {40, 160, 640, 2560};
164 static int lis3_8_rates[2] = {100, 400};
165 static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
166
167 /* ODR is Output Data Rate */
168 static int lis3lv02d_get_odr(void)
169 {
170         u8 ctrl;
171         int shift;
172
173         lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
174         ctrl &= lis3_dev.odr_mask;
175         shift = ffs(lis3_dev.odr_mask) - 1;
176         return lis3_dev.odrs[(ctrl >> shift)];
177 }
178
179 static int lis3lv02d_set_odr(int rate)
180 {
181         u8 ctrl;
182         int i, len, shift;
183
184         if (!rate)
185                 return -EINVAL;
186
187         lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
188         ctrl &= ~lis3_dev.odr_mask;
189         len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
190         shift = ffs(lis3_dev.odr_mask) - 1;
191
192         for (i = 0; i < len; i++)
193                 if (lis3_dev.odrs[i] == rate) {
194                         lis3_dev.write(&lis3_dev, CTRL_REG1,
195                                         ctrl | (i << shift));
196                         return 0;
197                 }
198         return -EINVAL;
199 }
200
201 static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
202 {
203         u8 ctlreg, reg;
204         s16 x, y, z;
205         u8 selftest;
206         int ret;
207
208         mutex_lock(&lis3->mutex);
209         if (lis3_dev.whoami == WAI_3DC) {
210                 ctlreg = CTRL_REG4;
211                 selftest = CTRL4_ST0;
212         } else {
213                 ctlreg = CTRL_REG1;
214                 if (lis3_dev.whoami == WAI_12B)
215                         selftest = CTRL1_ST;
216                 else
217                         selftest = CTRL1_STP;
218         }
219
220         lis3->read(lis3, ctlreg, &reg);
221         lis3->write(lis3, ctlreg, (reg | selftest));
222         msleep(lis3->pwron_delay / lis3lv02d_get_odr());
223
224         /* Read directly to avoid axis remap */
225         x = lis3->read_data(lis3, OUTX);
226         y = lis3->read_data(lis3, OUTY);
227         z = lis3->read_data(lis3, OUTZ);
228
229         /* back to normal settings */
230         lis3->write(lis3, ctlreg, reg);
231         msleep(lis3->pwron_delay / lis3lv02d_get_odr());
232
233         results[0] = x - lis3->read_data(lis3, OUTX);
234         results[1] = y - lis3->read_data(lis3, OUTY);
235         results[2] = z - lis3->read_data(lis3, OUTZ);
236
237         ret = 0;
238         if (lis3->pdata) {
239                 int i;
240                 for (i = 0; i < 3; i++) {
241                         /* Check against selftest acceptance limits */
242                         if ((results[i] < lis3->pdata->st_min_limits[i]) ||
243                             (results[i] > lis3->pdata->st_max_limits[i])) {
244                                 ret = -EIO;
245                                 goto fail;
246                         }
247                 }
248         }
249
250         /* test passed */
251 fail:
252         mutex_unlock(&lis3->mutex);
253         return ret;
254 }
255
256 void lis3lv02d_poweroff(struct lis3lv02d *lis3)
257 {
258         /* disable X,Y,Z axis and power down */
259         lis3->write(lis3, CTRL_REG1, 0x00);
260 }
261 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
262
263 void lis3lv02d_poweron(struct lis3lv02d *lis3)
264 {
265         u8 reg;
266
267         lis3->init(lis3);
268
269         /* LIS3 power on delay is quite long */
270         msleep(lis3->pwron_delay / lis3lv02d_get_odr());
271
272         /*
273          * Common configuration
274          * BDU: (12 bits sensors only) LSB and MSB values are not updated until
275          *      both have been read. So the value read will always be correct.
276          */
277         if (lis3->whoami ==  WAI_12B) {
278                 lis3->read(lis3, CTRL_REG2, &reg);
279                 reg |= CTRL2_BDU;
280                 lis3->write(lis3, CTRL_REG2, reg);
281         }
282 }
283 EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
284
285
286 static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
287 {
288         int x, y, z;
289
290         mutex_lock(&lis3_dev.mutex);
291         lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
292         input_report_abs(pidev->input, ABS_X, x);
293         input_report_abs(pidev->input, ABS_Y, y);
294         input_report_abs(pidev->input, ABS_Z, z);
295         input_sync(pidev->input);
296         mutex_unlock(&lis3_dev.mutex);
297 }
298
299 static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
300 {
301         if (!test_bit(0, &lis3_dev.misc_opened))
302                 goto out;
303
304         /*
305          * Be careful: on some HP laptops the bios force DD when on battery and
306          * the lid is closed. This leads to interrupts as soon as a little move
307          * is done.
308          */
309         atomic_inc(&lis3_dev.count);
310
311         wake_up_interruptible(&lis3_dev.misc_wait);
312         kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
313 out:
314         if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev &&
315             lis3_dev.idev->input->users)
316                 return IRQ_WAKE_THREAD;
317         return IRQ_HANDLED;
318 }
319
320 static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
321 {
322         struct input_dev *dev = lis3->idev->input;
323         u8 click_src;
324
325         mutex_lock(&lis3->mutex);
326         lis3->read(lis3, CLICK_SRC, &click_src);
327
328         if (click_src & CLICK_SINGLE_X) {
329                 input_report_key(dev, lis3->mapped_btns[0], 1);
330                 input_report_key(dev, lis3->mapped_btns[0], 0);
331         }
332
333         if (click_src & CLICK_SINGLE_Y) {
334                 input_report_key(dev, lis3->mapped_btns[1], 1);
335                 input_report_key(dev, lis3->mapped_btns[1], 0);
336         }
337
338         if (click_src & CLICK_SINGLE_Z) {
339                 input_report_key(dev, lis3->mapped_btns[2], 1);
340                 input_report_key(dev, lis3->mapped_btns[2], 0);
341         }
342         input_sync(dev);
343         mutex_unlock(&lis3->mutex);
344 }
345
346 static void lis302dl_interrupt_handle_ff_wu(struct lis3lv02d *lis3)
347 {
348         u8 wu1_src;
349         u8 wu2_src;
350
351         lis3->read(lis3, FF_WU_SRC_1, &wu1_src);
352         lis3->read(lis3, FF_WU_SRC_2, &wu2_src);
353
354         wu1_src = wu1_src & FF_WU_SRC_IA ? wu1_src : 0;
355         wu2_src = wu2_src & FF_WU_SRC_IA ? wu2_src : 0;
356
357         /* joystick poll is internally protected by the lis3->mutex. */
358         if (wu1_src || wu2_src)
359                 lis3lv02d_joystick_poll(lis3_dev.idev);
360 }
361
362 static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
363 {
364
365         struct lis3lv02d *lis3 = data;
366
367         if ((lis3->pdata->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
368                 lis302dl_interrupt_handle_click(lis3);
369         else
370                 lis302dl_interrupt_handle_ff_wu(lis3);
371
372         return IRQ_HANDLED;
373 }
374
375 static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
376 {
377
378         struct lis3lv02d *lis3 = data;
379
380         if ((lis3->pdata->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
381                 lis302dl_interrupt_handle_click(lis3);
382         else
383                 lis302dl_interrupt_handle_ff_wu(lis3);
384
385         return IRQ_HANDLED;
386 }
387
388 static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
389 {
390         if (test_and_set_bit(0, &lis3_dev.misc_opened))
391                 return -EBUSY; /* already open */
392
393         atomic_set(&lis3_dev.count, 0);
394         return 0;
395 }
396
397 static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
398 {
399         fasync_helper(-1, file, 0, &lis3_dev.async_queue);
400         clear_bit(0, &lis3_dev.misc_opened); /* release the device */
401         return 0;
402 }
403
404 static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
405                                 size_t count, loff_t *pos)
406 {
407         DECLARE_WAITQUEUE(wait, current);
408         u32 data;
409         unsigned char byte_data;
410         ssize_t retval = 1;
411
412         if (count < 1)
413                 return -EINVAL;
414
415         add_wait_queue(&lis3_dev.misc_wait, &wait);
416         while (true) {
417                 set_current_state(TASK_INTERRUPTIBLE);
418                 data = atomic_xchg(&lis3_dev.count, 0);
419                 if (data)
420                         break;
421
422                 if (file->f_flags & O_NONBLOCK) {
423                         retval = -EAGAIN;
424                         goto out;
425                 }
426
427                 if (signal_pending(current)) {
428                         retval = -ERESTARTSYS;
429                         goto out;
430                 }
431
432                 schedule();
433         }
434
435         if (data < 255)
436                 byte_data = data;
437         else
438                 byte_data = 255;
439
440         /* make sure we are not going into copy_to_user() with
441          * TASK_INTERRUPTIBLE state */
442         set_current_state(TASK_RUNNING);
443         if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
444                 retval = -EFAULT;
445
446 out:
447         __set_current_state(TASK_RUNNING);
448         remove_wait_queue(&lis3_dev.misc_wait, &wait);
449
450         return retval;
451 }
452
453 static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
454 {
455         poll_wait(file, &lis3_dev.misc_wait, wait);
456         if (atomic_read(&lis3_dev.count))
457                 return POLLIN | POLLRDNORM;
458         return 0;
459 }
460
461 static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
462 {
463         return fasync_helper(fd, file, on, &lis3_dev.async_queue);
464 }
465
466 static const struct file_operations lis3lv02d_misc_fops = {
467         .owner   = THIS_MODULE,
468         .llseek  = no_llseek,
469         .read    = lis3lv02d_misc_read,
470         .open    = lis3lv02d_misc_open,
471         .release = lis3lv02d_misc_release,
472         .poll    = lis3lv02d_misc_poll,
473         .fasync  = lis3lv02d_misc_fasync,
474 };
475
476 static struct miscdevice lis3lv02d_misc_device = {
477         .minor   = MISC_DYNAMIC_MINOR,
478         .name    = "freefall",
479         .fops    = &lis3lv02d_misc_fops,
480 };
481
482 int lis3lv02d_joystick_enable(void)
483 {
484         struct input_dev *input_dev;
485         int err;
486         int max_val, fuzz, flat;
487         int btns[] = {BTN_X, BTN_Y, BTN_Z};
488
489         if (lis3_dev.idev)
490                 return -EINVAL;
491
492         lis3_dev.idev = input_allocate_polled_device();
493         if (!lis3_dev.idev)
494                 return -ENOMEM;
495
496         lis3_dev.idev->poll = lis3lv02d_joystick_poll;
497         lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
498         lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN;
499         lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX;
500         input_dev = lis3_dev.idev->input;
501
502         input_dev->name       = "ST LIS3LV02DL Accelerometer";
503         input_dev->phys       = DRIVER_NAME "/input0";
504         input_dev->id.bustype = BUS_HOST;
505         input_dev->id.vendor  = 0;
506         input_dev->dev.parent = &lis3_dev.pdev->dev;
507
508         set_bit(EV_ABS, input_dev->evbit);
509         max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
510         fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
511         flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
512         input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
513         input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
514         input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
515
516         lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
517         lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
518         lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
519
520         err = input_register_polled_device(lis3_dev.idev);
521         if (err) {
522                 input_free_polled_device(lis3_dev.idev);
523                 lis3_dev.idev = NULL;
524         }
525
526         return err;
527 }
528 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
529
530 void lis3lv02d_joystick_disable(void)
531 {
532         if (lis3_dev.irq)
533                 free_irq(lis3_dev.irq, &lis3_dev);
534         if (lis3_dev.pdata && lis3_dev.pdata->irq2)
535                 free_irq(lis3_dev.pdata->irq2, &lis3_dev);
536
537         if (!lis3_dev.idev)
538                 return;
539
540         if (lis3_dev.irq)
541                 misc_deregister(&lis3lv02d_misc_device);
542         input_unregister_polled_device(lis3_dev.idev);
543         input_free_polled_device(lis3_dev.idev);
544         lis3_dev.idev = NULL;
545 }
546 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
547
548 /* Sysfs stuff */
549 static ssize_t lis3lv02d_selftest_show(struct device *dev,
550                                 struct device_attribute *attr, char *buf)
551 {
552         int result;
553         s16 values[3];
554
555         result = lis3lv02d_selftest(&lis3_dev, values);
556         return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
557                 values[0], values[1], values[2]);
558 }
559
560 static ssize_t lis3lv02d_position_show(struct device *dev,
561                                 struct device_attribute *attr, char *buf)
562 {
563         int x, y, z;
564
565         mutex_lock(&lis3_dev.mutex);
566         lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
567         mutex_unlock(&lis3_dev.mutex);
568         return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
569 }
570
571 static ssize_t lis3lv02d_rate_show(struct device *dev,
572                         struct device_attribute *attr, char *buf)
573 {
574         return sprintf(buf, "%d\n", lis3lv02d_get_odr());
575 }
576
577 static ssize_t lis3lv02d_rate_set(struct device *dev,
578                                 struct device_attribute *attr, const char *buf,
579                                 size_t count)
580 {
581         unsigned long rate;
582
583         if (strict_strtoul(buf, 0, &rate))
584                 return -EINVAL;
585
586         if (lis3lv02d_set_odr(rate))
587                 return -EINVAL;
588
589         return count;
590 }
591
592 static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
593 static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
594 static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
595                                             lis3lv02d_rate_set);
596
597 static struct attribute *lis3lv02d_attributes[] = {
598         &dev_attr_selftest.attr,
599         &dev_attr_position.attr,
600         &dev_attr_rate.attr,
601         NULL
602 };
603
604 static struct attribute_group lis3lv02d_attribute_group = {
605         .attrs = lis3lv02d_attributes
606 };
607
608
609 static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
610 {
611         lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
612         if (IS_ERR(lis3->pdev))
613                 return PTR_ERR(lis3->pdev);
614
615         return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
616 }
617
618 int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
619 {
620         sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
621         platform_device_unregister(lis3->pdev);
622         return 0;
623 }
624 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
625
626 static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
627                                 struct lis3lv02d_platform_data *p)
628 {
629         int err;
630         int ctrl2 = p->hipass_ctrl;
631
632         if (p->click_flags) {
633                 dev->write(dev, CLICK_CFG, p->click_flags);
634                 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
635                 dev->write(dev, CLICK_LATENCY, p->click_latency);
636                 dev->write(dev, CLICK_WINDOW, p->click_window);
637                 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
638                 dev->write(dev, CLICK_THSY_X,
639                         (p->click_thresh_x & 0xf) |
640                         (p->click_thresh_y << 4));
641
642                 if (dev->idev) {
643                         struct input_dev *input_dev = lis3_dev.idev->input;
644                         input_set_capability(input_dev, EV_KEY, BTN_X);
645                         input_set_capability(input_dev, EV_KEY, BTN_Y);
646                         input_set_capability(input_dev, EV_KEY, BTN_Z);
647                 }
648         }
649
650         if (p->wakeup_flags) {
651                 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
652                 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
653                 /* default to 2.5ms for now */
654                 dev->write(dev, FF_WU_DURATION_1, 1);
655                 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
656         }
657
658         if (p->wakeup_flags2) {
659                 dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
660                 dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
661                 /* default to 2.5ms for now */
662                 dev->write(dev, FF_WU_DURATION_2, 1);
663                 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
664         }
665         /* Configure hipass filters */
666         dev->write(dev, CTRL_REG2, ctrl2);
667
668         if (p->irq2) {
669                 err = request_threaded_irq(p->irq2,
670                                         NULL,
671                                         lis302dl_interrupt_thread2_8b,
672                                         IRQF_TRIGGER_RISING |
673                                         IRQF_ONESHOT,
674                                         DRIVER_NAME, &lis3_dev);
675                 if (err < 0)
676                         printk(KERN_ERR DRIVER_NAME
677                                 "No second IRQ. Limited functionality\n");
678         }
679 }
680
681 /*
682  * Initialise the accelerometer and the various subsystems.
683  * Should be rather independent of the bus system.
684  */
685 int lis3lv02d_init_device(struct lis3lv02d *dev)
686 {
687         int err;
688         irq_handler_t thread_fn;
689
690         dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
691
692         switch (dev->whoami) {
693         case WAI_12B:
694                 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
695                 dev->read_data = lis3lv02d_read_12;
696                 dev->mdps_max_val = 2048;
697                 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
698                 dev->odrs = lis3_12_rates;
699                 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
700                 dev->scale = LIS3_SENSITIVITY_12B;
701                 break;
702         case WAI_8B:
703                 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
704                 dev->read_data = lis3lv02d_read_8;
705                 dev->mdps_max_val = 128;
706                 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
707                 dev->odrs = lis3_8_rates;
708                 dev->odr_mask = CTRL1_DR;
709                 dev->scale = LIS3_SENSITIVITY_8B;
710                 break;
711         case WAI_3DC:
712                 printk(KERN_INFO DRIVER_NAME ": 8 bits 3DC sensor found\n");
713                 dev->read_data = lis3lv02d_read_8;
714                 dev->mdps_max_val = 128;
715                 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
716                 dev->odrs = lis3_3dc_rates;
717                 dev->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
718                 dev->scale = LIS3_SENSITIVITY_8B;
719                 break;
720         default:
721                 printk(KERN_ERR DRIVER_NAME
722                         ": unknown sensor type 0x%X\n", dev->whoami);
723                 return -EINVAL;
724         }
725
726         mutex_init(&dev->mutex);
727
728         lis3lv02d_add_fs(dev);
729         lis3lv02d_poweron(dev);
730
731         if (lis3lv02d_joystick_enable())
732                 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
733
734         /* passing in platform specific data is purely optional and only
735          * used by the SPI transport layer at the moment */
736         if (dev->pdata) {
737                 struct lis3lv02d_platform_data *p = dev->pdata;
738
739                 if (dev->whoami == WAI_8B)
740                         lis3lv02d_8b_configure(dev, p);
741
742                 if (p->irq_cfg)
743                         dev->write(dev, CTRL_REG3, p->irq_cfg);
744         }
745
746         /* bail if we did not get an IRQ from the bus layer */
747         if (!dev->irq) {
748                 printk(KERN_ERR DRIVER_NAME
749                         ": No IRQ. Disabling /dev/freefall\n");
750                 goto out;
751         }
752
753         /*
754          * The sensor can generate interrupts for free-fall and direction
755          * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
756          * the things simple and _fast_ we activate it only for free-fall, so
757          * no need to read register (very slow with ACPI). For the same reason,
758          * we forbid shared interrupts.
759          *
760          * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
761          * io-apic is not configurable (and generates a warning) but I keep it
762          * in case of support for other hardware.
763          */
764         if (dev->pdata && dev->whoami == WAI_8B)
765                 thread_fn = lis302dl_interrupt_thread1_8b;
766         else
767                 thread_fn = NULL;
768
769         err = request_threaded_irq(dev->irq, lis302dl_interrupt,
770                                 thread_fn,
771                                 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
772                                 DRIVER_NAME, &lis3_dev);
773
774         if (err < 0) {
775                 printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
776                 goto out;
777         }
778
779         if (misc_register(&lis3lv02d_misc_device))
780                 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
781 out:
782         return 0;
783 }
784 EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
785
786 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
787 MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
788 MODULE_LICENSE("GPL");