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