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