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> |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 30 | #include <linux/input-polldev.h> |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 31 | #include <linux/delay.h> |
| 32 | #include <linux/wait.h> |
| 33 | #include <linux/poll.h> |
| 34 | #include <linux/freezer.h> |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 35 | #include <linux/uaccess.h> |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 36 | #include <linux/miscdevice.h> |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 37 | #include <asm/atomic.h> |
| 38 | #include "lis3lv02d.h" |
| 39 | |
| 40 | #define DRIVER_NAME "lis3lv02d" |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 41 | |
| 42 | /* joystick device poll interval in milliseconds */ |
| 43 | #define MDPS_POLL_INTERVAL 50 |
| 44 | /* |
| 45 | * The sensor can also generate interrupts (DRDY) but it's pretty pointless |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 46 | * because they are generated even if the data do not change. So it's better |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 47 | * to keep the interrupt for the free-fall event. The values are updated at |
| 48 | * 40Hz (at the lowest frequency), but as it can be pretty time consuming on |
| 49 | * some low processor, we poll the sensor only at 20Hz... enough for the |
| 50 | * joystick. |
| 51 | */ |
| 52 | |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 53 | #define LIS3_PWRON_DELAY_WAI_12B (5000) |
| 54 | #define LIS3_PWRON_DELAY_WAI_8B (3000) |
| 55 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 56 | struct lis3lv02d lis3_dev = { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 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 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 62 | static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg) |
| 63 | { |
| 64 | s8 lo; |
| 65 | if (lis3->read(lis3, reg, &lo) < 0) |
| 66 | return 0; |
| 67 | |
| 68 | return lo; |
| 69 | } |
| 70 | |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 71 | static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg) |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 72 | { |
| 73 | u8 lo, hi; |
| 74 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 75 | lis3->read(lis3, reg - 1, &lo); |
| 76 | lis3->read(lis3, reg, &hi); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 77 | /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */ |
| 78 | return (s16)((hi << 8) | lo); |
| 79 | } |
| 80 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 81 | /** |
| 82 | * lis3lv02d_get_axis - For the given axis, give the value converted |
| 83 | * @axis: 1,2,3 - can also be negative |
| 84 | * @hw_values: raw values returned by the hardware |
| 85 | * |
| 86 | * Returns the converted value. |
| 87 | */ |
| 88 | static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3]) |
| 89 | { |
| 90 | if (axis > 0) |
| 91 | return hw_values[axis - 1]; |
| 92 | else |
| 93 | return -hw_values[-axis - 1]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * 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] | 98 | * @lis3: pointer to the device struct |
| 99 | * @x: where to store the X axis value |
| 100 | * @y: where to store the Y axis value |
| 101 | * @z: where to store the Z axis value |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 102 | * |
| 103 | * Note that 40Hz input device can eat up about 10% CPU at 800MHZ |
| 104 | */ |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 105 | 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] | 106 | { |
| 107 | int position[3]; |
| 108 | |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 109 | mutex_lock(&lis3->mutex); |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 110 | position[0] = lis3->read_data(lis3, OUTX); |
| 111 | position[1] = lis3->read_data(lis3, OUTY); |
| 112 | position[2] = lis3->read_data(lis3, OUTZ); |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 113 | mutex_unlock(&lis3->mutex); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 114 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 115 | *x = lis3lv02d_get_axis(lis3->ac.x, position); |
| 116 | *y = lis3lv02d_get_axis(lis3->ac.y, position); |
| 117 | *z = lis3lv02d_get_axis(lis3->ac.z, position); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 120 | /* conversion btw sampling rate and the register values */ |
| 121 | static int lis3_12_rates[4] = {40, 160, 640, 2560}; |
| 122 | static int lis3_8_rates[2] = {100, 400}; |
| 123 | |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame^] | 124 | /* ODR is Output Data Rate */ |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 125 | static int lis3lv02d_get_odr(void) |
| 126 | { |
| 127 | u8 ctrl; |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame^] | 128 | int shift; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 129 | |
| 130 | lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl); |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame^] | 131 | ctrl &= lis3_dev.odr_mask; |
| 132 | shift = ffs(lis3_dev.odr_mask) - 1; |
| 133 | return lis3_dev.odrs[(ctrl >> shift)]; |
| 134 | } |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 135 | |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame^] | 136 | static int lis3lv02d_set_odr(int rate) |
| 137 | { |
| 138 | u8 ctrl; |
| 139 | int i, len, shift; |
| 140 | |
| 141 | lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl); |
| 142 | ctrl &= ~lis3_dev.odr_mask; |
| 143 | len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */ |
| 144 | shift = ffs(lis3_dev.odr_mask) - 1; |
| 145 | |
| 146 | for (i = 0; i < len; i++) |
| 147 | if (lis3_dev.odrs[i] == rate) { |
| 148 | lis3_dev.write(&lis3_dev, CTRL_REG1, |
| 149 | ctrl | (i << shift)); |
| 150 | return 0; |
| 151 | } |
| 152 | return -EINVAL; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 155 | static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3]) |
| 156 | { |
| 157 | u8 reg; |
| 158 | s16 x, y, z; |
| 159 | u8 selftest; |
| 160 | int ret; |
| 161 | |
| 162 | mutex_lock(&lis3->mutex); |
| 163 | if (lis3_dev.whoami == WAI_12B) |
| 164 | selftest = CTRL1_ST; |
| 165 | else |
| 166 | selftest = CTRL1_STP; |
| 167 | |
| 168 | lis3->read(lis3, CTRL_REG1, ®); |
| 169 | lis3->write(lis3, CTRL_REG1, (reg | selftest)); |
| 170 | msleep(lis3->pwron_delay / lis3lv02d_get_odr()); |
| 171 | |
| 172 | /* Read directly to avoid axis remap */ |
| 173 | x = lis3->read_data(lis3, OUTX); |
| 174 | y = lis3->read_data(lis3, OUTY); |
| 175 | z = lis3->read_data(lis3, OUTZ); |
| 176 | |
| 177 | /* back to normal settings */ |
| 178 | lis3->write(lis3, CTRL_REG1, reg); |
| 179 | msleep(lis3->pwron_delay / lis3lv02d_get_odr()); |
| 180 | |
| 181 | results[0] = x - lis3->read_data(lis3, OUTX); |
| 182 | results[1] = y - lis3->read_data(lis3, OUTY); |
| 183 | results[2] = z - lis3->read_data(lis3, OUTZ); |
| 184 | |
| 185 | ret = 0; |
| 186 | if (lis3->pdata) { |
| 187 | int i; |
| 188 | for (i = 0; i < 3; i++) { |
| 189 | /* Check against selftest acceptance limits */ |
| 190 | if ((results[i] < lis3->pdata->st_min_limits[i]) || |
| 191 | (results[i] > lis3->pdata->st_max_limits[i])) { |
| 192 | ret = -EIO; |
| 193 | goto fail; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* test passed */ |
| 199 | fail: |
| 200 | mutex_unlock(&lis3->mutex); |
| 201 | return ret; |
| 202 | } |
| 203 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 204 | void lis3lv02d_poweroff(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 205 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 206 | /* disable X,Y,Z axis and power down */ |
| 207 | lis3->write(lis3, CTRL_REG1, 0x00); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 208 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(lis3lv02d_poweroff); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 210 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 211 | void lis3lv02d_poweron(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 212 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 213 | u8 reg; |
| 214 | |
| 215 | lis3->init(lis3); |
| 216 | |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 217 | /* LIS3 power on delay is quite long */ |
| 218 | msleep(lis3->pwron_delay / lis3lv02d_get_odr()); |
| 219 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 220 | /* |
| 221 | * Common configuration |
Éric Piel | 4b5d95b | 2009-12-14 18:01:40 -0800 | [diff] [blame] | 222 | * BDU: (12 bits sensors only) LSB and MSB values are not updated until |
| 223 | * both have been read. So the value read will always be correct. |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 224 | */ |
Éric Piel | 4b5d95b | 2009-12-14 18:01:40 -0800 | [diff] [blame] | 225 | if (lis3->whoami == WAI_12B) { |
| 226 | lis3->read(lis3, CTRL_REG2, ®); |
| 227 | reg |= CTRL2_BDU; |
| 228 | lis3->write(lis3, CTRL_REG2, reg); |
| 229 | } |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 230 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 231 | EXPORT_SYMBOL_GPL(lis3lv02d_poweron); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 232 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 233 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 234 | static irqreturn_t lis302dl_interrupt(int irq, void *dummy) |
| 235 | { |
| 236 | /* |
| 237 | * Be careful: on some HP laptops the bios force DD when on battery and |
| 238 | * the lid is closed. This leads to interrupts as soon as a little move |
| 239 | * is done. |
| 240 | */ |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 241 | atomic_inc(&lis3_dev.count); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 242 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 243 | wake_up_interruptible(&lis3_dev.misc_wait); |
| 244 | kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 245 | return IRQ_HANDLED; |
| 246 | } |
| 247 | |
| 248 | static int lis3lv02d_misc_open(struct inode *inode, struct file *file) |
| 249 | { |
| 250 | int ret; |
| 251 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 252 | if (test_and_set_bit(0, &lis3_dev.misc_opened)) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 253 | return -EBUSY; /* already open */ |
| 254 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 255 | atomic_set(&lis3_dev.count, 0); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | * The sensor can generate interrupts for free-fall and direction |
| 259 | * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep |
| 260 | * the things simple and _fast_ we activate it only for free-fall, so |
| 261 | * no need to read register (very slow with ACPI). For the same reason, |
| 262 | * we forbid shared interrupts. |
| 263 | * |
| 264 | * IRQF_TRIGGER_RISING seems pointless on HP laptops because the |
| 265 | * io-apic is not configurable (and generates a warning) but I keep it |
| 266 | * in case of support for other hardware. |
| 267 | */ |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 268 | ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING, |
| 269 | DRIVER_NAME, &lis3_dev); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 270 | |
| 271 | if (ret) { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 272 | clear_bit(0, &lis3_dev.misc_opened); |
| 273 | 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] | 274 | return -EBUSY; |
| 275 | } |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int lis3lv02d_misc_release(struct inode *inode, struct file *file) |
| 280 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 281 | fasync_helper(-1, file, 0, &lis3_dev.async_queue); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 282 | free_irq(lis3_dev.irq, &lis3_dev); |
| 283 | clear_bit(0, &lis3_dev.misc_opened); /* release the device */ |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf, |
| 288 | size_t count, loff_t *pos) |
| 289 | { |
| 290 | DECLARE_WAITQUEUE(wait, current); |
| 291 | u32 data; |
| 292 | unsigned char byte_data; |
| 293 | ssize_t retval = 1; |
| 294 | |
| 295 | if (count < 1) |
| 296 | return -EINVAL; |
| 297 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 298 | add_wait_queue(&lis3_dev.misc_wait, &wait); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 299 | while (true) { |
| 300 | set_current_state(TASK_INTERRUPTIBLE); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 301 | data = atomic_xchg(&lis3_dev.count, 0); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 302 | if (data) |
| 303 | break; |
| 304 | |
| 305 | if (file->f_flags & O_NONBLOCK) { |
| 306 | retval = -EAGAIN; |
| 307 | goto out; |
| 308 | } |
| 309 | |
| 310 | if (signal_pending(current)) { |
| 311 | retval = -ERESTARTSYS; |
| 312 | goto out; |
| 313 | } |
| 314 | |
| 315 | schedule(); |
| 316 | } |
| 317 | |
| 318 | if (data < 255) |
| 319 | byte_data = data; |
| 320 | else |
| 321 | byte_data = 255; |
| 322 | |
| 323 | /* make sure we are not going into copy_to_user() with |
| 324 | * TASK_INTERRUPTIBLE state */ |
| 325 | set_current_state(TASK_RUNNING); |
| 326 | if (copy_to_user(buf, &byte_data, sizeof(byte_data))) |
| 327 | retval = -EFAULT; |
| 328 | |
| 329 | out: |
| 330 | __set_current_state(TASK_RUNNING); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 331 | remove_wait_queue(&lis3_dev.misc_wait, &wait); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 332 | |
| 333 | return retval; |
| 334 | } |
| 335 | |
| 336 | static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait) |
| 337 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 338 | poll_wait(file, &lis3_dev.misc_wait, wait); |
| 339 | if (atomic_read(&lis3_dev.count)) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 340 | return POLLIN | POLLRDNORM; |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int lis3lv02d_misc_fasync(int fd, struct file *file, int on) |
| 345 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 346 | return fasync_helper(fd, file, on, &lis3_dev.async_queue); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static const struct file_operations lis3lv02d_misc_fops = { |
| 350 | .owner = THIS_MODULE, |
| 351 | .llseek = no_llseek, |
| 352 | .read = lis3lv02d_misc_read, |
| 353 | .open = lis3lv02d_misc_open, |
| 354 | .release = lis3lv02d_misc_release, |
| 355 | .poll = lis3lv02d_misc_poll, |
| 356 | .fasync = lis3lv02d_misc_fasync, |
| 357 | }; |
| 358 | |
| 359 | static struct miscdevice lis3lv02d_misc_device = { |
| 360 | .minor = MISC_DYNAMIC_MINOR, |
| 361 | .name = "freefall", |
| 362 | .fops = &lis3lv02d_misc_fops, |
| 363 | }; |
| 364 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 365 | static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 366 | { |
| 367 | int x, y, z; |
| 368 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 369 | lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z); |
Samu Onkalo | 5399541 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 370 | input_report_abs(pidev->input, ABS_X, x); |
| 371 | input_report_abs(pidev->input, ABS_Y, y); |
| 372 | input_report_abs(pidev->input, ABS_Z, z); |
Samu Onkalo | d25a8c8 | 2009-12-14 18:01:38 -0800 | [diff] [blame] | 373 | input_sync(pidev->input); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 376 | int lis3lv02d_joystick_enable(void) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 377 | { |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 378 | struct input_dev *input_dev; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 379 | int err; |
| 380 | |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 381 | if (lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 382 | return -EINVAL; |
| 383 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 384 | lis3_dev.idev = input_allocate_polled_device(); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 385 | if (!lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 386 | return -ENOMEM; |
| 387 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 388 | lis3_dev.idev->poll = lis3lv02d_joystick_poll; |
| 389 | lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL; |
| 390 | input_dev = lis3_dev.idev->input; |
| 391 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 392 | input_dev->name = "ST LIS3LV02DL Accelerometer"; |
| 393 | input_dev->phys = DRIVER_NAME "/input0"; |
| 394 | input_dev->id.bustype = BUS_HOST; |
| 395 | input_dev->id.vendor = 0; |
| 396 | input_dev->dev.parent = &lis3_dev.pdev->dev; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 397 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 398 | set_bit(EV_ABS, input_dev->evbit); |
| 399 | input_set_abs_params(input_dev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3); |
| 400 | input_set_abs_params(input_dev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3); |
| 401 | input_set_abs_params(input_dev, 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] | 402 | |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 403 | err = input_register_polled_device(lis3_dev.idev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 404 | if (err) { |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 405 | input_free_polled_device(lis3_dev.idev); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 406 | lis3_dev.idev = NULL; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return err; |
| 410 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 411 | EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 412 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 413 | void lis3lv02d_joystick_disable(void) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 414 | { |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 415 | if (!lis3_dev.idev) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 416 | return; |
| 417 | |
Eric Piel | c288424 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 418 | if (lis3_dev.irq) |
| 419 | misc_deregister(&lis3lv02d_misc_device); |
Eric Piel | dc6ea97 | 2009-06-16 15:34:15 -0700 | [diff] [blame] | 420 | input_unregister_polled_device(lis3_dev.idev); |
Samu Onkalo | 66c8569 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 421 | input_free_polled_device(lis3_dev.idev); |
Pavel Machek | be84cfc | 2009-03-31 15:24:26 -0700 | [diff] [blame] | 422 | lis3_dev.idev = NULL; |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 423 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 424 | EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 425 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 426 | /* Sysfs stuff */ |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 427 | static ssize_t lis3lv02d_selftest_show(struct device *dev, |
| 428 | struct device_attribute *attr, char *buf) |
| 429 | { |
| 430 | int result; |
| 431 | s16 values[3]; |
| 432 | |
| 433 | result = lis3lv02d_selftest(&lis3_dev, values); |
| 434 | return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL", |
| 435 | values[0], values[1], values[2]); |
| 436 | } |
| 437 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 438 | static ssize_t lis3lv02d_position_show(struct device *dev, |
| 439 | struct device_attribute *attr, char *buf) |
| 440 | { |
| 441 | int x, y, z; |
| 442 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 443 | lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 444 | return sprintf(buf, "(%d,%d,%d)\n", x, y, z); |
| 445 | } |
| 446 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 447 | static ssize_t lis3lv02d_rate_show(struct device *dev, |
| 448 | struct device_attribute *attr, char *buf) |
| 449 | { |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 450 | return sprintf(buf, "%d\n", lis3lv02d_get_odr()); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame^] | 453 | static ssize_t lis3lv02d_rate_set(struct device *dev, |
| 454 | struct device_attribute *attr, const char *buf, |
| 455 | size_t count) |
| 456 | { |
| 457 | unsigned long rate; |
| 458 | |
| 459 | if (strict_strtoul(buf, 0, &rate)) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | if (lis3lv02d_set_odr(rate)) |
| 463 | return -EINVAL; |
| 464 | |
| 465 | return count; |
| 466 | } |
| 467 | |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 468 | static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 469 | static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL); |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame^] | 470 | static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show, |
| 471 | lis3lv02d_rate_set); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 472 | |
| 473 | static struct attribute *lis3lv02d_attributes[] = { |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 474 | &dev_attr_selftest.attr, |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 475 | &dev_attr_position.attr, |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 476 | &dev_attr_rate.attr, |
| 477 | NULL |
| 478 | }; |
| 479 | |
| 480 | static struct attribute_group lis3lv02d_attribute_group = { |
| 481 | .attrs = lis3lv02d_attributes |
| 482 | }; |
| 483 | |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 484 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 485 | static int lis3lv02d_add_fs(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 486 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 487 | lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); |
| 488 | if (IS_ERR(lis3->pdev)) |
| 489 | return PTR_ERR(lis3->pdev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 490 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 491 | return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 494 | int lis3lv02d_remove_fs(struct lis3lv02d *lis3) |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 495 | { |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 496 | sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group); |
| 497 | platform_device_unregister(lis3->pdev); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 498 | return 0; |
| 499 | } |
Eric Piel | cfce41a | 2009-01-09 16:41:01 -0800 | [diff] [blame] | 500 | EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 501 | |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 502 | /* |
| 503 | * Initialise the accelerometer and the various subsystems. |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 504 | * Should be rather independent of the bus system. |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 505 | */ |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 506 | int lis3lv02d_init_device(struct lis3lv02d *dev) |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 507 | { |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 508 | dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I); |
| 509 | |
| 510 | switch (dev->whoami) { |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 511 | case WAI_12B: |
| 512 | printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n"); |
| 513 | dev->read_data = lis3lv02d_read_12; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 514 | dev->mdps_max_val = 2048; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 515 | dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B; |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame^] | 516 | dev->odrs = lis3_12_rates; |
| 517 | dev->odr_mask = CTRL1_DF0 | CTRL1_DF1; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 518 | break; |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 519 | case WAI_8B: |
| 520 | printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n"); |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 521 | dev->read_data = lis3lv02d_read_8; |
| 522 | dev->mdps_max_val = 128; |
Samu Onkalo | 641615a | 2009-12-14 18:01:41 -0800 | [diff] [blame] | 523 | dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B; |
Samu Onkalo | a253aae | 2009-12-14 18:01:44 -0800 | [diff] [blame^] | 524 | dev->odrs = lis3_8_rates; |
| 525 | dev->odr_mask = CTRL1_DR; |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 526 | break; |
| 527 | default: |
| 528 | printk(KERN_ERR DRIVER_NAME |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 529 | ": unknown sensor type 0x%X\n", dev->whoami); |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 530 | return -EINVAL; |
| 531 | } |
| 532 | |
Samu Onkalo | 2db4a76 | 2009-12-14 18:01:43 -0800 | [diff] [blame] | 533 | mutex_init(&dev->mutex); |
| 534 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 535 | lis3lv02d_add_fs(dev); |
Eric Piel | a002ee8 | 2009-06-16 15:34:14 -0700 | [diff] [blame] | 536 | lis3lv02d_poweron(dev); |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 537 | |
| 538 | if (lis3lv02d_joystick_enable()) |
| 539 | printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n"); |
| 540 | |
Daniel Mack | 8f3128e | 2009-06-16 15:34:17 -0700 | [diff] [blame] | 541 | /* passing in platform specific data is purely optional and only |
| 542 | * used by the SPI transport layer at the moment */ |
| 543 | if (dev->pdata) { |
| 544 | struct lis3lv02d_platform_data *p = dev->pdata; |
| 545 | |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 546 | if (p->click_flags && (dev->whoami == WAI_8B)) { |
Daniel Mack | 8f3128e | 2009-06-16 15:34:17 -0700 | [diff] [blame] | 547 | dev->write(dev, CLICK_CFG, p->click_flags); |
| 548 | dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit); |
| 549 | dev->write(dev, CLICK_LATENCY, p->click_latency); |
| 550 | dev->write(dev, CLICK_WINDOW, p->click_window); |
| 551 | dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf); |
| 552 | dev->write(dev, CLICK_THSY_X, |
| 553 | (p->click_thresh_x & 0xf) | |
| 554 | (p->click_thresh_y << 4)); |
| 555 | } |
| 556 | |
Éric Piel | bc62c14 | 2009-12-14 18:01:39 -0800 | [diff] [blame] | 557 | if (p->wakeup_flags && (dev->whoami == WAI_8B)) { |
Daniel Mack | 8873c33 | 2009-09-21 17:04:43 -0700 | [diff] [blame] | 558 | dev->write(dev, FF_WU_CFG_1, p->wakeup_flags); |
| 559 | dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f); |
| 560 | /* default to 2.5ms for now */ |
| 561 | dev->write(dev, FF_WU_DURATION_1, 1); |
| 562 | /* enable high pass filter for both free-fall units */ |
| 563 | dev->write(dev, CTRL_REG2, HP_FF_WU1 | HP_FF_WU2); |
| 564 | } |
| 565 | |
Daniel Mack | 8f3128e | 2009-06-16 15:34:17 -0700 | [diff] [blame] | 566 | if (p->irq_cfg) |
| 567 | dev->write(dev, CTRL_REG3, p->irq_cfg); |
| 568 | } |
| 569 | |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 570 | /* bail if we did not get an IRQ from the bus layer */ |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 571 | if (!dev->irq) { |
| 572 | printk(KERN_ERR DRIVER_NAME |
Daniel Mack | a38da2e | 2009-03-31 15:24:32 -0700 | [diff] [blame] | 573 | ": No IRQ. Disabling /dev/freefall\n"); |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 574 | goto out; |
| 575 | } |
| 576 | |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 577 | if (misc_register(&lis3lv02d_misc_device)) |
| 578 | printk(KERN_ERR DRIVER_NAME ": misc_register failed\n"); |
| 579 | out: |
Daniel Mack | ab337a6 | 2009-03-31 15:24:31 -0700 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | EXPORT_SYMBOL_GPL(lis3lv02d_init_device); |
| 583 | |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 584 | MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver"); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 585 | MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek"); |
Pavel Machek | 455fbdd | 2008-11-12 13:27:02 -0800 | [diff] [blame] | 586 | MODULE_LICENSE("GPL"); |