2 * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
4 * Copyright (C) 2007-2008 Yan Burman
5 * Copyright (C) 2008 Eric Piel
6 * Copyright (C) 2008-2009 Pavel Machek
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.
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.
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
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>
37 #include <linux/uaccess.h>
38 #include <linux/miscdevice.h>
39 #include <acpi/acpi_drivers.h>
40 #include <asm/atomic.h>
41 #include "lis3lv02d.h"
43 #define DRIVER_NAME "lis3lv02d"
45 /* joystick device poll interval in milliseconds */
46 #define MDPS_POLL_INTERVAL 50
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
56 struct acpi_lis3lv02d adev = {
57 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(adev.misc_wait),
60 EXPORT_SYMBOL_GPL(adev);
62 static int lis3lv02d_add_fs(struct acpi_device *device);
65 * lis3lv02d_get_axis - For the given axis, give the value converted
66 * @axis: 1,2,3 - can also be negative
67 * @hw_values: raw values returned by the hardware
69 * Returns the converted value.
71 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
74 return hw_values[axis - 1];
76 return -hw_values[-axis - 1];
80 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
81 * @handle: the handle to the device
82 * @x: where to store the X axis value
83 * @y: where to store the Y axis value
84 * @z: where to store the Z axis value
86 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
88 static void lis3lv02d_get_xyz(acpi_handle handle, int *x, int *y, int *z)
92 position[0] = adev.read_data(handle, OUTX);
93 position[1] = adev.read_data(handle, OUTY);
94 position[2] = adev.read_data(handle, OUTZ);
96 *x = lis3lv02d_get_axis(adev.ac.x, position);
97 *y = lis3lv02d_get_axis(adev.ac.y, position);
98 *z = lis3lv02d_get_axis(adev.ac.z, position);
101 void lis3lv02d_poweroff(acpi_handle handle)
105 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
107 void lis3lv02d_poweron(acpi_handle handle)
112 EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
115 * To be called before starting to use the device. It makes sure that the
116 * device will always be on until a call to lis3lv02d_decrease_use(). Not to be
117 * used from interrupt context.
119 static void lis3lv02d_increase_use(struct acpi_lis3lv02d *dev)
121 mutex_lock(&dev->lock);
123 if (dev->usage == 1) {
125 lis3lv02d_poweron(dev->device->handle);
127 mutex_unlock(&dev->lock);
131 * To be called whenever a usage of the device is stopped.
132 * It will make sure to turn off the device when there is not usage.
134 static void lis3lv02d_decrease_use(struct acpi_lis3lv02d *dev)
136 mutex_lock(&dev->lock);
139 lis3lv02d_poweroff(dev->device->handle);
140 mutex_unlock(&dev->lock);
143 static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
146 * Be careful: on some HP laptops the bios force DD when on battery and
147 * the lid is closed. This leads to interrupts as soon as a little move
150 atomic_inc(&adev.count);
152 wake_up_interruptible(&adev.misc_wait);
153 kill_fasync(&adev.async_queue, SIGIO, POLL_IN);
157 static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
161 if (test_and_set_bit(0, &adev.misc_opened))
162 return -EBUSY; /* already open */
164 atomic_set(&adev.count, 0);
167 * The sensor can generate interrupts for free-fall and direction
168 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
169 * the things simple and _fast_ we activate it only for free-fall, so
170 * no need to read register (very slow with ACPI). For the same reason,
171 * we forbid shared interrupts.
173 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
174 * io-apic is not configurable (and generates a warning) but I keep it
175 * in case of support for other hardware.
177 ret = request_irq(adev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
181 clear_bit(0, &adev.misc_opened);
182 printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", adev.irq);
185 lis3lv02d_increase_use(&adev);
186 printk("lis3: registered interrupt %d\n", adev.irq);
190 static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
192 fasync_helper(-1, file, 0, &adev.async_queue);
193 lis3lv02d_decrease_use(&adev);
194 free_irq(adev.irq, &adev);
195 clear_bit(0, &adev.misc_opened); /* release the device */
199 static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
200 size_t count, loff_t *pos)
202 DECLARE_WAITQUEUE(wait, current);
204 unsigned char byte_data;
210 add_wait_queue(&adev.misc_wait, &wait);
212 set_current_state(TASK_INTERRUPTIBLE);
213 data = atomic_xchg(&adev.count, 0);
217 if (file->f_flags & O_NONBLOCK) {
222 if (signal_pending(current)) {
223 retval = -ERESTARTSYS;
235 /* make sure we are not going into copy_to_user() with
236 * TASK_INTERRUPTIBLE state */
237 set_current_state(TASK_RUNNING);
238 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
242 __set_current_state(TASK_RUNNING);
243 remove_wait_queue(&adev.misc_wait, &wait);
248 static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
250 poll_wait(file, &adev.misc_wait, wait);
251 if (atomic_read(&adev.count))
252 return POLLIN | POLLRDNORM;
256 static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
258 return fasync_helper(fd, file, on, &adev.async_queue);
261 static const struct file_operations lis3lv02d_misc_fops = {
262 .owner = THIS_MODULE,
264 .read = lis3lv02d_misc_read,
265 .open = lis3lv02d_misc_open,
266 .release = lis3lv02d_misc_release,
267 .poll = lis3lv02d_misc_poll,
268 .fasync = lis3lv02d_misc_fasync,
271 static struct miscdevice lis3lv02d_misc_device = {
272 .minor = MISC_DYNAMIC_MINOR,
274 .fops = &lis3lv02d_misc_fops,
278 * lis3lv02d_joystick_kthread - Kthread polling function
279 * @data: unused - here to conform to threadfn prototype
281 static int lis3lv02d_joystick_kthread(void *data)
285 while (!kthread_should_stop()) {
286 lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
287 input_report_abs(adev.idev, ABS_X, x - adev.xcalib);
288 input_report_abs(adev.idev, ABS_Y, y - adev.ycalib);
289 input_report_abs(adev.idev, ABS_Z, z - adev.zcalib);
291 input_sync(adev.idev);
294 msleep_interruptible(MDPS_POLL_INTERVAL);
300 static int lis3lv02d_joystick_open(struct input_dev *input)
302 lis3lv02d_increase_use(&adev);
303 adev.kthread = kthread_run(lis3lv02d_joystick_kthread, NULL, "klis3lv02d");
304 if (IS_ERR(adev.kthread)) {
305 lis3lv02d_decrease_use(&adev);
306 return PTR_ERR(adev.kthread);
312 static void lis3lv02d_joystick_close(struct input_dev *input)
314 kthread_stop(adev.kthread);
315 lis3lv02d_decrease_use(&adev);
318 static inline void lis3lv02d_calibrate_joystick(void)
320 lis3lv02d_get_xyz(adev.device->handle, &adev.xcalib, &adev.ycalib, &adev.zcalib);
323 int lis3lv02d_joystick_enable(void)
330 adev.idev = input_allocate_device();
334 lis3lv02d_calibrate_joystick();
336 adev.idev->name = "ST LIS3LV02DL Accelerometer";
337 adev.idev->phys = DRIVER_NAME "/input0";
338 adev.idev->id.bustype = BUS_HOST;
339 adev.idev->id.vendor = 0;
340 adev.idev->dev.parent = &adev.pdev->dev;
341 adev.idev->open = lis3lv02d_joystick_open;
342 adev.idev->close = lis3lv02d_joystick_close;
344 set_bit(EV_ABS, adev.idev->evbit);
345 input_set_abs_params(adev.idev, ABS_X, -adev.mdps_max_val, adev.mdps_max_val, 3, 3);
346 input_set_abs_params(adev.idev, ABS_Y, -adev.mdps_max_val, adev.mdps_max_val, 3, 3);
347 input_set_abs_params(adev.idev, ABS_Z, -adev.mdps_max_val, adev.mdps_max_val, 3, 3);
349 err = input_register_device(adev.idev);
351 input_free_device(adev.idev);
357 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
359 void lis3lv02d_joystick_disable(void)
364 misc_deregister(&lis3lv02d_misc_device);
365 input_unregister_device(adev.idev);
368 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
371 * Initialise the accelerometer and the various subsystems.
372 * Should be rather independant of the bus system.
374 int lis3lv02d_init_device(struct acpi_lis3lv02d *dev)
376 mutex_init(&dev->lock);
377 lis3lv02d_add_fs(dev->device);
378 lis3lv02d_increase_use(dev);
380 if (lis3lv02d_joystick_enable())
381 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
383 printk("lis3_init_device: irq %d\n", dev->irq);
385 /* if we did not get an IRQ from ACPI - we have nothing more to do */
387 printk(KERN_ERR DRIVER_NAME
388 ": No IRQ in ACPI. Disabling /dev/freefall\n");
392 printk("lis3: registering device\n");
393 if (misc_register(&lis3lv02d_misc_device))
394 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
396 lis3lv02d_decrease_use(dev);
399 EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
402 static ssize_t lis3lv02d_position_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
407 lis3lv02d_increase_use(&adev);
408 lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
409 lis3lv02d_decrease_use(&adev);
410 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
413 static ssize_t lis3lv02d_calibrate_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 return sprintf(buf, "(%d,%d,%d)\n", adev.xcalib, adev.ycalib, adev.zcalib);
419 static ssize_t lis3lv02d_calibrate_store(struct device *dev,
420 struct device_attribute *attr,
421 const char *buf, size_t count)
423 lis3lv02d_increase_use(&adev);
424 lis3lv02d_calibrate_joystick();
425 lis3lv02d_decrease_use(&adev);
429 /* conversion btw sampling rate and the register values */
430 static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
431 static ssize_t lis3lv02d_rate_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
437 lis3lv02d_increase_use(&adev);
438 adev.read(adev.device->handle, CTRL_REG1, &ctrl);
439 lis3lv02d_decrease_use(&adev);
440 val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
441 return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
444 static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
445 static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
446 lis3lv02d_calibrate_store);
447 static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
449 static struct attribute *lis3lv02d_attributes[] = {
450 &dev_attr_position.attr,
451 &dev_attr_calibrate.attr,
456 static struct attribute_group lis3lv02d_attribute_group = {
457 .attrs = lis3lv02d_attributes
461 static int lis3lv02d_add_fs(struct acpi_device *device)
463 adev.pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
464 if (IS_ERR(adev.pdev))
465 return PTR_ERR(adev.pdev);
467 return sysfs_create_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
470 int lis3lv02d_remove_fs(void)
472 sysfs_remove_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
473 platform_device_unregister(adev.pdev);
476 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
478 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
479 MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
480 MODULE_LICENSE("GPL");