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