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