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