blob: 3afa3afc77f38d06dde0483cadb6f835a4b70053 [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>
30#include <linux/input.h>
31#include <linux/kthread.h>
32#include <linux/semaphore.h>
33#include <linux/delay.h>
34#include <linux/wait.h>
35#include <linux/poll.h>
36#include <linux/freezer.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080037#include <linux/uaccess.h>
Pavel Machekef2cfc72009-02-18 14:48:23 -080038#include <linux/miscdevice.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080039#include <acpi/acpi_drivers.h>
40#include <asm/atomic.h>
41#include "lis3lv02d.h"
42
43#define DRIVER_NAME "lis3lv02d"
Pavel Machek455fbdd2008-11-12 13:27:02 -080044
45/* joystick device poll interval in milliseconds */
46#define MDPS_POLL_INTERVAL 50
47/*
48 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
49 * because their are generated even if the data do not change. So it's better
50 * to keep the interrupt for the free-fall event. The values are updated at
51 * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
52 * some low processor, we poll the sensor only at 20Hz... enough for the
53 * joystick.
54 */
55
56/* Maximum value our axis may get for the input device (signed 12 bits) */
57#define MDPS_MAX_VAL 2048
58
Pavel Machekef2cfc72009-02-18 14:48:23 -080059struct acpi_lis3lv02d adev = {
60 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(adev.misc_wait),
61};
62
Eric Pielcfce41a2009-01-09 16:41:01 -080063EXPORT_SYMBOL_GPL(adev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080064
Pavel Machek455fbdd2008-11-12 13:27:02 -080065static int lis3lv02d_add_fs(struct acpi_device *device);
66
Pavel Machek455fbdd2008-11-12 13:27:02 -080067static s16 lis3lv02d_read_16(acpi_handle handle, int reg)
68{
69 u8 lo, hi;
70
Eric Pielcfce41a2009-01-09 16:41:01 -080071 adev.read(handle, reg, &lo);
72 adev.read(handle, reg + 1, &hi);
Pavel Machek455fbdd2008-11-12 13:27:02 -080073 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
74 return (s16)((hi << 8) | lo);
75}
76
77/**
78 * lis3lv02d_get_axis - For the given axis, give the value converted
79 * @axis: 1,2,3 - can also be negative
80 * @hw_values: raw values returned by the hardware
81 *
82 * Returns the converted value.
83 */
84static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
85{
86 if (axis > 0)
87 return hw_values[axis - 1];
88 else
89 return -hw_values[-axis - 1];
90}
91
92/**
93 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
94 * @handle: the handle to the device
95 * @x: where to store the X axis value
96 * @y: where to store the Y axis value
97 * @z: where to store the Z axis value
98 *
99 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
100 */
101static void lis3lv02d_get_xyz(acpi_handle handle, int *x, int *y, int *z)
102{
103 int position[3];
104
105 position[0] = lis3lv02d_read_16(handle, OUTX_L);
106 position[1] = lis3lv02d_read_16(handle, OUTY_L);
107 position[2] = lis3lv02d_read_16(handle, OUTZ_L);
108
109 *x = lis3lv02d_get_axis(adev.ac.x, position);
110 *y = lis3lv02d_get_axis(adev.ac.y, position);
111 *z = lis3lv02d_get_axis(adev.ac.z, position);
112}
113
Eric Pielcfce41a2009-01-09 16:41:01 -0800114void lis3lv02d_poweroff(acpi_handle handle)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800115{
116 adev.is_on = 0;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800117}
Eric Pielcfce41a2009-01-09 16:41:01 -0800118EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800119
Eric Pielcfce41a2009-01-09 16:41:01 -0800120void lis3lv02d_poweron(acpi_handle handle)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800121{
Pavel Machek455fbdd2008-11-12 13:27:02 -0800122 adev.is_on = 1;
Eric Pielcfce41a2009-01-09 16:41:01 -0800123 adev.init(handle);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800124}
Eric Pielcfce41a2009-01-09 16:41:01 -0800125EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800126
127/*
128 * To be called before starting to use the device. It makes sure that the
129 * device will always be on until a call to lis3lv02d_decrease_use(). Not to be
130 * used from interrupt context.
131 */
132static void lis3lv02d_increase_use(struct acpi_lis3lv02d *dev)
133{
134 mutex_lock(&dev->lock);
135 dev->usage++;
136 if (dev->usage == 1) {
137 if (!dev->is_on)
138 lis3lv02d_poweron(dev->device->handle);
139 }
140 mutex_unlock(&dev->lock);
141}
142
143/*
144 * To be called whenever a usage of the device is stopped.
145 * It will make sure to turn off the device when there is not usage.
146 */
147static void lis3lv02d_decrease_use(struct acpi_lis3lv02d *dev)
148{
149 mutex_lock(&dev->lock);
150 dev->usage--;
151 if (dev->usage == 0)
152 lis3lv02d_poweroff(dev->device->handle);
153 mutex_unlock(&dev->lock);
154}
155
Pavel Machekef2cfc72009-02-18 14:48:23 -0800156static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
157{
158 /*
159 * Be careful: on some HP laptops the bios force DD when on battery and
160 * the lid is closed. This leads to interrupts as soon as a little move
161 * is done.
162 */
163 atomic_inc(&adev.count);
164
165 wake_up_interruptible(&adev.misc_wait);
166 kill_fasync(&adev.async_queue, SIGIO, POLL_IN);
167 return IRQ_HANDLED;
168}
169
170static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
171{
172 int ret;
173
174 if (test_and_set_bit(0, &adev.misc_opened))
175 return -EBUSY; /* already open */
176
177 atomic_set(&adev.count, 0);
178
179 /*
180 * The sensor can generate interrupts for free-fall and direction
181 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
182 * the things simple and _fast_ we activate it only for free-fall, so
183 * no need to read register (very slow with ACPI). For the same reason,
184 * we forbid shared interrupts.
185 *
186 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
187 * io-apic is not configurable (and generates a warning) but I keep it
188 * in case of support for other hardware.
189 */
190 ret = request_irq(adev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
191 DRIVER_NAME, &adev);
192
193 if (ret) {
194 clear_bit(0, &adev.misc_opened);
195 printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", adev.irq);
196 return -EBUSY;
197 }
198 lis3lv02d_increase_use(&adev);
199 printk("lis3: registered interrupt %d\n", adev.irq);
200 return 0;
201}
202
203static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
204{
205 fasync_helper(-1, file, 0, &adev.async_queue);
206 lis3lv02d_decrease_use(&adev);
207 free_irq(adev.irq, &adev);
208 clear_bit(0, &adev.misc_opened); /* release the device */
209 return 0;
210}
211
212static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
213 size_t count, loff_t *pos)
214{
215 DECLARE_WAITQUEUE(wait, current);
216 u32 data;
217 unsigned char byte_data;
218 ssize_t retval = 1;
219
220 if (count < 1)
221 return -EINVAL;
222
223 add_wait_queue(&adev.misc_wait, &wait);
224 while (true) {
225 set_current_state(TASK_INTERRUPTIBLE);
226 data = atomic_xchg(&adev.count, 0);
227 if (data)
228 break;
229
230 if (file->f_flags & O_NONBLOCK) {
231 retval = -EAGAIN;
232 goto out;
233 }
234
235 if (signal_pending(current)) {
236 retval = -ERESTARTSYS;
237 goto out;
238 }
239
240 schedule();
241 }
242
243 if (data < 255)
244 byte_data = data;
245 else
246 byte_data = 255;
247
248 /* make sure we are not going into copy_to_user() with
249 * TASK_INTERRUPTIBLE state */
250 set_current_state(TASK_RUNNING);
251 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
252 retval = -EFAULT;
253
254out:
255 __set_current_state(TASK_RUNNING);
256 remove_wait_queue(&adev.misc_wait, &wait);
257
258 return retval;
259}
260
261static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
262{
263 poll_wait(file, &adev.misc_wait, wait);
264 if (atomic_read(&adev.count))
265 return POLLIN | POLLRDNORM;
266 return 0;
267}
268
269static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
270{
271 return fasync_helper(fd, file, on, &adev.async_queue);
272}
273
274static const struct file_operations lis3lv02d_misc_fops = {
275 .owner = THIS_MODULE,
276 .llseek = no_llseek,
277 .read = lis3lv02d_misc_read,
278 .open = lis3lv02d_misc_open,
279 .release = lis3lv02d_misc_release,
280 .poll = lis3lv02d_misc_poll,
281 .fasync = lis3lv02d_misc_fasync,
282};
283
284static struct miscdevice lis3lv02d_misc_device = {
285 .minor = MISC_DYNAMIC_MINOR,
286 .name = "freefall",
287 .fops = &lis3lv02d_misc_fops,
288};
289
Pavel Machek455fbdd2008-11-12 13:27:02 -0800290/**
291 * lis3lv02d_joystick_kthread - Kthread polling function
292 * @data: unused - here to conform to threadfn prototype
293 */
294static int lis3lv02d_joystick_kthread(void *data)
295{
296 int x, y, z;
297
298 while (!kthread_should_stop()) {
299 lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
300 input_report_abs(adev.idev, ABS_X, x - adev.xcalib);
301 input_report_abs(adev.idev, ABS_Y, y - adev.ycalib);
302 input_report_abs(adev.idev, ABS_Z, z - adev.zcalib);
303
304 input_sync(adev.idev);
305
306 try_to_freeze();
307 msleep_interruptible(MDPS_POLL_INTERVAL);
308 }
309
310 return 0;
311}
312
313static int lis3lv02d_joystick_open(struct input_dev *input)
314{
315 lis3lv02d_increase_use(&adev);
316 adev.kthread = kthread_run(lis3lv02d_joystick_kthread, NULL, "klis3lv02d");
317 if (IS_ERR(adev.kthread)) {
318 lis3lv02d_decrease_use(&adev);
319 return PTR_ERR(adev.kthread);
320 }
321
322 return 0;
323}
324
325static void lis3lv02d_joystick_close(struct input_dev *input)
326{
327 kthread_stop(adev.kthread);
328 lis3lv02d_decrease_use(&adev);
329}
330
Pavel Machek455fbdd2008-11-12 13:27:02 -0800331static inline void lis3lv02d_calibrate_joystick(void)
332{
333 lis3lv02d_get_xyz(adev.device->handle, &adev.xcalib, &adev.ycalib, &adev.zcalib);
334}
335
Eric Pielcfce41a2009-01-09 16:41:01 -0800336int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800337{
338 int err;
339
340 if (adev.idev)
341 return -EINVAL;
342
343 adev.idev = input_allocate_device();
344 if (!adev.idev)
345 return -ENOMEM;
346
347 lis3lv02d_calibrate_joystick();
348
349 adev.idev->name = "ST LIS3LV02DL Accelerometer";
350 adev.idev->phys = DRIVER_NAME "/input0";
351 adev.idev->id.bustype = BUS_HOST;
352 adev.idev->id.vendor = 0;
353 adev.idev->dev.parent = &adev.pdev->dev;
354 adev.idev->open = lis3lv02d_joystick_open;
355 adev.idev->close = lis3lv02d_joystick_close;
356
357 set_bit(EV_ABS, adev.idev->evbit);
358 input_set_abs_params(adev.idev, ABS_X, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
359 input_set_abs_params(adev.idev, ABS_Y, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
360 input_set_abs_params(adev.idev, ABS_Z, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
361
362 err = input_register_device(adev.idev);
363 if (err) {
364 input_free_device(adev.idev);
365 adev.idev = NULL;
366 }
367
368 return err;
369}
Eric Pielcfce41a2009-01-09 16:41:01 -0800370EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800371
Eric Pielcfce41a2009-01-09 16:41:01 -0800372void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800373{
374 if (!adev.idev)
375 return;
376
Pavel Machekef2cfc72009-02-18 14:48:23 -0800377 misc_deregister(&lis3lv02d_misc_device);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800378 input_unregister_device(adev.idev);
379 adev.idev = NULL;
380}
Eric Pielcfce41a2009-01-09 16:41:01 -0800381EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800382
383/*
384 * Initialise the accelerometer and the various subsystems.
385 * Should be rather independant of the bus system.
386 */
Eric Pielcfce41a2009-01-09 16:41:01 -0800387int lis3lv02d_init_device(struct acpi_lis3lv02d *dev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800388{
389 mutex_init(&dev->lock);
390 lis3lv02d_add_fs(dev->device);
391 lis3lv02d_increase_use(dev);
392
393 if (lis3lv02d_joystick_enable())
394 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
395
Pavel Machekef2cfc72009-02-18 14:48:23 -0800396 printk("lis3_init_device: irq %d\n", dev->irq);
397
398 /* if we did not get an IRQ from ACPI - we have nothing more to do */
399 if (!dev->irq) {
400 printk(KERN_ERR DRIVER_NAME
401 ": No IRQ in ACPI. Disabling /dev/freefall\n");
402 goto out;
403 }
404
405 printk("lis3: registering device\n");
406 if (misc_register(&lis3lv02d_misc_device))
407 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
408out:
Pavel Machek455fbdd2008-11-12 13:27:02 -0800409 lis3lv02d_decrease_use(dev);
410 return 0;
411}
Eric Pielcfce41a2009-01-09 16:41:01 -0800412EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800413
414/* Sysfs stuff */
415static ssize_t lis3lv02d_position_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
418 int x, y, z;
419
420 lis3lv02d_increase_use(&adev);
421 lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
422 lis3lv02d_decrease_use(&adev);
423 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
424}
425
426static ssize_t lis3lv02d_calibrate_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
428{
429 return sprintf(buf, "(%d,%d,%d)\n", adev.xcalib, adev.ycalib, adev.zcalib);
430}
431
432static ssize_t lis3lv02d_calibrate_store(struct device *dev,
433 struct device_attribute *attr,
434 const char *buf, size_t count)
435{
436 lis3lv02d_increase_use(&adev);
437 lis3lv02d_calibrate_joystick();
438 lis3lv02d_decrease_use(&adev);
439 return count;
440}
441
442/* conversion btw sampling rate and the register values */
443static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
444static ssize_t lis3lv02d_rate_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
446{
447 u8 ctrl;
448 int val;
449
450 lis3lv02d_increase_use(&adev);
Eric Pielcfce41a2009-01-09 16:41:01 -0800451 adev.read(adev.device->handle, CTRL_REG1, &ctrl);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800452 lis3lv02d_decrease_use(&adev);
453 val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
454 return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
455}
456
457static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
458static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
459 lis3lv02d_calibrate_store);
460static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
461
462static struct attribute *lis3lv02d_attributes[] = {
463 &dev_attr_position.attr,
464 &dev_attr_calibrate.attr,
465 &dev_attr_rate.attr,
466 NULL
467};
468
469static struct attribute_group lis3lv02d_attribute_group = {
470 .attrs = lis3lv02d_attributes
471};
472
Eric Pielcfce41a2009-01-09 16:41:01 -0800473
Pavel Machek455fbdd2008-11-12 13:27:02 -0800474static int lis3lv02d_add_fs(struct acpi_device *device)
475{
476 adev.pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
477 if (IS_ERR(adev.pdev))
478 return PTR_ERR(adev.pdev);
479
480 return sysfs_create_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
481}
482
Eric Pielcfce41a2009-01-09 16:41:01 -0800483int lis3lv02d_remove_fs(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800484{
485 sysfs_remove_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
486 platform_device_unregister(adev.pdev);
487 return 0;
488}
Eric Pielcfce41a2009-01-09 16:41:01 -0800489EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800490
491MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800492MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800493MODULE_LICENSE("GPL");
494