blob: 25f3850109538b079fadec2fe22eb1220ff35f10 [file] [log] [blame]
Pavel Machek455fbdd2008-11-12 13:27:02 -08001/*
2 * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
3 *
4 * Copyright (C) 2007-2008 Yan Burman
5 * Copyright (C) 2008 Eric Piel
Pavel Machekef2cfc72009-02-18 14:48:23 -08006 * Copyright (C) 2008-2009 Pavel Machek
Pavel Machek455fbdd2008-11-12 13:27:02 -08007 *
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>
Eric Pieldc6ea972009-06-16 15:34:15 -070030#include <linux/input-polldev.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080031#include <linux/delay.h>
32#include <linux/wait.h>
33#include <linux/poll.h>
34#include <linux/freezer.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080035#include <linux/uaccess.h>
Pavel Machekef2cfc72009-02-18 14:48:23 -080036#include <linux/miscdevice.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080037#include <asm/atomic.h>
38#include "lis3lv02d.h"
39
40#define DRIVER_NAME "lis3lv02d"
Pavel Machek455fbdd2008-11-12 13:27:02 -080041
42/* joystick device poll interval in milliseconds */
43#define MDPS_POLL_INTERVAL 50
Samu Onkalo4a70a412010-05-24 14:33:37 -070044#define MDPS_POLL_MIN 0
45#define MDPS_POLL_MAX 2000
Pavel Machek455fbdd2008-11-12 13:27:02 -080046/*
47 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
Éric Pielbc62c142009-12-14 18:01:39 -080048 * because they are generated even if the data do not change. So it's better
Pavel Machek455fbdd2008-11-12 13:27:02 -080049 * to keep the interrupt for the free-fall event. The values are updated at
50 * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
51 * some low processor, we poll the sensor only at 20Hz... enough for the
52 * joystick.
53 */
54
Samu Onkalo641615a2009-12-14 18:01:41 -080055#define LIS3_PWRON_DELAY_WAI_12B (5000)
56#define LIS3_PWRON_DELAY_WAI_8B (3000)
57
Samu Onkalo32496c72009-12-14 18:01:46 -080058/*
59 * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
60 * LIS302D spec says: 18 mG / digit
61 * LIS3_ACCURACY is used to increase accuracy of the intermediate
62 * calculation results.
63 */
64#define LIS3_ACCURACY 1024
65/* Sensitivity values for -2G +2G scale */
66#define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024)
67#define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY)
68
69#define LIS3_DEFAULT_FUZZ 3
70#define LIS3_DEFAULT_FLAT 3
71
Daniel Macka38da2e2009-03-31 15:24:32 -070072struct lis3lv02d lis3_dev = {
Pavel Machekbe84cfc2009-03-31 15:24:26 -070073 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
Pavel Machekef2cfc72009-02-18 14:48:23 -080074};
75
Pavel Machekbe84cfc2009-03-31 15:24:26 -070076EXPORT_SYMBOL_GPL(lis3_dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080077
Takashi Iwai2ee32142010-10-01 17:14:25 -040078/* just like param_set_int() but does sanity-check so that it won't point
79 * over the axis array size
80 */
81static int param_set_axis(const char *val, const struct kernel_param *kp)
82{
83 int ret = param_set_int(val, kp);
84 if (!ret) {
85 int val = *(int *)kp->arg;
86 if (val < 0)
87 val = -val;
88 if (!val || val > 3)
89 return -EINVAL;
90 }
91 return ret;
92}
93
94static struct kernel_param_ops param_ops_axis = {
95 .set = param_set_axis,
96 .get = param_get_int,
97};
98
99module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
100MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
101
Daniel Macka38da2e2009-03-31 15:24:32 -0700102static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
103{
104 s8 lo;
105 if (lis3->read(lis3, reg, &lo) < 0)
106 return 0;
107
108 return lo;
109}
110
Éric Pielbc62c142009-12-14 18:01:39 -0800111static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700112{
113 u8 lo, hi;
114
Daniel Macka38da2e2009-03-31 15:24:32 -0700115 lis3->read(lis3, reg - 1, &lo);
116 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700117 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
118 return (s16)((hi << 8) | lo);
119}
120
Pavel Machek455fbdd2008-11-12 13:27:02 -0800121/**
122 * lis3lv02d_get_axis - For the given axis, give the value converted
123 * @axis: 1,2,3 - can also be negative
124 * @hw_values: raw values returned by the hardware
125 *
126 * Returns the converted value.
127 */
128static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
129{
130 if (axis > 0)
131 return hw_values[axis - 1];
132 else
133 return -hw_values[-axis - 1];
134}
135
136/**
137 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -0700138 * @lis3: pointer to the device struct
139 * @x: where to store the X axis value
140 * @y: where to store the Y axis value
141 * @z: where to store the Z axis value
Pavel Machek455fbdd2008-11-12 13:27:02 -0800142 *
143 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
144 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700145static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800146{
147 int position[3];
Samu Onkalo32496c72009-12-14 18:01:46 -0800148 int i;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800149
Eric Piela002ee82009-06-16 15:34:14 -0700150 position[0] = lis3->read_data(lis3, OUTX);
151 position[1] = lis3->read_data(lis3, OUTY);
152 position[2] = lis3->read_data(lis3, OUTZ);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800153
Samu Onkalo32496c72009-12-14 18:01:46 -0800154 for (i = 0; i < 3; i++)
155 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
156
Eric Piela002ee82009-06-16 15:34:14 -0700157 *x = lis3lv02d_get_axis(lis3->ac.x, position);
158 *y = lis3lv02d_get_axis(lis3->ac.y, position);
159 *z = lis3lv02d_get_axis(lis3->ac.z, position);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800160}
161
Samu Onkalo641615a2009-12-14 18:01:41 -0800162/* conversion btw sampling rate and the register values */
163static int lis3_12_rates[4] = {40, 160, 640, 2560};
164static int lis3_8_rates[2] = {100, 400};
Takashi Iwai78537c32010-09-23 10:01:39 -0700165static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
Samu Onkalo641615a2009-12-14 18:01:41 -0800166
Samu Onkaloa253aae2009-12-14 18:01:44 -0800167/* ODR is Output Data Rate */
Samu Onkalo641615a2009-12-14 18:01:41 -0800168static int lis3lv02d_get_odr(void)
169{
170 u8 ctrl;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800171 int shift;
Samu Onkalo641615a2009-12-14 18:01:41 -0800172
173 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800174 ctrl &= lis3_dev.odr_mask;
175 shift = ffs(lis3_dev.odr_mask) - 1;
176 return lis3_dev.odrs[(ctrl >> shift)];
177}
Samu Onkalo641615a2009-12-14 18:01:41 -0800178
Samu Onkaloa253aae2009-12-14 18:01:44 -0800179static int lis3lv02d_set_odr(int rate)
180{
181 u8 ctrl;
182 int i, len, shift;
183
Takashi Iwai78537c32010-09-23 10:01:39 -0700184 if (!rate)
185 return -EINVAL;
186
Samu Onkaloa253aae2009-12-14 18:01:44 -0800187 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
188 ctrl &= ~lis3_dev.odr_mask;
189 len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
190 shift = ffs(lis3_dev.odr_mask) - 1;
191
192 for (i = 0; i < len; i++)
193 if (lis3_dev.odrs[i] == rate) {
194 lis3_dev.write(&lis3_dev, CTRL_REG1,
195 ctrl | (i << shift));
196 return 0;
197 }
198 return -EINVAL;
Samu Onkalo641615a2009-12-14 18:01:41 -0800199}
200
Samu Onkalo2db4a762009-12-14 18:01:43 -0800201static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
202{
Takashi Iwai78537c32010-09-23 10:01:39 -0700203 u8 ctlreg, reg;
Samu Onkalo2db4a762009-12-14 18:01:43 -0800204 s16 x, y, z;
205 u8 selftest;
206 int ret;
207
208 mutex_lock(&lis3->mutex);
Takashi Iwai78537c32010-09-23 10:01:39 -0700209 if (lis3_dev.whoami == WAI_3DC) {
210 ctlreg = CTRL_REG4;
211 selftest = CTRL4_ST0;
212 } else {
213 ctlreg = CTRL_REG1;
214 if (lis3_dev.whoami == WAI_12B)
215 selftest = CTRL1_ST;
216 else
217 selftest = CTRL1_STP;
218 }
Samu Onkalo2db4a762009-12-14 18:01:43 -0800219
Takashi Iwai78537c32010-09-23 10:01:39 -0700220 lis3->read(lis3, ctlreg, &reg);
221 lis3->write(lis3, ctlreg, (reg | selftest));
Samu Onkalo2db4a762009-12-14 18:01:43 -0800222 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
223
224 /* Read directly to avoid axis remap */
225 x = lis3->read_data(lis3, OUTX);
226 y = lis3->read_data(lis3, OUTY);
227 z = lis3->read_data(lis3, OUTZ);
228
229 /* back to normal settings */
Takashi Iwai78537c32010-09-23 10:01:39 -0700230 lis3->write(lis3, ctlreg, reg);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800231 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
232
233 results[0] = x - lis3->read_data(lis3, OUTX);
234 results[1] = y - lis3->read_data(lis3, OUTY);
235 results[2] = z - lis3->read_data(lis3, OUTZ);
236
237 ret = 0;
238 if (lis3->pdata) {
239 int i;
240 for (i = 0; i < 3; i++) {
241 /* Check against selftest acceptance limits */
242 if ((results[i] < lis3->pdata->st_min_limits[i]) ||
243 (results[i] > lis3->pdata->st_max_limits[i])) {
244 ret = -EIO;
245 goto fail;
246 }
247 }
248 }
249
250 /* test passed */
251fail:
252 mutex_unlock(&lis3->mutex);
253 return ret;
254}
255
Daniel Macka38da2e2009-03-31 15:24:32 -0700256void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800257{
Eric Piela002ee82009-06-16 15:34:14 -0700258 /* disable X,Y,Z axis and power down */
259 lis3->write(lis3, CTRL_REG1, 0x00);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800260}
Eric Pielcfce41a2009-01-09 16:41:01 -0800261EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800262
Daniel Macka38da2e2009-03-31 15:24:32 -0700263void lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800264{
Eric Piela002ee82009-06-16 15:34:14 -0700265 u8 reg;
266
267 lis3->init(lis3);
268
Samu Onkalo641615a2009-12-14 18:01:41 -0800269 /* LIS3 power on delay is quite long */
270 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
271
Eric Piela002ee82009-06-16 15:34:14 -0700272 /*
273 * Common configuration
Éric Piel4b5d95b2009-12-14 18:01:40 -0800274 * BDU: (12 bits sensors only) LSB and MSB values are not updated until
275 * both have been read. So the value read will always be correct.
Eric Piela002ee82009-06-16 15:34:14 -0700276 */
Éric Piel4b5d95b2009-12-14 18:01:40 -0800277 if (lis3->whoami == WAI_12B) {
278 lis3->read(lis3, CTRL_REG2, &reg);
279 reg |= CTRL2_BDU;
280 lis3->write(lis3, CTRL_REG2, reg);
281 }
Pavel Machek455fbdd2008-11-12 13:27:02 -0800282}
Eric Pielcfce41a2009-01-09 16:41:01 -0800283EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800284
Pavel Machek455fbdd2008-11-12 13:27:02 -0800285
Samu Onkalo6d94d402010-05-24 14:33:37 -0700286static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
287{
288 int x, y, z;
289
290 mutex_lock(&lis3_dev.mutex);
291 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
292 input_report_abs(pidev->input, ABS_X, x);
293 input_report_abs(pidev->input, ABS_Y, y);
294 input_report_abs(pidev->input, ABS_Z, z);
295 input_sync(pidev->input);
296 mutex_unlock(&lis3_dev.mutex);
297}
298
Pavel Machekef2cfc72009-02-18 14:48:23 -0800299static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
300{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700301 if (!test_bit(0, &lis3_dev.misc_opened))
302 goto out;
303
Pavel Machekef2cfc72009-02-18 14:48:23 -0800304 /*
305 * Be careful: on some HP laptops the bios force DD when on battery and
306 * the lid is closed. This leads to interrupts as soon as a little move
307 * is done.
308 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700309 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800310
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700311 wake_up_interruptible(&lis3_dev.misc_wait);
312 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700313out:
Takashi Iwaif7c77a32010-09-23 10:01:11 -0700314 if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev &&
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700315 lis3_dev.idev->input->users)
316 return IRQ_WAKE_THREAD;
317 return IRQ_HANDLED;
318}
319
Samu Onkalo6d94d402010-05-24 14:33:37 -0700320static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
321{
322 struct input_dev *dev = lis3->idev->input;
323 u8 click_src;
324
325 mutex_lock(&lis3->mutex);
326 lis3->read(lis3, CLICK_SRC, &click_src);
327
328 if (click_src & CLICK_SINGLE_X) {
329 input_report_key(dev, lis3->mapped_btns[0], 1);
330 input_report_key(dev, lis3->mapped_btns[0], 0);
331 }
332
333 if (click_src & CLICK_SINGLE_Y) {
334 input_report_key(dev, lis3->mapped_btns[1], 1);
335 input_report_key(dev, lis3->mapped_btns[1], 0);
336 }
337
338 if (click_src & CLICK_SINGLE_Z) {
339 input_report_key(dev, lis3->mapped_btns[2], 1);
340 input_report_key(dev, lis3->mapped_btns[2], 0);
341 }
342 input_sync(dev);
343 mutex_unlock(&lis3->mutex);
344}
345
346static void lis302dl_interrupt_handle_ff_wu(struct lis3lv02d *lis3)
347{
348 u8 wu1_src;
349 u8 wu2_src;
350
351 lis3->read(lis3, FF_WU_SRC_1, &wu1_src);
352 lis3->read(lis3, FF_WU_SRC_2, &wu2_src);
353
354 wu1_src = wu1_src & FF_WU_SRC_IA ? wu1_src : 0;
355 wu2_src = wu2_src & FF_WU_SRC_IA ? wu2_src : 0;
356
357 /* joystick poll is internally protected by the lis3->mutex. */
358 if (wu1_src || wu2_src)
359 lis3lv02d_joystick_poll(lis3_dev.idev);
360}
361
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700362static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
363{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700364
365 struct lis3lv02d *lis3 = data;
366
367 if ((lis3->pdata->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
368 lis302dl_interrupt_handle_click(lis3);
369 else
370 lis302dl_interrupt_handle_ff_wu(lis3);
371
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700372 return IRQ_HANDLED;
373}
374
375static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
376{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700377
378 struct lis3lv02d *lis3 = data;
379
380 if ((lis3->pdata->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
381 lis302dl_interrupt_handle_click(lis3);
382 else
383 lis302dl_interrupt_handle_ff_wu(lis3);
384
Pavel Machekef2cfc72009-02-18 14:48:23 -0800385 return IRQ_HANDLED;
386}
387
388static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
389{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700390 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800391 return -EBUSY; /* already open */
392
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700393 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800394 return 0;
395}
396
397static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
398{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700399 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700400 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Pavel Machekef2cfc72009-02-18 14:48:23 -0800401 return 0;
402}
403
404static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
405 size_t count, loff_t *pos)
406{
407 DECLARE_WAITQUEUE(wait, current);
408 u32 data;
409 unsigned char byte_data;
410 ssize_t retval = 1;
411
412 if (count < 1)
413 return -EINVAL;
414
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700415 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800416 while (true) {
417 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700418 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800419 if (data)
420 break;
421
422 if (file->f_flags & O_NONBLOCK) {
423 retval = -EAGAIN;
424 goto out;
425 }
426
427 if (signal_pending(current)) {
428 retval = -ERESTARTSYS;
429 goto out;
430 }
431
432 schedule();
433 }
434
435 if (data < 255)
436 byte_data = data;
437 else
438 byte_data = 255;
439
440 /* make sure we are not going into copy_to_user() with
441 * TASK_INTERRUPTIBLE state */
442 set_current_state(TASK_RUNNING);
443 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
444 retval = -EFAULT;
445
446out:
447 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700448 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800449
450 return retval;
451}
452
453static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
454{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700455 poll_wait(file, &lis3_dev.misc_wait, wait);
456 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800457 return POLLIN | POLLRDNORM;
458 return 0;
459}
460
461static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
462{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700463 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800464}
465
466static const struct file_operations lis3lv02d_misc_fops = {
467 .owner = THIS_MODULE,
468 .llseek = no_llseek,
469 .read = lis3lv02d_misc_read,
470 .open = lis3lv02d_misc_open,
471 .release = lis3lv02d_misc_release,
472 .poll = lis3lv02d_misc_poll,
473 .fasync = lis3lv02d_misc_fasync,
474};
475
476static struct miscdevice lis3lv02d_misc_device = {
477 .minor = MISC_DYNAMIC_MINOR,
478 .name = "freefall",
479 .fops = &lis3lv02d_misc_fops,
480};
481
Eric Pielcfce41a2009-01-09 16:41:01 -0800482int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800483{
Eric Pieldc6ea972009-06-16 15:34:15 -0700484 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800485 int err;
Samu Onkalo32496c72009-12-14 18:01:46 -0800486 int max_val, fuzz, flat;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700487 int btns[] = {BTN_X, BTN_Y, BTN_Z};
Pavel Machek455fbdd2008-11-12 13:27:02 -0800488
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700489 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800490 return -EINVAL;
491
Eric Pieldc6ea972009-06-16 15:34:15 -0700492 lis3_dev.idev = input_allocate_polled_device();
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700493 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800494 return -ENOMEM;
495
Eric Pieldc6ea972009-06-16 15:34:15 -0700496 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
497 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
Samu Onkalo4a70a412010-05-24 14:33:37 -0700498 lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN;
499 lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX;
Eric Pieldc6ea972009-06-16 15:34:15 -0700500 input_dev = lis3_dev.idev->input;
501
Eric Pieldc6ea972009-06-16 15:34:15 -0700502 input_dev->name = "ST LIS3LV02DL Accelerometer";
503 input_dev->phys = DRIVER_NAME "/input0";
504 input_dev->id.bustype = BUS_HOST;
505 input_dev->id.vendor = 0;
506 input_dev->dev.parent = &lis3_dev.pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800507
Eric Pieldc6ea972009-06-16 15:34:15 -0700508 set_bit(EV_ABS, input_dev->evbit);
Samu Onkalo32496c72009-12-14 18:01:46 -0800509 max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
510 fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
511 flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
512 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
513 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
514 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800515
Samu Onkalo6d94d402010-05-24 14:33:37 -0700516 lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
517 lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
518 lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
519
Eric Pieldc6ea972009-06-16 15:34:15 -0700520 err = input_register_polled_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800521 if (err) {
Eric Pieldc6ea972009-06-16 15:34:15 -0700522 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700523 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800524 }
525
526 return err;
527}
Eric Pielcfce41a2009-01-09 16:41:01 -0800528EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800529
Eric Pielcfce41a2009-01-09 16:41:01 -0800530void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800531{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700532 if (lis3_dev.irq)
533 free_irq(lis3_dev.irq, &lis3_dev);
534 if (lis3_dev.pdata && lis3_dev.pdata->irq2)
535 free_irq(lis3_dev.pdata->irq2, &lis3_dev);
536
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700537 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800538 return;
539
Eric Pielc2884242009-06-16 15:34:13 -0700540 if (lis3_dev.irq)
541 misc_deregister(&lis3lv02d_misc_device);
Eric Pieldc6ea972009-06-16 15:34:15 -0700542 input_unregister_polled_device(lis3_dev.idev);
Samu Onkalo66c85692009-12-14 18:01:39 -0800543 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700544 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800545}
Eric Pielcfce41a2009-01-09 16:41:01 -0800546EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800547
Pavel Machek455fbdd2008-11-12 13:27:02 -0800548/* Sysfs stuff */
Samu Onkalo2db4a762009-12-14 18:01:43 -0800549static ssize_t lis3lv02d_selftest_show(struct device *dev,
550 struct device_attribute *attr, char *buf)
551{
552 int result;
553 s16 values[3];
554
555 result = lis3lv02d_selftest(&lis3_dev, values);
556 return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
557 values[0], values[1], values[2]);
558}
559
Pavel Machek455fbdd2008-11-12 13:27:02 -0800560static ssize_t lis3lv02d_position_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 int x, y, z;
564
Samu Onkalo6d94d402010-05-24 14:33:37 -0700565 mutex_lock(&lis3_dev.mutex);
Daniel Macka38da2e2009-03-31 15:24:32 -0700566 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700567 mutex_unlock(&lis3_dev.mutex);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800568 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
569}
570
Pavel Machek455fbdd2008-11-12 13:27:02 -0800571static ssize_t lis3lv02d_rate_show(struct device *dev,
572 struct device_attribute *attr, char *buf)
573{
Samu Onkalo641615a2009-12-14 18:01:41 -0800574 return sprintf(buf, "%d\n", lis3lv02d_get_odr());
Pavel Machek455fbdd2008-11-12 13:27:02 -0800575}
576
Samu Onkaloa253aae2009-12-14 18:01:44 -0800577static ssize_t lis3lv02d_rate_set(struct device *dev,
578 struct device_attribute *attr, const char *buf,
579 size_t count)
580{
581 unsigned long rate;
582
583 if (strict_strtoul(buf, 0, &rate))
584 return -EINVAL;
585
586 if (lis3lv02d_set_odr(rate))
587 return -EINVAL;
588
589 return count;
590}
591
Samu Onkalo2db4a762009-12-14 18:01:43 -0800592static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800593static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800594static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
595 lis3lv02d_rate_set);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800596
597static struct attribute *lis3lv02d_attributes[] = {
Samu Onkalo2db4a762009-12-14 18:01:43 -0800598 &dev_attr_selftest.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800599 &dev_attr_position.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800600 &dev_attr_rate.attr,
601 NULL
602};
603
604static struct attribute_group lis3lv02d_attribute_group = {
605 .attrs = lis3lv02d_attributes
606};
607
Eric Pielcfce41a2009-01-09 16:41:01 -0800608
Daniel Macka38da2e2009-03-31 15:24:32 -0700609static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800610{
Eric Piela002ee82009-06-16 15:34:14 -0700611 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
612 if (IS_ERR(lis3->pdev))
613 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800614
Eric Piela002ee82009-06-16 15:34:14 -0700615 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800616}
617
Eric Piela002ee82009-06-16 15:34:14 -0700618int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800619{
Eric Piela002ee82009-06-16 15:34:14 -0700620 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
621 platform_device_unregister(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800622 return 0;
623}
Eric Pielcfce41a2009-01-09 16:41:01 -0800624EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800625
Samu Onkaloecc437a2010-05-24 14:33:34 -0700626static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
627 struct lis3lv02d_platform_data *p)
628{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700629 int err;
Samu Onkalo342c5f12010-05-24 14:33:35 -0700630 int ctrl2 = p->hipass_ctrl;
631
Samu Onkaloecc437a2010-05-24 14:33:34 -0700632 if (p->click_flags) {
633 dev->write(dev, CLICK_CFG, p->click_flags);
634 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
635 dev->write(dev, CLICK_LATENCY, p->click_latency);
636 dev->write(dev, CLICK_WINDOW, p->click_window);
637 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
638 dev->write(dev, CLICK_THSY_X,
639 (p->click_thresh_x & 0xf) |
640 (p->click_thresh_y << 4));
Samu Onkalo6d94d402010-05-24 14:33:37 -0700641
642 if (dev->idev) {
643 struct input_dev *input_dev = lis3_dev.idev->input;
644 input_set_capability(input_dev, EV_KEY, BTN_X);
645 input_set_capability(input_dev, EV_KEY, BTN_Y);
646 input_set_capability(input_dev, EV_KEY, BTN_Z);
647 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700648 }
649
650 if (p->wakeup_flags) {
651 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
652 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
653 /* default to 2.5ms for now */
654 dev->write(dev, FF_WU_DURATION_1, 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700655 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
Samu Onkaloecc437a2010-05-24 14:33:34 -0700656 }
Samu Onkalo342c5f12010-05-24 14:33:35 -0700657
658 if (p->wakeup_flags2) {
659 dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
660 dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
661 /* default to 2.5ms for now */
662 dev->write(dev, FF_WU_DURATION_2, 1);
663 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
664 }
665 /* Configure hipass filters */
666 dev->write(dev, CTRL_REG2, ctrl2);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700667
668 if (p->irq2) {
669 err = request_threaded_irq(p->irq2,
670 NULL,
671 lis302dl_interrupt_thread2_8b,
672 IRQF_TRIGGER_RISING |
673 IRQF_ONESHOT,
674 DRIVER_NAME, &lis3_dev);
675 if (err < 0)
676 printk(KERN_ERR DRIVER_NAME
677 "No second IRQ. Limited functionality\n");
678 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700679}
680
Daniel Mackab337a62009-03-31 15:24:31 -0700681/*
682 * Initialise the accelerometer and the various subsystems.
Éric Pielbc62c142009-12-14 18:01:39 -0800683 * Should be rather independent of the bus system.
Daniel Mackab337a62009-03-31 15:24:31 -0700684 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700685int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700686{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700687 int err;
688 irq_handler_t thread_fn;
689
Daniel Macka38da2e2009-03-31 15:24:32 -0700690 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
691
692 switch (dev->whoami) {
Éric Pielbc62c142009-12-14 18:01:39 -0800693 case WAI_12B:
694 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
695 dev->read_data = lis3lv02d_read_12;
Daniel Macka38da2e2009-03-31 15:24:32 -0700696 dev->mdps_max_val = 2048;
Samu Onkalo641615a2009-12-14 18:01:41 -0800697 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800698 dev->odrs = lis3_12_rates;
699 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
Samu Onkalo32496c72009-12-14 18:01:46 -0800700 dev->scale = LIS3_SENSITIVITY_12B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700701 break;
Éric Pielbc62c142009-12-14 18:01:39 -0800702 case WAI_8B:
703 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
Daniel Macka38da2e2009-03-31 15:24:32 -0700704 dev->read_data = lis3lv02d_read_8;
705 dev->mdps_max_val = 128;
Samu Onkalo641615a2009-12-14 18:01:41 -0800706 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800707 dev->odrs = lis3_8_rates;
708 dev->odr_mask = CTRL1_DR;
Samu Onkalo32496c72009-12-14 18:01:46 -0800709 dev->scale = LIS3_SENSITIVITY_8B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700710 break;
Takashi Iwai78537c32010-09-23 10:01:39 -0700711 case WAI_3DC:
712 printk(KERN_INFO DRIVER_NAME ": 8 bits 3DC sensor found\n");
713 dev->read_data = lis3lv02d_read_8;
714 dev->mdps_max_val = 128;
715 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
716 dev->odrs = lis3_3dc_rates;
717 dev->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
718 dev->scale = LIS3_SENSITIVITY_8B;
719 break;
Daniel Macka38da2e2009-03-31 15:24:32 -0700720 default:
721 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700722 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700723 return -EINVAL;
724 }
725
Samu Onkalo2db4a762009-12-14 18:01:43 -0800726 mutex_init(&dev->mutex);
727
Daniel Macka38da2e2009-03-31 15:24:32 -0700728 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700729 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700730
731 if (lis3lv02d_joystick_enable())
732 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
733
Daniel Mack8f3128e2009-06-16 15:34:17 -0700734 /* passing in platform specific data is purely optional and only
735 * used by the SPI transport layer at the moment */
736 if (dev->pdata) {
737 struct lis3lv02d_platform_data *p = dev->pdata;
738
Samu Onkaloecc437a2010-05-24 14:33:34 -0700739 if (dev->whoami == WAI_8B)
740 lis3lv02d_8b_configure(dev, p);
Daniel Mack8873c332009-09-21 17:04:43 -0700741
Daniel Mack8f3128e2009-06-16 15:34:17 -0700742 if (p->irq_cfg)
743 dev->write(dev, CTRL_REG3, p->irq_cfg);
744 }
745
Daniel Macka38da2e2009-03-31 15:24:32 -0700746 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700747 if (!dev->irq) {
748 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700749 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700750 goto out;
751 }
752
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700753 /*
754 * The sensor can generate interrupts for free-fall and direction
755 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
756 * the things simple and _fast_ we activate it only for free-fall, so
757 * no need to read register (very slow with ACPI). For the same reason,
758 * we forbid shared interrupts.
759 *
760 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
761 * io-apic is not configurable (and generates a warning) but I keep it
762 * in case of support for other hardware.
763 */
Takashi Iwaif7c77a32010-09-23 10:01:11 -0700764 if (dev->pdata && dev->whoami == WAI_8B)
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700765 thread_fn = lis302dl_interrupt_thread1_8b;
766 else
767 thread_fn = NULL;
768
769 err = request_threaded_irq(dev->irq, lis302dl_interrupt,
770 thread_fn,
771 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
772 DRIVER_NAME, &lis3_dev);
773
774 if (err < 0) {
775 printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
776 goto out;
777 }
778
Daniel Mackab337a62009-03-31 15:24:31 -0700779 if (misc_register(&lis3lv02d_misc_device))
780 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
781out:
Daniel Mackab337a62009-03-31 15:24:31 -0700782 return 0;
783}
784EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
785
Pavel Machek455fbdd2008-11-12 13:27:02 -0800786MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800787MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800788MODULE_LICENSE("GPL");