blob: 56a1d34d36705293e57ae36d66b9e6c770f340c7 [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
44/*
45 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
Éric Pielbc62c142009-12-14 18:01:39 -080046 * because they are generated even if the data do not change. So it's better
Pavel Machek455fbdd2008-11-12 13:27:02 -080047 * to keep the interrupt for the free-fall event. The values are updated at
48 * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
49 * some low processor, we poll the sensor only at 20Hz... enough for the
50 * joystick.
51 */
52
Samu Onkalo641615a2009-12-14 18:01:41 -080053#define LIS3_PWRON_DELAY_WAI_12B (5000)
54#define LIS3_PWRON_DELAY_WAI_8B (3000)
55
Samu Onkalo32496c72009-12-14 18:01:46 -080056/*
57 * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
58 * LIS302D spec says: 18 mG / digit
59 * LIS3_ACCURACY is used to increase accuracy of the intermediate
60 * calculation results.
61 */
62#define LIS3_ACCURACY 1024
63/* Sensitivity values for -2G +2G scale */
64#define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024)
65#define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY)
66
67#define LIS3_DEFAULT_FUZZ 3
68#define LIS3_DEFAULT_FLAT 3
69
Daniel Macka38da2e2009-03-31 15:24:32 -070070struct lis3lv02d lis3_dev = {
Pavel Machekbe84cfc2009-03-31 15:24:26 -070071 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
Pavel Machekef2cfc72009-02-18 14:48:23 -080072};
73
Pavel Machekbe84cfc2009-03-31 15:24:26 -070074EXPORT_SYMBOL_GPL(lis3_dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080075
Daniel Macka38da2e2009-03-31 15:24:32 -070076static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
77{
78 s8 lo;
79 if (lis3->read(lis3, reg, &lo) < 0)
80 return 0;
81
82 return lo;
83}
84
Éric Pielbc62c142009-12-14 18:01:39 -080085static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -070086{
87 u8 lo, hi;
88
Daniel Macka38da2e2009-03-31 15:24:32 -070089 lis3->read(lis3, reg - 1, &lo);
90 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -070091 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
92 return (s16)((hi << 8) | lo);
93}
94
Pavel Machek455fbdd2008-11-12 13:27:02 -080095/**
96 * lis3lv02d_get_axis - For the given axis, give the value converted
97 * @axis: 1,2,3 - can also be negative
98 * @hw_values: raw values returned by the hardware
99 *
100 * Returns the converted value.
101 */
102static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
103{
104 if (axis > 0)
105 return hw_values[axis - 1];
106 else
107 return -hw_values[-axis - 1];
108}
109
110/**
111 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -0700112 * @lis3: pointer to the device struct
113 * @x: where to store the X axis value
114 * @y: where to store the Y axis value
115 * @z: where to store the Z axis value
Pavel Machek455fbdd2008-11-12 13:27:02 -0800116 *
117 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
118 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700119static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800120{
121 int position[3];
Samu Onkalo32496c72009-12-14 18:01:46 -0800122 int i;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800123
Eric Piela002ee82009-06-16 15:34:14 -0700124 position[0] = lis3->read_data(lis3, OUTX);
125 position[1] = lis3->read_data(lis3, OUTY);
126 position[2] = lis3->read_data(lis3, OUTZ);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800127
Samu Onkalo32496c72009-12-14 18:01:46 -0800128 for (i = 0; i < 3; i++)
129 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
130
Eric Piela002ee82009-06-16 15:34:14 -0700131 *x = lis3lv02d_get_axis(lis3->ac.x, position);
132 *y = lis3lv02d_get_axis(lis3->ac.y, position);
133 *z = lis3lv02d_get_axis(lis3->ac.z, position);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800134}
135
Samu Onkalo641615a2009-12-14 18:01:41 -0800136/* conversion btw sampling rate and the register values */
137static int lis3_12_rates[4] = {40, 160, 640, 2560};
138static int lis3_8_rates[2] = {100, 400};
139
Samu Onkaloa253aae2009-12-14 18:01:44 -0800140/* ODR is Output Data Rate */
Samu Onkalo641615a2009-12-14 18:01:41 -0800141static int lis3lv02d_get_odr(void)
142{
143 u8 ctrl;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800144 int shift;
Samu Onkalo641615a2009-12-14 18:01:41 -0800145
146 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800147 ctrl &= lis3_dev.odr_mask;
148 shift = ffs(lis3_dev.odr_mask) - 1;
149 return lis3_dev.odrs[(ctrl >> shift)];
150}
Samu Onkalo641615a2009-12-14 18:01:41 -0800151
Samu Onkaloa253aae2009-12-14 18:01:44 -0800152static int lis3lv02d_set_odr(int rate)
153{
154 u8 ctrl;
155 int i, len, shift;
156
157 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
158 ctrl &= ~lis3_dev.odr_mask;
159 len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
160 shift = ffs(lis3_dev.odr_mask) - 1;
161
162 for (i = 0; i < len; i++)
163 if (lis3_dev.odrs[i] == rate) {
164 lis3_dev.write(&lis3_dev, CTRL_REG1,
165 ctrl | (i << shift));
166 return 0;
167 }
168 return -EINVAL;
Samu Onkalo641615a2009-12-14 18:01:41 -0800169}
170
Samu Onkalo2db4a762009-12-14 18:01:43 -0800171static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
172{
173 u8 reg;
174 s16 x, y, z;
175 u8 selftest;
176 int ret;
177
178 mutex_lock(&lis3->mutex);
179 if (lis3_dev.whoami == WAI_12B)
180 selftest = CTRL1_ST;
181 else
182 selftest = CTRL1_STP;
183
184 lis3->read(lis3, CTRL_REG1, &reg);
185 lis3->write(lis3, CTRL_REG1, (reg | selftest));
186 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
187
188 /* Read directly to avoid axis remap */
189 x = lis3->read_data(lis3, OUTX);
190 y = lis3->read_data(lis3, OUTY);
191 z = lis3->read_data(lis3, OUTZ);
192
193 /* back to normal settings */
194 lis3->write(lis3, CTRL_REG1, reg);
195 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
196
197 results[0] = x - lis3->read_data(lis3, OUTX);
198 results[1] = y - lis3->read_data(lis3, OUTY);
199 results[2] = z - lis3->read_data(lis3, OUTZ);
200
201 ret = 0;
202 if (lis3->pdata) {
203 int i;
204 for (i = 0; i < 3; i++) {
205 /* Check against selftest acceptance limits */
206 if ((results[i] < lis3->pdata->st_min_limits[i]) ||
207 (results[i] > lis3->pdata->st_max_limits[i])) {
208 ret = -EIO;
209 goto fail;
210 }
211 }
212 }
213
214 /* test passed */
215fail:
216 mutex_unlock(&lis3->mutex);
217 return ret;
218}
219
Daniel Macka38da2e2009-03-31 15:24:32 -0700220void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800221{
Eric Piela002ee82009-06-16 15:34:14 -0700222 /* disable X,Y,Z axis and power down */
223 lis3->write(lis3, CTRL_REG1, 0x00);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800224}
Eric Pielcfce41a2009-01-09 16:41:01 -0800225EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800226
Daniel Macka38da2e2009-03-31 15:24:32 -0700227void lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800228{
Eric Piela002ee82009-06-16 15:34:14 -0700229 u8 reg;
230
231 lis3->init(lis3);
232
Samu Onkalo641615a2009-12-14 18:01:41 -0800233 /* LIS3 power on delay is quite long */
234 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
235
Eric Piela002ee82009-06-16 15:34:14 -0700236 /*
237 * Common configuration
Éric Piel4b5d95b2009-12-14 18:01:40 -0800238 * BDU: (12 bits sensors only) LSB and MSB values are not updated until
239 * both have been read. So the value read will always be correct.
Eric Piela002ee82009-06-16 15:34:14 -0700240 */
Éric Piel4b5d95b2009-12-14 18:01:40 -0800241 if (lis3->whoami == WAI_12B) {
242 lis3->read(lis3, CTRL_REG2, &reg);
243 reg |= CTRL2_BDU;
244 lis3->write(lis3, CTRL_REG2, reg);
245 }
Pavel Machek455fbdd2008-11-12 13:27:02 -0800246}
Eric Pielcfce41a2009-01-09 16:41:01 -0800247EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800248
Pavel Machek455fbdd2008-11-12 13:27:02 -0800249
Samu Onkalo6d94d402010-05-24 14:33:37 -0700250static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
251{
252 int x, y, z;
253
254 mutex_lock(&lis3_dev.mutex);
255 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
256 input_report_abs(pidev->input, ABS_X, x);
257 input_report_abs(pidev->input, ABS_Y, y);
258 input_report_abs(pidev->input, ABS_Z, z);
259 input_sync(pidev->input);
260 mutex_unlock(&lis3_dev.mutex);
261}
262
Pavel Machekef2cfc72009-02-18 14:48:23 -0800263static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
264{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700265 if (!test_bit(0, &lis3_dev.misc_opened))
266 goto out;
267
Pavel Machekef2cfc72009-02-18 14:48:23 -0800268 /*
269 * Be careful: on some HP laptops the bios force DD when on battery and
270 * the lid is closed. This leads to interrupts as soon as a little move
271 * is done.
272 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700273 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800274
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700275 wake_up_interruptible(&lis3_dev.misc_wait);
276 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700277out:
278 if (lis3_dev.whoami == WAI_8B && lis3_dev.idev &&
279 lis3_dev.idev->input->users)
280 return IRQ_WAKE_THREAD;
281 return IRQ_HANDLED;
282}
283
Samu Onkalo6d94d402010-05-24 14:33:37 -0700284static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
285{
286 struct input_dev *dev = lis3->idev->input;
287 u8 click_src;
288
289 mutex_lock(&lis3->mutex);
290 lis3->read(lis3, CLICK_SRC, &click_src);
291
292 if (click_src & CLICK_SINGLE_X) {
293 input_report_key(dev, lis3->mapped_btns[0], 1);
294 input_report_key(dev, lis3->mapped_btns[0], 0);
295 }
296
297 if (click_src & CLICK_SINGLE_Y) {
298 input_report_key(dev, lis3->mapped_btns[1], 1);
299 input_report_key(dev, lis3->mapped_btns[1], 0);
300 }
301
302 if (click_src & CLICK_SINGLE_Z) {
303 input_report_key(dev, lis3->mapped_btns[2], 1);
304 input_report_key(dev, lis3->mapped_btns[2], 0);
305 }
306 input_sync(dev);
307 mutex_unlock(&lis3->mutex);
308}
309
310static void lis302dl_interrupt_handle_ff_wu(struct lis3lv02d *lis3)
311{
312 u8 wu1_src;
313 u8 wu2_src;
314
315 lis3->read(lis3, FF_WU_SRC_1, &wu1_src);
316 lis3->read(lis3, FF_WU_SRC_2, &wu2_src);
317
318 wu1_src = wu1_src & FF_WU_SRC_IA ? wu1_src : 0;
319 wu2_src = wu2_src & FF_WU_SRC_IA ? wu2_src : 0;
320
321 /* joystick poll is internally protected by the lis3->mutex. */
322 if (wu1_src || wu2_src)
323 lis3lv02d_joystick_poll(lis3_dev.idev);
324}
325
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700326static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
327{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700328
329 struct lis3lv02d *lis3 = data;
330
331 if ((lis3->pdata->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
332 lis302dl_interrupt_handle_click(lis3);
333 else
334 lis302dl_interrupt_handle_ff_wu(lis3);
335
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700336 return IRQ_HANDLED;
337}
338
339static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
340{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700341
342 struct lis3lv02d *lis3 = data;
343
344 if ((lis3->pdata->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
345 lis302dl_interrupt_handle_click(lis3);
346 else
347 lis302dl_interrupt_handle_ff_wu(lis3);
348
Pavel Machekef2cfc72009-02-18 14:48:23 -0800349 return IRQ_HANDLED;
350}
351
352static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
353{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700354 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800355 return -EBUSY; /* already open */
356
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700357 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800358 return 0;
359}
360
361static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
362{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700363 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700364 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Pavel Machekef2cfc72009-02-18 14:48:23 -0800365 return 0;
366}
367
368static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
369 size_t count, loff_t *pos)
370{
371 DECLARE_WAITQUEUE(wait, current);
372 u32 data;
373 unsigned char byte_data;
374 ssize_t retval = 1;
375
376 if (count < 1)
377 return -EINVAL;
378
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700379 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800380 while (true) {
381 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700382 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800383 if (data)
384 break;
385
386 if (file->f_flags & O_NONBLOCK) {
387 retval = -EAGAIN;
388 goto out;
389 }
390
391 if (signal_pending(current)) {
392 retval = -ERESTARTSYS;
393 goto out;
394 }
395
396 schedule();
397 }
398
399 if (data < 255)
400 byte_data = data;
401 else
402 byte_data = 255;
403
404 /* make sure we are not going into copy_to_user() with
405 * TASK_INTERRUPTIBLE state */
406 set_current_state(TASK_RUNNING);
407 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
408 retval = -EFAULT;
409
410out:
411 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700412 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800413
414 return retval;
415}
416
417static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
418{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700419 poll_wait(file, &lis3_dev.misc_wait, wait);
420 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800421 return POLLIN | POLLRDNORM;
422 return 0;
423}
424
425static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
426{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700427 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800428}
429
430static const struct file_operations lis3lv02d_misc_fops = {
431 .owner = THIS_MODULE,
432 .llseek = no_llseek,
433 .read = lis3lv02d_misc_read,
434 .open = lis3lv02d_misc_open,
435 .release = lis3lv02d_misc_release,
436 .poll = lis3lv02d_misc_poll,
437 .fasync = lis3lv02d_misc_fasync,
438};
439
440static struct miscdevice lis3lv02d_misc_device = {
441 .minor = MISC_DYNAMIC_MINOR,
442 .name = "freefall",
443 .fops = &lis3lv02d_misc_fops,
444};
445
Eric Pielcfce41a2009-01-09 16:41:01 -0800446int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800447{
Eric Pieldc6ea972009-06-16 15:34:15 -0700448 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800449 int err;
Samu Onkalo32496c72009-12-14 18:01:46 -0800450 int max_val, fuzz, flat;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700451 int btns[] = {BTN_X, BTN_Y, BTN_Z};
Pavel Machek455fbdd2008-11-12 13:27:02 -0800452
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700453 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800454 return -EINVAL;
455
Eric Pieldc6ea972009-06-16 15:34:15 -0700456 lis3_dev.idev = input_allocate_polled_device();
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700457 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800458 return -ENOMEM;
459
Eric Pieldc6ea972009-06-16 15:34:15 -0700460 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
461 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
462 input_dev = lis3_dev.idev->input;
463
Eric Pieldc6ea972009-06-16 15:34:15 -0700464 input_dev->name = "ST LIS3LV02DL Accelerometer";
465 input_dev->phys = DRIVER_NAME "/input0";
466 input_dev->id.bustype = BUS_HOST;
467 input_dev->id.vendor = 0;
468 input_dev->dev.parent = &lis3_dev.pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800469
Eric Pieldc6ea972009-06-16 15:34:15 -0700470 set_bit(EV_ABS, input_dev->evbit);
Samu Onkalo32496c72009-12-14 18:01:46 -0800471 max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
472 fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
473 flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
474 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
475 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
476 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800477
Samu Onkalo6d94d402010-05-24 14:33:37 -0700478 lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
479 lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
480 lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
481
Eric Pieldc6ea972009-06-16 15:34:15 -0700482 err = input_register_polled_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800483 if (err) {
Eric Pieldc6ea972009-06-16 15:34:15 -0700484 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700485 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800486 }
487
488 return err;
489}
Eric Pielcfce41a2009-01-09 16:41:01 -0800490EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800491
Eric Pielcfce41a2009-01-09 16:41:01 -0800492void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800493{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700494 if (lis3_dev.irq)
495 free_irq(lis3_dev.irq, &lis3_dev);
496 if (lis3_dev.pdata && lis3_dev.pdata->irq2)
497 free_irq(lis3_dev.pdata->irq2, &lis3_dev);
498
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700499 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800500 return;
501
Eric Pielc2884242009-06-16 15:34:13 -0700502 if (lis3_dev.irq)
503 misc_deregister(&lis3lv02d_misc_device);
Eric Pieldc6ea972009-06-16 15:34:15 -0700504 input_unregister_polled_device(lis3_dev.idev);
Samu Onkalo66c85692009-12-14 18:01:39 -0800505 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700506 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800507}
Eric Pielcfce41a2009-01-09 16:41:01 -0800508EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800509
Pavel Machek455fbdd2008-11-12 13:27:02 -0800510/* Sysfs stuff */
Samu Onkalo2db4a762009-12-14 18:01:43 -0800511static ssize_t lis3lv02d_selftest_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
514 int result;
515 s16 values[3];
516
517 result = lis3lv02d_selftest(&lis3_dev, values);
518 return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
519 values[0], values[1], values[2]);
520}
521
Pavel Machek455fbdd2008-11-12 13:27:02 -0800522static ssize_t lis3lv02d_position_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
525 int x, y, z;
526
Samu Onkalo6d94d402010-05-24 14:33:37 -0700527 mutex_lock(&lis3_dev.mutex);
Daniel Macka38da2e2009-03-31 15:24:32 -0700528 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700529 mutex_unlock(&lis3_dev.mutex);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800530 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
531}
532
Pavel Machek455fbdd2008-11-12 13:27:02 -0800533static ssize_t lis3lv02d_rate_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
Samu Onkalo641615a2009-12-14 18:01:41 -0800536 return sprintf(buf, "%d\n", lis3lv02d_get_odr());
Pavel Machek455fbdd2008-11-12 13:27:02 -0800537}
538
Samu Onkaloa253aae2009-12-14 18:01:44 -0800539static ssize_t lis3lv02d_rate_set(struct device *dev,
540 struct device_attribute *attr, const char *buf,
541 size_t count)
542{
543 unsigned long rate;
544
545 if (strict_strtoul(buf, 0, &rate))
546 return -EINVAL;
547
548 if (lis3lv02d_set_odr(rate))
549 return -EINVAL;
550
551 return count;
552}
553
Samu Onkalo2db4a762009-12-14 18:01:43 -0800554static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800555static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800556static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
557 lis3lv02d_rate_set);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800558
559static struct attribute *lis3lv02d_attributes[] = {
Samu Onkalo2db4a762009-12-14 18:01:43 -0800560 &dev_attr_selftest.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800561 &dev_attr_position.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800562 &dev_attr_rate.attr,
563 NULL
564};
565
566static struct attribute_group lis3lv02d_attribute_group = {
567 .attrs = lis3lv02d_attributes
568};
569
Eric Pielcfce41a2009-01-09 16:41:01 -0800570
Daniel Macka38da2e2009-03-31 15:24:32 -0700571static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800572{
Eric Piela002ee82009-06-16 15:34:14 -0700573 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
574 if (IS_ERR(lis3->pdev))
575 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800576
Eric Piela002ee82009-06-16 15:34:14 -0700577 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800578}
579
Eric Piela002ee82009-06-16 15:34:14 -0700580int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800581{
Eric Piela002ee82009-06-16 15:34:14 -0700582 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
583 platform_device_unregister(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800584 return 0;
585}
Eric Pielcfce41a2009-01-09 16:41:01 -0800586EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800587
Samu Onkaloecc437a2010-05-24 14:33:34 -0700588static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
589 struct lis3lv02d_platform_data *p)
590{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700591 int err;
Samu Onkalo342c5f12010-05-24 14:33:35 -0700592 int ctrl2 = p->hipass_ctrl;
593
Samu Onkaloecc437a2010-05-24 14:33:34 -0700594 if (p->click_flags) {
595 dev->write(dev, CLICK_CFG, p->click_flags);
596 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
597 dev->write(dev, CLICK_LATENCY, p->click_latency);
598 dev->write(dev, CLICK_WINDOW, p->click_window);
599 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
600 dev->write(dev, CLICK_THSY_X,
601 (p->click_thresh_x & 0xf) |
602 (p->click_thresh_y << 4));
Samu Onkalo6d94d402010-05-24 14:33:37 -0700603
604 if (dev->idev) {
605 struct input_dev *input_dev = lis3_dev.idev->input;
606 input_set_capability(input_dev, EV_KEY, BTN_X);
607 input_set_capability(input_dev, EV_KEY, BTN_Y);
608 input_set_capability(input_dev, EV_KEY, BTN_Z);
609 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700610 }
611
612 if (p->wakeup_flags) {
613 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
614 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
615 /* default to 2.5ms for now */
616 dev->write(dev, FF_WU_DURATION_1, 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700617 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
Samu Onkaloecc437a2010-05-24 14:33:34 -0700618 }
Samu Onkalo342c5f12010-05-24 14:33:35 -0700619
620 if (p->wakeup_flags2) {
621 dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
622 dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
623 /* default to 2.5ms for now */
624 dev->write(dev, FF_WU_DURATION_2, 1);
625 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
626 }
627 /* Configure hipass filters */
628 dev->write(dev, CTRL_REG2, ctrl2);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700629
630 if (p->irq2) {
631 err = request_threaded_irq(p->irq2,
632 NULL,
633 lis302dl_interrupt_thread2_8b,
634 IRQF_TRIGGER_RISING |
635 IRQF_ONESHOT,
636 DRIVER_NAME, &lis3_dev);
637 if (err < 0)
638 printk(KERN_ERR DRIVER_NAME
639 "No second IRQ. Limited functionality\n");
640 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700641}
642
Daniel Mackab337a62009-03-31 15:24:31 -0700643/*
644 * Initialise the accelerometer and the various subsystems.
Éric Pielbc62c142009-12-14 18:01:39 -0800645 * Should be rather independent of the bus system.
Daniel Mackab337a62009-03-31 15:24:31 -0700646 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700647int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700648{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700649 int err;
650 irq_handler_t thread_fn;
651
Daniel Macka38da2e2009-03-31 15:24:32 -0700652 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
653
654 switch (dev->whoami) {
Éric Pielbc62c142009-12-14 18:01:39 -0800655 case WAI_12B:
656 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
657 dev->read_data = lis3lv02d_read_12;
Daniel Macka38da2e2009-03-31 15:24:32 -0700658 dev->mdps_max_val = 2048;
Samu Onkalo641615a2009-12-14 18:01:41 -0800659 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800660 dev->odrs = lis3_12_rates;
661 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
Samu Onkalo32496c72009-12-14 18:01:46 -0800662 dev->scale = LIS3_SENSITIVITY_12B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700663 break;
Éric Pielbc62c142009-12-14 18:01:39 -0800664 case WAI_8B:
665 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
Daniel Macka38da2e2009-03-31 15:24:32 -0700666 dev->read_data = lis3lv02d_read_8;
667 dev->mdps_max_val = 128;
Samu Onkalo641615a2009-12-14 18:01:41 -0800668 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800669 dev->odrs = lis3_8_rates;
670 dev->odr_mask = CTRL1_DR;
Samu Onkalo32496c72009-12-14 18:01:46 -0800671 dev->scale = LIS3_SENSITIVITY_8B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700672 break;
673 default:
674 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700675 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700676 return -EINVAL;
677 }
678
Samu Onkalo2db4a762009-12-14 18:01:43 -0800679 mutex_init(&dev->mutex);
680
Daniel Macka38da2e2009-03-31 15:24:32 -0700681 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700682 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700683
684 if (lis3lv02d_joystick_enable())
685 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
686
Daniel Mack8f3128e2009-06-16 15:34:17 -0700687 /* passing in platform specific data is purely optional and only
688 * used by the SPI transport layer at the moment */
689 if (dev->pdata) {
690 struct lis3lv02d_platform_data *p = dev->pdata;
691
Samu Onkaloecc437a2010-05-24 14:33:34 -0700692 if (dev->whoami == WAI_8B)
693 lis3lv02d_8b_configure(dev, p);
Daniel Mack8873c332009-09-21 17:04:43 -0700694
Daniel Mack8f3128e2009-06-16 15:34:17 -0700695 if (p->irq_cfg)
696 dev->write(dev, CTRL_REG3, p->irq_cfg);
697 }
698
Daniel Macka38da2e2009-03-31 15:24:32 -0700699 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700700 if (!dev->irq) {
701 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700702 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700703 goto out;
704 }
705
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700706 /*
707 * The sensor can generate interrupts for free-fall and direction
708 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
709 * the things simple and _fast_ we activate it only for free-fall, so
710 * no need to read register (very slow with ACPI). For the same reason,
711 * we forbid shared interrupts.
712 *
713 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
714 * io-apic is not configurable (and generates a warning) but I keep it
715 * in case of support for other hardware.
716 */
717 if (dev->whoami == WAI_8B)
718 thread_fn = lis302dl_interrupt_thread1_8b;
719 else
720 thread_fn = NULL;
721
722 err = request_threaded_irq(dev->irq, lis302dl_interrupt,
723 thread_fn,
724 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
725 DRIVER_NAME, &lis3_dev);
726
727 if (err < 0) {
728 printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
729 goto out;
730 }
731
Daniel Mackab337a62009-03-31 15:24:31 -0700732 if (misc_register(&lis3lv02d_misc_device))
733 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
734out:
Daniel Mackab337a62009-03-31 15:24:31 -0700735 return 0;
736}
737EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
738
Pavel Machek455fbdd2008-11-12 13:27:02 -0800739MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800740MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800741MODULE_LICENSE("GPL");