9d37c576d526674dc12dc5dc478ffc45374c2c36
[firefly-linux-kernel-4.4.55.git] / drivers / misc / lis3lv02d / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/dmi.h>
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/platform_device.h>
31 #include <linux/interrupt.h>
32 #include <linux/input-polldev.h>
33 #include <linux/delay.h>
34 #include <linux/wait.h>
35 #include <linux/poll.h>
36 #include <linux/slab.h>
37 #include <linux/freezer.h>
38 #include <linux/uaccess.h>
39 #include <linux/miscdevice.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/atomic.h>
42 #include "lis3lv02d.h"
43
44 #define DRIVER_NAME     "lis3lv02d"
45
46 /* joystick device poll interval in milliseconds */
47 #define MDPS_POLL_INTERVAL 50
48 #define MDPS_POLL_MIN      0
49 #define MDPS_POLL_MAX      2000
50
51 #define LIS3_SYSFS_POWERDOWN_DELAY 5000 /* In milliseconds */
52
53 #define SELFTEST_OK            0
54 #define SELFTEST_FAIL          -1
55 #define SELFTEST_IRQ           -2
56
57 #define IRQ_LINE0              0
58 #define IRQ_LINE1              1
59
60 /*
61  * The sensor can also generate interrupts (DRDY) but it's pretty pointless
62  * because they are generated even if the data do not change. So it's better
63  * to keep the interrupt for the free-fall event. The values are updated at
64  * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
65  * some low processor, we poll the sensor only at 20Hz... enough for the
66  * joystick.
67  */
68
69 #define LIS3_PWRON_DELAY_WAI_12B        (5000)
70 #define LIS3_PWRON_DELAY_WAI_8B         (3000)
71
72 /*
73  * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
74  * LIS302D spec says: 18 mG / digit
75  * LIS3_ACCURACY is used to increase accuracy of the intermediate
76  * calculation results.
77  */
78 #define LIS3_ACCURACY                   1024
79 /* Sensitivity values for -2G +2G scale */
80 #define LIS3_SENSITIVITY_12B            ((LIS3_ACCURACY * 1000) / 1024)
81 #define LIS3_SENSITIVITY_8B             (18 * LIS3_ACCURACY)
82
83 /*
84  * LIS3331DLH spec says 1LSBs corresponds 4G/1024 -> 1LSB is 1000/1024 mG.
85  * Sensitivity values for +/-2G, outdata in 12 bits for +/-2G scale. so 4
86  * bits adjustment is required
87  */
88 #define LIS3DLH_SENSITIVITY_2G          ((LIS3_ACCURACY * 1000) / 1024)
89 #define SHIFT_ADJ_2G                    4
90
91 #define LIS3_DEFAULT_FUZZ_12B           3
92 #define LIS3_DEFAULT_FLAT_12B           3
93 #define LIS3_DEFAULT_FUZZ_8B            1
94 #define LIS3_DEFAULT_FLAT_8B            1
95
96 struct lis3lv02d lis3_dev = {
97         .misc_wait   = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
98 };
99 EXPORT_SYMBOL_GPL(lis3_dev);
100
101 /* just like param_set_int() but does sanity-check so that it won't point
102  * over the axis array size
103  */
104 static int param_set_axis(const char *val, const struct kernel_param *kp)
105 {
106         int ret = param_set_int(val, kp);
107         if (!ret) {
108                 int val = *(int *)kp->arg;
109                 if (val < 0)
110                         val = -val;
111                 if (!val || val > 3)
112                         return -EINVAL;
113         }
114         return ret;
115 }
116
117 static struct kernel_param_ops param_ops_axis = {
118         .set = param_set_axis,
119         .get = param_get_int,
120 };
121
122 #define param_check_axis(name, p) param_check_int(name, p)
123
124 module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
125 MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
126
127 static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
128 {
129         s8 lo;
130         if (lis3->read(lis3, reg, &lo) < 0)
131                 return 0;
132
133         return lo;
134 }
135
136 static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
137 {
138         u8 lo, hi;
139
140         lis3->read(lis3, reg - 1, &lo);
141         lis3->read(lis3, reg, &hi);
142         /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
143         return (s16)((hi << 8) | lo);
144 }
145
146 /* 12bits for 2G range, 13 bits for 4G range and 14 bits for 8G range */
147 static s16 lis3lv02d_read_16(struct lis3lv02d *lis3, int reg)
148 {
149         u8 lo, hi;
150         int v;
151
152         lis3->read(lis3, reg - 1, &lo);
153         lis3->read(lis3, reg, &hi);
154         v = (int) ((hi << 8) | lo);
155
156         return (s16) v >> lis3->shift_adj;
157 }
158
159 /**
160  * lis3lv02d_get_axis - For the given axis, give the value converted
161  * @axis:      1,2,3 - can also be negative
162  * @hw_values: raw values returned by the hardware
163  *
164  * Returns the converted value.
165  */
166 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
167 {
168         if (axis > 0)
169                 return hw_values[axis - 1];
170         else
171                 return -hw_values[-axis - 1];
172 }
173
174 /**
175  * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
176  * @lis3: pointer to the device struct
177  * @x:    where to store the X axis value
178  * @y:    where to store the Y axis value
179  * @z:    where to store the Z axis value
180  *
181  * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
182  */
183 static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
184 {
185         int position[3];
186         int i;
187
188         if (lis3->blkread) {
189                 if (lis3->whoami == WAI_12B) {
190                         u16 data[3];
191                         lis3->blkread(lis3, OUTX_L, 6, (u8 *)data);
192                         for (i = 0; i < 3; i++)
193                                 position[i] = (s16)le16_to_cpu(data[i]);
194                 } else {
195                         u8 data[5];
196                         /* Data: x, dummy, y, dummy, z */
197                         lis3->blkread(lis3, OUTX, 5, data);
198                         for (i = 0; i < 3; i++)
199                                 position[i] = (s8)data[i * 2];
200                 }
201         } else {
202                 position[0] = lis3->read_data(lis3, OUTX);
203                 position[1] = lis3->read_data(lis3, OUTY);
204                 position[2] = lis3->read_data(lis3, OUTZ);
205         }
206
207         for (i = 0; i < 3; i++)
208                 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
209
210         *x = lis3lv02d_get_axis(lis3->ac.x, position);
211         *y = lis3lv02d_get_axis(lis3->ac.y, position);
212         *z = lis3lv02d_get_axis(lis3->ac.z, position);
213 }
214
215 /* conversion btw sampling rate and the register values */
216 static int lis3_12_rates[4] = {40, 160, 640, 2560};
217 static int lis3_8_rates[2] = {100, 400};
218 static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
219 static int lis3_3dlh_rates[4] = {50, 100, 400, 1000};
220
221 /* ODR is Output Data Rate */
222 static int lis3lv02d_get_odr(struct lis3lv02d *lis3)
223 {
224         u8 ctrl;
225         int shift;
226
227         lis3->read(lis3, CTRL_REG1, &ctrl);
228         ctrl &= lis3->odr_mask;
229         shift = ffs(lis3->odr_mask) - 1;
230         return lis3->odrs[(ctrl >> shift)];
231 }
232
233 static int lis3lv02d_get_pwron_wait(struct lis3lv02d *lis3)
234 {
235         int div = lis3lv02d_get_odr(lis3);
236
237         if (WARN_ONCE(div == 0, "device returned spurious data"))
238                 return -ENXIO;
239
240         /* LIS3 power on delay is quite long */
241         msleep(lis3->pwron_delay / div);
242         return 0;
243 }
244
245 static int lis3lv02d_set_odr(struct lis3lv02d *lis3, int rate)
246 {
247         u8 ctrl;
248         int i, len, shift;
249
250         if (!rate)
251                 return -EINVAL;
252
253         lis3->read(lis3, CTRL_REG1, &ctrl);
254         ctrl &= ~lis3->odr_mask;
255         len = 1 << hweight_long(lis3->odr_mask); /* # of possible values */
256         shift = ffs(lis3->odr_mask) - 1;
257
258         for (i = 0; i < len; i++)
259                 if (lis3->odrs[i] == rate) {
260                         lis3->write(lis3, CTRL_REG1,
261                                         ctrl | (i << shift));
262                         return 0;
263                 }
264         return -EINVAL;
265 }
266
267 static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
268 {
269         u8 ctlreg, reg;
270         s16 x, y, z;
271         u8 selftest;
272         int ret;
273         u8 ctrl_reg_data;
274         unsigned char irq_cfg;
275
276         mutex_lock(&lis3->mutex);
277
278         irq_cfg = lis3->irq_cfg;
279         if (lis3->whoami == WAI_8B) {
280                 lis3->data_ready_count[IRQ_LINE0] = 0;
281                 lis3->data_ready_count[IRQ_LINE1] = 0;
282
283                 /* Change interrupt cfg to data ready for selftest */
284                 atomic_inc(&lis3->wake_thread);
285                 lis3->irq_cfg = LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY;
286                 lis3->read(lis3, CTRL_REG3, &ctrl_reg_data);
287                 lis3->write(lis3, CTRL_REG3, (ctrl_reg_data &
288                                 ~(LIS3_IRQ1_MASK | LIS3_IRQ2_MASK)) |
289                                 (LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY));
290         }
291
292         if ((lis3->whoami == WAI_3DC) || (lis3->whoami == WAI_3DLH)) {
293                 ctlreg = CTRL_REG4;
294                 selftest = CTRL4_ST0;
295         } else {
296                 ctlreg = CTRL_REG1;
297                 if (lis3->whoami == WAI_12B)
298                         selftest = CTRL1_ST;
299                 else
300                         selftest = CTRL1_STP;
301         }
302
303         lis3->read(lis3, ctlreg, &reg);
304         lis3->write(lis3, ctlreg, (reg | selftest));
305         ret = lis3lv02d_get_pwron_wait(lis3);
306         if (ret)
307                 goto fail;
308
309         /* Read directly to avoid axis remap */
310         x = lis3->read_data(lis3, OUTX);
311         y = lis3->read_data(lis3, OUTY);
312         z = lis3->read_data(lis3, OUTZ);
313
314         /* back to normal settings */
315         lis3->write(lis3, ctlreg, reg);
316         ret = lis3lv02d_get_pwron_wait(lis3);
317         if (ret)
318                 goto fail;
319
320         results[0] = x - lis3->read_data(lis3, OUTX);
321         results[1] = y - lis3->read_data(lis3, OUTY);
322         results[2] = z - lis3->read_data(lis3, OUTZ);
323
324         ret = 0;
325
326         if (lis3->whoami == WAI_8B) {
327                 /* Restore original interrupt configuration */
328                 atomic_dec(&lis3->wake_thread);
329                 lis3->write(lis3, CTRL_REG3, ctrl_reg_data);
330                 lis3->irq_cfg = irq_cfg;
331
332                 if ((irq_cfg & LIS3_IRQ1_MASK) &&
333                         lis3->data_ready_count[IRQ_LINE0] < 2) {
334                         ret = SELFTEST_IRQ;
335                         goto fail;
336                 }
337
338                 if ((irq_cfg & LIS3_IRQ2_MASK) &&
339                         lis3->data_ready_count[IRQ_LINE1] < 2) {
340                         ret = SELFTEST_IRQ;
341                         goto fail;
342                 }
343         }
344
345         if (lis3->pdata) {
346                 int i;
347                 for (i = 0; i < 3; i++) {
348                         /* Check against selftest acceptance limits */
349                         if ((results[i] < lis3->pdata->st_min_limits[i]) ||
350                             (results[i] > lis3->pdata->st_max_limits[i])) {
351                                 ret = SELFTEST_FAIL;
352                                 goto fail;
353                         }
354                 }
355         }
356
357         /* test passed */
358 fail:
359         mutex_unlock(&lis3->mutex);
360         return ret;
361 }
362
363 /*
364  * Order of registers in the list affects to order of the restore process.
365  * Perhaps it is a good idea to set interrupt enable register as a last one
366  * after all other configurations
367  */
368 static u8 lis3_wai8_regs[] = { FF_WU_CFG_1, FF_WU_THS_1, FF_WU_DURATION_1,
369                                FF_WU_CFG_2, FF_WU_THS_2, FF_WU_DURATION_2,
370                                CLICK_CFG, CLICK_SRC, CLICK_THSY_X, CLICK_THSZ,
371                                CLICK_TIMELIMIT, CLICK_LATENCY, CLICK_WINDOW,
372                                CTRL_REG1, CTRL_REG2, CTRL_REG3};
373
374 static u8 lis3_wai12_regs[] = {FF_WU_CFG, FF_WU_THS_L, FF_WU_THS_H,
375                                FF_WU_DURATION, DD_CFG, DD_THSI_L, DD_THSI_H,
376                                DD_THSE_L, DD_THSE_H,
377                                CTRL_REG1, CTRL_REG3, CTRL_REG2};
378
379 static inline void lis3_context_save(struct lis3lv02d *lis3)
380 {
381         int i;
382         for (i = 0; i < lis3->regs_size; i++)
383                 lis3->read(lis3, lis3->regs[i], &lis3->reg_cache[i]);
384         lis3->regs_stored = true;
385 }
386
387 static inline void lis3_context_restore(struct lis3lv02d *lis3)
388 {
389         int i;
390         if (lis3->regs_stored)
391                 for (i = 0; i < lis3->regs_size; i++)
392                         lis3->write(lis3, lis3->regs[i], lis3->reg_cache[i]);
393 }
394
395 void lis3lv02d_poweroff(struct lis3lv02d *lis3)
396 {
397         if (lis3->reg_ctrl)
398                 lis3_context_save(lis3);
399         /* disable X,Y,Z axis and power down */
400         lis3->write(lis3, CTRL_REG1, 0x00);
401         if (lis3->reg_ctrl)
402                 lis3->reg_ctrl(lis3, LIS3_REG_OFF);
403 }
404 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
405
406 int lis3lv02d_poweron(struct lis3lv02d *lis3)
407 {
408         int err;
409         u8 reg;
410
411         lis3->init(lis3);
412
413         /*
414          * Common configuration
415          * BDU: (12 bits sensors only) LSB and MSB values are not updated until
416          *      both have been read. So the value read will always be correct.
417          * Set BOOT bit to refresh factory tuning values.
418          */
419         if (lis3->pdata) {
420                 lis3->read(lis3, CTRL_REG2, &reg);
421                 if (lis3->whoami ==  WAI_12B)
422                         reg |= CTRL2_BDU | CTRL2_BOOT;
423                 else if (lis3->whoami ==  WAI_3DLH)
424                         reg |= CTRL2_BOOT_3DLH;
425                 else
426                         reg |= CTRL2_BOOT_8B;
427                 lis3->write(lis3, CTRL_REG2, reg);
428
429                 if (lis3->whoami ==  WAI_3DLH) {
430                         lis3->read(lis3, CTRL_REG4, &reg);
431                         reg |= CTRL4_BDU;
432                         lis3->write(lis3, CTRL_REG4, reg);
433                 }
434         }
435
436         err = lis3lv02d_get_pwron_wait(lis3);
437         if (err)
438                 return err;
439
440         if (lis3->reg_ctrl)
441                 lis3_context_restore(lis3);
442
443         return 0;
444 }
445 EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
446
447
448 static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
449 {
450         struct lis3lv02d *lis3 = pidev->private;
451         int x, y, z;
452
453         mutex_lock(&lis3->mutex);
454         lis3lv02d_get_xyz(lis3, &x, &y, &z);
455         input_report_abs(pidev->input, ABS_X, x);
456         input_report_abs(pidev->input, ABS_Y, y);
457         input_report_abs(pidev->input, ABS_Z, z);
458         input_sync(pidev->input);
459         mutex_unlock(&lis3->mutex);
460 }
461
462 static void lis3lv02d_joystick_open(struct input_polled_dev *pidev)
463 {
464         struct lis3lv02d *lis3 = pidev->private;
465
466         if (lis3->pm_dev)
467                 pm_runtime_get_sync(lis3->pm_dev);
468
469         if (lis3->pdata && lis3->whoami == WAI_8B && lis3->idev)
470                 atomic_set(&lis3->wake_thread, 1);
471         /*
472          * Update coordinates for the case where poll interval is 0 and
473          * the chip in running purely under interrupt control
474          */
475         lis3lv02d_joystick_poll(pidev);
476 }
477
478 static void lis3lv02d_joystick_close(struct input_polled_dev *pidev)
479 {
480         struct lis3lv02d *lis3 = pidev->private;
481
482         atomic_set(&lis3->wake_thread, 0);
483         if (lis3->pm_dev)
484                 pm_runtime_put(lis3->pm_dev);
485 }
486
487 static irqreturn_t lis302dl_interrupt(int irq, void *data)
488 {
489         struct lis3lv02d *lis3 = data;
490
491         if (!test_bit(0, &lis3->misc_opened))
492                 goto out;
493
494         /*
495          * Be careful: on some HP laptops the bios force DD when on battery and
496          * the lid is closed. This leads to interrupts as soon as a little move
497          * is done.
498          */
499         atomic_inc(&lis3->count);
500
501         wake_up_interruptible(&lis3->misc_wait);
502         kill_fasync(&lis3->async_queue, SIGIO, POLL_IN);
503 out:
504         if (atomic_read(&lis3->wake_thread))
505                 return IRQ_WAKE_THREAD;
506         return IRQ_HANDLED;
507 }
508
509 static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
510 {
511         struct input_dev *dev = lis3->idev->input;
512         u8 click_src;
513
514         mutex_lock(&lis3->mutex);
515         lis3->read(lis3, CLICK_SRC, &click_src);
516
517         if (click_src & CLICK_SINGLE_X) {
518                 input_report_key(dev, lis3->mapped_btns[0], 1);
519                 input_report_key(dev, lis3->mapped_btns[0], 0);
520         }
521
522         if (click_src & CLICK_SINGLE_Y) {
523                 input_report_key(dev, lis3->mapped_btns[1], 1);
524                 input_report_key(dev, lis3->mapped_btns[1], 0);
525         }
526
527         if (click_src & CLICK_SINGLE_Z) {
528                 input_report_key(dev, lis3->mapped_btns[2], 1);
529                 input_report_key(dev, lis3->mapped_btns[2], 0);
530         }
531         input_sync(dev);
532         mutex_unlock(&lis3->mutex);
533 }
534
535 static inline void lis302dl_data_ready(struct lis3lv02d *lis3, int index)
536 {
537         int dummy;
538
539         /* Dummy read to ack interrupt */
540         lis3lv02d_get_xyz(lis3, &dummy, &dummy, &dummy);
541         lis3->data_ready_count[index]++;
542 }
543
544 static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
545 {
546         struct lis3lv02d *lis3 = data;
547         u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ1_MASK;
548
549         if (irq_cfg == LIS3_IRQ1_CLICK)
550                 lis302dl_interrupt_handle_click(lis3);
551         else if (unlikely(irq_cfg == LIS3_IRQ1_DATA_READY))
552                 lis302dl_data_ready(lis3, IRQ_LINE0);
553         else
554                 lis3lv02d_joystick_poll(lis3->idev);
555
556         return IRQ_HANDLED;
557 }
558
559 static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
560 {
561         struct lis3lv02d *lis3 = data;
562         u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ2_MASK;
563
564         if (irq_cfg == LIS3_IRQ2_CLICK)
565                 lis302dl_interrupt_handle_click(lis3);
566         else if (unlikely(irq_cfg == LIS3_IRQ2_DATA_READY))
567                 lis302dl_data_ready(lis3, IRQ_LINE1);
568         else
569                 lis3lv02d_joystick_poll(lis3->idev);
570
571         return IRQ_HANDLED;
572 }
573
574 static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
575 {
576         struct lis3lv02d *lis3 = container_of(file->private_data,
577                                               struct lis3lv02d, miscdev);
578
579         if (test_and_set_bit(0, &lis3->misc_opened))
580                 return -EBUSY; /* already open */
581
582         if (lis3->pm_dev)
583                 pm_runtime_get_sync(lis3->pm_dev);
584
585         atomic_set(&lis3->count, 0);
586         return 0;
587 }
588
589 static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
590 {
591         struct lis3lv02d *lis3 = container_of(file->private_data,
592                                               struct lis3lv02d, miscdev);
593
594         fasync_helper(-1, file, 0, &lis3->async_queue);
595         clear_bit(0, &lis3->misc_opened); /* release the device */
596         if (lis3->pm_dev)
597                 pm_runtime_put(lis3->pm_dev);
598         return 0;
599 }
600
601 static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
602                                 size_t count, loff_t *pos)
603 {
604         struct lis3lv02d *lis3 = container_of(file->private_data,
605                                               struct lis3lv02d, miscdev);
606
607         DECLARE_WAITQUEUE(wait, current);
608         u32 data;
609         unsigned char byte_data;
610         ssize_t retval = 1;
611
612         if (count < 1)
613                 return -EINVAL;
614
615         add_wait_queue(&lis3->misc_wait, &wait);
616         while (true) {
617                 set_current_state(TASK_INTERRUPTIBLE);
618                 data = atomic_xchg(&lis3->count, 0);
619                 if (data)
620                         break;
621
622                 if (file->f_flags & O_NONBLOCK) {
623                         retval = -EAGAIN;
624                         goto out;
625                 }
626
627                 if (signal_pending(current)) {
628                         retval = -ERESTARTSYS;
629                         goto out;
630                 }
631
632                 schedule();
633         }
634
635         if (data < 255)
636                 byte_data = data;
637         else
638                 byte_data = 255;
639
640         /* make sure we are not going into copy_to_user() with
641          * TASK_INTERRUPTIBLE state */
642         set_current_state(TASK_RUNNING);
643         if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
644                 retval = -EFAULT;
645
646 out:
647         __set_current_state(TASK_RUNNING);
648         remove_wait_queue(&lis3->misc_wait, &wait);
649
650         return retval;
651 }
652
653 static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
654 {
655         struct lis3lv02d *lis3 = container_of(file->private_data,
656                                               struct lis3lv02d, miscdev);
657
658         poll_wait(file, &lis3->misc_wait, wait);
659         if (atomic_read(&lis3->count))
660                 return POLLIN | POLLRDNORM;
661         return 0;
662 }
663
664 static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
665 {
666         struct lis3lv02d *lis3 = container_of(file->private_data,
667                                               struct lis3lv02d, miscdev);
668
669         return fasync_helper(fd, file, on, &lis3->async_queue);
670 }
671
672 static const struct file_operations lis3lv02d_misc_fops = {
673         .owner   = THIS_MODULE,
674         .llseek  = no_llseek,
675         .read    = lis3lv02d_misc_read,
676         .open    = lis3lv02d_misc_open,
677         .release = lis3lv02d_misc_release,
678         .poll    = lis3lv02d_misc_poll,
679         .fasync  = lis3lv02d_misc_fasync,
680 };
681
682 int lis3lv02d_joystick_enable(struct lis3lv02d *lis3)
683 {
684         struct input_dev *input_dev;
685         int err;
686         int max_val, fuzz, flat;
687         int btns[] = {BTN_X, BTN_Y, BTN_Z};
688
689         if (lis3->idev)
690                 return -EINVAL;
691
692         lis3->idev = input_allocate_polled_device();
693         if (!lis3->idev)
694                 return -ENOMEM;
695
696         lis3->idev->poll = lis3lv02d_joystick_poll;
697         lis3->idev->open = lis3lv02d_joystick_open;
698         lis3->idev->close = lis3lv02d_joystick_close;
699         lis3->idev->poll_interval = MDPS_POLL_INTERVAL;
700         lis3->idev->poll_interval_min = MDPS_POLL_MIN;
701         lis3->idev->poll_interval_max = MDPS_POLL_MAX;
702         lis3->idev->private = lis3;
703         input_dev = lis3->idev->input;
704
705         input_dev->name       = "ST LIS3LV02DL Accelerometer";
706         input_dev->phys       = DRIVER_NAME "/input0";
707         input_dev->id.bustype = BUS_HOST;
708         input_dev->id.vendor  = 0;
709         input_dev->dev.parent = &lis3->pdev->dev;
710
711         set_bit(EV_ABS, input_dev->evbit);
712         max_val = (lis3->mdps_max_val * lis3->scale) / LIS3_ACCURACY;
713         if (lis3->whoami == WAI_12B) {
714                 fuzz = LIS3_DEFAULT_FUZZ_12B;
715                 flat = LIS3_DEFAULT_FLAT_12B;
716         } else {
717                 fuzz = LIS3_DEFAULT_FUZZ_8B;
718                 flat = LIS3_DEFAULT_FLAT_8B;
719         }
720         fuzz = (fuzz * lis3->scale) / LIS3_ACCURACY;
721         flat = (flat * lis3->scale) / LIS3_ACCURACY;
722
723         input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
724         input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
725         input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
726
727         lis3->mapped_btns[0] = lis3lv02d_get_axis(abs(lis3->ac.x), btns);
728         lis3->mapped_btns[1] = lis3lv02d_get_axis(abs(lis3->ac.y), btns);
729         lis3->mapped_btns[2] = lis3lv02d_get_axis(abs(lis3->ac.z), btns);
730
731         err = input_register_polled_device(lis3->idev);
732         if (err) {
733                 input_free_polled_device(lis3->idev);
734                 lis3->idev = NULL;
735         }
736
737         return err;
738 }
739 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
740
741 void lis3lv02d_joystick_disable(struct lis3lv02d *lis3)
742 {
743         if (lis3->irq)
744                 free_irq(lis3->irq, lis3);
745         if (lis3->pdata && lis3->pdata->irq2)
746                 free_irq(lis3->pdata->irq2, lis3);
747
748         if (!lis3->idev)
749                 return;
750
751         if (lis3->irq)
752                 misc_deregister(&lis3->miscdev);
753         input_unregister_polled_device(lis3->idev);
754         input_free_polled_device(lis3->idev);
755         lis3->idev = NULL;
756 }
757 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
758
759 /* Sysfs stuff */
760 static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
761 {
762         /*
763          * SYSFS functions are fast visitors so put-call
764          * immediately after the get-call. However, keep
765          * chip running for a while and schedule delayed
766          * suspend. This way periodic sysfs calls doesn't
767          * suffer from relatively long power up time.
768          */
769
770         if (lis3->pm_dev) {
771                 pm_runtime_get_sync(lis3->pm_dev);
772                 pm_runtime_put_noidle(lis3->pm_dev);
773                 pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
774         }
775 }
776
777 static ssize_t lis3lv02d_selftest_show(struct device *dev,
778                                 struct device_attribute *attr, char *buf)
779 {
780         struct lis3lv02d *lis3 = dev_get_drvdata(dev);
781         s16 values[3];
782
783         static const char ok[] = "OK";
784         static const char fail[] = "FAIL";
785         static const char irq[] = "FAIL_IRQ";
786         const char *res;
787
788         lis3lv02d_sysfs_poweron(lis3);
789         switch (lis3lv02d_selftest(lis3, values)) {
790         case SELFTEST_FAIL:
791                 res = fail;
792                 break;
793         case SELFTEST_IRQ:
794                 res = irq;
795                 break;
796         case SELFTEST_OK:
797         default:
798                 res = ok;
799                 break;
800         }
801         return sprintf(buf, "%s %d %d %d\n", res,
802                 values[0], values[1], values[2]);
803 }
804
805 static ssize_t lis3lv02d_position_show(struct device *dev,
806                                 struct device_attribute *attr, char *buf)
807 {
808         struct lis3lv02d *lis3 = dev_get_drvdata(dev);
809         int x, y, z;
810
811         lis3lv02d_sysfs_poweron(lis3);
812         mutex_lock(&lis3->mutex);
813         lis3lv02d_get_xyz(lis3, &x, &y, &z);
814         mutex_unlock(&lis3->mutex);
815         return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
816 }
817
818 static ssize_t lis3lv02d_rate_show(struct device *dev,
819                         struct device_attribute *attr, char *buf)
820 {
821         struct lis3lv02d *lis3 = dev_get_drvdata(dev);
822
823         lis3lv02d_sysfs_poweron(lis3);
824         return sprintf(buf, "%d\n", lis3lv02d_get_odr(lis3));
825 }
826
827 static ssize_t lis3lv02d_rate_set(struct device *dev,
828                                 struct device_attribute *attr, const char *buf,
829                                 size_t count)
830 {
831         struct lis3lv02d *lis3 = dev_get_drvdata(dev);
832         unsigned long rate;
833
834         if (strict_strtoul(buf, 0, &rate))
835                 return -EINVAL;
836
837         lis3lv02d_sysfs_poweron(lis3);
838         if (lis3lv02d_set_odr(lis3, rate))
839                 return -EINVAL;
840
841         return count;
842 }
843
844 static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
845 static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
846 static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
847                                             lis3lv02d_rate_set);
848
849 static struct attribute *lis3lv02d_attributes[] = {
850         &dev_attr_selftest.attr,
851         &dev_attr_position.attr,
852         &dev_attr_rate.attr,
853         NULL
854 };
855
856 static struct attribute_group lis3lv02d_attribute_group = {
857         .attrs = lis3lv02d_attributes
858 };
859
860
861 static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
862 {
863         lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
864         if (IS_ERR(lis3->pdev))
865                 return PTR_ERR(lis3->pdev);
866
867         platform_set_drvdata(lis3->pdev, lis3);
868         return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
869 }
870
871 int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
872 {
873         sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
874         platform_device_unregister(lis3->pdev);
875         if (lis3->pm_dev) {
876                 /* Barrier after the sysfs remove */
877                 pm_runtime_barrier(lis3->pm_dev);
878
879                 /* SYSFS may have left chip running. Turn off if necessary */
880                 if (!pm_runtime_suspended(lis3->pm_dev))
881                         lis3lv02d_poweroff(lis3);
882
883                 pm_runtime_disable(lis3->pm_dev);
884                 pm_runtime_set_suspended(lis3->pm_dev);
885         }
886         kfree(lis3->reg_cache);
887         return 0;
888 }
889 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
890
891 static void lis3lv02d_8b_configure(struct lis3lv02d *lis3,
892                                 struct lis3lv02d_platform_data *p)
893 {
894         int err;
895         int ctrl2 = p->hipass_ctrl;
896
897         if (p->click_flags) {
898                 lis3->write(lis3, CLICK_CFG, p->click_flags);
899                 lis3->write(lis3, CLICK_TIMELIMIT, p->click_time_limit);
900                 lis3->write(lis3, CLICK_LATENCY, p->click_latency);
901                 lis3->write(lis3, CLICK_WINDOW, p->click_window);
902                 lis3->write(lis3, CLICK_THSZ, p->click_thresh_z & 0xf);
903                 lis3->write(lis3, CLICK_THSY_X,
904                         (p->click_thresh_x & 0xf) |
905                         (p->click_thresh_y << 4));
906
907                 if (lis3->idev) {
908                         struct input_dev *input_dev = lis3->idev->input;
909                         input_set_capability(input_dev, EV_KEY, BTN_X);
910                         input_set_capability(input_dev, EV_KEY, BTN_Y);
911                         input_set_capability(input_dev, EV_KEY, BTN_Z);
912                 }
913         }
914
915         if (p->wakeup_flags) {
916                 lis3->write(lis3, FF_WU_CFG_1, p->wakeup_flags);
917                 lis3->write(lis3, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
918                 /* pdata value + 1 to keep this backward compatible*/
919                 lis3->write(lis3, FF_WU_DURATION_1, p->duration1 + 1);
920                 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
921         }
922
923         if (p->wakeup_flags2) {
924                 lis3->write(lis3, FF_WU_CFG_2, p->wakeup_flags2);
925                 lis3->write(lis3, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
926                 /* pdata value + 1 to keep this backward compatible*/
927                 lis3->write(lis3, FF_WU_DURATION_2, p->duration2 + 1);
928                 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
929         }
930         /* Configure hipass filters */
931         lis3->write(lis3, CTRL_REG2, ctrl2);
932
933         if (p->irq2) {
934                 err = request_threaded_irq(p->irq2,
935                                         NULL,
936                                         lis302dl_interrupt_thread2_8b,
937                                         IRQF_TRIGGER_RISING | IRQF_ONESHOT |
938                                         (p->irq_flags2 & IRQF_TRIGGER_MASK),
939                                         DRIVER_NAME, lis3);
940                 if (err < 0)
941                         pr_err("No second IRQ. Limited functionality\n");
942         }
943 }
944
945 /*
946  * Initialise the accelerometer and the various subsystems.
947  * Should be rather independent of the bus system.
948  */
949 int lis3lv02d_init_device(struct lis3lv02d *lis3)
950 {
951         int err;
952         irq_handler_t thread_fn;
953         int irq_flags = 0;
954
955         lis3->whoami = lis3lv02d_read_8(lis3, WHO_AM_I);
956
957         switch (lis3->whoami) {
958         case WAI_12B:
959                 pr_info("12 bits sensor found\n");
960                 lis3->read_data = lis3lv02d_read_12;
961                 lis3->mdps_max_val = 2048;
962                 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
963                 lis3->odrs = lis3_12_rates;
964                 lis3->odr_mask = CTRL1_DF0 | CTRL1_DF1;
965                 lis3->scale = LIS3_SENSITIVITY_12B;
966                 lis3->regs = lis3_wai12_regs;
967                 lis3->regs_size = ARRAY_SIZE(lis3_wai12_regs);
968                 break;
969         case WAI_8B:
970                 pr_info("8 bits sensor found\n");
971                 lis3->read_data = lis3lv02d_read_8;
972                 lis3->mdps_max_val = 128;
973                 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
974                 lis3->odrs = lis3_8_rates;
975                 lis3->odr_mask = CTRL1_DR;
976                 lis3->scale = LIS3_SENSITIVITY_8B;
977                 lis3->regs = lis3_wai8_regs;
978                 lis3->regs_size = ARRAY_SIZE(lis3_wai8_regs);
979                 break;
980         case WAI_3DC:
981                 pr_info("8 bits 3DC sensor found\n");
982                 lis3->read_data = lis3lv02d_read_8;
983                 lis3->mdps_max_val = 128;
984                 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
985                 lis3->odrs = lis3_3dc_rates;
986                 lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
987                 lis3->scale = LIS3_SENSITIVITY_8B;
988                 break;
989         case WAI_3DLH:
990                 pr_info("16 bits 3DLH sensor found\n");
991                 lis3->read_data = lis3lv02d_read_16;
992                 lis3->mdps_max_val = 2048; /* 12 bits for 2G */
993                 lis3->shift_adj = SHIFT_ADJ_2G;
994                 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
995                 lis3->odrs = lis3_3dlh_rates;
996                 lis3->odr_mask = CTRL1_DR0 | CTRL1_DR1;
997                 lis3->scale = LIS3DLH_SENSITIVITY_2G;
998                 break;
999         default:
1000                 pr_err("unknown sensor type 0x%X\n", lis3->whoami);
1001                 return -EINVAL;
1002         }
1003
1004         lis3->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
1005                                      sizeof(lis3_wai12_regs)), GFP_KERNEL);
1006
1007         if (lis3->reg_cache == NULL) {
1008                 printk(KERN_ERR DRIVER_NAME "out of memory\n");
1009                 return -ENOMEM;
1010         }
1011
1012         mutex_init(&lis3->mutex);
1013         atomic_set(&lis3->wake_thread, 0);
1014
1015         lis3lv02d_add_fs(lis3);
1016         err = lis3lv02d_poweron(lis3);
1017         if (err) {
1018                 lis3lv02d_remove_fs(lis3);
1019                 return err;
1020         }
1021
1022         if (lis3->pm_dev) {
1023                 pm_runtime_set_active(lis3->pm_dev);
1024                 pm_runtime_enable(lis3->pm_dev);
1025         }
1026
1027         if (lis3lv02d_joystick_enable(lis3))
1028                 pr_err("joystick initialization failed\n");
1029
1030         /* passing in platform specific data is purely optional and only
1031          * used by the SPI transport layer at the moment */
1032         if (lis3->pdata) {
1033                 struct lis3lv02d_platform_data *p = lis3->pdata;
1034
1035                 if (lis3->whoami == WAI_8B)
1036                         lis3lv02d_8b_configure(lis3, p);
1037
1038                 irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK;
1039
1040                 lis3->irq_cfg = p->irq_cfg;
1041                 if (p->irq_cfg)
1042                         lis3->write(lis3, CTRL_REG3, p->irq_cfg);
1043
1044                 if (p->default_rate)
1045                         lis3lv02d_set_odr(lis3, p->default_rate);
1046         }
1047
1048         /* bail if we did not get an IRQ from the bus layer */
1049         if (!lis3->irq) {
1050                 pr_debug("No IRQ. Disabling /dev/freefall\n");
1051                 goto out;
1052         }
1053
1054         /*
1055          * The sensor can generate interrupts for free-fall and direction
1056          * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
1057          * the things simple and _fast_ we activate it only for free-fall, so
1058          * no need to read register (very slow with ACPI). For the same reason,
1059          * we forbid shared interrupts.
1060          *
1061          * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
1062          * io-apic is not configurable (and generates a warning) but I keep it
1063          * in case of support for other hardware.
1064          */
1065         if (lis3->pdata && lis3->whoami == WAI_8B)
1066                 thread_fn = lis302dl_interrupt_thread1_8b;
1067         else
1068                 thread_fn = NULL;
1069
1070         err = request_threaded_irq(lis3->irq, lis302dl_interrupt,
1071                                 thread_fn,
1072                                 IRQF_TRIGGER_RISING | IRQF_ONESHOT |
1073                                 irq_flags,
1074                                 DRIVER_NAME, lis3);
1075
1076         if (err < 0) {
1077                 pr_err("Cannot get IRQ\n");
1078                 goto out;
1079         }
1080
1081         lis3->miscdev.minor     = MISC_DYNAMIC_MINOR;
1082         lis3->miscdev.name      = "freefall";
1083         lis3->miscdev.fops      = &lis3lv02d_misc_fops;
1084
1085         if (misc_register(&lis3->miscdev))
1086                 pr_err("misc_register failed\n");
1087 out:
1088         return 0;
1089 }
1090 EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
1091
1092 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
1093 MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
1094 MODULE_LICENSE("GPL");