hp_accel: adev is poor name of exported symbol
[deliverable/linux.git] / drivers / hwmon / lis3lv02d.c
CommitLineData
455fbdd3
PM
1/*
2 * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
3 *
4 * Copyright (C) 2007-2008 Yan Burman
5 * Copyright (C) 2008 Eric Piel
ef2cfc79 6 * Copyright (C) 2008-2009 Pavel Machek
455fbdd3
PM
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>
455fbdd3 37#include <linux/uaccess.h>
ef2cfc79 38#include <linux/miscdevice.h>
455fbdd3
PM
39#include <acpi/acpi_drivers.h>
40#include <asm/atomic.h>
41#include "lis3lv02d.h"
42
43#define DRIVER_NAME "lis3lv02d"
455fbdd3
PM
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
be84cfc5
PM
56struct acpi_lis3lv02d lis3_dev = {
57 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
ef2cfc79
PM
58};
59
be84cfc5 60EXPORT_SYMBOL_GPL(lis3_dev);
455fbdd3 61
455fbdd3
PM
62static int lis3lv02d_add_fs(struct acpi_device *device);
63
be84cfc5
PM
64static 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
455fbdd3
PM
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 */
81static 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 */
98static void lis3lv02d_get_xyz(acpi_handle handle, int *x, int *y, int *z)
99{
100 int position[3];
101
be84cfc5
PM
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);
455fbdd3 105
be84cfc5
PM
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);
455fbdd3
PM
109}
110
cfce41a6 111void lis3lv02d_poweroff(acpi_handle handle)
455fbdd3 112{
be84cfc5 113 lis3_dev.is_on = 0;
455fbdd3 114}
cfce41a6 115EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
455fbdd3 116
cfce41a6 117void lis3lv02d_poweron(acpi_handle handle)
455fbdd3 118{
be84cfc5
PM
119 lis3_dev.is_on = 1;
120 lis3_dev.init(handle);
455fbdd3 121}
cfce41a6 122EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
455fbdd3
PM
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 */
129static 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 */
144static 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
ef2cfc79
PM
153static 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 */
be84cfc5 160 atomic_inc(&lis3_dev.count);
ef2cfc79 161
be84cfc5
PM
162 wake_up_interruptible(&lis3_dev.misc_wait);
163 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
ef2cfc79
PM
164 return IRQ_HANDLED;
165}
166
167static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
168{
169 int ret;
170
be84cfc5 171 if (test_and_set_bit(0, &lis3_dev.misc_opened))
ef2cfc79
PM
172 return -EBUSY; /* already open */
173
be84cfc5 174 atomic_set(&lis3_dev.count, 0);
ef2cfc79
PM
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 */
be84cfc5
PM
187 ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
188 DRIVER_NAME, &lis3_dev);
ef2cfc79
PM
189
190 if (ret) {
be84cfc5
PM
191 clear_bit(0, &lis3_dev.misc_opened);
192 printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", lis3_dev.irq);
ef2cfc79
PM
193 return -EBUSY;
194 }
be84cfc5
PM
195 lis3lv02d_increase_use(&lis3_dev);
196 printk("lis3: registered interrupt %d\n", lis3_dev.irq);
ef2cfc79
PM
197 return 0;
198}
199
200static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
201{
be84cfc5
PM
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 */
ef2cfc79
PM
206 return 0;
207}
208
209static 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
be84cfc5 220 add_wait_queue(&lis3_dev.misc_wait, &wait);
ef2cfc79
PM
221 while (true) {
222 set_current_state(TASK_INTERRUPTIBLE);
be84cfc5 223 data = atomic_xchg(&lis3_dev.count, 0);
ef2cfc79
PM
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
251out:
252 __set_current_state(TASK_RUNNING);
be84cfc5 253 remove_wait_queue(&lis3_dev.misc_wait, &wait);
ef2cfc79
PM
254
255 return retval;
256}
257
258static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
259{
be84cfc5
PM
260 poll_wait(file, &lis3_dev.misc_wait, wait);
261 if (atomic_read(&lis3_dev.count))
ef2cfc79
PM
262 return POLLIN | POLLRDNORM;
263 return 0;
264}
265
266static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
267{
be84cfc5 268 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
ef2cfc79
PM
269}
270
271static 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
281static struct miscdevice lis3lv02d_misc_device = {
282 .minor = MISC_DYNAMIC_MINOR,
283 .name = "freefall",
284 .fops = &lis3lv02d_misc_fops,
285};
286
455fbdd3
PM
287/**
288 * lis3lv02d_joystick_kthread - Kthread polling function
289 * @data: unused - here to conform to threadfn prototype
290 */
291static int lis3lv02d_joystick_kthread(void *data)
292{
293 int x, y, z;
294
295 while (!kthread_should_stop()) {
be84cfc5
PM
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);
455fbdd3 300
be84cfc5 301 input_sync(lis3_dev.idev);
455fbdd3
PM
302
303 try_to_freeze();
304 msleep_interruptible(MDPS_POLL_INTERVAL);
305 }
306
307 return 0;
308}
309
310static int lis3lv02d_joystick_open(struct input_dev *input)
311{
be84cfc5
PM
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);
455fbdd3
PM
317 }
318
319 return 0;
320}
321
322static void lis3lv02d_joystick_close(struct input_dev *input)
323{
be84cfc5
PM
324 kthread_stop(lis3_dev.kthread);
325 lis3lv02d_decrease_use(&lis3_dev);
455fbdd3
PM
326}
327
455fbdd3
PM
328static inline void lis3lv02d_calibrate_joystick(void)
329{
be84cfc5 330 lis3lv02d_get_xyz(lis3_dev.device->handle, &lis3_dev.xcalib, &lis3_dev.ycalib, &lis3_dev.zcalib);
455fbdd3
PM
331}
332
cfce41a6 333int lis3lv02d_joystick_enable(void)
455fbdd3
PM
334{
335 int err;
336
be84cfc5 337 if (lis3_dev.idev)
455fbdd3
PM
338 return -EINVAL;
339
be84cfc5
PM
340 lis3_dev.idev = input_allocate_device();
341 if (!lis3_dev.idev)
455fbdd3
PM
342 return -ENOMEM;
343
344 lis3lv02d_calibrate_joystick();
345
be84cfc5
PM
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;
455fbdd3 353
be84cfc5
PM
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);
455fbdd3 358
be84cfc5 359 err = input_register_device(lis3_dev.idev);
455fbdd3 360 if (err) {
be84cfc5
PM
361 input_free_device(lis3_dev.idev);
362 lis3_dev.idev = NULL;
455fbdd3
PM
363 }
364
365 return err;
366}
cfce41a6 367EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
455fbdd3 368
cfce41a6 369void lis3lv02d_joystick_disable(void)
455fbdd3 370{
be84cfc5 371 if (!lis3_dev.idev)
455fbdd3
PM
372 return;
373
ef2cfc79 374 misc_deregister(&lis3lv02d_misc_device);
be84cfc5
PM
375 input_unregister_device(lis3_dev.idev);
376 lis3_dev.idev = NULL;
455fbdd3 377}
cfce41a6 378EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
455fbdd3
PM
379
380/*
381 * Initialise the accelerometer and the various subsystems.
382 * Should be rather independant of the bus system.
383 */
cfce41a6 384int lis3lv02d_init_device(struct acpi_lis3lv02d *dev)
455fbdd3
PM
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
ef2cfc79
PM
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");
405out:
455fbdd3
PM
406 lis3lv02d_decrease_use(dev);
407 return 0;
408}
cfce41a6 409EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
455fbdd3
PM
410
411/* Sysfs stuff */
412static ssize_t lis3lv02d_position_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
414{
415 int x, y, z;
416
be84cfc5
PM
417 lis3lv02d_increase_use(&lis3_dev);
418 lis3lv02d_get_xyz(lis3_dev.device->handle, &x, &y, &z);
419 lis3lv02d_decrease_use(&lis3_dev);
455fbdd3
PM
420 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
421}
422
423static ssize_t lis3lv02d_calibrate_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
425{
be84cfc5 426 return sprintf(buf, "(%d,%d,%d)\n", lis3_dev.xcalib, lis3_dev.ycalib, lis3_dev.zcalib);
455fbdd3
PM
427}
428
429static ssize_t lis3lv02d_calibrate_store(struct device *dev,
430 struct device_attribute *attr,
431 const char *buf, size_t count)
432{
be84cfc5 433 lis3lv02d_increase_use(&lis3_dev);
455fbdd3 434 lis3lv02d_calibrate_joystick();
be84cfc5 435 lis3lv02d_decrease_use(&lis3_dev);
455fbdd3
PM
436 return count;
437}
438
439/* conversion btw sampling rate and the register values */
440static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
441static ssize_t lis3lv02d_rate_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
443{
444 u8 ctrl;
445 int val;
446
be84cfc5
PM
447 lis3lv02d_increase_use(&lis3_dev);
448 lis3_dev.read(lis3_dev.device->handle, CTRL_REG1, &ctrl);
449 lis3lv02d_decrease_use(&lis3_dev);
455fbdd3
PM
450 val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
451 return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
452}
453
454static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
455static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
456 lis3lv02d_calibrate_store);
457static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
458
459static struct attribute *lis3lv02d_attributes[] = {
460 &dev_attr_position.attr,
461 &dev_attr_calibrate.attr,
462 &dev_attr_rate.attr,
463 NULL
464};
465
466static struct attribute_group lis3lv02d_attribute_group = {
467 .attrs = lis3lv02d_attributes
468};
469
cfce41a6 470
455fbdd3
PM
471static int lis3lv02d_add_fs(struct acpi_device *device)
472{
be84cfc5
PM
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);
455fbdd3 476
be84cfc5 477 return sysfs_create_group(&lis3_dev.pdev->dev.kobj, &lis3lv02d_attribute_group);
455fbdd3
PM
478}
479
cfce41a6 480int lis3lv02d_remove_fs(void)
455fbdd3 481{
be84cfc5
PM
482 sysfs_remove_group(&lis3_dev.pdev->dev.kobj, &lis3lv02d_attribute_group);
483 platform_device_unregister(lis3_dev.pdev);
455fbdd3
PM
484 return 0;
485}
cfce41a6 486EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
455fbdd3
PM
487
488MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
ef2cfc79 489MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
455fbdd3
PM
490MODULE_LICENSE("GPL");
491
This page took 0.107262 seconds and 5 git commands to generate.