LIS3LV02D: separate the core from HP ACPI API
[pandora-kernel.git] / drivers / hwmon / lis3lv02d.c
1 /*
2  *  lis3lv02d.c - ST LIS3LV02DL accelerometer driver
3  *
4  *  Copyright (C) 2007-2008 Yan Burman
5  *  Copyright (C) 2008 Eric Piel
6  *  Copyright (C) 2008 Pavel Machek
7  *
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>
37 #include <linux/uaccess.h>
38 #include <acpi/acpi_drivers.h>
39 #include <asm/atomic.h>
40 #include "lis3lv02d.h"
41
42 #define DRIVER_NAME     "lis3lv02d"
43
44 /* joystick device poll interval in milliseconds */
45 #define MDPS_POLL_INTERVAL 50
46 /*
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
52  * joystick.
53  */
54
55 /* Maximum value our axis may get for the input device (signed 12 bits) */
56 #define MDPS_MAX_VAL 2048
57
58 struct acpi_lis3lv02d adev;
59 EXPORT_SYMBOL_GPL(adev);
60
61 static int lis3lv02d_add_fs(struct acpi_device *device);
62
63 static s16 lis3lv02d_read_16(acpi_handle handle, int reg)
64 {
65         u8 lo, hi;
66
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);
71 }
72
73 /**
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
77  *
78  * Returns the converted value.
79  */
80 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
81 {
82         if (axis > 0)
83                 return hw_values[axis - 1];
84         else
85                 return -hw_values[-axis - 1];
86 }
87
88 /**
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
94  *
95  * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
96  */
97 static void lis3lv02d_get_xyz(acpi_handle handle, int *x, int *y, int *z)
98 {
99         int position[3];
100
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);
104
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);
108 }
109
110 void lis3lv02d_poweroff(acpi_handle handle)
111 {
112         adev.is_on = 0;
113         /* disable X,Y,Z axis and power down */
114         adev.write(handle, CTRL_REG1, 0x00);
115 }
116 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
117
118 void lis3lv02d_poweron(acpi_handle handle)
119 {
120         u8 val;
121
122         adev.is_on = 1;
123         adev.init(handle);
124         adev.write(handle, FF_WU_CFG, 0);
125         /*
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.
129          */
130         adev.read(handle, CTRL_REG2, &val);
131         val |= CTRL2_BDU | CTRL2_IEN;
132         adev.write(handle, CTRL_REG2, val);
133 }
134 EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
135
136 /*
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.
140  */
141 static void lis3lv02d_increase_use(struct acpi_lis3lv02d *dev)
142 {
143         mutex_lock(&dev->lock);
144         dev->usage++;
145         if (dev->usage == 1) {
146                 if (!dev->is_on)
147                         lis3lv02d_poweron(dev->device->handle);
148         }
149         mutex_unlock(&dev->lock);
150 }
151
152 /*
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.
155  */
156 static void lis3lv02d_decrease_use(struct acpi_lis3lv02d *dev)
157 {
158         mutex_lock(&dev->lock);
159         dev->usage--;
160         if (dev->usage == 0)
161                 lis3lv02d_poweroff(dev->device->handle);
162         mutex_unlock(&dev->lock);
163 }
164
165 /**
166  * lis3lv02d_joystick_kthread - Kthread polling function
167  * @data: unused - here to conform to threadfn prototype
168  */
169 static int lis3lv02d_joystick_kthread(void *data)
170 {
171         int x, y, z;
172
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);
178
179                 input_sync(adev.idev);
180
181                 try_to_freeze();
182                 msleep_interruptible(MDPS_POLL_INTERVAL);
183         }
184
185         return 0;
186 }
187
188 static int lis3lv02d_joystick_open(struct input_dev *input)
189 {
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);
195         }
196
197         return 0;
198 }
199
200 static void lis3lv02d_joystick_close(struct input_dev *input)
201 {
202         kthread_stop(adev.kthread);
203         lis3lv02d_decrease_use(&adev);
204 }
205
206
207 static inline void lis3lv02d_calibrate_joystick(void)
208 {
209         lis3lv02d_get_xyz(adev.device->handle, &adev.xcalib, &adev.ycalib, &adev.zcalib);
210 }
211
212 int lis3lv02d_joystick_enable(void)
213 {
214         int err;
215
216         if (adev.idev)
217                 return -EINVAL;
218
219         adev.idev = input_allocate_device();
220         if (!adev.idev)
221                 return -ENOMEM;
222
223         lis3lv02d_calibrate_joystick();
224
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;
232
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);
237
238         err = input_register_device(adev.idev);
239         if (err) {
240                 input_free_device(adev.idev);
241                 adev.idev = NULL;
242         }
243
244         return err;
245 }
246 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
247
248 void lis3lv02d_joystick_disable(void)
249 {
250         if (!adev.idev)
251                 return;
252
253         input_unregister_device(adev.idev);
254         adev.idev = NULL;
255 }
256 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
257
258 /*
259  * Initialise the accelerometer and the various subsystems.
260  * Should be rather independant of the bus system.
261  */
262 int lis3lv02d_init_device(struct acpi_lis3lv02d *dev)
263 {
264         mutex_init(&dev->lock);
265         lis3lv02d_add_fs(dev->device);
266         lis3lv02d_increase_use(dev);
267
268         if (lis3lv02d_joystick_enable())
269                 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
270
271         lis3lv02d_decrease_use(dev);
272         return 0;
273 }
274 EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
275
276 /* Sysfs stuff */
277 static ssize_t lis3lv02d_position_show(struct device *dev,
278                                 struct device_attribute *attr, char *buf)
279 {
280         int x, y, z;
281
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);
286 }
287
288 static ssize_t lis3lv02d_calibrate_show(struct device *dev,
289                                 struct device_attribute *attr, char *buf)
290 {
291         return sprintf(buf, "(%d,%d,%d)\n", adev.xcalib, adev.ycalib, adev.zcalib);
292 }
293
294 static ssize_t lis3lv02d_calibrate_store(struct device *dev,
295                                 struct device_attribute *attr,
296                                 const char *buf, size_t count)
297 {
298         lis3lv02d_increase_use(&adev);
299         lis3lv02d_calibrate_joystick();
300         lis3lv02d_decrease_use(&adev);
301         return count;
302 }
303
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)
308 {
309         u8 ctrl;
310         int val;
311
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]);
317 }
318
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);
323
324 static struct attribute *lis3lv02d_attributes[] = {
325         &dev_attr_position.attr,
326         &dev_attr_calibrate.attr,
327         &dev_attr_rate.attr,
328         NULL
329 };
330
331 static struct attribute_group lis3lv02d_attribute_group = {
332         .attrs = lis3lv02d_attributes
333 };
334
335
336 static int lis3lv02d_add_fs(struct acpi_device *device)
337 {
338         adev.pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
339         if (IS_ERR(adev.pdev))
340                 return PTR_ERR(adev.pdev);
341
342         return sysfs_create_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
343 }
344
345 int lis3lv02d_remove_fs(void)
346 {
347         sysfs_remove_group(&adev.pdev->dev.kobj, &lis3lv02d_attribute_group);
348         platform_device_unregister(adev.pdev);
349         return 0;
350 }
351 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
352
353 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
354 MODULE_AUTHOR("Yan Burman and Eric Piel");
355 MODULE_LICENSE("GPL");
356