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