2 * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
4 * Copyright (C) 2007-2008 Yan Burman
5 * Copyright (C) 2008 Eric Piel
6 * Copyright (C) 2008 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 <acpi/acpi_drivers.h>
39 #include <asm/atomic.h>
40 #include "lis3lv02d.h"
42 #define DRIVER_NAME "lis3lv02d"
44 /* joystick device poll interval in milliseconds */
45 #define MDPS_POLL_INTERVAL 50
47 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
48 * because their are generated even if the data do not change. So it's better
49 * 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
55 /* Maximum value our axis may get for the input device (signed 12 bits) */
56 #define MDPS_MAX_VAL 2048
58 struct acpi_lis3lv02d adev;
59 EXPORT_SYMBOL_GPL(adev);
61 static int lis3lv02d_add_fs(struct acpi_device *device);
63 static s16 lis3lv02d_read_16(acpi_handle handle, int reg)
67 adev.read(handle, reg, &lo);
68 adev.read(handle, reg + 1, &hi);
69 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
70 return (s16)((hi << 8) | lo);
74 * lis3lv02d_get_axis - For the given axis, give the value converted
75 * @axis: 1,2,3 - can also be negative
76 * @hw_values: raw values returned by the hardware
78 * Returns the converted value.
80 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
83 return hw_values[axis - 1];
85 return -hw_values[-axis - 1];
89 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
90 * @handle: the handle to the device
91 * @x: where to store the X axis value
92 * @y: where to store the Y axis value
93 * @z: where to store the Z axis value
95 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
97 static void lis3lv02d_get_xyz(acpi_handle handle, int *x, int *y, int *z)
101 position[0] = lis3lv02d_read_16(handle, OUTX_L);
102 position[1] = lis3lv02d_read_16(handle, OUTY_L);
103 position[2] = lis3lv02d_read_16(handle, OUTZ_L);
105 *x = lis3lv02d_get_axis(adev.ac.x, position);
106 *y = lis3lv02d_get_axis(adev.ac.y, position);
107 *z = lis3lv02d_get_axis(adev.ac.z, position);
110 void lis3lv02d_poweroff(acpi_handle handle)
113 /* disable X,Y,Z axis and power down */
114 adev.write(handle, CTRL_REG1, 0x00);
116 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
118 void lis3lv02d_poweron(acpi_handle handle)
124 adev.write(handle, FF_WU_CFG, 0);
126 * BDU: LSB and MSB values are not updated until both have been read.
127 * So the value read will always be correct.
128 * IEN: Interrupt for free-fall and DD, not for data-ready.
130 adev.read(handle, CTRL_REG2, &val);
131 val |= CTRL2_BDU | CTRL2_IEN;
132 adev.write(handle, CTRL_REG2, val);
134 EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
137 * To be called before starting to use the device. It makes sure that the
138 * device will always be on until a call to lis3lv02d_decrease_use(). Not to be
139 * used from interrupt context.
141 static void lis3lv02d_increase_use(struct acpi_lis3lv02d *dev)
143 mutex_lock(&dev->lock);
145 if (dev->usage == 1) {
147 lis3lv02d_poweron(dev->device->handle);
149 mutex_unlock(&dev->lock);
153 * To be called whenever a usage of the device is stopped.
154 * It will make sure to turn off the device when there is not usage.
156 static void lis3lv02d_decrease_use(struct acpi_lis3lv02d *dev)
158 mutex_lock(&dev->lock);
161 lis3lv02d_poweroff(dev->device->handle);
162 mutex_unlock(&dev->lock);
166 * lis3lv02d_joystick_kthread - Kthread polling function
167 * @data: unused - here to conform to threadfn prototype
169 static int lis3lv02d_joystick_kthread(void *data)
173 while (!kthread_should_stop()) {
174 lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
175 input_report_abs(adev.idev, ABS_X, x - adev.xcalib);
176 input_report_abs(adev.idev, ABS_Y, y - adev.ycalib);
177 input_report_abs(adev.idev, ABS_Z, z - adev.zcalib);
179 input_sync(adev.idev);
182 msleep_interruptible(MDPS_POLL_INTERVAL);
188 static int lis3lv02d_joystick_open(struct input_dev *input)
190 lis3lv02d_increase_use(&adev);
191 adev.kthread = kthread_run(lis3lv02d_joystick_kthread, NULL, "klis3lv02d");
192 if (IS_ERR(adev.kthread)) {
193 lis3lv02d_decrease_use(&adev);
194 return PTR_ERR(adev.kthread);
200 static void lis3lv02d_joystick_close(struct input_dev *input)
202 kthread_stop(adev.kthread);
203 lis3lv02d_decrease_use(&adev);
207 static inline void lis3lv02d_calibrate_joystick(void)
209 lis3lv02d_get_xyz(adev.device->handle, &adev.xcalib, &adev.ycalib, &adev.zcalib);
212 int lis3lv02d_joystick_enable(void)
219 adev.idev = input_allocate_device();
223 lis3lv02d_calibrate_joystick();
225 adev.idev->name = "ST LIS3LV02DL Accelerometer";
226 adev.idev->phys = DRIVER_NAME "/input0";
227 adev.idev->id.bustype = BUS_HOST;
228 adev.idev->id.vendor = 0;
229 adev.idev->dev.parent = &adev.pdev->dev;
230 adev.idev->open = lis3lv02d_joystick_open;
231 adev.idev->close = lis3lv02d_joystick_close;
233 set_bit(EV_ABS, adev.idev->evbit);
234 input_set_abs_params(adev.idev, ABS_X, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
235 input_set_abs_params(adev.idev, ABS_Y, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
236 input_set_abs_params(adev.idev, ABS_Z, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);
238 err = input_register_device(adev.idev);
240 input_free_device(adev.idev);
246 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
248 void lis3lv02d_joystick_disable(void)
253 input_unregister_device(adev.idev);
256 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
259 * Initialise the accelerometer and the various subsystems.
260 * Should be rather independant of the bus system.
262 int lis3lv02d_init_device(struct acpi_lis3lv02d *dev)
264 mutex_init(&dev->lock);
265 lis3lv02d_add_fs(dev->device);
266 lis3lv02d_increase_use(dev);
268 if (lis3lv02d_joystick_enable())
269 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
271 lis3lv02d_decrease_use(dev);
274 EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
277 static ssize_t lis3lv02d_position_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
282 lis3lv02d_increase_use(&adev);
283 lis3lv02d_get_xyz(adev.device->handle, &x, &y, &z);
284 lis3lv02d_decrease_use(&adev);
285 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
288 static ssize_t lis3lv02d_calibrate_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
291 return sprintf(buf, "(%d,%d,%d)\n", adev.xcalib, adev.ycalib, adev.zcalib);
294 static ssize_t lis3lv02d_calibrate_store(struct device *dev,
295 struct device_attribute *attr,
296 const char *buf, size_t count)
298 lis3lv02d_increase_use(&adev);
299 lis3lv02d_calibrate_joystick();
300 lis3lv02d_decrease_use(&adev);
304 /* conversion btw sampling rate and the register values */
305 static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
306 static ssize_t lis3lv02d_rate_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
312 lis3lv02d_increase_use(&adev);
313 adev.read(adev.device->handle, CTRL_REG1, &ctrl);
314 lis3lv02d_decrease_use(&adev);
315 val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
316 return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
319 static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
320 static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
321 lis3lv02d_calibrate_store);
322 static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
324 static struct attribute *lis3lv02d_attributes[] = {
325 &dev_attr_position.attr,
326 &dev_attr_calibrate.attr,
331 static struct attribute_group lis3lv02d_attribute_group = {
332 .attrs = lis3lv02d_attributes
336 static int lis3lv02d_add_fs(struct acpi_device *device)
338 adev.pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
339 if (IS_ERR(adev.pdev))
340 return PTR_ERR(adev.pdev);
342 return sysfs_create_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
345 int lis3lv02d_remove_fs(void)
347 sysfs_remove_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
348 platform_device_unregister(adev.pdev);
351 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
353 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
354 MODULE_AUTHOR("Yan Burman and Eric Piel");
355 MODULE_LICENSE("GPL");