blob: b2f2277cad3c01c714bc36634e6c23d4f56fc317 [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
Samu Onkalo2db4a762009-12-14 18:01:43 -0800124 mutex_lock(&lis3->mutex);
Eric Piela002ee82009-06-16 15:34:14 -0700125 position[0] = lis3->read_data(lis3, OUTX);
126 position[1] = lis3->read_data(lis3, OUTY);
127 position[2] = lis3->read_data(lis3, OUTZ);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800128 mutex_unlock(&lis3->mutex);
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
Pavel Machekef2cfc72009-02-18 14:48:23 -0800252static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
253{
254 /*
255 * Be careful: on some HP laptops the bios force DD when on battery and
256 * the lid is closed. This leads to interrupts as soon as a little move
257 * is done.
258 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700259 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800260
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700261 wake_up_interruptible(&lis3_dev.misc_wait);
262 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800263 return IRQ_HANDLED;
264}
265
266static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
267{
268 int ret;
269
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700270 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800271 return -EBUSY; /* already open */
272
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700273 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800274
275 /*
276 * The sensor can generate interrupts for free-fall and direction
277 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
278 * the things simple and _fast_ we activate it only for free-fall, so
279 * no need to read register (very slow with ACPI). For the same reason,
280 * we forbid shared interrupts.
281 *
282 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
283 * io-apic is not configurable (and generates a warning) but I keep it
284 * in case of support for other hardware.
285 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700286 ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
287 DRIVER_NAME, &lis3_dev);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800288
289 if (ret) {
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700290 clear_bit(0, &lis3_dev.misc_opened);
291 printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", lis3_dev.irq);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800292 return -EBUSY;
293 }
Pavel Machekef2cfc72009-02-18 14:48:23 -0800294 return 0;
295}
296
297static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
298{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700299 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700300 free_irq(lis3_dev.irq, &lis3_dev);
301 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Pavel Machekef2cfc72009-02-18 14:48:23 -0800302 return 0;
303}
304
305static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
306 size_t count, loff_t *pos)
307{
308 DECLARE_WAITQUEUE(wait, current);
309 u32 data;
310 unsigned char byte_data;
311 ssize_t retval = 1;
312
313 if (count < 1)
314 return -EINVAL;
315
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700316 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800317 while (true) {
318 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700319 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800320 if (data)
321 break;
322
323 if (file->f_flags & O_NONBLOCK) {
324 retval = -EAGAIN;
325 goto out;
326 }
327
328 if (signal_pending(current)) {
329 retval = -ERESTARTSYS;
330 goto out;
331 }
332
333 schedule();
334 }
335
336 if (data < 255)
337 byte_data = data;
338 else
339 byte_data = 255;
340
341 /* make sure we are not going into copy_to_user() with
342 * TASK_INTERRUPTIBLE state */
343 set_current_state(TASK_RUNNING);
344 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
345 retval = -EFAULT;
346
347out:
348 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700349 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800350
351 return retval;
352}
353
354static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
355{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700356 poll_wait(file, &lis3_dev.misc_wait, wait);
357 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800358 return POLLIN | POLLRDNORM;
359 return 0;
360}
361
362static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
363{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700364 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800365}
366
367static const struct file_operations lis3lv02d_misc_fops = {
368 .owner = THIS_MODULE,
369 .llseek = no_llseek,
370 .read = lis3lv02d_misc_read,
371 .open = lis3lv02d_misc_open,
372 .release = lis3lv02d_misc_release,
373 .poll = lis3lv02d_misc_poll,
374 .fasync = lis3lv02d_misc_fasync,
375};
376
377static struct miscdevice lis3lv02d_misc_device = {
378 .minor = MISC_DYNAMIC_MINOR,
379 .name = "freefall",
380 .fops = &lis3lv02d_misc_fops,
381};
382
Eric Pieldc6ea972009-06-16 15:34:15 -0700383static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800384{
385 int x, y, z;
386
Eric Pieldc6ea972009-06-16 15:34:15 -0700387 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Samu Onkalo53995412009-12-14 18:01:43 -0800388 input_report_abs(pidev->input, ABS_X, x);
389 input_report_abs(pidev->input, ABS_Y, y);
390 input_report_abs(pidev->input, ABS_Z, z);
Samu Onkalod25a8c82009-12-14 18:01:38 -0800391 input_sync(pidev->input);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800392}
393
Eric Pielcfce41a2009-01-09 16:41:01 -0800394int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800395{
Eric Pieldc6ea972009-06-16 15:34:15 -0700396 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800397 int err;
Samu Onkalo32496c72009-12-14 18:01:46 -0800398 int max_val, fuzz, flat;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800399
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700400 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800401 return -EINVAL;
402
Eric Pieldc6ea972009-06-16 15:34:15 -0700403 lis3_dev.idev = input_allocate_polled_device();
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700404 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800405 return -ENOMEM;
406
Eric Pieldc6ea972009-06-16 15:34:15 -0700407 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
408 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
409 input_dev = lis3_dev.idev->input;
410
Eric Pieldc6ea972009-06-16 15:34:15 -0700411 input_dev->name = "ST LIS3LV02DL Accelerometer";
412 input_dev->phys = DRIVER_NAME "/input0";
413 input_dev->id.bustype = BUS_HOST;
414 input_dev->id.vendor = 0;
415 input_dev->dev.parent = &lis3_dev.pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800416
Eric Pieldc6ea972009-06-16 15:34:15 -0700417 set_bit(EV_ABS, input_dev->evbit);
Samu Onkalo32496c72009-12-14 18:01:46 -0800418 max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
419 fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
420 flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
421 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
422 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
423 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800424
Eric Pieldc6ea972009-06-16 15:34:15 -0700425 err = input_register_polled_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800426 if (err) {
Eric Pieldc6ea972009-06-16 15:34:15 -0700427 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700428 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800429 }
430
431 return err;
432}
Eric Pielcfce41a2009-01-09 16:41:01 -0800433EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800434
Eric Pielcfce41a2009-01-09 16:41:01 -0800435void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800436{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700437 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800438 return;
439
Eric Pielc2884242009-06-16 15:34:13 -0700440 if (lis3_dev.irq)
441 misc_deregister(&lis3lv02d_misc_device);
Eric Pieldc6ea972009-06-16 15:34:15 -0700442 input_unregister_polled_device(lis3_dev.idev);
Samu Onkalo66c85692009-12-14 18:01:39 -0800443 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700444 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800445}
Eric Pielcfce41a2009-01-09 16:41:01 -0800446EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800447
Pavel Machek455fbdd2008-11-12 13:27:02 -0800448/* Sysfs stuff */
Samu Onkalo2db4a762009-12-14 18:01:43 -0800449static ssize_t lis3lv02d_selftest_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
452 int result;
453 s16 values[3];
454
455 result = lis3lv02d_selftest(&lis3_dev, values);
456 return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
457 values[0], values[1], values[2]);
458}
459
Pavel Machek455fbdd2008-11-12 13:27:02 -0800460static ssize_t lis3lv02d_position_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
463 int x, y, z;
464
Daniel Macka38da2e2009-03-31 15:24:32 -0700465 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800466 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
467}
468
Pavel Machek455fbdd2008-11-12 13:27:02 -0800469static ssize_t lis3lv02d_rate_show(struct device *dev,
470 struct device_attribute *attr, char *buf)
471{
Samu Onkalo641615a2009-12-14 18:01:41 -0800472 return sprintf(buf, "%d\n", lis3lv02d_get_odr());
Pavel Machek455fbdd2008-11-12 13:27:02 -0800473}
474
Samu Onkaloa253aae2009-12-14 18:01:44 -0800475static ssize_t lis3lv02d_rate_set(struct device *dev,
476 struct device_attribute *attr, const char *buf,
477 size_t count)
478{
479 unsigned long rate;
480
481 if (strict_strtoul(buf, 0, &rate))
482 return -EINVAL;
483
484 if (lis3lv02d_set_odr(rate))
485 return -EINVAL;
486
487 return count;
488}
489
Samu Onkalo2db4a762009-12-14 18:01:43 -0800490static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800491static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800492static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
493 lis3lv02d_rate_set);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800494
495static struct attribute *lis3lv02d_attributes[] = {
Samu Onkalo2db4a762009-12-14 18:01:43 -0800496 &dev_attr_selftest.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800497 &dev_attr_position.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800498 &dev_attr_rate.attr,
499 NULL
500};
501
502static struct attribute_group lis3lv02d_attribute_group = {
503 .attrs = lis3lv02d_attributes
504};
505
Eric Pielcfce41a2009-01-09 16:41:01 -0800506
Daniel Macka38da2e2009-03-31 15:24:32 -0700507static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800508{
Eric Piela002ee82009-06-16 15:34:14 -0700509 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
510 if (IS_ERR(lis3->pdev))
511 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800512
Eric Piela002ee82009-06-16 15:34:14 -0700513 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800514}
515
Eric Piela002ee82009-06-16 15:34:14 -0700516int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800517{
Eric Piela002ee82009-06-16 15:34:14 -0700518 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
519 platform_device_unregister(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800520 return 0;
521}
Eric Pielcfce41a2009-01-09 16:41:01 -0800522EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800523
Daniel Mackab337a62009-03-31 15:24:31 -0700524/*
525 * Initialise the accelerometer and the various subsystems.
Éric Pielbc62c142009-12-14 18:01:39 -0800526 * Should be rather independent of the bus system.
Daniel Mackab337a62009-03-31 15:24:31 -0700527 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700528int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700529{
Daniel Macka38da2e2009-03-31 15:24:32 -0700530 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
531
532 switch (dev->whoami) {
Éric Pielbc62c142009-12-14 18:01:39 -0800533 case WAI_12B:
534 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
535 dev->read_data = lis3lv02d_read_12;
Daniel Macka38da2e2009-03-31 15:24:32 -0700536 dev->mdps_max_val = 2048;
Samu Onkalo641615a2009-12-14 18:01:41 -0800537 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800538 dev->odrs = lis3_12_rates;
539 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
Samu Onkalo32496c72009-12-14 18:01:46 -0800540 dev->scale = LIS3_SENSITIVITY_12B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700541 break;
Éric Pielbc62c142009-12-14 18:01:39 -0800542 case WAI_8B:
543 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
Daniel Macka38da2e2009-03-31 15:24:32 -0700544 dev->read_data = lis3lv02d_read_8;
545 dev->mdps_max_val = 128;
Samu Onkalo641615a2009-12-14 18:01:41 -0800546 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800547 dev->odrs = lis3_8_rates;
548 dev->odr_mask = CTRL1_DR;
Samu Onkalo32496c72009-12-14 18:01:46 -0800549 dev->scale = LIS3_SENSITIVITY_8B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700550 break;
551 default:
552 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700553 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700554 return -EINVAL;
555 }
556
Samu Onkalo2db4a762009-12-14 18:01:43 -0800557 mutex_init(&dev->mutex);
558
Daniel Macka38da2e2009-03-31 15:24:32 -0700559 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700560 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700561
562 if (lis3lv02d_joystick_enable())
563 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
564
Daniel Mack8f3128e2009-06-16 15:34:17 -0700565 /* passing in platform specific data is purely optional and only
566 * used by the SPI transport layer at the moment */
567 if (dev->pdata) {
568 struct lis3lv02d_platform_data *p = dev->pdata;
569
Éric Pielbc62c142009-12-14 18:01:39 -0800570 if (p->click_flags && (dev->whoami == WAI_8B)) {
Daniel Mack8f3128e2009-06-16 15:34:17 -0700571 dev->write(dev, CLICK_CFG, p->click_flags);
572 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
573 dev->write(dev, CLICK_LATENCY, p->click_latency);
574 dev->write(dev, CLICK_WINDOW, p->click_window);
575 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
576 dev->write(dev, CLICK_THSY_X,
577 (p->click_thresh_x & 0xf) |
578 (p->click_thresh_y << 4));
579 }
580
Éric Pielbc62c142009-12-14 18:01:39 -0800581 if (p->wakeup_flags && (dev->whoami == WAI_8B)) {
Daniel Mack8873c332009-09-21 17:04:43 -0700582 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
583 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
584 /* default to 2.5ms for now */
585 dev->write(dev, FF_WU_DURATION_1, 1);
586 /* enable high pass filter for both free-fall units */
587 dev->write(dev, CTRL_REG2, HP_FF_WU1 | HP_FF_WU2);
588 }
589
Daniel Mack8f3128e2009-06-16 15:34:17 -0700590 if (p->irq_cfg)
591 dev->write(dev, CTRL_REG3, p->irq_cfg);
592 }
593
Daniel Macka38da2e2009-03-31 15:24:32 -0700594 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700595 if (!dev->irq) {
596 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700597 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700598 goto out;
599 }
600
Daniel Mackab337a62009-03-31 15:24:31 -0700601 if (misc_register(&lis3lv02d_misc_device))
602 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
603out:
Daniel Mackab337a62009-03-31 15:24:31 -0700604 return 0;
605}
606EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
607
Pavel Machek455fbdd2008-11-12 13:27:02 -0800608MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800609MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800610MODULE_LICENSE("GPL");