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