blob: fc591ae53107da8481a2ab5e02f53117ac6e2471 [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
Daniel Macka38da2e2009-03-31 15:24:32 -070078static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
79{
80 s8 lo;
81 if (lis3->read(lis3, reg, &lo) < 0)
82 return 0;
83
84 return lo;
85}
86
Éric Pielbc62c142009-12-14 18:01:39 -080087static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -070088{
89 u8 lo, hi;
90
Daniel Macka38da2e2009-03-31 15:24:32 -070091 lis3->read(lis3, reg - 1, &lo);
92 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -070093 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
94 return (s16)((hi << 8) | lo);
95}
96
Pavel Machek455fbdd2008-11-12 13:27:02 -080097/**
98 * lis3lv02d_get_axis - For the given axis, give the value converted
99 * @axis: 1,2,3 - can also be negative
100 * @hw_values: raw values returned by the hardware
101 *
102 * Returns the converted value.
103 */
104static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
105{
106 if (axis > 0)
107 return hw_values[axis - 1];
108 else
109 return -hw_values[-axis - 1];
110}
111
112/**
113 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -0700114 * @lis3: pointer to the device struct
115 * @x: where to store the X axis value
116 * @y: where to store the Y axis value
117 * @z: where to store the Z axis value
Pavel Machek455fbdd2008-11-12 13:27:02 -0800118 *
119 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
120 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700121static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800122{
123 int position[3];
Samu Onkalo32496c72009-12-14 18:01:46 -0800124 int i;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800125
Eric Piela002ee82009-06-16 15:34:14 -0700126 position[0] = lis3->read_data(lis3, OUTX);
127 position[1] = lis3->read_data(lis3, OUTY);
128 position[2] = lis3->read_data(lis3, OUTZ);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800129
Samu Onkalo32496c72009-12-14 18:01:46 -0800130 for (i = 0; i < 3; i++)
131 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
132
Eric Piela002ee82009-06-16 15:34:14 -0700133 *x = lis3lv02d_get_axis(lis3->ac.x, position);
134 *y = lis3lv02d_get_axis(lis3->ac.y, position);
135 *z = lis3lv02d_get_axis(lis3->ac.z, position);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800136}
137
Samu Onkalo641615a2009-12-14 18:01:41 -0800138/* conversion btw sampling rate and the register values */
139static int lis3_12_rates[4] = {40, 160, 640, 2560};
140static int lis3_8_rates[2] = {100, 400};
141
Samu Onkaloa253aae2009-12-14 18:01:44 -0800142/* ODR is Output Data Rate */
Samu Onkalo641615a2009-12-14 18:01:41 -0800143static int lis3lv02d_get_odr(void)
144{
145 u8 ctrl;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800146 int shift;
Samu Onkalo641615a2009-12-14 18:01:41 -0800147
148 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800149 ctrl &= lis3_dev.odr_mask;
150 shift = ffs(lis3_dev.odr_mask) - 1;
151 return lis3_dev.odrs[(ctrl >> shift)];
152}
Samu Onkalo641615a2009-12-14 18:01:41 -0800153
Samu Onkaloa253aae2009-12-14 18:01:44 -0800154static int lis3lv02d_set_odr(int rate)
155{
156 u8 ctrl;
157 int i, len, shift;
158
159 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
160 ctrl &= ~lis3_dev.odr_mask;
161 len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
162 shift = ffs(lis3_dev.odr_mask) - 1;
163
164 for (i = 0; i < len; i++)
165 if (lis3_dev.odrs[i] == rate) {
166 lis3_dev.write(&lis3_dev, CTRL_REG1,
167 ctrl | (i << shift));
168 return 0;
169 }
170 return -EINVAL;
Samu Onkalo641615a2009-12-14 18:01:41 -0800171}
172
Samu Onkalo2db4a762009-12-14 18:01:43 -0800173static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
174{
175 u8 reg;
176 s16 x, y, z;
177 u8 selftest;
178 int ret;
179
180 mutex_lock(&lis3->mutex);
181 if (lis3_dev.whoami == WAI_12B)
182 selftest = CTRL1_ST;
183 else
184 selftest = CTRL1_STP;
185
186 lis3->read(lis3, CTRL_REG1, &reg);
187 lis3->write(lis3, CTRL_REG1, (reg | selftest));
188 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
189
190 /* Read directly to avoid axis remap */
191 x = lis3->read_data(lis3, OUTX);
192 y = lis3->read_data(lis3, OUTY);
193 z = lis3->read_data(lis3, OUTZ);
194
195 /* back to normal settings */
196 lis3->write(lis3, CTRL_REG1, reg);
197 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
198
199 results[0] = x - lis3->read_data(lis3, OUTX);
200 results[1] = y - lis3->read_data(lis3, OUTY);
201 results[2] = z - lis3->read_data(lis3, OUTZ);
202
203 ret = 0;
204 if (lis3->pdata) {
205 int i;
206 for (i = 0; i < 3; i++) {
207 /* Check against selftest acceptance limits */
208 if ((results[i] < lis3->pdata->st_min_limits[i]) ||
209 (results[i] > lis3->pdata->st_max_limits[i])) {
210 ret = -EIO;
211 goto fail;
212 }
213 }
214 }
215
216 /* test passed */
217fail:
218 mutex_unlock(&lis3->mutex);
219 return ret;
220}
221
Daniel Macka38da2e2009-03-31 15:24:32 -0700222void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800223{
Eric Piela002ee82009-06-16 15:34:14 -0700224 /* disable X,Y,Z axis and power down */
225 lis3->write(lis3, CTRL_REG1, 0x00);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800226}
Eric Pielcfce41a2009-01-09 16:41:01 -0800227EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800228
Daniel Macka38da2e2009-03-31 15:24:32 -0700229void lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800230{
Eric Piela002ee82009-06-16 15:34:14 -0700231 u8 reg;
232
233 lis3->init(lis3);
234
Samu Onkalo641615a2009-12-14 18:01:41 -0800235 /* LIS3 power on delay is quite long */
236 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
237
Eric Piela002ee82009-06-16 15:34:14 -0700238 /*
239 * Common configuration
Éric Piel4b5d95b2009-12-14 18:01:40 -0800240 * BDU: (12 bits sensors only) LSB and MSB values are not updated until
241 * both have been read. So the value read will always be correct.
Eric Piela002ee82009-06-16 15:34:14 -0700242 */
Éric Piel4b5d95b2009-12-14 18:01:40 -0800243 if (lis3->whoami == WAI_12B) {
244 lis3->read(lis3, CTRL_REG2, &reg);
245 reg |= CTRL2_BDU;
246 lis3->write(lis3, CTRL_REG2, reg);
247 }
Pavel Machek455fbdd2008-11-12 13:27:02 -0800248}
Eric Pielcfce41a2009-01-09 16:41:01 -0800249EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800250
Pavel Machek455fbdd2008-11-12 13:27:02 -0800251
Samu Onkalo6d94d402010-05-24 14:33:37 -0700252static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
253{
254 int x, y, z;
255
256 mutex_lock(&lis3_dev.mutex);
257 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
258 input_report_abs(pidev->input, ABS_X, x);
259 input_report_abs(pidev->input, ABS_Y, y);
260 input_report_abs(pidev->input, ABS_Z, z);
261 input_sync(pidev->input);
262 mutex_unlock(&lis3_dev.mutex);
263}
264
Pavel Machekef2cfc72009-02-18 14:48:23 -0800265static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
266{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700267 if (!test_bit(0, &lis3_dev.misc_opened))
268 goto out;
269
Pavel Machekef2cfc72009-02-18 14:48:23 -0800270 /*
271 * Be careful: on some HP laptops the bios force DD when on battery and
272 * the lid is closed. This leads to interrupts as soon as a little move
273 * is done.
274 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700275 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800276
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700277 wake_up_interruptible(&lis3_dev.misc_wait);
278 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700279out:
Takashi Iwaif7c77a32010-09-23 10:01:11 -0700280 if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev &&
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700281 lis3_dev.idev->input->users)
282 return IRQ_WAKE_THREAD;
283 return IRQ_HANDLED;
284}
285
Samu Onkalo6d94d402010-05-24 14:33:37 -0700286static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
287{
288 struct input_dev *dev = lis3->idev->input;
289 u8 click_src;
290
291 mutex_lock(&lis3->mutex);
292 lis3->read(lis3, CLICK_SRC, &click_src);
293
294 if (click_src & CLICK_SINGLE_X) {
295 input_report_key(dev, lis3->mapped_btns[0], 1);
296 input_report_key(dev, lis3->mapped_btns[0], 0);
297 }
298
299 if (click_src & CLICK_SINGLE_Y) {
300 input_report_key(dev, lis3->mapped_btns[1], 1);
301 input_report_key(dev, lis3->mapped_btns[1], 0);
302 }
303
304 if (click_src & CLICK_SINGLE_Z) {
305 input_report_key(dev, lis3->mapped_btns[2], 1);
306 input_report_key(dev, lis3->mapped_btns[2], 0);
307 }
308 input_sync(dev);
309 mutex_unlock(&lis3->mutex);
310}
311
312static void lis302dl_interrupt_handle_ff_wu(struct lis3lv02d *lis3)
313{
314 u8 wu1_src;
315 u8 wu2_src;
316
317 lis3->read(lis3, FF_WU_SRC_1, &wu1_src);
318 lis3->read(lis3, FF_WU_SRC_2, &wu2_src);
319
320 wu1_src = wu1_src & FF_WU_SRC_IA ? wu1_src : 0;
321 wu2_src = wu2_src & FF_WU_SRC_IA ? wu2_src : 0;
322
323 /* joystick poll is internally protected by the lis3->mutex. */
324 if (wu1_src || wu2_src)
325 lis3lv02d_joystick_poll(lis3_dev.idev);
326}
327
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700328static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
329{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700330
331 struct lis3lv02d *lis3 = data;
332
333 if ((lis3->pdata->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
334 lis302dl_interrupt_handle_click(lis3);
335 else
336 lis302dl_interrupt_handle_ff_wu(lis3);
337
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700338 return IRQ_HANDLED;
339}
340
341static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
342{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700343
344 struct lis3lv02d *lis3 = data;
345
346 if ((lis3->pdata->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
347 lis302dl_interrupt_handle_click(lis3);
348 else
349 lis302dl_interrupt_handle_ff_wu(lis3);
350
Pavel Machekef2cfc72009-02-18 14:48:23 -0800351 return IRQ_HANDLED;
352}
353
354static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
355{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700356 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800357 return -EBUSY; /* already open */
358
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700359 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800360 return 0;
361}
362
363static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
364{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700365 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700366 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Pavel Machekef2cfc72009-02-18 14:48:23 -0800367 return 0;
368}
369
370static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
371 size_t count, loff_t *pos)
372{
373 DECLARE_WAITQUEUE(wait, current);
374 u32 data;
375 unsigned char byte_data;
376 ssize_t retval = 1;
377
378 if (count < 1)
379 return -EINVAL;
380
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700381 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800382 while (true) {
383 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700384 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800385 if (data)
386 break;
387
388 if (file->f_flags & O_NONBLOCK) {
389 retval = -EAGAIN;
390 goto out;
391 }
392
393 if (signal_pending(current)) {
394 retval = -ERESTARTSYS;
395 goto out;
396 }
397
398 schedule();
399 }
400
401 if (data < 255)
402 byte_data = data;
403 else
404 byte_data = 255;
405
406 /* make sure we are not going into copy_to_user() with
407 * TASK_INTERRUPTIBLE state */
408 set_current_state(TASK_RUNNING);
409 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
410 retval = -EFAULT;
411
412out:
413 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700414 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800415
416 return retval;
417}
418
419static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
420{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700421 poll_wait(file, &lis3_dev.misc_wait, wait);
422 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800423 return POLLIN | POLLRDNORM;
424 return 0;
425}
426
427static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
428{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700429 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800430}
431
432static const struct file_operations lis3lv02d_misc_fops = {
433 .owner = THIS_MODULE,
434 .llseek = no_llseek,
435 .read = lis3lv02d_misc_read,
436 .open = lis3lv02d_misc_open,
437 .release = lis3lv02d_misc_release,
438 .poll = lis3lv02d_misc_poll,
439 .fasync = lis3lv02d_misc_fasync,
440};
441
442static struct miscdevice lis3lv02d_misc_device = {
443 .minor = MISC_DYNAMIC_MINOR,
444 .name = "freefall",
445 .fops = &lis3lv02d_misc_fops,
446};
447
Eric Pielcfce41a2009-01-09 16:41:01 -0800448int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800449{
Eric Pieldc6ea972009-06-16 15:34:15 -0700450 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800451 int err;
Samu Onkalo32496c72009-12-14 18:01:46 -0800452 int max_val, fuzz, flat;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700453 int btns[] = {BTN_X, BTN_Y, BTN_Z};
Pavel Machek455fbdd2008-11-12 13:27:02 -0800454
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700455 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800456 return -EINVAL;
457
Eric Pieldc6ea972009-06-16 15:34:15 -0700458 lis3_dev.idev = input_allocate_polled_device();
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700459 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800460 return -ENOMEM;
461
Eric Pieldc6ea972009-06-16 15:34:15 -0700462 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
463 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
Samu Onkalo4a70a412010-05-24 14:33:37 -0700464 lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN;
465 lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX;
Eric Pieldc6ea972009-06-16 15:34:15 -0700466 input_dev = lis3_dev.idev->input;
467
Eric Pieldc6ea972009-06-16 15:34:15 -0700468 input_dev->name = "ST LIS3LV02DL Accelerometer";
469 input_dev->phys = DRIVER_NAME "/input0";
470 input_dev->id.bustype = BUS_HOST;
471 input_dev->id.vendor = 0;
472 input_dev->dev.parent = &lis3_dev.pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800473
Eric Pieldc6ea972009-06-16 15:34:15 -0700474 set_bit(EV_ABS, input_dev->evbit);
Samu Onkalo32496c72009-12-14 18:01:46 -0800475 max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
476 fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
477 flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
478 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
479 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
480 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800481
Samu Onkalo6d94d402010-05-24 14:33:37 -0700482 lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
483 lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
484 lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
485
Eric Pieldc6ea972009-06-16 15:34:15 -0700486 err = input_register_polled_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800487 if (err) {
Eric Pieldc6ea972009-06-16 15:34:15 -0700488 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700489 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800490 }
491
492 return err;
493}
Eric Pielcfce41a2009-01-09 16:41:01 -0800494EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800495
Eric Pielcfce41a2009-01-09 16:41:01 -0800496void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800497{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700498 if (lis3_dev.irq)
499 free_irq(lis3_dev.irq, &lis3_dev);
500 if (lis3_dev.pdata && lis3_dev.pdata->irq2)
501 free_irq(lis3_dev.pdata->irq2, &lis3_dev);
502
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700503 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800504 return;
505
Eric Pielc2884242009-06-16 15:34:13 -0700506 if (lis3_dev.irq)
507 misc_deregister(&lis3lv02d_misc_device);
Eric Pieldc6ea972009-06-16 15:34:15 -0700508 input_unregister_polled_device(lis3_dev.idev);
Samu Onkalo66c85692009-12-14 18:01:39 -0800509 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700510 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800511}
Eric Pielcfce41a2009-01-09 16:41:01 -0800512EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800513
Pavel Machek455fbdd2008-11-12 13:27:02 -0800514/* Sysfs stuff */
Samu Onkalo2db4a762009-12-14 18:01:43 -0800515static ssize_t lis3lv02d_selftest_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
518 int result;
519 s16 values[3];
520
521 result = lis3lv02d_selftest(&lis3_dev, values);
522 return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
523 values[0], values[1], values[2]);
524}
525
Pavel Machek455fbdd2008-11-12 13:27:02 -0800526static ssize_t lis3lv02d_position_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
528{
529 int x, y, z;
530
Samu Onkalo6d94d402010-05-24 14:33:37 -0700531 mutex_lock(&lis3_dev.mutex);
Daniel Macka38da2e2009-03-31 15:24:32 -0700532 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700533 mutex_unlock(&lis3_dev.mutex);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800534 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
535}
536
Pavel Machek455fbdd2008-11-12 13:27:02 -0800537static ssize_t lis3lv02d_rate_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
Samu Onkalo641615a2009-12-14 18:01:41 -0800540 return sprintf(buf, "%d\n", lis3lv02d_get_odr());
Pavel Machek455fbdd2008-11-12 13:27:02 -0800541}
542
Samu Onkaloa253aae2009-12-14 18:01:44 -0800543static ssize_t lis3lv02d_rate_set(struct device *dev,
544 struct device_attribute *attr, const char *buf,
545 size_t count)
546{
547 unsigned long rate;
548
549 if (strict_strtoul(buf, 0, &rate))
550 return -EINVAL;
551
552 if (lis3lv02d_set_odr(rate))
553 return -EINVAL;
554
555 return count;
556}
557
Samu Onkalo2db4a762009-12-14 18:01:43 -0800558static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800559static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800560static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
561 lis3lv02d_rate_set);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800562
563static struct attribute *lis3lv02d_attributes[] = {
Samu Onkalo2db4a762009-12-14 18:01:43 -0800564 &dev_attr_selftest.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800565 &dev_attr_position.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800566 &dev_attr_rate.attr,
567 NULL
568};
569
570static struct attribute_group lis3lv02d_attribute_group = {
571 .attrs = lis3lv02d_attributes
572};
573
Eric Pielcfce41a2009-01-09 16:41:01 -0800574
Daniel Macka38da2e2009-03-31 15:24:32 -0700575static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800576{
Eric Piela002ee82009-06-16 15:34:14 -0700577 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
578 if (IS_ERR(lis3->pdev))
579 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800580
Eric Piela002ee82009-06-16 15:34:14 -0700581 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800582}
583
Eric Piela002ee82009-06-16 15:34:14 -0700584int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800585{
Eric Piela002ee82009-06-16 15:34:14 -0700586 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
587 platform_device_unregister(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800588 return 0;
589}
Eric Pielcfce41a2009-01-09 16:41:01 -0800590EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800591
Samu Onkaloecc437a2010-05-24 14:33:34 -0700592static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
593 struct lis3lv02d_platform_data *p)
594{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700595 int err;
Samu Onkalo342c5f12010-05-24 14:33:35 -0700596 int ctrl2 = p->hipass_ctrl;
597
Samu Onkaloecc437a2010-05-24 14:33:34 -0700598 if (p->click_flags) {
599 dev->write(dev, CLICK_CFG, p->click_flags);
600 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
601 dev->write(dev, CLICK_LATENCY, p->click_latency);
602 dev->write(dev, CLICK_WINDOW, p->click_window);
603 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
604 dev->write(dev, CLICK_THSY_X,
605 (p->click_thresh_x & 0xf) |
606 (p->click_thresh_y << 4));
Samu Onkalo6d94d402010-05-24 14:33:37 -0700607
608 if (dev->idev) {
609 struct input_dev *input_dev = lis3_dev.idev->input;
610 input_set_capability(input_dev, EV_KEY, BTN_X);
611 input_set_capability(input_dev, EV_KEY, BTN_Y);
612 input_set_capability(input_dev, EV_KEY, BTN_Z);
613 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700614 }
615
616 if (p->wakeup_flags) {
617 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
618 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
619 /* default to 2.5ms for now */
620 dev->write(dev, FF_WU_DURATION_1, 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700621 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
Samu Onkaloecc437a2010-05-24 14:33:34 -0700622 }
Samu Onkalo342c5f12010-05-24 14:33:35 -0700623
624 if (p->wakeup_flags2) {
625 dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
626 dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
627 /* default to 2.5ms for now */
628 dev->write(dev, FF_WU_DURATION_2, 1);
629 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
630 }
631 /* Configure hipass filters */
632 dev->write(dev, CTRL_REG2, ctrl2);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700633
634 if (p->irq2) {
635 err = request_threaded_irq(p->irq2,
636 NULL,
637 lis302dl_interrupt_thread2_8b,
638 IRQF_TRIGGER_RISING |
639 IRQF_ONESHOT,
640 DRIVER_NAME, &lis3_dev);
641 if (err < 0)
642 printk(KERN_ERR DRIVER_NAME
643 "No second IRQ. Limited functionality\n");
644 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700645}
646
Daniel Mackab337a62009-03-31 15:24:31 -0700647/*
648 * Initialise the accelerometer and the various subsystems.
Éric Pielbc62c142009-12-14 18:01:39 -0800649 * Should be rather independent of the bus system.
Daniel Mackab337a62009-03-31 15:24:31 -0700650 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700651int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700652{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700653 int err;
654 irq_handler_t thread_fn;
655
Daniel Macka38da2e2009-03-31 15:24:32 -0700656 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
657
658 switch (dev->whoami) {
Éric Pielbc62c142009-12-14 18:01:39 -0800659 case WAI_12B:
660 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
661 dev->read_data = lis3lv02d_read_12;
Daniel Macka38da2e2009-03-31 15:24:32 -0700662 dev->mdps_max_val = 2048;
Samu Onkalo641615a2009-12-14 18:01:41 -0800663 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800664 dev->odrs = lis3_12_rates;
665 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
Samu Onkalo32496c72009-12-14 18:01:46 -0800666 dev->scale = LIS3_SENSITIVITY_12B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700667 break;
Éric Pielbc62c142009-12-14 18:01:39 -0800668 case WAI_8B:
669 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
Daniel Macka38da2e2009-03-31 15:24:32 -0700670 dev->read_data = lis3lv02d_read_8;
671 dev->mdps_max_val = 128;
Samu Onkalo641615a2009-12-14 18:01:41 -0800672 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800673 dev->odrs = lis3_8_rates;
674 dev->odr_mask = CTRL1_DR;
Samu Onkalo32496c72009-12-14 18:01:46 -0800675 dev->scale = LIS3_SENSITIVITY_8B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700676 break;
677 default:
678 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700679 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700680 return -EINVAL;
681 }
682
Samu Onkalo2db4a762009-12-14 18:01:43 -0800683 mutex_init(&dev->mutex);
684
Daniel Macka38da2e2009-03-31 15:24:32 -0700685 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700686 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700687
688 if (lis3lv02d_joystick_enable())
689 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
690
Daniel Mack8f3128e2009-06-16 15:34:17 -0700691 /* passing in platform specific data is purely optional and only
692 * used by the SPI transport layer at the moment */
693 if (dev->pdata) {
694 struct lis3lv02d_platform_data *p = dev->pdata;
695
Samu Onkaloecc437a2010-05-24 14:33:34 -0700696 if (dev->whoami == WAI_8B)
697 lis3lv02d_8b_configure(dev, p);
Daniel Mack8873c332009-09-21 17:04:43 -0700698
Daniel Mack8f3128e2009-06-16 15:34:17 -0700699 if (p->irq_cfg)
700 dev->write(dev, CTRL_REG3, p->irq_cfg);
701 }
702
Daniel Macka38da2e2009-03-31 15:24:32 -0700703 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700704 if (!dev->irq) {
705 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700706 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700707 goto out;
708 }
709
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700710 /*
711 * The sensor can generate interrupts for free-fall and direction
712 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
713 * the things simple and _fast_ we activate it only for free-fall, so
714 * no need to read register (very slow with ACPI). For the same reason,
715 * we forbid shared interrupts.
716 *
717 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
718 * io-apic is not configurable (and generates a warning) but I keep it
719 * in case of support for other hardware.
720 */
Takashi Iwaif7c77a32010-09-23 10:01:11 -0700721 if (dev->pdata && dev->whoami == WAI_8B)
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700722 thread_fn = lis302dl_interrupt_thread1_8b;
723 else
724 thread_fn = NULL;
725
726 err = request_threaded_irq(dev->irq, lis302dl_interrupt,
727 thread_fn,
728 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
729 DRIVER_NAME, &lis3_dev);
730
731 if (err < 0) {
732 printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
733 goto out;
734 }
735
Daniel Mackab337a62009-03-31 15:24:31 -0700736 if (misc_register(&lis3lv02d_misc_device))
737 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
738out:
Daniel Mackab337a62009-03-31 15:24:31 -0700739 return 0;
740}
741EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
742
Pavel Machek455fbdd2008-11-12 13:27:02 -0800743MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800744MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800745MODULE_LICENSE("GPL");